1 /* $NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $ */
4 * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. All advertising materials mentioning features or use of this software
15 * must display the following acknowledgement:
16 * This product includes software developed by Emmanuel Dreyfus
17 * 4. The name of the author may not be used to endorse or promote
18 * products derived from this software without specific prior written
21 * THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
25 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
34 #include <sys/cdefs.h>
35 __FBSDID("$FreeBSD$");
37 __KERNEL_RCSID(1, "$NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $");
40 #include "opt_compat.h"
41 #include "opt_kdtrace.h"
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/imgact.h>
46 #include <sys/kernel.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
53 #include <sys/queue.h>
54 #include <sys/sched.h>
60 #include <machine/../linux32/linux.h>
61 #include <machine/../linux32/linux32_proto.h>
63 #include <machine/../linux/linux.h>
64 #include <machine/../linux/linux_proto.h>
66 #include <compat/linux/linux_dtrace.h>
67 #include <compat/linux/linux_emul.h>
68 #include <compat/linux/linux_futex.h>
69 #include <compat/linux/linux_util.h>
72 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
74 /* Linuxulator-global DTrace probes */
75 LIN_SDT_PROBE_DECLARE(locks, emul_lock, locked);
76 LIN_SDT_PROBE_DECLARE(locks, emul_lock, unlock);
79 * Futex part for the special DTrace module "locks".
81 LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, locked, "struct mtx *");
82 LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, unlock, "struct mtx *");
87 LIN_SDT_PROBE_DEFINE1(futex, futex, create, "struct sx *");
88 LIN_SDT_PROBE_DEFINE1(futex, futex, destroy, "struct sx *");
91 * DTrace probes in this module.
93 LIN_SDT_PROBE_DEFINE2(futex, futex_put, entry, "struct futex *",
94 "struct waiting_proc *");
95 LIN_SDT_PROBE_DEFINE3(futex, futex_put, destroy, "uint32_t *", "uint32_t",
97 LIN_SDT_PROBE_DEFINE3(futex, futex_put, unlock, "uint32_t *", "uint32_t",
99 LIN_SDT_PROBE_DEFINE0(futex, futex_put, return);
100 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, entry, "uint32_t *", "struct futex **",
102 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, umtx_key_get_error, "int");
103 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, shared, "uint32_t *", "uint32_t",
105 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, null, "uint32_t *");
106 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, new, "uint32_t *", "uint32_t", "int");
107 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, return, "int");
108 LIN_SDT_PROBE_DEFINE3(futex, futex_get, entry, "uint32_t *",
109 "struct waiting_proc **", "struct futex **");
110 LIN_SDT_PROBE_DEFINE0(futex, futex_get, error);
111 LIN_SDT_PROBE_DEFINE1(futex, futex_get, return, "int");
112 LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, entry, "struct futex *",
113 "struct waiting_proc **", "int");
114 LIN_SDT_PROBE_DEFINE5(futex, futex_sleep, requeue_error, "int", "uint32_t *",
115 "struct waiting_proc *", "uint32_t *", "uint32_t");
116 LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, sleep_error, "int", "uint32_t *",
117 "struct waiting_proc *");
118 LIN_SDT_PROBE_DEFINE1(futex, futex_sleep, return, "int");
119 LIN_SDT_PROBE_DEFINE3(futex, futex_wake, entry, "struct futex *", "int",
121 LIN_SDT_PROBE_DEFINE3(futex, futex_wake, iterate, "uint32_t",
122 "struct waiting_proc *", "uint32_t");
123 LIN_SDT_PROBE_DEFINE1(futex, futex_wake, wakeup, "struct waiting_proc *");
124 LIN_SDT_PROBE_DEFINE1(futex, futex_wake, return, "int");
125 LIN_SDT_PROBE_DEFINE4(futex, futex_requeue, entry, "struct futex *", "int",
126 "struct futex *", "int");
127 LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, wakeup, "struct waiting_proc *");
128 LIN_SDT_PROBE_DEFINE3(futex, futex_requeue, requeue, "uint32_t *",
129 "struct waiting_proc *", "uint32_t");
130 LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, return, "int");
131 LIN_SDT_PROBE_DEFINE4(futex, futex_wait, entry, "struct futex *",
132 "struct waiting_proc **", "int", "uint32_t");
133 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, sleep_error, "int");
134 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, return, "int");
135 LIN_SDT_PROBE_DEFINE3(futex, futex_atomic_op, entry, "struct thread *",
137 LIN_SDT_PROBE_DEFINE4(futex, futex_atomic_op, decoded_op, "int", "int", "int",
139 LIN_SDT_PROBE_DEFINE0(futex, futex_atomic_op, missing_access_check);
140 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_op, "int");
141 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_cmp, "int");
142 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, return, "int");
143 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, entry, "struct thread *",
144 "struct linux_sys_futex_args *");
145 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_clockswitch);
146 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, itimerfix_error, "int");
147 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, copyin_error, "int");
148 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, invalid_cmp_requeue_use);
149 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wait, "uint32_t *",
150 "uint32_t", "uint32_t");
151 LIN_SDT_PROBE_DEFINE4(futex, linux_sys_futex, debug_wait_value_neq,
152 "uint32_t *", "uint32_t", "int", "uint32_t");
153 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wake, "uint32_t *",
154 "uint32_t", "uint32_t");
155 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_cmp_requeue, "uint32_t *",
156 "uint32_t", "uint32_t", "uint32_t *", "struct l_timespec *");
157 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, debug_cmp_requeue_value_neq,
159 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_wake_op, "uint32_t *",
160 "int", "uint32_t", "uint32_t *", "uint32_t");
161 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unhandled_efault);
162 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_lock_pi);
163 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_unlock_pi);
164 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_trylock_pi);
165 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, deprecated_requeue);
166 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_wait_requeue_pi);
167 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_cmp_requeue_pi);
168 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, unknown_operation, "int");
169 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, return, "int");
170 LIN_SDT_PROBE_DEFINE2(futex, linux_set_robust_list, entry, "struct thread *",
171 "struct linux_set_robust_list_args *");
172 LIN_SDT_PROBE_DEFINE0(futex, linux_set_robust_list, size_error);
173 LIN_SDT_PROBE_DEFINE1(futex, linux_set_robust_list, return, "int");
174 LIN_SDT_PROBE_DEFINE2(futex, linux_get_robust_list, entry, "struct thread *",
175 "struct linux_get_robust_list_args *");
176 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, copyout_error, "int");
177 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, return, "int");
178 LIN_SDT_PROBE_DEFINE3(futex, handle_futex_death, entry, "struct proc *",
179 "uint32_t *", "int");
180 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, copyin_error, "int");
181 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, return, "int");
182 LIN_SDT_PROBE_DEFINE3(futex, fetch_robust_entry, entry,
183 "struct linux_robust_list **", "struct linux_robust_list **", "int *");
184 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, copyin_error, "int");
185 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, return, "int");
186 LIN_SDT_PROBE_DEFINE1(futex, release_futexes, entry, "struct proc *");
187 LIN_SDT_PROBE_DEFINE1(futex, release_futexes, copyin_error, "int");
188 LIN_SDT_PROBE_DEFINE0(futex, release_futexes, return);
190 static MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
191 static MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futexes wp");
195 struct waiting_proc {
197 struct futex *wp_futex;
198 TAILQ_ENTRY(waiting_proc) wp_list;
203 uint32_t *f_uaddr; /* user-supplied value, for debug */
204 struct umtx_key f_key;
207 LIST_ENTRY(futex) f_list;
208 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
211 struct futex_list futex_list;
213 #define FUTEX_LOCK(f) sx_xlock(&(f)->f_lck)
214 #define FUTEX_UNLOCK(f) sx_xunlock(&(f)->f_lck)
215 #define FUTEX_INIT(f) do { \
216 sx_init_flags(&(f)->f_lck, "ftlk", \
218 LIN_SDT_PROBE1(futex, futex, create, \
221 #define FUTEX_DESTROY(f) do { \
222 LIN_SDT_PROBE1(futex, futex, destroy, \
224 sx_destroy(&(f)->f_lck); \
226 #define FUTEX_ASSERT_LOCKED(f) sx_assert(&(f)->f_lck, SA_XLOCKED)
228 struct mtx futex_mtx; /* protects the futex list */
229 #define FUTEXES_LOCK do { \
230 mtx_lock(&futex_mtx); \
231 LIN_SDT_PROBE1(locks, futex_mtx, \
232 locked, &futex_mtx); \
234 #define FUTEXES_UNLOCK do { \
235 LIN_SDT_PROBE1(locks, futex_mtx, \
236 unlock, &futex_mtx); \
237 mtx_unlock(&futex_mtx); \
240 /* flags for futex_get() */
241 #define FUTEX_CREATE_WP 0x1 /* create waiting_proc */
242 #define FUTEX_DONTCREATE 0x2 /* don't create futex if not exists */
243 #define FUTEX_DONTEXISTS 0x4 /* return EINVAL if futex exists */
244 #define FUTEX_SHARED 0x8 /* shared futex */
247 #define FUTEX_WP_REQUEUED 0x1 /* wp requeued - wp moved from wp_list
248 * of futex where thread sleep to wp_list
251 #define FUTEX_WP_REMOVED 0x2 /* wp is woken up and removed from futex
252 * wp_list to prevent double wakeup.
256 int futex_xchgl(int oparg, uint32_t *uaddr, int *oldval);
257 int futex_addl(int oparg, uint32_t *uaddr, int *oldval);
258 int futex_orl(int oparg, uint32_t *uaddr, int *oldval);
259 int futex_andl(int oparg, uint32_t *uaddr, int *oldval);
260 int futex_xorl(int oparg, uint32_t *uaddr, int *oldval);
263 futex_put(struct futex *f, struct waiting_proc *wp)
265 LIN_SDT_PROBE2(futex, futex_put, entry, f, wp);
267 FUTEX_ASSERT_LOCKED(f);
269 if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
270 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
271 free(wp, M_FUTEX_WP);
275 if (--f->f_refcount == 0) {
276 LIST_REMOVE(f, f_list);
280 LIN_SDT_PROBE3(futex, futex_put, destroy, f->f_uaddr,
281 f->f_refcount, f->f_key.shared);
282 LINUX_CTR3(sys_futex, "futex_put destroy uaddr %p ref %d "
283 "shared %d", f->f_uaddr, f->f_refcount, f->f_key.shared);
284 umtx_key_release(&f->f_key);
288 LIN_SDT_PROBE0(futex, futex_put, return);
292 LIN_SDT_PROBE3(futex, futex_put, unlock, f->f_uaddr, f->f_refcount,
294 LINUX_CTR3(sys_futex, "futex_put uaddr %p ref %d shared %d",
295 f->f_uaddr, f->f_refcount, f->f_key.shared);
299 LIN_SDT_PROBE0(futex, futex_put, return);
303 futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
305 struct futex *f, *tmpf;
309 LIN_SDT_PROBE3(futex, futex_get0, entry, uaddr, newf, flags);
313 error = umtx_key_get(uaddr, TYPE_FUTEX, (flags & FUTEX_SHARED) ?
314 AUTO_SHARE : THREAD_SHARE, &key);
316 LIN_SDT_PROBE1(futex, futex_get0, umtx_key_get_error, error);
317 LIN_SDT_PROBE1(futex, futex_get0, return, error);
322 LIST_FOREACH(f, &futex_list, f_list) {
323 if (umtx_key_match(&f->f_key, &key)) {
329 if (flags & FUTEX_DONTEXISTS) {
331 umtx_key_release(&key);
333 LIN_SDT_PROBE1(futex, futex_get0, return,
339 * Increment refcount of the found futex to
340 * prevent it from deallocation before FUTEX_LOCK()
344 umtx_key_release(&key);
348 LIN_SDT_PROBE3(futex, futex_get0, shared, uaddr,
349 f->f_refcount, f->f_key.shared);
350 LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d",
351 uaddr, f->f_refcount, f->f_key.shared);
353 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
358 if (flags & FUTEX_DONTCREATE) {
360 umtx_key_release(&key);
361 LIN_SDT_PROBE1(futex, futex_get0, null, uaddr);
362 LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
364 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
370 tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
371 tmpf->f_uaddr = uaddr;
373 tmpf->f_refcount = 1;
374 tmpf->f_bitset = FUTEX_BITSET_MATCH_ANY;
376 TAILQ_INIT(&tmpf->f_waiting_proc);
379 * Lock the new futex before an insert into the futex_list
380 * to prevent futex usage by other.
386 LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
389 LIN_SDT_PROBE3(futex, futex_get0, new, uaddr, tmpf->f_refcount,
391 LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d new",
392 uaddr, tmpf->f_refcount, tmpf->f_key.shared);
395 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
400 futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
405 LIN_SDT_PROBE3(futex, futex_get, entry, uaddr, wp, f);
407 if (flags & FUTEX_CREATE_WP) {
408 *wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
411 error = futex_get0(uaddr, f, flags);
413 LIN_SDT_PROBE0(futex, futex_get, error);
415 if (flags & FUTEX_CREATE_WP)
416 free(*wp, M_FUTEX_WP);
418 LIN_SDT_PROBE1(futex, futex_get, return, error);
421 if (flags & FUTEX_CREATE_WP) {
422 TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
423 (*wp)->wp_futex = *f;
426 LIN_SDT_PROBE1(futex, futex_get, return, error);
431 futex_sleep(struct futex *f, struct waiting_proc *wp, int timeout)
435 FUTEX_ASSERT_LOCKED(f);
436 LIN_SDT_PROBE3(futex, futex_sleep, entry, f, wp, timeout);
437 LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %d ref %d",
438 f->f_uaddr, wp, timeout, f->f_refcount);
439 error = sx_sleep(wp, &f->f_lck, PCATCH, "futex", timeout);
440 if (wp->wp_flags & FUTEX_WP_REQUEUED) {
441 KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
444 LIN_SDT_PROBE5(futex, futex_sleep, requeue_error, error,
445 f->f_uaddr, wp, wp->wp_futex->f_uaddr,
446 wp->wp_futex->f_refcount);
449 LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p wp"
450 " %p requeued uaddr %p ref %d",
451 error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
452 wp->wp_futex->f_refcount);
458 LIN_SDT_PROBE3(futex, futex_sleep, sleep_error, error,
461 LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
462 error, f->f_uaddr, wp);
467 LIN_SDT_PROBE1(futex, futex_sleep, return, error);
472 futex_wake(struct futex *f, int n, uint32_t bitset)
474 struct waiting_proc *wp, *wpt;
477 LIN_SDT_PROBE3(futex, futex_wake, entry, f, n, bitset);
480 LIN_SDT_PROBE1(futex, futex_wake, return, EINVAL);
484 FUTEX_ASSERT_LOCKED(f);
485 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
486 LIN_SDT_PROBE3(futex, futex_wake, iterate, f->f_uaddr, wp,
488 LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
489 f->f_uaddr, wp, f->f_refcount);
491 * Unless we find a matching bit in
492 * the bitset, continue searching.
494 if (!(wp->wp_futex->f_bitset & bitset))
497 wp->wp_flags |= FUTEX_WP_REMOVED;
498 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
499 LIN_SDT_PROBE1(futex, futex_wake, wakeup, wp);
505 LIN_SDT_PROBE1(futex, futex_wake, return, count);
510 futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
512 struct waiting_proc *wp, *wpt;
515 LIN_SDT_PROBE4(futex, futex_requeue, entry, f, n, f2, n2);
517 FUTEX_ASSERT_LOCKED(f);
518 FUTEX_ASSERT_LOCKED(f2);
520 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
522 LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
524 wp->wp_flags |= FUTEX_WP_REMOVED;
525 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
526 LIN_SDT_PROBE1(futex, futex_requeue, wakeup, wp);
529 LIN_SDT_PROBE3(futex, futex_requeue, requeue,
530 f->f_uaddr, wp, f2->f_uaddr);
531 LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
532 f->f_uaddr, wp, f2->f_uaddr);
533 wp->wp_flags |= FUTEX_WP_REQUEUED;
534 /* Move wp to wp_list of f2 futex */
535 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
536 TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
539 * Thread which sleeps on wp after waking should
540 * acquire f2 lock, so increment refcount of f2 to
541 * prevent it from premature deallocation.
552 LIN_SDT_PROBE1(futex, futex_requeue, return, count);
557 futex_wait(struct futex *f, struct waiting_proc *wp, int timeout_hz,
562 LIN_SDT_PROBE4(futex, futex_wait, entry, f, wp, timeout_hz, bitset);
565 LIN_SDT_PROBE1(futex, futex_wait, return, EINVAL);
569 f->f_bitset = bitset;
570 error = futex_sleep(f, wp, timeout_hz);
572 LIN_SDT_PROBE1(futex, futex_wait, sleep_error, error);
573 if (error == EWOULDBLOCK)
576 LIN_SDT_PROBE1(futex, futex_wait, return, error);
581 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
583 int op = (encoded_op >> 28) & 7;
584 int cmp = (encoded_op >> 24) & 15;
585 int oparg = (encoded_op << 8) >> 20;
586 int cmparg = (encoded_op << 20) >> 20;
589 LIN_SDT_PROBE3(futex, futex_atomic_op, entry, td, encoded_op, uaddr);
591 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
594 LIN_SDT_PROBE4(futex, futex_atomic_op, decoded_op, op, cmp, oparg,
597 /* XXX: Linux verifies access here and returns EFAULT */
598 LIN_SDT_PROBE0(futex, futex_atomic_op, missing_access_check);
602 ret = futex_xchgl(oparg, uaddr, &oldval);
605 ret = futex_addl(oparg, uaddr, &oldval);
608 ret = futex_orl(oparg, uaddr, &oldval);
611 ret = futex_andl(~oparg, uaddr, &oldval);
614 ret = futex_xorl(oparg, uaddr, &oldval);
617 LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_op, op);
623 LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
628 case FUTEX_OP_CMP_EQ:
629 ret = (oldval == cmparg);
631 case FUTEX_OP_CMP_NE:
632 ret = (oldval != cmparg);
634 case FUTEX_OP_CMP_LT:
635 ret = (oldval < cmparg);
637 case FUTEX_OP_CMP_GE:
638 ret = (oldval >= cmparg);
640 case FUTEX_OP_CMP_LE:
641 ret = (oldval <= cmparg);
643 case FUTEX_OP_CMP_GT:
644 ret = (oldval > cmparg);
647 LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_cmp, cmp);
651 LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
656 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
658 int clockrt, nrwake, op_ret, ret;
659 struct linux_emuldata *em;
660 struct waiting_proc *wp;
661 struct futex *f, *f2;
662 struct l_timespec timeout;
663 struct timeval utv, ctv;
668 LIN_SDT_PROBE2(futex, linux_sys_futex, entry, td, args);
670 if (args->op & LINUX_FUTEX_PRIVATE_FLAG) {
672 args->op &= ~LINUX_FUTEX_PRIVATE_FLAG;
674 flags = FUTEX_SHARED;
677 * Currently support for switching between CLOCK_MONOTONIC and
678 * CLOCK_REALTIME is not present. However Linux forbids the use of
679 * FUTEX_CLOCK_REALTIME with any op except FUTEX_WAIT_BITSET and
680 * FUTEX_WAIT_REQUEUE_PI.
682 clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME;
683 args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME;
684 if (clockrt && args->op != LINUX_FUTEX_WAIT_BITSET &&
685 args->op != LINUX_FUTEX_WAIT_REQUEUE_PI) {
686 LIN_SDT_PROBE0(futex, linux_sys_futex,
687 unimplemented_clockswitch);
688 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
696 case LINUX_FUTEX_WAIT:
697 args->val3 = FUTEX_BITSET_MATCH_ANY;
700 case LINUX_FUTEX_WAIT_BITSET:
701 LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wait, args->uaddr,
702 args->val, args->val3);
703 LINUX_CTR3(sys_futex, "WAIT uaddr %p val 0x%x bitset 0x%x",
704 args->uaddr, args->val, args->val3);
706 error = futex_get(args->uaddr, &wp, &f,
707 flags | FUTEX_CREATE_WP);
709 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
713 error = copyin(args->uaddr, &val, sizeof(val));
715 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
717 LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
721 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
724 if (val != args->val) {
725 LIN_SDT_PROBE4(futex, linux_sys_futex,
726 debug_wait_value_neq, args->uaddr, args->val, val,
728 LINUX_CTR3(sys_futex,
729 "WAIT uaddr %p val 0x%x != uval 0x%x",
730 args->uaddr, args->val, val);
733 LIN_SDT_PROBE1(futex, linux_sys_futex, return,
735 return (EWOULDBLOCK);
738 if (args->timeout != NULL) {
739 error = copyin(args->timeout, &timeout, sizeof(timeout));
741 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
743 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
747 TIMESPEC_TO_TIMEVAL(&utv, &timeout);
748 error = itimerfix(&utv);
750 LIN_SDT_PROBE1(futex, linux_sys_futex, itimerfix_error,
752 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
758 timevalsub(&utv, &ctv);
759 } else if (args->op == LINUX_FUTEX_WAIT_BITSET) {
761 timevalsub(&utv, &ctv);
765 timeout_hz = tvtohz(&utv);
769 error = futex_wait(f, wp, timeout_hz, args->val3);
772 case LINUX_FUTEX_WAKE:
773 args->val3 = FUTEX_BITSET_MATCH_ANY;
776 case LINUX_FUTEX_WAKE_BITSET:
777 LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wake, args->uaddr,
778 args->val, args->val3);
779 LINUX_CTR3(sys_futex, "WAKE uaddr %p nrwake 0x%x bitset 0x%x",
780 args->uaddr, args->val, args->val3);
782 error = futex_get(args->uaddr, NULL, &f,
783 flags | FUTEX_DONTCREATE);
785 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
790 td->td_retval[0] = 0;
792 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
795 td->td_retval[0] = futex_wake(f, args->val, args->val3);
799 case LINUX_FUTEX_CMP_REQUEUE:
800 LIN_SDT_PROBE5(futex, linux_sys_futex, debug_cmp_requeue,
801 args->uaddr, args->val, args->val3, args->uaddr2,
803 LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
804 "nrwake 0x%x uval 0x%x uaddr2 %p nrequeue 0x%x",
805 args->uaddr, args->val, args->val3, args->uaddr2,
809 * Linux allows this, we would not, it is an incorrect
810 * usage of declared ABI, so return EINVAL.
812 if (args->uaddr == args->uaddr2) {
813 LIN_SDT_PROBE0(futex, linux_sys_futex,
814 invalid_cmp_requeue_use);
815 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
819 error = futex_get(args->uaddr, NULL, &f, flags);
821 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
826 * To avoid deadlocks return EINVAL if second futex
827 * exists at this time.
829 * Glibc fall back to FUTEX_WAKE in case of any error
830 * returned by FUTEX_CMP_REQUEUE.
832 error = futex_get(args->uaddr2, NULL, &f2,
833 flags | FUTEX_DONTEXISTS);
837 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
840 error = copyin(args->uaddr, &val, sizeof(val));
842 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
844 LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
849 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
852 if (val != args->val3) {
853 LIN_SDT_PROBE2(futex, linux_sys_futex,
854 debug_cmp_requeue_value_neq, args->val, val);
855 LINUX_CTR2(sys_futex, "CMP_REQUEUE val 0x%x != uval 0x%x",
860 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EAGAIN);
864 nrwake = (int)(unsigned long)args->timeout;
865 td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
870 case LINUX_FUTEX_WAKE_OP:
871 LIN_SDT_PROBE5(futex, linux_sys_futex, debug_wake_op,
872 args->uaddr, args->op, args->val, args->uaddr2, args->val3);
873 LINUX_CTR5(sys_futex, "WAKE_OP "
874 "uaddr %p nrwake 0x%x uaddr2 %p op 0x%x nrwake2 0x%x",
875 args->uaddr, args->val, args->uaddr2, args->val3,
878 error = futex_get(args->uaddr, NULL, &f, flags);
880 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
884 if (args->uaddr != args->uaddr2)
885 error = futex_get(args->uaddr2, NULL, &f2, flags);
889 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
894 * This function returns positive number as results and
897 op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
899 LINUX_CTR2(sys_futex, "WAKE_OP atomic_op uaddr %p ret 0x%x",
900 args->uaddr, op_ret);
903 /* XXX: We don't handle the EFAULT yet. */
904 if (op_ret != -EFAULT) {
909 LIN_SDT_PROBE1(futex, linux_sys_futex, return,
913 LIN_SDT_PROBE0(futex, linux_sys_futex,
920 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EFAULT);
924 ret = futex_wake(f, args->val, args->val3);
928 nrwake = (int)(unsigned long)args->timeout;
931 op_ret += futex_wake(f2, nrwake, args->val3);
933 op_ret += futex_wake(f, nrwake, args->val3);
940 td->td_retval[0] = ret;
943 case LINUX_FUTEX_LOCK_PI:
944 /* not yet implemented */
947 "op LINUX_FUTEX_LOCK_PI not implemented\n");
948 LIN_SDT_PROBE0(futex, linux_sys_futex, unimplemented_lock_pi);
949 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
952 case LINUX_FUTEX_UNLOCK_PI:
953 /* not yet implemented */
956 "op LINUX_FUTEX_UNLOCK_PI not implemented\n");
957 LIN_SDT_PROBE0(futex, linux_sys_futex, unimplemented_unlock_pi);
958 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
961 case LINUX_FUTEX_TRYLOCK_PI:
962 /* not yet implemented */
965 "op LINUX_FUTEX_TRYLOCK_PI not implemented\n");
966 LIN_SDT_PROBE0(futex, linux_sys_futex,
967 unimplemented_trylock_pi);
968 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
971 case LINUX_FUTEX_REQUEUE:
974 * Glibc does not use this operation since version 2.3.3,
975 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
976 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
977 * FUTEX_REQUEUE returned EINVAL.
979 em = em_find(td->td_proc, EMUL_DONTLOCK);
980 if ((em->flags & LINUX_XDEPR_REQUEUEOP) == 0) {
983 "unsupported futex_requeue op\n");
984 em->flags |= LINUX_XDEPR_REQUEUEOP;
985 LIN_SDT_PROBE0(futex, linux_sys_futex,
989 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
992 case LINUX_FUTEX_WAIT_REQUEUE_PI:
993 /* not yet implemented */
996 "op FUTEX_WAIT_REQUEUE_PI not implemented\n");
997 LIN_SDT_PROBE0(futex, linux_sys_futex,
998 unimplemented_wait_requeue_pi);
999 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1002 case LINUX_FUTEX_CMP_REQUEUE_PI:
1003 /* not yet implemented */
1006 "op LINUX_FUTEX_CMP_REQUEUE_PI not implemented\n");
1007 LIN_SDT_PROBE0(futex, linux_sys_futex,
1008 unimplemented_cmp_requeue_pi);
1009 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1014 "linux_sys_futex: unknown op %d\n", args->op);
1015 LIN_SDT_PROBE1(futex, linux_sys_futex, unknown_operation,
1017 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1021 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
1026 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
1028 struct linux_emuldata *em;
1030 LIN_SDT_PROBE2(futex, linux_set_robust_list, entry, td, args);
1032 if (args->len != sizeof(struct linux_robust_list_head)) {
1033 LIN_SDT_PROBE0(futex, linux_set_robust_list, size_error);
1034 LIN_SDT_PROBE1(futex, linux_set_robust_list, return, EINVAL);
1038 em = em_find(td->td_proc, EMUL_DOLOCK);
1039 em->robust_futexes = args->head;
1040 EMUL_UNLOCK(&emul_lock);
1042 LIN_SDT_PROBE1(futex, linux_set_robust_list, return, 0);
1047 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
1049 struct linux_emuldata *em;
1050 struct linux_robust_list_head *head;
1051 l_size_t len = sizeof(struct linux_robust_list_head);
1054 LIN_SDT_PROBE2(futex, linux_get_robust_list, entry, td, args);
1057 em = em_find(td->td_proc, EMUL_DONTLOCK);
1058 head = em->robust_futexes;
1062 p = pfind(args->pid);
1064 LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1069 em = em_find(p, EMUL_DONTLOCK);
1071 if (priv_check(td, PRIV_CRED_SETUID) ||
1072 priv_check(td, PRIV_CRED_SETEUID) ||
1073 p_candebug(td, p)) {
1076 LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1080 head = em->robust_futexes;
1085 error = copyout(&len, args->len, sizeof(l_size_t));
1087 LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1089 LIN_SDT_PROBE1(futex, linux_get_robust_list, return, EFAULT);
1093 error = copyout(&head, args->head, sizeof(head));
1095 LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1099 LIN_SDT_PROBE1(futex, linux_get_robust_list, return, error);
1104 handle_futex_death(struct proc *p, uint32_t *uaddr, int pi)
1106 uint32_t uval, nval, mval;
1110 LIN_SDT_PROBE3(futex, handle_futex_death, entry, p, uaddr, pi);
1113 error = copyin(uaddr, &uval, 4);
1115 LIN_SDT_PROBE1(futex, handle_futex_death, copyin_error, error);
1116 LIN_SDT_PROBE1(futex, handle_futex_death, return, EFAULT);
1119 if ((uval & FUTEX_TID_MASK) == p->p_pid) {
1120 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1121 nval = casuword32(uaddr, uval, mval);
1124 LIN_SDT_PROBE1(futex, handle_futex_death, return,
1132 if (!pi && (uval & FUTEX_WAITERS)) {
1133 error = futex_get(uaddr, NULL, &f,
1134 FUTEX_DONTCREATE | FUTEX_SHARED);
1136 LIN_SDT_PROBE1(futex, handle_futex_death,
1141 futex_wake(f, 1, FUTEX_BITSET_MATCH_ANY);
1147 LIN_SDT_PROBE1(futex, handle_futex_death, return, 0);
1152 fetch_robust_entry(struct linux_robust_list **entry,
1153 struct linux_robust_list **head, int *pi)
1158 LIN_SDT_PROBE3(futex, fetch_robust_entry, entry, entry, head, pi);
1160 error = copyin((const void *)head, &uentry, sizeof(l_ulong));
1162 LIN_SDT_PROBE1(futex, fetch_robust_entry, copyin_error, error);
1163 LIN_SDT_PROBE1(futex, fetch_robust_entry, return, EFAULT);
1167 *entry = (void *)(uentry & ~1UL);
1170 LIN_SDT_PROBE1(futex, fetch_robust_entry, return, 0);
1174 /* This walks the list of robust futexes releasing them. */
1176 release_futexes(struct proc *p)
1178 struct linux_robust_list_head *head = NULL;
1179 struct linux_robust_list *entry, *next_entry, *pending;
1180 unsigned int limit = 2048, pi, next_pi, pip;
1181 struct linux_emuldata *em;
1182 l_long futex_offset;
1185 LIN_SDT_PROBE1(futex, release_futexes, entry, p);
1187 em = em_find(p, EMUL_DONTLOCK);
1188 head = em->robust_futexes;
1191 LIN_SDT_PROBE0(futex, release_futexes, return);
1195 if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi)) {
1196 LIN_SDT_PROBE0(futex, release_futexes, return);
1200 error = copyin(&head->futex_offset, &futex_offset,
1201 sizeof(futex_offset));
1203 LIN_SDT_PROBE1(futex, release_futexes, copyin_error, error);
1204 LIN_SDT_PROBE0(futex, release_futexes, return);
1208 if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip)) {
1209 LIN_SDT_PROBE0(futex, release_futexes, return);
1213 while (entry != &head->list) {
1214 rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
1216 if (entry != pending)
1217 if (handle_futex_death(p,
1218 (uint32_t *)((caddr_t)entry + futex_offset), pi)) {
1219 LIN_SDT_PROBE0(futex, release_futexes, return);
1223 LIN_SDT_PROBE0(futex, release_futexes, return);
1233 sched_relinquish(curthread);
1237 handle_futex_death(p, (uint32_t *)((caddr_t)pending + futex_offset), pip);
1239 LIN_SDT_PROBE0(futex, release_futexes, return);