xref: /aosp_15_r20/external/ltp/include/lapi/futex.h (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) 2015 Linux Test Project
4  * Copyright (C) 2021 SUSE LLC Andrea Cervesato <[email protected]>
5  */
6 
7 #ifndef LAPI_FUTEX_H__
8 #define LAPI_FUTEX_H__
9 
10 #include <stdint.h>
11 #include "config.h"
12 
13 typedef volatile uint32_t futex_t;
14 
15 #if !defined(SYS_futex) && defined(SYS_futex_time64)
16 #define SYS_futex SYS_futex_time64
17 #endif
18 
19 #ifdef HAVE_LINUX_FUTEX_H
20 # include <linux/futex.h>
21 #else
22 #include <unistd.h>
23 
24 #define FUTEX_WAIT		0
25 #define FUTEX_WAKE		1
26 #define FUTEX_FD		2
27 #define FUTEX_REQUEUE		3
28 #define FUTEX_CMP_REQUEUE	4
29 #define FUTEX_WAKE_OP		5
30 #define FUTEX_LOCK_PI		6
31 #define FUTEX_UNLOCK_PI		7
32 #define FUTEX_TRYLOCK_PI	8
33 #define FUTEX_WAIT_BITSET	9
34 #define FUTEX_WAKE_BITSET	10
35 #define FUTEX_WAIT_REQUEUE_PI	11
36 #define FUTEX_CMP_REQUEUE_PI	12
37 #define FUTEX_LOCK_PI2		13
38 
39 #define FUTEX_PRIVATE_FLAG	128
40 #define FUTEX_CLOCK_REALTIME	256
41 #define FUTEX_CMD_MASK		~(FUTEX_PRIVATE_FLAG | FUTEX_CLOCK_REALTIME)
42 
43 #define FUTEX_WAIT_PRIVATE	(FUTEX_WAIT | FUTEX_PRIVATE_FLAG)
44 #define FUTEX_WAKE_PRIVATE	(FUTEX_WAKE | FUTEX_PRIVATE_FLAG)
45 #define FUTEX_REQUEUE_PRIVATE	(FUTEX_REQUEUE | FUTEX_PRIVATE_FLAG)
46 #define FUTEX_CMP_REQUEUE_PRIVATE (FUTEX_CMP_REQUEUE | FUTEX_PRIVATE_FLAG)
47 #define FUTEX_WAKE_OP_PRIVATE	(FUTEX_WAKE_OP | FUTEX_PRIVATE_FLAG)
48 #define FUTEX_LOCK_PI_PRIVATE	(FUTEX_LOCK_PI | FUTEX_PRIVATE_FLAG)
49 #define FUTEX_LOCK_PI2_PRIVATE	(FUTEX_LOCK_PI2 | FUTEX_PRIVATE_FLAG)
50 #define FUTEX_UNLOCK_PI_PRIVATE	(FUTEX_UNLOCK_PI | FUTEX_PRIVATE_FLAG)
51 #define FUTEX_TRYLOCK_PI_PRIVATE (FUTEX_TRYLOCK_PI | FUTEX_PRIVATE_FLAG)
52 #define FUTEX_WAIT_BITSET_PRIVATE	(FUTEX_WAIT_BITSET | FUTEX_PRIVATE_FLAG)
53 #define FUTEX_WAKE_BITSET_PRIVATE	(FUTEX_WAKE_BITSET | FUTEX_PRIVATE_FLAG)
54 #define FUTEX_WAIT_REQUEUE_PI_PRIVATE	(FUTEX_WAIT_REQUEUE_PI | \
55 					 FUTEX_PRIVATE_FLAG)
56 #define FUTEX_CMP_REQUEUE_PI_PRIVATE	(FUTEX_CMP_REQUEUE_PI | \
57 					 FUTEX_PRIVATE_FLAG)
58 
59 /*
60  * Support for robust futexes: the kernel cleans up held futexes at
61  * thread exit time.
62  */
63 
64 /*
65  * Per-lock list entry - embedded in user-space locks, somewhere close
66  * to the futex field. (Note: user-space uses a double-linked list to
67  * achieve O(1) list add and remove, but the kernel only needs to know
68  * about the forward link)
69  *
70  * NOTE: this structure is part of the syscall ABI, and must not be
71  * changed.
72  */
73 struct robust_list {
74 	struct robust_list *next;
75 };
76 
77 /*
78  * Per-thread list head:
79  *
80  * NOTE: this structure is part of the syscall ABI, and must only be
81  * changed if the change is first communicated with the glibc folks.
82  * (When an incompatible change is done, we'll increase the structure
83  *  size, which glibc will detect)
84  */
85 struct robust_list_head {
86 	/*
87 	 * The head of the list. Points back to itself if empty:
88 	 */
89 	struct robust_list list;
90 
91 	/*
92 	 * This relative offset is set by user-space, it gives the kernel
93 	 * the relative position of the futex field to examine. This way
94 	 * we keep userspace flexible, to freely shape its data-structure,
95 	 * without hardcoding any particular offset into the kernel:
96 	 */
97 	long futex_offset;
98 
99 	/*
100 	 * The death of the thread may race with userspace setting
101 	 * up a lock's links. So to handle this race, userspace first
102 	 * sets this field to the address of the to-be-taken lock,
103 	 * then does the lock acquire, and then adds itself to the
104 	 * list, and then clears this field. Hence the kernel will
105 	 * always have full knowledge of all locks that the thread
106 	 * _might_ have taken. We check the owner TID in any case,
107 	 * so only truly owned locks will be handled.
108 	 */
109 	struct robust_list *list_op_pending;
110 };
111 
112 /*
113  * Are there any waiters for this robust futex:
114  */
115 #define FUTEX_WAITERS		0x80000000
116 
117 /*
118  * The kernel signals via this bit that a thread holding a futex
119  * has exited without unlocking the futex. The kernel also does
120  * a FUTEX_WAKE on such futexes, after setting the bit, to wake
121  * up any possible waiters:
122  */
123 #define FUTEX_OWNER_DIED	0x40000000
124 
125 /*
126  * The rest of the robust-futex field is for the TID:
127  */
128 #define FUTEX_TID_MASK		0x3fffffff
129 
130 /*
131  * This limit protects against a deliberately circular list.
132  * (Not worth introducing an rlimit for it)
133  */
134 #define ROBUST_LIST_LIMIT	2048
135 
136 /*
137  * bitset with all bits set for the FUTEX_xxx_BITSET OPs to request a
138  * match of any bit.
139  */
140 #define FUTEX_BITSET_MATCH_ANY	0xffffffff
141 
142 
143 #define FUTEX_OP_SET		0	/* *(int *)UADDR2 = OPARG; */
144 #define FUTEX_OP_ADD		1	/* *(int *)UADDR2 += OPARG; */
145 #define FUTEX_OP_OR		2	/* *(int *)UADDR2 |= OPARG; */
146 #define FUTEX_OP_ANDN		3	/* *(int *)UADDR2 &= ~OPARG; */
147 #define FUTEX_OP_XOR		4	/* *(int *)UADDR2 ^= OPARG; */
148 
149 #define FUTEX_OP_OPARG_SHIFT	8	/* Use (1 << OPARG) instead of OPARG.  */
150 
151 #define FUTEX_OP_CMP_EQ		0	/* if (oldval == CMPARG) wake */
152 #define FUTEX_OP_CMP_NE		1	/* if (oldval != CMPARG) wake */
153 #define FUTEX_OP_CMP_LT		2	/* if (oldval < CMPARG) wake */
154 #define FUTEX_OP_CMP_LE		3	/* if (oldval <= CMPARG) wake */
155 #define FUTEX_OP_CMP_GT		4	/* if (oldval > CMPARG) wake */
156 #define FUTEX_OP_CMP_GE		5	/* if (oldval >= CMPARG) wake */
157 
158 /* FUTEX_WAKE_OP will perform atomically
159    int oldval = *(int *)UADDR2;
160    *(int *)UADDR2 = oldval OP OPARG;
161    if (oldval CMP CMPARG)
162      wake UADDR2;  */
163 
164 #define FUTEX_OP(op, oparg, cmp, cmparg) \
165   (((op & 0xf) << 28) | ((cmp & 0xf) << 24)		\
166    | ((oparg & 0xfff) << 12) | (cmparg & 0xfff))
167 
168 #endif /* HAVE_LINUX_FUTEX_H */
169 
170 #ifndef HAVE_STRUCT_FUTEX_WAITV
171 /*
172  * Flags to specify the bit length of the futex word for futex2 syscalls.
173  * Currently, only 32 is supported.
174  */
175 #define FUTEX_32		2
176 
177 /*
178  * Max numbers of elements in a futex_waitv array
179  */
180 #define FUTEX_WAITV_MAX		128
181 
182 /**
183  * struct futex_waitv - A waiter for vectorized wait
184  * @val:	Expected value at uaddr
185  * @uaddr:	User address to wait on
186  * @flags:	Flags for this waiter
187  * @__reserved:	Reserved member to preserve data alignment. Should be 0.
188  */
189 struct futex_waitv {
190 	uint64_t val;
191 	uint64_t uaddr;
192 	uint32_t flags;
193 	uint32_t __reserved;
194 };
195 #endif /* HAVE_STRUCT_FUTEX_WAITV */
196 
197 #endif /* LAPI_FUTEX_H__ */
198