1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
3 * include/linux/userfaultfd.h
5 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org>
6 * Copyright (C) 2015 Red Hat, Inc.
10 #ifndef _LINUX_USERFAULTFD_H
11 #define _LINUX_USERFAULTFD_H
13 #include <linux/types.h>
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).
21 #define UFFD_API ((__u64)0xAA)
22 #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \
23 UFFD_FEATURE_EVENT_FORK | \
24 UFFD_FEATURE_EVENT_REMAP | \
25 UFFD_FEATURE_EVENT_REMOVE | \
26 UFFD_FEATURE_EVENT_UNMAP | \
27 UFFD_FEATURE_MISSING_HUGETLBFS | \
28 UFFD_FEATURE_MISSING_SHMEM | \
29 UFFD_FEATURE_SIGBUS | \
30 UFFD_FEATURE_THREAD_ID)
31 #define UFFD_API_IOCTLS \
32 ((__u64)1 << _UFFDIO_REGISTER | \
33 (__u64)1 << _UFFDIO_UNREGISTER | \
34 (__u64)1 << _UFFDIO_API)
35 #define UFFD_API_RANGE_IOCTLS \
36 ((__u64)1 << _UFFDIO_WAKE | \
37 (__u64)1 << _UFFDIO_COPY | \
38 (__u64)1 << _UFFDIO_ZEROPAGE | \
39 (__u64)1 << _UFFDIO_WRITEPROTECT)
40 #define UFFD_API_RANGE_IOCTLS_BASIC \
41 ((__u64)1 << _UFFDIO_WAKE | \
42 (__u64)1 << _UFFDIO_COPY)
45 * Valid ioctl command number range with this API is from 0x00 to
46 * 0x3F. UFFDIO_API is the fixed number, everything else can be
47 * changed by implementing a different UFFD_API. If sticking to the
48 * same UFFD_API more ioctl can be added and userland will be aware of
49 * which ioctl the running kernel implements through the ioctl command
50 * bitmask written by the UFFDIO_API.
52 #define _UFFDIO_REGISTER (0x00)
53 #define _UFFDIO_UNREGISTER (0x01)
54 #define _UFFDIO_WAKE (0x02)
55 #define _UFFDIO_COPY (0x03)
56 #define _UFFDIO_ZEROPAGE (0x04)
57 #define _UFFDIO_WRITEPROTECT (0x06)
58 #define _UFFDIO_API (0x3F)
60 /* userfaultfd ioctl ids */
62 #define UFFDIO_API _IOWR(UFFDIO, _UFFDIO_API, \
64 #define UFFDIO_REGISTER _IOWR(UFFDIO, _UFFDIO_REGISTER, \
65 struct uffdio_register)
66 #define UFFDIO_UNREGISTER _IOR(UFFDIO, _UFFDIO_UNREGISTER, \
68 #define UFFDIO_WAKE _IOR(UFFDIO, _UFFDIO_WAKE, \
70 #define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \
72 #define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \
73 struct uffdio_zeropage)
74 #define UFFDIO_WRITEPROTECT _IOWR(UFFDIO, _UFFDIO_WRITEPROTECT, \
75 struct uffdio_writeprotect)
77 /* read() structure */
110 /* unused reserved fields */
116 } __attribute__((packed
));
119 * Start at 0x12 and not at 0 to be more strict against bugs.
121 #define UFFD_EVENT_PAGEFAULT 0x12
122 #define UFFD_EVENT_FORK 0x13
123 #define UFFD_EVENT_REMAP 0x14
124 #define UFFD_EVENT_REMOVE 0x15
125 #define UFFD_EVENT_UNMAP 0x16
127 /* flags for UFFD_EVENT_PAGEFAULT */
128 #define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */
129 #define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */
132 /* userland asks for an API number and the features to enable */
135 * Kernel answers below with the all available features for
136 * the API, this notifies userland of which events and/or
137 * which flags for each event are enabled in the current
140 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
141 * are to be considered implicitly always enabled in all kernels as
142 * long as the uffdio_api.api requested matches UFFD_API.
144 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
145 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
146 * hugetlbfs virtual memory ranges. Adding or not adding
147 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
148 * no real functional effect after UFFDIO_API returns, but
149 * it's only useful for an initial feature set probe at
150 * UFFDIO_API time. There are two ways to use it:
152 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
153 * uffdio_api.features before calling UFFDIO_API, an error
154 * will be returned by UFFDIO_API on a kernel without
155 * hugetlbfs missing support
157 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
158 * uffdio_api.features and instead it will be set by the
159 * kernel in the uffdio_api.features if the kernel supports
160 * it, so userland can later check if the feature flag is
161 * present in uffdio_api.features after UFFDIO_API
164 * UFFD_FEATURE_MISSING_SHMEM works the same as
165 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
166 * (i.e. tmpfs and other shmem based APIs).
168 * UFFD_FEATURE_SIGBUS feature means no page-fault
169 * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead
170 * a SIGBUS signal will be sent to the faulting process.
172 * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will
173 * be returned, if feature is not requested 0 will be returned.
175 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0)
176 #define UFFD_FEATURE_EVENT_FORK (1<<1)
177 #define UFFD_FEATURE_EVENT_REMAP (1<<2)
178 #define UFFD_FEATURE_EVENT_REMOVE (1<<3)
179 #define UFFD_FEATURE_MISSING_HUGETLBFS (1<<4)
180 #define UFFD_FEATURE_MISSING_SHMEM (1<<5)
181 #define UFFD_FEATURE_EVENT_UNMAP (1<<6)
182 #define UFFD_FEATURE_SIGBUS (1<<7)
183 #define UFFD_FEATURE_THREAD_ID (1<<8)
189 struct uffdio_range
{
194 struct uffdio_register
{
195 struct uffdio_range range
;
196 #define UFFDIO_REGISTER_MODE_MISSING ((__u64)1<<0)
197 #define UFFDIO_REGISTER_MODE_WP ((__u64)1<<1)
201 * kernel answers which ioctl commands are available for the
202 * range, keep at the end as the last 8 bytes aren't read.
211 #define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0)
213 * UFFDIO_COPY_MODE_WP will map the page write protected on
214 * the fly. UFFDIO_COPY_MODE_WP is available only if the
215 * write protected ioctl is implemented for the range
216 * according to the uffdio_register.ioctls.
218 #define UFFDIO_COPY_MODE_WP ((__u64)1<<1)
222 * "copy" is written by the ioctl and must be at the end: the
223 * copy_from_user will not read the last 8 bytes.
228 struct uffdio_zeropage
{
229 struct uffdio_range range
;
230 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE ((__u64)1<<0)
234 * "zeropage" is written by the ioctl and must be at the end:
235 * the copy_from_user will not read the last 8 bytes.
240 struct uffdio_writeprotect
{
241 struct uffdio_range range
;
243 * UFFDIO_WRITEPROTECT_MODE_WP: set the flag to write protect a range,
244 * unset the flag to undo protection of a range which was previously
247 * UFFDIO_WRITEPROTECT_MODE_DONTWAKE: set the flag to avoid waking up
248 * any wait thread after the operation succeeds.
250 * NOTE: Write protecting a region (WP=1) is unrelated to page faults,
251 * therefore DONTWAKE flag is meaningless with WP=1. Removing write
252 * protection (WP=0) in response to a page fault wakes the faulting
253 * task unless DONTWAKE is set.
255 #define UFFDIO_WRITEPROTECT_MODE_WP ((__u64)1<<0)
256 #define UFFDIO_WRITEPROTECT_MODE_DONTWAKE ((__u64)1<<1)
260 #endif /* _LINUX_USERFAULTFD_H */