1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * Copyright (C) 2007 Davide Libenzi <davidel@xmailserver.org> 4 * Copyright (C) 2015,2022 Red Hat, Inc. 5 * 6 * Mostly copied/adapted from <linux/userfaultfd.h> 7 */ 8 9 #ifndef LAPI_USERFAULTFD_H__ 10 #define LAPI_USERFAULTFD_H__ 11 12 #include <unistd.h> 13 #include <sys/types.h> 14 #include "lapi/syscalls.h" 15 16 #ifdef HAVE_LINUX_USERFAULTFD_H 17 #include <linux/userfaultfd.h> 18 #endif 19 20 /* userfaultfd support was added in v4.1 */ 21 #ifndef UFFD_API 22 #define UFFD_API ((__u64)0xAA) 23 24 /* 25 * Valid ioctl command number range with this API is from 0x00 to 26 * 0x3F. UFFDIO_API is the fixed number, everything else can be 27 * changed by implementing a different UFFD_API. If sticking to the 28 * same UFFD_API more ioctl can be added and userland will be aware of 29 * which ioctl the running kernel implements through the ioctl command 30 * bitmask written by the UFFDIO_API. 31 */ 32 #define _UFFDIO_REGISTER (0x00) 33 #define _UFFDIO_UNREGISTER (0x01) 34 #define _UFFDIO_WAKE (0x02) 35 #define _UFFDIO_COPY (0x03) 36 #define _UFFDIO_ZEROPAGE (0x04) 37 #define _UFFDIO_API (0x3F) 38 39 /* userfaultfd ioctl ids */ 40 #define UFFDIO 0xAA 41 #define UFFDIO_API _IOWR(UFFDIO, _UFFDIO_API, \ 42 struct uffdio_api) 43 #define UFFDIO_REGISTER _IOWR(UFFDIO, _UFFDIO_REGISTER, \ 44 struct uffdio_register) 45 #define UFFDIO_UNREGISTER _IOR(UFFDIO, _UFFDIO_UNREGISTER, \ 46 struct uffdio_range) 47 #define UFFDIO_WAKE _IOR(UFFDIO, _UFFDIO_WAKE, \ 48 struct uffdio_range) 49 #define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \ 50 struct uffdio_copy) 51 #define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \ 52 struct uffdio_zeropage) 53 54 /* read() structure */ 55 struct uffd_msg { 56 __u8 event; 57 58 __u8 reserved1; 59 __u16 reserved2; 60 __u32 reserved3; 61 62 union { 63 struct { 64 __u64 flags; 65 __u64 address; 66 } pagefault; 67 68 struct { 69 /* unused reserved fields */ 70 __u64 reserved1; 71 __u64 reserved2; 72 __u64 reserved3; 73 } reserved; 74 } arg; 75 } __packed; 76 77 /* 78 * Start at 0x12 and not at 0 to be more strict against bugs. 79 */ 80 #define UFFD_EVENT_PAGEFAULT 0x12 81 82 /* flags for UFFD_EVENT_PAGEFAULT */ 83 #define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */ 84 #define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */ 85 86 struct uffdio_api { 87 /* userland asks for an API number and the features to enable */ 88 __u64 api; 89 /* 90 * Kernel answers below with the all available features for 91 * the API, this notifies userland of which events and/or 92 * which flags for each event are enabled in the current 93 * kernel. 94 * 95 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE 96 * are to be considered implicitly always enabled in all kernels as 97 * long as the uffdio_api.api requested matches UFFD_API. 98 */ 99 __u64 features; 100 101 __u64 ioctls; 102 }; 103 104 struct uffdio_range { 105 __u64 start; 106 __u64 len; 107 }; 108 109 struct uffdio_register { 110 struct uffdio_range range; 111 #define UFFDIO_REGISTER_MODE_MISSING ((__u64)1<<0) 112 #define UFFDIO_REGISTER_MODE_WP ((__u64)1<<1) 113 __u64 mode; 114 115 /* 116 * kernel answers which ioctl commands are available for the 117 * range, keep at the end as the last 8 bytes aren't read. 118 */ 119 __u64 ioctls; 120 }; 121 122 struct uffdio_copy { 123 __u64 dst; 124 __u64 src; 125 __u64 len; 126 /* 127 * There will be a wrprotection flag later that allows to map 128 * pages wrprotected on the fly. And such a flag will be 129 * available if the wrprotection ioctl are implemented for the 130 * range according to the uffdio_register.ioctls. 131 */ 132 #define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0) 133 __u64 mode; 134 135 /* 136 * "copy" is written by the ioctl and must be at the end: the 137 * copy_from_user will not read the last 8 bytes. 138 */ 139 __s64 copy; 140 }; 141 142 struct uffdio_zeropage { 143 struct uffdio_range range; 144 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE ((__u64)1<<0) 145 __u64 mode; 146 147 /* 148 * "zeropage" is written by the ioctl and must be at the end: 149 * the copy_from_user will not read the last 8 bytes. 150 */ 151 __s64 zeropage; 152 }; 153 #endif /* UFFD_API */ 154 155 156 /* UFFD_USER_MODE_ONLY was added in v5.11 */ 157 #ifndef UFFD_USER_MODE_ONLY 158 #define UFFD_USER_MODE_ONLY 1 159 #endif /* UFFD_USER_MODE_ONLY */ 160 161 162 /* UFFD_PAGEFAULT_FLAG_MINOR and UFFDIO_CONTINUE were added in v5.13 */ 163 #ifndef UFFD_PAGEFAULT_FLAG_MINOR 164 #define UFFD_FEATURE_MINOR_HUGETLBFS (1<<9) 165 #define UFFDIO_REGISTER_MODE_MINOR ((__u64)1<<2) 166 167 #define _UFFDIO_CONTINUE (0x07) 168 #define UFFDIO_CONTINUE _IOWR(UFFDIO, _UFFDIO_CONTINUE, \ 169 struct uffdio_continue) 170 171 struct uffdio_continue { 172 struct uffdio_range range; 173 #define UFFDIO_CONTINUE_MODE_DONTWAKE ((__u64)1<<0) 174 __u64 mode; 175 176 /* 177 * Fields below here are written by the ioctl and must be at the end: 178 * the copy_from_user will not read past here. 179 */ 180 __s64 mapped; 181 }; 182 #endif /* UFFD_PAGEFAULT_FLAG_MINOR */ 183 184 185 /* UFFD_FEATURE_MINOR_SHMEM was added in v5.14 */ 186 #ifndef UFFD_FEATURE_MINOR_SHMEM 187 #define UFFD_FEATURE_MINOR_SHMEM (1<<10) 188 #endif /* UFFD_FEATURE_MINOR_SHMEM */ 189 190 #endif /* LAPI_USERFAULTFD_H__ */ 191