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