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/types.h>
45 #include <sys/systm.h>
47 #include <sys/queue.h>
49 #include <sys/mutex.h>
51 #include <sys/malloc.h>
54 #include <machine/../linux32/linux.h>
55 #include <machine/../linux32/linux32_proto.h>
57 #include <machine/../linux/linux.h>
58 #include <machine/../linux/linux_proto.h>
60 #include <compat/linux/linux_futex.h>
66 struct futex *wp_new_futex;
67 TAILQ_ENTRY(waiting_proc) wp_list;
72 LIST_ENTRY(futex) f_list;
73 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
76 LIST_HEAD(futex_list, futex) futex_list;
77 struct sx futex_sx; /* this protects the LIST of futexes */
79 #define FUTEX_LOCK sx_xlock(&futex_sx)
80 #define FUTEX_UNLOCK sx_xunlock(&futex_sx)
82 #define FUTEX_LOCKED 1
83 #define FUTEX_UNLOCKED 0
85 #define FUTEX_SYSTEM_LOCK mtx_lock(&Giant)
86 #define FUTEX_SYSTEM_UNLOCK mtx_unlock(&Giant)
88 static struct futex *futex_get(void *, int);
89 static void futex_put(struct futex *);
90 static int futex_sleep(struct futex *, struct thread *, unsigned long);
91 static int futex_wake(struct futex *, int, struct futex *, int);
92 static int futex_atomic_op(struct thread *td, int encoded_op, caddr_t uaddr);
95 int futex_xchgl(int oparg, caddr_t uaddr, int *oldval);
96 int futex_addl(int oparg, caddr_t uaddr, int *oldval);
97 int futex_orl(int oparg, caddr_t uaddr, int *oldval);
98 int futex_andl(int oparg, caddr_t uaddr, int *oldval);
99 int futex_xorl(int oparg, caddr_t uaddr, int *oldval);
102 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
106 struct l_timespec timeout = {0, 0};
111 struct timeval tv = {0, 0};
116 if (ldebug(sys_futex))
117 printf(ARGS(futex, "%p, %i, %i, *, %p, %i"), args->uaddr, args->op,
118 args->val, args->uaddr2, args->val3);
122 * Our implementation provides only privates futexes. Most of the apps
123 * should use private futexes but don't claim so. Therefore we treat
124 * all futexes as private by clearing the FUTEX_PRIVATE_FLAG. It works
125 * in most cases (ie. when futexes are not shared on file descriptor
126 * or between different processes.).
128 args->op = (args->op & ~LINUX_FUTEX_PRIVATE_FLAG);
131 case LINUX_FUTEX_WAIT:
134 if ((error = copyin(args->uaddr,
135 &val, sizeof(val))) != 0) {
140 if (val != args->val) {
145 if (args->timeout != NULL) {
146 if ((error = copyin(args->timeout,
147 &timeout, sizeof(timeout))) != 0) {
154 if (ldebug(sys_futex))
155 printf("FUTEX_WAIT %d: val = %d, uaddr = %p, "
156 "*uaddr = %d, timeout = %d.%09lu\n",
157 td->td_proc->p_pid, args->val,
158 args->uaddr, val, timeout.tv_sec,
159 (unsigned long)timeout.tv_nsec);
161 tv.tv_usec = timeout.tv_sec * 1000000 + timeout.tv_nsec / 1000;
162 timeout_hz = tvtohz(&tv);
164 if (timeout.tv_sec == 0 && timeout.tv_nsec == 0)
167 * If the user process requests a non null timeout,
168 * make sure we do not turn it into an infinite
169 * timeout because timeout_hz gets null.
171 * We use a minimal timeout of 1/hz. Maybe it would
172 * make sense to just return ETIMEDOUT without sleeping.
174 if (((timeout.tv_sec != 0) || (timeout.tv_nsec != 0)) &&
179 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
180 ret = futex_sleep(f, td, timeout_hz);
184 if (ldebug(sys_futex))
185 printf("FUTEX_WAIT %d: uaddr = %p, "
186 "ret = %d\n", td->td_proc->p_pid, args->uaddr, ret);
191 case EWOULDBLOCK: /* timeout */
194 case EINTR: /* signal */
197 case 0: /* FUTEX_WAKE received */
199 if (ldebug(sys_futex))
200 printf("FUTEX_WAIT %d: uaddr = %p, "
202 td->td_proc->p_pid, args->uaddr);
208 if (ldebug(sys_futex))
209 printf("FUTEX_WAIT: unexpected ret = %d\n",
218 case LINUX_FUTEX_WAKE:
222 * XXX: Linux is able to cope with different addresses
223 * corresponding to the same mapped memory in the sleeping
224 * and waker process(es).
227 if (ldebug(sys_futex))
228 printf("FUTEX_WAKE %d: uaddr = %p, val = %d\n",
229 td->td_proc->p_pid, args->uaddr, args->val);
231 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
232 td->td_retval[0] = futex_wake(f, args->val, NULL, 0);
238 case LINUX_FUTEX_CMP_REQUEUE:
241 if ((error = copyin(args->uaddr,
242 &val, sizeof(val))) != 0) {
247 if (val != args->val3) {
252 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
253 newf = futex_get(args->uaddr2, FUTEX_UNLOCKED);
254 td->td_retval[0] = futex_wake(f, args->val, newf,
255 (int)(unsigned long)args->timeout);
262 case LINUX_FUTEX_REQUEUE:
265 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
266 newf = futex_get(args->uaddr2, FUTEX_UNLOCKED);
267 td->td_retval[0] = futex_wake(f, args->val, newf,
268 (int)(unsigned long)args->timeout);
277 printf("linux_sys_futex: unimplemented op %d\n",
282 case LINUX_FUTEX_WAKE_OP:
285 if (ldebug(sys_futex))
286 printf("FUTEX_WAKE_OP: %d: uaddr = %p, op = %d, "
287 "val = %x, uaddr2 = %p, val3 = %x\n",
288 td->td_proc->p_pid, args->uaddr, args->op,
289 args->val, args->uaddr2, args->val3);
291 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
292 f2 = futex_get(args->uaddr2, FUTEX_UNLOCKED);
295 * This function returns positive number as results and
298 op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
300 if (ldebug(sys_futex))
301 printf("futex_atomic_op ret %d\n", op_ret);
304 /* XXX: We don't handle the EFAULT yet. */
305 if (op_ret != -EFAULT) {
319 ret = futex_wake(f, args->val, NULL, 0);
324 * Linux abuses the address of the timespec parameter
325 * as the number of retries.
327 op_ret += futex_wake(f2,
328 (int)(unsigned long)args->timeout, NULL, 0);
332 td->td_retval[0] = ret;
337 case LINUX_FUTEX_LOCK_PI:
338 /* not yet implemented */
341 case LINUX_FUTEX_UNLOCK_PI:
342 /* not yet implemented */
345 case LINUX_FUTEX_TRYLOCK_PI:
346 /* not yet implemented */
350 printf("linux_sys_futex: unknown op %d\n",
357 static struct futex *
358 futex_get(void *uaddr, int locked)
362 if (locked == FUTEX_UNLOCKED)
364 LIST_FOREACH(f, &futex_list, f_list) {
365 if (f->f_uaddr == uaddr) {
367 if (locked == FUTEX_UNLOCKED)
373 f = malloc(sizeof(*f), M_LINUX, M_WAITOK);
376 TAILQ_INIT(&f->f_waiting_proc);
377 LIST_INSERT_HEAD(&futex_list, f, f_list);
378 if (locked == FUTEX_UNLOCKED)
390 if (f->f_refcount == 0) {
391 LIST_REMOVE(f, f_list);
400 futex_sleep(struct futex *f, struct thread *td, unsigned long timeout)
402 struct waiting_proc *wp;
405 wp = malloc(sizeof(*wp), M_LINUX, M_WAITOK);
407 wp->wp_new_futex = NULL;
409 TAILQ_INSERT_TAIL(&f->f_waiting_proc, wp, wp_list);
413 if (ldebug(sys_futex))
414 printf("FUTEX --> %d tlseep timeout = %ld\n",
415 td->td_proc->p_pid, timeout);
417 ret = tsleep(wp, PCATCH | PZERO, "linuxfutex", timeout);
419 if (ldebug(sys_futex))
420 printf("FUTEX -> %d tsleep returns %d\n",
421 td->td_proc->p_pid, ret);
425 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
428 /* if we got woken up in futex_wake */
429 if ((ret == 0) && (wp->wp_new_futex != NULL)) {
430 /* suspend us on the new futex */
431 ret = futex_sleep(wp->wp_new_futex, td, timeout);
432 /* and release the old one */
433 futex_put(wp->wp_new_futex);
442 futex_wake(struct futex *f, int n, struct futex *newf, int n2)
444 struct waiting_proc *wp;
448 * Linux is very strange it wakes up N threads for
449 * all operations BUT requeue ones where its N+1
452 count = newf ? 0 : 1;
455 TAILQ_FOREACH(wp, &f->f_waiting_proc, wp_list) {
461 /* futex_put called after tsleep */
462 wp->wp_new_futex = futex_get(newf->f_uaddr,
476 futex_atomic_op(struct thread *td, int encoded_op, caddr_t uaddr)
478 int op = (encoded_op >> 28) & 7;
479 int cmp = (encoded_op >> 24) & 15;
480 int oparg = (encoded_op << 8) >> 20;
481 int cmparg = (encoded_op << 20) >> 20;
484 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
488 if (ldebug(sys_futex))
489 printf("futex_atomic_op: op = %d, cmp = %d, oparg = %x, "
490 "cmparg = %x, uaddr = %p\n",
491 op, cmp, oparg, cmparg, uaddr);
493 /* XXX: linux verifies access here and returns EFAULT */
497 ret = futex_xchgl(oparg, uaddr, &oldval);
500 ret = futex_addl(oparg, uaddr, &oldval);
503 ret = futex_orl(oparg, uaddr, &oldval);
506 ret = futex_andl(~oparg, uaddr, &oldval);
509 ret = futex_xorl(oparg, uaddr, &oldval);
520 case FUTEX_OP_CMP_EQ:
521 return (oldval == cmparg);
522 case FUTEX_OP_CMP_NE:
523 return (oldval != cmparg);
524 case FUTEX_OP_CMP_LT:
525 return (oldval < cmparg);
526 case FUTEX_OP_CMP_GE:
527 return (oldval >= cmparg);
528 case FUTEX_OP_CMP_LE:
529 return (oldval <= cmparg);
530 case FUTEX_OP_CMP_GT:
531 return (oldval > cmparg);