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