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