userfaultfd.h 6.6 KB

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