blob: 3b059530dac95fa6e5dcf736e95a84fe80eb5f35 [file] [log] [blame]
Andrea Arcangeli10386282015-09-04 15:46:04 -07001/*
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 Arcangelie067eba2017-02-22 15:42:06 -080014/*
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 Arcangeli10386282015-09-04 15:46:04 -070020#define UFFD_API ((__u64)0xAA)
Andrea Arcangeli163e11b2017-02-22 15:43:19 -080021#define UFFD_API_FEATURES (UFFD_FEATURE_EVENT_FORK | \
22 UFFD_FEATURE_EVENT_REMAP | \
Mike Rapoportd8119142017-02-24 14:56:02 -080023 UFFD_FEATURE_EVENT_REMOVE | \
Mike Rapoport897ab3e2017-02-24 14:58:22 -080024 UFFD_FEATURE_EVENT_UNMAP | \
Andrea Arcangeli47dd9242017-02-22 15:43:58 -080025 UFFD_FEATURE_MISSING_HUGETLBFS | \
26 UFFD_FEATURE_MISSING_SHMEM)
Andrea Arcangeli10386282015-09-04 15:46:04 -070027#define UFFD_API_IOCTLS \
28 ((__u64)1 << _UFFDIO_REGISTER | \
29 (__u64)1 << _UFFDIO_UNREGISTER | \
30 (__u64)1 << _UFFDIO_API)
31#define UFFD_API_RANGE_IOCTLS \
Andrea Arcangeli1f1c6f02015-09-04 15:47:01 -070032 ((__u64)1 << _UFFDIO_WAKE | \
33 (__u64)1 << _UFFDIO_COPY | \
34 (__u64)1 << _UFFDIO_ZEROPAGE)
Mike Rapoportcac67322017-02-22 15:43:40 -080035#define UFFD_API_RANGE_IOCTLS_BASIC \
Mike Kravetzcab350a2017-02-22 15:43:04 -080036 ((__u64)1 << _UFFDIO_WAKE | \
37 (__u64)1 << _UFFDIO_COPY)
Andrea Arcangeli10386282015-09-04 15:46:04 -070038
39/*
40 * Valid ioctl command number range with this API is from 0x00 to
41 * 0x3F. UFFDIO_API is the fixed number, everything else can be
42 * changed by implementing a different UFFD_API. If sticking to the
43 * same UFFD_API more ioctl can be added and userland will be aware of
44 * which ioctl the running kernel implements through the ioctl command
45 * bitmask written by the UFFDIO_API.
46 */
47#define _UFFDIO_REGISTER (0x00)
48#define _UFFDIO_UNREGISTER (0x01)
49#define _UFFDIO_WAKE (0x02)
Andrea Arcangeli1f1c6f02015-09-04 15:47:01 -070050#define _UFFDIO_COPY (0x03)
51#define _UFFDIO_ZEROPAGE (0x04)
Andrea Arcangeli10386282015-09-04 15:46:04 -070052#define _UFFDIO_API (0x3F)
53
54/* userfaultfd ioctl ids */
55#define UFFDIO 0xAA
56#define UFFDIO_API _IOWR(UFFDIO, _UFFDIO_API, \
57 struct uffdio_api)
58#define UFFDIO_REGISTER _IOWR(UFFDIO, _UFFDIO_REGISTER, \
59 struct uffdio_register)
60#define UFFDIO_UNREGISTER _IOR(UFFDIO, _UFFDIO_UNREGISTER, \
61 struct uffdio_range)
62#define UFFDIO_WAKE _IOR(UFFDIO, _UFFDIO_WAKE, \
63 struct uffdio_range)
Andrea Arcangeli1f1c6f02015-09-04 15:47:01 -070064#define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \
65 struct uffdio_copy)
66#define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \
67 struct uffdio_zeropage)
Andrea Arcangeli10386282015-09-04 15:46:04 -070068
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070069/* read() structure */
70struct uffd_msg {
71 __u8 event;
72
73 __u8 reserved1;
74 __u16 reserved2;
75 __u32 reserved3;
76
77 union {
78 struct {
79 __u64 flags;
80 __u64 address;
81 } pagefault;
82
83 struct {
Pavel Emelyanov893e26e2017-02-22 15:42:27 -080084 __u32 ufd;
85 } fork;
86
87 struct {
Pavel Emelyanov72f87652017-02-22 15:42:34 -080088 __u64 from;
89 __u64 to;
90 __u64 len;
91 } remap;
92
93 struct {
Pavel Emelyanov05ce7722017-02-22 15:42:40 -080094 __u64 start;
95 __u64 end;
Mike Rapoportd8119142017-02-24 14:56:02 -080096 } remove;
Pavel Emelyanov05ce7722017-02-22 15:42:40 -080097
98 struct {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -070099 /* unused reserved fields */
100 __u64 reserved1;
101 __u64 reserved2;
102 __u64 reserved3;
103 } reserved;
104 } arg;
105} __packed;
Andrea Arcangeli10386282015-09-04 15:46:04 -0700106
Pavel Emelyanov3f602d22015-09-04 15:46:34 -0700107/*
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700108 * Start at 0x12 and not at 0 to be more strict against bugs.
Pavel Emelyanov3f602d22015-09-04 15:46:34 -0700109 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700110#define UFFD_EVENT_PAGEFAULT 0x12
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700111#define UFFD_EVENT_FORK 0x13
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800112#define UFFD_EVENT_REMAP 0x14
Mike Rapoportd8119142017-02-24 14:56:02 -0800113#define UFFD_EVENT_REMOVE 0x15
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800114#define UFFD_EVENT_UNMAP 0x16
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700115
116/* flags for UFFD_EVENT_PAGEFAULT */
117#define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */
118#define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */
Pavel Emelyanov3f602d22015-09-04 15:46:34 -0700119
Andrea Arcangeli10386282015-09-04 15:46:04 -0700120struct uffdio_api {
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700121 /* userland asks for an API number and the features to enable */
Andrea Arcangeli10386282015-09-04 15:46:04 -0700122 __u64 api;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700123 /*
124 * Kernel answers below with the all available features for
125 * the API, this notifies userland of which events and/or
126 * which flags for each event are enabled in the current
127 * kernel.
128 *
129 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
130 * are to be considered implicitly always enabled in all kernels as
131 * long as the uffdio_api.api requested matches UFFD_API.
Andrea Arcangeli163e11b2017-02-22 15:43:19 -0800132 *
133 * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
134 * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
135 * hugetlbfs virtual memory ranges. Adding or not adding
136 * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
137 * no real functional effect after UFFDIO_API returns, but
138 * it's only useful for an initial feature set probe at
139 * UFFDIO_API time. There are two ways to use it:
140 *
141 * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
142 * uffdio_api.features before calling UFFDIO_API, an error
143 * will be returned by UFFDIO_API on a kernel without
144 * hugetlbfs missing support
145 *
146 * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
147 * uffdio_api.features and instead it will be set by the
148 * kernel in the uffdio_api.features if the kernel supports
149 * it, so userland can later check if the feature flag is
150 * present in uffdio_api.features after UFFDIO_API
151 * succeeded.
Andrea Arcangeli47dd9242017-02-22 15:43:58 -0800152 *
153 * UFFD_FEATURE_MISSING_SHMEM works the same as
154 * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
155 * (i.e. tmpfs and other shmem based APIs).
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700156 */
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700157#define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0)
158#define UFFD_FEATURE_EVENT_FORK (1<<1)
Pavel Emelyanov72f87652017-02-22 15:42:34 -0800159#define UFFD_FEATURE_EVENT_REMAP (1<<2)
Mike Rapoportd8119142017-02-24 14:56:02 -0800160#define UFFD_FEATURE_EVENT_REMOVE (1<<3)
Andrea Arcangeli163e11b2017-02-22 15:43:19 -0800161#define UFFD_FEATURE_MISSING_HUGETLBFS (1<<4)
Andrea Arcangeli47dd9242017-02-22 15:43:58 -0800162#define UFFD_FEATURE_MISSING_SHMEM (1<<5)
Mike Rapoport897ab3e2017-02-24 14:58:22 -0800163#define UFFD_FEATURE_EVENT_UNMAP (1<<6)
Pavel Emelyanov3f602d22015-09-04 15:46:34 -0700164 __u64 features;
Andrea Arcangelia9b85f92015-09-04 15:46:37 -0700165
Andrea Arcangeli10386282015-09-04 15:46:04 -0700166 __u64 ioctls;
167};
168
169struct uffdio_range {
170 __u64 start;
171 __u64 len;
172};
173
174struct uffdio_register {
175 struct uffdio_range range;
176#define UFFDIO_REGISTER_MODE_MISSING ((__u64)1<<0)
177#define UFFDIO_REGISTER_MODE_WP ((__u64)1<<1)
178 __u64 mode;
179
180 /*
181 * kernel answers which ioctl commands are available for the
182 * range, keep at the end as the last 8 bytes aren't read.
183 */
184 __u64 ioctls;
185};
186
Andrea Arcangeli1f1c6f02015-09-04 15:47:01 -0700187struct uffdio_copy {
188 __u64 dst;
189 __u64 src;
190 __u64 len;
191 /*
192 * There will be a wrprotection flag later that allows to map
193 * pages wrprotected on the fly. And such a flag will be
194 * available if the wrprotection ioctl are implemented for the
195 * range according to the uffdio_register.ioctls.
196 */
197#define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0)
198 __u64 mode;
199
200 /*
201 * "copy" is written by the ioctl and must be at the end: the
202 * copy_from_user will not read the last 8 bytes.
203 */
204 __s64 copy;
205};
206
207struct uffdio_zeropage {
208 struct uffdio_range range;
209#define UFFDIO_ZEROPAGE_MODE_DONTWAKE ((__u64)1<<0)
210 __u64 mode;
211
212 /*
213 * "zeropage" is written by the ioctl and must be at the end:
214 * the copy_from_user will not read the last 8 bytes.
215 */
216 __s64 zeropage;
217};
218
Andrea Arcangeli10386282015-09-04 15:46:04 -0700219#endif /* _LINUX_USERFAULTFD_H */