Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.196
      1 /*	$NetBSD: kern_lwp.c,v 1.196 2019/03/01 09:02:03 hannken Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007, 2008, 2009 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  * Overview
     34  *
     35  *	Lightweight processes (LWPs) are the basic unit or thread of
     36  *	execution within the kernel.  The core state of an LWP is described
     37  *	by "struct lwp", also known as lwp_t.
     38  *
     39  *	Each LWP is contained within a process (described by "struct proc"),
     40  *	Every process contains at least one LWP, but may contain more.  The
     41  *	process describes attributes shared among all of its LWPs such as a
     42  *	private address space, global execution state (stopped, active,
     43  *	zombie, ...), signal disposition and so on.  On a multiprocessor
     44  *	machine, multiple LWPs be executing concurrently in the kernel.
     45  *
     46  * Execution states
     47  *
     48  *	At any given time, an LWP has overall state that is described by
     49  *	lwp::l_stat.  The states are broken into two sets below.  The first
     50  *	set is guaranteed to represent the absolute, current state of the
     51  *	LWP:
     52  *
     53  *	LSONPROC
     54  *
     55  *		On processor: the LWP is executing on a CPU, either in the
     56  *		kernel or in user space.
     57  *
     58  *	LSRUN
     59  *
     60  *		Runnable: the LWP is parked on a run queue, and may soon be
     61  *		chosen to run by an idle processor, or by a processor that
     62  *		has been asked to preempt a currently runnning but lower
     63  *		priority LWP.
     64  *
     65  *	LSIDL
     66  *
     67  *		Idle: the LWP has been created but has not yet executed,
     68  *		or it has ceased executing a unit of work and is waiting
     69  *		to be started again.
     70  *
     71  *	LSSUSPENDED:
     72  *
     73  *		Suspended: the LWP has had its execution suspended by
     74  *		another LWP in the same process using the _lwp_suspend()
     75  *		system call.  User-level LWPs also enter the suspended
     76  *		state when the system is shutting down.
     77  *
     78  *	The second set represent a "statement of intent" on behalf of the
     79  *	LWP.  The LWP may in fact be executing on a processor, may be
     80  *	sleeping or idle. It is expected to take the necessary action to
     81  *	stop executing or become "running" again within a short timeframe.
     82  *	The LP_RUNNING flag in lwp::l_pflag indicates that an LWP is running.
     83  *	Importantly, it indicates that its state is tied to a CPU.
     84  *
     85  *	LSZOMB:
     86  *
     87  *		Dead or dying: the LWP has released most of its resources
     88  *		and is about to switch away into oblivion, or has already
     89  *		switched away.  When it switches away, its few remaining
     90  *		resources can be collected.
     91  *
     92  *	LSSLEEP:
     93  *
     94  *		Sleeping: the LWP has entered itself onto a sleep queue, and
     95  *		has switched away or will switch away shortly to allow other
     96  *		LWPs to run on the CPU.
     97  *
     98  *	LSSTOP:
     99  *
    100  *		Stopped: the LWP has been stopped as a result of a job
    101  *		control signal, or as a result of the ptrace() interface.
    102  *
    103  *		Stopped LWPs may run briefly within the kernel to handle
    104  *		signals that they receive, but will not return to user space
    105  *		until their process' state is changed away from stopped.
    106  *
    107  *		Single LWPs within a process can not be set stopped
    108  *		selectively: all actions that can stop or continue LWPs
    109  *		occur at the process level.
    110  *
    111  * State transitions
    112  *
    113  *	Note that the LSSTOP state may only be set when returning to
    114  *	user space in userret(), or when sleeping interruptably.  The
    115  *	LSSUSPENDED state may only be set in userret().  Before setting
    116  *	those states, we try to ensure that the LWPs will release all
    117  *	locks that they hold, and at a minimum try to ensure that the
    118  *	LWP can be set runnable again by a signal.
    119  *
    120  *	LWPs may transition states in the following ways:
    121  *
    122  *	 RUN -------> ONPROC		ONPROC -----> RUN
    123  *		    				    > SLEEP
    124  *		    				    > STOPPED
    125  *						    > SUSPENDED
    126  *						    > ZOMB
    127  *						    > IDL (special cases)
    128  *
    129  *	 STOPPED ---> RUN		SUSPENDED --> RUN
    130  *	            > SLEEP
    131  *
    132  *	 SLEEP -----> ONPROC		IDL --------> RUN
    133  *		    > RUN			    > SUSPENDED
    134  *		    > STOPPED			    > STOPPED
    135  *						    > ONPROC (special cases)
    136  *
    137  *	Some state transitions are only possible with kernel threads (eg
    138  *	ONPROC -> IDL) and happen under tightly controlled circumstances
    139  *	free of unwanted side effects.
    140  *
    141  * Migration
    142  *
    143  *	Migration of threads from one CPU to another could be performed
    144  *	internally by the scheduler via sched_takecpu() or sched_catchlwp()
    145  *	functions.  The universal lwp_migrate() function should be used for
    146  *	any other cases.  Subsystems in the kernel must be aware that CPU
    147  *	of LWP may change, while it is not locked.
    148  *
    149  * Locking
    150  *
    151  *	The majority of fields in 'struct lwp' are covered by a single,
    152  *	general spin lock pointed to by lwp::l_mutex.  The locks covering
    153  *	each field are documented in sys/lwp.h.
    154  *
    155  *	State transitions must be made with the LWP's general lock held,
    156  *	and may cause the LWP's lock pointer to change.  Manipulation of
    157  *	the general lock is not performed directly, but through calls to
    158  *	lwp_lock(), lwp_unlock() and others.  It should be noted that the
    159  *	adaptive locks are not allowed to be released while the LWP's lock
    160  *	is being held (unlike for other spin-locks).
    161  *
    162  *	States and their associated locks:
    163  *
    164  *	LSONPROC, LSZOMB:
    165  *
    166  *		Always covered by spc_lwplock, which protects running LWPs.
    167  *		This is a per-CPU lock and matches lwp::l_cpu.
    168  *
    169  *	LSIDL, LSRUN:
    170  *
    171  *		Always covered by spc_mutex, which protects the run queues.
    172  *		This is a per-CPU lock and matches lwp::l_cpu.
    173  *
    174  *	LSSLEEP:
    175  *
    176  *		Covered by a lock associated with the sleep queue that the
    177  *		LWP resides on.  Matches lwp::l_sleepq::sq_mutex.
    178  *
    179  *	LSSTOP, LSSUSPENDED:
    180  *
    181  *		If the LWP was previously sleeping (l_wchan != NULL), then
    182  *		l_mutex references the sleep queue lock.  If the LWP was
    183  *		runnable or on the CPU when halted, or has been removed from
    184  *		the sleep queue since halted, then the lock is spc_lwplock.
    185  *
    186  *	The lock order is as follows:
    187  *
    188  *		spc::spc_lwplock ->
    189  *		    sleeptab::st_mutex ->
    190  *			tschain_t::tc_mutex ->
    191  *			    spc::spc_mutex
    192  *
    193  *	Each process has an scheduler state lock (proc::p_lock), and a
    194  *	number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
    195  *	so on.  When an LWP is to be entered into or removed from one of the
    196  *	following states, p_lock must be held and the process wide counters
    197  *	adjusted:
    198  *
    199  *		LSIDL, LSZOMB, LSSTOP, LSSUSPENDED
    200  *
    201  *	(But not always for kernel threads.  There are some special cases
    202  *	as mentioned above.  See kern_softint.c.)
    203  *
    204  *	Note that an LWP is considered running or likely to run soon if in
    205  *	one of the following states.  This affects the value of p_nrlwps:
    206  *
    207  *		LSRUN, LSONPROC, LSSLEEP
    208  *
    209  *	p_lock does not need to be held when transitioning among these
    210  *	three states, hence p_lock is rarely taken for state transitions.
    211  */
    212 
    213 #include <sys/cdefs.h>
    214 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.196 2019/03/01 09:02:03 hannken Exp $");
    215 
    216 #include "opt_ddb.h"
    217 #include "opt_lockdebug.h"
    218 #include "opt_dtrace.h"
    219 
    220 #define _LWP_API_PRIVATE
    221 
    222 #include <sys/param.h>
    223 #include <sys/systm.h>
    224 #include <sys/cpu.h>
    225 #include <sys/pool.h>
    226 #include <sys/proc.h>
    227 #include <sys/syscallargs.h>
    228 #include <sys/syscall_stats.h>
    229 #include <sys/kauth.h>
    230 #include <sys/pserialize.h>
    231 #include <sys/sleepq.h>
    232 #include <sys/lockdebug.h>
    233 #include <sys/kmem.h>
    234 #include <sys/pset.h>
    235 #include <sys/intr.h>
    236 #include <sys/lwpctl.h>
    237 #include <sys/atomic.h>
    238 #include <sys/filedesc.h>
    239 #include <sys/fstrans.h>
    240 #include <sys/dtrace_bsd.h>
    241 #include <sys/sdt.h>
    242 #include <sys/xcall.h>
    243 #include <sys/uidinfo.h>
    244 #include <sys/sysctl.h>
    245 
    246 #include <uvm/uvm_extern.h>
    247 #include <uvm/uvm_object.h>
    248 
    249 static pool_cache_t	lwp_cache	__read_mostly;
    250 struct lwplist		alllwp		__cacheline_aligned;
    251 
    252 static void		lwp_dtor(void *, void *);
    253 
    254 /* DTrace proc provider probes */
    255 SDT_PROVIDER_DEFINE(proc);
    256 
    257 SDT_PROBE_DEFINE1(proc, kernel, , lwp__create, "struct lwp *");
    258 SDT_PROBE_DEFINE1(proc, kernel, , lwp__start, "struct lwp *");
    259 SDT_PROBE_DEFINE1(proc, kernel, , lwp__exit, "struct lwp *");
    260 
    261 struct turnstile turnstile0;
    262 struct lwp lwp0 __aligned(MIN_LWP_ALIGNMENT) = {
    263 #ifdef LWP0_CPU_INFO
    264 	.l_cpu = LWP0_CPU_INFO,
    265 #endif
    266 #ifdef LWP0_MD_INITIALIZER
    267 	.l_md = LWP0_MD_INITIALIZER,
    268 #endif
    269 	.l_proc = &proc0,
    270 	.l_lid = 1,
    271 	.l_flag = LW_SYSTEM,
    272 	.l_stat = LSONPROC,
    273 	.l_ts = &turnstile0,
    274 	.l_syncobj = &sched_syncobj,
    275 	.l_refcnt = 1,
    276 	.l_priority = PRI_USER + NPRI_USER - 1,
    277 	.l_inheritedprio = -1,
    278 	.l_class = SCHED_OTHER,
    279 	.l_psid = PS_NONE,
    280 	.l_pi_lenders = SLIST_HEAD_INITIALIZER(&lwp0.l_pi_lenders),
    281 	.l_name = __UNCONST("swapper"),
    282 	.l_fd = &filedesc0,
    283 };
    284 
    285 static int sysctl_kern_maxlwp(SYSCTLFN_PROTO);
    286 
    287 /*
    288  * sysctl helper routine for kern.maxlwp. Ensures that the new
    289  * values are not too low or too high.
    290  */
    291 static int
    292 sysctl_kern_maxlwp(SYSCTLFN_ARGS)
    293 {
    294 	int error, nmaxlwp;
    295 	struct sysctlnode node;
    296 
    297 	nmaxlwp = maxlwp;
    298 	node = *rnode;
    299 	node.sysctl_data = &nmaxlwp;
    300 	error = sysctl_lookup(SYSCTLFN_CALL(&node));
    301 	if (error || newp == NULL)
    302 		return error;
    303 
    304 	if (nmaxlwp < 0 || nmaxlwp >= 65536)
    305 		return EINVAL;
    306 	if (nmaxlwp > cpu_maxlwp())
    307 		return EINVAL;
    308 	maxlwp = nmaxlwp;
    309 
    310 	return 0;
    311 }
    312 
    313 static void
    314 sysctl_kern_lwp_setup(void)
    315 {
    316 	struct sysctllog *clog = NULL;
    317 
    318 	sysctl_createv(&clog, 0, NULL, NULL,
    319 		       CTLFLAG_PERMANENT|CTLFLAG_READWRITE,
    320 		       CTLTYPE_INT, "maxlwp",
    321 		       SYSCTL_DESCR("Maximum number of simultaneous threads"),
    322 		       sysctl_kern_maxlwp, 0, NULL, 0,
    323 		       CTL_KERN, CTL_CREATE, CTL_EOL);
    324 }
    325 
    326 void
    327 lwpinit(void)
    328 {
    329 
    330 	LIST_INIT(&alllwp);
    331 	lwpinit_specificdata();
    332 	lwp_sys_init();
    333 	lwp_cache = pool_cache_init(sizeof(lwp_t), MIN_LWP_ALIGNMENT, 0, 0,
    334 	    "lwppl", NULL, IPL_NONE, NULL, lwp_dtor, NULL);
    335 
    336 	maxlwp = cpu_maxlwp();
    337 	sysctl_kern_lwp_setup();
    338 }
    339 
    340 void
    341 lwp0_init(void)
    342 {
    343 	struct lwp *l = &lwp0;
    344 
    345 	KASSERT((void *)uvm_lwp_getuarea(l) != NULL);
    346 	KASSERT(l->l_lid == proc0.p_nlwpid);
    347 
    348 	LIST_INSERT_HEAD(&alllwp, l, l_list);
    349 
    350 	callout_init(&l->l_timeout_ch, CALLOUT_MPSAFE);
    351 	callout_setfunc(&l->l_timeout_ch, sleepq_timeout, l);
    352 	cv_init(&l->l_sigcv, "sigwait");
    353 	cv_init(&l->l_waitcv, "vfork");
    354 
    355 	kauth_cred_hold(proc0.p_cred);
    356 	l->l_cred = proc0.p_cred;
    357 
    358 	kdtrace_thread_ctor(NULL, l);
    359 	lwp_initspecific(l);
    360 
    361 	SYSCALL_TIME_LWP_INIT(l);
    362 }
    363 
    364 static void
    365 lwp_dtor(void *arg, void *obj)
    366 {
    367 	lwp_t *l = obj;
    368 	uint64_t where;
    369 	(void)l;
    370 
    371 	/*
    372 	 * Provide a barrier to ensure that all mutex_oncpu() and rw_oncpu()
    373 	 * calls will exit before memory of LWP is returned to the pool, where
    374 	 * KVA of LWP structure might be freed and re-used for other purposes.
    375 	 * Kernel preemption is disabled around mutex_oncpu() and rw_oncpu()
    376 	 * callers, therefore cross-call to all CPUs will do the job.  Also,
    377 	 * the value of l->l_cpu must be still valid at this point.
    378 	 */
    379 	KASSERT(l->l_cpu != NULL);
    380 	where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL);
    381 	xc_wait(where);
    382 }
    383 
    384 /*
    385  * Set an suspended.
    386  *
    387  * Must be called with p_lock held, and the LWP locked.  Will unlock the
    388  * LWP before return.
    389  */
    390 int
    391 lwp_suspend(struct lwp *curl, struct lwp *t)
    392 {
    393 	int error;
    394 
    395 	KASSERT(mutex_owned(t->l_proc->p_lock));
    396 	KASSERT(lwp_locked(t, NULL));
    397 
    398 	KASSERT(curl != t || curl->l_stat == LSONPROC);
    399 
    400 	/*
    401 	 * If the current LWP has been told to exit, we must not suspend anyone
    402 	 * else or deadlock could occur.  We won't return to userspace.
    403 	 */
    404 	if ((curl->l_flag & (LW_WEXIT | LW_WCORE)) != 0) {
    405 		lwp_unlock(t);
    406 		return (EDEADLK);
    407 	}
    408 
    409 	error = 0;
    410 
    411 	switch (t->l_stat) {
    412 	case LSRUN:
    413 	case LSONPROC:
    414 		t->l_flag |= LW_WSUSPEND;
    415 		lwp_need_userret(t);
    416 		lwp_unlock(t);
    417 		break;
    418 
    419 	case LSSLEEP:
    420 		t->l_flag |= LW_WSUSPEND;
    421 
    422 		/*
    423 		 * Kick the LWP and try to get it to the kernel boundary
    424 		 * so that it will release any locks that it holds.
    425 		 * setrunnable() will release the lock.
    426 		 */
    427 		if ((t->l_flag & LW_SINTR) != 0)
    428 			setrunnable(t);
    429 		else
    430 			lwp_unlock(t);
    431 		break;
    432 
    433 	case LSSUSPENDED:
    434 		lwp_unlock(t);
    435 		break;
    436 
    437 	case LSSTOP:
    438 		t->l_flag |= LW_WSUSPEND;
    439 		setrunnable(t);
    440 		break;
    441 
    442 	case LSIDL:
    443 	case LSZOMB:
    444 		error = EINTR; /* It's what Solaris does..... */
    445 		lwp_unlock(t);
    446 		break;
    447 	}
    448 
    449 	return (error);
    450 }
    451 
    452 /*
    453  * Restart a suspended LWP.
    454  *
    455  * Must be called with p_lock held, and the LWP locked.  Will unlock the
    456  * LWP before return.
    457  */
    458 void
    459 lwp_continue(struct lwp *l)
    460 {
    461 
    462 	KASSERT(mutex_owned(l->l_proc->p_lock));
    463 	KASSERT(lwp_locked(l, NULL));
    464 
    465 	/* If rebooting or not suspended, then just bail out. */
    466 	if ((l->l_flag & LW_WREBOOT) != 0) {
    467 		lwp_unlock(l);
    468 		return;
    469 	}
    470 
    471 	l->l_flag &= ~LW_WSUSPEND;
    472 
    473 	if (l->l_stat != LSSUSPENDED) {
    474 		lwp_unlock(l);
    475 		return;
    476 	}
    477 
    478 	/* setrunnable() will release the lock. */
    479 	setrunnable(l);
    480 }
    481 
    482 /*
    483  * Restart a stopped LWP.
    484  *
    485  * Must be called with p_lock held, and the LWP NOT locked.  Will unlock the
    486  * LWP before return.
    487  */
    488 void
    489 lwp_unstop(struct lwp *l)
    490 {
    491 	struct proc *p = l->l_proc;
    492 
    493 	KASSERT(mutex_owned(proc_lock));
    494 	KASSERT(mutex_owned(p->p_lock));
    495 
    496 	lwp_lock(l);
    497 
    498 	/* If not stopped, then just bail out. */
    499 	if (l->l_stat != LSSTOP) {
    500 		lwp_unlock(l);
    501 		return;
    502 	}
    503 
    504 	p->p_stat = SACTIVE;
    505 	p->p_sflag &= ~PS_STOPPING;
    506 
    507 	if (!p->p_waited)
    508 		p->p_pptr->p_nstopchild--;
    509 
    510 	if (l->l_wchan == NULL) {
    511 		/* setrunnable() will release the lock. */
    512 		setrunnable(l);
    513 	} else if (p->p_xsig && (l->l_flag & LW_SINTR) != 0) {
    514 		/* setrunnable() so we can receive the signal */
    515 		setrunnable(l);
    516 	} else {
    517 		l->l_stat = LSSLEEP;
    518 		p->p_nrlwps++;
    519 		lwp_unlock(l);
    520 	}
    521 }
    522 
    523 /*
    524  * Wait for an LWP within the current process to exit.  If 'lid' is
    525  * non-zero, we are waiting for a specific LWP.
    526  *
    527  * Must be called with p->p_lock held.
    528  */
    529 int
    530 lwp_wait(struct lwp *l, lwpid_t lid, lwpid_t *departed, bool exiting)
    531 {
    532 	const lwpid_t curlid = l->l_lid;
    533 	proc_t *p = l->l_proc;
    534 	lwp_t *l2;
    535 	int error;
    536 
    537 	KASSERT(mutex_owned(p->p_lock));
    538 
    539 	p->p_nlwpwait++;
    540 	l->l_waitingfor = lid;
    541 
    542 	for (;;) {
    543 		int nfound;
    544 
    545 		/*
    546 		 * Avoid a race between exit1() and sigexit(): if the
    547 		 * process is dumping core, then we need to bail out: call
    548 		 * into lwp_userret() where we will be suspended until the
    549 		 * deed is done.
    550 		 */
    551 		if ((p->p_sflag & PS_WCORE) != 0) {
    552 			mutex_exit(p->p_lock);
    553 			lwp_userret(l);
    554 			KASSERT(false);
    555 		}
    556 
    557 		/*
    558 		 * First off, drain any detached LWP that is waiting to be
    559 		 * reaped.
    560 		 */
    561 		while ((l2 = p->p_zomblwp) != NULL) {
    562 			p->p_zomblwp = NULL;
    563 			lwp_free(l2, false, false);/* releases proc mutex */
    564 			mutex_enter(p->p_lock);
    565 		}
    566 
    567 		/*
    568 		 * Now look for an LWP to collect.  If the whole process is
    569 		 * exiting, count detached LWPs as eligible to be collected,
    570 		 * but don't drain them here.
    571 		 */
    572 		nfound = 0;
    573 		error = 0;
    574 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    575 			/*
    576 			 * If a specific wait and the target is waiting on
    577 			 * us, then avoid deadlock.  This also traps LWPs
    578 			 * that try to wait on themselves.
    579 			 *
    580 			 * Note that this does not handle more complicated
    581 			 * cycles, like: t1 -> t2 -> t3 -> t1.  The process
    582 			 * can still be killed so it is not a major problem.
    583 			 */
    584 			if (l2->l_lid == lid && l2->l_waitingfor == curlid) {
    585 				error = EDEADLK;
    586 				break;
    587 			}
    588 			if (l2 == l)
    589 				continue;
    590 			if ((l2->l_prflag & LPR_DETACHED) != 0) {
    591 				nfound += exiting;
    592 				continue;
    593 			}
    594 			if (lid != 0) {
    595 				if (l2->l_lid != lid)
    596 					continue;
    597 				/*
    598 				 * Mark this LWP as the first waiter, if there
    599 				 * is no other.
    600 				 */
    601 				if (l2->l_waiter == 0)
    602 					l2->l_waiter = curlid;
    603 			} else if (l2->l_waiter != 0) {
    604 				/*
    605 				 * It already has a waiter - so don't
    606 				 * collect it.  If the waiter doesn't
    607 				 * grab it we'll get another chance
    608 				 * later.
    609 				 */
    610 				nfound++;
    611 				continue;
    612 			}
    613 			nfound++;
    614 
    615 			/* No need to lock the LWP in order to see LSZOMB. */
    616 			if (l2->l_stat != LSZOMB)
    617 				continue;
    618 
    619 			/*
    620 			 * We're no longer waiting.  Reset the "first waiter"
    621 			 * pointer on the target, in case it was us.
    622 			 */
    623 			l->l_waitingfor = 0;
    624 			l2->l_waiter = 0;
    625 			p->p_nlwpwait--;
    626 			if (departed)
    627 				*departed = l2->l_lid;
    628 			sched_lwp_collect(l2);
    629 
    630 			/* lwp_free() releases the proc lock. */
    631 			lwp_free(l2, false, false);
    632 			mutex_enter(p->p_lock);
    633 			return 0;
    634 		}
    635 
    636 		if (error != 0)
    637 			break;
    638 		if (nfound == 0) {
    639 			error = ESRCH;
    640 			break;
    641 		}
    642 
    643 		/*
    644 		 * Note: since the lock will be dropped, need to restart on
    645 		 * wakeup to run all LWPs again, e.g. there may be new LWPs.
    646 		 */
    647 		if (exiting) {
    648 			KASSERT(p->p_nlwps > 1);
    649 			cv_wait(&p->p_lwpcv, p->p_lock);
    650 			error = EAGAIN;
    651 			break;
    652 		}
    653 
    654 		/*
    655 		 * If all other LWPs are waiting for exits or suspends
    656 		 * and the supply of zombies and potential zombies is
    657 		 * exhausted, then we are about to deadlock.
    658 		 *
    659 		 * If the process is exiting (and this LWP is not the one
    660 		 * that is coordinating the exit) then bail out now.
    661 		 */
    662 		if ((p->p_sflag & PS_WEXIT) != 0 ||
    663 		    p->p_nrlwps + p->p_nzlwps - p->p_ndlwps <= p->p_nlwpwait) {
    664 			error = EDEADLK;
    665 			break;
    666 		}
    667 
    668 		/*
    669 		 * Sit around and wait for something to happen.  We'll be
    670 		 * awoken if any of the conditions examined change: if an
    671 		 * LWP exits, is collected, or is detached.
    672 		 */
    673 		if ((error = cv_wait_sig(&p->p_lwpcv, p->p_lock)) != 0)
    674 			break;
    675 	}
    676 
    677 	/*
    678 	 * We didn't find any LWPs to collect, we may have received a
    679 	 * signal, or some other condition has caused us to bail out.
    680 	 *
    681 	 * If waiting on a specific LWP, clear the waiters marker: some
    682 	 * other LWP may want it.  Then, kick all the remaining waiters
    683 	 * so that they can re-check for zombies and for deadlock.
    684 	 */
    685 	if (lid != 0) {
    686 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    687 			if (l2->l_lid == lid) {
    688 				if (l2->l_waiter == curlid)
    689 					l2->l_waiter = 0;
    690 				break;
    691 			}
    692 		}
    693 	}
    694 	p->p_nlwpwait--;
    695 	l->l_waitingfor = 0;
    696 	cv_broadcast(&p->p_lwpcv);
    697 
    698 	return error;
    699 }
    700 
    701 static lwpid_t
    702 lwp_find_free_lid(lwpid_t try_lid, lwp_t * new_lwp, proc_t *p)
    703 {
    704 	#define LID_SCAN (1u << 31)
    705 	lwp_t *scan, *free_before;
    706 	lwpid_t nxt_lid;
    707 
    708 	/*
    709 	 * We want the first unused lid greater than or equal to
    710 	 * try_lid (modulo 2^31).
    711 	 * (If nothing else ld.elf_so doesn't want lwpid with the top bit set.)
    712 	 * We must not return 0, and avoiding 'LID_SCAN - 1' makes
    713 	 * the outer test easier.
    714 	 * This would be much easier if the list were sorted in
    715 	 * increasing order.
    716 	 * The list is kept sorted in decreasing order.
    717 	 * This code is only used after a process has generated 2^31 lwp.
    718 	 *
    719 	 * Code assumes it can always find an id.
    720 	 */
    721 
    722 	try_lid &= LID_SCAN - 1;
    723 	if (try_lid <= 1)
    724 		try_lid = 2;
    725 
    726 	free_before = NULL;
    727 	nxt_lid = LID_SCAN - 1;
    728 	LIST_FOREACH(scan, &p->p_lwps, l_sibling) {
    729 		if (scan->l_lid != nxt_lid) {
    730 			/* There are available lid before this entry */
    731 			free_before = scan;
    732 			if (try_lid > scan->l_lid)
    733 				break;
    734 		}
    735 		if (try_lid == scan->l_lid) {
    736 			/* The ideal lid is busy, take a higher one */
    737 			if (free_before != NULL) {
    738 				try_lid = free_before->l_lid + 1;
    739 				break;
    740 			}
    741 			/* No higher ones, reuse low numbers */
    742 			try_lid = 2;
    743 		}
    744 
    745 		nxt_lid = scan->l_lid - 1;
    746 		if (LIST_NEXT(scan, l_sibling) == NULL) {
    747 		    /* The value we have is lower than any existing lwp */
    748 		    LIST_INSERT_AFTER(scan, new_lwp, l_sibling);
    749 		    return try_lid;
    750 		}
    751 	}
    752 
    753 	LIST_INSERT_BEFORE(free_before, new_lwp, l_sibling);
    754 	return try_lid;
    755 }
    756 
    757 /*
    758  * Create a new LWP within process 'p2', using LWP 'l1' as a template.
    759  * The new LWP is created in state LSIDL and must be set running,
    760  * suspended, or stopped by the caller.
    761  */
    762 int
    763 lwp_create(lwp_t *l1, proc_t *p2, vaddr_t uaddr, int flags,
    764     void *stack, size_t stacksize, void (*func)(void *), void *arg,
    765     lwp_t **rnewlwpp, int sclass, const sigset_t *sigmask,
    766     const stack_t *sigstk)
    767 {
    768 	struct lwp *l2, *isfree;
    769 	turnstile_t *ts;
    770 	lwpid_t lid;
    771 
    772 	KASSERT(l1 == curlwp || l1->l_proc == &proc0);
    773 
    774 	/*
    775 	 * Enforce limits, excluding the first lwp and kthreads.
    776 	 */
    777 	if (p2->p_nlwps != 0 && p2 != &proc0) {
    778 		uid_t uid = kauth_cred_getuid(l1->l_cred);
    779 		int count = chglwpcnt(uid, 1);
    780 		if (__predict_false(count >
    781 		    p2->p_rlimit[RLIMIT_NTHR].rlim_cur)) {
    782 			if (kauth_authorize_process(l1->l_cred,
    783 			    KAUTH_PROCESS_RLIMIT, p2,
    784 			    KAUTH_ARG(KAUTH_REQ_PROCESS_RLIMIT_BYPASS),
    785 			    &p2->p_rlimit[RLIMIT_NTHR], KAUTH_ARG(RLIMIT_NTHR))
    786 			    != 0) {
    787 				(void)chglwpcnt(uid, -1);
    788 				return EAGAIN;
    789 			}
    790 		}
    791 	}
    792 
    793 	/*
    794 	 * First off, reap any detached LWP waiting to be collected.
    795 	 * We can re-use its LWP structure and turnstile.
    796 	 */
    797 	isfree = NULL;
    798 	if (p2->p_zomblwp != NULL) {
    799 		mutex_enter(p2->p_lock);
    800 		if ((isfree = p2->p_zomblwp) != NULL) {
    801 			p2->p_zomblwp = NULL;
    802 			lwp_free(isfree, true, false);/* releases proc mutex */
    803 		} else
    804 			mutex_exit(p2->p_lock);
    805 	}
    806 	if (isfree == NULL) {
    807 		l2 = pool_cache_get(lwp_cache, PR_WAITOK);
    808 		memset(l2, 0, sizeof(*l2));
    809 		l2->l_ts = pool_cache_get(turnstile_cache, PR_WAITOK);
    810 		SLIST_INIT(&l2->l_pi_lenders);
    811 	} else {
    812 		l2 = isfree;
    813 		ts = l2->l_ts;
    814 		KASSERT(l2->l_inheritedprio == -1);
    815 		KASSERT(SLIST_EMPTY(&l2->l_pi_lenders));
    816 		memset(l2, 0, sizeof(*l2));
    817 		l2->l_ts = ts;
    818 	}
    819 
    820 	l2->l_stat = LSIDL;
    821 	l2->l_proc = p2;
    822 	l2->l_refcnt = 1;
    823 	l2->l_class = sclass;
    824 
    825 	/*
    826 	 * If vfork(), we want the LWP to run fast and on the same CPU
    827 	 * as its parent, so that it can reuse the VM context and cache
    828 	 * footprint on the local CPU.
    829 	 */
    830 	l2->l_kpriority = ((flags & LWP_VFORK) ? true : false);
    831 	l2->l_kpribase = PRI_KERNEL;
    832 	l2->l_priority = l1->l_priority;
    833 	l2->l_inheritedprio = -1;
    834 	l2->l_protectprio = -1;
    835 	l2->l_auxprio = -1;
    836 	l2->l_flag = 0;
    837 	l2->l_pflag = LP_MPSAFE;
    838 	TAILQ_INIT(&l2->l_ld_locks);
    839 
    840 	/*
    841 	 * For vfork, borrow parent's lwpctl context if it exists.
    842 	 * This also causes us to return via lwp_userret.
    843 	 */
    844 	if (flags & LWP_VFORK && l1->l_lwpctl) {
    845 		l2->l_lwpctl = l1->l_lwpctl;
    846 		l2->l_flag |= LW_LWPCTL;
    847 	}
    848 
    849 	/*
    850 	 * If not the first LWP in the process, grab a reference to the
    851 	 * descriptor table.
    852 	 */
    853 	l2->l_fd = p2->p_fd;
    854 	if (p2->p_nlwps != 0) {
    855 		KASSERT(l1->l_proc == p2);
    856 		fd_hold(l2);
    857 	} else {
    858 		KASSERT(l1->l_proc != p2);
    859 	}
    860 
    861 	if (p2->p_flag & PK_SYSTEM) {
    862 		/* Mark it as a system LWP. */
    863 		l2->l_flag |= LW_SYSTEM;
    864 	}
    865 
    866 	kpreempt_disable();
    867 	l2->l_mutex = l1->l_cpu->ci_schedstate.spc_mutex;
    868 	l2->l_cpu = l1->l_cpu;
    869 	kpreempt_enable();
    870 
    871 	kdtrace_thread_ctor(NULL, l2);
    872 	lwp_initspecific(l2);
    873 	sched_lwp_fork(l1, l2);
    874 	lwp_update_creds(l2);
    875 	callout_init(&l2->l_timeout_ch, CALLOUT_MPSAFE);
    876 	callout_setfunc(&l2->l_timeout_ch, sleepq_timeout, l2);
    877 	cv_init(&l2->l_sigcv, "sigwait");
    878 	cv_init(&l2->l_waitcv, "vfork");
    879 	l2->l_syncobj = &sched_syncobj;
    880 
    881 	if (rnewlwpp != NULL)
    882 		*rnewlwpp = l2;
    883 
    884 	/*
    885 	 * PCU state needs to be saved before calling uvm_lwp_fork() so that
    886 	 * the MD cpu_lwp_fork() can copy the saved state to the new LWP.
    887 	 */
    888 	pcu_save_all(l1);
    889 
    890 	uvm_lwp_setuarea(l2, uaddr);
    891 	uvm_lwp_fork(l1, l2, stack, stacksize, func, (arg != NULL) ? arg : l2);
    892 
    893 	if ((flags & LWP_PIDLID) != 0) {
    894 		lid = proc_alloc_pid(p2);
    895 		l2->l_pflag |= LP_PIDLID;
    896 	} else {
    897 		lid = 0;
    898 	}
    899 
    900 	mutex_enter(p2->p_lock);
    901 
    902 	if ((flags & LWP_DETACHED) != 0) {
    903 		l2->l_prflag = LPR_DETACHED;
    904 		p2->p_ndlwps++;
    905 	} else
    906 		l2->l_prflag = 0;
    907 
    908 	l2->l_sigstk = *sigstk;
    909 	l2->l_sigmask = *sigmask;
    910 	TAILQ_INIT(&l2->l_sigpend.sp_info);
    911 	sigemptyset(&l2->l_sigpend.sp_set);
    912 
    913 	if (__predict_true(lid == 0)) {
    914 		/*
    915 		 * XXX: l_lid are expected to be unique (for a process)
    916 		 * if LWP_PIDLID is sometimes set this won't be true.
    917 		 * Once 2^31 threads have been allocated we have to
    918 		 * scan to ensure we allocate a unique value.
    919 		 */
    920 		lid = ++p2->p_nlwpid;
    921 		if (__predict_false(lid & LID_SCAN)) {
    922 			lid = lwp_find_free_lid(lid, l2, p2);
    923 			p2->p_nlwpid = lid | LID_SCAN;
    924 			/* l2 as been inserted into p_lwps in order */
    925 			goto skip_insert;
    926 		}
    927 		p2->p_nlwpid = lid;
    928 	}
    929 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    930     skip_insert:
    931 	l2->l_lid = lid;
    932 	p2->p_nlwps++;
    933 	p2->p_nrlwps++;
    934 
    935 	KASSERT(l2->l_affinity == NULL);
    936 
    937 	if ((p2->p_flag & PK_SYSTEM) == 0) {
    938 		/* Inherit the affinity mask. */
    939 		if (l1->l_affinity) {
    940 			/*
    941 			 * Note that we hold the state lock while inheriting
    942 			 * the affinity to avoid race with sched_setaffinity().
    943 			 */
    944 			lwp_lock(l1);
    945 			if (l1->l_affinity) {
    946 				kcpuset_use(l1->l_affinity);
    947 				l2->l_affinity = l1->l_affinity;
    948 			}
    949 			lwp_unlock(l1);
    950 		}
    951 		lwp_lock(l2);
    952 		/* Inherit a processor-set */
    953 		l2->l_psid = l1->l_psid;
    954 		/* Look for a CPU to start */
    955 		l2->l_cpu = sched_takecpu(l2);
    956 		lwp_unlock_to(l2, l2->l_cpu->ci_schedstate.spc_mutex);
    957 	}
    958 	mutex_exit(p2->p_lock);
    959 
    960 	SDT_PROBE(proc, kernel, , lwp__create, l2, 0, 0, 0, 0);
    961 
    962 	mutex_enter(proc_lock);
    963 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    964 	mutex_exit(proc_lock);
    965 
    966 	SYSCALL_TIME_LWP_INIT(l2);
    967 
    968 	if (p2->p_emul->e_lwp_fork)
    969 		(*p2->p_emul->e_lwp_fork)(l1, l2);
    970 
    971 	/* If the process is traced, report lwp creation to a debugger */
    972 	if ((p2->p_slflag & (PSL_TRACED|PSL_TRACELWP_CREATE|PSL_SYSCALL)) ==
    973 	    (PSL_TRACED|PSL_TRACELWP_CREATE)) {
    974 		ksiginfo_t ksi;
    975 
    976 		/* Tracing */
    977 		KASSERT((l2->l_flag & LW_SYSTEM) == 0);
    978 
    979 		p2->p_lwp_created = l2->l_lid;
    980 
    981 		KSI_INIT_EMPTY(&ksi);
    982 		ksi.ksi_signo = SIGTRAP;
    983 		ksi.ksi_code = TRAP_LWP;
    984 		mutex_enter(proc_lock);
    985 		kpsignal(p2, &ksi, NULL);
    986 		mutex_exit(proc_lock);
    987 	}
    988 
    989 	return (0);
    990 }
    991 
    992 /*
    993  * Called by MD code when a new LWP begins execution.  Must be called
    994  * with the previous LWP locked (so at splsched), or if there is no
    995  * previous LWP, at splsched.
    996  */
    997 void
    998 lwp_startup(struct lwp *prev, struct lwp *new_lwp)
    999 {
   1000 	KASSERTMSG(new_lwp == curlwp, "l %p curlwp %p prevlwp %p", new_lwp, curlwp, prev);
   1001 
   1002 	SDT_PROBE(proc, kernel, , lwp__start, new_lwp, 0, 0, 0, 0);
   1003 
   1004 	KASSERT(kpreempt_disabled());
   1005 	if (prev != NULL) {
   1006 		/*
   1007 		 * Normalize the count of the spin-mutexes, it was
   1008 		 * increased in mi_switch().  Unmark the state of
   1009 		 * context switch - it is finished for previous LWP.
   1010 		 */
   1011 		curcpu()->ci_mtx_count++;
   1012 		membar_exit();
   1013 		prev->l_ctxswtch = 0;
   1014 	}
   1015 	KPREEMPT_DISABLE(new_lwp);
   1016 	if (__predict_true(new_lwp->l_proc->p_vmspace))
   1017 		pmap_activate(new_lwp);
   1018 	spl0();
   1019 
   1020 	/* Note trip through cpu_switchto(). */
   1021 	pserialize_switchpoint();
   1022 
   1023 	LOCKDEBUG_BARRIER(NULL, 0);
   1024 	KPREEMPT_ENABLE(new_lwp);
   1025 	if ((new_lwp->l_pflag & LP_MPSAFE) == 0) {
   1026 		KERNEL_LOCK(1, new_lwp);
   1027 	}
   1028 }
   1029 
   1030 /*
   1031  * Exit an LWP.
   1032  */
   1033 void
   1034 lwp_exit(struct lwp *l)
   1035 {
   1036 	struct proc *p = l->l_proc;
   1037 	struct lwp *l2;
   1038 	bool current;
   1039 
   1040 	current = (l == curlwp);
   1041 
   1042 	KASSERT(current || (l->l_stat == LSIDL && l->l_target_cpu == NULL));
   1043 	KASSERT(p == curproc);
   1044 
   1045 	SDT_PROBE(proc, kernel, , lwp__exit, l, 0, 0, 0, 0);
   1046 
   1047 	/*
   1048 	 * Verify that we hold no locks other than the kernel lock.
   1049 	 */
   1050 	LOCKDEBUG_BARRIER(&kernel_lock, 0);
   1051 
   1052 	/* If the process is traced, report lwp termination to a debugger */
   1053 	if ((p->p_slflag & (PSL_TRACED|PSL_TRACELWP_EXIT|PSL_SYSCALL)) ==
   1054 	    (PSL_TRACED|PSL_TRACELWP_EXIT)) {
   1055 		ksiginfo_t ksi;
   1056 
   1057 		/* Tracing */
   1058 		KASSERT((l->l_flag & LW_SYSTEM) == 0);
   1059 
   1060 		p->p_lwp_exited = l->l_lid;
   1061 
   1062 		KSI_INIT_EMPTY(&ksi);
   1063 		ksi.ksi_signo = SIGTRAP;
   1064 		ksi.ksi_code = TRAP_LWP;
   1065 		mutex_enter(proc_lock);
   1066 		kpsignal(p, &ksi, NULL);
   1067 		mutex_exit(proc_lock);
   1068 	}
   1069 
   1070 	/*
   1071 	 * If we are the last live LWP in a process, we need to exit the
   1072 	 * entire process.  We do so with an exit status of zero, because
   1073 	 * it's a "controlled" exit, and because that's what Solaris does.
   1074 	 *
   1075 	 * We are not quite a zombie yet, but for accounting purposes we
   1076 	 * must increment the count of zombies here.
   1077 	 *
   1078 	 * Note: the last LWP's specificdata will be deleted here.
   1079 	 */
   1080 	mutex_enter(p->p_lock);
   1081 	if (p->p_nlwps - p->p_nzlwps == 1) {
   1082 		KASSERT(current == true);
   1083 		KASSERT(p != &proc0);
   1084 		/* XXXSMP kernel_lock not held */
   1085 		exit1(l, 0, 0);
   1086 		/* NOTREACHED */
   1087 	}
   1088 	p->p_nzlwps++;
   1089 	mutex_exit(p->p_lock);
   1090 
   1091 	if (p->p_emul->e_lwp_exit)
   1092 		(*p->p_emul->e_lwp_exit)(l);
   1093 
   1094 	/* Drop filedesc reference. */
   1095 	fd_free();
   1096 
   1097 	/* Release fstrans private data. */
   1098 	fstrans_lwp_dtor(l);
   1099 
   1100 	/* Delete the specificdata while it's still safe to sleep. */
   1101 	lwp_finispecific(l);
   1102 
   1103 	/*
   1104 	 * Release our cached credentials.
   1105 	 */
   1106 	kauth_cred_free(l->l_cred);
   1107 	callout_destroy(&l->l_timeout_ch);
   1108 
   1109 	/*
   1110 	 * Remove the LWP from the global list.
   1111 	 * Free its LID from the PID namespace if needed.
   1112 	 */
   1113 	mutex_enter(proc_lock);
   1114 	LIST_REMOVE(l, l_list);
   1115 	if ((l->l_pflag & LP_PIDLID) != 0 && l->l_lid != p->p_pid) {
   1116 		proc_free_pid(l->l_lid);
   1117 	}
   1118 	mutex_exit(proc_lock);
   1119 
   1120 	/*
   1121 	 * Get rid of all references to the LWP that others (e.g. procfs)
   1122 	 * may have, and mark the LWP as a zombie.  If the LWP is detached,
   1123 	 * mark it waiting for collection in the proc structure.  Note that
   1124 	 * before we can do that, we need to free any other dead, deatched
   1125 	 * LWP waiting to meet its maker.
   1126 	 */
   1127 	mutex_enter(p->p_lock);
   1128 	lwp_drainrefs(l);
   1129 
   1130 	if ((l->l_prflag & LPR_DETACHED) != 0) {
   1131 		while ((l2 = p->p_zomblwp) != NULL) {
   1132 			p->p_zomblwp = NULL;
   1133 			lwp_free(l2, false, false);/* releases proc mutex */
   1134 			mutex_enter(p->p_lock);
   1135 			l->l_refcnt++;
   1136 			lwp_drainrefs(l);
   1137 		}
   1138 		p->p_zomblwp = l;
   1139 	}
   1140 
   1141 	/*
   1142 	 * If we find a pending signal for the process and we have been
   1143 	 * asked to check for signals, then we lose: arrange to have
   1144 	 * all other LWPs in the process check for signals.
   1145 	 */
   1146 	if ((l->l_flag & LW_PENDSIG) != 0 &&
   1147 	    firstsig(&p->p_sigpend.sp_set) != 0) {
   1148 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
   1149 			lwp_lock(l2);
   1150 			l2->l_flag |= LW_PENDSIG;
   1151 			lwp_unlock(l2);
   1152 		}
   1153 	}
   1154 
   1155 	/*
   1156 	 * Release any PCU resources before becoming a zombie.
   1157 	 */
   1158 	pcu_discard_all(l);
   1159 
   1160 	lwp_lock(l);
   1161 	l->l_stat = LSZOMB;
   1162 	if (l->l_name != NULL) {
   1163 		strcpy(l->l_name, "(zombie)");
   1164 	}
   1165 	lwp_unlock(l);
   1166 	p->p_nrlwps--;
   1167 	cv_broadcast(&p->p_lwpcv);
   1168 	if (l->l_lwpctl != NULL)
   1169 		l->l_lwpctl->lc_curcpu = LWPCTL_CPU_EXITED;
   1170 	mutex_exit(p->p_lock);
   1171 
   1172 	/*
   1173 	 * We can no longer block.  At this point, lwp_free() may already
   1174 	 * be gunning for us.  On a multi-CPU system, we may be off p_lwps.
   1175 	 *
   1176 	 * Free MD LWP resources.
   1177 	 */
   1178 	cpu_lwp_free(l, 0);
   1179 
   1180 	if (current) {
   1181 		pmap_deactivate(l);
   1182 
   1183 		/*
   1184 		 * Release the kernel lock, and switch away into
   1185 		 * oblivion.
   1186 		 */
   1187 #ifdef notyet
   1188 		/* XXXSMP hold in lwp_userret() */
   1189 		KERNEL_UNLOCK_LAST(l);
   1190 #else
   1191 		KERNEL_UNLOCK_ALL(l, NULL);
   1192 #endif
   1193 		lwp_exit_switchaway(l);
   1194 	}
   1195 }
   1196 
   1197 /*
   1198  * Free a dead LWP's remaining resources.
   1199  *
   1200  * XXXLWP limits.
   1201  */
   1202 void
   1203 lwp_free(struct lwp *l, bool recycle, bool last)
   1204 {
   1205 	struct proc *p = l->l_proc;
   1206 	struct rusage *ru;
   1207 	ksiginfoq_t kq;
   1208 
   1209 	KASSERT(l != curlwp);
   1210 	KASSERT(last || mutex_owned(p->p_lock));
   1211 
   1212 	/*
   1213 	 * We use the process credentials instead of the lwp credentials here
   1214 	 * because the lwp credentials maybe cached (just after a setuid call)
   1215 	 * and we don't want pay for syncing, since the lwp is going away
   1216 	 * anyway
   1217 	 */
   1218 	if (p != &proc0 && p->p_nlwps != 1)
   1219 		(void)chglwpcnt(kauth_cred_getuid(p->p_cred), -1);
   1220 	/*
   1221 	 * If this was not the last LWP in the process, then adjust
   1222 	 * counters and unlock.
   1223 	 */
   1224 	if (!last) {
   1225 		/*
   1226 		 * Add the LWP's run time to the process' base value.
   1227 		 * This needs to co-incide with coming off p_lwps.
   1228 		 */
   1229 		bintime_add(&p->p_rtime, &l->l_rtime);
   1230 		p->p_pctcpu += l->l_pctcpu;
   1231 		ru = &p->p_stats->p_ru;
   1232 		ruadd(ru, &l->l_ru);
   1233 		ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
   1234 		ru->ru_nivcsw += l->l_nivcsw;
   1235 		LIST_REMOVE(l, l_sibling);
   1236 		p->p_nlwps--;
   1237 		p->p_nzlwps--;
   1238 		if ((l->l_prflag & LPR_DETACHED) != 0)
   1239 			p->p_ndlwps--;
   1240 
   1241 		/*
   1242 		 * Have any LWPs sleeping in lwp_wait() recheck for
   1243 		 * deadlock.
   1244 		 */
   1245 		cv_broadcast(&p->p_lwpcv);
   1246 		mutex_exit(p->p_lock);
   1247 	}
   1248 
   1249 #ifdef MULTIPROCESSOR
   1250 	/*
   1251 	 * In the unlikely event that the LWP is still on the CPU,
   1252 	 * then spin until it has switched away.  We need to release
   1253 	 * all locks to avoid deadlock against interrupt handlers on
   1254 	 * the target CPU.
   1255 	 */
   1256 	if ((l->l_pflag & LP_RUNNING) != 0 || l->l_cpu->ci_curlwp == l) {
   1257 		int count;
   1258 		(void)count; /* XXXgcc */
   1259 		KERNEL_UNLOCK_ALL(curlwp, &count);
   1260 		while ((l->l_pflag & LP_RUNNING) != 0 ||
   1261 		    l->l_cpu->ci_curlwp == l)
   1262 			SPINLOCK_BACKOFF_HOOK;
   1263 		KERNEL_LOCK(count, curlwp);
   1264 	}
   1265 #endif
   1266 
   1267 	/*
   1268 	 * Destroy the LWP's remaining signal information.
   1269 	 */
   1270 	ksiginfo_queue_init(&kq);
   1271 	sigclear(&l->l_sigpend, NULL, &kq);
   1272 	ksiginfo_queue_drain(&kq);
   1273 	cv_destroy(&l->l_sigcv);
   1274 	cv_destroy(&l->l_waitcv);
   1275 
   1276 	/*
   1277 	 * Free lwpctl structure and affinity.
   1278 	 */
   1279 	if (l->l_lwpctl) {
   1280 		lwp_ctl_free(l);
   1281 	}
   1282 	if (l->l_affinity) {
   1283 		kcpuset_unuse(l->l_affinity, NULL);
   1284 		l->l_affinity = NULL;
   1285 	}
   1286 
   1287 	/*
   1288 	 * Free the LWP's turnstile and the LWP structure itself unless the
   1289 	 * caller wants to recycle them.  Also, free the scheduler specific
   1290 	 * data.
   1291 	 *
   1292 	 * We can't return turnstile0 to the pool (it didn't come from it),
   1293 	 * so if it comes up just drop it quietly and move on.
   1294 	 *
   1295 	 * We don't recycle the VM resources at this time.
   1296 	 */
   1297 
   1298 	if (!recycle && l->l_ts != &turnstile0)
   1299 		pool_cache_put(turnstile_cache, l->l_ts);
   1300 	if (l->l_name != NULL)
   1301 		kmem_free(l->l_name, MAXCOMLEN);
   1302 
   1303 	cpu_lwp_free2(l);
   1304 	uvm_lwp_exit(l);
   1305 
   1306 	KASSERT(SLIST_EMPTY(&l->l_pi_lenders));
   1307 	KASSERT(l->l_inheritedprio == -1);
   1308 	KASSERT(l->l_blcnt == 0);
   1309 	kdtrace_thread_dtor(NULL, l);
   1310 	if (!recycle)
   1311 		pool_cache_put(lwp_cache, l);
   1312 }
   1313 
   1314 /*
   1315  * Migrate the LWP to the another CPU.  Unlocks the LWP.
   1316  */
   1317 void
   1318 lwp_migrate(lwp_t *l, struct cpu_info *tci)
   1319 {
   1320 	struct schedstate_percpu *tspc;
   1321 	int lstat = l->l_stat;
   1322 
   1323 	KASSERT(lwp_locked(l, NULL));
   1324 	KASSERT(tci != NULL);
   1325 
   1326 	/* If LWP is still on the CPU, it must be handled like LSONPROC */
   1327 	if ((l->l_pflag & LP_RUNNING) != 0) {
   1328 		lstat = LSONPROC;
   1329 	}
   1330 
   1331 	/*
   1332 	 * The destination CPU could be changed while previous migration
   1333 	 * was not finished.
   1334 	 */
   1335 	if (l->l_target_cpu != NULL) {
   1336 		l->l_target_cpu = tci;
   1337 		lwp_unlock(l);
   1338 		return;
   1339 	}
   1340 
   1341 	/* Nothing to do if trying to migrate to the same CPU */
   1342 	if (l->l_cpu == tci) {
   1343 		lwp_unlock(l);
   1344 		return;
   1345 	}
   1346 
   1347 	KASSERT(l->l_target_cpu == NULL);
   1348 	tspc = &tci->ci_schedstate;
   1349 	switch (lstat) {
   1350 	case LSRUN:
   1351 		l->l_target_cpu = tci;
   1352 		break;
   1353 	case LSIDL:
   1354 		l->l_cpu = tci;
   1355 		lwp_unlock_to(l, tspc->spc_mutex);
   1356 		return;
   1357 	case LSSLEEP:
   1358 		l->l_cpu = tci;
   1359 		break;
   1360 	case LSSTOP:
   1361 	case LSSUSPENDED:
   1362 		l->l_cpu = tci;
   1363 		if (l->l_wchan == NULL) {
   1364 			lwp_unlock_to(l, tspc->spc_lwplock);
   1365 			return;
   1366 		}
   1367 		break;
   1368 	case LSONPROC:
   1369 		l->l_target_cpu = tci;
   1370 		spc_lock(l->l_cpu);
   1371 		cpu_need_resched(l->l_cpu, RESCHED_KPREEMPT);
   1372 		spc_unlock(l->l_cpu);
   1373 		break;
   1374 	}
   1375 	lwp_unlock(l);
   1376 }
   1377 
   1378 /*
   1379  * Find the LWP in the process.  Arguments may be zero, in such case,
   1380  * the calling process and first LWP in the list will be used.
   1381  * On success - returns proc locked.
   1382  */
   1383 struct lwp *
   1384 lwp_find2(pid_t pid, lwpid_t lid)
   1385 {
   1386 	proc_t *p;
   1387 	lwp_t *l;
   1388 
   1389 	/* Find the process. */
   1390 	if (pid != 0) {
   1391 		mutex_enter(proc_lock);
   1392 		p = proc_find(pid);
   1393 		if (p == NULL) {
   1394 			mutex_exit(proc_lock);
   1395 			return NULL;
   1396 		}
   1397 		mutex_enter(p->p_lock);
   1398 		mutex_exit(proc_lock);
   1399 	} else {
   1400 		p = curlwp->l_proc;
   1401 		mutex_enter(p->p_lock);
   1402 	}
   1403 	/* Find the thread. */
   1404 	if (lid != 0) {
   1405 		l = lwp_find(p, lid);
   1406 	} else {
   1407 		l = LIST_FIRST(&p->p_lwps);
   1408 	}
   1409 	if (l == NULL) {
   1410 		mutex_exit(p->p_lock);
   1411 	}
   1412 	return l;
   1413 }
   1414 
   1415 /*
   1416  * Look up a live LWP within the specified process.
   1417  *
   1418  * Must be called with p->p_lock held.
   1419  */
   1420 struct lwp *
   1421 lwp_find(struct proc *p, lwpid_t id)
   1422 {
   1423 	struct lwp *l;
   1424 
   1425 	KASSERT(mutex_owned(p->p_lock));
   1426 
   1427 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1428 		if (l->l_lid == id)
   1429 			break;
   1430 	}
   1431 
   1432 	/*
   1433 	 * No need to lock - all of these conditions will
   1434 	 * be visible with the process level mutex held.
   1435 	 */
   1436 	if (l != NULL && (l->l_stat == LSIDL || l->l_stat == LSZOMB))
   1437 		l = NULL;
   1438 
   1439 	return l;
   1440 }
   1441 
   1442 /*
   1443  * Update an LWP's cached credentials to mirror the process' master copy.
   1444  *
   1445  * This happens early in the syscall path, on user trap, and on LWP
   1446  * creation.  A long-running LWP can also voluntarily choose to update
   1447  * its credentials by calling this routine.  This may be called from
   1448  * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
   1449  */
   1450 void
   1451 lwp_update_creds(struct lwp *l)
   1452 {
   1453 	kauth_cred_t oc;
   1454 	struct proc *p;
   1455 
   1456 	p = l->l_proc;
   1457 	oc = l->l_cred;
   1458 
   1459 	mutex_enter(p->p_lock);
   1460 	kauth_cred_hold(p->p_cred);
   1461 	l->l_cred = p->p_cred;
   1462 	l->l_prflag &= ~LPR_CRMOD;
   1463 	mutex_exit(p->p_lock);
   1464 	if (oc != NULL)
   1465 		kauth_cred_free(oc);
   1466 }
   1467 
   1468 /*
   1469  * Verify that an LWP is locked, and optionally verify that the lock matches
   1470  * one we specify.
   1471  */
   1472 int
   1473 lwp_locked(struct lwp *l, kmutex_t *mtx)
   1474 {
   1475 	kmutex_t *cur = l->l_mutex;
   1476 
   1477 	return mutex_owned(cur) && (mtx == cur || mtx == NULL);
   1478 }
   1479 
   1480 /*
   1481  * Lend a new mutex to an LWP.  The old mutex must be held.
   1482  */
   1483 void
   1484 lwp_setlock(struct lwp *l, kmutex_t *mtx)
   1485 {
   1486 
   1487 	KASSERT(mutex_owned(l->l_mutex));
   1488 
   1489 	membar_exit();
   1490 	l->l_mutex = mtx;
   1491 }
   1492 
   1493 /*
   1494  * Lend a new mutex to an LWP, and release the old mutex.  The old mutex
   1495  * must be held.
   1496  */
   1497 void
   1498 lwp_unlock_to(struct lwp *l, kmutex_t *mtx)
   1499 {
   1500 	kmutex_t *old;
   1501 
   1502 	KASSERT(lwp_locked(l, NULL));
   1503 
   1504 	old = l->l_mutex;
   1505 	membar_exit();
   1506 	l->l_mutex = mtx;
   1507 	mutex_spin_exit(old);
   1508 }
   1509 
   1510 int
   1511 lwp_trylock(struct lwp *l)
   1512 {
   1513 	kmutex_t *old;
   1514 
   1515 	for (;;) {
   1516 		if (!mutex_tryenter(old = l->l_mutex))
   1517 			return 0;
   1518 		if (__predict_true(l->l_mutex == old))
   1519 			return 1;
   1520 		mutex_spin_exit(old);
   1521 	}
   1522 }
   1523 
   1524 void
   1525 lwp_unsleep(lwp_t *l, bool cleanup)
   1526 {
   1527 
   1528 	KASSERT(mutex_owned(l->l_mutex));
   1529 	(*l->l_syncobj->sobj_unsleep)(l, cleanup);
   1530 }
   1531 
   1532 /*
   1533  * Handle exceptions for mi_userret().  Called if a member of LW_USERRET is
   1534  * set.
   1535  */
   1536 void
   1537 lwp_userret(struct lwp *l)
   1538 {
   1539 	struct proc *p;
   1540 	int sig;
   1541 
   1542 	KASSERT(l == curlwp);
   1543 	KASSERT(l->l_stat == LSONPROC);
   1544 	p = l->l_proc;
   1545 
   1546 #ifndef __HAVE_FAST_SOFTINTS
   1547 	/* Run pending soft interrupts. */
   1548 	if (l->l_cpu->ci_data.cpu_softints != 0)
   1549 		softint_overlay();
   1550 #endif
   1551 
   1552 	/*
   1553 	 * It is safe to do this read unlocked on a MP system..
   1554 	 */
   1555 	while ((l->l_flag & LW_USERRET) != 0) {
   1556 		/*
   1557 		 * Process pending signals first, unless the process
   1558 		 * is dumping core or exiting, where we will instead
   1559 		 * enter the LW_WSUSPEND case below.
   1560 		 */
   1561 		if ((l->l_flag & (LW_PENDSIG | LW_WCORE | LW_WEXIT)) ==
   1562 		    LW_PENDSIG) {
   1563 			mutex_enter(p->p_lock);
   1564 			while ((sig = issignal(l)) != 0)
   1565 				postsig(sig);
   1566 			mutex_exit(p->p_lock);
   1567 		}
   1568 
   1569 		/*
   1570 		 * Core-dump or suspend pending.
   1571 		 *
   1572 		 * In case of core dump, suspend ourselves, so that the kernel
   1573 		 * stack and therefore the userland registers saved in the
   1574 		 * trapframe are around for coredump() to write them out.
   1575 		 * We also need to save any PCU resources that we have so that
   1576 		 * they accessible for coredump().  We issue a wakeup on
   1577 		 * p->p_lwpcv so that sigexit() will write the core file out
   1578 		 * once all other LWPs are suspended.
   1579 		 */
   1580 		if ((l->l_flag & LW_WSUSPEND) != 0) {
   1581 			pcu_save_all(l);
   1582 			mutex_enter(p->p_lock);
   1583 			p->p_nrlwps--;
   1584 			cv_broadcast(&p->p_lwpcv);
   1585 			lwp_lock(l);
   1586 			l->l_stat = LSSUSPENDED;
   1587 			lwp_unlock(l);
   1588 			mutex_exit(p->p_lock);
   1589 			lwp_lock(l);
   1590 			mi_switch(l);
   1591 		}
   1592 
   1593 		/* Process is exiting. */
   1594 		if ((l->l_flag & LW_WEXIT) != 0) {
   1595 			lwp_exit(l);
   1596 			KASSERT(0);
   1597 			/* NOTREACHED */
   1598 		}
   1599 
   1600 		/* update lwpctl processor (for vfork child_return) */
   1601 		if (l->l_flag & LW_LWPCTL) {
   1602 			lwp_lock(l);
   1603 			KASSERT(kpreempt_disabled());
   1604 			l->l_lwpctl->lc_curcpu = (int)cpu_index(l->l_cpu);
   1605 			l->l_lwpctl->lc_pctr++;
   1606 			l->l_flag &= ~LW_LWPCTL;
   1607 			lwp_unlock(l);
   1608 		}
   1609 	}
   1610 }
   1611 
   1612 /*
   1613  * Force an LWP to enter the kernel, to take a trip through lwp_userret().
   1614  */
   1615 void
   1616 lwp_need_userret(struct lwp *l)
   1617 {
   1618 	KASSERT(lwp_locked(l, NULL));
   1619 
   1620 	/*
   1621 	 * Since the tests in lwp_userret() are done unlocked, make sure
   1622 	 * that the condition will be seen before forcing the LWP to enter
   1623 	 * kernel mode.
   1624 	 */
   1625 	membar_producer();
   1626 	cpu_signotify(l);
   1627 }
   1628 
   1629 /*
   1630  * Add one reference to an LWP.  This will prevent the LWP from
   1631  * exiting, thus keep the lwp structure and PCB around to inspect.
   1632  */
   1633 void
   1634 lwp_addref(struct lwp *l)
   1635 {
   1636 
   1637 	KASSERT(mutex_owned(l->l_proc->p_lock));
   1638 	KASSERT(l->l_stat != LSZOMB);
   1639 	KASSERT(l->l_refcnt != 0);
   1640 
   1641 	l->l_refcnt++;
   1642 }
   1643 
   1644 /*
   1645  * Remove one reference to an LWP.  If this is the last reference,
   1646  * then we must finalize the LWP's death.
   1647  */
   1648 void
   1649 lwp_delref(struct lwp *l)
   1650 {
   1651 	struct proc *p = l->l_proc;
   1652 
   1653 	mutex_enter(p->p_lock);
   1654 	lwp_delref2(l);
   1655 	mutex_exit(p->p_lock);
   1656 }
   1657 
   1658 /*
   1659  * Remove one reference to an LWP.  If this is the last reference,
   1660  * then we must finalize the LWP's death.  The proc mutex is held
   1661  * on entry.
   1662  */
   1663 void
   1664 lwp_delref2(struct lwp *l)
   1665 {
   1666 	struct proc *p = l->l_proc;
   1667 
   1668 	KASSERT(mutex_owned(p->p_lock));
   1669 	KASSERT(l->l_stat != LSZOMB);
   1670 	KASSERT(l->l_refcnt > 0);
   1671 	if (--l->l_refcnt == 0)
   1672 		cv_broadcast(&p->p_lwpcv);
   1673 }
   1674 
   1675 /*
   1676  * Drain all references to the current LWP.
   1677  */
   1678 void
   1679 lwp_drainrefs(struct lwp *l)
   1680 {
   1681 	struct proc *p = l->l_proc;
   1682 
   1683 	KASSERT(mutex_owned(p->p_lock));
   1684 	KASSERT(l->l_refcnt != 0);
   1685 
   1686 	l->l_refcnt--;
   1687 	while (l->l_refcnt != 0)
   1688 		cv_wait(&p->p_lwpcv, p->p_lock);
   1689 }
   1690 
   1691 /*
   1692  * Return true if the specified LWP is 'alive'.  Only p->p_lock need
   1693  * be held.
   1694  */
   1695 bool
   1696 lwp_alive(lwp_t *l)
   1697 {
   1698 
   1699 	KASSERT(mutex_owned(l->l_proc->p_lock));
   1700 
   1701 	switch (l->l_stat) {
   1702 	case LSSLEEP:
   1703 	case LSRUN:
   1704 	case LSONPROC:
   1705 	case LSSTOP:
   1706 	case LSSUSPENDED:
   1707 		return true;
   1708 	default:
   1709 		return false;
   1710 	}
   1711 }
   1712 
   1713 /*
   1714  * Return first live LWP in the process.
   1715  */
   1716 lwp_t *
   1717 lwp_find_first(proc_t *p)
   1718 {
   1719 	lwp_t *l;
   1720 
   1721 	KASSERT(mutex_owned(p->p_lock));
   1722 
   1723 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1724 		if (lwp_alive(l)) {
   1725 			return l;
   1726 		}
   1727 	}
   1728 
   1729 	return NULL;
   1730 }
   1731 
   1732 /*
   1733  * Allocate a new lwpctl structure for a user LWP.
   1734  */
   1735 int
   1736 lwp_ctl_alloc(vaddr_t *uaddr)
   1737 {
   1738 	lcproc_t *lp;
   1739 	u_int bit, i, offset;
   1740 	struct uvm_object *uao;
   1741 	int error;
   1742 	lcpage_t *lcp;
   1743 	proc_t *p;
   1744 	lwp_t *l;
   1745 
   1746 	l = curlwp;
   1747 	p = l->l_proc;
   1748 
   1749 	/* don't allow a vforked process to create lwp ctls */
   1750 	if (p->p_lflag & PL_PPWAIT)
   1751 		return EBUSY;
   1752 
   1753 	if (l->l_lcpage != NULL) {
   1754 		lcp = l->l_lcpage;
   1755 		*uaddr = lcp->lcp_uaddr + (vaddr_t)l->l_lwpctl - lcp->lcp_kaddr;
   1756 		return 0;
   1757 	}
   1758 
   1759 	/* First time around, allocate header structure for the process. */
   1760 	if ((lp = p->p_lwpctl) == NULL) {
   1761 		lp = kmem_alloc(sizeof(*lp), KM_SLEEP);
   1762 		mutex_init(&lp->lp_lock, MUTEX_DEFAULT, IPL_NONE);
   1763 		lp->lp_uao = NULL;
   1764 		TAILQ_INIT(&lp->lp_pages);
   1765 		mutex_enter(p->p_lock);
   1766 		if (p->p_lwpctl == NULL) {
   1767 			p->p_lwpctl = lp;
   1768 			mutex_exit(p->p_lock);
   1769 		} else {
   1770 			mutex_exit(p->p_lock);
   1771 			mutex_destroy(&lp->lp_lock);
   1772 			kmem_free(lp, sizeof(*lp));
   1773 			lp = p->p_lwpctl;
   1774 		}
   1775 	}
   1776 
   1777  	/*
   1778  	 * Set up an anonymous memory region to hold the shared pages.
   1779  	 * Map them into the process' address space.  The user vmspace
   1780  	 * gets the first reference on the UAO.
   1781  	 */
   1782 	mutex_enter(&lp->lp_lock);
   1783 	if (lp->lp_uao == NULL) {
   1784 		lp->lp_uao = uao_create(LWPCTL_UAREA_SZ, 0);
   1785 		lp->lp_cur = 0;
   1786 		lp->lp_max = LWPCTL_UAREA_SZ;
   1787 		lp->lp_uva = p->p_emul->e_vm_default_addr(p,
   1788 		     (vaddr_t)p->p_vmspace->vm_daddr, LWPCTL_UAREA_SZ,
   1789 		     p->p_vmspace->vm_map.flags & VM_MAP_TOPDOWN);
   1790 		error = uvm_map(&p->p_vmspace->vm_map, &lp->lp_uva,
   1791 		    LWPCTL_UAREA_SZ, lp->lp_uao, 0, 0, UVM_MAPFLAG(UVM_PROT_RW,
   1792 		    UVM_PROT_RW, UVM_INH_NONE, UVM_ADV_NORMAL, 0));
   1793 		if (error != 0) {
   1794 			uao_detach(lp->lp_uao);
   1795 			lp->lp_uao = NULL;
   1796 			mutex_exit(&lp->lp_lock);
   1797 			return error;
   1798 		}
   1799 	}
   1800 
   1801 	/* Get a free block and allocate for this LWP. */
   1802 	TAILQ_FOREACH(lcp, &lp->lp_pages, lcp_chain) {
   1803 		if (lcp->lcp_nfree != 0)
   1804 			break;
   1805 	}
   1806 	if (lcp == NULL) {
   1807 		/* Nothing available - try to set up a free page. */
   1808 		if (lp->lp_cur == lp->lp_max) {
   1809 			mutex_exit(&lp->lp_lock);
   1810 			return ENOMEM;
   1811 		}
   1812 		lcp = kmem_alloc(LWPCTL_LCPAGE_SZ, KM_SLEEP);
   1813 
   1814 		/*
   1815 		 * Wire the next page down in kernel space.  Since this
   1816 		 * is a new mapping, we must add a reference.
   1817 		 */
   1818 		uao = lp->lp_uao;
   1819 		(*uao->pgops->pgo_reference)(uao);
   1820 		lcp->lcp_kaddr = vm_map_min(kernel_map);
   1821 		error = uvm_map(kernel_map, &lcp->lcp_kaddr, PAGE_SIZE,
   1822 		    uao, lp->lp_cur, PAGE_SIZE,
   1823 		    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
   1824 		    UVM_INH_NONE, UVM_ADV_RANDOM, 0));
   1825 		if (error != 0) {
   1826 			mutex_exit(&lp->lp_lock);
   1827 			kmem_free(lcp, LWPCTL_LCPAGE_SZ);
   1828 			(*uao->pgops->pgo_detach)(uao);
   1829 			return error;
   1830 		}
   1831 		error = uvm_map_pageable(kernel_map, lcp->lcp_kaddr,
   1832 		    lcp->lcp_kaddr + PAGE_SIZE, FALSE, 0);
   1833 		if (error != 0) {
   1834 			mutex_exit(&lp->lp_lock);
   1835 			uvm_unmap(kernel_map, lcp->lcp_kaddr,
   1836 			    lcp->lcp_kaddr + PAGE_SIZE);
   1837 			kmem_free(lcp, LWPCTL_LCPAGE_SZ);
   1838 			return error;
   1839 		}
   1840 		/* Prepare the page descriptor and link into the list. */
   1841 		lcp->lcp_uaddr = lp->lp_uva + lp->lp_cur;
   1842 		lp->lp_cur += PAGE_SIZE;
   1843 		lcp->lcp_nfree = LWPCTL_PER_PAGE;
   1844 		lcp->lcp_rotor = 0;
   1845 		memset(lcp->lcp_bitmap, 0xff, LWPCTL_BITMAP_SZ);
   1846 		TAILQ_INSERT_HEAD(&lp->lp_pages, lcp, lcp_chain);
   1847 	}
   1848 	for (i = lcp->lcp_rotor; lcp->lcp_bitmap[i] == 0;) {
   1849 		if (++i >= LWPCTL_BITMAP_ENTRIES)
   1850 			i = 0;
   1851 	}
   1852 	bit = ffs(lcp->lcp_bitmap[i]) - 1;
   1853 	lcp->lcp_bitmap[i] ^= (1U << bit);
   1854 	lcp->lcp_rotor = i;
   1855 	lcp->lcp_nfree--;
   1856 	l->l_lcpage = lcp;
   1857 	offset = (i << 5) + bit;
   1858 	l->l_lwpctl = (lwpctl_t *)lcp->lcp_kaddr + offset;
   1859 	*uaddr = lcp->lcp_uaddr + offset * sizeof(lwpctl_t);
   1860 	mutex_exit(&lp->lp_lock);
   1861 
   1862 	KPREEMPT_DISABLE(l);
   1863 	l->l_lwpctl->lc_curcpu = (int)cpu_index(curcpu());
   1864 	KPREEMPT_ENABLE(l);
   1865 
   1866 	return 0;
   1867 }
   1868 
   1869 /*
   1870  * Free an lwpctl structure back to the per-process list.
   1871  */
   1872 void
   1873 lwp_ctl_free(lwp_t *l)
   1874 {
   1875 	struct proc *p = l->l_proc;
   1876 	lcproc_t *lp;
   1877 	lcpage_t *lcp;
   1878 	u_int map, offset;
   1879 
   1880 	/* don't free a lwp context we borrowed for vfork */
   1881 	if (p->p_lflag & PL_PPWAIT) {
   1882 		l->l_lwpctl = NULL;
   1883 		return;
   1884 	}
   1885 
   1886 	lp = p->p_lwpctl;
   1887 	KASSERT(lp != NULL);
   1888 
   1889 	lcp = l->l_lcpage;
   1890 	offset = (u_int)((lwpctl_t *)l->l_lwpctl - (lwpctl_t *)lcp->lcp_kaddr);
   1891 	KASSERT(offset < LWPCTL_PER_PAGE);
   1892 
   1893 	mutex_enter(&lp->lp_lock);
   1894 	lcp->lcp_nfree++;
   1895 	map = offset >> 5;
   1896 	lcp->lcp_bitmap[map] |= (1U << (offset & 31));
   1897 	if (lcp->lcp_bitmap[lcp->lcp_rotor] == 0)
   1898 		lcp->lcp_rotor = map;
   1899 	if (TAILQ_FIRST(&lp->lp_pages)->lcp_nfree == 0) {
   1900 		TAILQ_REMOVE(&lp->lp_pages, lcp, lcp_chain);
   1901 		TAILQ_INSERT_HEAD(&lp->lp_pages, lcp, lcp_chain);
   1902 	}
   1903 	mutex_exit(&lp->lp_lock);
   1904 }
   1905 
   1906 /*
   1907  * Process is exiting; tear down lwpctl state.  This can only be safely
   1908  * called by the last LWP in the process.
   1909  */
   1910 void
   1911 lwp_ctl_exit(void)
   1912 {
   1913 	lcpage_t *lcp, *next;
   1914 	lcproc_t *lp;
   1915 	proc_t *p;
   1916 	lwp_t *l;
   1917 
   1918 	l = curlwp;
   1919 	l->l_lwpctl = NULL;
   1920 	l->l_lcpage = NULL;
   1921 	p = l->l_proc;
   1922 	lp = p->p_lwpctl;
   1923 
   1924 	KASSERT(lp != NULL);
   1925 	KASSERT(p->p_nlwps == 1);
   1926 
   1927 	for (lcp = TAILQ_FIRST(&lp->lp_pages); lcp != NULL; lcp = next) {
   1928 		next = TAILQ_NEXT(lcp, lcp_chain);
   1929 		uvm_unmap(kernel_map, lcp->lcp_kaddr,
   1930 		    lcp->lcp_kaddr + PAGE_SIZE);
   1931 		kmem_free(lcp, LWPCTL_LCPAGE_SZ);
   1932 	}
   1933 
   1934 	if (lp->lp_uao != NULL) {
   1935 		uvm_unmap(&p->p_vmspace->vm_map, lp->lp_uva,
   1936 		    lp->lp_uva + LWPCTL_UAREA_SZ);
   1937 	}
   1938 
   1939 	mutex_destroy(&lp->lp_lock);
   1940 	kmem_free(lp, sizeof(*lp));
   1941 	p->p_lwpctl = NULL;
   1942 }
   1943 
   1944 /*
   1945  * Return the current LWP's "preemption counter".  Used to detect
   1946  * preemption across operations that can tolerate preemption without
   1947  * crashing, but which may generate incorrect results if preempted.
   1948  */
   1949 uint64_t
   1950 lwp_pctr(void)
   1951 {
   1952 
   1953 	return curlwp->l_ncsw;
   1954 }
   1955 
   1956 /*
   1957  * Set an LWP's private data pointer.
   1958  */
   1959 int
   1960 lwp_setprivate(struct lwp *l, void *ptr)
   1961 {
   1962 	int error = 0;
   1963 
   1964 	l->l_private = ptr;
   1965 #ifdef __HAVE_CPU_LWP_SETPRIVATE
   1966 	error = cpu_lwp_setprivate(l, ptr);
   1967 #endif
   1968 	return error;
   1969 }
   1970 
   1971 #if defined(DDB)
   1972 #include <machine/pcb.h>
   1973 
   1974 void
   1975 lwp_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   1976 {
   1977 	lwp_t *l;
   1978 
   1979 	LIST_FOREACH(l, &alllwp, l_list) {
   1980 		uintptr_t stack = (uintptr_t)KSTACK_LOWEST_ADDR(l);
   1981 
   1982 		if (addr < stack || stack + KSTACK_SIZE <= addr) {
   1983 			continue;
   1984 		}
   1985 		(*pr)("%p is %p+%zu, LWP %p's stack\n",
   1986 		    (void *)addr, (void *)stack,
   1987 		    (size_t)(addr - stack), l);
   1988 	}
   1989 }
   1990 #endif /* defined(DDB) */
   1991