Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.23.4.2
      1  1.23.4.2  rmind /*	$NetBSD: sys_lwp.c,v 1.23.4.2 2007/08/02 01:48:46 rmind Exp $	*/
      2  1.23.4.2  rmind 
      3  1.23.4.2  rmind /*-
      4  1.23.4.2  rmind  * Copyright (c) 2001, 2006, 2007 The NetBSD Foundation, Inc.
      5  1.23.4.2  rmind  * All rights reserved.
      6  1.23.4.2  rmind  *
      7  1.23.4.2  rmind  * This code is derived from software contributed to The NetBSD Foundation
      8  1.23.4.2  rmind  * by Nathan J. Williams, and Andrew Doran.
      9  1.23.4.2  rmind  *
     10  1.23.4.2  rmind  * Redistribution and use in source and binary forms, with or without
     11  1.23.4.2  rmind  * modification, are permitted provided that the following conditions
     12  1.23.4.2  rmind  * are met:
     13  1.23.4.2  rmind  * 1. Redistributions of source code must retain the above copyright
     14  1.23.4.2  rmind  *    notice, this list of conditions and the following disclaimer.
     15  1.23.4.2  rmind  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.23.4.2  rmind  *    notice, this list of conditions and the following disclaimer in the
     17  1.23.4.2  rmind  *    documentation and/or other materials provided with the distribution.
     18  1.23.4.2  rmind  * 3. All advertising materials mentioning features or use of this software
     19  1.23.4.2  rmind  *    must display the following acknowledgement:
     20  1.23.4.2  rmind  *        This product includes software developed by the NetBSD
     21  1.23.4.2  rmind  *        Foundation, Inc. and its contributors.
     22  1.23.4.2  rmind  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.23.4.2  rmind  *    contributors may be used to endorse or promote products derived
     24  1.23.4.2  rmind  *    from this software without specific prior written permission.
     25  1.23.4.2  rmind  *
     26  1.23.4.2  rmind  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.23.4.2  rmind  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.23.4.2  rmind  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.23.4.2  rmind  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.23.4.2  rmind  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.23.4.2  rmind  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.23.4.2  rmind  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.23.4.2  rmind  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.23.4.2  rmind  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.23.4.2  rmind  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.23.4.2  rmind  * POSSIBILITY OF SUCH DAMAGE.
     37  1.23.4.2  rmind  */
     38  1.23.4.2  rmind 
     39  1.23.4.2  rmind /*
     40  1.23.4.2  rmind  * Lightweight process (LWP) system calls.  See kern_lwp.c for a description
     41  1.23.4.2  rmind  * of LWPs.
     42  1.23.4.2  rmind  */
     43  1.23.4.2  rmind 
     44  1.23.4.2  rmind #include <sys/cdefs.h>
     45  1.23.4.2  rmind __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.23.4.2 2007/08/02 01:48:46 rmind Exp $");
     46  1.23.4.2  rmind 
     47  1.23.4.2  rmind #include <sys/param.h>
     48  1.23.4.2  rmind #include <sys/systm.h>
     49  1.23.4.2  rmind #include <sys/pool.h>
     50  1.23.4.2  rmind #include <sys/proc.h>
     51  1.23.4.2  rmind #include <sys/types.h>
     52  1.23.4.2  rmind #include <sys/syscallargs.h>
     53  1.23.4.2  rmind #include <sys/kauth.h>
     54  1.23.4.2  rmind #include <sys/kmem.h>
     55  1.23.4.2  rmind #include <sys/sleepq.h>
     56  1.23.4.2  rmind 
     57  1.23.4.2  rmind #include <uvm/uvm_extern.h>
     58  1.23.4.2  rmind 
     59  1.23.4.2  rmind #define	LWP_UNPARK_MAX		1024
     60  1.23.4.2  rmind 
     61  1.23.4.2  rmind syncobj_t lwp_park_sobj = {
     62  1.23.4.2  rmind 	SOBJ_SLEEPQ_FIFO,
     63  1.23.4.2  rmind 	sleepq_unsleep,
     64  1.23.4.2  rmind 	sleepq_changepri,
     65  1.23.4.2  rmind 	sleepq_lendpri,
     66  1.23.4.2  rmind 	syncobj_noowner,
     67  1.23.4.2  rmind };
     68  1.23.4.2  rmind 
     69  1.23.4.2  rmind sleeptab_t	lwp_park_tab;
     70  1.23.4.2  rmind 
     71  1.23.4.2  rmind void
     72  1.23.4.2  rmind lwp_sys_init(void)
     73  1.23.4.2  rmind {
     74  1.23.4.2  rmind 	sleeptab_init(&lwp_park_tab);
     75  1.23.4.2  rmind }
     76  1.23.4.2  rmind 
     77  1.23.4.2  rmind /* ARGSUSED */
     78  1.23.4.2  rmind int
     79  1.23.4.2  rmind sys__lwp_create(struct lwp *l, void *v, register_t *retval)
     80  1.23.4.2  rmind {
     81  1.23.4.2  rmind 	struct sys__lwp_create_args /* {
     82  1.23.4.2  rmind 		syscallarg(const ucontext_t *) ucp;
     83  1.23.4.2  rmind 		syscallarg(u_long) flags;
     84  1.23.4.2  rmind 		syscallarg(lwpid_t *) new_lwp;
     85  1.23.4.2  rmind 	} */ *uap = v;
     86  1.23.4.2  rmind 	struct proc *p = l->l_proc;
     87  1.23.4.2  rmind 	struct lwp *l2;
     88  1.23.4.2  rmind 	vaddr_t uaddr;
     89  1.23.4.2  rmind 	bool inmem;
     90  1.23.4.2  rmind 	ucontext_t *newuc;
     91  1.23.4.2  rmind 	int error, lid;
     92  1.23.4.2  rmind 
     93  1.23.4.2  rmind 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
     94  1.23.4.2  rmind 
     95  1.23.4.2  rmind 	error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize);
     96  1.23.4.2  rmind 	if (error) {
     97  1.23.4.2  rmind 		pool_put(&lwp_uc_pool, newuc);
     98  1.23.4.2  rmind 		return error;
     99  1.23.4.2  rmind 	}
    100  1.23.4.2  rmind 
    101  1.23.4.2  rmind 	/* XXX check against resource limits */
    102  1.23.4.2  rmind 
    103  1.23.4.2  rmind 	inmem = uvm_uarea_alloc(&uaddr);
    104  1.23.4.2  rmind 	if (__predict_false(uaddr == 0)) {
    105  1.23.4.2  rmind 		pool_put(&lwp_uc_pool, newuc);
    106  1.23.4.2  rmind 		return ENOMEM;
    107  1.23.4.2  rmind 	}
    108  1.23.4.2  rmind 
    109  1.23.4.2  rmind 	error = newlwp(l, p, uaddr, inmem,
    110  1.23.4.2  rmind 	    SCARG(uap, flags) & LWP_DETACHED,
    111  1.23.4.2  rmind 	    NULL, 0, p->p_emul->e_startlwp, newuc, &l2);
    112  1.23.4.2  rmind 	if (error) {
    113  1.23.4.2  rmind 		uvm_uarea_free(uaddr);
    114  1.23.4.2  rmind 		pool_put(&lwp_uc_pool, newuc);
    115  1.23.4.2  rmind 		return error;
    116  1.23.4.2  rmind 	}
    117  1.23.4.2  rmind 
    118  1.23.4.2  rmind 	lid = l2->l_lid;
    119  1.23.4.2  rmind 	error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    120  1.23.4.2  rmind 	if (error) {
    121  1.23.4.2  rmind 		lwp_exit(l2);
    122  1.23.4.2  rmind 		pool_put(&lwp_uc_pool, newuc);
    123  1.23.4.2  rmind 		return error;
    124  1.23.4.2  rmind 	}
    125  1.23.4.2  rmind 
    126  1.23.4.2  rmind 	/*
    127  1.23.4.2  rmind 	 * Set the new LWP running, unless the caller has requested that
    128  1.23.4.2  rmind 	 * it be created in suspended state.  If the process is stopping,
    129  1.23.4.2  rmind 	 * then the LWP is created stopped.
    130  1.23.4.2  rmind 	 */
    131  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    132  1.23.4.2  rmind 	lwp_lock(l2);
    133  1.23.4.2  rmind 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
    134  1.23.4.2  rmind 	    (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    135  1.23.4.2  rmind 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
    136  1.23.4.2  rmind 	    		l2->l_stat = LSSTOP;
    137  1.23.4.2  rmind 		else {
    138  1.23.4.2  rmind 			KASSERT(lwp_locked(l2, l2->l_cpu->ci_schedstate.spc_mutex));
    139  1.23.4.2  rmind 			p->p_nrlwps++;
    140  1.23.4.2  rmind 			l2->l_stat = LSRUN;
    141  1.23.4.2  rmind 			sched_enqueue(l2, false);
    142  1.23.4.2  rmind 		}
    143  1.23.4.2  rmind 	} else
    144  1.23.4.2  rmind 		l2->l_stat = LSSUSPENDED;
    145  1.23.4.2  rmind 	lwp_unlock(l2);
    146  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    147  1.23.4.2  rmind 
    148  1.23.4.2  rmind 	return 0;
    149  1.23.4.2  rmind }
    150  1.23.4.2  rmind 
    151  1.23.4.2  rmind int
    152  1.23.4.2  rmind sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    153  1.23.4.2  rmind {
    154  1.23.4.2  rmind 
    155  1.23.4.2  rmind 	lwp_exit(l);
    156  1.23.4.2  rmind 	return 0;
    157  1.23.4.2  rmind }
    158  1.23.4.2  rmind 
    159  1.23.4.2  rmind int
    160  1.23.4.2  rmind sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    161  1.23.4.2  rmind {
    162  1.23.4.2  rmind 
    163  1.23.4.2  rmind 	*retval = l->l_lid;
    164  1.23.4.2  rmind 	return 0;
    165  1.23.4.2  rmind }
    166  1.23.4.2  rmind 
    167  1.23.4.2  rmind int
    168  1.23.4.2  rmind sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    169  1.23.4.2  rmind {
    170  1.23.4.2  rmind 
    171  1.23.4.2  rmind 	*retval = (uintptr_t)l->l_private;
    172  1.23.4.2  rmind 	return 0;
    173  1.23.4.2  rmind }
    174  1.23.4.2  rmind 
    175  1.23.4.2  rmind int
    176  1.23.4.2  rmind sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    177  1.23.4.2  rmind {
    178  1.23.4.2  rmind 	struct sys__lwp_setprivate_args /* {
    179  1.23.4.2  rmind 		syscallarg(void *) ptr;
    180  1.23.4.2  rmind 	} */ *uap = v;
    181  1.23.4.2  rmind 
    182  1.23.4.2  rmind 	l->l_private = SCARG(uap, ptr);
    183  1.23.4.2  rmind 	return 0;
    184  1.23.4.2  rmind }
    185  1.23.4.2  rmind 
    186  1.23.4.2  rmind int
    187  1.23.4.2  rmind sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    188  1.23.4.2  rmind {
    189  1.23.4.2  rmind 	struct sys__lwp_suspend_args /* {
    190  1.23.4.2  rmind 		syscallarg(lwpid_t) target;
    191  1.23.4.2  rmind 	} */ *uap = v;
    192  1.23.4.2  rmind 	struct proc *p = l->l_proc;
    193  1.23.4.2  rmind 	struct lwp *t;
    194  1.23.4.2  rmind 	int error;
    195  1.23.4.2  rmind 
    196  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    197  1.23.4.2  rmind 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    198  1.23.4.2  rmind 		mutex_exit(&p->p_smutex);
    199  1.23.4.2  rmind 		return ESRCH;
    200  1.23.4.2  rmind 	}
    201  1.23.4.2  rmind 
    202  1.23.4.2  rmind 	/*
    203  1.23.4.2  rmind 	 * Check for deadlock, which is only possible when we're suspending
    204  1.23.4.2  rmind 	 * ourself.  XXX There is a short race here, as p_nrlwps is only
    205  1.23.4.2  rmind 	 * incremented when an LWP suspends itself on the kernel/user
    206  1.23.4.2  rmind 	 * boundary.  It's still possible to kill -9 the process so we
    207  1.23.4.2  rmind 	 * don't bother checking further.
    208  1.23.4.2  rmind 	 */
    209  1.23.4.2  rmind 	lwp_lock(t);
    210  1.23.4.2  rmind 	if ((t == l && p->p_nrlwps == 1) ||
    211  1.23.4.2  rmind 	    (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
    212  1.23.4.2  rmind 		lwp_unlock(t);
    213  1.23.4.2  rmind 		mutex_exit(&p->p_smutex);
    214  1.23.4.2  rmind 		return EDEADLK;
    215  1.23.4.2  rmind 	}
    216  1.23.4.2  rmind 
    217  1.23.4.2  rmind 	/*
    218  1.23.4.2  rmind 	 * Suspend the LWP.  XXX If it's on a different CPU, we should wait
    219  1.23.4.2  rmind 	 * for it to be preempted, where it will put itself to sleep.
    220  1.23.4.2  rmind 	 *
    221  1.23.4.2  rmind 	 * Suspension of the current LWP will happen on return to userspace.
    222  1.23.4.2  rmind 	 */
    223  1.23.4.2  rmind 	error = lwp_suspend(l, t);
    224  1.23.4.2  rmind 	if (error) {
    225  1.23.4.2  rmind 		mutex_exit(&p->p_smutex);
    226  1.23.4.2  rmind 		return error;
    227  1.23.4.2  rmind 	}
    228  1.23.4.2  rmind 
    229  1.23.4.2  rmind 	/*
    230  1.23.4.2  rmind 	 * Wait for:
    231  1.23.4.2  rmind 	 *  o process exiting
    232  1.23.4.2  rmind 	 *  o target LWP suspended
    233  1.23.4.2  rmind 	 *  o target LWP not suspended and L_WSUSPEND clear
    234  1.23.4.2  rmind 	 *  o target LWP exited
    235  1.23.4.2  rmind 	 */
    236  1.23.4.2  rmind 	for (;;) {
    237  1.23.4.2  rmind 		error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex);
    238  1.23.4.2  rmind 		if (error) {
    239  1.23.4.2  rmind 			error = ERESTART;
    240  1.23.4.2  rmind 			break;
    241  1.23.4.2  rmind 		}
    242  1.23.4.2  rmind 		if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
    243  1.23.4.2  rmind 			error = ERESTART;
    244  1.23.4.2  rmind 			break;
    245  1.23.4.2  rmind 		}
    246  1.23.4.2  rmind 		if (t->l_stat == LSSUSPENDED ||
    247  1.23.4.2  rmind 		    (t->l_flag & LW_WSUSPEND) == 0)
    248  1.23.4.2  rmind 			break;
    249  1.23.4.2  rmind 	}
    250  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    251  1.23.4.2  rmind 
    252  1.23.4.2  rmind 	return error;
    253  1.23.4.2  rmind }
    254  1.23.4.2  rmind 
    255  1.23.4.2  rmind int
    256  1.23.4.2  rmind sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    257  1.23.4.2  rmind {
    258  1.23.4.2  rmind 	struct sys__lwp_continue_args /* {
    259  1.23.4.2  rmind 		syscallarg(lwpid_t) target;
    260  1.23.4.2  rmind 	} */ *uap = v;
    261  1.23.4.2  rmind 	int error;
    262  1.23.4.2  rmind 	struct proc *p = l->l_proc;
    263  1.23.4.2  rmind 	struct lwp *t;
    264  1.23.4.2  rmind 
    265  1.23.4.2  rmind 	error = 0;
    266  1.23.4.2  rmind 
    267  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    268  1.23.4.2  rmind 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    269  1.23.4.2  rmind 		mutex_exit(&p->p_smutex);
    270  1.23.4.2  rmind 		return ESRCH;
    271  1.23.4.2  rmind 	}
    272  1.23.4.2  rmind 
    273  1.23.4.2  rmind 	lwp_lock(t);
    274  1.23.4.2  rmind 	lwp_continue(t);
    275  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    276  1.23.4.2  rmind 
    277  1.23.4.2  rmind 	return error;
    278  1.23.4.2  rmind }
    279  1.23.4.2  rmind 
    280  1.23.4.2  rmind int
    281  1.23.4.2  rmind sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    282  1.23.4.2  rmind {
    283  1.23.4.2  rmind 	struct sys__lwp_wakeup_args /* {
    284  1.23.4.2  rmind 		syscallarg(lwpid_t) target;
    285  1.23.4.2  rmind 	} */ *uap = v;
    286  1.23.4.2  rmind 	struct lwp *t;
    287  1.23.4.2  rmind 	struct proc *p;
    288  1.23.4.2  rmind 	int error;
    289  1.23.4.2  rmind 
    290  1.23.4.2  rmind 	p = l->l_proc;
    291  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    292  1.23.4.2  rmind 
    293  1.23.4.2  rmind 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    294  1.23.4.2  rmind 		mutex_exit(&p->p_smutex);
    295  1.23.4.2  rmind 		return ESRCH;
    296  1.23.4.2  rmind 	}
    297  1.23.4.2  rmind 
    298  1.23.4.2  rmind 	lwp_lock(t);
    299  1.23.4.2  rmind 	t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
    300  1.23.4.2  rmind 
    301  1.23.4.2  rmind 	if (t->l_stat != LSSLEEP) {
    302  1.23.4.2  rmind 		lwp_unlock(t);
    303  1.23.4.2  rmind 		error = ENODEV;
    304  1.23.4.2  rmind 	} else if ((t->l_flag & LW_SINTR) == 0) {
    305  1.23.4.2  rmind 		lwp_unlock(t);
    306  1.23.4.2  rmind 		error = EBUSY;
    307  1.23.4.2  rmind 	} else {
    308  1.23.4.2  rmind 		/* Wake it up.  lwp_unsleep() will release the LWP lock. */
    309  1.23.4.2  rmind 		lwp_unsleep(t);
    310  1.23.4.2  rmind 		error = 0;
    311  1.23.4.2  rmind 	}
    312  1.23.4.2  rmind 
    313  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    314  1.23.4.2  rmind 
    315  1.23.4.2  rmind 	return error;
    316  1.23.4.2  rmind }
    317  1.23.4.2  rmind 
    318  1.23.4.2  rmind int
    319  1.23.4.2  rmind sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    320  1.23.4.2  rmind {
    321  1.23.4.2  rmind 	struct sys__lwp_wait_args /* {
    322  1.23.4.2  rmind 		syscallarg(lwpid_t) wait_for;
    323  1.23.4.2  rmind 		syscallarg(lwpid_t *) departed;
    324  1.23.4.2  rmind 	} */ *uap = v;
    325  1.23.4.2  rmind 	struct proc *p = l->l_proc;
    326  1.23.4.2  rmind 	int error;
    327  1.23.4.2  rmind 	lwpid_t dep;
    328  1.23.4.2  rmind 
    329  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    330  1.23.4.2  rmind 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    331  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    332  1.23.4.2  rmind 
    333  1.23.4.2  rmind 	if (error)
    334  1.23.4.2  rmind 		return error;
    335  1.23.4.2  rmind 
    336  1.23.4.2  rmind 	if (SCARG(uap, departed)) {
    337  1.23.4.2  rmind 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    338  1.23.4.2  rmind 		if (error)
    339  1.23.4.2  rmind 			return error;
    340  1.23.4.2  rmind 	}
    341  1.23.4.2  rmind 
    342  1.23.4.2  rmind 	return 0;
    343  1.23.4.2  rmind }
    344  1.23.4.2  rmind 
    345  1.23.4.2  rmind /* ARGSUSED */
    346  1.23.4.2  rmind int
    347  1.23.4.2  rmind sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    348  1.23.4.2  rmind {
    349  1.23.4.2  rmind 	struct sys__lwp_kill_args /* {
    350  1.23.4.2  rmind 		syscallarg(lwpid_t)	target;
    351  1.23.4.2  rmind 		syscallarg(int)		signo;
    352  1.23.4.2  rmind 	} */ *uap = v;
    353  1.23.4.2  rmind 	struct proc *p = l->l_proc;
    354  1.23.4.2  rmind 	struct lwp *t;
    355  1.23.4.2  rmind 	ksiginfo_t ksi;
    356  1.23.4.2  rmind 	int signo = SCARG(uap, signo);
    357  1.23.4.2  rmind 	int error = 0;
    358  1.23.4.2  rmind 
    359  1.23.4.2  rmind 	if ((u_int)signo >= NSIG)
    360  1.23.4.2  rmind 		return EINVAL;
    361  1.23.4.2  rmind 
    362  1.23.4.2  rmind 	KSI_INIT(&ksi);
    363  1.23.4.2  rmind 	ksi.ksi_signo = signo;
    364  1.23.4.2  rmind 	ksi.ksi_code = SI_USER;
    365  1.23.4.2  rmind 	ksi.ksi_pid = p->p_pid;
    366  1.23.4.2  rmind 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    367  1.23.4.2  rmind 	ksi.ksi_lid = SCARG(uap, target);
    368  1.23.4.2  rmind 
    369  1.23.4.2  rmind 	mutex_enter(&proclist_mutex);
    370  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    371  1.23.4.2  rmind 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    372  1.23.4.2  rmind 		error = ESRCH;
    373  1.23.4.2  rmind 	else if (signo != 0)
    374  1.23.4.2  rmind 		kpsignal2(p, &ksi);
    375  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    376  1.23.4.2  rmind 	mutex_exit(&proclist_mutex);
    377  1.23.4.2  rmind 
    378  1.23.4.2  rmind 	return error;
    379  1.23.4.2  rmind }
    380  1.23.4.2  rmind 
    381  1.23.4.2  rmind int
    382  1.23.4.2  rmind sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
    383  1.23.4.2  rmind {
    384  1.23.4.2  rmind 	struct sys__lwp_detach_args /* {
    385  1.23.4.2  rmind 		syscallarg(lwpid_t)	target;
    386  1.23.4.2  rmind 	} */ *uap = v;
    387  1.23.4.2  rmind 	struct proc *p;
    388  1.23.4.2  rmind 	struct lwp *t;
    389  1.23.4.2  rmind 	lwpid_t target;
    390  1.23.4.2  rmind 	int error;
    391  1.23.4.2  rmind 
    392  1.23.4.2  rmind 	target = SCARG(uap, target);
    393  1.23.4.2  rmind 	p = l->l_proc;
    394  1.23.4.2  rmind 
    395  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    396  1.23.4.2  rmind 
    397  1.23.4.2  rmind 	if (l->l_lid == target)
    398  1.23.4.2  rmind 		t = l;
    399  1.23.4.2  rmind 	else {
    400  1.23.4.2  rmind 		/*
    401  1.23.4.2  rmind 		 * We can't use lwp_find() here because the target might
    402  1.23.4.2  rmind 		 * be a zombie.
    403  1.23.4.2  rmind 		 */
    404  1.23.4.2  rmind 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    405  1.23.4.2  rmind 			if (t->l_lid == target)
    406  1.23.4.2  rmind 				break;
    407  1.23.4.2  rmind 	}
    408  1.23.4.2  rmind 
    409  1.23.4.2  rmind 	/*
    410  1.23.4.2  rmind 	 * If the LWP is already detached, there's nothing to do.
    411  1.23.4.2  rmind 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    412  1.23.4.2  rmind 	 * is visible with the proc mutex held.
    413  1.23.4.2  rmind 	 *
    414  1.23.4.2  rmind 	 * After we have detached or released the LWP, kick any
    415  1.23.4.2  rmind 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    416  1.23.4.2  rmind 	 * for the target LWP to exit.
    417  1.23.4.2  rmind 	 */
    418  1.23.4.2  rmind 	if (t != NULL && t->l_stat != LSIDL) {
    419  1.23.4.2  rmind 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    420  1.23.4.2  rmind 			p->p_ndlwps++;
    421  1.23.4.2  rmind 			t->l_prflag |= LPR_DETACHED;
    422  1.23.4.2  rmind 			if (t->l_stat == LSZOMB) {
    423  1.23.4.2  rmind 				/* Releases proc mutex. */
    424  1.23.4.2  rmind 				lwp_free(t, false, false);
    425  1.23.4.2  rmind 				return 0;
    426  1.23.4.2  rmind 			}
    427  1.23.4.2  rmind 			error = 0;
    428  1.23.4.2  rmind 
    429  1.23.4.2  rmind 			/*
    430  1.23.4.2  rmind 			 * Have any LWPs sleeping in lwp_wait() recheck
    431  1.23.4.2  rmind 			 * for deadlock.
    432  1.23.4.2  rmind 			 */
    433  1.23.4.2  rmind 			cv_broadcast(&p->p_lwpcv);
    434  1.23.4.2  rmind 		} else
    435  1.23.4.2  rmind 			error = EINVAL;
    436  1.23.4.2  rmind 	} else
    437  1.23.4.2  rmind 		error = ESRCH;
    438  1.23.4.2  rmind 
    439  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    440  1.23.4.2  rmind 
    441  1.23.4.2  rmind 	return error;
    442  1.23.4.2  rmind }
    443  1.23.4.2  rmind 
    444  1.23.4.2  rmind static inline wchan_t
    445  1.23.4.2  rmind lwp_park_wchan(struct proc *p, const void *hint)
    446  1.23.4.2  rmind {
    447  1.23.4.2  rmind 
    448  1.23.4.2  rmind 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    449  1.23.4.2  rmind }
    450  1.23.4.2  rmind 
    451  1.23.4.2  rmind /*
    452  1.23.4.2  rmind  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    453  1.23.4.2  rmind  * will remain parked until another LWP in the same process calls in and
    454  1.23.4.2  rmind  * requests that it be unparked.
    455  1.23.4.2  rmind  */
    456  1.23.4.2  rmind int
    457  1.23.4.2  rmind sys__lwp_park(struct lwp *l, void *v, register_t *retval)
    458  1.23.4.2  rmind {
    459  1.23.4.2  rmind 	struct sys__lwp_park_args /* {
    460  1.23.4.2  rmind 		syscallarg(const struct timespec *)	ts;
    461  1.23.4.2  rmind 		syscallarg(ucontext_t *)		ucp;
    462  1.23.4.2  rmind 		syscallarg(const void *)		hint;
    463  1.23.4.2  rmind 	} */ *uap = v;
    464  1.23.4.2  rmind 	struct timespec ts;
    465  1.23.4.2  rmind 	int error;
    466  1.23.4.2  rmind 
    467  1.23.4.2  rmind 	if (SCARG(uap, ts) == NULL)
    468  1.23.4.2  rmind 		return do_sys_lwp_park(l, NULL, SCARG(uap, ucp),
    469  1.23.4.2  rmind 		    SCARG(uap, hint));
    470  1.23.4.2  rmind 
    471  1.23.4.2  rmind 	if ((error = copyin(SCARG(uap, ts), &ts, sizeof(ts))) != 0)
    472  1.23.4.2  rmind 		return error;
    473  1.23.4.2  rmind 
    474  1.23.4.2  rmind 	return do_sys_lwp_park(l, &ts, SCARG(uap, ucp), SCARG(uap, hint));
    475  1.23.4.2  rmind }
    476  1.23.4.2  rmind 
    477  1.23.4.2  rmind int
    478  1.23.4.2  rmind do_sys_lwp_park(struct lwp *l, struct timespec *ts, ucontext_t *uc,
    479  1.23.4.2  rmind 		const void *hint)
    480  1.23.4.2  rmind {
    481  1.23.4.2  rmind 	struct timespec tsx;
    482  1.23.4.2  rmind 	struct timeval tv;
    483  1.23.4.2  rmind 	sleepq_t *sq;
    484  1.23.4.2  rmind 	wchan_t wchan;
    485  1.23.4.2  rmind 	int timo, error;
    486  1.23.4.2  rmind 
    487  1.23.4.2  rmind 	/* Fix up the given timeout value. */
    488  1.23.4.2  rmind 	if (ts != NULL) {
    489  1.23.4.2  rmind 		getnanotime(&tsx);
    490  1.23.4.2  rmind 		timespecsub(ts, &tsx, ts);
    491  1.23.4.2  rmind 		tv.tv_sec = ts->tv_sec;
    492  1.23.4.2  rmind 		tv.tv_usec = ts->tv_nsec / 1000;
    493  1.23.4.2  rmind 		if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
    494  1.23.4.2  rmind 			return ETIMEDOUT;
    495  1.23.4.2  rmind 		if ((error = itimerfix(&tv)) != 0)
    496  1.23.4.2  rmind 			return error;
    497  1.23.4.2  rmind 		timo = tvtohz(&tv);
    498  1.23.4.2  rmind 	} else
    499  1.23.4.2  rmind 		timo = 0;
    500  1.23.4.2  rmind 
    501  1.23.4.2  rmind 	/* Find and lock the sleep queue. */
    502  1.23.4.2  rmind 	wchan = lwp_park_wchan(l->l_proc, hint);
    503  1.23.4.2  rmind 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    504  1.23.4.2  rmind 
    505  1.23.4.2  rmind 	/*
    506  1.23.4.2  rmind 	 * Before going the full route and blocking, check to see if an
    507  1.23.4.2  rmind 	 * unpark op is pending.
    508  1.23.4.2  rmind 	 */
    509  1.23.4.2  rmind 	lwp_lock(l);
    510  1.23.4.2  rmind 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    511  1.23.4.2  rmind 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    512  1.23.4.2  rmind 		lwp_unlock(l);
    513  1.23.4.2  rmind 		sleepq_unlock(sq);
    514  1.23.4.2  rmind 		return EALREADY;
    515  1.23.4.2  rmind 	}
    516  1.23.4.2  rmind 	lwp_unlock_to(l, sq->sq_mutex);
    517  1.23.4.2  rmind 
    518  1.23.4.2  rmind 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks); /* XXX for compat32 */
    519  1.23.4.2  rmind 	sleepq_enqueue(sq, sched_kpri(l), wchan, "parked", &lwp_park_sobj);
    520  1.23.4.2  rmind 	error = sleepq_block(timo, true);
    521  1.23.4.2  rmind 	switch (error) {
    522  1.23.4.2  rmind 	case EWOULDBLOCK:
    523  1.23.4.2  rmind 		error = ETIMEDOUT;
    524  1.23.4.2  rmind 		break;
    525  1.23.4.2  rmind 	case ERESTART:
    526  1.23.4.2  rmind 		error = EINTR;
    527  1.23.4.2  rmind 		break;
    528  1.23.4.2  rmind 	default:
    529  1.23.4.2  rmind 		/* nothing */
    530  1.23.4.2  rmind 		break;
    531  1.23.4.2  rmind 	}
    532  1.23.4.2  rmind 	return error;
    533  1.23.4.2  rmind }
    534  1.23.4.2  rmind 
    535  1.23.4.2  rmind int
    536  1.23.4.2  rmind sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
    537  1.23.4.2  rmind {
    538  1.23.4.2  rmind 	struct sys__lwp_unpark_args /* {
    539  1.23.4.2  rmind 		syscallarg(lwpid_t)		target;
    540  1.23.4.2  rmind 		syscallarg(const void *)	hint;
    541  1.23.4.2  rmind 	} */ *uap = v;
    542  1.23.4.2  rmind 	struct proc *p;
    543  1.23.4.2  rmind 	struct lwp *t;
    544  1.23.4.2  rmind 	sleepq_t *sq;
    545  1.23.4.2  rmind 	lwpid_t target;
    546  1.23.4.2  rmind 	wchan_t wchan;
    547  1.23.4.2  rmind 	int swapin;
    548  1.23.4.2  rmind 
    549  1.23.4.2  rmind 	p = l->l_proc;
    550  1.23.4.2  rmind 	target = SCARG(uap, target);
    551  1.23.4.2  rmind 
    552  1.23.4.2  rmind 	/*
    553  1.23.4.2  rmind 	 * Easy case: search for the LWP on the sleep queue.  If
    554  1.23.4.2  rmind 	 * it's parked, remove it from the queue and set running.
    555  1.23.4.2  rmind 	 */
    556  1.23.4.2  rmind 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    557  1.23.4.2  rmind 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    558  1.23.4.2  rmind 
    559  1.23.4.2  rmind 	TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    560  1.23.4.2  rmind 		if (t->l_proc == p && t->l_lid == target)
    561  1.23.4.2  rmind 			break;
    562  1.23.4.2  rmind 
    563  1.23.4.2  rmind 	if (__predict_true(t != NULL)) {
    564  1.23.4.2  rmind 		swapin = sleepq_remove(sq, t);
    565  1.23.4.2  rmind 		sleepq_unlock(sq);
    566  1.23.4.2  rmind 		if (swapin)
    567  1.23.4.2  rmind 			uvm_kick_scheduler();
    568  1.23.4.2  rmind 		return 0;
    569  1.23.4.2  rmind 	}
    570  1.23.4.2  rmind 
    571  1.23.4.2  rmind 	/*
    572  1.23.4.2  rmind 	 * The LWP hasn't parked yet.  Take the hit and mark the
    573  1.23.4.2  rmind 	 * operation as pending.
    574  1.23.4.2  rmind 	 */
    575  1.23.4.2  rmind 	sleepq_unlock(sq);
    576  1.23.4.2  rmind 	mutex_enter(&p->p_smutex);
    577  1.23.4.2  rmind 	if ((t = lwp_find(p, target)) == NULL) {
    578  1.23.4.2  rmind 		mutex_exit(&p->p_smutex);
    579  1.23.4.2  rmind 		return ESRCH;
    580  1.23.4.2  rmind 	}
    581  1.23.4.2  rmind 	lwp_lock(t);
    582  1.23.4.2  rmind 
    583  1.23.4.2  rmind 	/*
    584  1.23.4.2  rmind 	 * It may not have parked yet, we may have raced, or it
    585  1.23.4.2  rmind 	 * is parked on a different user sync object.
    586  1.23.4.2  rmind 	 */
    587  1.23.4.2  rmind 	if (t->l_syncobj == &lwp_park_sobj) {
    588  1.23.4.2  rmind 		/* Releases the LWP lock. */
    589  1.23.4.2  rmind 		lwp_unsleep(t);
    590  1.23.4.2  rmind 	} else {
    591  1.23.4.2  rmind 		/*
    592  1.23.4.2  rmind 		 * Set the operation pending.  The next call to _lwp_park
    593  1.23.4.2  rmind 		 * will return early.
    594  1.23.4.2  rmind 		 */
    595  1.23.4.2  rmind 		t->l_flag |= LW_UNPARKED;
    596  1.23.4.2  rmind 		lwp_unlock(t);
    597  1.23.4.2  rmind 	}
    598  1.23.4.2  rmind 
    599  1.23.4.2  rmind 	mutex_exit(&p->p_smutex);
    600  1.23.4.2  rmind 	return 0;
    601  1.23.4.2  rmind }
    602  1.23.4.2  rmind 
    603  1.23.4.2  rmind int
    604  1.23.4.2  rmind sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
    605  1.23.4.2  rmind {
    606  1.23.4.2  rmind 	struct sys__lwp_unpark_all_args /* {
    607  1.23.4.2  rmind 		syscallarg(const lwpid_t *)	targets;
    608  1.23.4.2  rmind 		syscallarg(size_t)		ntargets;
    609  1.23.4.2  rmind 		syscallarg(const void *)	hint;
    610  1.23.4.2  rmind 	} */ *uap = v;
    611  1.23.4.2  rmind 	struct proc *p;
    612  1.23.4.2  rmind 	struct lwp *t;
    613  1.23.4.2  rmind 	sleepq_t *sq;
    614  1.23.4.2  rmind 	wchan_t wchan;
    615  1.23.4.2  rmind 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    616  1.23.4.2  rmind 	int swapin, error;
    617  1.23.4.2  rmind 	u_int ntargets;
    618  1.23.4.2  rmind 	size_t sz;
    619  1.23.4.2  rmind 
    620  1.23.4.2  rmind 	p = l->l_proc;
    621  1.23.4.2  rmind 	ntargets = SCARG(uap, ntargets);
    622  1.23.4.2  rmind 
    623  1.23.4.2  rmind 	if (SCARG(uap, targets) == NULL) {
    624  1.23.4.2  rmind 		/*
    625  1.23.4.2  rmind 		 * Let the caller know how much we are willing to do, and
    626  1.23.4.2  rmind 		 * let it unpark the LWPs in blocks.
    627  1.23.4.2  rmind 		 */
    628  1.23.4.2  rmind 		*retval = LWP_UNPARK_MAX;
    629  1.23.4.2  rmind 		return 0;
    630  1.23.4.2  rmind 	}
    631  1.23.4.2  rmind 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    632  1.23.4.2  rmind 		return EINVAL;
    633  1.23.4.2  rmind 
    634  1.23.4.2  rmind 	/*
    635  1.23.4.2  rmind 	 * Copy in the target array.  If it's a small number of LWPs, then
    636  1.23.4.2  rmind 	 * place the numbers on the stack.
    637  1.23.4.2  rmind 	 */
    638  1.23.4.2  rmind 	sz = sizeof(target) * ntargets;
    639  1.23.4.2  rmind 	if (sz <= sizeof(targets))
    640  1.23.4.2  rmind 		tp = targets;
    641  1.23.4.2  rmind 	else {
    642  1.23.4.2  rmind 		KERNEL_LOCK(1, l);		/* XXXSMP */
    643  1.23.4.2  rmind 		tp = kmem_alloc(sz, KM_SLEEP);
    644  1.23.4.2  rmind 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    645  1.23.4.2  rmind 		if (tp == NULL)
    646  1.23.4.2  rmind 			return ENOMEM;
    647  1.23.4.2  rmind 	}
    648  1.23.4.2  rmind 	error = copyin(SCARG(uap, targets), tp, sz);
    649  1.23.4.2  rmind 	if (error != 0) {
    650  1.23.4.2  rmind 		if (tp != targets) {
    651  1.23.4.2  rmind 			KERNEL_LOCK(1, l);	/* XXXSMP */
    652  1.23.4.2  rmind 			kmem_free(tp, sz);
    653  1.23.4.2  rmind 			KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    654  1.23.4.2  rmind 		}
    655  1.23.4.2  rmind 		return error;
    656  1.23.4.2  rmind 	}
    657  1.23.4.2  rmind 
    658  1.23.4.2  rmind 	swapin = 0;
    659  1.23.4.2  rmind 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    660  1.23.4.2  rmind 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    661  1.23.4.2  rmind 
    662  1.23.4.2  rmind 	for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
    663  1.23.4.2  rmind 		target = *tpp;
    664  1.23.4.2  rmind 
    665  1.23.4.2  rmind 		/*
    666  1.23.4.2  rmind 		 * Easy case: search for the LWP on the sleep queue.  If
    667  1.23.4.2  rmind 		 * it's parked, remove it from the queue and set running.
    668  1.23.4.2  rmind 		 */
    669  1.23.4.2  rmind 		TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    670  1.23.4.2  rmind 			if (t->l_proc == p && t->l_lid == target)
    671  1.23.4.2  rmind 				break;
    672  1.23.4.2  rmind 
    673  1.23.4.2  rmind 		if (t != NULL) {
    674  1.23.4.2  rmind 			swapin |= sleepq_remove(sq, t);
    675  1.23.4.2  rmind 			continue;
    676  1.23.4.2  rmind 		}
    677  1.23.4.2  rmind 
    678  1.23.4.2  rmind 		/*
    679  1.23.4.2  rmind 		 * The LWP hasn't parked yet.  Take the hit and
    680  1.23.4.2  rmind 		 * mark the operation as pending.
    681  1.23.4.2  rmind 		 */
    682  1.23.4.2  rmind 		sleepq_unlock(sq);
    683  1.23.4.2  rmind 		mutex_enter(&p->p_smutex);
    684  1.23.4.2  rmind 		if ((t = lwp_find(p, target)) == NULL) {
    685  1.23.4.2  rmind 			mutex_exit(&p->p_smutex);
    686  1.23.4.2  rmind 			sleepq_lock(sq);
    687  1.23.4.2  rmind 			continue;
    688  1.23.4.2  rmind 		}
    689  1.23.4.2  rmind 		lwp_lock(t);
    690  1.23.4.2  rmind 
    691  1.23.4.2  rmind 		/*
    692  1.23.4.2  rmind 		 * It may not have parked yet, we may have raced, or
    693  1.23.4.2  rmind 		 * it is parked on a different user sync object.
    694  1.23.4.2  rmind 		 */
    695  1.23.4.2  rmind 		if (t->l_syncobj == &lwp_park_sobj) {
    696  1.23.4.2  rmind 			/* Releases the LWP lock. */
    697  1.23.4.2  rmind 			lwp_unsleep(t);
    698  1.23.4.2  rmind 		} else {
    699  1.23.4.2  rmind 			/*
    700  1.23.4.2  rmind 			 * Set the operation pending.  The next call to
    701  1.23.4.2  rmind 			 * _lwp_park will return early.
    702  1.23.4.2  rmind 			 */
    703  1.23.4.2  rmind 			t->l_flag |= LW_UNPARKED;
    704  1.23.4.2  rmind 			lwp_unlock(t);
    705  1.23.4.2  rmind 		}
    706  1.23.4.2  rmind 
    707  1.23.4.2  rmind 		mutex_exit(&p->p_smutex);
    708  1.23.4.2  rmind 		sleepq_lock(sq);
    709  1.23.4.2  rmind 	}
    710  1.23.4.2  rmind 
    711  1.23.4.2  rmind 	sleepq_unlock(sq);
    712  1.23.4.2  rmind 	if (tp != targets) {
    713  1.23.4.2  rmind 		KERNEL_LOCK(1, l);		/* XXXSMP */
    714  1.23.4.2  rmind 		kmem_free(tp, sz);
    715  1.23.4.2  rmind 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    716  1.23.4.2  rmind 	}
    717  1.23.4.2  rmind 	if (swapin)
    718  1.23.4.2  rmind 		uvm_kick_scheduler();
    719  1.23.4.2  rmind 
    720  1.23.4.2  rmind 	return 0;
    721  1.23.4.2  rmind }
    722