Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
0002 /*
0003  *  include/linux/userfaultfd.h
0004  *
0005  *  Copyright (C) 2007  Davide Libenzi <davidel@xmailserver.org>
0006  *  Copyright (C) 2015  Red Hat, Inc.
0007  *
0008  */
0009 
0010 #ifndef _LINUX_USERFAULTFD_H
0011 #define _LINUX_USERFAULTFD_H
0012 
0013 #include <linux/types.h>
0014 
0015 /*
0016  * If the UFFDIO_API is upgraded someday, the UFFDIO_UNREGISTER and
0017  * UFFDIO_WAKE ioctls should be defined as _IOW and not as _IOR.  In
0018  * userfaultfd.h we assumed the kernel was reading (instead _IOC_READ
0019  * means the userland is reading).
0020  */
0021 #define UFFD_API ((__u64)0xAA)
0022 #define UFFD_API_REGISTER_MODES (UFFDIO_REGISTER_MODE_MISSING | \
0023                  UFFDIO_REGISTER_MODE_WP |  \
0024                  UFFDIO_REGISTER_MODE_MINOR)
0025 #define UFFD_API_FEATURES (UFFD_FEATURE_PAGEFAULT_FLAG_WP | \
0026                UFFD_FEATURE_EVENT_FORK |        \
0027                UFFD_FEATURE_EVENT_REMAP |       \
0028                UFFD_FEATURE_EVENT_REMOVE |      \
0029                UFFD_FEATURE_EVENT_UNMAP |       \
0030                UFFD_FEATURE_MISSING_HUGETLBFS | \
0031                UFFD_FEATURE_MISSING_SHMEM |     \
0032                UFFD_FEATURE_SIGBUS |        \
0033                UFFD_FEATURE_THREAD_ID |     \
0034                UFFD_FEATURE_MINOR_HUGETLBFS |   \
0035                UFFD_FEATURE_MINOR_SHMEM |       \
0036                UFFD_FEATURE_EXACT_ADDRESS |     \
0037                UFFD_FEATURE_WP_HUGETLBFS_SHMEM)
0038 #define UFFD_API_IOCTLS             \
0039     ((__u64)1 << _UFFDIO_REGISTER |     \
0040      (__u64)1 << _UFFDIO_UNREGISTER |   \
0041      (__u64)1 << _UFFDIO_API)
0042 #define UFFD_API_RANGE_IOCTLS           \
0043     ((__u64)1 << _UFFDIO_WAKE |     \
0044      (__u64)1 << _UFFDIO_COPY |     \
0045      (__u64)1 << _UFFDIO_ZEROPAGE |     \
0046      (__u64)1 << _UFFDIO_WRITEPROTECT | \
0047      (__u64)1 << _UFFDIO_CONTINUE)
0048 #define UFFD_API_RANGE_IOCTLS_BASIC     \
0049     ((__u64)1 << _UFFDIO_WAKE |     \
0050      (__u64)1 << _UFFDIO_COPY |     \
0051      (__u64)1 << _UFFDIO_CONTINUE |     \
0052      (__u64)1 << _UFFDIO_WRITEPROTECT)
0053 
0054 /*
0055  * Valid ioctl command number range with this API is from 0x00 to
0056  * 0x3F.  UFFDIO_API is the fixed number, everything else can be
0057  * changed by implementing a different UFFD_API. If sticking to the
0058  * same UFFD_API more ioctl can be added and userland will be aware of
0059  * which ioctl the running kernel implements through the ioctl command
0060  * bitmask written by the UFFDIO_API.
0061  */
0062 #define _UFFDIO_REGISTER        (0x00)
0063 #define _UFFDIO_UNREGISTER      (0x01)
0064 #define _UFFDIO_WAKE            (0x02)
0065 #define _UFFDIO_COPY            (0x03)
0066 #define _UFFDIO_ZEROPAGE        (0x04)
0067 #define _UFFDIO_WRITEPROTECT        (0x06)
0068 #define _UFFDIO_CONTINUE        (0x07)
0069 #define _UFFDIO_API         (0x3F)
0070 
0071 /* userfaultfd ioctl ids */
0072 #define UFFDIO 0xAA
0073 #define UFFDIO_API      _IOWR(UFFDIO, _UFFDIO_API,  \
0074                       struct uffdio_api)
0075 #define UFFDIO_REGISTER     _IOWR(UFFDIO, _UFFDIO_REGISTER, \
0076                       struct uffdio_register)
0077 #define UFFDIO_UNREGISTER   _IOR(UFFDIO, _UFFDIO_UNREGISTER,    \
0078                      struct uffdio_range)
0079 #define UFFDIO_WAKE     _IOR(UFFDIO, _UFFDIO_WAKE,  \
0080                      struct uffdio_range)
0081 #define UFFDIO_COPY     _IOWR(UFFDIO, _UFFDIO_COPY, \
0082                       struct uffdio_copy)
0083 #define UFFDIO_ZEROPAGE     _IOWR(UFFDIO, _UFFDIO_ZEROPAGE, \
0084                       struct uffdio_zeropage)
0085 #define UFFDIO_WRITEPROTECT _IOWR(UFFDIO, _UFFDIO_WRITEPROTECT, \
0086                       struct uffdio_writeprotect)
0087 #define UFFDIO_CONTINUE     _IOWR(UFFDIO, _UFFDIO_CONTINUE, \
0088                       struct uffdio_continue)
0089 
0090 /* read() structure */
0091 struct uffd_msg {
0092     __u8    event;
0093 
0094     __u8    reserved1;
0095     __u16   reserved2;
0096     __u32   reserved3;
0097 
0098     union {
0099         struct {
0100             __u64   flags;
0101             __u64   address;
0102             union {
0103                 __u32 ptid;
0104             } feat;
0105         } pagefault;
0106 
0107         struct {
0108             __u32   ufd;
0109         } fork;
0110 
0111         struct {
0112             __u64   from;
0113             __u64   to;
0114             __u64   len;
0115         } remap;
0116 
0117         struct {
0118             __u64   start;
0119             __u64   end;
0120         } remove;
0121 
0122         struct {
0123             /* unused reserved fields */
0124             __u64   reserved1;
0125             __u64   reserved2;
0126             __u64   reserved3;
0127         } reserved;
0128     } arg;
0129 } __packed;
0130 
0131 /*
0132  * Start at 0x12 and not at 0 to be more strict against bugs.
0133  */
0134 #define UFFD_EVENT_PAGEFAULT    0x12
0135 #define UFFD_EVENT_FORK     0x13
0136 #define UFFD_EVENT_REMAP    0x14
0137 #define UFFD_EVENT_REMOVE   0x15
0138 #define UFFD_EVENT_UNMAP    0x16
0139 
0140 /* flags for UFFD_EVENT_PAGEFAULT */
0141 #define UFFD_PAGEFAULT_FLAG_WRITE   (1<<0)  /* If this was a write fault */
0142 #define UFFD_PAGEFAULT_FLAG_WP      (1<<1)  /* If reason is VM_UFFD_WP */
0143 #define UFFD_PAGEFAULT_FLAG_MINOR   (1<<2)  /* If reason is VM_UFFD_MINOR */
0144 
0145 struct uffdio_api {
0146     /* userland asks for an API number and the features to enable */
0147     __u64 api;
0148     /*
0149      * Kernel answers below with the all available features for
0150      * the API, this notifies userland of which events and/or
0151      * which flags for each event are enabled in the current
0152      * kernel.
0153      *
0154      * Note: UFFD_EVENT_PAGEFAULT and UFFD_PAGEFAULT_FLAG_WRITE
0155      * are to be considered implicitly always enabled in all kernels as
0156      * long as the uffdio_api.api requested matches UFFD_API.
0157      *
0158      * UFFD_FEATURE_MISSING_HUGETLBFS means an UFFDIO_REGISTER
0159      * with UFFDIO_REGISTER_MODE_MISSING mode will succeed on
0160      * hugetlbfs virtual memory ranges. Adding or not adding
0161      * UFFD_FEATURE_MISSING_HUGETLBFS to uffdio_api.features has
0162      * no real functional effect after UFFDIO_API returns, but
0163      * it's only useful for an initial feature set probe at
0164      * UFFDIO_API time. There are two ways to use it:
0165      *
0166      * 1) by adding UFFD_FEATURE_MISSING_HUGETLBFS to the
0167      *    uffdio_api.features before calling UFFDIO_API, an error
0168      *    will be returned by UFFDIO_API on a kernel without
0169      *    hugetlbfs missing support
0170      *
0171      * 2) the UFFD_FEATURE_MISSING_HUGETLBFS can not be added in
0172      *    uffdio_api.features and instead it will be set by the
0173      *    kernel in the uffdio_api.features if the kernel supports
0174      *    it, so userland can later check if the feature flag is
0175      *    present in uffdio_api.features after UFFDIO_API
0176      *    succeeded.
0177      *
0178      * UFFD_FEATURE_MISSING_SHMEM works the same as
0179      * UFFD_FEATURE_MISSING_HUGETLBFS, but it applies to shmem
0180      * (i.e. tmpfs and other shmem based APIs).
0181      *
0182      * UFFD_FEATURE_SIGBUS feature means no page-fault
0183      * (UFFD_EVENT_PAGEFAULT) event will be delivered, instead
0184      * a SIGBUS signal will be sent to the faulting process.
0185      *
0186      * UFFD_FEATURE_THREAD_ID pid of the page faulted task_struct will
0187      * be returned, if feature is not requested 0 will be returned.
0188      *
0189      * UFFD_FEATURE_MINOR_HUGETLBFS indicates that minor faults
0190      * can be intercepted (via REGISTER_MODE_MINOR) for
0191      * hugetlbfs-backed pages.
0192      *
0193      * UFFD_FEATURE_MINOR_SHMEM indicates the same support as
0194      * UFFD_FEATURE_MINOR_HUGETLBFS, but for shmem-backed pages instead.
0195      *
0196      * UFFD_FEATURE_EXACT_ADDRESS indicates that the exact address of page
0197      * faults would be provided and the offset within the page would not be
0198      * masked.
0199      *
0200      * UFFD_FEATURE_WP_HUGETLBFS_SHMEM indicates that userfaultfd
0201      * write-protection mode is supported on both shmem and hugetlbfs.
0202      */
0203 #define UFFD_FEATURE_PAGEFAULT_FLAG_WP      (1<<0)
0204 #define UFFD_FEATURE_EVENT_FORK         (1<<1)
0205 #define UFFD_FEATURE_EVENT_REMAP        (1<<2)
0206 #define UFFD_FEATURE_EVENT_REMOVE       (1<<3)
0207 #define UFFD_FEATURE_MISSING_HUGETLBFS      (1<<4)
0208 #define UFFD_FEATURE_MISSING_SHMEM      (1<<5)
0209 #define UFFD_FEATURE_EVENT_UNMAP        (1<<6)
0210 #define UFFD_FEATURE_SIGBUS         (1<<7)
0211 #define UFFD_FEATURE_THREAD_ID          (1<<8)
0212 #define UFFD_FEATURE_MINOR_HUGETLBFS        (1<<9)
0213 #define UFFD_FEATURE_MINOR_SHMEM        (1<<10)
0214 #define UFFD_FEATURE_EXACT_ADDRESS      (1<<11)
0215 #define UFFD_FEATURE_WP_HUGETLBFS_SHMEM     (1<<12)
0216     __u64 features;
0217 
0218     __u64 ioctls;
0219 };
0220 
0221 struct uffdio_range {
0222     __u64 start;
0223     __u64 len;
0224 };
0225 
0226 struct uffdio_register {
0227     struct uffdio_range range;
0228 #define UFFDIO_REGISTER_MODE_MISSING    ((__u64)1<<0)
0229 #define UFFDIO_REGISTER_MODE_WP     ((__u64)1<<1)
0230 #define UFFDIO_REGISTER_MODE_MINOR  ((__u64)1<<2)
0231     __u64 mode;
0232 
0233     /*
0234      * kernel answers which ioctl commands are available for the
0235      * range, keep at the end as the last 8 bytes aren't read.
0236      */
0237     __u64 ioctls;
0238 };
0239 
0240 struct uffdio_copy {
0241     __u64 dst;
0242     __u64 src;
0243     __u64 len;
0244 #define UFFDIO_COPY_MODE_DONTWAKE       ((__u64)1<<0)
0245     /*
0246      * UFFDIO_COPY_MODE_WP will map the page write protected on
0247      * the fly.  UFFDIO_COPY_MODE_WP is available only if the
0248      * write protected ioctl is implemented for the range
0249      * according to the uffdio_register.ioctls.
0250      */
0251 #define UFFDIO_COPY_MODE_WP         ((__u64)1<<1)
0252     __u64 mode;
0253 
0254     /*
0255      * "copy" is written by the ioctl and must be at the end: the
0256      * copy_from_user will not read the last 8 bytes.
0257      */
0258     __s64 copy;
0259 };
0260 
0261 struct uffdio_zeropage {
0262     struct uffdio_range range;
0263 #define UFFDIO_ZEROPAGE_MODE_DONTWAKE       ((__u64)1<<0)
0264     __u64 mode;
0265 
0266     /*
0267      * "zeropage" is written by the ioctl and must be at the end:
0268      * the copy_from_user will not read the last 8 bytes.
0269      */
0270     __s64 zeropage;
0271 };
0272 
0273 struct uffdio_writeprotect {
0274     struct uffdio_range range;
0275 /*
0276  * UFFDIO_WRITEPROTECT_MODE_WP: set the flag to write protect a range,
0277  * unset the flag to undo protection of a range which was previously
0278  * write protected.
0279  *
0280  * UFFDIO_WRITEPROTECT_MODE_DONTWAKE: set the flag to avoid waking up
0281  * any wait thread after the operation succeeds.
0282  *
0283  * NOTE: Write protecting a region (WP=1) is unrelated to page faults,
0284  * therefore DONTWAKE flag is meaningless with WP=1.  Removing write
0285  * protection (WP=0) in response to a page fault wakes the faulting
0286  * task unless DONTWAKE is set.
0287  */
0288 #define UFFDIO_WRITEPROTECT_MODE_WP     ((__u64)1<<0)
0289 #define UFFDIO_WRITEPROTECT_MODE_DONTWAKE   ((__u64)1<<1)
0290     __u64 mode;
0291 };
0292 
0293 struct uffdio_continue {
0294     struct uffdio_range range;
0295 #define UFFDIO_CONTINUE_MODE_DONTWAKE       ((__u64)1<<0)
0296     __u64 mode;
0297 
0298     /*
0299      * Fields below here are written by the ioctl and must be at the end:
0300      * the copy_from_user will not read past here.
0301      */
0302     __s64 mapped;
0303 };
0304 
0305 /*
0306  * Flags for the userfaultfd(2) system call itself.
0307  */
0308 
0309 /*
0310  * Create a userfaultfd that can handle page faults only in user mode.
0311  */
0312 #define UFFD_USER_MODE_ONLY 1
0313 
0314 #endif /* _LINUX_USERFAULTFD_H */