xref: /titanic_50/usr/src/uts/i86pc/os/intr.c (revision d291d9f21e8c0417aec99de243dd48bc400002d0)
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][0] += 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][0] += 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][0] += 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][0] += 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][0] += 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 /*
417  * Called with interrupts disabled by an interrupt thread to determine
418  * how much time has elapsed. See interrupt.s:intr_get_time() for detailed
419  * theory of operation.
420  */
421 uint64_t
422 intr_thread_get_time(struct cpu *cpu)
423 {
424 	struct machcpu *mcpu = &cpu->cpu_m;
425 	kthread_t *t = cpu->cpu_thread;
426 	uint64_t time, delta, ret;
427 	uint_t pil = t->t_pil;
428 
429 	ASSERT((cpu->cpu_intr_actv & CPU_INTR_ACTV_HIGH_LEVEL_MASK) == 0);
430 	ASSERT(t->t_flag & T_INTR_THREAD);
431 	ASSERT(pil != 0);
432 	ASSERT(t->t_intr_start != 0);
433 
434 	time = tsc_read();
435 	delta = time - t->t_intr_start;
436 	t->t_intr_start = time;
437 
438 	time = mcpu->intrstat[pil][0] + delta;
439 	ret = time - mcpu->intrstat[pil][1];
440 	mcpu->intrstat[pil][0] = time;
441 	mcpu->intrstat[pil][1] = time;
442 
443 	return (ret);
444 }
445 
446 caddr_t
447 dosoftint_prolog(
448 	struct cpu *cpu,
449 	caddr_t stackptr,
450 	uint32_t st_pending,
451 	uint_t oldpil)
452 {
453 	kthread_t *t, *volatile it;
454 	struct machcpu *mcpu = &cpu->cpu_m;
455 	uint_t pil;
456 
457 top:
458 	ASSERT(st_pending == mcpu->mcpu_softinfo.st_pending);
459 
460 	pil = bsrw_insn((uint16_t)st_pending);
461 	if (pil <= oldpil || pil <= cpu->cpu_base_spl)
462 		return (0);
463 
464 	/*
465 	 * XX64	Sigh.
466 	 *
467 	 * This is a transliteration of the i386 assembler code for
468 	 * soft interrupts.  One question is "why does this need
469 	 * to be atomic?"  One possible race is -other- processors
470 	 * posting soft interrupts to us in set_pending() i.e. the
471 	 * CPU might get preempted just after the address computation,
472 	 * but just before the atomic transaction, so another CPU would
473 	 * actually set the original CPU's st_pending bit.  However,
474 	 * it looks like it would be simpler to disable preemption there.
475 	 * Are there other races for which preemption control doesn't work?
476 	 *
477 	 * The i386 assembler version -also- checks to see if the bit
478 	 * being cleared was actually set; if it wasn't, it rechecks
479 	 * for more.  This seems a bit strange, as the only code that
480 	 * ever clears the bit is -this- code running with interrupts
481 	 * disabled on -this- CPU.  This code would probably be cheaper:
482 	 *
483 	 * atomic_and_32((uint32_t *)&mcpu->mcpu_softinfo.st_pending,
484 	 *   ~(1 << pil));
485 	 *
486 	 * and t->t_preempt--/++ around set_pending() even cheaper,
487 	 * but at this point, correctness is critical, so we slavishly
488 	 * emulate the i386 port.
489 	 */
490 	if (atomic_btr32((uint32_t *)&mcpu->mcpu_softinfo.st_pending, pil)
491 	    == 0) {
492 		st_pending = mcpu->mcpu_softinfo.st_pending;
493 		goto top;
494 	}
495 
496 	mcpu->mcpu_pri = pil;
497 	(*setspl)(pil);
498 
499 	/*
500 	 * Get set to run interrupt thread.
501 	 * There should always be an interrupt thread since we
502 	 * allocate one for each level on the CPU.
503 	 */
504 	it = cpu->cpu_intr_thread;
505 	cpu->cpu_intr_thread = it->t_link;
506 
507 	/*
508 	 * Note that the code in kcpc_overflow_intr -relies- on the
509 	 * ordering of events here - in particular that t->t_lwp of
510 	 * the interrupt thread is set to the pinned thread *before*
511 	 * curthread is changed
512 	 */
513 	t = cpu->cpu_thread;
514 	if (t->t_flag & T_INTR_THREAD) {
515 		hrtime_t intrtime = tsc_read() - t->t_intr_start;
516 		mcpu->intrstat[pil][0] += intrtime;
517 		cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
518 	}
519 	it->t_lwp = t->t_lwp;
520 	it->t_state = TS_ONPROC;
521 
522 	/*
523 	 * Push interrupted thread onto list from new thread.
524 	 * Set the new thread as the current one.
525 	 * Set interrupted thread's T_SP because if it is the idle thread,
526 	 * resume() may use that stack between threads.
527 	 */
528 
529 	ASSERT(SA((uintptr_t)stackptr) == (uintptr_t)stackptr);
530 	t->t_sp = (uintptr_t)stackptr;
531 
532 	it->t_intr = t;
533 	cpu->cpu_thread = it;
534 
535 	/*
536 	 * Set bit for this pil in CPU's interrupt active bitmask.
537 	 */
538 	ASSERT((cpu->cpu_intr_actv & (1 << pil)) == 0);
539 	cpu->cpu_intr_actv |= (1 << pil);
540 
541 	/*
542 	 * Initialize thread priority level from intr_pri
543 	 */
544 	it->t_pil = (uchar_t)pil;
545 	it->t_pri = (pri_t)pil + intr_pri;
546 	it->t_intr_start = tsc_read();
547 
548 	return (it->t_stk);
549 }
550 
551 void
552 dosoftint_epilog(struct cpu *cpu, uint_t oldpil)
553 {
554 	struct machcpu *mcpu = &cpu->cpu_m;
555 	kthread_t *t, *it;
556 	uint_t pil, basespl;
557 	hrtime_t intrtime;
558 
559 	it = cpu->cpu_thread;
560 	pil = it->t_pil;
561 
562 	cpu->cpu_stats.sys.intr[pil - 1]++;
563 
564 	ASSERT(cpu->cpu_intr_actv & (1 << pil));
565 	cpu->cpu_intr_actv &= ~(1 << pil);
566 	intrtime = tsc_read() - it->t_intr_start;
567 	mcpu->intrstat[pil][0] += intrtime;
568 	cpu->cpu_intracct[cpu->cpu_mstate] += intrtime;
569 
570 	/*
571 	 * If there is still an interrupted thread underneath this one
572 	 * then the interrupt was never blocked and the return is
573 	 * fairly simple.  Otherwise it isn't.
574 	 */
575 	if ((t = it->t_intr) == NULL) {
576 		/*
577 		 * Put thread back on the interrupt thread list.
578 		 * This was an interrupt thread, so set CPU's base SPL.
579 		 */
580 		set_base_spl();
581 		it->t_state = TS_FREE;
582 		it->t_link = cpu->cpu_intr_thread;
583 		cpu->cpu_intr_thread = it;
584 		(void) splhigh();
585 		swtch();
586 		/*NOTREACHED*/
587 	}
588 	it->t_link = cpu->cpu_intr_thread;
589 	cpu->cpu_intr_thread = it;
590 	it->t_state = TS_FREE;
591 	cpu->cpu_thread = t;
592 	if (t->t_flag & T_INTR_THREAD)
593 		t->t_intr_start = tsc_read();
594 	basespl = cpu->cpu_base_spl;
595 	pil = MAX(oldpil, basespl);
596 	mcpu->mcpu_pri = pil;
597 	(*setspl)(pil);
598 }
599 
600 /*
601  * Make the interrupted thread 'to' be runnable.
602  *
603  * Since t->t_sp has already been saved, t->t_pc is all
604  * that needs to be set in this function.
605  *
606  * Returns the interrupt level of the interrupt thread.
607  */
608 int
609 intr_passivate(
610 	kthread_t *it,		/* interrupt thread */
611 	kthread_t *t)		/* interrupted thread */
612 {
613 	extern void _sys_rtt();
614 
615 	ASSERT(it->t_flag & T_INTR_THREAD);
616 	ASSERT(SA(t->t_sp) == t->t_sp);
617 
618 	t->t_pc = (uintptr_t)_sys_rtt;
619 	return (it->t_pil);
620 }
621 
622 #endif	/* __amd64 */
623 
624 /*
625  * Allocate threads and stacks for interrupt handling.
626  */
627 #define	NINTR_THREADS	(LOCK_LEVEL-1)	/* number of interrupt threads */
628 
629 void
630 init_intr_threads(struct cpu *cp)
631 {
632 	int i;
633 
634 	for (i = 0; i < NINTR_THREADS; i++)
635 		thread_create_intr(cp);
636 
637 	cp->cpu_intr_stack = (caddr_t)segkp_get(segkp, INTR_STACK_SIZE,
638 		KPD_HASREDZONE | KPD_NO_ANON | KPD_LOCKED) +
639 		INTR_STACK_SIZE - SA(MINFRAME);
640 }
641 
642 /*
643  * Create interrupt kstats for this CPU.
644  */
645 void
646 cpu_create_intrstat(cpu_t *cp)
647 {
648 	int		i;
649 	kstat_t		*intr_ksp;
650 	kstat_named_t	*knp;
651 	char		name[KSTAT_STRLEN];
652 	zoneid_t	zoneid;
653 
654 	ASSERT(MUTEX_HELD(&cpu_lock));
655 
656 	if (pool_pset_enabled())
657 		zoneid = GLOBAL_ZONEID;
658 	else
659 		zoneid = ALL_ZONES;
660 
661 	intr_ksp = kstat_create_zone("cpu", cp->cpu_id, "intrstat", "misc",
662 	    KSTAT_TYPE_NAMED, PIL_MAX * 2, NULL, zoneid);
663 
664 	/*
665 	 * Initialize each PIL's named kstat
666 	 */
667 	if (intr_ksp != NULL) {
668 		intr_ksp->ks_update = cpu_kstat_intrstat_update;
669 		knp = (kstat_named_t *)intr_ksp->ks_data;
670 		intr_ksp->ks_private = cp;
671 		for (i = 0; i < PIL_MAX; i++) {
672 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-time",
673 			    i + 1);
674 			kstat_named_init(&knp[i * 2], name, KSTAT_DATA_UINT64);
675 			(void) snprintf(name, KSTAT_STRLEN, "level-%d-count",
676 			    i + 1);
677 			kstat_named_init(&knp[(i * 2) + 1], name,
678 			    KSTAT_DATA_UINT64);
679 		}
680 		kstat_install(intr_ksp);
681 	}
682 }
683 
684 /*
685  * Delete interrupt kstats for this CPU.
686  */
687 void
688 cpu_delete_intrstat(cpu_t *cp)
689 {
690 	kstat_delete_byname_zone("cpu", cp->cpu_id, "intrstat", ALL_ZONES);
691 }
692 
693 /*
694  * Convert interrupt statistics from CPU ticks to nanoseconds and
695  * update kstat.
696  */
697 int
698 cpu_kstat_intrstat_update(kstat_t *ksp, int rw)
699 {
700 	kstat_named_t	*knp = ksp->ks_data;
701 	cpu_t		*cpup = (cpu_t *)ksp->ks_private;
702 	int		i;
703 	hrtime_t	hrt;
704 
705 	if (rw == KSTAT_WRITE)
706 		return (EACCES);
707 
708 	for (i = 0; i < PIL_MAX; i++) {
709 		hrt = (hrtime_t)cpup->cpu_m.intrstat[i + 1][0];
710 		tsc_scalehrtime(&hrt);
711 		knp[i * 2].value.ui64 = (uint64_t)hrt;
712 		knp[(i * 2) + 1].value.ui64 = cpup->cpu_stats.sys.intr[i];
713 	}
714 
715 	return (0);
716 }
717 
718 /*
719  * An interrupt thread is ending a time slice, so compute the interval it
720  * ran for and update the statistic for its PIL.
721  */
722 void
723 cpu_intr_swtch_enter(kthread_id_t t)
724 {
725 	uint64_t	interval;
726 	uint64_t	start;
727 	cpu_t		*cpu;
728 
729 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
730 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
731 
732 	/*
733 	 * We could be here with a zero timestamp. This could happen if:
734 	 * an interrupt thread which no longer has a pinned thread underneath
735 	 * it (i.e. it blocked at some point in its past) has finished running
736 	 * its handler. intr_thread() updated the interrupt statistic for its
737 	 * PIL and zeroed its timestamp. Since there was no pinned thread to
738 	 * return to, swtch() gets called and we end up here.
739 	 *
740 	 * Note that we use atomic ops below (cas64 and atomic_add_64), which
741 	 * we don't use in the functions above, because we're not called
742 	 * with interrupts blocked, but the epilog/prolog functions are.
743 	 */
744 	if (t->t_intr_start) {
745 		do {
746 			start = t->t_intr_start;
747 			interval = tsc_read() - start;
748 		} while (cas64(&t->t_intr_start, start, 0) != start);
749 		cpu = CPU;
750 		cpu->cpu_m.intrstat[t->t_pil][0] += interval;
751 
752 		atomic_add_64((uint64_t *)&cpu->cpu_intracct[cpu->cpu_mstate],
753 		    interval);
754 	} else
755 		ASSERT(t->t_intr == NULL);
756 }
757 
758 /*
759  * An interrupt thread is returning from swtch(). Place a starting timestamp
760  * in its thread structure.
761  */
762 void
763 cpu_intr_swtch_exit(kthread_id_t t)
764 {
765 	uint64_t ts;
766 
767 	ASSERT((t->t_flag & T_INTR_THREAD) != 0);
768 	ASSERT(t->t_pil > 0 && t->t_pil <= LOCK_LEVEL);
769 
770 	do {
771 		ts = t->t_intr_start;
772 	} while (cas64(&t->t_intr_start, ts, tsc_read()) != ts);
773 }
774