xref: /qemu/linux-headers/linux/userfaultfd.h (revision 7271a819)
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_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 			union {
84 				__u32 ptid;
85 			} feat;
86 		} pagefault;
87 
88 		struct {
89 			__u32	ufd;
90 		} fork;
91 
92 		struct {
93 			__u64	from;
94 			__u64	to;
95 			__u64	len;
96 		} remap;
97 
98 		struct {
99 			__u64	start;
100 			__u64	end;
101 		} remove;
102 
103 		struct {
104 			/* unused reserved fields */
105 			__u64	reserved1;
106 			__u64	reserved2;
107 			__u64	reserved3;
108 		} reserved;
109 	} arg;
110 } __attribute__((packed));
111 
112 /*
113  * Start at 0x12 and not at 0 to be more strict against bugs.
114  */
115 #define UFFD_EVENT_PAGEFAULT	0x12
116 #define UFFD_EVENT_FORK		0x13
117 #define UFFD_EVENT_REMAP	0x14
118 #define UFFD_EVENT_REMOVE	0x15
119 #define UFFD_EVENT_UNMAP	0x16
120 
121 /* flags for UFFD_EVENT_PAGEFAULT */
122 #define UFFD_PAGEFAULT_FLAG_WRITE	(1<<0)	/* If this was a write fault */
123 #define UFFD_PAGEFAULT_FLAG_WP		(1<<1)	/* If reason is VM_UFFD_WP */
124 
125 struct uffdio_api {
126 	/* userland asks for an API number and the features to enable */
127 	__u64 api;
128 	/*
129 	 * Kernel answers below with the all available features for
130 	 * the API, this notifies userland of which events and/or
131 	 * which flags for each event are enabled in the current
132 	 * kernel.
133 	 *
134 	 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
135 	 * are to be considered implicitly always enabled in all kernels as
136 	 * long as the uffdio_api.api requested matches UFFD_API.
137 	 *
138 	 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
139 	 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
140 	 * hugetlbfs virtual memory ranges. Adding or not adding
141 	 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
142 	 * no real functional effect after UFFDIO_API returns, but
143 	 * it's only useful for an initial feature set probe at
144 	 * UFFDIO_API time. There are two ways to use it:
145 	 *
146 	 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
147 	 *    uffdio_api.features before calling UFFDIO_API, an error
148 	 *    will be returned by UFFDIO_API on a kernel without
149 	 *    hugetlbfs missing support
150 	 *
151 	 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
152 	 *    uffdio_api.features and instead it will be set by the
153 	 *    kernel in the uffdio_api.features if the kernel supports
154 	 *    it, so userland can later check if the feature flag is
155 	 *    present in uffdio_api.features after UFFDIO_API
156 	 *    succeeded.
157 	 *
158 	 * UFFD_FEATURE_MISSING_SHMEM works the same as
159 	 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
160 	 * (i.e. tmpfs and other shmem based APIs).
161 	 *
162 	 * UFFD_FEATURE_SIGBUS feature means no page-fault
163 	 * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead
164 	 * a SIGBUS signal will be sent to the faulting process.
165 	 *
166 	 * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will
167 	 * be returned, if feature is not requested 0 will be returned.
168 	 */
169 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
170 #define UFFD_FEATURE_EVENT_FORK			(1<<1)
171 #define UFFD_FEATURE_EVENT_REMAP		(1<<2)
172 #define UFFD_FEATURE_EVENT_REMOVE		(1<<3)
173 #define UFFD_FEATURE_MISSING_HUGETLBFS		(1<<4)
174 #define UFFD_FEATURE_MISSING_SHMEM		(1<<5)
175 #define UFFD_FEATURE_EVENT_UNMAP		(1<<6)
176 #define UFFD_FEATURE_SIGBUS			(1<<7)
177 #define UFFD_FEATURE_THREAD_ID			(1<<8)
178 	__u64 features;
179 
180 	__u64 ioctls;
181 };
182 
183 struct uffdio_range {
184 	__u64 start;
185 	__u64 len;
186 };
187 
188 struct uffdio_register {
189 	struct uffdio_range range;
190 #define UFFDIO_REGISTER_MODE_MISSING	((__u64)1<<0)
191 #define UFFDIO_REGISTER_MODE_WP		((__u64)1<<1)
192 	__u64 mode;
193 
194 	/*
195 	 * kernel answers which ioctl commands are available for the
196 	 * range, keep at the end as the last 8 bytes aren't read.
197 	 */
198 	__u64 ioctls;
199 };
200 
201 struct uffdio_copy {
202 	__u64 dst;
203 	__u64 src;
204 	__u64 len;
205 	/*
206 	 * There will be a wrprotection flag later that allows to map
207 	 * pages wrprotected on the fly. And such a flag will be
208 	 * available if the wrprotection ioctl are implemented for the
209 	 * range according to the uffdio_register.ioctls.
210 	 */
211 #define UFFDIO_COPY_MODE_DONTWAKE		((__u64)1<<0)
212 	__u64 mode;
213 
214 	/*
215 	 * "copy" is written by the ioctl and must be at the end: the
216 	 * copy_from_user will not read the last 8 bytes.
217 	 */
218 	__s64 copy;
219 };
220 
221 struct uffdio_zeropage {
222 	struct uffdio_range range;
223 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE		((__u64)1<<0)
224 	__u64 mode;
225 
226 	/*
227 	 * "zeropage" is written by the ioctl and must be at the end:
228 	 * the copy_from_user will not read the last 8 bytes.
229 	 */
230 	__s64 zeropage;
231 };
232 
233 #endif /* _LINUX_USERFAULTFD_H */
234