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