userfaultfd.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  1. /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
  2. /*
  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. #ifndef _LINUX_USERFAULTFD_H
  10. #define _LINUX_USERFAULTFD_H
  11. #include <linux/types.h>
  12. /* ioctls for /dev/userfaultfd */
  13. #define USERFAULTFD_IOC 0xAA
  14. #define USERFAULTFD_IOC_NEW _IO(USERFAULTFD_IOC, 0x00)
  15. /*
  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. */
  21. #define UFFD_API ((__u64)0xAA)
  22. #define UFFD_API_REGISTER_MODES (UFFDIO_REGISTER_MODE_MISSING | \
  23. UFFDIO_REGISTER_MODE_WP | \
  24. UFFDIO_REGISTER_MODE_MINOR)
  25. #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \
  26. UFFD_FEATURE_EVENT_FORK | \
  27. UFFD_FEATURE_EVENT_REMAP | \
  28. UFFD_FEATURE_EVENT_REMOVE | \
  29. UFFD_FEATURE_EVENT_UNMAP | \
  30. UFFD_FEATURE_MISSING_HUGETLBFS | \
  31. UFFD_FEATURE_MISSING_SHMEM | \
  32. UFFD_FEATURE_SIGBUS | \
  33. UFFD_FEATURE_THREAD_ID | \
  34. UFFD_FEATURE_MINOR_HUGETLBFS | \
  35. UFFD_FEATURE_MINOR_SHMEM | \
  36. UFFD_FEATURE_EXACT_ADDRESS | \
  37. UFFD_FEATURE_WP_HUGETLBFS_SHMEM | \
  38. UFFD_FEATURE_WP_UNPOPULATED)
  39. #define UFFD_API_IOCTLS \
  40. ((__u64)1 << _UFFDIO_REGISTER | \
  41. (__u64)1 << _UFFDIO_UNREGISTER | \
  42. (__u64)1 << _UFFDIO_API)
  43. #define UFFD_API_RANGE_IOCTLS \
  44. ((__u64)1 << _UFFDIO_WAKE | \
  45. (__u64)1 << _UFFDIO_COPY | \
  46. (__u64)1 << _UFFDIO_ZEROPAGE | \
  47. (__u64)1 << _UFFDIO_WRITEPROTECT | \
  48. (__u64)1 << _UFFDIO_CONTINUE)
  49. #define UFFD_API_RANGE_IOCTLS_BASIC \
  50. ((__u64)1 << _UFFDIO_WAKE | \
  51. (__u64)1 << _UFFDIO_COPY | \
  52. (__u64)1 << _UFFDIO_CONTINUE | \
  53. (__u64)1 << _UFFDIO_WRITEPROTECT)
  54. /*
  55. * Valid ioctl command number range with this API is from 0x00 to
  56. * 0x3F. UFFDIO_API is the fixed number, everything else can be
  57. * changed by implementing a different UFFD_API. If sticking to the
  58. * same UFFD_API more ioctl can be added and userland will be aware of
  59. * which ioctl the running kernel implements through the ioctl command
  60. * bitmask written by the UFFDIO_API.
  61. */
  62. #define _UFFDIO_REGISTER (0x00)
  63. #define _UFFDIO_UNREGISTER (0x01)
  64. #define _UFFDIO_WAKE (0x02)
  65. #define _UFFDIO_COPY (0x03)
  66. #define _UFFDIO_ZEROPAGE (0x04)
  67. #define _UFFDIO_WRITEPROTECT (0x06)
  68. #define _UFFDIO_CONTINUE (0x07)
  69. #define _UFFDIO_API (0x3F)
  70. /* userfaultfd ioctl ids */
  71. #define UFFDIO 0xAA
  72. #define UFFDIO_API _IOWR(UFFDIO, _UFFDIO_API, \
  73. struct uffdio_api)
  74. #define UFFDIO_REGISTER _IOWR(UFFDIO, _UFFDIO_REGISTER, \
  75. struct uffdio_register)
  76. #define UFFDIO_UNREGISTER _IOR(UFFDIO, _UFFDIO_UNREGISTER, \
  77. struct uffdio_range)
  78. #define UFFDIO_WAKE _IOR(UFFDIO, _UFFDIO_WAKE, \
  79. struct uffdio_range)
  80. #define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \
  81. struct uffdio_copy)
  82. #define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \
  83. struct uffdio_zeropage)
  84. #define UFFDIO_WRITEPROTECT _IOWR(UFFDIO, _UFFDIO_WRITEPROTECT, \
  85. struct uffdio_writeprotect)
  86. #define UFFDIO_CONTINUE _IOWR(UFFDIO, _UFFDIO_CONTINUE, \
  87. struct uffdio_continue)
  88. /* read() structure */
  89. struct uffd_msg {
  90. __u8 event;
  91. __u8 reserved1;
  92. __u16 reserved2;
  93. __u32 reserved3;
  94. union {
  95. struct {
  96. __u64 flags;
  97. __u64 address;
  98. union {
  99. __u32 ptid;
  100. } feat;
  101. } pagefault;
  102. struct {
  103. __u32 ufd;
  104. } fork;
  105. struct {
  106. __u64 from;
  107. __u64 to;
  108. __u64 len;
  109. } remap;
  110. struct {
  111. __u64 start;
  112. __u64 end;
  113. } remove;
  114. struct {
  115. /* unused reserved fields */
  116. __u64 reserved1;
  117. __u64 reserved2;
  118. __u64 reserved3;
  119. } reserved;
  120. } arg;
  121. } __attribute__((packed));
  122. /*
  123. * Start at 0x12 and not at 0 to be more strict against bugs.
  124. */
  125. #define UFFD_EVENT_PAGEFAULT 0x12
  126. #define UFFD_EVENT_FORK 0x13
  127. #define UFFD_EVENT_REMAP 0x14
  128. #define UFFD_EVENT_REMOVE 0x15
  129. #define UFFD_EVENT_UNMAP 0x16
  130. /* flags for UFFD_EVENT_PAGEFAULT */
  131. #define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */
  132. #define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */
  133. #define UFFD_PAGEFAULT_FLAG_MINOR (1<<2) /* If reason is VM_UFFD_MINOR */
  134. struct uffdio_api {
  135. /* userland asks for an API number and the features to enable */
  136. __u64 api;
  137. /*
  138. * Kernel answers below with the all available features for
  139. * the API, this notifies userland of which events and/or
  140. * which flags for each event are enabled in the current
  141. * kernel.
  142. *
  143. * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
  144. * are to be considered implicitly always enabled in all kernels as
  145. * long as the uffdio_api.api requested matches UFFD_API.
  146. *
  147. * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
  148. * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
  149. * hugetlbfs virtual memory ranges. Adding or not adding
  150. * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
  151. * no real functional effect after UFFDIO_API returns, but
  152. * it's only useful for an initial feature set probe at
  153. * UFFDIO_API time. There are two ways to use it:
  154. *
  155. * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
  156. * uffdio_api.features before calling UFFDIO_API, an error
  157. * will be returned by UFFDIO_API on a kernel without
  158. * hugetlbfs missing support
  159. *
  160. * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
  161. * uffdio_api.features and instead it will be set by the
  162. * kernel in the uffdio_api.features if the kernel supports
  163. * it, so userland can later check if the feature flag is
  164. * present in uffdio_api.features after UFFDIO_API
  165. * succeeded.
  166. *
  167. * UFFD_FEATURE_MISSING_SHMEM works the same as
  168. * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
  169. * (i.e. tmpfs and other shmem based APIs).
  170. *
  171. * UFFD_FEATURE_SIGBUS feature means no page-fault
  172. * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead
  173. * a SIGBUS signal will be sent to the faulting process.
  174. *
  175. * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will
  176. * be returned, if feature is not requested 0 will be returned.
  177. *
  178. * UFFD_FEATURE_MINOR_HUGETLBFS indicates that minor faults
  179. * can be intercepted (via REGISTER_MODE_MINOR) for
  180. * hugetlbfs-backed pages.
  181. *
  182. * UFFD_FEATURE_MINOR_SHMEM indicates the same support as
  183. * UFFD_FEATURE_MINOR_HUGETLBFS, but for shmem-backed pages instead.
  184. *
  185. * UFFD_FEATURE_EXACT_ADDRESS indicates that the exact address of page
  186. * faults would be provided and the offset within the page would not be
  187. * masked.
  188. *
  189. * UFFD_FEATURE_WP_HUGETLBFS_SHMEM indicates that userfaultfd
  190. * write-protection mode is supported on both shmem and hugetlbfs.
  191. *
  192. * UFFD_FEATURE_WP_UNPOPULATED indicates that userfaultfd
  193. * write-protection mode will always apply to unpopulated pages
  194. * (i.e. empty ptes). This will be the default behavior for shmem
  195. * & hugetlbfs, so this flag only affects anonymous memory behavior
  196. * when userfault write-protection mode is registered.
  197. */
  198. #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0)
  199. #define UFFD_FEATURE_EVENT_FORK (1<<1)
  200. #define UFFD_FEATURE_EVENT_REMAP (1<<2)
  201. #define UFFD_FEATURE_EVENT_REMOVE (1<<3)
  202. #define UFFD_FEATURE_MISSING_HUGETLBFS (1<<4)
  203. #define UFFD_FEATURE_MISSING_SHMEM (1<<5)
  204. #define UFFD_FEATURE_EVENT_UNMAP (1<<6)
  205. #define UFFD_FEATURE_SIGBUS (1<<7)
  206. #define UFFD_FEATURE_THREAD_ID (1<<8)
  207. #define UFFD_FEATURE_MINOR_HUGETLBFS (1<<9)
  208. #define UFFD_FEATURE_MINOR_SHMEM (1<<10)
  209. #define UFFD_FEATURE_EXACT_ADDRESS (1<<11)
  210. #define UFFD_FEATURE_WP_HUGETLBFS_SHMEM (1<<12)
  211. #define UFFD_FEATURE_WP_UNPOPULATED (1<<13)
  212. __u64 features;
  213. __u64 ioctls;
  214. };
  215. struct uffdio_range {
  216. __u64 start;
  217. __u64 len;
  218. };
  219. struct uffdio_register {
  220. struct uffdio_range range;
  221. #define UFFDIO_REGISTER_MODE_MISSING ((__u64)1<<0)
  222. #define UFFDIO_REGISTER_MODE_WP ((__u64)1<<1)
  223. #define UFFDIO_REGISTER_MODE_MINOR ((__u64)1<<2)
  224. __u64 mode;
  225. /*
  226. * kernel answers which ioctl commands are available for the
  227. * range, keep at the end as the last 8 bytes aren't read.
  228. */
  229. __u64 ioctls;
  230. };
  231. struct uffdio_copy {
  232. __u64 dst;
  233. __u64 src;
  234. __u64 len;
  235. #define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0)
  236. /*
  237. * UFFDIO_COPY_MODE_WP will map the page write protected on
  238. * the fly. UFFDIO_COPY_MODE_WP is available only if the
  239. * write protected ioctl is implemented for the range
  240. * according to the uffdio_register.ioctls.
  241. */
  242. #define UFFDIO_COPY_MODE_WP ((__u64)1<<1)
  243. __u64 mode;
  244. /*
  245. * "copy" is written by the ioctl and must be at the end: the
  246. * copy_from_user will not read the last 8 bytes.
  247. */
  248. __s64 copy;
  249. };
  250. struct uffdio_zeropage {
  251. struct uffdio_range range;
  252. #define UFFDIO_ZEROPAGE_MODE_DONTWAKE ((__u64)1<<0)
  253. __u64 mode;
  254. /*
  255. * "zeropage" is written by the ioctl and must be at the end:
  256. * the copy_from_user will not read the last 8 bytes.
  257. */
  258. __s64 zeropage;
  259. };
  260. struct uffdio_writeprotect {
  261. struct uffdio_range range;
  262. /*
  263. * UFFDIO_WRITEPROTECT_MODE_WP: set the flag to write protect a range,
  264. * unset the flag to undo protection of a range which was previously
  265. * write protected.
  266. *
  267. * UFFDIO_WRITEPROTECT_MODE_DONTWAKE: set the flag to avoid waking up
  268. * any wait thread after the operation succeeds.
  269. *
  270. * NOTE: Write protecting a region (WP=1) is unrelated to page faults,
  271. * therefore DONTWAKE flag is meaningless with WP=1. Removing write
  272. * protection (WP=0) in response to a page fault wakes the faulting
  273. * task unless DONTWAKE is set.
  274. */
  275. #define UFFDIO_WRITEPROTECT_MODE_WP ((__u64)1<<0)
  276. #define UFFDIO_WRITEPROTECT_MODE_DONTWAKE ((__u64)1<<1)
  277. __u64 mode;
  278. };
  279. struct uffdio_continue {
  280. struct uffdio_range range;
  281. #define UFFDIO_CONTINUE_MODE_DONTWAKE ((__u64)1<<0)
  282. /*
  283. * UFFDIO_CONTINUE_MODE_WP will map the page write protected on
  284. * the fly. UFFDIO_CONTINUE_MODE_WP is available only if the
  285. * write protected ioctl is implemented for the range
  286. * according to the uffdio_register.ioctls.
  287. */
  288. #define UFFDIO_CONTINUE_MODE_WP ((__u64)1<<1)
  289. __u64 mode;
  290. /*
  291. * Fields below here are written by the ioctl and must be at the end:
  292. * the copy_from_user will not read past here.
  293. */
  294. __s64 mapped;
  295. };
  296. /*
  297. * Flags for the userfaultfd(2) system call itself.
  298. */
  299. /*
  300. * Create a userfaultfd that can handle page faults only in user mode.
  301. */
  302. #define UFFD_USER_MODE_ONLY 1
  303. #endif /* _LINUX_USERFAULTFD_H */