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
14#define UFFD_API ((__u64)0xAA)
15/*
16 * After implementing the respective features it will become:
17 * #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \
18 *			      UFFD_FEATURE_EVENT_FORK)
19 */
20#define UFFD_API_FEATURES (0)
21#define UFFD_API_IOCTLS				\
22	((__u64)1 << _UFFDIO_REGISTER |		\
23	 (__u64)1 << _UFFDIO_UNREGISTER |	\
24	 (__u64)1 << _UFFDIO_API)
25#define UFFD_API_RANGE_IOCTLS			\
26	((__u64)1 << _UFFDIO_WAKE |		\
27	 (__u64)1 << _UFFDIO_COPY |		\
28	 (__u64)1 << _UFFDIO_ZEROPAGE)
29
30/*
31 * Valid ioctl command number range with this API is from 0x00 to
32 * 0x3F.  UFFDIO_API is the fixed number, everything else can be
33 * changed by implementing a different UFFD_API. If sticking to the
34 * same UFFD_API more ioctl can be added and userland will be aware of
35 * which ioctl the running kernel implements through the ioctl command
36 * bitmask written by the UFFDIO_API.
37 */
38#define _UFFDIO_REGISTER		(0x00)
39#define _UFFDIO_UNREGISTER		(0x01)
40#define _UFFDIO_WAKE			(0x02)
41#define _UFFDIO_COPY			(0x03)
42#define _UFFDIO_ZEROPAGE		(0x04)
43#define _UFFDIO_API			(0x3F)
44
45/* userfaultfd ioctl ids */
46#define UFFDIO 0xAA
47#define UFFDIO_API		_IOWR(UFFDIO, _UFFDIO_API,	\
48				      struct uffdio_api)
49#define UFFDIO_REGISTER		_IOWR(UFFDIO, _UFFDIO_REGISTER, \
50				      struct uffdio_register)
51#define UFFDIO_UNREGISTER	_IOR(UFFDIO, _UFFDIO_UNREGISTER,	\
52				     struct uffdio_range)
53#define UFFDIO_WAKE		_IOR(UFFDIO, _UFFDIO_WAKE,	\
54				     struct uffdio_range)
55#define UFFDIO_COPY		_IOWR(UFFDIO, _UFFDIO_COPY,	\
56				      struct uffdio_copy)
57#define UFFDIO_ZEROPAGE		_IOWR(UFFDIO, _UFFDIO_ZEROPAGE,	\
58				      struct uffdio_zeropage)
59
60/* read() structure */
61struct uffd_msg {
62	__u8	event;
63
64	__u8	reserved1;
65	__u16	reserved2;
66	__u32	reserved3;
67
68	union {
69		struct {
70			__u64	flags;
71			__u64	address;
72		} pagefault;
73
74		struct {
75			/* unused reserved fields */
76			__u64	reserved1;
77			__u64	reserved2;
78			__u64	reserved3;
79		} reserved;
80	} arg;
81} __packed;
82
83/*
84 * Start at 0x12 and not at 0 to be more strict against bugs.
85 */
86#define UFFD_EVENT_PAGEFAULT	0x12
87#if 0 /* not available yet */
88#define UFFD_EVENT_FORK		0x13
89#endif
90
91/* flags for UFFD_EVENT_PAGEFAULT */
92#define UFFD_PAGEFAULT_FLAG_WRITE	(1<<0)	/* If this was a write fault */
93#define UFFD_PAGEFAULT_FLAG_WP		(1<<1)	/* If reason is VM_UFFD_WP */
94
95struct uffdio_api {
96	/* userland asks for an API number and the features to enable */
97	__u64 api;
98	/*
99	 * Kernel answers below with the all available features for
100	 * the API, this notifies userland of which events and/or
101	 * which flags for each event are enabled in the current
102	 * kernel.
103	 *
104	 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
105	 * are to be considered implicitly always enabled in all kernels as
106	 * long as the uffdio_api.api requested matches UFFD_API.
107	 */
108#if 0 /* not available yet */
109#define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
110#define UFFD_FEATURE_EVENT_FORK			(1<<1)
111#endif
112	__u64 features;
113
114	__u64 ioctls;
115};
116
117struct uffdio_range {
118	__u64 start;
119	__u64 len;
120};
121
122struct uffdio_register {
123	struct uffdio_range range;
124#define UFFDIO_REGISTER_MODE_MISSING	((__u64)1<<0)
125#define UFFDIO_REGISTER_MODE_WP		((__u64)1<<1)
126	__u64 mode;
127
128	/*
129	 * kernel answers which ioctl commands are available for the
130	 * range, keep at the end as the last 8 bytes aren't read.
131	 */
132	__u64 ioctls;
133};
134
135struct uffdio_copy {
136	__u64 dst;
137	__u64 src;
138	__u64 len;
139	/*
140	 * There will be a wrprotection flag later that allows to map
141	 * pages wrprotected on the fly. And such a flag will be
142	 * available if the wrprotection ioctl are implemented for the
143	 * range according to the uffdio_register.ioctls.
144	 */
145#define UFFDIO_COPY_MODE_DONTWAKE		((__u64)1<<0)
146	__u64 mode;
147
148	/*
149	 * "copy" is written by the ioctl and must be at the end: the
150	 * copy_from_user will not read the last 8 bytes.
151	 */
152	__s64 copy;
153};
154
155struct uffdio_zeropage {
156	struct uffdio_range range;
157#define UFFDIO_ZEROPAGE_MODE_DONTWAKE		((__u64)1<<0)
158	__u64 mode;
159
160	/*
161	 * "zeropage" is written by the ioctl and must be at the end:
162	 * the copy_from_user will not read the last 8 bytes.
163	 */
164	__s64 zeropage;
165};
166
167#endif /* _LINUX_USERFAULTFD_H */
168