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