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