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