xref: /titanic_51/usr/src/uts/i86pc/os/intr.c (revision 8eea8e29cc4374d1ee24c25a07f45af132db3499)
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 
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 		mcpu->intrstat[nestpil] += tsc_read() -
138 		    mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)];
139 		/*
140 		 * Another high-level interrupt is active below this one, so
141 		 * there is no need to check for an interrupt thread.  That
142 		 * will be done by the lowest priority high-level interrupt
143 		 * active.
144 		 */
145 	} else {
146 		kthread_t *t = cpu->cpu_thread;
147 
148 		/*
149 		 * See if we are interrupting a low-level interrupt thread.
150 		 * If so, account for its time slice only if its time stamp
151 		 * is non-zero.
152 		 */
153 		if ((t->t_flag & T_INTR_THREAD) != 0 && t->t_intr_start != 0) {
154 			mcpu->intrstat[t->t_pil] +=
155 			    tsc_read() - t->t_intr_start;
156 			t->t_intr_start = 0;
157 		}
158 	}
159 
160 	/*
161 	 * Store starting timestamp in CPU structure for this PIL.
162 	 */
163 	mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] = tsc_read();
164 
165 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
166 
167 	if (pil == 15) {
168 		/*
169 		 * To support reentrant level 15 interrupts, we maintain a
170 		 * recursion count in the top half of cpu_intr_actv.  Only
171 		 * when this count hits zero do we clear the PIL 15 bit from
172 		 * the lower half of cpu_intr_actv.
173 		 */
174 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
175 		(*refcntp)++;
176 	}
177 
178 	mask = cpu->cpu_intr_actv;
179 
180 	cpu->cpu_intr_actv |= (1 << pil);
181 
182 	return (mask & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
183 }
184 
185 /*
186  * Does most of the work of returning from a high level interrupt.
187  *
188  * Returns 0 if there are no more high level interrupts (in which
189  * case we must switch back to the interrupted thread stack) or
190  * non-zero if there are more (in which case we should stay on it).
191  *
192  * Called with interrupts masked
193  */
194 int
195 hilevel_intr_epilog(struct cpu *cpu, uint_t pil, uint_t oldpil, uint_t vecnum)
196 {
197 	struct machcpu *mcpu = &cpu->cpu_m;
198 	uint_t mask;
199 
200 	ASSERT(mcpu->mcpu_pri == pil);
201 
202 	cpu->cpu_stats.sys.intr[pil - 1]++;
203 
204 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
205 
206 	if (pil == 15) {
207 		/*
208 		 * To support reentrant level 15 interrupts, we maintain a
209 		 * recursion count in the top half of cpu_intr_actv.  Only
210 		 * when this count hits zero do we clear the PIL 15 bit from
211 		 * the lower half of cpu_intr_actv.
212 		 */
213 		uint16_t *refcntp = (uint16_t *)&cpu->cpu_intr_actv + 1;
214 
215 		ASSERT(*refcntp > 0);
216 
217 		if (--(*refcntp) == 0)
218 			cpu->cpu_intr_actv &= ~(1 << pil);
219 	} else {
220 		cpu->cpu_intr_actv &= ~(1 << pil);
221 	}
222 
223 	ASSERT(mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)] != 0);
224 
225 	mcpu->intrstat[pil] +=
226 	    tsc_read() - mcpu->pil_high_start[pil - (LOCK_LEVEL + 1)];
227 
228 	/*
229 	 * Check for lower-pil nested high-level interrupt beneath
230 	 * current one.  If so, place a starting timestamp in its
231 	 * pil_high_start entry.
232 	 */
233 	mask = cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK;
234 	if (mask != 0) {
235 		int nestpil;
236 
237 		/*
238 		 * find PIL of nested interrupt
239 		 */
240 		nestpil = bsrw_insn((uint16_t)mask);
241 		ASSERT(nestpil < pil);
242 		mcpu->pil_high_start[nestpil - (LOCK_LEVEL + 1)] = tsc_read();
243 		/*
244 		 * (Another high-level interrupt is active below this one,
245 		 * so there is no need to check for an interrupt
246 		 * thread.  That will be done by the lowest priority
247 		 * high-level interrupt active.)
248 		 */
249 	} else {
250 		/*
251 		 * Check to see if there is a low-level interrupt active.
252 		 * If so, place a starting timestamp in the thread
253 		 * structure.
254 		 */
255 		kthread_t *t = cpu->cpu_thread;
256 
257 		if (t->t_flag & T_INTR_THREAD)
258 			t->t_intr_start = tsc_read();
259 	}
260 
261 	mcpu->mcpu_pri = oldpil;
262 	(void) (*setlvlx)(oldpil, vecnum);
263 
264 	return (cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK);
265 }
266 
267 /*
268  * Set up the cpu, thread and interrupt thread structures for
269  * executing an interrupt thread.  The new stack pointer of the
270  * interrupt thread (which *must* be switched to) is returned.
271  */
272 caddr_t
273 intr_thread_prolog(struct cpu *cpu, caddr_t stackptr, uint_t pil)
274 {
275 	struct machcpu *mcpu = &cpu->cpu_m;
276 	kthread_t *t, *volatile it;
277 
278 	ASSERT(pil > 0);
279 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
280 	cpu->cpu_intr_actv |= (1 << pil);
281 
282 	/*
283 	 * Get set to run an interrupt thread.
284 	 * There should always be an interrupt thread, since we
285 	 * allocate one for each level on each CPU.
286 	 *
287 	 * Note that the code in kcpc_overflow_intr -relies- on the
288 	 * ordering of events here - in particular that t->t_lwp of
289 	 * the interrupt thread is set to the pinned thread *before*
290 	 * curthread is changed.
291 	 */
292 	t = cpu->cpu_thread;
293 	if (t->t_flag & T_INTR_THREAD) {
294 		mcpu->intrstat[t->t_pil] += t->t_intr_start - tsc_read();
295 		t->t_intr_start = 0;
296 	}
297 
298 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
299 
300 	t->t_sp = (uintptr_t)stackptr;	/* mark stack in curthread for resume */
301 
302 	/*
303 	 * unlink the interrupt thread off the cpu
304 	 */
305 	it = cpu->cpu_intr_thread;
306 	cpu->cpu_intr_thread = it->t_link;
307 	it->t_intr = t;
308 	it->t_lwp = t->t_lwp;
309 
310 	/*
311 	 * (threads on the interrupt thread free list could have state
312 	 * preset to TS_ONPROC, but it helps in debugging if
313 	 * they're TS_FREE.)
314 	 */
315 	it->t_state = TS_ONPROC;
316 
317 	cpu->cpu_thread = it;		/* new curthread on this cpu */
318 	it->t_pil = (uchar_t)pil;
319 	it->t_pri = intr_pri + (pri_t)pil;
320 	it->t_intr_start = tsc_read();
321 
322 	return (it->t_stk);
323 }
324 
325 
326 #ifdef DEBUG
327 int intr_thread_cnt;
328 #endif
329 
330 /*
331  * Called with interrupts disabled
332  */
333 void
334 intr_thread_epilog(struct cpu *cpu, uint_t vec, uint_t oldpil)
335 {
336 	struct machcpu *mcpu = &cpu->cpu_m;
337 	kthread_t *t;
338 	kthread_t *it = cpu->cpu_thread;	/* curthread */
339 	uint_t pil, basespl;
340 
341 	pil = it->t_pil;
342 	cpu->cpu_stats.sys.intr[pil - 1]++;
343 
344 	ASSERT(it->t_intr_start != 0);
345 	mcpu->intrstat[pil] += tsc_read() - it->t_intr_start;
346 
347 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
348 	cpu->cpu_intr_actv &= ~(1 << pil);
349 
350 	/*
351 	 * If there is still an interrupted thread underneath this one
352 	 * then the interrupt was never blocked and the return is
353 	 * fairly simple.  Otherwise it isn't.
354 	 */
355 	if ((t = it->t_intr) == NULL) {
356 		/*
357 		 * The interrupted thread is no longer pinned underneath
358 		 * the interrupt thread.  This means the interrupt must
359 		 * have blocked, and the interrupted thread has been
360 		 * unpinned, and has probably been running around the
361 		 * system for a while.
362 		 *
363 		 * Since there is no longer a thread under this one, put
364 		 * this interrupt thread back on the CPU's free list and
365 		 * resume the idle thread which will dispatch the next
366 		 * thread to run.
367 		 */
368 #ifdef DEBUG
369 		intr_thread_cnt++;
370 #endif
371 		cpu->cpu_stats.sys.intrblk++;
372 		/*
373 		 * Set CPU's base SPL based on active interrupts bitmask
374 		 */
375 		set_base_spl();
376 		basespl = cpu->cpu_base_spl;
377 		mcpu->mcpu_pri = basespl;
378 		(*setlvlx)(basespl, vec);
379 		(void) splhigh();
380 		it->t_state = TS_FREE;
381 		/*
382 		 * Return interrupt thread to pool
383 		 */
384 		it->t_link = cpu->cpu_intr_thread;
385 		cpu->cpu_intr_thread = it;
386 		swtch();
387 		/*NOTREACHED*/
388 	}
389 
390 	/*
391 	 * Return interrupt thread to the pool
392 	 */
393 	it->t_link = cpu->cpu_intr_thread;
394 	cpu->cpu_intr_thread = it;
395 	it->t_state = TS_FREE;
396 
397 	basespl = cpu->cpu_base_spl;
398 	pil = MAX(oldpil, basespl);
399 	mcpu->mcpu_pri = pil;
400 	(*setlvlx)(pil, vec);
401 	t->t_intr_start = tsc_read();
402 	cpu->cpu_thread = t;
403 }
404 
405 caddr_t
406 dosoftint_prolog(
407 	struct cpu *cpu,
408 	caddr_t stackptr,
409 	uint32_t st_pending,
410 	uint_t oldpil)
411 {
412 	kthread_t *t, *volatile it;
413 	struct machcpu *mcpu = &cpu->cpu_m;
414 	uint_t pil;
415 
416 top:
417 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
418 
419 	pil = bsrw_insn((uint16_t)st_pending);
420 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
421 		return (0);
422 
423 	/*
424 	 * XX64	Sigh.
425 	 *
426 	 * This is a transliteration of the i386 assembler code for
427 	 * soft interrupts.  One question is "why does this need
428 	 * to be atomic?"  One possible race is -other- processors
429 	 * posting soft interrupts to us in set_pending() i.e. the
430 	 * CPU might get preempted just after the address computation,
431 	 * but just before the atomic transaction, so another CPU would
432 	 * actually set the original CPU's st_pending bit.  However,
433 	 * it looks like it would be simpler to disable preemption there.
434 	 * Are there other races for which preemption control doesn't work?
435 	 *
436 	 * The i386 assembler version -also- checks to see if the bit
437 	 * being cleared was actually set; if it wasn't, it rechecks
438 	 * for more.  This seems a bit strange, as the only code that
439 	 * ever clears the bit is -this- code running with interrupts
440 	 * disabled on -this- CPU.  This code would probably be cheaper:
441 	 *
442 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
443 	 *   ~(1 << pil));
444 	 *
445 	 * and t->t_preempt--/++ around set_pending() even cheaper,
446 	 * but at this point, correctness is critical, so we slavishly
447 	 * emulate the i386 port.
448 	 */
449 	if (atomic_btr32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, pil)
450 	    == 0) {
451 		st_pending = mcpu->mcpu_softinfo.st_pending;
452 		goto top;
453 	}
454 
455 	mcpu->mcpu_pri = pil;
456 	(*setspl)(pil);
457 
458 	/*
459 	 * Get set to run interrupt thread.
460 	 * There should always be an interrupt thread since we
461 	 * allocate one for each level on the CPU.
462 	 */
463 	it = cpu->cpu_intr_thread;
464 	cpu->cpu_intr_thread = it->t_link;
465 
466 	/*
467 	 * Note that the code in kcpc_overflow_intr -relies- on the
468 	 * ordering of events here - in particular that t->t_lwp of
469 	 * the interrupt thread is set to the pinned thread *before*
470 	 * curthread is changed
471 	 */
472 	t = cpu->cpu_thread;
473 	if (t->t_flag & T_INTR_THREAD)
474 		mcpu->intrstat[pil] += tsc_read() - t->t_intr_start;
475 	it->t_lwp = t->t_lwp;
476 	it->t_state = TS_ONPROC;
477 
478 	/*
479 	 * Push interrupted thread onto list from new thread.
480 	 * Set the new thread as the current one.
481 	 * Set interrupted thread's T_SP because if it is the idle thread,
482 	 * resume() may use that stack between threads.
483 	 */
484 
485 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
486 	t->t_sp = (uintptr_t)stackptr;
487 
488 	it->t_intr = t;
489 	cpu->cpu_thread = it;
490 
491 	/*
492 	 * Set bit for this pil in CPU's interrupt active bitmask.
493 	 */
494 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
495 	cpu->cpu_intr_actv |= (1 << pil);
496 
497 	/*
498 	 * Initialize thread priority level from intr_pri
499 	 */
500 	it->t_pil = (uchar_t)pil;
501 	it->t_pri = (pri_t)pil + intr_pri;
502 	it->t_intr_start = tsc_read();
503 
504 	return (it->t_stk);
505 }
506 
507 void
508 dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
509 {
510 	struct machcpu *mcpu = &cpu->cpu_m;
511 	kthread_t *t, *it;
512 	uint_t pil, basespl;
513 
514 	it = cpu->cpu_thread;
515 	pil = it->t_pil;
516 
517 	cpu->cpu_stats.sys.intr[pil - 1]++;
518 
519 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
520 	cpu->cpu_intr_actv &= ~(1 << pil);
521 	mcpu->intrstat[pil] += tsc_read() - it->t_intr_start;
522 
523 	/*
524 	 * If there is still an interrupted thread underneath this one
525 	 * then the interrupt was never blocked and the return is
526 	 * fairly simple.  Otherwise it isn't.
527 	 */
528 	if ((t = it->t_intr) == NULL) {
529 		/*
530 		 * Put thread back on the interrupt thread list.
531 		 * This was an interrupt thread, so set CPU's base SPL.
532 		 */
533 		set_base_spl();
534 		it->t_state = TS_FREE;
535 		it->t_link = cpu->cpu_intr_thread;
536 		cpu->cpu_intr_thread = it;
537 		(void) splhigh();
538 		swtch();
539 		/*NOTREACHED*/
540 	}
541 	it->t_link = cpu->cpu_intr_thread;
542 	cpu->cpu_intr_thread = it;
543 	it->t_state = TS_FREE;
544 	cpu->cpu_thread = t;
545 	if (t->t_flag & T_INTR_THREAD)
546 		t->t_intr_start = tsc_read();
547 	basespl = cpu->cpu_base_spl;
548 	pil = MAX(oldpil, basespl);
549 	mcpu->mcpu_pri = pil;
550 	(*setspl)(pil);
551 }
552 
553 /*
554  * Make the interrupted thread 'to' be runnable.
555  *
556  * Since t->t_sp has already been saved, t->t_pc is all
557  * that needs to be set in this function.
558  *
559  * Returns the interrupt level of the interrupt thread.
560  */
561 int
562 intr_passivate(
563 	kthread_t *it,		/* interrupt thread */
564 	kthread_t *t)		/* interrupted thread */
565 {
566 	extern void _sys_rtt();
567 
568 	ASSERT(it->t_flag & T_INTR_THREAD);
569 	ASSERT(SA(t->t_sp) == t->t_sp);
570 
571 	t->t_pc = (uintptr_t)_sys_rtt;
572 	return (it->t_pil);
573 }
574 
575 #endif	/* __amd64 */
576 
577 /*
578  * Allocate threads and stacks for interrupt handling.
579  */
580 #define	NINTR_THREADS	(LOCK_LEVEL-1)	/* number of interrupt threads */
581 
582 void
583 init_intr_threads(struct cpu *cp)
584 {
585 	int i;
586 
587 	for (i = 0; i < NINTR_THREADS; i++)
588 		thread_create_intr(cp);
589 
590 	cp->cpu_intr_stack = (caddr_t)segkp_get(segkp, INTR_STACK_SIZE,
591 		KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED) +
592 		INTR_STACK_SIZE - SA(MINFRAME);
593 }
594 
595 /*
596  * Create interrupt kstats for this CPU.
597  */
598 void
599 cpu_create_intrstat(cpu_t *cp)
600 {
601 	int		i;
602 	kstat_t		*intr_ksp;
603 	kstat_named_t	*knp;
604 	char		name[KSTAT_STRLEN];
605 	zoneid_t	zoneid;
606 
607 	ASSERT(MUTEX_HELD(&cpu_lock));
608 
609 	if (pool_pset_enabled())
610 		zoneid = GLOBAL_ZONEID;
611 	else
612 		zoneid = ALL_ZONES;
613 
614 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
615 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
616 
617 	/*
618 	 * Initialize each PIL's named kstat
619 	 */
620 	if (intr_ksp != NULL) {
621 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
622 		knp = (kstat_named_t *)intr_ksp->ks_data;
623 		intr_ksp->ks_private = cp;
624 		for (i = 0; i < PIL_MAX; i++) {
625 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
626 			    i + 1);
627 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
628 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
629 			    i + 1);
630 			kstat_named_init(&knp[(i * 2) + 1], name,
631 			    KSTAT_DATA_UINT64);
632 		}
633 		kstat_install(intr_ksp);
634 	}
635 }
636 
637 /*
638  * Delete interrupt kstats for this CPU.
639  */
640 void
641 cpu_delete_intrstat(cpu_t *cp)
642 {
643 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
644 }
645 
646 /*
647  * Convert interrupt statistics from CPU ticks to nanoseconds and
648  * update kstat.
649  */
650 int
651 cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
652 {
653 	kstat_named_t	*knp = ksp->ks_data;
654 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
655 	int		i;
656 	hrtime_t	hrt;
657 
658 	if (rw == KSTAT_WRITE)
659 		return (EACCES);
660 
661 	for (i = 0; i < PIL_MAX; i++) {
662 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1];
663 		tsc_scalehrtime(&hrt);
664 		knp[i * 2].value.ui64 = (uint64_t)hrt;
665 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
666 	}
667 
668 	return (0);
669 }
670 
671 /*
672  * An interrupt thread is ending a time slice, so compute the interval it
673  * ran for and update the statistic for its PIL.
674  */
675 void
676 cpu_intr_swtch_enter(kthread_id_t t)
677 {
678 	uint64_t	interval;
679 	uint64_t	start;
680 
681 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
682 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
683 
684 	/*
685 	 * We could be here with a zero timestamp. This could happen if:
686 	 * an interrupt thread which no longer has a pinned thread underneath
687 	 * it (i.e. it blocked at some point in its past) has finished running
688 	 * its handler. intr_thread() updated the interrupt statistic for its
689 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
690 	 * return to, swtch() gets called and we end up here.
691 	 */
692 	if (t->t_intr_start) {
693 		do {
694 			start = t->t_intr_start;
695 			interval = tsc_read() - start;
696 		} while (cas64(&t->t_intr_start, start, 0) != start);
697 		CPU->cpu_m.intrstat[t->t_pil] += interval;
698 	} else
699 		ASSERT(t->t_intr == NULL);
700 }
701 
702 /*
703  * An interrupt thread is returning from swtch(). Place a starting timestamp
704  * in its thread structure.
705  */
706 void
707 cpu_intr_swtch_exit(kthread_id_t t)
708 {
709 	uint64_t ts;
710 
711 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
712 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
713 
714 	do {
715 		ts = t->t_intr_start;
716 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
717 }
718