1 /*
2  *  include/linux/userfaultfd.h
3  *
4  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
5  *  Copyright (C) 2015  Red Hat, Inc.
6  *
7  */
8 
9 #ifndef _LINUX_USERFAULTFD_H
10 #define _LINUX_USERFAULTFD_H
11 
12 #include <linux/types.h>
13 
14 /*
15  * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and
16  * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR.  In
17  * userfaultfd.h we assumed the kernel was reading (instead _IOC_READ
18  * means the userland is reading).
19  */
20 #define UFFD_API ((__u64)0xAA)
21 #define UFFD_API_FEATURES (UFFD_FEATURE_EVENT_FORK |		\
22 			   UFFD_FEATURE_EVENT_REMAP |		\
23 			   UFFD_FEATURE_EVENT_MADVDONTNEED |	\
24 			   UFFD_FEATURE_MISSING_HUGETLBFS |	\
25 			   UFFD_FEATURE_MISSING_SHMEM)
26 #define UFFD_API_IOCTLS				\
27 	((__u64)1 << _UFFDIO_REGISTER |		\
28 	 (__u64)1 << _UFFDIO_UNREGISTER |	\
29 	 (__u64)1 << _UFFDIO_API)
30 #define UFFD_API_RANGE_IOCTLS			\
31 	((__u64)1 << _UFFDIO_WAKE |		\
32 	 (__u64)1 << _UFFDIO_COPY |		\
33 	 (__u64)1 << _UFFDIO_ZEROPAGE)
34 #define UFFD_API_RANGE_IOCTLS_BASIC		\
35 	((__u64)1 << _UFFDIO_WAKE |		\
36 	 (__u64)1 << _UFFDIO_COPY)
37 
38 /*
39  * Valid ioctl command number range with this API is from 0x00 to
40  * 0x3F.  UFFDIO_API is the fixed number, everything else can be
41  * changed by implementing a different UFFD_API. If sticking to the
42  * same UFFD_API more ioctl can be added and userland will be aware of
43  * which ioctl the running kernel implements through the ioctl command
44  * bitmask written by the UFFDIO_API.
45  */
46 #define _UFFDIO_REGISTER		(0x00)
47 #define _UFFDIO_UNREGISTER		(0x01)
48 #define _UFFDIO_WAKE			(0x02)
49 #define _UFFDIO_COPY			(0x03)
50 #define _UFFDIO_ZEROPAGE		(0x04)
51 #define _UFFDIO_API			(0x3F)
52 
53 /* userfaultfd ioctl ids */
54 #define UFFDIO 0xAA
55 #define UFFDIO_API		_IOWR(UFFDIO, _UFFDIO_API,	\
56 				      struct uffdio_api)
57 #define UFFDIO_REGISTER		_IOWR(UFFDIO, _UFFDIO_REGISTER, \
58 				      struct uffdio_register)
59 #define UFFDIO_UNREGISTER	_IOR(UFFDIO, _UFFDIO_UNREGISTER,	\
60 				     struct uffdio_range)
61 #define UFFDIO_WAKE		_IOR(UFFDIO, _UFFDIO_WAKE,	\
62 				     struct uffdio_range)
63 #define UFFDIO_COPY		_IOWR(UFFDIO, _UFFDIO_COPY,	\
64 				      struct uffdio_copy)
65 #define UFFDIO_ZEROPAGE		_IOWR(UFFDIO, _UFFDIO_ZEROPAGE,	\
66 				      struct uffdio_zeropage)
67 
68 /* read() structure */
69 struct uffd_msg {
70 	__u8	event;
71 
72 	__u8	reserved1;
73 	__u16	reserved2;
74 	__u32	reserved3;
75 
76 	union {
77 		struct {
78 			__u64	flags;
79 			__u64	address;
80 		} pagefault;
81 
82 		struct {
83 			__u32	ufd;
84 		} fork;
85 
86 		struct {
87 			__u64	from;
88 			__u64	to;
89 			__u64	len;
90 		} remap;
91 
92 		struct {
93 			__u64	start;
94 			__u64	end;
95 		} madv_dn;
96 
97 		struct {
98 			/* unused reserved fields */
99 			__u64	reserved1;
100 			__u64	reserved2;
101 			__u64	reserved3;
102 		} reserved;
103 	} arg;
104 } __attribute__((packed));
105 
106 /*
107  * Start at 0x12 and not at 0 to be more strict against bugs.
108  */
109 #define UFFD_EVENT_PAGEFAULT	0x12
110 #define UFFD_EVENT_FORK		0x13
111 #define UFFD_EVENT_REMAP	0x14
112 #define UFFD_EVENT_MADVDONTNEED	0x15
113 
114 /* flags for UFFD_EVENT_PAGEFAULT */
115 #define UFFD_PAGEFAULT_FLAG_WRITE	(1<<0)	/* If this was a write fault */
116 #define UFFD_PAGEFAULT_FLAG_WP		(1<<1)	/* If reason is VM_UFFD_WP */
117 
118 struct uffdio_api {
119 	/* userland asks for an API number and the features to enable */
120 	__u64 api;
121 	/*
122 	 * Kernel answers below with the all available features for
123 	 * the API, this notifies userland of which events and/or
124 	 * which flags for each event are enabled in the current
125 	 * kernel.
126 	 *
127 	 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
128 	 * are to be considered implicitly always enabled in all kernels as
129 	 * long as the uffdio_api.api requested matches UFFD_API.
130 	 *
131 	 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
132 	 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
133 	 * hugetlbfs virtual memory ranges. Adding or not adding
134 	 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
135 	 * no real functional effect after UFFDIO_API returns, but
136 	 * it's only useful for an initial feature set probe at
137 	 * UFFDIO_API time. There are two ways to use it:
138 	 *
139 	 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
140 	 *    uffdio_api.features before calling UFFDIO_API, an error
141 	 *    will be returned by UFFDIO_API on a kernel without
142 	 *    hugetlbfs missing support
143 	 *
144 	 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
145 	 *    uffdio_api.features and instead it will be set by the
146 	 *    kernel in the uffdio_api.features if the kernel supports
147 	 *    it, so userland can later check if the feature flag is
148 	 *    present in uffdio_api.features after UFFDIO_API
149 	 *    succeeded.
150 	 *
151 	 * UFFD_FEATURE_MISSING_SHMEM works the same as
152 	 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
153 	 * (i.e. tmpfs and other shmem based APIs).
154 	 */
155 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
156 #define UFFD_FEATURE_EVENT_FORK			(1<<1)
157 #define UFFD_FEATURE_EVENT_REMAP		(1<<2)
158 #define UFFD_FEATURE_EVENT_MADVDONTNEED		(1<<3)
159 #define UFFD_FEATURE_MISSING_HUGETLBFS		(1<<4)
160 #define UFFD_FEATURE_MISSING_SHMEM		(1<<5)
161 	__u64 features;
162 
163 	__u64 ioctls;
164 };
165 
166 struct uffdio_range {
167 	__u64 start;
168 	__u64 len;
169 };
170 
171 struct uffdio_register {
172 	struct uffdio_range range;
173 #define UFFDIO_REGISTER_MODE_MISSING	((__u64)1<<0)
174 #define UFFDIO_REGISTER_MODE_WP		((__u64)1<<1)
175 	__u64 mode;
176 
177 	/*
178 	 * kernel answers which ioctl commands are available for the
179 	 * range, keep at the end as the last 8 bytes aren't read.
180 	 */
181 	__u64 ioctls;
182 };
183 
184 struct uffdio_copy {
185 	__u64 dst;
186 	__u64 src;
187 	__u64 len;
188 	/*
189 	 * There will be a wrprotection flag later that allows to map
190 	 * pages wrprotected on the fly. And such a flag will be
191 	 * available if the wrprotection ioctl are implemented for the
192 	 * range according to the uffdio_register.ioctls.
193 	 */
194 #define UFFDIO_COPY_MODE_DONTWAKE		((__u64)1<<0)
195 	__u64 mode;
196 
197 	/*
198 	 * "copy" is written by the ioctl and must be at the end: the
199 	 * copy_from_user will not read the last 8 bytes.
200 	 */
201 	__s64 copy;
202 };
203 
204 struct uffdio_zeropage {
205 	struct uffdio_range range;
206 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE		((__u64)1<<0)
207 	__u64 mode;
208 
209 	/*
210 	 * "zeropage" is written by the ioctl and must be at the end:
211 	 * the copy_from_user will not read the last 8 bytes.
212 	 */
213 	__s64 zeropage;
214 };
215 
216 #endif /* _LINUX_USERFAULTFD_H */
217