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