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