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