Home | History | Annotate | Download | only in disp
      1 /*
      2  * CDDL HEADER START
      3  *
      4  * The contents of this file are subject to the terms of the
      5  * Common Development and Distribution License (the "License").
      6  * You may not use this file except in compliance with the License.
      7  *
      8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
      9  * or http://www.opensolaris.org/os/licensing.
     10  * See the License for the specific language governing permissions
     11  * and limitations under the License.
     12  *
     13  * When distributing Covered Code, include this CDDL HEADER in each
     14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
     15  * If applicable, add the following below this CDDL HEADER, with the
     16  * fields enclosed by brackets "[]" replaced with your own identifying
     17  * information: Portions Copyright [yyyy] [name of copyright owner]
     18  *
     19  * CDDL HEADER END
     20  */
     21 
     22 /*
     23  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
     24  * Use is subject to license terms.
     25  */
     26 
     27 #include <sys/types.h>
     28 #include <sys/param.h>
     29 #include <sys/sysmacros.h>
     30 #include <sys/signal.h>
     31 #include <sys/stack.h>
     32 #include <sys/pcb.h>
     33 #include <sys/user.h>
     34 #include <sys/systm.h>
     35 #include <sys/sysinfo.h>
     36 #include <sys/errno.h>
     37 #include <sys/cmn_err.h>
     38 #include <sys/cred.h>
     39 #include <sys/resource.h>
     40 #include <sys/task.h>
     41 #include <sys/project.h>
     42 #include <sys/proc.h>
     43 #include <sys/debug.h>
     44 #include <sys/disp.h>
     45 #include <sys/class.h>
     46 #include <vm/seg_kmem.h>
     47 #include <vm/seg_kp.h>
     48 #include <sys/machlock.h>
     49 #include <sys/kmem.h>
     50 #include <sys/varargs.h>
     51 #include <sys/turnstile.h>
     52 #include <sys/poll.h>
     53 #include <sys/vtrace.h>
     54 #include <sys/callb.h>
     55 #include <c2/audit.h>
     56 #include <sys/tnf.h>
     57 #include <sys/sobject.h>
     58 #include <sys/cpupart.h>
     59 #include <sys/pset.h>
     60 #include <sys/door.h>
     61 #include <sys/spl.h>
     62 #include <sys/copyops.h>
     63 #include <sys/rctl.h>
     64 #include <sys/brand.h>
     65 #include <sys/pool.h>
     66 #include <sys/zone.h>
     67 #include <sys/tsol/label.h>
     68 #include <sys/tsol/tndb.h>
     69 #include <sys/cpc_impl.h>
     70 #include <sys/sdt.h>
     71 #include <sys/reboot.h>
     72 #include <sys/kdi.h>
     73 #include <sys/schedctl.h>
     74 #include <sys/waitq.h>
     75 #include <sys/cpucaps.h>
     76 #include <sys/kiconv.h>
     77 
     78 struct kmem_cache *thread_cache;	/* cache of free threads */
     79 struct kmem_cache *lwp_cache;		/* cache of free lwps */
     80 struct kmem_cache *turnstile_cache;	/* cache of free turnstiles */
     81 
     82 /*
     83  * allthreads is only for use by kmem_readers.  All kernel loops can use
     84  * the current thread as a start/end point.
     85  */
     86 static kthread_t *allthreads = &t0;	/* circular list of all threads */
     87 
     88 static kcondvar_t reaper_cv;		/* synchronization var */
     89 kthread_t	*thread_deathrow;	/* circular list of reapable threads */
     90 kthread_t	*lwp_deathrow;		/* circular list of reapable threads */
     91 kmutex_t	reaplock;		/* protects lwp and thread deathrows */
     92 int	thread_reapcnt = 0;		/* number of threads on deathrow */
     93 int	lwp_reapcnt = 0;		/* number of lwps on deathrow */
     94 int	reaplimit = 16;			/* delay reaping until reaplimit */
     95 
     96 thread_free_lock_t	*thread_free_lock;
     97 					/* protects tick thread from reaper */
     98 
     99 extern int nthread;
    100 
    101 /* System Scheduling classes. */
    102 id_t	syscid;				/* system scheduling class ID */
    103 id_t	sysdccid = CLASS_UNUSED;	/* reset when SDC loads */
    104 
    105 void	*segkp_thread;			/* cookie for segkp pool */
    106 
    107 int lwp_cache_sz = 32;
    108 int t_cache_sz = 8;
    109 static kt_did_t next_t_id = 1;
    110 
    111 /* Default mode for thread binding to CPUs and processor sets */
    112 int default_binding_mode = TB_ALLHARD;
    113 
    114 /*
    115  * Min/Max stack sizes for stack size parameters
    116  */
    117 #define	MAX_STKSIZE	(32 * DEFAULTSTKSZ)
    118 #define	MIN_STKSIZE	DEFAULTSTKSZ
    119 
    120 /*
    121  * default_stksize overrides lwp_default_stksize if it is set.
    122  */
    123 int	default_stksize;
    124 int	lwp_default_stksize;
    125 
    126 static zone_key_t zone_thread_key;
    127 
    128 unsigned int kmem_stackinfo;		/* stackinfo feature on-off */
    129 kmem_stkinfo_t *kmem_stkinfo_log;	/* stackinfo circular log */
    130 static kmutex_t kmem_stkinfo_lock;	/* protects kmem_stkinfo_log */
    131 
    132 /*
    133  * forward declarations for internal thread specific data (tsd)
    134  */
    135 static void *tsd_realloc(void *, size_t, size_t);
    136 
    137 void thread_reaper(void);
    138 
    139 /* forward declarations for stackinfo feature */
    140 static void stkinfo_begin(kthread_t *);
    141 static void stkinfo_end(kthread_t *);
    142 static size_t stkinfo_percent(caddr_t, caddr_t, caddr_t);
    143 
    144 /*ARGSUSED*/
    145 static int
    146 turnstile_constructor(void *buf, void *cdrarg, int kmflags)
    147 {
    148 	bzero(buf, sizeof (turnstile_t));
    149 	return (0);
    150 }
    151 
    152 /*ARGSUSED*/
    153 static void
    154 turnstile_destructor(void *buf, void *cdrarg)
    155 {
    156 	turnstile_t *ts = buf;
    157 
    158 	ASSERT(ts->ts_free == NULL);
    159 	ASSERT(ts->ts_waiters == 0);
    160 	ASSERT(ts->ts_inheritor == NULL);
    161 	ASSERT(ts->ts_sleepq[0].sq_first == NULL);
    162 	ASSERT(ts->ts_sleepq[1].sq_first == NULL);
    163 }
    164 
    165 void
    166 thread_init(void)
    167 {
    168 	kthread_t *tp;
    169 	extern char sys_name[];
    170 	extern void idle();
    171 	struct cpu *cpu = CPU;
    172 	int i;
    173 	kmutex_t *lp;
    174 
    175 	mutex_init(&reaplock, NULL, MUTEX_SPIN, (void *)ipltospl(DISP_LEVEL));
    176 	thread_free_lock =
    177 	    kmem_alloc(sizeof (thread_free_lock_t) * THREAD_FREE_NUM, KM_SLEEP);
    178 	for (i = 0; i < THREAD_FREE_NUM; i++) {
    179 		lp = &thread_free_lock[i].tf_lock;
    180 		mutex_init(lp, NULL, MUTEX_DEFAULT, NULL);
    181 	}
    182 
    183 #if defined(__i386) || defined(__amd64)
    184 	thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
    185 	    PTR24_ALIGN, NULL, NULL, NULL, NULL, NULL, 0);
    186 
    187 	/*
    188 	 * "struct _klwp" includes a "struct pcb", which includes a
    189 	 * "struct fpu", which needs to be 16-byte aligned on amd64
    190 	 * (and even on i386 for fxsave/fxrstor).
    191 	 */
    192 	lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
    193 	    16, NULL, NULL, NULL, NULL, NULL, 0);
    194 #else
    195 	/*
    196 	 * Allocate thread structures from static_arena.  This prevents
    197 	 * issues where a thread tries to relocate its own thread
    198 	 * structure and touches it after the mapping has been suspended.
    199 	 */
    200 	thread_cache = kmem_cache_create("thread_cache", sizeof (kthread_t),
    201 	    PTR24_ALIGN, NULL, NULL, NULL, NULL, static_arena, 0);
    202 
    203 	lwp_stk_cache_init();
    204 
    205 	lwp_cache = kmem_cache_create("lwp_cache", sizeof (klwp_t),
    206 	    0, NULL, NULL, NULL, NULL, NULL, 0);
    207 #endif
    208 
    209 	turnstile_cache = kmem_cache_create("turnstile_cache",
    210 	    sizeof (turnstile_t), 0,
    211 	    turnstile_constructor, turnstile_destructor, NULL, NULL, NULL, 0);
    212 
    213 	label_init();
    214 	cred_init();
    215 
    216 	/*
    217 	 * Initialize various resource management facilities.
    218 	 */
    219 	rctl_init();
    220 	cpucaps_init();
    221 	/*
    222 	 * Zone_init() should be called before project_init() so that project ID
    223 	 * for the first project is initialized correctly.
    224 	 */
    225 	zone_init();
    226 	project_init();
    227 	brand_init();
    228 	kiconv_init();
    229 	task_init();
    230 	tcache_init();
    231 	pool_init();
    232 
    233 	curthread->t_ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
    234 
    235 	/*
    236 	 * Originally, we had two parameters to set default stack
    237 	 * size: one for lwp's (lwp_default_stksize), and one for
    238 	 * kernel-only threads (DEFAULTSTKSZ, a.k.a. _defaultstksz).
    239 	 * Now we have a third parameter that overrides both if it is
    240 	 * set to a legal stack size, called default_stksize.
    241 	 */
    242 
    243 	if (default_stksize == 0) {
    244 		default_stksize = DEFAULTSTKSZ;
    245 	} else if (default_stksize % PAGESIZE != 0 ||
    246 	    default_stksize > MAX_STKSIZE ||
    247 	    default_stksize < MIN_STKSIZE) {
    248 		cmn_err(CE_WARN, "Illegal stack size. Using %d",
    249 		    (int)DEFAULTSTKSZ);
    250 		default_stksize = DEFAULTSTKSZ;
    251 	} else {
    252 		lwp_default_stksize = default_stksize;
    253 	}
    254 
    255 	if (lwp_default_stksize == 0) {
    256 		lwp_default_stksize = default_stksize;
    257 	} else if (lwp_default_stksize % PAGESIZE != 0 ||
    258 	    lwp_default_stksize > MAX_STKSIZE ||
    259 	    lwp_default_stksize < MIN_STKSIZE) {
    260 		cmn_err(CE_WARN, "Illegal stack size. Using %d",
    261 		    default_stksize);
    262 		lwp_default_stksize = default_stksize;
    263 	}
    264 
    265 	segkp_lwp = segkp_cache_init(segkp, lwp_cache_sz,
    266 	    lwp_default_stksize,
    267 	    (KPD_NOWAIT | KPD_HASREDZONE | KPD_LOCKED));
    268 
    269 	segkp_thread = segkp_cache_init(segkp, t_cache_sz,
    270 	    default_stksize, KPD_HASREDZONE | KPD_LOCKED | KPD_NO_ANON);
    271 
    272 	(void) getcid(sys_name, &syscid);
    273 	curthread->t_cid = syscid;	/* current thread is t0 */
    274 
    275 	/*
    276 	 * Set up the first CPU's idle thread.
    277 	 * It runs whenever the CPU has nothing worthwhile to do.
    278 	 */
    279 	tp = thread_create(NULL, 0, idle, NULL, 0, &p0, TS_STOPPED, -1);
    280 	cpu->cpu_idle_thread = tp;
    281 	tp->t_preempt = 1;
    282 	tp->t_disp_queue = cpu->cpu_disp;
    283 	ASSERT(tp->t_disp_queue != NULL);
    284 	tp->t_bound_cpu = cpu;
    285 	tp->t_affinitycnt = 1;
    286 
    287 	/*
    288 	 * Registering a thread in the callback table is usually
    289 	 * done in the initialization code of the thread. In this
    290 	 * case, we do it right after thread creation to avoid
    291 	 * blocking idle thread while registering itself. It also
    292 	 * avoids the possibility of reregistration in case a CPU
    293 	 * restarts its idle thread.
    294 	 */
    295 	CALLB_CPR_INIT_SAFE(tp, "idle");
    296 
    297 	/*
    298 	 * Create the thread_reaper daemon. From this point on, exited
    299 	 * threads will get reaped.
    300 	 */
    301 	(void) thread_create(NULL, 0, (void (*)())thread_reaper,
    302 	    NULL, 0, &p0, TS_RUN, minclsyspri);
    303 
    304 	/*
    305 	 * Finish initializing the kernel memory allocator now that
    306 	 * thread_create() is available.
    307 	 */
    308 	kmem_thread_init();
    309 
    310 	if (boothowto & RB_DEBUG)
    311 		kdi_dvec_thravail();
    312 }
    313 
    314 /*
    315  * Create a thread.
    316  *
    317  * thread_create() blocks for memory if necessary.  It never fails.
    318  *
    319  * If stk is NULL, the thread is created at the base of the stack
    320  * and cannot be swapped.
    321  */
    322 kthread_t *
    323 thread_create(
    324 	caddr_t	stk,
    325 	size_t	stksize,
    326 	void	(*proc)(),
    327 	void	*arg,
    328 	size_t	len,
    329 	proc_t	 *pp,
    330 	int	state,
    331 	pri_t	pri)
    332 {
    333 	kthread_t *t;
    334 	extern struct classfuncs sys_classfuncs;
    335 	turnstile_t *ts;
    336 
    337 	/*
    338 	 * Every thread keeps a turnstile around in case it needs to block.
    339 	 * The only reason the turnstile is not simply part of the thread
    340 	 * structure is that we may have to break the association whenever
    341 	 * more than one thread blocks on a given synchronization object.
    342 	 * From a memory-management standpoint, turnstiles are like the
    343 	 * "attached mblks" that hang off dblks in the streams allocator.
    344 	 */
    345 	ts = kmem_cache_alloc(turnstile_cache, KM_SLEEP);
    346 
    347 	if (stk == NULL) {
    348 		/*
    349 		 * alloc both thread and stack in segkp chunk
    350 		 */
    351 
    352 		if (stksize < default_stksize)
    353 			stksize = default_stksize;
    354 
    355 		if (stksize == default_stksize) {
    356 			stk = (caddr_t)segkp_cache_get(segkp_thread);
    357 		} else {
    358 			stksize = roundup(stksize, PAGESIZE);
    359 			stk = (caddr_t)segkp_get(segkp, stksize,
    360 			    (KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED));
    361 		}
    362 
    363 		ASSERT(stk != NULL);
    364 
    365 		/*
    366 		 * The machine-dependent mutex code may require that
    367 		 * thread pointers (since they may be used for mutex owner
    368 		 * fields) have certain alignment requirements.
    369 		 * PTR24_ALIGN is the size of the alignment quanta.
    370 		 * XXX - assumes stack grows toward low addresses.
    371 		 */
    372 		if (stksize <= sizeof (kthread_t) + PTR24_ALIGN)
    373 			cmn_err(CE_PANIC, "thread_create: proposed stack size"
    374 			    " too small to hold thread.");
    375 #ifdef STACK_GROWTH_DOWN
    376 		stksize -= SA(sizeof (kthread_t) + PTR24_ALIGN - 1);
    377 		stksize &= -PTR24_ALIGN;	/* make thread aligned */
    378 		t = (kthread_t *)(stk + stksize);
    379 		bzero(t, sizeof (kthread_t));
    380 		if (audit_active)
    381 			audit_thread_create(t);
    382 		t->t_stk = stk + stksize;
    383 		t->t_stkbase = stk;
    384 #else	/* stack grows to larger addresses */
    385 		stksize -= SA(sizeof (kthread_t));
    386 		t = (kthread_t *)(stk);
    387 		bzero(t, sizeof (kthread_t));
    388 		t->t_stk = stk + sizeof (kthread_t);
    389 		t->t_stkbase = stk + stksize + sizeof (kthread_t);
    390 #endif	/* STACK_GROWTH_DOWN */
    391 		t->t_flag |= T_TALLOCSTK;
    392 		t->t_swap = stk;
    393 	} else {
    394 		t = kmem_cache_alloc(thread_cache, KM_SLEEP);
    395 		bzero(t, sizeof (kthread_t));
    396 		ASSERT(((uintptr_t)t & (PTR24_ALIGN - 1)) == 0);
    397 		if (audit_active)
    398 			audit_thread_create(t);
    399 		/*
    400 		 * Initialize t_stk to the kernel stack pointer to use
    401 		 * upon entry to the kernel
    402 		 */
    403 #ifdef STACK_GROWTH_DOWN
    404 		t->t_stk = stk + stksize;
    405 		t->t_stkbase = stk;
    406 #else
    407 		t->t_stk = stk;			/* 3b2-like */
    408 		t->t_stkbase = stk + stksize;
    409 #endif /* STACK_GROWTH_DOWN */
    410 	}
    411 
    412 	if (kmem_stackinfo != 0) {
    413 		stkinfo_begin(t);
    414 	}
    415 
    416 	/* set default stack flag */
    417 	if (stksize == lwp_default_stksize)
    418 		t->t_flag |= T_DFLTSTK;
    419 
    420 	t->t_ts = ts;
    421 
    422 	/*
    423 	 * p_cred could be NULL if it thread_create is called before cred_init
    424 	 * is called in main.
    425 	 */
    426 	mutex_enter(&pp->p_crlock);
    427 	if (pp->p_cred)
    428 		crhold(t->t_cred = pp->p_cred);
    429 	mutex_exit(&pp->p_crlock);
    430 	t->t_start = gethrestime_sec();
    431 	t->t_startpc = proc;
    432 	t->t_procp = pp;
    433 	t->t_clfuncs = &sys_classfuncs.thread;
    434 	t->t_cid = syscid;
    435 	t->t_pri = pri;
    436 	t->t_stime = ddi_get_lbolt();
    437 	t->t_schedflag = TS_LOAD | TS_DONT_SWAP;
    438 	t->t_bind_cpu = PBIND_NONE;
    439 	t->t_bindflag = (uchar_t)default_binding_mode;
    440 	t->t_bind_pset = PS_NONE;
    441 	t->t_plockp = &pp->p_lock;
    442 	t->t_copyops = NULL;
    443 	t->t_taskq = NULL;
    444 	t->t_anttime = 0;
    445 	t->t_hatdepth = 0;
    446 
    447 	t->t_dtrace_vtime = 1;	/* assure vtimestamp is always non-zero */
    448 
    449 	CPU_STATS_ADDQ(CPU, sys, nthreads, 1);
    450 #ifndef NPROBE
    451 	/* Kernel probe */
    452 	tnf_thread_create(t);
    453 #endif /* NPROBE */
    454 	LOCK_INIT_CLEAR(&t->t_lock);
    455 
    456 	/*
    457 	 * Callers who give us a NULL proc must do their own
    458 	 * stack initialization.  e.g. lwp_create()
    459 	 */
    460 	if (proc != NULL) {
    461 		t->t_stk = thread_stk_init(t->t_stk);
    462 		thread_load(t, proc, arg, len);
    463 	}
    464 
    465 	/*
    466 	 * Put a hold on project0. If this thread is actually in a
    467 	 * different project, then t_proj will be changed later in
    468 	 * lwp_create().  All kernel-only threads must be in project 0.
    469 	 */
    470 	t->t_proj = project_hold(proj0p);
    471 
    472 	lgrp_affinity_init(&t->t_lgrp_affinity);
    473 
    474 	mutex_enter(&pidlock);
    475 	nthread++;
    476 	t->t_did = next_t_id++;
    477 	t->t_prev = curthread->t_prev;
    478 	t->t_next = curthread;
    479 
    480 	/*
    481 	 * Add the thread to the list of all threads, and initialize
    482 	 * its t_cpu pointer.  We need to block preemption since
    483 	 * cpu_offline walks the thread list looking for threads
    484 	 * with t_cpu pointing to the CPU being offlined.  We want
    485 	 * to make sure that the list is consistent and that if t_cpu
    486 	 * is set, the thread is on the list.
    487 	 */
    488 	kpreempt_disable();
    489 	curthread->t_prev->t_next = t;
    490 	curthread->t_prev = t;
    491 
    492 	/*
    493 	 * Threads should never have a NULL t_cpu pointer so assign it
    494 	 * here.  If the thread is being created with state TS_RUN a
    495 	 * better CPU may be chosen when it is placed on the run queue.
    496 	 *
    497 	 * We need to keep kernel preemption disabled when setting all
    498 	 * three fields to keep them in sync.  Also, always create in
    499 	 * the default partition since that's where kernel threads go
    500 	 * (if this isn't a kernel thread, t_cpupart will be changed
    501 	 * in lwp_create before setting the thread runnable).
    502 	 */
    503 	t->t_cpupart = &cp_default;
    504 
    505 	/*
    506 	 * For now, affiliate this thread with the root lgroup.
    507 	 * Since the kernel does not (presently) allocate its memory
    508 	 * in a locality aware fashion, the root is an appropriate home.
    509 	 * If this thread is later associated with an lwp, it will have
    510 	 * it's lgroup re-assigned at that time.
    511 	 */
    512 	lgrp_move_thread(t, &cp_default.cp_lgrploads[LGRP_ROOTID], 1);
    513 
    514 	/*
    515 	 * Inherit the current cpu.  If this cpu isn't part of the chosen
    516 	 * lgroup, a new cpu will be chosen by cpu_choose when the thread
    517 	 * is ready to run.
    518 	 */
    519 	if (CPU->cpu_part == &cp_default)
    520 		t->t_cpu = CPU;
    521 	else
    522 		t->t_cpu = disp_lowpri_cpu(cp_default.cp_cpulist, t->t_lpl,
    523 		    t->t_pri, NULL);
    524 
    525 	t->t_disp_queue = t->t_cpu->cpu_disp;
    526 	kpreempt_enable();
    527 
    528 	/*
    529 	 * Initialize thread state and the dispatcher lock pointer.
    530 	 * Need to hold onto pidlock to block allthreads walkers until
    531 	 * the state is set.
    532 	 */
    533 	switch (state) {
    534 	case TS_RUN:
    535 		curthread->t_oldspl = splhigh();	/* get dispatcher spl */
    536 		THREAD_SET_STATE(t, TS_STOPPED, &transition_lock);
    537 		CL_SETRUN(t);
    538 		thread_unlock(t);
    539 		break;
    540 
    541 	case TS_ONPROC:
    542 		THREAD_ONPROC(t, t->t_cpu);
    543 		break;
    544 
    545 	case TS_FREE:
    546 		/*
    547 		 * Free state will be used for intr threads.
    548 		 * The interrupt routine must set the thread dispatcher
    549 		 * lock pointer (t_lockp) if starting on a CPU
    550 		 * other than the current one.
    551 		 */
    552 		THREAD_FREEINTR(t, CPU);
    553 		break;
    554 
    555 	case TS_STOPPED:
    556 		THREAD_SET_STATE(t, TS_STOPPED, &stop_lock);
    557 		break;
    558 
    559 	default:			/* TS_SLEEP, TS_ZOMB or TS_TRANS */
    560 		cmn_err(CE_PANIC, "thread_create: invalid state %d", state);
    561 	}
    562 	mutex_exit(&pidlock);
    563 	return (t);
    564 }
    565 
    566 /*
    567  * Move thread to project0 and take care of project reference counters.
    568  */
    569 void
    570 thread_rele(kthread_t *t)
    571 {
    572 	kproject_t *kpj;
    573 
    574 	thread_lock(t);
    575 
    576 	ASSERT(t == curthread || t->t_state == TS_FREE || t->t_procp == &p0);
    577 	kpj = ttoproj(t);
    578 	t->t_proj = proj0p;
    579 
    580 	thread_unlock(t);
    581 
    582 	if (kpj != proj0p) {
    583 		project_rele(kpj);
    584 		(void) project_hold(proj0p);
    585 	}
    586 }
    587 
    588 void
    589 thread_exit(void)
    590 {
    591 	kthread_t *t = curthread;
    592 
    593 	if ((t->t_proc_flag & TP_ZTHREAD) != 0)
    594 		cmn_err(CE_PANIC, "thread_exit: zthread_exit() not called");
    595 
    596 	tsd_exit();		/* Clean up this thread's TSD */
    597 
    598 	kcpc_passivate();	/* clean up performance counter state */
    599 
    600 	/*
    601 	 * No kernel thread should have called poll() without arranging
    602 	 * calling pollcleanup() here.
    603 	 */
    604 	ASSERT(t->t_pollstate == NULL);
    605 	ASSERT(t->t_schedctl == NULL);
    606 	if (t->t_door)
    607 		door_slam();	/* in case thread did an upcall */
    608 
    609 #ifndef NPROBE
    610 	/* Kernel probe */
    611 	if (t->t_tnf_tpdp)
    612 		tnf_thread_exit();
    613 #endif /* NPROBE */
    614 
    615 	thread_rele(t);
    616 	t->t_preempt++;
    617 
    618 	/*
    619 	 * remove thread from the all threads list so that
    620 	 * death-row can use the same pointers.
    621 	 */
    622 	mutex_enter(&pidlock);
    623 	t->t_next->t_prev = t->t_prev;
    624 	t->t_prev->t_next = t->t_next;
    625 	ASSERT(allthreads != t);	/* t0 never exits */
    626 	cv_broadcast(&t->t_joincv);	/* wake up anyone in thread_join */
    627 	mutex_exit(&pidlock);
    628 
    629 	if (t->t_ctx != NULL)
    630 		exitctx(t);
    631 	if (t->t_procp->p_pctx != NULL)
    632 		exitpctx(t->t_procp);
    633 
    634 	if (kmem_stackinfo != 0) {
    635 		stkinfo_end(t);
    636 	}
    637 
    638 	t->t_state = TS_ZOMB;	/* set zombie thread */
    639 
    640 	swtch_from_zombie();	/* give up the CPU */
    641 	/* NOTREACHED */
    642 }
    643 
    644 /*
    645  * Check to see if the specified thread is active (defined as being on
    646  * the thread list).  This is certainly a slow way to do this; if there's
    647  * ever a reason to speed it up, we could maintain a hash table of active
    648  * threads indexed by their t_did.
    649  */
    650 static kthread_t *
    651 did_to_thread(kt_did_t tid)
    652 {
    653 	kthread_t *t;
    654 
    655 	ASSERT(MUTEX_HELD(&pidlock));
    656 	for (t = curthread->t_next; t != curthread; t = t->t_next) {
    657 		if (t->t_did == tid)
    658 			break;
    659 	}
    660 	if (t->t_did == tid)
    661 		return (t);
    662 	else
    663 		return (NULL);
    664 }
    665 
    666 /*
    667  * Wait for specified thread to exit.  Returns immediately if the thread
    668  * could not be found, meaning that it has either already exited or never
    669  * existed.
    670  */
    671 void
    672 thread_join(kt_did_t tid)
    673 {
    674 	kthread_t *t;
    675 
    676 	ASSERT(tid != curthread->t_did);
    677 	ASSERT(tid != t0.t_did);
    678 
    679 	mutex_enter(&pidlock);
    680 	/*
    681 	 * Make sure we check that the thread is on the thread list
    682 	 * before blocking on it; otherwise we could end up blocking on
    683 	 * a cv that's already been freed.  In other words, don't cache
    684 	 * the thread pointer across calls to cv_wait.
    685 	 *
    686 	 * The choice of loop invariant means that whenever a thread
    687 	 * is taken off the allthreads list, a cv_broadcast must be
    688 	 * performed on that thread's t_joincv to wake up any waiters.
    689 	 * The broadcast doesn't have to happen right away, but it
    690 	 * shouldn't be postponed indefinitely (e.g., by doing it in
    691 	 * thread_free which may only be executed when the deathrow
    692 	 * queue is processed.
    693 	 */
    694 	while (t = did_to_thread(tid))
    695 		cv_wait(&t->t_joincv, &pidlock);
    696 	mutex_exit(&pidlock);
    697 }
    698 
    699 void
    700 thread_free_prevent(kthread_t *t)
    701 {
    702 	kmutex_t *lp;
    703 
    704 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
    705 	mutex_enter(lp);
    706 }
    707 
    708 void
    709 thread_free_allow(kthread_t *t)
    710 {
    711 	kmutex_t *lp;
    712 
    713 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
    714 	mutex_exit(lp);
    715 }
    716 
    717 static void
    718 thread_free_barrier(kthread_t *t)
    719 {
    720 	kmutex_t *lp;
    721 
    722 	lp = &thread_free_lock[THREAD_FREE_HASH(t)].tf_lock;
    723 	mutex_enter(lp);
    724 	mutex_exit(lp);
    725 }
    726 
    727 void
    728 thread_free(kthread_t *t)
    729 {
    730 	ASSERT(t != &t0 && t->t_state == TS_FREE);
    731 	ASSERT(t->t_door == NULL);
    732 	ASSERT(t->t_schedctl == NULL);
    733 	ASSERT(t->t_pollstate == NULL);
    734 
    735 	t->t_pri = 0;
    736 	t->t_pc = 0;
    737 	t->t_sp = 0;
    738 	t->t_wchan0 = NULL;
    739 	t->t_wchan = NULL;
    740 	if (t->t_cred != NULL) {
    741 		crfree(t->t_cred);
    742 		t->t_cred = 0;
    743 	}
    744 	if (t->t_pdmsg) {
    745 		kmem_free(t->t_pdmsg, strlen(t->t_pdmsg) + 1);
    746 		t->t_pdmsg = NULL;
    747 	}
    748 	if (audit_active)
    749 		audit_thread_free(t);
    750 #ifndef NPROBE
    751 	if (t->t_tnf_tpdp)
    752 		tnf_thread_free(t);
    753 #endif /* NPROBE */
    754 	if (t->t_cldata) {
    755 		CL_EXITCLASS(t->t_cid, (caddr_t *)t->t_cldata);
    756 	}
    757 	if (t->t_rprof != NULL) {
    758 		kmem_free(t->t_rprof, sizeof (*t->t_rprof));
    759 		t->t_rprof = NULL;
    760 	}
    761 	t->t_lockp = NULL;	/* nothing should try to lock this thread now */
    762 	if (t->t_lwp)
    763 		lwp_freeregs(t->t_lwp, 0);
    764 	if (t->t_ctx)
    765 		freectx(t, 0);
    766 	t->t_stk = NULL;
    767 	if (t->t_lwp)
    768 		lwp_stk_fini(t->t_lwp);
    769 	lock_clear(&t->t_lock);
    770 
    771 	if (t->t_ts->ts_waiters > 0)
    772 		panic("thread_free: turnstile still active");
    773 
    774 	kmem_cache_free(turnstile_cache, t->t_ts);
    775 
    776 	free_afd(&t->t_activefd);
    777 
    778 	/*
    779 	 * Barrier for the tick accounting code.  The tick accounting code
    780 	 * holds this lock to keep the thread from going away while it's
    781 	 * looking at it.
    782 	 */
    783 	thread_free_barrier(t);
    784 
    785 	ASSERT(ttoproj(t) == proj0p);
    786 	project_rele(ttoproj(t));
    787 
    788 	lgrp_affinity_free(&t->t_lgrp_affinity);
    789 
    790 	/*
    791 	 * Free thread struct and its stack.
    792 	 */
    793 	if (t->t_flag & T_TALLOCSTK) {
    794 		/* thread struct is embedded in stack */
    795 		segkp_release(segkp, t->t_swap);
    796 		mutex_enter(&pidlock);
    797 		nthread--;
    798 		mutex_exit(&pidlock);
    799 	} else {
    800 		if (t->t_swap) {
    801 			segkp_release(segkp, t->t_swap);
    802 			t->t_swap = NULL;
    803 		}
    804 		if (t->t_lwp) {
    805 			kmem_cache_free(lwp_cache, t->t_lwp);
    806 			t->t_lwp = NULL;
    807 		}
    808 		mutex_enter(&pidlock);
    809 		nthread--;
    810 		mutex_exit(&pidlock);
    811 		kmem_cache_free(thread_cache, t);
    812 	}
    813 }
    814 
    815 /*
    816  * Removes threads associated with the given zone from a deathrow queue.
    817  * tp is a pointer to the head of the deathrow queue, and countp is a
    818  * pointer to the current deathrow count.  Returns a linked list of
    819  * threads removed from the list.
    820  */
    821 static kthread_t *
    822 thread_zone_cleanup(kthread_t **tp, int *countp, zoneid_t zoneid)
    823 {
    824 	kthread_t *tmp, *list = NULL;
    825 	cred_t *cr;
    826 
    827 	ASSERT(MUTEX_HELD(&reaplock));
    828 	while (*tp != NULL) {
    829 		if ((cr = (*tp)->t_cred) != NULL && crgetzoneid(cr) == zoneid) {
    830 			tmp = *tp;
    831 			*tp = tmp->t_forw;
    832 			tmp->t_forw = list;
    833 			list = tmp;
    834 			(*countp)--;
    835 		} else {
    836 			tp = &(*tp)->t_forw;
    837 		}
    838 	}
    839 	return (list);
    840 }
    841 
    842 static void
    843 thread_reap_list(kthread_t *t)
    844 {
    845 	kthread_t *next;
    846 
    847 	while (t != NULL) {
    848 		next = t->t_forw;
    849 		thread_free(t);
    850 		t = next;
    851 	}
    852 }
    853 
    854 /* ARGSUSED */
    855 static void
    856 thread_zone_destroy(zoneid_t zoneid, void *unused)
    857 {
    858 	kthread_t *t, *l;
    859 
    860 	mutex_enter(&reaplock);
    861 	/*
    862 	 * Pull threads and lwps associated with zone off deathrow lists.
    863 	 */
    864 	t = thread_zone_cleanup(&thread_deathrow, &thread_reapcnt, zoneid);
    865 	l = thread_zone_cleanup(&lwp_deathrow, &lwp_reapcnt, zoneid);
    866 	mutex_exit(&reaplock);
    867 
    868 	/*
    869 	 * Guard against race condition in mutex_owner_running:
    870 	 * 	thread=owner(mutex)
    871 	 * 	<interrupt>
    872 	 * 				thread exits mutex
    873 	 * 				thread exits
    874 	 * 				thread reaped
    875 	 * 				thread struct freed
    876 	 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
    877 	 * A cross call to all cpus will cause the interrupt handler
    878 	 * to reset the PC if it is in mutex_owner_running, refreshing
    879 	 * stale thread pointers.
    880 	 */
    881 	mutex_sync();   /* sync with mutex code */
    882 
    883 	/*
    884 	 * Reap threads
    885 	 */
    886 	thread_reap_list(t);
    887 
    888 	/*
    889 	 * Reap lwps
    890 	 */
    891 	thread_reap_list(l);
    892 }
    893 
    894 /*
    895  * cleanup zombie threads that are on deathrow.
    896  */
    897 void
    898 thread_reaper()
    899 {
    900 	kthread_t *t, *l;
    901 	callb_cpr_t cprinfo;
    902 
    903 	/*
    904 	 * Register callback to clean up threads when zone is destroyed.
    905 	 */
    906 	zone_key_create(&zone_thread_key, NULL, NULL, thread_zone_destroy);
    907 
    908 	CALLB_CPR_INIT(&cprinfo, &reaplock, callb_generic_cpr, "t_reaper");
    909 	for (;;) {
    910 		mutex_enter(&reaplock);
    911 		while (thread_deathrow == NULL && lwp_deathrow == NULL) {
    912 			CALLB_CPR_SAFE_BEGIN(&cprinfo);
    913 			cv_wait(&reaper_cv, &reaplock);
    914 			CALLB_CPR_SAFE_END(&cprinfo, &reaplock);
    915 		}
    916 		/*
    917 		 * mutex_sync() needs to be called when reaping, but
    918 		 * not too often.  We limit reaping rate to once
    919 		 * per second.  Reaplimit is max rate at which threads can
    920 		 * be freed. Does not impact thread destruction/creation.
    921 		 */
    922 		t = thread_deathrow;
    923 		l = lwp_deathrow;
    924 		thread_deathrow = NULL;
    925 		lwp_deathrow = NULL;
    926 		thread_reapcnt = 0;
    927 		lwp_reapcnt = 0;
    928 		mutex_exit(&reaplock);
    929 
    930 		/*
    931 		 * Guard against race condition in mutex_owner_running:
    932 		 * 	thread=owner(mutex)
    933 		 * 	<interrupt>
    934 		 * 				thread exits mutex
    935 		 * 				thread exits
    936 		 * 				thread reaped
    937 		 * 				thread struct freed
    938 		 * cpu = thread->t_cpu <- BAD POINTER DEREFERENCE.
    939 		 * A cross call to all cpus will cause the interrupt handler
    940 		 * to reset the PC if it is in mutex_owner_running, refreshing
    941 		 * stale thread pointers.
    942 		 */
    943 		mutex_sync();   /* sync with mutex code */
    944 		/*
    945 		 * Reap threads
    946 		 */
    947 		thread_reap_list(t);
    948 
    949 		/*
    950 		 * Reap lwps
    951 		 */
    952 		thread_reap_list(l);
    953 		delay(hz);
    954 	}
    955 }
    956 
    957 /*
    958  * This is called by lwpcreate, etc.() to put a lwp_deathrow thread onto
    959  * thread_deathrow. The thread's state is changed already TS_FREE to indicate
    960  * that is reapable. The thread already holds the reaplock, and was already
    961  * freed.
    962  */
    963 void
    964 reapq_move_lq_to_tq(kthread_t *t)
    965 {
    966 	ASSERT(t->t_state == TS_FREE);
    967 	ASSERT(MUTEX_HELD(&reaplock));
    968 	t->t_forw = thread_deathrow;
    969 	thread_deathrow = t;
    970 	thread_reapcnt++;
    971 	if (lwp_reapcnt + thread_reapcnt > reaplimit)
    972 		cv_signal(&reaper_cv);  /* wake the reaper */
    973 }
    974 
    975 /*
    976  * This is called by resume() to put a zombie thread onto deathrow.
    977  * The thread's state is changed to TS_FREE to indicate that is reapable.
    978  * This is called from the idle thread so it must not block - just spin.
    979  */
    980 void
    981 reapq_add(kthread_t *t)
    982 {
    983 	mutex_enter(&reaplock);
    984 
    985 	/*
    986 	 * lwp_deathrow contains only threads with lwp linkage
    987 	 * that are of the default stacksize. Anything else goes
    988 	 * on thread_deathrow.
    989 	 */
    990 	if (ttolwp(t) && (t->t_flag & T_DFLTSTK)) {
    991 		t->t_forw = lwp_deathrow;
    992 		lwp_deathrow = t;
    993 		lwp_reapcnt++;
    994 	} else {
    995 		t->t_forw = thread_deathrow;
    996 		thread_deathrow = t;
    997 		thread_reapcnt++;
    998 	}
    999 	if (lwp_reapcnt + thread_reapcnt > reaplimit)
   1000 		cv_signal(&reaper_cv);	/* wake the reaper */
   1001 	t->t_state = TS_FREE;
   1002 	lock_clear(&t->t_lock);
   1003 
   1004 	/*
   1005 	 * Before we return, we need to grab and drop the thread lock for
   1006 	 * the dead thread.  At this point, the current thread is the idle
   1007 	 * thread, and the dead thread's CPU lock points to the current
   1008 	 * CPU -- and we must grab and drop the lock to synchronize with
   1009 	 * a racing thread walking a blocking chain that the zombie thread
   1010 	 * was recently in.  By this point, that blocking chain is (by
   1011 	 * definition) stale:  the dead thread is not holding any locks, and
   1012 	 * is therefore not in any blocking chains -- but if we do not regrab
   1013 	 * our lock before freeing the dead thread's data structures, the
   1014 	 * thread walking the (stale) blocking chain will die on memory
   1015 	 * corruption when it attempts to drop the dead thread's lock.  We
   1016 	 * only need do this once because there is no way for the dead thread
   1017 	 * to ever again be on a blocking chain:  once we have grabbed and
   1018 	 * dropped the thread lock, we are guaranteed that anyone that could
   1019 	 * have seen this thread in a blocking chain can no longer see it.
   1020 	 */
   1021 	thread_lock(t);
   1022 	thread_unlock(t);
   1023 
   1024 	mutex_exit(&reaplock);
   1025 }
   1026 
   1027 /*
   1028  * Install thread context ops for the current thread.
   1029  */
   1030 void
   1031 installctx(
   1032 	kthread_t *t,
   1033 	void	*arg,
   1034 	void	(*save)(void *),
   1035 	void	(*restore)(void *),
   1036 	void	(*fork)(void *, void *),
   1037 	void	(*lwp_create)(void *, void *),
   1038 	void	(*exit)(void *),
   1039 	void	(*free)(void *, int))
   1040 {
   1041 	struct ctxop *ctx;
   1042 
   1043 	ctx = kmem_alloc(sizeof (struct ctxop), KM_SLEEP);
   1044 	ctx->save_op = save;
   1045 	ctx->restore_op = restore;
   1046 	ctx->fork_op = fork;
   1047 	ctx->lwp_create_op = lwp_create;
   1048 	ctx->exit_op = exit;
   1049 	ctx->free_op = free;
   1050 	ctx->arg = arg;
   1051 	ctx->next = t->t_ctx;
   1052 	t->t_ctx = ctx;
   1053 }
   1054 
   1055 /*
   1056  * Remove the thread context ops from a thread.
   1057  */
   1058 int
   1059 removectx(
   1060 	kthread_t *t,
   1061 	void	*arg,
   1062 	void	(*save)(void *),
   1063 	void	(*restore)(void *),
   1064 	void	(*fork)(void *, void *),
   1065 	void	(*lwp_create)(void *, void *),
   1066 	void	(*exit)(void *),
   1067 	void	(*free)(void *, int))
   1068 {
   1069 	struct ctxop *ctx, *prev_ctx;
   1070 
   1071 	/*
   1072 	 * The incoming kthread_t (which is the thread for which the
   1073 	 * context ops will be removed) should be one of the following:
   1074 	 *
   1075 	 * a) the current thread,
   1076 	 *
   1077 	 * b) a thread of a process that's being forked (SIDL),
   1078 	 *
   1079 	 * c) a thread that belongs to the same process as the current
   1080 	 *    thread and for which the current thread is the agent thread,
   1081 	 *
   1082 	 * d) a thread that is TS_STOPPED which is indicative of it
   1083 	 *    being (if curthread is not an agent) a thread being created
   1084 	 *    as part of an lwp creation.
   1085 	 */
   1086 	ASSERT(t == curthread || ttoproc(t)->p_stat == SIDL ||
   1087 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
   1088 
   1089 	/*
   1090 	 * Serialize modifications to t->t_ctx to prevent the agent thread
   1091 	 * and the target thread from racing with each other during lwp exit.
   1092 	 */
   1093 	mutex_enter(&t->t_ctx_lock);
   1094 	prev_ctx = NULL;
   1095 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next) {
   1096 		if (ctx->save_op == save && ctx->restore_op == restore &&
   1097 		    ctx->fork_op == fork && ctx->lwp_create_op == lwp_create &&
   1098 		    ctx->exit_op == exit && ctx->free_op == free &&
   1099 		    ctx->arg == arg) {
   1100 			if (prev_ctx)
   1101 				prev_ctx->next = ctx->next;
   1102 			else
   1103 				t->t_ctx = ctx->next;
   1104 			mutex_exit(&t->t_ctx_lock);
   1105 			if (ctx->free_op != NULL)
   1106 				(ctx->free_op)(ctx->arg, 0);
   1107 			kmem_free(ctx, sizeof (struct ctxop));
   1108 			return (1);
   1109 		}
   1110 		prev_ctx = ctx;
   1111 	}
   1112 	mutex_exit(&t->t_ctx_lock);
   1113 
   1114 	return (0);
   1115 }
   1116 
   1117 void
   1118 savectx(kthread_t *t)
   1119 {
   1120 	struct ctxop *ctx;
   1121 
   1122 	ASSERT(t == curthread);
   1123 	for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
   1124 		if (ctx->save_op != NULL)
   1125 			(ctx->save_op)(ctx->arg);
   1126 }
   1127 
   1128 void
   1129 restorectx(kthread_t *t)
   1130 {
   1131 	struct ctxop *ctx;
   1132 
   1133 	ASSERT(t == curthread);
   1134 	for (ctx = t->t_ctx; ctx != 0; ctx = ctx->next)
   1135 		if (ctx->restore_op != NULL)
   1136 			(ctx->restore_op)(ctx->arg);
   1137 }
   1138 
   1139 void
   1140 forkctx(kthread_t *t, kthread_t *ct)
   1141 {
   1142 	struct ctxop *ctx;
   1143 
   1144 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
   1145 		if (ctx->fork_op != NULL)
   1146 			(ctx->fork_op)(t, ct);
   1147 }
   1148 
   1149 /*
   1150  * Note that this operator is only invoked via the _lwp_create
   1151  * system call.  The system may have other reasons to create lwps
   1152  * e.g. the agent lwp or the doors unreferenced lwp.
   1153  */
   1154 void
   1155 lwp_createctx(kthread_t *t, kthread_t *ct)
   1156 {
   1157 	struct ctxop *ctx;
   1158 
   1159 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
   1160 		if (ctx->lwp_create_op != NULL)
   1161 			(ctx->lwp_create_op)(t, ct);
   1162 }
   1163 
   1164 /*
   1165  * exitctx is called from thread_exit() and lwp_exit() to perform any actions
   1166  * needed when the thread/LWP leaves the processor for the last time. This
   1167  * routine is not intended to deal with freeing memory; freectx() is used for
   1168  * that purpose during thread_free(). This routine is provided to allow for
   1169  * clean-up that can't wait until thread_free().
   1170  */
   1171 void
   1172 exitctx(kthread_t *t)
   1173 {
   1174 	struct ctxop *ctx;
   1175 
   1176 	for (ctx = t->t_ctx; ctx != NULL; ctx = ctx->next)
   1177 		if (ctx->exit_op != NULL)
   1178 			(ctx->exit_op)(t);
   1179 }
   1180 
   1181 /*
   1182  * freectx is called from thread_free() and exec() to get
   1183  * rid of old thread context ops.
   1184  */
   1185 void
   1186 freectx(kthread_t *t, int isexec)
   1187 {
   1188 	struct ctxop *ctx;
   1189 
   1190 	while ((ctx = t->t_ctx) != NULL) {
   1191 		t->t_ctx = ctx->next;
   1192 		if (ctx->free_op != NULL)
   1193 			(ctx->free_op)(ctx->arg, isexec);
   1194 		kmem_free(ctx, sizeof (struct ctxop));
   1195 	}
   1196 }
   1197 
   1198 /*
   1199  * freectx_ctx is called from lwp_create() when lwp is reused from
   1200  * lwp_deathrow and its thread structure is added to thread_deathrow.
   1201  * The thread structure to which this ctx was attached may be already
   1202  * freed by the thread reaper so free_op implementations shouldn't rely
   1203  * on thread structure to which this ctx was attached still being around.
   1204  */
   1205 void
   1206 freectx_ctx(struct ctxop *ctx)
   1207 {
   1208 	struct ctxop *nctx;
   1209 
   1210 	ASSERT(ctx != NULL);
   1211 
   1212 	do {
   1213 		nctx = ctx->next;
   1214 		if (ctx->free_op != NULL)
   1215 			(ctx->free_op)(ctx->arg, 0);
   1216 		kmem_free(ctx, sizeof (struct ctxop));
   1217 	} while ((ctx = nctx) != NULL);
   1218 }
   1219 
   1220 /*
   1221  * Set the thread running; arrange for it to be swapped in if necessary.
   1222  */
   1223 void
   1224 setrun_locked(kthread_t *t)
   1225 {
   1226 	ASSERT(THREAD_LOCK_HELD(t));
   1227 	if (t->t_state == TS_SLEEP) {
   1228 		/*
   1229 		 * Take off sleep queue.
   1230 		 */
   1231 		SOBJ_UNSLEEP(t->t_sobj_ops, t);
   1232 	} else if (t->t_state & (TS_RUN | TS_ONPROC)) {
   1233 		/*
   1234 		 * Already on dispatcher queue.
   1235 		 */
   1236 		return;
   1237 	} else if (t->t_state == TS_WAIT) {
   1238 		waitq_setrun(t);
   1239 	} else if (t->t_state == TS_STOPPED) {
   1240 		/*
   1241 		 * All of the sending of SIGCONT (TC_XSTART) and /proc
   1242 		 * (TC_PSTART) and lwp_continue() (TC_CSTART) must have
   1243 		 * requested that the thread be run.
   1244 		 * Just calling setrun() is not sufficient to set a stopped
   1245 		 * thread running.  TP_TXSTART is always set if the thread
   1246 		 * is not stopped by a jobcontrol stop signal.
   1247 		 * TP_TPSTART is always set if /proc is not controlling it.
   1248 		 * TP_TCSTART is always set if lwp_suspend() didn't stop it.
   1249 		 * The thread won't be stopped unless one of these
   1250 		 * three mechanisms did it.
   1251 		 *
   1252 		 * These flags must be set before calling setrun_locked(t).
   1253 		 * They can't be passed as arguments because the streams
   1254 		 * code calls setrun() indirectly and the mechanism for
   1255 		 * doing so admits only one argument.  Note that the
   1256 		 * thread must be locked in order to change t_schedflags.
   1257 		 */
   1258 		if ((t->t_schedflag & TS_ALLSTART) != TS_ALLSTART)
   1259 			return;
   1260 		/*
   1261 		 * Process is no longer stopped (a thread is running).
   1262 		 */
   1263 		t->t_whystop = 0;
   1264 		t->t_whatstop = 0;
   1265 		/*
   1266 		 * Strictly speaking, we do not have to clear these
   1267 		 * flags here; they are cleared on entry to stop().
   1268 		 * However, they are confusing when doing kernel
   1269 		 * debugging or when they are revealed by ps(1).
   1270 		 */
   1271 		t->t_schedflag &= ~TS_ALLSTART;
   1272 		THREAD_TRANSITION(t);	/* drop stopped-thread lock */
   1273 		ASSERT(t->t_lockp == &transition_lock);
   1274 		ASSERT(t->t_wchan0 == NULL && t->t_wchan == NULL);
   1275 		/*
   1276 		 * Let the class put the process on the dispatcher queue.
   1277 		 */
   1278 		CL_SETRUN(t);
   1279 	}
   1280 }
   1281 
   1282 void
   1283 setrun(kthread_t *t)
   1284 {
   1285 	thread_lock(t);
   1286 	setrun_locked(t);
   1287 	thread_unlock(t);
   1288 }
   1289 
   1290 /*
   1291  * Unpin an interrupted thread.
   1292  *	When an interrupt occurs, the interrupt is handled on the stack
   1293  *	of an interrupt thread, taken from a pool linked to the CPU structure.
   1294  *
   1295  *	When swtch() is switching away from an interrupt thread because it
   1296  *	blocked or was preempted, this routine is called to complete the
   1297  *	saving of the interrupted thread state, and returns the interrupted
   1298  *	thread pointer so it may be resumed.
   1299  *
   1300  *	Called by swtch() only at high spl.
   1301  */
   1302 kthread_t *
   1303 thread_unpin()
   1304 {
   1305 	kthread_t	*t = curthread;	/* current thread */
   1306 	kthread_t	*itp;		/* interrupted thread */
   1307 	int		i;		/* interrupt level */
   1308 	extern int	intr_passivate();
   1309 
   1310 	ASSERT(t->t_intr != NULL);
   1311 
   1312 	itp = t->t_intr;		/* interrupted thread */
   1313 	t->t_intr = NULL;		/* clear interrupt ptr */
   1314 
   1315 	/*
   1316 	 * Get state from interrupt thread for the one
   1317 	 * it interrupted.
   1318 	 */
   1319 
   1320 	i = intr_passivate(t, itp);
   1321 
   1322 	TRACE_5(TR_FAC_INTR, TR_INTR_PASSIVATE,
   1323 	    "intr_passivate:level %d curthread %p (%T) ithread %p (%T)",
   1324 	    i, t, t, itp, itp);
   1325 
   1326 	/*
   1327 	 * Dissociate the current thread from the interrupted thread's LWP.
   1328 	 */
   1329 	t->t_lwp = NULL;
   1330 
   1331 	/*
   1332 	 * Interrupt handlers above the level that spinlocks block must
   1333 	 * not block.
   1334 	 */
   1335 #if DEBUG
   1336 	if (i < 0 || i > LOCK_LEVEL)
   1337 		cmn_err(CE_PANIC, "thread_unpin: ipl out of range %x", i);
   1338 #endif
   1339 
   1340 	/*
   1341 	 * Compute the CPU's base interrupt level based on the active
   1342 	 * interrupts.
   1343 	 */
   1344 	ASSERT(CPU->cpu_intr_actv & (1 << i));
   1345 	set_base_spl();
   1346 
   1347 	return (itp);
   1348 }
   1349 
   1350 /*
   1351  * Create and initialize an interrupt thread.
   1352  *	Returns non-zero on error.
   1353  *	Called at spl7() or better.
   1354  */
   1355 void
   1356 thread_create_intr(struct cpu *cp)
   1357 {
   1358 	kthread_t *tp;
   1359 
   1360 	tp = thread_create(NULL, 0,
   1361 	    (void (*)())thread_create_intr, NULL, 0, &p0, TS_ONPROC, 0);
   1362 
   1363 	/*
   1364 	 * Set the thread in the TS_FREE state.  The state will change
   1365 	 * to TS_ONPROC only while the interrupt is active.  Think of these
   1366 	 * as being on a private free list for the CPU.  Being TS_FREE keeps
   1367 	 * inactive interrupt threads out of debugger thread lists.
   1368 	 *
   1369 	 * We cannot call thread_create with TS_FREE because of the current
   1370 	 * checks there for ONPROC.  Fix this when thread_create takes flags.
   1371 	 */
   1372 	THREAD_FREEINTR(tp, cp);
   1373 
   1374 	/*
   1375 	 * Nobody should ever reference the credentials of an interrupt
   1376 	 * thread so make it NULL to catch any such references.
   1377 	 */
   1378 	tp->t_cred = NULL;
   1379 	tp->t_flag |= T_INTR_THREAD;
   1380 	tp->t_cpu = cp;
   1381 	tp->t_bound_cpu = cp;
   1382 	tp->t_disp_queue = cp->cpu_disp;
   1383 	tp->t_affinitycnt = 1;
   1384 	tp->t_preempt = 1;
   1385 
   1386 	/*
   1387 	 * Don't make a user-requested binding on this thread so that
   1388 	 * the processor can be offlined.
   1389 	 */
   1390 	tp->t_bind_cpu = PBIND_NONE;	/* no USER-requested binding */
   1391 	tp->t_bind_pset = PS_NONE;
   1392 
   1393 #if defined(__i386) || defined(__amd64)
   1394 	tp->t_stk -= STACK_ALIGN;
   1395 	*(tp->t_stk) = 0;		/* terminate intr thread stack */
   1396 #endif
   1397 
   1398 	/*
   1399 	 * Link onto CPU's interrupt pool.
   1400 	 */
   1401 	tp->t_link = cp->cpu_intr_thread;
   1402 	cp->cpu_intr_thread = tp;
   1403 }
   1404 
   1405 /*
   1406  * TSD -- THREAD SPECIFIC DATA
   1407  */
   1408 static kmutex_t		tsd_mutex;	 /* linked list spin lock */
   1409 static uint_t		tsd_nkeys;	 /* size of destructor array */
   1410 /* per-key destructor funcs */
   1411 static void 		(**tsd_destructor)(void *);
   1412 /* list of tsd_thread's */
   1413 static struct tsd_thread	*tsd_list;
   1414 
   1415 /*
   1416  * Default destructor
   1417  *	Needed because NULL destructor means that the key is unused
   1418  */
   1419 /* ARGSUSED */
   1420 void
   1421 tsd_defaultdestructor(void *value)
   1422 {}
   1423 
   1424 /*
   1425  * Create a key (index into per thread array)
   1426  *	Locks out tsd_create, tsd_destroy, and tsd_exit
   1427  *	May allocate memory with lock held
   1428  */
   1429 void
   1430 tsd_create(uint_t *keyp, void (*destructor)(void *))
   1431 {
   1432 	int	i;
   1433 	uint_t	nkeys;
   1434 
   1435 	/*
   1436 	 * if key is allocated, do nothing
   1437 	 */
   1438 	mutex_enter(&tsd_mutex);
   1439 	if (*keyp) {
   1440 		mutex_exit(&tsd_mutex);
   1441 		return;
   1442 	}
   1443 	/*
   1444 	 * find an unused key
   1445 	 */
   1446 	if (destructor == NULL)
   1447 		destructor = tsd_defaultdestructor;
   1448 
   1449 	for (i = 0; i < tsd_nkeys; ++i)
   1450 		if (tsd_destructor[i] == NULL)
   1451 			break;
   1452 
   1453 	/*
   1454 	 * if no unused keys, increase the size of the destructor array
   1455 	 */
   1456 	if (i == tsd_nkeys) {
   1457 		if ((nkeys = (tsd_nkeys << 1)) == 0)
   1458 			nkeys = 1;
   1459 		tsd_destructor =
   1460 		    (void (**)(void *))tsd_realloc((void *)tsd_destructor,
   1461 		    (size_t)(tsd_nkeys * sizeof (void (*)(void *))),
   1462 		    (size_t)(nkeys * sizeof (void (*)(void *))));
   1463 		tsd_nkeys = nkeys;
   1464 	}
   1465 
   1466 	/*
   1467 	 * allocate the next available unused key
   1468 	 */
   1469 	tsd_destructor[i] = destructor;
   1470 	*keyp = i + 1;
   1471 	mutex_exit(&tsd_mutex);
   1472 }
   1473 
   1474 /*
   1475  * Destroy a key -- this is for unloadable modules
   1476  *
   1477  * Assumes that the caller is preventing tsd_set and tsd_get
   1478  * Locks out tsd_create, tsd_destroy, and tsd_exit
   1479  * May free memory with lock held
   1480  */
   1481 void
   1482 tsd_destroy(uint_t *keyp)
   1483 {
   1484 	uint_t key;
   1485 	struct tsd_thread *tsd;
   1486 
   1487 	/*
   1488 	 * protect the key namespace and our destructor lists
   1489 	 */
   1490 	mutex_enter(&tsd_mutex);
   1491 	key = *keyp;
   1492 	*keyp = 0;
   1493 
   1494 	ASSERT(key <= tsd_nkeys);
   1495 
   1496 	/*
   1497 	 * if the key is valid
   1498 	 */
   1499 	if (key != 0) {
   1500 		uint_t k = key - 1;
   1501 		/*
   1502 		 * for every thread with TSD, call key's destructor
   1503 		 */
   1504 		for (tsd = tsd_list; tsd; tsd = tsd->ts_next) {
   1505 			/*
   1506 			 * no TSD for key in this thread
   1507 			 */
   1508 			if (key > tsd->ts_nkeys)
   1509 				continue;
   1510 			/*
   1511 			 * call destructor for key
   1512 			 */
   1513 			if (tsd->ts_value[k] && tsd_destructor[k])
   1514 				(*tsd_destructor[k])(tsd->ts_value[k]);
   1515 			/*
   1516 			 * reset value for key
   1517 			 */
   1518 			tsd->ts_value[k] = NULL;
   1519 		}
   1520 		/*
   1521 		 * actually free the key (NULL destructor == unused)
   1522 		 */
   1523 		tsd_destructor[k] = NULL;
   1524 	}
   1525 
   1526 	mutex_exit(&tsd_mutex);
   1527 }
   1528 
   1529 /*
   1530  * Quickly return the per thread value that was stored with the specified key
   1531  * Assumes the caller is protecting key from tsd_create and tsd_destroy
   1532  */
   1533 void *
   1534 tsd_get(uint_t key)
   1535 {
   1536 	return (tsd_agent_get(curthread, key));
   1537 }
   1538 
   1539 /*
   1540  * Set a per thread value indexed with the specified key
   1541  */
   1542 int
   1543 tsd_set(uint_t key, void *value)
   1544 {
   1545 	return (tsd_agent_set(curthread, key, value));
   1546 }
   1547 
   1548 /*
   1549  * Like tsd_get(), except that the agent lwp can get the tsd of
   1550  * another thread in the same process (the agent thread only runs when the
   1551  * process is completely stopped by /proc), or syslwp is creating a new lwp.
   1552  */
   1553 void *
   1554 tsd_agent_get(kthread_t *t, uint_t key)
   1555 {
   1556 	struct tsd_thread *tsd = t->t_tsd;
   1557 
   1558 	ASSERT(t == curthread ||
   1559 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
   1560 
   1561 	if (key && tsd != NULL && key <= tsd->ts_nkeys)
   1562 		return (tsd->ts_value[key - 1]);
   1563 	return (NULL);
   1564 }
   1565 
   1566 /*
   1567  * Like tsd_set(), except that the agent lwp can set the tsd of
   1568  * another thread in the same process, or syslwp can set the tsd
   1569  * of a thread it's in the middle of creating.
   1570  *
   1571  * Assumes the caller is protecting key from tsd_create and tsd_destroy
   1572  * May lock out tsd_destroy (and tsd_create), may allocate memory with
   1573  * lock held
   1574  */
   1575 int
   1576 tsd_agent_set(kthread_t *t, uint_t key, void *value)
   1577 {
   1578 	struct tsd_thread *tsd = t->t_tsd;
   1579 
   1580 	ASSERT(t == curthread ||
   1581 	    ttoproc(t)->p_agenttp == curthread || t->t_state == TS_STOPPED);
   1582 
   1583 	if (key == 0)
   1584 		return (EINVAL);
   1585 	if (tsd == NULL)
   1586 		tsd = t->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
   1587 	if (key <= tsd->ts_nkeys) {
   1588 		tsd->ts_value[key - 1] = value;
   1589 		return (0);
   1590 	}
   1591 
   1592 	ASSERT(key <= tsd_nkeys);
   1593 
   1594 	/*
   1595 	 * lock out tsd_destroy()
   1596 	 */
   1597 	mutex_enter(&tsd_mutex);
   1598 	if (tsd->ts_nkeys == 0) {
   1599 		/*
   1600 		 * Link onto list of threads with TSD
   1601 		 */
   1602 		if ((tsd->ts_next = tsd_list) != NULL)
   1603 			tsd_list->ts_prev = tsd;
   1604 		tsd_list = tsd;
   1605 	}
   1606 
   1607 	/*
   1608 	 * Allocate thread local storage and set the value for key
   1609 	 */
   1610 	tsd->ts_value = tsd_realloc(tsd->ts_value,
   1611 	    tsd->ts_nkeys * sizeof (void *),
   1612 	    key * sizeof (void *));
   1613 	tsd->ts_nkeys = key;
   1614 	tsd->ts_value[key - 1] = value;
   1615 	mutex_exit(&tsd_mutex);
   1616 
   1617 	return (0);
   1618 }
   1619 
   1620 
   1621 /*
   1622  * Return the per thread value that was stored with the specified key
   1623  *	If necessary, create the key and the value
   1624  *	Assumes the caller is protecting *keyp from tsd_destroy
   1625  */
   1626 void *
   1627 tsd_getcreate(uint_t *keyp, void (*destroy)(void *), void *(*allocate)(void))
   1628 {
   1629 	void *value;
   1630 	uint_t key = *keyp;
   1631 	struct tsd_thread *tsd = curthread->t_tsd;
   1632 
   1633 	if (tsd == NULL)
   1634 		tsd = curthread->t_tsd = kmem_zalloc(sizeof (*tsd), KM_SLEEP);
   1635 	if (key && key <= tsd->ts_nkeys && (value = tsd->ts_value[key - 1]))
   1636 		return (value);
   1637 	if (key == 0)
   1638 		tsd_create(keyp, destroy);
   1639 	(void) tsd_set(*keyp, value = (*allocate)());
   1640 
   1641 	return (value);
   1642 }
   1643 
   1644 /*
   1645  * Called from thread_exit() to run the destructor function for each tsd
   1646  *	Locks out tsd_create and tsd_destroy
   1647  *	Assumes that the destructor *DOES NOT* use tsd
   1648  */
   1649 void
   1650 tsd_exit(void)
   1651 {
   1652 	int i;
   1653 	struct tsd_thread *tsd = curthread->t_tsd;
   1654 
   1655 	if (tsd == NULL)
   1656 		return;
   1657 
   1658 	if (tsd->ts_nkeys == 0) {
   1659 		kmem_free(tsd, sizeof (*tsd));
   1660 		curthread->t_tsd = NULL;
   1661 		return;
   1662 	}
   1663 
   1664 	/*
   1665 	 * lock out tsd_create and tsd_destroy, call
   1666 	 * the destructor, and mark the value as destroyed.
   1667 	 */
   1668 	mutex_enter(&tsd_mutex);
   1669 
   1670 	for (i = 0; i < tsd->ts_nkeys; i++) {
   1671 		if (tsd->ts_value[i] && tsd_destructor[i])
   1672 			(*tsd_destructor[i])(tsd->ts_value[i]);
   1673 		tsd->ts_value[i] = NULL;
   1674 	}
   1675 
   1676 	/*
   1677 	 * remove from linked list of threads with TSD
   1678 	 */
   1679 	if (tsd->ts_next)
   1680 		tsd->ts_next->ts_prev = tsd->ts_prev;
   1681 	if (tsd->ts_prev)
   1682 		tsd->ts_prev->ts_next = tsd->ts_next;
   1683 	if (tsd_list == tsd)
   1684 		tsd_list = tsd->ts_next;
   1685 
   1686 	mutex_exit(&tsd_mutex);
   1687 
   1688 	/*
   1689 	 * free up the TSD
   1690 	 */
   1691 	kmem_free(tsd->ts_value, tsd->ts_nkeys * sizeof (void *));
   1692 	kmem_free(tsd, sizeof (struct tsd_thread));
   1693 	curthread->t_tsd = NULL;
   1694 }
   1695 
   1696 /*
   1697  * realloc
   1698  */
   1699 static void *
   1700 tsd_realloc(void *old, size_t osize, size_t nsize)
   1701 {
   1702 	void *new;
   1703 
   1704 	new = kmem_zalloc(nsize, KM_SLEEP);
   1705 	if (old) {
   1706 		bcopy(old, new, osize);
   1707 		kmem_free(old, osize);
   1708 	}
   1709 	return (new);
   1710 }
   1711 
   1712 /*
   1713  * Return non-zero if an interrupt is being serviced.
   1714  */
   1715 int
   1716 servicing_interrupt()
   1717 {
   1718 	int onintr = 0;
   1719 
   1720 	/* Are we an interrupt thread */
   1721 	if (curthread->t_flag & T_INTR_THREAD)
   1722 		return (1);
   1723 	/* Are we servicing a high level interrupt? */
   1724 	if (CPU_ON_INTR(CPU)) {
   1725 		kpreempt_disable();
   1726 		onintr = CPU_ON_INTR(CPU);
   1727 		kpreempt_enable();
   1728 	}
   1729 	return (onintr);
   1730 }
   1731 
   1732 
   1733 /*
   1734  * Change the dispatch priority of a thread in the system.
   1735  * Used when raising or lowering a thread's priority.
   1736  * (E.g., priority inheritance)
   1737  *
   1738  * Since threads are queued according to their priority, we
   1739  * we must check the thread's state to determine whether it
   1740  * is on a queue somewhere. If it is, we've got to:
   1741  *
   1742  *	o Dequeue the thread.
   1743  *	o Change its effective priority.
   1744  *	o Enqueue the thread.
   1745  *
   1746  * Assumptions: The thread whose priority we wish to change
   1747  * must be locked before we call thread_change_(e)pri().
   1748  * The thread_change(e)pri() function doesn't drop the thread
   1749  * lock--that must be done by its caller.
   1750  */
   1751 void
   1752 thread_change_epri(kthread_t *t, pri_t disp_pri)
   1753 {
   1754 	uint_t	state;
   1755 
   1756 	ASSERT(THREAD_LOCK_HELD(t));
   1757 
   1758 	/*
   1759 	 * If the inherited priority hasn't actually changed,
   1760 	 * just return.
   1761 	 */
   1762 	if (t->t_epri == disp_pri)
   1763 		return;
   1764 
   1765 	state = t->t_state;
   1766 
   1767 	/*
   1768 	 * If it's not on a queue, change the priority with impunity.
   1769 	 */
   1770 	if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
   1771 		t->t_epri = disp_pri;
   1772 		if (state == TS_ONPROC) {
   1773 			cpu_t *cp = t->t_disp_queue->disp_cpu;
   1774 
   1775 			if (t == cp->cpu_dispthread)
   1776 				cp->cpu_dispatch_pri = DISP_PRIO(t);
   1777 		}
   1778 	} else if (state == TS_SLEEP) {
   1779 		/*
   1780 		 * Take the thread out of its sleep queue.
   1781 		 * Change the inherited priority.
   1782 		 * Re-enqueue the thread.
   1783 		 * Each synchronization object exports a function
   1784 		 * to do this in an appropriate manner.
   1785 		 */
   1786 		SOBJ_CHANGE_EPRI(t->t_sobj_ops, t, disp_pri);
   1787 	} else if (state == TS_WAIT) {
   1788 		/*
   1789 		 * Re-enqueue a thread on the wait queue if its
   1790 		 * effective priority needs to change.
   1791 		 */
   1792 		if (disp_pri != t->t_epri)
   1793 			waitq_change_pri(t, disp_pri);
   1794 	} else {
   1795 		/*
   1796 		 * The thread is on a run queue.
   1797 		 * Note: setbackdq() may not put the thread
   1798 		 * back on the same run queue where it originally
   1799 		 * resided.
   1800 		 */
   1801 		(void) dispdeq(t);
   1802 		t->t_epri = disp_pri;
   1803 		setbackdq(t);
   1804 	}
   1805 	schedctl_set_cidpri(t);
   1806 }
   1807 
   1808 /*
   1809  * Function: Change the t_pri field of a thread.
   1810  * Side Effects: Adjust the thread ordering on a run queue
   1811  *		 or sleep queue, if necessary.
   1812  * Returns: 1 if the thread was on a run queue, else 0.
   1813  */
   1814 int
   1815 thread_change_pri(kthread_t *t, pri_t disp_pri, int front)
   1816 {
   1817 	uint_t	state;
   1818 	int	on_rq = 0;
   1819 
   1820 	ASSERT(THREAD_LOCK_HELD(t));
   1821 
   1822 	state = t->t_state;
   1823 	THREAD_WILLCHANGE_PRI(t, disp_pri);
   1824 
   1825 	/*
   1826 	 * If it's not on a queue, change the priority with impunity.
   1827 	 */
   1828 	if ((state & (TS_SLEEP | TS_RUN | TS_WAIT)) == 0) {
   1829 		t->t_pri = disp_pri;
   1830 
   1831 		if (state == TS_ONPROC) {
   1832 			cpu_t *cp = t->t_disp_queue->disp_cpu;
   1833 
   1834 			if (t == cp->cpu_dispthread)
   1835 				cp->cpu_dispatch_pri = DISP_PRIO(t);
   1836 		}
   1837 	} else if (state == TS_SLEEP) {
   1838 		/*
   1839 		 * If the priority has changed, take the thread out of
   1840 		 * its sleep queue and change the priority.
   1841 		 * Re-enqueue the thread.
   1842 		 * Each synchronization object exports a function
   1843 		 * to do this in an appropriate manner.
   1844 		 */
   1845 		if (disp_pri != t->t_pri)
   1846 			SOBJ_CHANGE_PRI(t->t_sobj_ops, t, disp_pri);
   1847 	} else if (state == TS_WAIT) {
   1848 		/*
   1849 		 * Re-enqueue a thread on the wait queue if its
   1850 		 * priority needs to change.
   1851 		 */
   1852 		if (disp_pri != t->t_pri)
   1853 			waitq_change_pri(t, disp_pri);
   1854 	} else {
   1855 		/*
   1856 		 * The thread is on a run queue.
   1857 		 * Note: setbackdq() may not put the thread
   1858 		 * back on the same run queue where it originally
   1859 		 * resided.
   1860 		 *
   1861 		 * We still requeue the thread even if the priority
   1862 		 * is unchanged to preserve round-robin (and other)
   1863 		 * effects between threads of the same priority.
   1864 		 */
   1865 		on_rq = dispdeq(t);
   1866 		ASSERT(on_rq);
   1867 		t->t_pri = disp_pri;
   1868 		if (front) {
   1869 			setfrontdq(t);
   1870 		} else {
   1871 			setbackdq(t);
   1872 		}
   1873 	}
   1874 	schedctl_set_cidpri(t);
   1875 	return (on_rq);
   1876 }
   1877 
   1878 /*
   1879  * Tunable kmem_stackinfo is set, fill the kernel thread stack with a
   1880  * specific pattern.
   1881  */
   1882 static void
   1883 stkinfo_begin(kthread_t *t)
   1884 {
   1885 	caddr_t	start;	/* stack start */
   1886 	caddr_t	end;	/* stack end  */
   1887 	uint64_t *ptr;	/* pattern pointer */
   1888 
   1889 	/*
   1890 	 * Stack grows up or down, see thread_create(),
   1891 	 * compute stack memory area start and end (start < end).
   1892 	 */
   1893 	if (t->t_stk > t->t_stkbase) {
   1894 		/* stack grows down */
   1895 		start = t->t_stkbase;
   1896 		end = t->t_stk;
   1897 	} else {
   1898 		/* stack grows up */
   1899 		start = t->t_stk;
   1900 		end = t->t_stkbase;
   1901 	}
   1902 
   1903 	/*
   1904 	 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
   1905 	 * alignement for start and end in stack area boundaries
   1906 	 * (protection against corrupt t_stkbase/t_stk data).
   1907 	 */
   1908 	if ((((uintptr_t)start) & 0x7) != 0) {
   1909 		start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
   1910 	}
   1911 	end = (caddr_t)(((uintptr_t)end) & (~0x7));
   1912 
   1913 	if ((end <= start) || (end - start) > (1024 * 1024)) {
   1914 		/* negative or stack size > 1 meg, assume bogus */
   1915 		return;
   1916 	}
   1917 
   1918 	/* fill stack area with a pattern (instead of zeros) */
   1919 	ptr = (uint64_t *)((void *)start);
   1920 	while (ptr < (uint64_t *)((void *)end)) {
   1921 		*ptr++ = KMEM_STKINFO_PATTERN;
   1922 	}
   1923 }
   1924 
   1925 
   1926 /*
   1927  * Tunable kmem_stackinfo is set, create stackinfo log if doesn't already exist,
   1928  * compute the percentage of kernel stack really used, and set in the log
   1929  * if it's the latest highest percentage.
   1930  */
   1931 static void
   1932 stkinfo_end(kthread_t *t)
   1933 {
   1934 	caddr_t	start;	/* stack start */
   1935 	caddr_t	end;	/* stack end  */
   1936 	uint64_t *ptr;	/* pattern pointer */
   1937 	size_t stksz;	/* stack size */
   1938 	size_t smallest = 0;
   1939 	size_t percent = 0;
   1940 	uint_t index = 0;
   1941 	uint_t i;
   1942 	static size_t smallest_percent = (size_t)-1;
   1943 	static uint_t full = 0;
   1944 
   1945 	/* create the stackinfo log, if doesn't already exist */
   1946 	mutex_enter(&kmem_stkinfo_lock);
   1947 	if (kmem_stkinfo_log == NULL) {
   1948 		kmem_stkinfo_log = (kmem_stkinfo_t *)
   1949 		    kmem_zalloc(KMEM_STKINFO_LOG_SIZE *
   1950 		    (sizeof (kmem_stkinfo_t)), KM_NOSLEEP);
   1951 		if (kmem_stkinfo_log == NULL) {
   1952 			mutex_exit(&kmem_stkinfo_lock);
   1953 			return;
   1954 		}
   1955 	}
   1956 	mutex_exit(&kmem_stkinfo_lock);
   1957 
   1958 	/*
   1959 	 * Stack grows up or down, see thread_create(),
   1960 	 * compute stack memory area start and end (start < end).
   1961 	 */
   1962 	if (t->t_stk > t->t_stkbase) {
   1963 		/* stack grows down */
   1964 		start = t->t_stkbase;
   1965 		end = t->t_stk;
   1966 	} else {
   1967 		/* stack grows up */
   1968 		start = t->t_stk;
   1969 		end = t->t_stkbase;
   1970 	}
   1971 
   1972 	/* stack size as found in kthread_t */
   1973 	stksz = end - start;
   1974 
   1975 	/*
   1976 	 * Stackinfo pattern size is 8 bytes. Ensure proper 8 bytes
   1977 	 * alignement for start and end in stack area boundaries
   1978 	 * (protection against corrupt t_stkbase/t_stk data).
   1979 	 */
   1980 	if ((((uintptr_t)start) & 0x7) != 0) {
   1981 		start = (caddr_t)((((uintptr_t)start) & (~0x7)) + 8);
   1982 	}
   1983 	end = (caddr_t)(((uintptr_t)end) & (~0x7));
   1984 
   1985 	if ((end <= start) || (end - start) > (1024 * 1024)) {
   1986 		/* negative or stack size > 1 meg, assume bogus */
   1987 		return;
   1988 	}
   1989 
   1990 	/* search until no pattern in the stack */
   1991 	if (t->t_stk > t->t_stkbase) {
   1992 		/* stack grows down */
   1993 #if defined(__i386) || defined(__amd64)
   1994 		/*
   1995 		 * 6 longs are pushed on stack, see thread_load(). Skip
   1996 		 * them, so if kthread has never run, percent is zero.
   1997 		 * 8 bytes alignement is preserved for a 32 bit kernel,
   1998 		 * 6 x 4 = 24, 24 is a multiple of 8.
   1999 		 *
   2000 		 */
   2001 		end -= (6 * sizeof (long));
   2002 #endif
   2003 		ptr = (uint64_t *)((void *)start);
   2004 		while (ptr < (uint64_t *)((void *)end)) {
   2005 			if (*ptr != KMEM_STKINFO_PATTERN) {
   2006 				percent = stkinfo_percent(end,
   2007 				    start, (caddr_t)ptr);
   2008 				break;
   2009 			}
   2010 			ptr++;
   2011 		}
   2012 	} else {
   2013 		/* stack grows up */
   2014 		ptr = (uint64_t *)((void *)end);
   2015 		ptr--;
   2016 		while (ptr >= (uint64_t *)((void *)start)) {
   2017 			if (*ptr != KMEM_STKINFO_PATTERN) {
   2018 				percent = stkinfo_percent(start,
   2019 				    end, (caddr_t)ptr);
   2020 				break;
   2021 			}
   2022 			ptr--;
   2023 		}
   2024 	}
   2025 
   2026 	DTRACE_PROBE3(stack__usage, kthread_t *, t,
   2027 	    size_t, stksz, size_t, percent);
   2028 
   2029 	if (percent == 0) {
   2030 		return;
   2031 	}
   2032 
   2033 	mutex_enter(&kmem_stkinfo_lock);
   2034 	if (full == KMEM_STKINFO_LOG_SIZE && percent < smallest_percent) {
   2035 		/*
   2036 		 * The log is full and already contains the highest values
   2037 		 */
   2038 		mutex_exit(&kmem_stkinfo_lock);
   2039 		return;
   2040 	}
   2041 
   2042 	/* keep a log of the highest used stack */
   2043 	for (i = 0; i < KMEM_STKINFO_LOG_SIZE; i++) {
   2044 		if (kmem_stkinfo_log[i].percent == 0) {
   2045 			index = i;
   2046 			full++;
   2047 			break;
   2048 		}
   2049 		if (smallest == 0) {
   2050 			smallest = kmem_stkinfo_log[i].percent;
   2051 			index = i;
   2052 			continue;
   2053 		}
   2054 		if (kmem_stkinfo_log[i].percent < smallest) {
   2055 			smallest = kmem_stkinfo_log[i].percent;
   2056 			index = i;
   2057 		}
   2058 	}
   2059 
   2060 	if (percent >= kmem_stkinfo_log[index].percent) {
   2061 		kmem_stkinfo_log[index].kthread = (caddr_t)t;
   2062 		kmem_stkinfo_log[index].t_startpc = (caddr_t)t->t_startpc;
   2063 		kmem_stkinfo_log[index].start = start;
   2064 		kmem_stkinfo_log[index].stksz = stksz;
   2065 		kmem_stkinfo_log[index].percent = percent;
   2066 		kmem_stkinfo_log[index].t_tid = t->t_tid;
   2067 		kmem_stkinfo_log[index].cmd[0] = '\0';
   2068 		if (t->t_tid != 0) {
   2069 			stksz = strlen((t->t_procp)->p_user.u_comm);
   2070 			if (stksz >= KMEM_STKINFO_STR_SIZE) {
   2071 				stksz = KMEM_STKINFO_STR_SIZE - 1;
   2072 				kmem_stkinfo_log[index].cmd[stksz] = '\0';
   2073 			} else {
   2074 				stksz += 1;
   2075 			}
   2076 			(void) memcpy(kmem_stkinfo_log[index].cmd,
   2077 			    (t->t_procp)->p_user.u_comm, stksz);
   2078 		}
   2079 		if (percent < smallest_percent) {
   2080 			smallest_percent = percent;
   2081 		}
   2082 	}
   2083 	mutex_exit(&kmem_stkinfo_lock);
   2084 }
   2085 
   2086 /*
   2087  * Tunable kmem_stackinfo is set, compute stack utilization percentage.
   2088  */
   2089 static size_t
   2090 stkinfo_percent(caddr_t t_stk, caddr_t t_stkbase, caddr_t sp)
   2091 {
   2092 	size_t percent;
   2093 	size_t s;
   2094 
   2095 	if (t_stk > t_stkbase) {
   2096 		/* stack grows down */
   2097 		if (sp > t_stk) {
   2098 			return (0);
   2099 		}
   2100 		if (sp < t_stkbase) {
   2101 			return (100);
   2102 		}
   2103 		percent = t_stk - sp + 1;
   2104 		s = t_stk - t_stkbase + 1;
   2105 	} else {
   2106 		/* stack grows up */
   2107 		if (sp < t_stk) {
   2108 			return (0);
   2109 		}
   2110 		if (sp > t_stkbase) {
   2111 			return (100);
   2112 		}
   2113 		percent = sp - t_stk + 1;
   2114 		s = t_stkbase - t_stk + 1;
   2115 	}
   2116 	percent = ((100 * percent) / s) + 1;
   2117 	if (percent > 100) {
   2118 		percent = 100;
   2119 	}
   2120 	return (percent);
   2121 }
   2122