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 /*
153a5eb5b4SPaolo Bonzini  * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and
163a5eb5b4SPaolo Bonzini  * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR.  In
173a5eb5b4SPaolo Bonzini  * userfaultfd.h we assumed the kernel was reading (instead _IOC_READ
183a5eb5b4SPaolo Bonzini  * means the userland is reading).
19c1fcf220SDr. David Alan Gilbert  */
203a5eb5b4SPaolo Bonzini #define UFFD_API ((__u64)0xAA)
213a5eb5b4SPaolo Bonzini #define UFFD_API_FEATURES (UFFD_FEATURE_EVENT_FORK |		\
223a5eb5b4SPaolo Bonzini 			   UFFD_FEATURE_EVENT_REMAP |		\
23*74c98e20SCornelia Huck 			   UFFD_FEATURE_EVENT_REMOVE |	\
24*74c98e20SCornelia Huck 			   UFFD_FEATURE_EVENT_UNMAP |		\
253a5eb5b4SPaolo Bonzini 			   UFFD_FEATURE_MISSING_HUGETLBFS |	\
263a5eb5b4SPaolo Bonzini 			   UFFD_FEATURE_MISSING_SHMEM)
27c1fcf220SDr. David Alan Gilbert #define UFFD_API_IOCTLS				\
28c1fcf220SDr. David Alan Gilbert 	((__u64)1 << _UFFDIO_REGISTER |		\
29c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_UNREGISTER |	\
30c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_API)
31c1fcf220SDr. David Alan Gilbert #define UFFD_API_RANGE_IOCTLS			\
32c1fcf220SDr. David Alan Gilbert 	((__u64)1 << _UFFDIO_WAKE |		\
33c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_COPY |		\
34c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_ZEROPAGE)
353a5eb5b4SPaolo Bonzini #define UFFD_API_RANGE_IOCTLS_BASIC		\
363a5eb5b4SPaolo Bonzini 	((__u64)1 << _UFFDIO_WAKE |		\
373a5eb5b4SPaolo Bonzini 	 (__u64)1 << _UFFDIO_COPY)
38c1fcf220SDr. David Alan Gilbert 
39c1fcf220SDr. David Alan Gilbert /*
40c1fcf220SDr. David Alan Gilbert  * Valid ioctl command number range with this API is from 0x00 to
41c1fcf220SDr. David Alan Gilbert  * 0x3F.  UFFDIO_API is the fixed number, everything else can be
42c1fcf220SDr. David Alan Gilbert  * changed by implementing a different UFFD_API. If sticking to the
43c1fcf220SDr. David Alan Gilbert  * same UFFD_API more ioctl can be added and userland will be aware of
44c1fcf220SDr. David Alan Gilbert  * which ioctl the running kernel implements through the ioctl command
45c1fcf220SDr. David Alan Gilbert  * bitmask written by the UFFDIO_API.
46c1fcf220SDr. David Alan Gilbert  */
47c1fcf220SDr. David Alan Gilbert #define _UFFDIO_REGISTER		(0x00)
48c1fcf220SDr. David Alan Gilbert #define _UFFDIO_UNREGISTER		(0x01)
49c1fcf220SDr. David Alan Gilbert #define _UFFDIO_WAKE			(0x02)
50c1fcf220SDr. David Alan Gilbert #define _UFFDIO_COPY			(0x03)
51c1fcf220SDr. David Alan Gilbert #define _UFFDIO_ZEROPAGE		(0x04)
52c1fcf220SDr. David Alan Gilbert #define _UFFDIO_API			(0x3F)
53c1fcf220SDr. David Alan Gilbert 
54c1fcf220SDr. David Alan Gilbert /* userfaultfd ioctl ids */
55c1fcf220SDr. David Alan Gilbert #define UFFDIO 0xAA
56c1fcf220SDr. David Alan Gilbert #define UFFDIO_API		_IOWR(UFFDIO, _UFFDIO_API,	\
57c1fcf220SDr. David Alan Gilbert 				      struct uffdio_api)
58c1fcf220SDr. David Alan Gilbert #define UFFDIO_REGISTER		_IOWR(UFFDIO, _UFFDIO_REGISTER, \
59c1fcf220SDr. David Alan Gilbert 				      struct uffdio_register)
60c1fcf220SDr. David Alan Gilbert #define UFFDIO_UNREGISTER	_IOR(UFFDIO, _UFFDIO_UNREGISTER,	\
61c1fcf220SDr. David Alan Gilbert 				     struct uffdio_range)
62c1fcf220SDr. David Alan Gilbert #define UFFDIO_WAKE		_IOR(UFFDIO, _UFFDIO_WAKE,	\
63c1fcf220SDr. David Alan Gilbert 				     struct uffdio_range)
64c1fcf220SDr. David Alan Gilbert #define UFFDIO_COPY		_IOWR(UFFDIO, _UFFDIO_COPY,	\
65c1fcf220SDr. David Alan Gilbert 				      struct uffdio_copy)
66c1fcf220SDr. David Alan Gilbert #define UFFDIO_ZEROPAGE		_IOWR(UFFDIO, _UFFDIO_ZEROPAGE,	\
67c1fcf220SDr. David Alan Gilbert 				      struct uffdio_zeropage)
68c1fcf220SDr. David Alan Gilbert 
69c1fcf220SDr. David Alan Gilbert /* read() structure */
70c1fcf220SDr. David Alan Gilbert struct uffd_msg {
71c1fcf220SDr. David Alan Gilbert 	__u8	event;
72c1fcf220SDr. David Alan Gilbert 
73c1fcf220SDr. David Alan Gilbert 	__u8	reserved1;
74c1fcf220SDr. David Alan Gilbert 	__u16	reserved2;
75c1fcf220SDr. David Alan Gilbert 	__u32	reserved3;
76c1fcf220SDr. David Alan Gilbert 
77c1fcf220SDr. David Alan Gilbert 	union {
78c1fcf220SDr. David Alan Gilbert 		struct {
79c1fcf220SDr. David Alan Gilbert 			__u64	flags;
80c1fcf220SDr. David Alan Gilbert 			__u64	address;
81c1fcf220SDr. David Alan Gilbert 		} pagefault;
82c1fcf220SDr. David Alan Gilbert 
83c1fcf220SDr. David Alan Gilbert 		struct {
843a5eb5b4SPaolo Bonzini 			__u32	ufd;
853a5eb5b4SPaolo Bonzini 		} fork;
863a5eb5b4SPaolo Bonzini 
873a5eb5b4SPaolo Bonzini 		struct {
883a5eb5b4SPaolo Bonzini 			__u64	from;
893a5eb5b4SPaolo Bonzini 			__u64	to;
903a5eb5b4SPaolo Bonzini 			__u64	len;
913a5eb5b4SPaolo Bonzini 		} remap;
923a5eb5b4SPaolo Bonzini 
933a5eb5b4SPaolo Bonzini 		struct {
943a5eb5b4SPaolo Bonzini 			__u64	start;
953a5eb5b4SPaolo Bonzini 			__u64	end;
96*74c98e20SCornelia Huck 		} remove;
973a5eb5b4SPaolo Bonzini 
983a5eb5b4SPaolo Bonzini 		struct {
99c1fcf220SDr. David Alan Gilbert 			/* unused reserved fields */
100c1fcf220SDr. David Alan Gilbert 			__u64	reserved1;
101c1fcf220SDr. David Alan Gilbert 			__u64	reserved2;
102c1fcf220SDr. David Alan Gilbert 			__u64	reserved3;
103c1fcf220SDr. David Alan Gilbert 		} reserved;
104c1fcf220SDr. David Alan Gilbert 	} arg;
105b89485a5SPaolo Bonzini } __attribute__((packed));
106c1fcf220SDr. David Alan Gilbert 
107c1fcf220SDr. David Alan Gilbert /*
108c1fcf220SDr. David Alan Gilbert  * Start at 0x12 and not at 0 to be more strict against bugs.
109c1fcf220SDr. David Alan Gilbert  */
110c1fcf220SDr. David Alan Gilbert #define UFFD_EVENT_PAGEFAULT	0x12
111c1fcf220SDr. David Alan Gilbert #define UFFD_EVENT_FORK		0x13
1123a5eb5b4SPaolo Bonzini #define UFFD_EVENT_REMAP	0x14
113*74c98e20SCornelia Huck #define UFFD_EVENT_REMOVE	0x15
114*74c98e20SCornelia Huck #define UFFD_EVENT_UNMAP	0x16
115c1fcf220SDr. David Alan Gilbert 
116c1fcf220SDr. David Alan Gilbert /* flags for UFFD_EVENT_PAGEFAULT */
117c1fcf220SDr. David Alan Gilbert #define UFFD_PAGEFAULT_FLAG_WRITE	(1<<0)	/* If this was a write fault */
118c1fcf220SDr. David Alan Gilbert #define UFFD_PAGEFAULT_FLAG_WP		(1<<1)	/* If reason is VM_UFFD_WP */
119c1fcf220SDr. David Alan Gilbert 
120c1fcf220SDr. David Alan Gilbert struct uffdio_api {
121c1fcf220SDr. David Alan Gilbert 	/* userland asks for an API number and the features to enable */
122c1fcf220SDr. David Alan Gilbert 	__u64 api;
123c1fcf220SDr. David Alan Gilbert 	/*
124c1fcf220SDr. David Alan Gilbert 	 * Kernel answers below with the all available features for
125c1fcf220SDr. David Alan Gilbert 	 * the API, this notifies userland of which events and/or
126c1fcf220SDr. David Alan Gilbert 	 * which flags for each event are enabled in the current
127c1fcf220SDr. David Alan Gilbert 	 * kernel.
128c1fcf220SDr. David Alan Gilbert 	 *
129c1fcf220SDr. David Alan Gilbert 	 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
130c1fcf220SDr. David Alan Gilbert 	 * are to be considered implicitly always enabled in all kernels as
131c1fcf220SDr. David Alan Gilbert 	 * long as the uffdio_api.api requested matches UFFD_API.
1323a5eb5b4SPaolo Bonzini 	 *
1333a5eb5b4SPaolo Bonzini 	 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
1343a5eb5b4SPaolo Bonzini 	 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
1353a5eb5b4SPaolo Bonzini 	 * hugetlbfs virtual memory ranges. Adding or not adding
1363a5eb5b4SPaolo Bonzini 	 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
1373a5eb5b4SPaolo Bonzini 	 * no real functional effect after UFFDIO_API returns, but
1383a5eb5b4SPaolo Bonzini 	 * it's only useful for an initial feature set probe at
1393a5eb5b4SPaolo Bonzini 	 * UFFDIO_API time. There are two ways to use it:
1403a5eb5b4SPaolo Bonzini 	 *
1413a5eb5b4SPaolo Bonzini 	 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
1423a5eb5b4SPaolo Bonzini 	 *    uffdio_api.features before calling UFFDIO_API, an error
1433a5eb5b4SPaolo Bonzini 	 *    will be returned by UFFDIO_API on a kernel without
1443a5eb5b4SPaolo Bonzini 	 *    hugetlbfs missing support
1453a5eb5b4SPaolo Bonzini 	 *
1463a5eb5b4SPaolo Bonzini 	 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
1473a5eb5b4SPaolo Bonzini 	 *    uffdio_api.features and instead it will be set by the
1483a5eb5b4SPaolo Bonzini 	 *    kernel in the uffdio_api.features if the kernel supports
1493a5eb5b4SPaolo Bonzini 	 *    it, so userland can later check if the feature flag is
1503a5eb5b4SPaolo Bonzini 	 *    present in uffdio_api.features after UFFDIO_API
1513a5eb5b4SPaolo Bonzini 	 *    succeeded.
1523a5eb5b4SPaolo Bonzini 	 *
1533a5eb5b4SPaolo Bonzini 	 * UFFD_FEATURE_MISSING_SHMEM works the same as
1543a5eb5b4SPaolo Bonzini 	 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
1553a5eb5b4SPaolo Bonzini 	 * (i.e. tmpfs and other shmem based APIs).
156c1fcf220SDr. David Alan Gilbert 	 */
157c1fcf220SDr. David Alan Gilbert #define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
158c1fcf220SDr. David Alan Gilbert #define UFFD_FEATURE_EVENT_FORK			(1<<1)
1593a5eb5b4SPaolo Bonzini #define UFFD_FEATURE_EVENT_REMAP		(1<<2)
160*74c98e20SCornelia Huck #define UFFD_FEATURE_EVENT_REMOVE		(1<<3)
1613a5eb5b4SPaolo Bonzini #define UFFD_FEATURE_MISSING_HUGETLBFS		(1<<4)
1623a5eb5b4SPaolo Bonzini #define UFFD_FEATURE_MISSING_SHMEM		(1<<5)
163*74c98e20SCornelia Huck #define UFFD_FEATURE_EVENT_UNMAP		(1<<6)
164c1fcf220SDr. David Alan Gilbert 	__u64 features;
165c1fcf220SDr. David Alan Gilbert 
166c1fcf220SDr. David Alan Gilbert 	__u64 ioctls;
167c1fcf220SDr. David Alan Gilbert };
168c1fcf220SDr. David Alan Gilbert 
169c1fcf220SDr. David Alan Gilbert struct uffdio_range {
170c1fcf220SDr. David Alan Gilbert 	__u64 start;
171c1fcf220SDr. David Alan Gilbert 	__u64 len;
172c1fcf220SDr. David Alan Gilbert };
173c1fcf220SDr. David Alan Gilbert 
174c1fcf220SDr. David Alan Gilbert struct uffdio_register {
175c1fcf220SDr. David Alan Gilbert 	struct uffdio_range range;
176c1fcf220SDr. David Alan Gilbert #define UFFDIO_REGISTER_MODE_MISSING	((__u64)1<<0)
177c1fcf220SDr. David Alan Gilbert #define UFFDIO_REGISTER_MODE_WP		((__u64)1<<1)
178c1fcf220SDr. David Alan Gilbert 	__u64 mode;
179c1fcf220SDr. David Alan Gilbert 
180c1fcf220SDr. David Alan Gilbert 	/*
181c1fcf220SDr. David Alan Gilbert 	 * kernel answers which ioctl commands are available for the
182c1fcf220SDr. David Alan Gilbert 	 * range, keep at the end as the last 8 bytes aren't read.
183c1fcf220SDr. David Alan Gilbert 	 */
184c1fcf220SDr. David Alan Gilbert 	__u64 ioctls;
185c1fcf220SDr. David Alan Gilbert };
186c1fcf220SDr. David Alan Gilbert 
187c1fcf220SDr. David Alan Gilbert struct uffdio_copy {
188c1fcf220SDr. David Alan Gilbert 	__u64 dst;
189c1fcf220SDr. David Alan Gilbert 	__u64 src;
190c1fcf220SDr. David Alan Gilbert 	__u64 len;
191c1fcf220SDr. David Alan Gilbert 	/*
192c1fcf220SDr. David Alan Gilbert 	 * There will be a wrprotection flag later that allows to map
193c1fcf220SDr. David Alan Gilbert 	 * pages wrprotected on the fly. And such a flag will be
194c1fcf220SDr. David Alan Gilbert 	 * available if the wrprotection ioctl are implemented for the
195c1fcf220SDr. David Alan Gilbert 	 * range according to the uffdio_register.ioctls.
196c1fcf220SDr. David Alan Gilbert 	 */
197c1fcf220SDr. David Alan Gilbert #define UFFDIO_COPY_MODE_DONTWAKE		((__u64)1<<0)
198c1fcf220SDr. David Alan Gilbert 	__u64 mode;
199c1fcf220SDr. David Alan Gilbert 
200c1fcf220SDr. David Alan Gilbert 	/*
201c1fcf220SDr. David Alan Gilbert 	 * "copy" is written by the ioctl and must be at the end: the
202c1fcf220SDr. David Alan Gilbert 	 * copy_from_user will not read the last 8 bytes.
203c1fcf220SDr. David Alan Gilbert 	 */
204c1fcf220SDr. David Alan Gilbert 	__s64 copy;
205c1fcf220SDr. David Alan Gilbert };
206c1fcf220SDr. David Alan Gilbert 
207c1fcf220SDr. David Alan Gilbert struct uffdio_zeropage {
208c1fcf220SDr. David Alan Gilbert 	struct uffdio_range range;
209c1fcf220SDr. David Alan Gilbert #define UFFDIO_ZEROPAGE_MODE_DONTWAKE		((__u64)1<<0)
210c1fcf220SDr. David Alan Gilbert 	__u64 mode;
211c1fcf220SDr. David Alan Gilbert 
212c1fcf220SDr. David Alan Gilbert 	/*
213c1fcf220SDr. David Alan Gilbert 	 * "zeropage" is written by the ioctl and must be at the end:
214c1fcf220SDr. David Alan Gilbert 	 * the copy_from_user will not read the last 8 bytes.
215c1fcf220SDr. David Alan Gilbert 	 */
216c1fcf220SDr. David Alan Gilbert 	__s64 zeropage;
217c1fcf220SDr. David Alan Gilbert };
218c1fcf220SDr. David Alan Gilbert 
219c1fcf220SDr. David Alan Gilbert #endif /* _LINUX_USERFAULTFD_H */
220