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"
42 #include <sys/param.h>
43 #include <sys/systm.h>
44 #include <sys/imgact.h>
45 #include <sys/kernel.h>
48 #include <sys/malloc.h>
49 #include <sys/mutex.h>
52 #include <sys/queue.h>
53 #include <sys/sched.h>
57 #include <machine/../linux32/linux.h>
58 #include <machine/../linux32/linux32_proto.h>
60 #include <machine/../linux/linux.h>
61 #include <machine/../linux/linux_proto.h>
63 #include <compat/linux/linux_futex.h>
64 #include <compat/linux/linux_emul.h>
65 #include <compat/linux/linux_util.h>
67 MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
68 MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futexes wp");
74 struct futex *wp_futex;
75 TAILQ_ENTRY(waiting_proc) wp_list;
82 LIST_ENTRY(futex) f_list;
83 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
86 struct futex_list futex_list;
88 #define FUTEX_LOCK(f) sx_xlock(&(f)->f_lck)
89 #define FUTEX_UNLOCK(f) sx_xunlock(&(f)->f_lck)
90 #define FUTEX_INIT(f) sx_init_flags(&(f)->f_lck, "ftlk", 0)
91 #define FUTEX_DESTROY(f) sx_destroy(&(f)->f_lck)
92 #define FUTEX_ASSERT_LOCKED(f) sx_assert(&(f)->f_lck, SA_XLOCKED)
94 struct mtx futex_mtx; /* protects the futex list */
95 #define FUTEXES_LOCK mtx_lock(&futex_mtx)
96 #define FUTEXES_UNLOCK mtx_unlock(&futex_mtx)
98 /* flags for futex_get() */
99 #define FUTEX_CREATE_WP 0x1 /* create waiting_proc */
100 #define FUTEX_DONTCREATE 0x2 /* don't create futex if not exists */
101 #define FUTEX_DONTEXISTS 0x4 /* return EINVAL if futex exists */
104 #define FUTEX_WP_REQUEUED 0x1 /* wp requeued - wp moved from wp_list
105 * of futex where thread sleep to wp_list
108 #define FUTEX_WP_REMOVED 0x2 /* wp is woken up and removed from futex
109 * wp_list to prevent double wakeup.
113 int futex_xchgl(int oparg, uint32_t *uaddr, int *oldval);
114 int futex_addl(int oparg, uint32_t *uaddr, int *oldval);
115 int futex_orl(int oparg, uint32_t *uaddr, int *oldval);
116 int futex_andl(int oparg, uint32_t *uaddr, int *oldval);
117 int futex_xorl(int oparg, uint32_t *uaddr, int *oldval);
120 futex_put(struct futex *f, struct waiting_proc *wp)
123 FUTEX_ASSERT_LOCKED(f);
125 if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
126 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
127 free(wp, M_FUTEX_WP);
131 if (--f->f_refcount == 0) {
132 LIST_REMOVE(f, f_list);
136 LINUX_CTR2(sys_futex, "futex_put destroy uaddr %p ref %d",
137 f->f_uaddr, f->f_refcount);
143 LINUX_CTR2(sys_futex, "futex_put uaddr %p ref %d",
144 f->f_uaddr, f->f_refcount);
150 futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
152 struct futex *f, *tmpf;
158 LIST_FOREACH(f, &futex_list, f_list) {
159 if (f->f_uaddr == uaddr) {
165 if (flags & FUTEX_DONTEXISTS) {
171 * Increment refcount of the found futex to
172 * prevent it from deallocation before FUTEX_LOCK()
179 LINUX_CTR2(sys_futex, "futex_get uaddr %p ref %d",
180 uaddr, f->f_refcount);
185 if (flags & FUTEX_DONTCREATE) {
187 LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
193 tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
194 tmpf->f_uaddr = uaddr;
195 tmpf->f_refcount = 1;
197 TAILQ_INIT(&tmpf->f_waiting_proc);
200 * Lock the new futex before an insert into the futex_list
201 * to prevent futex usage by other.
207 LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
210 LINUX_CTR2(sys_futex, "futex_get uaddr %p ref %d new",
211 uaddr, tmpf->f_refcount);
217 futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
222 if (flags & FUTEX_CREATE_WP) {
223 *wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
226 error = futex_get0(uaddr, f, flags);
228 if (flags & FUTEX_CREATE_WP)
229 free(*wp, M_FUTEX_WP);
232 if (flags & FUTEX_CREATE_WP) {
233 TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
234 (*wp)->wp_futex = *f;
241 futex_sleep(struct futex *f, struct waiting_proc *wp, unsigned long timeout)
245 FUTEX_ASSERT_LOCKED(f);
246 LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %ld ref %d",
247 f->f_uaddr, wp, timeout, f->f_refcount);
248 error = sx_sleep(wp, &f->f_lck, PCATCH, "futex", timeout);
249 if (wp->wp_flags & FUTEX_WP_REQUEUED) {
250 KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
251 LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p w"
252 " %p requeued uaddr %p ref %d",
253 error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
254 wp->wp_futex->f_refcount);
259 LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
260 error, f->f_uaddr, wp);
267 futex_wake(struct futex *f, int n)
269 struct waiting_proc *wp, *wpt;
272 FUTEX_ASSERT_LOCKED(f);
273 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
274 LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
275 f->f_uaddr, wp, f->f_refcount);
276 wp->wp_flags |= FUTEX_WP_REMOVED;
277 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
287 futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
289 struct waiting_proc *wp, *wpt;
292 FUTEX_ASSERT_LOCKED(f);
293 FUTEX_ASSERT_LOCKED(f2);
295 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
297 LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
299 wp->wp_flags |= FUTEX_WP_REMOVED;
300 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
303 LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
304 f->f_uaddr, wp, f2->f_uaddr);
305 wp->wp_flags |= FUTEX_WP_REQUEUED;
306 /* Move wp to wp_list of f2 futex */
307 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
308 TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
311 * Thread which sleeps on wp after waking should
312 * acquire f2 lock, so increment refcount of f2 to
313 * prevent it from premature deallocation.
328 futex_wait(struct futex *f, struct waiting_proc *wp, struct l_timespec *ts)
330 struct l_timespec timeout = {0, 0};
331 struct timeval tv = {0, 0};
336 error = copyin(ts, &timeout, sizeof(timeout));
341 tv.tv_usec = timeout.tv_sec * 1000000 + timeout.tv_nsec / 1000;
342 timeout_hz = tvtohz(&tv);
344 if (timeout.tv_sec == 0 && timeout.tv_nsec == 0)
348 * If the user process requests a non null timeout,
349 * make sure we do not turn it into an infinite
350 * timeout because timeout_hz gets null.
352 * We use a minimal timeout of 1/hz. Maybe it would
353 * make sense to just return ETIMEDOUT without sleeping.
355 if (((timeout.tv_sec != 0) || (timeout.tv_nsec != 0)) &&
359 error = futex_sleep(f, wp, timeout_hz);
360 if (error == EWOULDBLOCK)
367 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
369 int op = (encoded_op >> 28) & 7;
370 int cmp = (encoded_op >> 24) & 15;
371 int oparg = (encoded_op << 8) >> 20;
372 int cmparg = (encoded_op << 20) >> 20;
375 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
379 if (ldebug(sys_futex))
380 printf("futex_atomic_op: op = %d, cmp = %d, oparg = %x, "
381 "cmparg = %x, uaddr = %p\n",
382 op, cmp, oparg, cmparg, uaddr);
384 /* XXX: linux verifies access here and returns EFAULT */
388 ret = futex_xchgl(oparg, uaddr, &oldval);
391 ret = futex_addl(oparg, uaddr, &oldval);
394 ret = futex_orl(oparg, uaddr, &oldval);
397 ret = futex_andl(~oparg, uaddr, &oldval);
400 ret = futex_xorl(oparg, uaddr, &oldval);
411 case FUTEX_OP_CMP_EQ:
412 return (oldval == cmparg);
413 case FUTEX_OP_CMP_NE:
414 return (oldval != cmparg);
415 case FUTEX_OP_CMP_LT:
416 return (oldval < cmparg);
417 case FUTEX_OP_CMP_GE:
418 return (oldval >= cmparg);
419 case FUTEX_OP_CMP_LE:
420 return (oldval <= cmparg);
421 case FUTEX_OP_CMP_GT:
422 return (oldval > cmparg);
429 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
431 int op_ret, val, ret, nrwake;
432 struct linux_emuldata *em;
433 struct waiting_proc *wp;
434 struct futex *f, *f2;
438 * Our implementation provides only privates futexes. Most of the apps
439 * should use private futexes but don't claim so. Therefore we treat
440 * all futexes as private by clearing the FUTEX_PRIVATE_FLAG. It works
441 * in most cases (ie. when futexes are not shared on file descriptor
442 * or between different processes.).
444 args->op = (args->op & ~LINUX_FUTEX_PRIVATE_FLAG);
447 case LINUX_FUTEX_WAIT:
449 LINUX_CTR2(sys_futex, "WAIT val %d uaddr %p",
450 args->val, args->uaddr);
452 if (ldebug(sys_futex))
453 printf(ARGS(sys_futex, "futex_wait val %d uaddr %p"),
454 args->val, args->uaddr);
456 error = futex_get(args->uaddr, &wp, &f, FUTEX_CREATE_WP);
459 error = copyin(args->uaddr, &val, sizeof(val));
461 LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
466 if (val != args->val) {
467 LINUX_CTR3(sys_futex, "WAIT uaddr %p val %d != uval %d",
468 args->uaddr, args->val, val);
470 return (EWOULDBLOCK);
473 error = futex_wait(f, wp, args->timeout);
476 case LINUX_FUTEX_WAKE:
478 LINUX_CTR2(sys_futex, "WAKE val %d uaddr %p",
479 args->val, args->uaddr);
482 * XXX: Linux is able to cope with different addresses
483 * corresponding to the same mapped memory in the sleeping
484 * and waker process(es).
487 if (ldebug(sys_futex))
488 printf(ARGS(sys_futex, "futex_wake val %d uaddr %p"),
489 args->val, args->uaddr);
491 error = futex_get(args->uaddr, NULL, &f, FUTEX_DONTCREATE);
495 td->td_retval[0] = 0;
498 td->td_retval[0] = futex_wake(f, args->val);
502 case LINUX_FUTEX_CMP_REQUEUE:
504 LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
505 "val %d val3 %d uaddr2 %p val2 %d",
506 args->uaddr, args->val, args->val3, args->uaddr2,
507 (int)(unsigned long)args->timeout);
510 if (ldebug(sys_futex))
511 printf(ARGS(sys_futex, "futex_cmp_requeue uaddr %p "
512 "val %d val3 %d uaddr2 %p val2 %d"),
513 args->uaddr, args->val, args->val3, args->uaddr2,
514 (int)(unsigned long)args->timeout);
518 * Linux allows this, we would not, it is an incorrect
519 * usage of declared ABI, so return EINVAL.
521 if (args->uaddr == args->uaddr2)
523 error = futex_get0(args->uaddr, &f, 0);
528 * To avoid deadlocks return EINVAL if second futex
529 * exists at this time. Otherwise create the new futex
530 * and ignore false positive LOR which thus happens.
532 * Glibc fall back to FUTEX_WAKE in case of any error
533 * returned by FUTEX_CMP_REQUEUE.
535 error = futex_get0(args->uaddr2, &f2, FUTEX_DONTEXISTS);
540 error = copyin(args->uaddr, &val, sizeof(val));
542 LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
548 if (val != args->val3) {
549 LINUX_CTR2(sys_futex, "CMP_REQUEUE val %d != uval %d",
556 nrwake = (int)(unsigned long)args->timeout;
557 td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
562 case LINUX_FUTEX_WAKE_OP:
564 LINUX_CTR5(sys_futex, "WAKE_OP "
565 "uaddr %p op %d val %x uaddr2 %p val3 %x",
566 args->uaddr, args->op, args->val,
567 args->uaddr2, args->val3);
570 if (ldebug(sys_futex))
571 printf(ARGS(sys_futex, "futex_wake_op "
572 "uaddr %p op %d val %x uaddr2 %p val3 %x"),
573 args->uaddr, args->op, args->val,
574 args->uaddr2, args->val3);
576 error = futex_get0(args->uaddr, &f, 0);
579 if (args->uaddr != args->uaddr2)
580 error = futex_get0(args->uaddr2, &f2, 0);
587 * This function returns positive number as results and
590 op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
593 /* XXX: We don't handle the EFAULT yet. */
594 if (op_ret != -EFAULT) {
606 ret = futex_wake(f, args->val);
610 nrwake = (int)(unsigned long)args->timeout;
613 op_ret += futex_wake(f2, nrwake);
615 op_ret += futex_wake(f, nrwake);
622 td->td_retval[0] = ret;
625 case LINUX_FUTEX_LOCK_PI:
626 /* not yet implemented */
629 case LINUX_FUTEX_UNLOCK_PI:
630 /* not yet implemented */
633 case LINUX_FUTEX_TRYLOCK_PI:
634 /* not yet implemented */
637 case LINUX_FUTEX_REQUEUE:
640 * Glibc does not use this operation since version 2.3.3,
641 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
642 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
643 * FUTEX_REQUEUE returned EINVAL.
645 em = em_find(td->td_proc, EMUL_DONTLOCK);
646 if (em->used_requeue == 0) {
647 printf("linux(%s (%d)) sys_futex: "
648 "unsupported futex_requeue op\n",
649 td->td_proc->p_comm, td->td_proc->p_pid);
650 em->used_requeue = 1;
655 printf("linux_sys_futex: unknown op %d\n", args->op);
663 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
665 struct linux_emuldata *em;
668 if (ldebug(set_robust_list))
669 printf(ARGS(set_robust_list, "head %p len %d"),
670 args->head, args->len);
673 if (args->len != sizeof(struct linux_robust_list_head))
676 em = em_find(td->td_proc, EMUL_DOLOCK);
677 em->robust_futexes = args->head;
678 EMUL_UNLOCK(&emul_lock);
684 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
686 struct linux_emuldata *em;
687 struct linux_robust_list_head *head;
688 l_size_t len = sizeof(struct linux_robust_list_head);
692 if (ldebug(get_robust_list))
693 printf(ARGS(get_robust_list, ""));
697 em = em_find(td->td_proc, EMUL_DONTLOCK);
698 head = em->robust_futexes;
702 p = pfind(args->pid);
706 em = em_find(p, EMUL_DONTLOCK);
708 if (priv_check(td, PRIV_CRED_SETUID) ||
709 priv_check(td, PRIV_CRED_SETEUID) ||
714 head = em->robust_futexes;
719 error = copyout(&len, args->len, sizeof(l_size_t));
723 error = copyout(head, args->head, sizeof(struct linux_robust_list_head));
729 handle_futex_death(struct proc *p, uint32_t *uaddr, int pi)
731 uint32_t uval, nval, mval;
736 if (copyin(uaddr, &uval, 4))
738 if ((uval & FUTEX_TID_MASK) == p->p_pid) {
739 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
740 nval = casuword32(uaddr, uval, mval);
748 if (!pi && (uval & FUTEX_WAITERS)) {
749 error = futex_get(uaddr, NULL, &f,
764 fetch_robust_entry(struct linux_robust_list **entry,
765 struct linux_robust_list **head, int *pi)
769 if (copyin((const void *)head, &uentry, sizeof(l_ulong)))
772 *entry = (void *)(uentry & ~1UL);
778 /* This walks the list of robust futexes releasing them. */
780 release_futexes(struct proc *p)
782 struct linux_robust_list_head *head = NULL;
783 struct linux_robust_list *entry, *next_entry, *pending;
784 unsigned int limit = 2048, pi, next_pi, pip;
785 struct linux_emuldata *em;
789 em = em_find(p, EMUL_DONTLOCK);
790 head = em->robust_futexes;
795 if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi))
798 if (copyin(&head->futex_offset, &futex_offset, sizeof(futex_offset)))
801 if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip))
804 while (entry != &head->list) {
805 rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
807 if (entry != pending)
808 if (handle_futex_death(p, (uint32_t *)entry + futex_offset, pi))
819 sched_relinquish(curthread);
823 handle_futex_death(p, (uint32_t *)pending + futex_offset, pip);