Back to home page

OSCL-LXR

 
 

    


0001 /* SPDX-License-Identifier: GPL-2.0 WITH Linux-syscall-note */
0002 #ifndef _UAPI_LINUX_FUTEX_H
0003 #define _UAPI_LINUX_FUTEX_H
0004 
0005 #include <linux/compiler.h>
0006 #include <linux/types.h>
0007 
0008 /* Second argument to futex syscall */
0009 
0010 
0011 #define FUTEX_WAIT      0
0012 #define FUTEX_WAKE      1
0013 #define FUTEX_FD        2
0014 #define FUTEX_REQUEUE       3
0015 #define FUTEX_CMP_REQUEUE   4
0016 #define FUTEX_WAKE_OP       5
0017 #define FUTEX_LOCK_PI       6
0018 #define FUTEX_UNLOCK_PI     7
0019 #define FUTEX_TRYLOCK_PI    8
0020 #define FUTEX_WAIT_BITSET   9
0021 #define FUTEX_WAKE_BITSET   10
0022 #define FUTEX_WAIT_REQUEUE_PI   11
0023 #define FUTEX_CMP_REQUEUE_PI    12
0024 #define FUTEX_LOCK_PI2      13
0025 
0026 #define FUTEX_PRIVATE_FLAG  128
0027 #define FUTEX_CLOCK_REALTIME    256
0028 #define FUTEX_CMD_MASK      ~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME)
0029 
0030 #define FUTEX_WAIT_PRIVATE  (FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
0031 #define FUTEX_WAKE_PRIVATE  (FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
0032 #define FUTEX_REQUEUE_PRIVATE   (FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
0033 #define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
0034 #define FUTEX_WAKE_OP_PRIVATE   (FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
0035 #define FUTEX_LOCK_PI_PRIVATE   (FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
0036 #define FUTEX_LOCK_PI2_PRIVATE  (FUTEX_LOCK_PI2 | FUTEX_PRIVATE_FLAG)
0037 #define FUTEX_UNLOCK_PI_PRIVATE (FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
0038 #define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
0039 #define FUTEX_WAIT_BITSET_PRIVATE   (FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG)
0040 #define FUTEX_WAKE_BITSET_PRIVATE   (FUTEX_WAKE_BITSET | FUTEX_PRIVATE_FLAG)
0041 #define FUTEX_WAIT_REQUEUE_PI_PRIVATE   (FUTEX_WAIT_REQUEUE_PI | \
0042                      FUTEX_PRIVATE_FLAG)
0043 #define FUTEX_CMP_REQUEUE_PI_PRIVATE    (FUTEX_CMP_REQUEUE_PI | \
0044                      FUTEX_PRIVATE_FLAG)
0045 
0046 /*
0047  * Flags to specify the bit length of the futex word for futex2 syscalls.
0048  * Currently, only 32 is supported.
0049  */
0050 #define FUTEX_32        2
0051 
0052 /*
0053  * Max numbers of elements in a futex_waitv array
0054  */
0055 #define FUTEX_WAITV_MAX     128
0056 
0057 /**
0058  * struct futex_waitv - A waiter for vectorized wait
0059  * @val:    Expected value at uaddr
0060  * @uaddr:  User address to wait on
0061  * @flags:  Flags for this waiter
0062  * @__reserved: Reserved member to preserve data alignment. Should be 0.
0063  */
0064 struct futex_waitv {
0065     __u64 val;
0066     __u64 uaddr;
0067     __u32 flags;
0068     __u32 __reserved;
0069 };
0070 
0071 /*
0072  * Support for robust futexes: the kernel cleans up held futexes at
0073  * thread exit time.
0074  */
0075 
0076 /*
0077  * Per-lock list entry - embedded in user-space locks, somewhere close
0078  * to the futex field. (Note: user-space uses a double-linked list to
0079  * achieve O(1) list add and remove, but the kernel only needs to know
0080  * about the forward link)
0081  *
0082  * NOTE: this structure is part of the syscall ABI, and must not be
0083  * changed.
0084  */
0085 struct robust_list {
0086     struct robust_list __user *next;
0087 };
0088 
0089 /*
0090  * Per-thread list head:
0091  *
0092  * NOTE: this structure is part of the syscall ABI, and must only be
0093  * changed if the change is first communicated with the glibc folks.
0094  * (When an incompatible change is done, we'll increase the structure
0095  *  size, which glibc will detect)
0096  */
0097 struct robust_list_head {
0098     /*
0099      * The head of the list. Points back to itself if empty:
0100      */
0101     struct robust_list list;
0102 
0103     /*
0104      * This relative offset is set by user-space, it gives the kernel
0105      * the relative position of the futex field to examine. This way
0106      * we keep userspace flexible, to freely shape its data-structure,
0107      * without hardcoding any particular offset into the kernel:
0108      */
0109     long futex_offset;
0110 
0111     /*
0112      * The death of the thread may race with userspace setting
0113      * up a lock's links. So to handle this race, userspace first
0114      * sets this field to the address of the to-be-taken lock,
0115      * then does the lock acquire, and then adds itself to the
0116      * list, and then clears this field. Hence the kernel will
0117      * always have full knowledge of all locks that the thread
0118      * _might_ have taken. We check the owner TID in any case,
0119      * so only truly owned locks will be handled.
0120      */
0121     struct robust_list __user *list_op_pending;
0122 };
0123 
0124 /*
0125  * Are there any waiters for this robust futex:
0126  */
0127 #define FUTEX_WAITERS       0x80000000
0128 
0129 /*
0130  * The kernel signals via this bit that a thread holding a futex
0131  * has exited without unlocking the futex. The kernel also does
0132  * a FUTEX_WAKE on such futexes, after setting the bit, to wake
0133  * up any possible waiters:
0134  */
0135 #define FUTEX_OWNER_DIED    0x40000000
0136 
0137 /*
0138  * The rest of the robust-futex field is for the TID:
0139  */
0140 #define FUTEX_TID_MASK      0x3fffffff
0141 
0142 /*
0143  * This limit protects against a deliberately circular list.
0144  * (Not worth introducing an rlimit for it)
0145  */
0146 #define ROBUST_LIST_LIMIT   2048
0147 
0148 /*
0149  * bitset with all bits set for the FUTEX_xxx_BITSET OPs to request a
0150  * match of any bit.
0151  */
0152 #define FUTEX_BITSET_MATCH_ANY  0xffffffff
0153 
0154 
0155 #define FUTEX_OP_SET        0   /* *(int *)UADDR2 = OPARG; */
0156 #define FUTEX_OP_ADD        1   /* *(int *)UADDR2 += OPARG; */
0157 #define FUTEX_OP_OR     2   /* *(int *)UADDR2 |= OPARG; */
0158 #define FUTEX_OP_ANDN       3   /* *(int *)UADDR2 &= ~OPARG; */
0159 #define FUTEX_OP_XOR        4   /* *(int *)UADDR2 ^= OPARG; */
0160 
0161 #define FUTEX_OP_OPARG_SHIFT    8   /* Use (1 << OPARG) instead of OPARG.  */
0162 
0163 #define FUTEX_OP_CMP_EQ     0   /* if (oldval == CMPARG) wake */
0164 #define FUTEX_OP_CMP_NE     1   /* if (oldval != CMPARG) wake */
0165 #define FUTEX_OP_CMP_LT     2   /* if (oldval < CMPARG) wake */
0166 #define FUTEX_OP_CMP_LE     3   /* if (oldval <= CMPARG) wake */
0167 #define FUTEX_OP_CMP_GT     4   /* if (oldval > CMPARG) wake */
0168 #define FUTEX_OP_CMP_GE     5   /* if (oldval >= CMPARG) wake */
0169 
0170 /* FUTEX_WAKE_OP will perform atomically
0171    int oldval = *(int *)UADDR2;
0172    *(int *)UADDR2 = oldval OP OPARG;
0173    if (oldval CMP CMPARG)
0174      wake UADDR2;  */
0175 
0176 #define FUTEX_OP(op, oparg, cmp, cmparg) \
0177   (((op & 0xf) << 28) | ((cmp & 0xf) << 24)     \
0178    | ((oparg & 0xfff) << 12) | (cmparg & 0xfff))
0179 
0180 #endif /* _UAPI_LINUX_FUTEX_H */