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 "opt_compat.h"
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/imgact.h>
39 #include <sys/imgact_aout.h>
40 #include <sys/imgact_elf.h>
41 #include <sys/kern_syscall.h>
43 #include <sys/malloc.h>
45 #include <sys/signalvar.h>
46 #include <sys/sysent.h>
47 #include <sys/sysproto.h>
50 #include <sys/spinlock2.h>
53 #include <vm/vm_param.h>
54 #include <vm/vm_page.h>
55 #include <vm/vm_extern.h>
57 #include <sys/kernel.h>
58 #include <sys/module.h>
59 #include <machine/cpu.h>
60 #include <machine/limits.h>
62 #include "i386/linux.h"
63 #include "i386/linux_proto.h"
64 #include "linux_signal.h"
65 #include "linux_util.h"
66 #include "linux_emuldata.h"
68 MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
69 MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futexes wp");
75 struct futex *wp_futex;
76 TAILQ_ENTRY(waiting_proc) wp_list;
83 LIST_ENTRY(futex) f_list;
84 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
87 struct futex_list futex_list;
90 #define FUTEX_LOCK(f) spin_lock(&(f)->f_lck)
91 #define FUTEX_UNLOCK(f) spin_unlock(&(f)->f_lck)
92 #define FUTEX_INIT(f) spin_init(&(f)->f_lck)
93 #define FUTEX_SLEEP(f, id, flag, wm, timo) ssleep((id), &(f)->f_lck, (flag), (wm), (timo))
96 #define FUTEX_LOCK(f) lockmgr(&(f)->f_lck, LK_EXCLUSIVE)
97 #define FUTEX_UNLOCK(f) lockmgr(&(f)->f_lck, LK_RELEASE)
98 #define FUTEX_INIT(f) lockinit(&(f)->f_lck, "ftlk", 0, LK_CANRECURSE)
99 #define FUTEX_DESTROY(f) lockuninit(&(f)->f_lck)
100 #define FUTEX_ASSERT_LOCKED(f) KKASSERT(lockstatus(&(f)->f_lck, curthread) == LK_EXCLUSIVE)
101 #define FUTEX_SLEEP(f, id, flag, wm, timo) lksleep((id), &(f)->f_lck, (flag), (wm), (timo))
103 struct lock futex_mtx; /* protects the futex list */
104 #define FUTEXES_LOCK lockmgr(&futex_mtx, LK_EXCLUSIVE)
105 #define FUTEXES_UNLOCK lockmgr(&futex_mtx, LK_RELEASE)
107 /* Debug magic to take advantage of freebsd's mess */
109 #define LINUX_CTR_PREFIX
111 #define LINUX_CTR_PREFIX while (0)
114 #define LINUX_CTR1(a,b,c) LINUX_CTR_PREFIX kprintf("linux_futex: " b "\n",c)
115 #define LINUX_CTR2(a,b,c,d) LINUX_CTR_PREFIX kprintf("linux_futex: " b "\n",c,d)
116 #define LINUX_CTR3(a,b,c,d,e) LINUX_CTR_PREFIX kprintf("linux_futex: " b "\n",c,d,e)
117 #define LINUX_CTR4(a,b,c,d,e,f) LINUX_CTR_PREFIX kprintf("linux_futex: " b "\n",c,d,e,f)
118 #define LINUX_CTR5(a,b,c,d,e,f,g) LINUX_CTR_PREFIX kprintf("linux_futex: " b "\n",c,d,e,f,g)
122 /* flags for futex_get() */
123 #define FUTEX_CREATE_WP 0x1 /* create waiting_proc */
124 #define FUTEX_DONTCREATE 0x2 /* don't create futex if not exists */
125 #define FUTEX_DONTEXISTS 0x4 /* return EINVAL if futex exists */
128 #define FUTEX_WP_REQUEUED 0x1 /* wp requeued - wp moved from wp_list
129 * of futex where thread sleep to wp_list
132 #define FUTEX_WP_REMOVED 0x2 /* wp is woken up and removed from futex
133 * wp_list to prevent double wakeup.
137 int futex_xchgl(int oparg, uint32_t *uaddr, int *oldval);
138 int futex_addl(int oparg, uint32_t *uaddr, int *oldval);
139 int futex_orl(int oparg, uint32_t *uaddr, int *oldval);
140 int futex_andl(int oparg, uint32_t *uaddr, int *oldval);
141 int futex_xorl(int oparg, uint32_t *uaddr, int *oldval);
144 futex_put(struct futex *f, struct waiting_proc *wp)
146 FUTEX_ASSERT_LOCKED(f);
148 if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
149 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
150 kfree(wp, M_FUTEX_WP);
154 if (--f->f_refcount == 0) {
155 LIST_REMOVE(f, f_list);
159 LINUX_CTR2(sys_futex, "futex_put destroy uaddr %p ref %d",
160 f->f_uaddr, f->f_refcount);
166 LINUX_CTR2(sys_futex, "futex_put uaddr %p ref %d",
167 f->f_uaddr, f->f_refcount);
173 futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
175 struct futex *f, *tmpf;
181 LIST_FOREACH(f, &futex_list, f_list) {
182 if (f->f_uaddr == uaddr) {
186 kfree(tmpf, M_FUTEX);
188 if (flags & FUTEX_DONTEXISTS) {
194 * Increment refcount of the found futex to
195 * prevent it from deallocation before FUTEX_LOCK()
202 LINUX_CTR2(sys_futex, "futex_get uaddr %p ref %d",
203 uaddr, f->f_refcount);
208 if (flags & FUTEX_DONTCREATE) {
210 LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
216 tmpf = kmalloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
217 tmpf->f_uaddr = uaddr;
218 tmpf->f_refcount = 1;
220 TAILQ_INIT(&tmpf->f_waiting_proc);
223 * Lock the new futex before an insert into the futex_list
224 * to prevent futex usage by other.
230 LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
233 LINUX_CTR2(sys_futex, "futex_get uaddr %p ref %d new",
234 uaddr, tmpf->f_refcount);
240 futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
245 if (flags & FUTEX_CREATE_WP) {
246 *wp = kmalloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
249 error = futex_get0(uaddr, f, flags);
251 if (flags & FUTEX_CREATE_WP)
252 kfree(*wp, M_FUTEX_WP);
255 if (flags & FUTEX_CREATE_WP) {
256 TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
257 (*wp)->wp_futex = *f;
264 futex_sleep(struct futex *f, struct waiting_proc *wp, int timeout)
268 FUTEX_ASSERT_LOCKED(f);
269 LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %d ref %d",
270 f->f_uaddr, wp, timeout, f->f_refcount);
271 error = FUTEX_SLEEP(f, wp, PCATCH, "futex", timeout);
272 if (wp->wp_flags & FUTEX_WP_REQUEUED) {
273 KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
274 LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p w"
275 " %p requeued uaddr %p ref %d",
276 error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
277 wp->wp_futex->f_refcount);
288 futex_wake(struct futex *f, int n)
290 struct waiting_proc *wp, *wpt;
293 FUTEX_ASSERT_LOCKED(f);
294 TAILQ_FOREACH_MUTABLE(wp, &f->f_waiting_proc, wp_list, wpt) {
295 LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
296 f->f_uaddr, wp, f->f_refcount);
297 wp->wp_flags |= FUTEX_WP_REMOVED;
298 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
308 futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
310 struct waiting_proc *wp, *wpt;
313 FUTEX_ASSERT_LOCKED(f);
314 FUTEX_ASSERT_LOCKED(f2);
316 TAILQ_FOREACH_MUTABLE(wp, &f->f_waiting_proc, wp_list, wpt) {
318 LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
320 wp->wp_flags |= FUTEX_WP_REMOVED;
321 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
324 LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
325 f->f_uaddr, wp, f2->f_uaddr);
326 wp->wp_flags |= FUTEX_WP_REQUEUED;
327 /* Move wp to wp_list of f2 futex */
328 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
329 TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
332 * Thread which sleeps on wp after waking should
333 * acquire f2 lock, so increment refcount of f2 to
334 * prevent it from premature deallocation.
349 futex_wait(struct futex *f, struct waiting_proc *wp, struct l_timespec *ts)
351 struct l_timespec timeout;
357 error = copyin(ts, &timeout, sizeof(timeout));
360 TIMESPEC_TO_TIMEVAL(&tv, &timeout);
361 error = itimerfix(&tv);
364 timeout_hz = tvtohz_high(&tv);
369 error = futex_sleep(f, wp, timeout_hz);
370 if (error == EWOULDBLOCK)
377 futex_atomic_op(struct proc *p, int encoded_op, uint32_t *uaddr)
379 int op = (encoded_op >> 28) & 7;
380 int cmp = (encoded_op >> 24) & 15;
381 int oparg = (encoded_op << 8) >> 20;
382 int cmparg = (encoded_op << 20) >> 20;
385 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
389 if (ldebug(sys_futex))
390 kprintf("futex_atomic_op: op = %d, cmp = %d, oparg = %x, "
391 "cmparg = %x, uaddr = %p\n",
392 op, cmp, oparg, cmparg, uaddr);
394 /* XXX: linux verifies access here and returns EFAULT */
398 ret = futex_xchgl(oparg, uaddr, &oldval);
401 ret = futex_addl(oparg, uaddr, &oldval);
404 ret = futex_orl(oparg, uaddr, &oldval);
407 ret = futex_andl(~oparg, uaddr, &oldval);
410 ret = futex_xorl(oparg, uaddr, &oldval);
421 case FUTEX_OP_CMP_EQ:
422 return (oldval == cmparg);
423 case FUTEX_OP_CMP_NE:
424 return (oldval != cmparg);
425 case FUTEX_OP_CMP_LT:
426 return (oldval < cmparg);
427 case FUTEX_OP_CMP_GE:
428 return (oldval >= cmparg);
429 case FUTEX_OP_CMP_LE:
430 return (oldval <= cmparg);
431 case FUTEX_OP_CMP_GT:
432 return (oldval > cmparg);
439 sys_linux_sys_futex(struct linux_sys_futex_args *args)
441 int op_ret, val, ret, nrwake;
442 struct waiting_proc *wp;
443 struct futex *f, *f2 = NULL;
447 * Our implementation provides only privates futexes. Most of the apps
448 * should use private futexes but don't claim so. Therefore we treat
449 * all futexes as private by clearing the FUTEX_PRIVATE_FLAG. It works
450 * in most cases (ie. when futexes are not shared on file descriptor
451 * or between different processes.).
453 args->op = (args->op & ~LINUX_FUTEX_PRIVATE_FLAG);
456 case LINUX_FUTEX_WAIT:
457 LINUX_CTR2(sys_futex, "WAIT val %d uaddr %p",
458 args->val, args->uaddr);
460 if (ldebug(sys_futex))
461 kprintf(ARGS(sys_futex, "futex_wait val %d uaddr %p"),
462 args->val, args->uaddr);
464 error = futex_get(args->uaddr, &wp, &f, FUTEX_CREATE_WP);
467 error = copyin(args->uaddr, &val, sizeof(val));
469 LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
474 if (val != args->val) {
475 LINUX_CTR3(sys_futex, "WAIT uaddr %p val %d != uval %d",
476 args->uaddr, args->val, val);
478 return (EWOULDBLOCK);
481 error = futex_wait(f, wp, args->timeout);
484 case LINUX_FUTEX_WAKE:
486 LINUX_CTR2(sys_futex, "WAKE val %d uaddr %p",
487 args->val, args->uaddr);
490 * XXX: Linux is able to cope with different addresses
491 * corresponding to the same mapped memory in the sleeping
492 * and waker process(es).
495 if (ldebug(sys_futex))
496 kprintf(ARGS(sys_futex, "futex_wake val %d uaddr %p"),
497 args->val, args->uaddr);
499 error = futex_get(args->uaddr, NULL, &f, FUTEX_DONTCREATE);
503 args->sysmsg_iresult = 0;
506 args->sysmsg_iresult = futex_wake(f, args->val);
510 case LINUX_FUTEX_CMP_REQUEUE:
512 LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
513 "val %d val3 %d uaddr2 %p val2 %d",
514 args->uaddr, args->val, args->val3, args->uaddr2,
515 (int)(unsigned long)args->timeout);
518 if (ldebug(sys_futex))
519 kprintf(ARGS(sys_futex, "futex_cmp_requeue uaddr %p "
520 "val %d val3 %d uaddr2 %p val2 %d"),
521 args->uaddr, args->val, args->val3, args->uaddr2,
522 (int)(unsigned long)args->timeout);
525 * Linux allows this, we would not, it is an incorrect
526 * usage of declared ABI, so return EINVAL.
528 if (args->uaddr == args->uaddr2)
530 error = futex_get0(args->uaddr, &f, 0);
535 * To avoid deadlocks return EINVAL if second futex
536 * exists at this time. Otherwise create the new futex
537 * and ignore false positive LOR which thus happens.
539 * Glibc fall back to FUTEX_WAKE in case of any error
540 * returned by FUTEX_CMP_REQUEUE.
542 error = futex_get0(args->uaddr2, &f2, FUTEX_DONTEXISTS);
547 error = copyin(args->uaddr, &val, sizeof(val));
549 LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
555 if (val != args->val3) {
556 LINUX_CTR2(sys_futex, "CMP_REQUEUE val %d != uval %d",
563 nrwake = (int)(unsigned long)args->timeout;
564 args->sysmsg_iresult = futex_requeue(f, args->val, f2, nrwake);
569 case LINUX_FUTEX_WAKE_OP:
571 LINUX_CTR5(sys_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);
577 if (ldebug(sys_futex))
578 kprintf(ARGS(sys_futex, "futex_wake_op "
579 "uaddr %p op %d val %x uaddr2 %p val3 %x"),
580 args->uaddr, args->op, args->val,
581 args->uaddr2, args->val3);
583 error = futex_get0(args->uaddr, &f, 0);
586 if (args->uaddr != args->uaddr2)
587 error = futex_get0(args->uaddr2, &f2, 0);
594 * This function returns positive number as results and
597 op_ret = futex_atomic_op(curproc, args->val3, args->uaddr2);
600 /* XXX: We don't handle the EFAULT yet. */
601 if (op_ret != -EFAULT) {
613 ret = futex_wake(f, args->val);
617 nrwake = (int)(unsigned long)args->timeout;
620 op_ret += futex_wake(f2, nrwake);
622 op_ret += futex_wake(f, nrwake);
629 args->sysmsg_iresult = ret;
632 case LINUX_FUTEX_LOCK_PI:
633 /* not yet implemented */
636 case LINUX_FUTEX_UNLOCK_PI:
637 /* not yet implemented */
640 case LINUX_FUTEX_TRYLOCK_PI:
641 /* not yet implemented */
644 case LINUX_FUTEX_REQUEUE:
647 * Glibc does not use this operation since version 2.3.3,
648 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
649 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
650 * FUTEX_REQUEUE returned EINVAL.
655 kprintf("linux_sys_futex: unknown op %d\n", args->op);
663 sys_linux_set_robust_list(struct linux_set_robust_list_args *args)
666 if (ldebug(set_robust_list))
667 kprintf(ARGS(set_robust_list, "head %p len %d"),
668 args->head, args->len);
671 if (args->len != sizeof(struct linux_robust_list_head))
674 emuldata_set_robust(curproc, args->head);
682 sys_linux_get_robust_list(struct linux_get_robust_list_args *args)
684 struct linux_emuldata *em;
685 struct linux_robust_list_head empty_head;
686 struct linux_robust_list_head *head;
687 l_size_t len = sizeof(struct linux_robust_list_head);
691 if (ldebug(get_robust_list))
692 kprintf(ARGS(get_robust_list, ""));
695 if (args->pid == 0) {
697 em = emuldata_get(curproc);
698 KKASSERT(em != NULL);
699 if (em->robust_futexes == NULL) {
700 bzero(&empty_head, sizeof(empty_head));
703 head = em->robust_futexes;
709 p = pfind(args->pid);
715 em = emuldata_get(p);
716 head = em->robust_futexes;
718 /* XXX: ptrace? p_candebug?*/
719 if (priv_check(curthread, PRIV_CRED_SETUID) ||
720 priv_check(curthread, PRIV_CRED_SETEUID)/* ||
721 p_candebug(curproc, p) */) {
728 error = copyout(&len, args->len, sizeof(l_size_t));
732 error = copyout(head, args->head, sizeof(struct linux_robust_list_head));
738 handle_futex_death(struct proc *p, uint32_t *uaddr, int pi)
740 uint32_t uval, nval, mval;
745 if (copyin(uaddr, &uval, 4))
747 if ((uval & FUTEX_TID_MASK) == p->p_pid) {
748 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
749 nval = casuword((ulong *)uaddr, uval, mval);
757 if (!pi && (uval & FUTEX_WAITERS)) {
758 error = futex_get(uaddr, NULL, &f,
773 fetch_robust_entry(struct linux_robust_list **entry,
774 struct linux_robust_list **head, int *pi)
778 if (copyin((const void *)head, &uentry, sizeof(l_ulong)))
781 *entry = (void *)(uentry & ~1UL);
787 /* This walks the list of robust futexes releasing them. */
789 release_futexes(struct proc *p)
791 struct linux_robust_list_head *head = NULL;
792 struct linux_robust_list *entry, *next_entry, *pending;
793 unsigned int limit = 2048, pi, next_pi, pip;
794 struct linux_emuldata *em;
800 em = emuldata_get(p);
801 KKASSERT(em != NULL);
802 head = em->robust_futexes;
808 if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi))
811 if (copyin(&head->futex_offset, &futex_offset, sizeof(futex_offset)))
814 if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip))
817 while (entry != &head->list) {
818 rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
820 if (entry != pending)
821 if (handle_futex_death(p, (uint32_t *)entry + futex_offset, pi))
832 /* XXX: not sure about this yield, was sched_relinquish(curthread); */
837 handle_futex_death(p, (uint32_t *)pending + futex_offset, pip);