xref: /titanic_53/usr/src/uts/common/disp/thread.c (revision 7af88ac71631ebf259c6c4c22a9f649ddff3e270)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
545916cd2Sjpk  * Common Development and Distribution License (the "License").
645916cd2Sjpk  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21d4204c85Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7af88ac7SKuriakose Kuruvilla  * Copyright (c) 1991, 2010, Oracle and/or its affiliates. All rights reserved.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate #include <sys/types.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
297c478bd9Sstevel@tonic-gate #include <sys/signal.h>
307c478bd9Sstevel@tonic-gate #include <sys/stack.h>
317c478bd9Sstevel@tonic-gate #include <sys/pcb.h>
327c478bd9Sstevel@tonic-gate #include <sys/user.h>
337c478bd9Sstevel@tonic-gate #include <sys/systm.h>
347c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h>
357c478bd9Sstevel@tonic-gate #include <sys/errno.h>
367c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
377c478bd9Sstevel@tonic-gate #include <sys/cred.h>
387c478bd9Sstevel@tonic-gate #include <sys/resource.h>
397c478bd9Sstevel@tonic-gate #include <sys/task.h>
407c478bd9Sstevel@tonic-gate #include <sys/project.h>
417c478bd9Sstevel@tonic-gate #include <sys/proc.h>
427c478bd9Sstevel@tonic-gate #include <sys/debug.h>
437c478bd9Sstevel@tonic-gate #include <sys/disp.h>
447c478bd9Sstevel@tonic-gate #include <sys/class.h>
457c478bd9Sstevel@tonic-gate #include <vm/seg_kmem.h>
467c478bd9Sstevel@tonic-gate #include <vm/seg_kp.h>
477c478bd9Sstevel@tonic-gate #include <sys/machlock.h>
487c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
497c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
507c478bd9Sstevel@tonic-gate #include <sys/turnstile.h>
517c478bd9Sstevel@tonic-gate #include <sys/poll.h>
527c478bd9Sstevel@tonic-gate #include <sys/vtrace.h>
537c478bd9Sstevel@tonic-gate #include <sys/callb.h>
547c478bd9Sstevel@tonic-gate #include <c2/audit.h>
557c478bd9Sstevel@tonic-gate #include <sys/tnf.h>
567c478bd9Sstevel@tonic-gate #include <sys/sobject.h>
577c478bd9Sstevel@tonic-gate #include <sys/cpupart.h>
587c478bd9Sstevel@tonic-gate #include <sys/pset.h>
597c478bd9Sstevel@tonic-gate #include <sys/door.h>
607c478bd9Sstevel@tonic-gate #include <sys/spl.h>
617c478bd9Sstevel@tonic-gate #include <sys/copyops.h>
627c478bd9Sstevel@tonic-gate #include <sys/rctl.h>
639acbbeafSnn35248 #include <sys/brand.h>
647c478bd9Sstevel@tonic-gate #include <sys/pool.h>
657c478bd9Sstevel@tonic-gate #include <sys/zone.h>
6645916cd2Sjpk #include <sys/tsol/label.h>
6745916cd2Sjpk #include <sys/tsol/tndb.h>
687c478bd9Sstevel@tonic-gate #include <sys/cpc_impl.h>
697c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
707c478bd9Sstevel@tonic-gate #include <sys/reboot.h>
717c478bd9Sstevel@tonic-gate #include <sys/kdi.h>
72d4204c85Sraf #include <sys/schedctl.h>
73c97ad5cdSakolb #include <sys/waitq.h>
74c97ad5cdSakolb #include <sys/cpucaps.h>
75d14d7d31Sis #include <sys/kiconv.h>
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate struct kmem_cache *thread_cache;	/* cache of free threads */
787c478bd9Sstevel@tonic-gate struct kmem_cache *lwp_cache;		/* cache of free lwps */
797c478bd9Sstevel@tonic-gate struct kmem_cache *turnstile_cache;	/* cache of free turnstiles */
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * allthreads is only for use by kmem_readers.  All kernel loops can use
837c478bd9Sstevel@tonic-gate  * the current thread as a start/end point.
847c478bd9Sstevel@tonic-gate  */
857c478bd9Sstevel@tonic-gate static kthread_t *allthreads = &t0;	/* circular list of all threads */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static kcondvar_t reaper_cv;		/* synchronization var */
887c478bd9Sstevel@tonic-gate kthread_t	*thread_deathrow;	/* circular list of reapable threads */
897c478bd9Sstevel@tonic-gate kthread_t	*lwp_deathrow;		/* circular list of reapable threads */
907c478bd9Sstevel@tonic-gate kmutex_t	reaplock;		/* protects lwp and thread deathrows */
917c478bd9Sstevel@tonic-gate int	thread_reapcnt = 0;		/* number of threads on deathrow */
927c478bd9Sstevel@tonic-gate int	lwp_reapcnt = 0;		/* number of lwps on deathrow */
937c478bd9Sstevel@tonic-gate int	reaplimit = 16;			/* delay reaping until reaplimit */
947c478bd9Sstevel@tonic-gate 
952850d85bSmv143129 thread_free_lock_t	*thread_free_lock;
962850d85bSmv143129 					/* protects tick thread from reaper */
972850d85bSmv143129 
987c478bd9Sstevel@tonic-gate extern int nthread;
997c478bd9Sstevel@tonic-gate 
10035a5a358SJonathan Adams /* System Scheduling classes. */
1017c478bd9Sstevel@tonic-gate id_t	syscid;				/* system scheduling class ID */
10235a5a358SJonathan Adams id_t	sysdccid = CLASS_UNUSED;	/* reset when SDC loads */
10335a5a358SJonathan Adams 
1047c478bd9Sstevel@tonic-gate void	*segkp_thread;			/* cookie for segkp pool */
1057c478bd9Sstevel@tonic-gate 
1067c478bd9Sstevel@tonic-gate int lwp_cache_sz = 32;
1077c478bd9Sstevel@tonic-gate int t_cache_sz = 8;
1087c478bd9Sstevel@tonic-gate static kt_did_t next_t_id = 1;
1097c478bd9Sstevel@tonic-gate 
1100b70c467Sakolb /* Default mode for thread binding to CPUs and processor sets */
1110b70c467Sakolb int default_binding_mode = TB_ALLHARD;
1120b70c467Sakolb 
1137c478bd9Sstevel@tonic-gate /*
1147c478bd9Sstevel@tonic-gate  * Min/Max stack sizes for stack size parameters
1157c478bd9Sstevel@tonic-gate  */
1167c478bd9Sstevel@tonic-gate #define	MAX_STKSIZE	(32 * DEFAULTSTKSZ)
1177c478bd9Sstevel@tonic-gate #define	MIN_STKSIZE	DEFAULTSTKSZ
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate /*
1207c478bd9Sstevel@tonic-gate  * default_stksize overrides lwp_default_stksize if it is set.
1217c478bd9Sstevel@tonic-gate  */
1227c478bd9Sstevel@tonic-gate int	default_stksize;
1237c478bd9Sstevel@tonic-gate int	lwp_default_stksize;
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate static zone_key_t zone_thread_key;
1267c478bd9Sstevel@tonic-gate 
127bff31d89SPhilippe Jung unsigned int kmem_stackinfo;		/* stackinfo feature on-off */
128bff31d89SPhilippe Jung kmem_stkinfo_t *kmem_stkinfo_log;	/* stackinfo circular log */
129bff31d89SPhilippe Jung static kmutex_t kmem_stkinfo_lock;	/* protects kmem_stkinfo_log */
130bff31d89SPhilippe Jung 
1317c478bd9Sstevel@tonic-gate /*
1327c478bd9Sstevel@tonic-gate  * forward declarations for internal thread specific data (tsd)
1337c478bd9Sstevel@tonic-gate  */
1347c478bd9Sstevel@tonic-gate static void *tsd_realloc(void *, size_t, size_t);
1357c478bd9Sstevel@tonic-gate 
1365ce42f36Spraks void thread_reaper(void);
1375ce42f36Spraks 
138bff31d89SPhilippe Jung /* forward declarations for stackinfo feature */
139bff31d89SPhilippe Jung static void stkinfo_begin(kthread_t *);
140bff31d89SPhilippe Jung static void stkinfo_end(kthread_t *);
141bff31d89SPhilippe Jung static size_t stkinfo_percent(caddr_t, caddr_t, caddr_t);
142bff31d89SPhilippe Jung 
1437c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1447c478bd9Sstevel@tonic-gate static int
1457c478bd9Sstevel@tonic-gate turnstile_constructor(void *buf, void *cdrarg, int kmflags)
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate 	bzero(buf, sizeof (turnstile_t));
1487c478bd9Sstevel@tonic-gate 	return (0);
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate /*ARGSUSED*/
1527c478bd9Sstevel@tonic-gate static void
1537c478bd9Sstevel@tonic-gate turnstile_destructor(void *buf, void *cdrarg)
1547c478bd9Sstevel@tonic-gate {
1557c478bd9Sstevel@tonic-gate 	turnstile_t *ts = buf;
1567c478bd9Sstevel@tonic-gate 
1577c478bd9Sstevel@tonic-gate 	ASSERT(ts->ts_free == NULL);
1587c478bd9Sstevel@tonic-gate 	ASSERT(ts->ts_waiters == 0);
1597c478bd9Sstevel@tonic-gate 	ASSERT(ts->ts_inheritor == NULL);
1607c478bd9Sstevel@tonic-gate 	ASSERT(ts->ts_sleepq[0].sq_first == NULL);
1617c478bd9Sstevel@tonic-gate 	ASSERT(ts->ts_sleepq[1].sq_first == NULL);
1627c478bd9Sstevel@tonic-gate }
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate void
1657c478bd9Sstevel@tonic-gate thread_init(void)
1667c478bd9Sstevel@tonic-gate {
1677c478bd9Sstevel@tonic-gate 	kthread_t *tp;
1687c478bd9Sstevel@tonic-gate 	extern char sys_name[];
1697c478bd9Sstevel@tonic-gate 	extern void idle();
1707c478bd9Sstevel@tonic-gate 	struct cpu *cpu = CPU;
1712850d85bSmv143129 	int i;
1722850d85bSmv143129 	kmutex_t *lp;
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	mutex_init(&reaplock, NULL, MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
1752850d85bSmv143129 	thread_free_lock =
1762850d85bSmv143129 	    kmem_alloc(sizeof (thread_free_lock_t) * THREAD_FREE_NUM, KM_SLEEP);
1772850d85bSmv143129 	for (i = 0; i < THREAD_FREE_NUM; i++) {
1782850d85bSmv143129 		lp = &thread_free_lock[i].tf_lock;
1792850d85bSmv143129 		mutex_init(lp, NULL, MUTEX_DEFAULT, NULL);
1802850d85bSmv143129 	}
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
1837c478bd9Sstevel@tonic-gate 	thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
1847c478bd9Sstevel@tonic-gate 	    PTR24_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	/*
1877c478bd9Sstevel@tonic-gate 	 * "struct _klwp" includes a "struct pcb", which includes a
188*7af88ac7SKuriakose Kuruvilla 	 * "struct fpu", which needs to be 64-byte aligned on amd64
189*7af88ac7SKuriakose Kuruvilla 	 * (and even on i386) for xsave/xrstor.
1907c478bd9Sstevel@tonic-gate 	 */
1917c478bd9Sstevel@tonic-gate 	lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
192*7af88ac7SKuriakose Kuruvilla 	    64, NULL, NULL, NULL, NULL, NULL, 0);
1937c478bd9Sstevel@tonic-gate #else
1947c478bd9Sstevel@tonic-gate 	/*
1957c478bd9Sstevel@tonic-gate 	 * Allocate thread structures from static_arena.  This prevents
1967c478bd9Sstevel@tonic-gate 	 * issues where a thread tries to relocate its own thread
1977c478bd9Sstevel@tonic-gate 	 * structure and touches it after the mapping has been suspended.
1987c478bd9Sstevel@tonic-gate 	 */
1997c478bd9Sstevel@tonic-gate 	thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
2007c478bd9Sstevel@tonic-gate 	    PTR24_ALIGN, NULL, NULL, NULL, NULL, static_arena, 0);
2017c478bd9Sstevel@tonic-gate 
202a8599265Selowe 	lwp_stk_cache_init();
203a8599265Selowe 
2047c478bd9Sstevel@tonic-gate 	lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
2057c478bd9Sstevel@tonic-gate 	    0, NULL, NULL, NULL, NULL, NULL, 0);
2067c478bd9Sstevel@tonic-gate #endif
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	turnstile_cache = kmem_cache_create("turnstile_cache",
2097c478bd9Sstevel@tonic-gate 	    sizeof (turnstile_t), 0,
2107c478bd9Sstevel@tonic-gate 	    turnstile_constructor, turnstile_destructor, NULL, NULL, NULL, 0);
2117c478bd9Sstevel@tonic-gate 
21245916cd2Sjpk 	label_init();
2137c478bd9Sstevel@tonic-gate 	cred_init();
2147c478bd9Sstevel@tonic-gate 
215c97ad5cdSakolb 	/*
216c97ad5cdSakolb 	 * Initialize various resource management facilities.
217c97ad5cdSakolb 	 */
2187c478bd9Sstevel@tonic-gate 	rctl_init();
219c97ad5cdSakolb 	cpucaps_init();
220c97ad5cdSakolb 	/*
221c97ad5cdSakolb 	 * Zone_init() should be called before project_init() so that project ID
222c97ad5cdSakolb 	 * for the first project is initialized correctly.
223c97ad5cdSakolb 	 */
224c97ad5cdSakolb 	zone_init();
2257c478bd9Sstevel@tonic-gate 	project_init();
2269acbbeafSnn35248 	brand_init();
227d14d7d31Sis 	kiconv_init();
2287c478bd9Sstevel@tonic-gate 	task_init();
22945916cd2Sjpk 	tcache_init();
2307c478bd9Sstevel@tonic-gate 	pool_init();
2317c478bd9Sstevel@tonic-gate 
2327c478bd9Sstevel@tonic-gate 	curthread->t_ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	/*
2357c478bd9Sstevel@tonic-gate 	 * Originally, we had two parameters to set default stack
2367c478bd9Sstevel@tonic-gate 	 * size: one for lwp's (lwp_default_stksize), and one for
2377c478bd9Sstevel@tonic-gate 	 * kernel-only threads (DEFAULTSTKSZ, a.k.a. _defaultstksz).
2387c478bd9Sstevel@tonic-gate 	 * Now we have a third parameter that overrides both if it is
2397c478bd9Sstevel@tonic-gate 	 * set to a legal stack size, called default_stksize.
2407c478bd9Sstevel@tonic-gate 	 */
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 	if (default_stksize == 0) {
2437c478bd9Sstevel@tonic-gate 		default_stksize = DEFAULTSTKSZ;
2447c478bd9Sstevel@tonic-gate 	} else if (default_stksize % PAGESIZE != 0 ||
2457c478bd9Sstevel@tonic-gate 	    default_stksize > MAX_STKSIZE ||
2467c478bd9Sstevel@tonic-gate 	    default_stksize < MIN_STKSIZE) {
2477c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal stack size. Using %d",
2487c478bd9Sstevel@tonic-gate 		    (int)DEFAULTSTKSZ);
2497c478bd9Sstevel@tonic-gate 		default_stksize = DEFAULTSTKSZ;
2507c478bd9Sstevel@tonic-gate 	} else {
2517c478bd9Sstevel@tonic-gate 		lwp_default_stksize = default_stksize;
2527c478bd9Sstevel@tonic-gate 	}
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (lwp_default_stksize == 0) {
2557c478bd9Sstevel@tonic-gate 		lwp_default_stksize = default_stksize;
2567c478bd9Sstevel@tonic-gate 	} else if (lwp_default_stksize % PAGESIZE != 0 ||
2577c478bd9Sstevel@tonic-gate 	    lwp_default_stksize > MAX_STKSIZE ||
2587c478bd9Sstevel@tonic-gate 	    lwp_default_stksize < MIN_STKSIZE) {
2597c478bd9Sstevel@tonic-gate 		cmn_err(CE_WARN, "Illegal stack size. Using %d",
2607c478bd9Sstevel@tonic-gate 		    default_stksize);
2617c478bd9Sstevel@tonic-gate 		lwp_default_stksize = default_stksize;
2627c478bd9Sstevel@tonic-gate 	}
2637c478bd9Sstevel@tonic-gate 
2647c478bd9Sstevel@tonic-gate 	segkp_lwp = segkp_cache_init(segkp, lwp_cache_sz,
2657c478bd9Sstevel@tonic-gate 	    lwp_default_stksize,
2667c478bd9Sstevel@tonic-gate 	    (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED));
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 	segkp_thread = segkp_cache_init(segkp, t_cache_sz,
2697c478bd9Sstevel@tonic-gate 	    default_stksize, KPD_HASREDZONE | KPD_LOCKED | KPD_NO_ANON);
2707c478bd9Sstevel@tonic-gate 
2717c478bd9Sstevel@tonic-gate 	(void) getcid(sys_name, &syscid);
2727c478bd9Sstevel@tonic-gate 	curthread->t_cid = syscid;	/* current thread is t0 */
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	/*
2757c478bd9Sstevel@tonic-gate 	 * Set up the first CPU's idle thread.
2767c478bd9Sstevel@tonic-gate 	 * It runs whenever the CPU has nothing worthwhile to do.
2777c478bd9Sstevel@tonic-gate 	 */
2787c478bd9Sstevel@tonic-gate 	tp = thread_create(NULL, 0, idle, NULL, 0, &p0, TS_STOPPED, -1);
2797c478bd9Sstevel@tonic-gate 	cpu->cpu_idle_thread = tp;
2807c478bd9Sstevel@tonic-gate 	tp->t_preempt = 1;
2817c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = cpu->cpu_disp;
2827c478bd9Sstevel@tonic-gate 	ASSERT(tp->t_disp_queue != NULL);
2837c478bd9Sstevel@tonic-gate 	tp->t_bound_cpu = cpu;
2847c478bd9Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	/*
2877c478bd9Sstevel@tonic-gate 	 * Registering a thread in the callback table is usually
2887c478bd9Sstevel@tonic-gate 	 * done in the initialization code of the thread. In this
2897c478bd9Sstevel@tonic-gate 	 * case, we do it right after thread creation to avoid
2907c478bd9Sstevel@tonic-gate 	 * blocking idle thread while registering itself. It also
2917c478bd9Sstevel@tonic-gate 	 * avoids the possibility of reregistration in case a CPU
2927c478bd9Sstevel@tonic-gate 	 * restarts its idle thread.
2937c478bd9Sstevel@tonic-gate 	 */
2947c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT_SAFE(tp, "idle");
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	/*
2975ce42f36Spraks 	 * Create the thread_reaper daemon. From this point on, exited
2985ce42f36Spraks 	 * threads will get reaped.
2995ce42f36Spraks 	 */
3005ce42f36Spraks 	(void) thread_create(NULL, 0, (void (*)())thread_reaper,
3015ce42f36Spraks 	    NULL, 0, &p0, TS_RUN, minclsyspri);
3025ce42f36Spraks 
3035ce42f36Spraks 	/*
3047c478bd9Sstevel@tonic-gate 	 * Finish initializing the kernel memory allocator now that
3057c478bd9Sstevel@tonic-gate 	 * thread_create() is available.
3067c478bd9Sstevel@tonic-gate 	 */
3077c478bd9Sstevel@tonic-gate 	kmem_thread_init();
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 	if (boothowto & RB_DEBUG)
3107c478bd9Sstevel@tonic-gate 		kdi_dvec_thravail();
3117c478bd9Sstevel@tonic-gate }
3127c478bd9Sstevel@tonic-gate 
3137c478bd9Sstevel@tonic-gate /*
3147c478bd9Sstevel@tonic-gate  * Create a thread.
3157c478bd9Sstevel@tonic-gate  *
3167c478bd9Sstevel@tonic-gate  * thread_create() blocks for memory if necessary.  It never fails.
3177c478bd9Sstevel@tonic-gate  *
3187c478bd9Sstevel@tonic-gate  * If stk is NULL, the thread is created at the base of the stack
3197c478bd9Sstevel@tonic-gate  * and cannot be swapped.
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate kthread_t *
3227c478bd9Sstevel@tonic-gate thread_create(
3237c478bd9Sstevel@tonic-gate 	caddr_t	stk,
3247c478bd9Sstevel@tonic-gate 	size_t	stksize,
3257c478bd9Sstevel@tonic-gate 	void	(*proc)(),
3267c478bd9Sstevel@tonic-gate 	void	*arg,
3277c478bd9Sstevel@tonic-gate 	size_t	len,
3287c478bd9Sstevel@tonic-gate 	proc_t	 *pp,
3297c478bd9Sstevel@tonic-gate 	int	state,
3307c478bd9Sstevel@tonic-gate 	pri_t	pri)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	kthread_t *t;
3337c478bd9Sstevel@tonic-gate 	extern struct classfuncs sys_classfuncs;
3347c478bd9Sstevel@tonic-gate 	turnstile_t *ts;
3357c478bd9Sstevel@tonic-gate 
3367c478bd9Sstevel@tonic-gate 	/*
3377c478bd9Sstevel@tonic-gate 	 * Every thread keeps a turnstile around in case it needs to block.
3387c478bd9Sstevel@tonic-gate 	 * The only reason the turnstile is not simply part of the thread
3397c478bd9Sstevel@tonic-gate 	 * structure is that we may have to break the association whenever
3407c478bd9Sstevel@tonic-gate 	 * more than one thread blocks on a given synchronization object.
3417c478bd9Sstevel@tonic-gate 	 * From a memory-management standpoint, turnstiles are like the
3427c478bd9Sstevel@tonic-gate 	 * "attached mblks" that hang off dblks in the streams allocator.
3437c478bd9Sstevel@tonic-gate 	 */
3447c478bd9Sstevel@tonic-gate 	ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
3457c478bd9Sstevel@tonic-gate 
3467c478bd9Sstevel@tonic-gate 	if (stk == NULL) {
3477c478bd9Sstevel@tonic-gate 		/*
3487c478bd9Sstevel@tonic-gate 		 * alloc both thread and stack in segkp chunk
3497c478bd9Sstevel@tonic-gate 		 */
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 		if (stksize < default_stksize)
3527c478bd9Sstevel@tonic-gate 			stksize = default_stksize;
3537c478bd9Sstevel@tonic-gate 
3547c478bd9Sstevel@tonic-gate 		if (stksize == default_stksize) {
3557c478bd9Sstevel@tonic-gate 			stk = (caddr_t)segkp_cache_get(segkp_thread);
3567c478bd9Sstevel@tonic-gate 		} else {
3577c478bd9Sstevel@tonic-gate 			stksize = roundup(stksize, PAGESIZE);
3587c478bd9Sstevel@tonic-gate 			stk = (caddr_t)segkp_get(segkp, stksize,
3597c478bd9Sstevel@tonic-gate 			    (KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED));
3607c478bd9Sstevel@tonic-gate 		}
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 		ASSERT(stk != NULL);
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		/*
3657c478bd9Sstevel@tonic-gate 		 * The machine-dependent mutex code may require that
3667c478bd9Sstevel@tonic-gate 		 * thread pointers (since they may be used for mutex owner
3677c478bd9Sstevel@tonic-gate 		 * fields) have certain alignment requirements.
3687c478bd9Sstevel@tonic-gate 		 * PTR24_ALIGN is the size of the alignment quanta.
3697c478bd9Sstevel@tonic-gate 		 * XXX - assumes stack grows toward low addresses.
3707c478bd9Sstevel@tonic-gate 		 */
3717c478bd9Sstevel@tonic-gate 		if (stksize <= sizeof (kthread_t) + PTR24_ALIGN)
3727c478bd9Sstevel@tonic-gate 			cmn_err(CE_PANIC, "thread_create: proposed stack size"
3737c478bd9Sstevel@tonic-gate 			    " too small to hold thread.");
3747c478bd9Sstevel@tonic-gate #ifdef STACK_GROWTH_DOWN
3757c478bd9Sstevel@tonic-gate 		stksize -= SA(sizeof (kthread_t) + PTR24_ALIGN - 1);
3767c478bd9Sstevel@tonic-gate 		stksize &= -PTR24_ALIGN;	/* make thread aligned */
3777c478bd9Sstevel@tonic-gate 		t = (kthread_t *)(stk + stksize);
3787c478bd9Sstevel@tonic-gate 		bzero(t, sizeof (kthread_t));
3797c478bd9Sstevel@tonic-gate 		if (audit_active)
3807c478bd9Sstevel@tonic-gate 			audit_thread_create(t);
3817c478bd9Sstevel@tonic-gate 		t->t_stk = stk + stksize;
3827c478bd9Sstevel@tonic-gate 		t->t_stkbase = stk;
3837c478bd9Sstevel@tonic-gate #else	/* stack grows to larger addresses */
3847c478bd9Sstevel@tonic-gate 		stksize -= SA(sizeof (kthread_t));
3857c478bd9Sstevel@tonic-gate 		t = (kthread_t *)(stk);
3867c478bd9Sstevel@tonic-gate 		bzero(t, sizeof (kthread_t));
3877c478bd9Sstevel@tonic-gate 		t->t_stk = stk + sizeof (kthread_t);
3887c478bd9Sstevel@tonic-gate 		t->t_stkbase = stk + stksize + sizeof (kthread_t);
3897c478bd9Sstevel@tonic-gate #endif	/* STACK_GROWTH_DOWN */
3907c478bd9Sstevel@tonic-gate 		t->t_flag |= T_TALLOCSTK;
3917c478bd9Sstevel@tonic-gate 		t->t_swap = stk;
3927c478bd9Sstevel@tonic-gate 	} else {
3937c478bd9Sstevel@tonic-gate 		t = kmem_cache_alloc(thread_cache, KM_SLEEP);
3947c478bd9Sstevel@tonic-gate 		bzero(t, sizeof (kthread_t));
3957c478bd9Sstevel@tonic-gate 		ASSERT(((uintptr_t)t & (PTR24_ALIGN - 1)) == 0);
3967c478bd9Sstevel@tonic-gate 		if (audit_active)
3977c478bd9Sstevel@tonic-gate 			audit_thread_create(t);
3987c478bd9Sstevel@tonic-gate 		/*
3997c478bd9Sstevel@tonic-gate 		 * Initialize t_stk to the kernel stack pointer to use
4007c478bd9Sstevel@tonic-gate 		 * upon entry to the kernel
4017c478bd9Sstevel@tonic-gate 		 */
4027c478bd9Sstevel@tonic-gate #ifdef STACK_GROWTH_DOWN
4037c478bd9Sstevel@tonic-gate 		t->t_stk = stk + stksize;
4047c478bd9Sstevel@tonic-gate 		t->t_stkbase = stk;
4057c478bd9Sstevel@tonic-gate #else
4067c478bd9Sstevel@tonic-gate 		t->t_stk = stk;			/* 3b2-like */
4077c478bd9Sstevel@tonic-gate 		t->t_stkbase = stk + stksize;
4087c478bd9Sstevel@tonic-gate #endif /* STACK_GROWTH_DOWN */
4097c478bd9Sstevel@tonic-gate 	}
4107c478bd9Sstevel@tonic-gate 
411bff31d89SPhilippe Jung 	if (kmem_stackinfo != 0) {
412bff31d89SPhilippe Jung 		stkinfo_begin(t);
413bff31d89SPhilippe Jung 	}
414bff31d89SPhilippe Jung 
4157c478bd9Sstevel@tonic-gate 	t->t_ts = ts;
4167c478bd9Sstevel@tonic-gate 
4177c478bd9Sstevel@tonic-gate 	/*
4187c478bd9Sstevel@tonic-gate 	 * p_cred could be NULL if it thread_create is called before cred_init
4197c478bd9Sstevel@tonic-gate 	 * is called in main.
4207c478bd9Sstevel@tonic-gate 	 */
4217c478bd9Sstevel@tonic-gate 	mutex_enter(&pp->p_crlock);
4227c478bd9Sstevel@tonic-gate 	if (pp->p_cred)
4237c478bd9Sstevel@tonic-gate 		crhold(t->t_cred = pp->p_cred);
4247c478bd9Sstevel@tonic-gate 	mutex_exit(&pp->p_crlock);
4257c478bd9Sstevel@tonic-gate 	t->t_start = gethrestime_sec();
4267c478bd9Sstevel@tonic-gate 	t->t_startpc = proc;
4277c478bd9Sstevel@tonic-gate 	t->t_procp = pp;
4287c478bd9Sstevel@tonic-gate 	t->t_clfuncs = &sys_classfuncs.thread;
4297c478bd9Sstevel@tonic-gate 	t->t_cid = syscid;
4307c478bd9Sstevel@tonic-gate 	t->t_pri = pri;
431d3d50737SRafael Vanoni 	t->t_stime = ddi_get_lbolt();
4327c478bd9Sstevel@tonic-gate 	t->t_schedflag = TS_LOAD | TS_DONT_SWAP;
4337c478bd9Sstevel@tonic-gate 	t->t_bind_cpu = PBIND_NONE;
4340b70c467Sakolb 	t->t_bindflag = (uchar_t)default_binding_mode;
4357c478bd9Sstevel@tonic-gate 	t->t_bind_pset = PS_NONE;
4367c478bd9Sstevel@tonic-gate 	t->t_plockp = &pp->p_lock;
4377c478bd9Sstevel@tonic-gate 	t->t_copyops = NULL;
4387c478bd9Sstevel@tonic-gate 	t->t_taskq = NULL;
4397c478bd9Sstevel@tonic-gate 	t->t_anttime = 0;
4407c478bd9Sstevel@tonic-gate 	t->t_hatdepth = 0;
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	t->t_dtrace_vtime = 1;	/* assure vtimestamp is always non-zero */
4437c478bd9Sstevel@tonic-gate 
4447c478bd9Sstevel@tonic-gate 	CPU_STATS_ADDQ(CPU, sys, nthreads, 1);
4457c478bd9Sstevel@tonic-gate #ifndef NPROBE
4467c478bd9Sstevel@tonic-gate 	/* Kernel probe */
4477c478bd9Sstevel@tonic-gate 	tnf_thread_create(t);
4487c478bd9Sstevel@tonic-gate #endif /* NPROBE */
4497c478bd9Sstevel@tonic-gate 	LOCK_INIT_CLEAR(&t->t_lock);
4507c478bd9Sstevel@tonic-gate 
4517c478bd9Sstevel@tonic-gate 	/*
4527c478bd9Sstevel@tonic-gate 	 * Callers who give us a NULL proc must do their own
4537c478bd9Sstevel@tonic-gate 	 * stack initialization.  e.g. lwp_create()
4547c478bd9Sstevel@tonic-gate 	 */
4557c478bd9Sstevel@tonic-gate 	if (proc != NULL) {
4567c478bd9Sstevel@tonic-gate 		t->t_stk = thread_stk_init(t->t_stk);
4577c478bd9Sstevel@tonic-gate 		thread_load(t, proc, arg, len);
4587c478bd9Sstevel@tonic-gate 	}
4597c478bd9Sstevel@tonic-gate 
4607c478bd9Sstevel@tonic-gate 	/*
4617c478bd9Sstevel@tonic-gate 	 * Put a hold on project0. If this thread is actually in a
4627c478bd9Sstevel@tonic-gate 	 * different project, then t_proj will be changed later in
4637c478bd9Sstevel@tonic-gate 	 * lwp_create().  All kernel-only threads must be in project 0.
4647c478bd9Sstevel@tonic-gate 	 */
4657c478bd9Sstevel@tonic-gate 	t->t_proj = project_hold(proj0p);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	lgrp_affinity_init(&t->t_lgrp_affinity);
4687c478bd9Sstevel@tonic-gate 
4697c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
4707c478bd9Sstevel@tonic-gate 	nthread++;
4717c478bd9Sstevel@tonic-gate 	t->t_did = next_t_id++;
4727c478bd9Sstevel@tonic-gate 	t->t_prev = curthread->t_prev;
4737c478bd9Sstevel@tonic-gate 	t->t_next = curthread;
4747c478bd9Sstevel@tonic-gate 
4757c478bd9Sstevel@tonic-gate 	/*
4767c478bd9Sstevel@tonic-gate 	 * Add the thread to the list of all threads, and initialize
4777c478bd9Sstevel@tonic-gate 	 * its t_cpu pointer.  We need to block preemption since
4787c478bd9Sstevel@tonic-gate 	 * cpu_offline walks the thread list looking for threads
4797c478bd9Sstevel@tonic-gate 	 * with t_cpu pointing to the CPU being offlined.  We want
4807c478bd9Sstevel@tonic-gate 	 * to make sure that the list is consistent and that if t_cpu
4817c478bd9Sstevel@tonic-gate 	 * is set, the thread is on the list.
4827c478bd9Sstevel@tonic-gate 	 */
4837c478bd9Sstevel@tonic-gate 	kpreempt_disable();
4847c478bd9Sstevel@tonic-gate 	curthread->t_prev->t_next = t;
4857c478bd9Sstevel@tonic-gate 	curthread->t_prev = t;
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	/*
4887c478bd9Sstevel@tonic-gate 	 * Threads should never have a NULL t_cpu pointer so assign it
4897c478bd9Sstevel@tonic-gate 	 * here.  If the thread is being created with state TS_RUN a
4907c478bd9Sstevel@tonic-gate 	 * better CPU may be chosen when it is placed on the run queue.
4917c478bd9Sstevel@tonic-gate 	 *
4927c478bd9Sstevel@tonic-gate 	 * We need to keep kernel preemption disabled when setting all
4937c478bd9Sstevel@tonic-gate 	 * three fields to keep them in sync.  Also, always create in
4947c478bd9Sstevel@tonic-gate 	 * the default partition since that's where kernel threads go
4957c478bd9Sstevel@tonic-gate 	 * (if this isn't a kernel thread, t_cpupart will be changed
4967c478bd9Sstevel@tonic-gate 	 * in lwp_create before setting the thread runnable).
4977c478bd9Sstevel@tonic-gate 	 */
4987c478bd9Sstevel@tonic-gate 	t->t_cpupart = &cp_default;
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 	/*
5017c478bd9Sstevel@tonic-gate 	 * For now, affiliate this thread with the root lgroup.
5027c478bd9Sstevel@tonic-gate 	 * Since the kernel does not (presently) allocate its memory
5037c478bd9Sstevel@tonic-gate 	 * in a locality aware fashion, the root is an appropriate home.
5047c478bd9Sstevel@tonic-gate 	 * If this thread is later associated with an lwp, it will have
5057c478bd9Sstevel@tonic-gate 	 * it's lgroup re-assigned at that time.
5067c478bd9Sstevel@tonic-gate 	 */
5077c478bd9Sstevel@tonic-gate 	lgrp_move_thread(t, &cp_default.cp_lgrploads[LGRP_ROOTID], 1);
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	/*
5107c478bd9Sstevel@tonic-gate 	 * Inherit the current cpu.  If this cpu isn't part of the chosen
5117c478bd9Sstevel@tonic-gate 	 * lgroup, a new cpu will be chosen by cpu_choose when the thread
5127c478bd9Sstevel@tonic-gate 	 * is ready to run.
5137c478bd9Sstevel@tonic-gate 	 */
5147c478bd9Sstevel@tonic-gate 	if (CPU->cpu_part == &cp_default)
5157c478bd9Sstevel@tonic-gate 		t->t_cpu = CPU;
5167c478bd9Sstevel@tonic-gate 	else
5177c478bd9Sstevel@tonic-gate 		t->t_cpu = disp_lowpri_cpu(cp_default.cp_cpulist, t->t_lpl,
5187c478bd9Sstevel@tonic-gate 		    t->t_pri, NULL);
5197c478bd9Sstevel@tonic-gate 
5207c478bd9Sstevel@tonic-gate 	t->t_disp_queue = t->t_cpu->cpu_disp;
5217c478bd9Sstevel@tonic-gate 	kpreempt_enable();
5227c478bd9Sstevel@tonic-gate 
5237c478bd9Sstevel@tonic-gate 	/*
5247c478bd9Sstevel@tonic-gate 	 * Initialize thread state and the dispatcher lock pointer.
5257c478bd9Sstevel@tonic-gate 	 * Need to hold onto pidlock to block allthreads walkers until
5267c478bd9Sstevel@tonic-gate 	 * the state is set.
5277c478bd9Sstevel@tonic-gate 	 */
5287c478bd9Sstevel@tonic-gate 	switch (state) {
5297c478bd9Sstevel@tonic-gate 	case TS_RUN:
5307c478bd9Sstevel@tonic-gate 		curthread->t_oldspl = splhigh();	/* get dispatcher spl */
5317c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(t, TS_STOPPED, &transition_lock);
5327c478bd9Sstevel@tonic-gate 		CL_SETRUN(t);
5337c478bd9Sstevel@tonic-gate 		thread_unlock(t);
5347c478bd9Sstevel@tonic-gate 		break;
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 	case TS_ONPROC:
5377c478bd9Sstevel@tonic-gate 		THREAD_ONPROC(t, t->t_cpu);
5387c478bd9Sstevel@tonic-gate 		break;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	case TS_FREE:
5417c478bd9Sstevel@tonic-gate 		/*
5427c478bd9Sstevel@tonic-gate 		 * Free state will be used for intr threads.
5437c478bd9Sstevel@tonic-gate 		 * The interrupt routine must set the thread dispatcher
5447c478bd9Sstevel@tonic-gate 		 * lock pointer (t_lockp) if starting on a CPU
5457c478bd9Sstevel@tonic-gate 		 * other than the current one.
5467c478bd9Sstevel@tonic-gate 		 */
5477c478bd9Sstevel@tonic-gate 		THREAD_FREEINTR(t, CPU);
5487c478bd9Sstevel@tonic-gate 		break;
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate 	case TS_STOPPED:
5517c478bd9Sstevel@tonic-gate 		THREAD_SET_STATE(t, TS_STOPPED, &stop_lock);
5527c478bd9Sstevel@tonic-gate 		break;
5537c478bd9Sstevel@tonic-gate 
5547c478bd9Sstevel@tonic-gate 	default:			/* TS_SLEEP, TS_ZOMB or TS_TRANS */
5557c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "thread_create: invalid state %d", state);
5567c478bd9Sstevel@tonic-gate 	}
5577c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
5587c478bd9Sstevel@tonic-gate 	return (t);
5597c478bd9Sstevel@tonic-gate }
5607c478bd9Sstevel@tonic-gate 
5617c478bd9Sstevel@tonic-gate /*
5627c478bd9Sstevel@tonic-gate  * Move thread to project0 and take care of project reference counters.
5637c478bd9Sstevel@tonic-gate  */
5647c478bd9Sstevel@tonic-gate void
5657c478bd9Sstevel@tonic-gate thread_rele(kthread_t *t)
5667c478bd9Sstevel@tonic-gate {
5677c478bd9Sstevel@tonic-gate 	kproject_t *kpj;
5687c478bd9Sstevel@tonic-gate 
5697c478bd9Sstevel@tonic-gate 	thread_lock(t);
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread || t->t_state == TS_FREE || t->t_procp == &p0);
5727c478bd9Sstevel@tonic-gate 	kpj = ttoproj(t);
5737c478bd9Sstevel@tonic-gate 	t->t_proj = proj0p;
5747c478bd9Sstevel@tonic-gate 
5757c478bd9Sstevel@tonic-gate 	thread_unlock(t);
5767c478bd9Sstevel@tonic-gate 
5777c478bd9Sstevel@tonic-gate 	if (kpj != proj0p) {
5787c478bd9Sstevel@tonic-gate 		project_rele(kpj);
5797c478bd9Sstevel@tonic-gate 		(void) project_hold(proj0p);
5807c478bd9Sstevel@tonic-gate 	}
5817c478bd9Sstevel@tonic-gate }
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate void
5846a8288c7Scarlsonj thread_exit(void)
5857c478bd9Sstevel@tonic-gate {
5867c478bd9Sstevel@tonic-gate 	kthread_t *t = curthread;
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if ((t->t_proc_flag & TP_ZTHREAD) != 0)
5897c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "thread_exit: zthread_exit() not called");
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	tsd_exit();		/* Clean up this thread's TSD */
5927c478bd9Sstevel@tonic-gate 
5937c478bd9Sstevel@tonic-gate 	kcpc_passivate();	/* clean up performance counter state */
5947c478bd9Sstevel@tonic-gate 
5957c478bd9Sstevel@tonic-gate 	/*
5967c478bd9Sstevel@tonic-gate 	 * No kernel thread should have called poll() without arranging
5977c478bd9Sstevel@tonic-gate 	 * calling pollcleanup() here.
5987c478bd9Sstevel@tonic-gate 	 */
5997c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pollstate == NULL);
6007c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedctl == NULL);
6017c478bd9Sstevel@tonic-gate 	if (t->t_door)
6027c478bd9Sstevel@tonic-gate 		door_slam();	/* in case thread did an upcall */
6037c478bd9Sstevel@tonic-gate 
6047c478bd9Sstevel@tonic-gate #ifndef NPROBE
6057c478bd9Sstevel@tonic-gate 	/* Kernel probe */
6067c478bd9Sstevel@tonic-gate 	if (t->t_tnf_tpdp)
6077c478bd9Sstevel@tonic-gate 		tnf_thread_exit();
6087c478bd9Sstevel@tonic-gate #endif /* NPROBE */
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 	thread_rele(t);
6117c478bd9Sstevel@tonic-gate 	t->t_preempt++;
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 	/*
6147c478bd9Sstevel@tonic-gate 	 * remove thread from the all threads list so that
6157c478bd9Sstevel@tonic-gate 	 * death-row can use the same pointers.
6167c478bd9Sstevel@tonic-gate 	 */
6177c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
6187c478bd9Sstevel@tonic-gate 	t->t_next->t_prev = t->t_prev;
6197c478bd9Sstevel@tonic-gate 	t->t_prev->t_next = t->t_next;
6207c478bd9Sstevel@tonic-gate 	ASSERT(allthreads != t);	/* t0 never exits */
6217c478bd9Sstevel@tonic-gate 	cv_broadcast(&t->t_joincv);	/* wake up anyone in thread_join */
6227c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
6237c478bd9Sstevel@tonic-gate 
6247c478bd9Sstevel@tonic-gate 	if (t->t_ctx != NULL)
6257c478bd9Sstevel@tonic-gate 		exitctx(t);
6260baeff3dSrab 	if (t->t_procp->p_pctx != NULL)
6270baeff3dSrab 		exitpctx(t->t_procp);
6287c478bd9Sstevel@tonic-gate 
629bff31d89SPhilippe Jung 	if (kmem_stackinfo != 0) {
630bff31d89SPhilippe Jung 		stkinfo_end(t);
631bff31d89SPhilippe Jung 	}
632bff31d89SPhilippe Jung 
6337c478bd9Sstevel@tonic-gate 	t->t_state = TS_ZOMB;	/* set zombie thread */
6347c478bd9Sstevel@tonic-gate 
6357c478bd9Sstevel@tonic-gate 	swtch_from_zombie();	/* give up the CPU */
6367c478bd9Sstevel@tonic-gate 	/* NOTREACHED */
6377c478bd9Sstevel@tonic-gate }
6387c478bd9Sstevel@tonic-gate 
6397c478bd9Sstevel@tonic-gate /*
6407c478bd9Sstevel@tonic-gate  * Check to see if the specified thread is active (defined as being on
6417c478bd9Sstevel@tonic-gate  * the thread list).  This is certainly a slow way to do this; if there's
6427c478bd9Sstevel@tonic-gate  * ever a reason to speed it up, we could maintain a hash table of active
6437c478bd9Sstevel@tonic-gate  * threads indexed by their t_did.
6447c478bd9Sstevel@tonic-gate  */
6457c478bd9Sstevel@tonic-gate static kthread_t *
6467c478bd9Sstevel@tonic-gate did_to_thread(kt_did_t tid)
6477c478bd9Sstevel@tonic-gate {
6487c478bd9Sstevel@tonic-gate 	kthread_t *t;
6497c478bd9Sstevel@tonic-gate 
6507c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&pidlock));
6517c478bd9Sstevel@tonic-gate 	for (t = curthread->t_next; t != curthread; t = t->t_next) {
6527c478bd9Sstevel@tonic-gate 		if (t->t_did == tid)
6537c478bd9Sstevel@tonic-gate 			break;
6547c478bd9Sstevel@tonic-gate 	}
6557c478bd9Sstevel@tonic-gate 	if (t->t_did == tid)
6567c478bd9Sstevel@tonic-gate 		return (t);
6577c478bd9Sstevel@tonic-gate 	else
6587c478bd9Sstevel@tonic-gate 		return (NULL);
6597c478bd9Sstevel@tonic-gate }
6607c478bd9Sstevel@tonic-gate 
6617c478bd9Sstevel@tonic-gate /*
6627c478bd9Sstevel@tonic-gate  * Wait for specified thread to exit.  Returns immediately if the thread
6637c478bd9Sstevel@tonic-gate  * could not be found, meaning that it has either already exited or never
6647c478bd9Sstevel@tonic-gate  * existed.
6657c478bd9Sstevel@tonic-gate  */
6667c478bd9Sstevel@tonic-gate void
6677c478bd9Sstevel@tonic-gate thread_join(kt_did_t tid)
6687c478bd9Sstevel@tonic-gate {
6697c478bd9Sstevel@tonic-gate 	kthread_t *t;
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	ASSERT(tid != curthread->t_did);
6727c478bd9Sstevel@tonic-gate 	ASSERT(tid != t0.t_did);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	mutex_enter(&pidlock);
6757c478bd9Sstevel@tonic-gate 	/*
6767c478bd9Sstevel@tonic-gate 	 * Make sure we check that the thread is on the thread list
6777c478bd9Sstevel@tonic-gate 	 * before blocking on it; otherwise we could end up blocking on
6787c478bd9Sstevel@tonic-gate 	 * a cv that's already been freed.  In other words, don't cache
6797c478bd9Sstevel@tonic-gate 	 * the thread pointer across calls to cv_wait.
6807c478bd9Sstevel@tonic-gate 	 *
6817c478bd9Sstevel@tonic-gate 	 * The choice of loop invariant means that whenever a thread
6827c478bd9Sstevel@tonic-gate 	 * is taken off the allthreads list, a cv_broadcast must be
6837c478bd9Sstevel@tonic-gate 	 * performed on that thread's t_joincv to wake up any waiters.
6847c478bd9Sstevel@tonic-gate 	 * The broadcast doesn't have to happen right away, but it
6857c478bd9Sstevel@tonic-gate 	 * shouldn't be postponed indefinitely (e.g., by doing it in
6867c478bd9Sstevel@tonic-gate 	 * thread_free which may only be executed when the deathrow
6877c478bd9Sstevel@tonic-gate 	 * queue is processed.
6887c478bd9Sstevel@tonic-gate 	 */
6897c478bd9Sstevel@tonic-gate 	while (t = did_to_thread(tid))
6907c478bd9Sstevel@tonic-gate 		cv_wait(&t->t_joincv, &pidlock);
6917c478bd9Sstevel@tonic-gate 	mutex_exit(&pidlock);
6927c478bd9Sstevel@tonic-gate }
6937c478bd9Sstevel@tonic-gate 
6947c478bd9Sstevel@tonic-gate void
6952850d85bSmv143129 thread_free_prevent(kthread_t *t)
6962850d85bSmv143129 {
6972850d85bSmv143129 	kmutex_t *lp;
6982850d85bSmv143129 
6992850d85bSmv143129 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
7002850d85bSmv143129 	mutex_enter(lp);
7012850d85bSmv143129 }
7022850d85bSmv143129 
7032850d85bSmv143129 void
7042850d85bSmv143129 thread_free_allow(kthread_t *t)
7052850d85bSmv143129 {
7062850d85bSmv143129 	kmutex_t *lp;
7072850d85bSmv143129 
7082850d85bSmv143129 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
7092850d85bSmv143129 	mutex_exit(lp);
7102850d85bSmv143129 }
7112850d85bSmv143129 
7122850d85bSmv143129 static void
7132850d85bSmv143129 thread_free_barrier(kthread_t *t)
7142850d85bSmv143129 {
7152850d85bSmv143129 	kmutex_t *lp;
7162850d85bSmv143129 
7172850d85bSmv143129 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
7182850d85bSmv143129 	mutex_enter(lp);
7192850d85bSmv143129 	mutex_exit(lp);
7202850d85bSmv143129 }
7212850d85bSmv143129 
7222850d85bSmv143129 void
7237c478bd9Sstevel@tonic-gate thread_free(kthread_t *t)
7247c478bd9Sstevel@tonic-gate {
725d32efdadSJonathan Adams 	boolean_t allocstk = (t->t_flag & T_TALLOCSTK);
726d32efdadSJonathan Adams 	klwp_t *lwp = t->t_lwp;
727d32efdadSJonathan Adams 	caddr_t swap = t->t_swap;
728d32efdadSJonathan Adams 
7297c478bd9Sstevel@tonic-gate 	ASSERT(t != &t0 && t->t_state == TS_FREE);
7307c478bd9Sstevel@tonic-gate 	ASSERT(t->t_door == NULL);
7317c478bd9Sstevel@tonic-gate 	ASSERT(t->t_schedctl == NULL);
7327c478bd9Sstevel@tonic-gate 	ASSERT(t->t_pollstate == NULL);
7337c478bd9Sstevel@tonic-gate 
7347c478bd9Sstevel@tonic-gate 	t->t_pri = 0;
7357c478bd9Sstevel@tonic-gate 	t->t_pc = 0;
7367c478bd9Sstevel@tonic-gate 	t->t_sp = 0;
7377c478bd9Sstevel@tonic-gate 	t->t_wchan0 = NULL;
7387c478bd9Sstevel@tonic-gate 	t->t_wchan = NULL;
7397c478bd9Sstevel@tonic-gate 	if (t->t_cred != NULL) {
7407c478bd9Sstevel@tonic-gate 		crfree(t->t_cred);
7417c478bd9Sstevel@tonic-gate 		t->t_cred = 0;
7427c478bd9Sstevel@tonic-gate 	}
7437c478bd9Sstevel@tonic-gate 	if (t->t_pdmsg) {
7447c478bd9Sstevel@tonic-gate 		kmem_free(t->t_pdmsg, strlen(t->t_pdmsg) + 1);
7457c478bd9Sstevel@tonic-gate 		t->t_pdmsg = NULL;
7467c478bd9Sstevel@tonic-gate 	}
7477c478bd9Sstevel@tonic-gate 	if (audit_active)
7487c478bd9Sstevel@tonic-gate 		audit_thread_free(t);
7497c478bd9Sstevel@tonic-gate #ifndef NPROBE
7507c478bd9Sstevel@tonic-gate 	if (t->t_tnf_tpdp)
7517c478bd9Sstevel@tonic-gate 		tnf_thread_free(t);
7527c478bd9Sstevel@tonic-gate #endif /* NPROBE */
7537c478bd9Sstevel@tonic-gate 	if (t->t_cldata) {
7547c478bd9Sstevel@tonic-gate 		CL_EXITCLASS(t->t_cid, (caddr_t *)t->t_cldata);
7557c478bd9Sstevel@tonic-gate 	}
7567c478bd9Sstevel@tonic-gate 	if (t->t_rprof != NULL) {
7577c478bd9Sstevel@tonic-gate 		kmem_free(t->t_rprof, sizeof (*t->t_rprof));
7587c478bd9Sstevel@tonic-gate 		t->t_rprof = NULL;
7597c478bd9Sstevel@tonic-gate 	}
7607c478bd9Sstevel@tonic-gate 	t->t_lockp = NULL;	/* nothing should try to lock this thread now */
761d32efdadSJonathan Adams 	if (lwp)
762d32efdadSJonathan Adams 		lwp_freeregs(lwp, 0);
7637c478bd9Sstevel@tonic-gate 	if (t->t_ctx)
7647c478bd9Sstevel@tonic-gate 		freectx(t, 0);
7657c478bd9Sstevel@tonic-gate 	t->t_stk = NULL;
766d32efdadSJonathan Adams 	if (lwp)
767d32efdadSJonathan Adams 		lwp_stk_fini(lwp);
7687c478bd9Sstevel@tonic-gate 	lock_clear(&t->t_lock);
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	if (t->t_ts->ts_waiters > 0)
7717c478bd9Sstevel@tonic-gate 		panic("thread_free: turnstile still active");
7727c478bd9Sstevel@tonic-gate 
7737c478bd9Sstevel@tonic-gate 	kmem_cache_free(turnstile_cache, t->t_ts);
7747c478bd9Sstevel@tonic-gate 
7757c478bd9Sstevel@tonic-gate 	free_afd(&t->t_activefd);
7767c478bd9Sstevel@tonic-gate 
7777c478bd9Sstevel@tonic-gate 	/*
7782850d85bSmv143129 	 * Barrier for the tick accounting code.  The tick accounting code
7792850d85bSmv143129 	 * holds this lock to keep the thread from going away while it's
7802850d85bSmv143129 	 * looking at it.
7817c478bd9Sstevel@tonic-gate 	 */
7822850d85bSmv143129 	thread_free_barrier(t);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate 	ASSERT(ttoproj(t) == proj0p);
7857c478bd9Sstevel@tonic-gate 	project_rele(ttoproj(t));
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	lgrp_affinity_free(&t->t_lgrp_affinity);
7887c478bd9Sstevel@tonic-gate 
789d32efdadSJonathan Adams 	mutex_enter(&pidlock);
790d32efdadSJonathan Adams 	nthread--;
791d32efdadSJonathan Adams 	mutex_exit(&pidlock);
792d32efdadSJonathan Adams 
7937c478bd9Sstevel@tonic-gate 	/*
794d32efdadSJonathan Adams 	 * Free thread, lwp and stack.  This needs to be done carefully, since
795d32efdadSJonathan Adams 	 * if T_TALLOCSTK is set, the thread is part of the stack.
7967c478bd9Sstevel@tonic-gate 	 */
7977c478bd9Sstevel@tonic-gate 	t->t_lwp = NULL;
798d32efdadSJonathan Adams 	t->t_swap = NULL;
799d32efdadSJonathan Adams 
800d32efdadSJonathan Adams 	if (swap) {
801d32efdadSJonathan Adams 		segkp_release(segkp, swap);
8027c478bd9Sstevel@tonic-gate 	}
803d32efdadSJonathan Adams 	if (lwp) {
804d32efdadSJonathan Adams 		kmem_cache_free(lwp_cache, lwp);
805d32efdadSJonathan Adams 	}
806d32efdadSJonathan Adams 	if (!allocstk) {
8077c478bd9Sstevel@tonic-gate 		kmem_cache_free(thread_cache, t);
8087c478bd9Sstevel@tonic-gate 	}
8097c478bd9Sstevel@tonic-gate }
8107c478bd9Sstevel@tonic-gate 
8117c478bd9Sstevel@tonic-gate /*
8127c478bd9Sstevel@tonic-gate  * Removes threads associated with the given zone from a deathrow queue.
8137c478bd9Sstevel@tonic-gate  * tp is a pointer to the head of the deathrow queue, and countp is a
8147c478bd9Sstevel@tonic-gate  * pointer to the current deathrow count.  Returns a linked list of
8157c478bd9Sstevel@tonic-gate  * threads removed from the list.
8167c478bd9Sstevel@tonic-gate  */
8177c478bd9Sstevel@tonic-gate static kthread_t *
8187c478bd9Sstevel@tonic-gate thread_zone_cleanup(kthread_t **tp, int *countp, zoneid_t zoneid)
8197c478bd9Sstevel@tonic-gate {
8207c478bd9Sstevel@tonic-gate 	kthread_t *tmp, *list = NULL;
8217c478bd9Sstevel@tonic-gate 	cred_t *cr;
8227c478bd9Sstevel@tonic-gate 
8237c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&reaplock));
8247c478bd9Sstevel@tonic-gate 	while (*tp != NULL) {
8257c478bd9Sstevel@tonic-gate 		if ((cr = (*tp)->t_cred) != NULL && crgetzoneid(cr) == zoneid) {
8267c478bd9Sstevel@tonic-gate 			tmp = *tp;
8277c478bd9Sstevel@tonic-gate 			*tp = tmp->t_forw;
8287c478bd9Sstevel@tonic-gate 			tmp->t_forw = list;
8297c478bd9Sstevel@tonic-gate 			list = tmp;
8307c478bd9Sstevel@tonic-gate 			(*countp)--;
8317c478bd9Sstevel@tonic-gate 		} else {
8327c478bd9Sstevel@tonic-gate 			tp = &(*tp)->t_forw;
8337c478bd9Sstevel@tonic-gate 		}
8347c478bd9Sstevel@tonic-gate 	}
8357c478bd9Sstevel@tonic-gate 	return (list);
8367c478bd9Sstevel@tonic-gate }
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate static void
8397c478bd9Sstevel@tonic-gate thread_reap_list(kthread_t *t)
8407c478bd9Sstevel@tonic-gate {
8417c478bd9Sstevel@tonic-gate 	kthread_t *next;
8427c478bd9Sstevel@tonic-gate 
8437c478bd9Sstevel@tonic-gate 	while (t != NULL) {
8447c478bd9Sstevel@tonic-gate 		next = t->t_forw;
8457c478bd9Sstevel@tonic-gate 		thread_free(t);
8467c478bd9Sstevel@tonic-gate 		t = next;
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate /* ARGSUSED */
8517c478bd9Sstevel@tonic-gate static void
8527c478bd9Sstevel@tonic-gate thread_zone_destroy(zoneid_t zoneid, void *unused)
8537c478bd9Sstevel@tonic-gate {
8547c478bd9Sstevel@tonic-gate 	kthread_t *t, *l;
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate 	mutex_enter(&reaplock);
8577c478bd9Sstevel@tonic-gate 	/*
8587c478bd9Sstevel@tonic-gate 	 * Pull threads and lwps associated with zone off deathrow lists.
8597c478bd9Sstevel@tonic-gate 	 */
8607c478bd9Sstevel@tonic-gate 	t = thread_zone_cleanup(&thread_deathrow, &thread_reapcnt, zoneid);
8617c478bd9Sstevel@tonic-gate 	l = thread_zone_cleanup(&lwp_deathrow, &lwp_reapcnt, zoneid);
8627c478bd9Sstevel@tonic-gate 	mutex_exit(&reaplock);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	/*
865575a7426Spt157919 	 * Guard against race condition in mutex_owner_running:
866575a7426Spt157919 	 * 	thread=owner(mutex)
867575a7426Spt157919 	 * 	<interrupt>
868575a7426Spt157919 	 * 				thread exits mutex
869575a7426Spt157919 	 * 				thread exits
870575a7426Spt157919 	 * 				thread reaped
871575a7426Spt157919 	 * 				thread struct freed
872575a7426Spt157919 	 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
873575a7426Spt157919 	 * A cross call to all cpus will cause the interrupt handler
874575a7426Spt157919 	 * to reset the PC if it is in mutex_owner_running, refreshing
875575a7426Spt157919 	 * stale thread pointers.
876575a7426Spt157919 	 */
877575a7426Spt157919 	mutex_sync();   /* sync with mutex code */
878575a7426Spt157919 
879575a7426Spt157919 	/*
8807c478bd9Sstevel@tonic-gate 	 * Reap threads
8817c478bd9Sstevel@tonic-gate 	 */
8827c478bd9Sstevel@tonic-gate 	thread_reap_list(t);
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate 	/*
8857c478bd9Sstevel@tonic-gate 	 * Reap lwps
8867c478bd9Sstevel@tonic-gate 	 */
8877c478bd9Sstevel@tonic-gate 	thread_reap_list(l);
8887c478bd9Sstevel@tonic-gate }
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate /*
8917c478bd9Sstevel@tonic-gate  * cleanup zombie threads that are on deathrow.
8927c478bd9Sstevel@tonic-gate  */
8937c478bd9Sstevel@tonic-gate void
8947c478bd9Sstevel@tonic-gate thread_reaper()
8957c478bd9Sstevel@tonic-gate {
8967c478bd9Sstevel@tonic-gate 	kthread_t *t, *l;
8977c478bd9Sstevel@tonic-gate 	callb_cpr_t cprinfo;
8987c478bd9Sstevel@tonic-gate 
8997c478bd9Sstevel@tonic-gate 	/*
9007c478bd9Sstevel@tonic-gate 	 * Register callback to clean up threads when zone is destroyed.
9017c478bd9Sstevel@tonic-gate 	 */
9027c478bd9Sstevel@tonic-gate 	zone_key_create(&zone_thread_key, NULL, NULL, thread_zone_destroy);
9037c478bd9Sstevel@tonic-gate 
9047c478bd9Sstevel@tonic-gate 	CALLB_CPR_INIT(&cprinfo, &reaplock, callb_generic_cpr, "t_reaper");
9057c478bd9Sstevel@tonic-gate 	for (;;) {
9067c478bd9Sstevel@tonic-gate 		mutex_enter(&reaplock);
9077c478bd9Sstevel@tonic-gate 		while (thread_deathrow == NULL && lwp_deathrow == NULL) {
9087c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
9097c478bd9Sstevel@tonic-gate 			cv_wait(&reaper_cv, &reaplock);
9107c478bd9Sstevel@tonic-gate 			CALLB_CPR_SAFE_END(&cprinfo, &reaplock);
9117c478bd9Sstevel@tonic-gate 		}
912575a7426Spt157919 		/*
913575a7426Spt157919 		 * mutex_sync() needs to be called when reaping, but
914575a7426Spt157919 		 * not too often.  We limit reaping rate to once
915575a7426Spt157919 		 * per second.  Reaplimit is max rate at which threads can
916575a7426Spt157919 		 * be freed. Does not impact thread destruction/creation.
917575a7426Spt157919 		 */
9187c478bd9Sstevel@tonic-gate 		t = thread_deathrow;
9197c478bd9Sstevel@tonic-gate 		l = lwp_deathrow;
9207c478bd9Sstevel@tonic-gate 		thread_deathrow = NULL;
9217c478bd9Sstevel@tonic-gate 		lwp_deathrow = NULL;
9227c478bd9Sstevel@tonic-gate 		thread_reapcnt = 0;
9237c478bd9Sstevel@tonic-gate 		lwp_reapcnt = 0;
9247c478bd9Sstevel@tonic-gate 		mutex_exit(&reaplock);
9257c478bd9Sstevel@tonic-gate 
9267c478bd9Sstevel@tonic-gate 		/*
927575a7426Spt157919 		 * Guard against race condition in mutex_owner_running:
928575a7426Spt157919 		 * 	thread=owner(mutex)
929575a7426Spt157919 		 * 	<interrupt>
930575a7426Spt157919 		 * 				thread exits mutex
931575a7426Spt157919 		 * 				thread exits
932575a7426Spt157919 		 * 				thread reaped
933575a7426Spt157919 		 * 				thread struct freed
934575a7426Spt157919 		 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
935575a7426Spt157919 		 * A cross call to all cpus will cause the interrupt handler
936575a7426Spt157919 		 * to reset the PC if it is in mutex_owner_running, refreshing
937575a7426Spt157919 		 * stale thread pointers.
938575a7426Spt157919 		 */
939575a7426Spt157919 		mutex_sync();   /* sync with mutex code */
940575a7426Spt157919 		/*
9417c478bd9Sstevel@tonic-gate 		 * Reap threads
9427c478bd9Sstevel@tonic-gate 		 */
9437c478bd9Sstevel@tonic-gate 		thread_reap_list(t);
9447c478bd9Sstevel@tonic-gate 
9457c478bd9Sstevel@tonic-gate 		/*
9467c478bd9Sstevel@tonic-gate 		 * Reap lwps
9477c478bd9Sstevel@tonic-gate 		 */
9487c478bd9Sstevel@tonic-gate 		thread_reap_list(l);
949575a7426Spt157919 		delay(hz);
9507c478bd9Sstevel@tonic-gate 	}
9517c478bd9Sstevel@tonic-gate }
9527c478bd9Sstevel@tonic-gate 
9537c478bd9Sstevel@tonic-gate /*
954575a7426Spt157919  * This is called by lwpcreate, etc.() to put a lwp_deathrow thread onto
955575a7426Spt157919  * thread_deathrow. The thread's state is changed already TS_FREE to indicate
956575a7426Spt157919  * that is reapable. The thread already holds the reaplock, and was already
957575a7426Spt157919  * freed.
958575a7426Spt157919  */
959575a7426Spt157919 void
960575a7426Spt157919 reapq_move_lq_to_tq(kthread_t *t)
961575a7426Spt157919 {
962575a7426Spt157919 	ASSERT(t->t_state == TS_FREE);
963575a7426Spt157919 	ASSERT(MUTEX_HELD(&reaplock));
964575a7426Spt157919 	t->t_forw = thread_deathrow;
965575a7426Spt157919 	thread_deathrow = t;
966575a7426Spt157919 	thread_reapcnt++;
967575a7426Spt157919 	if (lwp_reapcnt + thread_reapcnt > reaplimit)
968575a7426Spt157919 		cv_signal(&reaper_cv);  /* wake the reaper */
969575a7426Spt157919 }
970575a7426Spt157919 
971575a7426Spt157919 /*
9727c478bd9Sstevel@tonic-gate  * This is called by resume() to put a zombie thread onto deathrow.
9737c478bd9Sstevel@tonic-gate  * The thread's state is changed to TS_FREE to indicate that is reapable.
974575a7426Spt157919  * This is called from the idle thread so it must not block - just spin.
9757c478bd9Sstevel@tonic-gate  */
9767c478bd9Sstevel@tonic-gate void
9777c478bd9Sstevel@tonic-gate reapq_add(kthread_t *t)
9787c478bd9Sstevel@tonic-gate {
9797c478bd9Sstevel@tonic-gate 	mutex_enter(&reaplock);
9807c478bd9Sstevel@tonic-gate 
9817c478bd9Sstevel@tonic-gate 	/*
982d32efdadSJonathan Adams 	 * lwp_deathrow contains threads with lwp linkage and
983d32efdadSJonathan Adams 	 * swappable thread stacks which have the default stacksize.
984d32efdadSJonathan Adams 	 * These threads' lwps and stacks may be reused by lwp_create().
985d32efdadSJonathan Adams 	 *
986d32efdadSJonathan Adams 	 * Anything else goes on thread_deathrow(), where it will eventually
987d32efdadSJonathan Adams 	 * be thread_free()d.
9887c478bd9Sstevel@tonic-gate 	 */
989d32efdadSJonathan Adams 	if (t->t_flag & T_LWPREUSE) {
990d32efdadSJonathan Adams 		ASSERT(ttolwp(t) != NULL);
9917c478bd9Sstevel@tonic-gate 		t->t_forw = lwp_deathrow;
9927c478bd9Sstevel@tonic-gate 		lwp_deathrow = t;
9937c478bd9Sstevel@tonic-gate 		lwp_reapcnt++;
9947c478bd9Sstevel@tonic-gate 	} else {
9957c478bd9Sstevel@tonic-gate 		t->t_forw = thread_deathrow;
9967c478bd9Sstevel@tonic-gate 		thread_deathrow = t;
9977c478bd9Sstevel@tonic-gate 		thread_reapcnt++;
9987c478bd9Sstevel@tonic-gate 	}
9997c478bd9Sstevel@tonic-gate 	if (lwp_reapcnt + thread_reapcnt > reaplimit)
10007c478bd9Sstevel@tonic-gate 		cv_signal(&reaper_cv);	/* wake the reaper */
10017c478bd9Sstevel@tonic-gate 	t->t_state = TS_FREE;
10027c478bd9Sstevel@tonic-gate 	lock_clear(&t->t_lock);
10032192a40fSqiao 
10042192a40fSqiao 	/*
10052192a40fSqiao 	 * Before we return, we need to grab and drop the thread lock for
10062192a40fSqiao 	 * the dead thread.  At this point, the current thread is the idle
10072192a40fSqiao 	 * thread, and the dead thread's CPU lock points to the current
10082192a40fSqiao 	 * CPU -- and we must grab and drop the lock to synchronize with
10092192a40fSqiao 	 * a racing thread walking a blocking chain that the zombie thread
10102192a40fSqiao 	 * was recently in.  By this point, that blocking chain is (by
10112192a40fSqiao 	 * definition) stale:  the dead thread is not holding any locks, and
10122192a40fSqiao 	 * is therefore not in any blocking chains -- but if we do not regrab
10132192a40fSqiao 	 * our lock before freeing the dead thread's data structures, the
10142192a40fSqiao 	 * thread walking the (stale) blocking chain will die on memory
10152192a40fSqiao 	 * corruption when it attempts to drop the dead thread's lock.  We
10162192a40fSqiao 	 * only need do this once because there is no way for the dead thread
10172192a40fSqiao 	 * to ever again be on a blocking chain:  once we have grabbed and
10182192a40fSqiao 	 * dropped the thread lock, we are guaranteed that anyone that could
10192192a40fSqiao 	 * have seen this thread in a blocking chain can no longer see it.
10202192a40fSqiao 	 */
10212192a40fSqiao 	thread_lock(t);
10222192a40fSqiao 	thread_unlock(t);
10232192a40fSqiao 
10247c478bd9Sstevel@tonic-gate 	mutex_exit(&reaplock);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate /*
10280baeff3dSrab  * Install thread context ops for the current thread.
10297c478bd9Sstevel@tonic-gate  */
10307c478bd9Sstevel@tonic-gate void
10317c478bd9Sstevel@tonic-gate installctx(
10327c478bd9Sstevel@tonic-gate 	kthread_t *t,
10337c478bd9Sstevel@tonic-gate 	void	*arg,
10347c478bd9Sstevel@tonic-gate 	void	(*save)(void *),
10357c478bd9Sstevel@tonic-gate 	void	(*restore)(void *),
10367c478bd9Sstevel@tonic-gate 	void	(*fork)(void *, void *),
10377c478bd9Sstevel@tonic-gate 	void	(*lwp_create)(void *, void *),
10387c478bd9Sstevel@tonic-gate 	void	(*exit)(void *),
10397c478bd9Sstevel@tonic-gate 	void	(*free)(void *, int))
10407c478bd9Sstevel@tonic-gate {
10417c478bd9Sstevel@tonic-gate 	struct ctxop *ctx;
10427c478bd9Sstevel@tonic-gate 
10437c478bd9Sstevel@tonic-gate 	ctx = kmem_alloc(sizeof (struct ctxop), KM_SLEEP);
10447c478bd9Sstevel@tonic-gate 	ctx->save_op = save;
10457c478bd9Sstevel@tonic-gate 	ctx->restore_op = restore;
10467c478bd9Sstevel@tonic-gate 	ctx->fork_op = fork;
10477c478bd9Sstevel@tonic-gate 	ctx->lwp_create_op = lwp_create;
10487c478bd9Sstevel@tonic-gate 	ctx->exit_op = exit;
10497c478bd9Sstevel@tonic-gate 	ctx->free_op = free;
10507c478bd9Sstevel@tonic-gate 	ctx->arg = arg;
10517c478bd9Sstevel@tonic-gate 	ctx->next = t->t_ctx;
10527c478bd9Sstevel@tonic-gate 	t->t_ctx = ctx;
10537c478bd9Sstevel@tonic-gate }
10547c478bd9Sstevel@tonic-gate 
10557c478bd9Sstevel@tonic-gate /*
10565a64ecfaStrevtom  * Remove the thread context ops from a thread.
10577c478bd9Sstevel@tonic-gate  */
10587c478bd9Sstevel@tonic-gate int
10597c478bd9Sstevel@tonic-gate removectx(
10607c478bd9Sstevel@tonic-gate 	kthread_t *t,
10617c478bd9Sstevel@tonic-gate 	void	*arg,
10627c478bd9Sstevel@tonic-gate 	void	(*save)(void *),
10637c478bd9Sstevel@tonic-gate 	void	(*restore)(void *),
10647c478bd9Sstevel@tonic-gate 	void	(*fork)(void *, void *),
10657c478bd9Sstevel@tonic-gate 	void	(*lwp_create)(void *, void *),
10667c478bd9Sstevel@tonic-gate 	void	(*exit)(void *),
10677c478bd9Sstevel@tonic-gate 	void	(*free)(void *, int))
10687c478bd9Sstevel@tonic-gate {
10697c478bd9Sstevel@tonic-gate 	struct ctxop *ctx, *prev_ctx;
10707c478bd9Sstevel@tonic-gate 
10715a64ecfaStrevtom 	/*
10725a64ecfaStrevtom 	 * The incoming kthread_t (which is the thread for which the
10735a64ecfaStrevtom 	 * context ops will be removed) should be one of the following:
10745a64ecfaStrevtom 	 *
10755a64ecfaStrevtom 	 * a) the current thread,
10765a64ecfaStrevtom 	 *
10775a64ecfaStrevtom 	 * b) a thread of a process that's being forked (SIDL),
10785a64ecfaStrevtom 	 *
10795a64ecfaStrevtom 	 * c) a thread that belongs to the same process as the current
10805a64ecfaStrevtom 	 *    thread and for which the current thread is the agent thread,
10815a64ecfaStrevtom 	 *
10825a64ecfaStrevtom 	 * d) a thread that is TS_STOPPED which is indicative of it
10835a64ecfaStrevtom 	 *    being (if curthread is not an agent) a thread being created
10845a64ecfaStrevtom 	 *    as part of an lwp creation.
10855a64ecfaStrevtom 	 */
10867c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread || ttoproc(t)->p_stat == SIDL ||
10873cf60c08Sdm120769 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
10887c478bd9Sstevel@tonic-gate 
10895a64ecfaStrevtom 	/*
10905a64ecfaStrevtom 	 * Serialize modifications to t->t_ctx to prevent the agent thread
10915a64ecfaStrevtom 	 * and the target thread from racing with each other during lwp exit.
10925a64ecfaStrevtom 	 */
10935a64ecfaStrevtom 	mutex_enter(&t->t_ctx_lock);
10947c478bd9Sstevel@tonic-gate 	prev_ctx = NULL;
10957c478bd9Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next) {
10967c478bd9Sstevel@tonic-gate 		if (ctx->save_op == save && ctx->restore_op == restore &&
10977c478bd9Sstevel@tonic-gate 		    ctx->fork_op == fork && ctx->lwp_create_op == lwp_create &&
10987c478bd9Sstevel@tonic-gate 		    ctx->exit_op == exit && ctx->free_op == free &&
10997c478bd9Sstevel@tonic-gate 		    ctx->arg == arg) {
11007c478bd9Sstevel@tonic-gate 			if (prev_ctx)
11017c478bd9Sstevel@tonic-gate 				prev_ctx->next = ctx->next;
11027c478bd9Sstevel@tonic-gate 			else
11037c478bd9Sstevel@tonic-gate 				t->t_ctx = ctx->next;
11045a64ecfaStrevtom 			mutex_exit(&t->t_ctx_lock);
11057c478bd9Sstevel@tonic-gate 			if (ctx->free_op != NULL)
11067c478bd9Sstevel@tonic-gate 				(ctx->free_op)(ctx->arg, 0);
11077c478bd9Sstevel@tonic-gate 			kmem_free(ctx, sizeof (struct ctxop));
11087c478bd9Sstevel@tonic-gate 			return (1);
11097c478bd9Sstevel@tonic-gate 		}
11107c478bd9Sstevel@tonic-gate 		prev_ctx = ctx;
11117c478bd9Sstevel@tonic-gate 	}
11125a64ecfaStrevtom 	mutex_exit(&t->t_ctx_lock);
11135a64ecfaStrevtom 
11147c478bd9Sstevel@tonic-gate 	return (0);
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate void
11187c478bd9Sstevel@tonic-gate savectx(kthread_t *t)
11197c478bd9Sstevel@tonic-gate {
11207c478bd9Sstevel@tonic-gate 	struct ctxop *ctx;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
11237c478bd9Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
11247c478bd9Sstevel@tonic-gate 		if (ctx->save_op != NULL)
11257c478bd9Sstevel@tonic-gate 			(ctx->save_op)(ctx->arg);
11267c478bd9Sstevel@tonic-gate }
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate void
11297c478bd9Sstevel@tonic-gate restorectx(kthread_t *t)
11307c478bd9Sstevel@tonic-gate {
11317c478bd9Sstevel@tonic-gate 	struct ctxop *ctx;
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread);
11347c478bd9Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
11357c478bd9Sstevel@tonic-gate 		if (ctx->restore_op != NULL)
11367c478bd9Sstevel@tonic-gate 			(ctx->restore_op)(ctx->arg);
11377c478bd9Sstevel@tonic-gate }
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate void
11407c478bd9Sstevel@tonic-gate forkctx(kthread_t *t, kthread_t *ct)
11417c478bd9Sstevel@tonic-gate {
11427c478bd9Sstevel@tonic-gate 	struct ctxop *ctx;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
11457c478bd9Sstevel@tonic-gate 		if (ctx->fork_op != NULL)
11467c478bd9Sstevel@tonic-gate 			(ctx->fork_op)(t, ct);
11477c478bd9Sstevel@tonic-gate }
11487c478bd9Sstevel@tonic-gate 
11497c478bd9Sstevel@tonic-gate /*
11507c478bd9Sstevel@tonic-gate  * Note that this operator is only invoked via the _lwp_create
11517c478bd9Sstevel@tonic-gate  * system call.  The system may have other reasons to create lwps
11527c478bd9Sstevel@tonic-gate  * e.g. the agent lwp or the doors unreferenced lwp.
11537c478bd9Sstevel@tonic-gate  */
11547c478bd9Sstevel@tonic-gate void
11557c478bd9Sstevel@tonic-gate lwp_createctx(kthread_t *t, kthread_t *ct)
11567c478bd9Sstevel@tonic-gate {
11577c478bd9Sstevel@tonic-gate 	struct ctxop *ctx;
11587c478bd9Sstevel@tonic-gate 
11597c478bd9Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
11607c478bd9Sstevel@tonic-gate 		if (ctx->lwp_create_op != NULL)
11617c478bd9Sstevel@tonic-gate 			(ctx->lwp_create_op)(t, ct);
11627c478bd9Sstevel@tonic-gate }
11637c478bd9Sstevel@tonic-gate 
11647c478bd9Sstevel@tonic-gate /*
11657c478bd9Sstevel@tonic-gate  * exitctx is called from thread_exit() and lwp_exit() to perform any actions
11667c478bd9Sstevel@tonic-gate  * needed when the thread/LWP leaves the processor for the last time. This
11677c478bd9Sstevel@tonic-gate  * routine is not intended to deal with freeing memory; freectx() is used for
11687c478bd9Sstevel@tonic-gate  * that purpose during thread_free(). This routine is provided to allow for
11697c478bd9Sstevel@tonic-gate  * clean-up that can't wait until thread_free().
11707c478bd9Sstevel@tonic-gate  */
11717c478bd9Sstevel@tonic-gate void
11727c478bd9Sstevel@tonic-gate exitctx(kthread_t *t)
11737c478bd9Sstevel@tonic-gate {
11747c478bd9Sstevel@tonic-gate 	struct ctxop *ctx;
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
11777c478bd9Sstevel@tonic-gate 		if (ctx->exit_op != NULL)
11787c478bd9Sstevel@tonic-gate 			(ctx->exit_op)(t);
11797c478bd9Sstevel@tonic-gate }
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate /*
11827c478bd9Sstevel@tonic-gate  * freectx is called from thread_free() and exec() to get
11830baeff3dSrab  * rid of old thread context ops.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate void
11867c478bd9Sstevel@tonic-gate freectx(kthread_t *t, int isexec)
11877c478bd9Sstevel@tonic-gate {
11887c478bd9Sstevel@tonic-gate 	struct ctxop *ctx;
11897c478bd9Sstevel@tonic-gate 
11907c478bd9Sstevel@tonic-gate 	while ((ctx = t->t_ctx) != NULL) {
11917c478bd9Sstevel@tonic-gate 		t->t_ctx = ctx->next;
11927c478bd9Sstevel@tonic-gate 		if (ctx->free_op != NULL)
11937c478bd9Sstevel@tonic-gate 			(ctx->free_op)(ctx->arg, isexec);
11947c478bd9Sstevel@tonic-gate 		kmem_free(ctx, sizeof (struct ctxop));
11957c478bd9Sstevel@tonic-gate 	}
11967c478bd9Sstevel@tonic-gate }
11977c478bd9Sstevel@tonic-gate 
11987c478bd9Sstevel@tonic-gate /*
1199575a7426Spt157919  * freectx_ctx is called from lwp_create() when lwp is reused from
1200575a7426Spt157919  * lwp_deathrow and its thread structure is added to thread_deathrow.
1201575a7426Spt157919  * The thread structure to which this ctx was attached may be already
1202575a7426Spt157919  * freed by the thread reaper so free_op implementations shouldn't rely
1203575a7426Spt157919  * on thread structure to which this ctx was attached still being around.
1204575a7426Spt157919  */
1205575a7426Spt157919 void
1206575a7426Spt157919 freectx_ctx(struct ctxop *ctx)
1207575a7426Spt157919 {
1208575a7426Spt157919 	struct ctxop *nctx;
1209575a7426Spt157919 
1210575a7426Spt157919 	ASSERT(ctx != NULL);
1211575a7426Spt157919 
1212575a7426Spt157919 	do {
1213575a7426Spt157919 		nctx = ctx->next;
1214575a7426Spt157919 		if (ctx->free_op != NULL)
1215575a7426Spt157919 			(ctx->free_op)(ctx->arg, 0);
1216575a7426Spt157919 		kmem_free(ctx, sizeof (struct ctxop));
1217575a7426Spt157919 	} while ((ctx = nctx) != NULL);
1218575a7426Spt157919 }
1219575a7426Spt157919 
1220575a7426Spt157919 /*
12217c478bd9Sstevel@tonic-gate  * Set the thread running; arrange for it to be swapped in if necessary.
12227c478bd9Sstevel@tonic-gate  */
12237c478bd9Sstevel@tonic-gate void
12247c478bd9Sstevel@tonic-gate setrun_locked(kthread_t *t)
12257c478bd9Sstevel@tonic-gate {
12267c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
12277c478bd9Sstevel@tonic-gate 	if (t->t_state == TS_SLEEP) {
12287c478bd9Sstevel@tonic-gate 		/*
12297c478bd9Sstevel@tonic-gate 		 * Take off sleep queue.
12307c478bd9Sstevel@tonic-gate 		 */
12317c478bd9Sstevel@tonic-gate 		SOBJ_UNSLEEP(t->t_sobj_ops, t);
12327c478bd9Sstevel@tonic-gate 	} else if (t->t_state & (TS_RUN | TS_ONPROC)) {
12337c478bd9Sstevel@tonic-gate 		/*
12347c478bd9Sstevel@tonic-gate 		 * Already on dispatcher queue.
12357c478bd9Sstevel@tonic-gate 		 */
12367c478bd9Sstevel@tonic-gate 		return;
1237c97ad5cdSakolb 	} else if (t->t_state == TS_WAIT) {
1238c97ad5cdSakolb 		waitq_setrun(t);
12397c478bd9Sstevel@tonic-gate 	} else if (t->t_state == TS_STOPPED) {
12407c478bd9Sstevel@tonic-gate 		/*
12417c478bd9Sstevel@tonic-gate 		 * All of the sending of SIGCONT (TC_XSTART) and /proc
12427c478bd9Sstevel@tonic-gate 		 * (TC_PSTART) and lwp_continue() (TC_CSTART) must have
12437c478bd9Sstevel@tonic-gate 		 * requested that the thread be run.
12447c478bd9Sstevel@tonic-gate 		 * Just calling setrun() is not sufficient to set a stopped
12457c478bd9Sstevel@tonic-gate 		 * thread running.  TP_TXSTART is always set if the thread
12467c478bd9Sstevel@tonic-gate 		 * is not stopped by a jobcontrol stop signal.
12477c478bd9Sstevel@tonic-gate 		 * TP_TPSTART is always set if /proc is not controlling it.
12487c478bd9Sstevel@tonic-gate 		 * TP_TCSTART is always set if lwp_suspend() didn't stop it.
12497c478bd9Sstevel@tonic-gate 		 * The thread won't be stopped unless one of these
12507c478bd9Sstevel@tonic-gate 		 * three mechanisms did it.
12517c478bd9Sstevel@tonic-gate 		 *
12527c478bd9Sstevel@tonic-gate 		 * These flags must be set before calling setrun_locked(t).
12537c478bd9Sstevel@tonic-gate 		 * They can't be passed as arguments because the streams
12547c478bd9Sstevel@tonic-gate 		 * code calls setrun() indirectly and the mechanism for
12557c478bd9Sstevel@tonic-gate 		 * doing so admits only one argument.  Note that the
12567c478bd9Sstevel@tonic-gate 		 * thread must be locked in order to change t_schedflags.
12577c478bd9Sstevel@tonic-gate 		 */
12587c478bd9Sstevel@tonic-gate 		if ((t->t_schedflag & TS_ALLSTART) != TS_ALLSTART)
12597c478bd9Sstevel@tonic-gate 			return;
12607c478bd9Sstevel@tonic-gate 		/*
12617c478bd9Sstevel@tonic-gate 		 * Process is no longer stopped (a thread is running).
12627c478bd9Sstevel@tonic-gate 		 */
12637c478bd9Sstevel@tonic-gate 		t->t_whystop = 0;
12647c478bd9Sstevel@tonic-gate 		t->t_whatstop = 0;
12657c478bd9Sstevel@tonic-gate 		/*
12667c478bd9Sstevel@tonic-gate 		 * Strictly speaking, we do not have to clear these
12677c478bd9Sstevel@tonic-gate 		 * flags here; they are cleared on entry to stop().
12687c478bd9Sstevel@tonic-gate 		 * However, they are confusing when doing kernel
12697c478bd9Sstevel@tonic-gate 		 * debugging or when they are revealed by ps(1).
12707c478bd9Sstevel@tonic-gate 		 */
12717c478bd9Sstevel@tonic-gate 		t->t_schedflag &= ~TS_ALLSTART;
12727c478bd9Sstevel@tonic-gate 		THREAD_TRANSITION(t);	/* drop stopped-thread lock */
12737c478bd9Sstevel@tonic-gate 		ASSERT(t->t_lockp == &transition_lock);
12747c478bd9Sstevel@tonic-gate 		ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
12757c478bd9Sstevel@tonic-gate 		/*
12767c478bd9Sstevel@tonic-gate 		 * Let the class put the process on the dispatcher queue.
12777c478bd9Sstevel@tonic-gate 		 */
12787c478bd9Sstevel@tonic-gate 		CL_SETRUN(t);
12797c478bd9Sstevel@tonic-gate 	}
12807c478bd9Sstevel@tonic-gate }
12817c478bd9Sstevel@tonic-gate 
12827c478bd9Sstevel@tonic-gate void
12837c478bd9Sstevel@tonic-gate setrun(kthread_t *t)
12847c478bd9Sstevel@tonic-gate {
12857c478bd9Sstevel@tonic-gate 	thread_lock(t);
12867c478bd9Sstevel@tonic-gate 	setrun_locked(t);
12877c478bd9Sstevel@tonic-gate 	thread_unlock(t);
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate /*
12917c478bd9Sstevel@tonic-gate  * Unpin an interrupted thread.
12927c478bd9Sstevel@tonic-gate  *	When an interrupt occurs, the interrupt is handled on the stack
12937c478bd9Sstevel@tonic-gate  *	of an interrupt thread, taken from a pool linked to the CPU structure.
12947c478bd9Sstevel@tonic-gate  *
12957c478bd9Sstevel@tonic-gate  *	When swtch() is switching away from an interrupt thread because it
12967c478bd9Sstevel@tonic-gate  *	blocked or was preempted, this routine is called to complete the
12977c478bd9Sstevel@tonic-gate  *	saving of the interrupted thread state, and returns the interrupted
12987c478bd9Sstevel@tonic-gate  *	thread pointer so it may be resumed.
12997c478bd9Sstevel@tonic-gate  *
13007c478bd9Sstevel@tonic-gate  *	Called by swtch() only at high spl.
13017c478bd9Sstevel@tonic-gate  */
13027c478bd9Sstevel@tonic-gate kthread_t *
13037c478bd9Sstevel@tonic-gate thread_unpin()
13047c478bd9Sstevel@tonic-gate {
13057c478bd9Sstevel@tonic-gate 	kthread_t	*t = curthread;	/* current thread */
13067c478bd9Sstevel@tonic-gate 	kthread_t	*itp;		/* interrupted thread */
13077c478bd9Sstevel@tonic-gate 	int		i;		/* interrupt level */
13087c478bd9Sstevel@tonic-gate 	extern int	intr_passivate();
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	ASSERT(t->t_intr != NULL);
13117c478bd9Sstevel@tonic-gate 
13127c478bd9Sstevel@tonic-gate 	itp = t->t_intr;		/* interrupted thread */
13137c478bd9Sstevel@tonic-gate 	t->t_intr = NULL;		/* clear interrupt ptr */
13147c478bd9Sstevel@tonic-gate 
13157c478bd9Sstevel@tonic-gate 	/*
13167c478bd9Sstevel@tonic-gate 	 * Get state from interrupt thread for the one
13177c478bd9Sstevel@tonic-gate 	 * it interrupted.
13187c478bd9Sstevel@tonic-gate 	 */
13197c478bd9Sstevel@tonic-gate 
13207c478bd9Sstevel@tonic-gate 	i = intr_passivate(t, itp);
13217c478bd9Sstevel@tonic-gate 
13227c478bd9Sstevel@tonic-gate 	TRACE_5(TR_FAC_INTR, TR_INTR_PASSIVATE,
13237c478bd9Sstevel@tonic-gate 	    "intr_passivate:level %d curthread %p (%T) ithread %p (%T)",
13247c478bd9Sstevel@tonic-gate 	    i, t, t, itp, itp);
13257c478bd9Sstevel@tonic-gate 
13267c478bd9Sstevel@tonic-gate 	/*
13277c478bd9Sstevel@tonic-gate 	 * Dissociate the current thread from the interrupted thread's LWP.
13287c478bd9Sstevel@tonic-gate 	 */
13297c478bd9Sstevel@tonic-gate 	t->t_lwp = NULL;
13307c478bd9Sstevel@tonic-gate 
13317c478bd9Sstevel@tonic-gate 	/*
13327c478bd9Sstevel@tonic-gate 	 * Interrupt handlers above the level that spinlocks block must
13337c478bd9Sstevel@tonic-gate 	 * not block.
13347c478bd9Sstevel@tonic-gate 	 */
13357c478bd9Sstevel@tonic-gate #if DEBUG
13367c478bd9Sstevel@tonic-gate 	if (i < 0 || i > LOCK_LEVEL)
13377c478bd9Sstevel@tonic-gate 		cmn_err(CE_PANIC, "thread_unpin: ipl out of range %x", i);
13387c478bd9Sstevel@tonic-gate #endif
13397c478bd9Sstevel@tonic-gate 
13407c478bd9Sstevel@tonic-gate 	/*
13417c478bd9Sstevel@tonic-gate 	 * Compute the CPU's base interrupt level based on the active
13427c478bd9Sstevel@tonic-gate 	 * interrupts.
13437c478bd9Sstevel@tonic-gate 	 */
13447c478bd9Sstevel@tonic-gate 	ASSERT(CPU->cpu_intr_actv & (1 << i));
13457c478bd9Sstevel@tonic-gate 	set_base_spl();
13467c478bd9Sstevel@tonic-gate 
13477c478bd9Sstevel@tonic-gate 	return (itp);
13487c478bd9Sstevel@tonic-gate }
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate /*
13517c478bd9Sstevel@tonic-gate  * Create and initialize an interrupt thread.
13527c478bd9Sstevel@tonic-gate  *	Returns non-zero on error.
13537c478bd9Sstevel@tonic-gate  *	Called at spl7() or better.
13547c478bd9Sstevel@tonic-gate  */
13557c478bd9Sstevel@tonic-gate void
13567c478bd9Sstevel@tonic-gate thread_create_intr(struct cpu *cp)
13577c478bd9Sstevel@tonic-gate {
13587c478bd9Sstevel@tonic-gate 	kthread_t *tp;
13597c478bd9Sstevel@tonic-gate 
13607c478bd9Sstevel@tonic-gate 	tp = thread_create(NULL, 0,
13617c478bd9Sstevel@tonic-gate 	    (void (*)())thread_create_intr, NULL, 0, &p0, TS_ONPROC, 0);
13627c478bd9Sstevel@tonic-gate 
13637c478bd9Sstevel@tonic-gate 	/*
13647c478bd9Sstevel@tonic-gate 	 * Set the thread in the TS_FREE state.  The state will change
13657c478bd9Sstevel@tonic-gate 	 * to TS_ONPROC only while the interrupt is active.  Think of these
13667c478bd9Sstevel@tonic-gate 	 * as being on a private free list for the CPU.  Being TS_FREE keeps
13677c478bd9Sstevel@tonic-gate 	 * inactive interrupt threads out of debugger thread lists.
13687c478bd9Sstevel@tonic-gate 	 *
13697c478bd9Sstevel@tonic-gate 	 * We cannot call thread_create with TS_FREE because of the current
13707c478bd9Sstevel@tonic-gate 	 * checks there for ONPROC.  Fix this when thread_create takes flags.
13717c478bd9Sstevel@tonic-gate 	 */
13727c478bd9Sstevel@tonic-gate 	THREAD_FREEINTR(tp, cp);
13737c478bd9Sstevel@tonic-gate 
13747c478bd9Sstevel@tonic-gate 	/*
13757c478bd9Sstevel@tonic-gate 	 * Nobody should ever reference the credentials of an interrupt
13767c478bd9Sstevel@tonic-gate 	 * thread so make it NULL to catch any such references.
13777c478bd9Sstevel@tonic-gate 	 */
13787c478bd9Sstevel@tonic-gate 	tp->t_cred = NULL;
13797c478bd9Sstevel@tonic-gate 	tp->t_flag |= T_INTR_THREAD;
13807c478bd9Sstevel@tonic-gate 	tp->t_cpu = cp;
13817c478bd9Sstevel@tonic-gate 	tp->t_bound_cpu = cp;
13827c478bd9Sstevel@tonic-gate 	tp->t_disp_queue = cp->cpu_disp;
13837c478bd9Sstevel@tonic-gate 	tp->t_affinitycnt = 1;
13847c478bd9Sstevel@tonic-gate 	tp->t_preempt = 1;
13857c478bd9Sstevel@tonic-gate 
13867c478bd9Sstevel@tonic-gate 	/*
13877c478bd9Sstevel@tonic-gate 	 * Don't make a user-requested binding on this thread so that
13887c478bd9Sstevel@tonic-gate 	 * the processor can be offlined.
13897c478bd9Sstevel@tonic-gate 	 */
13907c478bd9Sstevel@tonic-gate 	tp->t_bind_cpu = PBIND_NONE;	/* no USER-requested binding */
13917c478bd9Sstevel@tonic-gate 	tp->t_bind_pset = PS_NONE;
13927c478bd9Sstevel@tonic-gate 
13937c478bd9Sstevel@tonic-gate #if defined(__i386) || defined(__amd64)
13947c478bd9Sstevel@tonic-gate 	tp->t_stk -= STACK_ALIGN;
13957c478bd9Sstevel@tonic-gate 	*(tp->t_stk) = 0;		/* terminate intr thread stack */
13967c478bd9Sstevel@tonic-gate #endif
13977c478bd9Sstevel@tonic-gate 
13987c478bd9Sstevel@tonic-gate 	/*
13997c478bd9Sstevel@tonic-gate 	 * Link onto CPU's interrupt pool.
14007c478bd9Sstevel@tonic-gate 	 */
14017c478bd9Sstevel@tonic-gate 	tp->t_link = cp->cpu_intr_thread;
14027c478bd9Sstevel@tonic-gate 	cp->cpu_intr_thread = tp;
14037c478bd9Sstevel@tonic-gate }
14047c478bd9Sstevel@tonic-gate 
14057c478bd9Sstevel@tonic-gate /*
14067c478bd9Sstevel@tonic-gate  * TSD -- THREAD SPECIFIC DATA
14077c478bd9Sstevel@tonic-gate  */
14087c478bd9Sstevel@tonic-gate static kmutex_t		tsd_mutex;	 /* linked list spin lock */
14097c478bd9Sstevel@tonic-gate static uint_t		tsd_nkeys;	 /* size of destructor array */
14107c478bd9Sstevel@tonic-gate /* per-key destructor funcs */
14117c478bd9Sstevel@tonic-gate static void 		(**tsd_destructor)(void *);
14127c478bd9Sstevel@tonic-gate /* list of tsd_thread's */
14137c478bd9Sstevel@tonic-gate static struct tsd_thread	*tsd_list;
14147c478bd9Sstevel@tonic-gate 
14157c478bd9Sstevel@tonic-gate /*
14167c478bd9Sstevel@tonic-gate  * Default destructor
14177c478bd9Sstevel@tonic-gate  *	Needed because NULL destructor means that the key is unused
14187c478bd9Sstevel@tonic-gate  */
14197c478bd9Sstevel@tonic-gate /* ARGSUSED */
14207c478bd9Sstevel@tonic-gate void
14217c478bd9Sstevel@tonic-gate tsd_defaultdestructor(void *value)
14227c478bd9Sstevel@tonic-gate {}
14237c478bd9Sstevel@tonic-gate 
14247c478bd9Sstevel@tonic-gate /*
14257c478bd9Sstevel@tonic-gate  * Create a key (index into per thread array)
14267c478bd9Sstevel@tonic-gate  *	Locks out tsd_create, tsd_destroy, and tsd_exit
14277c478bd9Sstevel@tonic-gate  *	May allocate memory with lock held
14287c478bd9Sstevel@tonic-gate  */
14297c478bd9Sstevel@tonic-gate void
14307c478bd9Sstevel@tonic-gate tsd_create(uint_t *keyp, void (*destructor)(void *))
14317c478bd9Sstevel@tonic-gate {
14327c478bd9Sstevel@tonic-gate 	int	i;
14337c478bd9Sstevel@tonic-gate 	uint_t	nkeys;
14347c478bd9Sstevel@tonic-gate 
14357c478bd9Sstevel@tonic-gate 	/*
14367c478bd9Sstevel@tonic-gate 	 * if key is allocated, do nothing
14377c478bd9Sstevel@tonic-gate 	 */
14387c478bd9Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
14397c478bd9Sstevel@tonic-gate 	if (*keyp) {
14407c478bd9Sstevel@tonic-gate 		mutex_exit(&tsd_mutex);
14417c478bd9Sstevel@tonic-gate 		return;
14427c478bd9Sstevel@tonic-gate 	}
14437c478bd9Sstevel@tonic-gate 	/*
14447c478bd9Sstevel@tonic-gate 	 * find an unused key
14457c478bd9Sstevel@tonic-gate 	 */
14467c478bd9Sstevel@tonic-gate 	if (destructor == NULL)
14477c478bd9Sstevel@tonic-gate 		destructor = tsd_defaultdestructor;
14487c478bd9Sstevel@tonic-gate 
14497c478bd9Sstevel@tonic-gate 	for (i = 0; i < tsd_nkeys; ++i)
14507c478bd9Sstevel@tonic-gate 		if (tsd_destructor[i] == NULL)
14517c478bd9Sstevel@tonic-gate 			break;
14527c478bd9Sstevel@tonic-gate 
14537c478bd9Sstevel@tonic-gate 	/*
14547c478bd9Sstevel@tonic-gate 	 * if no unused keys, increase the size of the destructor array
14557c478bd9Sstevel@tonic-gate 	 */
14567c478bd9Sstevel@tonic-gate 	if (i == tsd_nkeys) {
14577c478bd9Sstevel@tonic-gate 		if ((nkeys = (tsd_nkeys << 1)) == 0)
14587c478bd9Sstevel@tonic-gate 			nkeys = 1;
14597c478bd9Sstevel@tonic-gate 		tsd_destructor =
14607c478bd9Sstevel@tonic-gate 		    (void (**)(void *))tsd_realloc((void *)tsd_destructor,
14617c478bd9Sstevel@tonic-gate 		    (size_t)(tsd_nkeys * sizeof (void (*)(void *))),
14627c478bd9Sstevel@tonic-gate 		    (size_t)(nkeys * sizeof (void (*)(void *))));
14637c478bd9Sstevel@tonic-gate 		tsd_nkeys = nkeys;
14647c478bd9Sstevel@tonic-gate 	}
14657c478bd9Sstevel@tonic-gate 
14667c478bd9Sstevel@tonic-gate 	/*
14677c478bd9Sstevel@tonic-gate 	 * allocate the next available unused key
14687c478bd9Sstevel@tonic-gate 	 */
14697c478bd9Sstevel@tonic-gate 	tsd_destructor[i] = destructor;
14707c478bd9Sstevel@tonic-gate 	*keyp = i + 1;
14717c478bd9Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
14727c478bd9Sstevel@tonic-gate }
14737c478bd9Sstevel@tonic-gate 
14747c478bd9Sstevel@tonic-gate /*
14757c478bd9Sstevel@tonic-gate  * Destroy a key -- this is for unloadable modules
14767c478bd9Sstevel@tonic-gate  *
14777c478bd9Sstevel@tonic-gate  * Assumes that the caller is preventing tsd_set and tsd_get
14787c478bd9Sstevel@tonic-gate  * Locks out tsd_create, tsd_destroy, and tsd_exit
14797c478bd9Sstevel@tonic-gate  * May free memory with lock held
14807c478bd9Sstevel@tonic-gate  */
14817c478bd9Sstevel@tonic-gate void
14827c478bd9Sstevel@tonic-gate tsd_destroy(uint_t *keyp)
14837c478bd9Sstevel@tonic-gate {
14847c478bd9Sstevel@tonic-gate 	uint_t key;
14857c478bd9Sstevel@tonic-gate 	struct tsd_thread *tsd;
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	/*
14887c478bd9Sstevel@tonic-gate 	 * protect the key namespace and our destructor lists
14897c478bd9Sstevel@tonic-gate 	 */
14907c478bd9Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
14917c478bd9Sstevel@tonic-gate 	key = *keyp;
14927c478bd9Sstevel@tonic-gate 	*keyp = 0;
14937c478bd9Sstevel@tonic-gate 
14947c478bd9Sstevel@tonic-gate 	ASSERT(key <= tsd_nkeys);
14957c478bd9Sstevel@tonic-gate 
14967c478bd9Sstevel@tonic-gate 	/*
14977c478bd9Sstevel@tonic-gate 	 * if the key is valid
14987c478bd9Sstevel@tonic-gate 	 */
14997c478bd9Sstevel@tonic-gate 	if (key != 0) {
15007c478bd9Sstevel@tonic-gate 		uint_t k = key - 1;
15017c478bd9Sstevel@tonic-gate 		/*
15027c478bd9Sstevel@tonic-gate 		 * for every thread with TSD, call key's destructor
15037c478bd9Sstevel@tonic-gate 		 */
15047c478bd9Sstevel@tonic-gate 		for (tsd = tsd_list; tsd; tsd = tsd->ts_next) {
15057c478bd9Sstevel@tonic-gate 			/*
15067c478bd9Sstevel@tonic-gate 			 * no TSD for key in this thread
15077c478bd9Sstevel@tonic-gate 			 */
15087c478bd9Sstevel@tonic-gate 			if (key > tsd->ts_nkeys)
15097c478bd9Sstevel@tonic-gate 				continue;
15107c478bd9Sstevel@tonic-gate 			/*
15117c478bd9Sstevel@tonic-gate 			 * call destructor for key
15127c478bd9Sstevel@tonic-gate 			 */
15137c478bd9Sstevel@tonic-gate 			if (tsd->ts_value[k] && tsd_destructor[k])
15147c478bd9Sstevel@tonic-gate 				(*tsd_destructor[k])(tsd->ts_value[k]);
15157c478bd9Sstevel@tonic-gate 			/*
15167c478bd9Sstevel@tonic-gate 			 * reset value for key
15177c478bd9Sstevel@tonic-gate 			 */
15187c478bd9Sstevel@tonic-gate 			tsd->ts_value[k] = NULL;
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 		/*
15217c478bd9Sstevel@tonic-gate 		 * actually free the key (NULL destructor == unused)
15227c478bd9Sstevel@tonic-gate 		 */
15237c478bd9Sstevel@tonic-gate 		tsd_destructor[k] = NULL;
15247c478bd9Sstevel@tonic-gate 	}
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate /*
15307c478bd9Sstevel@tonic-gate  * Quickly return the per thread value that was stored with the specified key
15317c478bd9Sstevel@tonic-gate  * Assumes the caller is protecting key from tsd_create and tsd_destroy
15327c478bd9Sstevel@tonic-gate  */
15337c478bd9Sstevel@tonic-gate void *
15347c478bd9Sstevel@tonic-gate tsd_get(uint_t key)
15357c478bd9Sstevel@tonic-gate {
15367c478bd9Sstevel@tonic-gate 	return (tsd_agent_get(curthread, key));
15377c478bd9Sstevel@tonic-gate }
15387c478bd9Sstevel@tonic-gate 
15397c478bd9Sstevel@tonic-gate /*
15407c478bd9Sstevel@tonic-gate  * Set a per thread value indexed with the specified key
15417c478bd9Sstevel@tonic-gate  */
15427c478bd9Sstevel@tonic-gate int
15437c478bd9Sstevel@tonic-gate tsd_set(uint_t key, void *value)
15447c478bd9Sstevel@tonic-gate {
15457c478bd9Sstevel@tonic-gate 	return (tsd_agent_set(curthread, key, value));
15467c478bd9Sstevel@tonic-gate }
15477c478bd9Sstevel@tonic-gate 
15487c478bd9Sstevel@tonic-gate /*
15497c478bd9Sstevel@tonic-gate  * Like tsd_get(), except that the agent lwp can get the tsd of
15507c478bd9Sstevel@tonic-gate  * another thread in the same process (the agent thread only runs when the
15517c478bd9Sstevel@tonic-gate  * process is completely stopped by /proc), or syslwp is creating a new lwp.
15527c478bd9Sstevel@tonic-gate  */
15537c478bd9Sstevel@tonic-gate void *
15547c478bd9Sstevel@tonic-gate tsd_agent_get(kthread_t *t, uint_t key)
15557c478bd9Sstevel@tonic-gate {
15567c478bd9Sstevel@tonic-gate 	struct tsd_thread *tsd = t->t_tsd;
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread ||
15597c478bd9Sstevel@tonic-gate 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
15607c478bd9Sstevel@tonic-gate 
15617c478bd9Sstevel@tonic-gate 	if (key && tsd != NULL && key <= tsd->ts_nkeys)
15627c478bd9Sstevel@tonic-gate 		return (tsd->ts_value[key - 1]);
15637c478bd9Sstevel@tonic-gate 	return (NULL);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate /*
15677c478bd9Sstevel@tonic-gate  * Like tsd_set(), except that the agent lwp can set the tsd of
15687c478bd9Sstevel@tonic-gate  * another thread in the same process, or syslwp can set the tsd
15697c478bd9Sstevel@tonic-gate  * of a thread it's in the middle of creating.
15707c478bd9Sstevel@tonic-gate  *
15717c478bd9Sstevel@tonic-gate  * Assumes the caller is protecting key from tsd_create and tsd_destroy
15727c478bd9Sstevel@tonic-gate  * May lock out tsd_destroy (and tsd_create), may allocate memory with
15737c478bd9Sstevel@tonic-gate  * lock held
15747c478bd9Sstevel@tonic-gate  */
15757c478bd9Sstevel@tonic-gate int
15767c478bd9Sstevel@tonic-gate tsd_agent_set(kthread_t *t, uint_t key, void *value)
15777c478bd9Sstevel@tonic-gate {
15787c478bd9Sstevel@tonic-gate 	struct tsd_thread *tsd = t->t_tsd;
15797c478bd9Sstevel@tonic-gate 
15807c478bd9Sstevel@tonic-gate 	ASSERT(t == curthread ||
15817c478bd9Sstevel@tonic-gate 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
15827c478bd9Sstevel@tonic-gate 
15837c478bd9Sstevel@tonic-gate 	if (key == 0)
15847c478bd9Sstevel@tonic-gate 		return (EINVAL);
15857c478bd9Sstevel@tonic-gate 	if (tsd == NULL)
15867c478bd9Sstevel@tonic-gate 		tsd = t->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
15877c478bd9Sstevel@tonic-gate 	if (key <= tsd->ts_nkeys) {
15887c478bd9Sstevel@tonic-gate 		tsd->ts_value[key - 1] = value;
15897c478bd9Sstevel@tonic-gate 		return (0);
15907c478bd9Sstevel@tonic-gate 	}
15917c478bd9Sstevel@tonic-gate 
15927c478bd9Sstevel@tonic-gate 	ASSERT(key <= tsd_nkeys);
15937c478bd9Sstevel@tonic-gate 
15947c478bd9Sstevel@tonic-gate 	/*
15957c478bd9Sstevel@tonic-gate 	 * lock out tsd_destroy()
15967c478bd9Sstevel@tonic-gate 	 */
15977c478bd9Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
15987c478bd9Sstevel@tonic-gate 	if (tsd->ts_nkeys == 0) {
15997c478bd9Sstevel@tonic-gate 		/*
16007c478bd9Sstevel@tonic-gate 		 * Link onto list of threads with TSD
16017c478bd9Sstevel@tonic-gate 		 */
16027c478bd9Sstevel@tonic-gate 		if ((tsd->ts_next = tsd_list) != NULL)
16037c478bd9Sstevel@tonic-gate 			tsd_list->ts_prev = tsd;
16047c478bd9Sstevel@tonic-gate 		tsd_list = tsd;
16057c478bd9Sstevel@tonic-gate 	}
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	/*
16087c478bd9Sstevel@tonic-gate 	 * Allocate thread local storage and set the value for key
16097c478bd9Sstevel@tonic-gate 	 */
16107c478bd9Sstevel@tonic-gate 	tsd->ts_value = tsd_realloc(tsd->ts_value,
16117c478bd9Sstevel@tonic-gate 	    tsd->ts_nkeys * sizeof (void *),
16127c478bd9Sstevel@tonic-gate 	    key * sizeof (void *));
16137c478bd9Sstevel@tonic-gate 	tsd->ts_nkeys = key;
16147c478bd9Sstevel@tonic-gate 	tsd->ts_value[key - 1] = value;
16157c478bd9Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
16167c478bd9Sstevel@tonic-gate 
16177c478bd9Sstevel@tonic-gate 	return (0);
16187c478bd9Sstevel@tonic-gate }
16197c478bd9Sstevel@tonic-gate 
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate /*
16227c478bd9Sstevel@tonic-gate  * Return the per thread value that was stored with the specified key
16237c478bd9Sstevel@tonic-gate  *	If necessary, create the key and the value
16247c478bd9Sstevel@tonic-gate  *	Assumes the caller is protecting *keyp from tsd_destroy
16257c478bd9Sstevel@tonic-gate  */
16267c478bd9Sstevel@tonic-gate void *
16277c478bd9Sstevel@tonic-gate tsd_getcreate(uint_t *keyp, void (*destroy)(void *), void *(*allocate)(void))
16287c478bd9Sstevel@tonic-gate {
16297c478bd9Sstevel@tonic-gate 	void *value;
16307c478bd9Sstevel@tonic-gate 	uint_t key = *keyp;
16317c478bd9Sstevel@tonic-gate 	struct tsd_thread *tsd = curthread->t_tsd;
16327c478bd9Sstevel@tonic-gate 
16337c478bd9Sstevel@tonic-gate 	if (tsd == NULL)
16347c478bd9Sstevel@tonic-gate 		tsd = curthread->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
16357c478bd9Sstevel@tonic-gate 	if (key && key <= tsd->ts_nkeys && (value = tsd->ts_value[key - 1]))
16367c478bd9Sstevel@tonic-gate 		return (value);
16377c478bd9Sstevel@tonic-gate 	if (key == 0)
16387c478bd9Sstevel@tonic-gate 		tsd_create(keyp, destroy);
16397c478bd9Sstevel@tonic-gate 	(void) tsd_set(*keyp, value = (*allocate)());
16407c478bd9Sstevel@tonic-gate 
16417c478bd9Sstevel@tonic-gate 	return (value);
16427c478bd9Sstevel@tonic-gate }
16437c478bd9Sstevel@tonic-gate 
16447c478bd9Sstevel@tonic-gate /*
16457c478bd9Sstevel@tonic-gate  * Called from thread_exit() to run the destructor function for each tsd
16467c478bd9Sstevel@tonic-gate  *	Locks out tsd_create and tsd_destroy
16477c478bd9Sstevel@tonic-gate  *	Assumes that the destructor *DOES NOT* use tsd
16487c478bd9Sstevel@tonic-gate  */
16497c478bd9Sstevel@tonic-gate void
16507c478bd9Sstevel@tonic-gate tsd_exit(void)
16517c478bd9Sstevel@tonic-gate {
16527c478bd9Sstevel@tonic-gate 	int i;
16537c478bd9Sstevel@tonic-gate 	struct tsd_thread *tsd = curthread->t_tsd;
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate 	if (tsd == NULL)
16567c478bd9Sstevel@tonic-gate 		return;
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 	if (tsd->ts_nkeys == 0) {
16597c478bd9Sstevel@tonic-gate 		kmem_free(tsd, sizeof (*tsd));
16607c478bd9Sstevel@tonic-gate 		curthread->t_tsd = NULL;
16617c478bd9Sstevel@tonic-gate 		return;
16627c478bd9Sstevel@tonic-gate 	}
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate 	/*
16657c478bd9Sstevel@tonic-gate 	 * lock out tsd_create and tsd_destroy, call
16667c478bd9Sstevel@tonic-gate 	 * the destructor, and mark the value as destroyed.
16677c478bd9Sstevel@tonic-gate 	 */
16687c478bd9Sstevel@tonic-gate 	mutex_enter(&tsd_mutex);
16697c478bd9Sstevel@tonic-gate 
16707c478bd9Sstevel@tonic-gate 	for (i = 0; i < tsd->ts_nkeys; i++) {
16717c478bd9Sstevel@tonic-gate 		if (tsd->ts_value[i] && tsd_destructor[i])
16727c478bd9Sstevel@tonic-gate 			(*tsd_destructor[i])(tsd->ts_value[i]);
16737c478bd9Sstevel@tonic-gate 		tsd->ts_value[i] = NULL;
16747c478bd9Sstevel@tonic-gate 	}
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate 	/*
16777c478bd9Sstevel@tonic-gate 	 * remove from linked list of threads with TSD
16787c478bd9Sstevel@tonic-gate 	 */
16797c478bd9Sstevel@tonic-gate 	if (tsd->ts_next)
16807c478bd9Sstevel@tonic-gate 		tsd->ts_next->ts_prev = tsd->ts_prev;
16817c478bd9Sstevel@tonic-gate 	if (tsd->ts_prev)
16827c478bd9Sstevel@tonic-gate 		tsd->ts_prev->ts_next = tsd->ts_next;
16837c478bd9Sstevel@tonic-gate 	if (tsd_list == tsd)
16847c478bd9Sstevel@tonic-gate 		tsd_list = tsd->ts_next;
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate 	mutex_exit(&tsd_mutex);
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate 	/*
16897c478bd9Sstevel@tonic-gate 	 * free up the TSD
16907c478bd9Sstevel@tonic-gate 	 */
16917c478bd9Sstevel@tonic-gate 	kmem_free(tsd->ts_value, tsd->ts_nkeys * sizeof (void *));
16927c478bd9Sstevel@tonic-gate 	kmem_free(tsd, sizeof (struct tsd_thread));
16937c478bd9Sstevel@tonic-gate 	curthread->t_tsd = NULL;
16947c478bd9Sstevel@tonic-gate }
16957c478bd9Sstevel@tonic-gate 
16967c478bd9Sstevel@tonic-gate /*
16977c478bd9Sstevel@tonic-gate  * realloc
16987c478bd9Sstevel@tonic-gate  */
16997c478bd9Sstevel@tonic-gate static void *
17007c478bd9Sstevel@tonic-gate tsd_realloc(void *old, size_t osize, size_t nsize)
17017c478bd9Sstevel@tonic-gate {
17027c478bd9Sstevel@tonic-gate 	void *new;
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate 	new = kmem_zalloc(nsize, KM_SLEEP);
17057c478bd9Sstevel@tonic-gate 	if (old) {
17067c478bd9Sstevel@tonic-gate 		bcopy(old, new, osize);
17077c478bd9Sstevel@tonic-gate 		kmem_free(old, osize);
17087c478bd9Sstevel@tonic-gate 	}
17097c478bd9Sstevel@tonic-gate 	return (new);
17107c478bd9Sstevel@tonic-gate }
17117c478bd9Sstevel@tonic-gate 
17127c478bd9Sstevel@tonic-gate /*
17137c478bd9Sstevel@tonic-gate  * Return non-zero if an interrupt is being serviced.
17147c478bd9Sstevel@tonic-gate  */
17157c478bd9Sstevel@tonic-gate int
17167c478bd9Sstevel@tonic-gate servicing_interrupt()
17177c478bd9Sstevel@tonic-gate {
17189c6cb9fcSsudheer 	int onintr = 0;
17199c6cb9fcSsudheer 
17209c6cb9fcSsudheer 	/* Are we an interrupt thread */
17219c6cb9fcSsudheer 	if (curthread->t_flag & T_INTR_THREAD)
17229c6cb9fcSsudheer 		return (1);
17239c6cb9fcSsudheer 	/* Are we servicing a high level interrupt? */
17249c6cb9fcSsudheer 	if (CPU_ON_INTR(CPU)) {
17259c6cb9fcSsudheer 		kpreempt_disable();
17269c6cb9fcSsudheer 		onintr = CPU_ON_INTR(CPU);
17279c6cb9fcSsudheer 		kpreempt_enable();
17289c6cb9fcSsudheer 	}
17299c6cb9fcSsudheer 	return (onintr);
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate /*
17347c478bd9Sstevel@tonic-gate  * Change the dispatch priority of a thread in the system.
17357c478bd9Sstevel@tonic-gate  * Used when raising or lowering a thread's priority.
17367c478bd9Sstevel@tonic-gate  * (E.g., priority inheritance)
17377c478bd9Sstevel@tonic-gate  *
17387c478bd9Sstevel@tonic-gate  * Since threads are queued according to their priority, we
17397c478bd9Sstevel@tonic-gate  * we must check the thread's state to determine whether it
17407c478bd9Sstevel@tonic-gate  * is on a queue somewhere. If it is, we've got to:
17417c478bd9Sstevel@tonic-gate  *
17427c478bd9Sstevel@tonic-gate  *	o Dequeue the thread.
17437c478bd9Sstevel@tonic-gate  *	o Change its effective priority.
17447c478bd9Sstevel@tonic-gate  *	o Enqueue the thread.
17457c478bd9Sstevel@tonic-gate  *
17467c478bd9Sstevel@tonic-gate  * Assumptions: The thread whose priority we wish to change
17477c478bd9Sstevel@tonic-gate  * must be locked before we call thread_change_(e)pri().
17487c478bd9Sstevel@tonic-gate  * The thread_change(e)pri() function doesn't drop the thread
17497c478bd9Sstevel@tonic-gate  * lock--that must be done by its caller.
17507c478bd9Sstevel@tonic-gate  */
17517c478bd9Sstevel@tonic-gate void
17527c478bd9Sstevel@tonic-gate thread_change_epri(kthread_t *t, pri_t disp_pri)
17537c478bd9Sstevel@tonic-gate {
17547c478bd9Sstevel@tonic-gate 	uint_t	state;
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
17577c478bd9Sstevel@tonic-gate 
17587c478bd9Sstevel@tonic-gate 	/*
17597c478bd9Sstevel@tonic-gate 	 * If the inherited priority hasn't actually changed,
17607c478bd9Sstevel@tonic-gate 	 * just return.
17617c478bd9Sstevel@tonic-gate 	 */
17627c478bd9Sstevel@tonic-gate 	if (t->t_epri == disp_pri)
17637c478bd9Sstevel@tonic-gate 		return;
17647c478bd9Sstevel@tonic-gate 
17657c478bd9Sstevel@tonic-gate 	state = t->t_state;
17667c478bd9Sstevel@tonic-gate 
17677c478bd9Sstevel@tonic-gate 	/*
1768d4204c85Sraf 	 * If it's not on a queue, change the priority with impunity.
17697c478bd9Sstevel@tonic-gate 	 */
1770c97ad5cdSakolb 	if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
17717c478bd9Sstevel@tonic-gate 		t->t_epri = disp_pri;
17727c478bd9Sstevel@tonic-gate 		if (state == TS_ONPROC) {
17737c478bd9Sstevel@tonic-gate 			cpu_t *cp = t->t_disp_queue->disp_cpu;
17747c478bd9Sstevel@tonic-gate 
17757c478bd9Sstevel@tonic-gate 			if (t == cp->cpu_dispthread)
17767c478bd9Sstevel@tonic-gate 				cp->cpu_dispatch_pri = DISP_PRIO(t);
17777c478bd9Sstevel@tonic-gate 		}
1778d4204c85Sraf 	} else if (state == TS_SLEEP) {
17797c478bd9Sstevel@tonic-gate 		/*
17807c478bd9Sstevel@tonic-gate 		 * Take the thread out of its sleep queue.
17817c478bd9Sstevel@tonic-gate 		 * Change the inherited priority.
17827c478bd9Sstevel@tonic-gate 		 * Re-enqueue the thread.
17837c478bd9Sstevel@tonic-gate 		 * Each synchronization object exports a function
17847c478bd9Sstevel@tonic-gate 		 * to do this in an appropriate manner.
17857c478bd9Sstevel@tonic-gate 		 */
17867c478bd9Sstevel@tonic-gate 		SOBJ_CHANGE_EPRI(t->t_sobj_ops, t, disp_pri);
1787c97ad5cdSakolb 	} else if (state == TS_WAIT) {
1788c97ad5cdSakolb 		/*
1789c97ad5cdSakolb 		 * Re-enqueue a thread on the wait queue if its
1790c97ad5cdSakolb 		 * effective priority needs to change.
1791c97ad5cdSakolb 		 */
1792c97ad5cdSakolb 		if (disp_pri != t->t_epri)
1793c97ad5cdSakolb 			waitq_change_pri(t, disp_pri);
17947c478bd9Sstevel@tonic-gate 	} else {
17957c478bd9Sstevel@tonic-gate 		/*
17967c478bd9Sstevel@tonic-gate 		 * The thread is on a run queue.
17977c478bd9Sstevel@tonic-gate 		 * Note: setbackdq() may not put the thread
17987c478bd9Sstevel@tonic-gate 		 * back on the same run queue where it originally
17997c478bd9Sstevel@tonic-gate 		 * resided.
18007c478bd9Sstevel@tonic-gate 		 */
18017c478bd9Sstevel@tonic-gate 		(void) dispdeq(t);
18027c478bd9Sstevel@tonic-gate 		t->t_epri = disp_pri;
18037c478bd9Sstevel@tonic-gate 		setbackdq(t);
18047c478bd9Sstevel@tonic-gate 	}
1805d4204c85Sraf 	schedctl_set_cidpri(t);
1806d4204c85Sraf }
18077c478bd9Sstevel@tonic-gate 
18087c478bd9Sstevel@tonic-gate /*
18097c478bd9Sstevel@tonic-gate  * Function: Change the t_pri field of a thread.
18107c478bd9Sstevel@tonic-gate  * Side Effects: Adjust the thread ordering on a run queue
18117c478bd9Sstevel@tonic-gate  *		 or sleep queue, if necessary.
18127c478bd9Sstevel@tonic-gate  * Returns: 1 if the thread was on a run queue, else 0.
18137c478bd9Sstevel@tonic-gate  */
18147c478bd9Sstevel@tonic-gate int
18157c478bd9Sstevel@tonic-gate thread_change_pri(kthread_t *t, pri_t disp_pri, int front)
18167c478bd9Sstevel@tonic-gate {
18177c478bd9Sstevel@tonic-gate 	uint_t	state;
18187c478bd9Sstevel@tonic-gate 	int	on_rq = 0;
18197c478bd9Sstevel@tonic-gate 
18207c478bd9Sstevel@tonic-gate 	ASSERT(THREAD_LOCK_HELD(t));
18217c478bd9Sstevel@tonic-gate 
18227c478bd9Sstevel@tonic-gate 	state = t->t_state;
18237c478bd9Sstevel@tonic-gate 	THREAD_WILLCHANGE_PRI(t, disp_pri);
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	/*
1826d4204c85Sraf 	 * If it's not on a queue, change the priority with impunity.
18277c478bd9Sstevel@tonic-gate 	 */
1828c97ad5cdSakolb 	if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
18297c478bd9Sstevel@tonic-gate 		t->t_pri = disp_pri;
18307c478bd9Sstevel@tonic-gate 
18317c478bd9Sstevel@tonic-gate 		if (state == TS_ONPROC) {
18327c478bd9Sstevel@tonic-gate 			cpu_t *cp = t->t_disp_queue->disp_cpu;
18337c478bd9Sstevel@tonic-gate 
18347c478bd9Sstevel@tonic-gate 			if (t == cp->cpu_dispthread)
18357c478bd9Sstevel@tonic-gate 				cp->cpu_dispatch_pri = DISP_PRIO(t);
18367c478bd9Sstevel@tonic-gate 		}
1837d4204c85Sraf 	} else if (state == TS_SLEEP) {
18387c478bd9Sstevel@tonic-gate 		/*
18397c478bd9Sstevel@tonic-gate 		 * If the priority has changed, take the thread out of
18407c478bd9Sstevel@tonic-gate 		 * its sleep queue and change the priority.
18417c478bd9Sstevel@tonic-gate 		 * Re-enqueue the thread.
18427c478bd9Sstevel@tonic-gate 		 * Each synchronization object exports a function
18437c478bd9Sstevel@tonic-gate 		 * to do this in an appropriate manner.
18447c478bd9Sstevel@tonic-gate 		 */
18457c478bd9Sstevel@tonic-gate 		if (disp_pri != t->t_pri)
18467c478bd9Sstevel@tonic-gate 			SOBJ_CHANGE_PRI(t->t_sobj_ops, t, disp_pri);
1847c97ad5cdSakolb 	} else if (state == TS_WAIT) {
1848c97ad5cdSakolb 		/*
1849c97ad5cdSakolb 		 * Re-enqueue a thread on the wait queue if its
1850c97ad5cdSakolb 		 * priority needs to change.
1851c97ad5cdSakolb 		 */
1852c97ad5cdSakolb 		if (disp_pri != t->t_pri)
1853c97ad5cdSakolb 			waitq_change_pri(t, disp_pri);
18547c478bd9Sstevel@tonic-gate 	} else {
18557c478bd9Sstevel@tonic-gate 		/*
18567c478bd9Sstevel@tonic-gate 		 * The thread is on a run queue.
18577c478bd9Sstevel@tonic-gate 		 * Note: setbackdq() may not put the thread
18587c478bd9Sstevel@tonic-gate 		 * back on the same run queue where it originally
18597c478bd9Sstevel@tonic-gate 		 * resided.
18607c478bd9Sstevel@tonic-gate 		 *
18617c478bd9Sstevel@tonic-gate 		 * We still requeue the thread even if the priority
18627c478bd9Sstevel@tonic-gate 		 * is unchanged to preserve round-robin (and other)
18637c478bd9Sstevel@tonic-gate 		 * effects between threads of the same priority.
18647c478bd9Sstevel@tonic-gate 		 */
18657c478bd9Sstevel@tonic-gate 		on_rq = dispdeq(t);
18667c478bd9Sstevel@tonic-gate 		ASSERT(on_rq);
18677c478bd9Sstevel@tonic-gate 		t->t_pri = disp_pri;
18687c478bd9Sstevel@tonic-gate 		if (front) {
18697c478bd9Sstevel@tonic-gate 			setfrontdq(t);
18707c478bd9Sstevel@tonic-gate 		} else {
18717c478bd9Sstevel@tonic-gate 			setbackdq(t);
18727c478bd9Sstevel@tonic-gate 		}
18737c478bd9Sstevel@tonic-gate 	}
1874d4204c85Sraf 	schedctl_set_cidpri(t);
18757c478bd9Sstevel@tonic-gate 	return (on_rq);
18767c478bd9Sstevel@tonic-gate }
1877bff31d89SPhilippe Jung 
1878bff31d89SPhilippe Jung /*
1879bff31d89SPhilippe Jung  * Tunable kmem_stackinfo is set, fill the kernel thread stack with a
1880bff31d89SPhilippe Jung  * specific pattern.
1881bff31d89SPhilippe Jung  */
1882bff31d89SPhilippe Jung static void
1883bff31d89SPhilippe Jung stkinfo_begin(kthread_t *t)
1884bff31d89SPhilippe Jung {
1885bff31d89SPhilippe Jung 	caddr_t	start;	/* stack start */
1886bff31d89SPhilippe Jung 	caddr_t	end;	/* stack end  */
1887bff31d89SPhilippe Jung 	uint64_t *ptr;	/* pattern pointer */
1888bff31d89SPhilippe Jung 
1889bff31d89SPhilippe Jung 	/*
1890bff31d89SPhilippe Jung 	 * Stack grows up or down, see thread_create(),
1891bff31d89SPhilippe Jung 	 * compute stack memory area start and end (start < end).
1892bff31d89SPhilippe Jung 	 */
1893bff31d89SPhilippe Jung 	if (t->t_stk > t->t_stkbase) {
1894bff31d89SPhilippe Jung 		/* stack grows down */
1895bff31d89SPhilippe Jung 		start = t->t_stkbase;
1896bff31d89SPhilippe Jung 		end = t->t_stk;
1897bff31d89SPhilippe Jung 	} else {
1898bff31d89SPhilippe Jung 		/* stack grows up */
1899bff31d89SPhilippe Jung 		start = t->t_stk;
1900bff31d89SPhilippe Jung 		end = t->t_stkbase;
1901bff31d89SPhilippe Jung 	}
1902bff31d89SPhilippe Jung 
1903bff31d89SPhilippe Jung 	/*
1904bff31d89SPhilippe Jung 	 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
1905bff31d89SPhilippe Jung 	 * alignement for start and end in stack area boundaries
1906bff31d89SPhilippe Jung 	 * (protection against corrupt t_stkbase/t_stk data).
1907bff31d89SPhilippe Jung 	 */
1908bff31d89SPhilippe Jung 	if ((((uintptr_t)start) & 0x7) != 0) {
1909bff31d89SPhilippe Jung 		start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
1910bff31d89SPhilippe Jung 	}
1911bff31d89SPhilippe Jung 	end = (caddr_t)(((uintptr_t)end) & (~0x7));
1912bff31d89SPhilippe Jung 
1913bff31d89SPhilippe Jung 	if ((end <= start) || (end - start) > (1024 * 1024)) {
1914bff31d89SPhilippe Jung 		/* negative or stack size > 1 meg, assume bogus */
1915bff31d89SPhilippe Jung 		return;
1916bff31d89SPhilippe Jung 	}
1917bff31d89SPhilippe Jung 
1918bff31d89SPhilippe Jung 	/* fill stack area with a pattern (instead of zeros) */
1919bff31d89SPhilippe Jung 	ptr = (uint64_t *)((void *)start);
1920bff31d89SPhilippe Jung 	while (ptr < (uint64_t *)((void *)end)) {
1921bff31d89SPhilippe Jung 		*ptr++ = KMEM_STKINFO_PATTERN;
1922bff31d89SPhilippe Jung 	}
1923bff31d89SPhilippe Jung }
1924bff31d89SPhilippe Jung 
1925bff31d89SPhilippe Jung 
1926bff31d89SPhilippe Jung /*
1927bff31d89SPhilippe Jung  * Tunable kmem_stackinfo is set, create stackinfo log if doesn't already exist,
1928bff31d89SPhilippe Jung  * compute the percentage of kernel stack really used, and set in the log
1929bff31d89SPhilippe Jung  * if it's the latest highest percentage.
1930bff31d89SPhilippe Jung  */
1931bff31d89SPhilippe Jung static void
1932bff31d89SPhilippe Jung stkinfo_end(kthread_t *t)
1933bff31d89SPhilippe Jung {
1934bff31d89SPhilippe Jung 	caddr_t	start;	/* stack start */
1935bff31d89SPhilippe Jung 	caddr_t	end;	/* stack end  */
1936bff31d89SPhilippe Jung 	uint64_t *ptr;	/* pattern pointer */
1937bff31d89SPhilippe Jung 	size_t stksz;	/* stack size */
1938bff31d89SPhilippe Jung 	size_t smallest = 0;
1939bff31d89SPhilippe Jung 	size_t percent = 0;
1940bff31d89SPhilippe Jung 	uint_t index = 0;
1941bff31d89SPhilippe Jung 	uint_t i;
1942bff31d89SPhilippe Jung 	static size_t smallest_percent = (size_t)-1;
1943bff31d89SPhilippe Jung 	static uint_t full = 0;
1944bff31d89SPhilippe Jung 
1945bff31d89SPhilippe Jung 	/* create the stackinfo log, if doesn't already exist */
1946bff31d89SPhilippe Jung 	mutex_enter(&kmem_stkinfo_lock);
1947bff31d89SPhilippe Jung 	if (kmem_stkinfo_log == NULL) {
1948bff31d89SPhilippe Jung 		kmem_stkinfo_log = (kmem_stkinfo_t *)
1949bff31d89SPhilippe Jung 		    kmem_zalloc(KMEM_STKINFO_LOG_SIZE *
1950bff31d89SPhilippe Jung 		    (sizeof (kmem_stkinfo_t)), KM_NOSLEEP);
1951bff31d89SPhilippe Jung 		if (kmem_stkinfo_log == NULL) {
1952bff31d89SPhilippe Jung 			mutex_exit(&kmem_stkinfo_lock);
1953bff31d89SPhilippe Jung 			return;
1954bff31d89SPhilippe Jung 		}
1955bff31d89SPhilippe Jung 	}
1956bff31d89SPhilippe Jung 	mutex_exit(&kmem_stkinfo_lock);
1957bff31d89SPhilippe Jung 
1958bff31d89SPhilippe Jung 	/*
1959bff31d89SPhilippe Jung 	 * Stack grows up or down, see thread_create(),
1960bff31d89SPhilippe Jung 	 * compute stack memory area start and end (start < end).
1961bff31d89SPhilippe Jung 	 */
1962bff31d89SPhilippe Jung 	if (t->t_stk > t->t_stkbase) {
1963bff31d89SPhilippe Jung 		/* stack grows down */
1964bff31d89SPhilippe Jung 		start = t->t_stkbase;
1965bff31d89SPhilippe Jung 		end = t->t_stk;
1966bff31d89SPhilippe Jung 	} else {
1967bff31d89SPhilippe Jung 		/* stack grows up */
1968bff31d89SPhilippe Jung 		start = t->t_stk;
1969bff31d89SPhilippe Jung 		end = t->t_stkbase;
1970bff31d89SPhilippe Jung 	}
1971bff31d89SPhilippe Jung 
1972bff31d89SPhilippe Jung 	/* stack size as found in kthread_t */
1973bff31d89SPhilippe Jung 	stksz = end - start;
1974bff31d89SPhilippe Jung 
1975bff31d89SPhilippe Jung 	/*
1976bff31d89SPhilippe Jung 	 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
1977bff31d89SPhilippe Jung 	 * alignement for start and end in stack area boundaries
1978bff31d89SPhilippe Jung 	 * (protection against corrupt t_stkbase/t_stk data).
1979bff31d89SPhilippe Jung 	 */
1980bff31d89SPhilippe Jung 	if ((((uintptr_t)start) & 0x7) != 0) {
1981bff31d89SPhilippe Jung 		start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
1982bff31d89SPhilippe Jung 	}
1983bff31d89SPhilippe Jung 	end = (caddr_t)(((uintptr_t)end) & (~0x7));
1984bff31d89SPhilippe Jung 
1985bff31d89SPhilippe Jung 	if ((end <= start) || (end - start) > (1024 * 1024)) {
1986bff31d89SPhilippe Jung 		/* negative or stack size > 1 meg, assume bogus */
1987bff31d89SPhilippe Jung 		return;
1988bff31d89SPhilippe Jung 	}
1989bff31d89SPhilippe Jung 
1990bff31d89SPhilippe Jung 	/* search until no pattern in the stack */
1991bff31d89SPhilippe Jung 	if (t->t_stk > t->t_stkbase) {
1992bff31d89SPhilippe Jung 		/* stack grows down */
1993bff31d89SPhilippe Jung #if defined(__i386) || defined(__amd64)
1994bff31d89SPhilippe Jung 		/*
1995bff31d89SPhilippe Jung 		 * 6 longs are pushed on stack, see thread_load(). Skip
1996bff31d89SPhilippe Jung 		 * them, so if kthread has never run, percent is zero.
1997bff31d89SPhilippe Jung 		 * 8 bytes alignement is preserved for a 32 bit kernel,
1998bff31d89SPhilippe Jung 		 * 6 x 4 = 24, 24 is a multiple of 8.
1999bff31d89SPhilippe Jung 		 *
2000bff31d89SPhilippe Jung 		 */
2001bff31d89SPhilippe Jung 		end -= (6 * sizeof (long));
2002bff31d89SPhilippe Jung #endif
2003bff31d89SPhilippe Jung 		ptr = (uint64_t *)((void *)start);
2004bff31d89SPhilippe Jung 		while (ptr < (uint64_t *)((void *)end)) {
2005bff31d89SPhilippe Jung 			if (*ptr != KMEM_STKINFO_PATTERN) {
2006bff31d89SPhilippe Jung 				percent = stkinfo_percent(end,
2007bff31d89SPhilippe Jung 				    start, (caddr_t)ptr);
2008bff31d89SPhilippe Jung 				break;
2009bff31d89SPhilippe Jung 			}
2010bff31d89SPhilippe Jung 			ptr++;
2011bff31d89SPhilippe Jung 		}
2012bff31d89SPhilippe Jung 	} else {
2013bff31d89SPhilippe Jung 		/* stack grows up */
2014bff31d89SPhilippe Jung 		ptr = (uint64_t *)((void *)end);
2015bff31d89SPhilippe Jung 		ptr--;
2016bff31d89SPhilippe Jung 		while (ptr >= (uint64_t *)((void *)start)) {
2017bff31d89SPhilippe Jung 			if (*ptr != KMEM_STKINFO_PATTERN) {
2018bff31d89SPhilippe Jung 				percent = stkinfo_percent(start,
2019bff31d89SPhilippe Jung 				    end, (caddr_t)ptr);
2020bff31d89SPhilippe Jung 				break;
2021bff31d89SPhilippe Jung 			}
2022bff31d89SPhilippe Jung 			ptr--;
2023bff31d89SPhilippe Jung 		}
2024bff31d89SPhilippe Jung 	}
2025bff31d89SPhilippe Jung 
2026bff31d89SPhilippe Jung 	DTRACE_PROBE3(stack__usage, kthread_t *, t,
2027bff31d89SPhilippe Jung 	    size_t, stksz, size_t, percent);
2028bff31d89SPhilippe Jung 
2029bff31d89SPhilippe Jung 	if (percent == 0) {
2030bff31d89SPhilippe Jung 		return;
2031bff31d89SPhilippe Jung 	}
2032bff31d89SPhilippe Jung 
2033bff31d89SPhilippe Jung 	mutex_enter(&kmem_stkinfo_lock);
2034bff31d89SPhilippe Jung 	if (full == KMEM_STKINFO_LOG_SIZE && percent < smallest_percent) {
2035bff31d89SPhilippe Jung 		/*
2036bff31d89SPhilippe Jung 		 * The log is full and already contains the highest values
2037bff31d89SPhilippe Jung 		 */
2038bff31d89SPhilippe Jung 		mutex_exit(&kmem_stkinfo_lock);
2039bff31d89SPhilippe Jung 		return;
2040bff31d89SPhilippe Jung 	}
2041bff31d89SPhilippe Jung 
2042bff31d89SPhilippe Jung 	/* keep a log of the highest used stack */
2043bff31d89SPhilippe Jung 	for (i = 0; i < KMEM_STKINFO_LOG_SIZE; i++) {
2044bff31d89SPhilippe Jung 		if (kmem_stkinfo_log[i].percent == 0) {
2045bff31d89SPhilippe Jung 			index = i;
2046bff31d89SPhilippe Jung 			full++;
2047bff31d89SPhilippe Jung 			break;
2048bff31d89SPhilippe Jung 		}
2049bff31d89SPhilippe Jung 		if (smallest == 0) {
2050bff31d89SPhilippe Jung 			smallest = kmem_stkinfo_log[i].percent;
2051bff31d89SPhilippe Jung 			index = i;
2052bff31d89SPhilippe Jung 			continue;
2053bff31d89SPhilippe Jung 		}
2054bff31d89SPhilippe Jung 		if (kmem_stkinfo_log[i].percent < smallest) {
2055bff31d89SPhilippe Jung 			smallest = kmem_stkinfo_log[i].percent;
2056bff31d89SPhilippe Jung 			index = i;
2057bff31d89SPhilippe Jung 		}
2058bff31d89SPhilippe Jung 	}
2059bff31d89SPhilippe Jung 
2060bff31d89SPhilippe Jung 	if (percent >= kmem_stkinfo_log[index].percent) {
2061bff31d89SPhilippe Jung 		kmem_stkinfo_log[index].kthread = (caddr_t)t;
2062bff31d89SPhilippe Jung 		kmem_stkinfo_log[index].t_startpc = (caddr_t)t->t_startpc;
2063bff31d89SPhilippe Jung 		kmem_stkinfo_log[index].start = start;
2064bff31d89SPhilippe Jung 		kmem_stkinfo_log[index].stksz = stksz;
2065bff31d89SPhilippe Jung 		kmem_stkinfo_log[index].percent = percent;
2066bff31d89SPhilippe Jung 		kmem_stkinfo_log[index].t_tid = t->t_tid;
2067bff31d89SPhilippe Jung 		kmem_stkinfo_log[index].cmd[0] = '\0';
2068bff31d89SPhilippe Jung 		if (t->t_tid != 0) {
2069bff31d89SPhilippe Jung 			stksz = strlen((t->t_procp)->p_user.u_comm);
2070bff31d89SPhilippe Jung 			if (stksz >= KMEM_STKINFO_STR_SIZE) {
2071bff31d89SPhilippe Jung 				stksz = KMEM_STKINFO_STR_SIZE - 1;
2072bff31d89SPhilippe Jung 				kmem_stkinfo_log[index].cmd[stksz] = '\0';
2073bff31d89SPhilippe Jung 			} else {
2074bff31d89SPhilippe Jung 				stksz += 1;
2075bff31d89SPhilippe Jung 			}
2076bff31d89SPhilippe Jung 			(void) memcpy(kmem_stkinfo_log[index].cmd,
2077bff31d89SPhilippe Jung 			    (t->t_procp)->p_user.u_comm, stksz);
2078bff31d89SPhilippe Jung 		}
2079bff31d89SPhilippe Jung 		if (percent < smallest_percent) {
2080bff31d89SPhilippe Jung 			smallest_percent = percent;
2081bff31d89SPhilippe Jung 		}
2082bff31d89SPhilippe Jung 	}
2083bff31d89SPhilippe Jung 	mutex_exit(&kmem_stkinfo_lock);
2084bff31d89SPhilippe Jung }
2085bff31d89SPhilippe Jung 
2086bff31d89SPhilippe Jung /*
2087bff31d89SPhilippe Jung  * Tunable kmem_stackinfo is set, compute stack utilization percentage.
2088bff31d89SPhilippe Jung  */
2089bff31d89SPhilippe Jung static size_t
2090bff31d89SPhilippe Jung stkinfo_percent(caddr_t t_stk, caddr_t t_stkbase, caddr_t sp)
2091bff31d89SPhilippe Jung {
2092bff31d89SPhilippe Jung 	size_t percent;
2093bff31d89SPhilippe Jung 	size_t s;
2094bff31d89SPhilippe Jung 
2095bff31d89SPhilippe Jung 	if (t_stk > t_stkbase) {
2096bff31d89SPhilippe Jung 		/* stack grows down */
2097bff31d89SPhilippe Jung 		if (sp > t_stk) {
2098bff31d89SPhilippe Jung 			return (0);
2099bff31d89SPhilippe Jung 		}
2100bff31d89SPhilippe Jung 		if (sp < t_stkbase) {
2101bff31d89SPhilippe Jung 			return (100);
2102bff31d89SPhilippe Jung 		}
2103bff31d89SPhilippe Jung 		percent = t_stk - sp + 1;
2104bff31d89SPhilippe Jung 		s = t_stk - t_stkbase + 1;
2105bff31d89SPhilippe Jung 	} else {
2106bff31d89SPhilippe Jung 		/* stack grows up */
2107bff31d89SPhilippe Jung 		if (sp < t_stk) {
2108bff31d89SPhilippe Jung 			return (0);
2109bff31d89SPhilippe Jung 		}
2110bff31d89SPhilippe Jung 		if (sp > t_stkbase) {
2111bff31d89SPhilippe Jung 			return (100);
2112bff31d89SPhilippe Jung 		}
2113bff31d89SPhilippe Jung 		percent = sp - t_stk + 1;
2114bff31d89SPhilippe Jung 		s = t_stkbase - t_stk + 1;
2115bff31d89SPhilippe Jung 	}
2116bff31d89SPhilippe Jung 	percent = ((100 * percent) / s) + 1;
2117bff31d89SPhilippe Jung 	if (percent > 100) {
2118bff31d89SPhilippe Jung 		percent = 100;
2119bff31d89SPhilippe Jung 	}
2120bff31d89SPhilippe Jung 	return (percent);
2121bff31d89SPhilippe Jung }
2122