Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.23.2.7
      1  1.23.2.7     joerg /*	$NetBSD: sys_lwp.c,v 1.23.2.7 2007/12/03 16:14:57 joerg 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.23.2.7     joerg __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.23.2.7 2007/12/03 16:14:57 joerg 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.23.2.6     joerg #include <sys/lwpctl.h>
     57       1.2        ad 
     58       1.2        ad #include <uvm/uvm_extern.h>
     59       1.2        ad 
     60       1.2        ad #define	LWP_UNPARK_MAX		1024
     61       1.2        ad 
     62       1.2        ad syncobj_t lwp_park_sobj = {
     63  1.23.2.3     joerg 	SOBJ_SLEEPQ_LIFO,
     64       1.2        ad 	sleepq_unsleep,
     65       1.7      yamt 	sleepq_changepri,
     66       1.7      yamt 	sleepq_lendpri,
     67       1.7      yamt 	syncobj_noowner,
     68       1.2        ad };
     69       1.2        ad 
     70       1.2        ad sleeptab_t	lwp_park_tab;
     71       1.2        ad 
     72       1.2        ad void
     73       1.2        ad lwp_sys_init(void)
     74       1.2        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.23.2.4     joerg 	error = lwp_create(l, p, uaddr, inmem, SCARG(uap, flags) & LWP_DETACHED,
    111  1.23.2.4     joerg 	    NULL, 0, p->p_emul->e_startlwp, newuc, &l2, l->l_class);
    112      1.18     rmind 	if (error) {
    113  1.23.2.4     joerg 		uvm_uarea_free(uaddr, curcpu());
    114      1.18     rmind 		pool_put(&lwp_uc_pool, newuc);
    115      1.18     rmind 		return error;
    116      1.18     rmind 	}
    117       1.2        ad 
    118      1.21     rmind 	lid = l2->l_lid;
    119      1.21     rmind 	error = copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    120      1.21     rmind 	if (error) {
    121      1.21     rmind 		lwp_exit(l2);
    122      1.21     rmind 		pool_put(&lwp_uc_pool, newuc);
    123      1.21     rmind 		return error;
    124      1.21     rmind 	}
    125      1.21     rmind 
    126       1.2        ad 	/*
    127       1.2        ad 	 * Set the new LWP running, unless the caller has requested that
    128       1.2        ad 	 * it be created in suspended state.  If the process is stopping,
    129       1.2        ad 	 * then the LWP is created stopped.
    130       1.2        ad 	 */
    131       1.2        ad 	mutex_enter(&p->p_smutex);
    132       1.2        ad 	lwp_lock(l2);
    133       1.2        ad 	if ((SCARG(uap, flags) & LWP_SUSPENDED) == 0 &&
    134       1.4     pavel 	    (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    135       1.2        ad 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0)
    136       1.2        ad 	    		l2->l_stat = LSSTOP;
    137       1.2        ad 		else {
    138      1.19      yamt 			KASSERT(lwp_locked(l2, l2->l_cpu->ci_schedstate.spc_mutex));
    139       1.2        ad 			p->p_nrlwps++;
    140       1.2        ad 			l2->l_stat = LSRUN;
    141      1.19      yamt 			sched_enqueue(l2, false);
    142       1.2        ad 		}
    143  1.23.2.7     joerg 		lwp_unlock(l2);
    144  1.23.2.7     joerg 	} else {
    145       1.2        ad 		l2->l_stat = LSSUSPENDED;
    146  1.23.2.7     joerg 		lwp_unlock_to(l2, &l2->l_cpu->ci_schedstate.spc_lwplock);
    147  1.23.2.7     joerg 	}
    148       1.2        ad 	mutex_exit(&p->p_smutex);
    149       1.2        ad 
    150       1.2        ad 	return 0;
    151       1.2        ad }
    152       1.2        ad 
    153       1.2        ad int
    154       1.2        ad sys__lwp_exit(struct lwp *l, void *v, register_t *retval)
    155       1.2        ad {
    156       1.2        ad 
    157       1.2        ad 	lwp_exit(l);
    158       1.2        ad 	return 0;
    159       1.2        ad }
    160       1.2        ad 
    161       1.2        ad int
    162       1.2        ad sys__lwp_self(struct lwp *l, void *v, register_t *retval)
    163       1.2        ad {
    164       1.2        ad 
    165       1.2        ad 	*retval = l->l_lid;
    166       1.2        ad 	return 0;
    167       1.2        ad }
    168       1.2        ad 
    169       1.2        ad int
    170       1.2        ad sys__lwp_getprivate(struct lwp *l, void *v, register_t *retval)
    171       1.2        ad {
    172       1.2        ad 
    173       1.2        ad 	*retval = (uintptr_t)l->l_private;
    174       1.2        ad 	return 0;
    175       1.2        ad }
    176       1.2        ad 
    177       1.2        ad int
    178       1.2        ad sys__lwp_setprivate(struct lwp *l, void *v, register_t *retval)
    179       1.2        ad {
    180       1.2        ad 	struct sys__lwp_setprivate_args /* {
    181       1.2        ad 		syscallarg(void *) ptr;
    182       1.2        ad 	} */ *uap = v;
    183       1.2        ad 
    184       1.2        ad 	l->l_private = SCARG(uap, ptr);
    185       1.2        ad 	return 0;
    186       1.2        ad }
    187       1.2        ad 
    188       1.2        ad int
    189       1.2        ad sys__lwp_suspend(struct lwp *l, void *v, register_t *retval)
    190       1.2        ad {
    191       1.2        ad 	struct sys__lwp_suspend_args /* {
    192       1.2        ad 		syscallarg(lwpid_t) target;
    193       1.2        ad 	} */ *uap = v;
    194       1.2        ad 	struct proc *p = l->l_proc;
    195       1.2        ad 	struct lwp *t;
    196       1.2        ad 	int error;
    197       1.2        ad 
    198       1.2        ad 	mutex_enter(&p->p_smutex);
    199       1.2        ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    200       1.2        ad 		mutex_exit(&p->p_smutex);
    201       1.2        ad 		return ESRCH;
    202       1.2        ad 	}
    203       1.2        ad 
    204       1.2        ad 	/*
    205       1.2        ad 	 * Check for deadlock, which is only possible when we're suspending
    206       1.2        ad 	 * ourself.  XXX There is a short race here, as p_nrlwps is only
    207       1.2        ad 	 * incremented when an LWP suspends itself on the kernel/user
    208       1.2        ad 	 * boundary.  It's still possible to kill -9 the process so we
    209       1.2        ad 	 * don't bother checking further.
    210       1.2        ad 	 */
    211       1.2        ad 	lwp_lock(t);
    212       1.2        ad 	if ((t == l && p->p_nrlwps == 1) ||
    213       1.4     pavel 	    (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
    214       1.2        ad 		lwp_unlock(t);
    215       1.2        ad 		mutex_exit(&p->p_smutex);
    216       1.2        ad 		return EDEADLK;
    217       1.2        ad 	}
    218       1.2        ad 
    219       1.2        ad 	/*
    220       1.2        ad 	 * Suspend the LWP.  XXX If it's on a different CPU, we should wait
    221       1.2        ad 	 * for it to be preempted, where it will put itself to sleep.
    222       1.2        ad 	 *
    223       1.2        ad 	 * Suspension of the current LWP will happen on return to userspace.
    224       1.2        ad 	 */
    225       1.2        ad 	error = lwp_suspend(l, t);
    226      1.23     rmind 	if (error) {
    227      1.23     rmind 		mutex_exit(&p->p_smutex);
    228      1.23     rmind 		return error;
    229      1.23     rmind 	}
    230      1.23     rmind 
    231      1.23     rmind 	/*
    232      1.23     rmind 	 * Wait for:
    233      1.23     rmind 	 *  o process exiting
    234      1.23     rmind 	 *  o target LWP suspended
    235      1.23     rmind 	 *  o target LWP not suspended and L_WSUSPEND clear
    236      1.23     rmind 	 *  o target LWP exited
    237      1.23     rmind 	 */
    238      1.23     rmind 	for (;;) {
    239      1.23     rmind 		error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex);
    240      1.23     rmind 		if (error) {
    241      1.23     rmind 			error = ERESTART;
    242      1.23     rmind 			break;
    243      1.23     rmind 		}
    244  1.23.2.2  jmcneill 		if (lwp_find(p, SCARG(uap, target)) == NULL) {
    245  1.23.2.2  jmcneill 			error = ESRCH;
    246  1.23.2.2  jmcneill 			break;
    247  1.23.2.2  jmcneill 		}
    248      1.23     rmind 		if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
    249      1.23     rmind 			error = ERESTART;
    250      1.23     rmind 			break;
    251      1.23     rmind 		}
    252      1.23     rmind 		if (t->l_stat == LSSUSPENDED ||
    253      1.23     rmind 		    (t->l_flag & LW_WSUSPEND) == 0)
    254      1.23     rmind 			break;
    255      1.23     rmind 	}
    256       1.2        ad 	mutex_exit(&p->p_smutex);
    257       1.2        ad 
    258       1.2        ad 	return error;
    259       1.2        ad }
    260       1.2        ad 
    261       1.2        ad int
    262       1.2        ad sys__lwp_continue(struct lwp *l, void *v, register_t *retval)
    263       1.2        ad {
    264       1.2        ad 	struct sys__lwp_continue_args /* {
    265       1.2        ad 		syscallarg(lwpid_t) target;
    266       1.2        ad 	} */ *uap = v;
    267       1.2        ad 	int error;
    268       1.2        ad 	struct proc *p = l->l_proc;
    269       1.2        ad 	struct lwp *t;
    270       1.2        ad 
    271       1.2        ad 	error = 0;
    272       1.2        ad 
    273       1.2        ad 	mutex_enter(&p->p_smutex);
    274       1.2        ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    275       1.2        ad 		mutex_exit(&p->p_smutex);
    276       1.2        ad 		return ESRCH;
    277       1.2        ad 	}
    278       1.2        ad 
    279       1.2        ad 	lwp_lock(t);
    280       1.2        ad 	lwp_continue(t);
    281       1.2        ad 	mutex_exit(&p->p_smutex);
    282       1.2        ad 
    283       1.2        ad 	return error;
    284       1.2        ad }
    285       1.2        ad 
    286       1.2        ad int
    287       1.2        ad sys__lwp_wakeup(struct lwp *l, void *v, register_t *retval)
    288       1.2        ad {
    289       1.2        ad 	struct sys__lwp_wakeup_args /* {
    290       1.2        ad 		syscallarg(lwpid_t) target;
    291       1.2        ad 	} */ *uap = v;
    292       1.2        ad 	struct lwp *t;
    293       1.2        ad 	struct proc *p;
    294       1.2        ad 	int error;
    295       1.2        ad 
    296       1.2        ad 	p = l->l_proc;
    297       1.2        ad 	mutex_enter(&p->p_smutex);
    298       1.2        ad 
    299       1.2        ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    300       1.2        ad 		mutex_exit(&p->p_smutex);
    301       1.2        ad 		return ESRCH;
    302       1.2        ad 	}
    303       1.2        ad 
    304       1.2        ad 	lwp_lock(t);
    305      1.15        ad 	t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
    306       1.2        ad 
    307       1.2        ad 	if (t->l_stat != LSSLEEP) {
    308      1.16        ad 		lwp_unlock(t);
    309       1.2        ad 		error = ENODEV;
    310      1.16        ad 	} else if ((t->l_flag & LW_SINTR) == 0) {
    311      1.16        ad 		lwp_unlock(t);
    312       1.2        ad 		error = EBUSY;
    313      1.16        ad 	} else {
    314      1.16        ad 		/* Wake it up.  lwp_unsleep() will release the LWP lock. */
    315      1.16        ad 		lwp_unsleep(t);
    316      1.16        ad 		error = 0;
    317       1.2        ad 	}
    318       1.2        ad 
    319       1.2        ad 	mutex_exit(&p->p_smutex);
    320       1.2        ad 
    321       1.2        ad 	return error;
    322       1.2        ad }
    323       1.2        ad 
    324       1.2        ad int
    325       1.2        ad sys__lwp_wait(struct lwp *l, void *v, register_t *retval)
    326       1.2        ad {
    327       1.2        ad 	struct sys__lwp_wait_args /* {
    328       1.2        ad 		syscallarg(lwpid_t) wait_for;
    329       1.2        ad 		syscallarg(lwpid_t *) departed;
    330       1.2        ad 	} */ *uap = v;
    331       1.2        ad 	struct proc *p = l->l_proc;
    332       1.2        ad 	int error;
    333       1.2        ad 	lwpid_t dep;
    334       1.2        ad 
    335       1.2        ad 	mutex_enter(&p->p_smutex);
    336       1.2        ad 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    337       1.2        ad 	mutex_exit(&p->p_smutex);
    338       1.2        ad 
    339       1.2        ad 	if (error)
    340       1.2        ad 		return error;
    341       1.2        ad 
    342       1.2        ad 	if (SCARG(uap, departed)) {
    343       1.2        ad 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    344       1.2        ad 		if (error)
    345       1.2        ad 			return error;
    346       1.2        ad 	}
    347       1.2        ad 
    348       1.2        ad 	return 0;
    349       1.2        ad }
    350       1.2        ad 
    351       1.2        ad /* ARGSUSED */
    352       1.2        ad int
    353       1.2        ad sys__lwp_kill(struct lwp *l, void *v, register_t *retval)
    354       1.2        ad {
    355       1.2        ad 	struct sys__lwp_kill_args /* {
    356       1.2        ad 		syscallarg(lwpid_t)	target;
    357       1.2        ad 		syscallarg(int)		signo;
    358       1.2        ad 	} */ *uap = v;
    359       1.2        ad 	struct proc *p = l->l_proc;
    360       1.2        ad 	struct lwp *t;
    361       1.2        ad 	ksiginfo_t ksi;
    362       1.2        ad 	int signo = SCARG(uap, signo);
    363       1.2        ad 	int error = 0;
    364       1.2        ad 
    365       1.2        ad 	if ((u_int)signo >= NSIG)
    366       1.2        ad 		return EINVAL;
    367       1.2        ad 
    368       1.2        ad 	KSI_INIT(&ksi);
    369       1.2        ad 	ksi.ksi_signo = signo;
    370       1.2        ad 	ksi.ksi_code = SI_USER;
    371       1.2        ad 	ksi.ksi_pid = p->p_pid;
    372       1.2        ad 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    373       1.2        ad 	ksi.ksi_lid = SCARG(uap, target);
    374       1.2        ad 
    375       1.2        ad 	mutex_enter(&proclist_mutex);
    376       1.2        ad 	mutex_enter(&p->p_smutex);
    377       1.2        ad 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    378       1.2        ad 		error = ESRCH;
    379       1.2        ad 	else if (signo != 0)
    380       1.2        ad 		kpsignal2(p, &ksi);
    381       1.2        ad 	mutex_exit(&p->p_smutex);
    382       1.2        ad 	mutex_exit(&proclist_mutex);
    383       1.2        ad 
    384       1.2        ad 	return error;
    385       1.2        ad }
    386       1.2        ad 
    387       1.2        ad int
    388       1.2        ad sys__lwp_detach(struct lwp *l, void *v, register_t *retval)
    389       1.2        ad {
    390       1.2        ad 	struct sys__lwp_detach_args /* {
    391       1.2        ad 		syscallarg(lwpid_t)	target;
    392       1.2        ad 	} */ *uap = v;
    393       1.2        ad 	struct proc *p;
    394       1.2        ad 	struct lwp *t;
    395       1.2        ad 	lwpid_t target;
    396       1.2        ad 	int error;
    397       1.2        ad 
    398       1.2        ad 	target = SCARG(uap, target);
    399       1.2        ad 	p = l->l_proc;
    400       1.2        ad 
    401       1.2        ad 	mutex_enter(&p->p_smutex);
    402       1.2        ad 
    403       1.2        ad 	if (l->l_lid == target)
    404       1.2        ad 		t = l;
    405       1.2        ad 	else {
    406       1.2        ad 		/*
    407       1.2        ad 		 * We can't use lwp_find() here because the target might
    408       1.2        ad 		 * be a zombie.
    409       1.2        ad 		 */
    410       1.2        ad 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    411       1.2        ad 			if (t->l_lid == target)
    412       1.2        ad 				break;
    413       1.2        ad 	}
    414       1.2        ad 
    415       1.2        ad 	/*
    416       1.2        ad 	 * If the LWP is already detached, there's nothing to do.
    417       1.2        ad 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    418       1.2        ad 	 * is visible with the proc mutex held.
    419       1.2        ad 	 *
    420       1.2        ad 	 * After we have detached or released the LWP, kick any
    421       1.2        ad 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    422       1.2        ad 	 * for the target LWP to exit.
    423       1.2        ad 	 */
    424       1.2        ad 	if (t != NULL && t->l_stat != LSIDL) {
    425       1.2        ad 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    426       1.2        ad 			p->p_ndlwps++;
    427       1.2        ad 			t->l_prflag |= LPR_DETACHED;
    428       1.2        ad 			if (t->l_stat == LSZOMB) {
    429      1.17        ad 				/* Releases proc mutex. */
    430      1.17        ad 				lwp_free(t, false, false);
    431       1.2        ad 				return 0;
    432       1.2        ad 			}
    433       1.2        ad 			error = 0;
    434      1.17        ad 
    435      1.17        ad 			/*
    436      1.17        ad 			 * Have any LWPs sleeping in lwp_wait() recheck
    437      1.17        ad 			 * for deadlock.
    438      1.17        ad 			 */
    439      1.17        ad 			cv_broadcast(&p->p_lwpcv);
    440       1.2        ad 		} else
    441       1.2        ad 			error = EINVAL;
    442       1.2        ad 	} else
    443       1.2        ad 		error = ESRCH;
    444       1.2        ad 
    445       1.2        ad 	mutex_exit(&p->p_smutex);
    446       1.2        ad 
    447       1.2        ad 	return error;
    448       1.2        ad }
    449       1.2        ad 
    450       1.2        ad static inline wchan_t
    451       1.2        ad lwp_park_wchan(struct proc *p, const void *hint)
    452       1.2        ad {
    453      1.22        ad 
    454       1.2        ad 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    455       1.2        ad }
    456       1.2        ad 
    457       1.2        ad int
    458  1.23.2.1  jmcneill lwp_unpark(lwpid_t target, const void *hint)
    459       1.2        ad {
    460  1.23.2.1  jmcneill 	sleepq_t *sq;
    461  1.23.2.1  jmcneill 	wchan_t wchan;
    462  1.23.2.1  jmcneill 	int swapin;
    463  1.23.2.1  jmcneill 	proc_t *p;
    464  1.23.2.1  jmcneill 	lwp_t *t;
    465      1.20       dsl 
    466  1.23.2.1  jmcneill 	/*
    467  1.23.2.1  jmcneill 	 * Easy case: search for the LWP on the sleep queue.  If
    468  1.23.2.1  jmcneill 	 * it's parked, remove it from the queue and set running.
    469  1.23.2.1  jmcneill 	 */
    470  1.23.2.1  jmcneill 	p = curproc;
    471  1.23.2.1  jmcneill 	wchan = lwp_park_wchan(p, hint);
    472  1.23.2.1  jmcneill 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    473      1.20       dsl 
    474  1.23.2.1  jmcneill 	TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    475  1.23.2.1  jmcneill 		if (t->l_proc == p && t->l_lid == target)
    476  1.23.2.1  jmcneill 			break;
    477  1.23.2.1  jmcneill 
    478  1.23.2.1  jmcneill 	if (__predict_true(t != NULL)) {
    479  1.23.2.1  jmcneill 		swapin = sleepq_remove(sq, t);
    480  1.23.2.1  jmcneill 		sleepq_unlock(sq);
    481  1.23.2.1  jmcneill 		if (swapin)
    482  1.23.2.1  jmcneill 			uvm_kick_scheduler();
    483  1.23.2.1  jmcneill 		return 0;
    484  1.23.2.1  jmcneill 	}
    485  1.23.2.1  jmcneill 
    486  1.23.2.1  jmcneill 	/*
    487  1.23.2.1  jmcneill 	 * The LWP hasn't parked yet.  Take the hit and mark the
    488  1.23.2.1  jmcneill 	 * operation as pending.
    489  1.23.2.1  jmcneill 	 */
    490  1.23.2.1  jmcneill 	sleepq_unlock(sq);
    491  1.23.2.1  jmcneill 
    492  1.23.2.1  jmcneill 	mutex_enter(&p->p_smutex);
    493  1.23.2.1  jmcneill 	if ((t = lwp_find(p, target)) == NULL) {
    494  1.23.2.1  jmcneill 		mutex_exit(&p->p_smutex);
    495  1.23.2.1  jmcneill 		return ESRCH;
    496  1.23.2.1  jmcneill 	}
    497  1.23.2.1  jmcneill 
    498  1.23.2.1  jmcneill 	/*
    499  1.23.2.1  jmcneill 	 * It may not have parked yet, we may have raced, or it
    500  1.23.2.1  jmcneill 	 * is parked on a different user sync object.
    501  1.23.2.1  jmcneill 	 */
    502  1.23.2.1  jmcneill 	lwp_lock(t);
    503  1.23.2.1  jmcneill 	if (t->l_syncobj == &lwp_park_sobj) {
    504  1.23.2.1  jmcneill 		/* Releases the LWP lock. */
    505  1.23.2.1  jmcneill 		lwp_unsleep(t);
    506  1.23.2.1  jmcneill 	} else {
    507  1.23.2.1  jmcneill 		/*
    508  1.23.2.1  jmcneill 		 * Set the operation pending.  The next call to _lwp_park
    509  1.23.2.1  jmcneill 		 * will return early.
    510  1.23.2.1  jmcneill 		 */
    511  1.23.2.1  jmcneill 		t->l_flag |= LW_UNPARKED;
    512  1.23.2.1  jmcneill 		lwp_unlock(t);
    513  1.23.2.1  jmcneill 	}
    514      1.20       dsl 
    515  1.23.2.1  jmcneill 	mutex_exit(&p->p_smutex);
    516  1.23.2.1  jmcneill 	return 0;
    517      1.20       dsl }
    518      1.20       dsl 
    519      1.20       dsl int
    520  1.23.2.1  jmcneill lwp_park(struct timespec *ts, const void *hint)
    521      1.20       dsl {
    522      1.20       dsl 	struct timespec tsx;
    523       1.2        ad 	sleepq_t *sq;
    524       1.2        ad 	wchan_t wchan;
    525       1.2        ad 	int timo, error;
    526  1.23.2.1  jmcneill 	lwp_t *l;
    527       1.2        ad 
    528       1.2        ad 	/* Fix up the given timeout value. */
    529      1.20       dsl 	if (ts != NULL) {
    530       1.2        ad 		getnanotime(&tsx);
    531  1.23.2.1  jmcneill 		timespecsub(ts, &tsx, &tsx);
    532  1.23.2.1  jmcneill 		if (tsx.tv_sec < 0 || (tsx.tv_sec == 0 && tsx.tv_nsec <= 0))
    533       1.2        ad 			return ETIMEDOUT;
    534  1.23.2.1  jmcneill 		if ((error = itimespecfix(&tsx)) != 0)
    535       1.2        ad 			return error;
    536  1.23.2.1  jmcneill 		timo = tstohz(&tsx);
    537  1.23.2.1  jmcneill 		KASSERT(timo != 0);
    538       1.2        ad 	} else
    539       1.2        ad 		timo = 0;
    540       1.2        ad 
    541       1.2        ad 	/* Find and lock the sleep queue. */
    542  1.23.2.1  jmcneill 	l = curlwp;
    543      1.20       dsl 	wchan = lwp_park_wchan(l->l_proc, hint);
    544       1.2        ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    545       1.2        ad 
    546       1.2        ad 	/*
    547       1.2        ad 	 * Before going the full route and blocking, check to see if an
    548       1.2        ad 	 * unpark op is pending.
    549       1.2        ad 	 */
    550      1.19      yamt 	lwp_lock(l);
    551       1.8        ad 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    552       1.8        ad 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    553      1.19      yamt 		lwp_unlock(l);
    554       1.2        ad 		sleepq_unlock(sq);
    555       1.2        ad 		return EALREADY;
    556       1.2        ad 	}
    557       1.8        ad 	lwp_unlock_to(l, sq->sq_mutex);
    558  1.23.2.1  jmcneill 	l->l_biglocks = 0;
    559  1.23.2.4     joerg 	sleepq_enqueue(sq, wchan, "parked", &lwp_park_sobj);
    560      1.19      yamt 	error = sleepq_block(timo, true);
    561      1.13      yamt 	switch (error) {
    562      1.14      yamt 	case EWOULDBLOCK:
    563      1.14      yamt 		error = ETIMEDOUT;
    564      1.14      yamt 		break;
    565      1.14      yamt 	case ERESTART:
    566      1.14      yamt 		error = EINTR;
    567      1.14      yamt 		break;
    568      1.14      yamt 	default:
    569      1.14      yamt 		/* nothing */
    570      1.14      yamt 		break;
    571      1.13      yamt 	}
    572      1.13      yamt 	return error;
    573       1.2        ad }
    574       1.2        ad 
    575  1.23.2.1  jmcneill /*
    576  1.23.2.1  jmcneill  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    577  1.23.2.1  jmcneill  * will remain parked until another LWP in the same process calls in and
    578  1.23.2.1  jmcneill  * requests that it be unparked.
    579  1.23.2.1  jmcneill  */
    580       1.2        ad int
    581  1.23.2.1  jmcneill sys__lwp_park(struct lwp *l, void *v, register_t *retval)
    582       1.2        ad {
    583  1.23.2.1  jmcneill 	struct sys__lwp_park_args /* {
    584  1.23.2.1  jmcneill 		syscallarg(const struct timespec *)	ts;
    585  1.23.2.1  jmcneill 		syscallarg(lwpid_t)			unpark;
    586  1.23.2.1  jmcneill 		syscallarg(const void *)		hint;
    587  1.23.2.1  jmcneill 		syscallarg(const void *)		unparkhint;
    588       1.2        ad 	} */ *uap = v;
    589  1.23.2.1  jmcneill 	struct timespec ts, *tsp;
    590  1.23.2.1  jmcneill 	int error;
    591       1.2        ad 
    592  1.23.2.1  jmcneill 	if (SCARG(uap, ts) == NULL)
    593  1.23.2.1  jmcneill 		tsp = NULL;
    594  1.23.2.1  jmcneill 	else {
    595  1.23.2.1  jmcneill 		error = copyin(SCARG(uap, ts), &ts, sizeof(ts));
    596  1.23.2.1  jmcneill 		if (error != 0)
    597  1.23.2.1  jmcneill 			return error;
    598  1.23.2.1  jmcneill 		tsp = &ts;
    599      1.15        ad 	}
    600      1.15        ad 
    601  1.23.2.1  jmcneill 	if (SCARG(uap, unpark) != 0) {
    602  1.23.2.1  jmcneill 		error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
    603  1.23.2.1  jmcneill 		if (error != 0)
    604  1.23.2.1  jmcneill 			return error;
    605      1.15        ad 	}
    606       1.2        ad 
    607  1.23.2.1  jmcneill 	return lwp_park(tsp, SCARG(uap, hint));
    608  1.23.2.1  jmcneill }
    609       1.2        ad 
    610  1.23.2.1  jmcneill int
    611  1.23.2.1  jmcneill sys__lwp_unpark(struct lwp *l, void *v, register_t *retval)
    612  1.23.2.1  jmcneill {
    613  1.23.2.1  jmcneill 	struct sys__lwp_unpark_args /* {
    614  1.23.2.1  jmcneill 		syscallarg(lwpid_t)		target;
    615  1.23.2.1  jmcneill 		syscallarg(const void *)	hint;
    616  1.23.2.1  jmcneill 	} */ *uap = v;
    617  1.23.2.1  jmcneill 
    618  1.23.2.1  jmcneill 	return lwp_unpark(SCARG(uap, target), SCARG(uap, hint));
    619       1.2        ad }
    620       1.2        ad 
    621       1.2        ad int
    622       1.2        ad sys__lwp_unpark_all(struct lwp *l, void *v, register_t *retval)
    623       1.2        ad {
    624       1.2        ad 	struct sys__lwp_unpark_all_args /* {
    625       1.2        ad 		syscallarg(const lwpid_t *)	targets;
    626       1.2        ad 		syscallarg(size_t)		ntargets;
    627       1.2        ad 		syscallarg(const void *)	hint;
    628       1.2        ad 	} */ *uap = v;
    629       1.2        ad 	struct proc *p;
    630       1.2        ad 	struct lwp *t;
    631       1.2        ad 	sleepq_t *sq;
    632       1.2        ad 	wchan_t wchan;
    633       1.2        ad 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    634       1.2        ad 	int swapin, error;
    635      1.15        ad 	u_int ntargets;
    636       1.2        ad 	size_t sz;
    637       1.2        ad 
    638       1.2        ad 	p = l->l_proc;
    639       1.2        ad 	ntargets = SCARG(uap, ntargets);
    640       1.2        ad 
    641       1.2        ad 	if (SCARG(uap, targets) == NULL) {
    642       1.2        ad 		/*
    643       1.2        ad 		 * Let the caller know how much we are willing to do, and
    644       1.2        ad 		 * let it unpark the LWPs in blocks.
    645       1.2        ad 		 */
    646       1.2        ad 		*retval = LWP_UNPARK_MAX;
    647       1.2        ad 		return 0;
    648       1.2        ad 	}
    649       1.2        ad 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    650       1.2        ad 		return EINVAL;
    651       1.2        ad 
    652       1.2        ad 	/*
    653       1.2        ad 	 * Copy in the target array.  If it's a small number of LWPs, then
    654       1.2        ad 	 * place the numbers on the stack.
    655       1.2        ad 	 */
    656       1.2        ad 	sz = sizeof(target) * ntargets;
    657       1.2        ad 	if (sz <= sizeof(targets))
    658       1.2        ad 		tp = targets;
    659       1.2        ad 	else {
    660       1.2        ad 		KERNEL_LOCK(1, l);		/* XXXSMP */
    661       1.2        ad 		tp = kmem_alloc(sz, KM_SLEEP);
    662       1.2        ad 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    663       1.2        ad 		if (tp == NULL)
    664       1.2        ad 			return ENOMEM;
    665       1.2        ad 	}
    666       1.2        ad 	error = copyin(SCARG(uap, targets), tp, sz);
    667       1.2        ad 	if (error != 0) {
    668       1.2        ad 		if (tp != targets) {
    669       1.2        ad 			KERNEL_LOCK(1, l);	/* XXXSMP */
    670       1.2        ad 			kmem_free(tp, sz);
    671       1.2        ad 			KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    672       1.2        ad 		}
    673       1.2        ad 		return error;
    674       1.2        ad 	}
    675       1.2        ad 
    676       1.2        ad 	swapin = 0;
    677       1.2        ad 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    678       1.2        ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan);
    679       1.2        ad 
    680       1.2        ad 	for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
    681       1.2        ad 		target = *tpp;
    682       1.2        ad 
    683       1.2        ad 		/*
    684       1.2        ad 		 * Easy case: search for the LWP on the sleep queue.  If
    685       1.2        ad 		 * it's parked, remove it from the queue and set running.
    686       1.2        ad 		 */
    687       1.2        ad 		TAILQ_FOREACH(t, &sq->sq_queue, l_sleepchain)
    688       1.2        ad 			if (t->l_proc == p && t->l_lid == target)
    689       1.2        ad 				break;
    690       1.2        ad 
    691       1.2        ad 		if (t != NULL) {
    692       1.2        ad 			swapin |= sleepq_remove(sq, t);
    693       1.2        ad 			continue;
    694       1.2        ad 		}
    695       1.2        ad 
    696       1.2        ad 		/*
    697       1.2        ad 		 * The LWP hasn't parked yet.  Take the hit and
    698       1.2        ad 		 * mark the operation as pending.
    699       1.2        ad 		 */
    700       1.2        ad 		sleepq_unlock(sq);
    701       1.2        ad 		mutex_enter(&p->p_smutex);
    702       1.2        ad 		if ((t = lwp_find(p, target)) == NULL) {
    703       1.2        ad 			mutex_exit(&p->p_smutex);
    704       1.2        ad 			sleepq_lock(sq);
    705       1.2        ad 			continue;
    706       1.2        ad 		}
    707       1.2        ad 		lwp_lock(t);
    708       1.2        ad 
    709      1.15        ad 		/*
    710      1.15        ad 		 * It may not have parked yet, we may have raced, or
    711      1.15        ad 		 * it is parked on a different user sync object.
    712      1.15        ad 		 */
    713      1.15        ad 		if (t->l_syncobj == &lwp_park_sobj) {
    714      1.15        ad 			/* Releases the LWP lock. */
    715      1.16        ad 			lwp_unsleep(t);
    716       1.2        ad 		} else {
    717       1.2        ad 			/*
    718      1.15        ad 			 * Set the operation pending.  The next call to
    719      1.15        ad 			 * _lwp_park will return early.
    720       1.2        ad 			 */
    721       1.8        ad 			t->l_flag |= LW_UNPARKED;
    722       1.2        ad 			lwp_unlock(t);
    723       1.2        ad 		}
    724      1.15        ad 
    725      1.15        ad 		mutex_exit(&p->p_smutex);
    726      1.15        ad 		sleepq_lock(sq);
    727       1.2        ad 	}
    728       1.2        ad 
    729       1.2        ad 	sleepq_unlock(sq);
    730       1.2        ad 	if (tp != targets) {
    731       1.2        ad 		KERNEL_LOCK(1, l);		/* XXXSMP */
    732       1.2        ad 		kmem_free(tp, sz);
    733       1.2        ad 		KERNEL_UNLOCK_ONE(l);		/* XXXSMP */
    734       1.2        ad 	}
    735       1.2        ad 	if (swapin)
    736       1.3        ad 		uvm_kick_scheduler();
    737      1.15        ad 
    738       1.2        ad 	return 0;
    739       1.2        ad }
    740  1.23.2.5     joerg 
    741  1.23.2.5     joerg int
    742  1.23.2.5     joerg sys__lwp_setname(struct lwp *l, void *v, register_t *retval)
    743  1.23.2.5     joerg {
    744  1.23.2.5     joerg 	struct sys__lwp_setname_args /* {
    745  1.23.2.5     joerg 		syscallarg(lwpid_t)		target;
    746  1.23.2.5     joerg 		syscallarg(const char *)	name;
    747  1.23.2.5     joerg 	} */ *uap = v;
    748  1.23.2.5     joerg 	char *name, *oname;
    749  1.23.2.6     joerg 	lwpid_t target;
    750  1.23.2.5     joerg 	proc_t *p;
    751  1.23.2.5     joerg 	lwp_t *t;
    752  1.23.2.5     joerg 	int error;
    753  1.23.2.5     joerg 
    754  1.23.2.6     joerg 	if ((target = SCARG(uap, target)) == 0)
    755  1.23.2.6     joerg 		target = l->l_lid;
    756  1.23.2.6     joerg 
    757  1.23.2.5     joerg 	name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
    758  1.23.2.5     joerg 	if (name == NULL)
    759  1.23.2.5     joerg 		return ENOMEM;
    760  1.23.2.5     joerg 	error = copyinstr(SCARG(uap, name), name, MAXCOMLEN, NULL);
    761  1.23.2.5     joerg 	switch (error) {
    762  1.23.2.5     joerg 	case ENAMETOOLONG:
    763  1.23.2.5     joerg 	case 0:
    764  1.23.2.5     joerg 		name[MAXCOMLEN - 1] = '\0';
    765  1.23.2.5     joerg 		break;
    766  1.23.2.5     joerg 	default:
    767  1.23.2.5     joerg 		kmem_free(name, MAXCOMLEN);
    768  1.23.2.5     joerg 		return error;
    769  1.23.2.5     joerg 	}
    770  1.23.2.5     joerg 
    771  1.23.2.5     joerg 	p = curproc;
    772  1.23.2.5     joerg 	mutex_enter(&p->p_smutex);
    773  1.23.2.6     joerg 	if ((t = lwp_find(p, target)) == NULL) {
    774  1.23.2.5     joerg 		mutex_exit(&p->p_smutex);
    775  1.23.2.5     joerg 		kmem_free(name, MAXCOMLEN);
    776  1.23.2.5     joerg 		return ESRCH;
    777  1.23.2.5     joerg 	}
    778  1.23.2.5     joerg 	lwp_lock(t);
    779  1.23.2.5     joerg 	oname = t->l_name;
    780  1.23.2.5     joerg 	t->l_name = name;
    781  1.23.2.5     joerg 	lwp_unlock(t);
    782  1.23.2.5     joerg 	mutex_exit(&p->p_smutex);
    783  1.23.2.5     joerg 
    784  1.23.2.5     joerg 	if (oname != NULL)
    785  1.23.2.5     joerg 		kmem_free(oname, MAXCOMLEN);
    786  1.23.2.5     joerg 
    787  1.23.2.5     joerg 	return 0;
    788  1.23.2.5     joerg }
    789  1.23.2.5     joerg 
    790  1.23.2.5     joerg int
    791  1.23.2.5     joerg sys__lwp_getname(struct lwp *l, void *v, register_t *retval)
    792  1.23.2.5     joerg {
    793  1.23.2.5     joerg 	struct sys__lwp_getname_args /* {
    794  1.23.2.5     joerg 		syscallarg(lwpid_t)		target;
    795  1.23.2.5     joerg 		syscallarg(char *)		name;
    796  1.23.2.5     joerg 		syscallarg(size_t)		len;
    797  1.23.2.5     joerg 	} */ *uap = v;
    798  1.23.2.5     joerg 	char name[MAXCOMLEN];
    799  1.23.2.6     joerg 	lwpid_t target;
    800  1.23.2.5     joerg 	proc_t *p;
    801  1.23.2.5     joerg 	lwp_t *t;
    802  1.23.2.5     joerg 
    803  1.23.2.6     joerg 	if ((target = SCARG(uap, target)) == 0)
    804  1.23.2.6     joerg 		target = l->l_lid;
    805  1.23.2.6     joerg 
    806  1.23.2.5     joerg 	p = curproc;
    807  1.23.2.5     joerg 	mutex_enter(&p->p_smutex);
    808  1.23.2.6     joerg 	if ((t = lwp_find(p, target)) == NULL) {
    809  1.23.2.5     joerg 		mutex_exit(&p->p_smutex);
    810  1.23.2.5     joerg 		return ESRCH;
    811  1.23.2.5     joerg 	}
    812  1.23.2.5     joerg 	lwp_lock(t);
    813  1.23.2.5     joerg 	if (t->l_name == NULL)
    814  1.23.2.5     joerg 		name[0] = '\0';
    815  1.23.2.5     joerg 	else
    816  1.23.2.5     joerg 		strcpy(name, t->l_name);
    817  1.23.2.5     joerg 	lwp_unlock(t);
    818  1.23.2.5     joerg 	mutex_exit(&p->p_smutex);
    819  1.23.2.5     joerg 
    820  1.23.2.5     joerg 	return copyoutstr(name, SCARG(uap, name), SCARG(uap, len), NULL);
    821  1.23.2.5     joerg }
    822  1.23.2.6     joerg 
    823  1.23.2.6     joerg int
    824  1.23.2.6     joerg sys__lwp_ctl(struct lwp *l, void *v, register_t *retval)
    825  1.23.2.6     joerg {
    826  1.23.2.6     joerg 	struct sys__lwp_ctl_args /* {
    827  1.23.2.6     joerg 		syscallarg(int)			features;
    828  1.23.2.6     joerg 		syscallarg(struct lwpctl **)	address;
    829  1.23.2.6     joerg 	} */ *uap = v;
    830  1.23.2.6     joerg 	int error, features;
    831  1.23.2.6     joerg 	vaddr_t vaddr;
    832  1.23.2.6     joerg 
    833  1.23.2.6     joerg 	features = SCARG(uap, features);
    834  1.23.2.6     joerg 	if ((features & ~LWPCTL_FEATURE_CURCPU) != 0)
    835  1.23.2.6     joerg 		return ENODEV;
    836  1.23.2.6     joerg 	if ((error = lwp_ctl_alloc(&vaddr)) != 0)
    837  1.23.2.6     joerg 		return error;
    838  1.23.2.6     joerg 	return copyout(&vaddr, SCARG(uap, address), sizeof(void *));
    839  1.23.2.6     joerg }
    840