Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.40.2.18
      1 /*	$NetBSD: kern_lwp.c,v 1.40.2.18 2007/02/01 06:21:07 ad Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007 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  * 3. All advertising materials mentioning features or use of this software
     19  *    must display the following acknowledgement:
     20  *        This product includes software developed by the NetBSD
     21  *        Foundation, Inc. and its contributors.
     22  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  *    contributors may be used to endorse or promote products derived
     24  *    from this software without specific prior written permission.
     25  *
     26  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  * POSSIBILITY OF SUCH DAMAGE.
     37  */
     38 
     39 /*
     40  * Overview
     41  *
     42  *	Lightweight processes (LWPs) are the basic unit (or thread) of
     43  *	execution within the kernel.  The core state of an LWP is described
     44  *	by "struct lwp".
     45  *
     46  *	Each LWP is contained within a process (described by "struct proc"),
     47  *	Every process contains at least one LWP, but may contain more.  The
     48  *	process describes attributes shared among all of its LWPs such as a
     49  *	private address space, global execution state (stopped, active,
     50  *	zombie, ...), signal disposition and so on.  On a multiprocessor
     51  *	machine, multiple LWPs be executing in kernel simultaneously.
     52  *
     53  *	Note that LWPs differ from kernel threads (kthreads) in that kernel
     54  *	threads are distinct processes (system processes) with no user space
     55  *	component, which themselves may contain one or more LWPs.
     56  *
     57  * Execution states
     58  *
     59  *	At any given time, an LWP has overall state that is described by
     60  *	lwp::l_stat.  The states are broken into two sets below.  The first
     61  *	set is guaranteed to represent the absolute, current state of the
     62  *	LWP:
     63  *
     64  * 	LSONPROC
     65  *
     66  * 		On processor: the LWP is executing on a CPU, either in the
     67  * 		kernel or in user space.
     68  *
     69  * 	LSRUN
     70  *
     71  * 		Runnable: the LWP is parked on a run queue, and may soon be
     72  * 		chosen to run by a idle processor, or by a processor that
     73  * 		has been asked to preempt a currently runnning but lower
     74  * 		priority LWP.  If the LWP is not swapped in (L_INMEM == 0)
     75  *		then the LWP is not on a run queue, but may be soon.
     76  *
     77  * 	LSIDL
     78  *
     79  * 		Idle: the LWP has been created but has not yet executed.
     80  * 		Whoever created the new LWP can be expected to set it to
     81  * 		another state shortly.
     82  *
     83  * 	LSSUSPENDED:
     84  *
     85  * 		Suspended: the LWP has had its execution suspended by
     86  *		another LWP in the same process using the _lwp_suspend()
     87  *		system call.  User-level LWPs also enter the suspended
     88  *		state when the system is shutting down.
     89  *
     90  *	The second set represent a "statement of intent" on behalf of the
     91  *	LWP.  The LWP may in fact be executing on a processor, may be
     92  *	sleeping, idle, or on a run queue. It is expected to take the
     93  *	necessary action to stop executing or become "running" again within
     94  *	a short timeframe.
     95  *
     96  * 	LSZOMB:
     97  *
     98  * 		Dead: the LWP has released most of its resources and is
     99  * 		about to switch away into oblivion.  When it switches away,
    100  * 		its few remaining resources will be collected.
    101  *
    102  * 	LSSLEEP:
    103  *
    104  * 		Sleeping: the LWP has entered itself onto a sleep queue, and
    105  * 		will switch away shortly to allow other LWPs to run on the
    106  * 		CPU.
    107  *
    108  * 	LSSTOP:
    109  *
    110  * 		Stopped: the LWP has been stopped as a result of a job
    111  * 		control signal, or as a result of the ptrace() interface.
    112  * 		Stopped LWPs may run briefly within the kernel to handle
    113  * 		signals that they receive, but will not return to user space
    114  * 		until their process' state is changed away from stopped.
    115  * 		Single LWPs within a process can not be set stopped
    116  * 		selectively: all actions that can stop or continue LWPs
    117  * 		occur at the process level.
    118  *
    119  * State transitions
    120  *
    121  *	Note that the LSSTOP and LSSUSPENDED states may only be set
    122  *	when returning to user space in userret(), or when sleeping
    123  *	interruptably.  Before setting those states, we try to ensure
    124  *	that the LWPs will release all kernel locks that they hold,
    125  *	and at a minimum try to ensure that the LWP can be set runnable
    126  *	again by a signal.
    127  *
    128  *	LWPs may transition states in the following ways:
    129  *
    130  *	 RUN -------> ONPROC		ONPROC -----> RUN
    131  *	            > STOPPED			    > SLEEP
    132  *	            > SUSPENDED			    > STOPPED
    133  *						    > SUSPENDED
    134  *						    > ZOMB
    135  *
    136  *	 STOPPED ---> RUN		SUSPENDED --> RUN
    137  *	            > SLEEP			    > SLEEP
    138  *
    139  *	 SLEEP -----> ONPROC		IDL --------> RUN
    140  *		    > RUN		            > SUSPENDED
    141  *		    > STOPPED                       > STOPPED
    142  *		    > SUSPENDED
    143  *
    144  * Locking
    145  *
    146  *	The majority of fields in 'struct lwp' are covered by a single,
    147  *	general spin mutex pointed to by lwp::l_mutex.  The locks covering
    148  *	each field are documented in sys/lwp.h.
    149  *
    150  *	State transitions must be made with the LWP's general lock held.  In
    151  *	a multiprocessor kernel, state transitions may cause the LWP's lock
    152  *	pointer to change.  On uniprocessor kernels, most scheduler and
    153  *	synchronisation objects such as sleep queues and LWPs are protected
    154  *	by only one mutex (sched_mutex).  In this case, LWPs' lock pointers
    155  *	will never change and will always reference sched_mutex.
    156  *
    157  *	Manipulation of the general lock is not performed directly, but
    158  *	through calls to lwp_lock(), lwp_relock() and similar.
    159  *
    160  *	States and their associated locks:
    161  *
    162  *	LSIDL, LSZOMB
    163  *
    164  *		Always covered by sched_mutex.
    165  *
    166  *	LSONPROC, LSRUN:
    167  *
    168  *		Always covered by sched_mutex, which protects the run queues
    169  *		and other miscellaneous items.  If the scheduler is changed
    170  *		to use per-CPU run queues, this may become a per-CPU mutex.
    171  *
    172  *	LSSLEEP:
    173  *
    174  *		Covered by a mutex associated with the sleep queue that the
    175  *		LWP resides on, indirectly referenced by 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 mutex.  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 mutex is sched_mutex.
    183  *
    184  *	The lock order is as follows:
    185  *
    186  *		sleepq_t::sq_mutex -> sched_mutex
    187  *
    188  *	Each process has an scheduler state mutex (proc::p_smutex), and a
    189  *	number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
    190  *	so on.  When an LWP is to be entered into or removed from one of the
    191  *	following states, p_mutex must be held and the process wide counters
    192  *	adjusted:
    193  *
    194  *		LSIDL, LSZOMB, LSSTOP, LSSUSPENDED
    195  *
    196  *	Note that an LWP is considered running or likely to run soon if in
    197  *	one of the following states.  This affects the value of p_nrlwps:
    198  *
    199  *		LSRUN, LSONPROC, LSSLEEP
    200  *
    201  *	p_smutex does not need to be held when transitioning among these
    202  *	three states.
    203  */
    204 
    205 #include <sys/cdefs.h>
    206 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.40.2.18 2007/02/01 06:21:07 ad Exp $");
    207 
    208 #include "opt_multiprocessor.h"
    209 #include "opt_lockdebug.h"
    210 
    211 #define _LWP_API_PRIVATE
    212 
    213 #include <sys/param.h>
    214 #include <sys/systm.h>
    215 #include <sys/pool.h>
    216 #include <sys/proc.h>
    217 #include <sys/syscallargs.h>
    218 #include <sys/kauth.h>
    219 #include <sys/sleepq.h>
    220 #include <sys/lockdebug.h>
    221 #include <sys/kmem.h>
    222 
    223 #include <uvm/uvm_extern.h>
    224 
    225 struct lwplist	alllwp;
    226 
    227 POOL_INIT(lwp_pool, sizeof(struct lwp), MIN_LWP_ALIGNMENT, 0, 0, "lwppl",
    228     &pool_allocator_nointr);
    229 POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    230     &pool_allocator_nointr);
    231 
    232 static specificdata_domain_t lwp_specificdata_domain;
    233 
    234 #define LWP_DEBUG
    235 
    236 #ifdef LWP_DEBUG
    237 int lwp_debug = 0;
    238 #define DPRINTF(x) if (lwp_debug) printf x
    239 #else
    240 #define DPRINTF(x)
    241 #endif
    242 
    243 void
    244 lwpinit(void)
    245 {
    246 
    247 	lwp_specificdata_domain = specificdata_domain_create();
    248 	KASSERT(lwp_specificdata_domain != NULL);
    249 	lwp_sys_init();
    250 }
    251 
    252 /*
    253  * Set an suspended.
    254  *
    255  * Must be called with p_smutex held, and the LWP locked.  Will unlock the
    256  * LWP before return.
    257  */
    258 int
    259 lwp_suspend(struct lwp *curl, struct lwp *t)
    260 {
    261 	int error;
    262 
    263 	LOCK_ASSERT(mutex_owned(&t->l_proc->p_smutex));
    264 	LOCK_ASSERT(lwp_locked(t, NULL));
    265 
    266 	KASSERT(curl != t || curl->l_stat == LSONPROC);
    267 
    268 	/*
    269 	 * If the current LWP has been told to exit, we must not suspend anyone
    270 	 * else or deadlock could occur.  We won't return to userspace.
    271 	 */
    272 	if ((curl->l_stat & (L_WEXIT | L_WCORE)) != 0) {
    273 		lwp_unlock(t);
    274 		return (EDEADLK);
    275 	}
    276 
    277 	error = 0;
    278 
    279 	switch (t->l_stat) {
    280 	case LSRUN:
    281 	case LSONPROC:
    282 		t->l_flag |= L_WSUSPEND;
    283 		lwp_need_userret(t);
    284 		lwp_unlock(t);
    285 		break;
    286 
    287 	case LSSLEEP:
    288 		t->l_flag |= L_WSUSPEND;
    289 
    290 		/*
    291 		 * Kick the LWP and try to get it to the kernel boundary
    292 		 * so that it will release any locks that it holds.
    293 		 * setrunnable() will release the lock.
    294 		 */
    295 		if ((t->l_flag & L_SINTR) != 0)
    296 			setrunnable(t);
    297 		else
    298 			lwp_unlock(t);
    299 		break;
    300 
    301 	case LSSUSPENDED:
    302 		lwp_unlock(t);
    303 		break;
    304 
    305 	case LSSTOP:
    306 		t->l_flag |= L_WSUSPEND;
    307 		setrunnable(t);
    308 		break;
    309 
    310 	case LSIDL:
    311 	case LSZOMB:
    312 		error = EINTR; /* It's what Solaris does..... */
    313 		lwp_unlock(t);
    314 		break;
    315 	}
    316 
    317 	/*
    318 	 * XXXLWP Wait for:
    319 	 *
    320 	 * o process exiting
    321 	 * o target LWP suspended
    322 	 * o target LWP not suspended and L_WSUSPEND clear
    323 	 * o target LWP exited
    324 	 */
    325 
    326 	 return (error);
    327 }
    328 
    329 /*
    330  * Restart a suspended LWP.
    331  *
    332  * Must be called with p_smutex held, and the LWP locked.  Will unlock the
    333  * LWP before return.
    334  */
    335 void
    336 lwp_continue(struct lwp *l)
    337 {
    338 
    339 	LOCK_ASSERT(mutex_owned(&l->l_proc->p_smutex));
    340 	LOCK_ASSERT(lwp_locked(l, NULL));
    341 
    342 	DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n",
    343 	    l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat,
    344 	    l->l_wchan));
    345 
    346 	/* If rebooting or not suspended, then just bail out. */
    347 	if ((l->l_flag & L_WREBOOT) != 0) {
    348 		lwp_unlock(l);
    349 		return;
    350 	}
    351 
    352 	l->l_flag &= ~L_WSUSPEND;
    353 
    354 	if (l->l_stat != LSSUSPENDED) {
    355 		lwp_unlock(l);
    356 		return;
    357 	}
    358 
    359 	/* setrunnable() will release the lock. */
    360 	setrunnable(l);
    361 }
    362 
    363 /*
    364  * Wait for an LWP within the current process to exit.  If 'lid' is
    365  * non-zero, we are waiting for a specific LWP.
    366  *
    367  * Must be called with p->p_smutex held.
    368  */
    369 int
    370 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    371 {
    372 	struct proc *p = l->l_proc;
    373 	struct lwp *l2;
    374 	int nfound, error;
    375 
    376 	DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
    377 	    p->p_pid, l->l_lid, lid));
    378 
    379 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    380 
    381 	/*
    382 	 * We try to check for deadlock:
    383 	 *
    384 	 * 1) If all other LWPs are waiting for exits or suspended.
    385 	 * 2) If we are trying to wait on ourself.
    386 	 *
    387 	 * XXX we'd like to check for a cycle of waiting LWPs (specific LID
    388 	 * waits, not any-LWP waits) and detect that sort of deadlock, but
    389 	 * we don't have a good place to store the lwp that is being waited
    390 	 * for. wchan is already filled with &p->p_nlwps, and putting the
    391 	 * lwp address in there for deadlock tracing would require exiting
    392 	 * LWPs to call wakeup on both their own address and &p->p_nlwps, to
    393 	 * get threads sleeping on any LWP exiting.
    394 	 */
    395 	if (lid == l->l_lid)
    396 		return EDEADLK;
    397 
    398 	p->p_nlwpwait++;
    399 
    400 	for (;;) {
    401 		/*
    402 		 * Avoid a race between exit1() and sigexit(): if the
    403 		 * process is dumping core, then we need to bail out: call
    404 		 * into lwp_userret() where we will be suspended until the
    405 		 * deed is done.
    406 		 */
    407 		if ((p->p_sflag & PS_WCORE) != 0) {
    408 			mutex_exit(&p->p_smutex);
    409 			lwp_userret(l);
    410 #ifdef DIAGNOSTIC
    411 			panic("lwp_wait1");
    412 #endif
    413 			/* NOTREACHED */
    414 		}
    415 
    416 		/*
    417 		 * First off, drain any detached LWP that is waiting to be
    418 		 * reaped.
    419 		 */
    420 		while ((l2 = p->p_zomblwp) != NULL) {
    421 			p->p_zomblwp = NULL;
    422 			lwp_free(l2, 0, 0);	/* releases proc mutex */
    423 			mutex_enter(&p->p_smutex);
    424 		}
    425 
    426 		/*
    427 		 * Now look for an LWP to collect.  If the whole process is
    428 		 * exiting, count detached LWPs as eligible to be collected,
    429 		 * but don't drain them here.
    430 		 */
    431 		nfound = 0;
    432 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    433 			if (l2 == l || (lid != 0 && l2->l_lid != lid))
    434 				continue;
    435 			if ((l2->l_prflag & LPR_DETACHED) != 0) {
    436 				nfound += ((flags & LWPWAIT_EXITCONTROL) != 0);
    437 				continue;
    438 			}
    439 			nfound++;
    440 
    441 			/* No need to lock the LWP in order to see LSZOMB. */
    442 			if (l2->l_stat != LSZOMB)
    443 				continue;
    444 
    445 			if (departed)
    446 				*departed = l2->l_lid;
    447 			lwp_free(l2, 0, 0);
    448 			mutex_enter(&p->p_smutex);
    449 			p->p_nlwpwait--;
    450 			return 0;
    451 		}
    452 
    453 		if (nfound == 0) {
    454 			error = ESRCH;
    455 			break;
    456 		}
    457 		if ((flags & LWPWAIT_EXITCONTROL) != 0) {
    458 			KASSERT(p->p_nlwps > 1);
    459 			cv_wait(&p->p_lwpcv, &p->p_smutex);
    460 			continue;
    461 		}
    462 		if ((p->p_sflag & PS_WEXIT) != 0 ||
    463 		    p->p_nrlwps <= p->p_nlwpwait + p->p_ndlwps) {
    464 			error = EDEADLK;
    465 			break;
    466 		}
    467 		if ((error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex)) != 0)
    468 			break;
    469 	}
    470 
    471 	p->p_nlwpwait--;
    472 	return error;
    473 }
    474 
    475 /*
    476  * Create a new LWP within process 'p2', using LWP 'l1' as a template.
    477  * The new LWP is created in state LSIDL and must be set running,
    478  * suspended, or stopped by the caller.
    479  */
    480 int
    481 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, boolean_t inmem,
    482     int flags, void *stack, size_t stacksize,
    483     void (*func)(void *), void *arg, struct lwp **rnewlwpp)
    484 {
    485 	struct lwp *l2, *isfree;
    486 	turnstile_t *ts;
    487 
    488 	/*
    489 	 * First off, reap any detached LWP waiting to be collected.
    490 	 * We can re-use its LWP structure and turnstile.
    491 	 */
    492 	isfree = NULL;
    493 	if (p2->p_zomblwp != NULL) {
    494 		mutex_enter(&p2->p_smutex);
    495 		if ((isfree = p2->p_zomblwp) != NULL) {
    496 			p2->p_zomblwp = NULL;
    497 			lwp_free(isfree, 1, 0);	/* releases proc mutex */
    498 		} else
    499 			mutex_exit(&p2->p_smutex);
    500 	}
    501 	if (isfree == NULL) {
    502 		l2 = pool_get(&lwp_pool, PR_WAITOK);
    503 		memset(l2, 0, sizeof(*l2));
    504 		l2->l_ts = pool_cache_get(&turnstile_cache, PR_WAITOK);
    505 	} else {
    506 		l2 = isfree;
    507 		ts = l2->l_ts;
    508 		memset(l2, 0, sizeof(*l2));
    509 		l2->l_ts = ts;
    510 	}
    511 
    512 	l2->l_stat = LSIDL;
    513 	l2->l_proc = p2;
    514 	l2->l_refcnt = 1;
    515 	l2->l_priority = l1->l_priority;
    516 	l2->l_usrpri = l1->l_usrpri;
    517 	l2->l_mutex = &sched_mutex;
    518 	l2->l_cpu = l1->l_cpu;
    519 	l2->l_flag = inmem ? L_INMEM : 0;
    520 	lwp_initspecific(l2);
    521 
    522 	if (p2->p_flag & P_SYSTEM) {
    523 		/*
    524 		 * Mark it as a system process and not a candidate for
    525 		 * swapping.
    526 		 */
    527 		l2->l_flag |= L_SYSTEM;
    528 	}
    529 
    530 	lwp_update_creds(l2);
    531 	callout_init(&l2->l_tsleep_ch);
    532 	cv_init(&l2->l_sigcv, "sigwait");
    533 	l2->l_syncobj = &sched_syncobj;
    534 
    535 	if (rnewlwpp != NULL)
    536 		*rnewlwpp = l2;
    537 
    538 	l2->l_addr = UAREA_TO_USER(uaddr);
    539 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    540 	    (arg != NULL) ? arg : l2);
    541 
    542 	mutex_enter(&p2->p_smutex);
    543 
    544 	if ((flags & LWP_DETACHED) != 0) {
    545 		l2->l_prflag = LPR_DETACHED;
    546 		p2->p_ndlwps++;
    547 	} else
    548 		l2->l_prflag = 0;
    549 
    550 	l2->l_sigmask = l1->l_sigmask;
    551 	CIRCLEQ_INIT(&l2->l_sigpend.sp_info);
    552 	sigemptyset(&l2->l_sigpend.sp_set);
    553 
    554 	l2->l_lid = ++p2->p_nlwpid;
    555 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    556 	p2->p_nlwps++;
    557 
    558 	mutex_exit(&p2->p_smutex);
    559 
    560 	mutex_enter(&proclist_mutex);
    561 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    562 	mutex_exit(&proclist_mutex);
    563 
    564 	if (p2->p_emul->e_lwp_fork)
    565 		(*p2->p_emul->e_lwp_fork)(l1, l2);
    566 
    567 	return (0);
    568 }
    569 
    570 /*
    571  * Quit the process.  This will call cpu_exit, which will call cpu_switch,
    572  * so this can only be used meaningfully if you're willing to switch away.
    573  * Calling with l!=curlwp would be weird.
    574  */
    575 void
    576 lwp_exit(struct lwp *l)
    577 {
    578 	struct proc *p = l->l_proc;
    579 	struct lwp *l2;
    580 
    581 	DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
    582 	DPRINTF((" nlwps: %d nzlwps: %d\n", p->p_nlwps, p->p_nzlwps));
    583 
    584 	/*
    585 	 * Verify that we hold no locks other than the kernel lock.
    586 	 */
    587 #ifdef MULTIPROCESSOR
    588 	LOCKDEBUG_BARRIER(&kernel_lock, 0);
    589 #else
    590 	LOCKDEBUG_BARRIER(NULL, 0);
    591 #endif
    592 
    593 	/*
    594 	 * If we are the last live LWP in a process, we need to exit the
    595 	 * entire process.  We do so with an exit status of zero, because
    596 	 * it's a "controlled" exit, and because that's what Solaris does.
    597 	 *
    598 	 * We are not quite a zombie yet, but for accounting purposes we
    599 	 * must increment the count of zombies here.
    600 	 *
    601 	 * Note: the last LWP's specificdata will be deleted here.
    602 	 */
    603 	mutex_enter(&p->p_smutex);
    604 	if (p->p_nlwps - p->p_nzlwps == 1) {
    605 		DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
    606 		    p->p_pid, l->l_lid));
    607 		exit1(l, 0);
    608 		/* NOTREACHED */
    609 	}
    610 	p->p_nzlwps++;
    611 	mutex_exit(&p->p_smutex);
    612 
    613 	if (p->p_emul->e_lwp_exit)
    614 		(*p->p_emul->e_lwp_exit)(l);
    615 
    616 	/* Delete the specificdata while it's still safe to sleep. */
    617 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
    618 
    619 	/*
    620 	 * Release our cached credentials.
    621 	 */
    622 	kauth_cred_free(l->l_cred);
    623 
    624 	/*
    625 	 * Remove the LWP from the global list.
    626 	 */
    627 	mutex_enter(&proclist_mutex);
    628 	LIST_REMOVE(l, l_list);
    629 	mutex_exit(&proclist_mutex);
    630 
    631 	/*
    632 	 * Get rid of all references to the LWP that others (e.g. procfs)
    633 	 * may have, and mark the LWP as a zombie.  If the LWP is detached,
    634 	 * mark it waiting for collection in the proc structure.  Note that
    635 	 * before we can do that, we need to free any other dead, deatched
    636 	 * LWP waiting to meet its maker.
    637 	 *
    638 	 * XXXSMP disable preemption.
    639 	 */
    640 	mutex_enter(&p->p_smutex);
    641 	lwp_drainrefs(l);
    642 
    643 	if ((l->l_prflag & LPR_DETACHED) != 0) {
    644 		while ((l2 = p->p_zomblwp) != NULL) {
    645 			p->p_zomblwp = NULL;
    646 			lwp_free(l2, 0, 0);	/* releases proc mutex */
    647 			mutex_enter(&p->p_smutex);
    648 		}
    649 		p->p_zomblwp = l;
    650 	}
    651 
    652 	/*
    653 	 * Clear any private, pending signals.  If we find a pending signal
    654 	 * for the process and we have been asked to check for signals, then
    655 	 * we loose badly: arrange to have all other LWPs in the process check
    656 	 * for signals.
    657 	 */
    658 	sigclear(&l->l_sigpend, NULL);
    659 	if ((l->l_flag & L_PENDSIG) != 0 &&
    660 	    firstsig(&p->p_sigpend.sp_set) != 0) {
    661 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    662 			lwp_lock(l2);
    663 			l2->l_flag |= L_PENDSIG;
    664 			lwp_unlock(l2);
    665 		}
    666 	}
    667 
    668 	lwp_lock(l);
    669 	l->l_stat = LSZOMB;
    670 	lwp_unlock(l);
    671 	p->p_nrlwps--;
    672 	mutex_exit(&p->p_smutex);
    673 
    674 	/*
    675 	 * We can no longer block.  At this point, lwp_free() may already
    676 	 * be gunning for us.  On a multi-CPU system, we may be off p_lwps.
    677 	 *
    678 	 * Free MD LWP resources.
    679 	 */
    680 #ifndef __NO_CPU_LWP_FREE
    681 	cpu_lwp_free(l, 0);
    682 #endif
    683 	pmap_deactivate(l);
    684 
    685 	/*
    686 	 * Release the kernel lock, signal another LWP to collect us,
    687 	 * and switch away into oblivion.
    688 	 */
    689 #ifdef notyet
    690 	/* XXXSMP hold in lwp_userret() */
    691 	KERNEL_UNLOCK_LAST(l);
    692 #else
    693 	KERNEL_UNLOCK_ALL(l, NULL);
    694 #endif
    695 
    696 	cv_broadcast(&p->p_lwpcv);
    697 	cpu_exit(l);
    698 }
    699 
    700 /*
    701  * We are called from cpu_exit() once it is safe to schedule the dead LWP's
    702  * resources to be freed (i.e., once we've switched to the idle PCB for the
    703  * current CPU).
    704  */
    705 void
    706 lwp_exit2(struct lwp *l)
    707 {
    708 	/* XXXSMP re-enable preemption */
    709 }
    710 
    711 /*
    712  * Free a dead LWP's remaining resources.
    713  *
    714  * XXXLWP limits.
    715  */
    716 void
    717 lwp_free(struct lwp *l, int recycle, int last)
    718 {
    719 	struct proc *p = l->l_proc;
    720 
    721 	/*
    722 	 * If this was not the last LWP in the process, then adjust
    723 	 * counters and unlock.
    724 	 */
    725 	if (!last) {
    726 		/*
    727 		 * Add the LWP's run time to the process' base value.
    728 		 * This needs to co-incide with coming off p_lwps.
    729 		 */
    730 		timeradd(&l->l_rtime, &p->p_rtime, &p->p_rtime);
    731 
    732 		LIST_REMOVE(l, l_sibling);
    733 		p->p_nlwps--;
    734 		p->p_nzlwps--;
    735 		if ((l->l_prflag & LPR_DETACHED) != 0)
    736 			p->p_ndlwps--;
    737 		mutex_exit(&p->p_smutex);
    738 
    739 #ifdef MULTIPROCESSOR
    740 		/*
    741 		 * In the unlikely event that the LWP is still on the CPU,
    742 		 * then spin until it has switched away.  We need to release
    743 		 * all locks to avoid deadlock against interrupt handlers on
    744 		 * the target CPU.
    745 		 */
    746 		if (l->l_cpu->ci_curlwp == l) {
    747 			int count;
    748 			KERNEL_UNLOCK_ALL(curlwp, &count);
    749 			while (l->l_cpu->ci_curlwp == l)
    750 				SPINLOCK_BACKOFF_HOOK;
    751 			KERNEL_LOCK(count, curlwp);
    752 		}
    753 #endif
    754 	}
    755 
    756 	/*
    757 	 * Free the LWP's turnstile and the LWP structure itself unless the
    758 	 * caller wants to recycle them.
    759 	 *
    760 	 * We can't return turnstile0 to the pool (it didn't come from it),
    761 	 * so if it comes up just drop it quietly and move on.
    762 	 *
    763 	 * We don't recycle the VM resources at this time.
    764 	 */
    765 	if (!recycle && l->l_ts != &turnstile0)
    766 		pool_cache_put(&turnstile_cache, l->l_ts);
    767 #ifndef __NO_CPU_LWP_FREE
    768 	cpu_lwp_free2(l);
    769 #endif
    770 	uvm_lwp_exit(l);
    771 	cv_destroy(&l->l_sigcv);
    772 	if (!recycle)
    773 		pool_put(&lwp_pool, l);
    774 }
    775 
    776 /*
    777  * Pick a LWP to represent the process for those operations which
    778  * want information about a "process" that is actually associated
    779  * with a LWP.
    780  *
    781  * If 'locking' is false, no locking or lock checks are performed.
    782  * This is intended for use by DDB.
    783  *
    784  * We don't bother locking the LWP here, since code that uses this
    785  * interface is broken by design and an exact match is not required.
    786  */
    787 struct lwp *
    788 proc_representative_lwp(struct proc *p, int *nrlwps, int locking)
    789 {
    790 	struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
    791 	struct lwp *signalled;
    792 	int cnt;
    793 
    794 	if (locking) {
    795 		LOCK_ASSERT(mutex_owned(&p->p_smutex));
    796 	}
    797 
    798 	/* Trivial case: only one LWP */
    799 	if (p->p_nlwps == 1) {
    800 		l = LIST_FIRST(&p->p_lwps);
    801 		if (nrlwps)
    802 			*nrlwps = (l->l_stat == LSONPROC || LSRUN);
    803 		return l;
    804 	}
    805 
    806 	cnt = 0;
    807 	switch (p->p_stat) {
    808 	case SSTOP:
    809 	case SACTIVE:
    810 		/* Pick the most live LWP */
    811 		onproc = running = sleeping = stopped = suspended = NULL;
    812 		signalled = NULL;
    813 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    814 			if (l->l_lid == p->p_sigctx.ps_lwp)
    815 				signalled = l;
    816 			switch (l->l_stat) {
    817 			case LSONPROC:
    818 				onproc = l;
    819 				cnt++;
    820 				break;
    821 			case LSRUN:
    822 				running = l;
    823 				cnt++;
    824 				break;
    825 			case LSSLEEP:
    826 				sleeping = l;
    827 				break;
    828 			case LSSTOP:
    829 				stopped = l;
    830 				break;
    831 			case LSSUSPENDED:
    832 				suspended = l;
    833 				break;
    834 			}
    835 		}
    836 		if (nrlwps)
    837 			*nrlwps = cnt;
    838 		if (signalled)
    839 			l = signalled;
    840 		else if (onproc)
    841 			l = onproc;
    842 		else if (running)
    843 			l = running;
    844 		else if (sleeping)
    845 			l = sleeping;
    846 		else if (stopped)
    847 			l = stopped;
    848 		else if (suspended)
    849 			l = suspended;
    850 		else
    851 			break;
    852 		return l;
    853 		if (nrlwps)
    854 			*nrlwps = 0;
    855 		l = LIST_FIRST(&p->p_lwps);
    856 		return l;
    857 #ifdef DIAGNOSTIC
    858 	case SIDL:
    859 	case SZOMB:
    860 	case SDYING:
    861 	case SDEAD:
    862 		if (locking)
    863 			mutex_exit(&p->p_smutex);
    864 		/* We have more than one LWP and we're in SIDL?
    865 		 * How'd that happen?
    866 		 */
    867 		panic("Too many LWPs in idle/dying process %d (%s) stat = %d",
    868 		    p->p_pid, p->p_comm, p->p_stat);
    869 		break;
    870 	default:
    871 		if (locking)
    872 			mutex_exit(&p->p_smutex);
    873 		panic("Process %d (%s) in unknown state %d",
    874 		    p->p_pid, p->p_comm, p->p_stat);
    875 #endif
    876 	}
    877 
    878 	if (locking)
    879 		mutex_exit(&p->p_smutex);
    880 	panic("proc_representative_lwp: couldn't find a lwp for process"
    881 		" %d (%s)", p->p_pid, p->p_comm);
    882 	/* NOTREACHED */
    883 	return NULL;
    884 }
    885 
    886 /*
    887  * Look up a live LWP within the speicifed process, and return it locked.
    888  *
    889  * Must be called with p->p_smutex held.
    890  */
    891 struct lwp *
    892 lwp_find(struct proc *p, int id)
    893 {
    894 	struct lwp *l;
    895 
    896 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    897 
    898 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    899 		if (l->l_lid == id)
    900 			break;
    901 	}
    902 
    903 	/*
    904 	 * No need to lock - all of these conditions will
    905 	 * be visible with the process level mutex held.
    906 	 */
    907 	if (l != NULL && (l->l_stat == LSIDL || l->l_stat == LSZOMB))
    908 		l = NULL;
    909 
    910 	return l;
    911 }
    912 
    913 /*
    914  * Update an LWP's cached credentials to mirror the process' master copy.
    915  *
    916  * This happens early in the syscall path, on user trap, and on LWP
    917  * creation.  A long-running LWP can also voluntarily choose to update
    918  * it's credentials by calling this routine.  This may be called from
    919  * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
    920  */
    921 void
    922 lwp_update_creds(struct lwp *l)
    923 {
    924 	kauth_cred_t oc;
    925 	struct proc *p;
    926 
    927 	p = l->l_proc;
    928 	oc = l->l_cred;
    929 
    930 	mutex_enter(&p->p_mutex);
    931 	kauth_cred_hold(p->p_cred);
    932 	l->l_cred = p->p_cred;
    933 	mutex_exit(&p->p_mutex);
    934 	if (oc != NULL) {
    935 		KERNEL_LOCK(1, l);	/* XXXSMP */
    936 		kauth_cred_free(oc);
    937 		KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    938 	}
    939 }
    940 
    941 /*
    942  * Verify that an LWP is locked, and optionally verify that the lock matches
    943  * one we specify.
    944  */
    945 int
    946 lwp_locked(struct lwp *l, kmutex_t *mtx)
    947 {
    948 	kmutex_t *cur = l->l_mutex;
    949 
    950 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    951 	return mutex_owned(cur) && (mtx == cur || mtx == NULL);
    952 #else
    953 	return mutex_owned(cur);
    954 #endif
    955 }
    956 
    957 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    958 /*
    959  * Lock an LWP.
    960  */
    961 void
    962 lwp_lock_retry(struct lwp *l, kmutex_t *old)
    963 {
    964 
    965 	/*
    966 	 * XXXgcc ignoring kmutex_t * volatile on i386
    967 	 *
    968 	 * gcc version 4.1.2 20061021 prerelease (NetBSD nb1 20061021)
    969 	 */
    970 #if 1
    971 	while (l->l_mutex != old) {
    972 #else
    973 	for (;;) {
    974 #endif
    975 		mutex_spin_exit(old);
    976 		old = l->l_mutex;
    977 		mutex_spin_enter(old);
    978 
    979 		/*
    980 		 * mutex_enter() will have posted a read barrier.  Re-test
    981 		 * l->l_mutex.  If it has changed, we need to try again.
    982 		 */
    983 #if 1
    984 	}
    985 #else
    986 	} while (__predict_false(l->l_mutex != old));
    987 #endif
    988 }
    989 #endif
    990 
    991 /*
    992  * Lend a new mutex to an LWP.  The old mutex must be held.
    993  */
    994 void
    995 lwp_setlock(struct lwp *l, kmutex_t *new)
    996 {
    997 
    998 	LOCK_ASSERT(mutex_owned(l->l_mutex));
    999 
   1000 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1001 	mb_write();
   1002 	l->l_mutex = new;
   1003 #else
   1004 	(void)new;
   1005 #endif
   1006 }
   1007 
   1008 /*
   1009  * Lend a new mutex to an LWP, and release the old mutex.  The old mutex
   1010  * must be held.
   1011  */
   1012 void
   1013 lwp_unlock_to(struct lwp *l, kmutex_t *new)
   1014 {
   1015 	kmutex_t *old;
   1016 
   1017 	LOCK_ASSERT(mutex_owned(l->l_mutex));
   1018 
   1019 	old = l->l_mutex;
   1020 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1021 	mb_write();
   1022 	l->l_mutex = new;
   1023 #else
   1024 	(void)new;
   1025 #endif
   1026 	mutex_spin_exit(old);
   1027 }
   1028 
   1029 /*
   1030  * Acquire a new mutex, and donate it to an LWP.  The LWP must already be
   1031  * locked.
   1032  */
   1033 void
   1034 lwp_relock(struct lwp *l, kmutex_t *new)
   1035 {
   1036 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1037 	kmutex_t *old;
   1038 #endif
   1039 
   1040 	LOCK_ASSERT(mutex_owned(l->l_mutex));
   1041 
   1042 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1043 	old = l->l_mutex;
   1044 	if (old != new) {
   1045 		mutex_spin_enter(new);
   1046 		l->l_mutex = new;
   1047 		mutex_spin_exit(old);
   1048 	}
   1049 #else
   1050 	(void)new;
   1051 #endif
   1052 }
   1053 
   1054 /*
   1055  * Handle exceptions for mi_userret().  Called if a member of L_USERRET is
   1056  * set.
   1057  */
   1058 void
   1059 lwp_userret(struct lwp *l)
   1060 {
   1061 	struct proc *p;
   1062 	int sig;
   1063 
   1064 	p = l->l_proc;
   1065 
   1066 	/*
   1067 	 * It should be safe to do this read unlocked on a multiprocessor
   1068 	 * system..
   1069 	 */
   1070 	while ((l->l_flag & L_USERRET) != 0) {
   1071 		/*
   1072 		 * Process pending signals first, unless the process
   1073 		 * is dumping core, where we will instead enter the
   1074 		 * L_WSUSPEND case below.
   1075 		 */
   1076 		if ((l->l_flag & (L_PENDSIG | L_WCORE)) == L_PENDSIG) {
   1077 			KERNEL_LOCK(1, l);	/* XXXSMP pool_put() below */
   1078 			mutex_enter(&p->p_smutex);
   1079 			while ((sig = issignal(l)) != 0)
   1080 				postsig(sig);
   1081 			mutex_exit(&p->p_smutex);
   1082 			KERNEL_UNLOCK_LAST(l);	/* XXXSMP */
   1083 		}
   1084 
   1085 		/*
   1086 		 * Core-dump or suspend pending.
   1087 		 *
   1088 		 * In case of core dump, suspend ourselves, so that the
   1089 		 * kernel stack and therefore the userland registers saved
   1090 		 * in the trapframe are around for coredump() to write them
   1091 		 * out.  We issue a wakeup on p->p_lwpcv so that sigexit()
   1092 		 * will write the core file out once all other LWPs are
   1093 		 * suspended.
   1094 		 */
   1095 		if ((l->l_flag & L_WSUSPEND) != 0) {
   1096 			mutex_enter(&p->p_smutex);
   1097 			p->p_nrlwps--;
   1098 			cv_broadcast(&p->p_lwpcv);
   1099 			lwp_lock(l);
   1100 			l->l_stat = LSSUSPENDED;
   1101 			mutex_exit(&p->p_smutex);
   1102 			mi_switch(l, NULL);
   1103 		}
   1104 
   1105 		/* Process is exiting. */
   1106 		if ((l->l_flag & L_WEXIT) != 0) {
   1107 			KERNEL_LOCK(1, l);
   1108 			lwp_exit(l);
   1109 			KASSERT(0);
   1110 			/* NOTREACHED */
   1111 		}
   1112 	}
   1113 }
   1114 
   1115 /*
   1116  * Force an LWP to enter the kernel, to take a trip through lwp_userret().
   1117  */
   1118 void
   1119 lwp_need_userret(struct lwp *l)
   1120 {
   1121 	LOCK_ASSERT(lwp_locked(l, NULL));
   1122 
   1123 	/*
   1124 	 * Since the tests in lwp_userret() are done unlocked, make sure
   1125 	 * that the condition will be seen before forcing the LWP to enter
   1126 	 * kernel mode.
   1127 	 */
   1128 	mb_write();
   1129 
   1130 	if (l->l_priority > PUSER)
   1131 		lwp_changepri(l, PUSER);
   1132 	cpu_signotify(l);
   1133 }
   1134 
   1135 /*
   1136  * Add one reference to an LWP.  This will prevent the LWP from
   1137  * exiting, thus keep the lwp structure and PCB around to inspect.
   1138  */
   1139 void
   1140 lwp_addref(struct lwp *l)
   1141 {
   1142 
   1143 	LOCK_ASSERT(mutex_owned(&l->l_proc->p_smutex));
   1144 	KASSERT(l->l_stat != LSZOMB);
   1145 	KASSERT(l->l_refcnt != 0);
   1146 
   1147 	l->l_refcnt++;
   1148 }
   1149 
   1150 /*
   1151  * Remove one reference to an LWP.  If this is the last reference,
   1152  * then we must finalize the LWP's death.
   1153  */
   1154 void
   1155 lwp_delref(struct lwp *l)
   1156 {
   1157 	struct proc *p = l->l_proc;
   1158 	u_int refcnt;
   1159 
   1160 	mutex_enter(&p->p_smutex);
   1161 	refcnt = --l->l_refcnt;
   1162 	mutex_exit(&p->p_smutex);
   1163 
   1164 	if (refcnt == 0)
   1165 		cv_broadcast(&p->p_refcv);
   1166 }
   1167 
   1168 /*
   1169  * Drain all references to the current LWP.
   1170  */
   1171 void
   1172 lwp_drainrefs(struct lwp *l)
   1173 {
   1174 	struct proc *p = l->l_proc;
   1175 
   1176 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1177 	KASSERT(l->l_refcnt != 0);
   1178 
   1179 	l->l_refcnt--;
   1180 	while (l->l_refcnt != 0)
   1181 		cv_wait(&p->p_refcv, &p->p_smutex);
   1182 }
   1183 
   1184 /*
   1185  * lwp_specific_key_create --
   1186  *	Create a key for subsystem lwp-specific data.
   1187  */
   1188 int
   1189 lwp_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1190 {
   1191 
   1192 	return (specificdata_key_create(lwp_specificdata_domain, keyp, dtor));
   1193 }
   1194 
   1195 /*
   1196  * lwp_specific_key_delete --
   1197  *	Delete a key for subsystem lwp-specific data.
   1198  */
   1199 void
   1200 lwp_specific_key_delete(specificdata_key_t key)
   1201 {
   1202 
   1203 	specificdata_key_delete(lwp_specificdata_domain, key);
   1204 }
   1205 
   1206 /*
   1207  * lwp_initspecific --
   1208  *	Initialize an LWP's specificdata container.
   1209  */
   1210 void
   1211 lwp_initspecific(struct lwp *l)
   1212 {
   1213 	int error;
   1214 
   1215 	error = specificdata_init(lwp_specificdata_domain, &l->l_specdataref);
   1216 	KASSERT(error == 0);
   1217 }
   1218 
   1219 /*
   1220  * lwp_finispecific --
   1221  *	Finalize an LWP's specificdata container.
   1222  */
   1223 void
   1224 lwp_finispecific(struct lwp *l)
   1225 {
   1226 
   1227 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
   1228 }
   1229 
   1230 /*
   1231  * lwp_getspecific --
   1232  *	Return lwp-specific data corresponding to the specified key.
   1233  *
   1234  *	Note: LWP specific data is NOT INTERLOCKED.  An LWP should access
   1235  *	only its OWN SPECIFIC DATA.  If it is necessary to access another
   1236  *	LWP's specifc data, care must be taken to ensure that doing so
   1237  *	would not cause internal data structure inconsistency (i.e. caller
   1238  *	can guarantee that the target LWP is not inside an lwp_getspecific()
   1239  *	or lwp_setspecific() call).
   1240  */
   1241 void *
   1242 lwp_getspecific(specificdata_key_t key)
   1243 {
   1244 
   1245 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
   1246 						  &curlwp->l_specdataref, key));
   1247 }
   1248 
   1249 void *
   1250 _lwp_getspecific_by_lwp(struct lwp *l, specificdata_key_t key)
   1251 {
   1252 
   1253 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
   1254 						  &l->l_specdataref, key));
   1255 }
   1256 
   1257 /*
   1258  * lwp_setspecific --
   1259  *	Set lwp-specific data corresponding to the specified key.
   1260  */
   1261 void
   1262 lwp_setspecific(specificdata_key_t key, void *data)
   1263 {
   1264 
   1265 	specificdata_setspecific(lwp_specificdata_domain,
   1266 				 &curlwp->l_specdataref, key, data);
   1267 }
   1268