xref: /titanic_53/usr/src/uts/common/os/lwp.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/systm.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/thread.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/proc.h>
35*7c478bd9Sstevel@tonic-gate #include <sys/task.h>
36*7c478bd9Sstevel@tonic-gate #include <sys/project.h>
37*7c478bd9Sstevel@tonic-gate #include <sys/signal.h>
38*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
39*7c478bd9Sstevel@tonic-gate #include <sys/vmparam.h>
40*7c478bd9Sstevel@tonic-gate #include <sys/stack.h>
41*7c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
42*7c478bd9Sstevel@tonic-gate #include <sys/prsystm.h>
43*7c478bd9Sstevel@tonic-gate #include <sys/cpuvar.h>
44*7c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
45*7c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
46*7c478bd9Sstevel@tonic-gate #include <sys/door.h>
47*7c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
48*7c478bd9Sstevel@tonic-gate #include <sys/debug.h>
49*7c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
50*7c478bd9Sstevel@tonic-gate #include <sys/schedctl.h>
51*7c478bd9Sstevel@tonic-gate #include <sys/poll.h>
52*7c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
53*7c478bd9Sstevel@tonic-gate #include <sys/lwp_upimutex_impl.h>
54*7c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
55*7c478bd9Sstevel@tonic-gate #include <sys/lgrp.h>
56*7c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
57*7c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
58*7c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
59*7c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
60*7c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
61*7c478bd9Sstevel@tonic-gate 
62*7c478bd9Sstevel@tonic-gate void *segkp_lwp;		/* cookie for pool of segkp resources */
63*7c478bd9Sstevel@tonic-gate 
64*7c478bd9Sstevel@tonic-gate /*
65*7c478bd9Sstevel@tonic-gate  * Create a thread that appears to be stopped at sys_rtt.
66*7c478bd9Sstevel@tonic-gate  */
67*7c478bd9Sstevel@tonic-gate klwp_t *
68*7c478bd9Sstevel@tonic-gate lwp_create(void (*proc)(), caddr_t arg, size_t len, proc_t *p,
69*7c478bd9Sstevel@tonic-gate     int state, int pri, const k_sigset_t *smask, int cid, id_t lwpid)
70*7c478bd9Sstevel@tonic-gate {
71*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = NULL;
72*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
73*7c478bd9Sstevel@tonic-gate 	kthread_t *tx;
74*7c478bd9Sstevel@tonic-gate 	cpupart_t *oldpart = NULL;
75*7c478bd9Sstevel@tonic-gate 	size_t	stksize;
76*7c478bd9Sstevel@tonic-gate 	caddr_t lwpdata = NULL;
77*7c478bd9Sstevel@tonic-gate 	processorid_t	binding;
78*7c478bd9Sstevel@tonic-gate 	int err = 0;
79*7c478bd9Sstevel@tonic-gate 	kproject_t *oldkpj, *newkpj;
80*7c478bd9Sstevel@tonic-gate 	void *bufp = NULL;
81*7c478bd9Sstevel@tonic-gate 	klwp_t *curlwp = ttolwp(curthread);
82*7c478bd9Sstevel@tonic-gate 	lwpent_t *lep;
83*7c478bd9Sstevel@tonic-gate 	lwpdir_t *old_dir = NULL;
84*7c478bd9Sstevel@tonic-gate 	uint_t old_dirsz = 0;
85*7c478bd9Sstevel@tonic-gate 	lwpdir_t **old_hash = NULL;
86*7c478bd9Sstevel@tonic-gate 	uint_t old_hashsz = 0;
87*7c478bd9Sstevel@tonic-gate 	int i;
88*7c478bd9Sstevel@tonic-gate 	int rctlfail = 0;
89*7c478bd9Sstevel@tonic-gate 
90*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
91*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
92*7c478bd9Sstevel@tonic-gate 	/*
93*7c478bd9Sstevel@tonic-gate 	 * don't enforce rctl limits on system processes
94*7c478bd9Sstevel@tonic-gate 	 */
95*7c478bd9Sstevel@tonic-gate 	if (cid != syscid) {
96*7c478bd9Sstevel@tonic-gate 		if (p->p_task->tk_nlwps >= p->p_task->tk_nlwps_ctl)
97*7c478bd9Sstevel@tonic-gate 			if (rctl_test(rc_task_lwps, p->p_task->tk_rctls, p,
98*7c478bd9Sstevel@tonic-gate 			    1, 0) & RCT_DENY)
99*7c478bd9Sstevel@tonic-gate 				rctlfail = 1;
100*7c478bd9Sstevel@tonic-gate 		if (p->p_task->tk_proj->kpj_nlwps >=
101*7c478bd9Sstevel@tonic-gate 		    p->p_task->tk_proj->kpj_nlwps_ctl)
102*7c478bd9Sstevel@tonic-gate 			if (rctl_test(rc_project_nlwps,
103*7c478bd9Sstevel@tonic-gate 			    p->p_task->tk_proj->kpj_rctls, p, 1, 0)
104*7c478bd9Sstevel@tonic-gate 			    & RCT_DENY)
105*7c478bd9Sstevel@tonic-gate 				rctlfail = 1;
106*7c478bd9Sstevel@tonic-gate 		if (p->p_zone->zone_nlwps >= p->p_zone->zone_nlwps_ctl)
107*7c478bd9Sstevel@tonic-gate 			if (rctl_test(rc_zone_nlwps, p->p_zone->zone_rctls, p,
108*7c478bd9Sstevel@tonic-gate 			    1, 0) & RCT_DENY)
109*7c478bd9Sstevel@tonic-gate 				rctlfail = 1;
110*7c478bd9Sstevel@tonic-gate 	}
111*7c478bd9Sstevel@tonic-gate 	if (rctlfail) {
112*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_zone->zone_nlwps_lock);
113*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
114*7c478bd9Sstevel@tonic-gate 		return (NULL);
115*7c478bd9Sstevel@tonic-gate 	}
116*7c478bd9Sstevel@tonic-gate 	p->p_task->tk_nlwps++;
117*7c478bd9Sstevel@tonic-gate 	p->p_task->tk_proj->kpj_nlwps++;
118*7c478bd9Sstevel@tonic-gate 	p->p_zone->zone_nlwps++;
119*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
120*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 	if (curlwp == NULL || (stksize = curlwp->lwp_childstksz) == 0)
123*7c478bd9Sstevel@tonic-gate 		stksize = lwp_default_stksize;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	/*
126*7c478bd9Sstevel@tonic-gate 	 * Try to reclaim a <lwp,stack> from 'deathrow'
127*7c478bd9Sstevel@tonic-gate 	 */
128*7c478bd9Sstevel@tonic-gate 	if (stksize == lwp_default_stksize) {
129*7c478bd9Sstevel@tonic-gate 		if (lwp_reapcnt > 0) {
130*7c478bd9Sstevel@tonic-gate 			mutex_enter(&reaplock);
131*7c478bd9Sstevel@tonic-gate 			if ((t = lwp_deathrow) != NULL) {
132*7c478bd9Sstevel@tonic-gate 				ASSERT(t->t_swap);
133*7c478bd9Sstevel@tonic-gate 				lwp_deathrow = t->t_forw;
134*7c478bd9Sstevel@tonic-gate 				lwp_reapcnt--;
135*7c478bd9Sstevel@tonic-gate 				lwpdata = t->t_swap;
136*7c478bd9Sstevel@tonic-gate 				lwp = t->t_lwp;
137*7c478bd9Sstevel@tonic-gate 			}
138*7c478bd9Sstevel@tonic-gate 			mutex_exit(&reaplock);
139*7c478bd9Sstevel@tonic-gate 			if (t) {
140*7c478bd9Sstevel@tonic-gate 				t->t_swap = NULL;
141*7c478bd9Sstevel@tonic-gate 				lwp_stk_fini(t->t_lwp);
142*7c478bd9Sstevel@tonic-gate 				t->t_lwp = NULL;
143*7c478bd9Sstevel@tonic-gate 				t->t_forw = NULL;
144*7c478bd9Sstevel@tonic-gate 				thread_free(t);
145*7c478bd9Sstevel@tonic-gate 			}
146*7c478bd9Sstevel@tonic-gate 		}
147*7c478bd9Sstevel@tonic-gate 		if (lwpdata == NULL &&
148*7c478bd9Sstevel@tonic-gate 		    (lwpdata = (caddr_t)segkp_cache_get(segkp_lwp)) == NULL) {
149*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
150*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_zone->zone_nlwps_lock);
151*7c478bd9Sstevel@tonic-gate 			p->p_task->tk_nlwps--;
152*7c478bd9Sstevel@tonic-gate 			p->p_task->tk_proj->kpj_nlwps--;
153*7c478bd9Sstevel@tonic-gate 			p->p_zone->zone_nlwps--;
154*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_zone->zone_nlwps_lock);
155*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
156*7c478bd9Sstevel@tonic-gate 			return (NULL);
157*7c478bd9Sstevel@tonic-gate 		}
158*7c478bd9Sstevel@tonic-gate 	} else {
159*7c478bd9Sstevel@tonic-gate 		stksize = roundup(stksize, PAGESIZE);
160*7c478bd9Sstevel@tonic-gate 		if ((lwpdata = (caddr_t)segkp_get(segkp, stksize,
161*7c478bd9Sstevel@tonic-gate 		    (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED))) == NULL) {
162*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_lock);
163*7c478bd9Sstevel@tonic-gate 			mutex_enter(&p->p_zone->zone_nlwps_lock);
164*7c478bd9Sstevel@tonic-gate 			p->p_task->tk_nlwps--;
165*7c478bd9Sstevel@tonic-gate 			p->p_task->tk_proj->kpj_nlwps--;
166*7c478bd9Sstevel@tonic-gate 			p->p_zone->zone_nlwps--;
167*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_zone->zone_nlwps_lock);
168*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
169*7c478bd9Sstevel@tonic-gate 			return (NULL);
170*7c478bd9Sstevel@tonic-gate 		}
171*7c478bd9Sstevel@tonic-gate 	}
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	/*
174*7c478bd9Sstevel@tonic-gate 	 * Create a thread, initializing the stack pointer
175*7c478bd9Sstevel@tonic-gate 	 */
176*7c478bd9Sstevel@tonic-gate 	t = thread_create(lwpdata, stksize, NULL, NULL, 0, p, TS_STOPPED, pri);
177*7c478bd9Sstevel@tonic-gate 
178*7c478bd9Sstevel@tonic-gate 	t->t_swap = lwpdata;	/* Start of page-able data */
179*7c478bd9Sstevel@tonic-gate 	if (lwp == NULL)
180*7c478bd9Sstevel@tonic-gate 		lwp = kmem_cache_alloc(lwp_cache, KM_SLEEP);
181*7c478bd9Sstevel@tonic-gate 	bzero(lwp, sizeof (*lwp));
182*7c478bd9Sstevel@tonic-gate 	t->t_lwp = lwp;
183*7c478bd9Sstevel@tonic-gate 
184*7c478bd9Sstevel@tonic-gate 	t->t_hold = *smask;
185*7c478bd9Sstevel@tonic-gate 	lwp->lwp_thread = t;
186*7c478bd9Sstevel@tonic-gate 	lwp->lwp_procp = p;
187*7c478bd9Sstevel@tonic-gate 	lwp->lwp_sigaltstack.ss_flags = SS_DISABLE;
188*7c478bd9Sstevel@tonic-gate 	if (curlwp != NULL && curlwp->lwp_childstksz != 0)
189*7c478bd9Sstevel@tonic-gate 		lwp->lwp_childstksz = curlwp->lwp_childstksz;
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate 	t->t_stk = lwp_stk_init(lwp, t->t_stk);
192*7c478bd9Sstevel@tonic-gate 	thread_load(t, proc, arg, len);
193*7c478bd9Sstevel@tonic-gate 
194*7c478bd9Sstevel@tonic-gate 	/*
195*7c478bd9Sstevel@tonic-gate 	 * Allocate the SIGPROF buffer if ITIMER_REALPROF is in effect.
196*7c478bd9Sstevel@tonic-gate 	 */
197*7c478bd9Sstevel@tonic-gate 	if (timerisset(&p->p_rprof_timer.it_value))
198*7c478bd9Sstevel@tonic-gate 		t->t_rprof = kmem_zalloc(sizeof (struct rprof), KM_SLEEP);
199*7c478bd9Sstevel@tonic-gate 
200*7c478bd9Sstevel@tonic-gate 	if (cid != NOCLASS)
201*7c478bd9Sstevel@tonic-gate 		(void) CL_ALLOC(&bufp, cid, KM_SLEEP);
202*7c478bd9Sstevel@tonic-gate 
203*7c478bd9Sstevel@tonic-gate 	/*
204*7c478bd9Sstevel@tonic-gate 	 * Allocate an lwp directory entry for the new lwp.
205*7c478bd9Sstevel@tonic-gate 	 */
206*7c478bd9Sstevel@tonic-gate 	lep = kmem_zalloc(sizeof (*lep), KM_SLEEP);
207*7c478bd9Sstevel@tonic-gate 
208*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
209*7c478bd9Sstevel@tonic-gate grow:
210*7c478bd9Sstevel@tonic-gate 	/*
211*7c478bd9Sstevel@tonic-gate 	 * Grow the lwp (thread) directory and lwpid hash table if necessary.
212*7c478bd9Sstevel@tonic-gate 	 * A note on the growth algorithm:
213*7c478bd9Sstevel@tonic-gate 	 *	The new lwp directory size is computed as:
214*7c478bd9Sstevel@tonic-gate 	 *		new = 2 * old + 2
215*7c478bd9Sstevel@tonic-gate 	 *	Starting with an initial size of 2 (see exec_common()),
216*7c478bd9Sstevel@tonic-gate 	 *	this yields numbers that are a power of two minus 2:
217*7c478bd9Sstevel@tonic-gate 	 *		2, 6, 14, 30, 62, 126, 254, 510, 1022, ...
218*7c478bd9Sstevel@tonic-gate 	 *	The size of the lwpid hash table must be a power of two
219*7c478bd9Sstevel@tonic-gate 	 *	and must be commensurate in size with the lwp directory
220*7c478bd9Sstevel@tonic-gate 	 *	so that hash bucket chains remain short.  Therefore,
221*7c478bd9Sstevel@tonic-gate 	 *	the lwpid hash table size is computed as:
222*7c478bd9Sstevel@tonic-gate 	 *		hashsz = (dirsz + 2) / 2
223*7c478bd9Sstevel@tonic-gate 	 *	which leads to these hash table sizes corresponding to
224*7c478bd9Sstevel@tonic-gate 	 *	the above directory sizes:
225*7c478bd9Sstevel@tonic-gate 	 *		2, 4, 8, 16, 32, 64, 128, 256, 512, ...
226*7c478bd9Sstevel@tonic-gate 	 */
227*7c478bd9Sstevel@tonic-gate 	while (p->p_lwpfree == NULL) {
228*7c478bd9Sstevel@tonic-gate 		uint_t dirsz = p->p_lwpdir_sz;
229*7c478bd9Sstevel@tonic-gate 		uint_t new_dirsz;
230*7c478bd9Sstevel@tonic-gate 		uint_t new_hashsz;
231*7c478bd9Sstevel@tonic-gate 		lwpdir_t *new_dir;
232*7c478bd9Sstevel@tonic-gate 		lwpdir_t *ldp;
233*7c478bd9Sstevel@tonic-gate 		lwpdir_t **new_hash;
234*7c478bd9Sstevel@tonic-gate 
235*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
236*7c478bd9Sstevel@tonic-gate 
237*7c478bd9Sstevel@tonic-gate 		if (old_dir != NULL) {
238*7c478bd9Sstevel@tonic-gate 			kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
239*7c478bd9Sstevel@tonic-gate 			kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
240*7c478bd9Sstevel@tonic-gate 			old_dir = NULL;
241*7c478bd9Sstevel@tonic-gate 			old_dirsz = 0;
242*7c478bd9Sstevel@tonic-gate 			old_hash = NULL;
243*7c478bd9Sstevel@tonic-gate 			old_hashsz = 0;
244*7c478bd9Sstevel@tonic-gate 		}
245*7c478bd9Sstevel@tonic-gate 		new_dirsz = 2 * dirsz + 2;
246*7c478bd9Sstevel@tonic-gate 		new_dir = kmem_zalloc(new_dirsz * sizeof (lwpdir_t), KM_SLEEP);
247*7c478bd9Sstevel@tonic-gate 		for (ldp = new_dir, i = 1; i < new_dirsz; i++, ldp++)
248*7c478bd9Sstevel@tonic-gate 			ldp->ld_next = ldp + 1;
249*7c478bd9Sstevel@tonic-gate 		new_hashsz = (new_dirsz + 2) / 2;
250*7c478bd9Sstevel@tonic-gate 		new_hash = kmem_zalloc(new_hashsz * sizeof (lwpdir_t *),
251*7c478bd9Sstevel@tonic-gate 			KM_SLEEP);
252*7c478bd9Sstevel@tonic-gate 
253*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
254*7c478bd9Sstevel@tonic-gate 		if (p == curproc)
255*7c478bd9Sstevel@tonic-gate 			prbarrier(p);
256*7c478bd9Sstevel@tonic-gate 
257*7c478bd9Sstevel@tonic-gate 		if (dirsz != p->p_lwpdir_sz || p->p_lwpfree != NULL) {
258*7c478bd9Sstevel@tonic-gate 			/*
259*7c478bd9Sstevel@tonic-gate 			 * Someone else beat us to it or some lwp exited.
260*7c478bd9Sstevel@tonic-gate 			 * Set up to free our memory and take a lap.
261*7c478bd9Sstevel@tonic-gate 			 */
262*7c478bd9Sstevel@tonic-gate 			old_dir = new_dir;
263*7c478bd9Sstevel@tonic-gate 			old_dirsz = new_dirsz;
264*7c478bd9Sstevel@tonic-gate 			old_hash = new_hash;
265*7c478bd9Sstevel@tonic-gate 			old_hashsz = new_hashsz;
266*7c478bd9Sstevel@tonic-gate 		} else {
267*7c478bd9Sstevel@tonic-gate 			old_dir = p->p_lwpdir;
268*7c478bd9Sstevel@tonic-gate 			old_dirsz = p->p_lwpdir_sz;
269*7c478bd9Sstevel@tonic-gate 			old_hash = p->p_tidhash;
270*7c478bd9Sstevel@tonic-gate 			old_hashsz = p->p_tidhash_sz;
271*7c478bd9Sstevel@tonic-gate 			p->p_lwpdir = new_dir;
272*7c478bd9Sstevel@tonic-gate 			p->p_lwpfree = new_dir;
273*7c478bd9Sstevel@tonic-gate 			p->p_lwpdir_sz = new_dirsz;
274*7c478bd9Sstevel@tonic-gate 			p->p_tidhash = new_hash;
275*7c478bd9Sstevel@tonic-gate 			p->p_tidhash_sz = new_hashsz;
276*7c478bd9Sstevel@tonic-gate 			/*
277*7c478bd9Sstevel@tonic-gate 			 * We simply hash in all of the old directory entries.
278*7c478bd9Sstevel@tonic-gate 			 * This works because the old directory has no empty
279*7c478bd9Sstevel@tonic-gate 			 * slots and the new hash table starts out empty.
280*7c478bd9Sstevel@tonic-gate 			 * This reproduces the original directory ordering
281*7c478bd9Sstevel@tonic-gate 			 * (required for /proc directory semantics).
282*7c478bd9Sstevel@tonic-gate 			 */
283*7c478bd9Sstevel@tonic-gate 			for (ldp = old_dir, i = 0; i < dirsz; i++, ldp++)
284*7c478bd9Sstevel@tonic-gate 				lwp_hash_in(p, ldp->ld_entry);
285*7c478bd9Sstevel@tonic-gate 			/*
286*7c478bd9Sstevel@tonic-gate 			 * Defer freeing memory until we drop p->p_lock,
287*7c478bd9Sstevel@tonic-gate 			 */
288*7c478bd9Sstevel@tonic-gate 		}
289*7c478bd9Sstevel@tonic-gate 	}
290*7c478bd9Sstevel@tonic-gate 
291*7c478bd9Sstevel@tonic-gate 	/*
292*7c478bd9Sstevel@tonic-gate 	 * Block the process against /proc while we manipulate p->p_tlist,
293*7c478bd9Sstevel@tonic-gate 	 * unless lwp_create() was called by /proc for the PCAGENT operation.
294*7c478bd9Sstevel@tonic-gate 	 * We want to do this early enough so that we don't drop p->p_lock
295*7c478bd9Sstevel@tonic-gate 	 * until the thread is put on the p->p_tlist.
296*7c478bd9Sstevel@tonic-gate 	 */
297*7c478bd9Sstevel@tonic-gate 	if (p == curproc) {
298*7c478bd9Sstevel@tonic-gate 		prbarrier(p);
299*7c478bd9Sstevel@tonic-gate 		/*
300*7c478bd9Sstevel@tonic-gate 		 * If the current lwp has been requested to stop, do so now.
301*7c478bd9Sstevel@tonic-gate 		 * Otherwise we have a race condition between /proc attempting
302*7c478bd9Sstevel@tonic-gate 		 * to stop the process and this thread creating a new lwp
303*7c478bd9Sstevel@tonic-gate 		 * that was not seen when the /proc PCSTOP request was issued.
304*7c478bd9Sstevel@tonic-gate 		 * We rely on stop() to call prbarrier(p) before returning.
305*7c478bd9Sstevel@tonic-gate 		 */
306*7c478bd9Sstevel@tonic-gate 		while ((curthread->t_proc_flag & TP_PRSTOP) &&
307*7c478bd9Sstevel@tonic-gate 		    !ttolwp(curthread)->lwp_nostop)
308*7c478bd9Sstevel@tonic-gate 			stop(PR_REQUESTED, 0);
309*7c478bd9Sstevel@tonic-gate 
310*7c478bd9Sstevel@tonic-gate 		/*
311*7c478bd9Sstevel@tonic-gate 		 * If process is exiting, there could be a race between
312*7c478bd9Sstevel@tonic-gate 		 * the agent lwp creation and the new lwp currently being
313*7c478bd9Sstevel@tonic-gate 		 * created. So to prevent this race lwp creation is failed
314*7c478bd9Sstevel@tonic-gate 		 * if the process is exiting.
315*7c478bd9Sstevel@tonic-gate 		 */
316*7c478bd9Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS|SKILLED)) {
317*7c478bd9Sstevel@tonic-gate 			err = 1;
318*7c478bd9Sstevel@tonic-gate 			goto error;
319*7c478bd9Sstevel@tonic-gate 		}
320*7c478bd9Sstevel@tonic-gate 
321*7c478bd9Sstevel@tonic-gate 		/*
322*7c478bd9Sstevel@tonic-gate 		 * Since we might have dropped p->p_lock, the
323*7c478bd9Sstevel@tonic-gate 		 * lwp directory free list might have changed.
324*7c478bd9Sstevel@tonic-gate 		 */
325*7c478bd9Sstevel@tonic-gate 		if (p->p_lwpfree == NULL)
326*7c478bd9Sstevel@tonic-gate 			goto grow;
327*7c478bd9Sstevel@tonic-gate 	}
328*7c478bd9Sstevel@tonic-gate 
329*7c478bd9Sstevel@tonic-gate 	kpreempt_disable();	/* can't grab cpu_lock here */
330*7c478bd9Sstevel@tonic-gate 
331*7c478bd9Sstevel@tonic-gate 	/*
332*7c478bd9Sstevel@tonic-gate 	 * Inherit processor and processor set bindings from curthread,
333*7c478bd9Sstevel@tonic-gate 	 * unless we're creating a new kernel process, in which case
334*7c478bd9Sstevel@tonic-gate 	 * clear all bindings.
335*7c478bd9Sstevel@tonic-gate 	 */
336*7c478bd9Sstevel@tonic-gate 	if (cid == syscid) {
337*7c478bd9Sstevel@tonic-gate 		t->t_bind_cpu = binding = PBIND_NONE;
338*7c478bd9Sstevel@tonic-gate 		t->t_cpupart = oldpart = &cp_default;
339*7c478bd9Sstevel@tonic-gate 		t->t_bind_pset = PS_NONE;
340*7c478bd9Sstevel@tonic-gate 	} else {
341*7c478bd9Sstevel@tonic-gate 		binding = curthread->t_bind_cpu;
342*7c478bd9Sstevel@tonic-gate 		t->t_bind_cpu = binding;
343*7c478bd9Sstevel@tonic-gate 		oldpart = t->t_cpupart;
344*7c478bd9Sstevel@tonic-gate 		t->t_cpupart = curthread->t_cpupart;
345*7c478bd9Sstevel@tonic-gate 		t->t_bind_pset = curthread->t_bind_pset;
346*7c478bd9Sstevel@tonic-gate 	}
347*7c478bd9Sstevel@tonic-gate 
348*7c478bd9Sstevel@tonic-gate 	/*
349*7c478bd9Sstevel@tonic-gate 	 * thread_create() initializes this thread's home lgroup to the root.
350*7c478bd9Sstevel@tonic-gate 	 * Choose a more suitable lgroup, since this thread is associated
351*7c478bd9Sstevel@tonic-gate 	 * with an lwp.
352*7c478bd9Sstevel@tonic-gate 	 */
353*7c478bd9Sstevel@tonic-gate 	ASSERT(oldpart != NULL);
354*7c478bd9Sstevel@tonic-gate 	if (binding != PBIND_NONE && t->t_affinitycnt == 0) {
355*7c478bd9Sstevel@tonic-gate 		t->t_bound_cpu = cpu[binding];
356*7c478bd9Sstevel@tonic-gate 		if (t->t_lpl != t->t_bound_cpu->cpu_lpl)
357*7c478bd9Sstevel@tonic-gate 			lgrp_move_thread(t, t->t_bound_cpu->cpu_lpl, 1);
358*7c478bd9Sstevel@tonic-gate 	} else {
359*7c478bd9Sstevel@tonic-gate 		lgrp_move_thread(t, lgrp_choose(t, t->t_cpupart), 1);
360*7c478bd9Sstevel@tonic-gate 	}
361*7c478bd9Sstevel@tonic-gate 
362*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
363*7c478bd9Sstevel@tonic-gate 
364*7c478bd9Sstevel@tonic-gate 	/*
365*7c478bd9Sstevel@tonic-gate 	 * make sure lpl points to our own partition
366*7c478bd9Sstevel@tonic-gate 	 */
367*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_lpl >= t->t_cpupart->cp_lgrploads);
368*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_lpl < t->t_cpupart->cp_lgrploads +
369*7c478bd9Sstevel@tonic-gate 	    t->t_cpupart->cp_nlgrploads);
370*7c478bd9Sstevel@tonic-gate 
371*7c478bd9Sstevel@tonic-gate 	/*
372*7c478bd9Sstevel@tonic-gate 	 * If we're creating a new process, then inherit the project from our
373*7c478bd9Sstevel@tonic-gate 	 * parent. If we're only creating an additional lwp then use the
374*7c478bd9Sstevel@tonic-gate 	 * project pointer of the target process.
375*7c478bd9Sstevel@tonic-gate 	 */
376*7c478bd9Sstevel@tonic-gate 	if (p->p_task == NULL)
377*7c478bd9Sstevel@tonic-gate 		newkpj = ttoproj(curthread);
378*7c478bd9Sstevel@tonic-gate 	else
379*7c478bd9Sstevel@tonic-gate 		newkpj = p->p_task->tk_proj;
380*7c478bd9Sstevel@tonic-gate 
381*7c478bd9Sstevel@tonic-gate 	/*
382*7c478bd9Sstevel@tonic-gate 	 * It is safe to point the thread to the new project without holding it
383*7c478bd9Sstevel@tonic-gate 	 * since we're holding the target process' p_lock here and therefore
384*7c478bd9Sstevel@tonic-gate 	 * we're guaranteed that it will not move to another project.
385*7c478bd9Sstevel@tonic-gate 	 */
386*7c478bd9Sstevel@tonic-gate 	oldkpj = ttoproj(t);
387*7c478bd9Sstevel@tonic-gate 	if (newkpj != oldkpj) {
388*7c478bd9Sstevel@tonic-gate 		t->t_proj = newkpj;
389*7c478bd9Sstevel@tonic-gate 		(void) project_hold(newkpj);
390*7c478bd9Sstevel@tonic-gate 		project_rele(oldkpj);
391*7c478bd9Sstevel@tonic-gate 	}
392*7c478bd9Sstevel@tonic-gate 
393*7c478bd9Sstevel@tonic-gate 	if (cid != NOCLASS) {
394*7c478bd9Sstevel@tonic-gate 		/*
395*7c478bd9Sstevel@tonic-gate 		 * If the lwp is being created in the current process
396*7c478bd9Sstevel@tonic-gate 		 * and matches the current thread's scheduling class,
397*7c478bd9Sstevel@tonic-gate 		 * we should propagate the current thread's scheduling
398*7c478bd9Sstevel@tonic-gate 		 * parameters by calling CL_FORK.  Otherwise just use
399*7c478bd9Sstevel@tonic-gate 		 * the defaults by calling CL_ENTERCLASS.
400*7c478bd9Sstevel@tonic-gate 		 */
401*7c478bd9Sstevel@tonic-gate 		if (p != curproc || curthread->t_cid != cid) {
402*7c478bd9Sstevel@tonic-gate 			err = CL_ENTERCLASS(t, cid, NULL, NULL, bufp);
403*7c478bd9Sstevel@tonic-gate 			t->t_pri = pri;	/* CL_ENTERCLASS may have changed it */
404*7c478bd9Sstevel@tonic-gate 		} else {
405*7c478bd9Sstevel@tonic-gate 			t->t_clfuncs = &(sclass[cid].cl_funcs->thread);
406*7c478bd9Sstevel@tonic-gate 			err = CL_FORK(curthread, t, bufp);
407*7c478bd9Sstevel@tonic-gate 			t->t_cid = cid;
408*7c478bd9Sstevel@tonic-gate 		}
409*7c478bd9Sstevel@tonic-gate 		if (err)
410*7c478bd9Sstevel@tonic-gate 			goto error;
411*7c478bd9Sstevel@tonic-gate 		else
412*7c478bd9Sstevel@tonic-gate 			bufp = NULL;
413*7c478bd9Sstevel@tonic-gate 	}
414*7c478bd9Sstevel@tonic-gate 
415*7c478bd9Sstevel@tonic-gate 	/*
416*7c478bd9Sstevel@tonic-gate 	 * If we were given an lwpid then use it, else allocate one.
417*7c478bd9Sstevel@tonic-gate 	 */
418*7c478bd9Sstevel@tonic-gate 	if (lwpid != 0)
419*7c478bd9Sstevel@tonic-gate 		t->t_tid = lwpid;
420*7c478bd9Sstevel@tonic-gate 	else {
421*7c478bd9Sstevel@tonic-gate 		/*
422*7c478bd9Sstevel@tonic-gate 		 * lwp/thread id 0 is never valid; reserved for special checks.
423*7c478bd9Sstevel@tonic-gate 		 * lwp/thread id 1 is reserved for the main thread.
424*7c478bd9Sstevel@tonic-gate 		 * Start again at 2 when INT_MAX has been reached
425*7c478bd9Sstevel@tonic-gate 		 * (id_t is a signed 32-bit integer).
426*7c478bd9Sstevel@tonic-gate 		 */
427*7c478bd9Sstevel@tonic-gate 		id_t prev_id = p->p_lwpid;	/* last allocated tid */
428*7c478bd9Sstevel@tonic-gate 
429*7c478bd9Sstevel@tonic-gate 		do {			/* avoid lwpid duplication */
430*7c478bd9Sstevel@tonic-gate 			if (p->p_lwpid == INT_MAX) {
431*7c478bd9Sstevel@tonic-gate 				p->p_flag |= SLWPWRAP;
432*7c478bd9Sstevel@tonic-gate 				p->p_lwpid = 1;
433*7c478bd9Sstevel@tonic-gate 			}
434*7c478bd9Sstevel@tonic-gate 			if ((t->t_tid = ++p->p_lwpid) == prev_id) {
435*7c478bd9Sstevel@tonic-gate 				/*
436*7c478bd9Sstevel@tonic-gate 				 * All lwpids are allocated; fail the request.
437*7c478bd9Sstevel@tonic-gate 				 */
438*7c478bd9Sstevel@tonic-gate 				err = 1;
439*7c478bd9Sstevel@tonic-gate 				goto error;
440*7c478bd9Sstevel@tonic-gate 			}
441*7c478bd9Sstevel@tonic-gate 			/*
442*7c478bd9Sstevel@tonic-gate 			 * We only need to worry about colliding with an id
443*7c478bd9Sstevel@tonic-gate 			 * that's already in use if this process has
444*7c478bd9Sstevel@tonic-gate 			 * cycled through all available lwp ids.
445*7c478bd9Sstevel@tonic-gate 			 */
446*7c478bd9Sstevel@tonic-gate 			if ((p->p_flag & SLWPWRAP) == 0)
447*7c478bd9Sstevel@tonic-gate 				break;
448*7c478bd9Sstevel@tonic-gate 		} while (lwp_hash_lookup(p, t->t_tid) != NULL);
449*7c478bd9Sstevel@tonic-gate 	}
450*7c478bd9Sstevel@tonic-gate 	p->p_lwpcnt++;
451*7c478bd9Sstevel@tonic-gate 	t->t_waitfor = -1;
452*7c478bd9Sstevel@tonic-gate 
453*7c478bd9Sstevel@tonic-gate 	/*
454*7c478bd9Sstevel@tonic-gate 	 * Turn microstate accounting on for thread if on for process.
455*7c478bd9Sstevel@tonic-gate 	 */
456*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SMSACCT)
457*7c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_MSACCT;
458*7c478bd9Sstevel@tonic-gate 
459*7c478bd9Sstevel@tonic-gate 	/*
460*7c478bd9Sstevel@tonic-gate 	 * If the process has watchpoints, mark the new thread as such.
461*7c478bd9Sstevel@tonic-gate 	 */
462*7c478bd9Sstevel@tonic-gate 	if (pr_watch_active(p))
463*7c478bd9Sstevel@tonic-gate 		watch_enable(t);
464*7c478bd9Sstevel@tonic-gate 
465*7c478bd9Sstevel@tonic-gate 	/*
466*7c478bd9Sstevel@tonic-gate 	 * The lwp is being created in the stopped state.
467*7c478bd9Sstevel@tonic-gate 	 * We set all the necessary flags to indicate that fact here.
468*7c478bd9Sstevel@tonic-gate 	 * We omit the TS_CREATE flag from t_schedflag so that the lwp
469*7c478bd9Sstevel@tonic-gate 	 * cannot be set running until the caller is finished with it,
470*7c478bd9Sstevel@tonic-gate 	 * even if lwp_continue() is called on it after we drop p->p_lock.
471*7c478bd9Sstevel@tonic-gate 	 * When the caller is finished with the newly-created lwp,
472*7c478bd9Sstevel@tonic-gate 	 * the caller must call lwp_create_done() to allow the lwp
473*7c478bd9Sstevel@tonic-gate 	 * to be set running.  If the TP_HOLDLWP is left set, the
474*7c478bd9Sstevel@tonic-gate 	 * lwp will suspend itself after reaching system call exit.
475*7c478bd9Sstevel@tonic-gate 	 */
476*7c478bd9Sstevel@tonic-gate 	init_mstate(t, LMS_STOPPED);
477*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_HOLDLWP;
478*7c478bd9Sstevel@tonic-gate 	t->t_schedflag |= (TS_ALLSTART & ~(TS_CSTART | TS_CREATE));
479*7c478bd9Sstevel@tonic-gate 	t->t_whystop = PR_SUSPENDED;
480*7c478bd9Sstevel@tonic-gate 	t->t_whatstop = SUSPEND_NORMAL;
481*7c478bd9Sstevel@tonic-gate 	t->t_sig_check = 1;	/* ensure that TP_HOLDLWP is honored */
482*7c478bd9Sstevel@tonic-gate 
483*7c478bd9Sstevel@tonic-gate 	/*
484*7c478bd9Sstevel@tonic-gate 	 * Set system call processing flags in case tracing or profiling
485*7c478bd9Sstevel@tonic-gate 	 * is set.  The first system call will evaluate these and turn
486*7c478bd9Sstevel@tonic-gate 	 * them off if they aren't needed.
487*7c478bd9Sstevel@tonic-gate 	 */
488*7c478bd9Sstevel@tonic-gate 	t->t_pre_sys = 1;
489*7c478bd9Sstevel@tonic-gate 	t->t_post_sys = 1;
490*7c478bd9Sstevel@tonic-gate 
491*7c478bd9Sstevel@tonic-gate 	/*
492*7c478bd9Sstevel@tonic-gate 	 * Insert the new thread into the list of all threads.
493*7c478bd9Sstevel@tonic-gate 	 */
494*7c478bd9Sstevel@tonic-gate 	if ((tx = p->p_tlist) == NULL) {
495*7c478bd9Sstevel@tonic-gate 		t->t_back = t;
496*7c478bd9Sstevel@tonic-gate 		t->t_forw = t;
497*7c478bd9Sstevel@tonic-gate 		p->p_tlist = t;
498*7c478bd9Sstevel@tonic-gate 	} else {
499*7c478bd9Sstevel@tonic-gate 		t->t_forw = tx;
500*7c478bd9Sstevel@tonic-gate 		t->t_back = tx->t_back;
501*7c478bd9Sstevel@tonic-gate 		tx->t_back->t_forw = t;
502*7c478bd9Sstevel@tonic-gate 		tx->t_back = t;
503*7c478bd9Sstevel@tonic-gate 	}
504*7c478bd9Sstevel@tonic-gate 
505*7c478bd9Sstevel@tonic-gate 	/*
506*7c478bd9Sstevel@tonic-gate 	 * Insert the new lwp into an lwp directory slot position
507*7c478bd9Sstevel@tonic-gate 	 * and into the lwpid hash table.
508*7c478bd9Sstevel@tonic-gate 	 */
509*7c478bd9Sstevel@tonic-gate 	lep->le_thread = t;
510*7c478bd9Sstevel@tonic-gate 	lep->le_lwpid = t->t_tid;
511*7c478bd9Sstevel@tonic-gate 	lep->le_start = t->t_start;
512*7c478bd9Sstevel@tonic-gate 	lwp_hash_in(p, lep);
513*7c478bd9Sstevel@tonic-gate 
514*7c478bd9Sstevel@tonic-gate 	if (state == TS_RUN) {
515*7c478bd9Sstevel@tonic-gate 		/*
516*7c478bd9Sstevel@tonic-gate 		 * We set the new lwp running immediately.
517*7c478bd9Sstevel@tonic-gate 		 */
518*7c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_HOLDLWP;
519*7c478bd9Sstevel@tonic-gate 		lwp_create_done(t);
520*7c478bd9Sstevel@tonic-gate 	}
521*7c478bd9Sstevel@tonic-gate 
522*7c478bd9Sstevel@tonic-gate error:
523*7c478bd9Sstevel@tonic-gate 	if (err) {
524*7c478bd9Sstevel@tonic-gate 		/*
525*7c478bd9Sstevel@tonic-gate 		 * We have failed to create an lwp, so decrement the number
526*7c478bd9Sstevel@tonic-gate 		 * of lwps in the task and let the lgroup load averages know
527*7c478bd9Sstevel@tonic-gate 		 * that this thread isn't going to show up.
528*7c478bd9Sstevel@tonic-gate 		 */
529*7c478bd9Sstevel@tonic-gate 		kpreempt_disable();
530*7c478bd9Sstevel@tonic-gate 		lgrp_move_thread(t, NULL, 1);
531*7c478bd9Sstevel@tonic-gate 		kpreempt_enable();
532*7c478bd9Sstevel@tonic-gate 
533*7c478bd9Sstevel@tonic-gate 		ASSERT(MUTEX_HELD(&p->p_lock));
534*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_zone->zone_nlwps_lock);
535*7c478bd9Sstevel@tonic-gate 		p->p_task->tk_nlwps--;
536*7c478bd9Sstevel@tonic-gate 		p->p_task->tk_proj->kpj_nlwps--;
537*7c478bd9Sstevel@tonic-gate 		p->p_zone->zone_nlwps--;
538*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_zone->zone_nlwps_lock);
539*7c478bd9Sstevel@tonic-gate 		if (cid != NOCLASS && bufp != NULL)
540*7c478bd9Sstevel@tonic-gate 			CL_FREE(cid, bufp);
541*7c478bd9Sstevel@tonic-gate 
542*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
543*7c478bd9Sstevel@tonic-gate 		t->t_state = TS_FREE;
544*7c478bd9Sstevel@tonic-gate 		thread_rele(t);
545*7c478bd9Sstevel@tonic-gate 
546*7c478bd9Sstevel@tonic-gate 		/*
547*7c478bd9Sstevel@tonic-gate 		 * We need to remove t from the list of all threads
548*7c478bd9Sstevel@tonic-gate 		 * because thread_exit()/lwp_exit() isn't called on t.
549*7c478bd9Sstevel@tonic-gate 		 */
550*7c478bd9Sstevel@tonic-gate 		mutex_enter(&pidlock);
551*7c478bd9Sstevel@tonic-gate 		ASSERT(t != t->t_next);		/* t0 never exits */
552*7c478bd9Sstevel@tonic-gate 		t->t_next->t_prev = t->t_prev;
553*7c478bd9Sstevel@tonic-gate 		t->t_prev->t_next = t->t_next;
554*7c478bd9Sstevel@tonic-gate 		mutex_exit(&pidlock);
555*7c478bd9Sstevel@tonic-gate 
556*7c478bd9Sstevel@tonic-gate 		thread_free(t);
557*7c478bd9Sstevel@tonic-gate 		kmem_free(lep, sizeof (*lep));
558*7c478bd9Sstevel@tonic-gate 		lwp = NULL;
559*7c478bd9Sstevel@tonic-gate 	} else {
560*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
561*7c478bd9Sstevel@tonic-gate 	}
562*7c478bd9Sstevel@tonic-gate 
563*7c478bd9Sstevel@tonic-gate 	if (old_dir != NULL) {
564*7c478bd9Sstevel@tonic-gate 		kmem_free(old_dir, old_dirsz * sizeof (*old_dir));
565*7c478bd9Sstevel@tonic-gate 		kmem_free(old_hash, old_hashsz * sizeof (*old_hash));
566*7c478bd9Sstevel@tonic-gate 	}
567*7c478bd9Sstevel@tonic-gate 
568*7c478bd9Sstevel@tonic-gate 	DTRACE_PROC1(lwp__create, kthread_t *, t);
569*7c478bd9Sstevel@tonic-gate 	return (lwp);
570*7c478bd9Sstevel@tonic-gate }
571*7c478bd9Sstevel@tonic-gate 
572*7c478bd9Sstevel@tonic-gate /*
573*7c478bd9Sstevel@tonic-gate  * lwp_create_done() is called by the caller of lwp_create() to set the
574*7c478bd9Sstevel@tonic-gate  * newly-created lwp running after the caller has finished manipulating it.
575*7c478bd9Sstevel@tonic-gate  */
576*7c478bd9Sstevel@tonic-gate void
577*7c478bd9Sstevel@tonic-gate lwp_create_done(kthread_t *t)
578*7c478bd9Sstevel@tonic-gate {
579*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
580*7c478bd9Sstevel@tonic-gate 
581*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
582*7c478bd9Sstevel@tonic-gate 
583*7c478bd9Sstevel@tonic-gate 	/*
584*7c478bd9Sstevel@tonic-gate 	 * We set the TS_CREATE and TS_CSTART flags and call setrun_locked().
585*7c478bd9Sstevel@tonic-gate 	 * (The absence of the TS_CREATE flag prevents the lwp from running
586*7c478bd9Sstevel@tonic-gate 	 * until we are finished with it, even if lwp_continue() is called on
587*7c478bd9Sstevel@tonic-gate 	 * it by some other lwp in the process or elsewhere in the kernel.)
588*7c478bd9Sstevel@tonic-gate 	 */
589*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
590*7c478bd9Sstevel@tonic-gate 	ASSERT(t->t_state == TS_STOPPED && !(t->t_schedflag & TS_CREATE));
591*7c478bd9Sstevel@tonic-gate 	/*
592*7c478bd9Sstevel@tonic-gate 	 * If TS_CSTART is set, lwp_continue(t) has been called and
593*7c478bd9Sstevel@tonic-gate 	 * has already incremented p_lwprcnt; avoid doing this twice.
594*7c478bd9Sstevel@tonic-gate 	 */
595*7c478bd9Sstevel@tonic-gate 	if (!(t->t_schedflag & TS_CSTART))
596*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
597*7c478bd9Sstevel@tonic-gate 	t->t_schedflag |= (TS_CSTART | TS_CREATE);
598*7c478bd9Sstevel@tonic-gate 	setrun_locked(t);
599*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
600*7c478bd9Sstevel@tonic-gate }
601*7c478bd9Sstevel@tonic-gate 
602*7c478bd9Sstevel@tonic-gate /*
603*7c478bd9Sstevel@tonic-gate  * Copy an LWP's active templates, and clear the latest contracts.
604*7c478bd9Sstevel@tonic-gate  */
605*7c478bd9Sstevel@tonic-gate void
606*7c478bd9Sstevel@tonic-gate lwp_ctmpl_copy(klwp_t *dst, klwp_t *src)
607*7c478bd9Sstevel@tonic-gate {
608*7c478bd9Sstevel@tonic-gate 	int i;
609*7c478bd9Sstevel@tonic-gate 
610*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ct_ntypes; i++) {
611*7c478bd9Sstevel@tonic-gate 		dst->lwp_ct_active[i] = ctmpl_dup(src->lwp_ct_active[i]);
612*7c478bd9Sstevel@tonic-gate 		dst->lwp_ct_latest[i] = NULL;
613*7c478bd9Sstevel@tonic-gate 	}
614*7c478bd9Sstevel@tonic-gate }
615*7c478bd9Sstevel@tonic-gate 
616*7c478bd9Sstevel@tonic-gate /*
617*7c478bd9Sstevel@tonic-gate  * Clear an LWP's contract template state.
618*7c478bd9Sstevel@tonic-gate  */
619*7c478bd9Sstevel@tonic-gate void
620*7c478bd9Sstevel@tonic-gate lwp_ctmpl_clear(klwp_t *lwp)
621*7c478bd9Sstevel@tonic-gate {
622*7c478bd9Sstevel@tonic-gate 	ct_template_t *tmpl;
623*7c478bd9Sstevel@tonic-gate 	int i;
624*7c478bd9Sstevel@tonic-gate 
625*7c478bd9Sstevel@tonic-gate 	for (i = 0; i < ct_ntypes; i++) {
626*7c478bd9Sstevel@tonic-gate 		if ((tmpl = lwp->lwp_ct_active[i]) != NULL) {
627*7c478bd9Sstevel@tonic-gate 			ctmpl_free(tmpl);
628*7c478bd9Sstevel@tonic-gate 			lwp->lwp_ct_active[i] = NULL;
629*7c478bd9Sstevel@tonic-gate 		}
630*7c478bd9Sstevel@tonic-gate 
631*7c478bd9Sstevel@tonic-gate 		if (lwp->lwp_ct_latest[i] != NULL) {
632*7c478bd9Sstevel@tonic-gate 			contract_rele(lwp->lwp_ct_latest[i]);
633*7c478bd9Sstevel@tonic-gate 			lwp->lwp_ct_latest[i] = NULL;
634*7c478bd9Sstevel@tonic-gate 		}
635*7c478bd9Sstevel@tonic-gate 	}
636*7c478bd9Sstevel@tonic-gate }
637*7c478bd9Sstevel@tonic-gate 
638*7c478bd9Sstevel@tonic-gate /*
639*7c478bd9Sstevel@tonic-gate  * Individual lwp exit.
640*7c478bd9Sstevel@tonic-gate  * If this is the last lwp, exit the whole process.
641*7c478bd9Sstevel@tonic-gate  */
642*7c478bd9Sstevel@tonic-gate void
643*7c478bd9Sstevel@tonic-gate lwp_exit(void)
644*7c478bd9Sstevel@tonic-gate {
645*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
646*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(t);
647*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
648*7c478bd9Sstevel@tonic-gate 
649*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
650*7c478bd9Sstevel@tonic-gate 
651*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
652*7c478bd9Sstevel@tonic-gate 
653*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
654*7c478bd9Sstevel@tonic-gate 	/*
655*7c478bd9Sstevel@tonic-gate 	 * Ensure that the user stack is fully abandoned..
656*7c478bd9Sstevel@tonic-gate 	 */
657*7c478bd9Sstevel@tonic-gate 	trash_user_windows();
658*7c478bd9Sstevel@tonic-gate #endif
659*7c478bd9Sstevel@tonic-gate 
660*7c478bd9Sstevel@tonic-gate 	tsd_exit();			/* free thread specific data */
661*7c478bd9Sstevel@tonic-gate 
662*7c478bd9Sstevel@tonic-gate 	kcpc_passivate();		/* Clean up performance counter state */
663*7c478bd9Sstevel@tonic-gate 
664*7c478bd9Sstevel@tonic-gate 	pollcleanup();
665*7c478bd9Sstevel@tonic-gate 
666*7c478bd9Sstevel@tonic-gate 	if (t->t_door)
667*7c478bd9Sstevel@tonic-gate 		door_slam();
668*7c478bd9Sstevel@tonic-gate 
669*7c478bd9Sstevel@tonic-gate 	if (t->t_schedctl != NULL)
670*7c478bd9Sstevel@tonic-gate 		schedctl_lwp_cleanup(t);
671*7c478bd9Sstevel@tonic-gate 
672*7c478bd9Sstevel@tonic-gate 	if (t->t_upimutex != NULL)
673*7c478bd9Sstevel@tonic-gate 		upimutex_cleanup();
674*7c478bd9Sstevel@tonic-gate 
675*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
676*7c478bd9Sstevel@tonic-gate 	lwp_cleanup();
677*7c478bd9Sstevel@tonic-gate 
678*7c478bd9Sstevel@tonic-gate 	/*
679*7c478bd9Sstevel@tonic-gate 	 * When this process is dumping core, its lwps are held here
680*7c478bd9Sstevel@tonic-gate 	 * until the core dump is finished. Then exitlwps() is called
681*7c478bd9Sstevel@tonic-gate 	 * again to release these lwps so that they can finish exiting.
682*7c478bd9Sstevel@tonic-gate 	 */
683*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SCOREDUMP)
684*7c478bd9Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
685*7c478bd9Sstevel@tonic-gate 
686*7c478bd9Sstevel@tonic-gate 	/*
687*7c478bd9Sstevel@tonic-gate 	 * Block the process against /proc now that we have really acquired
688*7c478bd9Sstevel@tonic-gate 	 * p->p_lock (to decrement p_lwpcnt and manipulate p_tlist at least).
689*7c478bd9Sstevel@tonic-gate 	 */
690*7c478bd9Sstevel@tonic-gate 	prbarrier(p);
691*7c478bd9Sstevel@tonic-gate 
692*7c478bd9Sstevel@tonic-gate 	/*
693*7c478bd9Sstevel@tonic-gate 	 * Call proc_exit() if this is the last non-daemon lwp in the process.
694*7c478bd9Sstevel@tonic-gate 	 */
695*7c478bd9Sstevel@tonic-gate 	if (!(t->t_proc_flag & TP_DAEMON) &&
696*7c478bd9Sstevel@tonic-gate 	    p->p_lwpcnt == p->p_lwpdaemon + 1) {
697*7c478bd9Sstevel@tonic-gate 		int why, what;
698*7c478bd9Sstevel@tonic-gate 
699*7c478bd9Sstevel@tonic-gate 		if (p->p_flag & SEXITLWPS) {
700*7c478bd9Sstevel@tonic-gate 			why = CLD_KILLED;
701*7c478bd9Sstevel@tonic-gate 			what = SIGKILL;
702*7c478bd9Sstevel@tonic-gate 		} else {
703*7c478bd9Sstevel@tonic-gate 			why = CLD_EXITED;
704*7c478bd9Sstevel@tonic-gate 			what = 0;
705*7c478bd9Sstevel@tonic-gate 		}
706*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
707*7c478bd9Sstevel@tonic-gate 		if (proc_exit(why, what) == 0) {
708*7c478bd9Sstevel@tonic-gate 			/* Restarting init. */
709*7c478bd9Sstevel@tonic-gate 			return;
710*7c478bd9Sstevel@tonic-gate 		}
711*7c478bd9Sstevel@tonic-gate 
712*7c478bd9Sstevel@tonic-gate 		/*
713*7c478bd9Sstevel@tonic-gate 		 * proc_exit() returns a non-zero value when some other
714*7c478bd9Sstevel@tonic-gate 		 * lwp got there first.  We just have to continue in
715*7c478bd9Sstevel@tonic-gate 		 * lwp_exit().
716*7c478bd9Sstevel@tonic-gate 		 */
717*7c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
718*7c478bd9Sstevel@tonic-gate 		ASSERT(curproc->p_flag & SEXITLWPS);
719*7c478bd9Sstevel@tonic-gate 		prbarrier(p);
720*7c478bd9Sstevel@tonic-gate 	}
721*7c478bd9Sstevel@tonic-gate 
722*7c478bd9Sstevel@tonic-gate 	DTRACE_PROC(lwp__exit);
723*7c478bd9Sstevel@tonic-gate 
724*7c478bd9Sstevel@tonic-gate 	/*
725*7c478bd9Sstevel@tonic-gate 	 * If the lwp is a detached lwp or if the process is exiting,
726*7c478bd9Sstevel@tonic-gate 	 * remove (lwp_hash_out()) the lwp from the lwp directory.
727*7c478bd9Sstevel@tonic-gate 	 * Otherwise null out the lwp's le_thread pointer in the lwp
728*7c478bd9Sstevel@tonic-gate 	 * directory so that other threads will see it as a zombie lwp.
729*7c478bd9Sstevel@tonic-gate 	 */
730*7c478bd9Sstevel@tonic-gate 	prlwpexit(t);		/* notify /proc */
731*7c478bd9Sstevel@tonic-gate 	if (!(t->t_proc_flag & TP_TWAIT) || (p->p_flag & SEXITLWPS))
732*7c478bd9Sstevel@tonic-gate 		lwp_hash_out(p, t->t_tid);
733*7c478bd9Sstevel@tonic-gate 	else {
734*7c478bd9Sstevel@tonic-gate 		ASSERT(!(t->t_proc_flag & TP_DAEMON));
735*7c478bd9Sstevel@tonic-gate 		p->p_lwpdir[t->t_dslot].ld_entry->le_thread = NULL;
736*7c478bd9Sstevel@tonic-gate 		p->p_zombcnt++;
737*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&p->p_lwpexit);
738*7c478bd9Sstevel@tonic-gate 	}
739*7c478bd9Sstevel@tonic-gate 	if (t->t_proc_flag & TP_DAEMON) {
740*7c478bd9Sstevel@tonic-gate 		p->p_lwpdaemon--;
741*7c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_DAEMON;
742*7c478bd9Sstevel@tonic-gate 	}
743*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_TWAIT;
744*7c478bd9Sstevel@tonic-gate 
745*7c478bd9Sstevel@tonic-gate 	/*
746*7c478bd9Sstevel@tonic-gate 	 * Maintain accurate lwp count for task.max-lwps resource control.
747*7c478bd9Sstevel@tonic-gate 	 */
748*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_zone->zone_nlwps_lock);
749*7c478bd9Sstevel@tonic-gate 	p->p_task->tk_nlwps--;
750*7c478bd9Sstevel@tonic-gate 	p->p_task->tk_proj->kpj_nlwps--;
751*7c478bd9Sstevel@tonic-gate 	p->p_zone->zone_nlwps--;
752*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_zone->zone_nlwps_lock);
753*7c478bd9Sstevel@tonic-gate 
754*7c478bd9Sstevel@tonic-gate 	CL_EXIT(t);		/* tell the scheduler that t is exiting */
755*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt != 0);
756*7c478bd9Sstevel@tonic-gate 	p->p_lwpcnt--;
757*7c478bd9Sstevel@tonic-gate 
758*7c478bd9Sstevel@tonic-gate 	/*
759*7c478bd9Sstevel@tonic-gate 	 * If all remaining non-daemon lwps are waiting in lwp_wait(),
760*7c478bd9Sstevel@tonic-gate 	 * wake them up so someone can return EDEADLK.
761*7c478bd9Sstevel@tonic-gate 	 * (See the block comment preceeding lwp_wait().)
762*7c478bd9Sstevel@tonic-gate 	 */
763*7c478bd9Sstevel@tonic-gate 	if (p->p_lwpcnt == p->p_lwpdaemon + (p->p_lwpwait - p->p_lwpdwait))
764*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&p->p_lwpexit);
765*7c478bd9Sstevel@tonic-gate 
766*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_LWPEXIT;
767*7c478bd9Sstevel@tonic-gate 	term_mstate(t);
768*7c478bd9Sstevel@tonic-gate #ifndef NPROBE
769*7c478bd9Sstevel@tonic-gate 	/* Kernel probe */
770*7c478bd9Sstevel@tonic-gate 	if (t->t_tnf_tpdp)
771*7c478bd9Sstevel@tonic-gate 		tnf_thread_exit();
772*7c478bd9Sstevel@tonic-gate #endif /* NPROBE */
773*7c478bd9Sstevel@tonic-gate 
774*7c478bd9Sstevel@tonic-gate 	t->t_forw->t_back = t->t_back;
775*7c478bd9Sstevel@tonic-gate 	t->t_back->t_forw = t->t_forw;
776*7c478bd9Sstevel@tonic-gate 	if (t == p->p_tlist)
777*7c478bd9Sstevel@tonic-gate 		p->p_tlist = t->t_forw;
778*7c478bd9Sstevel@tonic-gate 
779*7c478bd9Sstevel@tonic-gate 	/*
780*7c478bd9Sstevel@tonic-gate 	 * Clean up the signal state.
781*7c478bd9Sstevel@tonic-gate 	 */
782*7c478bd9Sstevel@tonic-gate 	if (t->t_sigqueue != NULL)
783*7c478bd9Sstevel@tonic-gate 		sigdelq(p, t, 0);
784*7c478bd9Sstevel@tonic-gate 	if (lwp->lwp_curinfo != NULL) {
785*7c478bd9Sstevel@tonic-gate 		siginfofree(lwp->lwp_curinfo);
786*7c478bd9Sstevel@tonic-gate 		lwp->lwp_curinfo = NULL;
787*7c478bd9Sstevel@tonic-gate 	}
788*7c478bd9Sstevel@tonic-gate 
789*7c478bd9Sstevel@tonic-gate 	thread_rele(t);
790*7c478bd9Sstevel@tonic-gate 
791*7c478bd9Sstevel@tonic-gate 	/*
792*7c478bd9Sstevel@tonic-gate 	 * Terminated lwps are associated with process zero and are put onto
793*7c478bd9Sstevel@tonic-gate 	 * death-row by resume().  Avoid preemption after resetting t->t_procp.
794*7c478bd9Sstevel@tonic-gate 	 */
795*7c478bd9Sstevel@tonic-gate 	t->t_preempt++;
796*7c478bd9Sstevel@tonic-gate 	t->t_procp = &p0;
797*7c478bd9Sstevel@tonic-gate 
798*7c478bd9Sstevel@tonic-gate 	/*
799*7c478bd9Sstevel@tonic-gate 	 * Notify the HAT about the change of address space
800*7c478bd9Sstevel@tonic-gate 	 */
801*7c478bd9Sstevel@tonic-gate 	hat_thread_exit(t);
802*7c478bd9Sstevel@tonic-gate 	/*
803*7c478bd9Sstevel@tonic-gate 	 * When this is the last running lwp in this process and some lwp is
804*7c478bd9Sstevel@tonic-gate 	 * waiting for this condition to become true, or this thread was being
805*7c478bd9Sstevel@tonic-gate 	 * suspended, then the waiting lwp is awakened.
806*7c478bd9Sstevel@tonic-gate 	 *
807*7c478bd9Sstevel@tonic-gate 	 * Also, if the process is exiting, we may have a thread waiting in
808*7c478bd9Sstevel@tonic-gate 	 * exitlwps() that needs to be notified.
809*7c478bd9Sstevel@tonic-gate 	 */
810*7c478bd9Sstevel@tonic-gate 	if (--p->p_lwprcnt == 0 || (t->t_proc_flag & TP_HOLDLWP) ||
811*7c478bd9Sstevel@tonic-gate 	    (p->p_flag & SEXITLWPS))
812*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
813*7c478bd9Sstevel@tonic-gate 
814*7c478bd9Sstevel@tonic-gate 	/*
815*7c478bd9Sstevel@tonic-gate 	 * Need to drop p_lock so we can reacquire pidlock.
816*7c478bd9Sstevel@tonic-gate 	 */
817*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
818*7c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
819*7c478bd9Sstevel@tonic-gate 
820*7c478bd9Sstevel@tonic-gate 	ASSERT(t != t->t_next);		/* t0 never exits */
821*7c478bd9Sstevel@tonic-gate 	t->t_next->t_prev = t->t_prev;
822*7c478bd9Sstevel@tonic-gate 	t->t_prev->t_next = t->t_next;
823*7c478bd9Sstevel@tonic-gate 	cv_broadcast(&t->t_joincv);	/* wake up anyone in thread_join */
824*7c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
825*7c478bd9Sstevel@tonic-gate 
826*7c478bd9Sstevel@tonic-gate 	lwp_pcb_exit();
827*7c478bd9Sstevel@tonic-gate 
828*7c478bd9Sstevel@tonic-gate 	if (t->t_ctx != NULL)
829*7c478bd9Sstevel@tonic-gate 		exitctx(t);
830*7c478bd9Sstevel@tonic-gate 
831*7c478bd9Sstevel@tonic-gate 	t->t_state = TS_ZOMB;
832*7c478bd9Sstevel@tonic-gate 	swtch_from_zombie();
833*7c478bd9Sstevel@tonic-gate 	/* never returns */
834*7c478bd9Sstevel@tonic-gate }
835*7c478bd9Sstevel@tonic-gate 
836*7c478bd9Sstevel@tonic-gate 
837*7c478bd9Sstevel@tonic-gate /*
838*7c478bd9Sstevel@tonic-gate  * Cleanup function for an exiting lwp.
839*7c478bd9Sstevel@tonic-gate  * Called both from lwp_exit() and from proc_exit().
840*7c478bd9Sstevel@tonic-gate  * p->p_lock is repeatedly released and grabbed in this function.
841*7c478bd9Sstevel@tonic-gate  */
842*7c478bd9Sstevel@tonic-gate void
843*7c478bd9Sstevel@tonic-gate lwp_cleanup(void)
844*7c478bd9Sstevel@tonic-gate {
845*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
846*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
847*7c478bd9Sstevel@tonic-gate 
848*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
849*7c478bd9Sstevel@tonic-gate 
850*7c478bd9Sstevel@tonic-gate 	/* untimeout any lwp-bound realtime timers */
851*7c478bd9Sstevel@tonic-gate 	if (p->p_itimer != NULL)
852*7c478bd9Sstevel@tonic-gate 		timer_lwpexit();
853*7c478bd9Sstevel@tonic-gate 
854*7c478bd9Sstevel@tonic-gate 	/*
855*7c478bd9Sstevel@tonic-gate 	 * If this is the /proc agent lwp that is exiting, readjust p_lwpid
856*7c478bd9Sstevel@tonic-gate 	 * so it appears that the agent never existed, and clear p_agenttp.
857*7c478bd9Sstevel@tonic-gate 	 */
858*7c478bd9Sstevel@tonic-gate 	if (t == p->p_agenttp) {
859*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_tid == p->p_lwpid);
860*7c478bd9Sstevel@tonic-gate 		p->p_lwpid--;
861*7c478bd9Sstevel@tonic-gate 		p->p_agenttp = NULL;
862*7c478bd9Sstevel@tonic-gate 	}
863*7c478bd9Sstevel@tonic-gate 
864*7c478bd9Sstevel@tonic-gate 	/*
865*7c478bd9Sstevel@tonic-gate 	 * Do lgroup bookkeeping to account for thread exiting.
866*7c478bd9Sstevel@tonic-gate 	 */
867*7c478bd9Sstevel@tonic-gate 	kpreempt_disable();
868*7c478bd9Sstevel@tonic-gate 	lgrp_move_thread(t, NULL, 1);
869*7c478bd9Sstevel@tonic-gate 	kpreempt_enable();
870*7c478bd9Sstevel@tonic-gate 
871*7c478bd9Sstevel@tonic-gate 	lwp_ctmpl_clear(ttolwp(t));
872*7c478bd9Sstevel@tonic-gate }
873*7c478bd9Sstevel@tonic-gate 
874*7c478bd9Sstevel@tonic-gate int
875*7c478bd9Sstevel@tonic-gate lwp_suspend(kthread_t *t)
876*7c478bd9Sstevel@tonic-gate {
877*7c478bd9Sstevel@tonic-gate 	int tid;
878*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
879*7c478bd9Sstevel@tonic-gate 
880*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
881*7c478bd9Sstevel@tonic-gate 
882*7c478bd9Sstevel@tonic-gate 	/*
883*7c478bd9Sstevel@tonic-gate 	 * Set the thread's TP_HOLDLWP flag so it will stop in holdlwp().
884*7c478bd9Sstevel@tonic-gate 	 * If an lwp is stopping itself, there is no need to wait.
885*7c478bd9Sstevel@tonic-gate 	 */
886*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag |= TP_HOLDLWP;
887*7c478bd9Sstevel@tonic-gate 	if (t == curthread) {
888*7c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;
889*7c478bd9Sstevel@tonic-gate 	} else {
890*7c478bd9Sstevel@tonic-gate 		/*
891*7c478bd9Sstevel@tonic-gate 		 * Make sure the lwp stops promptly.
892*7c478bd9Sstevel@tonic-gate 		 */
893*7c478bd9Sstevel@tonic-gate 		thread_lock(t);
894*7c478bd9Sstevel@tonic-gate 		t->t_sig_check = 1;
895*7c478bd9Sstevel@tonic-gate 		/*
896*7c478bd9Sstevel@tonic-gate 		 * XXX Should use virtual stop like /proc does instead of
897*7c478bd9Sstevel@tonic-gate 		 * XXX waking the thread to get it to stop.
898*7c478bd9Sstevel@tonic-gate 		 */
899*7c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP && (t->t_flag & T_WAKEABLE))
900*7c478bd9Sstevel@tonic-gate 			setrun_locked(t);
901*7c478bd9Sstevel@tonic-gate 		else if (t->t_state == TS_ONPROC && t->t_cpu != CPU)
902*7c478bd9Sstevel@tonic-gate 			poke_cpu(t->t_cpu->cpu_id);
903*7c478bd9Sstevel@tonic-gate 		tid = t->t_tid;	 /* remember thread ID */
904*7c478bd9Sstevel@tonic-gate 		/*
905*7c478bd9Sstevel@tonic-gate 		 * Wait for lwp to stop
906*7c478bd9Sstevel@tonic-gate 		 */
907*7c478bd9Sstevel@tonic-gate 		while (!SUSPENDED(t)) {
908*7c478bd9Sstevel@tonic-gate 			/*
909*7c478bd9Sstevel@tonic-gate 			 * Drop the thread lock before waiting and reacquire it
910*7c478bd9Sstevel@tonic-gate 			 * afterwards, so the thread can change its t_state
911*7c478bd9Sstevel@tonic-gate 			 * field.
912*7c478bd9Sstevel@tonic-gate 			 */
913*7c478bd9Sstevel@tonic-gate 			thread_unlock(t);
914*7c478bd9Sstevel@tonic-gate 
915*7c478bd9Sstevel@tonic-gate 			/*
916*7c478bd9Sstevel@tonic-gate 			 * Check if aborted by exitlwps().
917*7c478bd9Sstevel@tonic-gate 			 */
918*7c478bd9Sstevel@tonic-gate 			if (p->p_flag & SEXITLWPS)
919*7c478bd9Sstevel@tonic-gate 				lwp_exit();
920*7c478bd9Sstevel@tonic-gate 
921*7c478bd9Sstevel@tonic-gate 			/*
922*7c478bd9Sstevel@tonic-gate 			 * Cooperate with jobcontrol signals and /proc stopping
923*7c478bd9Sstevel@tonic-gate 			 * by calling cv_wait_sig() to wait for the target
924*7c478bd9Sstevel@tonic-gate 			 * lwp to stop.  Just using cv_wait() can lead to
925*7c478bd9Sstevel@tonic-gate 			 * deadlock because, if some other lwp has stopped
926*7c478bd9Sstevel@tonic-gate 			 * by either of these mechanisms, then p_lwprcnt will
927*7c478bd9Sstevel@tonic-gate 			 * never become zero if we do a cv_wait().
928*7c478bd9Sstevel@tonic-gate 			 */
929*7c478bd9Sstevel@tonic-gate 			if (!cv_wait_sig(&p->p_holdlwps, &p->p_lock))
930*7c478bd9Sstevel@tonic-gate 				return (EINTR);
931*7c478bd9Sstevel@tonic-gate 
932*7c478bd9Sstevel@tonic-gate 			/*
933*7c478bd9Sstevel@tonic-gate 			 * Check to see if thread died while we were
934*7c478bd9Sstevel@tonic-gate 			 * waiting for it to suspend.
935*7c478bd9Sstevel@tonic-gate 			 */
936*7c478bd9Sstevel@tonic-gate 			if (idtot(p, tid) == NULL)
937*7c478bd9Sstevel@tonic-gate 				return (ESRCH);
938*7c478bd9Sstevel@tonic-gate 
939*7c478bd9Sstevel@tonic-gate 			thread_lock(t);
940*7c478bd9Sstevel@tonic-gate 			/*
941*7c478bd9Sstevel@tonic-gate 			 * If TP_HOLDLWP flag goes away, lwp_continue() must
942*7c478bd9Sstevel@tonic-gate 			 * have been called while we were waiting, so cancel
943*7c478bd9Sstevel@tonic-gate 			 * the suspend.
944*7c478bd9Sstevel@tonic-gate 			 */
945*7c478bd9Sstevel@tonic-gate 			if ((t->t_proc_flag & TP_HOLDLWP) == 0) {
946*7c478bd9Sstevel@tonic-gate 				thread_unlock(t);
947*7c478bd9Sstevel@tonic-gate 				return (0);
948*7c478bd9Sstevel@tonic-gate 			}
949*7c478bd9Sstevel@tonic-gate 		}
950*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
951*7c478bd9Sstevel@tonic-gate 	}
952*7c478bd9Sstevel@tonic-gate 	return (0);
953*7c478bd9Sstevel@tonic-gate }
954*7c478bd9Sstevel@tonic-gate 
955*7c478bd9Sstevel@tonic-gate /*
956*7c478bd9Sstevel@tonic-gate  * continue a lwp that's been stopped by lwp_suspend().
957*7c478bd9Sstevel@tonic-gate  */
958*7c478bd9Sstevel@tonic-gate void
959*7c478bd9Sstevel@tonic-gate lwp_continue(kthread_t *t)
960*7c478bd9Sstevel@tonic-gate {
961*7c478bd9Sstevel@tonic-gate 	proc_t *p = ttoproc(t);
962*7c478bd9Sstevel@tonic-gate 	int was_suspended = t->t_proc_flag & TP_HOLDLWP;
963*7c478bd9Sstevel@tonic-gate 
964*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
965*7c478bd9Sstevel@tonic-gate 
966*7c478bd9Sstevel@tonic-gate 	t->t_proc_flag &= ~TP_HOLDLWP;
967*7c478bd9Sstevel@tonic-gate 	thread_lock(t);
968*7c478bd9Sstevel@tonic-gate 	if (SUSPENDED(t) &&
969*7c478bd9Sstevel@tonic-gate 	    !(p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH))) {
970*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
971*7c478bd9Sstevel@tonic-gate 		t->t_schedflag |= TS_CSTART;
972*7c478bd9Sstevel@tonic-gate 		setrun_locked(t);
973*7c478bd9Sstevel@tonic-gate 	}
974*7c478bd9Sstevel@tonic-gate 	thread_unlock(t);
975*7c478bd9Sstevel@tonic-gate 	/*
976*7c478bd9Sstevel@tonic-gate 	 * Wakeup anyone waiting for this thread to be suspended
977*7c478bd9Sstevel@tonic-gate 	 */
978*7c478bd9Sstevel@tonic-gate 	if (was_suspended)
979*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
980*7c478bd9Sstevel@tonic-gate }
981*7c478bd9Sstevel@tonic-gate 
982*7c478bd9Sstevel@tonic-gate /*
983*7c478bd9Sstevel@tonic-gate  * ********************************
984*7c478bd9Sstevel@tonic-gate  *  Miscellaneous lwp routines	  *
985*7c478bd9Sstevel@tonic-gate  * ********************************
986*7c478bd9Sstevel@tonic-gate  */
987*7c478bd9Sstevel@tonic-gate /*
988*7c478bd9Sstevel@tonic-gate  * When a process is undergoing a forkall(), its p_flag is set to SHOLDFORK.
989*7c478bd9Sstevel@tonic-gate  * This will cause the process's lwps to stop at a hold point.  A hold
990*7c478bd9Sstevel@tonic-gate  * point is where a kernel thread has a flat stack.  This is at the
991*7c478bd9Sstevel@tonic-gate  * return from a system call and at the return from a user level trap.
992*7c478bd9Sstevel@tonic-gate  *
993*7c478bd9Sstevel@tonic-gate  * When a process is undergoing a fork1() or vfork(), its p_flag is set to
994*7c478bd9Sstevel@tonic-gate  * SHOLDFORK1.  This will cause the process's lwps to stop at a modified
995*7c478bd9Sstevel@tonic-gate  * hold point.  The lwps in the process are not being cloned, so they
996*7c478bd9Sstevel@tonic-gate  * are held at the usual hold points and also within issig_forreal().
997*7c478bd9Sstevel@tonic-gate  * This has the side-effect that their system calls do not return
998*7c478bd9Sstevel@tonic-gate  * showing EINTR.
999*7c478bd9Sstevel@tonic-gate  *
1000*7c478bd9Sstevel@tonic-gate  * An lwp can also be held.  This is identified by the TP_HOLDLWP flag on
1001*7c478bd9Sstevel@tonic-gate  * the thread.  The TP_HOLDLWP flag is set in lwp_suspend(), where the active
1002*7c478bd9Sstevel@tonic-gate  * lwp is waiting for the target lwp to be stopped.
1003*7c478bd9Sstevel@tonic-gate  */
1004*7c478bd9Sstevel@tonic-gate void
1005*7c478bd9Sstevel@tonic-gate holdlwp(void)
1006*7c478bd9Sstevel@tonic-gate {
1007*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1008*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1009*7c478bd9Sstevel@tonic-gate 
1010*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1011*7c478bd9Sstevel@tonic-gate 	/*
1012*7c478bd9Sstevel@tonic-gate 	 * Don't terminate immediately if the process is dumping core.
1013*7c478bd9Sstevel@tonic-gate 	 * Once the process has dumped core, all lwps are terminated.
1014*7c478bd9Sstevel@tonic-gate 	 */
1015*7c478bd9Sstevel@tonic-gate 	if (!(p->p_flag & SCOREDUMP)) {
1016*7c478bd9Sstevel@tonic-gate 		if ((p->p_flag & SEXITLWPS) || (t->t_proc_flag & TP_EXITLWP))
1017*7c478bd9Sstevel@tonic-gate 			lwp_exit();
1018*7c478bd9Sstevel@tonic-gate 	}
1019*7c478bd9Sstevel@tonic-gate 	if (!(ISHOLD(p)) && !(p->p_flag & (SHOLDFORK1 | SHOLDWATCH))) {
1020*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1021*7c478bd9Sstevel@tonic-gate 		return;
1022*7c478bd9Sstevel@tonic-gate 	}
1023*7c478bd9Sstevel@tonic-gate 	/*
1024*7c478bd9Sstevel@tonic-gate 	 * stop() decrements p->p_lwprcnt and cv_signal()s &p->p_holdlwps
1025*7c478bd9Sstevel@tonic-gate 	 * when p->p_lwprcnt becomes zero.
1026*7c478bd9Sstevel@tonic-gate 	 */
1027*7c478bd9Sstevel@tonic-gate 	stop(PR_SUSPENDED, SUSPEND_NORMAL);
1028*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS)
1029*7c478bd9Sstevel@tonic-gate 		lwp_exit();
1030*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1031*7c478bd9Sstevel@tonic-gate }
1032*7c478bd9Sstevel@tonic-gate 
1033*7c478bd9Sstevel@tonic-gate /*
1034*7c478bd9Sstevel@tonic-gate  * Have all lwps within the process hold at a point where they are
1035*7c478bd9Sstevel@tonic-gate  * cloneable (SHOLDFORK) or just safe w.r.t. fork1 (SHOLDFORK1).
1036*7c478bd9Sstevel@tonic-gate  */
1037*7c478bd9Sstevel@tonic-gate int
1038*7c478bd9Sstevel@tonic-gate holdlwps(int holdflag)
1039*7c478bd9Sstevel@tonic-gate {
1040*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1041*7c478bd9Sstevel@tonic-gate 
1042*7c478bd9Sstevel@tonic-gate 	ASSERT(holdflag == SHOLDFORK || holdflag == SHOLDFORK1);
1043*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1044*7c478bd9Sstevel@tonic-gate 	schedctl_finish_sigblock(curthread);
1045*7c478bd9Sstevel@tonic-gate again:
1046*7c478bd9Sstevel@tonic-gate 	while (p->p_flag & (SEXITLWPS | SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
1047*7c478bd9Sstevel@tonic-gate 		/*
1048*7c478bd9Sstevel@tonic-gate 		 * If another lwp is doing a forkall() or proc_exit(), bail out.
1049*7c478bd9Sstevel@tonic-gate 		 */
1050*7c478bd9Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS | SHOLDFORK)) {
1051*7c478bd9Sstevel@tonic-gate 			mutex_exit(&p->p_lock);
1052*7c478bd9Sstevel@tonic-gate 			return (0);
1053*7c478bd9Sstevel@tonic-gate 		}
1054*7c478bd9Sstevel@tonic-gate 		/*
1055*7c478bd9Sstevel@tonic-gate 		 * Another lwp is doing a fork1() or is undergoing
1056*7c478bd9Sstevel@tonic-gate 		 * watchpoint activity.  We hold here for it to complete.
1057*7c478bd9Sstevel@tonic-gate 		 */
1058*7c478bd9Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
1059*7c478bd9Sstevel@tonic-gate 	}
1060*7c478bd9Sstevel@tonic-gate 	p->p_flag |= holdflag;
1061*7c478bd9Sstevel@tonic-gate 	pokelwps(p);
1062*7c478bd9Sstevel@tonic-gate 	--p->p_lwprcnt;
1063*7c478bd9Sstevel@tonic-gate 	/*
1064*7c478bd9Sstevel@tonic-gate 	 * Wait for the process to become quiescent (p->p_lwprcnt == 0).
1065*7c478bd9Sstevel@tonic-gate 	 */
1066*7c478bd9Sstevel@tonic-gate 	while (p->p_lwprcnt > 0) {
1067*7c478bd9Sstevel@tonic-gate 		/*
1068*7c478bd9Sstevel@tonic-gate 		 * Check if aborted by exitlwps().
1069*7c478bd9Sstevel@tonic-gate 		 * Also check if SHOLDWATCH is set; it takes precedence.
1070*7c478bd9Sstevel@tonic-gate 		 */
1071*7c478bd9Sstevel@tonic-gate 		if (p->p_flag & (SEXITLWPS | SHOLDWATCH)) {
1072*7c478bd9Sstevel@tonic-gate 			p->p_lwprcnt++;
1073*7c478bd9Sstevel@tonic-gate 			p->p_flag &= ~holdflag;
1074*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
1075*7c478bd9Sstevel@tonic-gate 			goto again;
1076*7c478bd9Sstevel@tonic-gate 		}
1077*7c478bd9Sstevel@tonic-gate 		/*
1078*7c478bd9Sstevel@tonic-gate 		 * Cooperate with jobcontrol signals and /proc stopping.
1079*7c478bd9Sstevel@tonic-gate 		 * If some other lwp has stopped by either of these
1080*7c478bd9Sstevel@tonic-gate 		 * mechanisms, then p_lwprcnt will never become zero
1081*7c478bd9Sstevel@tonic-gate 		 * and the process will appear deadlocked unless we
1082*7c478bd9Sstevel@tonic-gate 		 * stop here in sympathy with the other lwp before
1083*7c478bd9Sstevel@tonic-gate 		 * doing the cv_wait() below.
1084*7c478bd9Sstevel@tonic-gate 		 *
1085*7c478bd9Sstevel@tonic-gate 		 * If the other lwp stops after we do the cv_wait(), it
1086*7c478bd9Sstevel@tonic-gate 		 * will wake us up to loop around and do the sympathy stop.
1087*7c478bd9Sstevel@tonic-gate 		 *
1088*7c478bd9Sstevel@tonic-gate 		 * Since stop() drops p->p_lock, we must start from
1089*7c478bd9Sstevel@tonic-gate 		 * the top again on returning from stop().
1090*7c478bd9Sstevel@tonic-gate 		 */
1091*7c478bd9Sstevel@tonic-gate 		if (p->p_stopsig | (curthread->t_proc_flag & TP_PRSTOP)) {
1092*7c478bd9Sstevel@tonic-gate 			int whystop = p->p_stopsig? PR_JOBCONTROL :
1093*7c478bd9Sstevel@tonic-gate 			    PR_REQUESTED;
1094*7c478bd9Sstevel@tonic-gate 			p->p_lwprcnt++;
1095*7c478bd9Sstevel@tonic-gate 			p->p_flag &= ~holdflag;
1096*7c478bd9Sstevel@tonic-gate 			stop(whystop, p->p_stopsig);
1097*7c478bd9Sstevel@tonic-gate 			goto again;
1098*7c478bd9Sstevel@tonic-gate 		}
1099*7c478bd9Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
1100*7c478bd9Sstevel@tonic-gate 	}
1101*7c478bd9Sstevel@tonic-gate 	p->p_lwprcnt++;
1102*7c478bd9Sstevel@tonic-gate 	p->p_flag &= ~holdflag;
1103*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1104*7c478bd9Sstevel@tonic-gate 	return (1);
1105*7c478bd9Sstevel@tonic-gate }
1106*7c478bd9Sstevel@tonic-gate 
1107*7c478bd9Sstevel@tonic-gate /*
1108*7c478bd9Sstevel@tonic-gate  * See comments for holdwatch(), below.
1109*7c478bd9Sstevel@tonic-gate  */
1110*7c478bd9Sstevel@tonic-gate static int
1111*7c478bd9Sstevel@tonic-gate holdcheck(int clearflags)
1112*7c478bd9Sstevel@tonic-gate {
1113*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1114*7c478bd9Sstevel@tonic-gate 
1115*7c478bd9Sstevel@tonic-gate 	/*
1116*7c478bd9Sstevel@tonic-gate 	 * If we are trying to exit, that takes precedence over anything else.
1117*7c478bd9Sstevel@tonic-gate 	 */
1118*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS) {
1119*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
1120*7c478bd9Sstevel@tonic-gate 		p->p_flag &= ~clearflags;
1121*7c478bd9Sstevel@tonic-gate 		lwp_exit();
1122*7c478bd9Sstevel@tonic-gate 	}
1123*7c478bd9Sstevel@tonic-gate 
1124*7c478bd9Sstevel@tonic-gate 	/*
1125*7c478bd9Sstevel@tonic-gate 	 * If another thread is calling fork1(), stop the current thread so the
1126*7c478bd9Sstevel@tonic-gate 	 * other can complete.
1127*7c478bd9Sstevel@tonic-gate 	 */
1128*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SHOLDFORK1) {
1129*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
1130*7c478bd9Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
1131*7c478bd9Sstevel@tonic-gate 		if (p->p_flag & SEXITLWPS) {
1132*7c478bd9Sstevel@tonic-gate 			p->p_flag &= ~clearflags;
1133*7c478bd9Sstevel@tonic-gate 			lwp_exit();
1134*7c478bd9Sstevel@tonic-gate 		}
1135*7c478bd9Sstevel@tonic-gate 		return (-1);
1136*7c478bd9Sstevel@tonic-gate 	}
1137*7c478bd9Sstevel@tonic-gate 
1138*7c478bd9Sstevel@tonic-gate 	/*
1139*7c478bd9Sstevel@tonic-gate 	 * If another thread is calling fork(), then indicate we are doing
1140*7c478bd9Sstevel@tonic-gate 	 * watchpoint activity.  This will cause holdlwps() above to stop the
1141*7c478bd9Sstevel@tonic-gate 	 * forking thread, at which point we can continue with watchpoint
1142*7c478bd9Sstevel@tonic-gate 	 * activity.
1143*7c478bd9Sstevel@tonic-gate 	 */
1144*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SHOLDFORK) {
1145*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
1146*7c478bd9Sstevel@tonic-gate 		while (p->p_flag & SHOLDFORK) {
1147*7c478bd9Sstevel@tonic-gate 			p->p_flag |= SHOLDWATCH;
1148*7c478bd9Sstevel@tonic-gate 			cv_broadcast(&p->p_holdlwps);
1149*7c478bd9Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
1150*7c478bd9Sstevel@tonic-gate 			p->p_flag &= ~SHOLDWATCH;
1151*7c478bd9Sstevel@tonic-gate 		}
1152*7c478bd9Sstevel@tonic-gate 		return (-1);
1153*7c478bd9Sstevel@tonic-gate 	}
1154*7c478bd9Sstevel@tonic-gate 
1155*7c478bd9Sstevel@tonic-gate 	return (0);
1156*7c478bd9Sstevel@tonic-gate }
1157*7c478bd9Sstevel@tonic-gate 
1158*7c478bd9Sstevel@tonic-gate /*
1159*7c478bd9Sstevel@tonic-gate  * Stop all lwps within the process, holding themselves in the kernel while the
1160*7c478bd9Sstevel@tonic-gate  * active lwp undergoes watchpoint activity.  This is more complicated than
1161*7c478bd9Sstevel@tonic-gate  * expected because stop() relies on calling holdwatch() in order to copyin data
1162*7c478bd9Sstevel@tonic-gate  * from the user's address space.  A double barrier is used to prevent an
1163*7c478bd9Sstevel@tonic-gate  * infinite loop.
1164*7c478bd9Sstevel@tonic-gate  *
1165*7c478bd9Sstevel@tonic-gate  * 	o The first thread into holdwatch() is the 'master' thread and does
1166*7c478bd9Sstevel@tonic-gate  *        the following:
1167*7c478bd9Sstevel@tonic-gate  *
1168*7c478bd9Sstevel@tonic-gate  *              - Sets SHOLDWATCH on the current process
1169*7c478bd9Sstevel@tonic-gate  *              - Sets TP_WATCHSTOP on the current thread
1170*7c478bd9Sstevel@tonic-gate  *              - Waits for all threads to be either stopped or have
1171*7c478bd9Sstevel@tonic-gate  *                TP_WATCHSTOP set.
1172*7c478bd9Sstevel@tonic-gate  *              - Sets the SWATCHOK flag on the process
1173*7c478bd9Sstevel@tonic-gate  *              - Unsets TP_WATCHSTOP
1174*7c478bd9Sstevel@tonic-gate  *              - Waits for the other threads to completely stop
1175*7c478bd9Sstevel@tonic-gate  *              - Unsets SWATCHOK
1176*7c478bd9Sstevel@tonic-gate  *
1177*7c478bd9Sstevel@tonic-gate  * 	o If SHOLDWATCH is already set when we enter this function, then another
1178*7c478bd9Sstevel@tonic-gate  *        thread is already trying to stop this thread.  This 'slave' thread
1179*7c478bd9Sstevel@tonic-gate  *        does the following:
1180*7c478bd9Sstevel@tonic-gate  *
1181*7c478bd9Sstevel@tonic-gate  *              - Sets TP_WATCHSTOP on the current thread
1182*7c478bd9Sstevel@tonic-gate  *              - Waits for SWATCHOK flag to be set
1183*7c478bd9Sstevel@tonic-gate  *              - Calls stop()
1184*7c478bd9Sstevel@tonic-gate  *
1185*7c478bd9Sstevel@tonic-gate  * 	o If SWATCHOK is set on the process, then this function immediately
1186*7c478bd9Sstevel@tonic-gate  *        returns, as we must have been called via stop().
1187*7c478bd9Sstevel@tonic-gate  *
1188*7c478bd9Sstevel@tonic-gate  * In addition, there are other flags that take precedence over SHOLDWATCH:
1189*7c478bd9Sstevel@tonic-gate  *
1190*7c478bd9Sstevel@tonic-gate  * 	o If SEXITLWPS is set, exit immediately.
1191*7c478bd9Sstevel@tonic-gate  *
1192*7c478bd9Sstevel@tonic-gate  * 	o If SHOLDFORK1 is set, wait for fork1() to complete.
1193*7c478bd9Sstevel@tonic-gate  *
1194*7c478bd9Sstevel@tonic-gate  * 	o If SHOLDFORK is set, then watchpoint activity takes precedence In this
1195*7c478bd9Sstevel@tonic-gate  *        case, set SHOLDWATCH, signalling the forking thread to stop first.
1196*7c478bd9Sstevel@tonic-gate  *
1197*7c478bd9Sstevel@tonic-gate  * 	o If the process is being stopped via /proc (TP_PRSTOP is set), then we
1198*7c478bd9Sstevel@tonic-gate  *        stop the current thread.
1199*7c478bd9Sstevel@tonic-gate  *
1200*7c478bd9Sstevel@tonic-gate  * Returns 0 if all threads have been quiesced.  Returns non-zero if not all
1201*7c478bd9Sstevel@tonic-gate  * threads were stopped, or the list of watched pages has changed.
1202*7c478bd9Sstevel@tonic-gate  */
1203*7c478bd9Sstevel@tonic-gate int
1204*7c478bd9Sstevel@tonic-gate holdwatch(void)
1205*7c478bd9Sstevel@tonic-gate {
1206*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1207*7c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
1208*7c478bd9Sstevel@tonic-gate 	int ret = 0;
1209*7c478bd9Sstevel@tonic-gate 
1210*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1211*7c478bd9Sstevel@tonic-gate 
1212*7c478bd9Sstevel@tonic-gate 	p->p_lwprcnt--;
1213*7c478bd9Sstevel@tonic-gate 
1214*7c478bd9Sstevel@tonic-gate 	/*
1215*7c478bd9Sstevel@tonic-gate 	 * Check for bail-out conditions as outlined above.
1216*7c478bd9Sstevel@tonic-gate 	 */
1217*7c478bd9Sstevel@tonic-gate 	if (holdcheck(0) != 0) {
1218*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1219*7c478bd9Sstevel@tonic-gate 		return (-1);
1220*7c478bd9Sstevel@tonic-gate 	}
1221*7c478bd9Sstevel@tonic-gate 
1222*7c478bd9Sstevel@tonic-gate 	if (!(p->p_flag & SHOLDWATCH)) {
1223*7c478bd9Sstevel@tonic-gate 		/*
1224*7c478bd9Sstevel@tonic-gate 		 * We are the master watchpoint thread.  Set SHOLDWATCH and poke
1225*7c478bd9Sstevel@tonic-gate 		 * the other threads.
1226*7c478bd9Sstevel@tonic-gate 		 */
1227*7c478bd9Sstevel@tonic-gate 		p->p_flag |= SHOLDWATCH;
1228*7c478bd9Sstevel@tonic-gate 		pokelwps(p);
1229*7c478bd9Sstevel@tonic-gate 
1230*7c478bd9Sstevel@tonic-gate 		/*
1231*7c478bd9Sstevel@tonic-gate 		 * Wait for all threads to be stopped or have TP_WATCHSTOP set.
1232*7c478bd9Sstevel@tonic-gate 		 */
1233*7c478bd9Sstevel@tonic-gate 		while (pr_allstopped(p, 1) > 0) {
1234*7c478bd9Sstevel@tonic-gate 			if (holdcheck(SHOLDWATCH) != 0) {
1235*7c478bd9Sstevel@tonic-gate 				p->p_flag &= ~SHOLDWATCH;
1236*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
1237*7c478bd9Sstevel@tonic-gate 				return (-1);
1238*7c478bd9Sstevel@tonic-gate 			}
1239*7c478bd9Sstevel@tonic-gate 
1240*7c478bd9Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
1241*7c478bd9Sstevel@tonic-gate 		}
1242*7c478bd9Sstevel@tonic-gate 
1243*7c478bd9Sstevel@tonic-gate 		/*
1244*7c478bd9Sstevel@tonic-gate 		 * All threads are now stopped or in the process of stopping.
1245*7c478bd9Sstevel@tonic-gate 		 * Set SWATCHOK and let them stop completely.
1246*7c478bd9Sstevel@tonic-gate 		 */
1247*7c478bd9Sstevel@tonic-gate 		p->p_flag |= SWATCHOK;
1248*7c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_WATCHSTOP;
1249*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
1250*7c478bd9Sstevel@tonic-gate 
1251*7c478bd9Sstevel@tonic-gate 		while (pr_allstopped(p, 0) > 0) {
1252*7c478bd9Sstevel@tonic-gate 			/*
1253*7c478bd9Sstevel@tonic-gate 			 * At first glance, it may appear that we don't need a
1254*7c478bd9Sstevel@tonic-gate 			 * call to holdcheck() here.  But if the process gets a
1255*7c478bd9Sstevel@tonic-gate 			 * SIGKILL signal, one of our stopped threads may have
1256*7c478bd9Sstevel@tonic-gate 			 * been awakened and is waiting in exitlwps(), which
1257*7c478bd9Sstevel@tonic-gate 			 * takes precedence over watchpoints.
1258*7c478bd9Sstevel@tonic-gate 			 */
1259*7c478bd9Sstevel@tonic-gate 			if (holdcheck(SHOLDWATCH | SWATCHOK) != 0) {
1260*7c478bd9Sstevel@tonic-gate 				p->p_flag &= ~(SHOLDWATCH | SWATCHOK);
1261*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
1262*7c478bd9Sstevel@tonic-gate 				return (-1);
1263*7c478bd9Sstevel@tonic-gate 			}
1264*7c478bd9Sstevel@tonic-gate 
1265*7c478bd9Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
1266*7c478bd9Sstevel@tonic-gate 		}
1267*7c478bd9Sstevel@tonic-gate 
1268*7c478bd9Sstevel@tonic-gate 		/*
1269*7c478bd9Sstevel@tonic-gate 		 * All threads are now completely stopped.
1270*7c478bd9Sstevel@tonic-gate 		 */
1271*7c478bd9Sstevel@tonic-gate 		p->p_flag &= ~SWATCHOK;
1272*7c478bd9Sstevel@tonic-gate 		p->p_flag &= ~SHOLDWATCH;
1273*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
1274*7c478bd9Sstevel@tonic-gate 
1275*7c478bd9Sstevel@tonic-gate 	} else if (!(p->p_flag & SWATCHOK)) {
1276*7c478bd9Sstevel@tonic-gate 
1277*7c478bd9Sstevel@tonic-gate 		/*
1278*7c478bd9Sstevel@tonic-gate 		 * SHOLDWATCH is set, so another thread is trying to do
1279*7c478bd9Sstevel@tonic-gate 		 * watchpoint activity.  Indicate this thread is stopping, and
1280*7c478bd9Sstevel@tonic-gate 		 * wait for the OK from the master thread.
1281*7c478bd9Sstevel@tonic-gate 		 */
1282*7c478bd9Sstevel@tonic-gate 		t->t_proc_flag |= TP_WATCHSTOP;
1283*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
1284*7c478bd9Sstevel@tonic-gate 
1285*7c478bd9Sstevel@tonic-gate 		while (!(p->p_flag & SWATCHOK)) {
1286*7c478bd9Sstevel@tonic-gate 			if (holdcheck(0) != 0) {
1287*7c478bd9Sstevel@tonic-gate 				t->t_proc_flag &= ~TP_WATCHSTOP;
1288*7c478bd9Sstevel@tonic-gate 				mutex_exit(&p->p_lock);
1289*7c478bd9Sstevel@tonic-gate 				return (-1);
1290*7c478bd9Sstevel@tonic-gate 			}
1291*7c478bd9Sstevel@tonic-gate 
1292*7c478bd9Sstevel@tonic-gate 			cv_wait(&p->p_holdlwps, &p->p_lock);
1293*7c478bd9Sstevel@tonic-gate 		}
1294*7c478bd9Sstevel@tonic-gate 
1295*7c478bd9Sstevel@tonic-gate 		/*
1296*7c478bd9Sstevel@tonic-gate 		 * Once the master thread has given the OK, this thread can
1297*7c478bd9Sstevel@tonic-gate 		 * actually call stop().
1298*7c478bd9Sstevel@tonic-gate 		 */
1299*7c478bd9Sstevel@tonic-gate 		t->t_proc_flag &= ~TP_WATCHSTOP;
1300*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
1301*7c478bd9Sstevel@tonic-gate 
1302*7c478bd9Sstevel@tonic-gate 		stop(PR_SUSPENDED, SUSPEND_NORMAL);
1303*7c478bd9Sstevel@tonic-gate 
1304*7c478bd9Sstevel@tonic-gate 		/*
1305*7c478bd9Sstevel@tonic-gate 		 * It's not OK to do watchpoint activity, notify caller to
1306*7c478bd9Sstevel@tonic-gate 		 * retry.
1307*7c478bd9Sstevel@tonic-gate 		 */
1308*7c478bd9Sstevel@tonic-gate 		ret = -1;
1309*7c478bd9Sstevel@tonic-gate 
1310*7c478bd9Sstevel@tonic-gate 	} else {
1311*7c478bd9Sstevel@tonic-gate 
1312*7c478bd9Sstevel@tonic-gate 		/*
1313*7c478bd9Sstevel@tonic-gate 		 * The only way we can hit the case where SHOLDWATCH is set and
1314*7c478bd9Sstevel@tonic-gate 		 * SWATCHOK is set is if we are triggering this from within a
1315*7c478bd9Sstevel@tonic-gate 		 * stop() call.  Assert that this is the case.
1316*7c478bd9Sstevel@tonic-gate 		 */
1317*7c478bd9Sstevel@tonic-gate 
1318*7c478bd9Sstevel@tonic-gate 		ASSERT(t->t_proc_flag & TP_STOPPING);
1319*7c478bd9Sstevel@tonic-gate 		p->p_lwprcnt++;
1320*7c478bd9Sstevel@tonic-gate 	}
1321*7c478bd9Sstevel@tonic-gate 
1322*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1323*7c478bd9Sstevel@tonic-gate 
1324*7c478bd9Sstevel@tonic-gate 	return (ret);
1325*7c478bd9Sstevel@tonic-gate }
1326*7c478bd9Sstevel@tonic-gate 
1327*7c478bd9Sstevel@tonic-gate /*
1328*7c478bd9Sstevel@tonic-gate  * force all interruptible lwps to trap into the kernel.
1329*7c478bd9Sstevel@tonic-gate  */
1330*7c478bd9Sstevel@tonic-gate void
1331*7c478bd9Sstevel@tonic-gate pokelwps(proc_t *p)
1332*7c478bd9Sstevel@tonic-gate {
1333*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
1334*7c478bd9Sstevel@tonic-gate 
1335*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1336*7c478bd9Sstevel@tonic-gate 
1337*7c478bd9Sstevel@tonic-gate 	t = p->p_tlist;
1338*7c478bd9Sstevel@tonic-gate 	do {
1339*7c478bd9Sstevel@tonic-gate 		if (t == curthread)
1340*7c478bd9Sstevel@tonic-gate 			continue;
1341*7c478bd9Sstevel@tonic-gate 		thread_lock(t);
1342*7c478bd9Sstevel@tonic-gate 		aston(t);	/* make thread trap or do post_syscall */
1343*7c478bd9Sstevel@tonic-gate 		if (t->t_state == TS_SLEEP) {
1344*7c478bd9Sstevel@tonic-gate 			if (t->t_flag & T_WAKEABLE)
1345*7c478bd9Sstevel@tonic-gate 				setrun_locked(t);
1346*7c478bd9Sstevel@tonic-gate 		} else if (t->t_state == TS_STOPPED) {
1347*7c478bd9Sstevel@tonic-gate 			/*
1348*7c478bd9Sstevel@tonic-gate 			 * Ensure that proc_exit() is not blocked by lwps
1349*7c478bd9Sstevel@tonic-gate 			 * that were stopped via jobcontrol or /proc.
1350*7c478bd9Sstevel@tonic-gate 			 */
1351*7c478bd9Sstevel@tonic-gate 			if (p->p_flag & SEXITLWPS) {
1352*7c478bd9Sstevel@tonic-gate 				p->p_stopsig = 0;
1353*7c478bd9Sstevel@tonic-gate 				t->t_schedflag |= (TS_XSTART | TS_PSTART);
1354*7c478bd9Sstevel@tonic-gate 				setrun_locked(t);
1355*7c478bd9Sstevel@tonic-gate 			}
1356*7c478bd9Sstevel@tonic-gate 			/*
1357*7c478bd9Sstevel@tonic-gate 			 * If we are holding lwps for a forkall(),
1358*7c478bd9Sstevel@tonic-gate 			 * force lwps that have been suspended via
1359*7c478bd9Sstevel@tonic-gate 			 * lwp_suspend() and are suspended inside
1360*7c478bd9Sstevel@tonic-gate 			 * of a system call to proceed to their
1361*7c478bd9Sstevel@tonic-gate 			 * holdlwp() points where they are clonable.
1362*7c478bd9Sstevel@tonic-gate 			 */
1363*7c478bd9Sstevel@tonic-gate 			if ((p->p_flag & SHOLDFORK) && SUSPENDED(t)) {
1364*7c478bd9Sstevel@tonic-gate 				if ((t->t_schedflag & TS_CSTART) == 0) {
1365*7c478bd9Sstevel@tonic-gate 					p->p_lwprcnt++;
1366*7c478bd9Sstevel@tonic-gate 					t->t_schedflag |= TS_CSTART;
1367*7c478bd9Sstevel@tonic-gate 					setrun_locked(t);
1368*7c478bd9Sstevel@tonic-gate 				}
1369*7c478bd9Sstevel@tonic-gate 			}
1370*7c478bd9Sstevel@tonic-gate 		} else if (t->t_state == TS_ONPROC) {
1371*7c478bd9Sstevel@tonic-gate 			if (t->t_cpu != CPU)
1372*7c478bd9Sstevel@tonic-gate 				poke_cpu(t->t_cpu->cpu_id);
1373*7c478bd9Sstevel@tonic-gate 		}
1374*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1375*7c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
1376*7c478bd9Sstevel@tonic-gate }
1377*7c478bd9Sstevel@tonic-gate 
1378*7c478bd9Sstevel@tonic-gate /*
1379*7c478bd9Sstevel@tonic-gate  * undo the effects of holdlwps() or holdwatch().
1380*7c478bd9Sstevel@tonic-gate  */
1381*7c478bd9Sstevel@tonic-gate void
1382*7c478bd9Sstevel@tonic-gate continuelwps(proc_t *p)
1383*7c478bd9Sstevel@tonic-gate {
1384*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
1385*7c478bd9Sstevel@tonic-gate 
1386*7c478bd9Sstevel@tonic-gate 	/*
1387*7c478bd9Sstevel@tonic-gate 	 * If this flag is set, then the original holdwatch() didn't actually
1388*7c478bd9Sstevel@tonic-gate 	 * stop the process.  See comments for holdwatch().
1389*7c478bd9Sstevel@tonic-gate 	 */
1390*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SWATCHOK) {
1391*7c478bd9Sstevel@tonic-gate 		ASSERT(curthread->t_proc_flag & TP_STOPPING);
1392*7c478bd9Sstevel@tonic-gate 		return;
1393*7c478bd9Sstevel@tonic-gate 	}
1394*7c478bd9Sstevel@tonic-gate 
1395*7c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1396*7c478bd9Sstevel@tonic-gate 	ASSERT((p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) == 0);
1397*7c478bd9Sstevel@tonic-gate 
1398*7c478bd9Sstevel@tonic-gate 	t = p->p_tlist;
1399*7c478bd9Sstevel@tonic-gate 	do {
1400*7c478bd9Sstevel@tonic-gate 		thread_lock(t);		/* SUSPENDED looks at t_schedflag */
1401*7c478bd9Sstevel@tonic-gate 		if (SUSPENDED(t) && !(t->t_proc_flag & TP_HOLDLWP)) {
1402*7c478bd9Sstevel@tonic-gate 			p->p_lwprcnt++;
1403*7c478bd9Sstevel@tonic-gate 			t->t_schedflag |= TS_CSTART;
1404*7c478bd9Sstevel@tonic-gate 			setrun_locked(t);
1405*7c478bd9Sstevel@tonic-gate 		}
1406*7c478bd9Sstevel@tonic-gate 		thread_unlock(t);
1407*7c478bd9Sstevel@tonic-gate 	} while ((t = t->t_forw) != p->p_tlist);
1408*7c478bd9Sstevel@tonic-gate }
1409*7c478bd9Sstevel@tonic-gate 
1410*7c478bd9Sstevel@tonic-gate /*
1411*7c478bd9Sstevel@tonic-gate  * Force all other LWPs in the current process other than the caller to exit,
1412*7c478bd9Sstevel@tonic-gate  * and then cv_wait() on p_holdlwps for them to exit.  The exitlwps() function
1413*7c478bd9Sstevel@tonic-gate  * is typically used in these situations:
1414*7c478bd9Sstevel@tonic-gate  *
1415*7c478bd9Sstevel@tonic-gate  *   (a) prior to an exec() system call
1416*7c478bd9Sstevel@tonic-gate  *   (b) prior to dumping a core file
1417*7c478bd9Sstevel@tonic-gate  *   (c) prior to a uadmin() shutdown
1418*7c478bd9Sstevel@tonic-gate  *
1419*7c478bd9Sstevel@tonic-gate  * If the 'coredump' flag is set, other LWPs are quiesced but not destroyed.
1420*7c478bd9Sstevel@tonic-gate  * Multiple threads in the process can call this function at one time by
1421*7c478bd9Sstevel@tonic-gate  * triggering execs or core dumps simultaneously, so the SEXITLWPS bit is used
1422*7c478bd9Sstevel@tonic-gate  * to declare one particular thread the winner who gets to kill the others.
1423*7c478bd9Sstevel@tonic-gate  * If a thread wins the exitlwps() dance, zero is returned; otherwise an
1424*7c478bd9Sstevel@tonic-gate  * appropriate errno value is returned to caller for its system call to return.
1425*7c478bd9Sstevel@tonic-gate  */
1426*7c478bd9Sstevel@tonic-gate int
1427*7c478bd9Sstevel@tonic-gate exitlwps(int coredump)
1428*7c478bd9Sstevel@tonic-gate {
1429*7c478bd9Sstevel@tonic-gate 	proc_t *p = curproc;
1430*7c478bd9Sstevel@tonic-gate 	int heldcnt;
1431*7c478bd9Sstevel@tonic-gate 
1432*7c478bd9Sstevel@tonic-gate 	if (curthread->t_door)
1433*7c478bd9Sstevel@tonic-gate 		door_slam();
1434*7c478bd9Sstevel@tonic-gate 	if (p->p_door_list)
1435*7c478bd9Sstevel@tonic-gate 		door_revoke_all();
1436*7c478bd9Sstevel@tonic-gate 	if (curthread->t_schedctl != NULL)
1437*7c478bd9Sstevel@tonic-gate 		schedctl_lwp_cleanup(curthread);
1438*7c478bd9Sstevel@tonic-gate 
1439*7c478bd9Sstevel@tonic-gate 	/*
1440*7c478bd9Sstevel@tonic-gate 	 * Ensure that before starting to wait for other lwps to exit,
1441*7c478bd9Sstevel@tonic-gate 	 * cleanup all upimutexes held by curthread. Otherwise, some other
1442*7c478bd9Sstevel@tonic-gate 	 * lwp could be waiting (uninterruptibly) for a upimutex held by
1443*7c478bd9Sstevel@tonic-gate 	 * curthread, and the call to pokelwps() below would deadlock.
1444*7c478bd9Sstevel@tonic-gate 	 * Even if a blocked upimutex_lock is made interruptible,
1445*7c478bd9Sstevel@tonic-gate 	 * curthread's upimutexes need to be unlocked: do it here.
1446*7c478bd9Sstevel@tonic-gate 	 */
1447*7c478bd9Sstevel@tonic-gate 	if (curthread->t_upimutex != NULL)
1448*7c478bd9Sstevel@tonic-gate 		upimutex_cleanup();
1449*7c478bd9Sstevel@tonic-gate 
1450*7c478bd9Sstevel@tonic-gate 	/*
1451*7c478bd9Sstevel@tonic-gate 	 * Grab p_lock in order to check and set SEXITLWPS to declare a winner.
1452*7c478bd9Sstevel@tonic-gate 	 * We must also block any further /proc access from this point forward.
1453*7c478bd9Sstevel@tonic-gate 	 */
1454*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1455*7c478bd9Sstevel@tonic-gate 	prbarrier(p);
1456*7c478bd9Sstevel@tonic-gate 
1457*7c478bd9Sstevel@tonic-gate 	if (p->p_flag & SEXITLWPS) {
1458*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1459*7c478bd9Sstevel@tonic-gate 		aston(curthread);	/* force a trip through post_syscall */
1460*7c478bd9Sstevel@tonic-gate 		return (set_errno(EINTR));
1461*7c478bd9Sstevel@tonic-gate 	}
1462*7c478bd9Sstevel@tonic-gate 
1463*7c478bd9Sstevel@tonic-gate 	p->p_flag |= SEXITLWPS;
1464*7c478bd9Sstevel@tonic-gate 	if (coredump)		/* tell other lwps to stop, not exit */
1465*7c478bd9Sstevel@tonic-gate 		p->p_flag |= SCOREDUMP;
1466*7c478bd9Sstevel@tonic-gate 
1467*7c478bd9Sstevel@tonic-gate 	/*
1468*7c478bd9Sstevel@tonic-gate 	 * Give precedence to exitlwps() if a holdlwps() is
1469*7c478bd9Sstevel@tonic-gate 	 * in progress. The lwp doing the holdlwps() operation
1470*7c478bd9Sstevel@tonic-gate 	 * is aborted when it is awakened.
1471*7c478bd9Sstevel@tonic-gate 	 */
1472*7c478bd9Sstevel@tonic-gate 	while (p->p_flag & (SHOLDFORK | SHOLDFORK1 | SHOLDWATCH)) {
1473*7c478bd9Sstevel@tonic-gate 		cv_broadcast(&p->p_holdlwps);
1474*7c478bd9Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
1475*7c478bd9Sstevel@tonic-gate 	}
1476*7c478bd9Sstevel@tonic-gate 	p->p_flag |= SHOLDFORK;
1477*7c478bd9Sstevel@tonic-gate 	pokelwps(p);
1478*7c478bd9Sstevel@tonic-gate 
1479*7c478bd9Sstevel@tonic-gate 	/*
1480*7c478bd9Sstevel@tonic-gate 	 * Wait for process to become quiescent.
1481*7c478bd9Sstevel@tonic-gate 	 */
1482*7c478bd9Sstevel@tonic-gate 	--p->p_lwprcnt;
1483*7c478bd9Sstevel@tonic-gate 	while (p->p_lwprcnt > 0)
1484*7c478bd9Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
1485*7c478bd9Sstevel@tonic-gate 	p->p_lwprcnt++;
1486*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_lwprcnt == 1);
1487*7c478bd9Sstevel@tonic-gate 
1488*7c478bd9Sstevel@tonic-gate 	/*
1489*7c478bd9Sstevel@tonic-gate 	 * The SCOREDUMP flag puts the process into a quiescent
1490*7c478bd9Sstevel@tonic-gate 	 * state.  The process's lwps remain attached to this
1491*7c478bd9Sstevel@tonic-gate 	 * process until exitlwps() is called again without the
1492*7c478bd9Sstevel@tonic-gate 	 * 'coredump' flag set, then the lwps are terminated
1493*7c478bd9Sstevel@tonic-gate 	 * and the process can exit.
1494*7c478bd9Sstevel@tonic-gate 	 */
1495*7c478bd9Sstevel@tonic-gate 	if (coredump) {
1496*7c478bd9Sstevel@tonic-gate 		p->p_flag &= ~(SCOREDUMP | SHOLDFORK | SEXITLWPS);
1497*7c478bd9Sstevel@tonic-gate 		goto out;
1498*7c478bd9Sstevel@tonic-gate 	}
1499*7c478bd9Sstevel@tonic-gate 
1500*7c478bd9Sstevel@tonic-gate 	/*
1501*7c478bd9Sstevel@tonic-gate 	 * Determine if there are any lwps left dangling in
1502*7c478bd9Sstevel@tonic-gate 	 * the stopped state.  This happens when exitlwps()
1503*7c478bd9Sstevel@tonic-gate 	 * aborts a holdlwps() operation.
1504*7c478bd9Sstevel@tonic-gate 	 */
1505*7c478bd9Sstevel@tonic-gate 	p->p_flag &= ~SHOLDFORK;
1506*7c478bd9Sstevel@tonic-gate 	if ((heldcnt = p->p_lwpcnt) > 1) {
1507*7c478bd9Sstevel@tonic-gate 		kthread_t *t;
1508*7c478bd9Sstevel@tonic-gate 		for (t = curthread->t_forw; --heldcnt > 0; t = t->t_forw) {
1509*7c478bd9Sstevel@tonic-gate 			t->t_proc_flag &= ~TP_TWAIT;
1510*7c478bd9Sstevel@tonic-gate 			lwp_continue(t);
1511*7c478bd9Sstevel@tonic-gate 		}
1512*7c478bd9Sstevel@tonic-gate 	}
1513*7c478bd9Sstevel@tonic-gate 
1514*7c478bd9Sstevel@tonic-gate 	/*
1515*7c478bd9Sstevel@tonic-gate 	 * Wait for all other lwps to exit.
1516*7c478bd9Sstevel@tonic-gate 	 */
1517*7c478bd9Sstevel@tonic-gate 	--p->p_lwprcnt;
1518*7c478bd9Sstevel@tonic-gate 	while (p->p_lwpcnt > 1)
1519*7c478bd9Sstevel@tonic-gate 		cv_wait(&p->p_holdlwps, &p->p_lock);
1520*7c478bd9Sstevel@tonic-gate 	++p->p_lwprcnt;
1521*7c478bd9Sstevel@tonic-gate 	ASSERT(p->p_lwpcnt == 1 && p->p_lwprcnt == 1);
1522*7c478bd9Sstevel@tonic-gate 
1523*7c478bd9Sstevel@tonic-gate 	p->p_flag &= ~SEXITLWPS;
1524*7c478bd9Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_TWAIT;
1525*7c478bd9Sstevel@tonic-gate 
1526*7c478bd9Sstevel@tonic-gate out:
1527*7c478bd9Sstevel@tonic-gate 	if (!coredump && p->p_zombcnt) {	/* cleanup the zombie lwps */
1528*7c478bd9Sstevel@tonic-gate 		lwpdir_t *ldp;
1529*7c478bd9Sstevel@tonic-gate 		lwpent_t *lep;
1530*7c478bd9Sstevel@tonic-gate 		int i;
1531*7c478bd9Sstevel@tonic-gate 
1532*7c478bd9Sstevel@tonic-gate 		prbarrier(p);
1533*7c478bd9Sstevel@tonic-gate 		for (ldp = p->p_lwpdir, i = 0; i < p->p_lwpdir_sz; i++, ldp++) {
1534*7c478bd9Sstevel@tonic-gate 			lep = ldp->ld_entry;
1535*7c478bd9Sstevel@tonic-gate 			if (lep != NULL && lep->le_thread != curthread) {
1536*7c478bd9Sstevel@tonic-gate 				ASSERT(lep->le_thread == NULL);
1537*7c478bd9Sstevel@tonic-gate 				p->p_zombcnt--;
1538*7c478bd9Sstevel@tonic-gate 				lwp_hash_out(p, lep->le_lwpid);
1539*7c478bd9Sstevel@tonic-gate 			}
1540*7c478bd9Sstevel@tonic-gate 		}
1541*7c478bd9Sstevel@tonic-gate 		ASSERT(p->p_zombcnt == 0);
1542*7c478bd9Sstevel@tonic-gate 	}
1543*7c478bd9Sstevel@tonic-gate 
1544*7c478bd9Sstevel@tonic-gate 	/*
1545*7c478bd9Sstevel@tonic-gate 	 * If some other LWP in the process wanted us to suspend ourself,
1546*7c478bd9Sstevel@tonic-gate 	 * then we will not do it.  The other LWP is now terminated and
1547*7c478bd9Sstevel@tonic-gate 	 * no one will ever continue us again if we suspend ourself.
1548*7c478bd9Sstevel@tonic-gate 	 */
1549*7c478bd9Sstevel@tonic-gate 	curthread->t_proc_flag &= ~TP_HOLDLWP;
1550*7c478bd9Sstevel@tonic-gate 	p->p_flag &= ~(SHOLDFORK | SHOLDFORK1 | SHOLDWATCH | SLWPWRAP);
1551*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1552*7c478bd9Sstevel@tonic-gate 	return (0);
1553*7c478bd9Sstevel@tonic-gate }
1554*7c478bd9Sstevel@tonic-gate 
1555*7c478bd9Sstevel@tonic-gate /*
1556*7c478bd9Sstevel@tonic-gate  * duplicate a lwp.
1557*7c478bd9Sstevel@tonic-gate  */
1558*7c478bd9Sstevel@tonic-gate klwp_t *
1559*7c478bd9Sstevel@tonic-gate forklwp(klwp_t *lwp, proc_t *cp, id_t lwpid)
1560*7c478bd9Sstevel@tonic-gate {
1561*7c478bd9Sstevel@tonic-gate 	klwp_t *clwp;
1562*7c478bd9Sstevel@tonic-gate 	void *tregs, *tfpu;
1563*7c478bd9Sstevel@tonic-gate 	kthread_t *t = lwptot(lwp);
1564*7c478bd9Sstevel@tonic-gate 	kthread_t *ct;
1565*7c478bd9Sstevel@tonic-gate 	proc_t *p = lwptoproc(lwp);
1566*7c478bd9Sstevel@tonic-gate 	int cid;
1567*7c478bd9Sstevel@tonic-gate 	void *bufp;
1568*7c478bd9Sstevel@tonic-gate 	int val;
1569*7c478bd9Sstevel@tonic-gate 
1570*7c478bd9Sstevel@tonic-gate 	ASSERT(p == curproc);
1571*7c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread || (SUSPENDED(t) && lwp->lwp_asleep == 0));
1572*7c478bd9Sstevel@tonic-gate 
1573*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
1574*7c478bd9Sstevel@tonic-gate 	if (t == curthread)
1575*7c478bd9Sstevel@tonic-gate 		(void) flush_user_windows_to_stack(NULL);
1576*7c478bd9Sstevel@tonic-gate #endif
1577*7c478bd9Sstevel@tonic-gate 
1578*7c478bd9Sstevel@tonic-gate 	if (t == curthread)
1579*7c478bd9Sstevel@tonic-gate 		/* copy args out of registers first */
1580*7c478bd9Sstevel@tonic-gate 		(void) save_syscall_args();
1581*7c478bd9Sstevel@tonic-gate 	clwp = lwp_create(cp->p_lwpcnt == 0 ? lwp_rtt_initial : lwp_rtt,
1582*7c478bd9Sstevel@tonic-gate 	    NULL, 0, cp, TS_STOPPED, t->t_pri, &t->t_hold, NOCLASS, lwpid);
1583*7c478bd9Sstevel@tonic-gate 	if (clwp == NULL)
1584*7c478bd9Sstevel@tonic-gate 		return (NULL);
1585*7c478bd9Sstevel@tonic-gate 
1586*7c478bd9Sstevel@tonic-gate 	/*
1587*7c478bd9Sstevel@tonic-gate 	 * most of the parent's lwp can be copied to its duplicate,
1588*7c478bd9Sstevel@tonic-gate 	 * except for the fields that are unique to each lwp, like
1589*7c478bd9Sstevel@tonic-gate 	 * lwp_thread, lwp_procp, lwp_regs, and lwp_ap.
1590*7c478bd9Sstevel@tonic-gate 	 */
1591*7c478bd9Sstevel@tonic-gate 	ct = clwp->lwp_thread;
1592*7c478bd9Sstevel@tonic-gate 	tregs = clwp->lwp_regs;
1593*7c478bd9Sstevel@tonic-gate 	tfpu = clwp->lwp_fpu;
1594*7c478bd9Sstevel@tonic-gate 
1595*7c478bd9Sstevel@tonic-gate 	/* copy parent lwp to child lwp */
1596*7c478bd9Sstevel@tonic-gate 	*clwp = *lwp;
1597*7c478bd9Sstevel@tonic-gate 
1598*7c478bd9Sstevel@tonic-gate 	/* fix up child's lwp */
1599*7c478bd9Sstevel@tonic-gate 
1600*7c478bd9Sstevel@tonic-gate 	clwp->lwp_pcb.pcb_flags = 0;
1601*7c478bd9Sstevel@tonic-gate #if defined(__sparc)
1602*7c478bd9Sstevel@tonic-gate 	clwp->lwp_pcb.pcb_step = STEP_NONE;
1603*7c478bd9Sstevel@tonic-gate #endif
1604*7c478bd9Sstevel@tonic-gate 	clwp->lwp_cursig = 0;
1605*7c478bd9Sstevel@tonic-gate 	clwp->lwp_extsig = 0;
1606*7c478bd9Sstevel@tonic-gate 	clwp->lwp_curinfo = (struct sigqueue *)0;
1607*7c478bd9Sstevel@tonic-gate 	clwp->lwp_thread = ct;
1608*7c478bd9Sstevel@tonic-gate 	ct->t_sysnum = t->t_sysnum;
1609*7c478bd9Sstevel@tonic-gate 	clwp->lwp_regs = tregs;
1610*7c478bd9Sstevel@tonic-gate 	clwp->lwp_fpu = tfpu;
1611*7c478bd9Sstevel@tonic-gate 	clwp->lwp_ap = clwp->lwp_arg;
1612*7c478bd9Sstevel@tonic-gate 	clwp->lwp_procp = cp;
1613*7c478bd9Sstevel@tonic-gate 	bzero(clwp->lwp_timer, sizeof (clwp->lwp_timer));
1614*7c478bd9Sstevel@tonic-gate 	init_mstate(ct, LMS_STOPPED);
1615*7c478bd9Sstevel@tonic-gate 	bzero(&clwp->lwp_ru, sizeof (clwp->lwp_ru));
1616*7c478bd9Sstevel@tonic-gate 	clwp->lwp_lastfault = 0;
1617*7c478bd9Sstevel@tonic-gate 	clwp->lwp_lastfaddr = 0;
1618*7c478bd9Sstevel@tonic-gate 
1619*7c478bd9Sstevel@tonic-gate 	/* copy parent's struct regs to child. */
1620*7c478bd9Sstevel@tonic-gate 	lwp_forkregs(lwp, clwp);
1621*7c478bd9Sstevel@tonic-gate 
1622*7c478bd9Sstevel@tonic-gate 	/*
1623*7c478bd9Sstevel@tonic-gate 	 * fork device context, if any.
1624*7c478bd9Sstevel@tonic-gate 	 *
1625*7c478bd9Sstevel@tonic-gate 	 * Someday we could do the work to support the possibility of
1626*7c478bd9Sstevel@tonic-gate 	 * forkctx() or lwp_createctx() failing.  Currently, this would
1627*7c478bd9Sstevel@tonic-gate 	 * only be needed on x86 for the occasional process using a
1628*7c478bd9Sstevel@tonic-gate 	 * private LDT.
1629*7c478bd9Sstevel@tonic-gate 	 */
1630*7c478bd9Sstevel@tonic-gate 	if (t->t_ctx)
1631*7c478bd9Sstevel@tonic-gate 		forkctx(t, ct);
1632*7c478bd9Sstevel@tonic-gate 
1633*7c478bd9Sstevel@tonic-gate 	/* fix door state in the child */
1634*7c478bd9Sstevel@tonic-gate 	if (t->t_door)
1635*7c478bd9Sstevel@tonic-gate 		door_fork(t, ct);
1636*7c478bd9Sstevel@tonic-gate 
1637*7c478bd9Sstevel@tonic-gate 	/* copy current contract templates, clear latest contracts */
1638*7c478bd9Sstevel@tonic-gate 	lwp_ctmpl_copy(clwp, lwp);
1639*7c478bd9Sstevel@tonic-gate 
1640*7c478bd9Sstevel@tonic-gate 	mutex_enter(&cp->p_lock);
1641*7c478bd9Sstevel@tonic-gate 	/* lwp_create() set the TP_HOLDLWP flag */
1642*7c478bd9Sstevel@tonic-gate 	if (!(t->t_proc_flag & TP_HOLDLWP))
1643*7c478bd9Sstevel@tonic-gate 		ct->t_proc_flag &= ~TP_HOLDLWP;
1644*7c478bd9Sstevel@tonic-gate 	if (cp->p_flag & SMSACCT)
1645*7c478bd9Sstevel@tonic-gate 		ct->t_proc_flag |= TP_MSACCT;
1646*7c478bd9Sstevel@tonic-gate 	mutex_exit(&cp->p_lock);
1647*7c478bd9Sstevel@tonic-gate 
1648*7c478bd9Sstevel@tonic-gate retry:
1649*7c478bd9Sstevel@tonic-gate 	cid = t->t_cid;
1650*7c478bd9Sstevel@tonic-gate 
1651*7c478bd9Sstevel@tonic-gate 	val = CL_ALLOC(&bufp, cid, KM_SLEEP);
1652*7c478bd9Sstevel@tonic-gate 	ASSERT(val == 0);
1653*7c478bd9Sstevel@tonic-gate 
1654*7c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
1655*7c478bd9Sstevel@tonic-gate 	if (cid != t->t_cid) {
1656*7c478bd9Sstevel@tonic-gate 		/*
1657*7c478bd9Sstevel@tonic-gate 		 * Someone just changed this thread's scheduling class,
1658*7c478bd9Sstevel@tonic-gate 		 * so try pre-allocating the buffer again.  Hopefully we
1659*7c478bd9Sstevel@tonic-gate 		 * don't hit this often.
1660*7c478bd9Sstevel@tonic-gate 		 */
1661*7c478bd9Sstevel@tonic-gate 		mutex_exit(&p->p_lock);
1662*7c478bd9Sstevel@tonic-gate 		CL_FREE(cid, bufp);
1663*7c478bd9Sstevel@tonic-gate 		goto retry;
1664*7c478bd9Sstevel@tonic-gate 	}
1665*7c478bd9Sstevel@tonic-gate 
1666*7c478bd9Sstevel@tonic-gate 	ct->t_unpark = t->t_unpark;
1667*7c478bd9Sstevel@tonic-gate 	ct->t_clfuncs = t->t_clfuncs;
1668*7c478bd9Sstevel@tonic-gate 	CL_FORK(t, ct, bufp);
1669*7c478bd9Sstevel@tonic-gate 	ct->t_cid = t->t_cid;	/* after data allocated so prgetpsinfo works */
1670*7c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1671*7c478bd9Sstevel@tonic-gate 
1672*7c478bd9Sstevel@tonic-gate 	return (clwp);
1673*7c478bd9Sstevel@tonic-gate }
1674*7c478bd9Sstevel@tonic-gate 
1675*7c478bd9Sstevel@tonic-gate /*
1676*7c478bd9Sstevel@tonic-gate  * Add a new lwp entry to the lwp directory and to the lwpid hash table.
1677*7c478bd9Sstevel@tonic-gate  */
1678*7c478bd9Sstevel@tonic-gate void
1679*7c478bd9Sstevel@tonic-gate lwp_hash_in(proc_t *p, lwpent_t *lep)
1680*7c478bd9Sstevel@tonic-gate {
1681*7c478bd9Sstevel@tonic-gate 	lwpdir_t **ldpp;
1682*7c478bd9Sstevel@tonic-gate 	lwpdir_t *ldp;
1683*7c478bd9Sstevel@tonic-gate 	kthread_t *t;
1684*7c478bd9Sstevel@tonic-gate 
1685*7c478bd9Sstevel@tonic-gate 	/*
1686*7c478bd9Sstevel@tonic-gate 	 * Allocate a directory element from the free list.
1687*7c478bd9Sstevel@tonic-gate 	 * Code elsewhere guarantees a free slot.
1688*7c478bd9Sstevel@tonic-gate 	 */
1689*7c478bd9Sstevel@tonic-gate 	ldp = p->p_lwpfree;
1690*7c478bd9Sstevel@tonic-gate 	p->p_lwpfree = ldp->ld_next;
1691*7c478bd9Sstevel@tonic-gate 	ASSERT(ldp->ld_entry == NULL);
1692*7c478bd9Sstevel@tonic-gate 	ldp->ld_entry = lep;
1693*7c478bd9Sstevel@tonic-gate 
1694*7c478bd9Sstevel@tonic-gate 	/*
1695*7c478bd9Sstevel@tonic-gate 	 * Insert it into the lwpid hash table.
1696*7c478bd9Sstevel@tonic-gate 	 */
1697*7c478bd9Sstevel@tonic-gate 	ldpp = &p->p_tidhash[TIDHASH(p, lep->le_lwpid)];
1698*7c478bd9Sstevel@tonic-gate 	ldp->ld_next = *ldpp;
1699*7c478bd9Sstevel@tonic-gate 	*ldpp = ldp;
1700*7c478bd9Sstevel@tonic-gate 
1701*7c478bd9Sstevel@tonic-gate 	/*
1702*7c478bd9Sstevel@tonic-gate 	 * Set the active thread's directory slot entry.
1703*7c478bd9Sstevel@tonic-gate 	 */
1704*7c478bd9Sstevel@tonic-gate 	if ((t = lep->le_thread) != NULL) {
1705*7c478bd9Sstevel@tonic-gate 		ASSERT(lep->le_lwpid == t->t_tid);
1706*7c478bd9Sstevel@tonic-gate 		t->t_dslot = (int)(ldp - p->p_lwpdir);
1707*7c478bd9Sstevel@tonic-gate 	}
1708*7c478bd9Sstevel@tonic-gate }
1709*7c478bd9Sstevel@tonic-gate 
1710*7c478bd9Sstevel@tonic-gate /*
1711*7c478bd9Sstevel@tonic-gate  * Remove an lwp from the lwpid hash table and free its directory entry.
1712*7c478bd9Sstevel@tonic-gate  * This is done when a detached lwp exits in lwp_exit() or
1713*7c478bd9Sstevel@tonic-gate  * when a non-detached lwp is waited for in lwp_wait() or
1714*7c478bd9Sstevel@tonic-gate  * when a zombie lwp is detached in lwp_detach().
1715*7c478bd9Sstevel@tonic-gate  */
1716*7c478bd9Sstevel@tonic-gate void
1717*7c478bd9Sstevel@tonic-gate lwp_hash_out(proc_t *p, id_t lwpid)
1718*7c478bd9Sstevel@tonic-gate {
1719*7c478bd9Sstevel@tonic-gate 	lwpdir_t **ldpp;
1720*7c478bd9Sstevel@tonic-gate 	lwpdir_t *ldp;
1721*7c478bd9Sstevel@tonic-gate 	lwpent_t *lep;
1722*7c478bd9Sstevel@tonic-gate 
1723*7c478bd9Sstevel@tonic-gate 	for (ldpp = &p->p_tidhash[TIDHASH(p, lwpid)];
1724*7c478bd9Sstevel@tonic-gate 	    (ldp = *ldpp) != NULL; ldpp = &ldp->ld_next) {
1725*7c478bd9Sstevel@tonic-gate 		lep = ldp->ld_entry;
1726*7c478bd9Sstevel@tonic-gate 		if (lep->le_lwpid == lwpid) {
1727*7c478bd9Sstevel@tonic-gate 			prlwpfree(p, lep);	/* /proc deals with le_trace */
1728*7c478bd9Sstevel@tonic-gate 			*ldpp = ldp->ld_next;
1729*7c478bd9Sstevel@tonic-gate 			ldp->ld_entry = NULL;
1730*7c478bd9Sstevel@tonic-gate 			ldp->ld_next = p->p_lwpfree;
1731*7c478bd9Sstevel@tonic-gate 			p->p_lwpfree = ldp;
1732*7c478bd9Sstevel@tonic-gate 			kmem_free(lep, sizeof (*lep));
1733*7c478bd9Sstevel@tonic-gate 			break;
1734*7c478bd9Sstevel@tonic-gate 		}
1735*7c478bd9Sstevel@tonic-gate 	}
1736*7c478bd9Sstevel@tonic-gate }
1737*7c478bd9Sstevel@tonic-gate 
1738*7c478bd9Sstevel@tonic-gate /*
1739*7c478bd9Sstevel@tonic-gate  * Lookup an lwp in the lwpid hash table by lwpid.
1740*7c478bd9Sstevel@tonic-gate  */
1741*7c478bd9Sstevel@tonic-gate lwpdir_t *
1742*7c478bd9Sstevel@tonic-gate lwp_hash_lookup(proc_t *p, id_t lwpid)
1743*7c478bd9Sstevel@tonic-gate {
1744*7c478bd9Sstevel@tonic-gate 	lwpdir_t *ldp;
1745*7c478bd9Sstevel@tonic-gate 
1746*7c478bd9Sstevel@tonic-gate 	/*
1747*7c478bd9Sstevel@tonic-gate 	 * The process may be exiting, after p_tidhash has been set to NULL in
1748*7c478bd9Sstevel@tonic-gate 	 * proc_exit() but before prfee() has been called.  Return failure in
1749*7c478bd9Sstevel@tonic-gate 	 * this case.
1750*7c478bd9Sstevel@tonic-gate 	 */
1751*7c478bd9Sstevel@tonic-gate 	if (p->p_tidhash == NULL)
1752*7c478bd9Sstevel@tonic-gate 		return (NULL);
1753*7c478bd9Sstevel@tonic-gate 
1754*7c478bd9Sstevel@tonic-gate 	for (ldp = p->p_tidhash[TIDHASH(p, lwpid)];
1755*7c478bd9Sstevel@tonic-gate 	    ldp != NULL; ldp = ldp->ld_next) {
1756*7c478bd9Sstevel@tonic-gate 		if (ldp->ld_entry->le_lwpid == lwpid)
1757*7c478bd9Sstevel@tonic-gate 			return (ldp);
1758*7c478bd9Sstevel@tonic-gate 	}
1759*7c478bd9Sstevel@tonic-gate 
1760*7c478bd9Sstevel@tonic-gate 	return (NULL);
1761*7c478bd9Sstevel@tonic-gate }
1762*7c478bd9Sstevel@tonic-gate 
1763*7c478bd9Sstevel@tonic-gate /*
1764*7c478bd9Sstevel@tonic-gate  * Update the indicated LWP usage statistic for the current LWP.
1765*7c478bd9Sstevel@tonic-gate  */
1766*7c478bd9Sstevel@tonic-gate void
1767*7c478bd9Sstevel@tonic-gate lwp_stat_update(lwp_stat_id_t lwp_stat_id, long inc)
1768*7c478bd9Sstevel@tonic-gate {
1769*7c478bd9Sstevel@tonic-gate 	klwp_t *lwp = ttolwp(curthread);
1770*7c478bd9Sstevel@tonic-gate 
1771*7c478bd9Sstevel@tonic-gate 	if (lwp == NULL)
1772*7c478bd9Sstevel@tonic-gate 		return;
1773*7c478bd9Sstevel@tonic-gate 
1774*7c478bd9Sstevel@tonic-gate 	switch (lwp_stat_id) {
1775*7c478bd9Sstevel@tonic-gate 	case LWP_STAT_INBLK:
1776*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.inblock += inc;
1777*7c478bd9Sstevel@tonic-gate 		break;
1778*7c478bd9Sstevel@tonic-gate 	case LWP_STAT_OUBLK:
1779*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.oublock += inc;
1780*7c478bd9Sstevel@tonic-gate 		break;
1781*7c478bd9Sstevel@tonic-gate 	case LWP_STAT_MSGRCV:
1782*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.msgrcv += inc;
1783*7c478bd9Sstevel@tonic-gate 		break;
1784*7c478bd9Sstevel@tonic-gate 	case LWP_STAT_MSGSND:
1785*7c478bd9Sstevel@tonic-gate 		lwp->lwp_ru.msgsnd += inc;
1786*7c478bd9Sstevel@tonic-gate 		break;
1787*7c478bd9Sstevel@tonic-gate 	default:
1788*7c478bd9Sstevel@tonic-gate 		panic("lwp_stat_update: invalid lwp_stat_id 0x%x", lwp_stat_id);
1789*7c478bd9Sstevel@tonic-gate 	}
1790*7c478bd9Sstevel@tonic-gate }
1791