1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright © International Business Machines Corp., 2009
4 * Copyright (C) 2015 Cyril Hrubis <chrubis@suse.cz>
5 *
6 * DESCRIPTION
7 * Glibc independent futex library for testing kernel functionality.
8 *
9 * AUTHOR
10 * Darren Hart <dvhltc@us.ibm.com>
11 */
12
13 #ifndef _FUTEXTEST_H
14 #define _FUTEXTEST_H
15
16 #include <unistd.h>
17 #include <sys/syscall.h>
18 #include <sys/types.h>
19 #include <linux/futex.h>
20 #include "lapi/futex.h"
21 #include "tst_timer.h"
22
23 #define FUTEX_INITIALIZER 0
24
25 #ifndef FUTEX_CMP_REQUEUE
26 # define FUTEX_CMP_REQUEUE 4
27 #endif
28 #ifndef FUTEX_WAKE_OP
29 # define FUTEX_WAKE_OP 5
30 #endif
31 #ifndef FUTEX_LOCK_PI
32 # define FUTEX_LOCK_PI 6
33 #endif
34 #ifndef FUTEX_UNLOCK_PI
35 # define FUTEX_UNLOCK_PI 7
36 #endif
37 #ifndef FUTEX_WAIT_BITSET
38 # define FUTEX_WAIT_BITSET 9
39 #endif
40 #ifndef FUTEX_WAKE_BITSET
41 # define FUTEX_WAKE_BITSET 10
42 #endif
43 #ifndef FUTEX_WAIT_REQUEUE_PI
44 # define FUTEX_WAIT_REQUEUE_PI 11
45 #endif
46 #ifndef FUTEX_CMP_REQUEUE_PI
47 # define FUTEX_CMP_REQUEUE_PI 12
48 #endif
49 #ifndef FUTEX_PRIVATE_FLAG
50 # define FUTEX_PRIVATE_FLAG 128
51 #endif
52 #ifndef FUTEX_WAIT_REQUEUE_PI_PRIVATE
53 # define FUTEX_WAIT_REQUEUE_PI_PRIVATE (FUTEX_WAIT_REQUEUE_PI | \
54 FUTEX_PRIVATE_FLAG)
55 #endif
56 #ifndef FUTEX_REQUEUE_PI_PRIVATE
57 # define FUTEX_CMP_REQUEUE_PI_PRIVATE (FUTEX_CMP_REQUEUE_PI | \
58 FUTEX_PRIVATE_FLAG)
59 #endif
60
61 #ifndef FUTEX_CLOCK_REALTIME
62 # define FUTEX_CLOCK_REALTIME 256
63 #endif
64
65 enum futex_fn_type {
66 FUTEX_FN_FUTEX,
67 FUTEX_FN_FUTEX64,
68 };
69
70 struct futex_test_variants {
71 enum futex_fn_type fntype;
72 enum tst_ts_type tstype;
73 int (*gettime)(clockid_t clk_id, void *ts);
74 char *desc;
75 };
76
futex_supported_by_kernel(enum futex_fn_type fntype)77 static inline void futex_supported_by_kernel(enum futex_fn_type fntype)
78 {
79 if (fntype != FUTEX_FN_FUTEX64)
80 return;
81
82 /* Check if the syscall is implemented on the platform */
83 TEST(sys_futex_time64(NULL, 0, 0, NULL, NULL, 0));
84 if (TST_RET == -1 && TST_ERR == ENOSYS)
85 tst_brk(TCONF, "Test not supported on kernel/platform");
86 }
87
88 /**
89 * futex_syscall() - futex syscall wrapper
90 * @fntype: Futex function type
91 * @uaddr: address of first futex
92 * @op: futex op code
93 * @val: typically expected value of uaddr, but varies by op
94 * @timeout: typically an absolute struct tst_ts (except where noted
95 * otherwise). Overloaded by some ops
96 * @uaddr2: address of second futex for some ops\
97 * @val3: varies by op
98 * @opflags: flags to be bitwise OR'd with op, such as FUTEX_PRIVATE_FLAG
99 *
100 * futex_syscall() is used by all the following futex op wrappers. It can also be
101 * used for misuse and abuse testing. Generally, the specific op wrappers
102 * should be used instead. It is a macro instead of an static inline function as
103 * some of the types over overloaded (timeout is used for nr_requeue for
104 * example).
105 *
106 * These argument descriptions are the defaults for all
107 * like-named arguments in the following wrappers except where noted below.
108 */
futex_syscall(enum futex_fn_type fntype,futex_t * uaddr,int futex_op,futex_t val,void * timeout,futex_t * uaddr2,int val3,int opflags)109 static inline int futex_syscall(enum futex_fn_type fntype, futex_t *uaddr,
110 int futex_op, futex_t val, void *timeout,
111 futex_t *uaddr2, int val3, int opflags)
112 {
113 int (*func)(int *uaddr, int futex_op, int val, void *to, int *uaddr2, int val3);
114
115 if (fntype == FUTEX_FN_FUTEX)
116 func = sys_futex;
117 else
118 func = sys_futex_time64;
119
120 return func((int *)uaddr, futex_op | opflags, val, timeout, (int *)uaddr2, val3);
121 }
122
123 /**
124 * futex_wait() - block on uaddr with optional timeout
125 * @timeout: relative timeout
126 */
127 static inline int
futex_wait(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,struct tst_ts * timeout,int opflags)128 futex_wait(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
129 struct tst_ts *timeout, int opflags)
130 {
131 return futex_syscall(fntype, uaddr, FUTEX_WAIT, val,
132 tst_ts_get(timeout), NULL, 0, opflags);
133 }
134
135 /**
136 * futex_wake() - wake one or more tasks blocked on uaddr
137 * @nr_wake: wake up to this many tasks
138 */
139 static inline int
futex_wake(enum futex_fn_type fntype,futex_t * uaddr,int nr_wake,int opflags)140 futex_wake(enum futex_fn_type fntype, futex_t *uaddr, int nr_wake, int opflags)
141 {
142 return futex_syscall(fntype, uaddr, FUTEX_WAKE, nr_wake, NULL, NULL, 0,
143 opflags);
144 }
145
146 /**
147 * futex_wait_bitset() - block on uaddr with bitset
148 * @bitset: bitset to be used with futex_wake_bitset
149 */
150 static inline int
futex_wait_bitset(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,struct tst_ts * timeout,u_int32_t bitset,int opflags)151 futex_wait_bitset(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
152 struct tst_ts *timeout, u_int32_t bitset, int opflags)
153 {
154 return futex_syscall(fntype, uaddr, FUTEX_WAIT_BITSET, val,
155 tst_ts_get(timeout), NULL, bitset, opflags);
156 }
157
158 /**
159 * futex_wake_bitset() - wake one or more tasks blocked on uaddr with bitset
160 * @bitset: bitset to compare with that used in futex_wait_bitset
161 */
162 static inline int
futex_wake_bitset(enum futex_fn_type fntype,futex_t * uaddr,int nr_wake,u_int32_t bitset,int opflags)163 futex_wake_bitset(enum futex_fn_type fntype, futex_t *uaddr, int nr_wake,
164 u_int32_t bitset, int opflags)
165 {
166 return futex_syscall(fntype, uaddr, FUTEX_WAKE_BITSET, nr_wake, NULL,
167 NULL, bitset, opflags);
168 }
169
170 /**
171 * futex_lock_pi() - block on uaddr as a PI mutex
172 * @detect: whether (1) or not (0) to perform deadlock detection
173 */
174 static inline int
futex_lock_pi(enum futex_fn_type fntype,futex_t * uaddr,struct tst_ts * timeout,int detect,int opflags)175 futex_lock_pi(enum futex_fn_type fntype, futex_t *uaddr, struct tst_ts *timeout,
176 int detect, int opflags)
177 {
178 return futex_syscall(fntype, uaddr, FUTEX_LOCK_PI, detect,
179 tst_ts_get(timeout), NULL, 0, opflags);
180 }
181
182 /**
183 * futex_unlock_pi() - release uaddr as a PI mutex, waking the top waiter
184 */
185 static inline int
futex_unlock_pi(enum futex_fn_type fntype,futex_t * uaddr,int opflags)186 futex_unlock_pi(enum futex_fn_type fntype, futex_t *uaddr, int opflags)
187 {
188 return futex_syscall(fntype, uaddr, FUTEX_UNLOCK_PI, 0, NULL, NULL, 0,
189 opflags); }
190
191 /**
192 * futex_wake_op() - FIXME: COME UP WITH A GOOD ONE LINE DESCRIPTION
193 */
194 static inline int
futex_wake_op(enum futex_fn_type fntype,futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_wake2,int wake_op,int opflags)195 futex_wake_op(enum futex_fn_type fntype, futex_t *uaddr, futex_t *uaddr2,
196 int nr_wake, int nr_wake2, int wake_op, int opflags)
197 {
198 return futex_syscall(fntype, uaddr, FUTEX_WAKE_OP, nr_wake,
199 (void *)((unsigned long)nr_wake2), uaddr2, wake_op,
200 opflags);
201 }
202
203 /**
204 * futex_requeue() - requeue without expected value comparison, deprecated
205 * @nr_wake: wake up to this many tasks
206 * @nr_requeue: requeue up to this many tasks
207 *
208 * Due to its inherently racy implementation, futex_requeue() is deprecated in
209 * favor of futex_cmp_requeue().
210 */
211 static inline int
futex_requeue(enum futex_fn_type fntype,futex_t * uaddr,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)212 futex_requeue(enum futex_fn_type fntype, futex_t *uaddr, futex_t *uaddr2,
213 int nr_wake, int nr_requeue, int opflags)
214 {
215 return futex_syscall(fntype, uaddr, FUTEX_REQUEUE, nr_wake,
216 (void *)((unsigned long)nr_requeue), uaddr2, 0,
217 opflags);
218 }
219
220 /**
221 * futex_cmp_requeue() - requeue tasks from uaddr to uaddr2
222 * @nr_wake: wake up to this many tasks
223 * @nr_requeue: requeue up to this many tasks
224 */
225 static inline int
futex_cmp_requeue(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)226 futex_cmp_requeue(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
227 futex_t *uaddr2, int nr_wake, int nr_requeue, int opflags)
228 {
229 return futex_syscall(fntype, uaddr, FUTEX_CMP_REQUEUE, nr_wake,
230 (void *)((unsigned long)nr_requeue), uaddr2, val,
231 opflags);
232 }
233
234 /**
235 * futex_wait_requeue_pi() - block on uaddr and prepare to requeue to uaddr2
236 * @uaddr: non-PI futex source
237 * @uaddr2: PI futex target
238 *
239 * This is the first half of the requeue_pi mechanism. It shall always be
240 * paired with futex_cmp_requeue_pi().
241 */
242 static inline int
futex_wait_requeue_pi(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,struct tst_ts * timeout,int opflags)243 futex_wait_requeue_pi(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
244 futex_t *uaddr2, struct tst_ts *timeout, int opflags)
245 {
246 return futex_syscall(fntype, uaddr, FUTEX_WAIT_REQUEUE_PI, val,
247 tst_ts_get(timeout), uaddr2, 0, opflags);
248 }
249
250 /**
251 * futex_cmp_requeue_pi() - requeue tasks from uaddr to uaddr2 (PI aware)
252 * @uaddr: non-PI futex source
253 * @uaddr2: PI futex target
254 * @nr_wake: wake up to this many tasks
255 * @nr_requeue: requeue up to this many tasks
256 */
257 static inline int
futex_cmp_requeue_pi(enum futex_fn_type fntype,futex_t * uaddr,futex_t val,futex_t * uaddr2,int nr_wake,int nr_requeue,int opflags)258 futex_cmp_requeue_pi(enum futex_fn_type fntype, futex_t *uaddr, futex_t val,
259 futex_t *uaddr2, int nr_wake, int nr_requeue, int opflags)
260 {
261 return futex_syscall(fntype, uaddr, FUTEX_CMP_REQUEUE_PI, nr_wake,
262 (void *)((unsigned long)nr_requeue), uaddr2, val,
263 opflags);
264 }
265
266 /**
267 * futex_cmpxchg() - atomic compare and exchange
268 * @uaddr: The address of the futex to be modified
269 * @oldval: The expected value of the futex
270 * @newval: The new value to try and assign the futex
271 *
272 * Implement cmpxchg using gcc atomic builtins.
273 * http://gcc.gnu.org/onlinedocs/gcc-4.1.0/gcc/Atomic-Builtins.html
274 *
275 * Return the old futex value.
276 */
277 static inline u_int32_t
futex_cmpxchg(futex_t * uaddr,u_int32_t oldval,u_int32_t newval)278 futex_cmpxchg(futex_t *uaddr, u_int32_t oldval, u_int32_t newval)
279 {
280 return __sync_val_compare_and_swap(uaddr, oldval, newval);
281 }
282
283 /**
284 * futex_dec() - atomic decrement of the futex value
285 * @uaddr: The address of the futex to be modified
286 *
287 * Return the new futex value.
288 */
289 static inline u_int32_t
futex_dec(futex_t * uaddr)290 futex_dec(futex_t *uaddr)
291 {
292 return __sync_sub_and_fetch(uaddr, 1);
293 }
294
295 /**
296 * futex_inc() - atomic increment of the futex value
297 * @uaddr: the address of the futex to be modified
298 *
299 * Return the new futex value.
300 */
301 static inline u_int32_t
futex_inc(futex_t * uaddr)302 futex_inc(futex_t *uaddr)
303 {
304 return __sync_add_and_fetch(uaddr, 1);
305 }
306
307 /**
308 * futex_set() - atomic decrement of the futex value
309 * @uaddr: the address of the futex to be modified
310 * @newval: New value for the atomic_t
311 *
312 * Return the new futex value.
313 */
314 static inline u_int32_t
futex_set(futex_t * uaddr,u_int32_t newval)315 futex_set(futex_t *uaddr, u_int32_t newval)
316 {
317 *uaddr = newval;
318 return newval;
319 }
320
321 #endif
322