xref: /freebsd/sys/compat/linux/linux_futex.c (revision 2f513db7)
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 #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 
254 static void futex_put(struct futex *, struct waiting_proc *);
255 static int futex_get0(uint32_t *, struct futex **f, uint32_t);
256 static int futex_get(uint32_t *, struct waiting_proc **, struct futex **,
257     uint32_t);
258 static int futex_sleep(struct futex *, struct waiting_proc *, struct timespec *);
259 static int futex_wake(struct futex *, int, uint32_t);
260 static int futex_requeue(struct futex *, int, struct futex *, int);
261 static int futex_copyin_timeout(int, struct l_timespec *, int,
262     struct timespec *);
263 static int futex_wait(struct futex *, struct waiting_proc *, struct timespec *,
264     uint32_t);
265 static void futex_lock(struct futex *);
266 static void futex_unlock(struct futex *);
267 static int futex_atomic_op(struct thread *, int, uint32_t *);
268 static int handle_futex_death(struct linux_emuldata *, uint32_t *,
269     unsigned int);
270 static int fetch_robust_entry(struct linux_robust_list **,
271     struct linux_robust_list **, unsigned int *);
272 
273 static int
274 futex_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 
298 static void
299 futex_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 
339 static int
340 futex_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 	}
357 retry:
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 
439 static int
440 futex_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 
470 static inline void
471 futex_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 
480 static inline void
481 futex_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 
490 static int
491 futex_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 
551 static int
552 futex_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 
589 static int
590 futex_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 
636 static int
637 futex_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 		return (EINVAL);
647 	}
648 
649 	f->f_bitset = bitset;
650 	error = futex_sleep(f, wp, ts);
651 	if (error)
652 		LIN_SDT_PROBE1(futex, futex_wait, sleep_error, error);
653 	if (error == EWOULDBLOCK)
654 		error = ETIMEDOUT;
655 
656 	LIN_SDT_PROBE1(futex, futex_wait, return, error);
657 	return (error);
658 }
659 
660 static int
661 futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
662 {
663 	int op = (encoded_op >> 28) & 7;
664 	int cmp = (encoded_op >> 24) & 15;
665 	int oparg = (encoded_op << 8) >> 20;
666 	int cmparg = (encoded_op << 20) >> 20;
667 	int oldval = 0, ret;
668 
669 	LIN_SDT_PROBE3(futex, futex_atomic_op, entry, td, encoded_op, uaddr);
670 
671 	if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
672 		oparg = 1 << oparg;
673 
674 	LIN_SDT_PROBE4(futex, futex_atomic_op, decoded_op, op, cmp, oparg,
675 	    cmparg);
676 
677 	/* XXX: Linux verifies access here and returns EFAULT */
678 	LIN_SDT_PROBE0(futex, futex_atomic_op, missing_access_check);
679 
680 	switch (op) {
681 	case FUTEX_OP_SET:
682 		ret = futex_xchgl(oparg, uaddr, &oldval);
683 		break;
684 	case FUTEX_OP_ADD:
685 		ret = futex_addl(oparg, uaddr, &oldval);
686 		break;
687 	case FUTEX_OP_OR:
688 		ret = futex_orl(oparg, uaddr, &oldval);
689 		break;
690 	case FUTEX_OP_ANDN:
691 		ret = futex_andl(~oparg, uaddr, &oldval);
692 		break;
693 	case FUTEX_OP_XOR:
694 		ret = futex_xorl(oparg, uaddr, &oldval);
695 		break;
696 	default:
697 		LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_op, op);
698 		ret = -ENOSYS;
699 		break;
700 	}
701 
702 	if (ret) {
703 		LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
704 		return (ret);
705 	}
706 
707 	switch (cmp) {
708 	case FUTEX_OP_CMP_EQ:
709 		ret = (oldval == cmparg);
710 		break;
711 	case FUTEX_OP_CMP_NE:
712 		ret = (oldval != cmparg);
713 		break;
714 	case FUTEX_OP_CMP_LT:
715 		ret = (oldval < cmparg);
716 		break;
717 	case FUTEX_OP_CMP_GE:
718 		ret = (oldval >= cmparg);
719 		break;
720 	case FUTEX_OP_CMP_LE:
721 		ret = (oldval <= cmparg);
722 		break;
723 	case FUTEX_OP_CMP_GT:
724 		ret = (oldval > cmparg);
725 		break;
726 	default:
727 		LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_cmp, cmp);
728 		ret = -ENOSYS;
729 	}
730 
731 	LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
732 	return (ret);
733 }
734 
735 int
736 linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
737 {
738 	int clockrt, nrwake, op_ret, ret;
739 	struct linux_pemuldata *pem;
740 	struct waiting_proc *wp;
741 	struct futex *f, *f2;
742 	struct timespec uts, *ts;
743 	int error, save;
744 	uint32_t flags, val;
745 
746 	LIN_SDT_PROBE2(futex, linux_sys_futex, entry, td, args);
747 
748 	if (args->op & LINUX_FUTEX_PRIVATE_FLAG) {
749 		flags = 0;
750 		args->op &= ~LINUX_FUTEX_PRIVATE_FLAG;
751 	} else
752 		flags = FUTEX_SHARED;
753 
754 	/*
755 	 * Currently support for switching between CLOCK_MONOTONIC and
756 	 * CLOCK_REALTIME is not present. However Linux forbids the use of
757 	 * FUTEX_CLOCK_REALTIME with any op except FUTEX_WAIT_BITSET and
758 	 * FUTEX_WAIT_REQUEUE_PI.
759 	 */
760 	clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME;
761 	args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME;
762 	if (clockrt && args->op != LINUX_FUTEX_WAIT_BITSET &&
763 		args->op != LINUX_FUTEX_WAIT_REQUEUE_PI) {
764 		LIN_SDT_PROBE0(futex, linux_sys_futex,
765 		    unimplemented_clockswitch);
766 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
767 		return (ENOSYS);
768 	}
769 
770 	error = 0;
771 	f = f2 = NULL;
772 
773 	switch (args->op) {
774 	case LINUX_FUTEX_WAIT:
775 		args->val3 = FUTEX_BITSET_MATCH_ANY;
776 		/* FALLTHROUGH */
777 
778 	case LINUX_FUTEX_WAIT_BITSET:
779 		LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wait, args->uaddr,
780 		    args->val, args->val3);
781 		LINUX_CTR3(sys_futex, "WAIT uaddr %p val 0x%x bitset 0x%x",
782 		    args->uaddr, args->val, args->val3);
783 
784 		if (args->timeout != NULL) {
785 			error = futex_copyin_timeout(args->op, args->timeout,
786 			    clockrt, &uts);
787 			if (error) {
788 				LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
789 				    error);
790 				LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
791 				return (error);
792 			}
793 			ts = &uts;
794 		} else
795 			ts = NULL;
796 
797 retry0:
798 		error = futex_get(args->uaddr, &wp, &f,
799 		    flags | FUTEX_CREATE_WP);
800 		if (error) {
801 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
802 			return (error);
803 		}
804 
805 		error = copyin_nofault(args->uaddr, &val, sizeof(val));
806 		if (error) {
807 			futex_put(f, wp);
808 			error = copyin(args->uaddr, &val, sizeof(val));
809 			if (error == 0)
810 				goto retry0;
811 			LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
812 			    error);
813 			LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
814 			    error);
815 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
816 			return (error);
817 		}
818 		if (val != args->val) {
819 			LIN_SDT_PROBE4(futex, linux_sys_futex,
820 			    debug_wait_value_neq, args->uaddr, args->val, val,
821 			    args->val3);
822 			LINUX_CTR3(sys_futex,
823 			    "WAIT uaddr %p val 0x%x != uval 0x%x",
824 			    args->uaddr, args->val, val);
825 			futex_put(f, wp);
826 
827 			LIN_SDT_PROBE1(futex, linux_sys_futex, return,
828 			    EWOULDBLOCK);
829 			return (EWOULDBLOCK);
830 		}
831 
832 		error = futex_wait(f, wp, ts, args->val3);
833 		break;
834 
835 	case LINUX_FUTEX_WAKE:
836 		args->val3 = FUTEX_BITSET_MATCH_ANY;
837 		/* FALLTHROUGH */
838 
839 	case LINUX_FUTEX_WAKE_BITSET:
840 		LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wake, args->uaddr,
841 		    args->val, args->val3);
842 		LINUX_CTR3(sys_futex, "WAKE uaddr %p nrwake 0x%x bitset 0x%x",
843 		    args->uaddr, args->val, args->val3);
844 
845 		error = futex_get(args->uaddr, NULL, &f,
846 		    flags | FUTEX_DONTCREATE);
847 		if (error) {
848 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
849 			return (error);
850 		}
851 
852 		if (f == NULL) {
853 			td->td_retval[0] = 0;
854 
855 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
856 			return (error);
857 		}
858 		td->td_retval[0] = futex_wake(f, args->val, args->val3);
859 		futex_put(f, NULL);
860 		break;
861 
862 	case LINUX_FUTEX_CMP_REQUEUE:
863 		LIN_SDT_PROBE5(futex, linux_sys_futex, debug_cmp_requeue,
864 		    args->uaddr, args->val, args->val3, args->uaddr2,
865 		    args->timeout);
866 		LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
867 		    "nrwake 0x%x uval 0x%x uaddr2 %p nrequeue 0x%x",
868 		    args->uaddr, args->val, args->val3, args->uaddr2,
869 		    args->timeout);
870 
871 		/*
872 		 * Linux allows this, we would not, it is an incorrect
873 		 * usage of declared ABI, so return EINVAL.
874 		 */
875 		if (args->uaddr == args->uaddr2) {
876 			LIN_SDT_PROBE0(futex, linux_sys_futex,
877 			    invalid_cmp_requeue_use);
878 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
879 			return (EINVAL);
880 		}
881 
882 retry1:
883 		error = futex_get(args->uaddr, NULL, &f, flags | FUTEX_DONTLOCK);
884 		if (error) {
885 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
886 			return (error);
887 		}
888 
889 		/*
890 		 * To avoid deadlocks return EINVAL if second futex
891 		 * exists at this time.
892 		 *
893 		 * Glibc fall back to FUTEX_WAKE in case of any error
894 		 * returned by FUTEX_CMP_REQUEUE.
895 		 */
896 		error = futex_get(args->uaddr2, NULL, &f2,
897 		    flags | FUTEX_DONTEXISTS | FUTEX_DONTLOCK);
898 		if (error) {
899 			futex_put(f, NULL);
900 
901 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
902 			return (error);
903 		}
904 		futex_lock(f);
905 		futex_lock(f2);
906 		error = copyin_nofault(args->uaddr, &val, sizeof(val));
907 		if (error) {
908 			futex_put(f2, NULL);
909 			futex_put(f, NULL);
910 			error = copyin(args->uaddr, &val, sizeof(val));
911 			if (error == 0)
912 				goto retry1;
913 			LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
914 			    error);
915 			LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
916 			    error);
917 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
918 			return (error);
919 		}
920 		if (val != args->val3) {
921 			LIN_SDT_PROBE2(futex, linux_sys_futex,
922 			    debug_cmp_requeue_value_neq, args->val, val);
923 			LINUX_CTR2(sys_futex, "CMP_REQUEUE val 0x%x != uval 0x%x",
924 			    args->val, val);
925 			futex_put(f2, NULL);
926 			futex_put(f, NULL);
927 
928 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, EAGAIN);
929 			return (EAGAIN);
930 		}
931 
932 		nrwake = (int)(unsigned long)args->timeout;
933 		td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
934 		futex_put(f2, NULL);
935 		futex_put(f, NULL);
936 		break;
937 
938 	case LINUX_FUTEX_WAKE_OP:
939 		LIN_SDT_PROBE5(futex, linux_sys_futex, debug_wake_op,
940 		    args->uaddr, args->op, args->val, args->uaddr2, args->val3);
941 		LINUX_CTR5(sys_futex, "WAKE_OP "
942 		    "uaddr %p nrwake 0x%x uaddr2 %p op 0x%x nrwake2 0x%x",
943 		    args->uaddr, args->val, args->uaddr2, args->val3,
944 		    args->timeout);
945 
946 		if (args->uaddr == args->uaddr2) {
947 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
948 			return (EINVAL);
949 		}
950 
951 retry2:
952 		error = futex_get(args->uaddr, NULL, &f, flags | FUTEX_DONTLOCK);
953 		if (error) {
954 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
955 			return (error);
956 		}
957 
958 		error = futex_get(args->uaddr2, NULL, &f2, flags | FUTEX_DONTLOCK);
959 		if (error) {
960 			futex_put(f, NULL);
961 
962 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
963 			return (error);
964 		}
965 		futex_lock(f);
966 		futex_lock(f2);
967 
968 		/*
969 		 * This function returns positive number as results and
970 		 * negative as errors
971 		 */
972 		save = vm_fault_disable_pagefaults();
973 		op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
974 		vm_fault_enable_pagefaults(save);
975 
976 		LINUX_CTR2(sys_futex, "WAKE_OP atomic_op uaddr %p ret 0x%x",
977 		    args->uaddr, op_ret);
978 
979 		if (op_ret < 0) {
980 			if (f2 != NULL)
981 				futex_put(f2, NULL);
982 			futex_put(f, NULL);
983 			error = copyin(args->uaddr2, &val, sizeof(val));
984 			if (error == 0)
985 				goto retry2;
986 			LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
987 			return (error);
988 		}
989 
990 		ret = futex_wake(f, args->val, args->val3);
991 
992 		if (op_ret > 0) {
993 			op_ret = 0;
994 			nrwake = (int)(unsigned long)args->timeout;
995 
996 			if (f2 != NULL)
997 				op_ret += futex_wake(f2, nrwake, args->val3);
998 			else
999 				op_ret += futex_wake(f, nrwake, args->val3);
1000 			ret += op_ret;
1001 
1002 		}
1003 		if (f2 != NULL)
1004 			futex_put(f2, NULL);
1005 		futex_put(f, NULL);
1006 		td->td_retval[0] = ret;
1007 		break;
1008 
1009 	case LINUX_FUTEX_LOCK_PI:
1010 		/* not yet implemented */
1011 		pem = pem_find(td->td_proc);
1012 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1013 			linux_msg(td,
1014 				  "linux_sys_futex: "
1015 				  "unsupported futex_pi op\n");
1016 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1017 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1018 			    unimplemented_lock_pi);
1019 		}
1020 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1021 		return (ENOSYS);
1022 
1023 	case LINUX_FUTEX_UNLOCK_PI:
1024 		/* not yet implemented */
1025 		pem = pem_find(td->td_proc);
1026 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1027 			linux_msg(td,
1028 				  "linux_sys_futex: "
1029 				  "unsupported futex_pi op\n");
1030 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1031 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1032 			    unimplemented_unlock_pi);
1033 		}
1034 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1035 		return (ENOSYS);
1036 
1037 	case LINUX_FUTEX_TRYLOCK_PI:
1038 		/* not yet implemented */
1039 		pem = pem_find(td->td_proc);
1040 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1041 			linux_msg(td,
1042 				  "linux_sys_futex: "
1043 				  "unsupported futex_pi op\n");
1044 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1045 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1046 			    unimplemented_trylock_pi);
1047 		}
1048 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1049 		return (ENOSYS);
1050 
1051 	case LINUX_FUTEX_REQUEUE:
1052 		/*
1053 		 * Glibc does not use this operation since version 2.3.3,
1054 		 * as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
1055 		 * Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
1056 		 * FUTEX_REQUEUE returned EINVAL.
1057 		 */
1058 		pem = pem_find(td->td_proc);
1059 		if ((pem->flags & LINUX_XDEPR_REQUEUEOP) == 0) {
1060 			linux_msg(td,
1061 				  "linux_sys_futex: "
1062 				  "unsupported futex_requeue op\n");
1063 			pem->flags |= LINUX_XDEPR_REQUEUEOP;
1064 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1065 			    deprecated_requeue);
1066 		}
1067 
1068 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
1069 		return (EINVAL);
1070 
1071 	case LINUX_FUTEX_WAIT_REQUEUE_PI:
1072 		/* not yet implemented */
1073 		pem = pem_find(td->td_proc);
1074 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1075 			linux_msg(td,
1076 				  "linux_sys_futex: "
1077 				  "unsupported futex_pi op\n");
1078 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1079 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1080 			    unimplemented_wait_requeue_pi);
1081 		}
1082 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1083 		return (ENOSYS);
1084 
1085 	case LINUX_FUTEX_CMP_REQUEUE_PI:
1086 		/* not yet implemented */
1087 		pem = pem_find(td->td_proc);
1088 		if ((pem->flags & LINUX_XUNSUP_FUTEXPIOP) == 0) {
1089 			linux_msg(td,
1090 				  "linux_sys_futex: "
1091 				  "unsupported futex_pi op\n");
1092 			pem->flags |= LINUX_XUNSUP_FUTEXPIOP;
1093 			LIN_SDT_PROBE0(futex, linux_sys_futex,
1094 			    unimplemented_cmp_requeue_pi);
1095 		}
1096 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1097 		return (ENOSYS);
1098 
1099 	default:
1100 		linux_msg(td,
1101 			  "linux_sys_futex: unknown op %d\n", args->op);
1102 		LIN_SDT_PROBE1(futex, linux_sys_futex, unknown_operation,
1103 		    args->op);
1104 		LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
1105 		return (ENOSYS);
1106 	}
1107 
1108 	LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
1109 	return (error);
1110 }
1111 
1112 int
1113 linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
1114 {
1115 	struct linux_emuldata *em;
1116 
1117 	LIN_SDT_PROBE2(futex, linux_set_robust_list, entry, td, args);
1118 
1119 	if (args->len != sizeof(struct linux_robust_list_head)) {
1120 		LIN_SDT_PROBE0(futex, linux_set_robust_list, size_error);
1121 		LIN_SDT_PROBE1(futex, linux_set_robust_list, return, EINVAL);
1122 		return (EINVAL);
1123 	}
1124 
1125 	em = em_find(td);
1126 	em->robust_futexes = args->head;
1127 
1128 	LIN_SDT_PROBE1(futex, linux_set_robust_list, return, 0);
1129 	return (0);
1130 }
1131 
1132 int
1133 linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
1134 {
1135 	struct linux_emuldata *em;
1136 	struct linux_robust_list_head *head;
1137 	l_size_t len = sizeof(struct linux_robust_list_head);
1138 	struct thread *td2;
1139 	int error = 0;
1140 
1141 	LIN_SDT_PROBE2(futex, linux_get_robust_list, entry, td, args);
1142 
1143 	if (!args->pid) {
1144 		em = em_find(td);
1145 		KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1146 		head = em->robust_futexes;
1147 	} else {
1148 		td2 = tdfind(args->pid, -1);
1149 		if (td2 == NULL) {
1150 			LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1151 			    ESRCH);
1152 			return (ESRCH);
1153 		}
1154 		if (SV_PROC_ABI(td2->td_proc) != SV_ABI_LINUX) {
1155 			LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1156 			    EPERM);
1157 			PROC_UNLOCK(td2->td_proc);
1158 			return (EPERM);
1159 		}
1160 
1161 		em = em_find(td2);
1162 		KASSERT(em != NULL, ("get_robust_list: emuldata notfound.\n"));
1163 		/* XXX: ptrace? */
1164 		if (priv_check(td, PRIV_CRED_SETUID) ||
1165 		    priv_check(td, PRIV_CRED_SETEUID) ||
1166 		    p_candebug(td, td2->td_proc)) {
1167 			PROC_UNLOCK(td2->td_proc);
1168 
1169 			LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
1170 			    EPERM);
1171 			return (EPERM);
1172 		}
1173 		head = em->robust_futexes;
1174 
1175 		PROC_UNLOCK(td2->td_proc);
1176 	}
1177 
1178 	error = copyout(&len, args->len, sizeof(l_size_t));
1179 	if (error) {
1180 		LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1181 		    error);
1182 		LIN_SDT_PROBE1(futex, linux_get_robust_list, return, EFAULT);
1183 		return (EFAULT);
1184 	}
1185 
1186 	error = copyout(&head, args->head, sizeof(head));
1187 	if (error) {
1188 		LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
1189 		    error);
1190 	}
1191 
1192 	LIN_SDT_PROBE1(futex, linux_get_robust_list, return, error);
1193 	return (error);
1194 }
1195 
1196 static int
1197 handle_futex_death(struct linux_emuldata *em, uint32_t *uaddr,
1198     unsigned int pi)
1199 {
1200 	uint32_t uval, nval, mval;
1201 	struct futex *f;
1202 	int error;
1203 
1204 	LIN_SDT_PROBE3(futex, handle_futex_death, entry, em, uaddr, pi);
1205 
1206 retry:
1207 	error = copyin(uaddr, &uval, 4);
1208 	if (error) {
1209 		LIN_SDT_PROBE1(futex, handle_futex_death, copyin_error, error);
1210 		LIN_SDT_PROBE1(futex, handle_futex_death, return, EFAULT);
1211 		return (EFAULT);
1212 	}
1213 	if ((uval & FUTEX_TID_MASK) == em->em_tid) {
1214 		mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
1215 		nval = casuword32(uaddr, uval, mval);
1216 
1217 		if (nval == -1) {
1218 			LIN_SDT_PROBE1(futex, handle_futex_death, return,
1219 			    EFAULT);
1220 			return (EFAULT);
1221 		}
1222 
1223 		if (nval != uval)
1224 			goto retry;
1225 
1226 		if (!pi && (uval & FUTEX_WAITERS)) {
1227 			error = futex_get(uaddr, NULL, &f,
1228 			    FUTEX_DONTCREATE | FUTEX_SHARED);
1229 			if (error) {
1230 				LIN_SDT_PROBE1(futex, handle_futex_death,
1231 				    return, error);
1232 				return (error);
1233 			}
1234 			if (f != NULL) {
1235 				futex_wake(f, 1, FUTEX_BITSET_MATCH_ANY);
1236 				futex_put(f, NULL);
1237 			}
1238 		}
1239 	}
1240 
1241 	LIN_SDT_PROBE1(futex, handle_futex_death, return, 0);
1242 	return (0);
1243 }
1244 
1245 static int
1246 fetch_robust_entry(struct linux_robust_list **entry,
1247     struct linux_robust_list **head, unsigned int *pi)
1248 {
1249 	l_ulong uentry;
1250 	int error;
1251 
1252 	LIN_SDT_PROBE3(futex, fetch_robust_entry, entry, entry, head, pi);
1253 
1254 	error = copyin((const void *)head, &uentry, sizeof(l_ulong));
1255 	if (error) {
1256 		LIN_SDT_PROBE1(futex, fetch_robust_entry, copyin_error, error);
1257 		LIN_SDT_PROBE1(futex, fetch_robust_entry, return, EFAULT);
1258 		return (EFAULT);
1259 	}
1260 
1261 	*entry = (void *)(uentry & ~1UL);
1262 	*pi = uentry & 1;
1263 
1264 	LIN_SDT_PROBE1(futex, fetch_robust_entry, return, 0);
1265 	return (0);
1266 }
1267 
1268 /* This walks the list of robust futexes releasing them. */
1269 void
1270 release_futexes(struct thread *td, struct linux_emuldata *em)
1271 {
1272 	struct linux_robust_list_head *head = NULL;
1273 	struct linux_robust_list *entry, *next_entry, *pending;
1274 	unsigned int limit = 2048, pi, next_pi, pip;
1275 	l_long futex_offset;
1276 	int rc, error;
1277 
1278 	LIN_SDT_PROBE2(futex, release_futexes, entry, td, em);
1279 
1280 	head = em->robust_futexes;
1281 
1282 	if (head == NULL) {
1283 		LIN_SDT_PROBE0(futex, release_futexes, return);
1284 		return;
1285 	}
1286 
1287 	if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi)) {
1288 		LIN_SDT_PROBE0(futex, release_futexes, return);
1289 		return;
1290 	}
1291 
1292 	error = copyin(&head->futex_offset, &futex_offset,
1293 	    sizeof(futex_offset));
1294 	if (error) {
1295 		LIN_SDT_PROBE1(futex, release_futexes, copyin_error, error);
1296 		LIN_SDT_PROBE0(futex, release_futexes, return);
1297 		return;
1298 	}
1299 
1300 	if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip)) {
1301 		LIN_SDT_PROBE0(futex, release_futexes, return);
1302 		return;
1303 	}
1304 
1305 	while (entry != &head->list) {
1306 		rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
1307 
1308 		if (entry != pending)
1309 			if (handle_futex_death(em,
1310 			    (uint32_t *)((caddr_t)entry + futex_offset), pi)) {
1311 				LIN_SDT_PROBE0(futex, release_futexes, return);
1312 				return;
1313 			}
1314 		if (rc) {
1315 			LIN_SDT_PROBE0(futex, release_futexes, return);
1316 			return;
1317 		}
1318 
1319 		entry = next_entry;
1320 		pi = next_pi;
1321 
1322 		if (!--limit)
1323 			break;
1324 
1325 		sched_relinquish(curthread);
1326 	}
1327 
1328 	if (pending)
1329 		handle_futex_death(em, (uint32_t *)((caddr_t)pending + futex_offset), pip);
1330 
1331 	LIN_SDT_PROBE0(futex, release_futexes, return);
1332 }
1333