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 #define UFFD_API ((__u64)0xAA)
15c1fcf220SDr. David Alan Gilbert /*
16c1fcf220SDr. David Alan Gilbert  * After implementing the respective features it will become:
17c1fcf220SDr. David Alan Gilbert  * #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \
18c1fcf220SDr. David Alan Gilbert  *			      UFFD_FEATURE_EVENT_FORK)
19c1fcf220SDr. David Alan Gilbert  */
20c1fcf220SDr. David Alan Gilbert #define UFFD_API_FEATURES (0)
21c1fcf220SDr. David Alan Gilbert #define UFFD_API_IOCTLS				\
22c1fcf220SDr. David Alan Gilbert 	((__u64)1 << _UFFDIO_REGISTER |		\
23c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_UNREGISTER |	\
24c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_API)
25c1fcf220SDr. David Alan Gilbert #define UFFD_API_RANGE_IOCTLS			\
26c1fcf220SDr. David Alan Gilbert 	((__u64)1 << _UFFDIO_WAKE |		\
27c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_COPY |		\
28c1fcf220SDr. David Alan Gilbert 	 (__u64)1 << _UFFDIO_ZEROPAGE)
29c1fcf220SDr. David Alan Gilbert 
30c1fcf220SDr. David Alan Gilbert /*
31c1fcf220SDr. David Alan Gilbert  * Valid ioctl command number range with this API is from 0x00 to
32c1fcf220SDr. David Alan Gilbert  * 0x3F.  UFFDIO_API is the fixed number, everything else can be
33c1fcf220SDr. David Alan Gilbert  * changed by implementing a different UFFD_API. If sticking to the
34c1fcf220SDr. David Alan Gilbert  * same UFFD_API more ioctl can be added and userland will be aware of
35c1fcf220SDr. David Alan Gilbert  * which ioctl the running kernel implements through the ioctl command
36c1fcf220SDr. David Alan Gilbert  * bitmask written by the UFFDIO_API.
37c1fcf220SDr. David Alan Gilbert  */
38c1fcf220SDr. David Alan Gilbert #define _UFFDIO_REGISTER		(0x00)
39c1fcf220SDr. David Alan Gilbert #define _UFFDIO_UNREGISTER		(0x01)
40c1fcf220SDr. David Alan Gilbert #define _UFFDIO_WAKE			(0x02)
41c1fcf220SDr. David Alan Gilbert #define _UFFDIO_COPY			(0x03)
42c1fcf220SDr. David Alan Gilbert #define _UFFDIO_ZEROPAGE		(0x04)
43c1fcf220SDr. David Alan Gilbert #define _UFFDIO_API			(0x3F)
44c1fcf220SDr. David Alan Gilbert 
45c1fcf220SDr. David Alan Gilbert /* userfaultfd ioctl ids */
46c1fcf220SDr. David Alan Gilbert #define UFFDIO 0xAA
47c1fcf220SDr. David Alan Gilbert #define UFFDIO_API		_IOWR(UFFDIO, _UFFDIO_API,	\
48c1fcf220SDr. David Alan Gilbert 				      struct uffdio_api)
49c1fcf220SDr. David Alan Gilbert #define UFFDIO_REGISTER		_IOWR(UFFDIO, _UFFDIO_REGISTER, \
50c1fcf220SDr. David Alan Gilbert 				      struct uffdio_register)
51c1fcf220SDr. David Alan Gilbert #define UFFDIO_UNREGISTER	_IOR(UFFDIO, _UFFDIO_UNREGISTER,	\
52c1fcf220SDr. David Alan Gilbert 				     struct uffdio_range)
53c1fcf220SDr. David Alan Gilbert #define UFFDIO_WAKE		_IOR(UFFDIO, _UFFDIO_WAKE,	\
54c1fcf220SDr. David Alan Gilbert 				     struct uffdio_range)
55c1fcf220SDr. David Alan Gilbert #define UFFDIO_COPY		_IOWR(UFFDIO, _UFFDIO_COPY,	\
56c1fcf220SDr. David Alan Gilbert 				      struct uffdio_copy)
57c1fcf220SDr. David Alan Gilbert #define UFFDIO_ZEROPAGE		_IOWR(UFFDIO, _UFFDIO_ZEROPAGE,	\
58c1fcf220SDr. David Alan Gilbert 				      struct uffdio_zeropage)
59c1fcf220SDr. David Alan Gilbert 
60c1fcf220SDr. David Alan Gilbert /* read() structure */
61c1fcf220SDr. David Alan Gilbert struct uffd_msg {
62c1fcf220SDr. David Alan Gilbert 	__u8	event;
63c1fcf220SDr. David Alan Gilbert 
64c1fcf220SDr. David Alan Gilbert 	__u8	reserved1;
65c1fcf220SDr. David Alan Gilbert 	__u16	reserved2;
66c1fcf220SDr. David Alan Gilbert 	__u32	reserved3;
67c1fcf220SDr. David Alan Gilbert 
68c1fcf220SDr. David Alan Gilbert 	union {
69c1fcf220SDr. David Alan Gilbert 		struct {
70c1fcf220SDr. David Alan Gilbert 			__u64	flags;
71c1fcf220SDr. David Alan Gilbert 			__u64	address;
72c1fcf220SDr. David Alan Gilbert 		} pagefault;
73c1fcf220SDr. David Alan Gilbert 
74c1fcf220SDr. David Alan Gilbert 		struct {
75c1fcf220SDr. David Alan Gilbert 			/* unused reserved fields */
76c1fcf220SDr. David Alan Gilbert 			__u64	reserved1;
77c1fcf220SDr. David Alan Gilbert 			__u64	reserved2;
78c1fcf220SDr. David Alan Gilbert 			__u64	reserved3;
79c1fcf220SDr. David Alan Gilbert 		} reserved;
80c1fcf220SDr. David Alan Gilbert 	} arg;
81*b89485a5SPaolo Bonzini } __attribute__((packed));
82c1fcf220SDr. David Alan Gilbert 
83c1fcf220SDr. David Alan Gilbert /*
84c1fcf220SDr. David Alan Gilbert  * Start at 0x12 and not at 0 to be more strict against bugs.
85c1fcf220SDr. David Alan Gilbert  */
86c1fcf220SDr. David Alan Gilbert #define UFFD_EVENT_PAGEFAULT	0x12
87c1fcf220SDr. David Alan Gilbert #if 0 /* not available yet */
88c1fcf220SDr. David Alan Gilbert #define UFFD_EVENT_FORK		0x13
89c1fcf220SDr. David Alan Gilbert #endif
90c1fcf220SDr. David Alan Gilbert 
91c1fcf220SDr. David Alan Gilbert /* flags for UFFD_EVENT_PAGEFAULT */
92c1fcf220SDr. David Alan Gilbert #define UFFD_PAGEFAULT_FLAG_WRITE	(1<<0)	/* If this was a write fault */
93c1fcf220SDr. David Alan Gilbert #define UFFD_PAGEFAULT_FLAG_WP		(1<<1)	/* If reason is VM_UFFD_WP */
94c1fcf220SDr. David Alan Gilbert 
95c1fcf220SDr. David Alan Gilbert struct uffdio_api {
96c1fcf220SDr. David Alan Gilbert 	/* userland asks for an API number and the features to enable */
97c1fcf220SDr. David Alan Gilbert 	__u64 api;
98c1fcf220SDr. David Alan Gilbert 	/*
99c1fcf220SDr. David Alan Gilbert 	 * Kernel answers below with the all available features for
100c1fcf220SDr. David Alan Gilbert 	 * the API, this notifies userland of which events and/or
101c1fcf220SDr. David Alan Gilbert 	 * which flags for each event are enabled in the current
102c1fcf220SDr. David Alan Gilbert 	 * kernel.
103c1fcf220SDr. David Alan Gilbert 	 *
104c1fcf220SDr. David Alan Gilbert 	 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
105c1fcf220SDr. David Alan Gilbert 	 * are to be considered implicitly always enabled in all kernels as
106c1fcf220SDr. David Alan Gilbert 	 * long as the uffdio_api.api requested matches UFFD_API.
107c1fcf220SDr. David Alan Gilbert 	 */
108c1fcf220SDr. David Alan Gilbert #if 0 /* not available yet */
109c1fcf220SDr. David Alan Gilbert #define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
110c1fcf220SDr. David Alan Gilbert #define UFFD_FEATURE_EVENT_FORK			(1<<1)
111c1fcf220SDr. David Alan Gilbert #endif
112c1fcf220SDr. David Alan Gilbert 	__u64 features;
113c1fcf220SDr. David Alan Gilbert 
114c1fcf220SDr. David Alan Gilbert 	__u64 ioctls;
115c1fcf220SDr. David Alan Gilbert };
116c1fcf220SDr. David Alan Gilbert 
117c1fcf220SDr. David Alan Gilbert struct uffdio_range {
118c1fcf220SDr. David Alan Gilbert 	__u64 start;
119c1fcf220SDr. David Alan Gilbert 	__u64 len;
120c1fcf220SDr. David Alan Gilbert };
121c1fcf220SDr. David Alan Gilbert 
122c1fcf220SDr. David Alan Gilbert struct uffdio_register {
123c1fcf220SDr. David Alan Gilbert 	struct uffdio_range range;
124c1fcf220SDr. David Alan Gilbert #define UFFDIO_REGISTER_MODE_MISSING	((__u64)1<<0)
125c1fcf220SDr. David Alan Gilbert #define UFFDIO_REGISTER_MODE_WP		((__u64)1<<1)
126c1fcf220SDr. David Alan Gilbert 	__u64 mode;
127c1fcf220SDr. David Alan Gilbert 
128c1fcf220SDr. David Alan Gilbert 	/*
129c1fcf220SDr. David Alan Gilbert 	 * kernel answers which ioctl commands are available for the
130c1fcf220SDr. David Alan Gilbert 	 * range, keep at the end as the last 8 bytes aren't read.
131c1fcf220SDr. David Alan Gilbert 	 */
132c1fcf220SDr. David Alan Gilbert 	__u64 ioctls;
133c1fcf220SDr. David Alan Gilbert };
134c1fcf220SDr. David Alan Gilbert 
135c1fcf220SDr. David Alan Gilbert struct uffdio_copy {
136c1fcf220SDr. David Alan Gilbert 	__u64 dst;
137c1fcf220SDr. David Alan Gilbert 	__u64 src;
138c1fcf220SDr. David Alan Gilbert 	__u64 len;
139c1fcf220SDr. David Alan Gilbert 	/*
140c1fcf220SDr. David Alan Gilbert 	 * There will be a wrprotection flag later that allows to map
141c1fcf220SDr. David Alan Gilbert 	 * pages wrprotected on the fly. And such a flag will be
142c1fcf220SDr. David Alan Gilbert 	 * available if the wrprotection ioctl are implemented for the
143c1fcf220SDr. David Alan Gilbert 	 * range according to the uffdio_register.ioctls.
144c1fcf220SDr. David Alan Gilbert 	 */
145c1fcf220SDr. David Alan Gilbert #define UFFDIO_COPY_MODE_DONTWAKE		((__u64)1<<0)
146c1fcf220SDr. David Alan Gilbert 	__u64 mode;
147c1fcf220SDr. David Alan Gilbert 
148c1fcf220SDr. David Alan Gilbert 	/*
149c1fcf220SDr. David Alan Gilbert 	 * "copy" is written by the ioctl and must be at the end: the
150c1fcf220SDr. David Alan Gilbert 	 * copy_from_user will not read the last 8 bytes.
151c1fcf220SDr. David Alan Gilbert 	 */
152c1fcf220SDr. David Alan Gilbert 	__s64 copy;
153c1fcf220SDr. David Alan Gilbert };
154c1fcf220SDr. David Alan Gilbert 
155c1fcf220SDr. David Alan Gilbert struct uffdio_zeropage {
156c1fcf220SDr. David Alan Gilbert 	struct uffdio_range range;
157c1fcf220SDr. David Alan Gilbert #define UFFDIO_ZEROPAGE_MODE_DONTWAKE		((__u64)1<<0)
158c1fcf220SDr. David Alan Gilbert 	__u64 mode;
159c1fcf220SDr. David Alan Gilbert 
160c1fcf220SDr. David Alan Gilbert 	/*
161c1fcf220SDr. David Alan Gilbert 	 * "zeropage" is written by the ioctl and must be at the end:
162c1fcf220SDr. David Alan Gilbert 	 * the copy_from_user will not read the last 8 bytes.
163c1fcf220SDr. David Alan Gilbert 	 */
164c1fcf220SDr. David Alan Gilbert 	__s64 zeropage;
165c1fcf220SDr. David Alan Gilbert };
166c1fcf220SDr. David Alan Gilbert 
167c1fcf220SDr. David Alan Gilbert #endif /* _LINUX_USERFAULTFD_H */
168