xref: /freebsd/sys/kern/kern_intr.c (revision 5bd73b51076b5cb5a2c9810f76c1d7ed20c4460e)
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 #include "opt_kstack_usage_prof.h"
32 
33 #include <sys/param.h>
34 #include <sys/bus.h>
35 #include <sys/conf.h>
36 #include <sys/cpuset.h>
37 #include <sys/rtprio.h>
38 #include <sys/systm.h>
39 #include <sys/interrupt.h>
40 #include <sys/kernel.h>
41 #include <sys/kthread.h>
42 #include <sys/ktr.h>
43 #include <sys/limits.h>
44 #include <sys/lock.h>
45 #include <sys/malloc.h>
46 #include <sys/mutex.h>
47 #include <sys/priv.h>
48 #include <sys/proc.h>
49 #include <sys/random.h>
50 #include <sys/resourcevar.h>
51 #include <sys/sched.h>
52 #include <sys/smp.h>
53 #include <sys/sysctl.h>
54 #include <sys/syslog.h>
55 #include <sys/unistd.h>
56 #include <sys/vmmeter.h>
57 #include <machine/atomic.h>
58 #include <machine/cpu.h>
59 #include <machine/md_var.h>
60 #include <machine/stdarg.h>
61 #ifdef DDB
62 #include <ddb/ddb.h>
63 #include <ddb/db_sym.h>
64 #endif
65 
66 /*
67  * Describe an interrupt thread.  There is one of these per interrupt event.
68  */
69 struct intr_thread {
70 	struct intr_event *it_event;
71 	struct thread *it_thread;	/* Kernel thread. */
72 	int	it_flags;		/* (j) IT_* flags. */
73 	int	it_need;		/* Needs service. */
74 };
75 
76 /* Interrupt thread flags kept in it_flags */
77 #define	IT_DEAD		0x000001	/* Thread is waiting to exit. */
78 #define	IT_WAIT		0x000002	/* Thread is waiting for completion. */
79 
80 struct	intr_entropy {
81 	struct	thread *td;
82 	uintptr_t event;
83 };
84 
85 struct	intr_event *clk_intr_event;
86 struct	intr_event *tty_intr_event;
87 void	*vm_ih;
88 struct proc *intrproc;
89 
90 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
91 
92 static int intr_storm_threshold = 1000;
93 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
94     &intr_storm_threshold, 0,
95     "Number of consecutive interrupts before storm protection is enabled");
96 static TAILQ_HEAD(, intr_event) event_list =
97     TAILQ_HEAD_INITIALIZER(event_list);
98 static struct mtx event_lock;
99 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
100 
101 static void	intr_event_update(struct intr_event *ie);
102 #ifdef INTR_FILTER
103 static int	intr_event_schedule_thread(struct intr_event *ie,
104 		    struct intr_thread *ithd);
105 static int	intr_filter_loop(struct intr_event *ie,
106 		    struct trapframe *frame, struct intr_thread **ithd);
107 static struct intr_thread *ithread_create(const char *name,
108 			      struct intr_handler *ih);
109 #else
110 static int	intr_event_schedule_thread(struct intr_event *ie);
111 static struct intr_thread *ithread_create(const char *name);
112 #endif
113 static void	ithread_destroy(struct intr_thread *ithread);
114 static void	ithread_execute_handlers(struct proc *p,
115 		    struct intr_event *ie);
116 #ifdef INTR_FILTER
117 static void	priv_ithread_execute_handler(struct proc *p,
118 		    struct intr_handler *ih);
119 #endif
120 static void	ithread_loop(void *);
121 static void	ithread_update(struct intr_thread *ithd);
122 static void	start_softintr(void *);
123 
124 /* Map an interrupt type to an ithread priority. */
125 u_char
126 intr_priority(enum intr_type flags)
127 {
128 	u_char pri;
129 
130 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
131 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
132 	switch (flags) {
133 	case INTR_TYPE_TTY:
134 		pri = PI_TTY;
135 		break;
136 	case INTR_TYPE_BIO:
137 		pri = PI_DISK;
138 		break;
139 	case INTR_TYPE_NET:
140 		pri = PI_NET;
141 		break;
142 	case INTR_TYPE_CAM:
143 		pri = PI_DISK;
144 		break;
145 	case INTR_TYPE_AV:
146 		pri = PI_AV;
147 		break;
148 	case INTR_TYPE_CLK:
149 		pri = PI_REALTIME;
150 		break;
151 	case INTR_TYPE_MISC:
152 		pri = PI_DULL;          /* don't care */
153 		break;
154 	default:
155 		/* We didn't specify an interrupt level. */
156 		panic("intr_priority: no interrupt type in flags");
157 	}
158 
159 	return pri;
160 }
161 
162 /*
163  * Update an ithread based on the associated intr_event.
164  */
165 static void
166 ithread_update(struct intr_thread *ithd)
167 {
168 	struct intr_event *ie;
169 	struct thread *td;
170 	u_char pri;
171 
172 	ie = ithd->it_event;
173 	td = ithd->it_thread;
174 
175 	/* Determine the overall priority of this event. */
176 	if (TAILQ_EMPTY(&ie->ie_handlers))
177 		pri = PRI_MAX_ITHD;
178 	else
179 		pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
180 
181 	/* Update name and priority. */
182 	strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
183 #ifdef KTR
184 	sched_clear_tdname(td);
185 #endif
186 	thread_lock(td);
187 	sched_prio(td, pri);
188 	thread_unlock(td);
189 }
190 
191 /*
192  * Regenerate the full name of an interrupt event and update its priority.
193  */
194 static void
195 intr_event_update(struct intr_event *ie)
196 {
197 	struct intr_handler *ih;
198 	char *last;
199 	int missed, space;
200 
201 	/* Start off with no entropy and just the name of the event. */
202 	mtx_assert(&ie->ie_lock, MA_OWNED);
203 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
204 	ie->ie_flags &= ~IE_ENTROPY;
205 	missed = 0;
206 	space = 1;
207 
208 	/* Run through all the handlers updating values. */
209 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
210 		if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
211 		    sizeof(ie->ie_fullname)) {
212 			strcat(ie->ie_fullname, " ");
213 			strcat(ie->ie_fullname, ih->ih_name);
214 			space = 0;
215 		} else
216 			missed++;
217 		if (ih->ih_flags & IH_ENTROPY)
218 			ie->ie_flags |= IE_ENTROPY;
219 	}
220 
221 	/*
222 	 * If the handler names were too long, add +'s to indicate missing
223 	 * names. If we run out of room and still have +'s to add, change
224 	 * the last character from a + to a *.
225 	 */
226 	last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
227 	while (missed-- > 0) {
228 		if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
229 			if (*last == '+') {
230 				*last = '*';
231 				break;
232 			} else
233 				*last = '+';
234 		} else if (space) {
235 			strcat(ie->ie_fullname, " +");
236 			space = 0;
237 		} else
238 			strcat(ie->ie_fullname, "+");
239 	}
240 
241 	/*
242 	 * If this event has an ithread, update it's priority and
243 	 * name.
244 	 */
245 	if (ie->ie_thread != NULL)
246 		ithread_update(ie->ie_thread);
247 	CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
248 }
249 
250 int
251 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
252     void (*pre_ithread)(void *), void (*post_ithread)(void *),
253     void (*post_filter)(void *), int (*assign_cpu)(void *, int),
254     const char *fmt, ...)
255 {
256 	struct intr_event *ie;
257 	va_list ap;
258 
259 	/* The only valid flag during creation is IE_SOFT. */
260 	if ((flags & ~IE_SOFT) != 0)
261 		return (EINVAL);
262 	ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
263 	ie->ie_source = source;
264 	ie->ie_pre_ithread = pre_ithread;
265 	ie->ie_post_ithread = post_ithread;
266 	ie->ie_post_filter = post_filter;
267 	ie->ie_assign_cpu = assign_cpu;
268 	ie->ie_flags = flags;
269 	ie->ie_irq = irq;
270 	ie->ie_cpu = NOCPU;
271 	TAILQ_INIT(&ie->ie_handlers);
272 	mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
273 
274 	va_start(ap, fmt);
275 	vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
276 	va_end(ap);
277 	strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
278 	mtx_lock(&event_lock);
279 	TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
280 	mtx_unlock(&event_lock);
281 	if (event != NULL)
282 		*event = ie;
283 	CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
284 	return (0);
285 }
286 
287 /*
288  * Bind an interrupt event to the specified CPU.  Note that not all
289  * platforms support binding an interrupt to a CPU.  For those
290  * platforms this request will fail.  For supported platforms, any
291  * associated ithreads as well as the primary interrupt context will
292  * be bound to the specificed CPU.  Using a cpu id of NOCPU unbinds
293  * the interrupt event.
294  */
295 int
296 intr_event_bind(struct intr_event *ie, int cpu)
297 {
298 	lwpid_t id;
299 	int error;
300 
301 	/* Need a CPU to bind to. */
302 	if (cpu != NOCPU && CPU_ABSENT(cpu))
303 		return (EINVAL);
304 
305 	if (ie->ie_assign_cpu == NULL)
306 		return (EOPNOTSUPP);
307 
308 	error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
309 	if (error)
310 		return (error);
311 
312 	/*
313 	 * If we have any ithreads try to set their mask first to verify
314 	 * permissions, etc.
315 	 */
316 	mtx_lock(&ie->ie_lock);
317 	if (ie->ie_thread != NULL) {
318 		id = ie->ie_thread->it_thread->td_tid;
319 		mtx_unlock(&ie->ie_lock);
320 		error = cpuset_setithread(id, cpu);
321 		if (error)
322 			return (error);
323 	} else
324 		mtx_unlock(&ie->ie_lock);
325 	error = ie->ie_assign_cpu(ie->ie_source, cpu);
326 	if (error) {
327 		mtx_lock(&ie->ie_lock);
328 		if (ie->ie_thread != NULL) {
329 			cpu = ie->ie_cpu;
330 			id = ie->ie_thread->it_thread->td_tid;
331 			mtx_unlock(&ie->ie_lock);
332 			(void)cpuset_setithread(id, cpu);
333 		} else
334 			mtx_unlock(&ie->ie_lock);
335 		return (error);
336 	}
337 
338 	mtx_lock(&ie->ie_lock);
339 	ie->ie_cpu = cpu;
340 	mtx_unlock(&ie->ie_lock);
341 
342 	return (error);
343 }
344 
345 static struct intr_event *
346 intr_lookup(int irq)
347 {
348 	struct intr_event *ie;
349 
350 	mtx_lock(&event_lock);
351 	TAILQ_FOREACH(ie, &event_list, ie_list)
352 		if (ie->ie_irq == irq &&
353 		    (ie->ie_flags & IE_SOFT) == 0 &&
354 		    TAILQ_FIRST(&ie->ie_handlers) != NULL)
355 			break;
356 	mtx_unlock(&event_lock);
357 	return (ie);
358 }
359 
360 int
361 intr_setaffinity(int irq, void *m)
362 {
363 	struct intr_event *ie;
364 	cpuset_t *mask;
365 	u_char cpu;
366 	int n;
367 
368 	mask = m;
369 	cpu = NOCPU;
370 	/*
371 	 * If we're setting all cpus we can unbind.  Otherwise make sure
372 	 * only one cpu is in the set.
373 	 */
374 	if (CPU_CMP(cpuset_root, mask)) {
375 		for (n = 0; n < CPU_SETSIZE; n++) {
376 			if (!CPU_ISSET(n, mask))
377 				continue;
378 			if (cpu != NOCPU)
379 				return (EINVAL);
380 			cpu = (u_char)n;
381 		}
382 	}
383 	ie = intr_lookup(irq);
384 	if (ie == NULL)
385 		return (ESRCH);
386 	return (intr_event_bind(ie, cpu));
387 }
388 
389 int
390 intr_getaffinity(int irq, void *m)
391 {
392 	struct intr_event *ie;
393 	cpuset_t *mask;
394 
395 	mask = m;
396 	ie = intr_lookup(irq);
397 	if (ie == NULL)
398 		return (ESRCH);
399 	CPU_ZERO(mask);
400 	mtx_lock(&ie->ie_lock);
401 	if (ie->ie_cpu == NOCPU)
402 		CPU_COPY(cpuset_root, mask);
403 	else
404 		CPU_SET(ie->ie_cpu, mask);
405 	mtx_unlock(&ie->ie_lock);
406 	return (0);
407 }
408 
409 int
410 intr_event_destroy(struct intr_event *ie)
411 {
412 
413 	mtx_lock(&event_lock);
414 	mtx_lock(&ie->ie_lock);
415 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
416 		mtx_unlock(&ie->ie_lock);
417 		mtx_unlock(&event_lock);
418 		return (EBUSY);
419 	}
420 	TAILQ_REMOVE(&event_list, ie, ie_list);
421 #ifndef notyet
422 	if (ie->ie_thread != NULL) {
423 		ithread_destroy(ie->ie_thread);
424 		ie->ie_thread = NULL;
425 	}
426 #endif
427 	mtx_unlock(&ie->ie_lock);
428 	mtx_unlock(&event_lock);
429 	mtx_destroy(&ie->ie_lock);
430 	free(ie, M_ITHREAD);
431 	return (0);
432 }
433 
434 #ifndef INTR_FILTER
435 static struct intr_thread *
436 ithread_create(const char *name)
437 {
438 	struct intr_thread *ithd;
439 	struct thread *td;
440 	int error;
441 
442 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
443 
444 	error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
445 		    &td, RFSTOPPED | RFHIGHPID,
446 	    	    0, "intr", "%s", name);
447 	if (error)
448 		panic("kproc_create() failed with %d", error);
449 	thread_lock(td);
450 	sched_class(td, PRI_ITHD);
451 	TD_SET_IWAIT(td);
452 	thread_unlock(td);
453 	td->td_pflags |= TDP_ITHREAD;
454 	ithd->it_thread = td;
455 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
456 	return (ithd);
457 }
458 #else
459 static struct intr_thread *
460 ithread_create(const char *name, struct intr_handler *ih)
461 {
462 	struct intr_thread *ithd;
463 	struct thread *td;
464 	int error;
465 
466 	ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
467 
468 	error = kproc_kthread_add(ithread_loop, ih, &intrproc,
469 		    &td, RFSTOPPED | RFHIGHPID,
470 	    	    0, "intr", "%s", name);
471 	if (error)
472 		panic("kproc_create() failed with %d", error);
473 	thread_lock(td);
474 	sched_class(td, PRI_ITHD);
475 	TD_SET_IWAIT(td);
476 	thread_unlock(td);
477 	td->td_pflags |= TDP_ITHREAD;
478 	ithd->it_thread = td;
479 	CTR2(KTR_INTR, "%s: created %s", __func__, name);
480 	return (ithd);
481 }
482 #endif
483 
484 static void
485 ithread_destroy(struct intr_thread *ithread)
486 {
487 	struct thread *td;
488 
489 	CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
490 	td = ithread->it_thread;
491 	thread_lock(td);
492 	ithread->it_flags |= IT_DEAD;
493 	if (TD_AWAITING_INTR(td)) {
494 		TD_CLR_IWAIT(td);
495 		sched_add(td, SRQ_INTR);
496 	}
497 	thread_unlock(td);
498 }
499 
500 #ifndef INTR_FILTER
501 int
502 intr_event_add_handler(struct intr_event *ie, const char *name,
503     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
504     enum intr_type flags, void **cookiep)
505 {
506 	struct intr_handler *ih, *temp_ih;
507 	struct intr_thread *it;
508 
509 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
510 		return (EINVAL);
511 
512 	/* Allocate and populate an interrupt handler structure. */
513 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
514 	ih->ih_filter = filter;
515 	ih->ih_handler = handler;
516 	ih->ih_argument = arg;
517 	strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
518 	ih->ih_event = ie;
519 	ih->ih_pri = pri;
520 	if (flags & INTR_EXCL)
521 		ih->ih_flags = IH_EXCLUSIVE;
522 	if (flags & INTR_MPSAFE)
523 		ih->ih_flags |= IH_MPSAFE;
524 	if (flags & INTR_ENTROPY)
525 		ih->ih_flags |= IH_ENTROPY;
526 
527 	/* We can only have one exclusive handler in a event. */
528 	mtx_lock(&ie->ie_lock);
529 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
530 		if ((flags & INTR_EXCL) ||
531 		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
532 			mtx_unlock(&ie->ie_lock);
533 			free(ih, M_ITHREAD);
534 			return (EINVAL);
535 		}
536 	}
537 
538 	/* Create a thread if we need one. */
539 	while (ie->ie_thread == NULL && handler != NULL) {
540 		if (ie->ie_flags & IE_ADDING_THREAD)
541 			msleep(ie, &ie->ie_lock, 0, "ithread", 0);
542 		else {
543 			ie->ie_flags |= IE_ADDING_THREAD;
544 			mtx_unlock(&ie->ie_lock);
545 			it = ithread_create("intr: newborn");
546 			mtx_lock(&ie->ie_lock);
547 			ie->ie_flags &= ~IE_ADDING_THREAD;
548 			ie->ie_thread = it;
549 			it->it_event = ie;
550 			ithread_update(it);
551 			wakeup(ie);
552 		}
553 	}
554 
555 	/* Add the new handler to the event in priority order. */
556 	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
557 		if (temp_ih->ih_pri > ih->ih_pri)
558 			break;
559 	}
560 	if (temp_ih == NULL)
561 		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
562 	else
563 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
564 	intr_event_update(ie);
565 
566 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
567 	    ie->ie_name);
568 	mtx_unlock(&ie->ie_lock);
569 
570 	if (cookiep != NULL)
571 		*cookiep = ih;
572 	return (0);
573 }
574 #else
575 int
576 intr_event_add_handler(struct intr_event *ie, const char *name,
577     driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
578     enum intr_type flags, void **cookiep)
579 {
580 	struct intr_handler *ih, *temp_ih;
581 	struct intr_thread *it;
582 
583 	if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
584 		return (EINVAL);
585 
586 	/* Allocate and populate an interrupt handler structure. */
587 	ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
588 	ih->ih_filter = filter;
589 	ih->ih_handler = handler;
590 	ih->ih_argument = arg;
591 	strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
592 	ih->ih_event = ie;
593 	ih->ih_pri = pri;
594 	if (flags & INTR_EXCL)
595 		ih->ih_flags = IH_EXCLUSIVE;
596 	if (flags & INTR_MPSAFE)
597 		ih->ih_flags |= IH_MPSAFE;
598 	if (flags & INTR_ENTROPY)
599 		ih->ih_flags |= IH_ENTROPY;
600 
601 	/* We can only have one exclusive handler in a event. */
602 	mtx_lock(&ie->ie_lock);
603 	if (!TAILQ_EMPTY(&ie->ie_handlers)) {
604 		if ((flags & INTR_EXCL) ||
605 		    (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
606 			mtx_unlock(&ie->ie_lock);
607 			free(ih, M_ITHREAD);
608 			return (EINVAL);
609 		}
610 	}
611 
612 	/* For filtered handlers, create a private ithread to run on. */
613 	if (filter != NULL && handler != NULL) {
614 		mtx_unlock(&ie->ie_lock);
615 		it = ithread_create("intr: newborn", ih);
616 		mtx_lock(&ie->ie_lock);
617 		it->it_event = ie;
618 		ih->ih_thread = it;
619 		ithread_update(it); /* XXX - do we really need this?!?!? */
620 	} else { /* Create the global per-event thread if we need one. */
621 		while (ie->ie_thread == NULL && handler != NULL) {
622 			if (ie->ie_flags & IE_ADDING_THREAD)
623 				msleep(ie, &ie->ie_lock, 0, "ithread", 0);
624 			else {
625 				ie->ie_flags |= IE_ADDING_THREAD;
626 				mtx_unlock(&ie->ie_lock);
627 				it = ithread_create("intr: newborn", ih);
628 				mtx_lock(&ie->ie_lock);
629 				ie->ie_flags &= ~IE_ADDING_THREAD;
630 				ie->ie_thread = it;
631 				it->it_event = ie;
632 				ithread_update(it);
633 				wakeup(ie);
634 			}
635 		}
636 	}
637 
638 	/* Add the new handler to the event in priority order. */
639 	TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
640 		if (temp_ih->ih_pri > ih->ih_pri)
641 			break;
642 	}
643 	if (temp_ih == NULL)
644 		TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
645 	else
646 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
647 	intr_event_update(ie);
648 
649 	CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
650 	    ie->ie_name);
651 	mtx_unlock(&ie->ie_lock);
652 
653 	if (cookiep != NULL)
654 		*cookiep = ih;
655 	return (0);
656 }
657 #endif
658 
659 /*
660  * Append a description preceded by a ':' to the name of the specified
661  * interrupt handler.
662  */
663 int
664 intr_event_describe_handler(struct intr_event *ie, void *cookie,
665     const char *descr)
666 {
667 	struct intr_handler *ih;
668 	size_t space;
669 	char *start;
670 
671 	mtx_lock(&ie->ie_lock);
672 #ifdef INVARIANTS
673 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
674 		if (ih == cookie)
675 			break;
676 	}
677 	if (ih == NULL) {
678 		mtx_unlock(&ie->ie_lock);
679 		panic("handler %p not found in interrupt event %p", cookie, ie);
680 	}
681 #endif
682 	ih = cookie;
683 
684 	/*
685 	 * Look for an existing description by checking for an
686 	 * existing ":".  This assumes device names do not include
687 	 * colons.  If one is found, prepare to insert the new
688 	 * description at that point.  If one is not found, find the
689 	 * end of the name to use as the insertion point.
690 	 */
691 	start = strchr(ih->ih_name, ':');
692 	if (start == NULL)
693 		start = strchr(ih->ih_name, 0);
694 
695 	/*
696 	 * See if there is enough remaining room in the string for the
697 	 * description + ":".  The "- 1" leaves room for the trailing
698 	 * '\0'.  The "+ 1" accounts for the colon.
699 	 */
700 	space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
701 	if (strlen(descr) + 1 > space) {
702 		mtx_unlock(&ie->ie_lock);
703 		return (ENOSPC);
704 	}
705 
706 	/* Append a colon followed by the description. */
707 	*start = ':';
708 	strcpy(start + 1, descr);
709 	intr_event_update(ie);
710 	mtx_unlock(&ie->ie_lock);
711 	return (0);
712 }
713 
714 /*
715  * Return the ie_source field from the intr_event an intr_handler is
716  * associated with.
717  */
718 void *
719 intr_handler_source(void *cookie)
720 {
721 	struct intr_handler *ih;
722 	struct intr_event *ie;
723 
724 	ih = (struct intr_handler *)cookie;
725 	if (ih == NULL)
726 		return (NULL);
727 	ie = ih->ih_event;
728 	KASSERT(ie != NULL,
729 	    ("interrupt handler \"%s\" has a NULL interrupt event",
730 	    ih->ih_name));
731 	return (ie->ie_source);
732 }
733 
734 /*
735  * Sleep until an ithread finishes executing an interrupt handler.
736  *
737  * XXX Doesn't currently handle interrupt filters or fast interrupt
738  * handlers.  This is intended for compatibility with linux drivers
739  * only.  Do not use in BSD code.
740  */
741 void
742 _intr_drain(int irq)
743 {
744 	struct intr_event *ie;
745 	struct intr_thread *ithd;
746 	struct thread *td;
747 
748 	ie = intr_lookup(irq);
749 	if (ie == NULL)
750 		return;
751 	if (ie->ie_thread == NULL)
752 		return;
753 	ithd = ie->ie_thread;
754 	td = ithd->it_thread;
755 	/*
756 	 * We set the flag and wait for it to be cleared to avoid
757 	 * long delays with potentially busy interrupt handlers
758 	 * were we to only sample TD_AWAITING_INTR() every tick.
759 	 */
760 	thread_lock(td);
761 	if (!TD_AWAITING_INTR(td)) {
762 		ithd->it_flags |= IT_WAIT;
763 		while (ithd->it_flags & IT_WAIT) {
764 			thread_unlock(td);
765 			pause("idrain", 1);
766 			thread_lock(td);
767 		}
768 	}
769 	thread_unlock(td);
770 	return;
771 }
772 
773 
774 #ifndef INTR_FILTER
775 int
776 intr_event_remove_handler(void *cookie)
777 {
778 	struct intr_handler *handler = (struct intr_handler *)cookie;
779 	struct intr_event *ie;
780 #ifdef INVARIANTS
781 	struct intr_handler *ih;
782 #endif
783 #ifdef notyet
784 	int dead;
785 #endif
786 
787 	if (handler == NULL)
788 		return (EINVAL);
789 	ie = handler->ih_event;
790 	KASSERT(ie != NULL,
791 	    ("interrupt handler \"%s\" has a NULL interrupt event",
792 	    handler->ih_name));
793 	mtx_lock(&ie->ie_lock);
794 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
795 	    ie->ie_name);
796 #ifdef INVARIANTS
797 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
798 		if (ih == handler)
799 			goto ok;
800 	mtx_unlock(&ie->ie_lock);
801 	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
802 	    ih->ih_name, ie->ie_name);
803 ok:
804 #endif
805 	/*
806 	 * If there is no ithread, then just remove the handler and return.
807 	 * XXX: Note that an INTR_FAST handler might be running on another
808 	 * CPU!
809 	 */
810 	if (ie->ie_thread == NULL) {
811 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
812 		mtx_unlock(&ie->ie_lock);
813 		free(handler, M_ITHREAD);
814 		return (0);
815 	}
816 
817 	/*
818 	 * If the interrupt thread is already running, then just mark this
819 	 * handler as being dead and let the ithread do the actual removal.
820 	 *
821 	 * During a cold boot while cold is set, msleep() does not sleep,
822 	 * so we have to remove the handler here rather than letting the
823 	 * thread do it.
824 	 */
825 	thread_lock(ie->ie_thread->it_thread);
826 	if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
827 		handler->ih_flags |= IH_DEAD;
828 
829 		/*
830 		 * Ensure that the thread will process the handler list
831 		 * again and remove this handler if it has already passed
832 		 * it on the list.
833 		 */
834 		atomic_store_rel_int(&ie->ie_thread->it_need, 1);
835 	} else
836 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
837 	thread_unlock(ie->ie_thread->it_thread);
838 	while (handler->ih_flags & IH_DEAD)
839 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
840 	intr_event_update(ie);
841 #ifdef notyet
842 	/*
843 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
844 	 * this could lead to races of stale data when servicing an
845 	 * interrupt.
846 	 */
847 	dead = 1;
848 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
849 		if (!(ih->ih_flags & IH_FAST)) {
850 			dead = 0;
851 			break;
852 		}
853 	}
854 	if (dead) {
855 		ithread_destroy(ie->ie_thread);
856 		ie->ie_thread = NULL;
857 	}
858 #endif
859 	mtx_unlock(&ie->ie_lock);
860 	free(handler, M_ITHREAD);
861 	return (0);
862 }
863 
864 static int
865 intr_event_schedule_thread(struct intr_event *ie)
866 {
867 	struct intr_entropy entropy;
868 	struct intr_thread *it;
869 	struct thread *td;
870 	struct thread *ctd;
871 	struct proc *p;
872 
873 	/*
874 	 * If no ithread or no handlers, then we have a stray interrupt.
875 	 */
876 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
877 	    ie->ie_thread == NULL)
878 		return (EINVAL);
879 
880 	ctd = curthread;
881 	it = ie->ie_thread;
882 	td = it->it_thread;
883 	p = td->td_proc;
884 
885 	/*
886 	 * If any of the handlers for this ithread claim to be good
887 	 * sources of entropy, then gather some.
888 	 */
889 	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
890 		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
891 		    p->p_pid, td->td_name);
892 		entropy.event = (uintptr_t)ie;
893 		entropy.td = ctd;
894 		random_harvest(&entropy, sizeof(entropy), 2,
895 		    RANDOM_INTERRUPT);
896 	}
897 
898 	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
899 
900 	/*
901 	 * Set it_need to tell the thread to keep running if it is already
902 	 * running.  Then, lock the thread and see if we actually need to
903 	 * put it on the runqueue.
904 	 */
905 	atomic_store_rel_int(&it->it_need, 1);
906 	thread_lock(td);
907 	if (TD_AWAITING_INTR(td)) {
908 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
909 		    td->td_name);
910 		TD_CLR_IWAIT(td);
911 		sched_add(td, SRQ_INTR);
912 	} else {
913 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
914 		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
915 	}
916 	thread_unlock(td);
917 
918 	return (0);
919 }
920 #else
921 int
922 intr_event_remove_handler(void *cookie)
923 {
924 	struct intr_handler *handler = (struct intr_handler *)cookie;
925 	struct intr_event *ie;
926 	struct intr_thread *it;
927 #ifdef INVARIANTS
928 	struct intr_handler *ih;
929 #endif
930 #ifdef notyet
931 	int dead;
932 #endif
933 
934 	if (handler == NULL)
935 		return (EINVAL);
936 	ie = handler->ih_event;
937 	KASSERT(ie != NULL,
938 	    ("interrupt handler \"%s\" has a NULL interrupt event",
939 	    handler->ih_name));
940 	mtx_lock(&ie->ie_lock);
941 	CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
942 	    ie->ie_name);
943 #ifdef INVARIANTS
944 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
945 		if (ih == handler)
946 			goto ok;
947 	mtx_unlock(&ie->ie_lock);
948 	panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
949 	    ih->ih_name, ie->ie_name);
950 ok:
951 #endif
952 	/*
953 	 * If there are no ithreads (per event and per handler), then
954 	 * just remove the handler and return.
955 	 * XXX: Note that an INTR_FAST handler might be running on another CPU!
956 	 */
957 	if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
958 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
959 		mtx_unlock(&ie->ie_lock);
960 		free(handler, M_ITHREAD);
961 		return (0);
962 	}
963 
964 	/* Private or global ithread? */
965 	it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
966 	/*
967 	 * If the interrupt thread is already running, then just mark this
968 	 * handler as being dead and let the ithread do the actual removal.
969 	 *
970 	 * During a cold boot while cold is set, msleep() does not sleep,
971 	 * so we have to remove the handler here rather than letting the
972 	 * thread do it.
973 	 */
974 	thread_lock(it->it_thread);
975 	if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
976 		handler->ih_flags |= IH_DEAD;
977 
978 		/*
979 		 * Ensure that the thread will process the handler list
980 		 * again and remove this handler if it has already passed
981 		 * it on the list.
982 		 */
983 		atomic_store_rel_int(&it->it_need, 1);
984 	} else
985 		TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
986 	thread_unlock(it->it_thread);
987 	while (handler->ih_flags & IH_DEAD)
988 		msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
989 	/*
990 	 * At this point, the handler has been disconnected from the event,
991 	 * so we can kill the private ithread if any.
992 	 */
993 	if (handler->ih_thread) {
994 		ithread_destroy(handler->ih_thread);
995 		handler->ih_thread = NULL;
996 	}
997 	intr_event_update(ie);
998 #ifdef notyet
999 	/*
1000 	 * XXX: This could be bad in the case of ppbus(8).  Also, I think
1001 	 * this could lead to races of stale data when servicing an
1002 	 * interrupt.
1003 	 */
1004 	dead = 1;
1005 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1006 		if (handler != NULL) {
1007 			dead = 0;
1008 			break;
1009 		}
1010 	}
1011 	if (dead) {
1012 		ithread_destroy(ie->ie_thread);
1013 		ie->ie_thread = NULL;
1014 	}
1015 #endif
1016 	mtx_unlock(&ie->ie_lock);
1017 	free(handler, M_ITHREAD);
1018 	return (0);
1019 }
1020 
1021 static int
1022 intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
1023 {
1024 	struct intr_entropy entropy;
1025 	struct thread *td;
1026 	struct thread *ctd;
1027 	struct proc *p;
1028 
1029 	/*
1030 	 * If no ithread or no handlers, then we have a stray interrupt.
1031 	 */
1032 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
1033 		return (EINVAL);
1034 
1035 	ctd = curthread;
1036 	td = it->it_thread;
1037 	p = td->td_proc;
1038 
1039 	/*
1040 	 * If any of the handlers for this ithread claim to be good
1041 	 * sources of entropy, then gather some.
1042 	 */
1043 	if (harvest.interrupt && ie->ie_flags & IE_ENTROPY) {
1044 		CTR3(KTR_INTR, "%s: pid %d (%s) gathering entropy", __func__,
1045 		    p->p_pid, td->td_name);
1046 		entropy.event = (uintptr_t)ie;
1047 		entropy.td = ctd;
1048 		random_harvest(&entropy, sizeof(entropy), 2,
1049 		    RANDOM_INTERRUPT);
1050 	}
1051 
1052 	KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
1053 
1054 	/*
1055 	 * Set it_need to tell the thread to keep running if it is already
1056 	 * running.  Then, lock the thread and see if we actually need to
1057 	 * put it on the runqueue.
1058 	 */
1059 	atomic_store_rel_int(&it->it_need, 1);
1060 	thread_lock(td);
1061 	if (TD_AWAITING_INTR(td)) {
1062 		CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
1063 		    td->td_name);
1064 		TD_CLR_IWAIT(td);
1065 		sched_add(td, SRQ_INTR);
1066 	} else {
1067 		CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
1068 		    __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
1069 	}
1070 	thread_unlock(td);
1071 
1072 	return (0);
1073 }
1074 #endif
1075 
1076 /*
1077  * Allow interrupt event binding for software interrupt handlers -- a no-op,
1078  * since interrupts are generated in software rather than being directed by
1079  * a PIC.
1080  */
1081 static int
1082 swi_assign_cpu(void *arg, int cpu)
1083 {
1084 
1085 	return (0);
1086 }
1087 
1088 /*
1089  * Add a software interrupt handler to a specified event.  If a given event
1090  * is not specified, then a new event is created.
1091  */
1092 int
1093 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1094 	    void *arg, int pri, enum intr_type flags, void **cookiep)
1095 {
1096 	struct intr_event *ie;
1097 	int error;
1098 
1099 	if (flags & INTR_ENTROPY)
1100 		return (EINVAL);
1101 
1102 	ie = (eventp != NULL) ? *eventp : NULL;
1103 
1104 	if (ie != NULL) {
1105 		if (!(ie->ie_flags & IE_SOFT))
1106 			return (EINVAL);
1107 	} else {
1108 		error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1109 		    NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1110 		if (error)
1111 			return (error);
1112 		if (eventp != NULL)
1113 			*eventp = ie;
1114 	}
1115 	error = intr_event_add_handler(ie, name, NULL, handler, arg,
1116 	    PI_SWI(pri), flags, cookiep);
1117 	return (error);
1118 }
1119 
1120 /*
1121  * Schedule a software interrupt thread.
1122  */
1123 void
1124 swi_sched(void *cookie, int flags)
1125 {
1126 	struct intr_handler *ih = (struct intr_handler *)cookie;
1127 	struct intr_event *ie = ih->ih_event;
1128 	struct intr_entropy entropy;
1129 	int error;
1130 
1131 	CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1132 	    ih->ih_need);
1133 
1134 	if (harvest.swi) {
1135 		CTR2(KTR_INTR, "swi_sched: pid %d (%s) gathering entropy",
1136 		    curproc->p_pid, curthread->td_name);
1137 		entropy.event = (uintptr_t)ih;
1138 		entropy.td = curthread;
1139 		random_harvest(&entropy, sizeof(entropy), 1,
1140 		    RANDOM_SWI);
1141 	}
1142 
1143 	/*
1144 	 * Set ih_need for this handler so that if the ithread is already
1145 	 * running it will execute this handler on the next pass.  Otherwise,
1146 	 * it will execute it the next time it runs.
1147 	 */
1148 	atomic_store_rel_int(&ih->ih_need, 1);
1149 
1150 	if (!(flags & SWI_DELAY)) {
1151 		PCPU_INC(cnt.v_soft);
1152 #ifdef INTR_FILTER
1153 		error = intr_event_schedule_thread(ie, ie->ie_thread);
1154 #else
1155 		error = intr_event_schedule_thread(ie);
1156 #endif
1157 		KASSERT(error == 0, ("stray software interrupt"));
1158 	}
1159 }
1160 
1161 /*
1162  * Remove a software interrupt handler.  Currently this code does not
1163  * remove the associated interrupt event if it becomes empty.  Calling code
1164  * may do so manually via intr_event_destroy(), but that's not really
1165  * an optimal interface.
1166  */
1167 int
1168 swi_remove(void *cookie)
1169 {
1170 
1171 	return (intr_event_remove_handler(cookie));
1172 }
1173 
1174 #ifdef INTR_FILTER
1175 static void
1176 priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1177 {
1178 	struct intr_event *ie;
1179 
1180 	ie = ih->ih_event;
1181 	/*
1182 	 * If this handler is marked for death, remove it from
1183 	 * the list of handlers and wake up the sleeper.
1184 	 */
1185 	if (ih->ih_flags & IH_DEAD) {
1186 		mtx_lock(&ie->ie_lock);
1187 		TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1188 		ih->ih_flags &= ~IH_DEAD;
1189 		wakeup(ih);
1190 		mtx_unlock(&ie->ie_lock);
1191 		return;
1192 	}
1193 
1194 	/* Execute this handler. */
1195 	CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1196 	     __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1197 	     ih->ih_name, ih->ih_flags);
1198 
1199 	if (!(ih->ih_flags & IH_MPSAFE))
1200 		mtx_lock(&Giant);
1201 	ih->ih_handler(ih->ih_argument);
1202 	if (!(ih->ih_flags & IH_MPSAFE))
1203 		mtx_unlock(&Giant);
1204 }
1205 #endif
1206 
1207 /*
1208  * This is a public function for use by drivers that mux interrupt
1209  * handlers for child devices from their interrupt handler.
1210  */
1211 void
1212 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1213 {
1214 	struct intr_handler *ih, *ihn;
1215 
1216 	TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1217 		/*
1218 		 * If this handler is marked for death, remove it from
1219 		 * the list of handlers and wake up the sleeper.
1220 		 */
1221 		if (ih->ih_flags & IH_DEAD) {
1222 			mtx_lock(&ie->ie_lock);
1223 			TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1224 			ih->ih_flags &= ~IH_DEAD;
1225 			wakeup(ih);
1226 			mtx_unlock(&ie->ie_lock);
1227 			continue;
1228 		}
1229 
1230 		/* Skip filter only handlers */
1231 		if (ih->ih_handler == NULL)
1232 			continue;
1233 
1234 		/*
1235 		 * For software interrupt threads, we only execute
1236 		 * handlers that have their need flag set.  Hardware
1237 		 * interrupt threads always invoke all of their handlers.
1238 		 */
1239 		if (ie->ie_flags & IE_SOFT) {
1240 			if (atomic_load_acq_int(&ih->ih_need) == 0)
1241 				continue;
1242 			else
1243 				atomic_store_rel_int(&ih->ih_need, 0);
1244 		}
1245 
1246 		/* Execute this handler. */
1247 		CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1248 		    __func__, p->p_pid, (void *)ih->ih_handler,
1249 		    ih->ih_argument, ih->ih_name, ih->ih_flags);
1250 
1251 		if (!(ih->ih_flags & IH_MPSAFE))
1252 			mtx_lock(&Giant);
1253 		ih->ih_handler(ih->ih_argument);
1254 		if (!(ih->ih_flags & IH_MPSAFE))
1255 			mtx_unlock(&Giant);
1256 	}
1257 }
1258 
1259 static void
1260 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1261 {
1262 
1263 	/* Interrupt handlers should not sleep. */
1264 	if (!(ie->ie_flags & IE_SOFT))
1265 		THREAD_NO_SLEEPING();
1266 	intr_event_execute_handlers(p, ie);
1267 	if (!(ie->ie_flags & IE_SOFT))
1268 		THREAD_SLEEPING_OK();
1269 
1270 	/*
1271 	 * Interrupt storm handling:
1272 	 *
1273 	 * If this interrupt source is currently storming, then throttle
1274 	 * it to only fire the handler once  per clock tick.
1275 	 *
1276 	 * If this interrupt source is not currently storming, but the
1277 	 * number of back to back interrupts exceeds the storm threshold,
1278 	 * then enter storming mode.
1279 	 */
1280 	if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1281 	    !(ie->ie_flags & IE_SOFT)) {
1282 		/* Report the message only once every second. */
1283 		if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1284 			printf(
1285 	"interrupt storm detected on \"%s\"; throttling interrupt source\n",
1286 			    ie->ie_name);
1287 		}
1288 		pause("istorm", 1);
1289 	} else
1290 		ie->ie_count++;
1291 
1292 	/*
1293 	 * Now that all the handlers have had a chance to run, reenable
1294 	 * the interrupt source.
1295 	 */
1296 	if (ie->ie_post_ithread != NULL)
1297 		ie->ie_post_ithread(ie->ie_source);
1298 }
1299 
1300 #ifndef INTR_FILTER
1301 /*
1302  * This is the main code for interrupt threads.
1303  */
1304 static void
1305 ithread_loop(void *arg)
1306 {
1307 	struct intr_thread *ithd;
1308 	struct intr_event *ie;
1309 	struct thread *td;
1310 	struct proc *p;
1311 	int wake;
1312 
1313 	td = curthread;
1314 	p = td->td_proc;
1315 	ithd = (struct intr_thread *)arg;
1316 	KASSERT(ithd->it_thread == td,
1317 	    ("%s: ithread and proc linkage out of sync", __func__));
1318 	ie = ithd->it_event;
1319 	ie->ie_count = 0;
1320 	wake = 0;
1321 
1322 	/*
1323 	 * As long as we have interrupts outstanding, go through the
1324 	 * list of handlers, giving each one a go at it.
1325 	 */
1326 	for (;;) {
1327 		/*
1328 		 * If we are an orphaned thread, then just die.
1329 		 */
1330 		if (ithd->it_flags & IT_DEAD) {
1331 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1332 			    p->p_pid, td->td_name);
1333 			free(ithd, M_ITHREAD);
1334 			kthread_exit();
1335 		}
1336 
1337 		/*
1338 		 * Service interrupts.  If another interrupt arrives while
1339 		 * we are running, it will set it_need to note that we
1340 		 * should make another pass.
1341 		 */
1342 		while (atomic_load_acq_int(&ithd->it_need) != 0) {
1343 			/*
1344 			 * This might need a full read and write barrier
1345 			 * to make sure that this write posts before any
1346 			 * of the memory or device accesses in the
1347 			 * handlers.
1348 			 */
1349 			atomic_store_rel_int(&ithd->it_need, 0);
1350 			ithread_execute_handlers(p, ie);
1351 		}
1352 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1353 		mtx_assert(&Giant, MA_NOTOWNED);
1354 
1355 		/*
1356 		 * Processed all our interrupts.  Now get the sched
1357 		 * lock.  This may take a while and it_need may get
1358 		 * set again, so we have to check it again.
1359 		 */
1360 		thread_lock(td);
1361 		if ((atomic_load_acq_int(&ithd->it_need) == 0) &&
1362 		    !(ithd->it_flags & (IT_DEAD | IT_WAIT))) {
1363 			TD_SET_IWAIT(td);
1364 			ie->ie_count = 0;
1365 			mi_switch(SW_VOL | SWT_IWAIT, NULL);
1366 		}
1367 		if (ithd->it_flags & IT_WAIT) {
1368 			wake = 1;
1369 			ithd->it_flags &= ~IT_WAIT;
1370 		}
1371 		thread_unlock(td);
1372 		if (wake) {
1373 			wakeup(ithd);
1374 			wake = 0;
1375 		}
1376 	}
1377 }
1378 
1379 /*
1380  * Main interrupt handling body.
1381  *
1382  * Input:
1383  * o ie:                        the event connected to this interrupt.
1384  * o frame:                     some archs (i.e. i386) pass a frame to some.
1385  *                              handlers as their main argument.
1386  * Return value:
1387  * o 0:                         everything ok.
1388  * o EINVAL:                    stray interrupt.
1389  */
1390 int
1391 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1392 {
1393 	struct intr_handler *ih;
1394 	struct trapframe *oldframe;
1395 	struct thread *td;
1396 	int error, ret, thread;
1397 
1398 	td = curthread;
1399 
1400 #ifdef KSTACK_USAGE_PROF
1401 	intr_prof_stack_use(td, frame);
1402 #endif
1403 
1404 	/* An interrupt with no event or handlers is a stray interrupt. */
1405 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1406 		return (EINVAL);
1407 
1408 	/*
1409 	 * Execute fast interrupt handlers directly.
1410 	 * To support clock handlers, if a handler registers
1411 	 * with a NULL argument, then we pass it a pointer to
1412 	 * a trapframe as its argument.
1413 	 */
1414 	td->td_intr_nesting_level++;
1415 	thread = 0;
1416 	ret = 0;
1417 	critical_enter();
1418 	oldframe = td->td_intr_frame;
1419 	td->td_intr_frame = frame;
1420 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1421 		if (ih->ih_filter == NULL) {
1422 			thread = 1;
1423 			continue;
1424 		}
1425 		CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1426 		    ih->ih_filter, ih->ih_argument == NULL ? frame :
1427 		    ih->ih_argument, ih->ih_name);
1428 		if (ih->ih_argument == NULL)
1429 			ret = ih->ih_filter(frame);
1430 		else
1431 			ret = ih->ih_filter(ih->ih_argument);
1432 		KASSERT(ret == FILTER_STRAY ||
1433 		    ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1434 		    (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1435 		    ("%s: incorrect return value %#x from %s", __func__, ret,
1436 		    ih->ih_name));
1437 
1438 		/*
1439 		 * Wrapper handler special handling:
1440 		 *
1441 		 * in some particular cases (like pccard and pccbb),
1442 		 * the _real_ device handler is wrapped in a couple of
1443 		 * functions - a filter wrapper and an ithread wrapper.
1444 		 * In this case (and just in this case), the filter wrapper
1445 		 * could ask the system to schedule the ithread and mask
1446 		 * the interrupt source if the wrapped handler is composed
1447 		 * of just an ithread handler.
1448 		 *
1449 		 * TODO: write a generic wrapper to avoid people rolling
1450 		 * their own
1451 		 */
1452 		if (!thread) {
1453 			if (ret == FILTER_SCHEDULE_THREAD)
1454 				thread = 1;
1455 		}
1456 	}
1457 	td->td_intr_frame = oldframe;
1458 
1459 	if (thread) {
1460 		if (ie->ie_pre_ithread != NULL)
1461 			ie->ie_pre_ithread(ie->ie_source);
1462 	} else {
1463 		if (ie->ie_post_filter != NULL)
1464 			ie->ie_post_filter(ie->ie_source);
1465 	}
1466 
1467 	/* Schedule the ithread if needed. */
1468 	if (thread) {
1469 		error = intr_event_schedule_thread(ie);
1470 #ifndef XEN
1471 		KASSERT(error == 0, ("bad stray interrupt"));
1472 #else
1473 		if (error != 0)
1474 			log(LOG_WARNING, "bad stray interrupt");
1475 #endif
1476 	}
1477 	critical_exit();
1478 	td->td_intr_nesting_level--;
1479 	return (0);
1480 }
1481 #else
1482 /*
1483  * This is the main code for interrupt threads.
1484  */
1485 static void
1486 ithread_loop(void *arg)
1487 {
1488 	struct intr_thread *ithd;
1489 	struct intr_handler *ih;
1490 	struct intr_event *ie;
1491 	struct thread *td;
1492 	struct proc *p;
1493 	int priv;
1494 	int wake;
1495 
1496 	td = curthread;
1497 	p = td->td_proc;
1498 	ih = (struct intr_handler *)arg;
1499 	priv = (ih->ih_thread != NULL) ? 1 : 0;
1500 	ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1501 	KASSERT(ithd->it_thread == td,
1502 	    ("%s: ithread and proc linkage out of sync", __func__));
1503 	ie = ithd->it_event;
1504 	ie->ie_count = 0;
1505 	wake = 0;
1506 
1507 	/*
1508 	 * As long as we have interrupts outstanding, go through the
1509 	 * list of handlers, giving each one a go at it.
1510 	 */
1511 	for (;;) {
1512 		/*
1513 		 * If we are an orphaned thread, then just die.
1514 		 */
1515 		if (ithd->it_flags & IT_DEAD) {
1516 			CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1517 			    p->p_pid, td->td_name);
1518 			free(ithd, M_ITHREAD);
1519 			kthread_exit();
1520 		}
1521 
1522 		/*
1523 		 * Service interrupts.  If another interrupt arrives while
1524 		 * we are running, it will set it_need to note that we
1525 		 * should make another pass.
1526 		 */
1527 		while (atomic_load_acq_int(&ithd->it_need) != 0) {
1528 			/*
1529 			 * This might need a full read and write barrier
1530 			 * to make sure that this write posts before any
1531 			 * of the memory or device accesses in the
1532 			 * handlers.
1533 			 */
1534 			atomic_store_rel_int(&ithd->it_need, 0);
1535 			if (priv)
1536 				priv_ithread_execute_handler(p, ih);
1537 			else
1538 				ithread_execute_handlers(p, ie);
1539 		}
1540 		WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1541 		mtx_assert(&Giant, MA_NOTOWNED);
1542 
1543 		/*
1544 		 * Processed all our interrupts.  Now get the sched
1545 		 * lock.  This may take a while and it_need may get
1546 		 * set again, so we have to check it again.
1547 		 */
1548 		thread_lock(td);
1549 		if ((atomic_load_acq_int(&ithd->it_need) == 0) &&
1550 		    !(ithd->it_flags & (IT_DEAD | IT_WAIT))) {
1551 			TD_SET_IWAIT(td);
1552 			ie->ie_count = 0;
1553 			mi_switch(SW_VOL | SWT_IWAIT, NULL);
1554 		}
1555 		if (ithd->it_flags & IT_WAIT) {
1556 			wake = 1;
1557 			ithd->it_flags &= ~IT_WAIT;
1558 		}
1559 		thread_unlock(td);
1560 		if (wake) {
1561 			wakeup(ithd);
1562 			wake = 0;
1563 		}
1564 	}
1565 }
1566 
1567 /*
1568  * Main loop for interrupt filter.
1569  *
1570  * Some architectures (i386, amd64 and arm) require the optional frame
1571  * parameter, and use it as the main argument for fast handler execution
1572  * when ih_argument == NULL.
1573  *
1574  * Return value:
1575  * o FILTER_STRAY:              No filter recognized the event, and no
1576  *                              filter-less handler is registered on this
1577  *                              line.
1578  * o FILTER_HANDLED:            A filter claimed the event and served it.
1579  * o FILTER_SCHEDULE_THREAD:    No filter claimed the event, but there's at
1580  *                              least one filter-less handler on this line.
1581  * o FILTER_HANDLED |
1582  *   FILTER_SCHEDULE_THREAD:    A filter claimed the event, and asked for
1583  *                              scheduling the per-handler ithread.
1584  *
1585  * In case an ithread has to be scheduled, in *ithd there will be a
1586  * pointer to a struct intr_thread containing the thread to be
1587  * scheduled.
1588  */
1589 
1590 static int
1591 intr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1592 		 struct intr_thread **ithd)
1593 {
1594 	struct intr_handler *ih;
1595 	void *arg;
1596 	int ret, thread_only;
1597 
1598 	ret = 0;
1599 	thread_only = 0;
1600 	TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1601 		/*
1602 		 * Execute fast interrupt handlers directly.
1603 		 * To support clock handlers, if a handler registers
1604 		 * with a NULL argument, then we pass it a pointer to
1605 		 * a trapframe as its argument.
1606 		 */
1607 		arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1608 
1609 		CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1610 		     ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1611 
1612 		if (ih->ih_filter != NULL)
1613 			ret = ih->ih_filter(arg);
1614 		else {
1615 			thread_only = 1;
1616 			continue;
1617 		}
1618 		KASSERT(ret == FILTER_STRAY ||
1619 		    ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1620 		    (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1621 		    ("%s: incorrect return value %#x from %s", __func__, ret,
1622 		    ih->ih_name));
1623 		if (ret & FILTER_STRAY)
1624 			continue;
1625 		else {
1626 			*ithd = ih->ih_thread;
1627 			return (ret);
1628 		}
1629 	}
1630 
1631 	/*
1632 	 * No filters handled the interrupt and we have at least
1633 	 * one handler without a filter.  In this case, we schedule
1634 	 * all of the filter-less handlers to run in the ithread.
1635 	 */
1636 	if (thread_only) {
1637 		*ithd = ie->ie_thread;
1638 		return (FILTER_SCHEDULE_THREAD);
1639 	}
1640 	return (FILTER_STRAY);
1641 }
1642 
1643 /*
1644  * Main interrupt handling body.
1645  *
1646  * Input:
1647  * o ie:                        the event connected to this interrupt.
1648  * o frame:                     some archs (i.e. i386) pass a frame to some.
1649  *                              handlers as their main argument.
1650  * Return value:
1651  * o 0:                         everything ok.
1652  * o EINVAL:                    stray interrupt.
1653  */
1654 int
1655 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1656 {
1657 	struct intr_thread *ithd;
1658 	struct trapframe *oldframe;
1659 	struct thread *td;
1660 	int thread;
1661 
1662 	ithd = NULL;
1663 	td = curthread;
1664 
1665 	if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1666 		return (EINVAL);
1667 
1668 	td->td_intr_nesting_level++;
1669 	thread = 0;
1670 	critical_enter();
1671 	oldframe = td->td_intr_frame;
1672 	td->td_intr_frame = frame;
1673 	thread = intr_filter_loop(ie, frame, &ithd);
1674 	if (thread & FILTER_HANDLED) {
1675 		if (ie->ie_post_filter != NULL)
1676 			ie->ie_post_filter(ie->ie_source);
1677 	} else {
1678 		if (ie->ie_pre_ithread != NULL)
1679 			ie->ie_pre_ithread(ie->ie_source);
1680 	}
1681 	td->td_intr_frame = oldframe;
1682 	critical_exit();
1683 
1684 	/* Interrupt storm logic */
1685 	if (thread & FILTER_STRAY) {
1686 		ie->ie_count++;
1687 		if (ie->ie_count < intr_storm_threshold)
1688 			printf("Interrupt stray detection not present\n");
1689 	}
1690 
1691 	/* Schedule an ithread if needed. */
1692 	if (thread & FILTER_SCHEDULE_THREAD) {
1693 		if (intr_event_schedule_thread(ie, ithd) != 0)
1694 			panic("%s: impossible stray interrupt", __func__);
1695 	}
1696 	td->td_intr_nesting_level--;
1697 	return (0);
1698 }
1699 #endif
1700 
1701 #ifdef DDB
1702 /*
1703  * Dump details about an interrupt handler
1704  */
1705 static void
1706 db_dump_intrhand(struct intr_handler *ih)
1707 {
1708 	int comma;
1709 
1710 	db_printf("\t%-10s ", ih->ih_name);
1711 	switch (ih->ih_pri) {
1712 	case PI_REALTIME:
1713 		db_printf("CLK ");
1714 		break;
1715 	case PI_AV:
1716 		db_printf("AV  ");
1717 		break;
1718 	case PI_TTY:
1719 		db_printf("TTY ");
1720 		break;
1721 	case PI_NET:
1722 		db_printf("NET ");
1723 		break;
1724 	case PI_DISK:
1725 		db_printf("DISK");
1726 		break;
1727 	case PI_DULL:
1728 		db_printf("DULL");
1729 		break;
1730 	default:
1731 		if (ih->ih_pri >= PI_SOFT)
1732 			db_printf("SWI ");
1733 		else
1734 			db_printf("%4u", ih->ih_pri);
1735 		break;
1736 	}
1737 	db_printf(" ");
1738 	if (ih->ih_filter != NULL) {
1739 		db_printf("[F]");
1740 		db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1741 	}
1742 	if (ih->ih_handler != NULL) {
1743 		if (ih->ih_filter != NULL)
1744 			db_printf(",");
1745 		db_printf("[H]");
1746 		db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1747 	}
1748 	db_printf("(%p)", ih->ih_argument);
1749 	if (ih->ih_need ||
1750 	    (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1751 	    IH_MPSAFE)) != 0) {
1752 		db_printf(" {");
1753 		comma = 0;
1754 		if (ih->ih_flags & IH_EXCLUSIVE) {
1755 			if (comma)
1756 				db_printf(", ");
1757 			db_printf("EXCL");
1758 			comma = 1;
1759 		}
1760 		if (ih->ih_flags & IH_ENTROPY) {
1761 			if (comma)
1762 				db_printf(", ");
1763 			db_printf("ENTROPY");
1764 			comma = 1;
1765 		}
1766 		if (ih->ih_flags & IH_DEAD) {
1767 			if (comma)
1768 				db_printf(", ");
1769 			db_printf("DEAD");
1770 			comma = 1;
1771 		}
1772 		if (ih->ih_flags & IH_MPSAFE) {
1773 			if (comma)
1774 				db_printf(", ");
1775 			db_printf("MPSAFE");
1776 			comma = 1;
1777 		}
1778 		if (ih->ih_need) {
1779 			if (comma)
1780 				db_printf(", ");
1781 			db_printf("NEED");
1782 		}
1783 		db_printf("}");
1784 	}
1785 	db_printf("\n");
1786 }
1787 
1788 /*
1789  * Dump details about a event.
1790  */
1791 void
1792 db_dump_intr_event(struct intr_event *ie, int handlers)
1793 {
1794 	struct intr_handler *ih;
1795 	struct intr_thread *it;
1796 	int comma;
1797 
1798 	db_printf("%s ", ie->ie_fullname);
1799 	it = ie->ie_thread;
1800 	if (it != NULL)
1801 		db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1802 	else
1803 		db_printf("(no thread)");
1804 	if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1805 	    (it != NULL && it->it_need)) {
1806 		db_printf(" {");
1807 		comma = 0;
1808 		if (ie->ie_flags & IE_SOFT) {
1809 			db_printf("SOFT");
1810 			comma = 1;
1811 		}
1812 		if (ie->ie_flags & IE_ENTROPY) {
1813 			if (comma)
1814 				db_printf(", ");
1815 			db_printf("ENTROPY");
1816 			comma = 1;
1817 		}
1818 		if (ie->ie_flags & IE_ADDING_THREAD) {
1819 			if (comma)
1820 				db_printf(", ");
1821 			db_printf("ADDING_THREAD");
1822 			comma = 1;
1823 		}
1824 		if (it != NULL && it->it_need) {
1825 			if (comma)
1826 				db_printf(", ");
1827 			db_printf("NEED");
1828 		}
1829 		db_printf("}");
1830 	}
1831 	db_printf("\n");
1832 
1833 	if (handlers)
1834 		TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1835 		    db_dump_intrhand(ih);
1836 }
1837 
1838 /*
1839  * Dump data about interrupt handlers
1840  */
1841 DB_SHOW_COMMAND(intr, db_show_intr)
1842 {
1843 	struct intr_event *ie;
1844 	int all, verbose;
1845 
1846 	verbose = strchr(modif, 'v') != NULL;
1847 	all = strchr(modif, 'a') != NULL;
1848 	TAILQ_FOREACH(ie, &event_list, ie_list) {
1849 		if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1850 			continue;
1851 		db_dump_intr_event(ie, verbose);
1852 		if (db_pager_quit)
1853 			break;
1854 	}
1855 }
1856 #endif /* DDB */
1857 
1858 /*
1859  * Start standard software interrupt threads
1860  */
1861 static void
1862 start_softintr(void *dummy)
1863 {
1864 
1865 	if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1866 		panic("died while creating vm swi ithread");
1867 }
1868 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1869     NULL);
1870 
1871 /*
1872  * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1873  * The data for this machine dependent, and the declarations are in machine
1874  * dependent code.  The layout of intrnames and intrcnt however is machine
1875  * independent.
1876  *
1877  * We do not know the length of intrcnt and intrnames at compile time, so
1878  * calculate things at run time.
1879  */
1880 static int
1881 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1882 {
1883 	return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1884 }
1885 
1886 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1887     NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1888 
1889 static int
1890 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1891 {
1892 #ifdef SCTL_MASK32
1893 	uint32_t *intrcnt32;
1894 	unsigned i;
1895 	int error;
1896 
1897 	if (req->flags & SCTL_MASK32) {
1898 		if (!req->oldptr)
1899 			return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1900 		intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1901 		if (intrcnt32 == NULL)
1902 			return (ENOMEM);
1903 		for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1904 			intrcnt32[i] = intrcnt[i];
1905 		error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1906 		free(intrcnt32, M_TEMP);
1907 		return (error);
1908 	}
1909 #endif
1910 	return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1911 }
1912 
1913 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1914     NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1915 
1916 #ifdef DDB
1917 /*
1918  * DDB command to dump the interrupt statistics.
1919  */
1920 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1921 {
1922 	u_long *i;
1923 	char *cp;
1924 	u_int j;
1925 
1926 	cp = intrnames;
1927 	j = 0;
1928 	for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1929 	    i++, j++) {
1930 		if (*cp == '\0')
1931 			break;
1932 		if (*i != 0)
1933 			db_printf("%s\t%lu\n", cp, *i);
1934 		cp += strlen(cp) + 1;
1935 	}
1936 }
1937 #endif
1938