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