blob: 48f1a7c2f1f056117ce80166fdd126d1e32c08b4 [file] [log] [blame]
Greg Kroah-Hartman6f52b162017-11-01 15:08:43 +01001/* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
Andrea Arcangeli10386282015-09-04 15:46:04 -07002/*
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 Arcangelie067eba2017-02-22 15:42:06 -080015/*
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 Arcangeli10386282015-09-04 15:46:04 -070021#define UFFD_API ((__u64)0xAA)
Andrea Arcangelidd0db88d2017-03-09 16:16:49 -080022#define UFFD_API_FEATURES (UFFD_FEATURE_EVENT_FORK | \
Andrea Arcangeli163e11b2017-02-22 15:43:19 -080023 UFFD_FEATURE_EVENT_REMAP | \
Mike Rapoportd8119142017-02-24 14:56:02 -080024 UFFD_FEATURE_EVENT_REMOVE | \
Mike Rapoport897ab3e2017-02-24 14:58:22 -080025 UFFD_FEATURE_EVENT_UNMAP | \
Andrea Arcangeli47dd9242017-02-22 15:43:58 -080026 UFFD_FEATURE_MISSING_HUGETLBFS | \
Prakash Sangappa2d6d6f52017-09-06 16:23:39 -070027 UFFD_FEATURE_MISSING_SHMEM | \
Alexey Perevalov9d4ac932017-09-06 16:23:56 -070028 UFFD_FEATURE_SIGBUS | \
29 UFFD_FEATURE_THREAD_ID)
Andrea Arcangeli10386282015-09-04 15:46:04 -070030#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 Arcangeli1f1c6f02015-09-04 15:47:01 -070035 ((__u64)1 << _UFFDIO_WAKE | \
36 (__u64)1 << _UFFDIO_COPY | \
37 (__u64)1 << _UFFDIO_ZEROPAGE)
Mike Rapoportcac67322017-02-22 15:43:40 -080038#define UFFD_API_RANGE_IOCTLS_BASIC \
Mike Kravetzcab350a2017-02-22 15:43:04 -080039 ((__u64)1 << _UFFDIO_WAKE | \
40 (__u64)1 << _UFFDIO_COPY)
Andrea Arcangeli10386282015-09-04 15:46:04 -070041
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 Arcangeli1f1c6f02015-09-04 15:47:01 -070053#define _UFFDIO_COPY (0x03)
54#define _UFFDIO_ZEROPAGE (0x04)
Andrea Arcangeli10386282015-09-04 15:46:04 -070055#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 Arcangeli1f1c6f02015-09-04 15:47:01 -070067#define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \
68 struct uffdio_copy)
69#define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \
70 struct uffdio_zeropage)
Andrea Arcangeli10386282015-09-04 15:46:04 -070071
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070072/* read() structure */
73struct 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 Arcangelia36985d2017-09-06 16:23:59 -070084 union {
85 __u32 ptid;
86 } feat;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070087 } pagefault;
88
89 struct {
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080090 __u32 ufd;
91 } fork;
92
93 struct {
Pavel Emelyanov72f87652017-02-22 15:42:34 -080094 __u64 from;
95 __u64 to;
96 __u64 len;
97 } remap;
98
99 struct {
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800100 __u64 start;
101 __u64 end;
Mike Rapoportd8119142017-02-24 14:56:02 -0800102 } remove;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -0800103
104 struct {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700105 /* unused reserved fields */
106 __u64 reserved1;
107 __u64 reserved2;
108 __u64 reserved3;
109 } reserved;
110 } arg;
111} __packed;
Andrea Arcangeli10386282015-09-04 15:46:04 -0700112
Pavel Emelyanov3f602d22015-09-04 15:46:34 -0700113/*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700114 * Start at 0x12 and not at 0 to be more strict against bugs.
Pavel Emelyanov3f602d22015-09-04 15:46:34 -0700115 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700116#define UFFD_EVENT_PAGEFAULT 0x12
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700117#define UFFD_EVENT_FORK 0x13
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800118#define UFFD_EVENT_REMAP 0x14
Mike Rapoportd8119142017-02-24 14:56:02 -0800119#define UFFD_EVENT_REMOVE 0x15
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800120#define UFFD_EVENT_UNMAP 0x16
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700121
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 Emelyanov3f602d22015-09-04 15:46:34 -0700125
Andrea Arcangeli10386282015-09-04 15:46:04 -0700126struct uffdio_api {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700127 /* userland asks for an API number and the features to enable */
Andrea Arcangeli10386282015-09-04 15:46:04 -0700128 __u64 api;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700129 /*
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 Arcangeli163e11b2017-02-22 15:43:19 -0800138 *
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 Arcangeli47dd9242017-02-22 15:43:58 -0800158 *
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 Sangappa2d6d6f52017-09-06 16:23:39 -0700162 *
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 Perevalov9d4ac932017-09-06 16:23:56 -0700166 *
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 Arcangelia9b85f92015-09-04 15:46:37 -0700169 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700170#define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0)
171#define UFFD_FEATURE_EVENT_FORK (1<<1)
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800172#define UFFD_FEATURE_EVENT_REMAP (1<<2)
Mike Rapoportd8119142017-02-24 14:56:02 -0800173#define UFFD_FEATURE_EVENT_REMOVE (1<<3)
Andrea Arcangeli163e11b2017-02-22 15:43:19 -0800174#define UFFD_FEATURE_MISSING_HUGETLBFS (1<<4)
Andrea Arcangeli47dd9242017-02-22 15:43:58 -0800175#define UFFD_FEATURE_MISSING_SHMEM (1<<5)
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800176#define UFFD_FEATURE_EVENT_UNMAP (1<<6)
Prakash Sangappa2d6d6f52017-09-06 16:23:39 -0700177#define UFFD_FEATURE_SIGBUS (1<<7)
Alexey Perevalov9d4ac932017-09-06 16:23:56 -0700178#define UFFD_FEATURE_THREAD_ID (1<<8)
Pavel Emelyanov3f602d22015-09-04 15:46:34 -0700179 __u64 features;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700180
Andrea Arcangeli10386282015-09-04 15:46:04 -0700181 __u64 ioctls;
182};
183
184struct uffdio_range {
185 __u64 start;
186 __u64 len;
187};
188
189struct 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 Arcangeli1f1c6f02015-09-04 15:47:01 -0700202struct 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
222struct 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 Arcangeli10386282015-09-04 15:46:04 -0700234#endif /* _LINUX_USERFAULTFD_H */