Home | History | Annotate | Line # | Download | only in kern
sys_lwp.c revision 1.85
      1 /*	$NetBSD: sys_lwp.c,v 1.85 2023/09/23 18:48:04 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007, 2008, 2019, 2020, 2023
      5  *     The NetBSD Foundation, Inc.
      6  * All rights reserved.
      7  *
      8  * This code is derived from software contributed to The NetBSD Foundation
      9  * by Nathan J. Williams, and Andrew Doran.
     10  *
     11  * Redistribution and use in source and binary forms, with or without
     12  * modification, are permitted provided that the following conditions
     13  * are met:
     14  * 1. Redistributions of source code must retain the above copyright
     15  *    notice, this list of conditions and the following disclaimer.
     16  * 2. Redistributions in binary form must reproduce the above copyright
     17  *    notice, this list of conditions and the following disclaimer in the
     18  *    documentation and/or other materials provided with the distribution.
     19  *
     20  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     22  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     23  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     24  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     30  * POSSIBILITY OF SUCH DAMAGE.
     31  */
     32 
     33 /*
     34  * Lightweight process (LWP) system calls.  See kern_lwp.c for a description
     35  * of LWPs.
     36  */
     37 
     38 #include <sys/cdefs.h>
     39 __KERNEL_RCSID(0, "$NetBSD: sys_lwp.c,v 1.85 2023/09/23 18:48:04 ad Exp $");
     40 
     41 #include <sys/param.h>
     42 #include <sys/systm.h>
     43 #include <sys/pool.h>
     44 #include <sys/proc.h>
     45 #include <sys/types.h>
     46 #include <sys/syscallargs.h>
     47 #include <sys/kauth.h>
     48 #include <sys/kmem.h>
     49 #include <sys/ptrace.h>
     50 #include <sys/sleepq.h>
     51 #include <sys/lwpctl.h>
     52 #include <sys/cpu.h>
     53 #include <sys/pserialize.h>
     54 
     55 #include <uvm/uvm_extern.h>
     56 
     57 #define	LWP_UNPARK_MAX		1024
     58 
     59 static const stack_t lwp_ss_init = SS_INIT;
     60 
     61 /*
     62  * Parked LWPs get no priority boost on awakening as they blocked on
     63  * user space objects.  Maybe revisit?
     64  */
     65 syncobj_t lwp_park_syncobj = {
     66 	.sobj_name	= "lwp_park",
     67 	.sobj_flag	= SOBJ_SLEEPQ_NULL,
     68 	.sobj_boostpri  = PRI_USER,
     69 	.sobj_unsleep	= sleepq_unsleep,
     70 	.sobj_changepri	= sleepq_changepri,
     71 	.sobj_lendpri	= sleepq_lendpri,
     72 	.sobj_owner	= syncobj_noowner,
     73 };
     74 
     75 static void
     76 mi_startlwp(void *arg)
     77 {
     78 	struct lwp *l = curlwp;
     79 	struct proc *p = l->l_proc;
     80 
     81 	(p->p_emul->e_startlwp)(arg);
     82 
     83 	/* If the process is traced, report lwp creation to a debugger */
     84 	if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE)) ==
     85 	    (PSL_TRACED|PSL_TRACELWP_CREATE)) {
     86 		/* Paranoid check */
     87 		mutex_enter(&proc_lock);
     88 		if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE)) !=
     89 		    (PSL_TRACED|PSL_TRACELWP_CREATE)) {
     90 			mutex_exit(&proc_lock);
     91 			return;
     92 		}
     93 
     94 		mutex_enter(p->p_lock);
     95 		eventswitch(TRAP_LWP, PTRACE_LWP_CREATE, l->l_lid);
     96 	}
     97 }
     98 
     99 int
    100 do_lwp_create(lwp_t *l, void *arg, u_long flags, lwp_t **l2,
    101     const sigset_t *sigmask, const stack_t *sigstk)
    102 {
    103 	struct proc *p = l->l_proc;
    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, &lwp_ss_init);
    115 	if (__predict_false(error)) {
    116 		uvm_uarea_free(uaddr);
    117 		return error;
    118 	}
    119 
    120 	return 0;
    121 }
    122 
    123 int
    124 sys__lwp_create(struct lwp *l, const struct sys__lwp_create_args *uap,
    125     register_t *retval)
    126 {
    127 	/* {
    128 		syscallarg(const ucontext_t *) ucp;
    129 		syscallarg(u_long) flags;
    130 		syscallarg(lwpid_t *) new_lwp;
    131 	} */
    132 	struct proc *p = l->l_proc;
    133 	ucontext_t *newuc;
    134 	lwp_t *l2;
    135 	int error;
    136 
    137 	newuc = kmem_alloc(sizeof(ucontext_t), KM_SLEEP);
    138 	error = copyin(SCARG(uap, ucp), newuc, p->p_emul->e_ucsize);
    139 	if (error)
    140 		goto fail;
    141 
    142 	/* validate the ucontext */
    143 	if ((newuc->uc_flags & _UC_CPU) == 0) {
    144 		error = EINVAL;
    145 		goto fail;
    146 	}
    147 	error = cpu_mcontext_validate(l, &newuc->uc_mcontext);
    148 	if (error)
    149 		goto fail;
    150 
    151 	const sigset_t *sigmask = newuc->uc_flags & _UC_SIGMASK ?
    152 	    &newuc->uc_sigmask : &l->l_sigmask;
    153 	error = do_lwp_create(l, newuc, SCARG(uap, flags), &l2, sigmask,
    154 	    &SS_INIT);
    155 	if (error)
    156 		goto fail;
    157 
    158 	error = copyout(&l2->l_lid, SCARG(uap, new_lwp), sizeof(l2->l_lid));
    159 	if (error == 0) {
    160 		lwp_start(l2, SCARG(uap, flags));
    161 		return 0;
    162 	}
    163 	lwp_exit(l2);
    164  fail:
    165 	kmem_free(newuc, sizeof(ucontext_t));
    166 	return error;
    167 }
    168 
    169 int
    170 sys__lwp_exit(struct lwp *l, const void *v, register_t *retval)
    171 {
    172 
    173 	lwp_exit(l);
    174 	return 0;
    175 }
    176 
    177 int
    178 sys__lwp_self(struct lwp *l, const void *v, register_t *retval)
    179 {
    180 
    181 	*retval = l->l_lid;
    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 = proc_find_lwp(p, target);
    427 		KASSERT(t == NULL || t->l_lid == target);
    428 	}
    429 
    430 	/*
    431 	 * If the LWP is already detached, there's nothing to do.
    432 	 * If it's a zombie, we need to clean up after it.  LSZOMB
    433 	 * is visible with the proc mutex held.
    434 	 *
    435 	 * After we have detached or released the LWP, kick any
    436 	 * other LWPs that may be sitting in _lwp_wait(), waiting
    437 	 * for the target LWP to exit.
    438 	 */
    439 	if (t != NULL && t->l_stat != LSIDL) {
    440 		if ((t->l_prflag & LPR_DETACHED) == 0) {
    441 			p->p_ndlwps++;
    442 			t->l_prflag |= LPR_DETACHED;
    443 			if (t->l_stat == LSZOMB) {
    444 				/* Releases proc mutex. */
    445 				lwp_free(t, false, false);
    446 				return 0;
    447 			}
    448 			error = 0;
    449 
    450 			/*
    451 			 * Have any LWPs sleeping in lwp_wait() recheck
    452 			 * for deadlock.
    453 			 */
    454 			cv_broadcast(&p->p_lwpcv);
    455 		} else
    456 			error = EINVAL;
    457 	} else
    458 		error = ESRCH;
    459 
    460 	mutex_exit(p->p_lock);
    461 
    462 	return error;
    463 }
    464 
    465 int
    466 lwp_unpark(const lwpid_t *tp, const u_int ntargets)
    467 {
    468 	u_int target;
    469 	int error, s;
    470 	proc_t *p;
    471 	lwp_t *t;
    472 
    473 	p = curproc;
    474 	error = 0;
    475 
    476 	s = pserialize_read_enter();
    477 	for (target = 0; target < ntargets; target++) {
    478 		t = proc_find_lwp_unlocked(p, tp[target]);
    479 		if (__predict_false(t == NULL)) {
    480 			error = ESRCH;
    481 			continue;
    482 		}
    483 
    484 		KASSERT(lwp_locked(t, NULL));
    485 
    486 		if (__predict_true(t->l_syncobj == &lwp_park_syncobj)) {
    487 			/*
    488 			 * As expected it's parked, so wake it up.
    489 			 * lwp_unsleep() will release the LWP lock.
    490 			 */
    491 			lwp_unsleep(t, true);
    492 		} else if (__predict_false(t->l_stat == LSZOMB)) {
    493 			lwp_unlock(t);
    494 			error = ESRCH;
    495 		} else {
    496 			/*
    497 			 * It hasn't parked yet because the wakeup side won
    498 			 * the race, or something else has happened to make
    499 			 * the thread not park.  Why doesn't really matter.
    500 			 * Set the operation pending, so that the next call
    501 			 * to _lwp_park() in the LWP returns early.  If it
    502 			 * turns out to be a spurious wakeup, no harm done.
    503 			 */
    504 			t->l_flag |= LW_UNPARKED;
    505 			lwp_unlock(t);
    506 		}
    507 	}
    508 	pserialize_read_exit(s);
    509 
    510 	return error;
    511 }
    512 
    513 int
    514 lwp_park(clockid_t clock_id, int flags, struct timespec *ts)
    515 {
    516 	int timo, error;
    517 	struct timespec start;
    518 	lwp_t *l;
    519 	bool timeremain = !(flags & TIMER_ABSTIME) && ts;
    520 
    521 	if (ts != NULL) {
    522 		if ((error = ts2timo(clock_id, flags, ts, &timo,
    523 		    timeremain ? &start : NULL)) != 0)
    524 			return error;
    525 		KASSERT(timo != 0);
    526 	} else {
    527 		timo = 0;
    528 	}
    529 
    530 	/*
    531 	 * Before going the full route and blocking, check to see if an
    532 	 * unpark op is pending.
    533 	 */
    534 	l = curlwp;
    535 	lwp_lock(l);
    536 	if ((l->l_flag & (LW_CANCELLED | LW_UNPARKED)) != 0) {
    537 		l->l_flag &= ~(LW_CANCELLED | LW_UNPARKED);
    538 		lwp_unlock(l);
    539 		return EALREADY;
    540 	}
    541 	l->l_biglocks = 0;
    542 	sleepq_enqueue(NULL, l, "parked", &lwp_park_syncobj, true);
    543 	error = sleepq_block(timo, true, &lwp_park_syncobj);
    544 	switch (error) {
    545 	case EWOULDBLOCK:
    546 		error = ETIMEDOUT;
    547 		if (timeremain)
    548 			memset(ts, 0, sizeof(*ts));
    549 		break;
    550 	case ERESTART:
    551 		error = EINTR;
    552 		/*FALLTHROUGH*/
    553 	default:
    554 		if (timeremain)
    555 			clock_timeleft(clock_id, ts, &start);
    556 		break;
    557 	}
    558 	return error;
    559 }
    560 
    561 /*
    562  * 'park' an LWP waiting on a user-level synchronisation object.  The LWP
    563  * will remain parked until another LWP in the same process calls in and
    564  * requests that it be unparked.
    565  */
    566 int
    567 sys____lwp_park60(struct lwp *l, const struct sys____lwp_park60_args *uap,
    568     register_t *retval)
    569 {
    570 	/* {
    571 		syscallarg(clockid_t)			clock_id;
    572 		syscallarg(int)				flags;
    573 		syscallarg(struct timespec *)		ts;
    574 		syscallarg(lwpid_t)			unpark;
    575 		syscallarg(const void *)		hint;
    576 		syscallarg(const void *)		unparkhint;
    577 	} */
    578 	struct timespec ts, *tsp;
    579 	int error;
    580 
    581 	if (SCARG(uap, ts) == NULL)
    582 		tsp = NULL;
    583 	else {
    584 		error = copyin(SCARG(uap, ts), &ts, sizeof(ts));
    585 		if (error != 0)
    586 			return error;
    587 		tsp = &ts;
    588 	}
    589 
    590 	if (SCARG(uap, unpark) != 0) {
    591 		error = lwp_unpark(&SCARG(uap, unpark), 1);
    592 		if (error != 0)
    593 			return error;
    594 	}
    595 
    596 	error = lwp_park(SCARG(uap, clock_id), SCARG(uap, flags), tsp);
    597 	if (SCARG(uap, ts) != NULL && (SCARG(uap, flags) & TIMER_ABSTIME) == 0)
    598 		(void)copyout(tsp, SCARG(uap, ts), sizeof(*tsp));
    599 	return error;
    600 }
    601 
    602 int
    603 sys__lwp_unpark(struct lwp *l, const struct sys__lwp_unpark_args *uap,
    604     register_t *retval)
    605 {
    606 	/* {
    607 		syscallarg(lwpid_t)		target;
    608 		syscallarg(const void *)	hint;
    609 	} */
    610 
    611 	return lwp_unpark(&SCARG(uap, target), 1);
    612 }
    613 
    614 int
    615 sys__lwp_unpark_all(struct lwp *l, const struct sys__lwp_unpark_all_args *uap,
    616     register_t *retval)
    617 {
    618 	/* {
    619 		syscallarg(const lwpid_t *)	targets;
    620 		syscallarg(size_t)		ntargets;
    621 		syscallarg(const void *)	hint;
    622 	} */
    623 	lwpid_t targets[32], *tp;
    624 	int error;
    625 	u_int ntargets;
    626 	size_t sz;
    627 
    628 	ntargets = SCARG(uap, ntargets);
    629 	if (SCARG(uap, targets) == NULL) {
    630 		/*
    631 		 * Let the caller know how much we are willing to do, and
    632 		 * let it unpark the LWPs in blocks.
    633 		 */
    634 		*retval = LWP_UNPARK_MAX;
    635 		return 0;
    636 	}
    637 	if (ntargets > LWP_UNPARK_MAX || ntargets == 0)
    638 		return EINVAL;
    639 
    640 	/*
    641 	 * Copy in the target array.  If it's a small number of LWPs, then
    642 	 * place the numbers on the stack.
    643 	 */
    644 	sz = sizeof(lwpid_t) * ntargets;
    645 	if (sz <= sizeof(targets))
    646 		tp = targets;
    647 	else
    648 		tp = kmem_alloc(sz, KM_SLEEP);
    649 	error = copyin(SCARG(uap, targets), tp, sz);
    650 	if (error != 0) {
    651 		if (tp != targets) {
    652 			kmem_free(tp, sz);
    653 		}
    654 		return error;
    655 	}
    656 	error = lwp_unpark(tp, ntargets);
    657 	if (tp != targets)
    658 		kmem_free(tp, sz);
    659 	return error;
    660 }
    661 
    662 int
    663 sys__lwp_setname(struct lwp *l, const struct sys__lwp_setname_args *uap,
    664     register_t *retval)
    665 {
    666 	/* {
    667 		syscallarg(lwpid_t)		target;
    668 		syscallarg(const char *)	name;
    669 	} */
    670 	char *name, *oname;
    671 	lwpid_t target;
    672 	proc_t *p;
    673 	lwp_t *t;
    674 	int error;
    675 
    676 	if ((target = SCARG(uap, target)) == 0)
    677 		target = l->l_lid;
    678 
    679 	name = kmem_alloc(MAXCOMLEN, KM_SLEEP);
    680 	error = copyinstr(SCARG(uap, name), name, MAXCOMLEN, NULL);
    681 	switch (error) {
    682 	case ENAMETOOLONG:
    683 	case 0:
    684 		name[MAXCOMLEN - 1] = '\0';
    685 		break;
    686 	default:
    687 		kmem_free(name, MAXCOMLEN);
    688 		return error;
    689 	}
    690 
    691 	p = curproc;
    692 	mutex_enter(p->p_lock);
    693 	if ((t = lwp_find(p, target)) == NULL) {
    694 		mutex_exit(p->p_lock);
    695 		kmem_free(name, MAXCOMLEN);
    696 		return ESRCH;
    697 	}
    698 	lwp_lock(t);
    699 	oname = t->l_name;
    700 	t->l_name = name;
    701 	lwp_unlock(t);
    702 	mutex_exit(p->p_lock);
    703 
    704 	if (oname != NULL)
    705 		kmem_free(oname, MAXCOMLEN);
    706 
    707 	return 0;
    708 }
    709 
    710 int
    711 sys__lwp_getname(struct lwp *l, const struct sys__lwp_getname_args *uap,
    712     register_t *retval)
    713 {
    714 	/* {
    715 		syscallarg(lwpid_t)		target;
    716 		syscallarg(char *)		name;
    717 		syscallarg(size_t)		len;
    718 	} */
    719 	char name[MAXCOMLEN];
    720 	lwpid_t target;
    721 	size_t len;
    722 	proc_t *p;
    723 	lwp_t *t;
    724 
    725 	if ((target = SCARG(uap, target)) == 0)
    726 		target = l->l_lid;
    727 
    728 	p = curproc;
    729 	mutex_enter(p->p_lock);
    730 	if ((t = lwp_find(p, target)) == NULL) {
    731 		mutex_exit(p->p_lock);
    732 		return ESRCH;
    733 	}
    734 	lwp_lock(t);
    735 	if (t->l_name == NULL)
    736 		name[0] = '\0';
    737 	else
    738 		strlcpy(name, t->l_name, sizeof(name));
    739 	lwp_unlock(t);
    740 	mutex_exit(p->p_lock);
    741 
    742 	len = uimin(SCARG(uap, len), sizeof(name));
    743 
    744 	return copyoutstr(name, SCARG(uap, name), len, NULL);
    745 }
    746 
    747 int
    748 sys__lwp_ctl(struct lwp *l, const struct sys__lwp_ctl_args *uap,
    749     register_t *retval)
    750 {
    751 	/* {
    752 		syscallarg(int)			features;
    753 		syscallarg(struct lwpctl **)	address;
    754 	} */
    755 	int error, features;
    756 	vaddr_t vaddr;
    757 
    758 	features = SCARG(uap, features);
    759 	features &= ~(LWPCTL_FEATURE_CURCPU | LWPCTL_FEATURE_PCTR);
    760 	if (features != 0)
    761 		return ENODEV;
    762 	if ((error = lwp_ctl_alloc(&vaddr)) != 0)
    763 		return error;
    764 	return copyout(&vaddr, SCARG(uap, address), sizeof(void *));
    765 }
    766