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