2007-02-25 12:43:07 +00:00
|
|
|
/* $NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $ */
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
/*-
|
|
|
|
* Copyright (c) 2005 Emmanuel Dreyfus, all rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. All advertising materials mentioning features or use of this software
|
|
|
|
* must display the following acknowledgement:
|
|
|
|
* This product includes software developed by Emmanuel Dreyfus
|
2007-02-25 12:40:35 +00:00
|
|
|
* 4. The name of the author may not be used to endorse or promote
|
|
|
|
* products derived from this software without specific prior written
|
2006-08-15 12:20:59 +00:00
|
|
|
* permission.
|
|
|
|
*
|
2007-02-25 12:40:35 +00:00
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE THE AUTHOR AND CONTRIBUTORS ``AS IS''
|
|
|
|
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
|
2006-08-15 12:20:59 +00:00
|
|
|
* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2007-02-25 12:40:35 +00:00
|
|
|
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS
|
2006-08-15 12:20:59 +00:00
|
|
|
* BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
* POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
#if 0
|
2007-02-25 12:43:07 +00:00
|
|
|
__KERNEL_RCSID(1, "$NetBSD: linux_futex.c,v 1.7 2006/07/24 19:01:49 manu Exp $");
|
2006-08-15 12:20:59 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "opt_compat.h"
|
2012-05-05 19:42:38 +00:00
|
|
|
#include "opt_kdtrace.h"
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
#include <sys/param.h>
|
|
|
|
#include <sys/systm.h>
|
2008-05-13 20:01:27 +00:00
|
|
|
#include <sys/imgact.h>
|
2009-03-16 05:39:37 +00:00
|
|
|
#include <sys/kernel.h>
|
2009-05-07 16:14:31 +00:00
|
|
|
#include <sys/ktr.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
#include <sys/lock.h>
|
2009-03-16 05:39:37 +00:00
|
|
|
#include <sys/malloc.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
#include <sys/mutex.h>
|
2008-05-13 20:01:27 +00:00
|
|
|
#include <sys/priv.h>
|
2009-03-16 05:39:37 +00:00
|
|
|
#include <sys/proc.h>
|
|
|
|
#include <sys/queue.h>
|
2008-05-13 20:01:27 +00:00
|
|
|
#include <sys/sched.h>
|
2012-05-05 19:42:38 +00:00
|
|
|
#include <sys/sdt.h>
|
2006-09-09 16:25:25 +00:00
|
|
|
#include <sys/sx.h>
|
2011-02-23 13:23:28 +00:00
|
|
|
#include <sys/umtx.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
#ifdef COMPAT_LINUX32
|
|
|
|
#include <machine/../linux32/linux.h>
|
|
|
|
#include <machine/../linux32/linux32_proto.h>
|
|
|
|
#else
|
|
|
|
#include <machine/../linux/linux.h>
|
|
|
|
#include <machine/../linux/linux_proto.h>
|
|
|
|
#endif
|
2012-05-05 19:42:38 +00:00
|
|
|
#include <compat/linux/linux_dtrace.h>
|
2009-03-15 19:16:12 +00:00
|
|
|
#include <compat/linux/linux_emul.h>
|
2011-02-13 20:07:48 +00:00
|
|
|
#include <compat/linux/linux_futex.h>
|
2009-05-07 16:14:31 +00:00
|
|
|
#include <compat/linux/linux_util.h>
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
/* DTrace init */
|
|
|
|
LIN_SDT_PROVIDER_DECLARE(LINUX_DTRACE);
|
|
|
|
|
|
|
|
/* Linuxulator-global DTrace probes */
|
|
|
|
LIN_SDT_PROBE_DECLARE(locks, emul_lock, locked);
|
|
|
|
LIN_SDT_PROBE_DECLARE(locks, emul_lock, unlock);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Futex part for the special DTrace module "locks".
|
|
|
|
*/
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, locked, "struct mtx *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(locks, futex_mtx, unlock, "struct mtx *");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Per futex probes.
|
|
|
|
*/
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex, create, "struct sx *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex, destroy, "struct sx *");
|
|
|
|
|
|
|
|
/**
|
|
|
|
* DTrace probes in this module.
|
|
|
|
*/
|
|
|
|
LIN_SDT_PROBE_DEFINE2(futex, futex_put, entry, "struct futex *",
|
|
|
|
"struct waiting_proc *");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_put, destroy, "uint32_t *", "uint32_t",
|
|
|
|
"int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_put, unlock, "uint32_t *", "uint32_t",
|
|
|
|
"int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, futex_put, return);
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_get0, entry, "uint32_t *", "struct futex **",
|
|
|
|
"uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_get0, umtx_key_get_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_get0, shared, "uint32_t *", "uint32_t",
|
|
|
|
"int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_get0, null, "uint32_t *");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_get0, new, "uint32_t *", "uint32_t", "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_get0, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_get, entry, "uint32_t *",
|
|
|
|
"struct waiting_proc **", "struct futex **");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, futex_get, error);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_get, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, entry, "struct futex *",
|
|
|
|
"struct waiting_proc **", "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE5(futex, futex_sleep, requeue_error, "int", "uint32_t *",
|
|
|
|
"struct waiting_proc *", "uint32_t *", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_sleep, sleep_error, "int", "uint32_t *",
|
|
|
|
"struct waiting_proc *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_sleep, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_wake, entry, "struct futex *", "int",
|
|
|
|
"uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_wake, iterate, "uint32_t",
|
|
|
|
"struct waiting_proc *", "uin32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_wake, wakeup, "struct waiting_proc *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_wake, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE4(futex, futex_requeue, entry, "struct futex *", "int",
|
|
|
|
"struct futex *", "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, wakeup, "struct waiting_proc *");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_requeue, requeue, "uint32_t *",
|
|
|
|
"struct waiting_proc *", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_requeue, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE4(futex, futex_wait, entry, "struct futex *",
|
|
|
|
"struct waiting_proc **", "struct l_timespec *", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_wait, copyin_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_wait, itimerfix_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_wait, sleep_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_wait, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, futex_atomic_op, entry, "struct thread *",
|
|
|
|
"int", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE4(futex, futex_atomic_op, decoded_op, "int", "int", "int",
|
|
|
|
"int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, futex_atomic_op, missing_access_check);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_op, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, unimplemented_cmp, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, futex_atomic_op, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, entry, "struct thread *",
|
|
|
|
"struct linux_sys_futex_args *");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_clockswitch);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, copyin_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, invalid_cmp_requeue_use);
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wait, "uint32_t *",
|
|
|
|
"uint32_t", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE4(futex, linux_sys_futex, debug_wait_value_neq,
|
|
|
|
"uint32_t *", "uint32_t", "int", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, linux_sys_futex, debug_wake, "uint32_t *",
|
|
|
|
"uint32_t", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_cmp_requeue, "uint32_t *",
|
|
|
|
"uint32_t", "uint32_t", "uint32_t *", "struct l_timespec *");
|
|
|
|
LIN_SDT_PROBE_DEFINE2(futex, linux_sys_futex, debug_cmp_requeue_value_neq,
|
|
|
|
"uint32_t", "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE5(futex, linux_sys_futex, debug_wake_op, "uint32_t *",
|
|
|
|
"int", "uint32_t", "uint32_t *", "uint32_t");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unhandled_efault);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_lock_pi);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_unlock_pi);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_trylock_pi);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, deprecated_requeue);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_wait_requeue_pi);
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_sys_futex, unimplemented_cmp_requeue_pi);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, unknown_operation, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, linux_sys_futex, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE2(futex, linux_set_robust_list, entry, "struct thread *",
|
|
|
|
"struct linux_set_robust_list_args *");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, linux_set_robust_list, size_error);
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, linux_set_robust_list, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE2(futex, linux_get_robust_list, entry, "struct thread *",
|
|
|
|
"struct linux_get_robust_list_args *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, copyout_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, linux_get_robust_list, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, handle_futex_death, entry, "struct proc *",
|
|
|
|
"uint32_t *", "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, copyin_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, handle_futex_death, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE3(futex, fetch_robust_entry, entry,
|
|
|
|
"struct linux_robust_list **", "struct linux_robust_list **", "int *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, copyin_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, fetch_robust_entry, return, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, release_futexes, entry, "struct proc *");
|
|
|
|
LIN_SDT_PROBE_DEFINE1(futex, release_futexes, copyin_error, "int");
|
|
|
|
LIN_SDT_PROBE_DEFINE0(futex, release_futexes, return);
|
|
|
|
|
2011-11-07 06:44:47 +00:00
|
|
|
static MALLOC_DEFINE(M_FUTEX, "futex", "Linux futexes");
|
|
|
|
static MALLOC_DEFINE(M_FUTEX_WP, "futex wp", "Linux futexes wp");
|
2009-05-01 15:36:02 +00:00
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
struct futex;
|
|
|
|
|
|
|
|
struct waiting_proc {
|
2009-05-01 15:36:02 +00:00
|
|
|
uint32_t wp_flags;
|
|
|
|
struct futex *wp_futex;
|
2006-08-15 12:20:59 +00:00
|
|
|
TAILQ_ENTRY(waiting_proc) wp_list;
|
|
|
|
};
|
2009-05-01 15:36:02 +00:00
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
struct futex {
|
2009-05-01 15:36:02 +00:00
|
|
|
struct sx f_lck;
|
2011-02-23 13:23:28 +00:00
|
|
|
uint32_t *f_uaddr; /* user-supplied value, for debug */
|
|
|
|
struct umtx_key f_key;
|
2009-05-01 15:36:02 +00:00
|
|
|
uint32_t f_refcount;
|
2011-01-31 05:59:05 +00:00
|
|
|
uint32_t f_bitset;
|
2006-08-15 12:20:59 +00:00
|
|
|
LIST_ENTRY(futex) f_list;
|
|
|
|
TAILQ_HEAD(lf_waiting_proc, waiting_proc) f_waiting_proc;
|
|
|
|
};
|
|
|
|
|
2009-05-02 10:06:49 +00:00
|
|
|
struct futex_list futex_list;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
#define FUTEX_LOCK(f) sx_xlock(&(f)->f_lck)
|
|
|
|
#define FUTEX_UNLOCK(f) sx_xunlock(&(f)->f_lck)
|
2012-05-05 19:42:38 +00:00
|
|
|
#define FUTEX_INIT(f) do { \
|
|
|
|
sx_init_flags(&(f)->f_lck, "ftlk", \
|
|
|
|
SX_DUPOK); \
|
|
|
|
LIN_SDT_PROBE1(futex, futex, create, \
|
|
|
|
&(f)->f_lck); \
|
|
|
|
} while (0)
|
|
|
|
#define FUTEX_DESTROY(f) do { \
|
|
|
|
LIN_SDT_PROBE1(futex, futex, destroy, \
|
|
|
|
&(f)->f_lck); \
|
|
|
|
sx_destroy(&(f)->f_lck); \
|
|
|
|
} while (0)
|
2009-05-01 15:36:02 +00:00
|
|
|
#define FUTEX_ASSERT_LOCKED(f) sx_assert(&(f)->f_lck, SA_XLOCKED)
|
|
|
|
|
|
|
|
struct mtx futex_mtx; /* protects the futex list */
|
2012-05-05 19:42:38 +00:00
|
|
|
#define FUTEXES_LOCK do { \
|
|
|
|
mtx_lock(&futex_mtx); \
|
|
|
|
LIN_SDT_PROBE1(locks, futex_mtx, \
|
|
|
|
locked, &futex_mtx); \
|
|
|
|
} while (0)
|
|
|
|
#define FUTEXES_UNLOCK do { \
|
|
|
|
LIN_SDT_PROBE1(locks, futex_mtx, \
|
|
|
|
unlock, &futex_mtx); \
|
|
|
|
mtx_unlock(&futex_mtx); \
|
|
|
|
} while (0)
|
2009-05-01 15:36:02 +00:00
|
|
|
|
|
|
|
/* flags for futex_get() */
|
|
|
|
#define FUTEX_CREATE_WP 0x1 /* create waiting_proc */
|
|
|
|
#define FUTEX_DONTCREATE 0x2 /* don't create futex if not exists */
|
|
|
|
#define FUTEX_DONTEXISTS 0x4 /* return EINVAL if futex exists */
|
2011-02-23 13:23:28 +00:00
|
|
|
#define FUTEX_SHARED 0x8 /* shared futex */
|
2009-05-01 15:36:02 +00:00
|
|
|
|
|
|
|
/* wp_flags */
|
|
|
|
#define FUTEX_WP_REQUEUED 0x1 /* wp requeued - wp moved from wp_list
|
|
|
|
* of futex where thread sleep to wp_list
|
|
|
|
* of another futex.
|
|
|
|
*/
|
|
|
|
#define FUTEX_WP_REMOVED 0x2 /* wp is woken up and removed from futex
|
|
|
|
* wp_list to prevent double wakeup.
|
|
|
|
*/
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
/* support.s */
|
2009-05-01 15:36:02 +00:00
|
|
|
int futex_xchgl(int oparg, uint32_t *uaddr, int *oldval);
|
|
|
|
int futex_addl(int oparg, uint32_t *uaddr, int *oldval);
|
|
|
|
int futex_orl(int oparg, uint32_t *uaddr, int *oldval);
|
|
|
|
int futex_andl(int oparg, uint32_t *uaddr, int *oldval);
|
|
|
|
int futex_xorl(int oparg, uint32_t *uaddr, int *oldval);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
static void
|
|
|
|
futex_put(struct futex *f, struct waiting_proc *wp)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE2(futex, futex_put, entry, f, wp);
|
2008-03-20 17:03:55 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEX_ASSERT_LOCKED(f);
|
|
|
|
if (wp != NULL) {
|
|
|
|
if ((wp->wp_flags & FUTEX_WP_REMOVED) == 0)
|
|
|
|
TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
|
|
|
|
free(wp, M_FUTEX_WP);
|
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEXES_LOCK;
|
|
|
|
if (--f->f_refcount == 0) {
|
|
|
|
LIST_REMOVE(f, f_list);
|
|
|
|
FUTEXES_UNLOCK;
|
|
|
|
FUTEX_UNLOCK(f);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_put, destroy, f->f_uaddr,
|
|
|
|
f->f_refcount, f->f_key.shared);
|
2011-03-03 18:29:55 +00:00
|
|
|
LINUX_CTR3(sys_futex, "futex_put destroy uaddr %p ref %d "
|
|
|
|
"shared %d", f->f_uaddr, f->f_refcount, f->f_key.shared);
|
2011-02-23 13:23:28 +00:00
|
|
|
umtx_key_release(&f->f_key);
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEX_DESTROY(f);
|
|
|
|
free(f, M_FUTEX);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE0(futex, futex_put, return);
|
2009-05-01 15:36:02 +00:00
|
|
|
return;
|
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_put, unlock, f->f_uaddr, f->f_refcount,
|
|
|
|
f->f_key.shared);
|
2011-03-03 18:29:55 +00:00
|
|
|
LINUX_CTR3(sys_futex, "futex_put uaddr %p ref %d shared %d",
|
|
|
|
f->f_uaddr, f->f_refcount, f->f_key.shared);
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEXES_UNLOCK;
|
|
|
|
FUTEX_UNLOCK(f);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE0(futex, futex_put, return);
|
2009-05-01 15:36:02 +00:00
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
static int
|
|
|
|
futex_get0(uint32_t *uaddr, struct futex **newf, uint32_t flags)
|
|
|
|
{
|
|
|
|
struct futex *f, *tmpf;
|
2011-02-23 13:23:28 +00:00
|
|
|
struct umtx_key key;
|
|
|
|
int error;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_get0, entry, uaddr, newf, flags);
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
*newf = tmpf = NULL;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2011-02-23 13:23:28 +00:00
|
|
|
error = umtx_key_get(uaddr, TYPE_FUTEX, (flags & FUTEX_SHARED) ?
|
2011-03-03 18:19:10 +00:00
|
|
|
AUTO_SHARE : THREAD_SHARE, &key);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, futex_get0, umtx_key_get_error, error);
|
|
|
|
LIN_SDT_PROBE1(futex, futex_get0, return, error);
|
2011-02-23 13:23:28 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2009-05-01 15:36:02 +00:00
|
|
|
retry:
|
|
|
|
FUTEXES_LOCK;
|
|
|
|
LIST_FOREACH(f, &futex_list, f_list) {
|
2011-02-23 13:23:28 +00:00
|
|
|
if (umtx_key_match(&f->f_key, &key)) {
|
2009-05-01 15:36:02 +00:00
|
|
|
if (tmpf != NULL) {
|
|
|
|
FUTEX_UNLOCK(tmpf);
|
|
|
|
FUTEX_DESTROY(tmpf);
|
|
|
|
free(tmpf, M_FUTEX);
|
|
|
|
}
|
|
|
|
if (flags & FUTEX_DONTEXISTS) {
|
|
|
|
FUTEXES_UNLOCK;
|
2011-02-23 13:23:28 +00:00
|
|
|
umtx_key_release(&key);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, futex_get0, return,
|
|
|
|
EINVAL);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (EINVAL);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2007-03-30 01:07:28 +00:00
|
|
|
/*
|
2009-05-01 15:36:02 +00:00
|
|
|
* Increment refcount of the found futex to
|
|
|
|
* prevent it from deallocation before FUTEX_LOCK()
|
2006-08-26 10:36:16 +00:00
|
|
|
*/
|
2009-05-01 15:36:02 +00:00
|
|
|
++f->f_refcount;
|
|
|
|
FUTEXES_UNLOCK;
|
2011-02-23 13:23:28 +00:00
|
|
|
umtx_key_release(&key);
|
2008-03-20 17:03:55 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEX_LOCK(f);
|
|
|
|
*newf = f;
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_get0, shared, uaddr,
|
|
|
|
f->f_refcount, f->f_key.shared);
|
2011-03-03 18:29:55 +00:00
|
|
|
LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d",
|
|
|
|
uaddr, f->f_refcount, f->f_key.shared);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, futex_get0, return, 0);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
}
|
2008-03-20 17:03:55 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
if (flags & FUTEX_DONTCREATE) {
|
|
|
|
FUTEXES_UNLOCK;
|
2011-02-23 13:23:28 +00:00
|
|
|
umtx_key_release(&key);
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_get0, null, uaddr);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR1(sys_futex, "futex_get uaddr %p null", uaddr);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, futex_get0, return, 0);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (0);
|
|
|
|
}
|
2008-03-20 17:03:55 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
if (tmpf == NULL) {
|
|
|
|
FUTEXES_UNLOCK;
|
|
|
|
tmpf = malloc(sizeof(*tmpf), M_FUTEX, M_WAITOK | M_ZERO);
|
|
|
|
tmpf->f_uaddr = uaddr;
|
2011-02-23 13:23:28 +00:00
|
|
|
tmpf->f_key = key;
|
2009-05-01 15:36:02 +00:00
|
|
|
tmpf->f_refcount = 1;
|
2011-02-13 17:56:22 +00:00
|
|
|
tmpf->f_bitset = FUTEX_BITSET_MATCH_ANY;
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEX_INIT(tmpf);
|
|
|
|
TAILQ_INIT(&tmpf->f_waiting_proc);
|
2009-04-19 13:48:42 +00:00
|
|
|
|
|
|
|
/*
|
2009-05-01 15:36:02 +00:00
|
|
|
* Lock the new futex before an insert into the futex_list
|
|
|
|
* to prevent futex usage by other.
|
2009-04-19 13:48:42 +00:00
|
|
|
*/
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEX_LOCK(tmpf);
|
|
|
|
goto retry;
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
2009-05-01 15:36:02 +00:00
|
|
|
|
|
|
|
LIST_INSERT_HEAD(&futex_list, tmpf, f_list);
|
|
|
|
FUTEXES_UNLOCK;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_get0, new, uaddr, tmpf->f_refcount,
|
|
|
|
tmpf->f_key.shared);
|
2011-03-03 18:29:55 +00:00
|
|
|
LINUX_CTR3(sys_futex, "futex_get uaddr %p ref %d shared %d new",
|
|
|
|
uaddr, tmpf->f_refcount, tmpf->f_key.shared);
|
2009-05-01 15:36:02 +00:00
|
|
|
*newf = tmpf;
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, futex_get0, return, 0);
|
2007-05-23 08:33:06 +00:00
|
|
|
return (0);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
static int
|
|
|
|
futex_get(uint32_t *uaddr, struct waiting_proc **wp, struct futex **f,
|
|
|
|
uint32_t flags)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2009-05-01 15:36:02 +00:00
|
|
|
int error;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_get, entry, uaddr, wp, f);
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
if (flags & FUTEX_CREATE_WP) {
|
|
|
|
*wp = malloc(sizeof(struct waiting_proc), M_FUTEX_WP, M_WAITOK);
|
|
|
|
(*wp)->wp_flags = 0;
|
|
|
|
}
|
|
|
|
error = futex_get0(uaddr, f, flags);
|
|
|
|
if (error) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, futex_get, error);
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
if (flags & FUTEX_CREATE_WP)
|
|
|
|
free(*wp, M_FUTEX_WP);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, futex_get, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
if (flags & FUTEX_CREATE_WP) {
|
|
|
|
TAILQ_INSERT_HEAD(&(*f)->f_waiting_proc, *wp, wp_list);
|
|
|
|
(*wp)->wp_futex = *f;
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_get, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
static int
|
2010-10-06 18:51:22 +00:00
|
|
|
futex_sleep(struct futex *f, struct waiting_proc *wp, int timeout)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2009-05-01 15:36:02 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
FUTEX_ASSERT_LOCKED(f);
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_sleep, entry, f, wp, timeout);
|
2010-10-06 18:51:22 +00:00
|
|
|
LINUX_CTR4(sys_futex, "futex_sleep enter uaddr %p wp %p timo %d ref %d",
|
2009-05-07 16:14:31 +00:00
|
|
|
f->f_uaddr, wp, timeout, f->f_refcount);
|
2009-05-01 15:36:02 +00:00
|
|
|
error = sx_sleep(wp, &f->f_lck, PCATCH, "futex", timeout);
|
|
|
|
if (wp->wp_flags & FUTEX_WP_REQUEUED) {
|
|
|
|
KASSERT(f != wp->wp_futex, ("futex != wp_futex"));
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE5(futex, futex_sleep, requeue_error, error,
|
|
|
|
f->f_uaddr, wp, wp->wp_futex->f_uaddr,
|
|
|
|
wp->wp_futex->f_refcount);
|
|
|
|
}
|
|
|
|
|
|
|
|
LINUX_CTR5(sys_futex, "futex_sleep out error %d uaddr %p wp"
|
2009-05-07 16:14:31 +00:00
|
|
|
" %p requeued uaddr %p ref %d",
|
|
|
|
error, f->f_uaddr, wp, wp->wp_futex->f_uaddr,
|
|
|
|
wp->wp_futex->f_refcount);
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f, NULL);
|
|
|
|
f = wp->wp_futex;
|
|
|
|
FUTEX_LOCK(f);
|
2012-05-05 19:42:38 +00:00
|
|
|
} else {
|
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE3(futex, futex_sleep, sleep_error, error,
|
|
|
|
f->f_uaddr, wp);
|
|
|
|
}
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR3(sys_futex, "futex_sleep out error %d uaddr %p wp %p",
|
|
|
|
error, f->f_uaddr, wp);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f, wp);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, futex_sleep, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
2007-02-25 12:40:35 +00:00
|
|
|
static int
|
2011-01-31 05:59:05 +00:00
|
|
|
futex_wake(struct futex *f, int n, uint32_t bitset)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2009-05-01 15:36:02 +00:00
|
|
|
struct waiting_proc *wp, *wpt;
|
|
|
|
int count = 0;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_wake, entry, f, n, bitset);
|
|
|
|
|
|
|
|
if (bitset == 0) {
|
|
|
|
LIN_SDT_PROBE1(futex, futex_wake, return, EINVAL);
|
2011-01-31 05:59:05 +00:00
|
|
|
return (EINVAL);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2011-01-31 05:59:05 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEX_ASSERT_LOCKED(f);
|
|
|
|
TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_wake, iterate, f->f_uaddr, wp,
|
|
|
|
f->f_refcount);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR3(sys_futex, "futex_wake uaddr %p wp %p ref %d",
|
|
|
|
f->f_uaddr, wp, f->f_refcount);
|
2011-01-31 05:59:05 +00:00
|
|
|
/*
|
|
|
|
* Unless we find a matching bit in
|
|
|
|
* the bitset, continue searching.
|
|
|
|
*/
|
|
|
|
if (!(wp->wp_futex->f_bitset & bitset))
|
|
|
|
continue;
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
wp->wp_flags |= FUTEX_WP_REMOVED;
|
|
|
|
TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_wake, wakeup, wp);
|
2009-05-01 15:36:02 +00:00
|
|
|
wakeup_one(wp);
|
|
|
|
if (++count == n)
|
|
|
|
break;
|
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_wake, return, count);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (count);
|
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
static int
|
|
|
|
futex_requeue(struct futex *f, int n, struct futex *f2, int n2)
|
|
|
|
{
|
|
|
|
struct waiting_proc *wp, *wpt;
|
|
|
|
int count = 0;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE4(futex, futex_requeue, entry, f, n, f2, n2);
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
FUTEX_ASSERT_LOCKED(f);
|
|
|
|
FUTEX_ASSERT_LOCKED(f2);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
TAILQ_FOREACH_SAFE(wp, &f->f_waiting_proc, wp_list, wpt) {
|
|
|
|
if (++count <= n) {
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR2(sys_futex, "futex_req_wake uaddr %p wp %p",
|
|
|
|
f->f_uaddr, wp);
|
2009-05-01 15:36:02 +00:00
|
|
|
wp->wp_flags |= FUTEX_WP_REMOVED;
|
|
|
|
TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_requeue, wakeup, wp);
|
2009-05-01 15:36:02 +00:00
|
|
|
wakeup_one(wp);
|
|
|
|
} else {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_requeue, requeue,
|
|
|
|
f->f_uaddr, wp, f2->f_uaddr);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR3(sys_futex, "futex_requeue uaddr %p wp %p to %p",
|
|
|
|
f->f_uaddr, wp, f2->f_uaddr);
|
2009-05-01 15:36:02 +00:00
|
|
|
wp->wp_flags |= FUTEX_WP_REQUEUED;
|
|
|
|
/* Move wp to wp_list of f2 futex */
|
|
|
|
TAILQ_REMOVE(&f->f_waiting_proc, wp, wp_list);
|
|
|
|
TAILQ_INSERT_HEAD(&f2->f_waiting_proc, wp, wp_list);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
/*
|
|
|
|
* Thread which sleeps on wp after waking should
|
|
|
|
* acquire f2 lock, so increment refcount of f2 to
|
|
|
|
* prevent it from premature deallocation.
|
|
|
|
*/
|
|
|
|
wp->wp_futex = f2;
|
|
|
|
FUTEXES_LOCK;
|
|
|
|
++f2->f_refcount;
|
|
|
|
FUTEXES_UNLOCK;
|
|
|
|
if (count - n >= n2)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_requeue, return, count);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (count);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2011-01-31 05:59:05 +00:00
|
|
|
futex_wait(struct futex *f, struct waiting_proc *wp, struct l_timespec *ts,
|
|
|
|
uint32_t bitset)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2010-10-06 18:51:22 +00:00
|
|
|
struct l_timespec timeout;
|
|
|
|
struct timeval tv;
|
2009-05-01 15:36:02 +00:00
|
|
|
int timeout_hz;
|
|
|
|
int error;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE4(futex, futex_wait, entry, f, wp, ts, bitset);
|
|
|
|
|
|
|
|
if (bitset == 0) {
|
|
|
|
LIN_SDT_PROBE1(futex, futex_wait, return, EINVAL);
|
2011-01-31 05:59:05 +00:00
|
|
|
return (EINVAL);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
|
|
|
|
2011-01-31 05:59:05 +00:00
|
|
|
f->f_bitset = bitset;
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
if (ts != NULL) {
|
|
|
|
error = copyin(ts, &timeout, sizeof(timeout));
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, futex_wait, copyin_error, error);
|
|
|
|
LIN_SDT_PROBE1(futex, futex_wait, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2010-10-06 18:51:22 +00:00
|
|
|
TIMESPEC_TO_TIMEVAL(&tv, &timeout);
|
|
|
|
error = itimerfix(&tv);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, futex_wait, itimerfix_error,
|
|
|
|
error);
|
|
|
|
LIN_SDT_PROBE1(futex, futex_wait, return, error);
|
2010-10-06 18:51:22 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2010-10-06 18:51:22 +00:00
|
|
|
timeout_hz = tvtohz(&tv);
|
|
|
|
} else
|
2009-05-01 15:36:02 +00:00
|
|
|
timeout_hz = 0;
|
2007-03-30 01:07:28 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
error = futex_sleep(f, wp, timeout_hz);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, futex_wait, sleep_error, error);
|
|
|
|
}
|
2009-05-01 15:36:02 +00:00
|
|
|
if (error == EWOULDBLOCK)
|
|
|
|
error = ETIMEDOUT;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_wait, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_atomic_op(struct thread *td, int encoded_op, uint32_t *uaddr)
|
2006-08-15 12:20:59 +00:00
|
|
|
{
|
2007-03-30 01:07:28 +00:00
|
|
|
int op = (encoded_op >> 28) & 7;
|
|
|
|
int cmp = (encoded_op >> 24) & 15;
|
|
|
|
int oparg = (encoded_op << 8) >> 20;
|
|
|
|
int cmparg = (encoded_op << 20) >> 20;
|
|
|
|
int oldval = 0, ret;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, futex_atomic_op, entry, td, encoded_op, uaddr);
|
|
|
|
|
2006-08-15 12:20:59 +00:00
|
|
|
if (encoded_op & (FUTEX_OP_OPARG_SHIFT << 28))
|
2007-03-30 01:07:28 +00:00
|
|
|
oparg = 1 << oparg;
|
2006-08-15 12:20:59 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE4(futex, futex_atomic_op, decoded_op, op, cmp, oparg,
|
|
|
|
cmparg);
|
|
|
|
|
2011-03-10 19:50:12 +00:00
|
|
|
/* XXX: Linux verifies access here and returns EFAULT */
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, futex_atomic_op, missing_access_check);
|
2006-08-15 12:20:59 +00:00
|
|
|
|
|
|
|
switch (op) {
|
2007-03-30 01:07:28 +00:00
|
|
|
case FUTEX_OP_SET:
|
|
|
|
ret = futex_xchgl(oparg, uaddr, &oldval);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_ADD:
|
|
|
|
ret = futex_addl(oparg, uaddr, &oldval);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_OR:
|
|
|
|
ret = futex_orl(oparg, uaddr, &oldval);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_ANDN:
|
|
|
|
ret = futex_andl(~oparg, uaddr, &oldval);
|
|
|
|
break;
|
|
|
|
case FUTEX_OP_XOR:
|
|
|
|
ret = futex_xorl(oparg, uaddr, &oldval);
|
|
|
|
break;
|
|
|
|
default:
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_op, op);
|
2007-03-30 01:07:28 +00:00
|
|
|
ret = -ENOSYS;
|
2007-05-23 08:33:06 +00:00
|
|
|
break;
|
2007-03-30 01:07:28 +00:00
|
|
|
}
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (ret) {
|
|
|
|
LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
|
2007-05-23 08:33:06 +00:00
|
|
|
return (ret);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2007-05-23 08:33:06 +00:00
|
|
|
|
|
|
|
switch (cmp) {
|
|
|
|
case FUTEX_OP_CMP_EQ:
|
2012-05-05 19:42:38 +00:00
|
|
|
ret = (oldval == cmparg);
|
|
|
|
break;
|
2007-05-23 08:33:06 +00:00
|
|
|
case FUTEX_OP_CMP_NE:
|
2012-05-05 19:42:38 +00:00
|
|
|
ret = (oldval != cmparg);
|
|
|
|
break;
|
2007-05-23 08:33:06 +00:00
|
|
|
case FUTEX_OP_CMP_LT:
|
2012-05-05 19:42:38 +00:00
|
|
|
ret = (oldval < cmparg);
|
|
|
|
break;
|
2007-05-23 08:33:06 +00:00
|
|
|
case FUTEX_OP_CMP_GE:
|
2012-05-05 19:42:38 +00:00
|
|
|
ret = (oldval >= cmparg);
|
|
|
|
break;
|
2007-05-23 08:33:06 +00:00
|
|
|
case FUTEX_OP_CMP_LE:
|
2012-05-05 19:42:38 +00:00
|
|
|
ret = (oldval <= cmparg);
|
|
|
|
break;
|
2007-05-23 08:33:06 +00:00
|
|
|
case FUTEX_OP_CMP_GT:
|
2012-05-05 19:42:38 +00:00
|
|
|
ret = (oldval > cmparg);
|
|
|
|
break;
|
2007-05-23 08:33:06 +00:00
|
|
|
default:
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, futex_atomic_op, unimplemented_cmp, cmp);
|
|
|
|
ret = -ENOSYS;
|
2007-03-30 01:07:28 +00:00
|
|
|
}
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, futex_atomic_op, return, ret);
|
|
|
|
return (ret);
|
2006-08-15 12:20:59 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
int
|
|
|
|
linux_sys_futex(struct thread *td, struct linux_sys_futex_args *args)
|
|
|
|
{
|
2010-11-15 13:03:35 +00:00
|
|
|
int clockrt, nrwake, op_ret, ret, val;
|
2009-05-01 15:36:02 +00:00
|
|
|
struct linux_emuldata *em;
|
|
|
|
struct waiting_proc *wp;
|
2011-02-14 17:24:58 +00:00
|
|
|
struct futex *f, *f2;
|
|
|
|
int error;
|
2011-02-23 13:23:28 +00:00
|
|
|
uint32_t flags;
|
2009-05-01 15:36:02 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE2(futex, linux_sys_futex, entry, td, args);
|
|
|
|
|
2011-02-23 13:23:28 +00:00
|
|
|
if (args->op & LINUX_FUTEX_PRIVATE_FLAG) {
|
|
|
|
flags = 0;
|
|
|
|
args->op &= ~LINUX_FUTEX_PRIVATE_FLAG;
|
|
|
|
} else
|
|
|
|
flags = FUTEX_SHARED;
|
2010-11-15 13:03:35 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Currently support for switching between CLOCK_MONOTONIC and
|
|
|
|
* CLOCK_REALTIME is not present. However Linux forbids the use of
|
|
|
|
* FUTEX_CLOCK_REALTIME with any op except FUTEX_WAIT_BITSET and
|
|
|
|
* FUTEX_WAIT_REQUEUE_PI.
|
|
|
|
*/
|
|
|
|
clockrt = args->op & LINUX_FUTEX_CLOCK_REALTIME;
|
|
|
|
args->op = args->op & ~LINUX_FUTEX_CLOCK_REALTIME;
|
|
|
|
if (clockrt && args->op != LINUX_FUTEX_WAIT_BITSET &&
|
2012-05-05 19:42:38 +00:00
|
|
|
args->op != LINUX_FUTEX_WAIT_REQUEUE_PI) {
|
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex,
|
|
|
|
unimplemented_clockswitch);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
|
2010-11-15 13:03:35 +00:00
|
|
|
return (ENOSYS);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2009-05-01 15:36:02 +00:00
|
|
|
|
2011-02-14 17:24:58 +00:00
|
|
|
error = 0;
|
|
|
|
f = f2 = NULL;
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
switch (args->op) {
|
|
|
|
case LINUX_FUTEX_WAIT:
|
2011-01-31 05:59:05 +00:00
|
|
|
args->val3 = FUTEX_BITSET_MATCH_ANY;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
case LINUX_FUTEX_WAIT_BITSET:
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wait, args->uaddr,
|
|
|
|
args->val, args->val3);
|
2011-01-31 05:59:05 +00:00
|
|
|
LINUX_CTR3(sys_futex, "WAIT uaddr %p val %d val3 %d",
|
|
|
|
args->uaddr, args->val, args->val3);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
2011-02-23 13:23:28 +00:00
|
|
|
error = futex_get(args->uaddr, &wp, &f,
|
|
|
|
flags | FUTEX_CREATE_WP);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
error = copyin(args->uaddr, &val, sizeof(val));
|
|
|
|
if (error) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
|
|
|
|
error);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR1(sys_futex, "WAIT copyin failed %d",
|
|
|
|
error);
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f, wp);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
if (val != args->val) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE4(futex, linux_sys_futex,
|
|
|
|
debug_wait_value_neq, args->uaddr, args->val, val,
|
|
|
|
args->val3);
|
2011-01-31 05:59:05 +00:00
|
|
|
LINUX_CTR4(sys_futex,
|
|
|
|
"WAIT uaddr %p val %d != uval %d val3 %d",
|
|
|
|
args->uaddr, args->val, val, args->val3);
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f, wp);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return,
|
|
|
|
EWOULDBLOCK);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (EWOULDBLOCK);
|
|
|
|
}
|
|
|
|
|
2011-01-31 05:59:05 +00:00
|
|
|
error = futex_wait(f, wp, args->timeout, args->val3);
|
2009-05-01 15:36:02 +00:00
|
|
|
break;
|
|
|
|
|
|
|
|
case LINUX_FUTEX_WAKE:
|
2011-01-31 05:59:05 +00:00
|
|
|
args->val3 = FUTEX_BITSET_MATCH_ANY;
|
|
|
|
/* FALLTHROUGH */
|
|
|
|
|
|
|
|
case LINUX_FUTEX_WAKE_BITSET:
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, linux_sys_futex, debug_wake, args->uaddr,
|
|
|
|
args->val, args->val3);
|
2011-01-31 05:59:05 +00:00
|
|
|
LINUX_CTR3(sys_futex, "WAKE uaddr %p val % d val3 %d",
|
|
|
|
args->uaddr, args->val, args->val3);
|
2009-05-07 16:14:31 +00:00
|
|
|
|
2011-02-23 13:23:28 +00:00
|
|
|
error = futex_get(args->uaddr, NULL, &f,
|
|
|
|
flags | FUTEX_DONTCREATE);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
if (f == NULL) {
|
|
|
|
td->td_retval[0] = 0;
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2010-01-07 21:01:37 +00:00
|
|
|
return (error);
|
2009-05-01 15:36:02 +00:00
|
|
|
}
|
2011-01-31 05:59:05 +00:00
|
|
|
td->td_retval[0] = futex_wake(f, args->val, args->val3);
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f, NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LINUX_FUTEX_CMP_REQUEUE:
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE5(futex, linux_sys_futex, debug_cmp_requeue,
|
|
|
|
args->uaddr, args->val, args->val3, args->uaddr2,
|
|
|
|
args->timeout);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR5(sys_futex, "CMP_REQUEUE uaddr %p "
|
|
|
|
"val %d val3 %d uaddr2 %p val2 %d",
|
|
|
|
args->uaddr, args->val, args->val3, args->uaddr2,
|
|
|
|
(int)(unsigned long)args->timeout);
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
/*
|
|
|
|
* Linux allows this, we would not, it is an incorrect
|
|
|
|
* usage of declared ABI, so return EINVAL.
|
|
|
|
*/
|
2012-05-05 19:42:38 +00:00
|
|
|
if (args->uaddr == args->uaddr2) {
|
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex,
|
|
|
|
invalid_cmp_requeue_use);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (EINVAL);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
|
|
|
|
2011-02-23 13:23:28 +00:00
|
|
|
error = futex_get(args->uaddr, NULL, &f, flags);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2009-05-01 15:36:02 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* To avoid deadlocks return EINVAL if second futex
|
2011-02-13 18:46:34 +00:00
|
|
|
* exists at this time.
|
2009-05-01 15:36:02 +00:00
|
|
|
*
|
|
|
|
* Glibc fall back to FUTEX_WAKE in case of any error
|
|
|
|
* returned by FUTEX_CMP_REQUEUE.
|
|
|
|
*/
|
2011-02-23 13:23:28 +00:00
|
|
|
error = futex_get(args->uaddr2, NULL, &f2,
|
|
|
|
flags | FUTEX_DONTEXISTS);
|
2009-05-01 15:36:02 +00:00
|
|
|
if (error) {
|
|
|
|
futex_put(f, NULL);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
error = copyin(args->uaddr, &val, sizeof(val));
|
|
|
|
if (error) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, copyin_error,
|
|
|
|
error);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR1(sys_futex, "CMP_REQUEUE copyin failed %d",
|
|
|
|
error);
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f2, NULL);
|
|
|
|
futex_put(f, NULL);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
if (val != args->val3) {
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE2(futex, linux_sys_futex,
|
|
|
|
debug_cmp_requeue_value_neq, args->val, val);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR2(sys_futex, "CMP_REQUEUE val %d != uval %d",
|
|
|
|
args->val, val);
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f2, NULL);
|
|
|
|
futex_put(f, NULL);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, EAGAIN);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (EAGAIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
nrwake = (int)(unsigned long)args->timeout;
|
|
|
|
td->td_retval[0] = futex_requeue(f, args->val, f2, nrwake);
|
|
|
|
futex_put(f2, NULL);
|
|
|
|
futex_put(f, NULL);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LINUX_FUTEX_WAKE_OP:
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE5(futex, linux_sys_futex, debug_wake_op,
|
|
|
|
args->uaddr, args->op, args->val, args->uaddr2, args->val3);
|
2009-05-07 16:14:31 +00:00
|
|
|
LINUX_CTR5(sys_futex, "WAKE_OP "
|
|
|
|
"uaddr %p op %d val %x uaddr2 %p val3 %x",
|
|
|
|
args->uaddr, args->op, args->val,
|
|
|
|
args->uaddr2, args->val3);
|
|
|
|
|
2011-02-23 13:23:28 +00:00
|
|
|
error = futex_get(args->uaddr, NULL, &f, flags);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
if (args->uaddr != args->uaddr2)
|
2011-02-23 13:23:28 +00:00
|
|
|
error = futex_get(args->uaddr2, NULL, &f2, flags);
|
2009-05-01 15:36:02 +00:00
|
|
|
if (error) {
|
|
|
|
futex_put(f, NULL);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function returns positive number as results and
|
|
|
|
* negative as errors
|
|
|
|
*/
|
|
|
|
op_ret = futex_atomic_op(td, args->val3, args->uaddr2);
|
|
|
|
|
|
|
|
if (op_ret < 0) {
|
|
|
|
/* XXX: We don't handle the EFAULT yet. */
|
|
|
|
if (op_ret != -EFAULT) {
|
|
|
|
if (f2 != NULL)
|
|
|
|
futex_put(f2, NULL);
|
|
|
|
futex_put(f, NULL);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return,
|
|
|
|
-op_ret);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (-op_ret);
|
2012-05-05 19:42:38 +00:00
|
|
|
} else {
|
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex,
|
|
|
|
unhandled_efault);
|
2009-05-01 15:36:02 +00:00
|
|
|
}
|
|
|
|
if (f2 != NULL)
|
|
|
|
futex_put(f2, NULL);
|
|
|
|
futex_put(f, NULL);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, EFAULT);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (EFAULT);
|
|
|
|
}
|
|
|
|
|
2011-01-31 05:59:05 +00:00
|
|
|
ret = futex_wake(f, args->val, args->val3);
|
2009-05-01 15:36:02 +00:00
|
|
|
|
|
|
|
if (op_ret > 0) {
|
|
|
|
op_ret = 0;
|
|
|
|
nrwake = (int)(unsigned long)args->timeout;
|
|
|
|
|
|
|
|
if (f2 != NULL)
|
2011-01-31 05:59:05 +00:00
|
|
|
op_ret += futex_wake(f2, nrwake, args->val3);
|
2009-05-01 15:36:02 +00:00
|
|
|
else
|
2011-01-31 05:59:05 +00:00
|
|
|
op_ret += futex_wake(f, nrwake, args->val3);
|
2009-05-01 15:36:02 +00:00
|
|
|
ret += op_ret;
|
|
|
|
|
|
|
|
}
|
|
|
|
if (f2 != NULL)
|
|
|
|
futex_put(f2, NULL);
|
|
|
|
futex_put(f, NULL);
|
|
|
|
td->td_retval[0] = ret;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LINUX_FUTEX_LOCK_PI:
|
|
|
|
/* not yet implemented */
|
2010-11-15 13:03:35 +00:00
|
|
|
linux_msg(td,
|
|
|
|
"linux_sys_futex: "
|
2010-11-22 09:25:32 +00:00
|
|
|
"op LINUX_FUTEX_LOCK_PI not implemented\n");
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex, unimplemented_lock_pi);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (ENOSYS);
|
|
|
|
|
|
|
|
case LINUX_FUTEX_UNLOCK_PI:
|
|
|
|
/* not yet implemented */
|
2010-11-15 13:03:35 +00:00
|
|
|
linux_msg(td,
|
|
|
|
"linux_sys_futex: "
|
2010-11-22 09:25:32 +00:00
|
|
|
"op LINUX_FUTEX_UNLOCK_PI not implemented\n");
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex, unimplemented_unlock_pi);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (ENOSYS);
|
|
|
|
|
|
|
|
case LINUX_FUTEX_TRYLOCK_PI:
|
|
|
|
/* not yet implemented */
|
2010-11-15 13:03:35 +00:00
|
|
|
linux_msg(td,
|
|
|
|
"linux_sys_futex: "
|
2010-11-22 09:25:32 +00:00
|
|
|
"op LINUX_FUTEX_TRYLOCK_PI not implemented\n");
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex,
|
|
|
|
unimplemented_trylock_pi);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (ENOSYS);
|
|
|
|
|
|
|
|
case LINUX_FUTEX_REQUEUE:
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Glibc does not use this operation since version 2.3.3,
|
|
|
|
* as it is racy and replaced by FUTEX_CMP_REQUEUE operation.
|
|
|
|
* Glibc versions prior to 2.3.3 fall back to FUTEX_WAKE when
|
|
|
|
* FUTEX_REQUEUE returned EINVAL.
|
|
|
|
*/
|
|
|
|
em = em_find(td->td_proc, EMUL_DONTLOCK);
|
2011-02-12 20:58:59 +00:00
|
|
|
if ((em->flags & LINUX_XDEPR_REQUEUEOP) == 0) {
|
2010-11-15 13:03:35 +00:00
|
|
|
linux_msg(td,
|
|
|
|
"linux_sys_futex: "
|
|
|
|
"unsupported futex_requeue op\n");
|
2011-02-12 20:58:59 +00:00
|
|
|
em->flags |= LINUX_XDEPR_REQUEUEOP;
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex,
|
|
|
|
deprecated_requeue);
|
2009-05-01 15:36:02 +00:00
|
|
|
}
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, EINVAL);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (EINVAL);
|
|
|
|
|
2010-11-15 13:03:35 +00:00
|
|
|
case LINUX_FUTEX_WAIT_REQUEUE_PI:
|
|
|
|
/* not yet implemented */
|
|
|
|
linux_msg(td,
|
|
|
|
"linux_sys_futex: "
|
2010-11-22 09:25:32 +00:00
|
|
|
"op FUTEX_WAIT_REQUEUE_PI not implemented\n");
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex,
|
|
|
|
unimplemented_wait_requeue_pi);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
|
2010-11-15 13:03:35 +00:00
|
|
|
return (ENOSYS);
|
|
|
|
|
2011-01-31 06:06:23 +00:00
|
|
|
case LINUX_FUTEX_CMP_REQUEUE_PI:
|
|
|
|
/* not yet implemented */
|
|
|
|
linux_msg(td,
|
|
|
|
"linux_sys_futex: "
|
|
|
|
"op LINUX_FUTEX_CMP_REQUEUE_PI not implemented\n");
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE0(futex, linux_sys_futex,
|
|
|
|
unimplemented_cmp_requeue_pi);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
|
2011-01-31 06:06:23 +00:00
|
|
|
return (ENOSYS);
|
|
|
|
|
2009-05-01 15:36:02 +00:00
|
|
|
default:
|
2010-11-15 13:03:35 +00:00
|
|
|
linux_msg(td,
|
|
|
|
"linux_sys_futex: unknown op %d\n", args->op);
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, unknown_operation,
|
|
|
|
args->op);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, ENOSYS);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (ENOSYS);
|
|
|
|
}
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, linux_sys_futex, return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
2008-05-13 20:01:27 +00:00
|
|
|
int
|
|
|
|
linux_set_robust_list(struct thread *td, struct linux_set_robust_list_args *args)
|
|
|
|
{
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE2(futex, linux_set_robust_list, entry, td, args);
|
2009-05-01 15:36:02 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (args->len != sizeof(struct linux_robust_list_head)) {
|
|
|
|
LIN_SDT_PROBE0(futex, linux_set_robust_list, size_error);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_set_robust_list, return, EINVAL);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (EINVAL);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
em = em_find(td->td_proc, EMUL_DOLOCK);
|
|
|
|
em->robust_futexes = args->head;
|
|
|
|
EMUL_UNLOCK(&emul_lock);
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, linux_set_robust_list, return, 0);
|
2010-11-15 13:07:10 +00:00
|
|
|
return (0);
|
2008-05-13 20:01:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
linux_get_robust_list(struct thread *td, struct linux_get_robust_list_args *args)
|
|
|
|
{
|
|
|
|
struct linux_emuldata *em;
|
|
|
|
struct linux_robust_list_head *head;
|
|
|
|
l_size_t len = sizeof(struct linux_robust_list_head);
|
|
|
|
int error = 0;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE2(futex, linux_get_robust_list, entry, td, args);
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
if (!args->pid) {
|
|
|
|
em = em_find(td->td_proc, EMUL_DONTLOCK);
|
2010-11-15 13:07:10 +00:00
|
|
|
head = em->robust_futexes;
|
2008-05-13 20:01:27 +00:00
|
|
|
} else {
|
|
|
|
struct proc *p;
|
|
|
|
|
|
|
|
p = pfind(args->pid);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (p == NULL) {
|
|
|
|
LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
|
|
|
|
ESRCH);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (ESRCH);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
em = em_find(p, EMUL_DONTLOCK);
|
|
|
|
/* XXX: ptrace? */
|
2010-11-15 13:07:10 +00:00
|
|
|
if (priv_check(td, PRIV_CRED_SETUID) ||
|
2008-05-13 20:01:27 +00:00
|
|
|
priv_check(td, PRIV_CRED_SETEUID) ||
|
2009-06-14 17:53:55 +00:00
|
|
|
p_candebug(td, p)) {
|
|
|
|
PROC_UNLOCK(p);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, linux_get_robust_list, return,
|
|
|
|
EPERM);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (EPERM);
|
2009-06-14 17:53:55 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
head = em->robust_futexes;
|
2010-11-15 13:07:10 +00:00
|
|
|
|
2008-05-13 20:01:27 +00:00
|
|
|
PROC_UNLOCK(p);
|
|
|
|
}
|
|
|
|
|
|
|
|
error = copyout(&len, args->len, sizeof(l_size_t));
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
|
|
|
|
error);
|
|
|
|
LIN_SDT_PROBE1(futex, linux_get_robust_list, return, EFAULT);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (EFAULT);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
error = copyout(head, args->head, sizeof(struct linux_robust_list_head));
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, linux_get_robust_list, copyout_error,
|
|
|
|
error);
|
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, linux_get_robust_list, return, error);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (error);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2009-05-01 15:36:02 +00:00
|
|
|
handle_futex_death(struct proc *p, uint32_t *uaddr, int pi)
|
2008-05-13 20:01:27 +00:00
|
|
|
{
|
2009-05-01 15:36:02 +00:00
|
|
|
uint32_t uval, nval, mval;
|
2008-05-13 20:01:27 +00:00
|
|
|
struct futex *f;
|
2009-05-01 15:36:02 +00:00
|
|
|
int error;
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE3(futex, handle_futex_death, entry, p, uaddr, pi);
|
|
|
|
|
2008-05-13 20:01:27 +00:00
|
|
|
retry:
|
2012-05-05 19:42:38 +00:00
|
|
|
error = copyin(uaddr, &uval, 4);
|
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, handle_futex_death, copyin_error, error);
|
|
|
|
LIN_SDT_PROBE1(futex, handle_futex_death, return, EFAULT);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (EFAULT);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2009-05-01 15:36:02 +00:00
|
|
|
if ((uval & FUTEX_TID_MASK) == p->p_pid) {
|
2008-05-13 20:01:27 +00:00
|
|
|
mval = (uval & FUTEX_WAITERS) | FUTEX_OWNER_DIED;
|
|
|
|
nval = casuword32(uaddr, uval, mval);
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (nval == -1) {
|
|
|
|
LIN_SDT_PROBE1(futex, handle_futex_death, return,
|
|
|
|
EFAULT);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (EFAULT);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
if (nval != uval)
|
|
|
|
goto retry;
|
|
|
|
|
|
|
|
if (!pi && (uval & FUTEX_WAITERS)) {
|
2009-05-01 15:36:02 +00:00
|
|
|
error = futex_get(uaddr, NULL, &f,
|
2011-02-23 13:23:28 +00:00
|
|
|
FUTEX_DONTCREATE | FUTEX_SHARED);
|
2012-05-05 19:42:38 +00:00
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, handle_futex_death,
|
|
|
|
return, error);
|
2009-05-01 15:36:02 +00:00
|
|
|
return (error);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2009-05-01 15:36:02 +00:00
|
|
|
if (f != NULL) {
|
2011-01-31 05:59:05 +00:00
|
|
|
futex_wake(f, 1, FUTEX_BITSET_MATCH_ANY);
|
2009-05-01 15:36:02 +00:00
|
|
|
futex_put(f, NULL);
|
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, handle_futex_death, return, 0);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
fetch_robust_entry(struct linux_robust_list **entry,
|
|
|
|
struct linux_robust_list **head, int *pi)
|
|
|
|
{
|
|
|
|
l_ulong uentry;
|
2012-05-05 19:42:38 +00:00
|
|
|
int error;
|
|
|
|
|
|
|
|
LIN_SDT_PROBE3(futex, fetch_robust_entry, entry, entry, head, pi);
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
error = copyin((const void *)head, &uentry, sizeof(l_ulong));
|
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, fetch_robust_entry, copyin_error, error);
|
|
|
|
LIN_SDT_PROBE1(futex, fetch_robust_entry, return, EFAULT);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (EFAULT);
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
*entry = (void *)(uentry & ~1UL);
|
|
|
|
*pi = uentry & 1;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
LIN_SDT_PROBE1(futex, fetch_robust_entry, return, 0);
|
2008-05-13 20:01:27 +00:00
|
|
|
return (0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This walks the list of robust futexes releasing them. */
|
|
|
|
void
|
|
|
|
release_futexes(struct proc *p)
|
|
|
|
{
|
|
|
|
struct linux_robust_list_head *head = NULL;
|
|
|
|
struct linux_robust_list *entry, *next_entry, *pending;
|
|
|
|
unsigned int limit = 2048, pi, next_pi, pip;
|
|
|
|
struct linux_emuldata *em;
|
2008-11-16 15:45:41 +00:00
|
|
|
l_long futex_offset;
|
2012-05-05 19:42:38 +00:00
|
|
|
int rc, error;
|
|
|
|
|
|
|
|
LIN_SDT_PROBE1(futex, release_futexes, entry, p);
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
em = em_find(p, EMUL_DONTLOCK);
|
|
|
|
head = em->robust_futexes;
|
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (head == NULL) {
|
|
|
|
LIN_SDT_PROBE0(futex, release_futexes, return);
|
2008-05-13 20:01:27 +00:00
|
|
|
return;
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (fetch_robust_entry(&entry, PTRIN(&head->list.next), &pi)) {
|
|
|
|
LIN_SDT_PROBE0(futex, release_futexes, return);
|
2008-05-13 20:01:27 +00:00
|
|
|
return;
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
error = copyin(&head->futex_offset, &futex_offset,
|
|
|
|
sizeof(futex_offset));
|
|
|
|
if (error) {
|
|
|
|
LIN_SDT_PROBE1(futex, release_futexes, copyin_error, error);
|
|
|
|
LIN_SDT_PROBE0(futex, release_futexes, return);
|
2008-05-13 20:01:27 +00:00
|
|
|
return;
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
2012-05-05 19:42:38 +00:00
|
|
|
if (fetch_robust_entry(&pending, PTRIN(&head->pending_list), &pip)) {
|
|
|
|
LIN_SDT_PROBE0(futex, release_futexes, return);
|
2008-05-13 20:01:27 +00:00
|
|
|
return;
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
while (entry != &head->list) {
|
2008-10-14 07:59:23 +00:00
|
|
|
rc = fetch_robust_entry(&next_entry, PTRIN(&entry->next), &next_pi);
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
if (entry != pending)
|
2012-05-05 19:42:38 +00:00
|
|
|
if (handle_futex_death(p,
|
|
|
|
(uint32_t *)entry + futex_offset, pi)) {
|
|
|
|
LIN_SDT_PROBE0(futex, release_futexes, return);
|
2008-05-13 20:01:27 +00:00
|
|
|
return;
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
|
|
|
if (rc) {
|
|
|
|
LIN_SDT_PROBE0(futex, release_futexes, return);
|
2008-05-13 20:01:27 +00:00
|
|
|
return;
|
2012-05-05 19:42:38 +00:00
|
|
|
}
|
2008-05-13 20:01:27 +00:00
|
|
|
|
|
|
|
entry = next_entry;
|
|
|
|
pi = next_pi;
|
|
|
|
|
|
|
|
if (!--limit)
|
|
|
|
break;
|
|
|
|
|
|
|
|
sched_relinquish(curthread);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (pending)
|
2009-05-01 15:36:02 +00:00
|
|
|
handle_futex_death(p, (uint32_t *)pending + futex_offset, pip);
|
2012-05-05 19:42:38 +00:00
|
|
|
|
|
|
|
LIN_SDT_PROBE0(futex, release_futexes, return);
|
2008-05-13 20:01:27 +00:00
|
|
|
}
|