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