xref: /linux-6.15/include/uapi/linux/userfaultfd.h (revision 7677f7fd)
1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
2 /*
3  *  include/linux/userfaultfd.h
4  *
5  *  Copyright (C) 2007  Davide Libenzi <[email protected]>
6  *  Copyright (C) 2015  Red Hat, Inc.
7  *
8  */
9 
10 #ifndef _LINUX_USERFAULTFD_H
11 #define _LINUX_USERFAULTFD_H
12 
13 #include <linux/types.h>
14 
15 /*
16  * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and
17  * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR.  In
18  * userfaultfd.h we assumed the kernel was reading (instead _IOC_READ
19  * means the userland is reading).
20  */
21 #define UFFD_API ((__u64)0xAA)
22 #define UFFD_API_REGISTER_MODES (UFFDIO_REGISTER_MODE_MISSING |	\
23 				 UFFDIO_REGISTER_MODE_WP |	\
24 				 UFFDIO_REGISTER_MODE_MINOR)
25 #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP |	\
26 			   UFFD_FEATURE_EVENT_FORK |		\
27 			   UFFD_FEATURE_EVENT_REMAP |		\
28 			   UFFD_FEATURE_EVENT_REMOVE |		\
29 			   UFFD_FEATURE_EVENT_UNMAP |		\
30 			   UFFD_FEATURE_MISSING_HUGETLBFS |	\
31 			   UFFD_FEATURE_MISSING_SHMEM |		\
32 			   UFFD_FEATURE_SIGBUS |		\
33 			   UFFD_FEATURE_THREAD_ID |		\
34 			   UFFD_FEATURE_MINOR_HUGETLBFS)
35 #define UFFD_API_IOCTLS				\
36 	((__u64)1 << _UFFDIO_REGISTER |		\
37 	 (__u64)1 << _UFFDIO_UNREGISTER |	\
38 	 (__u64)1 << _UFFDIO_API)
39 #define UFFD_API_RANGE_IOCTLS			\
40 	((__u64)1 << _UFFDIO_WAKE |		\
41 	 (__u64)1 << _UFFDIO_COPY |		\
42 	 (__u64)1 << _UFFDIO_ZEROPAGE |		\
43 	 (__u64)1 << _UFFDIO_WRITEPROTECT)
44 #define UFFD_API_RANGE_IOCTLS_BASIC		\
45 	((__u64)1 << _UFFDIO_WAKE |		\
46 	 (__u64)1 << _UFFDIO_COPY)
47 
48 /*
49  * Valid ioctl command number range with this API is from 0x00 to
50  * 0x3F.  UFFDIO_API is the fixed number, everything else can be
51  * changed by implementing a different UFFD_API. If sticking to the
52  * same UFFD_API more ioctl can be added and userland will be aware of
53  * which ioctl the running kernel implements through the ioctl command
54  * bitmask written by the UFFDIO_API.
55  */
56 #define _UFFDIO_REGISTER		(0x00)
57 #define _UFFDIO_UNREGISTER		(0x01)
58 #define _UFFDIO_WAKE			(0x02)
59 #define _UFFDIO_COPY			(0x03)
60 #define _UFFDIO_ZEROPAGE		(0x04)
61 #define _UFFDIO_WRITEPROTECT		(0x06)
62 #define _UFFDIO_API			(0x3F)
63 
64 /* userfaultfd ioctl ids */
65 #define UFFDIO 0xAA
66 #define UFFDIO_API		_IOWR(UFFDIO, _UFFDIO_API,	\
67 				      struct uffdio_api)
68 #define UFFDIO_REGISTER		_IOWR(UFFDIO, _UFFDIO_REGISTER, \
69 				      struct uffdio_register)
70 #define UFFDIO_UNREGISTER	_IOR(UFFDIO, _UFFDIO_UNREGISTER,	\
71 				     struct uffdio_range)
72 #define UFFDIO_WAKE		_IOR(UFFDIO, _UFFDIO_WAKE,	\
73 				     struct uffdio_range)
74 #define UFFDIO_COPY		_IOWR(UFFDIO, _UFFDIO_COPY,	\
75 				      struct uffdio_copy)
76 #define UFFDIO_ZEROPAGE		_IOWR(UFFDIO, _UFFDIO_ZEROPAGE,	\
77 				      struct uffdio_zeropage)
78 #define UFFDIO_WRITEPROTECT	_IOWR(UFFDIO, _UFFDIO_WRITEPROTECT, \
79 				      struct uffdio_writeprotect)
80 
81 /* read() structure */
82 struct uffd_msg {
83 	__u8	event;
84 
85 	__u8	reserved1;
86 	__u16	reserved2;
87 	__u32	reserved3;
88 
89 	union {
90 		struct {
91 			__u64	flags;
92 			__u64	address;
93 			union {
94 				__u32 ptid;
95 			} feat;
96 		} pagefault;
97 
98 		struct {
99 			__u32	ufd;
100 		} fork;
101 
102 		struct {
103 			__u64	from;
104 			__u64	to;
105 			__u64	len;
106 		} remap;
107 
108 		struct {
109 			__u64	start;
110 			__u64	end;
111 		} remove;
112 
113 		struct {
114 			/* unused reserved fields */
115 			__u64	reserved1;
116 			__u64	reserved2;
117 			__u64	reserved3;
118 		} reserved;
119 	} arg;
120 } __packed;
121 
122 /*
123  * Start at 0x12 and not at 0 to be more strict against bugs.
124  */
125 #define UFFD_EVENT_PAGEFAULT	0x12
126 #define UFFD_EVENT_FORK		0x13
127 #define UFFD_EVENT_REMAP	0x14
128 #define UFFD_EVENT_REMOVE	0x15
129 #define UFFD_EVENT_UNMAP	0x16
130 
131 /* flags for UFFD_EVENT_PAGEFAULT */
132 #define UFFD_PAGEFAULT_FLAG_WRITE	(1<<0)	/* If this was a write fault */
133 #define UFFD_PAGEFAULT_FLAG_WP		(1<<1)	/* If reason is VM_UFFD_WP */
134 #define UFFD_PAGEFAULT_FLAG_MINOR	(1<<2)	/* If reason is VM_UFFD_MINOR */
135 
136 struct uffdio_api {
137 	/* userland asks for an API number and the features to enable */
138 	__u64 api;
139 	/*
140 	 * Kernel answers below with the all available features for
141 	 * the API, this notifies userland of which events and/or
142 	 * which flags for each event are enabled in the current
143 	 * kernel.
144 	 *
145 	 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
146 	 * are to be considered implicitly always enabled in all kernels as
147 	 * long as the uffdio_api.api requested matches UFFD_API.
148 	 *
149 	 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
150 	 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
151 	 * hugetlbfs virtual memory ranges. Adding or not adding
152 	 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
153 	 * no real functional effect after UFFDIO_API returns, but
154 	 * it's only useful for an initial feature set probe at
155 	 * UFFDIO_API time. There are two ways to use it:
156 	 *
157 	 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
158 	 *    uffdio_api.features before calling UFFDIO_API, an error
159 	 *    will be returned by UFFDIO_API on a kernel without
160 	 *    hugetlbfs missing support
161 	 *
162 	 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
163 	 *    uffdio_api.features and instead it will be set by the
164 	 *    kernel in the uffdio_api.features if the kernel supports
165 	 *    it, so userland can later check if the feature flag is
166 	 *    present in uffdio_api.features after UFFDIO_API
167 	 *    succeeded.
168 	 *
169 	 * UFFD_FEATURE_MISSING_SHMEM works the same as
170 	 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
171 	 * (i.e. tmpfs and other shmem based APIs).
172 	 *
173 	 * UFFD_FEATURE_SIGBUS feature means no page-fault
174 	 * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead
175 	 * a SIGBUS signal will be sent to the faulting process.
176 	 *
177 	 * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will
178 	 * be returned, if feature is not requested 0 will be returned.
179 	 *
180 	 * UFFD_FEATURE_MINOR_HUGETLBFS indicates that minor faults
181 	 * can be intercepted (via REGISTER_MODE_MINOR) for
182 	 * hugetlbfs-backed pages.
183 	 */
184 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
185 #define UFFD_FEATURE_EVENT_FORK			(1<<1)
186 #define UFFD_FEATURE_EVENT_REMAP		(1<<2)
187 #define UFFD_FEATURE_EVENT_REMOVE		(1<<3)
188 #define UFFD_FEATURE_MISSING_HUGETLBFS		(1<<4)
189 #define UFFD_FEATURE_MISSING_SHMEM		(1<<5)
190 #define UFFD_FEATURE_EVENT_UNMAP		(1<<6)
191 #define UFFD_FEATURE_SIGBUS			(1<<7)
192 #define UFFD_FEATURE_THREAD_ID			(1<<8)
193 #define UFFD_FEATURE_MINOR_HUGETLBFS		(1<<9)
194 	__u64 features;
195 
196 	__u64 ioctls;
197 };
198 
199 struct uffdio_range {
200 	__u64 start;
201 	__u64 len;
202 };
203 
204 struct uffdio_register {
205 	struct uffdio_range range;
206 #define UFFDIO_REGISTER_MODE_MISSING	((__u64)1<<0)
207 #define UFFDIO_REGISTER_MODE_WP		((__u64)1<<1)
208 #define UFFDIO_REGISTER_MODE_MINOR	((__u64)1<<2)
209 	__u64 mode;
210 
211 	/*
212 	 * kernel answers which ioctl commands are available for the
213 	 * range, keep at the end as the last 8 bytes aren't read.
214 	 */
215 	__u64 ioctls;
216 };
217 
218 struct uffdio_copy {
219 	__u64 dst;
220 	__u64 src;
221 	__u64 len;
222 #define UFFDIO_COPY_MODE_DONTWAKE		((__u64)1<<0)
223 	/*
224 	 * UFFDIO_COPY_MODE_WP will map the page write protected on
225 	 * the fly.  UFFDIO_COPY_MODE_WP is available only if the
226 	 * write protected ioctl is implemented for the range
227 	 * according to the uffdio_register.ioctls.
228 	 */
229 #define UFFDIO_COPY_MODE_WP			((__u64)1<<1)
230 	__u64 mode;
231 
232 	/*
233 	 * "copy" is written by the ioctl and must be at the end: the
234 	 * copy_from_user will not read the last 8 bytes.
235 	 */
236 	__s64 copy;
237 };
238 
239 struct uffdio_zeropage {
240 	struct uffdio_range range;
241 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE		((__u64)1<<0)
242 	__u64 mode;
243 
244 	/*
245 	 * "zeropage" is written by the ioctl and must be at the end:
246 	 * the copy_from_user will not read the last 8 bytes.
247 	 */
248 	__s64 zeropage;
249 };
250 
251 struct uffdio_writeprotect {
252 	struct uffdio_range range;
253 /*
254  * UFFDIO_WRITEPROTECT_MODE_WP: set the flag to write protect a range,
255  * unset the flag to undo protection of a range which was previously
256  * write protected.
257  *
258  * UFFDIO_WRITEPROTECT_MODE_DONTWAKE: set the flag to avoid waking up
259  * any wait thread after the operation succeeds.
260  *
261  * NOTE: Write protecting a region (WP=1) is unrelated to page faults,
262  * therefore DONTWAKE flag is meaningless with WP=1.  Removing write
263  * protection (WP=0) in response to a page fault wakes the faulting
264  * task unless DONTWAKE is set.
265  */
266 #define UFFDIO_WRITEPROTECT_MODE_WP		((__u64)1<<0)
267 #define UFFDIO_WRITEPROTECT_MODE_DONTWAKE	((__u64)1<<1)
268 	__u64 mode;
269 };
270 
271 /*
272  * Flags for the userfaultfd(2) system call itself.
273  */
274 
275 /*
276  * Create a userfaultfd that can handle page faults only in user mode.
277  */
278 #define UFFD_USER_MODE_ONLY 1
279 
280 #endif /* _LINUX_USERFAULTFD_H */
281