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