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