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