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