summaryrefslogtreecommitdiff
path: root/userfaultfd.c
blob: 7a60af192042f6f9facc564a03a22a1d707a4a7a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2015 Dmitry V. Levin <ldv@altlinux.org>
 * Copyright (c) 2015-2018 The strace developers.
 * All rights reserved.
 *
 * SPDX-License-Identifier: LGPL-2.1-or-later
 */

#include "defs.h"
#include "print_fields.h"
#include <fcntl.h>

#include "xlat/uffd_flags.h"

SYS_FUNC(userfaultfd)
{
	printflags(uffd_flags, tcp->u_arg[0], "UFFD_???");

	return RVAL_DECODED | RVAL_FD;
}

#ifdef HAVE_LINUX_USERFAULTFD_H
# include <linux/ioctl.h>
# include <linux/userfaultfd.h>

# include "xlat/uffd_api_features.h"
# include "xlat/uffd_api_flags.h"
# include "xlat/uffd_copy_flags.h"
# include "xlat/uffd_register_ioctl_flags.h"
# include "xlat/uffd_register_mode_flags.h"
# include "xlat/uffd_zeropage_flags.h"

static void
tprintf_uffdio_range(const struct uffdio_range *range)
{
	PRINT_FIELD_X("{", *range, start);
	PRINT_FIELD_X(", ", *range, len);
	tprints("}");
}

#define PRINT_FIELD_UFFDIO_RANGE(prefix_, where_, field_)		\
	do {								\
		STRACE_PRINTF("%s%s=", (prefix_), #field_);		\
		tprintf_uffdio_range(&(where_).field_);			\
	} while (0)

int
uffdio_ioctl(struct tcb *const tcp, const unsigned int code,
	     const kernel_ulong_t arg)
{
	switch (code) {
	case UFFDIO_API: {
		uint64_t *entering_features;
		struct uffdio_api ua;

		if (entering(tcp)) {
			tprints(", ");
			if (umove_or_printaddr(tcp, arg, &ua))
				break;
			PRINT_FIELD_X("{", ua, api);
			PRINT_FIELD_FLAGS(", ", ua, features, uffd_api_features,
					  "UFFD_FEATURE_???");
			entering_features = malloc(sizeof(*entering_features));
			if (entering_features) {
				*entering_features = ua.features;
				set_tcb_priv_data(tcp, entering_features, free);
			}

			return 0;
		}

		if (!syserror(tcp) && !umove(tcp, arg, &ua)) {
			entering_features = get_tcb_priv_data(tcp);

			if (!entering_features
			    || *entering_features != ua.features) {
				PRINT_FIELD_FLAGS(" => ", ua, features,
						  uffd_api_features,
						  "UFFD_FEATURE_???");
			}

			PRINT_FIELD_FLAGS(", ", ua, ioctls, uffd_api_flags,
					  "_UFFDIO_???");
		}

		tprints("}");

		break;
	}

	case UFFDIO_COPY: {
		struct uffdio_copy uc;

		if (entering(tcp)) {
			tprints(", ");
			if (umove_or_printaddr(tcp, arg, &uc))
				return RVAL_IOCTL_DECODED;
			PRINT_FIELD_X("{", uc, dst);
			PRINT_FIELD_X(", ", uc, src);
			PRINT_FIELD_X(", ", uc, len);
			PRINT_FIELD_FLAGS(", ", uc, mode, uffd_copy_flags,
					  "UFFDIO_COPY_???");

			return 0;
		}

		if (!syserror(tcp) && !umove(tcp, arg, &uc))
			PRINT_FIELD_X(", ", uc, copy);

		tprints("}");

		break;
	}

	case UFFDIO_REGISTER: {
		struct uffdio_register ur;

		if (entering(tcp)) {
			tprints(", ");
			if (umove_or_printaddr(tcp, arg, &ur))
				return RVAL_IOCTL_DECODED;
			PRINT_FIELD_UFFDIO_RANGE("{", ur, range);
			PRINT_FIELD_FLAGS(", ", ur, mode,
					  uffd_register_mode_flags,
					  "UFFDIO_REGISTER_MODE_???");

			return 0;
		}

		if (!syserror(tcp) && !umove(tcp, arg, &ur)) {
			PRINT_FIELD_FLAGS(", ", ur, ioctls,
					  uffd_register_ioctl_flags,
					  "UFFDIO_???");
		}

		tprints("}");

		break;
	}

	case UFFDIO_UNREGISTER:
	case UFFDIO_WAKE: {
		struct uffdio_range ura;

		tprints(", ");

		if (!umove_or_printaddr(tcp, arg, &ura))
			tprintf_uffdio_range(&ura);

		break;
	}

	case UFFDIO_ZEROPAGE: {
		struct uffdio_zeropage uz;

		if (entering(tcp)) {
			tprints(", ");
			if (umove_or_printaddr(tcp, arg, &uz))
				return RVAL_IOCTL_DECODED;
			PRINT_FIELD_UFFDIO_RANGE("{", uz, range);
			PRINT_FIELD_FLAGS(", ", uz, mode, uffd_zeropage_flags,
					  "UFFDIO_ZEROPAGE_???");

			return 0;
		}

		if (!syserror(tcp) && !umove(tcp, arg, &uz))
			PRINT_FIELD_X(", ", uz, zeropage);

		tprints("}");

		break;
	}

	default:
		return RVAL_DECODED;
	}

	return RVAL_IOCTL_DECODED;
}
#endif /* HAVE_LINUX_USERFAULTFD_H */