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