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: releng/10.3/sys/compat/linux/linux_futex.c 293897 2016-01-14 09:11:42Z glebius $");
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 #include "opt_kdtrace.h"
42
43 #include <sys/param.h>
44 #include <sys/systm.h>
45 #include <sys/imgact.h>
46 #include <sys/kernel.h>
47 #include <sys/ktr.h>
48 #include <sys/lock.h>
49 #include <sys/malloc.h>
50 #include <sys/mutex.h>
51 #include <sys/priv.h>
52 #include <sys/proc.h>
53 #include <sys/queue.h>
54 #include <sys/sched.h>
55 #include <sys/sdt.h>
56 #include <sys/sx.h>
57 #include <sys/umtx.h>
58
59 #ifdef COMPAT_LINUX32
60 #include <machine/../linux32/linux.h>
61 #include <machine/../linux32/linux32_proto.h>
62 #else
63 #include <machine/../linux/linux.h>
64 #include <machine/../linux/linux_proto.h>
65 #endif
66 #include <compat/linux/linux_dtrace.h>
67 #include <compat/linux/linux_emul.h>
68 #include <compat/linux/linux_futex.h>
69 #include <compat/linux/linux_timer.h>
70 #include <compat/linux/linux_util.h>
71
72 /* DTrace init */
73 LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
74
75 /**
76 * Futex part for the special DTrace module "locks".
77 */
78 LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, locked, "struct mtx *");
79 LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, unlock, "struct mtx *");
80
81 /**
82 * Per futex probes.
83 */
84 LIN_SDT_PROBE_DEFINE1(futex, futex, create, "struct sx *");
85 LIN_SDT_PROBE_DEFINE1(futex, futex, destroy, "struct sx *");
86
87 /**
88 * DTrace probes in this module.
89 */
90 LIN_SDT_PROBE_DEFINE2(futex, futex_put, entry, "struct futex *",
91 "struct waiting_proc *");
92 LIN_SDT_PROBE_DEFINE3(futex, futex_put, destroy, "uint32_t *", "uint32_t",
93 "int");
94 LIN_SDT_PROBE_DEFINE3(futex, futex_put, unlock, "uint32_t *", "uint32_t",
95 "int");
96 LIN_SDT_PROBE_DEFINE0(futex, futex_put, return);
97 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, entry, "uint32_t *", "struct futex **",
98 "uint32_t");
99 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, umtx_key_get_error, "int");
100 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, shared, "uint32_t *", "uint32_t",
101 "int");
102 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, null, "uint32_t *");
103 LIN_SDT_PROBE_DEFINE3(futex, futex_get0, new, "uint32_t *", "uint32_t", "int");
104 LIN_SDT_PROBE_DEFINE1(futex, futex_get0, return, "int");
105 LIN_SDT_PROBE_DEFINE3(futex, futex_get, entry, "uint32_t *",
106 "struct waiting_proc **", "struct futex **");
107 LIN_SDT_PROBE_DEFINE0(futex, futex_get, error);
108 LIN_SDT_PROBE_DEFINE1(futex, futex_get, return, "int");
109 LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, entry, "struct futex *",
110 "struct waiting_proc **", "int");
111 LIN_SDT_PROBE_DEFINE5(futex, futex_sleep, requeue_error, "int", "uint32_t *",
112 "struct waiting_proc *", "uint32_t *", "uint32_t");
113 LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, sleep_error, "int", "uint32_t *",
114 "struct waiting_proc *");
115 LIN_SDT_PROBE_DEFINE1(futex, futex_sleep, return, "int");
116 LIN_SDT_PROBE_DEFINE3(futex, futex_wake, entry, "struct futex *", "int",
117 "uint32_t");
118 LIN_SDT_PROBE_DEFINE3(futex, futex_wake, iterate, "uint32_t",
119 "struct waiting_proc *", "uint32_t");
120 LIN_SDT_PROBE_DEFINE1(futex, futex_wake, wakeup, "struct waiting_proc *");
121 LIN_SDT_PROBE_DEFINE1(futex, futex_wake, return, "int");
122 LIN_SDT_PROBE_DEFINE4(futex, futex_requeue, entry, "struct futex *", "int",
123 "struct futex *", "int");
124 LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, wakeup, "struct waiting_proc *");
125 LIN_SDT_PROBE_DEFINE3(futex, futex_requeue, requeue, "uint32_t *",
126 "struct waiting_proc *", "uint32_t");
127 LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, return, "int");
128 LIN_SDT_PROBE_DEFINE4(futex, futex_wait, entry, "struct futex *",
129 "struct waiting_proc **", "int", "uint32_t");
130 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, sleep_error, "int");
131 LIN_SDT_PROBE_DEFINE1(futex, futex_wait, return, "int");
132 LIN_SDT_PROBE_DEFINE3(futex, futex_atomic_op, entry, "struct thread *",
133 "int", "uint32_t");
134 LIN_SDT_PROBE_DEFINE4(futex, futex_atomic_op, decoded_op, "int", "int", "int",
135 "int");
136 LIN_SDT_PROBE_DEFINE0(futex, futex_atomic_op, missing_access_check);
137 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_op, "int");
138 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_cmp, "int");
139 LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, return, "int");
140 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, entry, "struct thread *",
141 "struct linux_sys_futex_args *");
142 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_clockswitch);
143 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, itimerfix_error, "int");
144 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, copyin_error, "int");
145 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, invalid_cmp_requeue_use);
146 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wait, "uint32_t *",
147 "uint32_t", "uint32_t");
148 LIN_SDT_PROBE_DEFINE4(futex, linux_sys_futex, debug_wait_value_neq,
149 "uint32_t *", "uint32_t", "int", "uint32_t");
150 LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wake, "uint32_t *",
151 "uint32_t", "uint32_t");
152 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_cmp_requeue, "uint32_t *",
153 "uint32_t", "uint32_t", "uint32_t *", "struct l_timespec *");
154 LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, debug_cmp_requeue_value_neq,
155 "uint32_t", "int");
156 LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_wake_op, "uint32_t *",
157 "int", "uint32_t", "uint32_t *", "uint32_t");
158 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unhandled_efault);
159 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_lock_pi);
160 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_unlock_pi);
161 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_trylock_pi);
162 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, deprecated_requeue);
163 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_wait_requeue_pi);
164 LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_cmp_requeue_pi);
165 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, unknown_operation, "int");
166 LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, return, "int");
167 LIN_SDT_PROBE_DEFINE2(futex, linux_set_robust_list, entry, "struct thread *",
168 "struct linux_set_robust_list_args *");
169 LIN_SDT_PROBE_DEFINE0(futex, linux_set_robust_list, size_error);
170 LIN_SDT_PROBE_DEFINE1(futex, linux_set_robust_list, return, "int");
171 LIN_SDT_PROBE_DEFINE2(futex, linux_get_robust_list, entry, "struct thread *",
172 "struct linux_get_robust_list_args *");
173 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, copyout_error, "int");
174 LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, return, "int");
175 LIN_SDT_PROBE_DEFINE3(futex, handle_futex_death, entry,
176 "struct linux_emuldata *", "uint32_t *", "unsigned int");
177 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, copyin_error, "int");
178 LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, return, "int");
179 LIN_SDT_PROBE_DEFINE3(futex, fetch_robust_entry, entry,
180 "struct linux_robust_list **", "struct linux_robust_list **",
181 "unsigned int *");
182 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, copyin_error, "int");
183 LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, return, "int");
184 LIN_SDT_PROBE_DEFINE2(futex, release_futexes, entry, "struct thread *",
185 "struct linux_emuldata *");
186 LIN_SDT_PROBE_DEFINE1(futex, release_futexes, copyin_error, "int");
187 LIN_SDT_PROBE_DEFINE0(futex, release_futexes, return);
188
189 struct futex;
190
191 struct waiting_proc {
192 uint32_t wp_flags;
193 struct futex *wp_futex;
194 TAILQ_ENTRY(waiting_proc) wp_list;
195 };
196
197 struct futex {
198 struct sx f_lck;
199 uint32_t *f_uaddr; /* user-supplied value, for debug */
200 struct umtx_key f_key;
201 uint32_t f_refcount;
202 uint32_t f_bitset;
203 LIST_ENTRY(futex) f_list;
204 TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
205 };
206
207 struct futex_list futex_list;
208
209 #define FUTEX_LOCK(f) sx_xlock(&(f)->f_lck)
210 #define FUTEX_UNLOCK(f) sx_xunlock(&(f)->f_lck)
211 #define FUTEX_INIT(f) do { \
212 sx_init_flags(&(f)->f_lck, "ftlk", \
213 SX_DUPOK); \
214 LIN_SDT_PROBE1(futex, futex, create, \
215 &(f)->f_lck); \
216 } while (0)
217 #define FUTEX_DESTROY(f) do { \
218 LIN_SDT_PROBE1(futex, futex, destroy, \
219 &(f)->f_lck); \
220 sx_destroy(&(f)->f_lck); \
221 } while (0)
222 #define FUTEX_ASSERT_LOCKED(f) sx_assert(&(f)->f_lck, SA_XLOCKED)
223
224 struct mtx futex_mtx; /* protects the futex list */
225 #define FUTEXES_LOCK do { \
226 mtx_lock(&futex_mtx); \
227 LIN_SDT_PROBE1(locks, futex_mtx, \
228 locked, &futex_mtx); \
229 } while (0)
230 #define FUTEXES_UNLOCK do { \
231 LIN_SDT_PROBE1(locks, futex_mtx, \
232 unlock, &futex_mtx); \
233 mtx_unlock(&futex_mtx); \
234 } while (0)
235
236 /* flags for futex_get() */
237 #define FUTEX_CREATE_WP 0x1 /* create waiting_proc */
238 #define FUTEX_DONTCREATE 0x2 /* don't create futex if not exists */
239 #define FUTEX_DONTEXISTS 0x4 /* return EINVAL if futex exists */
240 #define FUTEX_SHARED 0x8 /* shared futex */
241
242 /* wp_flags */
243 #define FUTEX_WP_REQUEUED 0x1 /* wp requeued - wp moved from wp_list
244 * of futex where thread sleep to wp_list
245 * of another futex.
246 */
247 #define FUTEX_WP_REMOVED 0x2 /* wp is woken up and removed from futex
248 * wp_list to prevent double wakeup.
249 */
250
251 static void futex_put(struct futex *, struct waiting_proc *);
252 static int futex_get0(uint32_t *, struct futex **f, uint32_t);
253 static int futex_get(uint32_t *, struct waiting_proc **, struct futex **,
254 uint32_t);
255 static int futex_sleep(struct futex *, struct waiting_proc *, int);
256 static int futex_wake(struct futex *, int, uint32_t);
257 static int futex_requeue(struct futex *, int, struct futex *, int);
258 static int futex_wait(struct futex *, struct waiting_proc *, int,
259 uint32_t);
260 static int futex_atomic_op(struct thread *, int, uint32_t *);
261 static int handle_futex_death(struct linux_emuldata *, uint32_t *,
262 unsigned int);
263 static int fetch_robust_entry(struct linux_robust_list **,
264 struct linux_robust_list **, unsigned int *);
265
266 /* support.s */
267 int futex_xchgl(int oparg, uint32_t *uaddr, int *oldval);
268 int futex_addl(int oparg, uint32_t *uaddr, int *oldval);
269 int futex_orl(int oparg, uint32_t *uaddr, int *oldval);
270 int futex_andl(int oparg, uint32_t *uaddr, int *oldval);
271 int futex_xorl(int oparg, uint32_t *uaddr, int *oldval);
272
273
274 static void
275 futex_put(struct futex *f, struct waiting_proc *wp)
276 {
277 LIN_SDT_PROBE2(futex, futex_put, entry, f, wp);
278
279 FUTEX_ASSERT_LOCKED(f);
280 if (wp != NULL) {
281 if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
282 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
283 free(wp, M_FUTEX_WP);
284 }
285
286 FUTEXES_LOCK;
287 if (--f->f_refcount == 0) {
288 LIST_REMOVE(f, f_list);
289 FUTEXES_UNLOCK;
290 FUTEX_UNLOCK(f);
291
292 LIN_SDT_PROBE3(futex, futex_put, destroy, f->f_uaddr,
293 f->f_refcount, f->f_key.shared);
294 LINUX_CTR3(sys_futex, "futex_put destroy uaddr %p ref %d "
295 "shared %d", f->f_uaddr, f->f_refcount, f->f_key.shared);
296 umtx_key_release(&f->f_key);
297 FUTEX_DESTROY(f);
298 free(f, M_FUTEX);
299
300 LIN_SDT_PROBE0(futex, futex_put, return);
301 return;
302 }
303
304 LIN_SDT_PROBE3(futex, futex_put, unlock, f->f_uaddr, f->f_refcount,
305 f->f_key.shared);
306 LINUX_CTR3(sys_futex, "futex_put uaddr %p ref %d shared %d",
307 f->f_uaddr, f->f_refcount, f->f_key.shared);
308 FUTEXES_UNLOCK;
309 FUTEX_UNLOCK(f);
310
311 LIN_SDT_PROBE0(futex, futex_put, return);
312 }
313
314 static int
315 futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
316 {
317 struct futex *f, *tmpf;
318 struct umtx_key key;
319 int error;
320
321 LIN_SDT_PROBE3(futex, futex_get0, entry, uaddr, newf, flags);
322
323 *newf = tmpf = NULL;
324
325 error = umtx_key_get(uaddr, TYPE_FUTEX, (flags & FUTEX_SHARED) ?
326 AUTO_SHARE : THREAD_SHARE, &key);
327 if (error) {
328 LIN_SDT_PROBE1(futex, futex_get0, umtx_key_get_error, error);
329 LIN_SDT_PROBE1(futex, futex_get0, return, error);
330 return (error);
331 }
332 retry:
333 FUTEXES_LOCK;
334 LIST_FOREACH(f, &futex_list, f_list) {
335 if (umtx_key_match(&f->f_key, &key)) {
336 if (tmpf != NULL) {
337 FUTEX_UNLOCK(tmpf);
338 FUTEX_DESTROY(tmpf);
339 free(tmpf, M_FUTEX);
340 }
341 if (flags & FUTEX_DONTEXISTS) {
342 FUTEXES_UNLOCK;
343 umtx_key_release(&key);
344
345 LIN_SDT_PROBE1(futex, futex_get0, return,
346 EINVAL);
347 return (EINVAL);
348 }
349
350 /*
351 * Increment refcount of the found futex to
352 * prevent it from deallocation before FUTEX_LOCK()
353 */
354 ++f->f_refcount;
355 FUTEXES_UNLOCK;
356 umtx_key_release(&key);
357
358 FUTEX_LOCK(f);
359 *newf = f;
360 LIN_SDT_PROBE3(futex, futex_get0, shared, uaddr,
361 f->f_refcount, f->f_key.shared);
362 LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d",
363 uaddr, f->f_refcount, f->f_key.shared);
364
365 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
366 return (0);
367 }
368 }
369
370 if (flags & FUTEX_DONTCREATE) {
371 FUTEXES_UNLOCK;
372 umtx_key_release(&key);
373 LIN_SDT_PROBE1(futex, futex_get0, null, uaddr);
374 LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
375
376 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
377 return (0);
378 }
379
380 if (tmpf == NULL) {
381 FUTEXES_UNLOCK;
382 tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
383 tmpf->f_uaddr = uaddr;
384 tmpf->f_key = key;
385 tmpf->f_refcount = 1;
386 tmpf->f_bitset = FUTEX_BITSET_MATCH_ANY;
387 FUTEX_INIT(tmpf);
388 TAILQ_INIT(&tmpf->f_waiting_proc);
389
390 /*
391 * Lock the new futex before an insert into the futex_list
392 * to prevent futex usage by other.
393 */
394 FUTEX_LOCK(tmpf);
395 goto retry;
396 }
397
398 LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
399 FUTEXES_UNLOCK;
400
401 LIN_SDT_PROBE3(futex, futex_get0, new, uaddr, tmpf->f_refcount,
402 tmpf->f_key.shared);
403 LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d new",
404 uaddr, tmpf->f_refcount, tmpf->f_key.shared);
405 *newf = tmpf;
406
407 LIN_SDT_PROBE1(futex, futex_get0, return, 0);
408 return (0);
409 }
410
411 static int
412 futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
413 uint32_t flags)
414 {
415 int error;
416
417 LIN_SDT_PROBE3(futex, futex_get, entry, uaddr, wp, f);
418
419 if (flags & FUTEX_CREATE_WP) {
420 *wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
421 (*wp)->wp_flags = 0;
422 }
423 error = futex_get0(uaddr, f, flags);
424 if (error) {
425 LIN_SDT_PROBE0(futex, futex_get, error);
426
427 if (flags & FUTEX_CREATE_WP)
428 free(*wp, M_FUTEX_WP);
429
430 LIN_SDT_PROBE1(futex, futex_get, return, error);
431 return (error);
432 }
433 if (flags & FUTEX_CREATE_WP) {
434 TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
435 (*wp)->wp_futex = *f;
436 }
437
438 LIN_SDT_PROBE1(futex, futex_get, return, error);
439 return (error);
440 }
441
442 static int
443 futex_sleep(struct futex *f, struct waiting_proc *wp, int timeout)
444 {
445 int error;
446
447 FUTEX_ASSERT_LOCKED(f);
448 LIN_SDT_PROBE3(futex, futex_sleep, entry, f, wp, timeout);
449 LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %d ref %d",
450 f->f_uaddr, wp, timeout, f->f_refcount);
451 error = sx_sleep(wp, &f->f_lck, PCATCH, "futex", timeout);
452 if (wp->wp_flags & FUTEX_WP_REQUEUED) {
453 KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
454
455 if (error) {
456 LIN_SDT_PROBE5(futex, futex_sleep, requeue_error, error,
457 f->f_uaddr, wp, wp->wp_futex->f_uaddr,
458 wp->wp_futex->f_refcount);
459 }
460
461 LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p wp"
462 " %p requeued uaddr %p ref %d",
463 error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
464 wp->wp_futex->f_refcount);
465 futex_put(f, NULL);
466 f = wp->wp_futex;
467 FUTEX_LOCK(f);
468 } else {
469 if (error) {
470 LIN_SDT_PROBE3(futex, futex_sleep, sleep_error, error,
471 f->f_uaddr, wp);
472 }
473 LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
474 error, f->f_uaddr, wp);
475 }
476
477 futex_put(f, wp);
478
479 LIN_SDT_PROBE1(futex, futex_sleep, return, error);
480 return (error);
481 }
482
483 static int
484 futex_wake(struct futex *f, int n, uint32_t bitset)
485 {
486 struct waiting_proc *wp, *wpt;
487 int count = 0;
488
489 LIN_SDT_PROBE3(futex, futex_wake, entry, f, n, bitset);
490
491 if (bitset == 0) {
492 LIN_SDT_PROBE1(futex, futex_wake, return, EINVAL);
493 return (EINVAL);
494 }
495
496 FUTEX_ASSERT_LOCKED(f);
497 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
498 LIN_SDT_PROBE3(futex, futex_wake, iterate, f->f_uaddr, wp,
499 f->f_refcount);
500 LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
501 f->f_uaddr, wp, f->f_refcount);
502 /*
503 * Unless we find a matching bit in
504 * the bitset, continue searching.
505 */
506 if (!(wp->wp_futex->f_bitset & bitset))
507 continue;
508
509 wp->wp_flags |= FUTEX_WP_REMOVED;
510 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
511 LIN_SDT_PROBE1(futex, futex_wake, wakeup, wp);
512 wakeup_one(wp);
513 if (++count == n)
514 break;
515 }
516
517 LIN_SDT_PROBE1(futex, futex_wake, return, count);
518 return (count);
519 }
520
521 static int
522 futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
523 {
524 struct waiting_proc *wp, *wpt;
525 int count = 0;
526
527 LIN_SDT_PROBE4(futex, futex_requeue, entry, f, n, f2, n2);
528
529 FUTEX_ASSERT_LOCKED(f);
530 FUTEX_ASSERT_LOCKED(f2);
531
532 TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
533 if (++count <= n) {
534 LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
535 f->f_uaddr, wp);
536 wp->wp_flags |= FUTEX_WP_REMOVED;
537 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
538 LIN_SDT_PROBE1(futex, futex_requeue, wakeup, wp);
539 wakeup_one(wp);
540 } else {
541 LIN_SDT_PROBE3(futex, futex_requeue, requeue,
542 f->f_uaddr, wp, f2->f_uaddr);
543 LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
544 f->f_uaddr, wp, f2->f_uaddr);
545 wp->wp_flags |= FUTEX_WP_REQUEUED;
546 /* Move wp to wp_list of f2 futex */
547 TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
548 TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
549
550 /*
551 * Thread which sleeps on wp after waking should
552 * acquire f2 lock, so increment refcount of f2 to
553 * prevent it from premature deallocation.
554 */
555 wp->wp_futex = f2;
556 FUTEXES_LOCK;
557 ++f2->f_refcount;
558 FUTEXES_UNLOCK;
559 if (count - n >= n2)
560 break;
561 }
562 }
563
564 LIN_SDT_PROBE1(futex, futex_requeue, return, count);
565 return (count);
566 }
567
568 static int
569 futex_wait(struct futex *f, struct waiting_proc *wp, int timeout_hz,
570 uint32_t bitset)
571 {
572 int error;
573
574 LIN_SDT_PROBE4(futex, futex_wait, entry, f, wp, timeout_hz, bitset);
575
576 if (bitset == 0) {
577 LIN_SDT_PROBE1(futex, futex_wait, return, EINVAL);
578 return (EINVAL);
579 }
580
581 f->f_bitset = bitset;
582 error = futex_sleep(f, wp, timeout_hz);
583 if (error)
584 LIN_SDT_PROBE1(futex, futex_wait, sleep_error, error);
585 if (error == EWOULDBLOCK)
586 error = ETIMEDOUT;
587
588 LIN_SDT_PROBE1(futex, futex_wait, return, error);
589 return (error);
590 }
591
592 static int
593 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
594 {
595 int op = (encoded_op >> 28) & 7;
596 int cmp = (encoded_op >> 24) & 15;
597 int oparg = (encoded_op << 8) >> 20;
598 int cmparg = (encoded_op << 20) >> 20;
599 int oldval = 0, ret;
600
601 LIN_SDT_PROBE3(futex, futex_atomic_op, entry, td, encoded_op, uaddr);
602
603 if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
604 oparg = 1 << oparg;
605
606 LIN_SDT_PROBE4(futex, futex_atomic_op, decoded_op, op, cmp, oparg,
607 cmparg);
608
609 /* XXX: Linux verifies access here and returns EFAULT */
610 LIN_SDT_PROBE0(futex, futex_atomic_op, missing_access_check);
611
612 switch (op) {
613 case FUTEX_OP_SET:
614 ret = futex_xchgl(oparg, uaddr, &oldval);
615 break;
616 case FUTEX_OP_ADD:
617 ret = futex_addl(oparg, uaddr, &oldval);
618 break;
619 case FUTEX_OP_OR:
620 ret = futex_orl(oparg, uaddr, &oldval);
621 break;
622 case FUTEX_OP_ANDN:
623 ret = futex_andl(~oparg, uaddr, &oldval);
624 break;
625 case FUTEX_OP_XOR:
626 ret = futex_xorl(oparg, uaddr, &oldval);
627 break;
628 default:
629 LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_op, op);
630 ret = -ENOSYS;
631 break;
632 }
633
634 if (ret) {
635 LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
636 return (ret);
637 }
638
639 switch (cmp) {
640 case FUTEX_OP_CMP_EQ:
641 ret = (oldval == cmparg);
642 break;
643 case FUTEX_OP_CMP_NE:
644 ret = (oldval != cmparg);
645 break;
646 case FUTEX_OP_CMP_LT:
647 ret = (oldval < cmparg);
648 break;
649 case FUTEX_OP_CMP_GE:
650 ret = (oldval >= cmparg);
651 break;
652 case FUTEX_OP_CMP_LE:
653 ret = (oldval <= cmparg);
654 break;
655 case FUTEX_OP_CMP_GT:
656 ret = (oldval > cmparg);
657 break;
658 default:
659 LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_cmp, cmp);
660 ret = -ENOSYS;
661 }
662
663 LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
664 return (ret);
665 }
666
667 int
668 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
669 {
670 int clockrt, nrwake, op_ret, ret;
671 struct linux_pemuldata *pem;
672 struct waiting_proc *wp;
673 struct futex *f, *f2;
674 struct l_timespec ltimeout;
675 struct timespec timeout;
676 struct timeval utv, ctv;
677 int timeout_hz;
678 int error;
679 uint32_t flags, val;
680
681 LIN_SDT_PROBE2(futex, linux_sys_futex, entry, td, args);
682
683 if (args->op & LINUX_FUTEX_PRIVATE_FLAG) {
684 flags = 0;
685 args->op &= ~LINUX_FUTEX_PRIVATE_FLAG;
686 } else
687 flags = FUTEX_SHARED;
688
689 /*
690 * Currently support for switching between CLOCK_MONOTONIC and
691 * CLOCK_REALTIME is not present. However Linux forbids the use of
692 * FUTEX_CLOCK_REALTIME with any op except FUTEX_WAIT_BITSET and
693 * FUTEX_WAIT_REQUEUE_PI.
694 */
695 clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME;
696 args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME;
697 if (clockrt && args->op != LINUX_FUTEX_WAIT_BITSET &&
698 args->op != LINUX_FUTEX_WAIT_REQUEUE_PI) {
699 LIN_SDT_PROBE0(futex, linux_sys_futex,
700 unimplemented_clockswitch);
701 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
702 return (ENOSYS);
703 }
704
705 error = 0;
706 f = f2 = NULL;
707
708 switch (args->op) {
709 case LINUX_FUTEX_WAIT:
710 args->val3 = FUTEX_BITSET_MATCH_ANY;
711 /* FALLTHROUGH */
712
713 case LINUX_FUTEX_WAIT_BITSET:
714 LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wait, args->uaddr,
715 args->val, args->val3);
716 LINUX_CTR3(sys_futex, "WAIT uaddr %p val 0x%x bitset 0x%x",
717 args->uaddr, args->val, args->val3);
718
719 if (args->timeout != NULL) {
720 error = copyin(args->timeout, <imeout, sizeof(ltimeout));
721 if (error) {
722 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
723 error);
724 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
725 return (error);
726 }
727 error = linux_to_native_timespec(&timeout, <imeout);
728 if (error)
729 return (error);
730 TIMESPEC_TO_TIMEVAL(&utv, &timeout);
731 error = itimerfix(&utv);
732 if (error) {
733 LIN_SDT_PROBE1(futex, linux_sys_futex, itimerfix_error,
734 error);
735 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
736 return (error);
737 }
738 if (clockrt) {
739 microtime(&ctv);
740 timevalsub(&utv, &ctv);
741 } else if (args->op == LINUX_FUTEX_WAIT_BITSET) {
742 microuptime(&ctv);
743 timevalsub(&utv, &ctv);
744 }
745 if (utv.tv_sec < 0)
746 timevalclear(&utv);
747 timeout_hz = tvtohz(&utv);
748 } else
749 timeout_hz = 0;
750
751 error = futex_get(args->uaddr, &wp, &f,
752 flags | FUTEX_CREATE_WP);
753 if (error) {
754 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
755 return (error);
756 }
757
758 error = copyin(args->uaddr, &val, sizeof(val));
759 if (error) {
760 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
761 error);
762 LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
763 error);
764 futex_put(f, wp);
765
766 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
767 return (error);
768 }
769 if (val != args->val) {
770 LIN_SDT_PROBE4(futex, linux_sys_futex,
771 debug_wait_value_neq, args->uaddr, args->val, val,
772 args->val3);
773 LINUX_CTR3(sys_futex,
774 "WAIT uaddr %p val 0x%x != uval 0x%x",
775 args->uaddr, args->val, val);
776 futex_put(f, wp);
777
778 LIN_SDT_PROBE1(futex, linux_sys_futex, return,
779 EWOULDBLOCK);
780 return (EWOULDBLOCK);
781 }
782
783 error = futex_wait(f, wp, timeout_hz, args->val3);
784 break;
785
786 case LINUX_FUTEX_WAKE:
787 args->val3 = FUTEX_BITSET_MATCH_ANY;
788 /* FALLTHROUGH */
789
790 case LINUX_FUTEX_WAKE_BITSET:
791 LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wake, args->uaddr,
792 args->val, args->val3);
793 LINUX_CTR3(sys_futex, "WAKE uaddr %p nrwake 0x%x bitset 0x%x",
794 args->uaddr, args->val, args->val3);
795
796 error = futex_get(args->uaddr, NULL, &f,
797 flags | FUTEX_DONTCREATE);
798 if (error) {
799 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
800 return (error);
801 }
802
803 if (f == NULL) {
804 td->td_retval[0] = 0;
805
806 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
807 return (error);
808 }
809 td->td_retval[0] = futex_wake(f, args->val, args->val3);
810 futex_put(f, NULL);
811 break;
812
813 case LINUX_FUTEX_CMP_REQUEUE:
814 LIN_SDT_PROBE5(futex, linux_sys_futex, debug_cmp_requeue,
815 args->uaddr, args->val, args->val3, args->uaddr2,
816 args->timeout);
817 LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
818 "nrwake 0x%x uval 0x%x uaddr2 %p nrequeue 0x%x",
819 args->uaddr, args->val, args->val3, args->uaddr2,
820 args->timeout);
821
822 /*
823 * Linux allows this, we would not, it is an incorrect
824 * usage of declared ABI, so return EINVAL.
825 */
826 if (args->uaddr == args->uaddr2) {
827 LIN_SDT_PROBE0(futex, linux_sys_futex,
828 invalid_cmp_requeue_use);
829 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
830 return (EINVAL);
831 }
832
833 error = futex_get(args->uaddr, NULL, &f, flags);
834 if (error) {
835 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
836 return (error);
837 }
838
839 /*
840 * To avoid deadlocks return EINVAL if second futex
841 * exists at this time.
842 *
843 * Glibc fall back to FUTEX_WAKE in case of any error
844 * returned by FUTEX_CMP_REQUEUE.
845 */
846 error = futex_get(args->uaddr2, NULL, &f2,
847 flags | FUTEX_DONTEXISTS);
848 if (error) {
849 futex_put(f, NULL);
850
851 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
852 return (error);
853 }
854 error = copyin(args->uaddr, &val, sizeof(val));
855 if (error) {
856 LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
857 error);
858 LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
859 error);
860 futex_put(f2, NULL);
861 futex_put(f, NULL);
862
863 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
864 return (error);
865 }
866 if (val != args->val3) {
867 LIN_SDT_PROBE2(futex, linux_sys_futex,
868 debug_cmp_requeue_value_neq, args->val, val);
869 LINUX_CTR2(sys_futex, "CMP_REQUEUE val 0x%x != uval 0x%x",
870 args->val, val);
871 futex_put(f2, NULL);
872 futex_put(f, NULL);
873
874 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EAGAIN);
875 return (EAGAIN);
876 }
877
878 nrwake = (int)(unsigned long)args->timeout;
879 td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
880 futex_put(f2, NULL);
881 futex_put(f, NULL);
882 break;
883
884 case LINUX_FUTEX_WAKE_OP:
885 LIN_SDT_PROBE5(futex, linux_sys_futex, debug_wake_op,
886 args->uaddr, args->op, args->val, args->uaddr2, args->val3);
887 LINUX_CTR5(sys_futex, "WAKE_OP "
888 "uaddr %p nrwake 0x%x uaddr2 %p op 0x%x nrwake2 0x%x",
889 args->uaddr, args->val, args->uaddr2, args->val3,
890 args->timeout);
891
892 error = futex_get(args->uaddr, NULL, &f, flags);
893 if (error) {
894 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
895 return (error);
896 }
897
898 if (args->uaddr != args->uaddr2)
899 error = futex_get(args->uaddr2, NULL, &f2, flags);
900 if (error) {
901 futex_put(f, NULL);
902
903 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
904 return (error);
905 }
906
907 /*
908 * This function returns positive number as results and
909 * negative as errors
910 */
911 op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
912
913 LINUX_CTR2(sys_futex, "WAKE_OP atomic_op uaddr %p ret 0x%x",
914 args->uaddr, op_ret);
915
916 if (op_ret < 0) {
917 /* XXX: We don't handle the EFAULT yet. */
918 if (op_ret != -EFAULT) {
919 if (f2 != NULL)
920 futex_put(f2, NULL);
921 futex_put(f, NULL);
922
923 LIN_SDT_PROBE1(futex, linux_sys_futex, return,
924 -op_ret);
925 return (-op_ret);
926 } else {
927 LIN_SDT_PROBE0(futex, linux_sys_futex,
928 unhandled_efault);
929 }
930 if (f2 != NULL)
931 futex_put(f2, NULL);
932 futex_put(f, NULL);
933
934 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EFAULT);
935 return (EFAULT);
936 }
937
938 ret = futex_wake(f, args->val, args->val3);
939
940 if (op_ret > 0) {
941 op_ret = 0;
942 nrwake = (int)(unsigned long)args->timeout;
943
944 if (f2 != NULL)
945 op_ret += futex_wake(f2, nrwake, args->val3);
946 else
947 op_ret += futex_wake(f, nrwake, args->val3);
948 ret += op_ret;
949
950 }
951 if (f2 != NULL)
952 futex_put(f2, NULL);
953 futex_put(f, NULL);
954 td->td_retval[0] = ret;
955 break;
956
957 case LINUX_FUTEX_LOCK_PI:
958 /* not yet implemented */
959 pem = pem_find(td->td_proc);
960 if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
961 linux_msg(td,
962 "linux_sys_futex: "
963 "unsupported futex_pi op\n");
964 pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
965 LIN_SDT_PROBE0(futex, linux_sys_futex,
966 unimplemented_lock_pi);
967 }
968 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
969 return (ENOSYS);
970
971 case LINUX_FUTEX_UNLOCK_PI:
972 /* not yet implemented */
973 pem = pem_find(td->td_proc);
974 if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
975 linux_msg(td,
976 "linux_sys_futex: "
977 "unsupported futex_pi op\n");
978 pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
979 LIN_SDT_PROBE0(futex, linux_sys_futex,
980 unimplemented_unlock_pi);
981 }
982 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
983 return (ENOSYS);
984
985 case LINUX_FUTEX_TRYLOCK_PI:
986 /* not yet implemented */
987 pem = pem_find(td->td_proc);
988 if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
989 linux_msg(td,
990 "linux_sys_futex: "
991 "unsupported futex_pi op\n");
992 pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
993 LIN_SDT_PROBE0(futex, linux_sys_futex,
994 unimplemented_trylock_pi);
995 }
996 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
997 return (ENOSYS);
998
999 case LINUX_FUTEX_REQUEUE:
1000
1001 /*
1002 * Glibc does not use this operation since version 2.3.3,
1003 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
1004 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
1005 * FUTEX_REQUEUE returned EINVAL.
1006 */
1007 pem = pem_find(td->td_proc);
1008 if ((pem->flags & LINUX_XDEPR_REQUEUEOP) == 0) {
1009 linux_msg(td,
1010 "linux_sys_futex: "
1011 "unsupported futex_requeue op\n");
1012 pem->flags |= LINUX_XDEPR_REQUEUEOP;
1013 LIN_SDT_PROBE0(futex, linux_sys_futex,
1014 deprecated_requeue);
1015 }
1016
1017 LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
1018 return (EINVAL);
1019
1020 case LINUX_FUTEX_WAIT_REQUEUE_PI:
1021 /* not yet implemented */
1022 pem = pem_find(td->td_proc);
1023 if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1024 linux_msg(td,
1025 "linux_sys_futex: "
1026 "unsupported futex_pi op\n");
1027 pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1028 LIN_SDT_PROBE0(futex, linux_sys_futex,
1029 unimplemented_wait_requeue_pi);
1030 }
1031 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1032 return (ENOSYS);
1033
1034 case LINUX_FUTEX_CMP_REQUEUE_PI:
1035 /* not yet implemented */
1036 pem = pem_find(td->td_proc);
1037 if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1038 linux_msg(td,
1039 "linux_sys_futex: "
1040 "unsupported futex_pi op\n");
1041 pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1042 LIN_SDT_PROBE0(futex, linux_sys_futex,
1043 unimplemented_cmp_requeue_pi);
1044 }
1045 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1046 return (ENOSYS);
1047
1048 default:
1049 linux_msg(td,
1050 "linux_sys_futex: unknown op %d\n", args->op);
1051 LIN_SDT_PROBE1(futex, linux_sys_futex, unknown_operation,
1052 args->op);
1053 LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1054 return (ENOSYS);
1055 }
1056
1057 LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
1058 return (error);
1059 }
1060
1061 int
1062 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
1063 {
1064 struct linux_emuldata *em;
1065
1066 LIN_SDT_PROBE2(futex, linux_set_robust_list, entry, td, args);
1067
1068 if (args->len != sizeof(struct linux_robust_list_head)) {
1069 LIN_SDT_PROBE0(futex, linux_set_robust_list, size_error);
1070 LIN_SDT_PROBE1(futex, linux_set_robust_list, return, EINVAL);
1071 return (EINVAL);
1072 }
1073
1074 em = em_find(td);
1075 em->robust_futexes = args->head;
1076
1077 LIN_SDT_PROBE1(futex, linux_set_robust_list, return, 0);
1078 return (0);
1079 }
1080
1081 int
1082 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
1083 {
1084 struct linux_emuldata *em;
1085 struct linux_robust_list_head *head;
1086 l_size_t len = sizeof(struct linux_robust_list_head);
1087 struct thread *td2;
1088 int error = 0;
1089
1090 LIN_SDT_PROBE2(futex, linux_get_robust_list, entry, td, args);
1091
1092 if (!args->pid) {
1093 em = em_find(td);
1094 KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1095 head = em->robust_futexes;
1096 } else {
1097 td2 = tdfind(args->pid, -1);
1098 if (td2 == NULL) {
1099 LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1100 ESRCH);
1101 return (ESRCH);
1102 }
1103 if (SV_PROC_ABI(td2->td_proc) != SV_ABI_LINUX) {
1104 LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1105 EPERM);
1106 PROC_UNLOCK(td2->td_proc);
1107 return (EPERM);
1108 }
1109
1110 em = em_find(td2);
1111 KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1112 /* XXX: ptrace? */
1113 if (priv_check(td, PRIV_CRED_SETUID) ||
1114 priv_check(td, PRIV_CRED_SETEUID) ||
1115 p_candebug(td, td2->td_proc)) {
1116 PROC_UNLOCK(td2->td_proc);
1117
1118 LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1119 EPERM);
1120 return (EPERM);
1121 }
1122 head = em->robust_futexes;
1123
1124 PROC_UNLOCK(td2->td_proc);
1125 }
1126
1127 error = copyout(&len, args->len, sizeof(l_size_t));
1128 if (error) {
1129 LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1130 error);
1131 LIN_SDT_PROBE1(futex, linux_get_robust_list, return, EFAULT);
1132 return (EFAULT);
1133 }
1134
1135 error = copyout(&head, args->head, sizeof(head));
1136 if (error) {
1137 LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1138 error);
1139 }
1140
1141 LIN_SDT_PROBE1(futex, linux_get_robust_list, return, error);
1142 return (error);
1143 }
1144
1145 static int
1146 handle_futex_death(struct linux_emuldata *em, uint32_t *uaddr,
1147 unsigned int pi)
1148 {
1149 uint32_t uval, nval, mval;
1150 struct futex *f;
1151 int error;
1152
1153 LIN_SDT_PROBE3(futex, handle_futex_death, entry, em, uaddr, pi);
1154
1155 retry:
1156 error = copyin(uaddr, &uval, 4);
1157 if (error) {
1158 LIN_SDT_PROBE1(futex, handle_futex_death, copyin_error, error);
1159 LIN_SDT_PROBE1(futex, handle_futex_death, return, EFAULT);
1160 return (EFAULT);
1161 }
1162 if ((uval & FUTEX_TID_MASK) == em->em_tid) {
1163 mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1164 nval = casuword32(uaddr, uval, mval);
1165
1166 if (nval == -1) {
1167 LIN_SDT_PROBE1(futex, handle_futex_death, return,
1168 EFAULT);
1169 return (EFAULT);
1170 }
1171
1172 if (nval != uval)
1173 goto retry;
1174
1175 if (!pi && (uval & FUTEX_WAITERS)) {
1176 error = futex_get(uaddr, NULL, &f,
1177 FUTEX_DONTCREATE | FUTEX_SHARED);
1178 if (error) {
1179 LIN_SDT_PROBE1(futex, handle_futex_death,
1180 return, error);
1181 return (error);
1182 }
1183 if (f != NULL) {
1184 futex_wake(f, 1, FUTEX_BITSET_MATCH_ANY);
1185 futex_put(f, NULL);
1186 }
1187 }
1188 }
1189
1190 LIN_SDT_PROBE1(futex, handle_futex_death, return, 0);
1191 return (0);
1192 }
1193
1194 static int
1195 fetch_robust_entry(struct linux_robust_list **entry,
1196 struct linux_robust_list **head, unsigned int *pi)
1197 {
1198 l_ulong uentry;
1199 int error;
1200
1201 LIN_SDT_PROBE3(futex, fetch_robust_entry, entry, entry, head, pi);
1202
1203 error = copyin((const void *)head, &uentry, sizeof(l_ulong));
1204 if (error) {
1205 LIN_SDT_PROBE1(futex, fetch_robust_entry, copyin_error, error);
1206 LIN_SDT_PROBE1(futex, fetch_robust_entry, return, EFAULT);
1207 return (EFAULT);
1208 }
1209
1210 *entry = (void *)(uentry & ~1UL);
1211 *pi = uentry & 1;
1212
1213 LIN_SDT_PROBE1(futex, fetch_robust_entry, return, 0);
1214 return (0);
1215 }
1216
1217 /* This walks the list of robust futexes releasing them. */
1218 void
1219 release_futexes(struct thread *td, struct linux_emuldata *em)
1220 {
1221 struct linux_robust_list_head *head = NULL;
1222 struct linux_robust_list *entry, *next_entry, *pending;
1223 unsigned int limit = 2048, pi, next_pi, pip;
1224 l_long futex_offset;
1225 int rc, error;
1226
1227 LIN_SDT_PROBE2(futex, release_futexes, entry, td, em);
1228
1229 head = em->robust_futexes;
1230
1231 if (head == NULL) {
1232 LIN_SDT_PROBE0(futex, release_futexes, return);
1233 return;
1234 }
1235
1236 if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi)) {
1237 LIN_SDT_PROBE0(futex, release_futexes, return);
1238 return;
1239 }
1240
1241 error = copyin(&head->futex_offset, &futex_offset,
1242 sizeof(futex_offset));
1243 if (error) {
1244 LIN_SDT_PROBE1(futex, release_futexes, copyin_error, error);
1245 LIN_SDT_PROBE0(futex, release_futexes, return);
1246 return;
1247 }
1248
1249 if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip)) {
1250 LIN_SDT_PROBE0(futex, release_futexes, return);
1251 return;
1252 }
1253
1254 while (entry != &head->list) {
1255 rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
1256
1257 if (entry != pending)
1258 if (handle_futex_death(em,
1259 (uint32_t *)((caddr_t)entry + futex_offset), pi)) {
1260 LIN_SDT_PROBE0(futex, release_futexes, return);
1261 return;
1262 }
1263 if (rc) {
1264 LIN_SDT_PROBE0(futex, release_futexes, return);
1265 return;
1266 }
1267
1268 entry = next_entry;
1269 pi = next_pi;
1270
1271 if (!--limit)
1272 break;
1273
1274 sched_relinquish(curthread);
1275 }
1276
1277 if (pending)
1278 handle_futex_death(em, (uint32_t *)((caddr_t)pending + futex_offset), pip);
1279
1280 LIN_SDT_PROBE0(futex, release_futexes, return);
1281 }
Cache object: 89206405230ec31bd27742902fe9b9fc
|