1 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */ 2 /* 3 * include/linux/userfaultfd.h 4 * 5 * Copyright (C) 2007 Davide Libenzi <[email protected]> 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_WRITEPROTECT (0x06) 56 #define _UFFDIO_API (0x3F) 57 58 /* userfaultfd ioctl ids */ 59 #define UFFDIO 0xAA 60 #define UFFDIO_API _IOWR(UFFDIO, _UFFDIO_API, \ 61 struct uffdio_api) 62 #define UFFDIO_REGISTER _IOWR(UFFDIO, _UFFDIO_REGISTER, \ 63 struct uffdio_register) 64 #define UFFDIO_UNREGISTER _IOR(UFFDIO, _UFFDIO_UNREGISTER, \ 65 struct uffdio_range) 66 #define UFFDIO_WAKE _IOR(UFFDIO, _UFFDIO_WAKE, \ 67 struct uffdio_range) 68 #define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \ 69 struct uffdio_copy) 70 #define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \ 71 struct uffdio_zeropage) 72 #define UFFDIO_WRITEPROTECT _IOWR(UFFDIO, _UFFDIO_WRITEPROTECT, \ 73 struct uffdio_writeprotect) 74 75 /* read() structure */ 76 struct uffd_msg { 77 __u8 event; 78 79 __u8 reserved1; 80 __u16 reserved2; 81 __u32 reserved3; 82 83 union { 84 struct { 85 __u64 flags; 86 __u64 address; 87 union { 88 __u32 ptid; 89 } feat; 90 } pagefault; 91 92 struct { 93 __u32 ufd; 94 } fork; 95 96 struct { 97 __u64 from; 98 __u64 to; 99 __u64 len; 100 } remap; 101 102 struct { 103 __u64 start; 104 __u64 end; 105 } remove; 106 107 struct { 108 /* unused reserved fields */ 109 __u64 reserved1; 110 __u64 reserved2; 111 __u64 reserved3; 112 } reserved; 113 } arg; 114 } __packed; 115 116 /* 117 * Start at 0x12 and not at 0 to be more strict against bugs. 118 */ 119 #define UFFD_EVENT_PAGEFAULT 0x12 120 #define UFFD_EVENT_FORK 0x13 121 #define UFFD_EVENT_REMAP 0x14 122 #define UFFD_EVENT_REMOVE 0x15 123 #define UFFD_EVENT_UNMAP 0x16 124 125 /* flags for UFFD_EVENT_PAGEFAULT */ 126 #define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */ 127 #define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */ 128 129 struct uffdio_api { 130 /* userland asks for an API number and the features to enable */ 131 __u64 api; 132 /* 133 * Kernel answers below with the all available features for 134 * the API, this notifies userland of which events and/or 135 * which flags for each event are enabled in the current 136 * kernel. 137 * 138 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE 139 * are to be considered implicitly always enabled in all kernels as 140 * long as the uffdio_api.api requested matches UFFD_API. 141 * 142 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER 143 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on 144 * hugetlbfs virtual memory ranges. Adding or not adding 145 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has 146 * no real functional effect after UFFDIO_API returns, but 147 * it's only useful for an initial feature set probe at 148 * UFFDIO_API time. There are two ways to use it: 149 * 150 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the 151 * uffdio_api.features before calling UFFDIO_API, an error 152 * will be returned by UFFDIO_API on a kernel without 153 * hugetlbfs missing support 154 * 155 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in 156 * uffdio_api.features and instead it will be set by the 157 * kernel in the uffdio_api.features if the kernel supports 158 * it, so userland can later check if the feature flag is 159 * present in uffdio_api.features after UFFDIO_API 160 * succeeded. 161 * 162 * UFFD_FEATURE_MISSING_SHMEM works the same as 163 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem 164 * (i.e. tmpfs and other shmem based APIs). 165 * 166 * UFFD_FEATURE_SIGBUS feature means no page-fault 167 * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead 168 * a SIGBUS signal will be sent to the faulting process. 169 * 170 * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will 171 * be returned, if feature is not requested 0 will be returned. 172 */ 173 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0) 174 #define UFFD_FEATURE_EVENT_FORK (1<<1) 175 #define UFFD_FEATURE_EVENT_REMAP (1<<2) 176 #define UFFD_FEATURE_EVENT_REMOVE (1<<3) 177 #define UFFD_FEATURE_MISSING_HUGETLBFS (1<<4) 178 #define UFFD_FEATURE_MISSING_SHMEM (1<<5) 179 #define UFFD_FEATURE_EVENT_UNMAP (1<<6) 180 #define UFFD_FEATURE_SIGBUS (1<<7) 181 #define UFFD_FEATURE_THREAD_ID (1<<8) 182 __u64 features; 183 184 __u64 ioctls; 185 }; 186 187 struct uffdio_range { 188 __u64 start; 189 __u64 len; 190 }; 191 192 struct uffdio_register { 193 struct uffdio_range range; 194 #define UFFDIO_REGISTER_MODE_MISSING ((__u64)1<<0) 195 #define UFFDIO_REGISTER_MODE_WP ((__u64)1<<1) 196 __u64 mode; 197 198 /* 199 * kernel answers which ioctl commands are available for the 200 * range, keep at the end as the last 8 bytes aren't read. 201 */ 202 __u64 ioctls; 203 }; 204 205 struct uffdio_copy { 206 __u64 dst; 207 __u64 src; 208 __u64 len; 209 #define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0) 210 /* 211 * UFFDIO_COPY_MODE_WP will map the page write protected on 212 * the fly. UFFDIO_COPY_MODE_WP is available only if the 213 * write protected ioctl is implemented for the range 214 * according to the uffdio_register.ioctls. 215 */ 216 #define UFFDIO_COPY_MODE_WP ((__u64)1<<1) 217 __u64 mode; 218 219 /* 220 * "copy" is written by the ioctl and must be at the end: the 221 * copy_from_user will not read the last 8 bytes. 222 */ 223 __s64 copy; 224 }; 225 226 struct uffdio_zeropage { 227 struct uffdio_range range; 228 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE ((__u64)1<<0) 229 __u64 mode; 230 231 /* 232 * "zeropage" is written by the ioctl and must be at the end: 233 * the copy_from_user will not read the last 8 bytes. 234 */ 235 __s64 zeropage; 236 }; 237 238 struct uffdio_writeprotect { 239 struct uffdio_range range; 240 /* 241 * UFFDIO_WRITEPROTECT_MODE_WP: set the flag to write protect a range, 242 * unset the flag to undo protection of a range which was previously 243 * write protected. 244 * 245 * UFFDIO_WRITEPROTECT_MODE_DONTWAKE: set the flag to avoid waking up 246 * any wait thread after the operation succeeds. 247 * 248 * NOTE: Write protecting a region (WP=1) is unrelated to page faults, 249 * therefore DONTWAKE flag is meaningless with WP=1. Removing write 250 * protection (WP=0) in response to a page fault wakes the faulting 251 * task unless DONTWAKE is set. 252 */ 253 #define UFFDIO_WRITEPROTECT_MODE_WP ((__u64)1<<0) 254 #define UFFDIO_WRITEPROTECT_MODE_DONTWAKE ((__u64)1<<1) 255 __u64 mode; 256 }; 257 258 #endif /* _LINUX_USERFAULTFD_H */ 259