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 /* ioctls for /dev/userfaultfd */ 16 #define USERFAULTFD_IOC 0xAA 17 #define USERFAULTFD_IOC_NEW _IO(USERFAULTFD_IOC, 0x00) 18 19 /* 20 * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and 21 * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR. In 22 * userfaultfd.h we assumed the kernel was reading (instead _IOC_READ 23 * means the userland is reading). 24 */ 25 #define UFFD_API ((__u64)0xAA) 26 #define UFFD_API_REGISTER_MODES (UFFDIO_REGISTER_MODE_MISSING | \ 27 UFFDIO_REGISTER_MODE_WP | \ 28 UFFDIO_REGISTER_MODE_MINOR | \ 29 UFFDIO_REGISTER_MODE_RWP) 30 #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \ 31 UFFD_FEATURE_EVENT_FORK | \ 32 UFFD_FEATURE_EVENT_REMAP | \ 33 UFFD_FEATURE_EVENT_REMOVE | \ 34 UFFD_FEATURE_EVENT_UNMAP | \ 35 UFFD_FEATURE_MISSING_HUGETLBFS | \ 36 UFFD_FEATURE_MISSING_SHMEM | \ 37 UFFD_FEATURE_SIGBUS | \ 38 UFFD_FEATURE_THREAD_ID | \ 39 UFFD_FEATURE_MINOR_HUGETLBFS | \ 40 UFFD_FEATURE_MINOR_SHMEM | \ 41 UFFD_FEATURE_EXACT_ADDRESS | \ 42 UFFD_FEATURE_WP_HUGETLBFS_SHMEM | \ 43 UFFD_FEATURE_WP_UNPOPULATED | \ 44 UFFD_FEATURE_POISON | \ 45 UFFD_FEATURE_WP_ASYNC | \ 46 UFFD_FEATURE_MOVE | \ 47 UFFD_FEATURE_RWP | \ 48 UFFD_FEATURE_RWP_ASYNC) 49 #define UFFD_API_IOCTLS \ 50 ((__u64)1 << _UFFDIO_REGISTER | \ 51 (__u64)1 << _UFFDIO_UNREGISTER | \ 52 (__u64)1 << _UFFDIO_SET_MODE | \ 53 (__u64)1 << _UFFDIO_API) 54 #define UFFD_API_RANGE_IOCTLS \ 55 ((__u64)1 << _UFFDIO_WAKE | \ 56 (__u64)1 << _UFFDIO_COPY | \ 57 (__u64)1 << _UFFDIO_ZEROPAGE | \ 58 (__u64)1 << _UFFDIO_MOVE | \ 59 (__u64)1 << _UFFDIO_WRITEPROTECT | \ 60 (__u64)1 << _UFFDIO_CONTINUE | \ 61 (__u64)1 << _UFFDIO_POISON | \ 62 (__u64)1 << _UFFDIO_RWPROTECT) 63 #define UFFD_API_RANGE_IOCTLS_BASIC \ 64 ((__u64)1 << _UFFDIO_WAKE | \ 65 (__u64)1 << _UFFDIO_COPY | \ 66 (__u64)1 << _UFFDIO_WRITEPROTECT | \ 67 (__u64)1 << _UFFDIO_CONTINUE | \ 68 (__u64)1 << _UFFDIO_POISON | \ 69 (__u64)1 << _UFFDIO_RWPROTECT) 70 71 /* 72 * Valid ioctl command number range with this API is from 0x00 to 73 * 0x3F. UFFDIO_API is the fixed number, everything else can be 74 * changed by implementing a different UFFD_API. If sticking to the 75 * same UFFD_API more ioctl can be added and userland will be aware of 76 * which ioctl the running kernel implements through the ioctl command 77 * bitmask written by the UFFDIO_API. 78 */ 79 #define _UFFDIO_REGISTER (0x00) 80 #define _UFFDIO_UNREGISTER (0x01) 81 #define _UFFDIO_WAKE (0x02) 82 #define _UFFDIO_COPY (0x03) 83 #define _UFFDIO_ZEROPAGE (0x04) 84 #define _UFFDIO_MOVE (0x05) 85 #define _UFFDIO_WRITEPROTECT (0x06) 86 #define _UFFDIO_CONTINUE (0x07) 87 #define _UFFDIO_POISON (0x08) 88 #define _UFFDIO_RWPROTECT (0x09) 89 #define _UFFDIO_SET_MODE (0x0A) 90 #define _UFFDIO_API (0x3F) 91 92 /* userfaultfd ioctl ids */ 93 #define UFFDIO 0xAA 94 #define UFFDIO_API _IOWR(UFFDIO, _UFFDIO_API, \ 95 struct uffdio_api) 96 #define UFFDIO_REGISTER _IOWR(UFFDIO, _UFFDIO_REGISTER, \ 97 struct uffdio_register) 98 #define UFFDIO_UNREGISTER _IOR(UFFDIO, _UFFDIO_UNREGISTER, \ 99 struct uffdio_range) 100 #define UFFDIO_WAKE _IOR(UFFDIO, _UFFDIO_WAKE, \ 101 struct uffdio_range) 102 #define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \ 103 struct uffdio_copy) 104 #define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \ 105 struct uffdio_zeropage) 106 #define UFFDIO_MOVE _IOWR(UFFDIO, _UFFDIO_MOVE, \ 107 struct uffdio_move) 108 #define UFFDIO_WRITEPROTECT _IOWR(UFFDIO, _UFFDIO_WRITEPROTECT, \ 109 struct uffdio_writeprotect) 110 #define UFFDIO_CONTINUE _IOWR(UFFDIO, _UFFDIO_CONTINUE, \ 111 struct uffdio_continue) 112 #define UFFDIO_POISON _IOWR(UFFDIO, _UFFDIO_POISON, \ 113 struct uffdio_poison) 114 #define UFFDIO_RWPROTECT _IOWR(UFFDIO, _UFFDIO_RWPROTECT, \ 115 struct uffdio_rwprotect) 116 #define UFFDIO_SET_MODE _IOW(UFFDIO, _UFFDIO_SET_MODE, \ 117 struct uffdio_set_mode) 118 119 /* read() structure */ 120 struct uffd_msg { 121 __u8 event; 122 123 __u8 reserved1; 124 __u16 reserved2; 125 __u32 reserved3; 126 127 union { 128 struct { 129 __u64 flags; 130 __u64 address; 131 union { 132 __u32 ptid; 133 } feat; 134 } pagefault; 135 136 struct { 137 __u32 ufd; 138 } fork; 139 140 struct { 141 __u64 from; 142 __u64 to; 143 __u64 len; 144 } remap; 145 146 struct { 147 __u64 start; 148 __u64 end; 149 } remove; 150 151 struct { 152 /* unused reserved fields */ 153 __u64 reserved1; 154 __u64 reserved2; 155 __u64 reserved3; 156 } reserved; 157 } arg; 158 } __packed; 159 160 /* 161 * Start at 0x12 and not at 0 to be more strict against bugs. 162 */ 163 #define UFFD_EVENT_PAGEFAULT 0x12 164 #define UFFD_EVENT_FORK 0x13 165 #define UFFD_EVENT_REMAP 0x14 166 #define UFFD_EVENT_REMOVE 0x15 167 #define UFFD_EVENT_UNMAP 0x16 168 169 /* flags for UFFD_EVENT_PAGEFAULT */ 170 #define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */ 171 #define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */ 172 #define UFFD_PAGEFAULT_FLAG_MINOR (1<<2) /* If reason is VM_UFFD_MINOR */ 173 #define UFFD_PAGEFAULT_FLAG_RWP (1<<3) /* If reason is VM_UFFD_RWP */ 174 175 struct uffdio_api { 176 /* userland asks for an API number and the features to enable */ 177 __u64 api; 178 /* 179 * Kernel answers below with the all available features for 180 * the API, this notifies userland of which events and/or 181 * which flags for each event are enabled in the current 182 * kernel. 183 * 184 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE 185 * are to be considered implicitly always enabled in all kernels as 186 * long as the uffdio_api.api requested matches UFFD_API. 187 * 188 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER 189 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on 190 * hugetlbfs virtual memory ranges. Adding or not adding 191 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has 192 * no real functional effect after UFFDIO_API returns, but 193 * it's only useful for an initial feature set probe at 194 * UFFDIO_API time. There are two ways to use it: 195 * 196 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the 197 * uffdio_api.features before calling UFFDIO_API, an error 198 * will be returned by UFFDIO_API on a kernel without 199 * hugetlbfs missing support 200 * 201 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in 202 * uffdio_api.features and instead it will be set by the 203 * kernel in the uffdio_api.features if the kernel supports 204 * it, so userland can later check if the feature flag is 205 * present in uffdio_api.features after UFFDIO_API 206 * succeeded. 207 * 208 * UFFD_FEATURE_MISSING_SHMEM works the same as 209 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem 210 * (i.e. tmpfs and other shmem based APIs). 211 * 212 * UFFD_FEATURE_SIGBUS feature means no page-fault 213 * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead 214 * a SIGBUS signal will be sent to the faulting process. 215 * 216 * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will 217 * be returned, if feature is not requested 0 will be returned. 218 * 219 * UFFD_FEATURE_MINOR_HUGETLBFS indicates that minor faults 220 * can be intercepted (via REGISTER_MODE_MINOR) for 221 * hugetlbfs-backed pages. 222 * 223 * UFFD_FEATURE_MINOR_SHMEM indicates the same support as 224 * UFFD_FEATURE_MINOR_HUGETLBFS, but for shmem-backed pages instead. 225 * 226 * UFFD_FEATURE_EXACT_ADDRESS indicates that the exact address of page 227 * faults would be provided and the offset within the page would not be 228 * masked. 229 * 230 * UFFD_FEATURE_WP_HUGETLBFS_SHMEM indicates that userfaultfd 231 * write-protection mode is supported on both shmem and hugetlbfs. 232 * 233 * UFFD_FEATURE_WP_UNPOPULATED indicates that userfaultfd 234 * write-protection mode will always apply to unpopulated pages 235 * (i.e. empty ptes). This will be the default behavior for shmem 236 * & hugetlbfs, so this flag only affects anonymous memory behavior 237 * when userfault write-protection mode is registered. 238 * 239 * UFFD_FEATURE_WP_ASYNC indicates that userfaultfd write-protection 240 * asynchronous mode is supported in which the write fault is 241 * automatically resolved and write-protection is un-set. 242 * It implies UFFD_FEATURE_WP_UNPOPULATED. 243 * 244 * UFFD_FEATURE_MOVE indicates that the kernel supports moving an 245 * existing page contents from userspace. 246 * 247 * UFFD_FEATURE_RWP indicates that the kernel supports 248 * UFFDIO_REGISTER_MODE_RWP for read-write protection tracking. 249 * Pages are made inaccessible via UFFDIO_RWPROTECT and faults 250 * are delivered when the pages are re-accessed. 251 * 252 * UFFD_FEATURE_RWP_ASYNC indicates asynchronous mode for 253 * UFFDIO_REGISTER_MODE_RWP. When set, faults on read-write 254 * protected pages are auto-resolved by the kernel (PTE 255 * permissions restored immediately) without delivering a message 256 * to the userfaultfd handler. Use PAGEMAP_SCAN with inverted 257 * PAGE_IS_ACCESSED to find pages that were not re-accessed. 258 */ 259 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0) 260 #define UFFD_FEATURE_EVENT_FORK (1<<1) 261 #define UFFD_FEATURE_EVENT_REMAP (1<<2) 262 #define UFFD_FEATURE_EVENT_REMOVE (1<<3) 263 #define UFFD_FEATURE_MISSING_HUGETLBFS (1<<4) 264 #define UFFD_FEATURE_MISSING_SHMEM (1<<5) 265 #define UFFD_FEATURE_EVENT_UNMAP (1<<6) 266 #define UFFD_FEATURE_SIGBUS (1<<7) 267 #define UFFD_FEATURE_THREAD_ID (1<<8) 268 #define UFFD_FEATURE_MINOR_HUGETLBFS (1<<9) 269 #define UFFD_FEATURE_MINOR_SHMEM (1<<10) 270 #define UFFD_FEATURE_EXACT_ADDRESS (1<<11) 271 #define UFFD_FEATURE_WP_HUGETLBFS_SHMEM (1<<12) 272 #define UFFD_FEATURE_WP_UNPOPULATED (1<<13) 273 #define UFFD_FEATURE_POISON (1<<14) 274 #define UFFD_FEATURE_WP_ASYNC (1<<15) 275 #define UFFD_FEATURE_MOVE (1<<16) 276 #define UFFD_FEATURE_RWP (1<<17) 277 #define UFFD_FEATURE_RWP_ASYNC (1<<18) 278 __u64 features; 279 280 __u64 ioctls; 281 }; 282 283 struct uffdio_range { 284 __u64 start; 285 __u64 len; 286 }; 287 288 struct uffdio_register { 289 struct uffdio_range range; 290 #define UFFDIO_REGISTER_MODE_MISSING ((__u64)1<<0) 291 #define UFFDIO_REGISTER_MODE_WP ((__u64)1<<1) 292 #define UFFDIO_REGISTER_MODE_MINOR ((__u64)1<<2) 293 #define UFFDIO_REGISTER_MODE_RWP ((__u64)1<<3) 294 __u64 mode; 295 296 /* 297 * kernel answers which ioctl commands are available for the 298 * range, keep at the end as the last 8 bytes aren't read. 299 */ 300 __u64 ioctls; 301 }; 302 303 struct uffdio_copy { 304 __u64 dst; 305 __u64 src; 306 __u64 len; 307 #define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0) 308 /* 309 * UFFDIO_COPY_MODE_WP will map the page write protected on 310 * the fly. UFFDIO_COPY_MODE_WP is available only if the 311 * write protected ioctl is implemented for the range 312 * according to the uffdio_register.ioctls. 313 */ 314 #define UFFDIO_COPY_MODE_WP ((__u64)1<<1) 315 __u64 mode; 316 317 /* 318 * "copy" is written by the ioctl and must be at the end: the 319 * copy_from_user will not read the last 8 bytes. 320 */ 321 __s64 copy; 322 }; 323 324 struct uffdio_zeropage { 325 struct uffdio_range range; 326 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE ((__u64)1<<0) 327 __u64 mode; 328 329 /* 330 * "zeropage" is written by the ioctl and must be at the end: 331 * the copy_from_user will not read the last 8 bytes. 332 */ 333 __s64 zeropage; 334 }; 335 336 struct uffdio_writeprotect { 337 struct uffdio_range range; 338 /* 339 * UFFDIO_WRITEPROTECT_MODE_WP: set the flag to write protect a range, 340 * unset the flag to undo protection of a range which was previously 341 * write protected. 342 * 343 * UFFDIO_WRITEPROTECT_MODE_DONTWAKE: set the flag to avoid waking up 344 * any wait thread after the operation succeeds. 345 * 346 * NOTE: Write protecting a region (WP=1) is unrelated to page faults, 347 * therefore DONTWAKE flag is meaningless with WP=1. Removing write 348 * protection (WP=0) in response to a page fault wakes the faulting 349 * task unless DONTWAKE is set. 350 */ 351 #define UFFDIO_WRITEPROTECT_MODE_WP ((__u64)1<<0) 352 #define UFFDIO_WRITEPROTECT_MODE_DONTWAKE ((__u64)1<<1) 353 __u64 mode; 354 }; 355 356 struct uffdio_continue { 357 struct uffdio_range range; 358 #define UFFDIO_CONTINUE_MODE_DONTWAKE ((__u64)1<<0) 359 /* 360 * UFFDIO_CONTINUE_MODE_WP will map the page write protected on 361 * the fly. UFFDIO_CONTINUE_MODE_WP is available only if the 362 * write protected ioctl is implemented for the range 363 * according to the uffdio_register.ioctls. 364 */ 365 #define UFFDIO_CONTINUE_MODE_WP ((__u64)1<<1) 366 __u64 mode; 367 368 /* 369 * Fields below here are written by the ioctl and must be at the end: 370 * the copy_from_user will not read past here. 371 */ 372 __s64 mapped; 373 }; 374 375 struct uffdio_poison { 376 struct uffdio_range range; 377 #define UFFDIO_POISON_MODE_DONTWAKE ((__u64)1<<0) 378 __u64 mode; 379 380 /* 381 * Fields below here are written by the ioctl and must be at the end: 382 * the copy_from_user will not read past here. 383 */ 384 __s64 updated; 385 }; 386 387 struct uffdio_rwprotect { 388 struct uffdio_range range; 389 /* !RWP means undo RWP-protection */ 390 #define UFFDIO_RWPROTECT_MODE_RWP ((__u64)1<<0) 391 #define UFFDIO_RWPROTECT_MODE_DONTWAKE ((__u64)1<<1) 392 __u64 mode; 393 }; 394 395 struct uffdio_move { 396 __u64 dst; 397 __u64 src; 398 __u64 len; 399 /* 400 * Especially if used to atomically remove memory from the 401 * address space the wake on the dst range is not needed. 402 */ 403 #define UFFDIO_MOVE_MODE_DONTWAKE ((__u64)1<<0) 404 #define UFFDIO_MOVE_MODE_ALLOW_SRC_HOLES ((__u64)1<<1) 405 __u64 mode; 406 /* 407 * "move" is written by the ioctl and must be at the end: the 408 * copy_from_user will not read the last 8 bytes. 409 */ 410 __s64 move; 411 }; 412 413 struct uffdio_set_mode { 414 /* 415 * Toggle async mode for features at runtime. 416 * Supported: UFFD_FEATURE_RWP_ASYNC. 417 * Setting a bit in both enable and disable is invalid. 418 */ 419 __u64 enable; 420 __u64 disable; 421 }; 422 423 /* 424 * Flags for the userfaultfd(2) system call itself. 425 */ 426 427 /* 428 * Create a userfaultfd that can handle page faults only in user mode. 429 */ 430 #define UFFD_USER_MODE_ONLY 1 431 432 #endif /* _LINUX_USERFAULTFD_H */ 433