Home | History | Annotate | Line # | Download | only in kern
kern_lwp.c revision 1.93.6.6
      1 /*	$NetBSD: kern_lwp.c,v 1.93.6.6 2009/01/17 13:29:18 mjf Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 2001, 2006, 2007, 2008 The NetBSD Foundation, Inc.
      5  * All rights reserved.
      6  *
      7  * This code is derived from software contributed to The NetBSD Foundation
      8  * by Nathan J. Williams, and Andrew Doran.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     13  * 1. Redistributions of source code must retain the above copyright
     14  *    notice, this list of conditions and the following disclaimer.
     15  * 2. Redistributions in binary form must reproduce the above copyright
     16  *    notice, this list of conditions and the following disclaimer in the
     17  *    documentation and/or other materials provided with the distribution.
     18  *
     19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29  * POSSIBILITY OF SUCH DAMAGE.
     30  */
     31 
     32 /*
     33  * Overview
     34  *
     35  *	Lightweight processes (LWPs) are the basic unit or thread of
     36  *	execution within the kernel.  The core state of an LWP is described
     37  *	by "struct lwp", also known as lwp_t.
     38  *
     39  *	Each LWP is contained within a process (described by "struct proc"),
     40  *	Every process contains at least one LWP, but may contain more.  The
     41  *	process describes attributes shared among all of its LWPs such as a
     42  *	private address space, global execution state (stopped, active,
     43  *	zombie, ...), signal disposition and so on.  On a multiprocessor
     44  *	machine, multiple LWPs be executing concurrently in the kernel.
     45  *
     46  * Execution states
     47  *
     48  *	At any given time, an LWP has overall state that is described by
     49  *	lwp::l_stat.  The states are broken into two sets below.  The first
     50  *	set is guaranteed to represent the absolute, current state of the
     51  *	LWP:
     52  *
     53  *	LSONPROC
     54  *
     55  *		On processor: the LWP is executing on a CPU, either in the
     56  *		kernel or in user space.
     57  *
     58  *	LSRUN
     59  *
     60  *		Runnable: the LWP is parked on a run queue, and may soon be
     61  *		chosen to run by an idle processor, or by a processor that
     62  *		has been asked to preempt a currently runnning but lower
     63  *		priority LWP.  If the LWP is not swapped in (LW_INMEM == 0)
     64  *		then the LWP is not on a run queue, but may be soon.
     65  *
     66  *	LSIDL
     67  *
     68  *		Idle: the LWP has been created but has not yet executed,
     69  *		or it has ceased executing a unit of work and is waiting
     70  *		to be started again.
     71  *
     72  *	LSSUSPENDED:
     73  *
     74  *		Suspended: the LWP has had its execution suspended by
     75  *		another LWP in the same process using the _lwp_suspend()
     76  *		system call.  User-level LWPs also enter the suspended
     77  *		state when the system is shutting down.
     78  *
     79  *	The second set represent a "statement of intent" on behalf of the
     80  *	LWP.  The LWP may in fact be executing on a processor, may be
     81  *	sleeping or idle. It is expected to take the necessary action to
     82  *	stop executing or become "running" again within a short timeframe.
     83  *	The LP_RUNNING flag in lwp::l_pflag indicates that an LWP is running.
     84  *	Importantly, it indicates that its state is tied to a CPU.
     85  *
     86  *	LSZOMB:
     87  *
     88  *		Dead or dying: the LWP has released most of its resources
     89  *		and is: a) about to switch away into oblivion b) has already
     90  *		switched away.  When it switches away, its few remaining
     91  *		resources can be collected.
     92  *
     93  *	LSSLEEP:
     94  *
     95  *		Sleeping: the LWP has entered itself onto a sleep queue, and
     96  *		has switched away or will switch away shortly to allow other
     97  *		LWPs to run on the CPU.
     98  *
     99  *	LSSTOP:
    100  *
    101  *		Stopped: the LWP has been stopped as a result of a job
    102  *		control signal, or as a result of the ptrace() interface.
    103  *
    104  *		Stopped LWPs may run briefly within the kernel to handle
    105  *		signals that they receive, but will not return to user space
    106  *		until their process' state is changed away from stopped.
    107  *
    108  *		Single LWPs within a process can not be set stopped
    109  *		selectively: all actions that can stop or continue LWPs
    110  *		occur at the process level.
    111  *
    112  * State transitions
    113  *
    114  *	Note that the LSSTOP state may only be set when returning to
    115  *	user space in userret(), or when sleeping interruptably.  The
    116  *	LSSUSPENDED state may only be set in userret().  Before setting
    117  *	those states, we try to ensure that the LWPs will release all
    118  *	locks that they hold, and at a minimum try to ensure that the
    119  *	LWP can be set runnable again by a signal.
    120  *
    121  *	LWPs may transition states in the following ways:
    122  *
    123  *	 RUN -------> ONPROC		ONPROC -----> RUN
    124  *		    > STOPPED			    > SLEEP
    125  *		    > SUSPENDED			    > STOPPED
    126  *						    > SUSPENDED
    127  *						    > ZOMB
    128  *
    129  *	 STOPPED ---> RUN		SUSPENDED --> RUN
    130  *	            > SLEEP			    > SLEEP
    131  *
    132  *	 SLEEP -----> ONPROC		IDL --------> RUN
    133  *		    > RUN			    > SUSPENDED
    134  *		    > STOPPED			    > STOPPED
    135  *		    > SUSPENDED
    136  *
    137  *	Other state transitions are possible with kernel threads (eg
    138  *	ONPROC -> IDL), but only happen under tightly controlled
    139  *	circumstances the side effects are understood.
    140  *
    141  * Migration
    142  *
    143  *	Migration of threads from one CPU to another could be performed
    144  *	internally by the scheduler via sched_takecpu() or sched_catchlwp()
    145  *	functions.  The universal lwp_migrate() function should be used for
    146  *	any other cases.  Subsystems in the kernel must be aware that CPU
    147  *	of LWP may change, while it is not locked.
    148  *
    149  * Locking
    150  *
    151  *	The majority of fields in 'struct lwp' are covered by a single,
    152  *	general spin lock pointed to by lwp::l_mutex.  The locks covering
    153  *	each field are documented in sys/lwp.h.
    154  *
    155  *	State transitions must be made with the LWP's general lock held,
    156  *	and may cause the LWP's lock pointer to change. Manipulation of
    157  *	the general lock is not performed directly, but through calls to
    158  *	lwp_lock(), lwp_relock() and similar.
    159  *
    160  *	States and their associated locks:
    161  *
    162  *	LSONPROC, LSZOMB:
    163  *
    164  *		Always covered by spc_lwplock, which protects running LWPs.
    165  *		This is a per-CPU lock.
    166  *
    167  *	LSIDL, LSRUN:
    168  *
    169  *		Always covered by spc_mutex, which protects the run queues.
    170  *		This is a per-CPU lock.
    171  *
    172  *	LSSLEEP:
    173  *
    174  *		Covered by a lock associated with the sleep queue that the
    175  *		LWP resides on.
    176  *
    177  *	LSSTOP, LSSUSPENDED:
    178  *
    179  *		If the LWP was previously sleeping (l_wchan != NULL), then
    180  *		l_mutex references the sleep queue lock.  If the LWP was
    181  *		runnable or on the CPU when halted, or has been removed from
    182  *		the sleep queue since halted, then the lock is spc_lwplock.
    183  *
    184  *	The lock order is as follows:
    185  *
    186  *		spc::spc_lwplock ->
    187  *		    sleeptab::st_mutex ->
    188  *			tschain_t::tc_mutex ->
    189  *			    spc::spc_mutex
    190  *
    191  *	Each process has an scheduler state lock (proc::p_lock), and a
    192  *	number of counters on LWPs and their states: p_nzlwps, p_nrlwps, and
    193  *	so on.  When an LWP is to be entered into or removed from one of the
    194  *	following states, p_lock must be held and the process wide counters
    195  *	adjusted:
    196  *
    197  *		LSIDL, LSZOMB, LSSTOP, LSSUSPENDED
    198  *
    199  *	Note that an LWP is considered running or likely to run soon if in
    200  *	one of the following states.  This affects the value of p_nrlwps:
    201  *
    202  *		LSRUN, LSONPROC, LSSLEEP
    203  *
    204  *	p_lock does not need to be held when transitioning among these
    205  *	three states.
    206  */
    207 
    208 #include <sys/cdefs.h>
    209 __KERNEL_RCSID(0, "$NetBSD: kern_lwp.c,v 1.93.6.6 2009/01/17 13:29:18 mjf Exp $");
    210 
    211 #include "opt_ddb.h"
    212 #include "opt_lockdebug.h"
    213 #include "opt_sa.h"
    214 
    215 #define _LWP_API_PRIVATE
    216 
    217 #include <sys/param.h>
    218 #include <sys/systm.h>
    219 #include <sys/cpu.h>
    220 #include <sys/pool.h>
    221 #include <sys/proc.h>
    222 #include <sys/sa.h>
    223 #include <sys/savar.h>
    224 #include <sys/syscallargs.h>
    225 #include <sys/syscall_stats.h>
    226 #include <sys/kauth.h>
    227 #include <sys/sleepq.h>
    228 #include <sys/user.h>
    229 #include <sys/lockdebug.h>
    230 #include <sys/kmem.h>
    231 #include <sys/pset.h>
    232 #include <sys/intr.h>
    233 #include <sys/lwpctl.h>
    234 #include <sys/atomic.h>
    235 
    236 #include <uvm/uvm_extern.h>
    237 #include <uvm/uvm_object.h>
    238 
    239 struct lwplist	alllwp = LIST_HEAD_INITIALIZER(alllwp);
    240 
    241 POOL_INIT(lwp_uc_pool, sizeof(ucontext_t), 0, 0, 0, "lwpucpl",
    242     &pool_allocator_nointr, IPL_NONE);
    243 
    244 static pool_cache_t lwp_cache;
    245 static specificdata_domain_t lwp_specificdata_domain;
    246 
    247 void
    248 lwpinit(void)
    249 {
    250 
    251 	lwp_specificdata_domain = specificdata_domain_create();
    252 	KASSERT(lwp_specificdata_domain != NULL);
    253 	lwp_sys_init();
    254 	lwp_cache = pool_cache_init(sizeof(lwp_t), MIN_LWP_ALIGNMENT, 0, 0,
    255 	    "lwppl", NULL, IPL_NONE, NULL, NULL, NULL);
    256 }
    257 
    258 /*
    259  * Set an suspended.
    260  *
    261  * Must be called with p_lock held, and the LWP locked.  Will unlock the
    262  * LWP before return.
    263  */
    264 int
    265 lwp_suspend(struct lwp *curl, struct lwp *t)
    266 {
    267 	int error;
    268 
    269 	KASSERT(mutex_owned(t->l_proc->p_lock));
    270 	KASSERT(lwp_locked(t, NULL));
    271 
    272 	KASSERT(curl != t || curl->l_stat == LSONPROC);
    273 
    274 	/*
    275 	 * If the current LWP has been told to exit, we must not suspend anyone
    276 	 * else or deadlock could occur.  We won't return to userspace.
    277 	 */
    278 	if ((curl->l_flag & (LW_WEXIT | LW_WCORE)) != 0) {
    279 		lwp_unlock(t);
    280 		return (EDEADLK);
    281 	}
    282 
    283 	error = 0;
    284 
    285 	switch (t->l_stat) {
    286 	case LSRUN:
    287 	case LSONPROC:
    288 		t->l_flag |= LW_WSUSPEND;
    289 		lwp_need_userret(t);
    290 		lwp_unlock(t);
    291 		break;
    292 
    293 	case LSSLEEP:
    294 		t->l_flag |= LW_WSUSPEND;
    295 
    296 		/*
    297 		 * Kick the LWP and try to get it to the kernel boundary
    298 		 * so that it will release any locks that it holds.
    299 		 * setrunnable() will release the lock.
    300 		 */
    301 		if ((t->l_flag & LW_SINTR) != 0)
    302 			setrunnable(t);
    303 		else
    304 			lwp_unlock(t);
    305 		break;
    306 
    307 	case LSSUSPENDED:
    308 		lwp_unlock(t);
    309 		break;
    310 
    311 	case LSSTOP:
    312 		t->l_flag |= LW_WSUSPEND;
    313 		setrunnable(t);
    314 		break;
    315 
    316 	case LSIDL:
    317 	case LSZOMB:
    318 		error = EINTR; /* It's what Solaris does..... */
    319 		lwp_unlock(t);
    320 		break;
    321 	}
    322 
    323 	return (error);
    324 }
    325 
    326 /*
    327  * Restart a suspended LWP.
    328  *
    329  * Must be called with p_lock held, and the LWP locked.  Will unlock the
    330  * LWP before return.
    331  */
    332 void
    333 lwp_continue(struct lwp *l)
    334 {
    335 
    336 	KASSERT(mutex_owned(l->l_proc->p_lock));
    337 	KASSERT(lwp_locked(l, NULL));
    338 
    339 	/* If rebooting or not suspended, then just bail out. */
    340 	if ((l->l_flag & LW_WREBOOT) != 0) {
    341 		lwp_unlock(l);
    342 		return;
    343 	}
    344 
    345 	l->l_flag &= ~LW_WSUSPEND;
    346 
    347 	if (l->l_stat != LSSUSPENDED) {
    348 		lwp_unlock(l);
    349 		return;
    350 	}
    351 
    352 	/* setrunnable() will release the lock. */
    353 	setrunnable(l);
    354 }
    355 
    356 /*
    357  * Wait for an LWP within the current process to exit.  If 'lid' is
    358  * non-zero, we are waiting for a specific LWP.
    359  *
    360  * Must be called with p->p_lock held.
    361  */
    362 int
    363 lwp_wait1(struct lwp *l, lwpid_t lid, lwpid_t *departed, int flags)
    364 {
    365 	struct proc *p = l->l_proc;
    366 	struct lwp *l2;
    367 	int nfound, error;
    368 	lwpid_t curlid;
    369 	bool exiting;
    370 
    371 	KASSERT(mutex_owned(p->p_lock));
    372 
    373 	p->p_nlwpwait++;
    374 	l->l_waitingfor = lid;
    375 	curlid = l->l_lid;
    376 	exiting = ((flags & LWPWAIT_EXITCONTROL) != 0);
    377 
    378 	for (;;) {
    379 		/*
    380 		 * Avoid a race between exit1() and sigexit(): if the
    381 		 * process is dumping core, then we need to bail out: call
    382 		 * into lwp_userret() where we will be suspended until the
    383 		 * deed is done.
    384 		 */
    385 		if ((p->p_sflag & PS_WCORE) != 0) {
    386 			mutex_exit(p->p_lock);
    387 			lwp_userret(l);
    388 #ifdef DIAGNOSTIC
    389 			panic("lwp_wait1");
    390 #endif
    391 			/* NOTREACHED */
    392 		}
    393 
    394 		/*
    395 		 * First off, drain any detached LWP that is waiting to be
    396 		 * reaped.
    397 		 */
    398 		while ((l2 = p->p_zomblwp) != NULL) {
    399 			p->p_zomblwp = NULL;
    400 			lwp_free(l2, false, false);/* releases proc mutex */
    401 			mutex_enter(p->p_lock);
    402 		}
    403 
    404 		/*
    405 		 * Now look for an LWP to collect.  If the whole process is
    406 		 * exiting, count detached LWPs as eligible to be collected,
    407 		 * but don't drain them here.
    408 		 */
    409 		nfound = 0;
    410 		error = 0;
    411 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    412 			/*
    413 			 * If a specific wait and the target is waiting on
    414 			 * us, then avoid deadlock.  This also traps LWPs
    415 			 * that try to wait on themselves.
    416 			 *
    417 			 * Note that this does not handle more complicated
    418 			 * cycles, like: t1 -> t2 -> t3 -> t1.  The process
    419 			 * can still be killed so it is not a major problem.
    420 			 */
    421 			if (l2->l_lid == lid && l2->l_waitingfor == curlid) {
    422 				error = EDEADLK;
    423 				break;
    424 			}
    425 			if (l2 == l)
    426 				continue;
    427 			if ((l2->l_prflag & LPR_DETACHED) != 0) {
    428 				nfound += exiting;
    429 				continue;
    430 			}
    431 			if (lid != 0) {
    432 				if (l2->l_lid != lid)
    433 					continue;
    434 				/*
    435 				 * Mark this LWP as the first waiter, if there
    436 				 * is no other.
    437 				 */
    438 				if (l2->l_waiter == 0)
    439 					l2->l_waiter = curlid;
    440 			} else if (l2->l_waiter != 0) {
    441 				/*
    442 				 * It already has a waiter - so don't
    443 				 * collect it.  If the waiter doesn't
    444 				 * grab it we'll get another chance
    445 				 * later.
    446 				 */
    447 				nfound++;
    448 				continue;
    449 			}
    450 			nfound++;
    451 
    452 			/* No need to lock the LWP in order to see LSZOMB. */
    453 			if (l2->l_stat != LSZOMB)
    454 				continue;
    455 
    456 			/*
    457 			 * We're no longer waiting.  Reset the "first waiter"
    458 			 * pointer on the target, in case it was us.
    459 			 */
    460 			l->l_waitingfor = 0;
    461 			l2->l_waiter = 0;
    462 			p->p_nlwpwait--;
    463 			if (departed)
    464 				*departed = l2->l_lid;
    465 			sched_lwp_collect(l2);
    466 
    467 			/* lwp_free() releases the proc lock. */
    468 			lwp_free(l2, false, false);
    469 			mutex_enter(p->p_lock);
    470 			return 0;
    471 		}
    472 
    473 		if (error != 0)
    474 			break;
    475 		if (nfound == 0) {
    476 			error = ESRCH;
    477 			break;
    478 		}
    479 
    480 		/*
    481 		 * The kernel is careful to ensure that it can not deadlock
    482 		 * when exiting - just keep waiting.
    483 		 */
    484 		if (exiting) {
    485 			KASSERT(p->p_nlwps > 1);
    486 			cv_wait(&p->p_lwpcv, p->p_lock);
    487 			continue;
    488 		}
    489 
    490 		/*
    491 		 * If all other LWPs are waiting for exits or suspends
    492 		 * and the supply of zombies and potential zombies is
    493 		 * exhausted, then we are about to deadlock.
    494 		 *
    495 		 * If the process is exiting (and this LWP is not the one
    496 		 * that is coordinating the exit) then bail out now.
    497 		 */
    498 		if ((p->p_sflag & PS_WEXIT) != 0 ||
    499 		    p->p_nrlwps + p->p_nzlwps - p->p_ndlwps <= p->p_nlwpwait) {
    500 			error = EDEADLK;
    501 			break;
    502 		}
    503 
    504 		/*
    505 		 * Sit around and wait for something to happen.  We'll be
    506 		 * awoken if any of the conditions examined change: if an
    507 		 * LWP exits, is collected, or is detached.
    508 		 */
    509 		if ((error = cv_wait_sig(&p->p_lwpcv, p->p_lock)) != 0)
    510 			break;
    511 	}
    512 
    513 	/*
    514 	 * We didn't find any LWPs to collect, we may have received a
    515 	 * signal, or some other condition has caused us to bail out.
    516 	 *
    517 	 * If waiting on a specific LWP, clear the waiters marker: some
    518 	 * other LWP may want it.  Then, kick all the remaining waiters
    519 	 * so that they can re-check for zombies and for deadlock.
    520 	 */
    521 	if (lid != 0) {
    522 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    523 			if (l2->l_lid == lid) {
    524 				if (l2->l_waiter == curlid)
    525 					l2->l_waiter = 0;
    526 				break;
    527 			}
    528 		}
    529 	}
    530 	p->p_nlwpwait--;
    531 	l->l_waitingfor = 0;
    532 	cv_broadcast(&p->p_lwpcv);
    533 
    534 	return error;
    535 }
    536 
    537 /*
    538  * Create a new LWP within process 'p2', using LWP 'l1' as a template.
    539  * The new LWP is created in state LSIDL and must be set running,
    540  * suspended, or stopped by the caller.
    541  */
    542 int
    543 lwp_create(lwp_t *l1, proc_t *p2, vaddr_t uaddr, bool inmem, int flags,
    544 	   void *stack, size_t stacksize, void (*func)(void *), void *arg,
    545 	   lwp_t **rnewlwpp, int sclass)
    546 {
    547 	struct lwp *l2, *isfree;
    548 	turnstile_t *ts;
    549 
    550 	KASSERT(l1 == curlwp || l1->l_proc == &proc0);
    551 
    552 	/*
    553 	 * First off, reap any detached LWP waiting to be collected.
    554 	 * We can re-use its LWP structure and turnstile.
    555 	 */
    556 	isfree = NULL;
    557 	if (p2->p_zomblwp != NULL) {
    558 		mutex_enter(p2->p_lock);
    559 		if ((isfree = p2->p_zomblwp) != NULL) {
    560 			p2->p_zomblwp = NULL;
    561 			lwp_free(isfree, true, false);/* releases proc mutex */
    562 		} else
    563 			mutex_exit(p2->p_lock);
    564 	}
    565 	if (isfree == NULL) {
    566 		l2 = pool_cache_get(lwp_cache, PR_WAITOK);
    567 		memset(l2, 0, sizeof(*l2));
    568 		l2->l_ts = pool_cache_get(turnstile_cache, PR_WAITOK);
    569 		SLIST_INIT(&l2->l_pi_lenders);
    570 	} else {
    571 		l2 = isfree;
    572 		ts = l2->l_ts;
    573 		KASSERT(l2->l_inheritedprio == -1);
    574 		KASSERT(SLIST_EMPTY(&l2->l_pi_lenders));
    575 		memset(l2, 0, sizeof(*l2));
    576 		l2->l_ts = ts;
    577 	}
    578 
    579 	l2->l_stat = LSIDL;
    580 	l2->l_proc = p2;
    581 	l2->l_refcnt = 1;
    582 	l2->l_class = sclass;
    583 
    584 	/*
    585 	 * If vfork(), we want the LWP to run fast and on the same CPU
    586 	 * as its parent, so that it can reuse the VM context and cache
    587 	 * footprint on the local CPU.
    588 	 */
    589 	l2->l_kpriority = ((flags & LWP_VFORK) ? true : false);
    590 	l2->l_kpribase = PRI_KERNEL;
    591 	l2->l_priority = l1->l_priority;
    592 	l2->l_inheritedprio = -1;
    593 	l2->l_flag = inmem ? LW_INMEM : 0;
    594 	l2->l_pflag = LP_MPSAFE;
    595 	l2->l_fd = p2->p_fd;
    596 	TAILQ_INIT(&l2->l_ld_locks);
    597 
    598 	if (p2->p_flag & PK_SYSTEM) {
    599 		/* Mark it as a system LWP and not a candidate for swapping */
    600 		l2->l_flag |= LW_SYSTEM;
    601 	}
    602 
    603 	kpreempt_disable();
    604 	l2->l_mutex = l1->l_cpu->ci_schedstate.spc_mutex;
    605 	l2->l_cpu = l1->l_cpu;
    606 	kpreempt_enable();
    607 
    608 	lwp_initspecific(l2);
    609 	sched_lwp_fork(l1, l2);
    610 	lwp_update_creds(l2);
    611 	callout_init(&l2->l_timeout_ch, CALLOUT_MPSAFE);
    612 	callout_setfunc(&l2->l_timeout_ch, sleepq_timeout, l2);
    613 	mutex_init(&l2->l_swaplock, MUTEX_DEFAULT, IPL_NONE);
    614 	cv_init(&l2->l_sigcv, "sigwait");
    615 	l2->l_syncobj = &sched_syncobj;
    616 
    617 	if (rnewlwpp != NULL)
    618 		*rnewlwpp = l2;
    619 
    620 	l2->l_addr = UAREA_TO_USER(uaddr);
    621 	uvm_lwp_fork(l1, l2, stack, stacksize, func,
    622 	    (arg != NULL) ? arg : l2);
    623 
    624 	mutex_enter(p2->p_lock);
    625 
    626 	if ((flags & LWP_DETACHED) != 0) {
    627 		l2->l_prflag = LPR_DETACHED;
    628 		p2->p_ndlwps++;
    629 	} else
    630 		l2->l_prflag = 0;
    631 
    632 	l2->l_sigmask = l1->l_sigmask;
    633 	CIRCLEQ_INIT(&l2->l_sigpend.sp_info);
    634 	sigemptyset(&l2->l_sigpend.sp_set);
    635 
    636 	p2->p_nlwpid++;
    637 	if (p2->p_nlwpid == 0)
    638 		p2->p_nlwpid++;
    639 	l2->l_lid = p2->p_nlwpid;
    640 	LIST_INSERT_HEAD(&p2->p_lwps, l2, l_sibling);
    641 	p2->p_nlwps++;
    642 
    643 	mutex_exit(p2->p_lock);
    644 
    645 	mutex_enter(proc_lock);
    646 	LIST_INSERT_HEAD(&alllwp, l2, l_list);
    647 	mutex_exit(proc_lock);
    648 
    649 	if ((p2->p_flag & PK_SYSTEM) == 0) {
    650 		/* Locking is needed, since LWP is in the list of all LWPs */
    651 		lwp_lock(l2);
    652 		/* Inherit a processor-set */
    653 		l2->l_psid = l1->l_psid;
    654 		/* Inherit an affinity */
    655 		if (l1->l_flag & LW_AFFINITY) {
    656 			proc_t *p = l1->l_proc;
    657 
    658 			mutex_enter(p->p_lock);
    659 			if (l1->l_flag & LW_AFFINITY) {
    660 				kcpuset_use(l1->l_affinity);
    661 				l2->l_affinity = l1->l_affinity;
    662 				l2->l_flag |= LW_AFFINITY;
    663 			}
    664 			mutex_exit(p->p_lock);
    665 		}
    666 		/* Look for a CPU to start */
    667 		l2->l_cpu = sched_takecpu(l2);
    668 		lwp_unlock_to(l2, l2->l_cpu->ci_schedstate.spc_mutex);
    669 	}
    670 
    671 	SYSCALL_TIME_LWP_INIT(l2);
    672 
    673 	if (p2->p_emul->e_lwp_fork)
    674 		(*p2->p_emul->e_lwp_fork)(l1, l2);
    675 
    676 	return (0);
    677 }
    678 
    679 /*
    680  * Called by MD code when a new LWP begins execution.  Must be called
    681  * with the previous LWP locked (so at splsched), or if there is no
    682  * previous LWP, at splsched.
    683  */
    684 void
    685 lwp_startup(struct lwp *prev, struct lwp *new)
    686 {
    687 
    688 	KASSERT(kpreempt_disabled());
    689 	if (prev != NULL) {
    690 		/*
    691 		 * Normalize the count of the spin-mutexes, it was
    692 		 * increased in mi_switch().  Unmark the state of
    693 		 * context switch - it is finished for previous LWP.
    694 		 */
    695 		curcpu()->ci_mtx_count++;
    696 		membar_exit();
    697 		prev->l_ctxswtch = 0;
    698 	}
    699 	KPREEMPT_DISABLE(new);
    700 	spl0();
    701 	pmap_activate(new);
    702 	LOCKDEBUG_BARRIER(NULL, 0);
    703 	KPREEMPT_ENABLE(new);
    704 	if ((new->l_pflag & LP_MPSAFE) == 0) {
    705 		KERNEL_LOCK(1, new);
    706 	}
    707 }
    708 
    709 /*
    710  * Exit an LWP.
    711  */
    712 void
    713 lwp_exit(struct lwp *l)
    714 {
    715 	struct proc *p = l->l_proc;
    716 	struct lwp *l2;
    717 	bool current;
    718 
    719 	current = (l == curlwp);
    720 
    721 	KASSERT(current || (l->l_stat == LSIDL && l->l_target_cpu == NULL));
    722 
    723 	/*
    724 	 * Verify that we hold no locks other than the kernel lock.
    725 	 */
    726 	LOCKDEBUG_BARRIER(&kernel_lock, 0);
    727 
    728 	/*
    729 	 * If we are the last live LWP in a process, we need to exit the
    730 	 * entire process.  We do so with an exit status of zero, because
    731 	 * it's a "controlled" exit, and because that's what Solaris does.
    732 	 *
    733 	 * We are not quite a zombie yet, but for accounting purposes we
    734 	 * must increment the count of zombies here.
    735 	 *
    736 	 * Note: the last LWP's specificdata will be deleted here.
    737 	 */
    738 	mutex_enter(p->p_lock);
    739 	if (p->p_nlwps - p->p_nzlwps == 1) {
    740 		KASSERT(current == true);
    741 		/* XXXSMP kernel_lock not held */
    742 		exit1(l, 0);
    743 		/* NOTREACHED */
    744 	}
    745 	p->p_nzlwps++;
    746 	mutex_exit(p->p_lock);
    747 
    748 	if (p->p_emul->e_lwp_exit)
    749 		(*p->p_emul->e_lwp_exit)(l);
    750 
    751 	/* Delete the specificdata while it's still safe to sleep. */
    752 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
    753 
    754 	/*
    755 	 * Release our cached credentials.
    756 	 */
    757 	kauth_cred_free(l->l_cred);
    758 	callout_destroy(&l->l_timeout_ch);
    759 
    760 	/*
    761 	 * While we can still block, mark the LWP as unswappable to
    762 	 * prevent conflicts with the with the swapper.
    763 	 */
    764 	if (current)
    765 		uvm_lwp_hold(l);
    766 
    767 	/*
    768 	 * Remove the LWP from the global list.
    769 	 */
    770 	mutex_enter(proc_lock);
    771 	LIST_REMOVE(l, l_list);
    772 	mutex_exit(proc_lock);
    773 
    774 	/*
    775 	 * Get rid of all references to the LWP that others (e.g. procfs)
    776 	 * may have, and mark the LWP as a zombie.  If the LWP is detached,
    777 	 * mark it waiting for collection in the proc structure.  Note that
    778 	 * before we can do that, we need to free any other dead, deatched
    779 	 * LWP waiting to meet its maker.
    780 	 */
    781 	mutex_enter(p->p_lock);
    782 	lwp_drainrefs(l);
    783 
    784 	if ((l->l_prflag & LPR_DETACHED) != 0) {
    785 		while ((l2 = p->p_zomblwp) != NULL) {
    786 			p->p_zomblwp = NULL;
    787 			lwp_free(l2, false, false);/* releases proc mutex */
    788 			mutex_enter(p->p_lock);
    789 			l->l_refcnt++;
    790 			lwp_drainrefs(l);
    791 		}
    792 		p->p_zomblwp = l;
    793 	}
    794 
    795 	/*
    796 	 * If we find a pending signal for the process and we have been
    797 	 * asked to check for signals, then we loose: arrange to have
    798 	 * all other LWPs in the process check for signals.
    799 	 */
    800 	if ((l->l_flag & LW_PENDSIG) != 0 &&
    801 	    firstsig(&p->p_sigpend.sp_set) != 0) {
    802 		LIST_FOREACH(l2, &p->p_lwps, l_sibling) {
    803 			lwp_lock(l2);
    804 			l2->l_flag |= LW_PENDSIG;
    805 			lwp_unlock(l2);
    806 		}
    807 	}
    808 
    809 	lwp_lock(l);
    810 	l->l_stat = LSZOMB;
    811 	if (l->l_name != NULL)
    812 		strcpy(l->l_name, "(zombie)");
    813 	if (l->l_flag & LW_AFFINITY)
    814 		l->l_flag &= ~LW_AFFINITY;
    815 	lwp_unlock(l);
    816 	p->p_nrlwps--;
    817 	cv_broadcast(&p->p_lwpcv);
    818 	if (l->l_lwpctl != NULL)
    819 		l->l_lwpctl->lc_curcpu = LWPCTL_CPU_EXITED;
    820 	mutex_exit(p->p_lock);
    821 
    822 	/* Safe without lock since LWP is in zombie state */
    823 	if (l->l_affinity) {
    824 		kcpuset_unuse(l->l_affinity, NULL);
    825 		l->l_affinity = NULL;
    826 	}
    827 
    828 	/*
    829 	 * We can no longer block.  At this point, lwp_free() may already
    830 	 * be gunning for us.  On a multi-CPU system, we may be off p_lwps.
    831 	 *
    832 	 * Free MD LWP resources.
    833 	 */
    834 #ifndef __NO_CPU_LWP_FREE
    835 	cpu_lwp_free(l, 0);
    836 #endif
    837 
    838 	if (current) {
    839 		pmap_deactivate(l);
    840 
    841 		/*
    842 		 * Release the kernel lock, and switch away into
    843 		 * oblivion.
    844 		 */
    845 #ifdef notyet
    846 		/* XXXSMP hold in lwp_userret() */
    847 		KERNEL_UNLOCK_LAST(l);
    848 #else
    849 		KERNEL_UNLOCK_ALL(l, NULL);
    850 #endif
    851 		lwp_exit_switchaway(l);
    852 	}
    853 }
    854 
    855 /*
    856  * Free a dead LWP's remaining resources.
    857  *
    858  * XXXLWP limits.
    859  */
    860 void
    861 lwp_free(struct lwp *l, bool recycle, bool last)
    862 {
    863 	struct proc *p = l->l_proc;
    864 	struct rusage *ru;
    865 	ksiginfoq_t kq;
    866 
    867 	KASSERT(l != curlwp);
    868 
    869 	/*
    870 	 * If this was not the last LWP in the process, then adjust
    871 	 * counters and unlock.
    872 	 */
    873 	if (!last) {
    874 		/*
    875 		 * Add the LWP's run time to the process' base value.
    876 		 * This needs to co-incide with coming off p_lwps.
    877 		 */
    878 		bintime_add(&p->p_rtime, &l->l_rtime);
    879 		p->p_pctcpu += l->l_pctcpu;
    880 		ru = &p->p_stats->p_ru;
    881 		ruadd(ru, &l->l_ru);
    882 		ru->ru_nvcsw += (l->l_ncsw - l->l_nivcsw);
    883 		ru->ru_nivcsw += l->l_nivcsw;
    884 		LIST_REMOVE(l, l_sibling);
    885 		p->p_nlwps--;
    886 		p->p_nzlwps--;
    887 		if ((l->l_prflag & LPR_DETACHED) != 0)
    888 			p->p_ndlwps--;
    889 
    890 		/*
    891 		 * Have any LWPs sleeping in lwp_wait() recheck for
    892 		 * deadlock.
    893 		 */
    894 		cv_broadcast(&p->p_lwpcv);
    895 		mutex_exit(p->p_lock);
    896 	}
    897 
    898 #ifdef MULTIPROCESSOR
    899 	/*
    900 	 * In the unlikely event that the LWP is still on the CPU,
    901 	 * then spin until it has switched away.  We need to release
    902 	 * all locks to avoid deadlock against interrupt handlers on
    903 	 * the target CPU.
    904 	 */
    905 	if ((l->l_pflag & LP_RUNNING) != 0 || l->l_cpu->ci_curlwp == l) {
    906 		int count;
    907 		(void)count; /* XXXgcc */
    908 		KERNEL_UNLOCK_ALL(curlwp, &count);
    909 		while ((l->l_pflag & LP_RUNNING) != 0 ||
    910 		    l->l_cpu->ci_curlwp == l)
    911 			SPINLOCK_BACKOFF_HOOK;
    912 		KERNEL_LOCK(count, curlwp);
    913 	}
    914 #endif
    915 
    916 	/*
    917 	 * Destroy the LWP's remaining signal information.
    918 	 */
    919 	ksiginfo_queue_init(&kq);
    920 	sigclear(&l->l_sigpend, NULL, &kq);
    921 	ksiginfo_queue_drain(&kq);
    922 	cv_destroy(&l->l_sigcv);
    923 	mutex_destroy(&l->l_swaplock);
    924 
    925 	/*
    926 	 * Free the LWP's turnstile and the LWP structure itself unless the
    927 	 * caller wants to recycle them.  Also, free the scheduler specific
    928 	 * data.
    929 	 *
    930 	 * We can't return turnstile0 to the pool (it didn't come from it),
    931 	 * so if it comes up just drop it quietly and move on.
    932 	 *
    933 	 * We don't recycle the VM resources at this time.
    934 	 */
    935 	if (l->l_lwpctl != NULL)
    936 		lwp_ctl_free(l);
    937 
    938 	if (!recycle && l->l_ts != &turnstile0)
    939 		pool_cache_put(turnstile_cache, l->l_ts);
    940 	if (l->l_name != NULL)
    941 		kmem_free(l->l_name, MAXCOMLEN);
    942 #ifndef __NO_CPU_LWP_FREE
    943 	cpu_lwp_free2(l);
    944 #endif
    945 	KASSERT((l->l_flag & LW_INMEM) != 0);
    946 	uvm_lwp_exit(l);
    947 	KASSERT(SLIST_EMPTY(&l->l_pi_lenders));
    948 	KASSERT(l->l_inheritedprio == -1);
    949 	if (!recycle)
    950 		pool_cache_put(lwp_cache, l);
    951 }
    952 
    953 /*
    954  * Migrate the LWP to the another CPU.  Unlocks the LWP.
    955  */
    956 void
    957 lwp_migrate(lwp_t *l, struct cpu_info *tci)
    958 {
    959 	struct schedstate_percpu *tspc;
    960 	int lstat = l->l_stat;
    961 
    962 	KASSERT(lwp_locked(l, NULL));
    963 	KASSERT(tci != NULL);
    964 
    965 	/* If LWP is still on the CPU, it must be handled like LSONPROC */
    966 	if ((l->l_pflag & LP_RUNNING) != 0) {
    967 		lstat = LSONPROC;
    968 	}
    969 
    970 	/*
    971 	 * The destination CPU could be changed while previous migration
    972 	 * was not finished.
    973 	 */
    974 	if (l->l_target_cpu != NULL) {
    975 		l->l_target_cpu = tci;
    976 		lwp_unlock(l);
    977 		return;
    978 	}
    979 
    980 	/* Nothing to do if trying to migrate to the same CPU */
    981 	if (l->l_cpu == tci) {
    982 		lwp_unlock(l);
    983 		return;
    984 	}
    985 
    986 	KASSERT(l->l_target_cpu == NULL);
    987 	tspc = &tci->ci_schedstate;
    988 	switch (lstat) {
    989 	case LSRUN:
    990 		if (l->l_flag & LW_INMEM) {
    991 			l->l_target_cpu = tci;
    992 			lwp_unlock(l);
    993 			return;
    994 		}
    995 	case LSIDL:
    996 		l->l_cpu = tci;
    997 		lwp_unlock_to(l, tspc->spc_mutex);
    998 		return;
    999 	case LSSLEEP:
   1000 		l->l_cpu = tci;
   1001 		break;
   1002 	case LSSTOP:
   1003 	case LSSUSPENDED:
   1004 		l->l_cpu = tci;
   1005 		if (l->l_wchan == NULL) {
   1006 			lwp_unlock_to(l, tspc->spc_lwplock);
   1007 			return;
   1008 		}
   1009 		break;
   1010 	case LSONPROC:
   1011 		l->l_target_cpu = tci;
   1012 		spc_lock(l->l_cpu);
   1013 		cpu_need_resched(l->l_cpu, RESCHED_KPREEMPT);
   1014 		spc_unlock(l->l_cpu);
   1015 		break;
   1016 	}
   1017 	lwp_unlock(l);
   1018 }
   1019 
   1020 /*
   1021  * Find the LWP in the process.  Arguments may be zero, in such case,
   1022  * the calling process and first LWP in the list will be used.
   1023  * On success - returns proc locked.
   1024  */
   1025 struct lwp *
   1026 lwp_find2(pid_t pid, lwpid_t lid)
   1027 {
   1028 	proc_t *p;
   1029 	lwp_t *l;
   1030 
   1031 	/* Find the process */
   1032 	p = (pid == 0) ? curlwp->l_proc : p_find(pid, PFIND_UNLOCK_FAIL);
   1033 	if (p == NULL)
   1034 		return NULL;
   1035 	mutex_enter(p->p_lock);
   1036 	if (pid != 0) {
   1037 		/* Case of p_find */
   1038 		mutex_exit(proc_lock);
   1039 	}
   1040 
   1041 	/* Find the thread */
   1042 	l = (lid == 0) ? LIST_FIRST(&p->p_lwps) : lwp_find(p, lid);
   1043 	if (l == NULL) {
   1044 		mutex_exit(p->p_lock);
   1045 	}
   1046 
   1047 	return l;
   1048 }
   1049 
   1050 /*
   1051  * Look up a live LWP within the speicifed process, and return it locked.
   1052  *
   1053  * Must be called with p->p_lock held.
   1054  */
   1055 struct lwp *
   1056 lwp_find(struct proc *p, int id)
   1057 {
   1058 	struct lwp *l;
   1059 
   1060 	KASSERT(mutex_owned(p->p_lock));
   1061 
   1062 	LIST_FOREACH(l, &p->p_lwps, l_sibling) {
   1063 		if (l->l_lid == id)
   1064 			break;
   1065 	}
   1066 
   1067 	/*
   1068 	 * No need to lock - all of these conditions will
   1069 	 * be visible with the process level mutex held.
   1070 	 */
   1071 	if (l != NULL && (l->l_stat == LSIDL || l->l_stat == LSZOMB))
   1072 		l = NULL;
   1073 
   1074 	return l;
   1075 }
   1076 
   1077 /*
   1078  * Update an LWP's cached credentials to mirror the process' master copy.
   1079  *
   1080  * This happens early in the syscall path, on user trap, and on LWP
   1081  * creation.  A long-running LWP can also voluntarily choose to update
   1082  * it's credentials by calling this routine.  This may be called from
   1083  * LWP_CACHE_CREDS(), which checks l->l_cred != p->p_cred beforehand.
   1084  */
   1085 void
   1086 lwp_update_creds(struct lwp *l)
   1087 {
   1088 	kauth_cred_t oc;
   1089 	struct proc *p;
   1090 
   1091 	p = l->l_proc;
   1092 	oc = l->l_cred;
   1093 
   1094 	mutex_enter(p->p_lock);
   1095 	kauth_cred_hold(p->p_cred);
   1096 	l->l_cred = p->p_cred;
   1097 	l->l_prflag &= ~LPR_CRMOD;
   1098 	mutex_exit(p->p_lock);
   1099 	if (oc != NULL)
   1100 		kauth_cred_free(oc);
   1101 }
   1102 
   1103 /*
   1104  * Verify that an LWP is locked, and optionally verify that the lock matches
   1105  * one we specify.
   1106  */
   1107 int
   1108 lwp_locked(struct lwp *l, kmutex_t *mtx)
   1109 {
   1110 	kmutex_t *cur = l->l_mutex;
   1111 
   1112 	return mutex_owned(cur) && (mtx == cur || mtx == NULL);
   1113 }
   1114 
   1115 /*
   1116  * Lock an LWP.
   1117  */
   1118 kmutex_t *
   1119 lwp_lock_retry(struct lwp *l, kmutex_t *old)
   1120 {
   1121 
   1122 	/*
   1123 	 * XXXgcc ignoring kmutex_t * volatile on i386
   1124 	 *
   1125 	 * gcc version 4.1.2 20061021 prerelease (NetBSD nb1 20061021)
   1126 	 */
   1127 #if 1
   1128 	while (l->l_mutex != old) {
   1129 #else
   1130 	for (;;) {
   1131 #endif
   1132 		mutex_spin_exit(old);
   1133 		old = l->l_mutex;
   1134 		mutex_spin_enter(old);
   1135 
   1136 		/*
   1137 		 * mutex_enter() will have posted a read barrier.  Re-test
   1138 		 * l->l_mutex.  If it has changed, we need to try again.
   1139 		 */
   1140 #if 1
   1141 	}
   1142 #else
   1143 	} while (__predict_false(l->l_mutex != old));
   1144 #endif
   1145 
   1146 	return old;
   1147 }
   1148 
   1149 /*
   1150  * Lend a new mutex to an LWP.  The old mutex must be held.
   1151  */
   1152 void
   1153 lwp_setlock(struct lwp *l, kmutex_t *new)
   1154 {
   1155 
   1156 	KASSERT(mutex_owned(l->l_mutex));
   1157 
   1158 	membar_exit();
   1159 	l->l_mutex = new;
   1160 }
   1161 
   1162 /*
   1163  * Lend a new mutex to an LWP, and release the old mutex.  The old mutex
   1164  * must be held.
   1165  */
   1166 void
   1167 lwp_unlock_to(struct lwp *l, kmutex_t *new)
   1168 {
   1169 	kmutex_t *old;
   1170 
   1171 	KASSERT(mutex_owned(l->l_mutex));
   1172 
   1173 	old = l->l_mutex;
   1174 	membar_exit();
   1175 	l->l_mutex = new;
   1176 	mutex_spin_exit(old);
   1177 }
   1178 
   1179 /*
   1180  * Acquire a new mutex, and donate it to an LWP.  The LWP must already be
   1181  * locked.
   1182  */
   1183 void
   1184 lwp_relock(struct lwp *l, kmutex_t *new)
   1185 {
   1186 	kmutex_t *old;
   1187 
   1188 	KASSERT(mutex_owned(l->l_mutex));
   1189 
   1190 	old = l->l_mutex;
   1191 	if (old != new) {
   1192 		mutex_spin_enter(new);
   1193 		l->l_mutex = new;
   1194 		mutex_spin_exit(old);
   1195 	}
   1196 }
   1197 
   1198 int
   1199 lwp_trylock(struct lwp *l)
   1200 {
   1201 	kmutex_t *old;
   1202 
   1203 	for (;;) {
   1204 		if (!mutex_tryenter(old = l->l_mutex))
   1205 			return 0;
   1206 		if (__predict_true(l->l_mutex == old))
   1207 			return 1;
   1208 		mutex_spin_exit(old);
   1209 	}
   1210 }
   1211 
   1212 u_int
   1213 lwp_unsleep(lwp_t *l, bool cleanup)
   1214 {
   1215 
   1216 	KASSERT(mutex_owned(l->l_mutex));
   1217 
   1218 	return (*l->l_syncobj->sobj_unsleep)(l, cleanup);
   1219 }
   1220 
   1221 
   1222 /*
   1223  * Handle exceptions for mi_userret().  Called if a member of LW_USERRET is
   1224  * set.
   1225  */
   1226 void
   1227 lwp_userret(struct lwp *l)
   1228 {
   1229 	struct proc *p;
   1230 	void (*hook)(void);
   1231 	int sig;
   1232 
   1233 	KASSERT(l == curlwp);
   1234 	KASSERT(l->l_stat == LSONPROC);
   1235 	p = l->l_proc;
   1236 
   1237 #ifndef __HAVE_FAST_SOFTINTS
   1238 	/* Run pending soft interrupts. */
   1239 	if (l->l_cpu->ci_data.cpu_softints != 0)
   1240 		softint_overlay();
   1241 #endif
   1242 
   1243 #ifdef KERN_SA
   1244 	/* Generate UNBLOCKED upcall if needed */
   1245 	if (l->l_flag & LW_SA_BLOCKING) {
   1246 		sa_unblock_userret(l);
   1247 		/* NOTREACHED */
   1248 	}
   1249 #endif
   1250 
   1251 	/*
   1252 	 * It should be safe to do this read unlocked on a multiprocessor
   1253 	 * system..
   1254 	 *
   1255 	 * LW_SA_UPCALL will be handled after the while() loop, so don't
   1256 	 * consider it now.
   1257 	 */
   1258 	while ((l->l_flag & (LW_USERRET & ~(LW_SA_UPCALL))) != 0) {
   1259 		/*
   1260 		 * Process pending signals first, unless the process
   1261 		 * is dumping core or exiting, where we will instead
   1262 		 * enter the LW_WSUSPEND case below.
   1263 		 */
   1264 		if ((l->l_flag & (LW_PENDSIG | LW_WCORE | LW_WEXIT)) ==
   1265 		    LW_PENDSIG) {
   1266 			mutex_enter(p->p_lock);
   1267 			while ((sig = issignal(l)) != 0)
   1268 				postsig(sig);
   1269 			mutex_exit(p->p_lock);
   1270 		}
   1271 
   1272 		/*
   1273 		 * Core-dump or suspend pending.
   1274 		 *
   1275 		 * In case of core dump, suspend ourselves, so that the
   1276 		 * kernel stack and therefore the userland registers saved
   1277 		 * in the trapframe are around for coredump() to write them
   1278 		 * out.  We issue a wakeup on p->p_lwpcv so that sigexit()
   1279 		 * will write the core file out once all other LWPs are
   1280 		 * suspended.
   1281 		 */
   1282 		if ((l->l_flag & LW_WSUSPEND) != 0) {
   1283 			mutex_enter(p->p_lock);
   1284 			p->p_nrlwps--;
   1285 			cv_broadcast(&p->p_lwpcv);
   1286 			lwp_lock(l);
   1287 			l->l_stat = LSSUSPENDED;
   1288 			lwp_unlock(l);
   1289 			mutex_exit(p->p_lock);
   1290 			lwp_lock(l);
   1291 			mi_switch(l);
   1292 		}
   1293 
   1294 		/* Process is exiting. */
   1295 		if ((l->l_flag & LW_WEXIT) != 0) {
   1296 			lwp_exit(l);
   1297 			KASSERT(0);
   1298 			/* NOTREACHED */
   1299 		}
   1300 
   1301 		/* Call userret hook; used by Linux emulation. */
   1302 		if ((l->l_flag & LW_WUSERRET) != 0) {
   1303 			lwp_lock(l);
   1304 			l->l_flag &= ~LW_WUSERRET;
   1305 			lwp_unlock(l);
   1306 			hook = p->p_userret;
   1307 			p->p_userret = NULL;
   1308 			(*hook)();
   1309 		}
   1310 	}
   1311 
   1312 #ifdef KERN_SA
   1313 	/*
   1314 	 * Timer events are handled specially.  We only try once to deliver
   1315 	 * pending timer upcalls; if if fails, we can try again on the next
   1316 	 * loop around.  If we need to re-enter lwp_userret(), MD code will
   1317 	 * bounce us back here through the trap path after we return.
   1318 	 */
   1319 	if (p->p_timerpend)
   1320 		timerupcall(l);
   1321 	if (l->l_flag & LW_SA_UPCALL)
   1322 		sa_upcall_userret(l);
   1323 #endif /* KERN_SA */
   1324 }
   1325 
   1326 /*
   1327  * Force an LWP to enter the kernel, to take a trip through lwp_userret().
   1328  */
   1329 void
   1330 lwp_need_userret(struct lwp *l)
   1331 {
   1332 	KASSERT(lwp_locked(l, NULL));
   1333 
   1334 	/*
   1335 	 * Since the tests in lwp_userret() are done unlocked, make sure
   1336 	 * that the condition will be seen before forcing the LWP to enter
   1337 	 * kernel mode.
   1338 	 */
   1339 	membar_producer();
   1340 	cpu_signotify(l);
   1341 }
   1342 
   1343 /*
   1344  * Add one reference to an LWP.  This will prevent the LWP from
   1345  * exiting, thus keep the lwp structure and PCB around to inspect.
   1346  */
   1347 void
   1348 lwp_addref(struct lwp *l)
   1349 {
   1350 
   1351 	KASSERT(mutex_owned(l->l_proc->p_lock));
   1352 	KASSERT(l->l_stat != LSZOMB);
   1353 	KASSERT(l->l_refcnt != 0);
   1354 
   1355 	l->l_refcnt++;
   1356 }
   1357 
   1358 /*
   1359  * Remove one reference to an LWP.  If this is the last reference,
   1360  * then we must finalize the LWP's death.
   1361  */
   1362 void
   1363 lwp_delref(struct lwp *l)
   1364 {
   1365 	struct proc *p = l->l_proc;
   1366 
   1367 	mutex_enter(p->p_lock);
   1368 	KASSERT(l->l_stat != LSZOMB);
   1369 	KASSERT(l->l_refcnt > 0);
   1370 	if (--l->l_refcnt == 0)
   1371 		cv_broadcast(&p->p_lwpcv);
   1372 	mutex_exit(p->p_lock);
   1373 }
   1374 
   1375 /*
   1376  * Drain all references to the current LWP.
   1377  */
   1378 void
   1379 lwp_drainrefs(struct lwp *l)
   1380 {
   1381 	struct proc *p = l->l_proc;
   1382 
   1383 	KASSERT(mutex_owned(p->p_lock));
   1384 	KASSERT(l->l_refcnt != 0);
   1385 
   1386 	l->l_refcnt--;
   1387 	while (l->l_refcnt != 0)
   1388 		cv_wait(&p->p_lwpcv, p->p_lock);
   1389 }
   1390 
   1391 /*
   1392  * lwp_specific_key_create --
   1393  *	Create a key for subsystem lwp-specific data.
   1394  */
   1395 int
   1396 lwp_specific_key_create(specificdata_key_t *keyp, specificdata_dtor_t dtor)
   1397 {
   1398 
   1399 	return (specificdata_key_create(lwp_specificdata_domain, keyp, dtor));
   1400 }
   1401 
   1402 /*
   1403  * lwp_specific_key_delete --
   1404  *	Delete a key for subsystem lwp-specific data.
   1405  */
   1406 void
   1407 lwp_specific_key_delete(specificdata_key_t key)
   1408 {
   1409 
   1410 	specificdata_key_delete(lwp_specificdata_domain, key);
   1411 }
   1412 
   1413 /*
   1414  * lwp_initspecific --
   1415  *	Initialize an LWP's specificdata container.
   1416  */
   1417 void
   1418 lwp_initspecific(struct lwp *l)
   1419 {
   1420 	int error;
   1421 
   1422 	error = specificdata_init(lwp_specificdata_domain, &l->l_specdataref);
   1423 	KASSERT(error == 0);
   1424 }
   1425 
   1426 /*
   1427  * lwp_finispecific --
   1428  *	Finalize an LWP's specificdata container.
   1429  */
   1430 void
   1431 lwp_finispecific(struct lwp *l)
   1432 {
   1433 
   1434 	specificdata_fini(lwp_specificdata_domain, &l->l_specdataref);
   1435 }
   1436 
   1437 /*
   1438  * lwp_getspecific --
   1439  *	Return lwp-specific data corresponding to the specified key.
   1440  *
   1441  *	Note: LWP specific data is NOT INTERLOCKED.  An LWP should access
   1442  *	only its OWN SPECIFIC DATA.  If it is necessary to access another
   1443  *	LWP's specifc data, care must be taken to ensure that doing so
   1444  *	would not cause internal data structure inconsistency (i.e. caller
   1445  *	can guarantee that the target LWP is not inside an lwp_getspecific()
   1446  *	or lwp_setspecific() call).
   1447  */
   1448 void *
   1449 lwp_getspecific(specificdata_key_t key)
   1450 {
   1451 
   1452 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
   1453 						  &curlwp->l_specdataref, key));
   1454 }
   1455 
   1456 void *
   1457 _lwp_getspecific_by_lwp(struct lwp *l, specificdata_key_t key)
   1458 {
   1459 
   1460 	return (specificdata_getspecific_unlocked(lwp_specificdata_domain,
   1461 						  &l->l_specdataref, key));
   1462 }
   1463 
   1464 /*
   1465  * lwp_setspecific --
   1466  *	Set lwp-specific data corresponding to the specified key.
   1467  */
   1468 void
   1469 lwp_setspecific(specificdata_key_t key, void *data)
   1470 {
   1471 
   1472 	specificdata_setspecific(lwp_specificdata_domain,
   1473 				 &curlwp->l_specdataref, key, data);
   1474 }
   1475 
   1476 /*
   1477  * Allocate a new lwpctl structure for a user LWP.
   1478  */
   1479 int
   1480 lwp_ctl_alloc(vaddr_t *uaddr)
   1481 {
   1482 	lcproc_t *lp;
   1483 	u_int bit, i, offset;
   1484 	struct uvm_object *uao;
   1485 	int error;
   1486 	lcpage_t *lcp;
   1487 	proc_t *p;
   1488 	lwp_t *l;
   1489 
   1490 	l = curlwp;
   1491 	p = l->l_proc;
   1492 
   1493 	if (l->l_lcpage != NULL) {
   1494 		lcp = l->l_lcpage;
   1495 		*uaddr = lcp->lcp_uaddr + (vaddr_t)l->l_lwpctl - lcp->lcp_kaddr;
   1496 		return (EINVAL);
   1497 	}
   1498 
   1499 	/* First time around, allocate header structure for the process. */
   1500 	if ((lp = p->p_lwpctl) == NULL) {
   1501 		lp = kmem_alloc(sizeof(*lp), KM_SLEEP);
   1502 		mutex_init(&lp->lp_lock, MUTEX_DEFAULT, IPL_NONE);
   1503 		lp->lp_uao = NULL;
   1504 		TAILQ_INIT(&lp->lp_pages);
   1505 		mutex_enter(p->p_lock);
   1506 		if (p->p_lwpctl == NULL) {
   1507 			p->p_lwpctl = lp;
   1508 			mutex_exit(p->p_lock);
   1509 		} else {
   1510 			mutex_exit(p->p_lock);
   1511 			mutex_destroy(&lp->lp_lock);
   1512 			kmem_free(lp, sizeof(*lp));
   1513 			lp = p->p_lwpctl;
   1514 		}
   1515 	}
   1516 
   1517  	/*
   1518  	 * Set up an anonymous memory region to hold the shared pages.
   1519  	 * Map them into the process' address space.  The user vmspace
   1520  	 * gets the first reference on the UAO.
   1521  	 */
   1522 	mutex_enter(&lp->lp_lock);
   1523 	if (lp->lp_uao == NULL) {
   1524 		lp->lp_uao = uao_create(LWPCTL_UAREA_SZ, 0);
   1525 		lp->lp_cur = 0;
   1526 		lp->lp_max = LWPCTL_UAREA_SZ;
   1527 		lp->lp_uva = p->p_emul->e_vm_default_addr(p,
   1528 		     (vaddr_t)p->p_vmspace->vm_daddr, LWPCTL_UAREA_SZ);
   1529 		error = uvm_map(&p->p_vmspace->vm_map, &lp->lp_uva,
   1530 		    LWPCTL_UAREA_SZ, lp->lp_uao, 0, 0, UVM_MAPFLAG(UVM_PROT_RW,
   1531 		    UVM_PROT_RW, UVM_INH_NONE, UVM_ADV_NORMAL, 0));
   1532 		if (error != 0) {
   1533 			uao_detach(lp->lp_uao);
   1534 			lp->lp_uao = NULL;
   1535 			mutex_exit(&lp->lp_lock);
   1536 			return error;
   1537 		}
   1538 	}
   1539 
   1540 	/* Get a free block and allocate for this LWP. */
   1541 	TAILQ_FOREACH(lcp, &lp->lp_pages, lcp_chain) {
   1542 		if (lcp->lcp_nfree != 0)
   1543 			break;
   1544 	}
   1545 	if (lcp == NULL) {
   1546 		/* Nothing available - try to set up a free page. */
   1547 		if (lp->lp_cur == lp->lp_max) {
   1548 			mutex_exit(&lp->lp_lock);
   1549 			return ENOMEM;
   1550 		}
   1551 		lcp = kmem_alloc(LWPCTL_LCPAGE_SZ, KM_SLEEP);
   1552 		if (lcp == NULL) {
   1553 			mutex_exit(&lp->lp_lock);
   1554 			return ENOMEM;
   1555 		}
   1556 		/*
   1557 		 * Wire the next page down in kernel space.  Since this
   1558 		 * is a new mapping, we must add a reference.
   1559 		 */
   1560 		uao = lp->lp_uao;
   1561 		(*uao->pgops->pgo_reference)(uao);
   1562 		lcp->lcp_kaddr = vm_map_min(kernel_map);
   1563 		error = uvm_map(kernel_map, &lcp->lcp_kaddr, PAGE_SIZE,
   1564 		    uao, lp->lp_cur, PAGE_SIZE,
   1565 		    UVM_MAPFLAG(UVM_PROT_RW, UVM_PROT_RW,
   1566 		    UVM_INH_NONE, UVM_ADV_RANDOM, 0));
   1567 		if (error != 0) {
   1568 			mutex_exit(&lp->lp_lock);
   1569 			kmem_free(lcp, LWPCTL_LCPAGE_SZ);
   1570 			(*uao->pgops->pgo_detach)(uao);
   1571 			return error;
   1572 		}
   1573 		error = uvm_map_pageable(kernel_map, lcp->lcp_kaddr,
   1574 		    lcp->lcp_kaddr + PAGE_SIZE, FALSE, 0);
   1575 		if (error != 0) {
   1576 			mutex_exit(&lp->lp_lock);
   1577 			uvm_unmap(kernel_map, lcp->lcp_kaddr,
   1578 			    lcp->lcp_kaddr + PAGE_SIZE);
   1579 			kmem_free(lcp, LWPCTL_LCPAGE_SZ);
   1580 			return error;
   1581 		}
   1582 		/* Prepare the page descriptor and link into the list. */
   1583 		lcp->lcp_uaddr = lp->lp_uva + lp->lp_cur;
   1584 		lp->lp_cur += PAGE_SIZE;
   1585 		lcp->lcp_nfree = LWPCTL_PER_PAGE;
   1586 		lcp->lcp_rotor = 0;
   1587 		memset(lcp->lcp_bitmap, 0xff, LWPCTL_BITMAP_SZ);
   1588 		TAILQ_INSERT_HEAD(&lp->lp_pages, lcp, lcp_chain);
   1589 	}
   1590 	for (i = lcp->lcp_rotor; lcp->lcp_bitmap[i] == 0;) {
   1591 		if (++i >= LWPCTL_BITMAP_ENTRIES)
   1592 			i = 0;
   1593 	}
   1594 	bit = ffs(lcp->lcp_bitmap[i]) - 1;
   1595 	lcp->lcp_bitmap[i] ^= (1 << bit);
   1596 	lcp->lcp_rotor = i;
   1597 	lcp->lcp_nfree--;
   1598 	l->l_lcpage = lcp;
   1599 	offset = (i << 5) + bit;
   1600 	l->l_lwpctl = (lwpctl_t *)lcp->lcp_kaddr + offset;
   1601 	*uaddr = lcp->lcp_uaddr + offset * sizeof(lwpctl_t);
   1602 	mutex_exit(&lp->lp_lock);
   1603 
   1604 	KPREEMPT_DISABLE(l);
   1605 	l->l_lwpctl->lc_curcpu = (int)curcpu()->ci_data.cpu_index;
   1606 	KPREEMPT_ENABLE(l);
   1607 
   1608 	return 0;
   1609 }
   1610 
   1611 /*
   1612  * Free an lwpctl structure back to the per-process list.
   1613  */
   1614 void
   1615 lwp_ctl_free(lwp_t *l)
   1616 {
   1617 	lcproc_t *lp;
   1618 	lcpage_t *lcp;
   1619 	u_int map, offset;
   1620 
   1621 	lp = l->l_proc->p_lwpctl;
   1622 	KASSERT(lp != NULL);
   1623 
   1624 	lcp = l->l_lcpage;
   1625 	offset = (u_int)((lwpctl_t *)l->l_lwpctl - (lwpctl_t *)lcp->lcp_kaddr);
   1626 	KASSERT(offset < LWPCTL_PER_PAGE);
   1627 
   1628 	mutex_enter(&lp->lp_lock);
   1629 	lcp->lcp_nfree++;
   1630 	map = offset >> 5;
   1631 	lcp->lcp_bitmap[map] |= (1 << (offset & 31));
   1632 	if (lcp->lcp_bitmap[lcp->lcp_rotor] == 0)
   1633 		lcp->lcp_rotor = map;
   1634 	if (TAILQ_FIRST(&lp->lp_pages)->lcp_nfree == 0) {
   1635 		TAILQ_REMOVE(&lp->lp_pages, lcp, lcp_chain);
   1636 		TAILQ_INSERT_HEAD(&lp->lp_pages, lcp, lcp_chain);
   1637 	}
   1638 	mutex_exit(&lp->lp_lock);
   1639 }
   1640 
   1641 /*
   1642  * Process is exiting; tear down lwpctl state.  This can only be safely
   1643  * called by the last LWP in the process.
   1644  */
   1645 void
   1646 lwp_ctl_exit(void)
   1647 {
   1648 	lcpage_t *lcp, *next;
   1649 	lcproc_t *lp;
   1650 	proc_t *p;
   1651 	lwp_t *l;
   1652 
   1653 	l = curlwp;
   1654 	l->l_lwpctl = NULL;
   1655 	l->l_lcpage = NULL;
   1656 	p = l->l_proc;
   1657 	lp = p->p_lwpctl;
   1658 
   1659 	KASSERT(lp != NULL);
   1660 	KASSERT(p->p_nlwps == 1);
   1661 
   1662 	for (lcp = TAILQ_FIRST(&lp->lp_pages); lcp != NULL; lcp = next) {
   1663 		next = TAILQ_NEXT(lcp, lcp_chain);
   1664 		uvm_unmap(kernel_map, lcp->lcp_kaddr,
   1665 		    lcp->lcp_kaddr + PAGE_SIZE);
   1666 		kmem_free(lcp, LWPCTL_LCPAGE_SZ);
   1667 	}
   1668 
   1669 	if (lp->lp_uao != NULL) {
   1670 		uvm_unmap(&p->p_vmspace->vm_map, lp->lp_uva,
   1671 		    lp->lp_uva + LWPCTL_UAREA_SZ);
   1672 	}
   1673 
   1674 	mutex_destroy(&lp->lp_lock);
   1675 	kmem_free(lp, sizeof(*lp));
   1676 	p->p_lwpctl = NULL;
   1677 }
   1678 
   1679 #if defined(DDB)
   1680 void
   1681 lwp_whatis(uintptr_t addr, void (*pr)(const char *, ...))
   1682 {
   1683 	lwp_t *l;
   1684 
   1685 	LIST_FOREACH(l, &alllwp, l_list) {
   1686 		uintptr_t stack = (uintptr_t)KSTACK_LOWEST_ADDR(l);
   1687 
   1688 		if (addr < stack || stack + KSTACK_SIZE <= addr) {
   1689 			continue;
   1690 		}
   1691 		(*pr)("%p is %p+%zu, LWP %p's stack\n",
   1692 		    (void *)addr, (void *)stack,
   1693 		    (size_t)(addr - stack), l);
   1694 	}
   1695 }
   1696 #endif /* defined(DDB) */
   1697