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