xref: /onnv-gate/usr/src/uts/common/os/lwp.c (revision 4503:8cf697a3acff)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
52712Snn35248  * Common Development and Distribution License (the "License").
62712Snn35248  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21390Sraf 
220Sstevel@tonic-gate /*
233551Sjohansen  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate #include <sys/param.h>
300Sstevel@tonic-gate #include <sys/types.h>
310Sstevel@tonic-gate #include <sys/sysmacros.h>
320Sstevel@tonic-gate #include <sys/systm.h>
330Sstevel@tonic-gate #include <sys/thread.h>
340Sstevel@tonic-gate #include <sys/proc.h>
350Sstevel@tonic-gate #include <sys/task.h>
360Sstevel@tonic-gate #include <sys/project.h>
370Sstevel@tonic-gate #include <sys/signal.h>
380Sstevel@tonic-gate #include <sys/errno.h>
390Sstevel@tonic-gate #include <sys/vmparam.h>
400Sstevel@tonic-gate #include <sys/stack.h>
410Sstevel@tonic-gate #include <sys/procfs.h>
420Sstevel@tonic-gate #include <sys/prsystm.h>
430Sstevel@tonic-gate #include <sys/cpuvar.h>
440Sstevel@tonic-gate #include <sys/kmem.h>
450Sstevel@tonic-gate #include <sys/vtrace.h>
460Sstevel@tonic-gate #include <sys/door.h>
470Sstevel@tonic-gate #include <vm/seg_kp.h>
480Sstevel@tonic-gate #include <sys/debug.h>
490Sstevel@tonic-gate #include <sys/tnf.h>
500Sstevel@tonic-gate #include <sys/schedctl.h>
510Sstevel@tonic-gate #include <sys/poll.h>
520Sstevel@tonic-gate #include <sys/copyops.h>
530Sstevel@tonic-gate #include <sys/lwp_upimutex_impl.h>
540Sstevel@tonic-gate #include <sys/cpupart.h>
550Sstevel@tonic-gate #include <sys/lgrp.h>
560Sstevel@tonic-gate #include <sys/rctl.h>
570Sstevel@tonic-gate #include <sys/contract_impl.h>
580Sstevel@tonic-gate #include <sys/cpc_impl.h>
590Sstevel@tonic-gate #include <sys/sdt.h>
600Sstevel@tonic-gate #include <sys/cmn_err.h>
612712Snn35248 #include <sys/brand.h>
620Sstevel@tonic-gate 
630Sstevel@tonic-gate void *segkp_lwp;		/* cookie for pool of segkp resources */
640Sstevel@tonic-gate 
650Sstevel@tonic-gate /*
660Sstevel@tonic-gate  * Create a thread that appears to be stopped at sys_rtt.
670Sstevel@tonic-gate  */
680Sstevel@tonic-gate klwp_t *
690Sstevel@tonic-gate lwp_create(void (*proc)(), caddr_t arg, size_t len, proc_t *p,
700Sstevel@tonic-gate     int state, int pri, const k_sigset_t *smask, int cid, id_t lwpid)
710Sstevel@tonic-gate {
720Sstevel@tonic-gate 	klwp_t *lwp = NULL;
730Sstevel@tonic-gate 	kthread_t *t;
740Sstevel@tonic-gate 	kthread_t *tx;
750Sstevel@tonic-gate 	cpupart_t *oldpart = NULL;
760Sstevel@tonic-gate 	size_t	stksize;
770Sstevel@tonic-gate 	caddr_t lwpdata = NULL;
780Sstevel@tonic-gate 	processorid_t	binding;
790Sstevel@tonic-gate 	int err = 0;
800Sstevel@tonic-gate 	kproject_t *oldkpj, *newkpj;
810Sstevel@tonic-gate 	void *bufp = NULL;
820Sstevel@tonic-gate 	klwp_t *curlwp = ttolwp(curthread);
830Sstevel@tonic-gate 	lwpent_t *lep;
840Sstevel@tonic-gate 	lwpdir_t *old_dir = NULL;
850Sstevel@tonic-gate 	uint_t old_dirsz = 0;
860Sstevel@tonic-gate 	lwpdir_t **old_hash = NULL;
870Sstevel@tonic-gate 	uint_t old_hashsz = 0;
880Sstevel@tonic-gate 	int i;
890Sstevel@tonic-gate 	int rctlfail = 0;
902712Snn35248 	boolean_t branded = 0;
910Sstevel@tonic-gate 
920Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
930Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
940Sstevel@tonic-gate 	/*
950Sstevel@tonic-gate 	 * don't enforce rctl limits on system processes
960Sstevel@tonic-gate 	 */
970Sstevel@tonic-gate 	if (cid != syscid) {
980Sstevel@tonic-gate 		if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl)
990Sstevel@tonic-gate 			if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p,
1000Sstevel@tonic-gate 			    1, 0) & RCT_DENY)
1010Sstevel@tonic-gate 				rctlfail = 1;
1020Sstevel@tonic-gate 		if (p->p_task->tk_proj->kpj_nlwps >=
1030Sstevel@tonic-gate 		    p->p_task->tk_proj->kpj_nlwps_ctl)
1040Sstevel@tonic-gate 			if (rctl_test(rc_project_nlwps,
1050Sstevel@tonic-gate 			    p->p_task->tk_proj->kpj_rctls, p, 1, 0)
1060Sstevel@tonic-gate 			    & RCT_DENY)
1070Sstevel@tonic-gate 				rctlfail = 1;
1080Sstevel@tonic-gate 		if (p->p_zone->zone_nlwps >= p->p_zone->zone_nlwps_ctl)
1090Sstevel@tonic-gate 			if (rctl_test(rc_zone_nlwps, p->p_zone->zone_rctls, p,
1100Sstevel@tonic-gate 			    1, 0) & RCT_DENY)
1110Sstevel@tonic-gate 				rctlfail = 1;
1120Sstevel@tonic-gate 	}
1130Sstevel@tonic-gate 	if (rctlfail) {
1140Sstevel@tonic-gate 		mutex_exit(&p->p_zone->zone_nlwps_lock);
1150Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1160Sstevel@tonic-gate 		return (NULL);
1170Sstevel@tonic-gate 	}
1180Sstevel@tonic-gate 	p->p_task->tk_nlwps++;
1190Sstevel@tonic-gate 	p->p_task->tk_proj->kpj_nlwps++;
1200Sstevel@tonic-gate 	p->p_zone->zone_nlwps++;
1210Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
1220Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1230Sstevel@tonic-gate 
1240Sstevel@tonic-gate 	if (curlwp == NULL || (stksize = curlwp->lwp_childstksz) == 0)
1250Sstevel@tonic-gate 		stksize = lwp_default_stksize;
1260Sstevel@tonic-gate 
1270Sstevel@tonic-gate 	/*
1280Sstevel@tonic-gate 	 * Try to reclaim a <lwp,stack> from 'deathrow'
1290Sstevel@tonic-gate 	 */
1300Sstevel@tonic-gate 	if (stksize == lwp_default_stksize) {
1310Sstevel@tonic-gate 		if (lwp_reapcnt > 0) {
1320Sstevel@tonic-gate 			mutex_enter(&reaplock);
1330Sstevel@tonic-gate 			if ((t = lwp_deathrow) != NULL) {
1340Sstevel@tonic-gate 				ASSERT(t->t_swap);
1350Sstevel@tonic-gate 				lwp_deathrow = t->t_forw;
1360Sstevel@tonic-gate 				lwp_reapcnt--;
1370Sstevel@tonic-gate 				lwpdata = t->t_swap;
1380Sstevel@tonic-gate 				lwp = t->t_lwp;
1390Sstevel@tonic-gate 			}
1400Sstevel@tonic-gate 			mutex_exit(&reaplock);
1410Sstevel@tonic-gate 			if (t) {
1420Sstevel@tonic-gate 				t->t_swap = NULL;
1430Sstevel@tonic-gate 				lwp_stk_fini(t->t_lwp);
1440Sstevel@tonic-gate 				t->t_lwp = NULL;
1450Sstevel@tonic-gate 				t->t_forw = NULL;
1460Sstevel@tonic-gate 				thread_free(t);
1470Sstevel@tonic-gate 			}
1480Sstevel@tonic-gate 		}
1490Sstevel@tonic-gate 		if (lwpdata == NULL &&
1500Sstevel@tonic-gate 		    (lwpdata = (caddr_t)segkp_cache_get(segkp_lwp)) == NULL) {
1510Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1520Sstevel@tonic-gate 			mutex_enter(&p->p_zone->zone_nlwps_lock);
1530Sstevel@tonic-gate 			p->p_task->tk_nlwps--;
1540Sstevel@tonic-gate 			p->p_task->tk_proj->kpj_nlwps--;
1550Sstevel@tonic-gate 			p->p_zone->zone_nlwps--;
1560Sstevel@tonic-gate 			mutex_exit(&p->p_zone->zone_nlwps_lock);
1570Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1580Sstevel@tonic-gate 			return (NULL);
1590Sstevel@tonic-gate 		}
1600Sstevel@tonic-gate 	} else {
1610Sstevel@tonic-gate 		stksize = roundup(stksize, PAGESIZE);
1620Sstevel@tonic-gate 		if ((lwpdata = (caddr_t)segkp_get(segkp, stksize,
1630Sstevel@tonic-gate 		    (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED))) == NULL) {
1640Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
1650Sstevel@tonic-gate 			mutex_enter(&p->p_zone->zone_nlwps_lock);
1660Sstevel@tonic-gate 			p->p_task->tk_nlwps--;
1670Sstevel@tonic-gate 			p->p_task->tk_proj->kpj_nlwps--;
1680Sstevel@tonic-gate 			p->p_zone->zone_nlwps--;
1690Sstevel@tonic-gate 			mutex_exit(&p->p_zone->zone_nlwps_lock);
1700Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1710Sstevel@tonic-gate 			return (NULL);
1720Sstevel@tonic-gate 		}
1730Sstevel@tonic-gate 	}
1740Sstevel@tonic-gate 
1750Sstevel@tonic-gate 	/*
1760Sstevel@tonic-gate 	 * Create a thread, initializing the stack pointer
1770Sstevel@tonic-gate 	 */
1780Sstevel@tonic-gate 	t = thread_create(lwpdata, stksize, NULL, NULL, 0, p, TS_STOPPED, pri);
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	t->t_swap = lwpdata;	/* Start of page-able data */
1810Sstevel@tonic-gate 	if (lwp == NULL)
1820Sstevel@tonic-gate 		lwp = kmem_cache_alloc(lwp_cache, KM_SLEEP);
1830Sstevel@tonic-gate 	bzero(lwp, sizeof (*lwp));
1840Sstevel@tonic-gate 	t->t_lwp = lwp;
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	t->t_hold = *smask;
1870Sstevel@tonic-gate 	lwp->lwp_thread = t;
1880Sstevel@tonic-gate 	lwp->lwp_procp = p;
1890Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
1900Sstevel@tonic-gate 	if (curlwp != NULL && curlwp->lwp_childstksz != 0)
1910Sstevel@tonic-gate 		lwp->lwp_childstksz = curlwp->lwp_childstksz;
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate 	t->t_stk = lwp_stk_init(lwp, t->t_stk);
1940Sstevel@tonic-gate 	thread_load(t, proc, arg, len);
1950Sstevel@tonic-gate 
1960Sstevel@tonic-gate 	/*
1970Sstevel@tonic-gate 	 * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	if (timerisset(&p->p_rprof_timer.it_value))
2000Sstevel@tonic-gate 		t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP);
2010Sstevel@tonic-gate 
2020Sstevel@tonic-gate 	if (cid != NOCLASS)
2030Sstevel@tonic-gate 		(void) CL_ALLOC(&bufp, cid, KM_SLEEP);
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate 	/*
2060Sstevel@tonic-gate 	 * Allocate an lwp directory entry for the new lwp.
2070Sstevel@tonic-gate 	 */
2080Sstevel@tonic-gate 	lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
2090Sstevel@tonic-gate 
2100Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2110Sstevel@tonic-gate grow:
2120Sstevel@tonic-gate 	/*
2130Sstevel@tonic-gate 	 * Grow the lwp (thread) directory and lwpid hash table if necessary.
2140Sstevel@tonic-gate 	 * A note on the growth algorithm:
2150Sstevel@tonic-gate 	 *	The new lwp directory size is computed as:
2160Sstevel@tonic-gate 	 *		new = 2 * old + 2
2170Sstevel@tonic-gate 	 *	Starting with an initial size of 2 (see exec_common()),
2180Sstevel@tonic-gate 	 *	this yields numbers that are a power of two minus 2:
2190Sstevel@tonic-gate 	 *		2, 6, 14, 30, 62, 126, 254, 510, 1022, ...
2200Sstevel@tonic-gate 	 *	The size of the lwpid hash table must be a power of two
2210Sstevel@tonic-gate 	 *	and must be commensurate in size with the lwp directory
2220Sstevel@tonic-gate 	 *	so that hash bucket chains remain short.  Therefore,
2230Sstevel@tonic-gate 	 *	the lwpid hash table size is computed as:
2240Sstevel@tonic-gate 	 *		hashsz = (dirsz + 2) / 2
2250Sstevel@tonic-gate 	 *	which leads to these hash table sizes corresponding to
2260Sstevel@tonic-gate 	 *	the above directory sizes:
2270Sstevel@tonic-gate 	 *		2, 4, 8, 16, 32, 64, 128, 256, 512, ...
2280Sstevel@tonic-gate 	 */
2290Sstevel@tonic-gate 	while (p->p_lwpfree == NULL) {
2300Sstevel@tonic-gate 		uint_t dirsz = p->p_lwpdir_sz;
2310Sstevel@tonic-gate 		uint_t new_dirsz;
2320Sstevel@tonic-gate 		uint_t new_hashsz;
2330Sstevel@tonic-gate 		lwpdir_t *new_dir;
2340Sstevel@tonic-gate 		lwpdir_t *ldp;
2350Sstevel@tonic-gate 		lwpdir_t **new_hash;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 		if (old_dir != NULL) {
2400Sstevel@tonic-gate 			kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
2410Sstevel@tonic-gate 			kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
2420Sstevel@tonic-gate 			old_dir = NULL;
2430Sstevel@tonic-gate 			old_dirsz = 0;
2440Sstevel@tonic-gate 			old_hash = NULL;
2450Sstevel@tonic-gate 			old_hashsz = 0;
2460Sstevel@tonic-gate 		}
2470Sstevel@tonic-gate 		new_dirsz = 2 * dirsz + 2;
2480Sstevel@tonic-gate 		new_dir = kmem_zalloc(new_dirsz * sizeof (lwpdir_t), KM_SLEEP);
2490Sstevel@tonic-gate 		for (ldp = new_dir, i = 1; i < new_dirsz; i++, ldp++)
2500Sstevel@tonic-gate 			ldp->ld_next = ldp + 1;
2510Sstevel@tonic-gate 		new_hashsz = (new_dirsz + 2) / 2;
2520Sstevel@tonic-gate 		new_hash = kmem_zalloc(new_hashsz * sizeof (lwpdir_t *),
2530Sstevel@tonic-gate 			KM_SLEEP);
2540Sstevel@tonic-gate 
2550Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
2560Sstevel@tonic-gate 		if (p == curproc)
2570Sstevel@tonic-gate 			prbarrier(p);
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 		if (dirsz != p->p_lwpdir_sz || p->p_lwpfree != NULL) {
2600Sstevel@tonic-gate 			/*
2610Sstevel@tonic-gate 			 * Someone else beat us to it or some lwp exited.
2620Sstevel@tonic-gate 			 * Set up to free our memory and take a lap.
2630Sstevel@tonic-gate 			 */
2640Sstevel@tonic-gate 			old_dir = new_dir;
2650Sstevel@tonic-gate 			old_dirsz = new_dirsz;
2660Sstevel@tonic-gate 			old_hash = new_hash;
2670Sstevel@tonic-gate 			old_hashsz = new_hashsz;
2680Sstevel@tonic-gate 		} else {
2690Sstevel@tonic-gate 			old_dir = p->p_lwpdir;
2700Sstevel@tonic-gate 			old_dirsz = p->p_lwpdir_sz;
2710Sstevel@tonic-gate 			old_hash = p->p_tidhash;
2720Sstevel@tonic-gate 			old_hashsz = p->p_tidhash_sz;
2730Sstevel@tonic-gate 			p->p_lwpdir = new_dir;
2740Sstevel@tonic-gate 			p->p_lwpfree = new_dir;
2750Sstevel@tonic-gate 			p->p_lwpdir_sz = new_dirsz;
2760Sstevel@tonic-gate 			p->p_tidhash = new_hash;
2770Sstevel@tonic-gate 			p->p_tidhash_sz = new_hashsz;
2780Sstevel@tonic-gate 			/*
2790Sstevel@tonic-gate 			 * We simply hash in all of the old directory entries.
2800Sstevel@tonic-gate 			 * This works because the old directory has no empty
2810Sstevel@tonic-gate 			 * slots and the new hash table starts out empty.
2820Sstevel@tonic-gate 			 * This reproduces the original directory ordering
2830Sstevel@tonic-gate 			 * (required for /proc directory semantics).
2840Sstevel@tonic-gate 			 */
2850Sstevel@tonic-gate 			for (ldp = old_dir, i = 0; i < dirsz; i++, ldp++)
2860Sstevel@tonic-gate 				lwp_hash_in(p, ldp->ld_entry);
2870Sstevel@tonic-gate 			/*
2880Sstevel@tonic-gate 			 * Defer freeing memory until we drop p->p_lock,
2890Sstevel@tonic-gate 			 */
2900Sstevel@tonic-gate 		}
2910Sstevel@tonic-gate 	}
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	/*
2940Sstevel@tonic-gate 	 * Block the process against /proc while we manipulate p->p_tlist,
2950Sstevel@tonic-gate 	 * unless lwp_create() was called by /proc for the PCAGENT operation.
2960Sstevel@tonic-gate 	 * We want to do this early enough so that we don't drop p->p_lock
2970Sstevel@tonic-gate 	 * until the thread is put on the p->p_tlist.
2980Sstevel@tonic-gate 	 */
2990Sstevel@tonic-gate 	if (p == curproc) {
3000Sstevel@tonic-gate 		prbarrier(p);
3010Sstevel@tonic-gate 		/*
3020Sstevel@tonic-gate 		 * If the current lwp has been requested to stop, do so now.
3030Sstevel@tonic-gate 		 * Otherwise we have a race condition between /proc attempting
3040Sstevel@tonic-gate 		 * to stop the process and this thread creating a new lwp
3050Sstevel@tonic-gate 		 * that was not seen when the /proc PCSTOP request was issued.
3060Sstevel@tonic-gate 		 * We rely on stop() to call prbarrier(p) before returning.
3070Sstevel@tonic-gate 		 */
3080Sstevel@tonic-gate 		while ((curthread->t_proc_flag & TP_PRSTOP) &&
3090Sstevel@tonic-gate 		    !ttolwp(curthread)->lwp_nostop)
3100Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		/*
3130Sstevel@tonic-gate 		 * If process is exiting, there could be a race between
3140Sstevel@tonic-gate 		 * the agent lwp creation and the new lwp currently being
3150Sstevel@tonic-gate 		 * created. So to prevent this race lwp creation is failed
3160Sstevel@tonic-gate 		 * if the process is exiting.
3170Sstevel@tonic-gate 		 */
3180Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
3190Sstevel@tonic-gate 			err = 1;
3200Sstevel@tonic-gate 			goto error;
3210Sstevel@tonic-gate 		}
3220Sstevel@tonic-gate 
3230Sstevel@tonic-gate 		/*
3240Sstevel@tonic-gate 		 * Since we might have dropped p->p_lock, the
3250Sstevel@tonic-gate 		 * lwp directory free list might have changed.
3260Sstevel@tonic-gate 		 */
3270Sstevel@tonic-gate 		if (p->p_lwpfree == NULL)
3280Sstevel@tonic-gate 			goto grow;
3290Sstevel@tonic-gate 	}
3300Sstevel@tonic-gate 
3310Sstevel@tonic-gate 	kpreempt_disable();	/* can't grab cpu_lock here */
3320Sstevel@tonic-gate 
3330Sstevel@tonic-gate 	/*
3340Sstevel@tonic-gate 	 * Inherit processor and processor set bindings from curthread,
3350Sstevel@tonic-gate 	 * unless we're creating a new kernel process, in which case
3360Sstevel@tonic-gate 	 * clear all bindings.
3370Sstevel@tonic-gate 	 */
3380Sstevel@tonic-gate 	if (cid == syscid) {
3390Sstevel@tonic-gate 		t->t_bind_cpu = binding = PBIND_NONE;
3400Sstevel@tonic-gate 		t->t_cpupart = oldpart = &cp_default;
3410Sstevel@tonic-gate 		t->t_bind_pset = PS_NONE;
3420Sstevel@tonic-gate 	} else {
3430Sstevel@tonic-gate 		binding = curthread->t_bind_cpu;
3440Sstevel@tonic-gate 		t->t_bind_cpu = binding;
3450Sstevel@tonic-gate 		oldpart = t->t_cpupart;
3460Sstevel@tonic-gate 		t->t_cpupart = curthread->t_cpupart;
3470Sstevel@tonic-gate 		t->t_bind_pset = curthread->t_bind_pset;
3480Sstevel@tonic-gate 	}
3490Sstevel@tonic-gate 
3500Sstevel@tonic-gate 	/*
3510Sstevel@tonic-gate 	 * thread_create() initializes this thread's home lgroup to the root.
3520Sstevel@tonic-gate 	 * Choose a more suitable lgroup, since this thread is associated
3530Sstevel@tonic-gate 	 * with an lwp.
3540Sstevel@tonic-gate 	 */
3550Sstevel@tonic-gate 	ASSERT(oldpart != NULL);
3560Sstevel@tonic-gate 	if (binding != PBIND_NONE && t->t_affinitycnt == 0) {
3570Sstevel@tonic-gate 		t->t_bound_cpu = cpu[binding];
3580Sstevel@tonic-gate 		if (t->t_lpl != t->t_bound_cpu->cpu_lpl)
3590Sstevel@tonic-gate 			lgrp_move_thread(t, t->t_bound_cpu->cpu_lpl, 1);
3600Sstevel@tonic-gate 	} else {
3610Sstevel@tonic-gate 		lgrp_move_thread(t, lgrp_choose(t, t->t_cpupart), 1);
3620Sstevel@tonic-gate 	}
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 	kpreempt_enable();
3650Sstevel@tonic-gate 
3660Sstevel@tonic-gate 	/*
3670Sstevel@tonic-gate 	 * make sure lpl points to our own partition
3680Sstevel@tonic-gate 	 */
3690Sstevel@tonic-gate 	ASSERT(t->t_lpl >= t->t_cpupart->cp_lgrploads);
3700Sstevel@tonic-gate 	ASSERT(t->t_lpl < t->t_cpupart->cp_lgrploads +
3710Sstevel@tonic-gate 	    t->t_cpupart->cp_nlgrploads);
3720Sstevel@tonic-gate 
3730Sstevel@tonic-gate 	/*
3740Sstevel@tonic-gate 	 * If we're creating a new process, then inherit the project from our
3750Sstevel@tonic-gate 	 * parent. If we're only creating an additional lwp then use the
3760Sstevel@tonic-gate 	 * project pointer of the target process.
3770Sstevel@tonic-gate 	 */
3780Sstevel@tonic-gate 	if (p->p_task == NULL)
3790Sstevel@tonic-gate 		newkpj = ttoproj(curthread);
3800Sstevel@tonic-gate 	else
3810Sstevel@tonic-gate 		newkpj = p->p_task->tk_proj;
3820Sstevel@tonic-gate 
3830Sstevel@tonic-gate 	/*
3840Sstevel@tonic-gate 	 * It is safe to point the thread to the new project without holding it
3850Sstevel@tonic-gate 	 * since we're holding the target process' p_lock here and therefore
3860Sstevel@tonic-gate 	 * we're guaranteed that it will not move to another project.
3870Sstevel@tonic-gate 	 */
3880Sstevel@tonic-gate 	oldkpj = ttoproj(t);
3890Sstevel@tonic-gate 	if (newkpj != oldkpj) {
3900Sstevel@tonic-gate 		t->t_proj = newkpj;
3910Sstevel@tonic-gate 		(void) project_hold(newkpj);
3920Sstevel@tonic-gate 		project_rele(oldkpj);
3930Sstevel@tonic-gate 	}
3940Sstevel@tonic-gate 
3950Sstevel@tonic-gate 	if (cid != NOCLASS) {
3960Sstevel@tonic-gate 		/*
3970Sstevel@tonic-gate 		 * If the lwp is being created in the current process
3980Sstevel@tonic-gate 		 * and matches the current thread's scheduling class,
3990Sstevel@tonic-gate 		 * we should propagate the current thread's scheduling
4000Sstevel@tonic-gate 		 * parameters by calling CL_FORK.  Otherwise just use
4010Sstevel@tonic-gate 		 * the defaults by calling CL_ENTERCLASS.
4020Sstevel@tonic-gate 		 */
4030Sstevel@tonic-gate 		if (p != curproc || curthread->t_cid != cid) {
4040Sstevel@tonic-gate 			err = CL_ENTERCLASS(t, cid, NULL, NULL, bufp);
4050Sstevel@tonic-gate 			t->t_pri = pri;	/* CL_ENTERCLASS may have changed it */
4060Sstevel@tonic-gate 		} else {
4070Sstevel@tonic-gate 			t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
4080Sstevel@tonic-gate 			err = CL_FORK(curthread, t, bufp);
4090Sstevel@tonic-gate 			t->t_cid = cid;
4100Sstevel@tonic-gate 		}
4110Sstevel@tonic-gate 		if (err)
4120Sstevel@tonic-gate 			goto error;
4130Sstevel@tonic-gate 		else
4140Sstevel@tonic-gate 			bufp = NULL;
4150Sstevel@tonic-gate 	}
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * If we were given an lwpid then use it, else allocate one.
4190Sstevel@tonic-gate 	 */
4200Sstevel@tonic-gate 	if (lwpid != 0)
4210Sstevel@tonic-gate 		t->t_tid = lwpid;
4220Sstevel@tonic-gate 	else {
4230Sstevel@tonic-gate 		/*
4240Sstevel@tonic-gate 		 * lwp/thread id 0 is never valid; reserved for special checks.
4250Sstevel@tonic-gate 		 * lwp/thread id 1 is reserved for the main thread.
4260Sstevel@tonic-gate 		 * Start again at 2 when INT_MAX has been reached
4270Sstevel@tonic-gate 		 * (id_t is a signed 32-bit integer).
4280Sstevel@tonic-gate 		 */
4290Sstevel@tonic-gate 		id_t prev_id = p->p_lwpid;	/* last allocated tid */
4300Sstevel@tonic-gate 
4310Sstevel@tonic-gate 		do {			/* avoid lwpid duplication */
4320Sstevel@tonic-gate 			if (p->p_lwpid == INT_MAX) {
4330Sstevel@tonic-gate 				p->p_flag |= SLWPWRAP;
4340Sstevel@tonic-gate 				p->p_lwpid = 1;
4350Sstevel@tonic-gate 			}
4360Sstevel@tonic-gate 			if ((t->t_tid = ++p->p_lwpid) == prev_id) {
4370Sstevel@tonic-gate 				/*
4380Sstevel@tonic-gate 				 * All lwpids are allocated; fail the request.
4390Sstevel@tonic-gate 				 */
4400Sstevel@tonic-gate 				err = 1;
4410Sstevel@tonic-gate 				goto error;
4420Sstevel@tonic-gate 			}
4430Sstevel@tonic-gate 			/*
4440Sstevel@tonic-gate 			 * We only need to worry about colliding with an id
4450Sstevel@tonic-gate 			 * that's already in use if this process has
4460Sstevel@tonic-gate 			 * cycled through all available lwp ids.
4470Sstevel@tonic-gate 			 */
4480Sstevel@tonic-gate 			if ((p->p_flag & SLWPWRAP) == 0)
4490Sstevel@tonic-gate 				break;
4500Sstevel@tonic-gate 		} while (lwp_hash_lookup(p, t->t_tid) != NULL);
4510Sstevel@tonic-gate 	}
4522712Snn35248 
4532712Snn35248 	/*
4542712Snn35248 	 * If this is a branded process, let the brand do any necessary lwp
4552712Snn35248 	 * initialization.
4562712Snn35248 	 */
4572712Snn35248 	if (PROC_IS_BRANDED(p)) {
4582712Snn35248 		if (BROP(p)->b_initlwp(lwp)) {
4592712Snn35248 			err = 1;
4602712Snn35248 			goto error;
4612712Snn35248 		}
4622712Snn35248 		branded = 1;
4632712Snn35248 	}
4642712Snn35248 
4654426Saguzovsk 	if (t->t_tid == 1) {
4664426Saguzovsk 		kpreempt_disable();
4674426Saguzovsk 		ASSERT(t->t_lpl != NULL);
4684426Saguzovsk 		p->p_t1_lgrpid = t->t_lpl->lpl_lgrpid;
4694426Saguzovsk 		kpreempt_enable();
4704426Saguzovsk 		if (p->p_tr_lgrpid != LGRP_NONE &&
4714426Saguzovsk 		    p->p_tr_lgrpid != p->p_t1_lgrpid) {
4724426Saguzovsk 			lgrp_update_trthr_migrations(1);
4734426Saguzovsk 		}
4744426Saguzovsk 	}
4754426Saguzovsk 
4760Sstevel@tonic-gate 	p->p_lwpcnt++;
4770Sstevel@tonic-gate 	t->t_waitfor = -1;
4780Sstevel@tonic-gate 
4790Sstevel@tonic-gate 	/*
4800Sstevel@tonic-gate 	 * Turn microstate accounting on for thread if on for process.
4810Sstevel@tonic-gate 	 */
4820Sstevel@tonic-gate 	if (p->p_flag & SMSACCT)
4830Sstevel@tonic-gate 		t->t_proc_flag |= TP_MSACCT;
4840Sstevel@tonic-gate 
4850Sstevel@tonic-gate 	/*
4860Sstevel@tonic-gate 	 * If the process has watchpoints, mark the new thread as such.
4870Sstevel@tonic-gate 	 */
4880Sstevel@tonic-gate 	if (pr_watch_active(p))
4890Sstevel@tonic-gate 		watch_enable(t);
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	/*
4920Sstevel@tonic-gate 	 * The lwp is being created in the stopped state.
4930Sstevel@tonic-gate 	 * We set all the necessary flags to indicate that fact here.
4940Sstevel@tonic-gate 	 * We omit the TS_CREATE flag from t_schedflag so that the lwp
4950Sstevel@tonic-gate 	 * cannot be set running until the caller is finished with it,
4960Sstevel@tonic-gate 	 * even if lwp_continue() is called on it after we drop p->p_lock.
4970Sstevel@tonic-gate 	 * When the caller is finished with the newly-created lwp,
4980Sstevel@tonic-gate 	 * the caller must call lwp_create_done() to allow the lwp
4990Sstevel@tonic-gate 	 * to be set running.  If the TP_HOLDLWP is left set, the
5000Sstevel@tonic-gate 	 * lwp will suspend itself after reaching system call exit.
5010Sstevel@tonic-gate 	 */
5020Sstevel@tonic-gate 	init_mstate(t, LMS_STOPPED);
5030Sstevel@tonic-gate 	t->t_proc_flag |= TP_HOLDLWP;
5040Sstevel@tonic-gate 	t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE));
5050Sstevel@tonic-gate 	t->t_whystop = PR_SUSPENDED;
5060Sstevel@tonic-gate 	t->t_whatstop = SUSPEND_NORMAL;
5070Sstevel@tonic-gate 	t->t_sig_check = 1;	/* ensure that TP_HOLDLWP is honored */
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	/*
5100Sstevel@tonic-gate 	 * Set system call processing flags in case tracing or profiling
5110Sstevel@tonic-gate 	 * is set.  The first system call will evaluate these and turn
5120Sstevel@tonic-gate 	 * them off if they aren't needed.
5130Sstevel@tonic-gate 	 */
5140Sstevel@tonic-gate 	t->t_pre_sys = 1;
5150Sstevel@tonic-gate 	t->t_post_sys = 1;
5160Sstevel@tonic-gate 
5170Sstevel@tonic-gate 	/*
5180Sstevel@tonic-gate 	 * Insert the new thread into the list of all threads.
5190Sstevel@tonic-gate 	 */
5200Sstevel@tonic-gate 	if ((tx = p->p_tlist) == NULL) {
5210Sstevel@tonic-gate 		t->t_back = t;
5220Sstevel@tonic-gate 		t->t_forw = t;
5230Sstevel@tonic-gate 		p->p_tlist = t;
5240Sstevel@tonic-gate 	} else {
5250Sstevel@tonic-gate 		t->t_forw = tx;
5260Sstevel@tonic-gate 		t->t_back = tx->t_back;
5270Sstevel@tonic-gate 		tx->t_back->t_forw = t;
5280Sstevel@tonic-gate 		tx->t_back = t;
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 
5310Sstevel@tonic-gate 	/*
5320Sstevel@tonic-gate 	 * Insert the new lwp into an lwp directory slot position
5330Sstevel@tonic-gate 	 * and into the lwpid hash table.
5340Sstevel@tonic-gate 	 */
5350Sstevel@tonic-gate 	lep->le_thread = t;
5360Sstevel@tonic-gate 	lep->le_lwpid = t->t_tid;
5370Sstevel@tonic-gate 	lep->le_start = t->t_start;
5380Sstevel@tonic-gate 	lwp_hash_in(p, lep);
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	if (state == TS_RUN) {
5410Sstevel@tonic-gate 		/*
5420Sstevel@tonic-gate 		 * We set the new lwp running immediately.
5430Sstevel@tonic-gate 		 */
5440Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
5450Sstevel@tonic-gate 		lwp_create_done(t);
5460Sstevel@tonic-gate 	}
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate error:
5490Sstevel@tonic-gate 	if (err) {
5500Sstevel@tonic-gate 		/*
5510Sstevel@tonic-gate 		 * We have failed to create an lwp, so decrement the number
5520Sstevel@tonic-gate 		 * of lwps in the task and let the lgroup load averages know
5530Sstevel@tonic-gate 		 * that this thread isn't going to show up.
5540Sstevel@tonic-gate 		 */
5550Sstevel@tonic-gate 		kpreempt_disable();
5560Sstevel@tonic-gate 		lgrp_move_thread(t, NULL, 1);
5570Sstevel@tonic-gate 		kpreempt_enable();
5580Sstevel@tonic-gate 
5590Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
5600Sstevel@tonic-gate 		mutex_enter(&p->p_zone->zone_nlwps_lock);
5610Sstevel@tonic-gate 		p->p_task->tk_nlwps--;
5620Sstevel@tonic-gate 		p->p_task->tk_proj->kpj_nlwps--;
5630Sstevel@tonic-gate 		p->p_zone->zone_nlwps--;
5640Sstevel@tonic-gate 		mutex_exit(&p->p_zone->zone_nlwps_lock);
5650Sstevel@tonic-gate 		if (cid != NOCLASS && bufp != NULL)
5660Sstevel@tonic-gate 			CL_FREE(cid, bufp);
5670Sstevel@tonic-gate 
5682712Snn35248 		if (branded)
5692712Snn35248 			BROP(p)->b_freelwp(lwp);
5702712Snn35248 
5710Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5720Sstevel@tonic-gate 		t->t_state = TS_FREE;
5730Sstevel@tonic-gate 		thread_rele(t);
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 		/*
5760Sstevel@tonic-gate 		 * We need to remove t from the list of all threads
5770Sstevel@tonic-gate 		 * because thread_exit()/lwp_exit() isn't called on t.
5780Sstevel@tonic-gate 		 */
5790Sstevel@tonic-gate 		mutex_enter(&pidlock);
5800Sstevel@tonic-gate 		ASSERT(t != t->t_next);		/* t0 never exits */
5810Sstevel@tonic-gate 		t->t_next->t_prev = t->t_prev;
5820Sstevel@tonic-gate 		t->t_prev->t_next = t->t_next;
5830Sstevel@tonic-gate 		mutex_exit(&pidlock);
5840Sstevel@tonic-gate 
5850Sstevel@tonic-gate 		thread_free(t);
5860Sstevel@tonic-gate 		kmem_free(lep, sizeof (*lep));
5870Sstevel@tonic-gate 		lwp = NULL;
5880Sstevel@tonic-gate 	} else {
5890Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
5900Sstevel@tonic-gate 	}
5910Sstevel@tonic-gate 
5920Sstevel@tonic-gate 	if (old_dir != NULL) {
5930Sstevel@tonic-gate 		kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
5940Sstevel@tonic-gate 		kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
5950Sstevel@tonic-gate 	}
5960Sstevel@tonic-gate 
5970Sstevel@tonic-gate 	DTRACE_PROC1(lwp__create, kthread_t *, t);
5980Sstevel@tonic-gate 	return (lwp);
5990Sstevel@tonic-gate }
6000Sstevel@tonic-gate 
6010Sstevel@tonic-gate /*
6020Sstevel@tonic-gate  * lwp_create_done() is called by the caller of lwp_create() to set the
6030Sstevel@tonic-gate  * newly-created lwp running after the caller has finished manipulating it.
6040Sstevel@tonic-gate  */
6050Sstevel@tonic-gate void
6060Sstevel@tonic-gate lwp_create_done(kthread_t *t)
6070Sstevel@tonic-gate {
6080Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6110Sstevel@tonic-gate 
6120Sstevel@tonic-gate 	/*
6130Sstevel@tonic-gate 	 * We set the TS_CREATE and TS_CSTART flags and call setrun_locked().
6140Sstevel@tonic-gate 	 * (The absence of the TS_CREATE flag prevents the lwp from running
6150Sstevel@tonic-gate 	 * until we are finished with it, even if lwp_continue() is called on
6160Sstevel@tonic-gate 	 * it by some other lwp in the process or elsewhere in the kernel.)
6170Sstevel@tonic-gate 	 */
6180Sstevel@tonic-gate 	thread_lock(t);
6190Sstevel@tonic-gate 	ASSERT(t->t_state == TS_STOPPED && !(t->t_schedflag & TS_CREATE));
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * If TS_CSTART is set, lwp_continue(t) has been called and
6220Sstevel@tonic-gate 	 * has already incremented p_lwprcnt; avoid doing this twice.
6230Sstevel@tonic-gate 	 */
6240Sstevel@tonic-gate 	if (!(t->t_schedflag & TS_CSTART))
6250Sstevel@tonic-gate 		p->p_lwprcnt++;
6260Sstevel@tonic-gate 	t->t_schedflag |= (TS_CSTART | TS_CREATE);
6270Sstevel@tonic-gate 	setrun_locked(t);
6280Sstevel@tonic-gate 	thread_unlock(t);
6290Sstevel@tonic-gate }
6300Sstevel@tonic-gate 
6310Sstevel@tonic-gate /*
6320Sstevel@tonic-gate  * Copy an LWP's active templates, and clear the latest contracts.
6330Sstevel@tonic-gate  */
6340Sstevel@tonic-gate void
6350Sstevel@tonic-gate lwp_ctmpl_copy(klwp_t *dst, klwp_t *src)
6360Sstevel@tonic-gate {
6370Sstevel@tonic-gate 	int i;
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate 	for (i = 0; i < ct_ntypes; i++) {
6400Sstevel@tonic-gate 		dst->lwp_ct_active[i] = ctmpl_dup(src->lwp_ct_active[i]);
6410Sstevel@tonic-gate 		dst->lwp_ct_latest[i] = NULL;
6420Sstevel@tonic-gate 	}
6430Sstevel@tonic-gate }
6440Sstevel@tonic-gate 
6450Sstevel@tonic-gate /*
6460Sstevel@tonic-gate  * Clear an LWP's contract template state.
6470Sstevel@tonic-gate  */
6480Sstevel@tonic-gate void
6490Sstevel@tonic-gate lwp_ctmpl_clear(klwp_t *lwp)
6500Sstevel@tonic-gate {
6510Sstevel@tonic-gate 	ct_template_t *tmpl;
6520Sstevel@tonic-gate 	int i;
6530Sstevel@tonic-gate 
6540Sstevel@tonic-gate 	for (i = 0; i < ct_ntypes; i++) {
6550Sstevel@tonic-gate 		if ((tmpl = lwp->lwp_ct_active[i]) != NULL) {
6560Sstevel@tonic-gate 			ctmpl_free(tmpl);
6570Sstevel@tonic-gate 			lwp->lwp_ct_active[i] = NULL;
6580Sstevel@tonic-gate 		}
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 		if (lwp->lwp_ct_latest[i] != NULL) {
6610Sstevel@tonic-gate 			contract_rele(lwp->lwp_ct_latest[i]);
6620Sstevel@tonic-gate 			lwp->lwp_ct_latest[i] = NULL;
6630Sstevel@tonic-gate 		}
6640Sstevel@tonic-gate 	}
6650Sstevel@tonic-gate }
6660Sstevel@tonic-gate 
6670Sstevel@tonic-gate /*
6680Sstevel@tonic-gate  * Individual lwp exit.
6690Sstevel@tonic-gate  * If this is the last lwp, exit the whole process.
6700Sstevel@tonic-gate  */
6710Sstevel@tonic-gate void
6720Sstevel@tonic-gate lwp_exit(void)
6730Sstevel@tonic-gate {
6740Sstevel@tonic-gate 	kthread_t *t = curthread;
6750Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
6760Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
6790Sstevel@tonic-gate 
6800Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate #if defined(__sparc)
6830Sstevel@tonic-gate 	/*
6840Sstevel@tonic-gate 	 * Ensure that the user stack is fully abandoned..
6850Sstevel@tonic-gate 	 */
6860Sstevel@tonic-gate 	trash_user_windows();
6870Sstevel@tonic-gate #endif
6880Sstevel@tonic-gate 
6890Sstevel@tonic-gate 	tsd_exit();			/* free thread specific data */
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	kcpc_passivate();		/* Clean up performance counter state */
6920Sstevel@tonic-gate 
6930Sstevel@tonic-gate 	pollcleanup();
6940Sstevel@tonic-gate 
6950Sstevel@tonic-gate 	if (t->t_door)
6960Sstevel@tonic-gate 		door_slam();
6970Sstevel@tonic-gate 
6980Sstevel@tonic-gate 	if (t->t_schedctl != NULL)
6990Sstevel@tonic-gate 		schedctl_lwp_cleanup(t);
7000Sstevel@tonic-gate 
7010Sstevel@tonic-gate 	if (t->t_upimutex != NULL)
7020Sstevel@tonic-gate 		upimutex_cleanup();
7030Sstevel@tonic-gate 
7042712Snn35248 	/*
7052712Snn35248 	 * Perform any brand specific exit processing, then release any
7062712Snn35248 	 * brand data associated with the lwp
7072712Snn35248 	 */
7082712Snn35248 	if (PROC_IS_BRANDED(p))
7092712Snn35248 		BROP(p)->b_lwpexit(lwp);
7102712Snn35248 
7110Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
7120Sstevel@tonic-gate 	lwp_cleanup();
7130Sstevel@tonic-gate 
7140Sstevel@tonic-gate 	/*
7150Sstevel@tonic-gate 	 * When this process is dumping core, its lwps are held here
7160Sstevel@tonic-gate 	 * until the core dump is finished. Then exitlwps() is called
7170Sstevel@tonic-gate 	 * again to release these lwps so that they can finish exiting.
7180Sstevel@tonic-gate 	 */
7190Sstevel@tonic-gate 	if (p->p_flag & SCOREDUMP)
7200Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
7210Sstevel@tonic-gate 
7220Sstevel@tonic-gate 	/*
7230Sstevel@tonic-gate 	 * Block the process against /proc now that we have really acquired
7240Sstevel@tonic-gate 	 * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least).
7250Sstevel@tonic-gate 	 */
7260Sstevel@tonic-gate 	prbarrier(p);
7270Sstevel@tonic-gate 
7280Sstevel@tonic-gate 	/*
7290Sstevel@tonic-gate 	 * Call proc_exit() if this is the last non-daemon lwp in the process.
7300Sstevel@tonic-gate 	 */
7310Sstevel@tonic-gate 	if (!(t->t_proc_flag & TP_DAEMON) &&
7320Sstevel@tonic-gate 	    p->p_lwpcnt == p->p_lwpdaemon + 1) {
7330Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
734390Sraf 		if (proc_exit(CLD_EXITED, 0) == 0) {
7350Sstevel@tonic-gate 			/* Restarting init. */
7360Sstevel@tonic-gate 			return;
7370Sstevel@tonic-gate 		}
7380Sstevel@tonic-gate 
7390Sstevel@tonic-gate 		/*
7400Sstevel@tonic-gate 		 * proc_exit() returns a non-zero value when some other
7410Sstevel@tonic-gate 		 * lwp got there first.  We just have to continue in
7420Sstevel@tonic-gate 		 * lwp_exit().
7430Sstevel@tonic-gate 		 */
7440Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
7450Sstevel@tonic-gate 		ASSERT(curproc->p_flag & SEXITLWPS);
7460Sstevel@tonic-gate 		prbarrier(p);
7470Sstevel@tonic-gate 	}
7480Sstevel@tonic-gate 
7490Sstevel@tonic-gate 	DTRACE_PROC(lwp__exit);
7500Sstevel@tonic-gate 
7510Sstevel@tonic-gate 	/*
7520Sstevel@tonic-gate 	 * If the lwp is a detached lwp or if the process is exiting,
7530Sstevel@tonic-gate 	 * remove (lwp_hash_out()) the lwp from the lwp directory.
7540Sstevel@tonic-gate 	 * Otherwise null out the lwp's le_thread pointer in the lwp
7550Sstevel@tonic-gate 	 * directory so that other threads will see it as a zombie lwp.
7560Sstevel@tonic-gate 	 */
7570Sstevel@tonic-gate 	prlwpexit(t);		/* notify /proc */
7580Sstevel@tonic-gate 	if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS))
7590Sstevel@tonic-gate 		lwp_hash_out(p, t->t_tid);
7600Sstevel@tonic-gate 	else {
7610Sstevel@tonic-gate 		ASSERT(!(t->t_proc_flag & TP_DAEMON));
7620Sstevel@tonic-gate 		p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL;
7630Sstevel@tonic-gate 		p->p_zombcnt++;
7640Sstevel@tonic-gate 		cv_broadcast(&p->p_lwpexit);
7650Sstevel@tonic-gate 	}
7660Sstevel@tonic-gate 	if (t->t_proc_flag & TP_DAEMON) {
7670Sstevel@tonic-gate 		p->p_lwpdaemon--;
7680Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_DAEMON;
7690Sstevel@tonic-gate 	}
7700Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_TWAIT;
7710Sstevel@tonic-gate 
7720Sstevel@tonic-gate 	/*
7730Sstevel@tonic-gate 	 * Maintain accurate lwp count for task.max-lwps resource control.
7740Sstevel@tonic-gate 	 */
7750Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
7760Sstevel@tonic-gate 	p->p_task->tk_nlwps--;
7770Sstevel@tonic-gate 	p->p_task->tk_proj->kpj_nlwps--;
7780Sstevel@tonic-gate 	p->p_zone->zone_nlwps--;
7790Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
7800Sstevel@tonic-gate 
7810Sstevel@tonic-gate 	CL_EXIT(t);		/* tell the scheduler that t is exiting */
7820Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt != 0);
7830Sstevel@tonic-gate 	p->p_lwpcnt--;
7840Sstevel@tonic-gate 
7850Sstevel@tonic-gate 	/*
7860Sstevel@tonic-gate 	 * If all remaining non-daemon lwps are waiting in lwp_wait(),
7870Sstevel@tonic-gate 	 * wake them up so someone can return EDEADLK.
7880Sstevel@tonic-gate 	 * (See the block comment preceeding lwp_wait().)
7890Sstevel@tonic-gate 	 */
7900Sstevel@tonic-gate 	if (p->p_lwpcnt == p->p_lwpdaemon + (p->p_lwpwait - p->p_lwpdwait))
7910Sstevel@tonic-gate 		cv_broadcast(&p->p_lwpexit);
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate 	t->t_proc_flag |= TP_LWPEXIT;
7940Sstevel@tonic-gate 	term_mstate(t);
7953792Sakolb 
7960Sstevel@tonic-gate #ifndef NPROBE
7970Sstevel@tonic-gate 	/* Kernel probe */
7980Sstevel@tonic-gate 	if (t->t_tnf_tpdp)
7990Sstevel@tonic-gate 		tnf_thread_exit();
8000Sstevel@tonic-gate #endif /* NPROBE */
8010Sstevel@tonic-gate 
8020Sstevel@tonic-gate 	t->t_forw->t_back = t->t_back;
8030Sstevel@tonic-gate 	t->t_back->t_forw = t->t_forw;
8040Sstevel@tonic-gate 	if (t == p->p_tlist)
8050Sstevel@tonic-gate 		p->p_tlist = t->t_forw;
8060Sstevel@tonic-gate 
8070Sstevel@tonic-gate 	/*
8080Sstevel@tonic-gate 	 * Clean up the signal state.
8090Sstevel@tonic-gate 	 */
8100Sstevel@tonic-gate 	if (t->t_sigqueue != NULL)
8110Sstevel@tonic-gate 		sigdelq(p, t, 0);
8120Sstevel@tonic-gate 	if (lwp->lwp_curinfo != NULL) {
8130Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
8140Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
8150Sstevel@tonic-gate 	}
8160Sstevel@tonic-gate 
8170Sstevel@tonic-gate 	thread_rele(t);
8180Sstevel@tonic-gate 
8190Sstevel@tonic-gate 	/*
8200Sstevel@tonic-gate 	 * Terminated lwps are associated with process zero and are put onto
8210Sstevel@tonic-gate 	 * death-row by resume().  Avoid preemption after resetting t->t_procp.
8220Sstevel@tonic-gate 	 */
8230Sstevel@tonic-gate 	t->t_preempt++;
8241217Srab 
8251217Srab 	if (t->t_ctx != NULL)
8261217Srab 		exitctx(t);
8271217Srab 	if (p->p_pctx != NULL)
8281217Srab 		exitpctx(p);
8291217Srab 
8300Sstevel@tonic-gate 	t->t_procp = &p0;
8310Sstevel@tonic-gate 
8320Sstevel@tonic-gate 	/*
8330Sstevel@tonic-gate 	 * Notify the HAT about the change of address space
8340Sstevel@tonic-gate 	 */
8350Sstevel@tonic-gate 	hat_thread_exit(t);
8360Sstevel@tonic-gate 	/*
8370Sstevel@tonic-gate 	 * When this is the last running lwp in this process and some lwp is
8380Sstevel@tonic-gate 	 * waiting for this condition to become true, or this thread was being
8390Sstevel@tonic-gate 	 * suspended, then the waiting lwp is awakened.
8400Sstevel@tonic-gate 	 *
8410Sstevel@tonic-gate 	 * Also, if the process is exiting, we may have a thread waiting in
8420Sstevel@tonic-gate 	 * exitlwps() that needs to be notified.
8430Sstevel@tonic-gate 	 */
8440Sstevel@tonic-gate 	if (--p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP) ||
8450Sstevel@tonic-gate 	    (p->p_flag & SEXITLWPS))
8460Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
8470Sstevel@tonic-gate 
8480Sstevel@tonic-gate 	/*
8490Sstevel@tonic-gate 	 * Need to drop p_lock so we can reacquire pidlock.
8500Sstevel@tonic-gate 	 */
8510Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
8520Sstevel@tonic-gate 	mutex_enter(&pidlock);
8530Sstevel@tonic-gate 
8540Sstevel@tonic-gate 	ASSERT(t != t->t_next);		/* t0 never exits */
8550Sstevel@tonic-gate 	t->t_next->t_prev = t->t_prev;
8560Sstevel@tonic-gate 	t->t_prev->t_next = t->t_next;
8570Sstevel@tonic-gate 	cv_broadcast(&t->t_joincv);	/* wake up anyone in thread_join */
8580Sstevel@tonic-gate 	mutex_exit(&pidlock);
8590Sstevel@tonic-gate 
8600Sstevel@tonic-gate 	lwp_pcb_exit();
8610Sstevel@tonic-gate 
8620Sstevel@tonic-gate 	t->t_state = TS_ZOMB;
8630Sstevel@tonic-gate 	swtch_from_zombie();
8640Sstevel@tonic-gate 	/* never returns */
8650Sstevel@tonic-gate }
8660Sstevel@tonic-gate 
8670Sstevel@tonic-gate 
8680Sstevel@tonic-gate /*
8690Sstevel@tonic-gate  * Cleanup function for an exiting lwp.
8700Sstevel@tonic-gate  * Called both from lwp_exit() and from proc_exit().
8710Sstevel@tonic-gate  * p->p_lock is repeatedly released and grabbed in this function.
8720Sstevel@tonic-gate  */
8730Sstevel@tonic-gate void
8740Sstevel@tonic-gate lwp_cleanup(void)
8750Sstevel@tonic-gate {
8760Sstevel@tonic-gate 	kthread_t *t = curthread;
8770Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
8780Sstevel@tonic-gate 
8790Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
8800Sstevel@tonic-gate 
8810Sstevel@tonic-gate 	/* untimeout any lwp-bound realtime timers */
8820Sstevel@tonic-gate 	if (p->p_itimer != NULL)
8830Sstevel@tonic-gate 		timer_lwpexit();
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 	/*
8860Sstevel@tonic-gate 	 * If this is the /proc agent lwp that is exiting, readjust p_lwpid
8870Sstevel@tonic-gate 	 * so it appears that the agent never existed, and clear p_agenttp.
8880Sstevel@tonic-gate 	 */
8890Sstevel@tonic-gate 	if (t == p->p_agenttp) {
8900Sstevel@tonic-gate 		ASSERT(t->t_tid == p->p_lwpid);
8910Sstevel@tonic-gate 		p->p_lwpid--;
8920Sstevel@tonic-gate 		p->p_agenttp = NULL;
8930Sstevel@tonic-gate 	}
8940Sstevel@tonic-gate 
8950Sstevel@tonic-gate 	/*
8960Sstevel@tonic-gate 	 * Do lgroup bookkeeping to account for thread exiting.
8970Sstevel@tonic-gate 	 */
8980Sstevel@tonic-gate 	kpreempt_disable();
8990Sstevel@tonic-gate 	lgrp_move_thread(t, NULL, 1);
9004426Saguzovsk 	if (t->t_tid == 1) {
9014426Saguzovsk 		p->p_t1_lgrpid = LGRP_NONE;
9024426Saguzovsk 	}
9030Sstevel@tonic-gate 	kpreempt_enable();
9040Sstevel@tonic-gate 
9050Sstevel@tonic-gate 	lwp_ctmpl_clear(ttolwp(t));
9060Sstevel@tonic-gate }
9070Sstevel@tonic-gate 
9080Sstevel@tonic-gate int
9090Sstevel@tonic-gate lwp_suspend(kthread_t *t)
9100Sstevel@tonic-gate {
9110Sstevel@tonic-gate 	int tid;
9120Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
9130Sstevel@tonic-gate 
9140Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
9150Sstevel@tonic-gate 
9160Sstevel@tonic-gate 	/*
9170Sstevel@tonic-gate 	 * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp().
9180Sstevel@tonic-gate 	 * If an lwp is stopping itself, there is no need to wait.
9190Sstevel@tonic-gate 	 */
920875Sraf top:
9210Sstevel@tonic-gate 	t->t_proc_flag |= TP_HOLDLWP;
9220Sstevel@tonic-gate 	if (t == curthread) {
9230Sstevel@tonic-gate 		t->t_sig_check = 1;
9240Sstevel@tonic-gate 	} else {
9250Sstevel@tonic-gate 		/*
9260Sstevel@tonic-gate 		 * Make sure the lwp stops promptly.
9270Sstevel@tonic-gate 		 */
9280Sstevel@tonic-gate 		thread_lock(t);
9290Sstevel@tonic-gate 		t->t_sig_check = 1;
9300Sstevel@tonic-gate 		/*
9310Sstevel@tonic-gate 		 * XXX Should use virtual stop like /proc does instead of
9320Sstevel@tonic-gate 		 * XXX waking the thread to get it to stop.
9330Sstevel@tonic-gate 		 */
9343792Sakolb 		if (ISWAKEABLE(t) || ISWAITING(t)) {
9350Sstevel@tonic-gate 			setrun_locked(t);
9363792Sakolb 		} else if (t->t_state == TS_ONPROC && t->t_cpu != CPU) {
9370Sstevel@tonic-gate 			poke_cpu(t->t_cpu->cpu_id);
9383792Sakolb 		}
9393792Sakolb 
9400Sstevel@tonic-gate 		tid = t->t_tid;	 /* remember thread ID */
9410Sstevel@tonic-gate 		/*
9420Sstevel@tonic-gate 		 * Wait for lwp to stop
9430Sstevel@tonic-gate 		 */
9440Sstevel@tonic-gate 		while (!SUSPENDED(t)) {
9450Sstevel@tonic-gate 			/*
9460Sstevel@tonic-gate 			 * Drop the thread lock before waiting and reacquire it
9470Sstevel@tonic-gate 			 * afterwards, so the thread can change its t_state
9480Sstevel@tonic-gate 			 * field.
9490Sstevel@tonic-gate 			 */
9500Sstevel@tonic-gate 			thread_unlock(t);
9510Sstevel@tonic-gate 
9520Sstevel@tonic-gate 			/*
9530Sstevel@tonic-gate 			 * Check if aborted by exitlwps().
9540Sstevel@tonic-gate 			 */
9550Sstevel@tonic-gate 			if (p->p_flag & SEXITLWPS)
9560Sstevel@tonic-gate 				lwp_exit();
9570Sstevel@tonic-gate 
9580Sstevel@tonic-gate 			/*
9590Sstevel@tonic-gate 			 * Cooperate with jobcontrol signals and /proc stopping
9600Sstevel@tonic-gate 			 * by calling cv_wait_sig() to wait for the target
9610Sstevel@tonic-gate 			 * lwp to stop.  Just using cv_wait() can lead to
9620Sstevel@tonic-gate 			 * deadlock because, if some other lwp has stopped
9630Sstevel@tonic-gate 			 * by either of these mechanisms, then p_lwprcnt will
9640Sstevel@tonic-gate 			 * never become zero if we do a cv_wait().
9650Sstevel@tonic-gate 			 */
9660Sstevel@tonic-gate 			if (!cv_wait_sig(&p->p_holdlwps, &p->p_lock))
9670Sstevel@tonic-gate 				return (EINTR);
9680Sstevel@tonic-gate 
9690Sstevel@tonic-gate 			/*
9700Sstevel@tonic-gate 			 * Check to see if thread died while we were
9710Sstevel@tonic-gate 			 * waiting for it to suspend.
9720Sstevel@tonic-gate 			 */
9730Sstevel@tonic-gate 			if (idtot(p, tid) == NULL)
9740Sstevel@tonic-gate 				return (ESRCH);
9750Sstevel@tonic-gate 
9760Sstevel@tonic-gate 			thread_lock(t);
9770Sstevel@tonic-gate 			/*
978875Sraf 			 * If the TP_HOLDLWP flag went away, lwp_continue()
979875Sraf 			 * or vfork() must have been called while we were
980875Sraf 			 * waiting, so start over again.
9810Sstevel@tonic-gate 			 */
9820Sstevel@tonic-gate 			if ((t->t_proc_flag & TP_HOLDLWP) == 0) {
9830Sstevel@tonic-gate 				thread_unlock(t);
984875Sraf 				goto top;
9850Sstevel@tonic-gate 			}
9860Sstevel@tonic-gate 		}
9870Sstevel@tonic-gate 		thread_unlock(t);
9880Sstevel@tonic-gate 	}
9890Sstevel@tonic-gate 	return (0);
9900Sstevel@tonic-gate }
9910Sstevel@tonic-gate 
9920Sstevel@tonic-gate /*
9930Sstevel@tonic-gate  * continue a lwp that's been stopped by lwp_suspend().
9940Sstevel@tonic-gate  */
9950Sstevel@tonic-gate void
9960Sstevel@tonic-gate lwp_continue(kthread_t *t)
9970Sstevel@tonic-gate {
9980Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
9990Sstevel@tonic-gate 	int was_suspended = t->t_proc_flag & TP_HOLDLWP;
10000Sstevel@tonic-gate 
10010Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
10020Sstevel@tonic-gate 
10030Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_HOLDLWP;
10040Sstevel@tonic-gate 	thread_lock(t);
10050Sstevel@tonic-gate 	if (SUSPENDED(t) &&
10060Sstevel@tonic-gate 	    !(p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH))) {
10070Sstevel@tonic-gate 		p->p_lwprcnt++;
10080Sstevel@tonic-gate 		t->t_schedflag |= TS_CSTART;
10090Sstevel@tonic-gate 		setrun_locked(t);
10100Sstevel@tonic-gate 	}
10110Sstevel@tonic-gate 	thread_unlock(t);
10120Sstevel@tonic-gate 	/*
10130Sstevel@tonic-gate 	 * Wakeup anyone waiting for this thread to be suspended
10140Sstevel@tonic-gate 	 */
10150Sstevel@tonic-gate 	if (was_suspended)
10160Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
10170Sstevel@tonic-gate }
10180Sstevel@tonic-gate 
10190Sstevel@tonic-gate /*
10200Sstevel@tonic-gate  * ********************************
10210Sstevel@tonic-gate  *  Miscellaneous lwp routines	  *
10220Sstevel@tonic-gate  * ********************************
10230Sstevel@tonic-gate  */
10240Sstevel@tonic-gate /*
10250Sstevel@tonic-gate  * When a process is undergoing a forkall(), its p_flag is set to SHOLDFORK.
10260Sstevel@tonic-gate  * This will cause the process's lwps to stop at a hold point.  A hold
10270Sstevel@tonic-gate  * point is where a kernel thread has a flat stack.  This is at the
10280Sstevel@tonic-gate  * return from a system call and at the return from a user level trap.
10290Sstevel@tonic-gate  *
10300Sstevel@tonic-gate  * When a process is undergoing a fork1() or vfork(), its p_flag is set to
10310Sstevel@tonic-gate  * SHOLDFORK1.  This will cause the process's lwps to stop at a modified
10320Sstevel@tonic-gate  * hold point.  The lwps in the process are not being cloned, so they
10330Sstevel@tonic-gate  * are held at the usual hold points and also within issig_forreal().
10340Sstevel@tonic-gate  * This has the side-effect that their system calls do not return
10350Sstevel@tonic-gate  * showing EINTR.
10360Sstevel@tonic-gate  *
10370Sstevel@tonic-gate  * An lwp can also be held.  This is identified by the TP_HOLDLWP flag on
10380Sstevel@tonic-gate  * the thread.  The TP_HOLDLWP flag is set in lwp_suspend(), where the active
10390Sstevel@tonic-gate  * lwp is waiting for the target lwp to be stopped.
10400Sstevel@tonic-gate  */
10410Sstevel@tonic-gate void
10420Sstevel@tonic-gate holdlwp(void)
10430Sstevel@tonic-gate {
10440Sstevel@tonic-gate 	proc_t *p = curproc;
10450Sstevel@tonic-gate 	kthread_t *t = curthread;
10460Sstevel@tonic-gate 
10470Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
10480Sstevel@tonic-gate 	/*
10490Sstevel@tonic-gate 	 * Don't terminate immediately if the process is dumping core.
10500Sstevel@tonic-gate 	 * Once the process has dumped core, all lwps are terminated.
10510Sstevel@tonic-gate 	 */
10520Sstevel@tonic-gate 	if (!(p->p_flag & SCOREDUMP)) {
10530Sstevel@tonic-gate 		if ((p->p_flag & SEXITLWPS) || (t->t_proc_flag & TP_EXITLWP))
10540Sstevel@tonic-gate 			lwp_exit();
10550Sstevel@tonic-gate 	}
10560Sstevel@tonic-gate 	if (!(ISHOLD(p)) && !(p->p_flag & (SHOLDFORK1 | SHOLDWATCH))) {
10570Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
10580Sstevel@tonic-gate 		return;
10590Sstevel@tonic-gate 	}
10600Sstevel@tonic-gate 	/*
10610Sstevel@tonic-gate 	 * stop() decrements p->p_lwprcnt and cv_signal()s &p->p_holdlwps
10620Sstevel@tonic-gate 	 * when p->p_lwprcnt becomes zero.
10630Sstevel@tonic-gate 	 */
10640Sstevel@tonic-gate 	stop(PR_SUSPENDED, SUSPEND_NORMAL);
10650Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS)
10660Sstevel@tonic-gate 		lwp_exit();
10670Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
10680Sstevel@tonic-gate }
10690Sstevel@tonic-gate 
10700Sstevel@tonic-gate /*
10710Sstevel@tonic-gate  * Have all lwps within the process hold at a point where they are
10720Sstevel@tonic-gate  * cloneable (SHOLDFORK) or just safe w.r.t. fork1 (SHOLDFORK1).
10730Sstevel@tonic-gate  */
10740Sstevel@tonic-gate int
10750Sstevel@tonic-gate holdlwps(int holdflag)
10760Sstevel@tonic-gate {
10770Sstevel@tonic-gate 	proc_t *p = curproc;
10780Sstevel@tonic-gate 
10790Sstevel@tonic-gate 	ASSERT(holdflag == SHOLDFORK || holdflag == SHOLDFORK1);
10800Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
10810Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
10820Sstevel@tonic-gate again:
10830Sstevel@tonic-gate 	while (p->p_flag & (SEXITLWPS | SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
10840Sstevel@tonic-gate 		/*
10850Sstevel@tonic-gate 		 * If another lwp is doing a forkall() or proc_exit(), bail out.
10860Sstevel@tonic-gate 		 */
10870Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS | SHOLDFORK)) {
10880Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
10890Sstevel@tonic-gate 			return (0);
10900Sstevel@tonic-gate 		}
10910Sstevel@tonic-gate 		/*
10920Sstevel@tonic-gate 		 * Another lwp is doing a fork1() or is undergoing
10930Sstevel@tonic-gate 		 * watchpoint activity.  We hold here for it to complete.
10940Sstevel@tonic-gate 		 */
10950Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
10960Sstevel@tonic-gate 	}
10970Sstevel@tonic-gate 	p->p_flag |= holdflag;
10980Sstevel@tonic-gate 	pokelwps(p);
10990Sstevel@tonic-gate 	--p->p_lwprcnt;
11000Sstevel@tonic-gate 	/*
11010Sstevel@tonic-gate 	 * Wait for the process to become quiescent (p->p_lwprcnt == 0).
11020Sstevel@tonic-gate 	 */
11030Sstevel@tonic-gate 	while (p->p_lwprcnt > 0) {
11040Sstevel@tonic-gate 		/*
11050Sstevel@tonic-gate 		 * Check if aborted by exitlwps().
11060Sstevel@tonic-gate 		 * Also check if SHOLDWATCH is set; it takes precedence.
11070Sstevel@tonic-gate 		 */
11080Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS | SHOLDWATCH)) {
11090Sstevel@tonic-gate 			p->p_lwprcnt++;
11100Sstevel@tonic-gate 			p->p_flag &= ~holdflag;
11110Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
11120Sstevel@tonic-gate 			goto again;
11130Sstevel@tonic-gate 		}
11140Sstevel@tonic-gate 		/*
11150Sstevel@tonic-gate 		 * Cooperate with jobcontrol signals and /proc stopping.
11160Sstevel@tonic-gate 		 * If some other lwp has stopped by either of these
11170Sstevel@tonic-gate 		 * mechanisms, then p_lwprcnt will never become zero
11180Sstevel@tonic-gate 		 * and the process will appear deadlocked unless we
11190Sstevel@tonic-gate 		 * stop here in sympathy with the other lwp before
11200Sstevel@tonic-gate 		 * doing the cv_wait() below.
11210Sstevel@tonic-gate 		 *
11220Sstevel@tonic-gate 		 * If the other lwp stops after we do the cv_wait(), it
11230Sstevel@tonic-gate 		 * will wake us up to loop around and do the sympathy stop.
11240Sstevel@tonic-gate 		 *
11250Sstevel@tonic-gate 		 * Since stop() drops p->p_lock, we must start from
11260Sstevel@tonic-gate 		 * the top again on returning from stop().
11270Sstevel@tonic-gate 		 */
11280Sstevel@tonic-gate 		if (p->p_stopsig | (curthread->t_proc_flag & TP_PRSTOP)) {
11290Sstevel@tonic-gate 			int whystop = p->p_stopsig? PR_JOBCONTROL :
11300Sstevel@tonic-gate 			    PR_REQUESTED;
11310Sstevel@tonic-gate 			p->p_lwprcnt++;
11320Sstevel@tonic-gate 			p->p_flag &= ~holdflag;
11330Sstevel@tonic-gate 			stop(whystop, p->p_stopsig);
11340Sstevel@tonic-gate 			goto again;
11350Sstevel@tonic-gate 		}
11360Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
11370Sstevel@tonic-gate 	}
11380Sstevel@tonic-gate 	p->p_lwprcnt++;
11390Sstevel@tonic-gate 	p->p_flag &= ~holdflag;
11400Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
11410Sstevel@tonic-gate 	return (1);
11420Sstevel@tonic-gate }
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate /*
11450Sstevel@tonic-gate  * See comments for holdwatch(), below.
11460Sstevel@tonic-gate  */
11470Sstevel@tonic-gate static int
11480Sstevel@tonic-gate holdcheck(int clearflags)
11490Sstevel@tonic-gate {
11500Sstevel@tonic-gate 	proc_t *p = curproc;
11510Sstevel@tonic-gate 
11520Sstevel@tonic-gate 	/*
11530Sstevel@tonic-gate 	 * If we are trying to exit, that takes precedence over anything else.
11540Sstevel@tonic-gate 	 */
11550Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS) {
11560Sstevel@tonic-gate 		p->p_lwprcnt++;
11570Sstevel@tonic-gate 		p->p_flag &= ~clearflags;
11580Sstevel@tonic-gate 		lwp_exit();
11590Sstevel@tonic-gate 	}
11600Sstevel@tonic-gate 
11610Sstevel@tonic-gate 	/*
11620Sstevel@tonic-gate 	 * If another thread is calling fork1(), stop the current thread so the
11630Sstevel@tonic-gate 	 * other can complete.
11640Sstevel@tonic-gate 	 */
11650Sstevel@tonic-gate 	if (p->p_flag & SHOLDFORK1) {
11660Sstevel@tonic-gate 		p->p_lwprcnt++;
11670Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
11680Sstevel@tonic-gate 		if (p->p_flag & SEXITLWPS) {
11690Sstevel@tonic-gate 			p->p_flag &= ~clearflags;
11700Sstevel@tonic-gate 			lwp_exit();
11710Sstevel@tonic-gate 		}
11720Sstevel@tonic-gate 		return (-1);
11730Sstevel@tonic-gate 	}
11740Sstevel@tonic-gate 
11750Sstevel@tonic-gate 	/*
11760Sstevel@tonic-gate 	 * If another thread is calling fork(), then indicate we are doing
11770Sstevel@tonic-gate 	 * watchpoint activity.  This will cause holdlwps() above to stop the
11780Sstevel@tonic-gate 	 * forking thread, at which point we can continue with watchpoint
11790Sstevel@tonic-gate 	 * activity.
11800Sstevel@tonic-gate 	 */
11810Sstevel@tonic-gate 	if (p->p_flag & SHOLDFORK) {
11820Sstevel@tonic-gate 		p->p_lwprcnt++;
11830Sstevel@tonic-gate 		while (p->p_flag & SHOLDFORK) {
11840Sstevel@tonic-gate 			p->p_flag |= SHOLDWATCH;
11850Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
11860Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
11870Sstevel@tonic-gate 			p->p_flag &= ~SHOLDWATCH;
11880Sstevel@tonic-gate 		}
11890Sstevel@tonic-gate 		return (-1);
11900Sstevel@tonic-gate 	}
11910Sstevel@tonic-gate 
11920Sstevel@tonic-gate 	return (0);
11930Sstevel@tonic-gate }
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate /*
11960Sstevel@tonic-gate  * Stop all lwps within the process, holding themselves in the kernel while the
11970Sstevel@tonic-gate  * active lwp undergoes watchpoint activity.  This is more complicated than
11980Sstevel@tonic-gate  * expected because stop() relies on calling holdwatch() in order to copyin data
11990Sstevel@tonic-gate  * from the user's address space.  A double barrier is used to prevent an
12000Sstevel@tonic-gate  * infinite loop.
12010Sstevel@tonic-gate  *
12020Sstevel@tonic-gate  * 	o The first thread into holdwatch() is the 'master' thread and does
12030Sstevel@tonic-gate  *        the following:
12040Sstevel@tonic-gate  *
12050Sstevel@tonic-gate  *              - Sets SHOLDWATCH on the current process
12060Sstevel@tonic-gate  *              - Sets TP_WATCHSTOP on the current thread
12070Sstevel@tonic-gate  *              - Waits for all threads to be either stopped or have
12080Sstevel@tonic-gate  *                TP_WATCHSTOP set.
12090Sstevel@tonic-gate  *              - Sets the SWATCHOK flag on the process
12100Sstevel@tonic-gate  *              - Unsets TP_WATCHSTOP
12110Sstevel@tonic-gate  *              - Waits for the other threads to completely stop
12120Sstevel@tonic-gate  *              - Unsets SWATCHOK
12130Sstevel@tonic-gate  *
12140Sstevel@tonic-gate  * 	o If SHOLDWATCH is already set when we enter this function, then another
12150Sstevel@tonic-gate  *        thread is already trying to stop this thread.  This 'slave' thread
12160Sstevel@tonic-gate  *        does the following:
12170Sstevel@tonic-gate  *
12180Sstevel@tonic-gate  *              - Sets TP_WATCHSTOP on the current thread
12190Sstevel@tonic-gate  *              - Waits for SWATCHOK flag to be set
12200Sstevel@tonic-gate  *              - Calls stop()
12210Sstevel@tonic-gate  *
12220Sstevel@tonic-gate  * 	o If SWATCHOK is set on the process, then this function immediately
12230Sstevel@tonic-gate  *        returns, as we must have been called via stop().
12240Sstevel@tonic-gate  *
12250Sstevel@tonic-gate  * In addition, there are other flags that take precedence over SHOLDWATCH:
12260Sstevel@tonic-gate  *
12270Sstevel@tonic-gate  * 	o If SEXITLWPS is set, exit immediately.
12280Sstevel@tonic-gate  *
12290Sstevel@tonic-gate  * 	o If SHOLDFORK1 is set, wait for fork1() to complete.
12300Sstevel@tonic-gate  *
12310Sstevel@tonic-gate  * 	o If SHOLDFORK is set, then watchpoint activity takes precedence In this
12320Sstevel@tonic-gate  *        case, set SHOLDWATCH, signalling the forking thread to stop first.
12330Sstevel@tonic-gate  *
12340Sstevel@tonic-gate  * 	o If the process is being stopped via /proc (TP_PRSTOP is set), then we
12350Sstevel@tonic-gate  *        stop the current thread.
12360Sstevel@tonic-gate  *
12370Sstevel@tonic-gate  * Returns 0 if all threads have been quiesced.  Returns non-zero if not all
12380Sstevel@tonic-gate  * threads were stopped, or the list of watched pages has changed.
12390Sstevel@tonic-gate  */
12400Sstevel@tonic-gate int
12410Sstevel@tonic-gate holdwatch(void)
12420Sstevel@tonic-gate {
12430Sstevel@tonic-gate 	proc_t *p = curproc;
12440Sstevel@tonic-gate 	kthread_t *t = curthread;
12450Sstevel@tonic-gate 	int ret = 0;
12460Sstevel@tonic-gate 
12470Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
12480Sstevel@tonic-gate 
12490Sstevel@tonic-gate 	p->p_lwprcnt--;
12500Sstevel@tonic-gate 
12510Sstevel@tonic-gate 	/*
12520Sstevel@tonic-gate 	 * Check for bail-out conditions as outlined above.
12530Sstevel@tonic-gate 	 */
12540Sstevel@tonic-gate 	if (holdcheck(0) != 0) {
12550Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
12560Sstevel@tonic-gate 		return (-1);
12570Sstevel@tonic-gate 	}
12580Sstevel@tonic-gate 
12590Sstevel@tonic-gate 	if (!(p->p_flag & SHOLDWATCH)) {
12600Sstevel@tonic-gate 		/*
12610Sstevel@tonic-gate 		 * We are the master watchpoint thread.  Set SHOLDWATCH and poke
12620Sstevel@tonic-gate 		 * the other threads.
12630Sstevel@tonic-gate 		 */
12640Sstevel@tonic-gate 		p->p_flag |= SHOLDWATCH;
12650Sstevel@tonic-gate 		pokelwps(p);
12660Sstevel@tonic-gate 
12670Sstevel@tonic-gate 		/*
12680Sstevel@tonic-gate 		 * Wait for all threads to be stopped or have TP_WATCHSTOP set.
12690Sstevel@tonic-gate 		 */
12700Sstevel@tonic-gate 		while (pr_allstopped(p, 1) > 0) {
12710Sstevel@tonic-gate 			if (holdcheck(SHOLDWATCH) != 0) {
12720Sstevel@tonic-gate 				p->p_flag &= ~SHOLDWATCH;
12730Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
12740Sstevel@tonic-gate 				return (-1);
12750Sstevel@tonic-gate 			}
12760Sstevel@tonic-gate 
12770Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
12780Sstevel@tonic-gate 		}
12790Sstevel@tonic-gate 
12800Sstevel@tonic-gate 		/*
12810Sstevel@tonic-gate 		 * All threads are now stopped or in the process of stopping.
12820Sstevel@tonic-gate 		 * Set SWATCHOK and let them stop completely.
12830Sstevel@tonic-gate 		 */
12840Sstevel@tonic-gate 		p->p_flag |= SWATCHOK;
12850Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_WATCHSTOP;
12860Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
12870Sstevel@tonic-gate 
12880Sstevel@tonic-gate 		while (pr_allstopped(p, 0) > 0) {
12890Sstevel@tonic-gate 			/*
12900Sstevel@tonic-gate 			 * At first glance, it may appear that we don't need a
12910Sstevel@tonic-gate 			 * call to holdcheck() here.  But if the process gets a
12920Sstevel@tonic-gate 			 * SIGKILL signal, one of our stopped threads may have
12930Sstevel@tonic-gate 			 * been awakened and is waiting in exitlwps(), which
12940Sstevel@tonic-gate 			 * takes precedence over watchpoints.
12950Sstevel@tonic-gate 			 */
12960Sstevel@tonic-gate 			if (holdcheck(SHOLDWATCH | SWATCHOK) != 0) {
12970Sstevel@tonic-gate 				p->p_flag &= ~(SHOLDWATCH | SWATCHOK);
12980Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
12990Sstevel@tonic-gate 				return (-1);
13000Sstevel@tonic-gate 			}
13010Sstevel@tonic-gate 
13020Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
13030Sstevel@tonic-gate 		}
13040Sstevel@tonic-gate 
13050Sstevel@tonic-gate 		/*
13060Sstevel@tonic-gate 		 * All threads are now completely stopped.
13070Sstevel@tonic-gate 		 */
13080Sstevel@tonic-gate 		p->p_flag &= ~SWATCHOK;
13090Sstevel@tonic-gate 		p->p_flag &= ~SHOLDWATCH;
13100Sstevel@tonic-gate 		p->p_lwprcnt++;
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	} else if (!(p->p_flag & SWATCHOK)) {
13130Sstevel@tonic-gate 
13140Sstevel@tonic-gate 		/*
13150Sstevel@tonic-gate 		 * SHOLDWATCH is set, so another thread is trying to do
13160Sstevel@tonic-gate 		 * watchpoint activity.  Indicate this thread is stopping, and
13170Sstevel@tonic-gate 		 * wait for the OK from the master thread.
13180Sstevel@tonic-gate 		 */
13190Sstevel@tonic-gate 		t->t_proc_flag |= TP_WATCHSTOP;
13200Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 		while (!(p->p_flag & SWATCHOK)) {
13230Sstevel@tonic-gate 			if (holdcheck(0) != 0) {
13240Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_WATCHSTOP;
13250Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
13260Sstevel@tonic-gate 				return (-1);
13270Sstevel@tonic-gate 			}
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
13300Sstevel@tonic-gate 		}
13310Sstevel@tonic-gate 
13320Sstevel@tonic-gate 		/*
13330Sstevel@tonic-gate 		 * Once the master thread has given the OK, this thread can
13340Sstevel@tonic-gate 		 * actually call stop().
13350Sstevel@tonic-gate 		 */
13360Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_WATCHSTOP;
13370Sstevel@tonic-gate 		p->p_lwprcnt++;
13380Sstevel@tonic-gate 
13390Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
13400Sstevel@tonic-gate 
13410Sstevel@tonic-gate 		/*
13420Sstevel@tonic-gate 		 * It's not OK to do watchpoint activity, notify caller to
13430Sstevel@tonic-gate 		 * retry.
13440Sstevel@tonic-gate 		 */
13450Sstevel@tonic-gate 		ret = -1;
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	} else {
13480Sstevel@tonic-gate 
13490Sstevel@tonic-gate 		/*
13500Sstevel@tonic-gate 		 * The only way we can hit the case where SHOLDWATCH is set and
13510Sstevel@tonic-gate 		 * SWATCHOK is set is if we are triggering this from within a
13520Sstevel@tonic-gate 		 * stop() call.  Assert that this is the case.
13530Sstevel@tonic-gate 		 */
13540Sstevel@tonic-gate 
13550Sstevel@tonic-gate 		ASSERT(t->t_proc_flag & TP_STOPPING);
13560Sstevel@tonic-gate 		p->p_lwprcnt++;
13570Sstevel@tonic-gate 	}
13580Sstevel@tonic-gate 
13590Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
13600Sstevel@tonic-gate 
13610Sstevel@tonic-gate 	return (ret);
13620Sstevel@tonic-gate }
13630Sstevel@tonic-gate 
13640Sstevel@tonic-gate /*
13650Sstevel@tonic-gate  * force all interruptible lwps to trap into the kernel.
13660Sstevel@tonic-gate  */
13670Sstevel@tonic-gate void
13680Sstevel@tonic-gate pokelwps(proc_t *p)
13690Sstevel@tonic-gate {
13700Sstevel@tonic-gate 	kthread_t *t;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	t = p->p_tlist;
13750Sstevel@tonic-gate 	do {
13760Sstevel@tonic-gate 		if (t == curthread)
13770Sstevel@tonic-gate 			continue;
13780Sstevel@tonic-gate 		thread_lock(t);
13790Sstevel@tonic-gate 		aston(t);	/* make thread trap or do post_syscall */
13803792Sakolb 		if (ISWAKEABLE(t) || ISWAITING(t)) {
13813792Sakolb 			setrun_locked(t);
13820Sstevel@tonic-gate 		} else if (t->t_state == TS_STOPPED) {
13830Sstevel@tonic-gate 			/*
13840Sstevel@tonic-gate 			 * Ensure that proc_exit() is not blocked by lwps
13850Sstevel@tonic-gate 			 * that were stopped via jobcontrol or /proc.
13860Sstevel@tonic-gate 			 */
13870Sstevel@tonic-gate 			if (p->p_flag & SEXITLWPS) {
13880Sstevel@tonic-gate 				p->p_stopsig = 0;
13890Sstevel@tonic-gate 				t->t_schedflag |= (TS_XSTART | TS_PSTART);
13900Sstevel@tonic-gate 				setrun_locked(t);
13910Sstevel@tonic-gate 			}
13920Sstevel@tonic-gate 			/*
13930Sstevel@tonic-gate 			 * If we are holding lwps for a forkall(),
13940Sstevel@tonic-gate 			 * force lwps that have been suspended via
13950Sstevel@tonic-gate 			 * lwp_suspend() and are suspended inside
13960Sstevel@tonic-gate 			 * of a system call to proceed to their
13970Sstevel@tonic-gate 			 * holdlwp() points where they are clonable.
13980Sstevel@tonic-gate 			 */
13990Sstevel@tonic-gate 			if ((p->p_flag & SHOLDFORK) && SUSPENDED(t)) {
14000Sstevel@tonic-gate 				if ((t->t_schedflag & TS_CSTART) == 0) {
14010Sstevel@tonic-gate 					p->p_lwprcnt++;
14020Sstevel@tonic-gate 					t->t_schedflag |= TS_CSTART;
14030Sstevel@tonic-gate 					setrun_locked(t);
14040Sstevel@tonic-gate 				}
14050Sstevel@tonic-gate 			}
14060Sstevel@tonic-gate 		} else if (t->t_state == TS_ONPROC) {
14070Sstevel@tonic-gate 			if (t->t_cpu != CPU)
14080Sstevel@tonic-gate 				poke_cpu(t->t_cpu->cpu_id);
14090Sstevel@tonic-gate 		}
14100Sstevel@tonic-gate 		thread_unlock(t);
14110Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
14120Sstevel@tonic-gate }
14130Sstevel@tonic-gate 
14140Sstevel@tonic-gate /*
14150Sstevel@tonic-gate  * undo the effects of holdlwps() or holdwatch().
14160Sstevel@tonic-gate  */
14170Sstevel@tonic-gate void
14180Sstevel@tonic-gate continuelwps(proc_t *p)
14190Sstevel@tonic-gate {
14200Sstevel@tonic-gate 	kthread_t *t;
14210Sstevel@tonic-gate 
14220Sstevel@tonic-gate 	/*
14230Sstevel@tonic-gate 	 * If this flag is set, then the original holdwatch() didn't actually
14240Sstevel@tonic-gate 	 * stop the process.  See comments for holdwatch().
14250Sstevel@tonic-gate 	 */
14260Sstevel@tonic-gate 	if (p->p_flag & SWATCHOK) {
14270Sstevel@tonic-gate 		ASSERT(curthread->t_proc_flag & TP_STOPPING);
14280Sstevel@tonic-gate 		return;
14290Sstevel@tonic-gate 	}
14300Sstevel@tonic-gate 
14310Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
14320Sstevel@tonic-gate 	ASSERT((p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) == 0);
14330Sstevel@tonic-gate 
14340Sstevel@tonic-gate 	t = p->p_tlist;
14350Sstevel@tonic-gate 	do {
14360Sstevel@tonic-gate 		thread_lock(t);		/* SUSPENDED looks at t_schedflag */
14370Sstevel@tonic-gate 		if (SUSPENDED(t) && !(t->t_proc_flag & TP_HOLDLWP)) {
14380Sstevel@tonic-gate 			p->p_lwprcnt++;
14390Sstevel@tonic-gate 			t->t_schedflag |= TS_CSTART;
14400Sstevel@tonic-gate 			setrun_locked(t);
14410Sstevel@tonic-gate 		}
14420Sstevel@tonic-gate 		thread_unlock(t);
14430Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /*
14470Sstevel@tonic-gate  * Force all other LWPs in the current process other than the caller to exit,
14480Sstevel@tonic-gate  * and then cv_wait() on p_holdlwps for them to exit.  The exitlwps() function
14490Sstevel@tonic-gate  * is typically used in these situations:
14500Sstevel@tonic-gate  *
14510Sstevel@tonic-gate  *   (a) prior to an exec() system call
14520Sstevel@tonic-gate  *   (b) prior to dumping a core file
14530Sstevel@tonic-gate  *   (c) prior to a uadmin() shutdown
14540Sstevel@tonic-gate  *
14550Sstevel@tonic-gate  * If the 'coredump' flag is set, other LWPs are quiesced but not destroyed.
14560Sstevel@tonic-gate  * Multiple threads in the process can call this function at one time by
14570Sstevel@tonic-gate  * triggering execs or core dumps simultaneously, so the SEXITLWPS bit is used
14580Sstevel@tonic-gate  * to declare one particular thread the winner who gets to kill the others.
14590Sstevel@tonic-gate  * If a thread wins the exitlwps() dance, zero is returned; otherwise an
14600Sstevel@tonic-gate  * appropriate errno value is returned to caller for its system call to return.
14610Sstevel@tonic-gate  */
14620Sstevel@tonic-gate int
14630Sstevel@tonic-gate exitlwps(int coredump)
14640Sstevel@tonic-gate {
14650Sstevel@tonic-gate 	proc_t *p = curproc;
14660Sstevel@tonic-gate 	int heldcnt;
14670Sstevel@tonic-gate 
14680Sstevel@tonic-gate 	if (curthread->t_door)
14690Sstevel@tonic-gate 		door_slam();
14700Sstevel@tonic-gate 	if (p->p_door_list)
14710Sstevel@tonic-gate 		door_revoke_all();
14720Sstevel@tonic-gate 	if (curthread->t_schedctl != NULL)
14730Sstevel@tonic-gate 		schedctl_lwp_cleanup(curthread);
14740Sstevel@tonic-gate 
14750Sstevel@tonic-gate 	/*
14760Sstevel@tonic-gate 	 * Ensure that before starting to wait for other lwps to exit,
14770Sstevel@tonic-gate 	 * cleanup all upimutexes held by curthread. Otherwise, some other
14780Sstevel@tonic-gate 	 * lwp could be waiting (uninterruptibly) for a upimutex held by
14790Sstevel@tonic-gate 	 * curthread, and the call to pokelwps() below would deadlock.
14800Sstevel@tonic-gate 	 * Even if a blocked upimutex_lock is made interruptible,
14810Sstevel@tonic-gate 	 * curthread's upimutexes need to be unlocked: do it here.
14820Sstevel@tonic-gate 	 */
14830Sstevel@tonic-gate 	if (curthread->t_upimutex != NULL)
14840Sstevel@tonic-gate 		upimutex_cleanup();
14850Sstevel@tonic-gate 
14860Sstevel@tonic-gate 	/*
14870Sstevel@tonic-gate 	 * Grab p_lock in order to check and set SEXITLWPS to declare a winner.
14880Sstevel@tonic-gate 	 * We must also block any further /proc access from this point forward.
14890Sstevel@tonic-gate 	 */
14900Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
14910Sstevel@tonic-gate 	prbarrier(p);
14920Sstevel@tonic-gate 
14930Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS) {
14940Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
14950Sstevel@tonic-gate 		aston(curthread);	/* force a trip through post_syscall */
14960Sstevel@tonic-gate 		return (set_errno(EINTR));
14970Sstevel@tonic-gate 	}
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 	p->p_flag |= SEXITLWPS;
15000Sstevel@tonic-gate 	if (coredump)		/* tell other lwps to stop, not exit */
15010Sstevel@tonic-gate 		p->p_flag |= SCOREDUMP;
15020Sstevel@tonic-gate 
15030Sstevel@tonic-gate 	/*
15040Sstevel@tonic-gate 	 * Give precedence to exitlwps() if a holdlwps() is
15050Sstevel@tonic-gate 	 * in progress. The lwp doing the holdlwps() operation
15060Sstevel@tonic-gate 	 * is aborted when it is awakened.
15070Sstevel@tonic-gate 	 */
15080Sstevel@tonic-gate 	while (p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
15090Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
15100Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
1511390Sraf 		prbarrier(p);
15120Sstevel@tonic-gate 	}
15130Sstevel@tonic-gate 	p->p_flag |= SHOLDFORK;
15140Sstevel@tonic-gate 	pokelwps(p);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	/*
15170Sstevel@tonic-gate 	 * Wait for process to become quiescent.
15180Sstevel@tonic-gate 	 */
15190Sstevel@tonic-gate 	--p->p_lwprcnt;
1520390Sraf 	while (p->p_lwprcnt > 0) {
15210Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
1522390Sraf 		prbarrier(p);
1523390Sraf 	}
15240Sstevel@tonic-gate 	p->p_lwprcnt++;
15250Sstevel@tonic-gate 	ASSERT(p->p_lwprcnt == 1);
15260Sstevel@tonic-gate 
15270Sstevel@tonic-gate 	/*
15280Sstevel@tonic-gate 	 * The SCOREDUMP flag puts the process into a quiescent
15290Sstevel@tonic-gate 	 * state.  The process's lwps remain attached to this
15300Sstevel@tonic-gate 	 * process until exitlwps() is called again without the
15310Sstevel@tonic-gate 	 * 'coredump' flag set, then the lwps are terminated
15320Sstevel@tonic-gate 	 * and the process can exit.
15330Sstevel@tonic-gate 	 */
15340Sstevel@tonic-gate 	if (coredump) {
15350Sstevel@tonic-gate 		p->p_flag &= ~(SCOREDUMP | SHOLDFORK | SEXITLWPS);
15360Sstevel@tonic-gate 		goto out;
15370Sstevel@tonic-gate 	}
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate 	/*
15400Sstevel@tonic-gate 	 * Determine if there are any lwps left dangling in
15410Sstevel@tonic-gate 	 * the stopped state.  This happens when exitlwps()
15420Sstevel@tonic-gate 	 * aborts a holdlwps() operation.
15430Sstevel@tonic-gate 	 */
15440Sstevel@tonic-gate 	p->p_flag &= ~SHOLDFORK;
15450Sstevel@tonic-gate 	if ((heldcnt = p->p_lwpcnt) > 1) {
15460Sstevel@tonic-gate 		kthread_t *t;
15470Sstevel@tonic-gate 		for (t = curthread->t_forw; --heldcnt > 0; t = t->t_forw) {
15480Sstevel@tonic-gate 			t->t_proc_flag &= ~TP_TWAIT;
15490Sstevel@tonic-gate 			lwp_continue(t);
15500Sstevel@tonic-gate 		}
15510Sstevel@tonic-gate 	}
15520Sstevel@tonic-gate 
15530Sstevel@tonic-gate 	/*
15540Sstevel@tonic-gate 	 * Wait for all other lwps to exit.
15550Sstevel@tonic-gate 	 */
15560Sstevel@tonic-gate 	--p->p_lwprcnt;
1557390Sraf 	while (p->p_lwpcnt > 1) {
15580Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
1559390Sraf 		prbarrier(p);
1560390Sraf 	}
15610Sstevel@tonic-gate 	++p->p_lwprcnt;
15620Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_lwprcnt == 1);
15630Sstevel@tonic-gate 
15640Sstevel@tonic-gate 	p->p_flag &= ~SEXITLWPS;
15650Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_TWAIT;
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate out:
15680Sstevel@tonic-gate 	if (!coredump && p->p_zombcnt) {	/* cleanup the zombie lwps */
15690Sstevel@tonic-gate 		lwpdir_t *ldp;
15700Sstevel@tonic-gate 		lwpent_t *lep;
15710Sstevel@tonic-gate 		int i;
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate 		for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
15740Sstevel@tonic-gate 			lep = ldp->ld_entry;
15750Sstevel@tonic-gate 			if (lep != NULL && lep->le_thread != curthread) {
15760Sstevel@tonic-gate 				ASSERT(lep->le_thread == NULL);
15770Sstevel@tonic-gate 				p->p_zombcnt--;
15780Sstevel@tonic-gate 				lwp_hash_out(p, lep->le_lwpid);
15790Sstevel@tonic-gate 			}
15800Sstevel@tonic-gate 		}
15810Sstevel@tonic-gate 		ASSERT(p->p_zombcnt == 0);
15820Sstevel@tonic-gate 	}
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate 	/*
15850Sstevel@tonic-gate 	 * If some other LWP in the process wanted us to suspend ourself,
15860Sstevel@tonic-gate 	 * then we will not do it.  The other LWP is now terminated and
15870Sstevel@tonic-gate 	 * no one will ever continue us again if we suspend ourself.
15880Sstevel@tonic-gate 	 */
15890Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_HOLDLWP;
15900Sstevel@tonic-gate 	p->p_flag &= ~(SHOLDFORK | SHOLDFORK1 | SHOLDWATCH | SLWPWRAP);
15910Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
15920Sstevel@tonic-gate 	return (0);
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate /*
15960Sstevel@tonic-gate  * duplicate a lwp.
15970Sstevel@tonic-gate  */
15980Sstevel@tonic-gate klwp_t *
15990Sstevel@tonic-gate forklwp(klwp_t *lwp, proc_t *cp, id_t lwpid)
16000Sstevel@tonic-gate {
16010Sstevel@tonic-gate 	klwp_t *clwp;
16020Sstevel@tonic-gate 	void *tregs, *tfpu;
16030Sstevel@tonic-gate 	kthread_t *t = lwptot(lwp);
16040Sstevel@tonic-gate 	kthread_t *ct;
16050Sstevel@tonic-gate 	proc_t *p = lwptoproc(lwp);
16060Sstevel@tonic-gate 	int cid;
16070Sstevel@tonic-gate 	void *bufp;
16082712Snn35248 	void *brand_data;
16090Sstevel@tonic-gate 	int val;
16100Sstevel@tonic-gate 
16110Sstevel@tonic-gate 	ASSERT(p == curproc);
16120Sstevel@tonic-gate 	ASSERT(t == curthread || (SUSPENDED(t) && lwp->lwp_asleep == 0));
16130Sstevel@tonic-gate 
16140Sstevel@tonic-gate #if defined(__sparc)
16150Sstevel@tonic-gate 	if (t == curthread)
16160Sstevel@tonic-gate 		(void) flush_user_windows_to_stack(NULL);
16170Sstevel@tonic-gate #endif
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate 	if (t == curthread)
16200Sstevel@tonic-gate 		/* copy args out of registers first */
16210Sstevel@tonic-gate 		(void) save_syscall_args();
16222712Snn35248 
16230Sstevel@tonic-gate 	clwp = lwp_create(cp->p_lwpcnt == 0 ? lwp_rtt_initial : lwp_rtt,
16240Sstevel@tonic-gate 	    NULL, 0, cp, TS_STOPPED, t->t_pri, &t->t_hold, NOCLASS, lwpid);
16250Sstevel@tonic-gate 	if (clwp == NULL)
16260Sstevel@tonic-gate 		return (NULL);
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate 	/*
16290Sstevel@tonic-gate 	 * most of the parent's lwp can be copied to its duplicate,
16300Sstevel@tonic-gate 	 * except for the fields that are unique to each lwp, like
16310Sstevel@tonic-gate 	 * lwp_thread, lwp_procp, lwp_regs, and lwp_ap.
16320Sstevel@tonic-gate 	 */
16330Sstevel@tonic-gate 	ct = clwp->lwp_thread;
16340Sstevel@tonic-gate 	tregs = clwp->lwp_regs;
16350Sstevel@tonic-gate 	tfpu = clwp->lwp_fpu;
16362712Snn35248 	brand_data = clwp->lwp_brand;
16370Sstevel@tonic-gate 
16383551Sjohansen 	/*
16393551Sjohansen 	 * Copy parent lwp to child lwp.  Hold child's p_lock to prevent
16403551Sjohansen 	 * mstate_aggr_state() from reading stale mstate entries copied
16413551Sjohansen 	 * from lwp to clwp.
16423551Sjohansen 	 */
16433551Sjohansen 	mutex_enter(&cp->p_lock);
16440Sstevel@tonic-gate 	*clwp = *lwp;
16450Sstevel@tonic-gate 
16463551Sjohansen 	/* clear microstate and resource usage data in new lwp */
16473551Sjohansen 	init_mstate(ct, LMS_STOPPED);
16483551Sjohansen 	bzero(&clwp->lwp_ru, sizeof (clwp->lwp_ru));
16493551Sjohansen 	mutex_exit(&cp->p_lock);
16503551Sjohansen 
16510Sstevel@tonic-gate 	/* fix up child's lwp */
16520Sstevel@tonic-gate 
1653*4503Ssudheer 	clwp->lwp_pcb.pcb_flags = 0;
1654*4503Ssudheer #if defined(__sparc)
16550Sstevel@tonic-gate 	clwp->lwp_pcb.pcb_step = STEP_NONE;
16560Sstevel@tonic-gate #endif
16570Sstevel@tonic-gate 	clwp->lwp_cursig = 0;
16580Sstevel@tonic-gate 	clwp->lwp_extsig = 0;
16590Sstevel@tonic-gate 	clwp->lwp_curinfo = (struct sigqueue *)0;
16600Sstevel@tonic-gate 	clwp->lwp_thread = ct;
16610Sstevel@tonic-gate 	ct->t_sysnum = t->t_sysnum;
16620Sstevel@tonic-gate 	clwp->lwp_regs = tregs;
16630Sstevel@tonic-gate 	clwp->lwp_fpu = tfpu;
16642712Snn35248 	clwp->lwp_brand = brand_data;
16650Sstevel@tonic-gate 	clwp->lwp_ap = clwp->lwp_arg;
16660Sstevel@tonic-gate 	clwp->lwp_procp = cp;
16670Sstevel@tonic-gate 	bzero(clwp->lwp_timer, sizeof (clwp->lwp_timer));
16680Sstevel@tonic-gate 	clwp->lwp_lastfault = 0;
16690Sstevel@tonic-gate 	clwp->lwp_lastfaddr = 0;
16700Sstevel@tonic-gate 
16710Sstevel@tonic-gate 	/* copy parent's struct regs to child. */
16720Sstevel@tonic-gate 	lwp_forkregs(lwp, clwp);
16730Sstevel@tonic-gate 
16740Sstevel@tonic-gate 	/*
16751217Srab 	 * Fork thread context ops, if any.
16760Sstevel@tonic-gate 	 */
16770Sstevel@tonic-gate 	if (t->t_ctx)
16780Sstevel@tonic-gate 		forkctx(t, ct);
16790Sstevel@tonic-gate 
16800Sstevel@tonic-gate 	/* fix door state in the child */
16810Sstevel@tonic-gate 	if (t->t_door)
16820Sstevel@tonic-gate 		door_fork(t, ct);
16830Sstevel@tonic-gate 
16840Sstevel@tonic-gate 	/* copy current contract templates, clear latest contracts */
16850Sstevel@tonic-gate 	lwp_ctmpl_copy(clwp, lwp);
16860Sstevel@tonic-gate 
16870Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
16880Sstevel@tonic-gate 	/* lwp_create() set the TP_HOLDLWP flag */
16890Sstevel@tonic-gate 	if (!(t->t_proc_flag & TP_HOLDLWP))
16900Sstevel@tonic-gate 		ct->t_proc_flag &= ~TP_HOLDLWP;
16910Sstevel@tonic-gate 	if (cp->p_flag & SMSACCT)
16920Sstevel@tonic-gate 		ct->t_proc_flag |= TP_MSACCT;
16930Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
16940Sstevel@tonic-gate 
16952712Snn35248 	/* Allow brand to propagate brand-specific state */
16962712Snn35248 	if (PROC_IS_BRANDED(p))
16972712Snn35248 		BROP(p)->b_forklwp(lwp, clwp);
16982712Snn35248 
16990Sstevel@tonic-gate retry:
17000Sstevel@tonic-gate 	cid = t->t_cid;
17010Sstevel@tonic-gate 
17020Sstevel@tonic-gate 	val = CL_ALLOC(&bufp, cid, KM_SLEEP);
17030Sstevel@tonic-gate 	ASSERT(val == 0);
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
17060Sstevel@tonic-gate 	if (cid != t->t_cid) {
17070Sstevel@tonic-gate 		/*
17080Sstevel@tonic-gate 		 * Someone just changed this thread's scheduling class,
17090Sstevel@tonic-gate 		 * so try pre-allocating the buffer again.  Hopefully we
17100Sstevel@tonic-gate 		 * don't hit this often.
17110Sstevel@tonic-gate 		 */
17120Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
17130Sstevel@tonic-gate 		CL_FREE(cid, bufp);
17140Sstevel@tonic-gate 		goto retry;
17150Sstevel@tonic-gate 	}
17160Sstevel@tonic-gate 
17170Sstevel@tonic-gate 	ct->t_unpark = t->t_unpark;
17180Sstevel@tonic-gate 	ct->t_clfuncs = t->t_clfuncs;
17190Sstevel@tonic-gate 	CL_FORK(t, ct, bufp);
17200Sstevel@tonic-gate 	ct->t_cid = t->t_cid;	/* after data allocated so prgetpsinfo works */
17210Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
17220Sstevel@tonic-gate 
17230Sstevel@tonic-gate 	return (clwp);
17240Sstevel@tonic-gate }
17250Sstevel@tonic-gate 
17260Sstevel@tonic-gate /*
17270Sstevel@tonic-gate  * Add a new lwp entry to the lwp directory and to the lwpid hash table.
17280Sstevel@tonic-gate  */
17290Sstevel@tonic-gate void
17300Sstevel@tonic-gate lwp_hash_in(proc_t *p, lwpent_t *lep)
17310Sstevel@tonic-gate {
17320Sstevel@tonic-gate 	lwpdir_t **ldpp;
17330Sstevel@tonic-gate 	lwpdir_t *ldp;
17340Sstevel@tonic-gate 	kthread_t *t;
17350Sstevel@tonic-gate 
17360Sstevel@tonic-gate 	/*
17370Sstevel@tonic-gate 	 * Allocate a directory element from the free list.
17380Sstevel@tonic-gate 	 * Code elsewhere guarantees a free slot.
17390Sstevel@tonic-gate 	 */
17400Sstevel@tonic-gate 	ldp = p->p_lwpfree;
17410Sstevel@tonic-gate 	p->p_lwpfree = ldp->ld_next;
17420Sstevel@tonic-gate 	ASSERT(ldp->ld_entry == NULL);
17430Sstevel@tonic-gate 	ldp->ld_entry = lep;
17440Sstevel@tonic-gate 
17450Sstevel@tonic-gate 	/*
17460Sstevel@tonic-gate 	 * Insert it into the lwpid hash table.
17470Sstevel@tonic-gate 	 */
17480Sstevel@tonic-gate 	ldpp = &p->p_tidhash[TIDHASH(p, lep->le_lwpid)];
17490Sstevel@tonic-gate 	ldp->ld_next = *ldpp;
17500Sstevel@tonic-gate 	*ldpp = ldp;
17510Sstevel@tonic-gate 
17520Sstevel@tonic-gate 	/*
17530Sstevel@tonic-gate 	 * Set the active thread's directory slot entry.
17540Sstevel@tonic-gate 	 */
17550Sstevel@tonic-gate 	if ((t = lep->le_thread) != NULL) {
17560Sstevel@tonic-gate 		ASSERT(lep->le_lwpid == t->t_tid);
17570Sstevel@tonic-gate 		t->t_dslot = (int)(ldp - p->p_lwpdir);
17580Sstevel@tonic-gate 	}
17590Sstevel@tonic-gate }
17600Sstevel@tonic-gate 
17610Sstevel@tonic-gate /*
17620Sstevel@tonic-gate  * Remove an lwp from the lwpid hash table and free its directory entry.
17630Sstevel@tonic-gate  * This is done when a detached lwp exits in lwp_exit() or
17640Sstevel@tonic-gate  * when a non-detached lwp is waited for in lwp_wait() or
17650Sstevel@tonic-gate  * when a zombie lwp is detached in lwp_detach().
17660Sstevel@tonic-gate  */
17670Sstevel@tonic-gate void
17680Sstevel@tonic-gate lwp_hash_out(proc_t *p, id_t lwpid)
17690Sstevel@tonic-gate {
17700Sstevel@tonic-gate 	lwpdir_t **ldpp;
17710Sstevel@tonic-gate 	lwpdir_t *ldp;
17720Sstevel@tonic-gate 	lwpent_t *lep;
17730Sstevel@tonic-gate 
17740Sstevel@tonic-gate 	for (ldpp = &p->p_tidhash[TIDHASH(p, lwpid)];
17750Sstevel@tonic-gate 	    (ldp = *ldpp) != NULL; ldpp = &ldp->ld_next) {
17760Sstevel@tonic-gate 		lep = ldp->ld_entry;
17770Sstevel@tonic-gate 		if (lep->le_lwpid == lwpid) {
17780Sstevel@tonic-gate 			prlwpfree(p, lep);	/* /proc deals with le_trace */
17790Sstevel@tonic-gate 			*ldpp = ldp->ld_next;
17800Sstevel@tonic-gate 			ldp->ld_entry = NULL;
17810Sstevel@tonic-gate 			ldp->ld_next = p->p_lwpfree;
17820Sstevel@tonic-gate 			p->p_lwpfree = ldp;
17830Sstevel@tonic-gate 			kmem_free(lep, sizeof (*lep));
17840Sstevel@tonic-gate 			break;
17850Sstevel@tonic-gate 		}
17860Sstevel@tonic-gate 	}
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate /*
17900Sstevel@tonic-gate  * Lookup an lwp in the lwpid hash table by lwpid.
17910Sstevel@tonic-gate  */
17920Sstevel@tonic-gate lwpdir_t *
17930Sstevel@tonic-gate lwp_hash_lookup(proc_t *p, id_t lwpid)
17940Sstevel@tonic-gate {
17950Sstevel@tonic-gate 	lwpdir_t *ldp;
17960Sstevel@tonic-gate 
17970Sstevel@tonic-gate 	/*
17980Sstevel@tonic-gate 	 * The process may be exiting, after p_tidhash has been set to NULL in
17990Sstevel@tonic-gate 	 * proc_exit() but before prfee() has been called.  Return failure in
18000Sstevel@tonic-gate 	 * this case.
18010Sstevel@tonic-gate 	 */
18020Sstevel@tonic-gate 	if (p->p_tidhash == NULL)
18030Sstevel@tonic-gate 		return (NULL);
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate 	for (ldp = p->p_tidhash[TIDHASH(p, lwpid)];
18060Sstevel@tonic-gate 	    ldp != NULL; ldp = ldp->ld_next) {
18070Sstevel@tonic-gate 		if (ldp->ld_entry->le_lwpid == lwpid)
18080Sstevel@tonic-gate 			return (ldp);
18090Sstevel@tonic-gate 	}
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate 	return (NULL);
18120Sstevel@tonic-gate }
18130Sstevel@tonic-gate 
18140Sstevel@tonic-gate /*
18150Sstevel@tonic-gate  * Update the indicated LWP usage statistic for the current LWP.
18160Sstevel@tonic-gate  */
18170Sstevel@tonic-gate void
18180Sstevel@tonic-gate lwp_stat_update(lwp_stat_id_t lwp_stat_id, long inc)
18190Sstevel@tonic-gate {
18200Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	if (lwp == NULL)
18230Sstevel@tonic-gate 		return;
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	switch (lwp_stat_id) {
18260Sstevel@tonic-gate 	case LWP_STAT_INBLK:
18270Sstevel@tonic-gate 		lwp->lwp_ru.inblock += inc;
18280Sstevel@tonic-gate 		break;
18290Sstevel@tonic-gate 	case LWP_STAT_OUBLK:
18300Sstevel@tonic-gate 		lwp->lwp_ru.oublock += inc;
18310Sstevel@tonic-gate 		break;
18320Sstevel@tonic-gate 	case LWP_STAT_MSGRCV:
18330Sstevel@tonic-gate 		lwp->lwp_ru.msgrcv += inc;
18340Sstevel@tonic-gate 		break;
18350Sstevel@tonic-gate 	case LWP_STAT_MSGSND:
18360Sstevel@tonic-gate 		lwp->lwp_ru.msgsnd += inc;
18370Sstevel@tonic-gate 		break;
18380Sstevel@tonic-gate 	default:
18390Sstevel@tonic-gate 		panic("lwp_stat_update: invalid lwp_stat_id 0x%x", lwp_stat_id);
18400Sstevel@tonic-gate 	}
18410Sstevel@tonic-gate }
1842