112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris/*
212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris *  include/linux/userfaultfd.h
312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris *
412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris *  Copyright (C) 2015  Red Hat, Inc.
612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris *
712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris */
812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#ifndef _LINUX_USERFAULTFD_H
1012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define _LINUX_USERFAULTFD_H
1112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
1212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#include <linux/types.h>
1312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
1412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_API ((__u64)0xAA)
1512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris/*
1612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * After implementing the respective features it will become:
1712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \
1812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris *			      UFFD_FEATURE_EVENT_FORK)
1912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris */
2012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_API_FEATURES (0)
2112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_API_IOCTLS				\
2212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	((__u64)1 << _UFFDIO_REGISTER |		\
2312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 (__u64)1 << _UFFDIO_UNREGISTER |	\
2412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 (__u64)1 << _UFFDIO_API)
2512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_API_RANGE_IOCTLS			\
2612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	((__u64)1 << _UFFDIO_WAKE |		\
2712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 (__u64)1 << _UFFDIO_COPY |		\
2812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 (__u64)1 << _UFFDIO_ZEROPAGE)
2912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
3012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris/*
3112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * Valid ioctl command number range with this API is from 0x00 to
3212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * 0x3F.  UFFDIO_API is the fixed number, everything else can be
3312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * changed by implementing a different UFFD_API. If sticking to the
3412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * same UFFD_API more ioctl can be added and userland will be aware of
3512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * which ioctl the running kernel implements through the ioctl command
3612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * bitmask written by the UFFDIO_API.
3712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris */
3812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define _UFFDIO_REGISTER		(0x00)
3912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define _UFFDIO_UNREGISTER		(0x01)
4012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define _UFFDIO_WAKE			(0x02)
4112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define _UFFDIO_COPY			(0x03)
4212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define _UFFDIO_ZEROPAGE		(0x04)
4312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define _UFFDIO_API			(0x3F)
4412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
4512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris/* userfaultfd ioctl ids */
4612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO 0xAA
4712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_API		_IOWR(UFFDIO, _UFFDIO_API,	\
4812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris				      struct uffdio_api)
4912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_REGISTER		_IOWR(UFFDIO, _UFFDIO_REGISTER, \
5012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris				      struct uffdio_register)
5112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_UNREGISTER	_IOR(UFFDIO, _UFFDIO_UNREGISTER,	\
5212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris				     struct uffdio_range)
5312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_WAKE		_IOR(UFFDIO, _UFFDIO_WAKE,	\
5412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris				     struct uffdio_range)
5512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_COPY		_IOWR(UFFDIO, _UFFDIO_COPY,	\
5612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris				      struct uffdio_copy)
5712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_ZEROPAGE		_IOWR(UFFDIO, _UFFDIO_ZEROPAGE,	\
5812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris				      struct uffdio_zeropage)
5912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
6012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris/* read() structure */
6112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferrisstruct uffd_msg {
6212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u8	event;
6312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
6412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u8	reserved1;
6512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u16	reserved2;
6612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u32	reserved3;
6712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
6812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	union {
6912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris		struct {
7012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris			__u64	flags;
7112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris			__u64	address;
7212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris		} pagefault;
7312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
7412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris		struct {
7512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris			/* unused reserved fields */
7612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris			__u64	reserved1;
7712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris			__u64	reserved2;
7812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris			__u64	reserved3;
7912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris		} reserved;
8012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	} arg;
8112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris} __packed;
8212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
8312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris/*
8412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris * Start at 0x12 and not at 0 to be more strict against bugs.
8512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris */
8612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_EVENT_PAGEFAULT	0x12
8712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#if 0 /* not available yet */
8812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_EVENT_FORK		0x13
8912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#endif
9012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
9112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris/* flags for UFFD_EVENT_PAGEFAULT */
9212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_PAGEFAULT_FLAG_WRITE	(1<<0)	/* If this was a write fault */
9312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_PAGEFAULT_FLAG_WP		(1<<1)	/* If reason is VM_UFFD_WP */
9412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
9512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferrisstruct uffdio_api {
9612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	/* userland asks for an API number and the features to enable */
9712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 api;
9812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	/*
9912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * Kernel answers below with the all available features for
10012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * the API, this notifies userland of which events and/or
10112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * which flags for each event are enabled in the current
10212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * kernel.
10312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 *
10412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
10512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * are to be considered implicitly always enabled in all kernels as
10612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * long as the uffdio_api.api requested matches UFFD_API.
10712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 */
10812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#if 0 /* not available yet */
10912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_FEATURE_PAGEFAULT_FLAG_WP		(1<<0)
11012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFD_FEATURE_EVENT_FORK			(1<<1)
11112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#endif
11212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 features;
11312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
11412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 ioctls;
11512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris};
11612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
11712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferrisstruct uffdio_range {
11812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 start;
11912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 len;
12012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris};
12112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
12212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferrisstruct uffdio_register {
12312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	struct uffdio_range range;
12412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_REGISTER_MODE_MISSING	((__u64)1<<0)
12512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_REGISTER_MODE_WP		((__u64)1<<1)
12612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 mode;
12712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
12812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	/*
12912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * kernel answers which ioctl commands are available for the
13012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * range, keep at the end as the last 8 bytes aren't read.
13112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 */
13212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 ioctls;
13312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris};
13412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
13512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferrisstruct uffdio_copy {
13612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 dst;
13712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 src;
13812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 len;
13912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	/*
14012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * There will be a wrprotection flag later that allows to map
14112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * pages wrprotected on the fly. And such a flag will be
14212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * available if the wrprotection ioctl are implemented for the
14312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * range according to the uffdio_register.ioctls.
14412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 */
14512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_COPY_MODE_DONTWAKE		((__u64)1<<0)
14612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 mode;
14712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
14812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	/*
14912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * "copy" is written by the ioctl and must be at the end: the
15012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * copy_from_user will not read the last 8 bytes.
15112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 */
15212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__s64 copy;
15312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris};
15412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
15512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferrisstruct uffdio_zeropage {
15612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	struct uffdio_range range;
15712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#define UFFDIO_ZEROPAGE_MODE_DONTWAKE		((__u64)1<<0)
15812e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__u64 mode;
15912e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
16012e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	/*
16112e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * "zeropage" is written by the ioctl and must be at the end:
16212e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 * the copy_from_user will not read the last 8 bytes.
16312e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	 */
16412e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris	__s64 zeropage;
16512e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris};
16612e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris
16712e1f28cce28271eb109a5bae818a804f0c3bb27Christopher Ferris#endif /* _LINUX_USERFAULTFD_H */
168