xref: /freebsd/sys/kern/kern_intr.c (revision 7405fcc3381c8c93bf5f248b9a7bdf7b6021cf0e)
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/limits.h>
42 #include <sys/lock.h>
43 #include <sys/malloc.h>
44 #include <sys/mutex.h>
45 #include <sys/proc.h>
46 #include <sys/random.h>
47 #include <sys/resourcevar.h>
48 #include <sys/sched.h>
49 #include <sys/sysctl.h>
50 #include <sys/unistd.h>
51 #include <sys/vmmeter.h>
52 #include <machine/atomic.h>
53 #include <machine/cpu.h>
54 #include <machine/md_var.h>
55 #include <machine/stdarg.h>
56 #ifdef DDB
57 #include <ddb/ddb.h>
58 #include <ddb/db_sym.h>
59 #endif
60 
61 /*
62  * Describe an interrupt thread.  There is one of these per interrupt event.
63  */
64 struct intr_thread {
65 	struct intr_event *it_event;
66 	struct thread *it_thread;	/* Kernel thread. */
67 	int	it_flags;		/* (j) IT_* flags. */
68 	int	it_need;		/* Needs service. */
69 };
70 
71 /* Interrupt thread flags kept in it_flags */
72 #define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
73 
74 struct	intr_entropy {
75 	struct	thread *td;
76 	uintptr_t event;
77 };
78 
79 struct	intr_event *clk_intr_event;
80 struct	intr_event *tty_intr_event;
81 void	*softclock_ih;
82 void	*vm_ih;
83 
84 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
85 
86 static int intr_storm_threshold = 500;
87 TUNABLE_INT("hw.intr_storm_threshold", &intr_storm_threshold);
88 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RW,
89     &intr_storm_threshold, 0,
90     "Number of consecutive interrupts before storm protection is enabled");
91 static TAILQ_HEAD(, intr_event) event_list =
92     TAILQ_HEAD_INITIALIZER(event_list);
93 
94 static void	intr_event_update(struct intr_event *ie);
95 static struct intr_thread *ithread_create(const char *name);
96 static void	ithread_destroy(struct intr_thread *ithread);
97 static void	ithread_execute_handlers(struct proc *p, struct intr_event *ie);
98 static void	ithread_loop(void *);
99 static void	ithread_update(struct intr_thread *ithd);
100 static void	start_softintr(void *);
101 
102 /* Map an interrupt type to an ithread priority. */
103 u_char
104 intr_priority(enum intr_type flags)
105 {
106 	u_char pri;
107 
108 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
109 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
110 	switch (flags) {
111 	case INTR_TYPE_TTY:
112 		pri = PI_TTYLOW;
113 		break;
114 	case INTR_TYPE_BIO:
115 		/*
116 		 * XXX We need to refine this.  BSD/OS distinguishes
117 		 * between tape and disk priorities.
118 		 */
119 		pri = PI_DISK;
120 		break;
121 	case INTR_TYPE_NET:
122 		pri = PI_NET;
123 		break;
124 	case INTR_TYPE_CAM:
125 		pri = PI_DISK;          /* XXX or PI_CAM? */
126 		break;
127 	case INTR_TYPE_AV:		/* Audio/video */
128 		pri = PI_AV;
129 		break;
130 	case INTR_TYPE_CLK:
131 		pri = PI_REALTIME;
132 		break;
133 	case INTR_TYPE_MISC:
134 		pri = PI_DULL;          /* don't care */
135 		break;
136 	default:
137 		/* We didn't specify an interrupt level. */
138 		panic("intr_priority: no interrupt type in flags");
139 	}
140 
141 	return pri;
142 }
143 
144 /*
145  * Update an ithread based on the associated intr_event.
146  */
147 static void
148 ithread_update(struct intr_thread *ithd)
149 {
150 	struct intr_event *ie;
151 	struct thread *td;
152 	u_char pri;
153 
154 	ie = ithd->it_event;
155 	td = ithd->it_thread;
156 
157 	/* Determine the overall priority of this event. */
158 	if (TAILQ_EMPTY(&ie->ie_handlers))
159 		pri = PRI_MAX_ITHD;
160 	else
161 		pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
162 
163 	/* Update name and priority. */
164 	strlcpy(td->td_proc->p_comm, ie->ie_fullname,
165 	    sizeof(td->td_proc->p_comm));
166 	mtx_lock_spin(&sched_lock);
167 	sched_prio(td, pri);
168 	mtx_unlock_spin(&sched_lock);
169 }
170 
171 /*
172  * Regenerate the full name of an interrupt event and update its priority.
173  */
174 static void
175 intr_event_update(struct intr_event *ie)
176 {
177 	struct intr_handler *ih;
178 	char *last;
179 	int missed, space;
180 
181 	/* Start off with no entropy and just the name of the event. */
182 	mtx_assert(&ie->ie_lock, MA_OWNED);
183 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
184 	ie->ie_flags &= ~IE_ENTROPY;
185 	missed = 0;
186 	space = 1;
187 
188 	/* Run through all the handlers updating values. */
189 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
190 		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
191 		    sizeof(ie->ie_fullname)) {
192 			strcat(ie->ie_fullname, " ");
193 			strcat(ie->ie_fullname, ih->ih_name);
194 			space = 0;
195 		} else
196 			missed++;
197 		if (ih->ih_flags & IH_ENTROPY)
198 			ie->ie_flags |= IE_ENTROPY;
199 	}
200 
201 	/*
202 	 * If the handler names were too long, add +'s to indicate missing
203 	 * names. If we run out of room and still have +'s to add, change
204 	 * the last character from a + to a *.
205 	 */
206 	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
207 	while (missed-- > 0) {
208 		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
209 			if (*last == '+') {
210 				*last = '*';
211 				break;
212 			} else
213 				*last = '+';
214 		} else if (space) {
215 			strcat(ie->ie_fullname, " +");
216 			space = 0;
217 		} else
218 			strcat(ie->ie_fullname, "+");
219 	}
220 
221 	/*
222 	 * If this event has an ithread, update it's priority and
223 	 * name.
224 	 */
225 	if (ie->ie_thread != NULL)
226 		ithread_update(ie->ie_thread);
227 	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
228 }
229 
230 int
231 intr_event_create(struct intr_event **event, void *source, int flags,
232     void (*enable)(void *), const char *fmt, ...)
233 {
234 	struct intr_event *ie;
235 	va_list ap;
236 
237 	/* The only valid flag during creation is IE_SOFT. */
238 	if ((flags & ~IE_SOFT) != 0)
239 		return (EINVAL);
240 	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
241 	ie->ie_source = source;
242 	ie->ie_enable = enable;
243 	ie->ie_flags = flags;
244 	TAILQ_INIT(&ie->ie_handlers);
245 	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
246 
247 	va_start(ap, fmt);
248 	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
249 	va_end(ap);
250 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
251 	mtx_pool_lock(mtxpool_sleep, &event_list);
252 	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
253 	mtx_pool_unlock(mtxpool_sleep, &event_list);
254 	if (event != NULL)
255 		*event = ie;
256 	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
257 	return (0);
258 }
259 
260 int
261 intr_event_destroy(struct intr_event *ie)
262 {
263 
264 	mtx_lock(&ie->ie_lock);
265 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
266 		mtx_unlock(&ie->ie_lock);
267 		return (EBUSY);
268 	}
269 	mtx_pool_lock(mtxpool_sleep, &event_list);
270 	TAILQ_REMOVE(&event_list, ie, ie_list);
271 	mtx_pool_unlock(mtxpool_sleep, &event_list);
272 #ifndef notyet
273 	if (ie->ie_thread != NULL) {
274 		ithread_destroy(ie->ie_thread);
275 		ie->ie_thread = NULL;
276 	}
277 #endif
278 	mtx_unlock(&ie->ie_lock);
279 	mtx_destroy(&ie->ie_lock);
280 	free(ie, M_ITHREAD);
281 	return (0);
282 }
283 
284 static struct intr_thread *
285 ithread_create(const char *name)
286 {
287 	struct intr_thread *ithd;
288 	struct thread *td;
289 	struct proc *p;
290 	int error;
291 
292 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
293 
294 	error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID,
295 	    0, "%s", name);
296 	if (error)
297 		panic("kthread_create() failed with %d", error);
298 	td = FIRST_THREAD_IN_PROC(p);	/* XXXKSE */
299 	mtx_lock_spin(&sched_lock);
300 	sched_class(td, PRI_ITHD);
301 	TD_SET_IWAIT(td);
302 	mtx_unlock_spin(&sched_lock);
303 	td->td_pflags |= TDP_ITHREAD;
304 	ithd->it_thread = td;
305 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
306 	return (ithd);
307 }
308 
309 static void
310 ithread_destroy(struct intr_thread *ithread)
311 {
312 	struct thread *td;
313 
314 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
315 	td = ithread->it_thread;
316 	mtx_lock_spin(&sched_lock);
317 	ithread->it_flags |= IT_DEAD;
318 	if (TD_AWAITING_INTR(td)) {
319 		TD_CLR_IWAIT(td);
320 		sched_add(td, SRQ_INTR);
321 	}
322 	mtx_unlock_spin(&sched_lock);
323 }
324 
325 int
326 intr_event_add_handler(struct intr_event *ie, const char *name,
327     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
328     enum intr_type flags, void **cookiep)
329 {
330 	struct intr_handler *ih, *temp_ih;
331 	struct intr_thread *it;
332 
333 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
334 		return (EINVAL);
335 
336 	/* Allocate and populate an interrupt handler structure. */
337 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
338 	ih->ih_filter = filter;
339 	ih->ih_handler = handler;
340 	ih->ih_argument = arg;
341 	ih->ih_name = name;
342 	ih->ih_event = ie;
343 	ih->ih_pri = pri;
344 	if (flags & INTR_EXCL)
345 		ih->ih_flags = IH_EXCLUSIVE;
346 	if (flags & INTR_MPSAFE)
347 		ih->ih_flags |= IH_MPSAFE;
348 	if (flags & INTR_ENTROPY)
349 		ih->ih_flags |= IH_ENTROPY;
350 
351 	/* We can only have one exclusive handler in a event. */
352 	mtx_lock(&ie->ie_lock);
353 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
354 		if ((flags & INTR_EXCL) ||
355 		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
356 			mtx_unlock(&ie->ie_lock);
357 			free(ih, M_ITHREAD);
358 			return (EINVAL);
359 		}
360 	}
361 
362 	/* Add the new handler to the event in priority order. */
363 	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
364 		if (temp_ih->ih_pri > ih->ih_pri)
365 			break;
366 	}
367 	if (temp_ih == NULL)
368 		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
369 	else
370 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
371 	intr_event_update(ie);
372 
373 	/* Create a thread if we need one. */
374 	while (ie->ie_thread == NULL && handler != NULL) {
375 		if (ie->ie_flags & IE_ADDING_THREAD)
376 			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
377 		else {
378 			ie->ie_flags |= IE_ADDING_THREAD;
379 			mtx_unlock(&ie->ie_lock);
380 			it = ithread_create("intr: newborn");
381 			mtx_lock(&ie->ie_lock);
382 			ie->ie_flags &= ~IE_ADDING_THREAD;
383 			ie->ie_thread = it;
384 			it->it_event = ie;
385 			ithread_update(it);
386 			wakeup(ie);
387 		}
388 	}
389 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
390 	    ie->ie_name);
391 	mtx_unlock(&ie->ie_lock);
392 
393 	if (cookiep != NULL)
394 		*cookiep = ih;
395 	return (0);
396 }
397 
398 /*
399  * Return the ie_source field from the intr_event an intr_handler is
400  * associated with.
401  */
402 void *
403 intr_handler_source(void *cookie)
404 {
405 	struct intr_handler *ih;
406 	struct intr_event *ie;
407 
408 	ih = (struct intr_handler *)cookie;
409 	if (ih == NULL)
410 		return (NULL);
411 	ie = ih->ih_event;
412 	KASSERT(ie != NULL,
413 	    ("interrupt handler \"%s\" has a NULL interrupt event",
414 	    ih->ih_name));
415 	return (ie->ie_source);
416 }
417 
418 int
419 intr_event_remove_handler(void *cookie)
420 {
421 	struct intr_handler *handler = (struct intr_handler *)cookie;
422 	struct intr_event *ie;
423 #ifdef INVARIANTS
424 	struct intr_handler *ih;
425 #endif
426 #ifdef notyet
427 	int dead;
428 #endif
429 
430 	if (handler == NULL)
431 		return (EINVAL);
432 	ie = handler->ih_event;
433 	KASSERT(ie != NULL,
434 	    ("interrupt handler \"%s\" has a NULL interrupt event",
435 	    handler->ih_name));
436 	mtx_lock(&ie->ie_lock);
437 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
438 	    ie->ie_name);
439 #ifdef INVARIANTS
440 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
441 		if (ih == handler)
442 			goto ok;
443 	mtx_unlock(&ie->ie_lock);
444 	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
445 	    ih->ih_name, ie->ie_name);
446 ok:
447 #endif
448 	/*
449 	 * If there is no ithread, then just remove the handler and return.
450 	 * XXX: Note that an INTR_FAST handler might be running on another
451 	 * CPU!
452 	 */
453 	if (ie->ie_thread == NULL) {
454 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
455 		mtx_unlock(&ie->ie_lock);
456 		free(handler, M_ITHREAD);
457 		return (0);
458 	}
459 
460 	/*
461 	 * If the interrupt thread is already running, then just mark this
462 	 * handler as being dead and let the ithread do the actual removal.
463 	 *
464 	 * During a cold boot while cold is set, msleep() does not sleep,
465 	 * so we have to remove the handler here rather than letting the
466 	 * thread do it.
467 	 */
468 	mtx_lock_spin(&sched_lock);
469 	if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
470 		handler->ih_flags |= IH_DEAD;
471 
472 		/*
473 		 * Ensure that the thread will process the handler list
474 		 * again and remove this handler if it has already passed
475 		 * it on the list.
476 		 */
477 		ie->ie_thread->it_need = 1;
478 	} else
479 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
480 	mtx_unlock_spin(&sched_lock);
481 	while (handler->ih_flags & IH_DEAD)
482 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
483 	intr_event_update(ie);
484 #ifdef notyet
485 	/*
486 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
487 	 * this could lead to races of stale data when servicing an
488 	 * interrupt.
489 	 */
490 	dead = 1;
491 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
492 		if (!(ih->ih_flags & IH_FAST)) {
493 			dead = 0;
494 			break;
495 		}
496 	}
497 	if (dead) {
498 		ithread_destroy(ie->ie_thread);
499 		ie->ie_thread = NULL;
500 	}
501 #endif
502 	mtx_unlock(&ie->ie_lock);
503 	free(handler, M_ITHREAD);
504 	return (0);
505 }
506 
507 int
508 intr_event_schedule_thread(struct intr_event *ie)
509 {
510 	struct intr_entropy entropy;
511 	struct intr_thread *it;
512 	struct thread *td;
513 	struct thread *ctd;
514 	struct proc *p;
515 
516 	/*
517 	 * If no ithread or no handlers, then we have a stray interrupt.
518 	 */
519 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
520 	    ie->ie_thread == NULL)
521 		return (EINVAL);
522 
523 	ctd = curthread;
524 	it = ie->ie_thread;
525 	td = it->it_thread;
526 	p = td->td_proc;
527 
528 	/*
529 	 * If any of the handlers for this ithread claim to be good
530 	 * sources of entropy, then gather some.
531 	 */
532 	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
533 		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
534 		    p->p_pid, p->p_comm);
535 		entropy.event = (uintptr_t)ie;
536 		entropy.td = ctd;
537 		random_harvest(&entropy, sizeof(entropy), 2, 0,
538 		    RANDOM_INTERRUPT);
539 	}
540 
541 	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
542 
543 	/*
544 	 * Set it_need to tell the thread to keep running if it is already
545 	 * running.  Then, grab sched_lock and see if we actually need to
546 	 * put this thread on the runqueue.
547 	 */
548 	it->it_need = 1;
549 	mtx_lock_spin(&sched_lock);
550 	if (TD_AWAITING_INTR(td)) {
551 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
552 		    p->p_comm);
553 		TD_CLR_IWAIT(td);
554 		sched_add(td, SRQ_INTR);
555 	} else {
556 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
557 		    __func__, p->p_pid, p->p_comm, it->it_need, td->td_state);
558 	}
559 	mtx_unlock_spin(&sched_lock);
560 
561 	return (0);
562 }
563 
564 /*
565  * Add a software interrupt handler to a specified event.  If a given event
566  * is not specified, then a new event is created.
567  */
568 int
569 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
570 	    void *arg, int pri, enum intr_type flags, void **cookiep)
571 {
572 	struct intr_event *ie;
573 	int error;
574 
575 	if (flags & (INTR_FAST | INTR_ENTROPY))
576 		return (EINVAL);
577 
578 	ie = (eventp != NULL) ? *eventp : NULL;
579 
580 	if (ie != NULL) {
581 		if (!(ie->ie_flags & IE_SOFT))
582 			return (EINVAL);
583 	} else {
584 		error = intr_event_create(&ie, NULL, IE_SOFT, NULL,
585 		    "swi%d:", pri);
586 		if (error)
587 			return (error);
588 		if (eventp != NULL)
589 			*eventp = ie;
590 	}
591 	return (intr_event_add_handler(ie, name, NULL, handler, arg,
592 		    (pri * RQ_PPQ) + PI_SOFT, flags, cookiep));
593 		    /* XXKSE.. think of a better way to get separate queues */
594 }
595 
596 /*
597  * Schedule a software interrupt thread.
598  */
599 void
600 swi_sched(void *cookie, int flags)
601 {
602 	struct intr_handler *ih = (struct intr_handler *)cookie;
603 	struct intr_event *ie = ih->ih_event;
604 	int error;
605 
606 	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
607 	    ih->ih_need);
608 
609 	/*
610 	 * Set ih_need for this handler so that if the ithread is already
611 	 * running it will execute this handler on the next pass.  Otherwise,
612 	 * it will execute it the next time it runs.
613 	 */
614 	atomic_store_rel_int(&ih->ih_need, 1);
615 
616 	if (!(flags & SWI_DELAY)) {
617 		PCPU_LAZY_INC(cnt.v_soft);
618 		error = intr_event_schedule_thread(ie);
619 		KASSERT(error == 0, ("stray software interrupt"));
620 	}
621 }
622 
623 /*
624  * Remove a software interrupt handler.  Currently this code does not
625  * remove the associated interrupt event if it becomes empty.  Calling code
626  * may do so manually via intr_event_destroy(), but that's not really
627  * an optimal interface.
628  */
629 int
630 swi_remove(void *cookie)
631 {
632 
633 	return (intr_event_remove_handler(cookie));
634 }
635 
636 static void
637 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
638 {
639 	struct intr_handler *ih, *ihn;
640 
641 	/* Interrupt handlers should not sleep. */
642 	if (!(ie->ie_flags & IE_SOFT))
643 		THREAD_NO_SLEEPING();
644 	TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
645 
646 		/*
647 		 * If this handler is marked for death, remove it from
648 		 * the list of handlers and wake up the sleeper.
649 		 */
650 		if (ih->ih_flags & IH_DEAD) {
651 			mtx_lock(&ie->ie_lock);
652 			TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
653 			ih->ih_flags &= ~IH_DEAD;
654 			wakeup(ih);
655 			mtx_unlock(&ie->ie_lock);
656 			continue;
657 		}
658 
659 		/*
660 		 * For software interrupt threads, we only execute
661 		 * handlers that have their need flag set.  Hardware
662 		 * interrupt threads always invoke all of their handlers.
663 		 */
664 		if (ie->ie_flags & IE_SOFT) {
665 			if (!ih->ih_need)
666 				continue;
667 			else
668 				atomic_store_rel_int(&ih->ih_need, 0);
669 		}
670 
671 		/* Execute this handler. */
672 		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
673 		    __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
674 		    ih->ih_name, ih->ih_flags);
675 
676 		if (!(ih->ih_flags & IH_MPSAFE))
677 			mtx_lock(&Giant);
678 		ih->ih_handler(ih->ih_argument);
679 		if (!(ih->ih_flags & IH_MPSAFE))
680 			mtx_unlock(&Giant);
681 	}
682 	if (!(ie->ie_flags & IE_SOFT))
683 		THREAD_SLEEPING_OK();
684 
685 	/*
686 	 * Interrupt storm handling:
687 	 *
688 	 * If this interrupt source is currently storming, then throttle
689 	 * it to only fire the handler once  per clock tick.
690 	 *
691 	 * If this interrupt source is not currently storming, but the
692 	 * number of back to back interrupts exceeds the storm threshold,
693 	 * then enter storming mode.
694 	 */
695 	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold) {
696 		if (ie->ie_warned == 0) {
697 			printf(
698 	"Interrupt storm detected on \"%s\"; throttling interrupt source\n",
699 			    ie->ie_name);
700 			ie->ie_warned = 1;
701 		}
702 		tsleep(&ie->ie_count, 0, "istorm", 1);
703 	} else
704 		ie->ie_count++;
705 
706 	/*
707 	 * Now that all the handlers have had a chance to run, reenable
708 	 * the interrupt source.
709 	 */
710 	if (ie->ie_enable != NULL)
711 		ie->ie_enable(ie->ie_source);
712 }
713 
714 /*
715  * This is the main code for interrupt threads.
716  */
717 static void
718 ithread_loop(void *arg)
719 {
720 	struct intr_thread *ithd;
721 	struct intr_event *ie;
722 	struct thread *td;
723 	struct proc *p;
724 
725 	td = curthread;
726 	p = td->td_proc;
727 	ithd = (struct intr_thread *)arg;
728 	KASSERT(ithd->it_thread == td,
729 	    ("%s: ithread and proc linkage out of sync", __func__));
730 	ie = ithd->it_event;
731 	ie->ie_count = 0;
732 
733 	/*
734 	 * As long as we have interrupts outstanding, go through the
735 	 * list of handlers, giving each one a go at it.
736 	 */
737 	for (;;) {
738 		/*
739 		 * If we are an orphaned thread, then just die.
740 		 */
741 		if (ithd->it_flags & IT_DEAD) {
742 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
743 			    p->p_pid, p->p_comm);
744 			free(ithd, M_ITHREAD);
745 			kthread_exit(0);
746 		}
747 
748 		/*
749 		 * Service interrupts.  If another interrupt arrives while
750 		 * we are running, it will set it_need to note that we
751 		 * should make another pass.
752 		 */
753 		while (ithd->it_need) {
754 			/*
755 			 * This might need a full read and write barrier
756 			 * to make sure that this write posts before any
757 			 * of the memory or device accesses in the
758 			 * handlers.
759 			 */
760 			atomic_store_rel_int(&ithd->it_need, 0);
761 			ithread_execute_handlers(p, ie);
762 		}
763 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
764 		mtx_assert(&Giant, MA_NOTOWNED);
765 
766 		/*
767 		 * Processed all our interrupts.  Now get the sched
768 		 * lock.  This may take a while and it_need may get
769 		 * set again, so we have to check it again.
770 		 */
771 		mtx_lock_spin(&sched_lock);
772 		if (!ithd->it_need && !(ithd->it_flags & IT_DEAD)) {
773 			TD_SET_IWAIT(td);
774 			ie->ie_count = 0;
775 			mi_switch(SW_VOL, NULL);
776 		}
777 		mtx_unlock_spin(&sched_lock);
778 	}
779 }
780 
781 #ifdef DDB
782 /*
783  * Dump details about an interrupt handler
784  */
785 static void
786 db_dump_intrhand(struct intr_handler *ih)
787 {
788 	int comma;
789 
790 	db_printf("\t%-10s ", ih->ih_name);
791 	switch (ih->ih_pri) {
792 	case PI_REALTIME:
793 		db_printf("CLK ");
794 		break;
795 	case PI_AV:
796 		db_printf("AV  ");
797 		break;
798 	case PI_TTYHIGH:
799 	case PI_TTYLOW:
800 		db_printf("TTY ");
801 		break;
802 	case PI_TAPE:
803 		db_printf("TAPE");
804 		break;
805 	case PI_NET:
806 		db_printf("NET ");
807 		break;
808 	case PI_DISK:
809 	case PI_DISKLOW:
810 		db_printf("DISK");
811 		break;
812 	case PI_DULL:
813 		db_printf("DULL");
814 		break;
815 	default:
816 		if (ih->ih_pri >= PI_SOFT)
817 			db_printf("SWI ");
818 		else
819 			db_printf("%4u", ih->ih_pri);
820 		break;
821 	}
822 	db_printf(" ");
823 	db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
824 	db_printf("(%p)", ih->ih_argument);
825 	if (ih->ih_need ||
826 	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
827 	    IH_MPSAFE)) != 0) {
828 		db_printf(" {");
829 		comma = 0;
830 		if (ih->ih_flags & IH_EXCLUSIVE) {
831 			if (comma)
832 				db_printf(", ");
833 			db_printf("EXCL");
834 			comma = 1;
835 		}
836 		if (ih->ih_flags & IH_ENTROPY) {
837 			if (comma)
838 				db_printf(", ");
839 			db_printf("ENTROPY");
840 			comma = 1;
841 		}
842 		if (ih->ih_flags & IH_DEAD) {
843 			if (comma)
844 				db_printf(", ");
845 			db_printf("DEAD");
846 			comma = 1;
847 		}
848 		if (ih->ih_flags & IH_MPSAFE) {
849 			if (comma)
850 				db_printf(", ");
851 			db_printf("MPSAFE");
852 			comma = 1;
853 		}
854 		if (ih->ih_need) {
855 			if (comma)
856 				db_printf(", ");
857 			db_printf("NEED");
858 		}
859 		db_printf("}");
860 	}
861 	db_printf("\n");
862 }
863 
864 /*
865  * Dump details about a event.
866  */
867 void
868 db_dump_intr_event(struct intr_event *ie, int handlers)
869 {
870 	struct intr_handler *ih;
871 	struct intr_thread *it;
872 	int comma;
873 
874 	db_printf("%s ", ie->ie_fullname);
875 	it = ie->ie_thread;
876 	if (it != NULL)
877 		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
878 	else
879 		db_printf("(no thread)");
880 	if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
881 	    (it != NULL && it->it_need)) {
882 		db_printf(" {");
883 		comma = 0;
884 		if (ie->ie_flags & IE_SOFT) {
885 			db_printf("SOFT");
886 			comma = 1;
887 		}
888 		if (ie->ie_flags & IE_ENTROPY) {
889 			if (comma)
890 				db_printf(", ");
891 			db_printf("ENTROPY");
892 			comma = 1;
893 		}
894 		if (ie->ie_flags & IE_ADDING_THREAD) {
895 			if (comma)
896 				db_printf(", ");
897 			db_printf("ADDING_THREAD");
898 			comma = 1;
899 		}
900 		if (it != NULL && it->it_need) {
901 			if (comma)
902 				db_printf(", ");
903 			db_printf("NEED");
904 		}
905 		db_printf("}");
906 	}
907 	db_printf("\n");
908 
909 	if (handlers)
910 		TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
911 		    db_dump_intrhand(ih);
912 }
913 
914 /*
915  * Dump data about interrupt handlers
916  */
917 DB_SHOW_COMMAND(intr, db_show_intr)
918 {
919 	struct intr_event *ie;
920 	int all, verbose;
921 
922 	verbose = index(modif, 'v') != NULL;
923 	all = index(modif, 'a') != NULL;
924 	TAILQ_FOREACH(ie, &event_list, ie_list) {
925 		if (!all && TAILQ_EMPTY(&ie->ie_handlers))
926 			continue;
927 		db_dump_intr_event(ie, verbose);
928 		if (db_pager_quit)
929 			break;
930 	}
931 }
932 #endif /* DDB */
933 
934 /*
935  * Start standard software interrupt threads
936  */
937 static void
938 start_softintr(void *dummy)
939 {
940 	struct proc *p;
941 
942 	if (swi_add(&clk_intr_event, "clock", softclock, NULL, SWI_CLOCK,
943 		INTR_MPSAFE, &softclock_ih) ||
944 	    swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
945 		panic("died while creating standard software ithreads");
946 
947 	p = clk_intr_event->ie_thread->it_thread->td_proc;
948 	PROC_LOCK(p);
949 	p->p_flag |= P_NOLOAD;
950 	PROC_UNLOCK(p);
951 }
952 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL)
953 
954 /*
955  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
956  * The data for this machine dependent, and the declarations are in machine
957  * dependent code.  The layout of intrnames and intrcnt however is machine
958  * independent.
959  *
960  * We do not know the length of intrcnt and intrnames at compile time, so
961  * calculate things at run time.
962  */
963 static int
964 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
965 {
966 	return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames,
967 	   req));
968 }
969 
970 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
971     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
972 
973 static int
974 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
975 {
976 	return (sysctl_handle_opaque(oidp, intrcnt,
977 	    (char *)eintrcnt - (char *)intrcnt, req));
978 }
979 
980 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
981     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
982 
983 #ifdef DDB
984 /*
985  * DDB command to dump the interrupt statistics.
986  */
987 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
988 {
989 	u_long *i;
990 	char *cp;
991 
992 	cp = intrnames;
993 	for (i = intrcnt; i != eintrcnt && !db_pager_quit; i++) {
994 		if (*cp == '\0')
995 			break;
996 		if (*i != 0)
997 			db_printf("%s\t%lu\n", cp, *i);
998 		cp += strlen(cp) + 1;
999 	}
1000 }
1001 #endif
1002