xref: /titanic_51/usr/src/uts/i86pc/os/intr.c (revision 45916cd2fec6e79bca5dee0421bd39e3c2910d1e)
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/cpuvar.h>
29 #include <sys/regset.h>
30 #include <sys/psw.h>
31 #include <sys/types.h>
32 #include <sys/thread.h>
33 #include <sys/systm.h>
34 #include <sys/segments.h>
35 #include <sys/pcb.h>
36 #include <sys/trap.h>
37 #include <sys/ftrace.h>
38 #include <sys/traptrace.h>
39 #include <sys/clock.h>
40 #include <sys/panic.h>
41 #include <sys/disp.h>
42 #include <vm/seg_kp.h>
43 #include <sys/stack.h>
44 #include <sys/sysmacros.h>
45 #include <sys/cmn_err.h>
46 #include <sys/kstat.h>
47 #include <sys/smp_impldefs.h>
48 #include <sys/pool_pset.h>
49 #include <sys/zone.h>
50 #include <sys/bitmap.h>
51 
52 #if defined(__amd64)
53 
54 #if defined(__lint)
55 /*
56  * atomic_btr32() is a gcc __inline__ function, defined in <asm/bitmap.h>
57  * For lint purposes, define it here.
58  */
59 uint_t
60 atomic_btr32(uint32_t *pending, uint_t pil)
61 {
62 	return (*pending &= ~(1 << pil));
63 }
64 #else
65 
66 extern uint_t atomic_btr32(uint32_t *pending, uint_t pil);
67 
68 #endif
69 
70 /*
71  * This code is amd64-only for now, but as time permits, we should
72  * use this on i386 too.
73  */
74 
75 /*
76  * Some questions to ponder:
77  * -	in several of these routines, we make multiple calls to tsc_read()
78  *	without invoking functions .. couldn't we just reuse the same
79  *	timestamp sometimes?
80  * -	if we have the inline, we can probably make set_base_spl be a
81  *	C routine too.
82  */
83 
84 static uint_t
85 bsrw_insn(uint16_t mask)
86 {
87 	uint_t index = sizeof (mask) * NBBY - 1;
88 
89 	ASSERT(mask != 0);
90 
91 	while ((mask & (1 << index)) == 0)
92 		index--;
93 	return (index);
94 }
95 
96 /*
97  * Do all the work necessary to set up the cpu and thread structures
98  * to dispatch a high-level interrupt.
99  *
100  * Returns 0 if we're -not- already on the high-level interrupt stack,
101  * (and *must* switch to it), non-zero if we are already on that stack.
102  *
103  * Called with interrupts masked.
104  * The 'pil' is already set to the appropriate level for rp->r_trapno.
105  */
106 int
107 hilevel_intr_prolog(struct cpu *cpu, uint_t pil, uint_t oldpil, struct regs *rp)
108 {
109 	struct machcpu *mcpu = &cpu->cpu_m;
110 	uint_t mask;
111 	hrtime_t intrtime;
112 
113 	ASSERT(pil > LOCK_LEVEL);
114 
115 	if (pil == CBE_HIGH_PIL) {
116 		cpu->cpu_profile_pil = oldpil;
117 		if (USERMODE(rp->r_cs)) {
118 			cpu->cpu_profile_pc = 0;
119 			cpu->cpu_profile_upc = rp->r_pc;
120 		} else {
121 			cpu->cpu_profile_pc = rp->r_pc;
122 			cpu->cpu_profile_upc = 0;
123 		}
124 	}
125 
126 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
127 	if (mask != 0) {
128 		int nestpil;
129 
130 		/*
131 		 * We have interrupted another high-level interrupt.
132 		 * Load starting timestamp, compute interval, update
133 		 * cumulative counter.
134 		 */
135 		nestpil = bsrw_insn((uint16_t)mask);
136 		ASSERT(nestpil < pil);
137 		intrtime = tsc_read() -
138 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
139 		mcpu->intrstat[nestpil][0] += intrtime;
140 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
141 		/*
142 		 * Another high-level interrupt is active below this one, so
143 		 * there is no need to check for an interrupt thread.  That
144 		 * will be done by the lowest priority high-level interrupt
145 		 * active.
146 		 */
147 	} else {
148 		kthread_t *t = cpu->cpu_thread;
149 
150 		/*
151 		 * See if we are interrupting a low-level interrupt thread.
152 		 * If so, account for its time slice only if its time stamp
153 		 * is non-zero.
154 		 */
155 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
156 			intrtime = tsc_read() - t->t_intr_start;
157 			mcpu->intrstat[t->t_pil][0] += intrtime;
158 			cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
159 			t->t_intr_start = 0;
160 		}
161 	}
162 
163 	/*
164 	 * Store starting timestamp in CPU structure for this PIL.
165 	 */
166 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = tsc_read();
167 
168 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
169 
170 	if (pil == 15) {
171 		/*
172 		 * To support reentrant level 15 interrupts, we maintain a
173 		 * recursion count in the top half of cpu_intr_actv.  Only
174 		 * when this count hits zero do we clear the PIL 15 bit from
175 		 * the lower half of cpu_intr_actv.
176 		 */
177 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
178 		(*refcntp)++;
179 	}
180 
181 	mask = cpu->cpu_intr_actv;
182 
183 	cpu->cpu_intr_actv |= (1 << pil);
184 
185 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
186 }
187 
188 /*
189  * Does most of the work of returning from a high level interrupt.
190  *
191  * Returns 0 if there are no more high level interrupts (in which
192  * case we must switch back to the interrupted thread stack) or
193  * non-zero if there are more (in which case we should stay on it).
194  *
195  * Called with interrupts masked
196  */
197 int
198 hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
199 {
200 	struct machcpu *mcpu = &cpu->cpu_m;
201 	uint_t mask;
202 	hrtime_t intrtime;
203 
204 	ASSERT(mcpu->mcpu_pri == pil);
205 
206 	cpu->cpu_stats.sys.intr[pil - 1]++;
207 
208 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
209 
210 	if (pil == 15) {
211 		/*
212 		 * To support reentrant level 15 interrupts, we maintain a
213 		 * recursion count in the top half of cpu_intr_actv.  Only
214 		 * when this count hits zero do we clear the PIL 15 bit from
215 		 * the lower half of cpu_intr_actv.
216 		 */
217 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
218 
219 		ASSERT(*refcntp > 0);
220 
221 		if (--(*refcntp) == 0)
222 			cpu->cpu_intr_actv &= ~(1 << pil);
223 	} else {
224 		cpu->cpu_intr_actv &= ~(1 << pil);
225 	}
226 
227 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
228 
229 	intrtime = tsc_read() - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
230 	mcpu->intrstat[pil][0] += intrtime;
231 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
232 
233 	/*
234 	 * Check for lower-pil nested high-level interrupt beneath
235 	 * current one.  If so, place a starting timestamp in its
236 	 * pil_high_start entry.
237 	 */
238 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
239 	if (mask != 0) {
240 		int nestpil;
241 
242 		/*
243 		 * find PIL of nested interrupt
244 		 */
245 		nestpil = bsrw_insn((uint16_t)mask);
246 		ASSERT(nestpil < pil);
247 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = tsc_read();
248 		/*
249 		 * (Another high-level interrupt is active below this one,
250 		 * so there is no need to check for an interrupt
251 		 * thread.  That will be done by the lowest priority
252 		 * high-level interrupt active.)
253 		 */
254 	} else {
255 		/*
256 		 * Check to see if there is a low-level interrupt active.
257 		 * If so, place a starting timestamp in the thread
258 		 * structure.
259 		 */
260 		kthread_t *t = cpu->cpu_thread;
261 
262 		if (t->t_flag & T_INTR_THREAD)
263 			t->t_intr_start = tsc_read();
264 	}
265 
266 	mcpu->mcpu_pri = oldpil;
267 	(void) (*setlvlx)(oldpil, vecnum);
268 
269 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
270 }
271 
272 /*
273  * Set up the cpu, thread and interrupt thread structures for
274  * executing an interrupt thread.  The new stack pointer of the
275  * interrupt thread (which *must* be switched to) is returned.
276  */
277 caddr_t
278 intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
279 {
280 	struct machcpu *mcpu = &cpu->cpu_m;
281 	kthread_t *t, *volatile it;
282 
283 	ASSERT(pil > 0);
284 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
285 	cpu->cpu_intr_actv |= (1 << pil);
286 
287 	/*
288 	 * Get set to run an interrupt thread.
289 	 * There should always be an interrupt thread, since we
290 	 * allocate one for each level on each CPU.
291 	 *
292 	 * t_intr_start could be zero due to cpu_intr_swtch_enter.
293 	 */
294 	t = cpu->cpu_thread;
295 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
296 		hrtime_t intrtime = tsc_read() - t->t_intr_start;
297 		mcpu->intrstat[t->t_pil][0] += intrtime;
298 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
299 		t->t_intr_start = 0;
300 	}
301 
302 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
303 
304 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
305 
306 	/*
307 	 * unlink the interrupt thread off the cpu
308 	 *
309 	 * Note that the code in kcpc_overflow_intr -relies- on the
310 	 * ordering of events here - in particular that t->t_lwp of
311 	 * the interrupt thread is set to the pinned thread *before*
312 	 * curthread is changed.
313 	 */
314 	it = cpu->cpu_intr_thread;
315 	cpu->cpu_intr_thread = it->t_link;
316 	it->t_intr = t;
317 	it->t_lwp = t->t_lwp;
318 
319 	/*
320 	 * (threads on the interrupt thread free list could have state
321 	 * preset to TS_ONPROC, but it helps in debugging if
322 	 * they're TS_FREE.)
323 	 */
324 	it->t_state = TS_ONPROC;
325 
326 	cpu->cpu_thread = it;		/* new curthread on this cpu */
327 	it->t_pil = (uchar_t)pil;
328 	it->t_pri = intr_pri + (pri_t)pil;
329 	it->t_intr_start = tsc_read();
330 
331 	return (it->t_stk);
332 }
333 
334 
335 #ifdef DEBUG
336 int intr_thread_cnt;
337 #endif
338 
339 /*
340  * Called with interrupts disabled
341  */
342 void
343 intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
344 {
345 	struct machcpu *mcpu = &cpu->cpu_m;
346 	kthread_t *t;
347 	kthread_t *it = cpu->cpu_thread;	/* curthread */
348 	uint_t pil, basespl;
349 	hrtime_t intrtime;
350 
351 	pil = it->t_pil;
352 	cpu->cpu_stats.sys.intr[pil - 1]++;
353 
354 	ASSERT(it->t_intr_start != 0);
355 	intrtime = tsc_read() - it->t_intr_start;
356 	mcpu->intrstat[pil][0] += intrtime;
357 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
358 
359 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
360 	cpu->cpu_intr_actv &= ~(1 << pil);
361 
362 	/*
363 	 * If there is still an interrupted thread underneath this one
364 	 * then the interrupt was never blocked and the return is
365 	 * fairly simple.  Otherwise it isn't.
366 	 */
367 	if ((t = it->t_intr) == NULL) {
368 		/*
369 		 * The interrupted thread is no longer pinned underneath
370 		 * the interrupt thread.  This means the interrupt must
371 		 * have blocked, and the interrupted thread has been
372 		 * unpinned, and has probably been running around the
373 		 * system for a while.
374 		 *
375 		 * Since there is no longer a thread under this one, put
376 		 * this interrupt thread back on the CPU's free list and
377 		 * resume the idle thread which will dispatch the next
378 		 * thread to run.
379 		 */
380 #ifdef DEBUG
381 		intr_thread_cnt++;
382 #endif
383 		cpu->cpu_stats.sys.intrblk++;
384 		/*
385 		 * Set CPU's base SPL based on active interrupts bitmask
386 		 */
387 		set_base_spl();
388 		basespl = cpu->cpu_base_spl;
389 		mcpu->mcpu_pri = basespl;
390 		(*setlvlx)(basespl, vec);
391 		(void) splhigh();
392 		it->t_state = TS_FREE;
393 		/*
394 		 * Return interrupt thread to pool
395 		 */
396 		it->t_link = cpu->cpu_intr_thread;
397 		cpu->cpu_intr_thread = it;
398 		swtch();
399 		/*NOTREACHED*/
400 	}
401 
402 	/*
403 	 * Return interrupt thread to the pool
404 	 */
405 	it->t_link = cpu->cpu_intr_thread;
406 	cpu->cpu_intr_thread = it;
407 	it->t_state = TS_FREE;
408 
409 	basespl = cpu->cpu_base_spl;
410 	pil = MAX(oldpil, basespl);
411 	mcpu->mcpu_pri = pil;
412 	(*setlvlx)(pil, vec);
413 	t->t_intr_start = tsc_read();
414 	cpu->cpu_thread = t;
415 }
416 
417 /*
418  * Called with interrupts disabled by an interrupt thread to determine
419  * how much time has elapsed. See interrupt.s:intr_get_time() for detailed
420  * theory of operation.
421  */
422 uint64_t
423 intr_thread_get_time(struct cpu *cpu)
424 {
425 	struct machcpu *mcpu = &cpu->cpu_m;
426 	kthread_t *t = cpu->cpu_thread;
427 	uint64_t time, delta, ret;
428 	uint_t pil = t->t_pil;
429 
430 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
431 	ASSERT(t->t_flag & T_INTR_THREAD);
432 	ASSERT(pil != 0);
433 	ASSERT(t->t_intr_start != 0);
434 
435 	time = tsc_read();
436 	delta = time - t->t_intr_start;
437 	t->t_intr_start = time;
438 
439 	time = mcpu->intrstat[pil][0] + delta;
440 	ret = time - mcpu->intrstat[pil][1];
441 	mcpu->intrstat[pil][0] = time;
442 	mcpu->intrstat[pil][1] = time;
443 
444 	return (ret);
445 }
446 
447 caddr_t
448 dosoftint_prolog(
449 	struct cpu *cpu,
450 	caddr_t stackptr,
451 	uint32_t st_pending,
452 	uint_t oldpil)
453 {
454 	kthread_t *t, *volatile it;
455 	struct machcpu *mcpu = &cpu->cpu_m;
456 	uint_t pil;
457 
458 top:
459 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
460 
461 	pil = bsrw_insn((uint16_t)st_pending);
462 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
463 		return (0);
464 
465 	/*
466 	 * XX64	Sigh.
467 	 *
468 	 * This is a transliteration of the i386 assembler code for
469 	 * soft interrupts.  One question is "why does this need
470 	 * to be atomic?"  One possible race is -other- processors
471 	 * posting soft interrupts to us in set_pending() i.e. the
472 	 * CPU might get preempted just after the address computation,
473 	 * but just before the atomic transaction, so another CPU would
474 	 * actually set the original CPU's st_pending bit.  However,
475 	 * it looks like it would be simpler to disable preemption there.
476 	 * Are there other races for which preemption control doesn't work?
477 	 *
478 	 * The i386 assembler version -also- checks to see if the bit
479 	 * being cleared was actually set; if it wasn't, it rechecks
480 	 * for more.  This seems a bit strange, as the only code that
481 	 * ever clears the bit is -this- code running with interrupts
482 	 * disabled on -this- CPU.  This code would probably be cheaper:
483 	 *
484 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
485 	 *   ~(1 << pil));
486 	 *
487 	 * and t->t_preempt--/++ around set_pending() even cheaper,
488 	 * but at this point, correctness is critical, so we slavishly
489 	 * emulate the i386 port.
490 	 */
491 	if (atomic_btr32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, pil)
492 	    == 0) {
493 		st_pending = mcpu->mcpu_softinfo.st_pending;
494 		goto top;
495 	}
496 
497 	mcpu->mcpu_pri = pil;
498 	(*setspl)(pil);
499 
500 	/*
501 	 * Get set to run interrupt thread.
502 	 * There should always be an interrupt thread since we
503 	 * allocate one for each level on the CPU.
504 	 */
505 	it = cpu->cpu_intr_thread;
506 	cpu->cpu_intr_thread = it->t_link;
507 
508 	/* t_intr_start could be zero due to cpu_intr_swtch_enter. */
509 	t = cpu->cpu_thread;
510 	if ((t->t_flag & T_INTR_THREAD) && t->t_intr_start != 0) {
511 		hrtime_t intrtime = tsc_read() - t->t_intr_start;
512 		mcpu->intrstat[pil][0] += intrtime;
513 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
514 		t->t_intr_start = 0;
515 	}
516 
517 	/*
518 	 * Note that the code in kcpc_overflow_intr -relies- on the
519 	 * ordering of events here - in particular that t->t_lwp of
520 	 * the interrupt thread is set to the pinned thread *before*
521 	 * curthread is changed.
522 	 */
523 	it->t_lwp = t->t_lwp;
524 	it->t_state = TS_ONPROC;
525 
526 	/*
527 	 * Push interrupted thread onto list from new thread.
528 	 * Set the new thread as the current one.
529 	 * Set interrupted thread's T_SP because if it is the idle thread,
530 	 * resume() may use that stack between threads.
531 	 */
532 
533 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
534 	t->t_sp = (uintptr_t)stackptr;
535 
536 	it->t_intr = t;
537 	cpu->cpu_thread = it;
538 
539 	/*
540 	 * Set bit for this pil in CPU's interrupt active bitmask.
541 	 */
542 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
543 	cpu->cpu_intr_actv |= (1 << pil);
544 
545 	/*
546 	 * Initialize thread priority level from intr_pri
547 	 */
548 	it->t_pil = (uchar_t)pil;
549 	it->t_pri = (pri_t)pil + intr_pri;
550 	it->t_intr_start = tsc_read();
551 
552 	return (it->t_stk);
553 }
554 
555 void
556 dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
557 {
558 	struct machcpu *mcpu = &cpu->cpu_m;
559 	kthread_t *t, *it;
560 	uint_t pil, basespl;
561 	hrtime_t intrtime;
562 
563 	it = cpu->cpu_thread;
564 	pil = it->t_pil;
565 
566 	cpu->cpu_stats.sys.intr[pil - 1]++;
567 
568 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
569 	cpu->cpu_intr_actv &= ~(1 << pil);
570 	intrtime = tsc_read() - it->t_intr_start;
571 	mcpu->intrstat[pil][0] += intrtime;
572 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
573 
574 	/*
575 	 * If there is still an interrupted thread underneath this one
576 	 * then the interrupt was never blocked and the return is
577 	 * fairly simple.  Otherwise it isn't.
578 	 */
579 	if ((t = it->t_intr) == NULL) {
580 		/*
581 		 * Put thread back on the interrupt thread list.
582 		 * This was an interrupt thread, so set CPU's base SPL.
583 		 */
584 		set_base_spl();
585 		it->t_state = TS_FREE;
586 		it->t_link = cpu->cpu_intr_thread;
587 		cpu->cpu_intr_thread = it;
588 		(void) splhigh();
589 		swtch();
590 		/*NOTREACHED*/
591 	}
592 	it->t_link = cpu->cpu_intr_thread;
593 	cpu->cpu_intr_thread = it;
594 	it->t_state = TS_FREE;
595 	cpu->cpu_thread = t;
596 	if (t->t_flag & T_INTR_THREAD)
597 		t->t_intr_start = tsc_read();
598 	basespl = cpu->cpu_base_spl;
599 	pil = MAX(oldpil, basespl);
600 	mcpu->mcpu_pri = pil;
601 	(*setspl)(pil);
602 }
603 
604 /*
605  * Make the interrupted thread 'to' be runnable.
606  *
607  * Since t->t_sp has already been saved, t->t_pc is all
608  * that needs to be set in this function.
609  *
610  * Returns the interrupt level of the interrupt thread.
611  */
612 int
613 intr_passivate(
614 	kthread_t *it,		/* interrupt thread */
615 	kthread_t *t)		/* interrupted thread */
616 {
617 	extern void _sys_rtt();
618 
619 	ASSERT(it->t_flag & T_INTR_THREAD);
620 	ASSERT(SA(t->t_sp) == t->t_sp);
621 
622 	t->t_pc = (uintptr_t)_sys_rtt;
623 	return (it->t_pil);
624 }
625 
626 #endif	/* __amd64 */
627 
628 /*
629  * Create interrupt kstats for this CPU.
630  */
631 void
632 cpu_create_intrstat(cpu_t *cp)
633 {
634 	int		i;
635 	kstat_t		*intr_ksp;
636 	kstat_named_t	*knp;
637 	char		name[KSTAT_STRLEN];
638 	zoneid_t	zoneid;
639 
640 	ASSERT(MUTEX_HELD(&cpu_lock));
641 
642 	if (pool_pset_enabled())
643 		zoneid = GLOBAL_ZONEID;
644 	else
645 		zoneid = ALL_ZONES;
646 
647 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
648 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
649 
650 	/*
651 	 * Initialize each PIL's named kstat
652 	 */
653 	if (intr_ksp != NULL) {
654 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
655 		knp = (kstat_named_t *)intr_ksp->ks_data;
656 		intr_ksp->ks_private = cp;
657 		for (i = 0; i < PIL_MAX; i++) {
658 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
659 			    i + 1);
660 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
661 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
662 			    i + 1);
663 			kstat_named_init(&knp[(i * 2) + 1], name,
664 			    KSTAT_DATA_UINT64);
665 		}
666 		kstat_install(intr_ksp);
667 	}
668 }
669 
670 /*
671  * Delete interrupt kstats for this CPU.
672  */
673 void
674 cpu_delete_intrstat(cpu_t *cp)
675 {
676 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
677 }
678 
679 /*
680  * Convert interrupt statistics from CPU ticks to nanoseconds and
681  * update kstat.
682  */
683 int
684 cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
685 {
686 	kstat_named_t	*knp = ksp->ks_data;
687 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
688 	int		i;
689 	hrtime_t	hrt;
690 
691 	if (rw == KSTAT_WRITE)
692 		return (EACCES);
693 
694 	for (i = 0; i < PIL_MAX; i++) {
695 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
696 		tsc_scalehrtime(&hrt);
697 		knp[i * 2].value.ui64 = (uint64_t)hrt;
698 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
699 	}
700 
701 	return (0);
702 }
703 
704 /*
705  * An interrupt thread is ending a time slice, so compute the interval it
706  * ran for and update the statistic for its PIL.
707  */
708 void
709 cpu_intr_swtch_enter(kthread_id_t t)
710 {
711 	uint64_t	interval;
712 	uint64_t	start;
713 	cpu_t		*cpu;
714 
715 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
716 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
717 
718 	/*
719 	 * We could be here with a zero timestamp. This could happen if:
720 	 * an interrupt thread which no longer has a pinned thread underneath
721 	 * it (i.e. it blocked at some point in its past) has finished running
722 	 * its handler. intr_thread() updated the interrupt statistic for its
723 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
724 	 * return to, swtch() gets called and we end up here.
725 	 *
726 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
727 	 * we don't use in the functions above, because we're not called
728 	 * with interrupts blocked, but the epilog/prolog functions are.
729 	 */
730 	if (t->t_intr_start) {
731 		do {
732 			start = t->t_intr_start;
733 			interval = tsc_read() - start;
734 		} while (cas64(&t->t_intr_start, start, 0) != start);
735 		cpu = CPU;
736 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
737 
738 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
739 		    interval);
740 	} else
741 		ASSERT(t->t_intr == NULL);
742 }
743 
744 /*
745  * An interrupt thread is returning from swtch(). Place a starting timestamp
746  * in its thread structure.
747  */
748 void
749 cpu_intr_swtch_exit(kthread_id_t t)
750 {
751 	uint64_t ts;
752 
753 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
754 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
755 
756 	do {
757 		ts = t->t_intr_start;
758 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
759 }
760