Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.56.12.3
      1  1.56.12.3     skrll /*	$NetBSD: sys_lwp.c,v 1.56.12.3 2017/08/28 17:53:07 skrll Exp $	*/
      2        1.2        ad 
      3        1.2        ad /*-
      4       1.36        ad  * Copyright (c) 2001, 2006, 2007, 2008 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  *
     19        1.2        ad  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20        1.2        ad  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21        1.2        ad  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22        1.2        ad  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23        1.2        ad  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24        1.2        ad  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25        1.2        ad  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26        1.2        ad  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27        1.2        ad  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28        1.2        ad  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29        1.2        ad  * POSSIBILITY OF SUCH DAMAGE.
     30        1.2        ad  */
     31        1.2        ad 
     32        1.2        ad /*
     33        1.2        ad  * Lightweight process (LWP) system calls.  See kern_lwp.c for a description
     34        1.2        ad  * of LWPs.
     35        1.2        ad  */
     36        1.2        ad 
     37        1.2        ad #include <sys/cdefs.h>
     38  1.56.12.3     skrll __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.56.12.3 2017/08/28 17:53:07 skrll Exp $");
     39        1.2        ad 
     40        1.2        ad #include <sys/param.h>
     41        1.2        ad #include <sys/systm.h>
     42        1.2        ad #include <sys/pool.h>
     43        1.2        ad #include <sys/proc.h>
     44        1.2        ad #include <sys/types.h>
     45        1.2        ad #include <sys/syscallargs.h>
     46        1.2        ad #include <sys/kauth.h>
     47        1.2        ad #include <sys/kmem.h>
     48        1.2        ad #include <sys/sleepq.h>
     49       1.30        ad #include <sys/lwpctl.h>
     50       1.45        ad #include <sys/cpu.h>
     51        1.2        ad 
     52        1.2        ad #include <uvm/uvm_extern.h>
     53        1.2        ad 
     54        1.2        ad #define	LWP_UNPARK_MAX		1024
     55        1.2        ad 
     56       1.47     rmind static syncobj_t lwp_park_sobj = {
     57       1.26        ad 	SOBJ_SLEEPQ_LIFO,
     58        1.2        ad 	sleepq_unsleep,
     59        1.7      yamt 	sleepq_changepri,
     60        1.7      yamt 	sleepq_lendpri,
     61        1.7      yamt 	syncobj_noowner,
     62        1.2        ad };
     63        1.2        ad 
     64       1.47     rmind static sleeptab_t	lwp_park_tab;
     65        1.2        ad 
     66        1.2        ad void
     67        1.2        ad lwp_sys_init(void)
     68        1.2        ad {
     69        1.2        ad 	sleeptab_init(&lwp_park_tab);
     70        1.2        ad }
     71        1.2        ad 
     72        1.2        ad int
     73  1.56.12.3     skrll do_lwp_create(lwp_t *l, void *arg, u_long flags, lwpid_t *new_lwp,
     74  1.56.12.3     skrll     const sigset_t *sigmask, const stack_t *sigstk)
     75        1.2        ad {
     76        1.2        ad 	struct proc *p = l->l_proc;
     77        1.2        ad 	struct lwp *l2;
     78       1.50     skrll 	struct schedstate_percpu *spc;
     79        1.2        ad 	vaddr_t uaddr;
     80       1.54    martin 	int error;
     81        1.2        ad 
     82        1.2        ad 	/* XXX check against resource limits */
     83        1.2        ad 
     84       1.46     rmind 	uaddr = uvm_uarea_alloc();
     85       1.54    martin 	if (__predict_false(uaddr == 0))
     86        1.2        ad 		return ENOMEM;
     87        1.2        ad 
     88  1.56.12.3     skrll 	error = lwp_create(l, p, uaddr, flags & LWP_DETACHED, NULL, 0,
     89  1.56.12.3     skrll 	    p->p_emul->e_startlwp, arg, &l2, l->l_class, sigmask, &SS_INIT);
     90       1.46     rmind 	if (__predict_false(error)) {
     91       1.46     rmind 		uvm_uarea_free(uaddr);
     92       1.18     rmind 		return error;
     93       1.18     rmind 	}
     94        1.2        ad 
     95       1.54    martin 	*new_lwp = l2->l_lid;
     96       1.21     rmind 
     97        1.2        ad 	/*
     98        1.2        ad 	 * Set the new LWP running, unless the caller has requested that
     99        1.2        ad 	 * it be created in suspended state.  If the process is stopping,
    100        1.2        ad 	 * then the LWP is created stopped.
    101        1.2        ad 	 */
    102       1.39        ad 	mutex_enter(p->p_lock);
    103        1.2        ad 	lwp_lock(l2);
    104       1.50     skrll 	spc = &l2->l_cpu->ci_schedstate;
    105       1.54    martin 	if ((flags & LWP_SUSPENDED) == 0 &&
    106        1.4     pavel 	    (l->l_flag & (LW_WREBOOT | LW_WSUSPEND | LW_WEXIT)) == 0) {
    107       1.50     skrll 	    	if (p->p_stat == SSTOP || (p->p_sflag & PS_STOPPING) != 0) {
    108       1.50     skrll 			KASSERT(l2->l_wchan == NULL);
    109        1.2        ad 	    		l2->l_stat = LSSTOP;
    110       1.51      yamt 			p->p_nrlwps--;
    111       1.50     skrll 			lwp_unlock_to(l2, spc->spc_lwplock);
    112       1.50     skrll 		} else {
    113       1.50     skrll 			KASSERT(lwp_locked(l2, spc->spc_mutex));
    114        1.2        ad 			l2->l_stat = LSRUN;
    115       1.19      yamt 			sched_enqueue(l2, false);
    116       1.50     skrll 			lwp_unlock(l2);
    117        1.2        ad 		}
    118       1.31        ad 	} else {
    119        1.2        ad 		l2->l_stat = LSSUSPENDED;
    120       1.51      yamt 		p->p_nrlwps--;
    121       1.50     skrll 		lwp_unlock_to(l2, spc->spc_lwplock);
    122       1.31        ad 	}
    123       1.39        ad 	mutex_exit(p->p_lock);
    124        1.2        ad 
    125        1.2        ad 	return 0;
    126        1.2        ad }
    127        1.2        ad 
    128        1.2        ad int
    129       1.54    martin sys__lwp_create(struct lwp *l, const struct sys__lwp_create_args *uap,
    130       1.54    martin     register_t *retval)
    131       1.54    martin {
    132       1.54    martin 	/* {
    133       1.54    martin 		syscallarg(const ucontext_t *) ucp;
    134       1.54    martin 		syscallarg(u_long) flags;
    135       1.54    martin 		syscallarg(lwpid_t *) new_lwp;
    136       1.54    martin 	} */
    137       1.54    martin 	struct proc *p = l->l_proc;
    138  1.56.12.1     skrll 	ucontext_t *newuc;
    139       1.54    martin 	lwpid_t lid;
    140       1.54    martin 	int error;
    141       1.54    martin 
    142       1.54    martin 	newuc = kmem_alloc(sizeof(ucontext_t), KM_SLEEP);
    143       1.54    martin 	error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize);
    144       1.54    martin 	if (error)
    145       1.54    martin 		goto fail;
    146       1.54    martin 
    147       1.54    martin 	/* validate the ucontext */
    148       1.54    martin 	if ((newuc->uc_flags & _UC_CPU) == 0) {
    149       1.54    martin 		error = EINVAL;
    150       1.54    martin 		goto fail;
    151       1.54    martin 	}
    152       1.54    martin 	error = cpu_mcontext_validate(l, &newuc->uc_mcontext);
    153       1.54    martin 	if (error)
    154       1.54    martin 		goto fail;
    155       1.54    martin 
    156  1.56.12.3     skrll 	const sigset_t *sigmask = newuc->uc_flags & _UC_SIGMASK ?
    157  1.56.12.3     skrll 	    &newuc->uc_sigmask : &l->l_sigmask;
    158  1.56.12.3     skrll 	error = do_lwp_create(l, newuc, SCARG(uap, flags), &lid, sigmask,
    159  1.56.12.3     skrll 	    &SS_INIT);
    160       1.54    martin 	if (error)
    161       1.54    martin 		goto fail;
    162       1.54    martin 
    163       1.54    martin 	/*
    164       1.54    martin 	 * do not free ucontext in case of an error here,
    165       1.54    martin 	 * the lwp will actually run and access it
    166       1.54    martin 	 */
    167       1.54    martin 	return copyout(&lid, SCARG(uap, new_lwp), sizeof(lid));
    168       1.54    martin 
    169       1.54    martin fail:
    170       1.54    martin 	kmem_free(newuc, sizeof(ucontext_t));
    171       1.54    martin 	return error;
    172       1.54    martin }
    173       1.54    martin 
    174       1.54    martin int
    175       1.32       dsl sys__lwp_exit(struct lwp *l, const void *v, register_t *retval)
    176        1.2        ad {
    177        1.2        ad 
    178        1.2        ad 	lwp_exit(l);
    179        1.2        ad 	return 0;
    180        1.2        ad }
    181        1.2        ad 
    182        1.2        ad int
    183       1.32       dsl sys__lwp_self(struct lwp *l, const void *v, register_t *retval)
    184        1.2        ad {
    185        1.2        ad 
    186        1.2        ad 	*retval = l->l_lid;
    187        1.2        ad 	return 0;
    188        1.2        ad }
    189        1.2        ad 
    190        1.2        ad int
    191       1.32       dsl sys__lwp_getprivate(struct lwp *l, const void *v, register_t *retval)
    192        1.2        ad {
    193        1.2        ad 
    194        1.2        ad 	*retval = (uintptr_t)l->l_private;
    195        1.2        ad 	return 0;
    196        1.2        ad }
    197        1.2        ad 
    198        1.2        ad int
    199       1.47     rmind sys__lwp_setprivate(struct lwp *l, const struct sys__lwp_setprivate_args *uap,
    200       1.47     rmind     register_t *retval)
    201        1.2        ad {
    202       1.32       dsl 	/* {
    203        1.2        ad 		syscallarg(void *) ptr;
    204       1.32       dsl 	} */
    205        1.2        ad 
    206       1.52       chs 	return lwp_setprivate(l, SCARG(uap, ptr));
    207        1.2        ad }
    208        1.2        ad 
    209        1.2        ad int
    210       1.47     rmind sys__lwp_suspend(struct lwp *l, const struct sys__lwp_suspend_args *uap,
    211       1.47     rmind     register_t *retval)
    212        1.2        ad {
    213       1.32       dsl 	/* {
    214        1.2        ad 		syscallarg(lwpid_t) target;
    215       1.32       dsl 	} */
    216        1.2        ad 	struct proc *p = l->l_proc;
    217        1.2        ad 	struct lwp *t;
    218        1.2        ad 	int error;
    219        1.2        ad 
    220       1.39        ad 	mutex_enter(p->p_lock);
    221        1.2        ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    222       1.39        ad 		mutex_exit(p->p_lock);
    223        1.2        ad 		return ESRCH;
    224        1.2        ad 	}
    225        1.2        ad 
    226        1.2        ad 	/*
    227        1.2        ad 	 * Check for deadlock, which is only possible when we're suspending
    228        1.2        ad 	 * ourself.  XXX There is a short race here, as p_nrlwps is only
    229        1.2        ad 	 * incremented when an LWP suspends itself on the kernel/user
    230        1.2        ad 	 * boundary.  It's still possible to kill -9 the process so we
    231        1.2        ad 	 * don't bother checking further.
    232        1.2        ad 	 */
    233        1.2        ad 	lwp_lock(t);
    234        1.2        ad 	if ((t == l && p->p_nrlwps == 1) ||
    235        1.4     pavel 	    (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
    236        1.2        ad 		lwp_unlock(t);
    237       1.39        ad 		mutex_exit(p->p_lock);
    238        1.2        ad 		return EDEADLK;
    239        1.2        ad 	}
    240        1.2        ad 
    241        1.2        ad 	/*
    242        1.2        ad 	 * Suspend the LWP.  XXX If it's on a different CPU, we should wait
    243  1.56.12.2     skrll 	 * for it to be preempted, where it will put itself to sleep.
    244        1.2        ad 	 *
    245        1.2        ad 	 * Suspension of the current LWP will happen on return to userspace.
    246        1.2        ad 	 */
    247        1.2        ad 	error = lwp_suspend(l, t);
    248       1.23     rmind 	if (error) {
    249       1.39        ad 		mutex_exit(p->p_lock);
    250       1.23     rmind 		return error;
    251       1.23     rmind 	}
    252       1.23     rmind 
    253       1.23     rmind 	/*
    254       1.23     rmind 	 * Wait for:
    255       1.23     rmind 	 *  o process exiting
    256       1.23     rmind 	 *  o target LWP suspended
    257       1.23     rmind 	 *  o target LWP not suspended and L_WSUSPEND clear
    258       1.23     rmind 	 *  o target LWP exited
    259       1.23     rmind 	 */
    260       1.23     rmind 	for (;;) {
    261       1.39        ad 		error = cv_wait_sig(&p->p_lwpcv, p->p_lock);
    262       1.23     rmind 		if (error) {
    263       1.23     rmind 			error = ERESTART;
    264       1.23     rmind 			break;
    265       1.23     rmind 		}
    266       1.25     rmind 		if (lwp_find(p, SCARG(uap, target)) == NULL) {
    267       1.25     rmind 			error = ESRCH;
    268       1.25     rmind 			break;
    269       1.25     rmind 		}
    270       1.23     rmind 		if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
    271       1.23     rmind 			error = ERESTART;
    272       1.23     rmind 			break;
    273       1.23     rmind 		}
    274       1.23     rmind 		if (t->l_stat == LSSUSPENDED ||
    275       1.23     rmind 		    (t->l_flag & LW_WSUSPEND) == 0)
    276       1.23     rmind 			break;
    277       1.23     rmind 	}
    278       1.39        ad 	mutex_exit(p->p_lock);
    279        1.2        ad 
    280        1.2        ad 	return error;
    281        1.2        ad }
    282        1.2        ad 
    283        1.2        ad int
    284       1.47     rmind sys__lwp_continue(struct lwp *l, const struct sys__lwp_continue_args *uap,
    285       1.47     rmind     register_t *retval)
    286        1.2        ad {
    287       1.32       dsl 	/* {
    288        1.2        ad 		syscallarg(lwpid_t) target;
    289       1.32       dsl 	} */
    290        1.2        ad 	int error;
    291        1.2        ad 	struct proc *p = l->l_proc;
    292        1.2        ad 	struct lwp *t;
    293        1.2        ad 
    294        1.2        ad 	error = 0;
    295        1.2        ad 
    296       1.39        ad 	mutex_enter(p->p_lock);
    297        1.2        ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    298       1.39        ad 		mutex_exit(p->p_lock);
    299        1.2        ad 		return ESRCH;
    300        1.2        ad 	}
    301        1.2        ad 
    302        1.2        ad 	lwp_lock(t);
    303        1.2        ad 	lwp_continue(t);
    304       1.39        ad 	mutex_exit(p->p_lock);
    305        1.2        ad 
    306        1.2        ad 	return error;
    307        1.2        ad }
    308        1.2        ad 
    309        1.2        ad int
    310       1.47     rmind sys__lwp_wakeup(struct lwp *l, const struct sys__lwp_wakeup_args *uap,
    311       1.47     rmind     register_t *retval)
    312        1.2        ad {
    313       1.32       dsl 	/* {
    314        1.2        ad 		syscallarg(lwpid_t) target;
    315       1.32       dsl 	} */
    316        1.2        ad 	struct lwp *t;
    317        1.2        ad 	struct proc *p;
    318        1.2        ad 	int error;
    319        1.2        ad 
    320        1.2        ad 	p = l->l_proc;
    321       1.39        ad 	mutex_enter(p->p_lock);
    322        1.2        ad 
    323        1.2        ad 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    324       1.39        ad 		mutex_exit(p->p_lock);
    325        1.2        ad 		return ESRCH;
    326        1.2        ad 	}
    327        1.2        ad 
    328        1.2        ad 	lwp_lock(t);
    329       1.15        ad 	t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
    330        1.2        ad 
    331        1.2        ad 	if (t->l_stat != LSSLEEP) {
    332       1.16        ad 		lwp_unlock(t);
    333        1.2        ad 		error = ENODEV;
    334       1.16        ad 	} else if ((t->l_flag & LW_SINTR) == 0) {
    335       1.16        ad 		lwp_unlock(t);
    336        1.2        ad 		error = EBUSY;
    337       1.16        ad 	} else {
    338       1.16        ad 		/* Wake it up.  lwp_unsleep() will release the LWP lock. */
    339       1.46     rmind 		lwp_unsleep(t, true);
    340       1.16        ad 		error = 0;
    341        1.2        ad 	}
    342        1.2        ad 
    343       1.39        ad 	mutex_exit(p->p_lock);
    344        1.2        ad 
    345        1.2        ad 	return error;
    346        1.2        ad }
    347        1.2        ad 
    348        1.2        ad int
    349       1.47     rmind sys__lwp_wait(struct lwp *l, const struct sys__lwp_wait_args *uap,
    350       1.47     rmind     register_t *retval)
    351        1.2        ad {
    352       1.32       dsl 	/* {
    353        1.2        ad 		syscallarg(lwpid_t) wait_for;
    354        1.2        ad 		syscallarg(lwpid_t *) departed;
    355       1.32       dsl 	} */
    356        1.2        ad 	struct proc *p = l->l_proc;
    357        1.2        ad 	int error;
    358        1.2        ad 	lwpid_t dep;
    359        1.2        ad 
    360       1.39        ad 	mutex_enter(p->p_lock);
    361       1.55     rmind 	error = lwp_wait(l, SCARG(uap, wait_for), &dep, false);
    362       1.39        ad 	mutex_exit(p->p_lock);
    363        1.2        ad 
    364       1.55     rmind 	if (!error && SCARG(uap, departed)) {
    365        1.2        ad 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    366        1.2        ad 	}
    367        1.2        ad 
    368       1.55     rmind 	return error;
    369        1.2        ad }
    370        1.2        ad 
    371        1.2        ad int
    372       1.47     rmind sys__lwp_kill(struct lwp *l, const struct sys__lwp_kill_args *uap,
    373       1.47     rmind     register_t *retval)
    374        1.2        ad {
    375       1.32       dsl 	/* {
    376        1.2        ad 		syscallarg(lwpid_t)	target;
    377        1.2        ad 		syscallarg(int)		signo;
    378       1.32       dsl 	} */
    379        1.2        ad 	struct proc *p = l->l_proc;
    380        1.2        ad 	struct lwp *t;
    381        1.2        ad 	ksiginfo_t ksi;
    382        1.2        ad 	int signo = SCARG(uap, signo);
    383        1.2        ad 	int error = 0;
    384        1.2        ad 
    385        1.2        ad 	if ((u_int)signo >= NSIG)
    386        1.2        ad 		return EINVAL;
    387        1.2        ad 
    388        1.2        ad 	KSI_INIT(&ksi);
    389        1.2        ad 	ksi.ksi_signo = signo;
    390       1.43        ad 	ksi.ksi_code = SI_LWP;
    391        1.2        ad 	ksi.ksi_pid = p->p_pid;
    392        1.2        ad 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    393        1.2        ad 	ksi.ksi_lid = SCARG(uap, target);
    394        1.2        ad 
    395       1.38        ad 	mutex_enter(proc_lock);
    396       1.39        ad 	mutex_enter(p->p_lock);
    397        1.2        ad 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    398        1.2        ad 		error = ESRCH;
    399        1.2        ad 	else if (signo != 0)
    400        1.2        ad 		kpsignal2(p, &ksi);
    401       1.39        ad 	mutex_exit(p->p_lock);
    402       1.38        ad 	mutex_exit(proc_lock);
    403        1.2        ad 
    404        1.2        ad 	return error;
    405        1.2        ad }
    406        1.2        ad 
    407        1.2        ad int
    408       1.47     rmind sys__lwp_detach(struct lwp *l, const struct sys__lwp_detach_args *uap,
    409       1.47     rmind     register_t *retval)
    410        1.2        ad {
    411       1.32       dsl 	/* {
    412        1.2        ad 		syscallarg(lwpid_t)	target;
    413       1.32       dsl 	} */
    414        1.2        ad 	struct proc *p;
    415        1.2        ad 	struct lwp *t;
    416        1.2        ad 	lwpid_t target;
    417        1.2        ad 	int error;
    418        1.2        ad 
    419        1.2        ad 	target = SCARG(uap, target);
    420        1.2        ad 	p = l->l_proc;
    421        1.2        ad 
    422       1.39        ad 	mutex_enter(p->p_lock);
    423        1.2        ad 
    424        1.2        ad 	if (l->l_lid == target)
    425        1.2        ad 		t = l;
    426        1.2        ad 	else {
    427        1.2        ad 		/*
    428        1.2        ad 		 * We can't use lwp_find() here because the target might
    429        1.2        ad 		 * be a zombie.
    430        1.2        ad 		 */
    431        1.2        ad 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    432        1.2        ad 			if (t->l_lid == target)
    433        1.2        ad 				break;
    434        1.2        ad 	}
    435        1.2        ad 
    436        1.2        ad 	/*
    437        1.2        ad 	 * If the LWP is already detached, there's nothing to do.
    438        1.2        ad 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    439        1.2        ad 	 * is visible with the proc mutex held.
    440        1.2        ad 	 *
    441        1.2        ad 	 * After we have detached or released the LWP, kick any
    442        1.2        ad 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    443        1.2        ad 	 * for the target LWP to exit.
    444        1.2        ad 	 */
    445        1.2        ad 	if (t != NULL && t->l_stat != LSIDL) {
    446        1.2        ad 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    447        1.2        ad 			p->p_ndlwps++;
    448        1.2        ad 			t->l_prflag |= LPR_DETACHED;
    449        1.2        ad 			if (t->l_stat == LSZOMB) {
    450       1.17        ad 				/* Releases proc mutex. */
    451       1.17        ad 				lwp_free(t, false, false);
    452        1.2        ad 				return 0;
    453        1.2        ad 			}
    454        1.2        ad 			error = 0;
    455       1.17        ad 
    456       1.17        ad 			/*
    457       1.17        ad 			 * Have any LWPs sleeping in lwp_wait() recheck
    458       1.17        ad 			 * for deadlock.
    459       1.17        ad 			 */
    460       1.17        ad 			cv_broadcast(&p->p_lwpcv);
    461        1.2        ad 		} else
    462        1.2        ad 			error = EINVAL;
    463        1.2        ad 	} else
    464        1.2        ad 		error = ESRCH;
    465        1.2        ad 
    466       1.39        ad 	mutex_exit(p->p_lock);
    467        1.2        ad 
    468        1.2        ad 	return error;
    469        1.2        ad }
    470        1.2        ad 
    471        1.2        ad static inline wchan_t
    472        1.2        ad lwp_park_wchan(struct proc *p, const void *hint)
    473        1.2        ad {
    474       1.22        ad 
    475        1.2        ad 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    476        1.2        ad }
    477        1.2        ad 
    478        1.2        ad int
    479       1.24        ad lwp_unpark(lwpid_t target, const void *hint)
    480        1.2        ad {
    481       1.24        ad 	sleepq_t *sq;
    482       1.24        ad 	wchan_t wchan;
    483       1.41        ad 	kmutex_t *mp;
    484       1.24        ad 	proc_t *p;
    485       1.24        ad 	lwp_t *t;
    486       1.24        ad 
    487       1.24        ad 	/*
    488       1.24        ad 	 * Easy case: search for the LWP on the sleep queue.  If
    489       1.24        ad 	 * it's parked, remove it from the queue and set running.
    490       1.24        ad 	 */
    491       1.24        ad 	p = curproc;
    492       1.24        ad 	wchan = lwp_park_wchan(p, hint);
    493       1.41        ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp);
    494       1.24        ad 
    495       1.41        ad 	TAILQ_FOREACH(t, sq, l_sleepchain)
    496       1.24        ad 		if (t->l_proc == p && t->l_lid == target)
    497       1.24        ad 			break;
    498       1.24        ad 
    499       1.24        ad 	if (__predict_true(t != NULL)) {
    500       1.46     rmind 		sleepq_remove(sq, t);
    501       1.41        ad 		mutex_spin_exit(mp);
    502       1.24        ad 		return 0;
    503       1.24        ad 	}
    504       1.24        ad 
    505       1.24        ad 	/*
    506       1.24        ad 	 * The LWP hasn't parked yet.  Take the hit and mark the
    507       1.24        ad 	 * operation as pending.
    508       1.24        ad 	 */
    509       1.41        ad 	mutex_spin_exit(mp);
    510       1.20       dsl 
    511       1.39        ad 	mutex_enter(p->p_lock);
    512       1.24        ad 	if ((t = lwp_find(p, target)) == NULL) {
    513       1.39        ad 		mutex_exit(p->p_lock);
    514       1.24        ad 		return ESRCH;
    515       1.24        ad 	}
    516       1.20       dsl 
    517       1.24        ad 	/*
    518       1.24        ad 	 * It may not have parked yet, we may have raced, or it
    519       1.24        ad 	 * is parked on a different user sync object.
    520       1.24        ad 	 */
    521       1.24        ad 	lwp_lock(t);
    522       1.24        ad 	if (t->l_syncobj == &lwp_park_sobj) {
    523       1.24        ad 		/* Releases the LWP lock. */
    524       1.46     rmind 		lwp_unsleep(t, true);
    525       1.24        ad 	} else {
    526       1.24        ad 		/*
    527       1.24        ad 		 * Set the operation pending.  The next call to _lwp_park
    528       1.24        ad 		 * will return early.
    529       1.24        ad 		 */
    530       1.24        ad 		t->l_flag |= LW_UNPARKED;
    531       1.24        ad 		lwp_unlock(t);
    532       1.24        ad 	}
    533       1.20       dsl 
    534       1.39        ad 	mutex_exit(p->p_lock);
    535       1.24        ad 	return 0;
    536       1.20       dsl }
    537       1.20       dsl 
    538       1.20       dsl int
    539       1.56  christos lwp_park(clockid_t clock_id, int flags, struct timespec *ts, const void *hint)
    540       1.20       dsl {
    541        1.2        ad 	sleepq_t *sq;
    542       1.41        ad 	kmutex_t *mp;
    543        1.2        ad 	wchan_t wchan;
    544        1.2        ad 	int timo, error;
    545       1.24        ad 	lwp_t *l;
    546        1.2        ad 
    547       1.20       dsl 	if (ts != NULL) {
    548       1.56  christos 		if ((error = ts2timo(clock_id, flags, ts, &timo, NULL)) != 0)
    549        1.2        ad 			return error;
    550       1.24        ad 		KASSERT(timo != 0);
    551       1.48     rmind 	} else {
    552        1.2        ad 		timo = 0;
    553       1.48     rmind 	}
    554        1.2        ad 
    555        1.2        ad 	/* Find and lock the sleep queue. */
    556       1.24        ad 	l = curlwp;
    557       1.20       dsl 	wchan = lwp_park_wchan(l->l_proc, hint);
    558       1.41        ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp);
    559        1.2        ad 
    560        1.2        ad 	/*
    561        1.2        ad 	 * Before going the full route and blocking, check to see if an
    562        1.2        ad 	 * unpark op is pending.
    563        1.2        ad 	 */
    564       1.19      yamt 	lwp_lock(l);
    565        1.8        ad 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    566        1.8        ad 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    567       1.19      yamt 		lwp_unlock(l);
    568       1.41        ad 		mutex_spin_exit(mp);
    569        1.2        ad 		return EALREADY;
    570        1.2        ad 	}
    571       1.41        ad 	lwp_unlock_to(l, mp);
    572       1.24        ad 	l->l_biglocks = 0;
    573       1.27        ad 	sleepq_enqueue(sq, wchan, "parked", &lwp_park_sobj);
    574       1.19      yamt 	error = sleepq_block(timo, true);
    575       1.13      yamt 	switch (error) {
    576       1.14      yamt 	case EWOULDBLOCK:
    577       1.14      yamt 		error = ETIMEDOUT;
    578       1.14      yamt 		break;
    579       1.14      yamt 	case ERESTART:
    580       1.14      yamt 		error = EINTR;
    581       1.14      yamt 		break;
    582       1.14      yamt 	default:
    583       1.14      yamt 		/* nothing */
    584       1.14      yamt 		break;
    585       1.13      yamt 	}
    586       1.13      yamt 	return error;
    587        1.2        ad }
    588        1.2        ad 
    589       1.24        ad /*
    590       1.24        ad  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    591       1.24        ad  * will remain parked until another LWP in the same process calls in and
    592       1.24        ad  * requests that it be unparked.
    593       1.24        ad  */
    594        1.2        ad int
    595       1.56  christos sys____lwp_park60(struct lwp *l, const struct sys____lwp_park60_args *uap,
    596       1.44  christos     register_t *retval)
    597        1.2        ad {
    598       1.32       dsl 	/* {
    599       1.56  christos 		syscallarg(clockid_t)			clock_id;
    600       1.56  christos 		syscallarg(int)				flags;
    601       1.24        ad 		syscallarg(const struct timespec *)	ts;
    602       1.24        ad 		syscallarg(lwpid_t)			unpark;
    603       1.24        ad 		syscallarg(const void *)		hint;
    604       1.24        ad 		syscallarg(const void *)		unparkhint;
    605       1.32       dsl 	} */
    606       1.24        ad 	struct timespec ts, *tsp;
    607       1.24        ad 	int error;
    608        1.2        ad 
    609       1.24        ad 	if (SCARG(uap, ts) == NULL)
    610       1.24        ad 		tsp = NULL;
    611       1.24        ad 	else {
    612       1.24        ad 		error = copyin(SCARG(uap, ts), &ts, sizeof(ts));
    613       1.24        ad 		if (error != 0)
    614       1.24        ad 			return error;
    615       1.24        ad 		tsp = &ts;
    616       1.24        ad 	}
    617        1.2        ad 
    618       1.24        ad 	if (SCARG(uap, unpark) != 0) {
    619       1.24        ad 		error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
    620       1.24        ad 		if (error != 0)
    621       1.24        ad 			return error;
    622       1.15        ad 	}
    623       1.15        ad 
    624       1.56  christos 	return lwp_park(SCARG(uap, clock_id), SCARG(uap, flags), tsp,
    625       1.56  christos 	    SCARG(uap, hint));
    626       1.24        ad }
    627        1.2        ad 
    628       1.24        ad int
    629       1.47     rmind sys__lwp_unpark(struct lwp *l, const struct sys__lwp_unpark_args *uap,
    630       1.47     rmind     register_t *retval)
    631       1.24        ad {
    632       1.32       dsl 	/* {
    633       1.24        ad 		syscallarg(lwpid_t)		target;
    634       1.24        ad 		syscallarg(const void *)	hint;
    635       1.32       dsl 	} */
    636        1.2        ad 
    637       1.24        ad 	return lwp_unpark(SCARG(uap, target), SCARG(uap, hint));
    638        1.2        ad }
    639        1.2        ad 
    640        1.2        ad int
    641       1.47     rmind sys__lwp_unpark_all(struct lwp *l, const struct sys__lwp_unpark_all_args *uap,
    642       1.47     rmind     register_t *retval)
    643        1.2        ad {
    644       1.32       dsl 	/* {
    645        1.2        ad 		syscallarg(const lwpid_t *)	targets;
    646        1.2        ad 		syscallarg(size_t)		ntargets;
    647        1.2        ad 		syscallarg(const void *)	hint;
    648       1.32       dsl 	} */
    649        1.2        ad 	struct proc *p;
    650        1.2        ad 	struct lwp *t;
    651        1.2        ad 	sleepq_t *sq;
    652        1.2        ad 	wchan_t wchan;
    653        1.2        ad 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    654       1.46     rmind 	int error;
    655       1.41        ad 	kmutex_t *mp;
    656       1.15        ad 	u_int ntargets;
    657        1.2        ad 	size_t sz;
    658        1.2        ad 
    659        1.2        ad 	p = l->l_proc;
    660        1.2        ad 	ntargets = SCARG(uap, ntargets);
    661        1.2        ad 
    662        1.2        ad 	if (SCARG(uap, targets) == NULL) {
    663        1.2        ad 		/*
    664        1.2        ad 		 * Let the caller know how much we are willing to do, and
    665        1.2        ad 		 * let it unpark the LWPs in blocks.
    666        1.2        ad 		 */
    667        1.2        ad 		*retval = LWP_UNPARK_MAX;
    668        1.2        ad 		return 0;
    669        1.2        ad 	}
    670        1.2        ad 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    671        1.2        ad 		return EINVAL;
    672        1.2        ad 
    673        1.2        ad 	/*
    674        1.2        ad 	 * Copy in the target array.  If it's a small number of LWPs, then
    675        1.2        ad 	 * place the numbers on the stack.
    676        1.2        ad 	 */
    677        1.2        ad 	sz = sizeof(target) * ntargets;
    678        1.2        ad 	if (sz <= sizeof(targets))
    679        1.2        ad 		tp = targets;
    680  1.56.12.3     skrll 	else
    681        1.2        ad 		tp = kmem_alloc(sz, KM_SLEEP);
    682        1.2        ad 	error = copyin(SCARG(uap, targets), tp, sz);
    683        1.2        ad 	if (error != 0) {
    684        1.2        ad 		if (tp != targets) {
    685        1.2        ad 			kmem_free(tp, sz);
    686        1.2        ad 		}
    687        1.2        ad 		return error;
    688        1.2        ad 	}
    689        1.2        ad 
    690        1.2        ad 	wchan = lwp_park_wchan(p, SCARG(uap, hint));
    691       1.41        ad 	sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp);
    692        1.2        ad 
    693        1.2        ad 	for (tmax = tp + ntargets, tpp = tp; tpp < tmax; tpp++) {
    694        1.2        ad 		target = *tpp;
    695        1.2        ad 
    696        1.2        ad 		/*
    697        1.2        ad 		 * Easy case: search for the LWP on the sleep queue.  If
    698        1.2        ad 		 * it's parked, remove it from the queue and set running.
    699        1.2        ad 		 */
    700       1.41        ad 		TAILQ_FOREACH(t, sq, l_sleepchain)
    701        1.2        ad 			if (t->l_proc == p && t->l_lid == target)
    702        1.2        ad 				break;
    703        1.2        ad 
    704        1.2        ad 		if (t != NULL) {
    705       1.46     rmind 			sleepq_remove(sq, t);
    706        1.2        ad 			continue;
    707        1.2        ad 		}
    708        1.2        ad 
    709        1.2        ad 		/*
    710        1.2        ad 		 * The LWP hasn't parked yet.  Take the hit and
    711        1.2        ad 		 * mark the operation as pending.
    712        1.2        ad 		 */
    713       1.41        ad 		mutex_spin_exit(mp);
    714       1.39        ad 		mutex_enter(p->p_lock);
    715        1.2        ad 		if ((t = lwp_find(p, target)) == NULL) {
    716       1.39        ad 			mutex_exit(p->p_lock);
    717       1.41        ad 			mutex_spin_enter(mp);
    718        1.2        ad 			continue;
    719        1.2        ad 		}
    720        1.2        ad 		lwp_lock(t);
    721        1.2        ad 
    722       1.15        ad 		/*
    723       1.15        ad 		 * It may not have parked yet, we may have raced, or
    724       1.15        ad 		 * it is parked on a different user sync object.
    725       1.15        ad 		 */
    726       1.15        ad 		if (t->l_syncobj == &lwp_park_sobj) {
    727       1.15        ad 			/* Releases the LWP lock. */
    728       1.46     rmind 			lwp_unsleep(t, true);
    729        1.2        ad 		} else {
    730        1.2        ad 			/*
    731       1.15        ad 			 * Set the operation pending.  The next call to
    732       1.15        ad 			 * _lwp_park will return early.
    733        1.2        ad 			 */
    734        1.8        ad 			t->l_flag |= LW_UNPARKED;
    735        1.2        ad 			lwp_unlock(t);
    736        1.2        ad 		}
    737       1.15        ad 
    738       1.39        ad 		mutex_exit(p->p_lock);
    739       1.41        ad 		mutex_spin_enter(mp);
    740        1.2        ad 	}
    741        1.2        ad 
    742       1.41        ad 	mutex_spin_exit(mp);
    743       1.33        ad 	if (tp != targets)
    744        1.2        ad 		kmem_free(tp, sz);
    745       1.15        ad 
    746        1.2        ad 	return 0;
    747        1.2        ad }
    748       1.28        ad 
    749       1.28        ad int
    750       1.47     rmind sys__lwp_setname(struct lwp *l, const struct sys__lwp_setname_args *uap,
    751       1.47     rmind     register_t *retval)
    752       1.28        ad {
    753       1.32       dsl 	/* {
    754       1.28        ad 		syscallarg(lwpid_t)		target;
    755       1.28        ad 		syscallarg(const char *)	name;
    756       1.32       dsl 	} */
    757       1.28        ad 	char *name, *oname;
    758       1.30        ad 	lwpid_t target;
    759       1.28        ad 	proc_t *p;
    760       1.28        ad 	lwp_t *t;
    761       1.28        ad 	int error;
    762       1.28        ad 
    763       1.30        ad 	if ((target = SCARG(uap, target)) == 0)
    764       1.30        ad 		target = l->l_lid;
    765       1.30        ad 
    766       1.28        ad 	name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
    767       1.28        ad 	error = copyinstr(SCARG(uap, name), name, MAXCOMLEN, NULL);
    768       1.28        ad 	switch (error) {
    769       1.28        ad 	case ENAMETOOLONG:
    770       1.28        ad 	case 0:
    771       1.28        ad 		name[MAXCOMLEN - 1] = '\0';
    772       1.28        ad 		break;
    773       1.28        ad 	default:
    774       1.28        ad 		kmem_free(name, MAXCOMLEN);
    775       1.28        ad 		return error;
    776       1.28        ad 	}
    777       1.28        ad 
    778       1.28        ad 	p = curproc;
    779       1.39        ad 	mutex_enter(p->p_lock);
    780       1.30        ad 	if ((t = lwp_find(p, target)) == NULL) {
    781       1.39        ad 		mutex_exit(p->p_lock);
    782       1.28        ad 		kmem_free(name, MAXCOMLEN);
    783       1.28        ad 		return ESRCH;
    784       1.28        ad 	}
    785       1.28        ad 	lwp_lock(t);
    786       1.28        ad 	oname = t->l_name;
    787       1.28        ad 	t->l_name = name;
    788       1.28        ad 	lwp_unlock(t);
    789       1.39        ad 	mutex_exit(p->p_lock);
    790       1.28        ad 
    791       1.28        ad 	if (oname != NULL)
    792       1.28        ad 		kmem_free(oname, MAXCOMLEN);
    793       1.28        ad 
    794       1.28        ad 	return 0;
    795       1.28        ad }
    796       1.28        ad 
    797       1.28        ad int
    798       1.47     rmind sys__lwp_getname(struct lwp *l, const struct sys__lwp_getname_args *uap,
    799       1.47     rmind     register_t *retval)
    800       1.28        ad {
    801       1.32       dsl 	/* {
    802       1.28        ad 		syscallarg(lwpid_t)		target;
    803       1.28        ad 		syscallarg(char *)		name;
    804       1.28        ad 		syscallarg(size_t)		len;
    805       1.32       dsl 	} */
    806       1.28        ad 	char name[MAXCOMLEN];
    807       1.30        ad 	lwpid_t target;
    808       1.28        ad 	proc_t *p;
    809       1.28        ad 	lwp_t *t;
    810       1.28        ad 
    811       1.30        ad 	if ((target = SCARG(uap, target)) == 0)
    812       1.30        ad 		target = l->l_lid;
    813       1.30        ad 
    814       1.28        ad 	p = curproc;
    815       1.39        ad 	mutex_enter(p->p_lock);
    816       1.30        ad 	if ((t = lwp_find(p, target)) == NULL) {
    817       1.39        ad 		mutex_exit(p->p_lock);
    818       1.28        ad 		return ESRCH;
    819       1.28        ad 	}
    820       1.28        ad 	lwp_lock(t);
    821       1.28        ad 	if (t->l_name == NULL)
    822       1.28        ad 		name[0] = '\0';
    823       1.28        ad 	else
    824  1.56.12.2     skrll 		strlcpy(name, t->l_name, sizeof(name));
    825       1.28        ad 	lwp_unlock(t);
    826       1.39        ad 	mutex_exit(p->p_lock);
    827       1.28        ad 
    828       1.28        ad 	return copyoutstr(name, SCARG(uap, name), SCARG(uap, len), NULL);
    829       1.28        ad }
    830       1.30        ad 
    831       1.30        ad int
    832       1.47     rmind sys__lwp_ctl(struct lwp *l, const struct sys__lwp_ctl_args *uap,
    833       1.47     rmind     register_t *retval)
    834       1.30        ad {
    835       1.32       dsl 	/* {
    836       1.30        ad 		syscallarg(int)			features;
    837       1.30        ad 		syscallarg(struct lwpctl **)	address;
    838       1.32       dsl 	} */
    839       1.30        ad 	int error, features;
    840       1.30        ad 	vaddr_t vaddr;
    841       1.30        ad 
    842       1.30        ad 	features = SCARG(uap, features);
    843       1.35        ad 	features &= ~(LWPCTL_FEATURE_CURCPU | LWPCTL_FEATURE_PCTR);
    844       1.35        ad 	if (features != 0)
    845       1.30        ad 		return ENODEV;
    846       1.30        ad 	if ((error = lwp_ctl_alloc(&vaddr)) != 0)
    847       1.30        ad 		return error;
    848       1.30        ad 	return copyout(&vaddr, SCARG(uap, address), sizeof(void *));
    849       1.30        ad }
    850