xref: /freebsd/sys/kern/kern_intr.c (revision d2387d42b8da231a5b95cbc313825fb2aadf26f6)
1 /*
2  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice unmodified, this list of conditions, and the following
10  *    disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25  */
26 
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
29 
30 #include "opt_ddb.h"
31 
32 #include <sys/param.h>
33 #include <sys/bus.h>
34 #include <sys/conf.h>
35 #include <sys/rtprio.h>
36 #include <sys/systm.h>
37 #include <sys/interrupt.h>
38 #include <sys/kernel.h>
39 #include <sys/kthread.h>
40 #include <sys/ktr.h>
41 #include <sys/lock.h>
42 #include <sys/malloc.h>
43 #include <sys/mutex.h>
44 #include <sys/proc.h>
45 #include <sys/random.h>
46 #include <sys/resourcevar.h>
47 #include <sys/sysctl.h>
48 #include <sys/unistd.h>
49 #include <sys/vmmeter.h>
50 #include <machine/atomic.h>
51 #include <machine/cpu.h>
52 #include <machine/md_var.h>
53 #include <machine/stdarg.h>
54 #ifdef DDB
55 #include <ddb/ddb.h>
56 #include <ddb/db_sym.h>
57 #endif
58 
59 struct	int_entropy {
60 	struct	proc *proc;
61 	uintptr_t vector;
62 };
63 
64 void	*vm_ih;
65 void	*softclock_ih;
66 struct	ithd *clk_ithd;
67 struct	ithd *tty_ithd;
68 
69 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
70 
71 static void	ithread_update(struct ithd *);
72 static void	ithread_loop(void *);
73 static void	start_softintr(void *);
74 
75 u_char
76 ithread_priority(enum intr_type flags)
77 {
78 	u_char pri;
79 
80 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
81 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
82 	switch (flags) {
83 	case INTR_TYPE_TTY:
84 		pri = PI_TTYLOW;
85 		break;
86 	case INTR_TYPE_BIO:
87 		/*
88 		 * XXX We need to refine this.  BSD/OS distinguishes
89 		 * between tape and disk priorities.
90 		 */
91 		pri = PI_DISK;
92 		break;
93 	case INTR_TYPE_NET:
94 		pri = PI_NET;
95 		break;
96 	case INTR_TYPE_CAM:
97 		pri = PI_DISK;          /* XXX or PI_CAM? */
98 		break;
99 	case INTR_TYPE_AV:		/* Audio/video */
100 		pri = PI_AV;
101 		break;
102 	case INTR_TYPE_CLK:
103 		pri = PI_REALTIME;
104 		break;
105 	case INTR_TYPE_MISC:
106 		pri = PI_DULL;          /* don't care */
107 		break;
108 	default:
109 		/* We didn't specify an interrupt level. */
110 		panic("ithread_priority: no interrupt type in flags");
111 	}
112 
113 	return pri;
114 }
115 
116 /*
117  * Regenerate the name (p_comm) and priority for a threaded interrupt thread.
118  */
119 static void
120 ithread_update(struct ithd *ithd)
121 {
122 	struct intrhand *ih;
123 	struct thread *td;
124 	struct proc *p;
125 	int entropy;
126 
127 	mtx_assert(&ithd->it_lock, MA_OWNED);
128 	td = ithd->it_td;
129 	if (td == NULL)
130 		return;
131 	p = td->td_proc;
132 
133 	strlcpy(p->p_comm, ithd->it_name, sizeof(p->p_comm));
134 
135 	ih = TAILQ_FIRST(&ithd->it_handlers);
136 	if (ih == NULL) {
137 		mtx_lock_spin(&sched_lock);
138 		td->td_priority = PRI_MAX_ITHD;
139 		td->td_base_pri = PRI_MAX_ITHD;
140 		mtx_unlock_spin(&sched_lock);
141 		ithd->it_flags &= ~IT_ENTROPY;
142 		return;
143 	}
144 	entropy = 0;
145 	mtx_lock_spin(&sched_lock);
146 	td->td_priority = ih->ih_pri;
147 	td->td_base_pri = ih->ih_pri;
148 	mtx_unlock_spin(&sched_lock);
149 	TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
150 		if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 <
151 		    sizeof(p->p_comm)) {
152 			strcat(p->p_comm, " ");
153 			strcat(p->p_comm, ih->ih_name);
154 		} else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) {
155 			if (p->p_comm[sizeof(p->p_comm) - 2] == '+')
156 				p->p_comm[sizeof(p->p_comm) - 2] = '*';
157 			else
158 				p->p_comm[sizeof(p->p_comm) - 2] = '+';
159 		} else
160 			strcat(p->p_comm, "+");
161 		if (ih->ih_flags & IH_ENTROPY)
162 			entropy++;
163 	}
164 	if (entropy)
165 		ithd->it_flags |= IT_ENTROPY;
166 	else
167 		ithd->it_flags &= ~IT_ENTROPY;
168 	CTR2(KTR_INTR, "%s: updated %s", __func__, p->p_comm);
169 }
170 
171 int
172 ithread_create(struct ithd **ithread, uintptr_t vector, int flags,
173     void (*disable)(uintptr_t), void (*enable)(uintptr_t), const char *fmt, ...)
174 {
175 	struct ithd *ithd;
176 	struct thread *td;
177 	struct proc *p;
178 	int error;
179 	va_list ap;
180 
181 	/* The only valid flag during creation is IT_SOFT. */
182 	if ((flags & ~IT_SOFT) != 0)
183 		return (EINVAL);
184 
185 	ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO);
186 	ithd->it_vector = vector;
187 	ithd->it_disable = disable;
188 	ithd->it_enable = enable;
189 	ithd->it_flags = flags;
190 	TAILQ_INIT(&ithd->it_handlers);
191 	mtx_init(&ithd->it_lock, "ithread", NULL, MTX_DEF);
192 
193 	va_start(ap, fmt);
194 	vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap);
195 	va_end(ap);
196 
197 	error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID,
198 	    0, "%s", ithd->it_name);
199 	if (error) {
200 		mtx_destroy(&ithd->it_lock);
201 		free(ithd, M_ITHREAD);
202 		return (error);
203 	}
204 	td = FIRST_THREAD_IN_PROC(p);	/* XXXKSE */
205 	mtx_lock_spin(&sched_lock);
206 	td->td_ksegrp->kg_pri_class = PRI_ITHD;
207 	td->td_priority = PRI_MAX_ITHD;
208 	TD_SET_IWAIT(td);
209 	mtx_unlock_spin(&sched_lock);
210 	ithd->it_td = td;
211 	td->td_ithd = ithd;
212 	if (ithread != NULL)
213 		*ithread = ithd;
214 	CTR2(KTR_INTR, "%s: created %s", __func__, ithd->it_name);
215 	return (0);
216 }
217 
218 int
219 ithread_destroy(struct ithd *ithread)
220 {
221 
222 	struct thread *td;
223 	if (ithread == NULL)
224 		return (EINVAL);
225 
226 	td = ithread->it_td;
227 	mtx_lock(&ithread->it_lock);
228 	if (!TAILQ_EMPTY(&ithread->it_handlers)) {
229 		mtx_unlock(&ithread->it_lock);
230 		return (EINVAL);
231 	}
232 	ithread->it_flags |= IT_DEAD;
233 	mtx_lock_spin(&sched_lock);
234 	if (TD_AWAITING_INTR(td)) {
235 		TD_CLR_IWAIT(td);
236 		setrunqueue(td);
237 	}
238 	mtx_unlock_spin(&sched_lock);
239 	mtx_unlock(&ithread->it_lock);
240 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_name);
241 	return (0);
242 }
243 
244 int
245 ithread_add_handler(struct ithd* ithread, const char *name,
246     driver_intr_t handler, void *arg, u_char pri, enum intr_type flags,
247     void **cookiep)
248 {
249 	struct intrhand *ih, *temp_ih;
250 
251 	if (ithread == NULL || name == NULL || handler == NULL)
252 		return (EINVAL);
253 
254 	ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO);
255 	ih->ih_handler = handler;
256 	ih->ih_argument = arg;
257 	ih->ih_name = name;
258 	ih->ih_ithread = ithread;
259 	ih->ih_pri = pri;
260 	if (flags & INTR_FAST)
261 		ih->ih_flags = IH_FAST;
262 	else if (flags & INTR_EXCL)
263 		ih->ih_flags = IH_EXCLUSIVE;
264 	if (flags & INTR_MPSAFE)
265 		ih->ih_flags |= IH_MPSAFE;
266 	if (flags & INTR_ENTROPY)
267 		ih->ih_flags |= IH_ENTROPY;
268 
269 	mtx_lock(&ithread->it_lock);
270 	if ((flags & INTR_EXCL) != 0 && !TAILQ_EMPTY(&ithread->it_handlers))
271 		goto fail;
272 	if (!TAILQ_EMPTY(&ithread->it_handlers)) {
273 		temp_ih = TAILQ_FIRST(&ithread->it_handlers);
274 		if (temp_ih->ih_flags & IH_EXCLUSIVE)
275 			goto fail;
276 		if ((ih->ih_flags & IH_FAST) && !(temp_ih->ih_flags & IH_FAST))
277 			goto fail;
278 		if (!(ih->ih_flags & IH_FAST) && (temp_ih->ih_flags & IH_FAST))
279 			goto fail;
280 	}
281 
282 	TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next)
283 	    if (temp_ih->ih_pri > ih->ih_pri)
284 		    break;
285 	if (temp_ih == NULL)
286 		TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next);
287 	else
288 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
289 	ithread_update(ithread);
290 	mtx_unlock(&ithread->it_lock);
291 
292 	if (cookiep != NULL)
293 		*cookiep = ih;
294 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
295 	    ithread->it_name);
296 	return (0);
297 
298 fail:
299 	mtx_unlock(&ithread->it_lock);
300 	free(ih, M_ITHREAD);
301 	return (EINVAL);
302 }
303 
304 int
305 ithread_remove_handler(void *cookie)
306 {
307 	struct intrhand *handler = (struct intrhand *)cookie;
308 	struct ithd *ithread;
309 #ifdef INVARIANTS
310 	struct intrhand *ih;
311 #endif
312 
313 	if (handler == NULL)
314 		return (EINVAL);
315 	ithread = handler->ih_ithread;
316 	KASSERT(ithread != NULL,
317 	    ("interrupt handler \"%s\" has a NULL interrupt thread",
318 		handler->ih_name));
319 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
320 	    ithread->it_name);
321 	mtx_lock(&ithread->it_lock);
322 #ifdef INVARIANTS
323 	TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next)
324 		if (ih == handler)
325 			goto ok;
326 	mtx_unlock(&ithread->it_lock);
327 	panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"",
328 	    ih->ih_name, ithread->it_name);
329 ok:
330 #endif
331 	/*
332 	 * If the interrupt thread is already running, then just mark this
333 	 * handler as being dead and let the ithread do the actual removal.
334 	 *
335 	 * During a cold boot while cold is set, msleep() does not sleep,
336 	 * so we have to remove the handler here rather than letting the
337 	 * thread do it.
338 	 */
339 	mtx_lock_spin(&sched_lock);
340 	if (!TD_AWAITING_INTR(ithread->it_td) && !cold) {
341 		handler->ih_flags |= IH_DEAD;
342 
343 		/*
344 		 * Ensure that the thread will process the handler list
345 		 * again and remove this handler if it has already passed
346 		 * it on the list.
347 		 */
348 		ithread->it_need = 1;
349 	} else
350 		TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next);
351 	mtx_unlock_spin(&sched_lock);
352 	if ((handler->ih_flags & IH_DEAD) != 0)
353 		msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0);
354 	ithread_update(ithread);
355 	mtx_unlock(&ithread->it_lock);
356 	free(handler, M_ITHREAD);
357 	return (0);
358 }
359 
360 int
361 ithread_schedule(struct ithd *ithread, int do_switch)
362 {
363 	struct int_entropy entropy;
364 	struct thread *td;
365 	struct thread *ctd;
366 	struct proc *p;
367 
368 	/*
369 	 * If no ithread or no handlers, then we have a stray interrupt.
370 	 */
371 	if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers))
372 		return (EINVAL);
373 
374 	ctd = curthread;
375 	/*
376 	 * If any of the handlers for this ithread claim to be good
377 	 * sources of entropy, then gather some.
378 	 */
379 	if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) {
380 		entropy.vector = ithread->it_vector;
381 		entropy.proc = ctd->td_proc;
382 		random_harvest(&entropy, sizeof(entropy), 2, 0,
383 		    RANDOM_INTERRUPT);
384 	}
385 
386 	td = ithread->it_td;
387 	p = td->td_proc;
388 	KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name));
389 	CTR4(KTR_INTR, "%s: pid %d: (%s) need = %d",
390 	    __func__, p->p_pid, p->p_comm, ithread->it_need);
391 
392 	/*
393 	 * Set it_need to tell the thread to keep running if it is already
394 	 * running.  Then, grab sched_lock and see if we actually need to
395 	 * put this thread on the runqueue.  If so and the do_switch flag is
396 	 * true and it is safe to switch, then switch to the ithread
397 	 * immediately.  Otherwise, set the needresched flag to guarantee
398 	 * that this ithread will run before any userland processes.
399 	 */
400 	ithread->it_need = 1;
401 	mtx_lock_spin(&sched_lock);
402 	if (TD_AWAITING_INTR(td)) {
403 		CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, p->p_pid);
404 		TD_CLR_IWAIT(td);
405 		setrunqueue(td);
406 		if (do_switch &&
407 		    (ctd->td_critnest == 1) ) {
408 			KASSERT((TD_IS_RUNNING(ctd)),
409 			    ("ithread_schedule: Bad state for curthread."));
410 			if (ctd->td_flags & TDF_IDLETD)
411 				ctd->td_state = TDS_CAN_RUN; /* XXXKSE */
412 			mi_switch(SW_INVOL);
413 		} else {
414 			curthread->td_flags |= TDF_NEEDRESCHED;
415 		}
416 	} else {
417 		CTR4(KTR_INTR, "%s: pid %d: it_need %d, state %d",
418 		    __func__, p->p_pid, ithread->it_need, td->td_state);
419 	}
420 	mtx_unlock_spin(&sched_lock);
421 
422 	return (0);
423 }
424 
425 int
426 swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler,
427 	    void *arg, int pri, enum intr_type flags, void **cookiep)
428 {
429 	struct ithd *ithd;
430 	int error;
431 
432 	if (flags & (INTR_FAST | INTR_ENTROPY))
433 		return (EINVAL);
434 
435 	ithd = (ithdp != NULL) ? *ithdp : NULL;
436 
437 	if (ithd != NULL) {
438 		if ((ithd->it_flags & IT_SOFT) == 0)
439 			return(EINVAL);
440 	} else {
441 		error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL,
442 		    "swi%d:", pri);
443 		if (error)
444 			return (error);
445 
446 		if (ithdp != NULL)
447 			*ithdp = ithd;
448 	}
449 	return (ithread_add_handler(ithd, name, handler, arg,
450 		    (pri * RQ_PPQ) + PI_SOFT, flags, cookiep));
451 }
452 
453 
454 /*
455  * Schedule a heavyweight software interrupt process.
456  */
457 void
458 swi_sched(void *cookie, int flags)
459 {
460 	struct intrhand *ih = (struct intrhand *)cookie;
461 	struct ithd *it = ih->ih_ithread;
462 	int error;
463 
464 	atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */
465 
466 	CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d",
467 		it->it_td->td_proc->p_pid, it->it_td->td_proc->p_comm, it->it_need);
468 
469 	/*
470 	 * Set ih_need for this handler so that if the ithread is already
471 	 * running it will execute this handler on the next pass.  Otherwise,
472 	 * it will execute it the next time it runs.
473 	 */
474 	atomic_store_rel_int(&ih->ih_need, 1);
475 	if (!(flags & SWI_DELAY)) {
476 		error = ithread_schedule(it, !cold && !dumping);
477 		KASSERT(error == 0, ("stray software interrupt"));
478 	}
479 }
480 
481 /*
482  * This is the main code for interrupt threads.
483  */
484 static void
485 ithread_loop(void *arg)
486 {
487 	struct ithd *ithd;		/* our thread context */
488 	struct intrhand *ih;		/* and our interrupt handler chain */
489 	struct thread *td;
490 	struct proc *p;
491 
492 	td = curthread;
493 	p = td->td_proc;
494 	ithd = (struct ithd *)arg;	/* point to myself */
495 	KASSERT(ithd->it_td == td && td->td_ithd == ithd,
496 	    ("%s: ithread and proc linkage out of sync", __func__));
497 
498 	/*
499 	 * As long as we have interrupts outstanding, go through the
500 	 * list of handlers, giving each one a go at it.
501 	 */
502 	for (;;) {
503 		/*
504 		 * If we are an orphaned thread, then just die.
505 		 */
506 		if (ithd->it_flags & IT_DEAD) {
507 			CTR3(KTR_INTR, "%s: pid %d: (%s) exiting", __func__,
508 			    p->p_pid, p->p_comm);
509 			td->td_ithd = NULL;
510 			mtx_destroy(&ithd->it_lock);
511 			free(ithd, M_ITHREAD);
512 			kthread_exit(0);
513 		}
514 
515 		CTR4(KTR_INTR, "%s: pid %d: (%s) need=%d", __func__,
516 		     p->p_pid, p->p_comm, ithd->it_need);
517 		while (ithd->it_need) {
518 			/*
519 			 * Service interrupts.  If another interrupt
520 			 * arrives while we are running, they will set
521 			 * it_need to denote that we should make
522 			 * another pass.
523 			 */
524 			atomic_store_rel_int(&ithd->it_need, 0);
525 restart:
526 			TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
527 				if (ithd->it_flags & IT_SOFT && !ih->ih_need)
528 					continue;
529 				atomic_store_rel_int(&ih->ih_need, 0);
530 				CTR6(KTR_INTR,
531 				    "%s: pid %d ih=%p: %p(%p) flg=%x", __func__,
532 				    p->p_pid, (void *)ih,
533 				    (void *)ih->ih_handler, ih->ih_argument,
534 				    ih->ih_flags);
535 
536 				if ((ih->ih_flags & IH_DEAD) != 0) {
537 					mtx_lock(&ithd->it_lock);
538 					TAILQ_REMOVE(&ithd->it_handlers, ih,
539 					    ih_next);
540 					wakeup(ih);
541 					mtx_unlock(&ithd->it_lock);
542 					goto restart;
543 				}
544 				if ((ih->ih_flags & IH_MPSAFE) == 0)
545 					mtx_lock(&Giant);
546 				ih->ih_handler(ih->ih_argument);
547 				if ((ih->ih_flags & IH_MPSAFE) == 0)
548 					mtx_unlock(&Giant);
549 			}
550 		}
551 
552 		/*
553 		 * Processed all our interrupts.  Now get the sched
554 		 * lock.  This may take a while and it_need may get
555 		 * set again, so we have to check it again.
556 		 */
557 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
558 		mtx_assert(&Giant, MA_NOTOWNED);
559 		mtx_lock_spin(&sched_lock);
560 		if (!ithd->it_need) {
561 			/*
562 			 * Should we call this earlier in the loop above?
563 			 */
564 			if (ithd->it_enable != NULL)
565 				ithd->it_enable(ithd->it_vector);
566 			TD_SET_IWAIT(td); /* we're idle */
567 			CTR2(KTR_INTR, "%s: pid %d: done", __func__, p->p_pid);
568 			mi_switch(SW_VOL);
569 			CTR2(KTR_INTR, "%s: pid %d: resumed", __func__, p->p_pid);
570 		}
571 		mtx_unlock_spin(&sched_lock);
572 	}
573 }
574 
575 #ifdef DDB
576 /*
577  * Dump details about an interrupt handler
578  */
579 static void
580 db_dump_intrhand(struct intrhand *ih)
581 {
582 	int comma;
583 
584 	db_printf("\t%-10s ", ih->ih_name);
585 	switch (ih->ih_pri) {
586 	case PI_REALTIME:
587 		db_printf("CLK ");
588 		break;
589 	case PI_AV:
590 		db_printf("AV  ");
591 		break;
592 	case PI_TTYHIGH:
593 	case PI_TTYLOW:
594 		db_printf("TTY ");
595 		break;
596 	case PI_TAPE:
597 		db_printf("TAPE");
598 		break;
599 	case PI_NET:
600 		db_printf("NET ");
601 		break;
602 	case PI_DISK:
603 	case PI_DISKLOW:
604 		db_printf("DISK");
605 		break;
606 	case PI_DULL:
607 		db_printf("DULL");
608 		break;
609 	default:
610 		if (ih->ih_pri >= PI_SOFT)
611 			db_printf("SWI ");
612 		else
613 			db_printf("%4u", ih->ih_pri);
614 		break;
615 	}
616 	db_printf(" ");
617 	db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
618 	db_printf("(%p)", ih->ih_argument);
619 	if (ih->ih_need ||
620 	    (ih->ih_flags & (IH_FAST | IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
621 	    IH_MPSAFE)) != 0) {
622 		db_printf(" {");
623 		comma = 0;
624 		if (ih->ih_flags & IH_FAST) {
625 			db_printf("FAST");
626 			comma = 1;
627 		}
628 		if (ih->ih_flags & IH_EXCLUSIVE) {
629 			if (comma)
630 				db_printf(", ");
631 			db_printf("EXCL");
632 			comma = 1;
633 		}
634 		if (ih->ih_flags & IH_ENTROPY) {
635 			if (comma)
636 				db_printf(", ");
637 			db_printf("ENTROPY");
638 			comma = 1;
639 		}
640 		if (ih->ih_flags & IH_DEAD) {
641 			if (comma)
642 				db_printf(", ");
643 			db_printf("DEAD");
644 			comma = 1;
645 		}
646 		if (ih->ih_flags & IH_MPSAFE) {
647 			if (comma)
648 				db_printf(", ");
649 			db_printf("MPSAFE");
650 			comma = 1;
651 		}
652 		if (ih->ih_need) {
653 			if (comma)
654 				db_printf(", ");
655 			db_printf("NEED");
656 		}
657 		db_printf("}");
658 	}
659 	db_printf("\n");
660 }
661 
662 /*
663  * Dump details about an ithread
664  */
665 void
666 db_dump_ithread(struct ithd *ithd, int handlers)
667 {
668 	struct proc *p;
669 	struct intrhand *ih;
670 	int comma;
671 
672 	if (ithd->it_td != NULL) {
673 		p = ithd->it_td->td_proc;
674 		db_printf("%s (pid %d)", p->p_comm, p->p_pid);
675 	} else
676 		db_printf("%s: (no thread)", ithd->it_name);
677 	if ((ithd->it_flags & (IT_SOFT | IT_ENTROPY | IT_DEAD)) != 0 ||
678 	    ithd->it_need) {
679 		db_printf(" {");
680 		comma = 0;
681 		if (ithd->it_flags & IT_SOFT) {
682 			db_printf("SOFT");
683 			comma = 1;
684 		}
685 		if (ithd->it_flags & IT_ENTROPY) {
686 			if (comma)
687 				db_printf(", ");
688 			db_printf("ENTROPY");
689 			comma = 1;
690 		}
691 		if (ithd->it_flags & IT_DEAD) {
692 			if (comma)
693 				db_printf(", ");
694 			db_printf("DEAD");
695 			comma = 1;
696 		}
697 		if (ithd->it_need) {
698 			if (comma)
699 				db_printf(", ");
700 			db_printf("NEED");
701 		}
702 		db_printf("}");
703 	}
704 	db_printf("\n");
705 
706 	if (handlers)
707 		TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next)
708 		    db_dump_intrhand(ih);
709 }
710 #endif /* DDB */
711 
712 /*
713  * Start standard software interrupt threads
714  */
715 static void
716 start_softintr(void *dummy)
717 {
718 	struct proc *p;
719 
720 	if (swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK,
721 		INTR_MPSAFE, &softclock_ih) ||
722 	    swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
723 		panic("died while creating standard software ithreads");
724 
725 	p = clk_ithd->it_td->td_proc;
726 	PROC_LOCK(p);
727 	p->p_flag |= P_NOLOAD;
728 	PROC_UNLOCK(p);
729 }
730 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL)
731 
732 /*
733  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
734  * The data for this machine dependent, and the declarations are in machine
735  * dependent code.  The layout of intrnames and intrcnt however is machine
736  * independent.
737  *
738  * We do not know the length of intrcnt and intrnames at compile time, so
739  * calculate things at run time.
740  */
741 static int
742 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
743 {
744 	return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
745 	   req));
746 }
747 
748 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
749     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
750 
751 static int
752 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
753 {
754 	return (sysctl_handle_opaque(oidp, intrcnt,
755 	    (char *)eintrcnt - (char *)intrcnt, req));
756 }
757 
758 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
759     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
760 
761 #ifdef DDB
762 /*
763  * DDB command to dump the interrupt statistics.
764  */
765 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
766 {
767 	u_long *i;
768 	char *cp;
769 	int quit;
770 
771 	cp = intrnames;
772 	db_setup_paging(db_simple_pager, &quit, DB_LINES_PER_PAGE);
773 	for (i = intrcnt, quit = 0; i != eintrcnt && !quit; i++) {
774 		if (*cp == '\0')
775 			break;
776 		if (*i != 0)
777 			db_printf("%s\t%lu\n", cp, *i);
778 		cp += strlen(cp) + 1;
779 	}
780 }
781 #endif
782