Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.12.2.1
      1  1.12.2.1       ad /*	$NetBSD: sys_lwp.c,v 1.12.2.1 2007/04/10 11:41:11 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.1       ad __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.12.2.1 2007/04/10 11:41:11 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.2       ad 	SOBJ_SLEEPQ_SORTED,
     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 #ifdef LWP_COUNTERS
     72       1.2       ad struct evcnt	lwp_ev_park_early = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
     73       1.2       ad     NULL, "_lwp_park", "unparked early");
     74       1.2       ad struct evcnt	lwp_ev_park_raced = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
     75       1.2       ad     NULL, "_lwp_park", "raced");
     76       1.8       ad struct evcnt	lwp_ev_park_slowpath = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
     77       1.8       ad     NULL, "_lwp_park", "slowpath");
     78       1.2       ad struct evcnt	lwp_ev_park_miss = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
     79       1.2       ad     NULL, "_lwp_park", "not parked");
     80       1.2       ad struct evcnt	lwp_ev_park_bcast = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
     81       1.2       ad     NULL, "_lwp_park", "broadcast unpark");
     82       1.2       ad struct evcnt	lwp_ev_park_targ = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
     83       1.2       ad     NULL, "_lwp_park", "targeted unpark");
     84       1.2       ad struct evcnt	lwp_ev_park = EVCNT_INITIALIZER(EVCNT_TYPE_MISC,
     85       1.2       ad     NULL, "_lwp_park", "parked");
     86       1.2       ad 
     87       1.2       ad #define	LWP_COUNT(ev, val)	(ev).ev_count += (val)	/* XXXSMP */
     88       1.2       ad #else
     89       1.2       ad #define	LWP_COUNT(ev, val)	/* nothing */
     90       1.2       ad #endif
     91       1.2       ad 
     92       1.2       ad void
     93       1.2       ad lwp_sys_init(void)
     94       1.2       ad {
     95       1.2       ad 	sleeptab_init(&lwp_park_tab);
     96       1.2       ad #ifdef LWP_COUNTERS
     97       1.2       ad 	evcnt_attach_static(&lwp_ev_park_early);
     98       1.8       ad 	evcnt_attach_static(&lwp_ev_park_slowpath);
     99       1.2       ad 	evcnt_attach_static(&lwp_ev_park_raced);
    100       1.2       ad 	evcnt_attach_static(&lwp_ev_park_miss);
    101       1.2       ad 	evcnt_attach_static(&lwp_ev_park_bcast);
    102       1.2       ad 	evcnt_attach_static(&lwp_ev_park_targ);
    103       1.2       ad 	evcnt_attach_static(&lwp_ev_park);
    104       1.2       ad #endif
    105       1.2       ad }
    106       1.2       ad 
    107       1.2       ad /* ARGSUSED */
    108       1.2       ad int
    109       1.2       ad sys__lwp_create(struct lwp *l, void *v, register_t *retval)
    110       1.2       ad {
    111       1.2       ad 	struct sys__lwp_create_args /* {
    112       1.2       ad 		syscallarg(const ucontext_t *) ucp;
    113       1.2       ad 		syscallarg(u_long) flags;
    114       1.2       ad 		syscallarg(lwpid_t *) new_lwp;
    115       1.2       ad 	} */ *uap = v;
    116       1.2       ad 	struct proc *p = l->l_proc;
    117       1.2       ad 	struct lwp *l2;
    118       1.2       ad 	vaddr_t uaddr;
    119       1.6  thorpej 	bool inmem;
    120       1.2       ad 	ucontext_t *newuc;
    121       1.2       ad 	int error, lid;
    122       1.2       ad 
    123       1.2       ad 	newuc = pool_get(&lwp_uc_pool, PR_WAITOK);
    124       1.2       ad 
    125       1.2       ad 	error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize);
    126       1.2       ad 	if (error) {
    127       1.2       ad 		pool_put(&lwp_uc_pool, newuc);
    128       1.2       ad 		return error;
    129       1.2       ad 	}
    130       1.2       ad 
    131       1.2       ad 	/* XXX check against resource limits */
    132       1.2       ad 
    133       1.2       ad 	inmem = uvm_uarea_alloc(&uaddr);
    134       1.2       ad 	if (__predict_false(uaddr == 0)) {
    135       1.2       ad 		pool_put(&lwp_uc_pool, newuc);
    136       1.2       ad 		return ENOMEM;
    137       1.2       ad 	}
    138       1.2       ad 
    139       1.2       ad 	newlwp(l, p, uaddr, inmem,
    140       1.2       ad 	    SCARG(uap, flags) & LWP_DETACHED,
    141       1.5     cube 	    NULL, 0, p->p_emul->e_startlwp, newuc, &l2);
    142       1.2       ad 
    143       1.2       ad 	/*
    144       1.2       ad 	 * Set the new LWP running, unless the caller has requested that
    145       1.2       ad 	 * it be created in suspended state.  If the process is stopping,
    146       1.2       ad 	 * then the LWP is created stopped.
    147       1.2       ad 	 */
    148       1.2       ad 	mutex_enter(&p->p_smutex);
    149       1.2       ad 	lwp_lock(l2);
    150       1.2       ad 	lid = l2->l_lid;
    151       1.2       ad 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
    152       1.4    pavel 	    (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    153       1.2       ad 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
    154       1.2       ad 	    		l2->l_stat = LSSTOP;
    155       1.2       ad 		else {
    156       1.8       ad 			KASSERT(lwp_locked(l2, &sched_mutex));
    157       1.2       ad 			p->p_nrlwps++;
    158       1.2       ad 			l2->l_stat = LSRUN;
    159       1.2       ad 			setrunqueue(l2);
    160       1.2       ad 		}
    161       1.2       ad 	} else
    162       1.2       ad 		l2->l_stat = LSSUSPENDED;
    163       1.2       ad 	lwp_unlock(l2);
    164       1.2       ad 	mutex_exit(&p->p_smutex);
    165       1.2       ad 
    166       1.2       ad 	error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    167       1.2       ad 	if (error)
    168       1.2       ad 		return error;
    169       1.2       ad 
    170       1.2       ad 	return 0;
    171       1.2       ad }
    172       1.2       ad 
    173       1.2       ad int
    174       1.2       ad sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    175       1.2       ad {
    176       1.2       ad 
    177       1.2       ad 	lwp_exit(l);
    178       1.2       ad 	return 0;
    179       1.2       ad }
    180       1.2       ad 
    181       1.2       ad int
    182       1.2       ad sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    183       1.2       ad {
    184       1.2       ad 
    185       1.2       ad 	*retval = l->l_lid;
    186       1.2       ad 	return 0;
    187       1.2       ad }
    188       1.2       ad 
    189       1.2       ad int
    190       1.2       ad sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    191       1.2       ad {
    192       1.2       ad 
    193       1.2       ad 	*retval = (uintptr_t)l->l_private;
    194       1.2       ad 	return 0;
    195       1.2       ad }
    196       1.2       ad 
    197       1.2       ad int
    198       1.2       ad sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    199       1.2       ad {
    200       1.2       ad 	struct sys__lwp_setprivate_args /* {
    201       1.2       ad 		syscallarg(void *) ptr;
    202       1.2       ad 	} */ *uap = v;
    203       1.2       ad 
    204       1.2       ad 	l->l_private = SCARG(uap, ptr);
    205       1.2       ad 	return 0;
    206       1.2       ad }
    207       1.2       ad 
    208       1.2       ad int
    209       1.2       ad sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    210       1.2       ad {
    211       1.2       ad 	struct sys__lwp_suspend_args /* {
    212       1.2       ad 		syscallarg(lwpid_t) target;
    213       1.2       ad 	} */ *uap = v;
    214       1.2       ad 	struct proc *p = l->l_proc;
    215       1.2       ad 	struct lwp *t;
    216       1.2       ad 	int error;
    217       1.2       ad 
    218       1.2       ad 	mutex_enter(&p->p_smutex);
    219       1.2       ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    220       1.2       ad 		mutex_exit(&p->p_smutex);
    221       1.2       ad 		return ESRCH;
    222       1.2       ad 	}
    223       1.2       ad 
    224       1.2       ad 	/*
    225       1.2       ad 	 * Check for deadlock, which is only possible when we're suspending
    226       1.2       ad 	 * ourself.  XXX There is a short race here, as p_nrlwps is only
    227       1.2       ad 	 * incremented when an LWP suspends itself on the kernel/user
    228       1.2       ad 	 * boundary.  It's still possible to kill -9 the process so we
    229       1.2       ad 	 * don't bother checking further.
    230       1.2       ad 	 */
    231       1.2       ad 	lwp_lock(t);
    232       1.2       ad 	if ((t == l && p->p_nrlwps == 1) ||
    233       1.4    pavel 	    (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
    234       1.2       ad 		lwp_unlock(t);
    235       1.2       ad 		mutex_exit(&p->p_smutex);
    236       1.2       ad 		return EDEADLK;
    237       1.2       ad 	}
    238       1.2       ad 
    239       1.2       ad 	/*
    240       1.2       ad 	 * Suspend the LWP.  XXX If it's on a different CPU, we should wait
    241       1.2       ad 	 * for it to be preempted, where it will put itself to sleep.
    242       1.2       ad 	 *
    243       1.2       ad 	 * Suspension of the current LWP will happen on return to userspace.
    244       1.2       ad 	 */
    245       1.2       ad 	error = lwp_suspend(l, t);
    246       1.2       ad 	mutex_exit(&p->p_smutex);
    247       1.2       ad 
    248       1.2       ad 	return error;
    249       1.2       ad }
    250       1.2       ad 
    251       1.2       ad int
    252       1.2       ad sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    253       1.2       ad {
    254       1.2       ad 	struct sys__lwp_continue_args /* {
    255       1.2       ad 		syscallarg(lwpid_t) target;
    256       1.2       ad 	} */ *uap = v;
    257       1.2       ad 	int error;
    258       1.2       ad 	struct proc *p = l->l_proc;
    259       1.2       ad 	struct lwp *t;
    260       1.2       ad 
    261       1.2       ad 	error = 0;
    262       1.2       ad 
    263       1.2       ad 	mutex_enter(&p->p_smutex);
    264       1.2       ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    265       1.2       ad 		mutex_exit(&p->p_smutex);
    266       1.2       ad 		return ESRCH;
    267       1.2       ad 	}
    268       1.2       ad 
    269       1.2       ad 	lwp_lock(t);
    270       1.2       ad 	lwp_continue(t);
    271       1.2       ad 	mutex_exit(&p->p_smutex);
    272       1.2       ad 
    273       1.2       ad 	return error;
    274       1.2       ad }
    275       1.2       ad 
    276       1.2       ad int
    277       1.2       ad sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    278       1.2       ad {
    279       1.2       ad 	struct sys__lwp_wakeup_args /* {
    280       1.2       ad 		syscallarg(lwpid_t) target;
    281       1.2       ad 	} */ *uap = v;
    282       1.2       ad 	struct lwp *t;
    283       1.2       ad 	struct proc *p;
    284       1.2       ad 	int error;
    285       1.2       ad 
    286       1.2       ad 	p = l->l_proc;
    287       1.2       ad 	mutex_enter(&p->p_smutex);
    288       1.2       ad 
    289       1.2       ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    290       1.2       ad 		mutex_exit(&p->p_smutex);
    291       1.2       ad 		return ESRCH;
    292       1.2       ad 	}
    293       1.2       ad 
    294       1.2       ad 	lwp_lock(t);
    295      1.11       ad 	t->l_flag |= LW_CANCELLED;
    296       1.2       ad 
    297       1.2       ad 	if (t->l_stat != LSSLEEP) {
    298       1.2       ad 		error = ENODEV;
    299       1.2       ad 		goto bad;
    300       1.2       ad 	}
    301       1.2       ad 
    302       1.4    pavel 	if ((t->l_flag & LW_SINTR) == 0) {
    303       1.2       ad 		error = EBUSY;
    304       1.2       ad 		goto bad;
    305       1.2       ad 	}
    306       1.2       ad 
    307      1.11       ad 	/* Wake it up.  setrunnable() will release the LWP lock. */
    308       1.2       ad 	setrunnable(t);
    309       1.2       ad 	mutex_exit(&p->p_smutex);
    310       1.2       ad 	return 0;
    311       1.2       ad 
    312       1.2       ad  bad:
    313       1.2       ad  	lwp_unlock(t);
    314       1.2       ad 	mutex_exit(&p->p_smutex);
    315       1.2       ad 	return error;
    316       1.2       ad }
    317       1.2       ad 
    318       1.2       ad int
    319       1.2       ad sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    320       1.2       ad {
    321       1.2       ad 	struct sys__lwp_wait_args /* {
    322       1.2       ad 		syscallarg(lwpid_t) wait_for;
    323       1.2       ad 		syscallarg(lwpid_t *) departed;
    324       1.2       ad 	} */ *uap = v;
    325       1.2       ad 	struct proc *p = l->l_proc;
    326       1.2       ad 	int error;
    327       1.2       ad 	lwpid_t dep;
    328       1.2       ad 
    329       1.2       ad 	mutex_enter(&p->p_smutex);
    330       1.2       ad 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    331       1.2       ad 	mutex_exit(&p->p_smutex);
    332       1.2       ad 
    333       1.2       ad 	if (error)
    334       1.2       ad 		return error;
    335       1.2       ad 
    336       1.2       ad 	if (SCARG(uap, departed)) {
    337       1.2       ad 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    338       1.2       ad 		if (error)
    339       1.2       ad 			return error;
    340       1.2       ad 	}
    341       1.2       ad 
    342       1.2       ad 	return 0;
    343       1.2       ad }
    344       1.2       ad 
    345       1.2       ad /* ARGSUSED */
    346       1.2       ad int
    347       1.2       ad sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    348       1.2       ad {
    349       1.2       ad 	struct sys__lwp_kill_args /* {
    350       1.2       ad 		syscallarg(lwpid_t)	target;
    351       1.2       ad 		syscallarg(int)		signo;
    352       1.2       ad 	} */ *uap = v;
    353       1.2       ad 	struct proc *p = l->l_proc;
    354       1.2       ad 	struct lwp *t;
    355       1.2       ad 	ksiginfo_t ksi;
    356       1.2       ad 	int signo = SCARG(uap, signo);
    357       1.2       ad 	int error = 0;
    358       1.2       ad 
    359       1.2       ad 	if ((u_int)signo >= NSIG)
    360       1.2       ad 		return EINVAL;
    361       1.2       ad 
    362       1.2       ad 	KSI_INIT(&ksi);
    363       1.2       ad 	ksi.ksi_signo = signo;
    364       1.2       ad 	ksi.ksi_code = SI_USER;
    365       1.2       ad 	ksi.ksi_pid = p->p_pid;
    366       1.2       ad 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    367       1.2       ad 	ksi.ksi_lid = SCARG(uap, target);
    368       1.2       ad 
    369       1.2       ad 	mutex_enter(&proclist_mutex);
    370       1.2       ad 	mutex_enter(&p->p_smutex);
    371       1.2       ad 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    372       1.2       ad 		error = ESRCH;
    373       1.2       ad 	else if (signo != 0)
    374       1.2       ad 		kpsignal2(p, &ksi);
    375       1.2       ad 	mutex_exit(&p->p_smutex);
    376       1.2       ad 	mutex_exit(&proclist_mutex);
    377       1.2       ad 
    378       1.2       ad 	return error;
    379       1.2       ad }
    380       1.2       ad 
    381       1.2       ad int
    382       1.2       ad sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
    383       1.2       ad {
    384       1.2       ad 	struct sys__lwp_detach_args /* {
    385       1.2       ad 		syscallarg(lwpid_t)	target;
    386       1.2       ad 	} */ *uap = v;
    387       1.2       ad 	struct proc *p;
    388       1.2       ad 	struct lwp *t;
    389       1.2       ad 	lwpid_t target;
    390       1.2       ad 	int error;
    391       1.2       ad 
    392       1.2       ad 	target = SCARG(uap, target);
    393       1.2       ad 	p = l->l_proc;
    394       1.2       ad 
    395       1.2       ad 	mutex_enter(&p->p_smutex);
    396       1.2       ad 
    397       1.2       ad 	if (l->l_lid == target)
    398       1.2       ad 		t = l;
    399       1.2       ad 	else {
    400       1.2       ad 		/*
    401       1.2       ad 		 * We can't use lwp_find() here because the target might
    402       1.2       ad 		 * be a zombie.
    403       1.2       ad 		 */
    404       1.2       ad 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    405       1.2       ad 			if (t->l_lid == target)
    406       1.2       ad 				break;
    407       1.2       ad 	}
    408       1.2       ad 
    409       1.2       ad 	/*
    410       1.2       ad 	 * If the LWP is already detached, there's nothing to do.
    411       1.2       ad 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    412       1.2       ad 	 * is visible with the proc mutex held.
    413       1.2       ad 	 *
    414       1.2       ad 	 * After we have detached or released the LWP, kick any
    415       1.2       ad 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    416       1.2       ad 	 * for the target LWP to exit.
    417       1.2       ad 	 */
    418       1.2       ad 	if (t != NULL && t->l_stat != LSIDL) {
    419       1.2       ad 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    420       1.2       ad 			p->p_ndlwps++;
    421       1.2       ad 			t->l_prflag |= LPR_DETACHED;
    422       1.2       ad 			if (t->l_stat == LSZOMB) {
    423       1.2       ad 				cv_broadcast(&p->p_lwpcv);
    424       1.2       ad 				lwp_free(t, 0, 0); /* releases proc mutex */
    425       1.2       ad 				return 0;
    426       1.2       ad 			}
    427       1.2       ad 			error = 0;
    428       1.2       ad 		} else
    429       1.2       ad 			error = EINVAL;
    430       1.2       ad 	} else
    431       1.2       ad 		error = ESRCH;
    432       1.2       ad 
    433       1.2       ad 	cv_broadcast(&p->p_lwpcv);
    434       1.2       ad 	mutex_exit(&p->p_smutex);
    435       1.2       ad 
    436       1.2       ad 	return error;
    437       1.2       ad }
    438       1.2       ad 
    439       1.2       ad static inline wchan_t
    440       1.2       ad lwp_park_wchan(struct proc *p, const void *hint)
    441       1.2       ad {
    442       1.2       ad 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    443       1.2       ad }
    444       1.2       ad 
    445       1.2       ad /*
    446       1.2       ad  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    447       1.2       ad  * will remain parked until another LWP in the same process calls in and
    448       1.2       ad  * requests that it be unparked.
    449       1.2       ad  */
    450       1.2       ad int
    451       1.2       ad sys__lwp_park(struct lwp *l, void *v, register_t *retval)
    452       1.2       ad {
    453       1.2       ad 	struct sys__lwp_park_args /* {
    454       1.2       ad 		syscallarg(const struct timespec *)	ts;
    455       1.2       ad 		syscallarg(ucontext_t *)		uc;
    456       1.2       ad 		syscallarg(const void *)		hint;
    457       1.2       ad 	} */ *uap = v;
    458       1.2       ad 	const struct timespec *tsp;
    459       1.2       ad 	struct timespec ts, tsx;
    460       1.2       ad 	struct timeval tv;
    461       1.2       ad 	sleepq_t *sq;
    462       1.2       ad 	wchan_t wchan;
    463       1.2       ad 	int timo, error;
    464       1.2       ad 
    465       1.2       ad 	/* Fix up the given timeout value. */
    466       1.2       ad 	if ((tsp = SCARG(uap, ts)) != NULL) {
    467       1.2       ad 		if ((error = copyin(tsp, &ts, sizeof(ts))) != 0)
    468       1.2       ad 			return error;
    469       1.2       ad 		getnanotime(&tsx);
    470       1.2       ad 		timespecsub(&ts, &tsx, &ts);
    471       1.2       ad 		tv.tv_sec = ts.tv_sec;
    472       1.2       ad 		tv.tv_usec = ts.tv_nsec / 1000;
    473       1.2       ad 		if (tv.tv_sec < 0 || (tv.tv_sec == 0 && tv.tv_usec < 0))
    474       1.2       ad 			return ETIMEDOUT;
    475       1.2       ad 		if ((error = itimerfix(&tv)) != 0)
    476       1.2       ad 			return error;
    477       1.2       ad 		timo = tvtohz(&tv);
    478       1.2       ad 	} else
    479       1.2       ad 		timo = 0;
    480       1.2       ad 
    481       1.2       ad 	/* Find and lock the sleep queue. */
    482       1.2       ad 	wchan = lwp_park_wchan(l->l_proc, SCARG(uap, hint));
    483       1.2       ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    484       1.2       ad 
    485       1.2       ad 	/*
    486       1.2       ad 	 * Before going the full route and blocking, check to see if an
    487       1.2       ad 	 * unpark op is pending.
    488       1.2       ad 	 */
    489       1.9       ad 	sleepq_lwp_lock(l);
    490       1.8       ad 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    491       1.8       ad 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    492       1.9       ad 		sleepq_lwp_unlock(l);
    493       1.2       ad 		sleepq_unlock(sq);
    494       1.2       ad 		LWP_COUNT(lwp_ev_park_early, 1);
    495       1.2       ad 		return EALREADY;
    496       1.2       ad 	}
    497       1.9       ad #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    498       1.8       ad 	lwp_unlock_to(l, sq->sq_mutex);
    499       1.9       ad #endif
    500       1.2       ad 
    501       1.2       ad 	/*
    502       1.2       ad 	 * For now we ignore the ucontext argument.  In the future, we may
    503       1.2       ad 	 * put our stack up to be recycled.  If it's binned, a trampoline
    504       1.2       ad 	 * function could call sleepq_unblock() on our behalf.
    505       1.2       ad 	 */
    506       1.2       ad 	LWP_COUNT(lwp_ev_park, 1);
    507      1.10       ad 	KERNEL_UNLOCK_ALL(l, &l->l_biglocks); /* XXX for compat32 */
    508       1.2       ad 	sleepq_block(sq, sched_kpri(l), wchan, "parked", timo, 1,
    509       1.2       ad 	    &lwp_park_sobj);
    510       1.2       ad 	error = sleepq_unblock(timo, 1);
    511       1.2       ad 	return error == EWOULDBLOCK ? ETIMEDOUT : error;
    512       1.2       ad }
    513       1.2       ad 
    514       1.2       ad int
    515       1.2       ad sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
    516       1.2       ad {
    517       1.2       ad 	struct sys__lwp_unpark_args /* {
    518       1.2       ad 		syscallarg(lwpid_t)		target;
    519       1.2       ad 		syscallarg(const void *)	hint;
    520       1.2       ad 	} */ *uap = v;
    521       1.2       ad 	struct proc *p;
    522       1.2       ad 	struct lwp *t;
    523       1.2       ad 	sleepq_t *sq;
    524       1.2       ad 	lwpid_t target;
    525       1.2       ad 	wchan_t wchan;
    526       1.2       ad 	int swapin;
    527       1.2       ad 
    528       1.2       ad 	p = l->l_proc;
    529       1.2       ad 	target = SCARG(uap, target);
    530       1.2       ad 
    531       1.2       ad 	/*
    532       1.2       ad 	 * Easy case: search for the LWP on the sleep queue.  If
    533       1.2       ad 	 * it's parked, remove it from the queue and set running.
    534       1.2       ad 	 */
    535       1.2       ad 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    536       1.2       ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    537       1.2       ad 
    538       1.2       ad 	TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    539       1.2       ad 		if (t->l_proc == p && t->l_lid == target)
    540       1.2       ad 			break;
    541       1.2       ad 
    542       1.2       ad 	if (t == NULL) {
    543       1.2       ad 		/*
    544       1.2       ad 		 * The LWP hasn't parked yet.  Take the hit
    545       1.2       ad 		 * and mark the operation as pending.
    546       1.2       ad 		 */
    547       1.8       ad 		LWP_COUNT(lwp_ev_park_slowpath, 1);
    548       1.2       ad 		sleepq_unlock(sq);
    549       1.2       ad 		mutex_enter(&p->p_smutex);
    550       1.2       ad 		if ((t = lwp_find(p, target)) == NULL) {
    551       1.2       ad 			mutex_exit(&p->p_smutex);
    552       1.2       ad 			return ESRCH;
    553       1.2       ad 		}
    554       1.2       ad 		lwp_lock(t);
    555       1.2       ad 		mutex_exit(&p->p_smutex);
    556       1.2       ad 
    557       1.2       ad 		if (t->l_sleepq == sq) {
    558       1.2       ad 			/*
    559       1.2       ad 			 * We have raced, and the LWP is now parked.
    560       1.2       ad 			 * Wake it in the usual way.
    561       1.2       ad 			 */
    562       1.2       ad 			KASSERT(t->l_syncobj == &lwp_park_sobj);
    563       1.8       ad 			KASSERT(lwp_locked(t, sq->sq_mutex));
    564       1.2       ad 			LWP_COUNT(lwp_ev_park_raced, 1);
    565       1.2       ad 		} else {
    566       1.2       ad 			/*
    567      1.12     yamt 			 * It may not have parked yet, or is parked
    568       1.2       ad 			 * on a different user sync object.  The
    569       1.2       ad 			 * latter is an application error.
    570       1.2       ad 			 */
    571       1.8       ad 			t->l_flag |= LW_UNPARKED;
    572       1.2       ad 			lwp_unlock(t);
    573       1.2       ad 			return 0;
    574       1.2       ad 		}
    575       1.2       ad 	}
    576       1.2       ad 
    577       1.2       ad 	swapin = sleepq_remove(sq, t);
    578       1.8       ad 	LWP_COUNT(lwp_ev_park_targ, 1);
    579       1.2       ad 	sleepq_unlock(sq);
    580       1.2       ad 	if (swapin)
    581       1.3       ad 		uvm_kick_scheduler();
    582       1.2       ad 	return 0;
    583       1.2       ad }
    584       1.2       ad 
    585       1.2       ad int
    586       1.2       ad sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
    587       1.2       ad {
    588       1.2       ad 	struct sys__lwp_unpark_all_args /* {
    589       1.2       ad 		syscallarg(const lwpid_t *)	targets;
    590       1.2       ad 		syscallarg(size_t)		ntargets;
    591       1.2       ad 		syscallarg(const void *)	hint;
    592       1.2       ad 	} */ *uap = v;
    593       1.2       ad 	struct proc *p;
    594       1.2       ad 	struct lwp *t;
    595       1.2       ad 	sleepq_t *sq;
    596       1.2       ad 	wchan_t wchan;
    597       1.2       ad 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    598       1.2       ad 	int swapin, error;
    599       1.2       ad 	u_int ntargets, unparked;
    600       1.2       ad 	size_t sz;
    601       1.2       ad 
    602       1.2       ad 	p = l->l_proc;
    603       1.2       ad 	ntargets = SCARG(uap, ntargets);
    604       1.2       ad 
    605       1.2       ad 	if (SCARG(uap, targets) == NULL) {
    606       1.2       ad 		/*
    607       1.2       ad 		 * Let the caller know how much we are willing to do, and
    608       1.2       ad 		 * let it unpark the LWPs in blocks.
    609       1.2       ad 		 */
    610       1.2       ad 		*retval = LWP_UNPARK_MAX;
    611       1.2       ad 		return 0;
    612       1.2       ad 	}
    613       1.2       ad 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    614       1.2       ad 		return EINVAL;
    615       1.2       ad 
    616       1.2       ad 	/*
    617       1.2       ad 	 * Copy in the target array.  If it's a small number of LWPs, then
    618       1.2       ad 	 * place the numbers on the stack.
    619       1.2       ad 	 */
    620       1.2       ad 	sz = sizeof(target) * ntargets;
    621       1.2       ad 	if (sz <= sizeof(targets))
    622       1.2       ad 		tp = targets;
    623       1.2       ad 	else {
    624       1.2       ad 		tp = kmem_alloc(sz, KM_SLEEP);
    625       1.2       ad 		if (tp == NULL)
    626       1.2       ad 			return ENOMEM;
    627       1.2       ad 	}
    628       1.2       ad 	error = copyin(SCARG(uap, targets), tp, sz);
    629       1.2       ad 	if (error != 0) {
    630  1.12.2.1       ad 		if (tp != targets)
    631       1.2       ad 			kmem_free(tp, sz);
    632       1.2       ad 		return error;
    633       1.2       ad 	}
    634       1.2       ad 
    635       1.2       ad 	unparked = 0;
    636       1.2       ad 	swapin = 0;
    637       1.2       ad 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    638       1.2       ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    639       1.2       ad 
    640       1.2       ad 	for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
    641       1.2       ad 		target = *tpp;
    642       1.2       ad 
    643       1.2       ad 		/*
    644       1.2       ad 		 * Easy case: search for the LWP on the sleep queue.  If
    645       1.2       ad 		 * it's parked, remove it from the queue and set running.
    646       1.2       ad 		 */
    647       1.2       ad 		TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    648       1.2       ad 			if (t->l_proc == p && t->l_lid == target)
    649       1.2       ad 				break;
    650       1.2       ad 
    651       1.2       ad 		if (t != NULL) {
    652       1.2       ad 			swapin |= sleepq_remove(sq, t);
    653       1.2       ad 			unparked++;
    654       1.2       ad 			continue;
    655       1.2       ad 		}
    656       1.2       ad 
    657       1.2       ad 		/*
    658       1.2       ad 		 * The LWP hasn't parked yet.  Take the hit and
    659       1.2       ad 		 * mark the operation as pending.
    660       1.2       ad 		 */
    661       1.8       ad 		LWP_COUNT(lwp_ev_park_slowpath, 1);
    662       1.2       ad 		sleepq_unlock(sq);
    663       1.2       ad 		mutex_enter(&p->p_smutex);
    664       1.2       ad 		if ((t = lwp_find(p, target)) == NULL) {
    665       1.2       ad 			mutex_exit(&p->p_smutex);
    666       1.2       ad 			sleepq_lock(sq);
    667       1.2       ad 			continue;
    668       1.2       ad 		}
    669       1.2       ad 		lwp_lock(t);
    670       1.2       ad 		mutex_exit(&p->p_smutex);
    671       1.2       ad 
    672       1.2       ad 		if (t->l_sleepq == sq) {
    673       1.2       ad 			/*
    674       1.2       ad 			 * We have raced, and the LWP is now parked.
    675       1.2       ad 			 * Wake it in the usual way.
    676       1.2       ad 			 */
    677       1.2       ad 			KASSERT(t->l_syncobj == &lwp_park_sobj);
    678       1.8       ad 			KASSERT(lwp_locked(t, sq->sq_mutex));
    679       1.2       ad 			LWP_COUNT(lwp_ev_park_raced, 1);
    680       1.2       ad 			swapin |= sleepq_remove(sq, t);
    681       1.2       ad 			unparked++;
    682       1.2       ad 		} else {
    683       1.2       ad 			/*
    684      1.12     yamt 			 * It may not have parked yet, or is parked
    685       1.2       ad 			 * on a different user sync object.  The
    686       1.2       ad 			 * latter is an application error.
    687       1.2       ad 			 */
    688       1.8       ad 			t->l_flag |= LW_UNPARKED;
    689       1.2       ad 			lwp_unlock(t);
    690       1.2       ad 			sleepq_lock(sq);
    691       1.2       ad 		}
    692       1.2       ad 	}
    693       1.2       ad 
    694       1.2       ad 	sleepq_unlock(sq);
    695  1.12.2.1       ad 	if (tp != targets)
    696       1.2       ad 		kmem_free(tp, sz);
    697       1.2       ad 	if (swapin)
    698       1.3       ad 		uvm_kick_scheduler();
    699       1.2       ad 	LWP_COUNT(lwp_ev_park_bcast, unparked);
    700       1.2       ad 	LWP_COUNT(lwp_ev_park_miss, (ntargets - unparked));
    701       1.2       ad 	/* XXXAD return unparked; */
    702       1.2       ad 	return 0;
    703       1.2       ad }
    704