xref: /onnv-gate/usr/src/uts/common/disp/thread.c (revision 13134:8315ff49e22e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51676Sjpk  * Common Development and Distribution License (the "License").
61676Sjpk  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
216247Sraf 
220Sstevel@tonic-gate /*
23*13134Skuriakose.kuruvilla@oracle.com  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #include <sys/types.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/sysmacros.h>
290Sstevel@tonic-gate #include <sys/signal.h>
300Sstevel@tonic-gate #include <sys/stack.h>
310Sstevel@tonic-gate #include <sys/pcb.h>
320Sstevel@tonic-gate #include <sys/user.h>
330Sstevel@tonic-gate #include <sys/systm.h>
340Sstevel@tonic-gate #include <sys/sysinfo.h>
350Sstevel@tonic-gate #include <sys/errno.h>
360Sstevel@tonic-gate #include <sys/cmn_err.h>
370Sstevel@tonic-gate #include <sys/cred.h>
380Sstevel@tonic-gate #include <sys/resource.h>
390Sstevel@tonic-gate #include <sys/task.h>
400Sstevel@tonic-gate #include <sys/project.h>
410Sstevel@tonic-gate #include <sys/proc.h>
420Sstevel@tonic-gate #include <sys/debug.h>
430Sstevel@tonic-gate #include <sys/disp.h>
440Sstevel@tonic-gate #include <sys/class.h>
450Sstevel@tonic-gate #include <vm/seg_kmem.h>
460Sstevel@tonic-gate #include <vm/seg_kp.h>
470Sstevel@tonic-gate #include <sys/machlock.h>
480Sstevel@tonic-gate #include <sys/kmem.h>
490Sstevel@tonic-gate #include <sys/varargs.h>
500Sstevel@tonic-gate #include <sys/turnstile.h>
510Sstevel@tonic-gate #include <sys/poll.h>
520Sstevel@tonic-gate #include <sys/vtrace.h>
530Sstevel@tonic-gate #include <sys/callb.h>
540Sstevel@tonic-gate #include <c2/audit.h>
550Sstevel@tonic-gate #include <sys/tnf.h>
560Sstevel@tonic-gate #include <sys/sobject.h>
570Sstevel@tonic-gate #include <sys/cpupart.h>
580Sstevel@tonic-gate #include <sys/pset.h>
590Sstevel@tonic-gate #include <sys/door.h>
600Sstevel@tonic-gate #include <sys/spl.h>
610Sstevel@tonic-gate #include <sys/copyops.h>
620Sstevel@tonic-gate #include <sys/rctl.h>
632712Snn35248 #include <sys/brand.h>
640Sstevel@tonic-gate #include <sys/pool.h>
650Sstevel@tonic-gate #include <sys/zone.h>
661676Sjpk #include <sys/tsol/label.h>
671676Sjpk #include <sys/tsol/tndb.h>
680Sstevel@tonic-gate #include <sys/cpc_impl.h>
690Sstevel@tonic-gate #include <sys/sdt.h>
700Sstevel@tonic-gate #include <sys/reboot.h>
710Sstevel@tonic-gate #include <sys/kdi.h>
726247Sraf #include <sys/schedctl.h>
733792Sakolb #include <sys/waitq.h>
743792Sakolb #include <sys/cpucaps.h>
755206Sis #include <sys/kiconv.h>
760Sstevel@tonic-gate 
770Sstevel@tonic-gate struct kmem_cache *thread_cache;	/* cache of free threads */
780Sstevel@tonic-gate struct kmem_cache *lwp_cache;		/* cache of free lwps */
790Sstevel@tonic-gate struct kmem_cache *turnstile_cache;	/* cache of free turnstiles */
800Sstevel@tonic-gate 
810Sstevel@tonic-gate /*
820Sstevel@tonic-gate  * allthreads is only for use by kmem_readers.  All kernel loops can use
830Sstevel@tonic-gate  * the current thread as a start/end point.
840Sstevel@tonic-gate  */
850Sstevel@tonic-gate static kthread_t *allthreads = &t0;	/* circular list of all threads */
860Sstevel@tonic-gate 
870Sstevel@tonic-gate static kcondvar_t reaper_cv;		/* synchronization var */
880Sstevel@tonic-gate kthread_t	*thread_deathrow;	/* circular list of reapable threads */
890Sstevel@tonic-gate kthread_t	*lwp_deathrow;		/* circular list of reapable threads */
900Sstevel@tonic-gate kmutex_t	reaplock;		/* protects lwp and thread deathrows */
910Sstevel@tonic-gate int	thread_reapcnt = 0;		/* number of threads on deathrow */
920Sstevel@tonic-gate int	lwp_reapcnt = 0;		/* number of lwps on deathrow */
930Sstevel@tonic-gate int	reaplimit = 16;			/* delay reaping until reaplimit */
940Sstevel@tonic-gate 
955788Smv143129 thread_free_lock_t	*thread_free_lock;
965788Smv143129 					/* protects tick thread from reaper */
975788Smv143129 
980Sstevel@tonic-gate extern int nthread;
990Sstevel@tonic-gate 
10011173SJonathan.Adams@Sun.COM /* System Scheduling classes. */
1010Sstevel@tonic-gate id_t	syscid;				/* system scheduling class ID */
10211173SJonathan.Adams@Sun.COM id_t	sysdccid = CLASS_UNUSED;	/* reset when SDC loads */
10311173SJonathan.Adams@Sun.COM 
1040Sstevel@tonic-gate void	*segkp_thread;			/* cookie for segkp pool */
1050Sstevel@tonic-gate 
1060Sstevel@tonic-gate int lwp_cache_sz = 32;
1070Sstevel@tonic-gate int t_cache_sz = 8;
1080Sstevel@tonic-gate static kt_did_t next_t_id = 1;
1090Sstevel@tonic-gate 
1106298Sakolb /* Default mode for thread binding to CPUs and processor sets */
1116298Sakolb int default_binding_mode = TB_ALLHARD;
1126298Sakolb 
1130Sstevel@tonic-gate /*
1140Sstevel@tonic-gate  * Min/Max stack sizes for stack size parameters
1150Sstevel@tonic-gate  */
1160Sstevel@tonic-gate #define	MAX_STKSIZE	(32 * DEFAULTSTKSZ)
1170Sstevel@tonic-gate #define	MIN_STKSIZE	DEFAULTSTKSZ
1180Sstevel@tonic-gate 
1190Sstevel@tonic-gate /*
1200Sstevel@tonic-gate  * default_stksize overrides lwp_default_stksize if it is set.
1210Sstevel@tonic-gate  */
1220Sstevel@tonic-gate int	default_stksize;
1230Sstevel@tonic-gate int	lwp_default_stksize;
1240Sstevel@tonic-gate 
1250Sstevel@tonic-gate static zone_key_t zone_thread_key;
1260Sstevel@tonic-gate 
1277854SPhilippe.Jung@Sun.COM unsigned int kmem_stackinfo;		/* stackinfo feature on-off */
1287854SPhilippe.Jung@Sun.COM kmem_stkinfo_t *kmem_stkinfo_log;	/* stackinfo circular log */
1297854SPhilippe.Jung@Sun.COM static kmutex_t kmem_stkinfo_lock;	/* protects kmem_stkinfo_log */
1307854SPhilippe.Jung@Sun.COM 
1310Sstevel@tonic-gate /*
1320Sstevel@tonic-gate  * forward declarations for internal thread specific data (tsd)
1330Sstevel@tonic-gate  */
1340Sstevel@tonic-gate static void *tsd_realloc(void *, size_t, size_t);
1350Sstevel@tonic-gate 
1364036Spraks void thread_reaper(void);
1374036Spraks 
1387854SPhilippe.Jung@Sun.COM /* forward declarations for stackinfo feature */
1397854SPhilippe.Jung@Sun.COM static void stkinfo_begin(kthread_t *);
1407854SPhilippe.Jung@Sun.COM static void stkinfo_end(kthread_t *);
1417854SPhilippe.Jung@Sun.COM static size_t stkinfo_percent(caddr_t, caddr_t, caddr_t);
1427854SPhilippe.Jung@Sun.COM 
1430Sstevel@tonic-gate /*ARGSUSED*/
1440Sstevel@tonic-gate static int
turnstile_constructor(void * buf,void * cdrarg,int kmflags)1450Sstevel@tonic-gate turnstile_constructor(void *buf, void *cdrarg, int kmflags)
1460Sstevel@tonic-gate {
1470Sstevel@tonic-gate 	bzero(buf, sizeof (turnstile_t));
1480Sstevel@tonic-gate 	return (0);
1490Sstevel@tonic-gate }
1500Sstevel@tonic-gate 
1510Sstevel@tonic-gate /*ARGSUSED*/
1520Sstevel@tonic-gate static void
turnstile_destructor(void * buf,void * cdrarg)1530Sstevel@tonic-gate turnstile_destructor(void *buf, void *cdrarg)
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	turnstile_t *ts = buf;
1560Sstevel@tonic-gate 
1570Sstevel@tonic-gate 	ASSERT(ts->ts_free == NULL);
1580Sstevel@tonic-gate 	ASSERT(ts->ts_waiters == 0);
1590Sstevel@tonic-gate 	ASSERT(ts->ts_inheritor == NULL);
1600Sstevel@tonic-gate 	ASSERT(ts->ts_sleepq[0].sq_first == NULL);
1610Sstevel@tonic-gate 	ASSERT(ts->ts_sleepq[1].sq_first == NULL);
1620Sstevel@tonic-gate }
1630Sstevel@tonic-gate 
1640Sstevel@tonic-gate void
thread_init(void)1650Sstevel@tonic-gate thread_init(void)
1660Sstevel@tonic-gate {
1670Sstevel@tonic-gate 	kthread_t *tp;
1680Sstevel@tonic-gate 	extern char sys_name[];
1690Sstevel@tonic-gate 	extern void idle();
1700Sstevel@tonic-gate 	struct cpu *cpu = CPU;
1715788Smv143129 	int i;
1725788Smv143129 	kmutex_t *lp;
1730Sstevel@tonic-gate 
1740Sstevel@tonic-gate 	mutex_init(&reaplock, NULL, MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
1755788Smv143129 	thread_free_lock =
1765788Smv143129 	    kmem_alloc(sizeof (thread_free_lock_t) * THREAD_FREE_NUM, KM_SLEEP);
1775788Smv143129 	for (i = 0; i < THREAD_FREE_NUM; i++) {
1785788Smv143129 		lp = &thread_free_lock[i].tf_lock;
1795788Smv143129 		mutex_init(lp, NULL, MUTEX_DEFAULT, NULL);
1805788Smv143129 	}
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1830Sstevel@tonic-gate 	thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
1840Sstevel@tonic-gate 	    PTR24_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	/*
1870Sstevel@tonic-gate 	 * "struct _klwp" includes a "struct pcb", which includes a
188*13134Skuriakose.kuruvilla@oracle.com 	 * "struct fpu", which needs to be 64-byte aligned on amd64
189*13134Skuriakose.kuruvilla@oracle.com 	 * (and even on i386) for xsave/xrstor.
1900Sstevel@tonic-gate 	 */
1910Sstevel@tonic-gate 	lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
192*13134Skuriakose.kuruvilla@oracle.com 	    64, NULL, NULL, NULL, NULL, NULL, 0);
1930Sstevel@tonic-gate #else
1940Sstevel@tonic-gate 	/*
1950Sstevel@tonic-gate 	 * Allocate thread structures from static_arena.  This prevents
1960Sstevel@tonic-gate 	 * issues where a thread tries to relocate its own thread
1970Sstevel@tonic-gate 	 * structure and touches it after the mapping has been suspended.
1980Sstevel@tonic-gate 	 */
1990Sstevel@tonic-gate 	thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
2000Sstevel@tonic-gate 	    PTR24_ALIGN, NULL, NULL, NULL, NULL, static_arena, 0);
2010Sstevel@tonic-gate 
2022005Selowe 	lwp_stk_cache_init();
2032005Selowe 
2040Sstevel@tonic-gate 	lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
2050Sstevel@tonic-gate 	    0, NULL, NULL, NULL, NULL, NULL, 0);
2060Sstevel@tonic-gate #endif
2070Sstevel@tonic-gate 
2080Sstevel@tonic-gate 	turnstile_cache = kmem_cache_create("turnstile_cache",
2090Sstevel@tonic-gate 	    sizeof (turnstile_t), 0,
2100Sstevel@tonic-gate 	    turnstile_constructor, turnstile_destructor, NULL, NULL, NULL, 0);
2110Sstevel@tonic-gate 
2121676Sjpk 	label_init();
2130Sstevel@tonic-gate 	cred_init();
2140Sstevel@tonic-gate 
2153792Sakolb 	/*
2163792Sakolb 	 * Initialize various resource management facilities.
2173792Sakolb 	 */
2180Sstevel@tonic-gate 	rctl_init();
2193792Sakolb 	cpucaps_init();
2203792Sakolb 	/*
2213792Sakolb 	 * Zone_init() should be called before project_init() so that project ID
2223792Sakolb 	 * for the first project is initialized correctly.
2233792Sakolb 	 */
2243792Sakolb 	zone_init();
2250Sstevel@tonic-gate 	project_init();
2262712Snn35248 	brand_init();
2275206Sis 	kiconv_init();
2280Sstevel@tonic-gate 	task_init();
2291676Sjpk 	tcache_init();
2300Sstevel@tonic-gate 	pool_init();
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	curthread->t_ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	/*
2350Sstevel@tonic-gate 	 * Originally, we had two parameters to set default stack
2360Sstevel@tonic-gate 	 * size: one for lwp's (lwp_default_stksize), and one for
2370Sstevel@tonic-gate 	 * kernel-only threads (DEFAULTSTKSZ, a.k.a. _defaultstksz).
2380Sstevel@tonic-gate 	 * Now we have a third parameter that overrides both if it is
2390Sstevel@tonic-gate 	 * set to a legal stack size, called default_stksize.
2400Sstevel@tonic-gate 	 */
2410Sstevel@tonic-gate 
2420Sstevel@tonic-gate 	if (default_stksize == 0) {
2430Sstevel@tonic-gate 		default_stksize = DEFAULTSTKSZ;
2440Sstevel@tonic-gate 	} else if (default_stksize % PAGESIZE != 0 ||
2450Sstevel@tonic-gate 	    default_stksize > MAX_STKSIZE ||
2460Sstevel@tonic-gate 	    default_stksize < MIN_STKSIZE) {
2470Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal stack size. Using %d",
2480Sstevel@tonic-gate 		    (int)DEFAULTSTKSZ);
2490Sstevel@tonic-gate 		default_stksize = DEFAULTSTKSZ;
2500Sstevel@tonic-gate 	} else {
2510Sstevel@tonic-gate 		lwp_default_stksize = default_stksize;
2520Sstevel@tonic-gate 	}
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	if (lwp_default_stksize == 0) {
2550Sstevel@tonic-gate 		lwp_default_stksize = default_stksize;
2560Sstevel@tonic-gate 	} else if (lwp_default_stksize % PAGESIZE != 0 ||
2570Sstevel@tonic-gate 	    lwp_default_stksize > MAX_STKSIZE ||
2580Sstevel@tonic-gate 	    lwp_default_stksize < MIN_STKSIZE) {
2590Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal stack size. Using %d",
2600Sstevel@tonic-gate 		    default_stksize);
2610Sstevel@tonic-gate 		lwp_default_stksize = default_stksize;
2620Sstevel@tonic-gate 	}
2630Sstevel@tonic-gate 
2640Sstevel@tonic-gate 	segkp_lwp = segkp_cache_init(segkp, lwp_cache_sz,
2650Sstevel@tonic-gate 	    lwp_default_stksize,
2660Sstevel@tonic-gate 	    (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED));
2670Sstevel@tonic-gate 
2680Sstevel@tonic-gate 	segkp_thread = segkp_cache_init(segkp, t_cache_sz,
2690Sstevel@tonic-gate 	    default_stksize, KPD_HASREDZONE | KPD_LOCKED | KPD_NO_ANON);
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate 	(void) getcid(sys_name, &syscid);
2720Sstevel@tonic-gate 	curthread->t_cid = syscid;	/* current thread is t0 */
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	/*
2750Sstevel@tonic-gate 	 * Set up the first CPU's idle thread.
2760Sstevel@tonic-gate 	 * It runs whenever the CPU has nothing worthwhile to do.
2770Sstevel@tonic-gate 	 */
2780Sstevel@tonic-gate 	tp = thread_create(NULL, 0, idle, NULL, 0, &p0, TS_STOPPED, -1);
2790Sstevel@tonic-gate 	cpu->cpu_idle_thread = tp;
2800Sstevel@tonic-gate 	tp->t_preempt = 1;
2810Sstevel@tonic-gate 	tp->t_disp_queue = cpu->cpu_disp;
2820Sstevel@tonic-gate 	ASSERT(tp->t_disp_queue != NULL);
2830Sstevel@tonic-gate 	tp->t_bound_cpu = cpu;
2840Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	/*
2870Sstevel@tonic-gate 	 * Registering a thread in the callback table is usually
2880Sstevel@tonic-gate 	 * done in the initialization code of the thread. In this
2890Sstevel@tonic-gate 	 * case, we do it right after thread creation to avoid
2900Sstevel@tonic-gate 	 * blocking idle thread while registering itself. It also
2910Sstevel@tonic-gate 	 * avoids the possibility of reregistration in case a CPU
2920Sstevel@tonic-gate 	 * restarts its idle thread.
2930Sstevel@tonic-gate 	 */
2940Sstevel@tonic-gate 	CALLB_CPR_INIT_SAFE(tp, "idle");
2950Sstevel@tonic-gate 
2960Sstevel@tonic-gate 	/*
2974036Spraks 	 * Create the thread_reaper daemon. From this point on, exited
2984036Spraks 	 * threads will get reaped.
2994036Spraks 	 */
3004036Spraks 	(void) thread_create(NULL, 0, (void (*)())thread_reaper,
3014036Spraks 	    NULL, 0, &p0, TS_RUN, minclsyspri);
3024036Spraks 
3034036Spraks 	/*
3040Sstevel@tonic-gate 	 * Finish initializing the kernel memory allocator now that
3050Sstevel@tonic-gate 	 * thread_create() is available.
3060Sstevel@tonic-gate 	 */
3070Sstevel@tonic-gate 	kmem_thread_init();
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
3100Sstevel@tonic-gate 		kdi_dvec_thravail();
3110Sstevel@tonic-gate }
3120Sstevel@tonic-gate 
3130Sstevel@tonic-gate /*
3140Sstevel@tonic-gate  * Create a thread.
3150Sstevel@tonic-gate  *
3160Sstevel@tonic-gate  * thread_create() blocks for memory if necessary.  It never fails.
3170Sstevel@tonic-gate  *
3180Sstevel@tonic-gate  * If stk is NULL, the thread is created at the base of the stack
3190Sstevel@tonic-gate  * and cannot be swapped.
3200Sstevel@tonic-gate  */
3210Sstevel@tonic-gate kthread_t *
thread_create(caddr_t stk,size_t stksize,void (* proc)(),void * arg,size_t len,proc_t * pp,int state,pri_t pri)3220Sstevel@tonic-gate thread_create(
3230Sstevel@tonic-gate 	caddr_t	stk,
3240Sstevel@tonic-gate 	size_t	stksize,
3250Sstevel@tonic-gate 	void	(*proc)(),
3260Sstevel@tonic-gate 	void	*arg,
3270Sstevel@tonic-gate 	size_t	len,
3280Sstevel@tonic-gate 	proc_t	 *pp,
3290Sstevel@tonic-gate 	int	state,
3300Sstevel@tonic-gate 	pri_t	pri)
3310Sstevel@tonic-gate {
3320Sstevel@tonic-gate 	kthread_t *t;
3330Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
3340Sstevel@tonic-gate 	turnstile_t *ts;
3350Sstevel@tonic-gate 
3360Sstevel@tonic-gate 	/*
3370Sstevel@tonic-gate 	 * Every thread keeps a turnstile around in case it needs to block.
3380Sstevel@tonic-gate 	 * The only reason the turnstile is not simply part of the thread
3390Sstevel@tonic-gate 	 * structure is that we may have to break the association whenever
3400Sstevel@tonic-gate 	 * more than one thread blocks on a given synchronization object.
3410Sstevel@tonic-gate 	 * From a memory-management standpoint, turnstiles are like the
3420Sstevel@tonic-gate 	 * "attached mblks" that hang off dblks in the streams allocator.
3430Sstevel@tonic-gate 	 */
3440Sstevel@tonic-gate 	ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
3450Sstevel@tonic-gate 
3460Sstevel@tonic-gate 	if (stk == NULL) {
3470Sstevel@tonic-gate 		/*
3480Sstevel@tonic-gate 		 * alloc both thread and stack in segkp chunk
3490Sstevel@tonic-gate 		 */
3500Sstevel@tonic-gate 
3510Sstevel@tonic-gate 		if (stksize < default_stksize)
3520Sstevel@tonic-gate 			stksize = default_stksize;
3530Sstevel@tonic-gate 
3540Sstevel@tonic-gate 		if (stksize == default_stksize) {
3550Sstevel@tonic-gate 			stk = (caddr_t)segkp_cache_get(segkp_thread);
3560Sstevel@tonic-gate 		} else {
3570Sstevel@tonic-gate 			stksize = roundup(stksize, PAGESIZE);
3580Sstevel@tonic-gate 			stk = (caddr_t)segkp_get(segkp, stksize,
3590Sstevel@tonic-gate 			    (KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED));
3600Sstevel@tonic-gate 		}
3610Sstevel@tonic-gate 
3620Sstevel@tonic-gate 		ASSERT(stk != NULL);
3630Sstevel@tonic-gate 
3640Sstevel@tonic-gate 		/*
3650Sstevel@tonic-gate 		 * The machine-dependent mutex code may require that
3660Sstevel@tonic-gate 		 * thread pointers (since they may be used for mutex owner
3670Sstevel@tonic-gate 		 * fields) have certain alignment requirements.
3680Sstevel@tonic-gate 		 * PTR24_ALIGN is the size of the alignment quanta.
3690Sstevel@tonic-gate 		 * XXX - assumes stack grows toward low addresses.
3700Sstevel@tonic-gate 		 */
3710Sstevel@tonic-gate 		if (stksize <= sizeof (kthread_t) + PTR24_ALIGN)
3720Sstevel@tonic-gate 			cmn_err(CE_PANIC, "thread_create: proposed stack size"
3730Sstevel@tonic-gate 			    " too small to hold thread.");
3740Sstevel@tonic-gate #ifdef STACK_GROWTH_DOWN
3750Sstevel@tonic-gate 		stksize -= SA(sizeof (kthread_t) + PTR24_ALIGN - 1);
3760Sstevel@tonic-gate 		stksize &= -PTR24_ALIGN;	/* make thread aligned */
3770Sstevel@tonic-gate 		t = (kthread_t *)(stk + stksize);
3780Sstevel@tonic-gate 		bzero(t, sizeof (kthread_t));
3790Sstevel@tonic-gate 		if (audit_active)
3800Sstevel@tonic-gate 			audit_thread_create(t);
3810Sstevel@tonic-gate 		t->t_stk = stk + stksize;
3820Sstevel@tonic-gate 		t->t_stkbase = stk;
3830Sstevel@tonic-gate #else	/* stack grows to larger addresses */
3840Sstevel@tonic-gate 		stksize -= SA(sizeof (kthread_t));
3850Sstevel@tonic-gate 		t = (kthread_t *)(stk);
3860Sstevel@tonic-gate 		bzero(t, sizeof (kthread_t));
3870Sstevel@tonic-gate 		t->t_stk = stk + sizeof (kthread_t);
3880Sstevel@tonic-gate 		t->t_stkbase = stk + stksize + sizeof (kthread_t);
3890Sstevel@tonic-gate #endif	/* STACK_GROWTH_DOWN */
3900Sstevel@tonic-gate 		t->t_flag |= T_TALLOCSTK;
3910Sstevel@tonic-gate 		t->t_swap = stk;
3920Sstevel@tonic-gate 	} else {
3930Sstevel@tonic-gate 		t = kmem_cache_alloc(thread_cache, KM_SLEEP);
3940Sstevel@tonic-gate 		bzero(t, sizeof (kthread_t));
3950Sstevel@tonic-gate 		ASSERT(((uintptr_t)t & (PTR24_ALIGN - 1)) == 0);
3960Sstevel@tonic-gate 		if (audit_active)
3970Sstevel@tonic-gate 			audit_thread_create(t);
3980Sstevel@tonic-gate 		/*
3990Sstevel@tonic-gate 		 * Initialize t_stk to the kernel stack pointer to use
4000Sstevel@tonic-gate 		 * upon entry to the kernel
4010Sstevel@tonic-gate 		 */
4020Sstevel@tonic-gate #ifdef STACK_GROWTH_DOWN
4030Sstevel@tonic-gate 		t->t_stk = stk + stksize;
4040Sstevel@tonic-gate 		t->t_stkbase = stk;
4050Sstevel@tonic-gate #else
4060Sstevel@tonic-gate 		t->t_stk = stk;			/* 3b2-like */
4070Sstevel@tonic-gate 		t->t_stkbase = stk + stksize;
4080Sstevel@tonic-gate #endif /* STACK_GROWTH_DOWN */
4090Sstevel@tonic-gate 	}
4100Sstevel@tonic-gate 
4117854SPhilippe.Jung@Sun.COM 	if (kmem_stackinfo != 0) {
4127854SPhilippe.Jung@Sun.COM 		stkinfo_begin(t);
4137854SPhilippe.Jung@Sun.COM 	}
4147854SPhilippe.Jung@Sun.COM 
4150Sstevel@tonic-gate 	t->t_ts = ts;
4160Sstevel@tonic-gate 
4170Sstevel@tonic-gate 	/*
4180Sstevel@tonic-gate 	 * p_cred could be NULL if it thread_create is called before cred_init
4190Sstevel@tonic-gate 	 * is called in main.
4200Sstevel@tonic-gate 	 */
4210Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
4220Sstevel@tonic-gate 	if (pp->p_cred)
4230Sstevel@tonic-gate 		crhold(t->t_cred = pp->p_cred);
4240Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
4250Sstevel@tonic-gate 	t->t_start = gethrestime_sec();
4260Sstevel@tonic-gate 	t->t_startpc = proc;
4270Sstevel@tonic-gate 	t->t_procp = pp;
4280Sstevel@tonic-gate 	t->t_clfuncs = &sys_classfuncs.thread;
4290Sstevel@tonic-gate 	t->t_cid = syscid;
4300Sstevel@tonic-gate 	t->t_pri = pri;
43111066Srafael.vanoni@sun.com 	t->t_stime = ddi_get_lbolt();
4320Sstevel@tonic-gate 	t->t_schedflag = TS_LOAD | TS_DONT_SWAP;
4330Sstevel@tonic-gate 	t->t_bind_cpu = PBIND_NONE;
4346298Sakolb 	t->t_bindflag = (uchar_t)default_binding_mode;
4350Sstevel@tonic-gate 	t->t_bind_pset = PS_NONE;
4360Sstevel@tonic-gate 	t->t_plockp = &pp->p_lock;
4370Sstevel@tonic-gate 	t->t_copyops = NULL;
4380Sstevel@tonic-gate 	t->t_taskq = NULL;
4390Sstevel@tonic-gate 	t->t_anttime = 0;
4400Sstevel@tonic-gate 	t->t_hatdepth = 0;
4410Sstevel@tonic-gate 
4420Sstevel@tonic-gate 	t->t_dtrace_vtime = 1;	/* assure vtimestamp is always non-zero */
4430Sstevel@tonic-gate 
4440Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, nthreads, 1);
4450Sstevel@tonic-gate #ifndef NPROBE
4460Sstevel@tonic-gate 	/* Kernel probe */
4470Sstevel@tonic-gate 	tnf_thread_create(t);
4480Sstevel@tonic-gate #endif /* NPROBE */
4490Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&t->t_lock);
4500Sstevel@tonic-gate 
4510Sstevel@tonic-gate 	/*
4520Sstevel@tonic-gate 	 * Callers who give us a NULL proc must do their own
4530Sstevel@tonic-gate 	 * stack initialization.  e.g. lwp_create()
4540Sstevel@tonic-gate 	 */
4550Sstevel@tonic-gate 	if (proc != NULL) {
4560Sstevel@tonic-gate 		t->t_stk = thread_stk_init(t->t_stk);
4570Sstevel@tonic-gate 		thread_load(t, proc, arg, len);
4580Sstevel@tonic-gate 	}
4590Sstevel@tonic-gate 
4600Sstevel@tonic-gate 	/*
4610Sstevel@tonic-gate 	 * Put a hold on project0. If this thread is actually in a
4620Sstevel@tonic-gate 	 * different project, then t_proj will be changed later in
4630Sstevel@tonic-gate 	 * lwp_create().  All kernel-only threads must be in project 0.
4640Sstevel@tonic-gate 	 */
4650Sstevel@tonic-gate 	t->t_proj = project_hold(proj0p);
4660Sstevel@tonic-gate 
4670Sstevel@tonic-gate 	lgrp_affinity_init(&t->t_lgrp_affinity);
4680Sstevel@tonic-gate 
4690Sstevel@tonic-gate 	mutex_enter(&pidlock);
4700Sstevel@tonic-gate 	nthread++;
4710Sstevel@tonic-gate 	t->t_did = next_t_id++;
4720Sstevel@tonic-gate 	t->t_prev = curthread->t_prev;
4730Sstevel@tonic-gate 	t->t_next = curthread;
4740Sstevel@tonic-gate 
4750Sstevel@tonic-gate 	/*
4760Sstevel@tonic-gate 	 * Add the thread to the list of all threads, and initialize
4770Sstevel@tonic-gate 	 * its t_cpu pointer.  We need to block preemption since
4780Sstevel@tonic-gate 	 * cpu_offline walks the thread list looking for threads
4790Sstevel@tonic-gate 	 * with t_cpu pointing to the CPU being offlined.  We want
4800Sstevel@tonic-gate 	 * to make sure that the list is consistent and that if t_cpu
4810Sstevel@tonic-gate 	 * is set, the thread is on the list.
4820Sstevel@tonic-gate 	 */
4830Sstevel@tonic-gate 	kpreempt_disable();
4840Sstevel@tonic-gate 	curthread->t_prev->t_next = t;
4850Sstevel@tonic-gate 	curthread->t_prev = t;
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	/*
4880Sstevel@tonic-gate 	 * Threads should never have a NULL t_cpu pointer so assign it
4890Sstevel@tonic-gate 	 * here.  If the thread is being created with state TS_RUN a
4900Sstevel@tonic-gate 	 * better CPU may be chosen when it is placed on the run queue.
4910Sstevel@tonic-gate 	 *
4920Sstevel@tonic-gate 	 * We need to keep kernel preemption disabled when setting all
4930Sstevel@tonic-gate 	 * three fields to keep them in sync.  Also, always create in
4940Sstevel@tonic-gate 	 * the default partition since that's where kernel threads go
4950Sstevel@tonic-gate 	 * (if this isn't a kernel thread, t_cpupart will be changed
4960Sstevel@tonic-gate 	 * in lwp_create before setting the thread runnable).
4970Sstevel@tonic-gate 	 */
4980Sstevel@tonic-gate 	t->t_cpupart = &cp_default;
4990Sstevel@tonic-gate 
5000Sstevel@tonic-gate 	/*
5010Sstevel@tonic-gate 	 * For now, affiliate this thread with the root lgroup.
5020Sstevel@tonic-gate 	 * Since the kernel does not (presently) allocate its memory
5030Sstevel@tonic-gate 	 * in a locality aware fashion, the root is an appropriate home.
5040Sstevel@tonic-gate 	 * If this thread is later associated with an lwp, it will have
5050Sstevel@tonic-gate 	 * it's lgroup re-assigned at that time.
5060Sstevel@tonic-gate 	 */
5070Sstevel@tonic-gate 	lgrp_move_thread(t, &cp_default.cp_lgrploads[LGRP_ROOTID], 1);
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	/*
5100Sstevel@tonic-gate 	 * Inherit the current cpu.  If this cpu isn't part of the chosen
5110Sstevel@tonic-gate 	 * lgroup, a new cpu will be chosen by cpu_choose when the thread
5120Sstevel@tonic-gate 	 * is ready to run.
5130Sstevel@tonic-gate 	 */
5140Sstevel@tonic-gate 	if (CPU->cpu_part == &cp_default)
5150Sstevel@tonic-gate 		t->t_cpu = CPU;
5160Sstevel@tonic-gate 	else
5170Sstevel@tonic-gate 		t->t_cpu = disp_lowpri_cpu(cp_default.cp_cpulist, t->t_lpl,
5180Sstevel@tonic-gate 		    t->t_pri, NULL);
5190Sstevel@tonic-gate 
5200Sstevel@tonic-gate 	t->t_disp_queue = t->t_cpu->cpu_disp;
5210Sstevel@tonic-gate 	kpreempt_enable();
5220Sstevel@tonic-gate 
5230Sstevel@tonic-gate 	/*
5240Sstevel@tonic-gate 	 * Initialize thread state and the dispatcher lock pointer.
5250Sstevel@tonic-gate 	 * Need to hold onto pidlock to block allthreads walkers until
5260Sstevel@tonic-gate 	 * the state is set.
5270Sstevel@tonic-gate 	 */
5280Sstevel@tonic-gate 	switch (state) {
5290Sstevel@tonic-gate 	case TS_RUN:
5300Sstevel@tonic-gate 		curthread->t_oldspl = splhigh();	/* get dispatcher spl */
5310Sstevel@tonic-gate 		THREAD_SET_STATE(t, TS_STOPPED, &transition_lock);
5320Sstevel@tonic-gate 		CL_SETRUN(t);
5330Sstevel@tonic-gate 		thread_unlock(t);
5340Sstevel@tonic-gate 		break;
5350Sstevel@tonic-gate 
5360Sstevel@tonic-gate 	case TS_ONPROC:
5370Sstevel@tonic-gate 		THREAD_ONPROC(t, t->t_cpu);
5380Sstevel@tonic-gate 		break;
5390Sstevel@tonic-gate 
5400Sstevel@tonic-gate 	case TS_FREE:
5410Sstevel@tonic-gate 		/*
5420Sstevel@tonic-gate 		 * Free state will be used for intr threads.
5430Sstevel@tonic-gate 		 * The interrupt routine must set the thread dispatcher
5440Sstevel@tonic-gate 		 * lock pointer (t_lockp) if starting on a CPU
5450Sstevel@tonic-gate 		 * other than the current one.
5460Sstevel@tonic-gate 		 */
5470Sstevel@tonic-gate 		THREAD_FREEINTR(t, CPU);
5480Sstevel@tonic-gate 		break;
5490Sstevel@tonic-gate 
5500Sstevel@tonic-gate 	case TS_STOPPED:
5510Sstevel@tonic-gate 		THREAD_SET_STATE(t, TS_STOPPED, &stop_lock);
5520Sstevel@tonic-gate 		break;
5530Sstevel@tonic-gate 
5540Sstevel@tonic-gate 	default:			/* TS_SLEEP, TS_ZOMB or TS_TRANS */
5550Sstevel@tonic-gate 		cmn_err(CE_PANIC, "thread_create: invalid state %d", state);
5560Sstevel@tonic-gate 	}
5570Sstevel@tonic-gate 	mutex_exit(&pidlock);
5580Sstevel@tonic-gate 	return (t);
5590Sstevel@tonic-gate }
5600Sstevel@tonic-gate 
5610Sstevel@tonic-gate /*
5620Sstevel@tonic-gate  * Move thread to project0 and take care of project reference counters.
5630Sstevel@tonic-gate  */
5640Sstevel@tonic-gate void
thread_rele(kthread_t * t)5650Sstevel@tonic-gate thread_rele(kthread_t *t)
5660Sstevel@tonic-gate {
5670Sstevel@tonic-gate 	kproject_t *kpj;
5680Sstevel@tonic-gate 
5690Sstevel@tonic-gate 	thread_lock(t);
5700Sstevel@tonic-gate 
5710Sstevel@tonic-gate 	ASSERT(t == curthread || t->t_state == TS_FREE || t->t_procp == &p0);
5720Sstevel@tonic-gate 	kpj = ttoproj(t);
5730Sstevel@tonic-gate 	t->t_proj = proj0p;
5740Sstevel@tonic-gate 
5750Sstevel@tonic-gate 	thread_unlock(t);
5760Sstevel@tonic-gate 
5770Sstevel@tonic-gate 	if (kpj != proj0p) {
5780Sstevel@tonic-gate 		project_rele(kpj);
5790Sstevel@tonic-gate 		(void) project_hold(proj0p);
5800Sstevel@tonic-gate 	}
5810Sstevel@tonic-gate }
5820Sstevel@tonic-gate 
5830Sstevel@tonic-gate void
thread_exit(void)5845023Scarlsonj thread_exit(void)
5850Sstevel@tonic-gate {
5860Sstevel@tonic-gate 	kthread_t *t = curthread;
5870Sstevel@tonic-gate 
5880Sstevel@tonic-gate 	if ((t->t_proc_flag & TP_ZTHREAD) != 0)
5890Sstevel@tonic-gate 		cmn_err(CE_PANIC, "thread_exit: zthread_exit() not called");
5900Sstevel@tonic-gate 
5910Sstevel@tonic-gate 	tsd_exit();		/* Clean up this thread's TSD */
5920Sstevel@tonic-gate 
5930Sstevel@tonic-gate 	kcpc_passivate();	/* clean up performance counter state */
5940Sstevel@tonic-gate 
5950Sstevel@tonic-gate 	/*
5960Sstevel@tonic-gate 	 * No kernel thread should have called poll() without arranging
5970Sstevel@tonic-gate 	 * calling pollcleanup() here.
5980Sstevel@tonic-gate 	 */
5990Sstevel@tonic-gate 	ASSERT(t->t_pollstate == NULL);
6000Sstevel@tonic-gate 	ASSERT(t->t_schedctl == NULL);
6010Sstevel@tonic-gate 	if (t->t_door)
6020Sstevel@tonic-gate 		door_slam();	/* in case thread did an upcall */
6030Sstevel@tonic-gate 
6040Sstevel@tonic-gate #ifndef NPROBE
6050Sstevel@tonic-gate 	/* Kernel probe */
6060Sstevel@tonic-gate 	if (t->t_tnf_tpdp)
6070Sstevel@tonic-gate 		tnf_thread_exit();
6080Sstevel@tonic-gate #endif /* NPROBE */
6090Sstevel@tonic-gate 
6100Sstevel@tonic-gate 	thread_rele(t);
6110Sstevel@tonic-gate 	t->t_preempt++;
6120Sstevel@tonic-gate 
6130Sstevel@tonic-gate 	/*
6140Sstevel@tonic-gate 	 * remove thread from the all threads list so that
6150Sstevel@tonic-gate 	 * death-row can use the same pointers.
6160Sstevel@tonic-gate 	 */
6170Sstevel@tonic-gate 	mutex_enter(&pidlock);
6180Sstevel@tonic-gate 	t->t_next->t_prev = t->t_prev;
6190Sstevel@tonic-gate 	t->t_prev->t_next = t->t_next;
6200Sstevel@tonic-gate 	ASSERT(allthreads != t);	/* t0 never exits */
6210Sstevel@tonic-gate 	cv_broadcast(&t->t_joincv);	/* wake up anyone in thread_join */
6220Sstevel@tonic-gate 	mutex_exit(&pidlock);
6230Sstevel@tonic-gate 
6240Sstevel@tonic-gate 	if (t->t_ctx != NULL)
6250Sstevel@tonic-gate 		exitctx(t);
6261217Srab 	if (t->t_procp->p_pctx != NULL)
6271217Srab 		exitpctx(t->t_procp);
6280Sstevel@tonic-gate 
6297854SPhilippe.Jung@Sun.COM 	if (kmem_stackinfo != 0) {
6307854SPhilippe.Jung@Sun.COM 		stkinfo_end(t);
6317854SPhilippe.Jung@Sun.COM 	}
6327854SPhilippe.Jung@Sun.COM 
6330Sstevel@tonic-gate 	t->t_state = TS_ZOMB;	/* set zombie thread */
6340Sstevel@tonic-gate 
6350Sstevel@tonic-gate 	swtch_from_zombie();	/* give up the CPU */
6360Sstevel@tonic-gate 	/* NOTREACHED */
6370Sstevel@tonic-gate }
6380Sstevel@tonic-gate 
6390Sstevel@tonic-gate /*
6400Sstevel@tonic-gate  * Check to see if the specified thread is active (defined as being on
6410Sstevel@tonic-gate  * the thread list).  This is certainly a slow way to do this; if there's
6420Sstevel@tonic-gate  * ever a reason to speed it up, we could maintain a hash table of active
6430Sstevel@tonic-gate  * threads indexed by their t_did.
6440Sstevel@tonic-gate  */
6450Sstevel@tonic-gate static kthread_t *
did_to_thread(kt_did_t tid)6460Sstevel@tonic-gate did_to_thread(kt_did_t tid)
6470Sstevel@tonic-gate {
6480Sstevel@tonic-gate 	kthread_t *t;
6490Sstevel@tonic-gate 
6500Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6510Sstevel@tonic-gate 	for (t = curthread->t_next; t != curthread; t = t->t_next) {
6520Sstevel@tonic-gate 		if (t->t_did == tid)
6530Sstevel@tonic-gate 			break;
6540Sstevel@tonic-gate 	}
6550Sstevel@tonic-gate 	if (t->t_did == tid)
6560Sstevel@tonic-gate 		return (t);
6570Sstevel@tonic-gate 	else
6580Sstevel@tonic-gate 		return (NULL);
6590Sstevel@tonic-gate }
6600Sstevel@tonic-gate 
6610Sstevel@tonic-gate /*
6620Sstevel@tonic-gate  * Wait for specified thread to exit.  Returns immediately if the thread
6630Sstevel@tonic-gate  * could not be found, meaning that it has either already exited or never
6640Sstevel@tonic-gate  * existed.
6650Sstevel@tonic-gate  */
6660Sstevel@tonic-gate void
thread_join(kt_did_t tid)6670Sstevel@tonic-gate thread_join(kt_did_t tid)
6680Sstevel@tonic-gate {
6690Sstevel@tonic-gate 	kthread_t *t;
6700Sstevel@tonic-gate 
6710Sstevel@tonic-gate 	ASSERT(tid != curthread->t_did);
6720Sstevel@tonic-gate 	ASSERT(tid != t0.t_did);
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	mutex_enter(&pidlock);
6750Sstevel@tonic-gate 	/*
6760Sstevel@tonic-gate 	 * Make sure we check that the thread is on the thread list
6770Sstevel@tonic-gate 	 * before blocking on it; otherwise we could end up blocking on
6780Sstevel@tonic-gate 	 * a cv that's already been freed.  In other words, don't cache
6790Sstevel@tonic-gate 	 * the thread pointer across calls to cv_wait.
6800Sstevel@tonic-gate 	 *
6810Sstevel@tonic-gate 	 * The choice of loop invariant means that whenever a thread
6820Sstevel@tonic-gate 	 * is taken off the allthreads list, a cv_broadcast must be
6830Sstevel@tonic-gate 	 * performed on that thread's t_joincv to wake up any waiters.
6840Sstevel@tonic-gate 	 * The broadcast doesn't have to happen right away, but it
6850Sstevel@tonic-gate 	 * shouldn't be postponed indefinitely (e.g., by doing it in
6860Sstevel@tonic-gate 	 * thread_free which may only be executed when the deathrow
6870Sstevel@tonic-gate 	 * queue is processed.
6880Sstevel@tonic-gate 	 */
6890Sstevel@tonic-gate 	while (t = did_to_thread(tid))
6900Sstevel@tonic-gate 		cv_wait(&t->t_joincv, &pidlock);
6910Sstevel@tonic-gate 	mutex_exit(&pidlock);
6920Sstevel@tonic-gate }
6930Sstevel@tonic-gate 
6940Sstevel@tonic-gate void
thread_free_prevent(kthread_t * t)6955788Smv143129 thread_free_prevent(kthread_t *t)
6965788Smv143129 {
6975788Smv143129 	kmutex_t *lp;
6985788Smv143129 
6995788Smv143129 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
7005788Smv143129 	mutex_enter(lp);
7015788Smv143129 }
7025788Smv143129 
7035788Smv143129 void
thread_free_allow(kthread_t * t)7045788Smv143129 thread_free_allow(kthread_t *t)
7055788Smv143129 {
7065788Smv143129 	kmutex_t *lp;
7075788Smv143129 
7085788Smv143129 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
7095788Smv143129 	mutex_exit(lp);
7105788Smv143129 }
7115788Smv143129 
7125788Smv143129 static void
thread_free_barrier(kthread_t * t)7135788Smv143129 thread_free_barrier(kthread_t *t)
7145788Smv143129 {
7155788Smv143129 	kmutex_t *lp;
7165788Smv143129 
7175788Smv143129 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
7185788Smv143129 	mutex_enter(lp);
7195788Smv143129 	mutex_exit(lp);
7205788Smv143129 }
7215788Smv143129 
7225788Smv143129 void
thread_free(kthread_t * t)7230Sstevel@tonic-gate thread_free(kthread_t *t)
7240Sstevel@tonic-gate {
72511292SJonathan.Adams@Sun.COM 	boolean_t allocstk = (t->t_flag & T_TALLOCSTK);
72611292SJonathan.Adams@Sun.COM 	klwp_t *lwp = t->t_lwp;
72711292SJonathan.Adams@Sun.COM 	caddr_t swap = t->t_swap;
72811292SJonathan.Adams@Sun.COM 
7290Sstevel@tonic-gate 	ASSERT(t != &t0 && t->t_state == TS_FREE);
7300Sstevel@tonic-gate 	ASSERT(t->t_door == NULL);
7310Sstevel@tonic-gate 	ASSERT(t->t_schedctl == NULL);
7320Sstevel@tonic-gate 	ASSERT(t->t_pollstate == NULL);
7330Sstevel@tonic-gate 
7340Sstevel@tonic-gate 	t->t_pri = 0;
7350Sstevel@tonic-gate 	t->t_pc = 0;
7360Sstevel@tonic-gate 	t->t_sp = 0;
7370Sstevel@tonic-gate 	t->t_wchan0 = NULL;
7380Sstevel@tonic-gate 	t->t_wchan = NULL;
7390Sstevel@tonic-gate 	if (t->t_cred != NULL) {
7400Sstevel@tonic-gate 		crfree(t->t_cred);
7410Sstevel@tonic-gate 		t->t_cred = 0;
7420Sstevel@tonic-gate 	}
7430Sstevel@tonic-gate 	if (t->t_pdmsg) {
7440Sstevel@tonic-gate 		kmem_free(t->t_pdmsg, strlen(t->t_pdmsg) + 1);
7450Sstevel@tonic-gate 		t->t_pdmsg = NULL;
7460Sstevel@tonic-gate 	}
7470Sstevel@tonic-gate 	if (audit_active)
7480Sstevel@tonic-gate 		audit_thread_free(t);
7490Sstevel@tonic-gate #ifndef NPROBE
7500Sstevel@tonic-gate 	if (t->t_tnf_tpdp)
7510Sstevel@tonic-gate 		tnf_thread_free(t);
7520Sstevel@tonic-gate #endif /* NPROBE */
7530Sstevel@tonic-gate 	if (t->t_cldata) {
7540Sstevel@tonic-gate 		CL_EXITCLASS(t->t_cid, (caddr_t *)t->t_cldata);
7550Sstevel@tonic-gate 	}
7560Sstevel@tonic-gate 	if (t->t_rprof != NULL) {
7570Sstevel@tonic-gate 		kmem_free(t->t_rprof, sizeof (*t->t_rprof));
7580Sstevel@tonic-gate 		t->t_rprof = NULL;
7590Sstevel@tonic-gate 	}
7600Sstevel@tonic-gate 	t->t_lockp = NULL;	/* nothing should try to lock this thread now */
76111292SJonathan.Adams@Sun.COM 	if (lwp)
76211292SJonathan.Adams@Sun.COM 		lwp_freeregs(lwp, 0);
7630Sstevel@tonic-gate 	if (t->t_ctx)
7640Sstevel@tonic-gate 		freectx(t, 0);
7650Sstevel@tonic-gate 	t->t_stk = NULL;
76611292SJonathan.Adams@Sun.COM 	if (lwp)
76711292SJonathan.Adams@Sun.COM 		lwp_stk_fini(lwp);
7680Sstevel@tonic-gate 	lock_clear(&t->t_lock);
7690Sstevel@tonic-gate 
7700Sstevel@tonic-gate 	if (t->t_ts->ts_waiters > 0)
7710Sstevel@tonic-gate 		panic("thread_free: turnstile still active");
7720Sstevel@tonic-gate 
7730Sstevel@tonic-gate 	kmem_cache_free(turnstile_cache, t->t_ts);
7740Sstevel@tonic-gate 
7750Sstevel@tonic-gate 	free_afd(&t->t_activefd);
7760Sstevel@tonic-gate 
7770Sstevel@tonic-gate 	/*
7785788Smv143129 	 * Barrier for the tick accounting code.  The tick accounting code
7795788Smv143129 	 * holds this lock to keep the thread from going away while it's
7805788Smv143129 	 * looking at it.
7810Sstevel@tonic-gate 	 */
7825788Smv143129 	thread_free_barrier(t);
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	ASSERT(ttoproj(t) == proj0p);
7850Sstevel@tonic-gate 	project_rele(ttoproj(t));
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate 	lgrp_affinity_free(&t->t_lgrp_affinity);
7880Sstevel@tonic-gate 
78911292SJonathan.Adams@Sun.COM 	mutex_enter(&pidlock);
79011292SJonathan.Adams@Sun.COM 	nthread--;
79111292SJonathan.Adams@Sun.COM 	mutex_exit(&pidlock);
79211292SJonathan.Adams@Sun.COM 
7930Sstevel@tonic-gate 	/*
79411292SJonathan.Adams@Sun.COM 	 * Free thread, lwp and stack.  This needs to be done carefully, since
79511292SJonathan.Adams@Sun.COM 	 * if T_TALLOCSTK is set, the thread is part of the stack.
7960Sstevel@tonic-gate 	 */
79711292SJonathan.Adams@Sun.COM 	t->t_lwp = NULL;
79811292SJonathan.Adams@Sun.COM 	t->t_swap = NULL;
79911292SJonathan.Adams@Sun.COM 
80011292SJonathan.Adams@Sun.COM 	if (swap) {
80111292SJonathan.Adams@Sun.COM 		segkp_release(segkp, swap);
80211292SJonathan.Adams@Sun.COM 	}
80311292SJonathan.Adams@Sun.COM 	if (lwp) {
80411292SJonathan.Adams@Sun.COM 		kmem_cache_free(lwp_cache, lwp);
80511292SJonathan.Adams@Sun.COM 	}
80611292SJonathan.Adams@Sun.COM 	if (!allocstk) {
8070Sstevel@tonic-gate 		kmem_cache_free(thread_cache, t);
8080Sstevel@tonic-gate 	}
8090Sstevel@tonic-gate }
8100Sstevel@tonic-gate 
8110Sstevel@tonic-gate /*
8120Sstevel@tonic-gate  * Removes threads associated with the given zone from a deathrow queue.
8130Sstevel@tonic-gate  * tp is a pointer to the head of the deathrow queue, and countp is a
8140Sstevel@tonic-gate  * pointer to the current deathrow count.  Returns a linked list of
8150Sstevel@tonic-gate  * threads removed from the list.
8160Sstevel@tonic-gate  */
8170Sstevel@tonic-gate static kthread_t *
thread_zone_cleanup(kthread_t ** tp,int * countp,zoneid_t zoneid)8180Sstevel@tonic-gate thread_zone_cleanup(kthread_t **tp, int *countp, zoneid_t zoneid)
8190Sstevel@tonic-gate {
8200Sstevel@tonic-gate 	kthread_t *tmp, *list = NULL;
8210Sstevel@tonic-gate 	cred_t *cr;
8220Sstevel@tonic-gate 
8230Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&reaplock));
8240Sstevel@tonic-gate 	while (*tp != NULL) {
8250Sstevel@tonic-gate 		if ((cr = (*tp)->t_cred) != NULL && crgetzoneid(cr) == zoneid) {
8260Sstevel@tonic-gate 			tmp = *tp;
8270Sstevel@tonic-gate 			*tp = tmp->t_forw;
8280Sstevel@tonic-gate 			tmp->t_forw = list;
8290Sstevel@tonic-gate 			list = tmp;
8300Sstevel@tonic-gate 			(*countp)--;
8310Sstevel@tonic-gate 		} else {
8320Sstevel@tonic-gate 			tp = &(*tp)->t_forw;
8330Sstevel@tonic-gate 		}
8340Sstevel@tonic-gate 	}
8350Sstevel@tonic-gate 	return (list);
8360Sstevel@tonic-gate }
8370Sstevel@tonic-gate 
8380Sstevel@tonic-gate static void
thread_reap_list(kthread_t * t)8390Sstevel@tonic-gate thread_reap_list(kthread_t *t)
8400Sstevel@tonic-gate {
8410Sstevel@tonic-gate 	kthread_t *next;
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	while (t != NULL) {
8440Sstevel@tonic-gate 		next = t->t_forw;
8450Sstevel@tonic-gate 		thread_free(t);
8460Sstevel@tonic-gate 		t = next;
8470Sstevel@tonic-gate 	}
8480Sstevel@tonic-gate }
8490Sstevel@tonic-gate 
8500Sstevel@tonic-gate /* ARGSUSED */
8510Sstevel@tonic-gate static void
thread_zone_destroy(zoneid_t zoneid,void * unused)8520Sstevel@tonic-gate thread_zone_destroy(zoneid_t zoneid, void *unused)
8530Sstevel@tonic-gate {
8540Sstevel@tonic-gate 	kthread_t *t, *l;
8550Sstevel@tonic-gate 
8560Sstevel@tonic-gate 	mutex_enter(&reaplock);
8570Sstevel@tonic-gate 	/*
8580Sstevel@tonic-gate 	 * Pull threads and lwps associated with zone off deathrow lists.
8590Sstevel@tonic-gate 	 */
8600Sstevel@tonic-gate 	t = thread_zone_cleanup(&thread_deathrow, &thread_reapcnt, zoneid);
8610Sstevel@tonic-gate 	l = thread_zone_cleanup(&lwp_deathrow, &lwp_reapcnt, zoneid);
8620Sstevel@tonic-gate 	mutex_exit(&reaplock);
8630Sstevel@tonic-gate 
8640Sstevel@tonic-gate 	/*
8655834Spt157919 	 * Guard against race condition in mutex_owner_running:
8665834Spt157919 	 * 	thread=owner(mutex)
8675834Spt157919 	 * 	<interrupt>
8685834Spt157919 	 * 				thread exits mutex
8695834Spt157919 	 * 				thread exits
8705834Spt157919 	 * 				thread reaped
8715834Spt157919 	 * 				thread struct freed
8725834Spt157919 	 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
8735834Spt157919 	 * A cross call to all cpus will cause the interrupt handler
8745834Spt157919 	 * to reset the PC if it is in mutex_owner_running, refreshing
8755834Spt157919 	 * stale thread pointers.
8765834Spt157919 	 */
8775834Spt157919 	mutex_sync();   /* sync with mutex code */
8785834Spt157919 
8795834Spt157919 	/*
8800Sstevel@tonic-gate 	 * Reap threads
8810Sstevel@tonic-gate 	 */
8820Sstevel@tonic-gate 	thread_reap_list(t);
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	/*
8850Sstevel@tonic-gate 	 * Reap lwps
8860Sstevel@tonic-gate 	 */
8870Sstevel@tonic-gate 	thread_reap_list(l);
8880Sstevel@tonic-gate }
8890Sstevel@tonic-gate 
8900Sstevel@tonic-gate /*
8910Sstevel@tonic-gate  * cleanup zombie threads that are on deathrow.
8920Sstevel@tonic-gate  */
8930Sstevel@tonic-gate void
thread_reaper()8940Sstevel@tonic-gate thread_reaper()
8950Sstevel@tonic-gate {
8960Sstevel@tonic-gate 	kthread_t *t, *l;
8970Sstevel@tonic-gate 	callb_cpr_t cprinfo;
8980Sstevel@tonic-gate 
8990Sstevel@tonic-gate 	/*
9000Sstevel@tonic-gate 	 * Register callback to clean up threads when zone is destroyed.
9010Sstevel@tonic-gate 	 */
9020Sstevel@tonic-gate 	zone_key_create(&zone_thread_key, NULL, NULL, thread_zone_destroy);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &reaplock, callb_generic_cpr, "t_reaper");
9050Sstevel@tonic-gate 	for (;;) {
9060Sstevel@tonic-gate 		mutex_enter(&reaplock);
9070Sstevel@tonic-gate 		while (thread_deathrow == NULL && lwp_deathrow == NULL) {
9080Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
9090Sstevel@tonic-gate 			cv_wait(&reaper_cv, &reaplock);
9100Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &reaplock);
9110Sstevel@tonic-gate 		}
9125834Spt157919 		/*
9135834Spt157919 		 * mutex_sync() needs to be called when reaping, but
9145834Spt157919 		 * not too often.  We limit reaping rate to once
9155834Spt157919 		 * per second.  Reaplimit is max rate at which threads can
9165834Spt157919 		 * be freed. Does not impact thread destruction/creation.
9175834Spt157919 		 */
9180Sstevel@tonic-gate 		t = thread_deathrow;
9190Sstevel@tonic-gate 		l = lwp_deathrow;
9200Sstevel@tonic-gate 		thread_deathrow = NULL;
9210Sstevel@tonic-gate 		lwp_deathrow = NULL;
9220Sstevel@tonic-gate 		thread_reapcnt = 0;
9230Sstevel@tonic-gate 		lwp_reapcnt = 0;
9240Sstevel@tonic-gate 		mutex_exit(&reaplock);
9250Sstevel@tonic-gate 
9260Sstevel@tonic-gate 		/*
9275834Spt157919 		 * Guard against race condition in mutex_owner_running:
9285834Spt157919 		 * 	thread=owner(mutex)
9295834Spt157919 		 * 	<interrupt>
9305834Spt157919 		 * 				thread exits mutex
9315834Spt157919 		 * 				thread exits
9325834Spt157919 		 * 				thread reaped
9335834Spt157919 		 * 				thread struct freed
9345834Spt157919 		 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
9355834Spt157919 		 * A cross call to all cpus will cause the interrupt handler
9365834Spt157919 		 * to reset the PC if it is in mutex_owner_running, refreshing
9375834Spt157919 		 * stale thread pointers.
9385834Spt157919 		 */
9395834Spt157919 		mutex_sync();   /* sync with mutex code */
9405834Spt157919 		/*
9410Sstevel@tonic-gate 		 * Reap threads
9420Sstevel@tonic-gate 		 */
9430Sstevel@tonic-gate 		thread_reap_list(t);
9440Sstevel@tonic-gate 
9450Sstevel@tonic-gate 		/*
9460Sstevel@tonic-gate 		 * Reap lwps
9470Sstevel@tonic-gate 		 */
9480Sstevel@tonic-gate 		thread_reap_list(l);
9495834Spt157919 		delay(hz);
9500Sstevel@tonic-gate 	}
9510Sstevel@tonic-gate }
9520Sstevel@tonic-gate 
9530Sstevel@tonic-gate /*
9545834Spt157919  * This is called by lwpcreate, etc.() to put a lwp_deathrow thread onto
9555834Spt157919  * thread_deathrow. The thread's state is changed already TS_FREE to indicate
9565834Spt157919  * that is reapable. The thread already holds the reaplock, and was already
9575834Spt157919  * freed.
9585834Spt157919  */
9595834Spt157919 void
reapq_move_lq_to_tq(kthread_t * t)9605834Spt157919 reapq_move_lq_to_tq(kthread_t *t)
9615834Spt157919 {
9625834Spt157919 	ASSERT(t->t_state == TS_FREE);
9635834Spt157919 	ASSERT(MUTEX_HELD(&reaplock));
9645834Spt157919 	t->t_forw = thread_deathrow;
9655834Spt157919 	thread_deathrow = t;
9665834Spt157919 	thread_reapcnt++;
9675834Spt157919 	if (lwp_reapcnt + thread_reapcnt > reaplimit)
9685834Spt157919 		cv_signal(&reaper_cv);  /* wake the reaper */
9695834Spt157919 }
9705834Spt157919 
9715834Spt157919 /*
9720Sstevel@tonic-gate  * This is called by resume() to put a zombie thread onto deathrow.
9730Sstevel@tonic-gate  * The thread's state is changed to TS_FREE to indicate that is reapable.
9745834Spt157919  * This is called from the idle thread so it must not block - just spin.
9750Sstevel@tonic-gate  */
9760Sstevel@tonic-gate void
reapq_add(kthread_t * t)9770Sstevel@tonic-gate reapq_add(kthread_t *t)
9780Sstevel@tonic-gate {
9790Sstevel@tonic-gate 	mutex_enter(&reaplock);
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate 	/*
98211292SJonathan.Adams@Sun.COM 	 * lwp_deathrow contains threads with lwp linkage and
98311292SJonathan.Adams@Sun.COM 	 * swappable thread stacks which have the default stacksize.
98411292SJonathan.Adams@Sun.COM 	 * These threads' lwps and stacks may be reused by lwp_create().
98511292SJonathan.Adams@Sun.COM 	 *
98611292SJonathan.Adams@Sun.COM 	 * Anything else goes on thread_deathrow(), where it will eventually
98711292SJonathan.Adams@Sun.COM 	 * be thread_free()d.
9880Sstevel@tonic-gate 	 */
98911292SJonathan.Adams@Sun.COM 	if (t->t_flag & T_LWPREUSE) {
99011292SJonathan.Adams@Sun.COM 		ASSERT(ttolwp(t) != NULL);
9910Sstevel@tonic-gate 		t->t_forw = lwp_deathrow;
9920Sstevel@tonic-gate 		lwp_deathrow = t;
9930Sstevel@tonic-gate 		lwp_reapcnt++;
9940Sstevel@tonic-gate 	} else {
9950Sstevel@tonic-gate 		t->t_forw = thread_deathrow;
9960Sstevel@tonic-gate 		thread_deathrow = t;
9970Sstevel@tonic-gate 		thread_reapcnt++;
9980Sstevel@tonic-gate 	}
9990Sstevel@tonic-gate 	if (lwp_reapcnt + thread_reapcnt > reaplimit)
10000Sstevel@tonic-gate 		cv_signal(&reaper_cv);	/* wake the reaper */
10010Sstevel@tonic-gate 	t->t_state = TS_FREE;
10020Sstevel@tonic-gate 	lock_clear(&t->t_lock);
10034686Sqiao 
10044686Sqiao 	/*
10054686Sqiao 	 * Before we return, we need to grab and drop the thread lock for
10064686Sqiao 	 * the dead thread.  At this point, the current thread is the idle
10074686Sqiao 	 * thread, and the dead thread's CPU lock points to the current
10084686Sqiao 	 * CPU -- and we must grab and drop the lock to synchronize with
10094686Sqiao 	 * a racing thread walking a blocking chain that the zombie thread
10104686Sqiao 	 * was recently in.  By this point, that blocking chain is (by
10114686Sqiao 	 * definition) stale:  the dead thread is not holding any locks, and
10124686Sqiao 	 * is therefore not in any blocking chains -- but if we do not regrab
10134686Sqiao 	 * our lock before freeing the dead thread's data structures, the
10144686Sqiao 	 * thread walking the (stale) blocking chain will die on memory
10154686Sqiao 	 * corruption when it attempts to drop the dead thread's lock.  We
10164686Sqiao 	 * only need do this once because there is no way for the dead thread
10174686Sqiao 	 * to ever again be on a blocking chain:  once we have grabbed and
10184686Sqiao 	 * dropped the thread lock, we are guaranteed that anyone that could
10194686Sqiao 	 * have seen this thread in a blocking chain can no longer see it.
10204686Sqiao 	 */
10214686Sqiao 	thread_lock(t);
10224686Sqiao 	thread_unlock(t);
10234686Sqiao 
10240Sstevel@tonic-gate 	mutex_exit(&reaplock);
10250Sstevel@tonic-gate }
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate /*
10281217Srab  * Install thread context ops for the current thread.
10290Sstevel@tonic-gate  */
10300Sstevel@tonic-gate void
installctx(kthread_t * t,void * arg,void (* save)(void *),void (* restore)(void *),void (* fork)(void *,void *),void (* lwp_create)(void *,void *),void (* exit)(void *),void (* free)(void *,int))10310Sstevel@tonic-gate installctx(
10320Sstevel@tonic-gate 	kthread_t *t,
10330Sstevel@tonic-gate 	void	*arg,
10340Sstevel@tonic-gate 	void	(*save)(void *),
10350Sstevel@tonic-gate 	void	(*restore)(void *),
10360Sstevel@tonic-gate 	void	(*fork)(void *, void *),
10370Sstevel@tonic-gate 	void	(*lwp_create)(void *, void *),
10380Sstevel@tonic-gate 	void	(*exit)(void *),
10390Sstevel@tonic-gate 	void	(*free)(void *, int))
10400Sstevel@tonic-gate {
10410Sstevel@tonic-gate 	struct ctxop *ctx;
10420Sstevel@tonic-gate 
10430Sstevel@tonic-gate 	ctx = kmem_alloc(sizeof (struct ctxop), KM_SLEEP);
10440Sstevel@tonic-gate 	ctx->save_op = save;
10450Sstevel@tonic-gate 	ctx->restore_op = restore;
10460Sstevel@tonic-gate 	ctx->fork_op = fork;
10470Sstevel@tonic-gate 	ctx->lwp_create_op = lwp_create;
10480Sstevel@tonic-gate 	ctx->exit_op = exit;
10490Sstevel@tonic-gate 	ctx->free_op = free;
10500Sstevel@tonic-gate 	ctx->arg = arg;
10510Sstevel@tonic-gate 	ctx->next = t->t_ctx;
10520Sstevel@tonic-gate 	t->t_ctx = ctx;
10530Sstevel@tonic-gate }
10540Sstevel@tonic-gate 
10550Sstevel@tonic-gate /*
10562646Strevtom  * Remove the thread context ops from a thread.
10570Sstevel@tonic-gate  */
10580Sstevel@tonic-gate int
removectx(kthread_t * t,void * arg,void (* save)(void *),void (* restore)(void *),void (* fork)(void *,void *),void (* lwp_create)(void *,void *),void (* exit)(void *),void (* free)(void *,int))10590Sstevel@tonic-gate removectx(
10600Sstevel@tonic-gate 	kthread_t *t,
10610Sstevel@tonic-gate 	void	*arg,
10620Sstevel@tonic-gate 	void	(*save)(void *),
10630Sstevel@tonic-gate 	void	(*restore)(void *),
10640Sstevel@tonic-gate 	void	(*fork)(void *, void *),
10650Sstevel@tonic-gate 	void	(*lwp_create)(void *, void *),
10660Sstevel@tonic-gate 	void	(*exit)(void *),
10670Sstevel@tonic-gate 	void	(*free)(void *, int))
10680Sstevel@tonic-gate {
10690Sstevel@tonic-gate 	struct ctxop *ctx, *prev_ctx;
10700Sstevel@tonic-gate 
10712646Strevtom 	/*
10722646Strevtom 	 * The incoming kthread_t (which is the thread for which the
10732646Strevtom 	 * context ops will be removed) should be one of the following:
10742646Strevtom 	 *
10752646Strevtom 	 * a) the current thread,
10762646Strevtom 	 *
10772646Strevtom 	 * b) a thread of a process that's being forked (SIDL),
10782646Strevtom 	 *
10792646Strevtom 	 * c) a thread that belongs to the same process as the current
10802646Strevtom 	 *    thread and for which the current thread is the agent thread,
10812646Strevtom 	 *
10822646Strevtom 	 * d) a thread that is TS_STOPPED which is indicative of it
10832646Strevtom 	 *    being (if curthread is not an agent) a thread being created
10842646Strevtom 	 *    as part of an lwp creation.
10852646Strevtom 	 */
10860Sstevel@tonic-gate 	ASSERT(t == curthread || ttoproc(t)->p_stat == SIDL ||
10872337Sdm120769 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
10880Sstevel@tonic-gate 
10892646Strevtom 	/*
10902646Strevtom 	 * Serialize modifications to t->t_ctx to prevent the agent thread
10912646Strevtom 	 * and the target thread from racing with each other during lwp exit.
10922646Strevtom 	 */
10932646Strevtom 	mutex_enter(&t->t_ctx_lock);
10940Sstevel@tonic-gate 	prev_ctx = NULL;
10950Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next) {
10960Sstevel@tonic-gate 		if (ctx->save_op == save && ctx->restore_op == restore &&
10970Sstevel@tonic-gate 		    ctx->fork_op == fork && ctx->lwp_create_op == lwp_create &&
10980Sstevel@tonic-gate 		    ctx->exit_op == exit && ctx->free_op == free &&
10990Sstevel@tonic-gate 		    ctx->arg == arg) {
11000Sstevel@tonic-gate 			if (prev_ctx)
11010Sstevel@tonic-gate 				prev_ctx->next = ctx->next;
11020Sstevel@tonic-gate 			else
11030Sstevel@tonic-gate 				t->t_ctx = ctx->next;
11042646Strevtom 			mutex_exit(&t->t_ctx_lock);
11050Sstevel@tonic-gate 			if (ctx->free_op != NULL)
11060Sstevel@tonic-gate 				(ctx->free_op)(ctx->arg, 0);
11070Sstevel@tonic-gate 			kmem_free(ctx, sizeof (struct ctxop));
11080Sstevel@tonic-gate 			return (1);
11090Sstevel@tonic-gate 		}
11100Sstevel@tonic-gate 		prev_ctx = ctx;
11110Sstevel@tonic-gate 	}
11122646Strevtom 	mutex_exit(&t->t_ctx_lock);
11132646Strevtom 
11140Sstevel@tonic-gate 	return (0);
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate void
savectx(kthread_t * t)11180Sstevel@tonic-gate savectx(kthread_t *t)
11190Sstevel@tonic-gate {
11200Sstevel@tonic-gate 	struct ctxop *ctx;
11210Sstevel@tonic-gate 
11220Sstevel@tonic-gate 	ASSERT(t == curthread);
11230Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
11240Sstevel@tonic-gate 		if (ctx->save_op != NULL)
11250Sstevel@tonic-gate 			(ctx->save_op)(ctx->arg);
11260Sstevel@tonic-gate }
11270Sstevel@tonic-gate 
11280Sstevel@tonic-gate void
restorectx(kthread_t * t)11290Sstevel@tonic-gate restorectx(kthread_t *t)
11300Sstevel@tonic-gate {
11310Sstevel@tonic-gate 	struct ctxop *ctx;
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate 	ASSERT(t == curthread);
11340Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
11350Sstevel@tonic-gate 		if (ctx->restore_op != NULL)
11360Sstevel@tonic-gate 			(ctx->restore_op)(ctx->arg);
11370Sstevel@tonic-gate }
11380Sstevel@tonic-gate 
11390Sstevel@tonic-gate void
forkctx(kthread_t * t,kthread_t * ct)11400Sstevel@tonic-gate forkctx(kthread_t *t, kthread_t *ct)
11410Sstevel@tonic-gate {
11420Sstevel@tonic-gate 	struct ctxop *ctx;
11430Sstevel@tonic-gate 
11440Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
11450Sstevel@tonic-gate 		if (ctx->fork_op != NULL)
11460Sstevel@tonic-gate 			(ctx->fork_op)(t, ct);
11470Sstevel@tonic-gate }
11480Sstevel@tonic-gate 
11490Sstevel@tonic-gate /*
11500Sstevel@tonic-gate  * Note that this operator is only invoked via the _lwp_create
11510Sstevel@tonic-gate  * system call.  The system may have other reasons to create lwps
11520Sstevel@tonic-gate  * e.g. the agent lwp or the doors unreferenced lwp.
11530Sstevel@tonic-gate  */
11540Sstevel@tonic-gate void
lwp_createctx(kthread_t * t,kthread_t * ct)11550Sstevel@tonic-gate lwp_createctx(kthread_t *t, kthread_t *ct)
11560Sstevel@tonic-gate {
11570Sstevel@tonic-gate 	struct ctxop *ctx;
11580Sstevel@tonic-gate 
11590Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
11600Sstevel@tonic-gate 		if (ctx->lwp_create_op != NULL)
11610Sstevel@tonic-gate 			(ctx->lwp_create_op)(t, ct);
11620Sstevel@tonic-gate }
11630Sstevel@tonic-gate 
11640Sstevel@tonic-gate /*
11650Sstevel@tonic-gate  * exitctx is called from thread_exit() and lwp_exit() to perform any actions
11660Sstevel@tonic-gate  * needed when the thread/LWP leaves the processor for the last time. This
11670Sstevel@tonic-gate  * routine is not intended to deal with freeing memory; freectx() is used for
11680Sstevel@tonic-gate  * that purpose during thread_free(). This routine is provided to allow for
11690Sstevel@tonic-gate  * clean-up that can't wait until thread_free().
11700Sstevel@tonic-gate  */
11710Sstevel@tonic-gate void
exitctx(kthread_t * t)11720Sstevel@tonic-gate exitctx(kthread_t *t)
11730Sstevel@tonic-gate {
11740Sstevel@tonic-gate 	struct ctxop *ctx;
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
11770Sstevel@tonic-gate 		if (ctx->exit_op != NULL)
11780Sstevel@tonic-gate 			(ctx->exit_op)(t);
11790Sstevel@tonic-gate }
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate /*
11820Sstevel@tonic-gate  * freectx is called from thread_free() and exec() to get
11831217Srab  * rid of old thread context ops.
11840Sstevel@tonic-gate  */
11850Sstevel@tonic-gate void
freectx(kthread_t * t,int isexec)11860Sstevel@tonic-gate freectx(kthread_t *t, int isexec)
11870Sstevel@tonic-gate {
11880Sstevel@tonic-gate 	struct ctxop *ctx;
11890Sstevel@tonic-gate 
11900Sstevel@tonic-gate 	while ((ctx = t->t_ctx) != NULL) {
11910Sstevel@tonic-gate 		t->t_ctx = ctx->next;
11920Sstevel@tonic-gate 		if (ctx->free_op != NULL)
11930Sstevel@tonic-gate 			(ctx->free_op)(ctx->arg, isexec);
11940Sstevel@tonic-gate 		kmem_free(ctx, sizeof (struct ctxop));
11950Sstevel@tonic-gate 	}
11960Sstevel@tonic-gate }
11970Sstevel@tonic-gate 
11980Sstevel@tonic-gate /*
11995834Spt157919  * freectx_ctx is called from lwp_create() when lwp is reused from
12005834Spt157919  * lwp_deathrow and its thread structure is added to thread_deathrow.
12015834Spt157919  * The thread structure to which this ctx was attached may be already
12025834Spt157919  * freed by the thread reaper so free_op implementations shouldn't rely
12035834Spt157919  * on thread structure to which this ctx was attached still being around.
12045834Spt157919  */
12055834Spt157919 void
freectx_ctx(struct ctxop * ctx)12065834Spt157919 freectx_ctx(struct ctxop *ctx)
12075834Spt157919 {
12085834Spt157919 	struct ctxop *nctx;
12095834Spt157919 
12105834Spt157919 	ASSERT(ctx != NULL);
12115834Spt157919 
12125834Spt157919 	do {
12135834Spt157919 		nctx = ctx->next;
12145834Spt157919 		if (ctx->free_op != NULL)
12155834Spt157919 			(ctx->free_op)(ctx->arg, 0);
12165834Spt157919 		kmem_free(ctx, sizeof (struct ctxop));
12175834Spt157919 	} while ((ctx = nctx) != NULL);
12185834Spt157919 }
12195834Spt157919 
12205834Spt157919 /*
12210Sstevel@tonic-gate  * Set the thread running; arrange for it to be swapped in if necessary.
12220Sstevel@tonic-gate  */
12230Sstevel@tonic-gate void
setrun_locked(kthread_t * t)12240Sstevel@tonic-gate setrun_locked(kthread_t *t)
12250Sstevel@tonic-gate {
12260Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
12270Sstevel@tonic-gate 	if (t->t_state == TS_SLEEP) {
12280Sstevel@tonic-gate 		/*
12290Sstevel@tonic-gate 		 * Take off sleep queue.
12300Sstevel@tonic-gate 		 */
12310Sstevel@tonic-gate 		SOBJ_UNSLEEP(t->t_sobj_ops, t);
12320Sstevel@tonic-gate 	} else if (t->t_state & (TS_RUN | TS_ONPROC)) {
12330Sstevel@tonic-gate 		/*
12340Sstevel@tonic-gate 		 * Already on dispatcher queue.
12350Sstevel@tonic-gate 		 */
12360Sstevel@tonic-gate 		return;
12373792Sakolb 	} else if (t->t_state == TS_WAIT) {
12383792Sakolb 		waitq_setrun(t);
12390Sstevel@tonic-gate 	} else if (t->t_state == TS_STOPPED) {
12400Sstevel@tonic-gate 		/*
12410Sstevel@tonic-gate 		 * All of the sending of SIGCONT (TC_XSTART) and /proc
12420Sstevel@tonic-gate 		 * (TC_PSTART) and lwp_continue() (TC_CSTART) must have
12430Sstevel@tonic-gate 		 * requested that the thread be run.
12440Sstevel@tonic-gate 		 * Just calling setrun() is not sufficient to set a stopped
12450Sstevel@tonic-gate 		 * thread running.  TP_TXSTART is always set if the thread
12460Sstevel@tonic-gate 		 * is not stopped by a jobcontrol stop signal.
12470Sstevel@tonic-gate 		 * TP_TPSTART is always set if /proc is not controlling it.
12480Sstevel@tonic-gate 		 * TP_TCSTART is always set if lwp_suspend() didn't stop it.
12490Sstevel@tonic-gate 		 * The thread won't be stopped unless one of these
12500Sstevel@tonic-gate 		 * three mechanisms did it.
12510Sstevel@tonic-gate 		 *
12520Sstevel@tonic-gate 		 * These flags must be set before calling setrun_locked(t).
12530Sstevel@tonic-gate 		 * They can't be passed as arguments because the streams
12540Sstevel@tonic-gate 		 * code calls setrun() indirectly and the mechanism for
12550Sstevel@tonic-gate 		 * doing so admits only one argument.  Note that the
12560Sstevel@tonic-gate 		 * thread must be locked in order to change t_schedflags.
12570Sstevel@tonic-gate 		 */
12580Sstevel@tonic-gate 		if ((t->t_schedflag & TS_ALLSTART) != TS_ALLSTART)
12590Sstevel@tonic-gate 			return;
12600Sstevel@tonic-gate 		/*
12610Sstevel@tonic-gate 		 * Process is no longer stopped (a thread is running).
12620Sstevel@tonic-gate 		 */
12630Sstevel@tonic-gate 		t->t_whystop = 0;
12640Sstevel@tonic-gate 		t->t_whatstop = 0;
12650Sstevel@tonic-gate 		/*
12660Sstevel@tonic-gate 		 * Strictly speaking, we do not have to clear these
12670Sstevel@tonic-gate 		 * flags here; they are cleared on entry to stop().
12680Sstevel@tonic-gate 		 * However, they are confusing when doing kernel
12690Sstevel@tonic-gate 		 * debugging or when they are revealed by ps(1).
12700Sstevel@tonic-gate 		 */
12710Sstevel@tonic-gate 		t->t_schedflag &= ~TS_ALLSTART;
12720Sstevel@tonic-gate 		THREAD_TRANSITION(t);	/* drop stopped-thread lock */
12730Sstevel@tonic-gate 		ASSERT(t->t_lockp == &transition_lock);
12740Sstevel@tonic-gate 		ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
12750Sstevel@tonic-gate 		/*
12760Sstevel@tonic-gate 		 * Let the class put the process on the dispatcher queue.
12770Sstevel@tonic-gate 		 */
12780Sstevel@tonic-gate 		CL_SETRUN(t);
12790Sstevel@tonic-gate 	}
12800Sstevel@tonic-gate }
12810Sstevel@tonic-gate 
12820Sstevel@tonic-gate void
setrun(kthread_t * t)12830Sstevel@tonic-gate setrun(kthread_t *t)
12840Sstevel@tonic-gate {
12850Sstevel@tonic-gate 	thread_lock(t);
12860Sstevel@tonic-gate 	setrun_locked(t);
12870Sstevel@tonic-gate 	thread_unlock(t);
12880Sstevel@tonic-gate }
12890Sstevel@tonic-gate 
12900Sstevel@tonic-gate /*
12910Sstevel@tonic-gate  * Unpin an interrupted thread.
12920Sstevel@tonic-gate  *	When an interrupt occurs, the interrupt is handled on the stack
12930Sstevel@tonic-gate  *	of an interrupt thread, taken from a pool linked to the CPU structure.
12940Sstevel@tonic-gate  *
12950Sstevel@tonic-gate  *	When swtch() is switching away from an interrupt thread because it
12960Sstevel@tonic-gate  *	blocked or was preempted, this routine is called to complete the
12970Sstevel@tonic-gate  *	saving of the interrupted thread state, and returns the interrupted
12980Sstevel@tonic-gate  *	thread pointer so it may be resumed.
12990Sstevel@tonic-gate  *
13000Sstevel@tonic-gate  *	Called by swtch() only at high spl.
13010Sstevel@tonic-gate  */
13020Sstevel@tonic-gate kthread_t *
thread_unpin()13030Sstevel@tonic-gate thread_unpin()
13040Sstevel@tonic-gate {
13050Sstevel@tonic-gate 	kthread_t	*t = curthread;	/* current thread */
13060Sstevel@tonic-gate 	kthread_t	*itp;		/* interrupted thread */
13070Sstevel@tonic-gate 	int		i;		/* interrupt level */
13080Sstevel@tonic-gate 	extern int	intr_passivate();
13090Sstevel@tonic-gate 
13100Sstevel@tonic-gate 	ASSERT(t->t_intr != NULL);
13110Sstevel@tonic-gate 
13120Sstevel@tonic-gate 	itp = t->t_intr;		/* interrupted thread */
13130Sstevel@tonic-gate 	t->t_intr = NULL;		/* clear interrupt ptr */
13140Sstevel@tonic-gate 
13150Sstevel@tonic-gate 	/*
13160Sstevel@tonic-gate 	 * Get state from interrupt thread for the one
13170Sstevel@tonic-gate 	 * it interrupted.
13180Sstevel@tonic-gate 	 */
13190Sstevel@tonic-gate 
13200Sstevel@tonic-gate 	i = intr_passivate(t, itp);
13210Sstevel@tonic-gate 
13220Sstevel@tonic-gate 	TRACE_5(TR_FAC_INTR, TR_INTR_PASSIVATE,
13234686Sqiao 	    "intr_passivate:level %d curthread %p (%T) ithread %p (%T)",
13244686Sqiao 	    i, t, t, itp, itp);
13250Sstevel@tonic-gate 
13260Sstevel@tonic-gate 	/*
13270Sstevel@tonic-gate 	 * Dissociate the current thread from the interrupted thread's LWP.
13280Sstevel@tonic-gate 	 */
13290Sstevel@tonic-gate 	t->t_lwp = NULL;
13300Sstevel@tonic-gate 
13310Sstevel@tonic-gate 	/*
13320Sstevel@tonic-gate 	 * Interrupt handlers above the level that spinlocks block must
13330Sstevel@tonic-gate 	 * not block.
13340Sstevel@tonic-gate 	 */
13350Sstevel@tonic-gate #if DEBUG
13360Sstevel@tonic-gate 	if (i < 0 || i > LOCK_LEVEL)
13370Sstevel@tonic-gate 		cmn_err(CE_PANIC, "thread_unpin: ipl out of range %x", i);
13380Sstevel@tonic-gate #endif
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	/*
13410Sstevel@tonic-gate 	 * Compute the CPU's base interrupt level based on the active
13420Sstevel@tonic-gate 	 * interrupts.
13430Sstevel@tonic-gate 	 */
13440Sstevel@tonic-gate 	ASSERT(CPU->cpu_intr_actv & (1 << i));
13450Sstevel@tonic-gate 	set_base_spl();
13460Sstevel@tonic-gate 
13470Sstevel@tonic-gate 	return (itp);
13480Sstevel@tonic-gate }
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate /*
13510Sstevel@tonic-gate  * Create and initialize an interrupt thread.
13520Sstevel@tonic-gate  *	Returns non-zero on error.
13530Sstevel@tonic-gate  *	Called at spl7() or better.
13540Sstevel@tonic-gate  */
13550Sstevel@tonic-gate void
thread_create_intr(struct cpu * cp)13560Sstevel@tonic-gate thread_create_intr(struct cpu *cp)
13570Sstevel@tonic-gate {
13580Sstevel@tonic-gate 	kthread_t *tp;
13590Sstevel@tonic-gate 
13600Sstevel@tonic-gate 	tp = thread_create(NULL, 0,
13610Sstevel@tonic-gate 	    (void (*)())thread_create_intr, NULL, 0, &p0, TS_ONPROC, 0);
13620Sstevel@tonic-gate 
13630Sstevel@tonic-gate 	/*
13640Sstevel@tonic-gate 	 * Set the thread in the TS_FREE state.  The state will change
13650Sstevel@tonic-gate 	 * to TS_ONPROC only while the interrupt is active.  Think of these
13660Sstevel@tonic-gate 	 * as being on a private free list for the CPU.  Being TS_FREE keeps
13670Sstevel@tonic-gate 	 * inactive interrupt threads out of debugger thread lists.
13680Sstevel@tonic-gate 	 *
13690Sstevel@tonic-gate 	 * We cannot call thread_create with TS_FREE because of the current
13700Sstevel@tonic-gate 	 * checks there for ONPROC.  Fix this when thread_create takes flags.
13710Sstevel@tonic-gate 	 */
13720Sstevel@tonic-gate 	THREAD_FREEINTR(tp, cp);
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate 	/*
13750Sstevel@tonic-gate 	 * Nobody should ever reference the credentials of an interrupt
13760Sstevel@tonic-gate 	 * thread so make it NULL to catch any such references.
13770Sstevel@tonic-gate 	 */
13780Sstevel@tonic-gate 	tp->t_cred = NULL;
13790Sstevel@tonic-gate 	tp->t_flag |= T_INTR_THREAD;
13800Sstevel@tonic-gate 	tp->t_cpu = cp;
13810Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
13820Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
13830Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
13840Sstevel@tonic-gate 	tp->t_preempt = 1;
13850Sstevel@tonic-gate 
13860Sstevel@tonic-gate 	/*
13870Sstevel@tonic-gate 	 * Don't make a user-requested binding on this thread so that
13880Sstevel@tonic-gate 	 * the processor can be offlined.
13890Sstevel@tonic-gate 	 */
13900Sstevel@tonic-gate 	tp->t_bind_cpu = PBIND_NONE;	/* no USER-requested binding */
13910Sstevel@tonic-gate 	tp->t_bind_pset = PS_NONE;
13920Sstevel@tonic-gate 
13930Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
13940Sstevel@tonic-gate 	tp->t_stk -= STACK_ALIGN;
13950Sstevel@tonic-gate 	*(tp->t_stk) = 0;		/* terminate intr thread stack */
13960Sstevel@tonic-gate #endif
13970Sstevel@tonic-gate 
13980Sstevel@tonic-gate 	/*
13990Sstevel@tonic-gate 	 * Link onto CPU's interrupt pool.
14000Sstevel@tonic-gate 	 */
14010Sstevel@tonic-gate 	tp->t_link = cp->cpu_intr_thread;
14020Sstevel@tonic-gate 	cp->cpu_intr_thread = tp;
14030Sstevel@tonic-gate }
14040Sstevel@tonic-gate 
14050Sstevel@tonic-gate /*
14060Sstevel@tonic-gate  * TSD -- THREAD SPECIFIC DATA
14070Sstevel@tonic-gate  */
14080Sstevel@tonic-gate static kmutex_t		tsd_mutex;	 /* linked list spin lock */
14090Sstevel@tonic-gate static uint_t		tsd_nkeys;	 /* size of destructor array */
14100Sstevel@tonic-gate /* per-key destructor funcs */
14110Sstevel@tonic-gate static void 		(**tsd_destructor)(void *);
14120Sstevel@tonic-gate /* list of tsd_thread's */
14130Sstevel@tonic-gate static struct tsd_thread	*tsd_list;
14140Sstevel@tonic-gate 
14150Sstevel@tonic-gate /*
14160Sstevel@tonic-gate  * Default destructor
14170Sstevel@tonic-gate  *	Needed because NULL destructor means that the key is unused
14180Sstevel@tonic-gate  */
14190Sstevel@tonic-gate /* ARGSUSED */
14200Sstevel@tonic-gate void
tsd_defaultdestructor(void * value)14210Sstevel@tonic-gate tsd_defaultdestructor(void *value)
14220Sstevel@tonic-gate {}
14230Sstevel@tonic-gate 
14240Sstevel@tonic-gate /*
14250Sstevel@tonic-gate  * Create a key (index into per thread array)
14260Sstevel@tonic-gate  *	Locks out tsd_create, tsd_destroy, and tsd_exit
14270Sstevel@tonic-gate  *	May allocate memory with lock held
14280Sstevel@tonic-gate  */
14290Sstevel@tonic-gate void
tsd_create(uint_t * keyp,void (* destructor)(void *))14300Sstevel@tonic-gate tsd_create(uint_t *keyp, void (*destructor)(void *))
14310Sstevel@tonic-gate {
14320Sstevel@tonic-gate 	int	i;
14330Sstevel@tonic-gate 	uint_t	nkeys;
14340Sstevel@tonic-gate 
14350Sstevel@tonic-gate 	/*
14360Sstevel@tonic-gate 	 * if key is allocated, do nothing
14370Sstevel@tonic-gate 	 */
14380Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
14390Sstevel@tonic-gate 	if (*keyp) {
14400Sstevel@tonic-gate 		mutex_exit(&tsd_mutex);
14410Sstevel@tonic-gate 		return;
14420Sstevel@tonic-gate 	}
14430Sstevel@tonic-gate 	/*
14440Sstevel@tonic-gate 	 * find an unused key
14450Sstevel@tonic-gate 	 */
14460Sstevel@tonic-gate 	if (destructor == NULL)
14470Sstevel@tonic-gate 		destructor = tsd_defaultdestructor;
14480Sstevel@tonic-gate 
14490Sstevel@tonic-gate 	for (i = 0; i < tsd_nkeys; ++i)
14500Sstevel@tonic-gate 		if (tsd_destructor[i] == NULL)
14510Sstevel@tonic-gate 			break;
14520Sstevel@tonic-gate 
14530Sstevel@tonic-gate 	/*
14540Sstevel@tonic-gate 	 * if no unused keys, increase the size of the destructor array
14550Sstevel@tonic-gate 	 */
14560Sstevel@tonic-gate 	if (i == tsd_nkeys) {
14570Sstevel@tonic-gate 		if ((nkeys = (tsd_nkeys << 1)) == 0)
14580Sstevel@tonic-gate 			nkeys = 1;
14590Sstevel@tonic-gate 		tsd_destructor =
14600Sstevel@tonic-gate 		    (void (**)(void *))tsd_realloc((void *)tsd_destructor,
14610Sstevel@tonic-gate 		    (size_t)(tsd_nkeys * sizeof (void (*)(void *))),
14620Sstevel@tonic-gate 		    (size_t)(nkeys * sizeof (void (*)(void *))));
14630Sstevel@tonic-gate 		tsd_nkeys = nkeys;
14640Sstevel@tonic-gate 	}
14650Sstevel@tonic-gate 
14660Sstevel@tonic-gate 	/*
14670Sstevel@tonic-gate 	 * allocate the next available unused key
14680Sstevel@tonic-gate 	 */
14690Sstevel@tonic-gate 	tsd_destructor[i] = destructor;
14700Sstevel@tonic-gate 	*keyp = i + 1;
14710Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
14720Sstevel@tonic-gate }
14730Sstevel@tonic-gate 
14740Sstevel@tonic-gate /*
14750Sstevel@tonic-gate  * Destroy a key -- this is for unloadable modules
14760Sstevel@tonic-gate  *
14770Sstevel@tonic-gate  * Assumes that the caller is preventing tsd_set and tsd_get
14780Sstevel@tonic-gate  * Locks out tsd_create, tsd_destroy, and tsd_exit
14790Sstevel@tonic-gate  * May free memory with lock held
14800Sstevel@tonic-gate  */
14810Sstevel@tonic-gate void
tsd_destroy(uint_t * keyp)14820Sstevel@tonic-gate tsd_destroy(uint_t *keyp)
14830Sstevel@tonic-gate {
14840Sstevel@tonic-gate 	uint_t key;
14850Sstevel@tonic-gate 	struct tsd_thread *tsd;
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	/*
14880Sstevel@tonic-gate 	 * protect the key namespace and our destructor lists
14890Sstevel@tonic-gate 	 */
14900Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
14910Sstevel@tonic-gate 	key = *keyp;
14920Sstevel@tonic-gate 	*keyp = 0;
14930Sstevel@tonic-gate 
14940Sstevel@tonic-gate 	ASSERT(key <= tsd_nkeys);
14950Sstevel@tonic-gate 
14960Sstevel@tonic-gate 	/*
14970Sstevel@tonic-gate 	 * if the key is valid
14980Sstevel@tonic-gate 	 */
14990Sstevel@tonic-gate 	if (key != 0) {
15000Sstevel@tonic-gate 		uint_t k = key - 1;
15010Sstevel@tonic-gate 		/*
15020Sstevel@tonic-gate 		 * for every thread with TSD, call key's destructor
15030Sstevel@tonic-gate 		 */
15040Sstevel@tonic-gate 		for (tsd = tsd_list; tsd; tsd = tsd->ts_next) {
15050Sstevel@tonic-gate 			/*
15060Sstevel@tonic-gate 			 * no TSD for key in this thread
15070Sstevel@tonic-gate 			 */
15080Sstevel@tonic-gate 			if (key > tsd->ts_nkeys)
15090Sstevel@tonic-gate 				continue;
15100Sstevel@tonic-gate 			/*
15110Sstevel@tonic-gate 			 * call destructor for key
15120Sstevel@tonic-gate 			 */
15130Sstevel@tonic-gate 			if (tsd->ts_value[k] && tsd_destructor[k])
15140Sstevel@tonic-gate 				(*tsd_destructor[k])(tsd->ts_value[k]);
15150Sstevel@tonic-gate 			/*
15160Sstevel@tonic-gate 			 * reset value for key
15170Sstevel@tonic-gate 			 */
15180Sstevel@tonic-gate 			tsd->ts_value[k] = NULL;
15190Sstevel@tonic-gate 		}
15200Sstevel@tonic-gate 		/*
15210Sstevel@tonic-gate 		 * actually free the key (NULL destructor == unused)
15220Sstevel@tonic-gate 		 */
15230Sstevel@tonic-gate 		tsd_destructor[k] = NULL;
15240Sstevel@tonic-gate 	}
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
15270Sstevel@tonic-gate }
15280Sstevel@tonic-gate 
15290Sstevel@tonic-gate /*
15300Sstevel@tonic-gate  * Quickly return the per thread value that was stored with the specified key
15310Sstevel@tonic-gate  * Assumes the caller is protecting key from tsd_create and tsd_destroy
15320Sstevel@tonic-gate  */
15330Sstevel@tonic-gate void *
tsd_get(uint_t key)15340Sstevel@tonic-gate tsd_get(uint_t key)
15350Sstevel@tonic-gate {
15360Sstevel@tonic-gate 	return (tsd_agent_get(curthread, key));
15370Sstevel@tonic-gate }
15380Sstevel@tonic-gate 
15390Sstevel@tonic-gate /*
15400Sstevel@tonic-gate  * Set a per thread value indexed with the specified key
15410Sstevel@tonic-gate  */
15420Sstevel@tonic-gate int
tsd_set(uint_t key,void * value)15430Sstevel@tonic-gate tsd_set(uint_t key, void *value)
15440Sstevel@tonic-gate {
15450Sstevel@tonic-gate 	return (tsd_agent_set(curthread, key, value));
15460Sstevel@tonic-gate }
15470Sstevel@tonic-gate 
15480Sstevel@tonic-gate /*
15490Sstevel@tonic-gate  * Like tsd_get(), except that the agent lwp can get the tsd of
15500Sstevel@tonic-gate  * another thread in the same process (the agent thread only runs when the
15510Sstevel@tonic-gate  * process is completely stopped by /proc), or syslwp is creating a new lwp.
15520Sstevel@tonic-gate  */
15530Sstevel@tonic-gate void *
tsd_agent_get(kthread_t * t,uint_t key)15540Sstevel@tonic-gate tsd_agent_get(kthread_t *t, uint_t key)
15550Sstevel@tonic-gate {
15560Sstevel@tonic-gate 	struct tsd_thread *tsd = t->t_tsd;
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate 	ASSERT(t == curthread ||
15590Sstevel@tonic-gate 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
15600Sstevel@tonic-gate 
15610Sstevel@tonic-gate 	if (key && tsd != NULL && key <= tsd->ts_nkeys)
15620Sstevel@tonic-gate 		return (tsd->ts_value[key - 1]);
15630Sstevel@tonic-gate 	return (NULL);
15640Sstevel@tonic-gate }
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate /*
15670Sstevel@tonic-gate  * Like tsd_set(), except that the agent lwp can set the tsd of
15680Sstevel@tonic-gate  * another thread in the same process, or syslwp can set the tsd
15690Sstevel@tonic-gate  * of a thread it's in the middle of creating.
15700Sstevel@tonic-gate  *
15710Sstevel@tonic-gate  * Assumes the caller is protecting key from tsd_create and tsd_destroy
15720Sstevel@tonic-gate  * May lock out tsd_destroy (and tsd_create), may allocate memory with
15730Sstevel@tonic-gate  * lock held
15740Sstevel@tonic-gate  */
15750Sstevel@tonic-gate int
tsd_agent_set(kthread_t * t,uint_t key,void * value)15760Sstevel@tonic-gate tsd_agent_set(kthread_t *t, uint_t key, void *value)
15770Sstevel@tonic-gate {
15780Sstevel@tonic-gate 	struct tsd_thread *tsd = t->t_tsd;
15790Sstevel@tonic-gate 
15800Sstevel@tonic-gate 	ASSERT(t == curthread ||
15810Sstevel@tonic-gate 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
15820Sstevel@tonic-gate 
15830Sstevel@tonic-gate 	if (key == 0)
15840Sstevel@tonic-gate 		return (EINVAL);
15850Sstevel@tonic-gate 	if (tsd == NULL)
15860Sstevel@tonic-gate 		tsd = t->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
15870Sstevel@tonic-gate 	if (key <= tsd->ts_nkeys) {
15880Sstevel@tonic-gate 		tsd->ts_value[key - 1] = value;
15890Sstevel@tonic-gate 		return (0);
15900Sstevel@tonic-gate 	}
15910Sstevel@tonic-gate 
15920Sstevel@tonic-gate 	ASSERT(key <= tsd_nkeys);
15930Sstevel@tonic-gate 
15940Sstevel@tonic-gate 	/*
15950Sstevel@tonic-gate 	 * lock out tsd_destroy()
15960Sstevel@tonic-gate 	 */
15970Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
15980Sstevel@tonic-gate 	if (tsd->ts_nkeys == 0) {
15990Sstevel@tonic-gate 		/*
16000Sstevel@tonic-gate 		 * Link onto list of threads with TSD
16010Sstevel@tonic-gate 		 */
16020Sstevel@tonic-gate 		if ((tsd->ts_next = tsd_list) != NULL)
16030Sstevel@tonic-gate 			tsd_list->ts_prev = tsd;
16040Sstevel@tonic-gate 		tsd_list = tsd;
16050Sstevel@tonic-gate 	}
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate 	/*
16080Sstevel@tonic-gate 	 * Allocate thread local storage and set the value for key
16090Sstevel@tonic-gate 	 */
16100Sstevel@tonic-gate 	tsd->ts_value = tsd_realloc(tsd->ts_value,
16110Sstevel@tonic-gate 	    tsd->ts_nkeys * sizeof (void *),
16120Sstevel@tonic-gate 	    key * sizeof (void *));
16130Sstevel@tonic-gate 	tsd->ts_nkeys = key;
16140Sstevel@tonic-gate 	tsd->ts_value[key - 1] = value;
16150Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
16160Sstevel@tonic-gate 
16170Sstevel@tonic-gate 	return (0);
16180Sstevel@tonic-gate }
16190Sstevel@tonic-gate 
16200Sstevel@tonic-gate 
16210Sstevel@tonic-gate /*
16220Sstevel@tonic-gate  * Return the per thread value that was stored with the specified key
16230Sstevel@tonic-gate  *	If necessary, create the key and the value
16240Sstevel@tonic-gate  *	Assumes the caller is protecting *keyp from tsd_destroy
16250Sstevel@tonic-gate  */
16260Sstevel@tonic-gate void *
tsd_getcreate(uint_t * keyp,void (* destroy)(void *),void * (* allocate)(void))16270Sstevel@tonic-gate tsd_getcreate(uint_t *keyp, void (*destroy)(void *), void *(*allocate)(void))
16280Sstevel@tonic-gate {
16290Sstevel@tonic-gate 	void *value;
16300Sstevel@tonic-gate 	uint_t key = *keyp;
16310Sstevel@tonic-gate 	struct tsd_thread *tsd = curthread->t_tsd;
16320Sstevel@tonic-gate 
16330Sstevel@tonic-gate 	if (tsd == NULL)
16340Sstevel@tonic-gate 		tsd = curthread->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
16350Sstevel@tonic-gate 	if (key && key <= tsd->ts_nkeys && (value = tsd->ts_value[key - 1]))
16360Sstevel@tonic-gate 		return (value);
16370Sstevel@tonic-gate 	if (key == 0)
16380Sstevel@tonic-gate 		tsd_create(keyp, destroy);
16390Sstevel@tonic-gate 	(void) tsd_set(*keyp, value = (*allocate)());
16400Sstevel@tonic-gate 
16410Sstevel@tonic-gate 	return (value);
16420Sstevel@tonic-gate }
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate /*
16450Sstevel@tonic-gate  * Called from thread_exit() to run the destructor function for each tsd
16460Sstevel@tonic-gate  *	Locks out tsd_create and tsd_destroy
16470Sstevel@tonic-gate  *	Assumes that the destructor *DOES NOT* use tsd
16480Sstevel@tonic-gate  */
16490Sstevel@tonic-gate void
tsd_exit(void)16500Sstevel@tonic-gate tsd_exit(void)
16510Sstevel@tonic-gate {
16520Sstevel@tonic-gate 	int i;
16530Sstevel@tonic-gate 	struct tsd_thread *tsd = curthread->t_tsd;
16540Sstevel@tonic-gate 
16550Sstevel@tonic-gate 	if (tsd == NULL)
16560Sstevel@tonic-gate 		return;
16570Sstevel@tonic-gate 
16580Sstevel@tonic-gate 	if (tsd->ts_nkeys == 0) {
16590Sstevel@tonic-gate 		kmem_free(tsd, sizeof (*tsd));
16600Sstevel@tonic-gate 		curthread->t_tsd = NULL;
16610Sstevel@tonic-gate 		return;
16620Sstevel@tonic-gate 	}
16630Sstevel@tonic-gate 
16640Sstevel@tonic-gate 	/*
16650Sstevel@tonic-gate 	 * lock out tsd_create and tsd_destroy, call
16660Sstevel@tonic-gate 	 * the destructor, and mark the value as destroyed.
16670Sstevel@tonic-gate 	 */
16680Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
16690Sstevel@tonic-gate 
16700Sstevel@tonic-gate 	for (i = 0; i < tsd->ts_nkeys; i++) {
16710Sstevel@tonic-gate 		if (tsd->ts_value[i] && tsd_destructor[i])
16720Sstevel@tonic-gate 			(*tsd_destructor[i])(tsd->ts_value[i]);
16730Sstevel@tonic-gate 		tsd->ts_value[i] = NULL;
16740Sstevel@tonic-gate 	}
16750Sstevel@tonic-gate 
16760Sstevel@tonic-gate 	/*
16770Sstevel@tonic-gate 	 * remove from linked list of threads with TSD
16780Sstevel@tonic-gate 	 */
16790Sstevel@tonic-gate 	if (tsd->ts_next)
16800Sstevel@tonic-gate 		tsd->ts_next->ts_prev = tsd->ts_prev;
16810Sstevel@tonic-gate 	if (tsd->ts_prev)
16820Sstevel@tonic-gate 		tsd->ts_prev->ts_next = tsd->ts_next;
16830Sstevel@tonic-gate 	if (tsd_list == tsd)
16840Sstevel@tonic-gate 		tsd_list = tsd->ts_next;
16850Sstevel@tonic-gate 
16860Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
16870Sstevel@tonic-gate 
16880Sstevel@tonic-gate 	/*
16890Sstevel@tonic-gate 	 * free up the TSD
16900Sstevel@tonic-gate 	 */
16910Sstevel@tonic-gate 	kmem_free(tsd->ts_value, tsd->ts_nkeys * sizeof (void *));
16920Sstevel@tonic-gate 	kmem_free(tsd, sizeof (struct tsd_thread));
16930Sstevel@tonic-gate 	curthread->t_tsd = NULL;
16940Sstevel@tonic-gate }
16950Sstevel@tonic-gate 
16960Sstevel@tonic-gate /*
16970Sstevel@tonic-gate  * realloc
16980Sstevel@tonic-gate  */
16990Sstevel@tonic-gate static void *
tsd_realloc(void * old,size_t osize,size_t nsize)17000Sstevel@tonic-gate tsd_realloc(void *old, size_t osize, size_t nsize)
17010Sstevel@tonic-gate {
17020Sstevel@tonic-gate 	void *new;
17030Sstevel@tonic-gate 
17040Sstevel@tonic-gate 	new = kmem_zalloc(nsize, KM_SLEEP);
17050Sstevel@tonic-gate 	if (old) {
17060Sstevel@tonic-gate 		bcopy(old, new, osize);
17070Sstevel@tonic-gate 		kmem_free(old, osize);
17080Sstevel@tonic-gate 	}
17090Sstevel@tonic-gate 	return (new);
17100Sstevel@tonic-gate }
17110Sstevel@tonic-gate 
17120Sstevel@tonic-gate /*
17130Sstevel@tonic-gate  * Return non-zero if an interrupt is being serviced.
17140Sstevel@tonic-gate  */
17150Sstevel@tonic-gate int
servicing_interrupt()17160Sstevel@tonic-gate servicing_interrupt()
17170Sstevel@tonic-gate {
17181845Ssudheer 	int onintr = 0;
17191845Ssudheer 
17201845Ssudheer 	/* Are we an interrupt thread */
17211845Ssudheer 	if (curthread->t_flag & T_INTR_THREAD)
17221845Ssudheer 		return (1);
17231845Ssudheer 	/* Are we servicing a high level interrupt? */
17241845Ssudheer 	if (CPU_ON_INTR(CPU)) {
17251845Ssudheer 		kpreempt_disable();
17261845Ssudheer 		onintr = CPU_ON_INTR(CPU);
17271845Ssudheer 		kpreempt_enable();
17281845Ssudheer 	}
17291845Ssudheer 	return (onintr);
17300Sstevel@tonic-gate }
17310Sstevel@tonic-gate 
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate /*
17340Sstevel@tonic-gate  * Change the dispatch priority of a thread in the system.
17350Sstevel@tonic-gate  * Used when raising or lowering a thread's priority.
17360Sstevel@tonic-gate  * (E.g., priority inheritance)
17370Sstevel@tonic-gate  *
17380Sstevel@tonic-gate  * Since threads are queued according to their priority, we
17390Sstevel@tonic-gate  * we must check the thread's state to determine whether it
17400Sstevel@tonic-gate  * is on a queue somewhere. If it is, we've got to:
17410Sstevel@tonic-gate  *
17420Sstevel@tonic-gate  *	o Dequeue the thread.
17430Sstevel@tonic-gate  *	o Change its effective priority.
17440Sstevel@tonic-gate  *	o Enqueue the thread.
17450Sstevel@tonic-gate  *
17460Sstevel@tonic-gate  * Assumptions: The thread whose priority we wish to change
17470Sstevel@tonic-gate  * must be locked before we call thread_change_(e)pri().
17480Sstevel@tonic-gate  * The thread_change(e)pri() function doesn't drop the thread
17490Sstevel@tonic-gate  * lock--that must be done by its caller.
17500Sstevel@tonic-gate  */
17510Sstevel@tonic-gate void
thread_change_epri(kthread_t * t,pri_t disp_pri)17520Sstevel@tonic-gate thread_change_epri(kthread_t *t, pri_t disp_pri)
17530Sstevel@tonic-gate {
17540Sstevel@tonic-gate 	uint_t	state;
17550Sstevel@tonic-gate 
17560Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
17570Sstevel@tonic-gate 
17580Sstevel@tonic-gate 	/*
17590Sstevel@tonic-gate 	 * If the inherited priority hasn't actually changed,
17600Sstevel@tonic-gate 	 * just return.
17610Sstevel@tonic-gate 	 */
17620Sstevel@tonic-gate 	if (t->t_epri == disp_pri)
17630Sstevel@tonic-gate 		return;
17640Sstevel@tonic-gate 
17650Sstevel@tonic-gate 	state = t->t_state;
17660Sstevel@tonic-gate 
17670Sstevel@tonic-gate 	/*
17686247Sraf 	 * If it's not on a queue, change the priority with impunity.
17690Sstevel@tonic-gate 	 */
17703792Sakolb 	if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
17710Sstevel@tonic-gate 		t->t_epri = disp_pri;
17720Sstevel@tonic-gate 		if (state == TS_ONPROC) {
17730Sstevel@tonic-gate 			cpu_t *cp = t->t_disp_queue->disp_cpu;
17740Sstevel@tonic-gate 
17750Sstevel@tonic-gate 			if (t == cp->cpu_dispthread)
17760Sstevel@tonic-gate 				cp->cpu_dispatch_pri = DISP_PRIO(t);
17770Sstevel@tonic-gate 		}
17786247Sraf 	} else if (state == TS_SLEEP) {
17790Sstevel@tonic-gate 		/*
17800Sstevel@tonic-gate 		 * Take the thread out of its sleep queue.
17810Sstevel@tonic-gate 		 * Change the inherited priority.
17820Sstevel@tonic-gate 		 * Re-enqueue the thread.
17830Sstevel@tonic-gate 		 * Each synchronization object exports a function
17840Sstevel@tonic-gate 		 * to do this in an appropriate manner.
17850Sstevel@tonic-gate 		 */
17860Sstevel@tonic-gate 		SOBJ_CHANGE_EPRI(t->t_sobj_ops, t, disp_pri);
17873792Sakolb 	} else if (state == TS_WAIT) {
17883792Sakolb 		/*
17893792Sakolb 		 * Re-enqueue a thread on the wait queue if its
17903792Sakolb 		 * effective priority needs to change.
17913792Sakolb 		 */
17923792Sakolb 		if (disp_pri != t->t_epri)
17933792Sakolb 			waitq_change_pri(t, disp_pri);
17940Sstevel@tonic-gate 	} else {
17950Sstevel@tonic-gate 		/*
17960Sstevel@tonic-gate 		 * The thread is on a run queue.
17970Sstevel@tonic-gate 		 * Note: setbackdq() may not put the thread
17980Sstevel@tonic-gate 		 * back on the same run queue where it originally
17990Sstevel@tonic-gate 		 * resided.
18000Sstevel@tonic-gate 		 */
18010Sstevel@tonic-gate 		(void) dispdeq(t);
18020Sstevel@tonic-gate 		t->t_epri = disp_pri;
18030Sstevel@tonic-gate 		setbackdq(t);
18040Sstevel@tonic-gate 	}
18056247Sraf 	schedctl_set_cidpri(t);
18066247Sraf }
18070Sstevel@tonic-gate 
18080Sstevel@tonic-gate /*
18090Sstevel@tonic-gate  * Function: Change the t_pri field of a thread.
18100Sstevel@tonic-gate  * Side Effects: Adjust the thread ordering on a run queue
18110Sstevel@tonic-gate  *		 or sleep queue, if necessary.
18120Sstevel@tonic-gate  * Returns: 1 if the thread was on a run queue, else 0.
18130Sstevel@tonic-gate  */
18140Sstevel@tonic-gate int
thread_change_pri(kthread_t * t,pri_t disp_pri,int front)18150Sstevel@tonic-gate thread_change_pri(kthread_t *t, pri_t disp_pri, int front)
18160Sstevel@tonic-gate {
18170Sstevel@tonic-gate 	uint_t	state;
18180Sstevel@tonic-gate 	int	on_rq = 0;
18190Sstevel@tonic-gate 
18200Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
18210Sstevel@tonic-gate 
18220Sstevel@tonic-gate 	state = t->t_state;
18230Sstevel@tonic-gate 	THREAD_WILLCHANGE_PRI(t, disp_pri);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	/*
18266247Sraf 	 * If it's not on a queue, change the priority with impunity.
18270Sstevel@tonic-gate 	 */
18283792Sakolb 	if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
18290Sstevel@tonic-gate 		t->t_pri = disp_pri;
18300Sstevel@tonic-gate 
18310Sstevel@tonic-gate 		if (state == TS_ONPROC) {
18320Sstevel@tonic-gate 			cpu_t *cp = t->t_disp_queue->disp_cpu;
18330Sstevel@tonic-gate 
18340Sstevel@tonic-gate 			if (t == cp->cpu_dispthread)
18350Sstevel@tonic-gate 				cp->cpu_dispatch_pri = DISP_PRIO(t);
18360Sstevel@tonic-gate 		}
18376247Sraf 	} else if (state == TS_SLEEP) {
18380Sstevel@tonic-gate 		/*
18390Sstevel@tonic-gate 		 * If the priority has changed, take the thread out of
18400Sstevel@tonic-gate 		 * its sleep queue and change the priority.
18410Sstevel@tonic-gate 		 * Re-enqueue the thread.
18420Sstevel@tonic-gate 		 * Each synchronization object exports a function
18430Sstevel@tonic-gate 		 * to do this in an appropriate manner.
18440Sstevel@tonic-gate 		 */
18450Sstevel@tonic-gate 		if (disp_pri != t->t_pri)
18460Sstevel@tonic-gate 			SOBJ_CHANGE_PRI(t->t_sobj_ops, t, disp_pri);
18473792Sakolb 	} else if (state == TS_WAIT) {
18483792Sakolb 		/*
18493792Sakolb 		 * Re-enqueue a thread on the wait queue if its
18503792Sakolb 		 * priority needs to change.
18513792Sakolb 		 */
18523792Sakolb 		if (disp_pri != t->t_pri)
18533792Sakolb 			waitq_change_pri(t, disp_pri);
18540Sstevel@tonic-gate 	} else {
18550Sstevel@tonic-gate 		/*
18560Sstevel@tonic-gate 		 * The thread is on a run queue.
18570Sstevel@tonic-gate 		 * Note: setbackdq() may not put the thread
18580Sstevel@tonic-gate 		 * back on the same run queue where it originally
18590Sstevel@tonic-gate 		 * resided.
18600Sstevel@tonic-gate 		 *
18610Sstevel@tonic-gate 		 * We still requeue the thread even if the priority
18620Sstevel@tonic-gate 		 * is unchanged to preserve round-robin (and other)
18630Sstevel@tonic-gate 		 * effects between threads of the same priority.
18640Sstevel@tonic-gate 		 */
18650Sstevel@tonic-gate 		on_rq = dispdeq(t);
18660Sstevel@tonic-gate 		ASSERT(on_rq);
18670Sstevel@tonic-gate 		t->t_pri = disp_pri;
18680Sstevel@tonic-gate 		if (front) {
18690Sstevel@tonic-gate 			setfrontdq(t);
18700Sstevel@tonic-gate 		} else {
18710Sstevel@tonic-gate 			setbackdq(t);
18720Sstevel@tonic-gate 		}
18730Sstevel@tonic-gate 	}
18746247Sraf 	schedctl_set_cidpri(t);
18750Sstevel@tonic-gate 	return (on_rq);
18760Sstevel@tonic-gate }
18777854SPhilippe.Jung@Sun.COM 
18787854SPhilippe.Jung@Sun.COM /*
18797854SPhilippe.Jung@Sun.COM  * Tunable kmem_stackinfo is set, fill the kernel thread stack with a
18807854SPhilippe.Jung@Sun.COM  * specific pattern.
18817854SPhilippe.Jung@Sun.COM  */
18827854SPhilippe.Jung@Sun.COM static void
stkinfo_begin(kthread_t * t)18837854SPhilippe.Jung@Sun.COM stkinfo_begin(kthread_t *t)
18847854SPhilippe.Jung@Sun.COM {
18857854SPhilippe.Jung@Sun.COM 	caddr_t	start;	/* stack start */
18867854SPhilippe.Jung@Sun.COM 	caddr_t	end;	/* stack end  */
18877854SPhilippe.Jung@Sun.COM 	uint64_t *ptr;	/* pattern pointer */
18887854SPhilippe.Jung@Sun.COM 
18897854SPhilippe.Jung@Sun.COM 	/*
18907854SPhilippe.Jung@Sun.COM 	 * Stack grows up or down, see thread_create(),
18917854SPhilippe.Jung@Sun.COM 	 * compute stack memory area start and end (start < end).
18927854SPhilippe.Jung@Sun.COM 	 */
18937854SPhilippe.Jung@Sun.COM 	if (t->t_stk > t->t_stkbase) {
18947854SPhilippe.Jung@Sun.COM 		/* stack grows down */
18957854SPhilippe.Jung@Sun.COM 		start = t->t_stkbase;
18967854SPhilippe.Jung@Sun.COM 		end = t->t_stk;
18977854SPhilippe.Jung@Sun.COM 	} else {
18987854SPhilippe.Jung@Sun.COM 		/* stack grows up */
18997854SPhilippe.Jung@Sun.COM 		start = t->t_stk;
19007854SPhilippe.Jung@Sun.COM 		end = t->t_stkbase;
19017854SPhilippe.Jung@Sun.COM 	}
19027854SPhilippe.Jung@Sun.COM 
19037854SPhilippe.Jung@Sun.COM 	/*
19047854SPhilippe.Jung@Sun.COM 	 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
19057854SPhilippe.Jung@Sun.COM 	 * alignement for start and end in stack area boundaries
19067854SPhilippe.Jung@Sun.COM 	 * (protection against corrupt t_stkbase/t_stk data).
19077854SPhilippe.Jung@Sun.COM 	 */
19087854SPhilippe.Jung@Sun.COM 	if ((((uintptr_t)start) & 0x7) != 0) {
19097854SPhilippe.Jung@Sun.COM 		start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
19107854SPhilippe.Jung@Sun.COM 	}
19117854SPhilippe.Jung@Sun.COM 	end = (caddr_t)(((uintptr_t)end) & (~0x7));
19127854SPhilippe.Jung@Sun.COM 
19137854SPhilippe.Jung@Sun.COM 	if ((end <= start) || (end - start) > (1024 * 1024)) {
19147854SPhilippe.Jung@Sun.COM 		/* negative or stack size > 1 meg, assume bogus */
19157854SPhilippe.Jung@Sun.COM 		return;
19167854SPhilippe.Jung@Sun.COM 	}
19177854SPhilippe.Jung@Sun.COM 
19187854SPhilippe.Jung@Sun.COM 	/* fill stack area with a pattern (instead of zeros) */
19197854SPhilippe.Jung@Sun.COM 	ptr = (uint64_t *)((void *)start);
19207854SPhilippe.Jung@Sun.COM 	while (ptr < (uint64_t *)((void *)end)) {
19217854SPhilippe.Jung@Sun.COM 		*ptr++ = KMEM_STKINFO_PATTERN;
19227854SPhilippe.Jung@Sun.COM 	}
19237854SPhilippe.Jung@Sun.COM }
19247854SPhilippe.Jung@Sun.COM 
19257854SPhilippe.Jung@Sun.COM 
19267854SPhilippe.Jung@Sun.COM /*
19277854SPhilippe.Jung@Sun.COM  * Tunable kmem_stackinfo is set, create stackinfo log if doesn't already exist,
19287854SPhilippe.Jung@Sun.COM  * compute the percentage of kernel stack really used, and set in the log
19297854SPhilippe.Jung@Sun.COM  * if it's the latest highest percentage.
19307854SPhilippe.Jung@Sun.COM  */
19317854SPhilippe.Jung@Sun.COM static void
stkinfo_end(kthread_t * t)19327854SPhilippe.Jung@Sun.COM stkinfo_end(kthread_t *t)
19337854SPhilippe.Jung@Sun.COM {
19347854SPhilippe.Jung@Sun.COM 	caddr_t	start;	/* stack start */
19357854SPhilippe.Jung@Sun.COM 	caddr_t	end;	/* stack end  */
19367854SPhilippe.Jung@Sun.COM 	uint64_t *ptr;	/* pattern pointer */
19377854SPhilippe.Jung@Sun.COM 	size_t stksz;	/* stack size */
19387854SPhilippe.Jung@Sun.COM 	size_t smallest = 0;
19397854SPhilippe.Jung@Sun.COM 	size_t percent = 0;
19407854SPhilippe.Jung@Sun.COM 	uint_t index = 0;
19417854SPhilippe.Jung@Sun.COM 	uint_t i;
19427854SPhilippe.Jung@Sun.COM 	static size_t smallest_percent = (size_t)-1;
19437854SPhilippe.Jung@Sun.COM 	static uint_t full = 0;
19447854SPhilippe.Jung@Sun.COM 
19457854SPhilippe.Jung@Sun.COM 	/* create the stackinfo log, if doesn't already exist */
19467854SPhilippe.Jung@Sun.COM 	mutex_enter(&kmem_stkinfo_lock);
19477854SPhilippe.Jung@Sun.COM 	if (kmem_stkinfo_log == NULL) {
19487854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log = (kmem_stkinfo_t *)
19497854SPhilippe.Jung@Sun.COM 		    kmem_zalloc(KMEM_STKINFO_LOG_SIZE *
19507854SPhilippe.Jung@Sun.COM 		    (sizeof (kmem_stkinfo_t)), KM_NOSLEEP);
19517854SPhilippe.Jung@Sun.COM 		if (kmem_stkinfo_log == NULL) {
19527854SPhilippe.Jung@Sun.COM 			mutex_exit(&kmem_stkinfo_lock);
19537854SPhilippe.Jung@Sun.COM 			return;
19547854SPhilippe.Jung@Sun.COM 		}
19557854SPhilippe.Jung@Sun.COM 	}
19567854SPhilippe.Jung@Sun.COM 	mutex_exit(&kmem_stkinfo_lock);
19577854SPhilippe.Jung@Sun.COM 
19587854SPhilippe.Jung@Sun.COM 	/*
19597854SPhilippe.Jung@Sun.COM 	 * Stack grows up or down, see thread_create(),
19607854SPhilippe.Jung@Sun.COM 	 * compute stack memory area start and end (start < end).
19617854SPhilippe.Jung@Sun.COM 	 */
19627854SPhilippe.Jung@Sun.COM 	if (t->t_stk > t->t_stkbase) {
19637854SPhilippe.Jung@Sun.COM 		/* stack grows down */
19647854SPhilippe.Jung@Sun.COM 		start = t->t_stkbase;
19657854SPhilippe.Jung@Sun.COM 		end = t->t_stk;
19667854SPhilippe.Jung@Sun.COM 	} else {
19677854SPhilippe.Jung@Sun.COM 		/* stack grows up */
19687854SPhilippe.Jung@Sun.COM 		start = t->t_stk;
19697854SPhilippe.Jung@Sun.COM 		end = t->t_stkbase;
19707854SPhilippe.Jung@Sun.COM 	}
19717854SPhilippe.Jung@Sun.COM 
19727854SPhilippe.Jung@Sun.COM 	/* stack size as found in kthread_t */
19737854SPhilippe.Jung@Sun.COM 	stksz = end - start;
19747854SPhilippe.Jung@Sun.COM 
19757854SPhilippe.Jung@Sun.COM 	/*
19767854SPhilippe.Jung@Sun.COM 	 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
19777854SPhilippe.Jung@Sun.COM 	 * alignement for start and end in stack area boundaries
19787854SPhilippe.Jung@Sun.COM 	 * (protection against corrupt t_stkbase/t_stk data).
19797854SPhilippe.Jung@Sun.COM 	 */
19807854SPhilippe.Jung@Sun.COM 	if ((((uintptr_t)start) & 0x7) != 0) {
19817854SPhilippe.Jung@Sun.COM 		start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
19827854SPhilippe.Jung@Sun.COM 	}
19837854SPhilippe.Jung@Sun.COM 	end = (caddr_t)(((uintptr_t)end) & (~0x7));
19847854SPhilippe.Jung@Sun.COM 
19857854SPhilippe.Jung@Sun.COM 	if ((end <= start) || (end - start) > (1024 * 1024)) {
19867854SPhilippe.Jung@Sun.COM 		/* negative or stack size > 1 meg, assume bogus */
19877854SPhilippe.Jung@Sun.COM 		return;
19887854SPhilippe.Jung@Sun.COM 	}
19897854SPhilippe.Jung@Sun.COM 
19907854SPhilippe.Jung@Sun.COM 	/* search until no pattern in the stack */
19917854SPhilippe.Jung@Sun.COM 	if (t->t_stk > t->t_stkbase) {
19927854SPhilippe.Jung@Sun.COM 		/* stack grows down */
19937854SPhilippe.Jung@Sun.COM #if defined(__i386) || defined(__amd64)
19947854SPhilippe.Jung@Sun.COM 		/*
19957854SPhilippe.Jung@Sun.COM 		 * 6 longs are pushed on stack, see thread_load(). Skip
19967854SPhilippe.Jung@Sun.COM 		 * them, so if kthread has never run, percent is zero.
19977854SPhilippe.Jung@Sun.COM 		 * 8 bytes alignement is preserved for a 32 bit kernel,
19987854SPhilippe.Jung@Sun.COM 		 * 6 x 4 = 24, 24 is a multiple of 8.
19997854SPhilippe.Jung@Sun.COM 		 *
20007854SPhilippe.Jung@Sun.COM 		 */
20017854SPhilippe.Jung@Sun.COM 		end -= (6 * sizeof (long));
20027854SPhilippe.Jung@Sun.COM #endif
20037854SPhilippe.Jung@Sun.COM 		ptr = (uint64_t *)((void *)start);
20047854SPhilippe.Jung@Sun.COM 		while (ptr < (uint64_t *)((void *)end)) {
20057854SPhilippe.Jung@Sun.COM 			if (*ptr != KMEM_STKINFO_PATTERN) {
20067854SPhilippe.Jung@Sun.COM 				percent = stkinfo_percent(end,
20077854SPhilippe.Jung@Sun.COM 				    start, (caddr_t)ptr);
20087854SPhilippe.Jung@Sun.COM 				break;
20097854SPhilippe.Jung@Sun.COM 			}
20107854SPhilippe.Jung@Sun.COM 			ptr++;
20117854SPhilippe.Jung@Sun.COM 		}
20127854SPhilippe.Jung@Sun.COM 	} else {
20137854SPhilippe.Jung@Sun.COM 		/* stack grows up */
20147854SPhilippe.Jung@Sun.COM 		ptr = (uint64_t *)((void *)end);
20157854SPhilippe.Jung@Sun.COM 		ptr--;
20167854SPhilippe.Jung@Sun.COM 		while (ptr >= (uint64_t *)((void *)start)) {
20177854SPhilippe.Jung@Sun.COM 			if (*ptr != KMEM_STKINFO_PATTERN) {
20187854SPhilippe.Jung@Sun.COM 				percent = stkinfo_percent(start,
20197854SPhilippe.Jung@Sun.COM 				    end, (caddr_t)ptr);
20207854SPhilippe.Jung@Sun.COM 				break;
20217854SPhilippe.Jung@Sun.COM 			}
20227854SPhilippe.Jung@Sun.COM 			ptr--;
20237854SPhilippe.Jung@Sun.COM 		}
20247854SPhilippe.Jung@Sun.COM 	}
20257854SPhilippe.Jung@Sun.COM 
20267854SPhilippe.Jung@Sun.COM 	DTRACE_PROBE3(stack__usage, kthread_t *, t,
20277854SPhilippe.Jung@Sun.COM 	    size_t, stksz, size_t, percent);
20287854SPhilippe.Jung@Sun.COM 
20297854SPhilippe.Jung@Sun.COM 	if (percent == 0) {
20307854SPhilippe.Jung@Sun.COM 		return;
20317854SPhilippe.Jung@Sun.COM 	}
20327854SPhilippe.Jung@Sun.COM 
20337854SPhilippe.Jung@Sun.COM 	mutex_enter(&kmem_stkinfo_lock);
20347854SPhilippe.Jung@Sun.COM 	if (full == KMEM_STKINFO_LOG_SIZE && percent < smallest_percent) {
20357854SPhilippe.Jung@Sun.COM 		/*
20367854SPhilippe.Jung@Sun.COM 		 * The log is full and already contains the highest values
20377854SPhilippe.Jung@Sun.COM 		 */
20387854SPhilippe.Jung@Sun.COM 		mutex_exit(&kmem_stkinfo_lock);
20397854SPhilippe.Jung@Sun.COM 		return;
20407854SPhilippe.Jung@Sun.COM 	}
20417854SPhilippe.Jung@Sun.COM 
20427854SPhilippe.Jung@Sun.COM 	/* keep a log of the highest used stack */
20437854SPhilippe.Jung@Sun.COM 	for (i = 0; i < KMEM_STKINFO_LOG_SIZE; i++) {
20447854SPhilippe.Jung@Sun.COM 		if (kmem_stkinfo_log[i].percent == 0) {
20457854SPhilippe.Jung@Sun.COM 			index = i;
20467854SPhilippe.Jung@Sun.COM 			full++;
20477854SPhilippe.Jung@Sun.COM 			break;
20487854SPhilippe.Jung@Sun.COM 		}
20497854SPhilippe.Jung@Sun.COM 		if (smallest == 0) {
20507854SPhilippe.Jung@Sun.COM 			smallest = kmem_stkinfo_log[i].percent;
20517854SPhilippe.Jung@Sun.COM 			index = i;
20527854SPhilippe.Jung@Sun.COM 			continue;
20537854SPhilippe.Jung@Sun.COM 		}
20547854SPhilippe.Jung@Sun.COM 		if (kmem_stkinfo_log[i].percent < smallest) {
20557854SPhilippe.Jung@Sun.COM 			smallest = kmem_stkinfo_log[i].percent;
20567854SPhilippe.Jung@Sun.COM 			index = i;
20577854SPhilippe.Jung@Sun.COM 		}
20587854SPhilippe.Jung@Sun.COM 	}
20597854SPhilippe.Jung@Sun.COM 
20607854SPhilippe.Jung@Sun.COM 	if (percent >= kmem_stkinfo_log[index].percent) {
20617854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log[index].kthread = (caddr_t)t;
20627854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log[index].t_startpc = (caddr_t)t->t_startpc;
20637854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log[index].start = start;
20647854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log[index].stksz = stksz;
20657854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log[index].percent = percent;
20667854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log[index].t_tid = t->t_tid;
20677854SPhilippe.Jung@Sun.COM 		kmem_stkinfo_log[index].cmd[0] = '\0';
20687854SPhilippe.Jung@Sun.COM 		if (t->t_tid != 0) {
20697854SPhilippe.Jung@Sun.COM 			stksz = strlen((t->t_procp)->p_user.u_comm);
20707854SPhilippe.Jung@Sun.COM 			if (stksz >= KMEM_STKINFO_STR_SIZE) {
20717854SPhilippe.Jung@Sun.COM 				stksz = KMEM_STKINFO_STR_SIZE - 1;
20727854SPhilippe.Jung@Sun.COM 				kmem_stkinfo_log[index].cmd[stksz] = '\0';
20737854SPhilippe.Jung@Sun.COM 			} else {
20747854SPhilippe.Jung@Sun.COM 				stksz += 1;
20757854SPhilippe.Jung@Sun.COM 			}
20767854SPhilippe.Jung@Sun.COM 			(void) memcpy(kmem_stkinfo_log[index].cmd,
20777854SPhilippe.Jung@Sun.COM 			    (t->t_procp)->p_user.u_comm, stksz);
20787854SPhilippe.Jung@Sun.COM 		}
20797854SPhilippe.Jung@Sun.COM 		if (percent < smallest_percent) {
20807854SPhilippe.Jung@Sun.COM 			smallest_percent = percent;
20817854SPhilippe.Jung@Sun.COM 		}
20827854SPhilippe.Jung@Sun.COM 	}
20837854SPhilippe.Jung@Sun.COM 	mutex_exit(&kmem_stkinfo_lock);
20847854SPhilippe.Jung@Sun.COM }
20857854SPhilippe.Jung@Sun.COM 
20867854SPhilippe.Jung@Sun.COM /*
20877854SPhilippe.Jung@Sun.COM  * Tunable kmem_stackinfo is set, compute stack utilization percentage.
20887854SPhilippe.Jung@Sun.COM  */
20897854SPhilippe.Jung@Sun.COM static size_t
stkinfo_percent(caddr_t t_stk,caddr_t t_stkbase,caddr_t sp)20907854SPhilippe.Jung@Sun.COM stkinfo_percent(caddr_t t_stk, caddr_t t_stkbase, caddr_t sp)
20917854SPhilippe.Jung@Sun.COM {
20927854SPhilippe.Jung@Sun.COM 	size_t percent;
20937854SPhilippe.Jung@Sun.COM 	size_t s;
20947854SPhilippe.Jung@Sun.COM 
20957854SPhilippe.Jung@Sun.COM 	if (t_stk > t_stkbase) {
20967854SPhilippe.Jung@Sun.COM 		/* stack grows down */
20977854SPhilippe.Jung@Sun.COM 		if (sp > t_stk) {
20987854SPhilippe.Jung@Sun.COM 			return (0);
20997854SPhilippe.Jung@Sun.COM 		}
21007854SPhilippe.Jung@Sun.COM 		if (sp < t_stkbase) {
21017854SPhilippe.Jung@Sun.COM 			return (100);
21027854SPhilippe.Jung@Sun.COM 		}
21037854SPhilippe.Jung@Sun.COM 		percent = t_stk - sp + 1;
21047854SPhilippe.Jung@Sun.COM 		s = t_stk - t_stkbase + 1;
21057854SPhilippe.Jung@Sun.COM 	} else {
21067854SPhilippe.Jung@Sun.COM 		/* stack grows up */
21077854SPhilippe.Jung@Sun.COM 		if (sp < t_stk) {
21087854SPhilippe.Jung@Sun.COM 			return (0);
21097854SPhilippe.Jung@Sun.COM 		}
21107854SPhilippe.Jung@Sun.COM 		if (sp > t_stkbase) {
21117854SPhilippe.Jung@Sun.COM 			return (100);
21127854SPhilippe.Jung@Sun.COM 		}
21137854SPhilippe.Jung@Sun.COM 		percent = sp - t_stk + 1;
21147854SPhilippe.Jung@Sun.COM 		s = t_stkbase - t_stk + 1;
21157854SPhilippe.Jung@Sun.COM 	}
21167854SPhilippe.Jung@Sun.COM 	percent = ((100 * percent) / s) + 1;
21177854SPhilippe.Jung@Sun.COM 	if (percent > 100) {
21187854SPhilippe.Jung@Sun.COM 		percent = 100;
21197854SPhilippe.Jung@Sun.COM 	}
21207854SPhilippe.Jung@Sun.COM 	return (percent);
21217854SPhilippe.Jung@Sun.COM }
2122