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