Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.55.2.5
      1 /*	$NetBSD: kern_lwp.c,v 1.55.2.5 2007/02/27 16:54:22 yamt 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  *		tschain_t::tc_mutex |
    188  *
    189  *	Each process has an scheduler state mutex (proc::p_smutex), and a
    190  *	number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
    191  *	so on.  When an LWP is to be entered into or removed from one of the
    192  *	following states, p_mutex must be held and the process wide counters
    193  *	adjusted:
    194  *
    195  *		LSIDL, LSZOMB, LSSTOP, LSSUSPENDED
    196  *
    197  *	Note that an LWP is considered running or likely to run soon if in
    198  *	one of the following states.  This affects the value of p_nrlwps:
    199  *
    200  *		LSRUN, LSONPROC, LSSLEEP
    201  *
    202  *	p_smutex does not need to be held when transitioning among these
    203  *	three states.
    204  */
    205 
    206 #include <sys/cdefs.h>
    207 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.55.2.5 2007/02/27 16:54:22 yamt Exp $");
    208 
    209 #include "opt_multiprocessor.h"
    210 #include "opt_lockdebug.h"
    211 
    212 #define _LWP_API_PRIVATE
    213 
    214 #include <sys/param.h>
    215 #include <sys/systm.h>
    216 #include <sys/cpu.h>
    217 #include <sys/pool.h>
    218 #include <sys/proc.h>
    219 #include <sys/syscallargs.h>
    220 #include <sys/syscall_stats.h>
    221 #include <sys/kauth.h>
    222 #include <sys/sleepq.h>
    223 #include <sys/lockdebug.h>
    224 #include <sys/kmem.h>
    225 
    226 #include <uvm/uvm_extern.h>
    227 
    228 struct lwplist	alllwp;
    229 
    230 POOL_INIT(lwp_pool, sizeof(struct lwp), MIN_LWP_ALIGNMENT, 0, 0, "lwppl",
    231     &pool_allocator_nointr);
    232 POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    233     &pool_allocator_nointr);
    234 
    235 static specificdata_domain_t lwp_specificdata_domain;
    236 
    237 #define LWP_DEBUG
    238 
    239 #ifdef LWP_DEBUG
    240 int lwp_debug = 0;
    241 #define DPRINTF(x) if (lwp_debug) printf x
    242 #else
    243 #define DPRINTF(x)
    244 #endif
    245 
    246 void
    247 lwpinit(void)
    248 {
    249 
    250 	lwp_specificdata_domain = specificdata_domain_create();
    251 	KASSERT(lwp_specificdata_domain != NULL);
    252 	lwp_sys_init();
    253 }
    254 
    255 /*
    256  * Set an suspended.
    257  *
    258  * Must be called with p_smutex held, and the LWP locked.  Will unlock the
    259  * LWP before return.
    260  */
    261 int
    262 lwp_suspend(struct lwp *curl, struct lwp *t)
    263 {
    264 	int error;
    265 
    266 	LOCK_ASSERT(mutex_owned(&t->l_proc->p_smutex));
    267 	LOCK_ASSERT(lwp_locked(t, NULL));
    268 
    269 	KASSERT(curl != t || curl->l_stat == LSONPROC);
    270 
    271 	/*
    272 	 * If the current LWP has been told to exit, we must not suspend anyone
    273 	 * else or deadlock could occur.  We won't return to userspace.
    274 	 */
    275 	if ((curl->l_stat & (LW_WEXIT | LW_WCORE)) != 0) {
    276 		lwp_unlock(t);
    277 		return (EDEADLK);
    278 	}
    279 
    280 	error = 0;
    281 
    282 	switch (t->l_stat) {
    283 	case LSRUN:
    284 	case LSONPROC:
    285 		t->l_flag |= LW_WSUSPEND;
    286 		lwp_need_userret(t);
    287 		lwp_unlock(t);
    288 		break;
    289 
    290 	case LSSLEEP:
    291 		t->l_flag |= LW_WSUSPEND;
    292 
    293 		/*
    294 		 * Kick the LWP and try to get it to the kernel boundary
    295 		 * so that it will release any locks that it holds.
    296 		 * setrunnable() will release the lock.
    297 		 */
    298 		if ((t->l_flag & LW_SINTR) != 0)
    299 			setrunnable(t);
    300 		else
    301 			lwp_unlock(t);
    302 		break;
    303 
    304 	case LSSUSPENDED:
    305 		lwp_unlock(t);
    306 		break;
    307 
    308 	case LSSTOP:
    309 		t->l_flag |= LW_WSUSPEND;
    310 		setrunnable(t);
    311 		break;
    312 
    313 	case LSIDL:
    314 	case LSZOMB:
    315 		error = EINTR; /* It's what Solaris does..... */
    316 		lwp_unlock(t);
    317 		break;
    318 	}
    319 
    320 	/*
    321 	 * XXXLWP Wait for:
    322 	 *
    323 	 * o process exiting
    324 	 * o target LWP suspended
    325 	 * o target LWP not suspended and L_WSUSPEND clear
    326 	 * o target LWP exited
    327 	 */
    328 
    329 	 return (error);
    330 }
    331 
    332 /*
    333  * Restart a suspended LWP.
    334  *
    335  * Must be called with p_smutex held, and the LWP locked.  Will unlock the
    336  * LWP before return.
    337  */
    338 void
    339 lwp_continue(struct lwp *l)
    340 {
    341 
    342 	LOCK_ASSERT(mutex_owned(&l->l_proc->p_smutex));
    343 	LOCK_ASSERT(lwp_locked(l, NULL));
    344 
    345 	DPRINTF(("lwp_continue of %d.%d (%s), state %d, wchan %p\n",
    346 	    l->l_proc->p_pid, l->l_lid, l->l_proc->p_comm, l->l_stat,
    347 	    l->l_wchan));
    348 
    349 	/* If rebooting or not suspended, then just bail out. */
    350 	if ((l->l_flag & LW_WREBOOT) != 0) {
    351 		lwp_unlock(l);
    352 		return;
    353 	}
    354 
    355 	l->l_flag &= ~LW_WSUSPEND;
    356 
    357 	if (l->l_stat != LSSUSPENDED) {
    358 		lwp_unlock(l);
    359 		return;
    360 	}
    361 
    362 	/* setrunnable() will release the lock. */
    363 	setrunnable(l);
    364 }
    365 
    366 /*
    367  * Wait for an LWP within the current process to exit.  If 'lid' is
    368  * non-zero, we are waiting for a specific LWP.
    369  *
    370  * Must be called with p->p_smutex held.
    371  */
    372 int
    373 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    374 {
    375 	struct proc *p = l->l_proc;
    376 	struct lwp *l2;
    377 	int nfound, error;
    378 
    379 	DPRINTF(("lwp_wait1: %d.%d waiting for %d.\n",
    380 	    p->p_pid, l->l_lid, lid));
    381 
    382 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    383 
    384 	/*
    385 	 * We try to check for deadlock:
    386 	 *
    387 	 * 1) If all other LWPs are waiting for exits or suspended.
    388 	 * 2) If we are trying to wait on ourself.
    389 	 *
    390 	 * XXX we'd like to check for a cycle of waiting LWPs (specific LID
    391 	 * waits, not any-LWP waits) and detect that sort of deadlock, but
    392 	 * we don't have a good place to store the lwp that is being waited
    393 	 * for. wchan is already filled with &p->p_nlwps, and putting the
    394 	 * lwp address in there for deadlock tracing would require exiting
    395 	 * LWPs to call wakeup on both their own address and &p->p_nlwps, to
    396 	 * get threads sleeping on any LWP exiting.
    397 	 */
    398 	if (lid == l->l_lid)
    399 		return EDEADLK;
    400 
    401 	p->p_nlwpwait++;
    402 
    403 	for (;;) {
    404 		/*
    405 		 * Avoid a race between exit1() and sigexit(): if the
    406 		 * process is dumping core, then we need to bail out: call
    407 		 * into lwp_userret() where we will be suspended until the
    408 		 * deed is done.
    409 		 */
    410 		if ((p->p_sflag & PS_WCORE) != 0) {
    411 			mutex_exit(&p->p_smutex);
    412 			lwp_userret(l);
    413 #ifdef DIAGNOSTIC
    414 			panic("lwp_wait1");
    415 #endif
    416 			/* NOTREACHED */
    417 		}
    418 
    419 		/*
    420 		 * First off, drain any detached LWP that is waiting to be
    421 		 * reaped.
    422 		 */
    423 		while ((l2 = p->p_zomblwp) != NULL) {
    424 			p->p_zomblwp = NULL;
    425 			lwp_free(l2, 0, 0);	/* releases proc mutex */
    426 			mutex_enter(&p->p_smutex);
    427 		}
    428 
    429 		/*
    430 		 * Now look for an LWP to collect.  If the whole process is
    431 		 * exiting, count detached LWPs as eligible to be collected,
    432 		 * but don't drain them here.
    433 		 */
    434 		nfound = 0;
    435 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    436 			if (l2 == l || (lid != 0 && l2->l_lid != lid))
    437 				continue;
    438 			if ((l2->l_prflag & LPR_DETACHED) != 0) {
    439 				nfound += ((flags & LWPWAIT_EXITCONTROL) != 0);
    440 				continue;
    441 			}
    442 			nfound++;
    443 
    444 			/* No need to lock the LWP in order to see LSZOMB. */
    445 			if (l2->l_stat != LSZOMB)
    446 				continue;
    447 
    448 			if (departed)
    449 				*departed = l2->l_lid;
    450 			lwp_free(l2, 0, 0);
    451 			mutex_enter(&p->p_smutex);
    452 			p->p_nlwpwait--;
    453 			return 0;
    454 		}
    455 
    456 		if (nfound == 0) {
    457 			error = ESRCH;
    458 			break;
    459 		}
    460 		if ((flags & LWPWAIT_EXITCONTROL) != 0) {
    461 			KASSERT(p->p_nlwps > 1);
    462 			cv_wait(&p->p_lwpcv, &p->p_smutex);
    463 			continue;
    464 		}
    465 		if ((p->p_sflag & PS_WEXIT) != 0 ||
    466 		    p->p_nrlwps <= p->p_nlwpwait + p->p_ndlwps) {
    467 			error = EDEADLK;
    468 			break;
    469 		}
    470 		if ((error = cv_wait_sig(&p->p_lwpcv, &p->p_smutex)) != 0)
    471 			break;
    472 	}
    473 
    474 	p->p_nlwpwait--;
    475 	return error;
    476 }
    477 
    478 /*
    479  * Create a new LWP within process 'p2', using LWP 'l1' as a template.
    480  * The new LWP is created in state LSIDL and must be set running,
    481  * suspended, or stopped by the caller.
    482  */
    483 int
    484 newlwp(struct lwp *l1, struct proc *p2, vaddr_t uaddr, bool inmem,
    485     int flags, void *stack, size_t stacksize,
    486     void (*func)(void *), void *arg, struct lwp **rnewlwpp)
    487 {
    488 	struct lwp *l2, *isfree;
    489 	turnstile_t *ts;
    490 
    491 	/*
    492 	 * First off, reap any detached LWP waiting to be collected.
    493 	 * We can re-use its LWP structure and turnstile.
    494 	 */
    495 	isfree = NULL;
    496 	if (p2->p_zomblwp != NULL) {
    497 		mutex_enter(&p2->p_smutex);
    498 		if ((isfree = p2->p_zomblwp) != NULL) {
    499 			p2->p_zomblwp = NULL;
    500 			lwp_free(isfree, 1, 0);	/* releases proc mutex */
    501 		} else
    502 			mutex_exit(&p2->p_smutex);
    503 	}
    504 	if (isfree == NULL) {
    505 		l2 = pool_get(&lwp_pool, PR_WAITOK);
    506 		memset(l2, 0, sizeof(*l2));
    507 		l2->l_ts = pool_cache_get(&turnstile_cache, PR_WAITOK);
    508 		SLIST_INIT(&l2->l_pi_lenders);
    509 	} else {
    510 		l2 = isfree;
    511 		ts = l2->l_ts;
    512 		KASSERT(l2->l_inheritedprio == MAXPRI);
    513 		KASSERT(SLIST_EMPTY(&l2->l_pi_lenders));
    514 		memset(l2, 0, sizeof(*l2));
    515 		l2->l_ts = ts;
    516 	}
    517 
    518 	l2->l_stat = LSIDL;
    519 	l2->l_proc = p2;
    520 	l2->l_refcnt = 1;
    521 	l2->l_priority = l1->l_priority;
    522 	l2->l_usrpri = l1->l_usrpri;
    523 	l2->l_inheritedprio = MAXPRI;
    524 	l2->l_mutex = &sched_mutex;
    525 	l2->l_cpu = l1->l_cpu;
    526 	l2->l_flag = inmem ? LW_INMEM : 0;
    527 	lwp_initspecific(l2);
    528 
    529 	if (p2->p_flag & PK_SYSTEM) {
    530 		/*
    531 		 * Mark it as a system process and not a candidate for
    532 		 * swapping.
    533 		 */
    534 		l2->l_flag |= LW_SYSTEM;
    535 	}
    536 
    537 	lwp_update_creds(l2);
    538 	callout_init(&l2->l_tsleep_ch);
    539 	cv_init(&l2->l_sigcv, "sigwait");
    540 	l2->l_syncobj = &sched_syncobj;
    541 
    542 	if (rnewlwpp != NULL)
    543 		*rnewlwpp = l2;
    544 
    545 	l2->l_addr = UAREA_TO_USER(uaddr);
    546 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    547 	    (arg != NULL) ? arg : l2);
    548 
    549 	mutex_enter(&p2->p_smutex);
    550 
    551 	if ((flags & LWP_DETACHED) != 0) {
    552 		l2->l_prflag = LPR_DETACHED;
    553 		p2->p_ndlwps++;
    554 	} else
    555 		l2->l_prflag = 0;
    556 
    557 	l2->l_sigmask = l1->l_sigmask;
    558 	CIRCLEQ_INIT(&l2->l_sigpend.sp_info);
    559 	sigemptyset(&l2->l_sigpend.sp_set);
    560 
    561 	p2->p_nlwpid++;
    562 	if (p2->p_nlwpid == 0)
    563 		p2->p_nlwpid++;
    564 	l2->l_lid = p2->p_nlwpid;
    565 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    566 	p2->p_nlwps++;
    567 
    568 	mutex_exit(&p2->p_smutex);
    569 
    570 	mutex_enter(&proclist_mutex);
    571 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    572 	mutex_exit(&proclist_mutex);
    573 
    574 	SYSCALL_TIME_LWP_INIT(l2);
    575 
    576 	if (p2->p_emul->e_lwp_fork)
    577 		(*p2->p_emul->e_lwp_fork)(l1, l2);
    578 
    579 	return (0);
    580 }
    581 
    582 /*
    583  * Quit the process.
    584  * this can only be used meaningfully if you're willing to switch away.
    585  * Calling with l!=curlwp would be weird.
    586  */
    587 void
    588 lwp_exit(struct lwp *l)
    589 {
    590 	struct proc *p = l->l_proc;
    591 	struct lwp *l2;
    592 
    593 	DPRINTF(("lwp_exit: %d.%d exiting.\n", p->p_pid, l->l_lid));
    594 	DPRINTF((" nlwps: %d nzlwps: %d\n", p->p_nlwps, p->p_nzlwps));
    595 
    596 	/*
    597 	 * Verify that we hold no locks other than the kernel lock.
    598 	 */
    599 #ifdef MULTIPROCESSOR
    600 	LOCKDEBUG_BARRIER(&kernel_lock, 0);
    601 #else
    602 	LOCKDEBUG_BARRIER(NULL, 0);
    603 #endif
    604 
    605 	/*
    606 	 * If we are the last live LWP in a process, we need to exit the
    607 	 * entire process.  We do so with an exit status of zero, because
    608 	 * it's a "controlled" exit, and because that's what Solaris does.
    609 	 *
    610 	 * We are not quite a zombie yet, but for accounting purposes we
    611 	 * must increment the count of zombies here.
    612 	 *
    613 	 * Note: the last LWP's specificdata will be deleted here.
    614 	 */
    615 	mutex_enter(&p->p_smutex);
    616 	if (p->p_nlwps - p->p_nzlwps == 1) {
    617 		DPRINTF(("lwp_exit: %d.%d calling exit1()\n",
    618 		    p->p_pid, l->l_lid));
    619 		exit1(l, 0);
    620 		/* NOTREACHED */
    621 	}
    622 	p->p_nzlwps++;
    623 	mutex_exit(&p->p_smutex);
    624 
    625 	if (p->p_emul->e_lwp_exit)
    626 		(*p->p_emul->e_lwp_exit)(l);
    627 
    628 	/* Delete the specificdata while it's still safe to sleep. */
    629 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
    630 
    631 	/*
    632 	 * Release our cached credentials.
    633 	 */
    634 	kauth_cred_free(l->l_cred);
    635 
    636 	/*
    637 	 * Remove the LWP from the global list.
    638 	 */
    639 	mutex_enter(&proclist_mutex);
    640 	LIST_REMOVE(l, l_list);
    641 	mutex_exit(&proclist_mutex);
    642 
    643 	/*
    644 	 * Get rid of all references to the LWP that others (e.g. procfs)
    645 	 * may have, and mark the LWP as a zombie.  If the LWP is detached,
    646 	 * mark it waiting for collection in the proc structure.  Note that
    647 	 * before we can do that, we need to free any other dead, deatched
    648 	 * LWP waiting to meet its maker.
    649 	 *
    650 	 * XXXSMP disable preemption.
    651 	 */
    652 	mutex_enter(&p->p_smutex);
    653 	lwp_drainrefs(l);
    654 
    655 	if ((l->l_prflag & LPR_DETACHED) != 0) {
    656 		while ((l2 = p->p_zomblwp) != NULL) {
    657 			p->p_zomblwp = NULL;
    658 			lwp_free(l2, 0, 0);	/* releases proc mutex */
    659 			mutex_enter(&p->p_smutex);
    660 		}
    661 		p->p_zomblwp = l;
    662 	}
    663 
    664 	/*
    665 	 * If we find a pending signal for the process and we have been
    666 	 * asked to check for signals, then we loose: arrange to have
    667 	 * all other LWPs in the process check for signals.
    668 	 */
    669 	if ((l->l_flag & LW_PENDSIG) != 0 &&
    670 	    firstsig(&p->p_sigpend.sp_set) != 0) {
    671 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    672 			lwp_lock(l2);
    673 			l2->l_flag |= LW_PENDSIG;
    674 			lwp_unlock(l2);
    675 		}
    676 	}
    677 
    678 	lwp_lock(l);
    679 	l->l_stat = LSZOMB;
    680 	lwp_unlock(l);
    681 	p->p_nrlwps--;
    682 	cv_broadcast(&p->p_lwpcv);
    683 	mutex_exit(&p->p_smutex);
    684 
    685 	/*
    686 	 * We can no longer block.  At this point, lwp_free() may already
    687 	 * be gunning for us.  On a multi-CPU system, we may be off p_lwps.
    688 	 *
    689 	 * Free MD LWP resources.
    690 	 */
    691 #ifndef __NO_CPU_LWP_FREE
    692 	cpu_lwp_free(l, 0);
    693 #endif
    694 	pmap_deactivate(l);
    695 
    696 	/*
    697 	 * Release the kernel lock, signal another LWP to collect us,
    698 	 * and switch away into oblivion.
    699 	 */
    700 #ifdef notyet
    701 	/* XXXSMP hold in lwp_userret() */
    702 	KERNEL_UNLOCK_LAST(l);
    703 #else
    704 	KERNEL_UNLOCK_ALL(l, NULL);
    705 #endif
    706 
    707 	lwp_exit_switchaway(l);
    708 }
    709 
    710 void
    711 lwp_exit_switchaway(struct lwp *l)
    712 {
    713 	struct cpu_info *ci;
    714 	struct lwp *idlelwp;
    715 
    716 	uvmexp.swtch++; /* XXXSMP unlocked */
    717 
    718 	ci = curcpu();
    719 	idlelwp = ci->ci_data.cpu_idlelwp;
    720 
    721 	idlelwp->l_stat = LSONPROC;
    722 	cpu_switchto(NULL, idlelwp);
    723 }
    724 
    725 /*
    726  * Free a dead LWP's remaining resources.
    727  *
    728  * XXXLWP limits.
    729  */
    730 void
    731 lwp_free(struct lwp *l, int recycle, int last)
    732 {
    733 	struct proc *p = l->l_proc;
    734 	ksiginfoq_t kq;
    735 
    736 	/*
    737 	 * If this was not the last LWP in the process, then adjust
    738 	 * counters and unlock.
    739 	 */
    740 	if (!last) {
    741 		/*
    742 		 * Add the LWP's run time to the process' base value.
    743 		 * This needs to co-incide with coming off p_lwps.
    744 		 */
    745 		timeradd(&l->l_rtime, &p->p_rtime, &p->p_rtime);
    746 		LIST_REMOVE(l, l_sibling);
    747 		p->p_nlwps--;
    748 		p->p_nzlwps--;
    749 		if ((l->l_prflag & LPR_DETACHED) != 0)
    750 			p->p_ndlwps--;
    751 		mutex_exit(&p->p_smutex);
    752 
    753 #ifdef MULTIPROCESSOR
    754 		/*
    755 		 * In the unlikely event that the LWP is still on the CPU,
    756 		 * then spin until it has switched away.  We need to release
    757 		 * all locks to avoid deadlock against interrupt handlers on
    758 		 * the target CPU.
    759 		 */
    760 		if (l->l_cpu->ci_curlwp == l) {
    761 			int count;
    762 			KERNEL_UNLOCK_ALL(curlwp, &count);
    763 			while (l->l_cpu->ci_curlwp == l)
    764 				SPINLOCK_BACKOFF_HOOK;
    765 			KERNEL_LOCK(count, curlwp);
    766 		}
    767 #endif
    768 	}
    769 
    770 	/*
    771 	 * Destroy the LWP's remaining signal information.
    772 	 */
    773 	ksiginfo_queue_init(&kq);
    774 	sigclear(&l->l_sigpend, NULL, &kq);
    775 	ksiginfo_queue_drain(&kq);
    776 	cv_destroy(&l->l_sigcv);
    777 
    778 	/*
    779 	 * Free the LWP's turnstile and the LWP structure itself unless the
    780 	 * caller wants to recycle them.
    781 	 *
    782 	 * We can't return turnstile0 to the pool (it didn't come from it),
    783 	 * so if it comes up just drop it quietly and move on.
    784 	 *
    785 	 * We don't recycle the VM resources at this time.
    786 	 */
    787 	KERNEL_LOCK(1, curlwp);		/* XXXSMP */
    788 	if (!recycle && l->l_ts != &turnstile0)
    789 		pool_cache_put(&turnstile_cache, l->l_ts);
    790 #ifndef __NO_CPU_LWP_FREE
    791 	cpu_lwp_free2(l);
    792 #endif
    793 	uvm_lwp_exit(l);
    794 	KASSERT(SLIST_EMPTY(&l->l_pi_lenders));
    795 	KASSERT(l->l_inheritedprio == MAXPRI);
    796 	if (!recycle)
    797 		pool_put(&lwp_pool, l);
    798 	KERNEL_UNLOCK_ONE(curlwp);	/* XXXSMP */
    799 }
    800 
    801 /*
    802  * Pick a LWP to represent the process for those operations which
    803  * want information about a "process" that is actually associated
    804  * with a LWP.
    805  *
    806  * If 'locking' is false, no locking or lock checks are performed.
    807  * This is intended for use by DDB.
    808  *
    809  * We don't bother locking the LWP here, since code that uses this
    810  * interface is broken by design and an exact match is not required.
    811  */
    812 struct lwp *
    813 proc_representative_lwp(struct proc *p, int *nrlwps, int locking)
    814 {
    815 	struct lwp *l, *onproc, *running, *sleeping, *stopped, *suspended;
    816 	struct lwp *signalled;
    817 	int cnt;
    818 
    819 	if (locking) {
    820 		LOCK_ASSERT(mutex_owned(&p->p_smutex));
    821 	}
    822 
    823 	/* Trivial case: only one LWP */
    824 	if (p->p_nlwps == 1) {
    825 		l = LIST_FIRST(&p->p_lwps);
    826 		if (nrlwps)
    827 			*nrlwps = (l->l_stat == LSONPROC || LSRUN);
    828 		return l;
    829 	}
    830 
    831 	cnt = 0;
    832 	switch (p->p_stat) {
    833 	case SSTOP:
    834 	case SACTIVE:
    835 		/* Pick the most live LWP */
    836 		onproc = running = sleeping = stopped = suspended = NULL;
    837 		signalled = NULL;
    838 		LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    839 			if ((l->l_flag & LW_IDLE) != 0) {
    840 				continue;
    841 			}
    842 			if (l->l_lid == p->p_sigctx.ps_lwp)
    843 				signalled = l;
    844 			switch (l->l_stat) {
    845 			case LSONPROC:
    846 				onproc = l;
    847 				cnt++;
    848 				break;
    849 			case LSRUN:
    850 				running = l;
    851 				cnt++;
    852 				break;
    853 			case LSSLEEP:
    854 				sleeping = l;
    855 				break;
    856 			case LSSTOP:
    857 				stopped = l;
    858 				break;
    859 			case LSSUSPENDED:
    860 				suspended = l;
    861 				break;
    862 			}
    863 		}
    864 		if (nrlwps)
    865 			*nrlwps = cnt;
    866 		if (signalled)
    867 			l = signalled;
    868 		else if (onproc)
    869 			l = onproc;
    870 		else if (running)
    871 			l = running;
    872 		else if (sleeping)
    873 			l = sleeping;
    874 		else if (stopped)
    875 			l = stopped;
    876 		else if (suspended)
    877 			l = suspended;
    878 		else
    879 			break;
    880 		return l;
    881 		if (nrlwps)
    882 			*nrlwps = 0;
    883 		l = LIST_FIRST(&p->p_lwps);
    884 		return l;
    885 #ifdef DIAGNOSTIC
    886 	case SIDL:
    887 	case SZOMB:
    888 	case SDYING:
    889 	case SDEAD:
    890 		if (locking)
    891 			mutex_exit(&p->p_smutex);
    892 		/* We have more than one LWP and we're in SIDL?
    893 		 * How'd that happen?
    894 		 */
    895 		panic("Too many LWPs in idle/dying process %d (%s) stat = %d",
    896 		    p->p_pid, p->p_comm, p->p_stat);
    897 		break;
    898 	default:
    899 		if (locking)
    900 			mutex_exit(&p->p_smutex);
    901 		panic("Process %d (%s) in unknown state %d",
    902 		    p->p_pid, p->p_comm, p->p_stat);
    903 #endif
    904 	}
    905 
    906 	if (locking)
    907 		mutex_exit(&p->p_smutex);
    908 	panic("proc_representative_lwp: couldn't find a lwp for process"
    909 		" %d (%s)", p->p_pid, p->p_comm);
    910 	/* NOTREACHED */
    911 	return NULL;
    912 }
    913 
    914 /*
    915  * Look up a live LWP within the speicifed process, and return it locked.
    916  *
    917  * Must be called with p->p_smutex held.
    918  */
    919 struct lwp *
    920 lwp_find(struct proc *p, int id)
    921 {
    922 	struct lwp *l;
    923 
    924 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
    925 
    926 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
    927 		if (l->l_lid == id)
    928 			break;
    929 	}
    930 
    931 	/*
    932 	 * No need to lock - all of these conditions will
    933 	 * be visible with the process level mutex held.
    934 	 */
    935 	if (l != NULL && (l->l_stat == LSIDL || l->l_stat == LSZOMB))
    936 		l = NULL;
    937 
    938 	return l;
    939 }
    940 
    941 /*
    942  * Update an LWP's cached credentials to mirror the process' master copy.
    943  *
    944  * This happens early in the syscall path, on user trap, and on LWP
    945  * creation.  A long-running LWP can also voluntarily choose to update
    946  * it's credentials by calling this routine.  This may be called from
    947  * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
    948  */
    949 void
    950 lwp_update_creds(struct lwp *l)
    951 {
    952 	kauth_cred_t oc;
    953 	struct proc *p;
    954 
    955 	p = l->l_proc;
    956 	oc = l->l_cred;
    957 
    958 	mutex_enter(&p->p_mutex);
    959 	kauth_cred_hold(p->p_cred);
    960 	l->l_cred = p->p_cred;
    961 	mutex_exit(&p->p_mutex);
    962 	if (oc != NULL) {
    963 		KERNEL_LOCK(1, l);	/* XXXSMP */
    964 		kauth_cred_free(oc);
    965 		KERNEL_UNLOCK_ONE(l);	/* XXXSMP */
    966 	}
    967 }
    968 
    969 /*
    970  * Verify that an LWP is locked, and optionally verify that the lock matches
    971  * one we specify.
    972  */
    973 int
    974 lwp_locked(struct lwp *l, kmutex_t *mtx)
    975 {
    976 	kmutex_t *cur = l->l_mutex;
    977 
    978 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    979 	return mutex_owned(cur) && (mtx == cur || mtx == NULL);
    980 #else
    981 	return mutex_owned(cur);
    982 #endif
    983 }
    984 
    985 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
    986 /*
    987  * Lock an LWP.
    988  */
    989 void
    990 lwp_lock_retry(struct lwp *l, kmutex_t *old)
    991 {
    992 
    993 	/*
    994 	 * XXXgcc ignoring kmutex_t * volatile on i386
    995 	 *
    996 	 * gcc version 4.1.2 20061021 prerelease (NetBSD nb1 20061021)
    997 	 */
    998 #if 1
    999 	while (l->l_mutex != old) {
   1000 #else
   1001 	for (;;) {
   1002 #endif
   1003 		mutex_spin_exit(old);
   1004 		old = l->l_mutex;
   1005 		mutex_spin_enter(old);
   1006 
   1007 		/*
   1008 		 * mutex_enter() will have posted a read barrier.  Re-test
   1009 		 * l->l_mutex.  If it has changed, we need to try again.
   1010 		 */
   1011 #if 1
   1012 	}
   1013 #else
   1014 	} while (__predict_false(l->l_mutex != old));
   1015 #endif
   1016 }
   1017 #endif
   1018 
   1019 /*
   1020  * Lend a new mutex to an LWP.  The old mutex must be held.
   1021  */
   1022 void
   1023 lwp_setlock(struct lwp *l, kmutex_t *new)
   1024 {
   1025 
   1026 	LOCK_ASSERT(mutex_owned(l->l_mutex));
   1027 
   1028 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1029 	mb_write();
   1030 	l->l_mutex = new;
   1031 #else
   1032 	(void)new;
   1033 #endif
   1034 }
   1035 
   1036 /*
   1037  * Lend a new mutex to an LWP, and release the old mutex.  The old mutex
   1038  * must be held.
   1039  */
   1040 void
   1041 lwp_unlock_to(struct lwp *l, kmutex_t *new)
   1042 {
   1043 	kmutex_t *old;
   1044 
   1045 	LOCK_ASSERT(mutex_owned(l->l_mutex));
   1046 
   1047 	old = l->l_mutex;
   1048 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1049 	mb_write();
   1050 	l->l_mutex = new;
   1051 #else
   1052 	(void)new;
   1053 #endif
   1054 	mutex_spin_exit(old);
   1055 }
   1056 
   1057 /*
   1058  * Acquire a new mutex, and donate it to an LWP.  The LWP must already be
   1059  * locked.
   1060  */
   1061 void
   1062 lwp_relock(struct lwp *l, kmutex_t *new)
   1063 {
   1064 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1065 	kmutex_t *old;
   1066 #endif
   1067 
   1068 	LOCK_ASSERT(mutex_owned(l->l_mutex));
   1069 
   1070 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1071 	old = l->l_mutex;
   1072 	if (old != new) {
   1073 		mutex_spin_enter(new);
   1074 		l->l_mutex = new;
   1075 		mutex_spin_exit(old);
   1076 	}
   1077 #else
   1078 	(void)new;
   1079 #endif
   1080 }
   1081 
   1082 int
   1083 lwp_trylock(struct lwp *l)
   1084 {
   1085 #if defined(MULTIPROCESSOR) || defined(LOCKDEBUG)
   1086 	kmutex_t *old;
   1087 
   1088 	for (;;) {
   1089 		if (!mutex_tryenter(old = l->l_mutex))
   1090 			return 0;
   1091 		if (__predict_true(l->l_mutex == old))
   1092 			return 1;
   1093 		mutex_spin_exit(old);
   1094 	}
   1095 #else
   1096 	return mutex_tryenter(l->l_mutex);
   1097 #endif
   1098 }
   1099 
   1100 /*
   1101  * Handle exceptions for mi_userret().  Called if a member of LW_USERRET is
   1102  * set.
   1103  */
   1104 void
   1105 lwp_userret(struct lwp *l)
   1106 {
   1107 	struct proc *p;
   1108 	void (*hook)(void);
   1109 	int sig;
   1110 
   1111 	p = l->l_proc;
   1112 
   1113 	/*
   1114 	 * It should be safe to do this read unlocked on a multiprocessor
   1115 	 * system..
   1116 	 */
   1117 	while ((l->l_flag & LW_USERRET) != 0) {
   1118 		/*
   1119 		 * Process pending signals first, unless the process
   1120 		 * is dumping core, where we will instead enter the
   1121 		 * L_WSUSPEND case below.
   1122 		 */
   1123 		if ((l->l_flag & (LW_PENDSIG | LW_WCORE)) == LW_PENDSIG) {
   1124 			KERNEL_LOCK(1, l);	/* XXXSMP pool_put() below */
   1125 			mutex_enter(&p->p_smutex);
   1126 			while ((sig = issignal(l)) != 0)
   1127 				postsig(sig);
   1128 			mutex_exit(&p->p_smutex);
   1129 			KERNEL_UNLOCK_LAST(l);	/* XXXSMP */
   1130 		}
   1131 
   1132 		/*
   1133 		 * Core-dump or suspend pending.
   1134 		 *
   1135 		 * In case of core dump, suspend ourselves, so that the
   1136 		 * kernel stack and therefore the userland registers saved
   1137 		 * in the trapframe are around for coredump() to write them
   1138 		 * out.  We issue a wakeup on p->p_lwpcv so that sigexit()
   1139 		 * will write the core file out once all other LWPs are
   1140 		 * suspended.
   1141 		 */
   1142 		if ((l->l_flag & LW_WSUSPEND) != 0) {
   1143 			mutex_enter(&p->p_smutex);
   1144 			p->p_nrlwps--;
   1145 			cv_broadcast(&p->p_lwpcv);
   1146 			lwp_lock(l);
   1147 			l->l_stat = LSSUSPENDED;
   1148 			mutex_exit(&p->p_smutex);
   1149 			mi_switch(l, NULL);
   1150 		}
   1151 
   1152 		/* Process is exiting. */
   1153 		if ((l->l_flag & LW_WEXIT) != 0) {
   1154 			KERNEL_LOCK(1, l);
   1155 			lwp_exit(l);
   1156 			KASSERT(0);
   1157 			/* NOTREACHED */
   1158 		}
   1159 
   1160 		/* Call userret hook; used by Linux emulation. */
   1161 		if ((l->l_flag & LW_WUSERRET) != 0) {
   1162 			lwp_lock(l);
   1163 			l->l_flag &= ~LW_WUSERRET;
   1164 			lwp_unlock(l);
   1165 			hook = p->p_userret;
   1166 			p->p_userret = NULL;
   1167 			(*hook)();
   1168 		}
   1169 	}
   1170 }
   1171 
   1172 /*
   1173  * Force an LWP to enter the kernel, to take a trip through lwp_userret().
   1174  */
   1175 void
   1176 lwp_need_userret(struct lwp *l)
   1177 {
   1178 	LOCK_ASSERT(lwp_locked(l, NULL));
   1179 
   1180 	/*
   1181 	 * Since the tests in lwp_userret() are done unlocked, make sure
   1182 	 * that the condition will be seen before forcing the LWP to enter
   1183 	 * kernel mode.
   1184 	 */
   1185 	mb_write();
   1186 	cpu_signotify(l);
   1187 }
   1188 
   1189 /*
   1190  * Add one reference to an LWP.  This will prevent the LWP from
   1191  * exiting, thus keep the lwp structure and PCB around to inspect.
   1192  */
   1193 void
   1194 lwp_addref(struct lwp *l)
   1195 {
   1196 
   1197 	LOCK_ASSERT(mutex_owned(&l->l_proc->p_smutex));
   1198 	KASSERT(l->l_stat != LSZOMB);
   1199 	KASSERT(l->l_refcnt != 0);
   1200 
   1201 	l->l_refcnt++;
   1202 }
   1203 
   1204 /*
   1205  * Remove one reference to an LWP.  If this is the last reference,
   1206  * then we must finalize the LWP's death.
   1207  */
   1208 void
   1209 lwp_delref(struct lwp *l)
   1210 {
   1211 	struct proc *p = l->l_proc;
   1212 
   1213 	mutex_enter(&p->p_smutex);
   1214 	if (--l->l_refcnt == 0)
   1215 		cv_broadcast(&p->p_refcv);
   1216 	mutex_exit(&p->p_smutex);
   1217 }
   1218 
   1219 /*
   1220  * Drain all references to the current LWP.
   1221  */
   1222 void
   1223 lwp_drainrefs(struct lwp *l)
   1224 {
   1225 	struct proc *p = l->l_proc;
   1226 
   1227 	LOCK_ASSERT(mutex_owned(&p->p_smutex));
   1228 	KASSERT(l->l_refcnt != 0);
   1229 
   1230 	l->l_refcnt--;
   1231 	while (l->l_refcnt != 0)
   1232 		cv_wait(&p->p_refcv, &p->p_smutex);
   1233 }
   1234 
   1235 /*
   1236  * lwp_specific_key_create --
   1237  *	Create a key for subsystem lwp-specific data.
   1238  */
   1239 int
   1240 lwp_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1241 {
   1242 
   1243 	return (specificdata_key_create(lwp_specificdata_domain, keyp, dtor));
   1244 }
   1245 
   1246 /*
   1247  * lwp_specific_key_delete --
   1248  *	Delete a key for subsystem lwp-specific data.
   1249  */
   1250 void
   1251 lwp_specific_key_delete(specificdata_key_t key)
   1252 {
   1253 
   1254 	specificdata_key_delete(lwp_specificdata_domain, key);
   1255 }
   1256 
   1257 /*
   1258  * lwp_initspecific --
   1259  *	Initialize an LWP's specificdata container.
   1260  */
   1261 void
   1262 lwp_initspecific(struct lwp *l)
   1263 {
   1264 	int error;
   1265 
   1266 	error = specificdata_init(lwp_specificdata_domain, &l->l_specdataref);
   1267 	KASSERT(error == 0);
   1268 }
   1269 
   1270 /*
   1271  * lwp_finispecific --
   1272  *	Finalize an LWP's specificdata container.
   1273  */
   1274 void
   1275 lwp_finispecific(struct lwp *l)
   1276 {
   1277 
   1278 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
   1279 }
   1280 
   1281 /*
   1282  * lwp_getspecific --
   1283  *	Return lwp-specific data corresponding to the specified key.
   1284  *
   1285  *	Note: LWP specific data is NOT INTERLOCKED.  An LWP should access
   1286  *	only its OWN SPECIFIC DATA.  If it is necessary to access another
   1287  *	LWP's specifc data, care must be taken to ensure that doing so
   1288  *	would not cause internal data structure inconsistency (i.e. caller
   1289  *	can guarantee that the target LWP is not inside an lwp_getspecific()
   1290  *	or lwp_setspecific() call).
   1291  */
   1292 void *
   1293 lwp_getspecific(specificdata_key_t key)
   1294 {
   1295 
   1296 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
   1297 						  &curlwp->l_specdataref, key));
   1298 }
   1299 
   1300 void *
   1301 _lwp_getspecific_by_lwp(struct lwp *l, specificdata_key_t key)
   1302 {
   1303 
   1304 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
   1305 						  &l->l_specdataref, key));
   1306 }
   1307 
   1308 /*
   1309  * lwp_setspecific --
   1310  *	Set lwp-specific data corresponding to the specified key.
   1311  */
   1312 void
   1313 lwp_setspecific(specificdata_key_t key, void *data)
   1314 {
   1315 
   1316 	specificdata_setspecific(lwp_specificdata_domain,
   1317 				 &curlwp->l_specdataref, key, data);
   1318 }
   1319