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