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