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