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