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