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