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 **", "struct l_timespec *", "uint32_t");
133 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, copyin_error, "int");
134 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, itimerfix_error, "int");
135 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, sleep_error, "int");
136 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, return, "int");
137 LIN_SDT_PROBE_DEFINE3(futex, futex_atomic_op, entry, "struct thread *",
139 LIN_SDT_PROBE_DEFINE4(futex, futex_atomic_op, decoded_op, "int", "int", "int",
141 LIN_SDT_PROBE_DEFINE0(futex, futex_atomic_op, missing_access_check);
142 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_op, "int");
143 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_cmp, "int");
144 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, return, "int");
145 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, entry, "struct thread *",
146 "struct linux_sys_futex_args *");
147 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_clockswitch);
148 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, copyin_error, "int");
149 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, invalid_cmp_requeue_use);
150 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wait, "uint32_t *",
151 "uint32_t", "uint32_t");
152 LIN_SDT_PROBE_DEFINE4(futex, linux_sys_futex, debug_wait_value_neq,
153 "uint32_t *", "uint32_t", "int", "uint32_t");
154 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wake, "uint32_t *",
155 "uint32_t", "uint32_t");
156 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_cmp_requeue, "uint32_t *",
157 "uint32_t", "uint32_t", "uint32_t *", "struct l_timespec *");
158 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, debug_cmp_requeue_value_neq,
160 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_wake_op, "uint32_t *",
161 "int", "uint32_t", "uint32_t *", "uint32_t");
162 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unhandled_efault);
163 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_lock_pi);
164 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_unlock_pi);
165 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_trylock_pi);
166 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, deprecated_requeue);
167 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_wait_requeue_pi);
168 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_cmp_requeue_pi);
169 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, unknown_operation, "int");
170 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, return, "int");
171 LIN_SDT_PROBE_DEFINE2(futex, linux_set_robust_list, entry, "struct thread *",
172 "struct linux_set_robust_list_args *");
173 LIN_SDT_PROBE_DEFINE0(futex, linux_set_robust_list, size_error);
174 LIN_SDT_PROBE_DEFINE1(futex, linux_set_robust_list, return, "int");
175 LIN_SDT_PROBE_DEFINE2(futex, linux_get_robust_list, entry, "struct thread *",
176 "struct linux_get_robust_list_args *");
177 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, copyout_error, "int");
178 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, return, "int");
179 LIN_SDT_PROBE_DEFINE3(futex, handle_futex_death, entry, "struct proc *",
180 "uint32_t *", "int");
181 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, copyin_error, "int");
182 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, return, "int");
183 LIN_SDT_PROBE_DEFINE3(futex, fetch_robust_entry, entry,
184 "struct linux_robust_list **", "struct linux_robust_list **", "int *");
185 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, copyin_error, "int");
186 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, return, "int");
187 LIN_SDT_PROBE_DEFINE1(futex, release_futexes, entry, "struct proc *");
188 LIN_SDT_PROBE_DEFINE1(futex, release_futexes, copyin_error, "int");
189 LIN_SDT_PROBE_DEFINE0(futex, release_futexes, return);
191 static MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
192 static MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futexes wp");
196 struct waiting_proc {
198 struct futex *wp_futex;
199 TAILQ_ENTRY(waiting_proc) wp_list;
204 uint32_t *f_uaddr; /* user-supplied value, for debug */
205 struct umtx_key f_key;
208 LIST_ENTRY(futex) f_list;
209 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
212 struct futex_list futex_list;
214 #define FUTEX_LOCK(f) sx_xlock(&(f)->f_lck)
215 #define FUTEX_UNLOCK(f) sx_xunlock(&(f)->f_lck)
216 #define FUTEX_INIT(f) do { \
217 sx_init_flags(&(f)->f_lck, "ftlk", \
219 LIN_SDT_PROBE1(futex, futex, create, \
222 #define FUTEX_DESTROY(f) do { \
223 LIN_SDT_PROBE1(futex, futex, destroy, \
225 sx_destroy(&(f)->f_lck); \
227 #define FUTEX_ASSERT_LOCKED(f) sx_assert(&(f)->f_lck, SA_XLOCKED)
229 struct mtx futex_mtx; /* protects the futex list */
230 #define FUTEXES_LOCK do { \
231 mtx_lock(&futex_mtx); \
232 LIN_SDT_PROBE1(locks, futex_mtx, \
233 locked, &futex_mtx); \
235 #define FUTEXES_UNLOCK do { \
236 LIN_SDT_PROBE1(locks, futex_mtx, \
237 unlock, &futex_mtx); \
238 mtx_unlock(&futex_mtx); \
241 /* flags for futex_get() */
242 #define FUTEX_CREATE_WP 0x1 /* create waiting_proc */
243 #define FUTEX_DONTCREATE 0x2 /* don't create futex if not exists */
244 #define FUTEX_DONTEXISTS 0x4 /* return EINVAL if futex exists */
245 #define FUTEX_SHARED 0x8 /* shared futex */
248 #define FUTEX_WP_REQUEUED 0x1 /* wp requeued - wp moved from wp_list
249 * of futex where thread sleep to wp_list
252 #define FUTEX_WP_REMOVED 0x2 /* wp is woken up and removed from futex
253 * wp_list to prevent double wakeup.
257 int futex_xchgl(int oparg, uint32_t *uaddr, int *oldval);
258 int futex_addl(int oparg, uint32_t *uaddr, int *oldval);
259 int futex_orl(int oparg, uint32_t *uaddr, int *oldval);
260 int futex_andl(int oparg, uint32_t *uaddr, int *oldval);
261 int futex_xorl(int oparg, uint32_t *uaddr, int *oldval);
264 futex_put(struct futex *f, struct waiting_proc *wp)
266 LIN_SDT_PROBE2(futex, futex_put, entry, f, wp);
268 FUTEX_ASSERT_LOCKED(f);
270 if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
271 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
272 free(wp, M_FUTEX_WP);
276 if (--f->f_refcount == 0) {
277 LIST_REMOVE(f, f_list);
281 LIN_SDT_PROBE3(futex, futex_put, destroy, f->f_uaddr,
282 f->f_refcount, f->f_key.shared);
283 LINUX_CTR3(sys_futex, "futex_put destroy uaddr %p ref %d "
284 "shared %d", f->f_uaddr, f->f_refcount, f->f_key.shared);
285 umtx_key_release(&f->f_key);
289 LIN_SDT_PROBE0(futex, futex_put, return);
293 LIN_SDT_PROBE3(futex, futex_put, unlock, f->f_uaddr, f->f_refcount,
295 LINUX_CTR3(sys_futex, "futex_put uaddr %p ref %d shared %d",
296 f->f_uaddr, f->f_refcount, f->f_key.shared);
300 LIN_SDT_PROBE0(futex, futex_put, return);
304 futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
306 struct futex *f, *tmpf;
310 LIN_SDT_PROBE3(futex, futex_get0, entry, uaddr, newf, flags);
314 error = umtx_key_get(uaddr, TYPE_FUTEX, (flags & FUTEX_SHARED) ?
315 AUTO_SHARE : THREAD_SHARE, &key);
317 LIN_SDT_PROBE1(futex, futex_get0, umtx_key_get_error, error);
318 LIN_SDT_PROBE1(futex, futex_get0, return, error);
323 LIST_FOREACH(f, &futex_list, f_list) {
324 if (umtx_key_match(&f->f_key, &key)) {
330 if (flags & FUTEX_DONTEXISTS) {
332 umtx_key_release(&key);
334 LIN_SDT_PROBE1(futex, futex_get0, return,
340 * Increment refcount of the found futex to
341 * prevent it from deallocation before FUTEX_LOCK()
345 umtx_key_release(&key);
349 LIN_SDT_PROBE3(futex, futex_get0, shared, uaddr,
350 f->f_refcount, f->f_key.shared);
351 LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d",
352 uaddr, f->f_refcount, f->f_key.shared);
354 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
359 if (flags & FUTEX_DONTCREATE) {
361 umtx_key_release(&key);
362 LIN_SDT_PROBE1(futex, futex_get0, null, uaddr);
363 LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
365 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
371 tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
372 tmpf->f_uaddr = uaddr;
374 tmpf->f_refcount = 1;
375 tmpf->f_bitset = FUTEX_BITSET_MATCH_ANY;
377 TAILQ_INIT(&tmpf->f_waiting_proc);
380 * Lock the new futex before an insert into the futex_list
381 * to prevent futex usage by other.
387 LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
390 LIN_SDT_PROBE3(futex, futex_get0, new, uaddr, tmpf->f_refcount,
392 LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d new",
393 uaddr, tmpf->f_refcount, tmpf->f_key.shared);
396 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
401 futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
406 LIN_SDT_PROBE3(futex, futex_get, entry, uaddr, wp, f);
408 if (flags & FUTEX_CREATE_WP) {
409 *wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
412 error = futex_get0(uaddr, f, flags);
414 LIN_SDT_PROBE0(futex, futex_get, error);
416 if (flags & FUTEX_CREATE_WP)
417 free(*wp, M_FUTEX_WP);
419 LIN_SDT_PROBE1(futex, futex_get, return, error);
422 if (flags & FUTEX_CREATE_WP) {
423 TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
424 (*wp)->wp_futex = *f;
427 LIN_SDT_PROBE1(futex, futex_get, return, error);
432 futex_sleep(struct futex *f, struct waiting_proc *wp, int timeout)
436 FUTEX_ASSERT_LOCKED(f);
437 LIN_SDT_PROBE3(futex, futex_sleep, entry, f, wp, timeout);
438 LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %d ref %d",
439 f->f_uaddr, wp, timeout, f->f_refcount);
440 error = sx_sleep(wp, &f->f_lck, PCATCH, "futex", timeout);
441 if (wp->wp_flags & FUTEX_WP_REQUEUED) {
442 KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
445 LIN_SDT_PROBE5(futex, futex_sleep, requeue_error, error,
446 f->f_uaddr, wp, wp->wp_futex->f_uaddr,
447 wp->wp_futex->f_refcount);
450 LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p wp"
451 " %p requeued uaddr %p ref %d",
452 error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
453 wp->wp_futex->f_refcount);
459 LIN_SDT_PROBE3(futex, futex_sleep, sleep_error, error,
462 LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
463 error, f->f_uaddr, wp);
468 LIN_SDT_PROBE1(futex, futex_sleep, return, error);
473 futex_wake(struct futex *f, int n, uint32_t bitset)
475 struct waiting_proc *wp, *wpt;
478 LIN_SDT_PROBE3(futex, futex_wake, entry, f, n, bitset);
481 LIN_SDT_PROBE1(futex, futex_wake, return, EINVAL);
485 FUTEX_ASSERT_LOCKED(f);
486 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
487 LIN_SDT_PROBE3(futex, futex_wake, iterate, f->f_uaddr, wp,
489 LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
490 f->f_uaddr, wp, f->f_refcount);
492 * Unless we find a matching bit in
493 * the bitset, continue searching.
495 if (!(wp->wp_futex->f_bitset & bitset))
498 wp->wp_flags |= FUTEX_WP_REMOVED;
499 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
500 LIN_SDT_PROBE1(futex, futex_wake, wakeup, wp);
506 LIN_SDT_PROBE1(futex, futex_wake, return, count);
511 futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
513 struct waiting_proc *wp, *wpt;
516 LIN_SDT_PROBE4(futex, futex_requeue, entry, f, n, f2, n2);
518 FUTEX_ASSERT_LOCKED(f);
519 FUTEX_ASSERT_LOCKED(f2);
521 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
523 LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
525 wp->wp_flags |= FUTEX_WP_REMOVED;
526 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
527 LIN_SDT_PROBE1(futex, futex_requeue, wakeup, wp);
530 LIN_SDT_PROBE3(futex, futex_requeue, requeue,
531 f->f_uaddr, wp, f2->f_uaddr);
532 LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
533 f->f_uaddr, wp, f2->f_uaddr);
534 wp->wp_flags |= FUTEX_WP_REQUEUED;
535 /* Move wp to wp_list of f2 futex */
536 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
537 TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
540 * Thread which sleeps on wp after waking should
541 * acquire f2 lock, so increment refcount of f2 to
542 * prevent it from premature deallocation.
553 LIN_SDT_PROBE1(futex, futex_requeue, return, count);
558 futex_wait(struct futex *f, struct waiting_proc *wp, struct l_timespec *ts,
561 struct l_timespec timeout;
566 LIN_SDT_PROBE4(futex, futex_wait, entry, f, wp, ts, bitset);
569 LIN_SDT_PROBE1(futex, futex_wait, return, EINVAL);
573 f->f_bitset = bitset;
576 error = copyin(ts, &timeout, sizeof(timeout));
578 LIN_SDT_PROBE1(futex, futex_wait, copyin_error, error);
579 LIN_SDT_PROBE1(futex, futex_wait, return, error);
582 TIMESPEC_TO_TIMEVAL(&tv, &timeout);
583 error = itimerfix(&tv);
585 LIN_SDT_PROBE1(futex, futex_wait, itimerfix_error,
587 LIN_SDT_PROBE1(futex, futex_wait, return, error);
590 timeout_hz = tvtohz(&tv);
594 error = futex_sleep(f, wp, timeout_hz);
596 LIN_SDT_PROBE1(futex, futex_wait, sleep_error, error);
598 if (error == EWOULDBLOCK)
601 LIN_SDT_PROBE1(futex, futex_wait, return, error);
606 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
608 int op = (encoded_op >> 28) & 7;
609 int cmp = (encoded_op >> 24) & 15;
610 int oparg = (encoded_op << 8) >> 20;
611 int cmparg = (encoded_op << 20) >> 20;
614 LIN_SDT_PROBE3(futex, futex_atomic_op, entry, td, encoded_op, uaddr);
616 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
619 LIN_SDT_PROBE4(futex, futex_atomic_op, decoded_op, op, cmp, oparg,
622 /* XXX: Linux verifies access here and returns EFAULT */
623 LIN_SDT_PROBE0(futex, futex_atomic_op, missing_access_check);
627 ret = futex_xchgl(oparg, uaddr, &oldval);
630 ret = futex_addl(oparg, uaddr, &oldval);
633 ret = futex_orl(oparg, uaddr, &oldval);
636 ret = futex_andl(~oparg, uaddr, &oldval);
639 ret = futex_xorl(oparg, uaddr, &oldval);
642 LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_op, op);
648 LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
653 case FUTEX_OP_CMP_EQ:
654 ret = (oldval == cmparg);
656 case FUTEX_OP_CMP_NE:
657 ret = (oldval != cmparg);
659 case FUTEX_OP_CMP_LT:
660 ret = (oldval < cmparg);
662 case FUTEX_OP_CMP_GE:
663 ret = (oldval >= cmparg);
665 case FUTEX_OP_CMP_LE:
666 ret = (oldval <= cmparg);
668 case FUTEX_OP_CMP_GT:
669 ret = (oldval > cmparg);
672 LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_cmp, cmp);
676 LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
681 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
683 int clockrt, nrwake, op_ret, ret, val;
684 struct linux_emuldata *em;
685 struct waiting_proc *wp;
686 struct futex *f, *f2;
690 LIN_SDT_PROBE2(futex, linux_sys_futex, entry, td, args);
692 if (args->op & LINUX_FUTEX_PRIVATE_FLAG) {
694 args->op &= ~LINUX_FUTEX_PRIVATE_FLAG;
696 flags = FUTEX_SHARED;
699 * Currently support for switching between CLOCK_MONOTONIC and
700 * CLOCK_REALTIME is not present. However Linux forbids the use of
701 * FUTEX_CLOCK_REALTIME with any op except FUTEX_WAIT_BITSET and
702 * FUTEX_WAIT_REQUEUE_PI.
704 clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME;
705 args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME;
706 if (clockrt && args->op != LINUX_FUTEX_WAIT_BITSET &&
707 args->op != LINUX_FUTEX_WAIT_REQUEUE_PI) {
708 LIN_SDT_PROBE0(futex, linux_sys_futex,
709 unimplemented_clockswitch);
710 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
718 case LINUX_FUTEX_WAIT:
719 args->val3 = FUTEX_BITSET_MATCH_ANY;
722 case LINUX_FUTEX_WAIT_BITSET:
723 LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wait, args->uaddr,
724 args->val, args->val3);
725 LINUX_CTR3(sys_futex, "WAIT uaddr %p val %d val3 %d",
726 args->uaddr, args->val, args->val3);
728 error = futex_get(args->uaddr, &wp, &f,
729 flags | FUTEX_CREATE_WP);
731 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
735 error = copyin(args->uaddr, &val, sizeof(val));
737 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
739 LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
743 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
746 if (val != args->val) {
747 LIN_SDT_PROBE4(futex, linux_sys_futex,
748 debug_wait_value_neq, args->uaddr, args->val, val,
750 LINUX_CTR4(sys_futex,
751 "WAIT uaddr %p val %d != uval %d val3 %d",
752 args->uaddr, args->val, val, args->val3);
755 LIN_SDT_PROBE1(futex, linux_sys_futex, return,
757 return (EWOULDBLOCK);
760 error = futex_wait(f, wp, args->timeout, args->val3);
763 case LINUX_FUTEX_WAKE:
764 args->val3 = FUTEX_BITSET_MATCH_ANY;
767 case LINUX_FUTEX_WAKE_BITSET:
768 LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wake, args->uaddr,
769 args->val, args->val3);
770 LINUX_CTR3(sys_futex, "WAKE uaddr %p val % d val3 %d",
771 args->uaddr, args->val, args->val3);
773 error = futex_get(args->uaddr, NULL, &f,
774 flags | FUTEX_DONTCREATE);
776 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
781 td->td_retval[0] = 0;
783 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
786 td->td_retval[0] = futex_wake(f, args->val, args->val3);
790 case LINUX_FUTEX_CMP_REQUEUE:
791 LIN_SDT_PROBE5(futex, linux_sys_futex, debug_cmp_requeue,
792 args->uaddr, args->val, args->val3, args->uaddr2,
794 LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
795 "val %d val3 %d uaddr2 %p val2 %d",
796 args->uaddr, args->val, args->val3, args->uaddr2,
797 (int)(unsigned long)args->timeout);
800 * Linux allows this, we would not, it is an incorrect
801 * usage of declared ABI, so return EINVAL.
803 if (args->uaddr == args->uaddr2) {
804 LIN_SDT_PROBE0(futex, linux_sys_futex,
805 invalid_cmp_requeue_use);
806 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
810 error = futex_get(args->uaddr, NULL, &f, flags);
812 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
817 * To avoid deadlocks return EINVAL if second futex
818 * exists at this time.
820 * Glibc fall back to FUTEX_WAKE in case of any error
821 * returned by FUTEX_CMP_REQUEUE.
823 error = futex_get(args->uaddr2, NULL, &f2,
824 flags | FUTEX_DONTEXISTS);
828 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
831 error = copyin(args->uaddr, &val, sizeof(val));
833 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
835 LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
840 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
843 if (val != args->val3) {
844 LIN_SDT_PROBE2(futex, linux_sys_futex,
845 debug_cmp_requeue_value_neq, args->val, val);
846 LINUX_CTR2(sys_futex, "CMP_REQUEUE val %d != uval %d",
851 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EAGAIN);
855 nrwake = (int)(unsigned long)args->timeout;
856 td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
861 case LINUX_FUTEX_WAKE_OP:
862 LIN_SDT_PROBE5(futex, linux_sys_futex, debug_wake_op,
863 args->uaddr, args->op, args->val, args->uaddr2, args->val3);
864 LINUX_CTR5(sys_futex, "WAKE_OP "
865 "uaddr %p op %d val %x uaddr2 %p val3 %x",
866 args->uaddr, args->op, args->val,
867 args->uaddr2, args->val3);
869 error = futex_get(args->uaddr, NULL, &f, flags);
871 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
875 if (args->uaddr != args->uaddr2)
876 error = futex_get(args->uaddr2, NULL, &f2, flags);
880 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
885 * This function returns positive number as results and
888 op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
891 /* XXX: We don't handle the EFAULT yet. */
892 if (op_ret != -EFAULT) {
897 LIN_SDT_PROBE1(futex, linux_sys_futex, return,
901 LIN_SDT_PROBE0(futex, linux_sys_futex,
908 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EFAULT);
912 ret = futex_wake(f, args->val, args->val3);
916 nrwake = (int)(unsigned long)args->timeout;
919 op_ret += futex_wake(f2, nrwake, args->val3);
921 op_ret += futex_wake(f, nrwake, args->val3);
928 td->td_retval[0] = ret;
931 case LINUX_FUTEX_LOCK_PI:
932 /* not yet implemented */
935 "op LINUX_FUTEX_LOCK_PI not implemented\n");
936 LIN_SDT_PROBE0(futex, linux_sys_futex, unimplemented_lock_pi);
937 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
940 case LINUX_FUTEX_UNLOCK_PI:
941 /* not yet implemented */
944 "op LINUX_FUTEX_UNLOCK_PI not implemented\n");
945 LIN_SDT_PROBE0(futex, linux_sys_futex, unimplemented_unlock_pi);
946 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
949 case LINUX_FUTEX_TRYLOCK_PI:
950 /* not yet implemented */
953 "op LINUX_FUTEX_TRYLOCK_PI not implemented\n");
954 LIN_SDT_PROBE0(futex, linux_sys_futex,
955 unimplemented_trylock_pi);
956 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
959 case LINUX_FUTEX_REQUEUE:
962 * Glibc does not use this operation since version 2.3.3,
963 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
964 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
965 * FUTEX_REQUEUE returned EINVAL.
967 em = em_find(td->td_proc, EMUL_DONTLOCK);
968 if ((em->flags & LINUX_XDEPR_REQUEUEOP) == 0) {
971 "unsupported futex_requeue op\n");
972 em->flags |= LINUX_XDEPR_REQUEUEOP;
973 LIN_SDT_PROBE0(futex, linux_sys_futex,
977 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
980 case LINUX_FUTEX_WAIT_REQUEUE_PI:
981 /* not yet implemented */
984 "op FUTEX_WAIT_REQUEUE_PI not implemented\n");
985 LIN_SDT_PROBE0(futex, linux_sys_futex,
986 unimplemented_wait_requeue_pi);
987 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
990 case LINUX_FUTEX_CMP_REQUEUE_PI:
991 /* not yet implemented */
994 "op LINUX_FUTEX_CMP_REQUEUE_PI not implemented\n");
995 LIN_SDT_PROBE0(futex, linux_sys_futex,
996 unimplemented_cmp_requeue_pi);
997 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1002 "linux_sys_futex: unknown op %d\n", args->op);
1003 LIN_SDT_PROBE1(futex, linux_sys_futex, unknown_operation,
1005 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1009 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
1014 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
1016 struct linux_emuldata *em;
1018 LIN_SDT_PROBE2(futex, linux_set_robust_list, entry, td, args);
1020 if (args->len != sizeof(struct linux_robust_list_head)) {
1021 LIN_SDT_PROBE0(futex, linux_set_robust_list, size_error);
1022 LIN_SDT_PROBE1(futex, linux_set_robust_list, return, EINVAL);
1026 em = em_find(td->td_proc, EMUL_DOLOCK);
1027 em->robust_futexes = args->head;
1028 EMUL_UNLOCK(&emul_lock);
1030 LIN_SDT_PROBE1(futex, linux_set_robust_list, return, 0);
1035 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
1037 struct linux_emuldata *em;
1038 struct linux_robust_list_head *head;
1039 l_size_t len = sizeof(struct linux_robust_list_head);
1042 LIN_SDT_PROBE2(futex, linux_get_robust_list, entry, td, args);
1045 em = em_find(td->td_proc, EMUL_DONTLOCK);
1046 head = em->robust_futexes;
1050 p = pfind(args->pid);
1052 LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1057 em = em_find(p, EMUL_DONTLOCK);
1059 if (priv_check(td, PRIV_CRED_SETUID) ||
1060 priv_check(td, PRIV_CRED_SETEUID) ||
1061 p_candebug(td, p)) {
1064 LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1068 head = em->robust_futexes;
1073 error = copyout(&len, args->len, sizeof(l_size_t));
1075 LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1077 LIN_SDT_PROBE1(futex, linux_get_robust_list, return, EFAULT);
1081 error = copyout(head, args->head, sizeof(struct linux_robust_list_head));
1083 LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1087 LIN_SDT_PROBE1(futex, linux_get_robust_list, return, error);
1092 handle_futex_death(struct proc *p, uint32_t *uaddr, int pi)
1094 uint32_t uval, nval, mval;
1098 LIN_SDT_PROBE3(futex, handle_futex_death, entry, p, uaddr, pi);
1101 error = copyin(uaddr, &uval, 4);
1103 LIN_SDT_PROBE1(futex, handle_futex_death, copyin_error, error);
1104 LIN_SDT_PROBE1(futex, handle_futex_death, return, EFAULT);
1107 if ((uval & FUTEX_TID_MASK) == p->p_pid) {
1108 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1109 nval = casuword32(uaddr, uval, mval);
1112 LIN_SDT_PROBE1(futex, handle_futex_death, return,
1120 if (!pi && (uval & FUTEX_WAITERS)) {
1121 error = futex_get(uaddr, NULL, &f,
1122 FUTEX_DONTCREATE | FUTEX_SHARED);
1124 LIN_SDT_PROBE1(futex, handle_futex_death,
1129 futex_wake(f, 1, FUTEX_BITSET_MATCH_ANY);
1135 LIN_SDT_PROBE1(futex, handle_futex_death, return, 0);
1140 fetch_robust_entry(struct linux_robust_list **entry,
1141 struct linux_robust_list **head, int *pi)
1146 LIN_SDT_PROBE3(futex, fetch_robust_entry, entry, entry, head, pi);
1148 error = copyin((const void *)head, &uentry, sizeof(l_ulong));
1150 LIN_SDT_PROBE1(futex, fetch_robust_entry, copyin_error, error);
1151 LIN_SDT_PROBE1(futex, fetch_robust_entry, return, EFAULT);
1155 *entry = (void *)(uentry & ~1UL);
1158 LIN_SDT_PROBE1(futex, fetch_robust_entry, return, 0);
1162 /* This walks the list of robust futexes releasing them. */
1164 release_futexes(struct proc *p)
1166 struct linux_robust_list_head *head = NULL;
1167 struct linux_robust_list *entry, *next_entry, *pending;
1168 unsigned int limit = 2048, pi, next_pi, pip;
1169 struct linux_emuldata *em;
1170 l_long futex_offset;
1173 LIN_SDT_PROBE1(futex, release_futexes, entry, p);
1175 em = em_find(p, EMUL_DONTLOCK);
1176 head = em->robust_futexes;
1179 LIN_SDT_PROBE0(futex, release_futexes, return);
1183 if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi)) {
1184 LIN_SDT_PROBE0(futex, release_futexes, return);
1188 error = copyin(&head->futex_offset, &futex_offset,
1189 sizeof(futex_offset));
1191 LIN_SDT_PROBE1(futex, release_futexes, copyin_error, error);
1192 LIN_SDT_PROBE0(futex, release_futexes, return);
1196 if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip)) {
1197 LIN_SDT_PROBE0(futex, release_futexes, return);
1201 while (entry != &head->list) {
1202 rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
1204 if (entry != pending)
1205 if (handle_futex_death(p,
1206 (uint32_t *)((caddr_t)entry + futex_offset), pi)) {
1207 LIN_SDT_PROBE0(futex, release_futexes, return);
1211 LIN_SDT_PROBE0(futex, release_futexes, return);
1221 sched_relinquish(curthread);
1225 handle_futex_death(p, (uint32_t *)((caddr_t)pending + futex_offset), pip);
1227 LIN_SDT_PROBE0(futex, release_futexes, return);