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