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