userfaultfd.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. /*
  13. * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and
  14. * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR. In
  15. * userfaultfd.h we assumed the kernel was reading (instead _IOC_READ
  16. * means the userland is reading).
  17. */
  18. #define UFFD_API ((__u64)0xAA)
  19. #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \
  20. UFFD_FEATURE_EVENT_FORK | \
  21. UFFD_FEATURE_EVENT_REMAP | \
  22. UFFD_FEATURE_EVENT_REMOVE | \
  23. UFFD_FEATURE_EVENT_UNMAP | \
  24. UFFD_FEATURE_MISSING_HUGETLBFS | \
  25. UFFD_FEATURE_MISSING_SHMEM | \
  26. UFFD_FEATURE_SIGBUS | \
  27. UFFD_FEATURE_THREAD_ID)
  28. #define UFFD_API_IOCTLS \
  29. ((__u64)1 << _UFFDIO_REGISTER | \
  30. (__u64)1 << _UFFDIO_UNREGISTER | \
  31. (__u64)1 << _UFFDIO_API)
  32. #define UFFD_API_RANGE_IOCTLS \
  33. ((__u64)1 << _UFFDIO_WAKE | \
  34. (__u64)1 << _UFFDIO_COPY | \
  35. (__u64)1 << _UFFDIO_ZEROPAGE | \
  36. (__u64)1 << _UFFDIO_WRITEPROTECT)
  37. #define UFFD_API_RANGE_IOCTLS_BASIC \
  38. ((__u64)1 << _UFFDIO_WAKE | \
  39. (__u64)1 << _UFFDIO_COPY)
  40. /*
  41. * Valid ioctl command number range with this API is from 0x00 to
  42. * 0x3F. UFFDIO_API is the fixed number, everything else can be
  43. * changed by implementing a different UFFD_API. If sticking to the
  44. * same UFFD_API more ioctl can be added and userland will be aware of
  45. * which ioctl the running kernel implements through the ioctl command
  46. * bitmask written by the UFFDIO_API.
  47. */
  48. #define _UFFDIO_REGISTER (0x00)
  49. #define _UFFDIO_UNREGISTER (0x01)
  50. #define _UFFDIO_WAKE (0x02)
  51. #define _UFFDIO_COPY (0x03)
  52. #define _UFFDIO_ZEROPAGE (0x04)
  53. #define _UFFDIO_WRITEPROTECT (0x06)
  54. #define _UFFDIO_API (0x3F)
  55. /* userfaultfd ioctl ids */
  56. #define UFFDIO 0xAA
  57. #define UFFDIO_API _IOWR(UFFDIO, _UFFDIO_API, \
  58. struct uffdio_api)
  59. #define UFFDIO_REGISTER _IOWR(UFFDIO, _UFFDIO_REGISTER, \
  60. struct uffdio_register)
  61. #define UFFDIO_UNREGISTER _IOR(UFFDIO, _UFFDIO_UNREGISTER, \
  62. struct uffdio_range)
  63. #define UFFDIO_WAKE _IOR(UFFDIO, _UFFDIO_WAKE, \
  64. struct uffdio_range)
  65. #define UFFDIO_COPY _IOWR(UFFDIO, _UFFDIO_COPY, \
  66. struct uffdio_copy)
  67. #define UFFDIO_ZEROPAGE _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \
  68. struct uffdio_zeropage)
  69. #define UFFDIO_WRITEPROTECT _IOWR(UFFDIO, _UFFDIO_WRITEPROTECT, \
  70. struct uffdio_writeprotect)
  71. /* read() structure */
  72. struct uffd_msg {
  73. __u8 event;
  74. __u8 reserved1;
  75. __u16 reserved2;
  76. __u32 reserved3;
  77. union {
  78. struct {
  79. __u64 flags;
  80. __u64 address;
  81. union {
  82. __u32 ptid;
  83. } feat;
  84. } pagefault;
  85. struct {
  86. __u32 ufd;
  87. } fork;
  88. struct {
  89. __u64 from;
  90. __u64 to;
  91. __u64 len;
  92. } remap;
  93. struct {
  94. __u64 start;
  95. __u64 end;
  96. } remove;
  97. struct {
  98. /* unused reserved fields */
  99. __u64 reserved1;
  100. __u64 reserved2;
  101. __u64 reserved3;
  102. } reserved;
  103. } arg;
  104. } __attribute__((packed));
  105. /*
  106. * Start at 0x12 and not at 0 to be more strict against bugs.
  107. */
  108. #define UFFD_EVENT_PAGEFAULT 0x12
  109. #define UFFD_EVENT_FORK 0x13
  110. #define UFFD_EVENT_REMAP 0x14
  111. #define UFFD_EVENT_REMOVE 0x15
  112. #define UFFD_EVENT_UNMAP 0x16
  113. /* flags for UFFD_EVENT_PAGEFAULT */
  114. #define UFFD_PAGEFAULT_FLAG_WRITE (1<<0) /* If this was a write fault */
  115. #define UFFD_PAGEFAULT_FLAG_WP (1<<1) /* If reason is VM_UFFD_WP */
  116. struct uffdio_api {
  117. /* userland asks for an API number and the features to enable */
  118. __u64 api;
  119. /*
  120. * Kernel answers below with the all available features for
  121. * the API, this notifies userland of which events and/or
  122. * which flags for each event are enabled in the current
  123. * kernel.
  124. *
  125. * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
  126. * are to be considered implicitly always enabled in all kernels as
  127. * long as the uffdio_api.api requested matches UFFD_API.
  128. *
  129. * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
  130. * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
  131. * hugetlbfs virtual memory ranges. Adding or not adding
  132. * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
  133. * no real functional effect after UFFDIO_API returns, but
  134. * it's only useful for an initial feature set probe at
  135. * UFFDIO_API time. There are two ways to use it:
  136. *
  137. * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
  138. * uffdio_api.features before calling UFFDIO_API, an error
  139. * will be returned by UFFDIO_API on a kernel without
  140. * hugetlbfs missing support
  141. *
  142. * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
  143. * uffdio_api.features and instead it will be set by the
  144. * kernel in the uffdio_api.features if the kernel supports
  145. * it, so userland can later check if the feature flag is
  146. * present in uffdio_api.features after UFFDIO_API
  147. * succeeded.
  148. *
  149. * UFFD_FEATURE_MISSING_SHMEM works the same as
  150. * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
  151. * (i.e. tmpfs and other shmem based APIs).
  152. *
  153. * UFFD_FEATURE_SIGBUS feature means no page-fault
  154. * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead
  155. * a SIGBUS signal will be sent to the faulting process.
  156. *
  157. * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will
  158. * be returned, if feature is not requested 0 will be returned.
  159. */
  160. #define UFFD_FEATURE_PAGEFAULT_FLAG_WP (1<<0)
  161. #define UFFD_FEATURE_EVENT_FORK (1<<1)
  162. #define UFFD_FEATURE_EVENT_REMAP (1<<2)
  163. #define UFFD_FEATURE_EVENT_REMOVE (1<<3)
  164. #define UFFD_FEATURE_MISSING_HUGETLBFS (1<<4)
  165. #define UFFD_FEATURE_MISSING_SHMEM (1<<5)
  166. #define UFFD_FEATURE_EVENT_UNMAP (1<<6)
  167. #define UFFD_FEATURE_SIGBUS (1<<7)
  168. #define UFFD_FEATURE_THREAD_ID (1<<8)
  169. __u64 features;
  170. __u64 ioctls;
  171. };
  172. struct uffdio_range {
  173. __u64 start;
  174. __u64 len;
  175. };
  176. struct uffdio_register {
  177. struct uffdio_range range;
  178. #define UFFDIO_REGISTER_MODE_MISSING ((__u64)1<<0)
  179. #define UFFDIO_REGISTER_MODE_WP ((__u64)1<<1)
  180. __u64 mode;
  181. /*
  182. * kernel answers which ioctl commands are available for the
  183. * range, keep at the end as the last 8 bytes aren't read.
  184. */
  185. __u64 ioctls;
  186. };
  187. struct uffdio_copy {
  188. __u64 dst;
  189. __u64 src;
  190. __u64 len;
  191. #define UFFDIO_COPY_MODE_DONTWAKE ((__u64)1<<0)
  192. /*
  193. * UFFDIO_COPY_MODE_WP will map the page write protected on
  194. * the fly. UFFDIO_COPY_MODE_WP is available only if the
  195. * write protected ioctl is implemented for the range
  196. * according to the uffdio_register.ioctls.
  197. */
  198. #define UFFDIO_COPY_MODE_WP ((__u64)1<<1)
  199. __u64 mode;
  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. struct uffdio_zeropage {
  207. struct uffdio_range range;
  208. #define UFFDIO_ZEROPAGE_MODE_DONTWAKE ((__u64)1<<0)
  209. __u64 mode;
  210. /*
  211. * "zeropage" is written by the ioctl and must be at the end:
  212. * the copy_from_user will not read the last 8 bytes.
  213. */
  214. __s64 zeropage;
  215. };
  216. struct uffdio_writeprotect {
  217. struct uffdio_range range;
  218. /*
  219. * UFFDIO_WRITEPROTECT_MODE_WP: set the flag to write protect a range,
  220. * unset the flag to undo protection of a range which was previously
  221. * write protected.
  222. *
  223. * UFFDIO_WRITEPROTECT_MODE_DONTWAKE: set the flag to avoid waking up
  224. * any wait thread after the operation succeeds.
  225. *
  226. * NOTE: Write protecting a region (WP=1) is unrelated to page faults,
  227. * therefore DONTWAKE flag is meaningless with WP=1. Removing write
  228. * protection (WP=0) in response to a page fault wakes the faulting
  229. * task unless DONTWAKE is set.
  230. */
  231. #define UFFDIO_WRITEPROTECT_MODE_WP ((__u64)1<<0)
  232. #define UFFDIO_WRITEPROTECT_MODE_DONTWAKE ((__u64)1<<1)
  233. __u64 mode;
  234. };
  235. #endif /* _LINUX_USERFAULTFD_H */