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