linux_futex.c (c2ede4b379d8fa04de02c36201538fd610763299) linux_futex.c (ad2056f2c46cd6f28b2f7a4bd0e1a72198b19eee)
1/* $NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $ */
1/* $NetBSD: linux_futex.c,v 1.5 2005/11/23 16:14:57 manu Exp $ */
2
3/*-
4 * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
2
3/*-
4 * Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
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
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
19 * permission.
20 *
19 * permission.
20 *
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,
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
23 * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
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.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36#if 0
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.
32 */
33
34#include <sys/cdefs.h>
35__FBSDID("$FreeBSD$");
36#if 0
37__KERNEL_RCSID(1, "$NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $");
37 __KERNEL_RCSID(1, "$NetBSD: linux_futex.c,v 1.5 2005/11/23 16:14:57 manu Exp $");
38#endif
39
40#include "opt_compat.h"
41
42#include <sys/param.h>
38#endif
39
40#include "opt_compat.h"
41
42#include <sys/param.h>
43#include <sys/types.h>
44#include <sys/time.h>
43#include <sys/systm.h>
45#include <sys/systm.h>
44#include <sys/imgact.h>
45#include <sys/kernel.h>
46#include <sys/ktr.h>
47#include <sys/lock.h>
48#include <sys/malloc.h>
49#include <sys/mutex.h>
50#include <sys/priv.h>
51#include <sys/proc.h>
52#include <sys/queue.h>
46#include <sys/proc.h>
47#include <sys/queue.h>
53#include <sys/sched.h>
54#include <sys/sx.h>
48#include <sys/lock.h>
49#include <sys/mutex.h>
50#include <sys/malloc.h>
55
56#ifdef COMPAT_LINUX32
57#include <machine/../linux32/linux.h>
58#include <machine/../linux32/linux32_proto.h>
59#else
60#include <machine/../linux/linux.h>
61#include <machine/../linux/linux_proto.h>
62#endif
63#include <compat/linux/linux_futex.h>
51
52#ifdef COMPAT_LINUX32
53#include <machine/../linux32/linux.h>
54#include <machine/../linux32/linux32_proto.h>
55#else
56#include <machine/../linux/linux.h>
57#include <machine/../linux/linux_proto.h>
58#endif
59#include <compat/linux/linux_futex.h>
64#include <compat/linux/linux_emul.h>
65#include <compat/linux/linux_util.h>
66
60
67MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
68MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futexes wp");
69
70struct futex;
71
72struct waiting_proc {
61struct futex;
62
63struct waiting_proc {
73 uint32_t wp_flags;
74 struct futex *wp_futex;
64 struct thread *wp_t;
65 struct futex *wp_new_futex;
75 TAILQ_ENTRY(waiting_proc) wp_list;
76};
66 TAILQ_ENTRY(waiting_proc) wp_list;
67};
77
78struct futex {
68struct futex {
79 struct sx f_lck;
80 uint32_t *f_uaddr;
81 uint32_t f_refcount;
69 void *f_uaddr;
70 int f_refcount;
82 LIST_ENTRY(futex) f_list;
83 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
84};
85
71 LIST_ENTRY(futex) f_list;
72 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
73};
74
86struct futex_list futex_list;
75LIST_HEAD(futex_list, futex) futex_list;
76struct mtx futex_mtx; /* this protects the LIST of futexes */
87
77
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)
78#define FUTEX_LOCK mtx_lock(&futex_mtx)
79#define FUTEX_UNLOCK mtx_unlock(&futex_mtx)
93
80
94struct mtx futex_mtx; /* protects the futex list */
95#define FUTEXES_LOCK mtx_lock(&futex_mtx)
96#define FUTEXES_UNLOCK mtx_unlock(&futex_mtx)
81#define FUTEX_LOCKED 1
82#define FUTEX_UNLOCKED 0
97
83
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 */
84#define FUTEX_SYSTEM_LOCK mtx_lock(&Giant)
85#define FUTEX_SYSTEM_UNLOCK mtx_unlock(&Giant)
102
86
103/* wp_flags */
104#define FUTEX_WP_REQUEUED 0x1 /* wp requeued - wp moved from wp_list
105 * of futex where thread sleep to wp_list
106 * of another futex.
107 */
108#define FUTEX_WP_REMOVED 0x2 /* wp is woken up and removed from futex
109 * wp_list to prevent double wakeup.
110 */
87static struct futex *futex_get(void *, int);
88static void futex_put(struct futex *);
89static int futex_sleep(struct futex *, struct thread *, unsigned long);
90static int futex_wake(struct futex *, int, struct futex *);
91#ifdef __i386__
92static int futex_atomic_op(struct thread *td, int encoded_op, caddr_t uaddr);
93#endif
111
112/* support.s */
94
95/* support.s */
113int futex_xchgl(int oparg, uint32_t *uaddr, int *oldval);
114int futex_addl(int oparg, uint32_t *uaddr, int *oldval);
115int futex_orl(int oparg, uint32_t *uaddr, int *oldval);
116int futex_andl(int oparg, uint32_t *uaddr, int *oldval);
117int futex_xorl(int oparg, uint32_t *uaddr, int *oldval);
96int futex_xchgl(int oparg, caddr_t uaddr, int *oldval);
97int futex_addl(int oparg, caddr_t uaddr, int *oldval);
98int futex_orl(int oparg, caddr_t uaddr, int *oldval);
99int futex_andnl(int oparg, caddr_t uaddr, int *oldval);
100int futex_xorl(int oparg, caddr_t uaddr, int *oldval);
118
101
119static void
120futex_put(struct futex *f, struct waiting_proc *wp)
102int
103linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
121{
104{
105 int val;
106 int ret;
107 struct l_timespec timeout = { 0, 0 };
108 int error = 0;
109 struct futex *f;
110 struct futex *newf;
111 int timeout_hz;
112 struct timeval tv = {0, 0};
113#ifdef __i386__
114 struct futex *f2;
115 int op_ret;
116#endif
122
117
123 FUTEX_ASSERT_LOCKED(f);
124 if (wp != NULL) {
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);
128 }
118#ifdef DEBUG
119 if (ldebug(sys_futex))
120 printf(ARGS(futex,"%p, %i, %i"), args->uaddr, args->op, args->val);
121#endif
129
122
130 FUTEXES_LOCK;
131 if (--f->f_refcount == 0) {
132 LIST_REMOVE(f, f_list);
133 FUTEXES_UNLOCK;
134 FUTEX_UNLOCK(f);
123 switch (args->op) {
124 case LINUX_FUTEX_WAIT:
125 FUTEX_SYSTEM_LOCK;
135
126
136 LINUX_CTR2(sys_futex, "futex_put destroy uaddr %p ref %d",
137 f->f_uaddr, f->f_refcount);
138 FUTEX_DESTROY(f);
139 free(f, M_FUTEX);
140 return;
141 }
127 if ((error = copyin(args->uaddr,
128 &val, sizeof(val))) != 0) {
129 FUTEX_SYSTEM_UNLOCK;
130 return error;
131 }
142
132
143 LINUX_CTR2(sys_futex, "futex_put uaddr %p ref %d",
144 f->f_uaddr, f->f_refcount);
145 FUTEXES_UNLOCK;
146 FUTEX_UNLOCK(f);
147}
133 if (val != args->val) {
134 FUTEX_SYSTEM_UNLOCK;
135 return EWOULDBLOCK;
136 }
148
137
149static int
150futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
151{
152 struct futex *f, *tmpf;
153
154 *newf = tmpf = NULL;
155
156retry:
157 FUTEXES_LOCK;
158 LIST_FOREACH(f, &futex_list, f_list) {
159 if (f->f_uaddr == uaddr) {
160 if (tmpf != NULL) {
161 FUTEX_UNLOCK(tmpf);
162 FUTEX_DESTROY(tmpf);
163 free(tmpf, M_FUTEX);
138 if (args->timeout != NULL) {
139 if ((error = copyin(args->timeout,
140 &timeout, sizeof(timeout))) != 0) {
141 FUTEX_SYSTEM_UNLOCK;
142 return error;
164 }
143 }
165 if (flags & FUTEX_DONTEXISTS) {
166 FUTEXES_UNLOCK;
167 return (EINVAL);
168 }
169
170 /*
171 * Increment refcount of the found futex to
172 * prevent it from deallocation before FUTEX_LOCK()
173 */
174 ++f->f_refcount;
175 FUTEXES_UNLOCK;
176
177 FUTEX_LOCK(f);
178 *newf = f;
179 LINUX_CTR2(sys_futex, "futex_get uaddr %p ref %d",
180 uaddr, f->f_refcount);
181 return (0);
182 }
144 }
183 }
184
145
185 if (flags & FUTEX_DONTCREATE) {
186 FUTEXES_UNLOCK;
187 LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
188 return (0);
189 }
146#ifdef DEBUG
147 if (ldebug(sys_futex))
148 printf("FUTEX_WAIT %d: val = %d, uaddr = %p, "
149 "*uaddr = %d, timeout = %d.%09ld\n",
150 td->td_proc->p_pid, args->val,
151 args->uaddr, val, timeout.tv_sec, timeout.tv_nsec);
152#endif
153 tv.tv_usec = timeout.tv_sec * 1000000 + timeout.tv_nsec / 1000;
154 timeout_hz = tvtohz(&tv);
190
155
191 if (tmpf == NULL) {
192 FUTEXES_UNLOCK;
193 tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
194 tmpf->f_uaddr = uaddr;
195 tmpf->f_refcount = 1;
196 FUTEX_INIT(tmpf);
197 TAILQ_INIT(&tmpf->f_waiting_proc);
156 if (timeout.tv_sec == 0 && timeout.tv_nsec == 0)
157 timeout_hz = 0;
158 /*
159 * If the user process requests a non null timeout,
160 * make sure we do not turn it into an infinite
161 * timeout because timeout_hz gets null.
162 *
163 * We use a minimal timeout of 1/hz. Maybe it would
164 * make sense to just return ETIMEDOUT without sleeping.
165 */
166 if (((timeout.tv_sec != 0) || (timeout.tv_nsec != 0)) &&
167 (timeout_hz == 0))
168 timeout_hz = 1;
198
169
199 /*
200 * Lock the new futex before an insert into the futex_list
201 * to prevent futex usage by other.
202 */
203 FUTEX_LOCK(tmpf);
204 goto retry;
205 }
206
170
207 LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
208 FUTEXES_UNLOCK;
171 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
172 ret = futex_sleep(f, td, timeout_hz);
173 futex_put(f);
209
174
210 LINUX_CTR2(sys_futex, "futex_get uaddr %p ref %d new",
211 uaddr, tmpf->f_refcount);
212 *newf = tmpf;
213 return (0);
214}
175#ifdef DEBUG
176 if (ldebug(sys_futex))
177 printf("FUTEX_WAIT %d: uaddr = %p, "
178 "ret = %d\n", td->td_proc->p_pid, args->uaddr, ret);
179#endif
215
180
216static int
217futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
218 uint32_t flags)
219{
220 int error;
221
222 if (flags & FUTEX_CREATE_WP) {
223 *wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
224 (*wp)->wp_flags = 0;
225 }
226 error = futex_get0(uaddr, f, flags);
227 if (error) {
228 if (flags & FUTEX_CREATE_WP)
229 free(*wp, M_FUTEX_WP);
230 return (error);
231 }
232 if (flags & FUTEX_CREATE_WP) {
233 TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
234 (*wp)->wp_futex = *f;
235 }
236
237 return (error);
238}
239
240static int
241futex_sleep(struct futex *f, struct waiting_proc *wp, unsigned long timeout)
242{
243 int error;
244
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);
255 futex_put(f, NULL);
256 f = wp->wp_futex;
257 FUTEX_LOCK(f);
258 } else
259 LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
260 error, f->f_uaddr, wp);
261
262 futex_put(f, wp);
263 return (error);
264}
265
266static int
267futex_wake(struct futex *f, int n)
268{
269 struct waiting_proc *wp, *wpt;
270 int count = 0;
271
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);
278 wakeup_one(wp);
279 if (++count == n)
181 FUTEX_SYSTEM_UNLOCK;
182 switch (ret) {
183 case EWOULDBLOCK: /* timeout */
184 return ETIMEDOUT;
280 break;
185 break;
281 }
282
283 return (count);
284}
285
286static int
287futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
288{
289 struct waiting_proc *wp, *wpt;
290 int count = 0;
291
292 FUTEX_ASSERT_LOCKED(f);
293 FUTEX_ASSERT_LOCKED(f2);
294
295 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
296 if (++count <= n) {
297 LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
298 f->f_uaddr, wp);
299 wp->wp_flags |= FUTEX_WP_REMOVED;
300 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
301 wakeup_one(wp);
302 } else {
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);
309
310 /*
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.
314 */
315 wp->wp_futex = f2;
316 FUTEXES_LOCK;
317 ++f2->f_refcount;
318 FUTEXES_UNLOCK;
319 if (count - n >= n2)
320 break;
321 }
322 }
323
324 return (count);
325}
326
327static int
328futex_wait(struct futex *f, struct waiting_proc *wp, struct l_timespec *ts)
329{
330 struct l_timespec timeout = {0, 0};
331 struct timeval tv = {0, 0};
332 int timeout_hz;
333 int error;
334
335 if (ts != NULL) {
336 error = copyin(ts, &timeout, sizeof(timeout));
337 if (error)
338 return (error);
339 }
340
341 tv.tv_usec = timeout.tv_sec * 1000000 + timeout.tv_nsec / 1000;
342 timeout_hz = tvtohz(&tv);
343
344 if (timeout.tv_sec == 0 && timeout.tv_nsec == 0)
345 timeout_hz = 0;
346
347 /*
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.
351 *
352 * We use a minimal timeout of 1/hz. Maybe it would
353 * make sense to just return ETIMEDOUT without sleeping.
354 */
355 if (((timeout.tv_sec != 0) || (timeout.tv_nsec != 0)) &&
356 (timeout_hz == 0))
357 timeout_hz = 1;
358
359 error = futex_sleep(f, wp, timeout_hz);
360 if (error == EWOULDBLOCK)
361 error = ETIMEDOUT;
362
363 return (error);
364}
365
366static int
367futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
368{
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;
373 int oldval = 0, ret;
374
375 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
376 oparg = 1 << oparg;
377
186 case EINTR: /* signal */
187 return EINTR;
188 break;
189 case 0: /* FUTEX_WAKE received */
378#ifdef DEBUG
190#ifdef DEBUG
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);
191 if (ldebug(sys_futex))
192 printf("FUTEX_WAIT %d: uaddr = %p, got FUTEX_WAKE\n",
193 td->td_proc->p_pid, args->uaddr);
383#endif
194#endif
384 /* XXX: linux verifies access here and returns EFAULT */
385
386 switch (op) {
387 case FUTEX_OP_SET:
388 ret = futex_xchgl(oparg, uaddr, &oldval);
389 break;
390 case FUTEX_OP_ADD:
391 ret = futex_addl(oparg, uaddr, &oldval);
392 break;
393 case FUTEX_OP_OR:
394 ret = futex_orl(oparg, uaddr, &oldval);
395 break;
396 case FUTEX_OP_ANDN:
397 ret = futex_andl(~oparg, uaddr, &oldval);
398 break;
399 case FUTEX_OP_XOR:
400 ret = futex_xorl(oparg, uaddr, &oldval);
401 break;
402 default:
403 ret = -ENOSYS;
404 break;
405 }
406
407 if (ret)
408 return (ret);
409
410 switch (cmp) {
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);
423 default:
424 return (-ENOSYS);
425 }
426}
427
428int
429linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
430{
431 int op_ret, val, ret, nrwake;
432 struct linux_emuldata *em;
433 struct waiting_proc *wp;
434 struct futex *f, *f2;
435 int error = 0;
436
437 /*
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.).
443 */
444 args->op = (args->op & ~LINUX_FUTEX_PRIVATE_FLAG);
445
446 switch (args->op) {
447 case LINUX_FUTEX_WAIT:
448
449 LINUX_CTR2(sys_futex, "WAIT val %d uaddr %p",
450 args->val, args->uaddr);
195 return 0;
196 break;
197 default:
451#ifdef DEBUG
198#ifdef DEBUG
452 if (ldebug(sys_futex))
453 printf(ARGS(sys_futex, "futex_wait val %d uaddr %p"),
454 args->val, args->uaddr);
199 if (ldebug(sys_futex))
200 printf("FUTEX_WAIT: unexpected ret = %d\n", ret);
455#endif
201#endif
456 error = futex_get(args->uaddr, &wp, &f, FUTEX_CREATE_WP);
457 if (error)
458 return (error);
459 error = copyin(args->uaddr, &val, sizeof(val));
460 if (error) {
461 LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
462 error);
463 futex_put(f, wp);
464 return (error);
202 break;
465 }
203 }
466 if (val != args->val) {
467 LINUX_CTR3(sys_futex, "WAIT uaddr %p val %d != uval %d",
468 args->uaddr, args->val, val);
469 futex_put(f, wp);
470 return (EWOULDBLOCK);
471 }
472
204
473 error = futex_wait(f, wp, args->timeout);
205 /* NOTREACHED */
474 break;
206 break;
475
207
476 case LINUX_FUTEX_WAKE:
208 case LINUX_FUTEX_WAKE:
209 FUTEX_SYSTEM_LOCK;
477
210
478 LINUX_CTR2(sys_futex, "WAKE val %d uaddr %p",
479 args->val, args->uaddr);
480
481 /*
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).
211 /*
212 * XXX: Linux is able cope with different addresses
213 * corresponding to the same mapped memory in the sleeping
214 * and the waker process.
485 */
486#ifdef DEBUG
487 if (ldebug(sys_futex))
215 */
216#ifdef DEBUG
217 if (ldebug(sys_futex))
488 printf(ARGS(sys_futex, "futex_wake val %d uaddr %p"),
489 args->val, args->uaddr);
218 printf("FUTEX_WAKE %d: uaddr = %p, val = %d\n",
219 td->td_proc->p_pid, args->uaddr, args->val);
490#endif
220#endif
491 error = futex_get(args->uaddr, NULL, &f, FUTEX_DONTCREATE);
492 if (error)
493 return (error);
494 if (f == NULL) {
495 td->td_retval[0] = 0;
496 return (error);
497 }
498 td->td_retval[0] = futex_wake(f, args->val);
499 futex_put(f, NULL);
221 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
222 td->td_retval[0] = futex_wake(f, args->val, NULL);
223 futex_put(f);
224
225 FUTEX_SYSTEM_UNLOCK;
500 break;
501
502 case LINUX_FUTEX_CMP_REQUEUE:
226 break;
227
228 case LINUX_FUTEX_CMP_REQUEUE:
229 FUTEX_SYSTEM_LOCK;
503
230
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);
508
509#ifdef DEBUG
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);
515#endif
516
517 /*
518 * Linux allows this, we would not, it is an incorrect
519 * usage of declared ABI, so return EINVAL.
520 */
521 if (args->uaddr == args->uaddr2)
522 return (EINVAL);
523 error = futex_get0(args->uaddr, &f, 0);
524 if (error)
525 return (error);
526
527 /*
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.
531 *
532 * Glibc fall back to FUTEX_WAKE in case of any error
533 * returned by FUTEX_CMP_REQUEUE.
534 */
535 error = futex_get0(args->uaddr2, &f2, FUTEX_DONTEXISTS);
536 if (error) {
537 futex_put(f, NULL);
538 return (error);
231 if ((error = copyin(args->uaddr,
232 &val, sizeof(val))) != 0) {
233 FUTEX_SYSTEM_UNLOCK;
234 return error;
539 }
235 }
540 error = copyin(args->uaddr, &val, sizeof(val));
541 if (error) {
542 LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
543 error);
544 futex_put(f2, NULL);
545 futex_put(f, NULL);
546 return (error);
547 }
236
548 if (val != args->val3) {
237 if (val != args->val3) {
549 LINUX_CTR2(sys_futex, "CMP_REQUEUE val %d != uval %d",
550 args->val, val);
551 futex_put(f2, NULL);
552 futex_put(f, NULL);
553 return (EAGAIN);
238 FUTEX_SYSTEM_UNLOCK;
239 return EAGAIN;
554 }
555
240 }
241
556 nrwake = (int)(unsigned long)args->timeout;
557 td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
558 futex_put(f2, NULL);
559 futex_put(f, NULL);
242 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
243 newf = futex_get(args->uaddr2, FUTEX_UNLOCKED);
244 td->td_retval[0] = futex_wake(f, args->val, newf);
245 futex_put(f);
246 futex_put(newf);
247
248 FUTEX_SYSTEM_UNLOCK;
560 break;
561
249 break;
250
562 case LINUX_FUTEX_WAKE_OP:
251 case LINUX_FUTEX_REQUEUE:
252 FUTEX_SYSTEM_LOCK;
253
254 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
255 newf = futex_get(args->uaddr2, FUTEX_UNLOCKED);
256 td->td_retval[0] = futex_wake(f, args->val, newf);
257 futex_put(f);
258 futex_put(newf);
259
260 FUTEX_SYSTEM_UNLOCK;
261 break;
563
262
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);
263 case LINUX_FUTEX_FD:
264 printf("linux_sys_futex: unimplemented op %d\n",
265 args->op);
266 break;
568
267
268 case LINUX_FUTEX_WAKE_OP:
269#ifdef __i386__
270 FUTEX_SYSTEM_LOCK;
569#ifdef DEBUG
570 if (ldebug(sys_futex))
271#ifdef DEBUG
272 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);
273 printf("FUTEX_WAKE_OP: %d: uaddr = %p, op = %d, val = %d, uaddr2 = %p, val3 = %d\n",
274 td->td_proc->p_pid, args->uaddr, args->op, args->val, args->uaddr2, args->val3);
575#endif
275#endif
576 error = futex_get0(args->uaddr, &f, 0);
577 if (error)
578 return (error);
579 if (args->uaddr != args->uaddr2)
580 error = futex_get0(args->uaddr2, &f2, 0);
581 if (error) {
582 futex_put(f, NULL);
583 return (error);
584 }
276 f = futex_get(args->uaddr, FUTEX_UNLOCKED);
277 f2 = futex_get(args->uaddr2, FUTEX_UNLOCKED);
585
278
586 /*
587 * This function returns positive number as results and
588 * negative as errors
279 /* This function returns positive number as results
280 * and negative as errors
589 */
590 op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
281 */
282 op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
591
592 if (op_ret < 0) {
283 if (op_ret < 0) {
593 /* XXX: We don't handle the EFAULT yet. */
594 if (op_ret != -EFAULT) {
595 if (f2 != NULL)
596 futex_put(f2, NULL);
597 futex_put(f, NULL);
598 return (-op_ret);
284
285 /* XXX: we dont handle the EFAULT yet */
286 if (op_ret != -EFAULT) {
287 futex_put(f);
288 futex_put(f2);
289 FUTEX_SYSTEM_UNLOCK;
290 return (-op_ret);
599 }
291 }
600 if (f2 != NULL)
601 futex_put(f2, NULL);
602 futex_put(f, NULL);
292
293 futex_put(f);
294 futex_put(f2);
295
296 FUTEX_SYSTEM_UNLOCK;
603 return (EFAULT);
297 return (EFAULT);
298
604 }
605
299 }
300
606 ret = futex_wake(f, args->val);
607
301 ret = futex_wake(f, args->val, NULL);
302 futex_put(f);
608 if (op_ret > 0) {
303 if (op_ret > 0) {
609 op_ret = 0;
610 nrwake = (int)(unsigned long)args->timeout;
611
612 if (f2 != NULL)
613 op_ret += futex_wake(f2, nrwake);
614 else
615 op_ret += futex_wake(f, nrwake);
304 printf("second wakeup\n");
305 op_ret = 0;
306 /* Linux always puts there 0 retries */
307 op_ret += futex_wake(f2, 0, NULL);
616 ret += op_ret;
308 ret += op_ret;
617
618 }
309 }
619 if (f2 != NULL)
620 futex_put(f2, NULL);
621 futex_put(f, NULL);
310 futex_put(f2);
622 td->td_retval[0] = ret;
311 td->td_retval[0] = ret;
623 break;
624
312
625 case LINUX_FUTEX_LOCK_PI:
626 /* not yet implemented */
627 return (ENOSYS);
313 FUTEX_SYSTEM_UNLOCK;
314#else
315 printf("linux_sys_futex: wake_op not implemented");
316#endif
317 break;
628
318
629 case LINUX_FUTEX_UNLOCK_PI:
630 /* not yet implemented */
631 return (ENOSYS);
632
633 case LINUX_FUTEX_TRYLOCK_PI:
634 /* not yet implemented */
635 return (ENOSYS);
636
637 case LINUX_FUTEX_REQUEUE:
638
639 /*
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.
644 */
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;
651 }
652 return (EINVAL);
653
654 default:
319 default:
655 printf("linux_sys_futex: unknown op %d\n", args->op);
656 return (ENOSYS);
320 printf("linux_sys_futex: unknown op %d\n",
321 args->op);
322 break;
657 }
323 }
658
659 return (error);
324 return 0;
660}
661
325}
326
662int
663linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
327static struct futex *
328futex_get(void *uaddr, int locked)
664{
329{
665 struct linux_emuldata *em;
330 struct futex *f;
666
331
667#ifdef DEBUG
668 if (ldebug(set_robust_list))
669 printf(ARGS(set_robust_list, "head %p len %d"),
670 args->head, args->len);
671#endif
332 if (locked == FUTEX_UNLOCKED)
333 FUTEX_LOCK;
334 LIST_FOREACH(f, &futex_list, f_list) {
335 if (f->f_uaddr == uaddr) {
336 f->f_refcount++;
337 if (locked == FUTEX_UNLOCKED)
338 FUTEX_UNLOCK;
339 return f;
340 }
341 }
342 if (locked == FUTEX_UNLOCKED)
343 FUTEX_UNLOCK;
672
344
673 if (args->len != sizeof(struct linux_robust_list_head))
674 return (EINVAL);
345 /* Not found, create it */
346 f = malloc(sizeof(*f), M_LINUX, M_WAITOK);
347 f->f_uaddr = uaddr;
348 f->f_refcount = 1;
349 TAILQ_INIT(&f->f_waiting_proc);
350 if (locked == FUTEX_UNLOCKED)
351 FUTEX_LOCK;
352 LIST_INSERT_HEAD(&futex_list, f, f_list);
353 if (locked == FUTEX_UNLOCKED)
354 FUTEX_UNLOCK;
675
355
676 em = em_find(td->td_proc, EMUL_DOLOCK);
677 em->robust_futexes = args->head;
678 EMUL_UNLOCK(&emul_lock);
356 return f;
357}
679
358
680 return (0);
359static void
360futex_put(f)
361 struct futex *f;
362{
363 FUTEX_LOCK;
364 f->f_refcount--;
365 if (f->f_refcount == 0) {
366 LIST_REMOVE(f, f_list);
367 free(f, M_LINUX);
368 }
369 FUTEX_UNLOCK;
370
371 return;
681}
682
372}
373
683int
684linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
374static int
375futex_sleep(struct futex *f, struct thread *td, unsigned long timeout)
685{
376{
686 struct linux_emuldata *em;
687 struct linux_robust_list_head *head;
688 l_size_t len = sizeof(struct linux_robust_list_head);
689 int error = 0;
377 struct waiting_proc *wp;
378 int ret;
690
379
691#ifdef DEBUG
692 if (ldebug(get_robust_list))
693 printf(ARGS(get_robust_list, ""));
380 wp = malloc(sizeof(*wp), M_LINUX, M_WAITOK);
381 wp->wp_t = td;
382 wp->wp_new_futex = NULL;
383 FUTEX_LOCK;
384 TAILQ_INSERT_TAIL(&f->f_waiting_proc, wp, wp_list);
385 FUTEX_UNLOCK;
386
387#ifdef DEBUG
388 if (ldebug(sys_futex))
389 printf("FUTEX --> %d tlseep timeout = %ld\n", td->td_proc->p_pid,
390 timeout);
694#endif
391#endif
392 ret = tsleep(wp, PCATCH|PZERO, "linuxfutex", timeout);
695
393
696 if (!args->pid) {
697 em = em_find(td->td_proc, EMUL_DONTLOCK);
698 head = em->robust_futexes;
699 } else {
700 struct proc *p;
394 FUTEX_LOCK;
395 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
396 FUTEX_UNLOCK;
701
397
702 p = pfind(args->pid);
703 if (p == NULL)
704 return (ESRCH);
705
706 em = em_find(p, EMUL_DONTLOCK);
707 /* XXX: ptrace? */
708 if (priv_check(td, PRIV_CRED_SETUID) ||
709 priv_check(td, PRIV_CRED_SETEUID) ||
710 p_candebug(td, p)) {
711 PROC_UNLOCK(p);
712 return (EPERM);
713 }
714 head = em->robust_futexes;
715
716 PROC_UNLOCK(p);
398 if ((ret == 0) && (wp->wp_new_futex != NULL)) {
399 ret = futex_sleep(wp->wp_new_futex, td, timeout);
400 futex_put(wp->wp_new_futex); /* futex_get called in wakeup */
717 }
718
401 }
402
719 error = copyout(&len, args->len, sizeof(l_size_t));
720 if (error)
721 return (EFAULT);
403 free(wp, M_LINUX);
722
404
723 error = copyout(head, args->head, sizeof(struct linux_robust_list_head));
724
725 return (error);
405 return ret;
726}
727
728static int
406}
407
408static int
729handle_futex_death(struct proc *p, uint32_t *uaddr, int pi)
409futex_wake(struct futex *f, int n, struct futex *newf)
730{
410{
731 uint32_t uval, nval, mval;
732 struct futex *f;
733 int error;
411 struct waiting_proc *wp;
412 int count = 0;
734
413
735retry:
736 if (copyin(uaddr, &uval, 4))
737 return (EFAULT);
738 if ((uval & FUTEX_TID_MASK) == p->p_pid) {
739 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
740 nval = casuword32(uaddr, uval, mval);
741
742 if (nval == -1)
743 return (EFAULT);
744
745 if (nval != uval)
746 goto retry;
747
748 if (!pi && (uval & FUTEX_WAITERS)) {
749 error = futex_get(uaddr, NULL, &f,
750 FUTEX_DONTCREATE);
751 if (error)
752 return (error);
753 if (f != NULL) {
754 futex_wake(f, 1);
755 futex_put(f, NULL);
414 FUTEX_LOCK;
415 TAILQ_FOREACH(wp, &f->f_waiting_proc, wp_list) {
416 if (count <= n) {
417 wakeup(wp);
418 count++;
419 } else {
420 if (newf != NULL) {
421 /* futex_put called after tsleep */
422 wp->wp_new_futex = futex_get(newf->f_uaddr, FUTEX_LOCKED);
423 wakeup(wp);
756 }
757 }
758 }
424 }
425 }
426 }
427 FUTEX_UNLOCK;
759
428
760 return (0);
429 return count;
761}
762
430}
431
432#ifdef __i386__
763static int
433static int
764fetch_robust_entry(struct linux_robust_list **entry,
765 struct linux_robust_list **head, int *pi)
434futex_atomic_op(struct thread *td, int encoded_op, caddr_t uaddr)
766{
435{
767 l_ulong uentry;
436 int op = (encoded_op >> 28) & 7;
437 int cmp = (encoded_op >> 24) & 15;
438 int oparg = (encoded_op << 8) >> 20;
439 int cmparg = (encoded_op << 20) >> 20;
440 int oldval = 0, ret;
768
441
769 if (copyin((const void *)head, &uentry, sizeof(l_ulong)))
770 return (EFAULT);
442 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
443 oparg = 1 << oparg;
771
444
772 *entry = (void *)(uentry & ~1UL);
773 *pi = uentry & 1;
445#ifdef DEBUG
446 printf("futex_atomic_op: op = %d, cmp = %d, oparg = %d, cmparg = %d, uaddr = %p\n",
447 op, cmp, oparg, cmparg, uaddr);
448#endif
449 /* XXX: linux verifies access here and returns EFAULT */
774
450
775 return (0);
776}
451 critical_enter();
777
452
778/* This walks the list of robust futexes releasing them. */
779void
780release_futexes(struct proc *p)
781{
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;
786 l_long futex_offset;
787 int rc;
788
789 em = em_find(p, EMUL_DONTLOCK);
790 head = em->robust_futexes;
791
792 if (head == NULL)
793 return;
794
795 if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi))
796 return;
797
798 if (copyin(&head->futex_offset, &futex_offset, sizeof(futex_offset)))
799 return;
800
801 if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip))
802 return;
803
804 while (entry != &head->list) {
805 rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
806
807 if (entry != pending)
808 if (handle_futex_death(p, (uint32_t *)entry + futex_offset, pi))
809 return;
810 if (rc)
811 return;
812
813 entry = next_entry;
814 pi = next_pi;
815
816 if (!--limit)
453 switch (op) {
454 case FUTEX_OP_SET:
455 ret = futex_xchgl(oparg, uaddr, &oldval);
817 break;
456 break;
818
819 sched_relinquish(curthread);
457 case FUTEX_OP_ADD:
458 ret = futex_addl(oparg, uaddr, &oldval);
459 break;
460 case FUTEX_OP_OR:
461 ret = futex_orl(oparg, uaddr, &oldval);
462 break;
463 case FUTEX_OP_ANDN:
464 ret = futex_andnl(oparg, uaddr, &oldval);
465 break;
466 case FUTEX_OP_XOR:
467 ret = futex_xorl(oparg, uaddr, &oldval);
468 break;
469 default:
470 ret = -ENOSYS;
820 }
821
471 }
472
822 if (pending)
823 handle_futex_death(p, (uint32_t *)pending + futex_offset, pip);
473 critical_exit();
474
475 if (!ret)
476 switch (cmp) {
477 case FUTEX_OP_CMP_EQ:
478 ret = (oldval == cmparg);
479 break;
480 case FUTEX_OP_CMP_NE:
481 ret = (oldval != cmparg);
482 break;
483 case FUTEX_OP_CMP_LT:
484 ret = (oldval < cmparg);
485 break;
486 case FUTEX_OP_CMP_GE:
487 ret = (oldval >= cmparg);
488 break;
489 case FUTEX_OP_CMP_LE:
490 ret = (oldval <= cmparg);
491 break;
492 case FUTEX_OP_CMP_GT:
493 ret = (oldval > cmparg);
494 break;
495 default: ret = -ENOSYS;
496 }
497
498 return (ret);
824}
499}
500#endif