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