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