Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.46
      1 /*	$NetBSD: sys_lwp.c,v 1.46 2009/10/21 21:12:06 rmind 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.46 2009/10/21 21:12:06 rmind 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 #include <sys/cpu.h>
     51 
     52 #include <uvm/uvm_extern.h>
     53 
     54 #include "opt_sa.h"
     55 
     56 #define	LWP_UNPARK_MAX		1024
     57 
     58 syncobj_t lwp_park_sobj = {
     59 	SOBJ_SLEEPQ_LIFO,
     60 	sleepq_unsleep,
     61 	sleepq_changepri,
     62 	sleepq_lendpri,
     63 	syncobj_noowner,
     64 };
     65 
     66 sleeptab_t	lwp_park_tab;
     67 
     68 void
     69 lwp_sys_init(void)
     70 {
     71 	sleeptab_init(&lwp_park_tab);
     72 }
     73 
     74 /* ARGSUSED */
     75 int
     76 sys__lwp_create(struct lwp *l, const struct sys__lwp_create_args *uap, register_t *retval)
     77 {
     78 	/* {
     79 		syscallarg(const ucontext_t *) ucp;
     80 		syscallarg(u_long) flags;
     81 		syscallarg(lwpid_t *) new_lwp;
     82 	} */
     83 	struct proc *p = l->l_proc;
     84 	struct lwp *l2;
     85 	vaddr_t uaddr;
     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 	uaddr = uvm_uarea_alloc();
    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, SCARG(uap, flags) & LWP_DETACHED,
    115 	    NULL, 0, p->p_emul->e_startlwp, newuc, &l2, l->l_class);
    116 	if (__predict_false(error)) {
    117 		uvm_uarea_free(uaddr);
    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 #ifdef __HAVE_CPU_LWP_SETPRIVATE
    190 	cpu_lwp_setprivate(l, SCARG(uap, ptr));
    191 #endif
    192 
    193 	return 0;
    194 }
    195 
    196 int
    197 sys__lwp_suspend(struct lwp *l, const struct sys__lwp_suspend_args *uap, register_t *retval)
    198 {
    199 	/* {
    200 		syscallarg(lwpid_t) target;
    201 	} */
    202 	struct proc *p = l->l_proc;
    203 	struct lwp *t;
    204 	int error;
    205 
    206 	mutex_enter(p->p_lock);
    207 
    208 #ifdef KERN_SA
    209 	if ((p->p_sflag & PS_SA) != 0 || p->p_sa != NULL) {
    210 		mutex_exit(p->p_lock);
    211 		return EINVAL;
    212 	}
    213 #endif
    214 
    215 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    216 		mutex_exit(p->p_lock);
    217 		return ESRCH;
    218 	}
    219 
    220 	/*
    221 	 * Check for deadlock, which is only possible when we're suspending
    222 	 * ourself.  XXX There is a short race here, as p_nrlwps is only
    223 	 * incremented when an LWP suspends itself on the kernel/user
    224 	 * boundary.  It's still possible to kill -9 the process so we
    225 	 * don't bother checking further.
    226 	 */
    227 	lwp_lock(t);
    228 	if ((t == l && p->p_nrlwps == 1) ||
    229 	    (l->l_flag & (LW_WCORE | LW_WEXIT)) != 0) {
    230 		lwp_unlock(t);
    231 		mutex_exit(p->p_lock);
    232 		return EDEADLK;
    233 	}
    234 
    235 	/*
    236 	 * Suspend the LWP.  XXX If it's on a different CPU, we should wait
    237 	 * for it to be preempted, where it will put itself to sleep.
    238 	 *
    239 	 * Suspension of the current LWP will happen on return to userspace.
    240 	 */
    241 	error = lwp_suspend(l, t);
    242 	if (error) {
    243 		mutex_exit(p->p_lock);
    244 		return error;
    245 	}
    246 
    247 	/*
    248 	 * Wait for:
    249 	 *  o process exiting
    250 	 *  o target LWP suspended
    251 	 *  o target LWP not suspended and L_WSUSPEND clear
    252 	 *  o target LWP exited
    253 	 */
    254 	for (;;) {
    255 		error = cv_wait_sig(&p->p_lwpcv, p->p_lock);
    256 		if (error) {
    257 			error = ERESTART;
    258 			break;
    259 		}
    260 		if (lwp_find(p, SCARG(uap, target)) == NULL) {
    261 			error = ESRCH;
    262 			break;
    263 		}
    264 		if ((l->l_flag | t->l_flag) & (LW_WCORE | LW_WEXIT)) {
    265 			error = ERESTART;
    266 			break;
    267 		}
    268 		if (t->l_stat == LSSUSPENDED ||
    269 		    (t->l_flag & LW_WSUSPEND) == 0)
    270 			break;
    271 	}
    272 	mutex_exit(p->p_lock);
    273 
    274 	return error;
    275 }
    276 
    277 int
    278 sys__lwp_continue(struct lwp *l, const struct sys__lwp_continue_args *uap, register_t *retval)
    279 {
    280 	/* {
    281 		syscallarg(lwpid_t) target;
    282 	} */
    283 	int error;
    284 	struct proc *p = l->l_proc;
    285 	struct lwp *t;
    286 
    287 	error = 0;
    288 
    289 	mutex_enter(p->p_lock);
    290 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    291 		mutex_exit(p->p_lock);
    292 		return ESRCH;
    293 	}
    294 
    295 	lwp_lock(t);
    296 	lwp_continue(t);
    297 	mutex_exit(p->p_lock);
    298 
    299 	return error;
    300 }
    301 
    302 int
    303 sys__lwp_wakeup(struct lwp *l, const struct sys__lwp_wakeup_args *uap, register_t *retval)
    304 {
    305 	/* {
    306 		syscallarg(lwpid_t) target;
    307 	} */
    308 	struct lwp *t;
    309 	struct proc *p;
    310 	int error;
    311 
    312 	p = l->l_proc;
    313 	mutex_enter(p->p_lock);
    314 
    315 	if ((t = lwp_find(p, SCARG(uap, target))) == NULL) {
    316 		mutex_exit(p->p_lock);
    317 		return ESRCH;
    318 	}
    319 
    320 	lwp_lock(t);
    321 	t->l_flag |= (LW_CANCELLED | LW_UNPARKED);
    322 
    323 	if (t->l_stat != LSSLEEP) {
    324 		lwp_unlock(t);
    325 		error = ENODEV;
    326 	} else if ((t->l_flag & LW_SINTR) == 0) {
    327 		lwp_unlock(t);
    328 		error = EBUSY;
    329 	} else {
    330 		/* Wake it up.  lwp_unsleep() will release the LWP lock. */
    331 		lwp_unsleep(t, true);
    332 		error = 0;
    333 	}
    334 
    335 	mutex_exit(p->p_lock);
    336 
    337 	return error;
    338 }
    339 
    340 int
    341 sys__lwp_wait(struct lwp *l, const struct sys__lwp_wait_args *uap, register_t *retval)
    342 {
    343 	/* {
    344 		syscallarg(lwpid_t) wait_for;
    345 		syscallarg(lwpid_t *) departed;
    346 	} */
    347 	struct proc *p = l->l_proc;
    348 	int error;
    349 	lwpid_t dep;
    350 
    351 	mutex_enter(p->p_lock);
    352 	error = lwp_wait1(l, SCARG(uap, wait_for), &dep, 0);
    353 	mutex_exit(p->p_lock);
    354 
    355 	if (error)
    356 		return error;
    357 
    358 	if (SCARG(uap, departed)) {
    359 		error = copyout(&dep, SCARG(uap, departed), sizeof(dep));
    360 		if (error)
    361 			return error;
    362 	}
    363 
    364 	return 0;
    365 }
    366 
    367 /* ARGSUSED */
    368 int
    369 sys__lwp_kill(struct lwp *l, const struct sys__lwp_kill_args *uap, register_t *retval)
    370 {
    371 	/* {
    372 		syscallarg(lwpid_t)	target;
    373 		syscallarg(int)		signo;
    374 	} */
    375 	struct proc *p = l->l_proc;
    376 	struct lwp *t;
    377 	ksiginfo_t ksi;
    378 	int signo = SCARG(uap, signo);
    379 	int error = 0;
    380 
    381 	if ((u_int)signo >= NSIG)
    382 		return EINVAL;
    383 
    384 	KSI_INIT(&ksi);
    385 	ksi.ksi_signo = signo;
    386 	ksi.ksi_code = SI_LWP;
    387 	ksi.ksi_pid = p->p_pid;
    388 	ksi.ksi_uid = kauth_cred_geteuid(l->l_cred);
    389 	ksi.ksi_lid = SCARG(uap, target);
    390 
    391 	mutex_enter(proc_lock);
    392 	mutex_enter(p->p_lock);
    393 	if ((t = lwp_find(p, ksi.ksi_lid)) == NULL)
    394 		error = ESRCH;
    395 	else if (signo != 0)
    396 		kpsignal2(p, &ksi);
    397 	mutex_exit(p->p_lock);
    398 	mutex_exit(proc_lock);
    399 
    400 	return error;
    401 }
    402 
    403 int
    404 sys__lwp_detach(struct lwp *l, const struct sys__lwp_detach_args *uap, register_t *retval)
    405 {
    406 	/* {
    407 		syscallarg(lwpid_t)	target;
    408 	} */
    409 	struct proc *p;
    410 	struct lwp *t;
    411 	lwpid_t target;
    412 	int error;
    413 
    414 	target = SCARG(uap, target);
    415 	p = l->l_proc;
    416 
    417 	mutex_enter(p->p_lock);
    418 
    419 	if (l->l_lid == target)
    420 		t = l;
    421 	else {
    422 		/*
    423 		 * We can't use lwp_find() here because the target might
    424 		 * be a zombie.
    425 		 */
    426 		LIST_FOREACH(t, &p->p_lwps, l_sibling)
    427 			if (t->l_lid == target)
    428 				break;
    429 	}
    430 
    431 	/*
    432 	 * If the LWP is already detached, there's nothing to do.
    433 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    434 	 * is visible with the proc mutex held.
    435 	 *
    436 	 * After we have detached or released the LWP, kick any
    437 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    438 	 * for the target LWP to exit.
    439 	 */
    440 	if (t != NULL && t->l_stat != LSIDL) {
    441 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    442 			p->p_ndlwps++;
    443 			t->l_prflag |= LPR_DETACHED;
    444 			if (t->l_stat == LSZOMB) {
    445 				/* Releases proc mutex. */
    446 				lwp_free(t, false, false);
    447 				return 0;
    448 			}
    449 			error = 0;
    450 
    451 			/*
    452 			 * Have any LWPs sleeping in lwp_wait() recheck
    453 			 * for deadlock.
    454 			 */
    455 			cv_broadcast(&p->p_lwpcv);
    456 		} else
    457 			error = EINVAL;
    458 	} else
    459 		error = ESRCH;
    460 
    461 	mutex_exit(p->p_lock);
    462 
    463 	return error;
    464 }
    465 
    466 static inline wchan_t
    467 lwp_park_wchan(struct proc *p, const void *hint)
    468 {
    469 
    470 	return (wchan_t)((uintptr_t)p ^ (uintptr_t)hint);
    471 }
    472 
    473 int
    474 lwp_unpark(lwpid_t target, const void *hint)
    475 {
    476 	sleepq_t *sq;
    477 	wchan_t wchan;
    478 	kmutex_t *mp;
    479 	proc_t *p;
    480 	lwp_t *t;
    481 
    482 	/*
    483 	 * Easy case: search for the LWP on the sleep queue.  If
    484 	 * it's parked, remove it from the queue and set running.
    485 	 */
    486 	p = curproc;
    487 	wchan = lwp_park_wchan(p, hint);
    488 	sq = sleeptab_lookup(&lwp_park_tab, wchan, &mp);
    489 
    490 	TAILQ_FOREACH(t, sq, l_sleepchain)
    491 		if (t->l_proc == p && t->l_lid == target)
    492 			break;
    493 
    494 	if (__predict_true(t != NULL)) {
    495 		sleepq_remove(sq, t);
    496 		mutex_spin_exit(mp);
    497 		return 0;
    498 	}
    499 
    500 	/*
    501 	 * The LWP hasn't parked yet.  Take the hit and mark the
    502 	 * operation as pending.
    503 	 */
    504 	mutex_spin_exit(mp);
    505 
    506 	mutex_enter(p->p_lock);
    507 	if ((t = lwp_find(p, target)) == NULL) {
    508 		mutex_exit(p->p_lock);
    509 		return ESRCH;
    510 	}
    511 
    512 	/*
    513 	 * It may not have parked yet, we may have raced, or it
    514 	 * is parked on a different user sync object.
    515 	 */
    516 	lwp_lock(t);
    517 	if (t->l_syncobj == &lwp_park_sobj) {
    518 		/* Releases the LWP lock. */
    519 		lwp_unsleep(t, true);
    520 	} else {
    521 		/*
    522 		 * Set the operation pending.  The next call to _lwp_park
    523 		 * will return early.
    524 		 */
    525 		t->l_flag |= LW_UNPARKED;
    526 		lwp_unlock(t);
    527 	}
    528 
    529 	mutex_exit(p->p_lock);
    530 	return 0;
    531 }
    532 
    533 int
    534 lwp_park(struct timespec *ts, const void *hint)
    535 {
    536 	struct timespec tsx;
    537 	sleepq_t *sq;
    538 	kmutex_t *mp;
    539 	wchan_t wchan;
    540 	int timo, error;
    541 	lwp_t *l;
    542 
    543 	/* Fix up the given timeout value. */
    544 	if (ts != NULL) {
    545 		getnanotime(&tsx);
    546 		timespecsub(ts, &tsx, &tsx);
    547 		if (tsx.tv_sec < 0 || (tsx.tv_sec == 0 && tsx.tv_nsec <= 0))
    548 			return ETIMEDOUT;
    549 		if ((error = itimespecfix(&tsx)) != 0)
    550 			return error;
    551 		timo = tstohz(&tsx);
    552 		KASSERT(timo != 0);
    553 	} else
    554 		timo = 0;
    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 		break;
    580 	case ERESTART:
    581 		error = EINTR;
    582 		break;
    583 	default:
    584 		/* nothing */
    585 		break;
    586 	}
    587 	return error;
    588 }
    589 
    590 /*
    591  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    592  * will remain parked until another LWP in the same process calls in and
    593  * requests that it be unparked.
    594  */
    595 int
    596 sys____lwp_park50(struct lwp *l, const struct sys____lwp_park50_args *uap,
    597     register_t *retval)
    598 {
    599 	/* {
    600 		syscallarg(const struct timespec *)	ts;
    601 		syscallarg(lwpid_t)			unpark;
    602 		syscallarg(const void *)		hint;
    603 		syscallarg(const void *)		unparkhint;
    604 	} */
    605 	struct timespec ts, *tsp;
    606 	int error;
    607 
    608 	if (SCARG(uap, ts) == NULL)
    609 		tsp = NULL;
    610 	else {
    611 		error = copyin(SCARG(uap, ts), &ts, sizeof(ts));
    612 		if (error != 0)
    613 			return error;
    614 		tsp = &ts;
    615 	}
    616 
    617 	if (SCARG(uap, unpark) != 0) {
    618 		error = lwp_unpark(SCARG(uap, unpark), SCARG(uap, unparkhint));
    619 		if (error != 0)
    620 			return error;
    621 	}
    622 
    623 	return lwp_park(tsp, SCARG(uap, hint));
    624 }
    625 
    626 int
    627 sys__lwp_unpark(struct lwp *l, const struct sys__lwp_unpark_args *uap, register_t *retval)
    628 {
    629 	/* {
    630 		syscallarg(lwpid_t)		target;
    631 		syscallarg(const void *)	hint;
    632 	} */
    633 
    634 	return lwp_unpark(SCARG(uap, target), SCARG(uap, hint));
    635 }
    636 
    637 int
    638 sys__lwp_unpark_all(struct lwp *l, const struct sys__lwp_unpark_all_args *uap, register_t *retval)
    639 {
    640 	/* {
    641 		syscallarg(const lwpid_t *)	targets;
    642 		syscallarg(size_t)		ntargets;
    643 		syscallarg(const void *)	hint;
    644 	} */
    645 	struct proc *p;
    646 	struct lwp *t;
    647 	sleepq_t *sq;
    648 	wchan_t wchan;
    649 	lwpid_t targets[32], *tp, *tpp, *tmax, target;
    650 	int error;
    651 	kmutex_t *mp;
    652 	u_int ntargets;
    653 	size_t sz;
    654 
    655 	p = l->l_proc;
    656 	ntargets = SCARG(uap, ntargets);
    657 
    658 	if (SCARG(uap, targets) == NULL) {
    659 		/*
    660 		 * Let the caller know how much we are willing to do, and
    661 		 * let it unpark the LWPs in blocks.
    662 		 */
    663 		*retval = LWP_UNPARK_MAX;
    664 		return 0;
    665 	}
    666 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    667 		return EINVAL;
    668 
    669 	/*
    670 	 * Copy in the target array.  If it's a small number of LWPs, then
    671 	 * place the numbers on the stack.
    672 	 */
    673 	sz = sizeof(target) * ntargets;
    674 	if (sz <= sizeof(targets))
    675 		tp = targets;
    676 	else {
    677 		tp = kmem_alloc(sz, KM_SLEEP);
    678 		if (tp == NULL)
    679 			return ENOMEM;
    680 	}
    681 	error = copyin(SCARG(uap, targets), tp, sz);
    682 	if (error != 0) {
    683 		if (tp != targets) {
    684 			kmem_free(tp, sz);
    685 		}
    686 		return error;
    687 	}
    688 
    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 			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 			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 
    745 	return 0;
    746 }
    747 
    748 int
    749 sys__lwp_setname(struct lwp *l, const struct sys__lwp_setname_args *uap, register_t *retval)
    750 {
    751 	/* {
    752 		syscallarg(lwpid_t)		target;
    753 		syscallarg(const char *)	name;
    754 	} */
    755 	char *name, *oname;
    756 	lwpid_t target;
    757 	proc_t *p;
    758 	lwp_t *t;
    759 	int error;
    760 
    761 	if ((target = SCARG(uap, target)) == 0)
    762 		target = l->l_lid;
    763 
    764 	name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
    765 	if (name == NULL)
    766 		return ENOMEM;
    767 	error = copyinstr(SCARG(uap, name), name, MAXCOMLEN, NULL);
    768 	switch (error) {
    769 	case ENAMETOOLONG:
    770 	case 0:
    771 		name[MAXCOMLEN - 1] = '\0';
    772 		break;
    773 	default:
    774 		kmem_free(name, MAXCOMLEN);
    775 		return error;
    776 	}
    777 
    778 	p = curproc;
    779 	mutex_enter(p->p_lock);
    780 	if ((t = lwp_find(p, target)) == NULL) {
    781 		mutex_exit(p->p_lock);
    782 		kmem_free(name, MAXCOMLEN);
    783 		return ESRCH;
    784 	}
    785 	lwp_lock(t);
    786 	oname = t->l_name;
    787 	t->l_name = name;
    788 	lwp_unlock(t);
    789 	mutex_exit(p->p_lock);
    790 
    791 	if (oname != NULL)
    792 		kmem_free(oname, MAXCOMLEN);
    793 
    794 	return 0;
    795 }
    796 
    797 int
    798 sys__lwp_getname(struct lwp *l, const struct sys__lwp_getname_args *uap, register_t *retval)
    799 {
    800 	/* {
    801 		syscallarg(lwpid_t)		target;
    802 		syscallarg(char *)		name;
    803 		syscallarg(size_t)		len;
    804 	} */
    805 	char name[MAXCOMLEN];
    806 	lwpid_t target;
    807 	proc_t *p;
    808 	lwp_t *t;
    809 
    810 	if ((target = SCARG(uap, target)) == 0)
    811 		target = l->l_lid;
    812 
    813 	p = curproc;
    814 	mutex_enter(p->p_lock);
    815 	if ((t = lwp_find(p, target)) == NULL) {
    816 		mutex_exit(p->p_lock);
    817 		return ESRCH;
    818 	}
    819 	lwp_lock(t);
    820 	if (t->l_name == NULL)
    821 		name[0] = '\0';
    822 	else
    823 		strcpy(name, t->l_name);
    824 	lwp_unlock(t);
    825 	mutex_exit(p->p_lock);
    826 
    827 	return copyoutstr(name, SCARG(uap, name), SCARG(uap, len), NULL);
    828 }
    829 
    830 int
    831 sys__lwp_ctl(struct lwp *l, const struct sys__lwp_ctl_args *uap, register_t *retval)
    832 {
    833 	/* {
    834 		syscallarg(int)			features;
    835 		syscallarg(struct lwpctl **)	address;
    836 	} */
    837 	int error, features;
    838 	vaddr_t vaddr;
    839 
    840 	features = SCARG(uap, features);
    841 	features &= ~(LWPCTL_FEATURE_CURCPU | LWPCTL_FEATURE_PCTR);
    842 	if (features != 0)
    843 		return ENODEV;
    844 	if ((error = lwp_ctl_alloc(&vaddr)) != 0)
    845 		return error;
    846 	return copyout(&vaddr, SCARG(uap, address), sizeof(void *));
    847 }
    848