2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
10 * 1. Redistributions of source code must retain the above copyright
11 * notice unmodified, this list of conditions, and the following
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
33 #include "opt_kstack_usage_prof.h"
35 #include <sys/param.h>
38 #include <sys/cpuset.h>
39 #include <sys/rtprio.h>
40 #include <sys/systm.h>
41 #include <sys/interrupt.h>
42 #include <sys/kernel.h>
43 #include <sys/kthread.h>
45 #include <sys/limits.h>
47 #include <sys/malloc.h>
48 #include <sys/mutex.h>
51 #include <sys/random.h>
52 #include <sys/resourcevar.h>
53 #include <sys/sched.h>
55 #include <sys/sysctl.h>
56 #include <sys/syslog.h>
57 #include <sys/unistd.h>
58 #include <sys/vmmeter.h>
59 #include <machine/atomic.h>
60 #include <machine/cpu.h>
61 #include <machine/md_var.h>
62 #include <machine/stdarg.h>
65 #include <ddb/db_sym.h>
69 * Describe an interrupt thread. There is one of these per interrupt event.
72 struct intr_event *it_event;
73 struct thread *it_thread; /* Kernel thread. */
74 int it_flags; /* (j) IT_* flags. */
75 int it_need; /* Needs service. */
78 /* Interrupt thread flags kept in it_flags */
79 #define IT_DEAD 0x000001 /* Thread is waiting to exit. */
80 #define IT_WAIT 0x000002 /* Thread is waiting for completion. */
87 struct intr_event *clk_intr_event;
88 struct intr_event *tty_intr_event;
90 struct proc *intrproc;
92 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
94 static int intr_storm_threshold = 1000;
95 SYSCTL_INT(_hw, OID_AUTO, intr_storm_threshold, CTLFLAG_RWTUN,
96 &intr_storm_threshold, 0,
97 "Number of consecutive interrupts before storm protection is enabled");
98 static TAILQ_HEAD(, intr_event) event_list =
99 TAILQ_HEAD_INITIALIZER(event_list);
100 static struct mtx event_lock;
101 MTX_SYSINIT(intr_event_list, &event_lock, "intr event list", MTX_DEF);
103 static void intr_event_update(struct intr_event *ie);
105 static int intr_event_schedule_thread(struct intr_event *ie,
106 struct intr_thread *ithd);
107 static int intr_filter_loop(struct intr_event *ie,
108 struct trapframe *frame, struct intr_thread **ithd);
109 static struct intr_thread *ithread_create(const char *name,
110 struct intr_handler *ih);
112 static int intr_event_schedule_thread(struct intr_event *ie);
113 static struct intr_thread *ithread_create(const char *name);
115 static void ithread_destroy(struct intr_thread *ithread);
116 static void ithread_execute_handlers(struct proc *p,
117 struct intr_event *ie);
119 static void priv_ithread_execute_handler(struct proc *p,
120 struct intr_handler *ih);
122 static void ithread_loop(void *);
123 static void ithread_update(struct intr_thread *ithd);
124 static void start_softintr(void *);
126 /* Map an interrupt type to an ithread priority. */
128 intr_priority(enum intr_type flags)
132 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
133 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
154 pri = PI_DULL; /* don't care */
157 /* We didn't specify an interrupt level. */
158 panic("intr_priority: no interrupt type in flags");
165 * Update an ithread based on the associated intr_event.
168 ithread_update(struct intr_thread *ithd)
170 struct intr_event *ie;
175 td = ithd->it_thread;
177 /* Determine the overall priority of this event. */
178 if (TAILQ_EMPTY(&ie->ie_handlers))
181 pri = TAILQ_FIRST(&ie->ie_handlers)->ih_pri;
183 /* Update name and priority. */
184 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
186 sched_clear_tdname(td);
194 * Regenerate the full name of an interrupt event and update its priority.
197 intr_event_update(struct intr_event *ie)
199 struct intr_handler *ih;
203 /* Start off with no entropy and just the name of the event. */
204 mtx_assert(&ie->ie_lock, MA_OWNED);
205 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
206 ie->ie_flags &= ~IE_ENTROPY;
210 /* Run through all the handlers updating values. */
211 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
212 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
213 sizeof(ie->ie_fullname)) {
214 strcat(ie->ie_fullname, " ");
215 strcat(ie->ie_fullname, ih->ih_name);
219 if (ih->ih_flags & IH_ENTROPY)
220 ie->ie_flags |= IE_ENTROPY;
224 * If the handler names were too long, add +'s to indicate missing
225 * names. If we run out of room and still have +'s to add, change
226 * the last character from a + to a *.
228 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
229 while (missed-- > 0) {
230 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
237 strcat(ie->ie_fullname, " +");
240 strcat(ie->ie_fullname, "+");
244 * If this event has an ithread, update it's priority and
247 if (ie->ie_thread != NULL)
248 ithread_update(ie->ie_thread);
249 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
253 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
254 void (*pre_ithread)(void *), void (*post_ithread)(void *),
255 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
256 const char *fmt, ...)
258 struct intr_event *ie;
261 /* The only valid flag during creation is IE_SOFT. */
262 if ((flags & ~IE_SOFT) != 0)
264 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
265 ie->ie_source = source;
266 ie->ie_pre_ithread = pre_ithread;
267 ie->ie_post_ithread = post_ithread;
268 ie->ie_post_filter = post_filter;
269 ie->ie_assign_cpu = assign_cpu;
270 ie->ie_flags = flags;
273 TAILQ_INIT(&ie->ie_handlers);
274 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
277 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
279 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
280 mtx_lock(&event_lock);
281 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
282 mtx_unlock(&event_lock);
285 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
290 * Bind an interrupt event to the specified CPU. Note that not all
291 * platforms support binding an interrupt to a CPU. For those
292 * platforms this request will fail. Using a cpu id of NOCPU unbinds
293 * the interrupt event.
296 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
301 /* Need a CPU to bind to. */
302 if (cpu != NOCPU && CPU_ABSENT(cpu))
305 if (ie->ie_assign_cpu == NULL)
308 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
313 * If we have any ithreads try to set their mask first to verify
317 mtx_lock(&ie->ie_lock);
318 if (ie->ie_thread != NULL) {
319 id = ie->ie_thread->it_thread->td_tid;
320 mtx_unlock(&ie->ie_lock);
321 error = cpuset_setithread(id, cpu);
325 mtx_unlock(&ie->ie_lock);
328 error = ie->ie_assign_cpu(ie->ie_source, cpu);
331 mtx_lock(&ie->ie_lock);
332 if (ie->ie_thread != NULL) {
334 id = ie->ie_thread->it_thread->td_tid;
335 mtx_unlock(&ie->ie_lock);
336 (void)cpuset_setithread(id, cpu);
338 mtx_unlock(&ie->ie_lock);
344 mtx_lock(&ie->ie_lock);
346 mtx_unlock(&ie->ie_lock);
353 * Bind an interrupt event to the specified CPU. For supported platforms, any
354 * associated ithreads as well as the primary interrupt context will be bound
355 * to the specificed CPU.
358 intr_event_bind(struct intr_event *ie, int cpu)
361 return (_intr_event_bind(ie, cpu, true, true));
365 * Bind an interrupt event to the specified CPU, but do not bind associated
369 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
372 return (_intr_event_bind(ie, cpu, true, false));
376 * Bind an interrupt event's ithread to the specified CPU.
379 intr_event_bind_ithread(struct intr_event *ie, int cpu)
382 return (_intr_event_bind(ie, cpu, false, true));
385 static struct intr_event *
388 struct intr_event *ie;
390 mtx_lock(&event_lock);
391 TAILQ_FOREACH(ie, &event_list, ie_list)
392 if (ie->ie_irq == irq &&
393 (ie->ie_flags & IE_SOFT) == 0 &&
394 TAILQ_FIRST(&ie->ie_handlers) != NULL)
396 mtx_unlock(&event_lock);
401 intr_setaffinity(int irq, int mode, void *m)
403 struct intr_event *ie;
410 * If we're setting all cpus we can unbind. Otherwise make sure
411 * only one cpu is in the set.
413 if (CPU_CMP(cpuset_root, mask)) {
414 for (n = 0; n < CPU_SETSIZE; n++) {
415 if (!CPU_ISSET(n, mask))
422 ie = intr_lookup(irq);
427 return (intr_event_bind(ie, cpu));
428 case CPU_WHICH_INTRHANDLER:
429 return (intr_event_bind_irqonly(ie, cpu));
430 case CPU_WHICH_ITHREAD:
431 return (intr_event_bind_ithread(ie, cpu));
438 intr_getaffinity(int irq, int mode, void *m)
440 struct intr_event *ie;
448 ie = intr_lookup(irq);
456 case CPU_WHICH_INTRHANDLER:
457 mtx_lock(&ie->ie_lock);
458 if (ie->ie_cpu == NOCPU)
459 CPU_COPY(cpuset_root, mask);
461 CPU_SET(ie->ie_cpu, mask);
462 mtx_unlock(&ie->ie_lock);
464 case CPU_WHICH_ITHREAD:
465 mtx_lock(&ie->ie_lock);
466 if (ie->ie_thread == NULL) {
467 mtx_unlock(&ie->ie_lock);
468 CPU_COPY(cpuset_root, mask);
470 id = ie->ie_thread->it_thread->td_tid;
471 mtx_unlock(&ie->ie_lock);
472 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
475 CPU_COPY(&td->td_cpuset->cs_mask, mask);
485 intr_event_destroy(struct intr_event *ie)
488 mtx_lock(&event_lock);
489 mtx_lock(&ie->ie_lock);
490 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
491 mtx_unlock(&ie->ie_lock);
492 mtx_unlock(&event_lock);
495 TAILQ_REMOVE(&event_list, ie, ie_list);
497 if (ie->ie_thread != NULL) {
498 ithread_destroy(ie->ie_thread);
499 ie->ie_thread = NULL;
502 mtx_unlock(&ie->ie_lock);
503 mtx_unlock(&event_lock);
504 mtx_destroy(&ie->ie_lock);
510 static struct intr_thread *
511 ithread_create(const char *name)
513 struct intr_thread *ithd;
517 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
519 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
520 &td, RFSTOPPED | RFHIGHPID,
521 0, "intr", "%s", name);
523 panic("kproc_create() failed with %d", error);
525 sched_class(td, PRI_ITHD);
528 td->td_pflags |= TDP_ITHREAD;
529 ithd->it_thread = td;
530 CTR2(KTR_INTR, "%s: created %s", __func__, name);
534 static struct intr_thread *
535 ithread_create(const char *name, struct intr_handler *ih)
537 struct intr_thread *ithd;
541 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
543 error = kproc_kthread_add(ithread_loop, ih, &intrproc,
544 &td, RFSTOPPED | RFHIGHPID,
545 0, "intr", "%s", name);
547 panic("kproc_create() failed with %d", error);
549 sched_class(td, PRI_ITHD);
552 td->td_pflags |= TDP_ITHREAD;
553 ithd->it_thread = td;
554 CTR2(KTR_INTR, "%s: created %s", __func__, name);
560 ithread_destroy(struct intr_thread *ithread)
564 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
565 td = ithread->it_thread;
567 ithread->it_flags |= IT_DEAD;
568 if (TD_AWAITING_INTR(td)) {
570 sched_add(td, SRQ_INTR);
577 intr_event_add_handler(struct intr_event *ie, const char *name,
578 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
579 enum intr_type flags, void **cookiep)
581 struct intr_handler *ih, *temp_ih;
582 struct intr_thread *it;
584 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
587 /* Allocate and populate an interrupt handler structure. */
588 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
589 ih->ih_filter = filter;
590 ih->ih_handler = handler;
591 ih->ih_argument = arg;
592 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
595 if (flags & INTR_EXCL)
596 ih->ih_flags = IH_EXCLUSIVE;
597 if (flags & INTR_MPSAFE)
598 ih->ih_flags |= IH_MPSAFE;
599 if (flags & INTR_ENTROPY)
600 ih->ih_flags |= IH_ENTROPY;
602 /* We can only have one exclusive handler in a event. */
603 mtx_lock(&ie->ie_lock);
604 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
605 if ((flags & INTR_EXCL) ||
606 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
607 mtx_unlock(&ie->ie_lock);
613 /* Create a thread if we need one. */
614 while (ie->ie_thread == NULL && handler != NULL) {
615 if (ie->ie_flags & IE_ADDING_THREAD)
616 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
618 ie->ie_flags |= IE_ADDING_THREAD;
619 mtx_unlock(&ie->ie_lock);
620 it = ithread_create("intr: newborn");
621 mtx_lock(&ie->ie_lock);
622 ie->ie_flags &= ~IE_ADDING_THREAD;
630 /* Add the new handler to the event in priority order. */
631 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
632 if (temp_ih->ih_pri > ih->ih_pri)
636 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
638 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
639 intr_event_update(ie);
641 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
643 mtx_unlock(&ie->ie_lock);
651 intr_event_add_handler(struct intr_event *ie, const char *name,
652 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
653 enum intr_type flags, void **cookiep)
655 struct intr_handler *ih, *temp_ih;
656 struct intr_thread *it;
658 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
661 /* Allocate and populate an interrupt handler structure. */
662 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
663 ih->ih_filter = filter;
664 ih->ih_handler = handler;
665 ih->ih_argument = arg;
666 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
669 if (flags & INTR_EXCL)
670 ih->ih_flags = IH_EXCLUSIVE;
671 if (flags & INTR_MPSAFE)
672 ih->ih_flags |= IH_MPSAFE;
673 if (flags & INTR_ENTROPY)
674 ih->ih_flags |= IH_ENTROPY;
676 /* We can only have one exclusive handler in a event. */
677 mtx_lock(&ie->ie_lock);
678 if (!TAILQ_EMPTY(&ie->ie_handlers)) {
679 if ((flags & INTR_EXCL) ||
680 (TAILQ_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
681 mtx_unlock(&ie->ie_lock);
687 /* For filtered handlers, create a private ithread to run on. */
688 if (filter != NULL && handler != NULL) {
689 mtx_unlock(&ie->ie_lock);
690 it = ithread_create("intr: newborn", ih);
691 mtx_lock(&ie->ie_lock);
694 ithread_update(it); /* XXX - do we really need this?!?!? */
695 } else { /* Create the global per-event thread if we need one. */
696 while (ie->ie_thread == NULL && handler != NULL) {
697 if (ie->ie_flags & IE_ADDING_THREAD)
698 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
700 ie->ie_flags |= IE_ADDING_THREAD;
701 mtx_unlock(&ie->ie_lock);
702 it = ithread_create("intr: newborn", ih);
703 mtx_lock(&ie->ie_lock);
704 ie->ie_flags &= ~IE_ADDING_THREAD;
713 /* Add the new handler to the event in priority order. */
714 TAILQ_FOREACH(temp_ih, &ie->ie_handlers, ih_next) {
715 if (temp_ih->ih_pri > ih->ih_pri)
719 TAILQ_INSERT_TAIL(&ie->ie_handlers, ih, ih_next);
721 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
722 intr_event_update(ie);
724 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
726 mtx_unlock(&ie->ie_lock);
735 * Append a description preceded by a ':' to the name of the specified
739 intr_event_describe_handler(struct intr_event *ie, void *cookie,
742 struct intr_handler *ih;
746 mtx_lock(&ie->ie_lock);
748 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
753 mtx_unlock(&ie->ie_lock);
754 panic("handler %p not found in interrupt event %p", cookie, ie);
760 * Look for an existing description by checking for an
761 * existing ":". This assumes device names do not include
762 * colons. If one is found, prepare to insert the new
763 * description at that point. If one is not found, find the
764 * end of the name to use as the insertion point.
766 start = strchr(ih->ih_name, ':');
768 start = strchr(ih->ih_name, 0);
771 * See if there is enough remaining room in the string for the
772 * description + ":". The "- 1" leaves room for the trailing
773 * '\0'. The "+ 1" accounts for the colon.
775 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
776 if (strlen(descr) + 1 > space) {
777 mtx_unlock(&ie->ie_lock);
781 /* Append a colon followed by the description. */
783 strcpy(start + 1, descr);
784 intr_event_update(ie);
785 mtx_unlock(&ie->ie_lock);
790 * Return the ie_source field from the intr_event an intr_handler is
794 intr_handler_source(void *cookie)
796 struct intr_handler *ih;
797 struct intr_event *ie;
799 ih = (struct intr_handler *)cookie;
804 ("interrupt handler \"%s\" has a NULL interrupt event",
806 return (ie->ie_source);
810 * Sleep until an ithread finishes executing an interrupt handler.
812 * XXX Doesn't currently handle interrupt filters or fast interrupt
813 * handlers. This is intended for compatibility with linux drivers
814 * only. Do not use in BSD code.
819 struct intr_event *ie;
820 struct intr_thread *ithd;
823 ie = intr_lookup(irq);
826 if (ie->ie_thread == NULL)
828 ithd = ie->ie_thread;
829 td = ithd->it_thread;
831 * We set the flag and wait for it to be cleared to avoid
832 * long delays with potentially busy interrupt handlers
833 * were we to only sample TD_AWAITING_INTR() every tick.
836 if (!TD_AWAITING_INTR(td)) {
837 ithd->it_flags |= IT_WAIT;
838 while (ithd->it_flags & IT_WAIT) {
851 intr_event_remove_handler(void *cookie)
853 struct intr_handler *handler = (struct intr_handler *)cookie;
854 struct intr_event *ie;
856 struct intr_handler *ih;
864 ie = handler->ih_event;
866 ("interrupt handler \"%s\" has a NULL interrupt event",
868 mtx_lock(&ie->ie_lock);
869 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
872 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
875 mtx_unlock(&ie->ie_lock);
876 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
877 ih->ih_name, ie->ie_name);
881 * If there is no ithread, then just remove the handler and return.
882 * XXX: Note that an INTR_FAST handler might be running on another
885 if (ie->ie_thread == NULL) {
886 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
887 mtx_unlock(&ie->ie_lock);
888 free(handler, M_ITHREAD);
893 * If the interrupt thread is already running, then just mark this
894 * handler as being dead and let the ithread do the actual removal.
896 * During a cold boot while cold is set, msleep() does not sleep,
897 * so we have to remove the handler here rather than letting the
900 thread_lock(ie->ie_thread->it_thread);
901 if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
902 handler->ih_flags |= IH_DEAD;
905 * Ensure that the thread will process the handler list
906 * again and remove this handler if it has already passed
909 * The release part of the following store ensures
910 * that the update of ih_flags is ordered before the
911 * it_need setting. See the comment before
912 * atomic_cmpset_acq(&ithd->it_need, ...) operation in
913 * the ithread_execute_handlers().
915 atomic_store_rel_int(&ie->ie_thread->it_need, 1);
917 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
918 thread_unlock(ie->ie_thread->it_thread);
919 while (handler->ih_flags & IH_DEAD)
920 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
921 intr_event_update(ie);
924 * XXX: This could be bad in the case of ppbus(8). Also, I think
925 * this could lead to races of stale data when servicing an
929 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
930 if (!(ih->ih_flags & IH_FAST)) {
936 ithread_destroy(ie->ie_thread);
937 ie->ie_thread = NULL;
940 mtx_unlock(&ie->ie_lock);
941 free(handler, M_ITHREAD);
946 intr_event_schedule_thread(struct intr_event *ie)
948 struct intr_entropy entropy;
949 struct intr_thread *it;
954 * If no ithread or no handlers, then we have a stray interrupt.
956 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) ||
957 ie->ie_thread == NULL)
965 * If any of the handlers for this ithread claim to be good
966 * sources of entropy, then gather some.
968 if (ie->ie_flags & IE_ENTROPY) {
969 entropy.event = (uintptr_t)ie;
971 random_harvest_queue(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT);
974 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
977 * Set it_need to tell the thread to keep running if it is already
978 * running. Then, lock the thread and see if we actually need to
979 * put it on the runqueue.
981 * Use store_rel to arrange that the store to ih_need in
982 * swi_sched() is before the store to it_need and prepare for
983 * transfer of this order to loads in the ithread.
985 atomic_store_rel_int(&it->it_need, 1);
987 if (TD_AWAITING_INTR(td)) {
988 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
991 sched_add(td, SRQ_INTR);
993 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
994 __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
1002 intr_event_remove_handler(void *cookie)
1004 struct intr_handler *handler = (struct intr_handler *)cookie;
1005 struct intr_event *ie;
1006 struct intr_thread *it;
1008 struct intr_handler *ih;
1014 if (handler == NULL)
1016 ie = handler->ih_event;
1018 ("interrupt handler \"%s\" has a NULL interrupt event",
1020 mtx_lock(&ie->ie_lock);
1021 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
1024 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1027 mtx_unlock(&ie->ie_lock);
1028 panic("interrupt handler \"%s\" not found in interrupt event \"%s\"",
1029 ih->ih_name, ie->ie_name);
1033 * If there are no ithreads (per event and per handler), then
1034 * just remove the handler and return.
1035 * XXX: Note that an INTR_FAST handler might be running on another CPU!
1037 if (ie->ie_thread == NULL && handler->ih_thread == NULL) {
1038 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
1039 mtx_unlock(&ie->ie_lock);
1040 free(handler, M_ITHREAD);
1044 /* Private or global ithread? */
1045 it = (handler->ih_thread) ? handler->ih_thread : ie->ie_thread;
1047 * If the interrupt thread is already running, then just mark this
1048 * handler as being dead and let the ithread do the actual removal.
1050 * During a cold boot while cold is set, msleep() does not sleep,
1051 * so we have to remove the handler here rather than letting the
1054 thread_lock(it->it_thread);
1055 if (!TD_AWAITING_INTR(it->it_thread) && !cold) {
1056 handler->ih_flags |= IH_DEAD;
1059 * Ensure that the thread will process the handler list
1060 * again and remove this handler if it has already passed
1063 * The release part of the following store ensures
1064 * that the update of ih_flags is ordered before the
1065 * it_need setting. See the comment before
1066 * atomic_cmpset_acq(&ithd->it_need, ...) operation in
1067 * the ithread_execute_handlers().
1069 atomic_store_rel_int(&it->it_need, 1);
1071 TAILQ_REMOVE(&ie->ie_handlers, handler, ih_next);
1072 thread_unlock(it->it_thread);
1073 while (handler->ih_flags & IH_DEAD)
1074 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
1076 * At this point, the handler has been disconnected from the event,
1077 * so we can kill the private ithread if any.
1079 if (handler->ih_thread) {
1080 ithread_destroy(handler->ih_thread);
1081 handler->ih_thread = NULL;
1083 intr_event_update(ie);
1086 * XXX: This could be bad in the case of ppbus(8). Also, I think
1087 * this could lead to races of stale data when servicing an
1091 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1092 if (handler != NULL) {
1098 ithread_destroy(ie->ie_thread);
1099 ie->ie_thread = NULL;
1102 mtx_unlock(&ie->ie_lock);
1103 free(handler, M_ITHREAD);
1108 intr_event_schedule_thread(struct intr_event *ie, struct intr_thread *it)
1110 struct intr_entropy entropy;
1116 * If no ithread or no handlers, then we have a stray interrupt.
1118 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers) || it == NULL)
1126 * If any of the handlers for this ithread claim to be good
1127 * sources of entropy, then gather some.
1129 if (ie->ie_flags & IE_ENTROPY) {
1130 entropy.event = (uintptr_t)ie;
1132 random_harvest_queue(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT);
1135 KASSERT(p != NULL, ("ithread %s has no process", ie->ie_name));
1138 * Set it_need to tell the thread to keep running if it is already
1139 * running. Then, lock the thread and see if we actually need to
1140 * put it on the runqueue.
1142 * Use store_rel to arrange that the store to ih_need in
1143 * swi_sched() is before the store to it_need and prepare for
1144 * transfer of this order to loads in the ithread.
1146 atomic_store_rel_int(&it->it_need, 1);
1148 if (TD_AWAITING_INTR(td)) {
1149 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, p->p_pid,
1152 sched_add(td, SRQ_INTR);
1154 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
1155 __func__, p->p_pid, td->td_name, it->it_need, td->td_state);
1164 * Allow interrupt event binding for software interrupt handlers -- a no-op,
1165 * since interrupts are generated in software rather than being directed by
1169 swi_assign_cpu(void *arg, int cpu)
1176 * Add a software interrupt handler to a specified event. If a given event
1177 * is not specified, then a new event is created.
1180 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
1181 void *arg, int pri, enum intr_type flags, void **cookiep)
1183 struct intr_event *ie;
1186 if (flags & INTR_ENTROPY)
1189 ie = (eventp != NULL) ? *eventp : NULL;
1192 if (!(ie->ie_flags & IE_SOFT))
1195 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
1196 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
1202 error = intr_event_add_handler(ie, name, NULL, handler, arg,
1203 PI_SWI(pri), flags, cookiep);
1208 * Schedule a software interrupt thread.
1211 swi_sched(void *cookie, int flags)
1213 struct intr_handler *ih = (struct intr_handler *)cookie;
1214 struct intr_event *ie = ih->ih_event;
1215 struct intr_entropy entropy;
1218 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
1221 entropy.event = (uintptr_t)ih;
1222 entropy.td = curthread;
1223 random_harvest_queue(&entropy, sizeof(entropy), 1, RANDOM_SWI);
1226 * Set ih_need for this handler so that if the ithread is already
1227 * running it will execute this handler on the next pass. Otherwise,
1228 * it will execute it the next time it runs.
1232 if (!(flags & SWI_DELAY)) {
1235 error = intr_event_schedule_thread(ie, ie->ie_thread);
1237 error = intr_event_schedule_thread(ie);
1239 KASSERT(error == 0, ("stray software interrupt"));
1244 * Remove a software interrupt handler. Currently this code does not
1245 * remove the associated interrupt event if it becomes empty. Calling code
1246 * may do so manually via intr_event_destroy(), but that's not really
1247 * an optimal interface.
1250 swi_remove(void *cookie)
1253 return (intr_event_remove_handler(cookie));
1258 priv_ithread_execute_handler(struct proc *p, struct intr_handler *ih)
1260 struct intr_event *ie;
1264 * If this handler is marked for death, remove it from
1265 * the list of handlers and wake up the sleeper.
1267 if (ih->ih_flags & IH_DEAD) {
1268 mtx_lock(&ie->ie_lock);
1269 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1270 ih->ih_flags &= ~IH_DEAD;
1272 mtx_unlock(&ie->ie_lock);
1276 /* Execute this handler. */
1277 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1278 __func__, p->p_pid, (void *)ih->ih_handler, ih->ih_argument,
1279 ih->ih_name, ih->ih_flags);
1281 if (!(ih->ih_flags & IH_MPSAFE))
1283 ih->ih_handler(ih->ih_argument);
1284 if (!(ih->ih_flags & IH_MPSAFE))
1290 * This is a public function for use by drivers that mux interrupt
1291 * handlers for child devices from their interrupt handler.
1294 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
1296 struct intr_handler *ih, *ihn;
1298 TAILQ_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
1300 * If this handler is marked for death, remove it from
1301 * the list of handlers and wake up the sleeper.
1303 if (ih->ih_flags & IH_DEAD) {
1304 mtx_lock(&ie->ie_lock);
1305 TAILQ_REMOVE(&ie->ie_handlers, ih, ih_next);
1306 ih->ih_flags &= ~IH_DEAD;
1308 mtx_unlock(&ie->ie_lock);
1312 /* Skip filter only handlers */
1313 if (ih->ih_handler == NULL)
1317 * For software interrupt threads, we only execute
1318 * handlers that have their need flag set. Hardware
1319 * interrupt threads always invoke all of their handlers.
1321 * ih_need can only be 0 or 1. Failed cmpset below
1322 * means that there is no request to execute handlers,
1323 * so a retry of the cmpset is not needed.
1325 if ((ie->ie_flags & IE_SOFT) != 0 &&
1326 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1329 /* Execute this handler. */
1330 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1331 __func__, p->p_pid, (void *)ih->ih_handler,
1332 ih->ih_argument, ih->ih_name, ih->ih_flags);
1334 if (!(ih->ih_flags & IH_MPSAFE))
1336 ih->ih_handler(ih->ih_argument);
1337 if (!(ih->ih_flags & IH_MPSAFE))
1343 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1346 /* Interrupt handlers should not sleep. */
1347 if (!(ie->ie_flags & IE_SOFT))
1348 THREAD_NO_SLEEPING();
1349 intr_event_execute_handlers(p, ie);
1350 if (!(ie->ie_flags & IE_SOFT))
1351 THREAD_SLEEPING_OK();
1354 * Interrupt storm handling:
1356 * If this interrupt source is currently storming, then throttle
1357 * it to only fire the handler once per clock tick.
1359 * If this interrupt source is not currently storming, but the
1360 * number of back to back interrupts exceeds the storm threshold,
1361 * then enter storming mode.
1363 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1364 !(ie->ie_flags & IE_SOFT)) {
1365 /* Report the message only once every second. */
1366 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1368 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1376 * Now that all the handlers have had a chance to run, reenable
1377 * the interrupt source.
1379 if (ie->ie_post_ithread != NULL)
1380 ie->ie_post_ithread(ie->ie_source);
1385 * This is the main code for interrupt threads.
1388 ithread_loop(void *arg)
1390 struct intr_thread *ithd;
1391 struct intr_event *ie;
1398 ithd = (struct intr_thread *)arg;
1399 KASSERT(ithd->it_thread == td,
1400 ("%s: ithread and proc linkage out of sync", __func__));
1401 ie = ithd->it_event;
1406 * As long as we have interrupts outstanding, go through the
1407 * list of handlers, giving each one a go at it.
1411 * If we are an orphaned thread, then just die.
1413 if (ithd->it_flags & IT_DEAD) {
1414 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1415 p->p_pid, td->td_name);
1416 free(ithd, M_ITHREAD);
1421 * Service interrupts. If another interrupt arrives while
1422 * we are running, it will set it_need to note that we
1423 * should make another pass.
1425 * The load_acq part of the following cmpset ensures
1426 * that the load of ih_need in ithread_execute_handlers()
1427 * is ordered after the load of it_need here.
1429 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0)
1430 ithread_execute_handlers(p, ie);
1431 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1432 mtx_assert(&Giant, MA_NOTOWNED);
1435 * Processed all our interrupts. Now get the sched
1436 * lock. This may take a while and it_need may get
1437 * set again, so we have to check it again.
1440 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1441 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1444 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1446 if (ithd->it_flags & IT_WAIT) {
1448 ithd->it_flags &= ~IT_WAIT;
1459 * Main interrupt handling body.
1462 * o ie: the event connected to this interrupt.
1463 * o frame: some archs (i.e. i386) pass a frame to some.
1464 * handlers as their main argument.
1466 * o 0: everything ok.
1467 * o EINVAL: stray interrupt.
1470 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1472 struct intr_handler *ih;
1473 struct trapframe *oldframe;
1479 #ifdef KSTACK_USAGE_PROF
1480 intr_prof_stack_use(td, frame);
1483 /* An interrupt with no event or handlers is a stray interrupt. */
1484 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1488 * Execute fast interrupt handlers directly.
1489 * To support clock handlers, if a handler registers
1490 * with a NULL argument, then we pass it a pointer to
1491 * a trapframe as its argument.
1493 td->td_intr_nesting_level++;
1497 oldframe = td->td_intr_frame;
1498 td->td_intr_frame = frame;
1499 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1500 if (ih->ih_filter == NULL) {
1504 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1505 ih->ih_filter, ih->ih_argument == NULL ? frame :
1506 ih->ih_argument, ih->ih_name);
1507 if (ih->ih_argument == NULL)
1508 ret = ih->ih_filter(frame);
1510 ret = ih->ih_filter(ih->ih_argument);
1511 KASSERT(ret == FILTER_STRAY ||
1512 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1513 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1514 ("%s: incorrect return value %#x from %s", __func__, ret,
1518 * Wrapper handler special handling:
1520 * in some particular cases (like pccard and pccbb),
1521 * the _real_ device handler is wrapped in a couple of
1522 * functions - a filter wrapper and an ithread wrapper.
1523 * In this case (and just in this case), the filter wrapper
1524 * could ask the system to schedule the ithread and mask
1525 * the interrupt source if the wrapped handler is composed
1526 * of just an ithread handler.
1528 * TODO: write a generic wrapper to avoid people rolling
1532 if (ret == FILTER_SCHEDULE_THREAD)
1536 td->td_intr_frame = oldframe;
1539 if (ie->ie_pre_ithread != NULL)
1540 ie->ie_pre_ithread(ie->ie_source);
1542 if (ie->ie_post_filter != NULL)
1543 ie->ie_post_filter(ie->ie_source);
1546 /* Schedule the ithread if needed. */
1550 error = intr_event_schedule_thread(ie);
1551 KASSERT(error == 0, ("bad stray interrupt"));
1554 td->td_intr_nesting_level--;
1559 * This is the main code for interrupt threads.
1562 ithread_loop(void *arg)
1564 struct intr_thread *ithd;
1565 struct intr_handler *ih;
1566 struct intr_event *ie;
1574 ih = (struct intr_handler *)arg;
1575 priv = (ih->ih_thread != NULL) ? 1 : 0;
1576 ithd = (priv) ? ih->ih_thread : ih->ih_event->ie_thread;
1577 KASSERT(ithd->it_thread == td,
1578 ("%s: ithread and proc linkage out of sync", __func__));
1579 ie = ithd->it_event;
1584 * As long as we have interrupts outstanding, go through the
1585 * list of handlers, giving each one a go at it.
1589 * If we are an orphaned thread, then just die.
1591 if (ithd->it_flags & IT_DEAD) {
1592 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1593 p->p_pid, td->td_name);
1594 free(ithd, M_ITHREAD);
1599 * Service interrupts. If another interrupt arrives while
1600 * we are running, it will set it_need to note that we
1601 * should make another pass.
1603 * The load_acq part of the following cmpset ensures
1604 * that the load of ih_need in ithread_execute_handlers()
1605 * is ordered after the load of it_need here.
1607 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0) {
1609 priv_ithread_execute_handler(p, ih);
1611 ithread_execute_handlers(p, ie);
1613 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1614 mtx_assert(&Giant, MA_NOTOWNED);
1617 * Processed all our interrupts. Now get the sched
1618 * lock. This may take a while and it_need may get
1619 * set again, so we have to check it again.
1622 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1623 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1626 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1628 if (ithd->it_flags & IT_WAIT) {
1630 ithd->it_flags &= ~IT_WAIT;
1641 * Main loop for interrupt filter.
1643 * Some architectures (i386, amd64 and arm) require the optional frame
1644 * parameter, and use it as the main argument for fast handler execution
1645 * when ih_argument == NULL.
1648 * o FILTER_STRAY: No filter recognized the event, and no
1649 * filter-less handler is registered on this
1651 * o FILTER_HANDLED: A filter claimed the event and served it.
1652 * o FILTER_SCHEDULE_THREAD: No filter claimed the event, but there's at
1653 * least one filter-less handler on this line.
1654 * o FILTER_HANDLED |
1655 * FILTER_SCHEDULE_THREAD: A filter claimed the event, and asked for
1656 * scheduling the per-handler ithread.
1658 * In case an ithread has to be scheduled, in *ithd there will be a
1659 * pointer to a struct intr_thread containing the thread to be
1664 intr_filter_loop(struct intr_event *ie, struct trapframe *frame,
1665 struct intr_thread **ithd)
1667 struct intr_handler *ih;
1669 int ret, thread_only;
1673 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next) {
1675 * Execute fast interrupt handlers directly.
1676 * To support clock handlers, if a handler registers
1677 * with a NULL argument, then we pass it a pointer to
1678 * a trapframe as its argument.
1680 arg = ((ih->ih_argument == NULL) ? frame : ih->ih_argument);
1682 CTR5(KTR_INTR, "%s: exec %p/%p(%p) for %s", __func__,
1683 ih->ih_filter, ih->ih_handler, arg, ih->ih_name);
1685 if (ih->ih_filter != NULL)
1686 ret = ih->ih_filter(arg);
1691 KASSERT(ret == FILTER_STRAY ||
1692 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1693 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1694 ("%s: incorrect return value %#x from %s", __func__, ret,
1696 if (ret & FILTER_STRAY)
1699 *ithd = ih->ih_thread;
1705 * No filters handled the interrupt and we have at least
1706 * one handler without a filter. In this case, we schedule
1707 * all of the filter-less handlers to run in the ithread.
1710 *ithd = ie->ie_thread;
1711 return (FILTER_SCHEDULE_THREAD);
1713 return (FILTER_STRAY);
1717 * Main interrupt handling body.
1720 * o ie: the event connected to this interrupt.
1721 * o frame: some archs (i.e. i386) pass a frame to some.
1722 * handlers as their main argument.
1724 * o 0: everything ok.
1725 * o EINVAL: stray interrupt.
1728 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1730 struct intr_thread *ithd;
1731 struct trapframe *oldframe;
1738 if (ie == NULL || TAILQ_EMPTY(&ie->ie_handlers))
1741 td->td_intr_nesting_level++;
1744 oldframe = td->td_intr_frame;
1745 td->td_intr_frame = frame;
1746 thread = intr_filter_loop(ie, frame, &ithd);
1747 if (thread & FILTER_HANDLED) {
1748 if (ie->ie_post_filter != NULL)
1749 ie->ie_post_filter(ie->ie_source);
1751 if (ie->ie_pre_ithread != NULL)
1752 ie->ie_pre_ithread(ie->ie_source);
1754 td->td_intr_frame = oldframe;
1757 /* Interrupt storm logic */
1758 if (thread & FILTER_STRAY) {
1760 if (ie->ie_count < intr_storm_threshold)
1761 printf("Interrupt stray detection not present\n");
1764 /* Schedule an ithread if needed. */
1765 if (thread & FILTER_SCHEDULE_THREAD) {
1766 if (intr_event_schedule_thread(ie, ithd) != 0)
1767 panic("%s: impossible stray interrupt", __func__);
1769 td->td_intr_nesting_level--;
1776 * Dump details about an interrupt handler
1779 db_dump_intrhand(struct intr_handler *ih)
1783 db_printf("\t%-10s ", ih->ih_name);
1784 switch (ih->ih_pri) {
1804 if (ih->ih_pri >= PI_SOFT)
1807 db_printf("%4u", ih->ih_pri);
1811 if (ih->ih_filter != NULL) {
1813 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1815 if (ih->ih_handler != NULL) {
1816 if (ih->ih_filter != NULL)
1819 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1821 db_printf("(%p)", ih->ih_argument);
1823 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1827 if (ih->ih_flags & IH_EXCLUSIVE) {
1833 if (ih->ih_flags & IH_ENTROPY) {
1836 db_printf("ENTROPY");
1839 if (ih->ih_flags & IH_DEAD) {
1845 if (ih->ih_flags & IH_MPSAFE) {
1848 db_printf("MPSAFE");
1862 * Dump details about a event.
1865 db_dump_intr_event(struct intr_event *ie, int handlers)
1867 struct intr_handler *ih;
1868 struct intr_thread *it;
1871 db_printf("%s ", ie->ie_fullname);
1874 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1876 db_printf("(no thread)");
1877 if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1878 (it != NULL && it->it_need)) {
1881 if (ie->ie_flags & IE_SOFT) {
1885 if (ie->ie_flags & IE_ENTROPY) {
1888 db_printf("ENTROPY");
1891 if (ie->ie_flags & IE_ADDING_THREAD) {
1894 db_printf("ADDING_THREAD");
1897 if (it != NULL && it->it_need) {
1907 TAILQ_FOREACH(ih, &ie->ie_handlers, ih_next)
1908 db_dump_intrhand(ih);
1912 * Dump data about interrupt handlers
1914 DB_SHOW_COMMAND(intr, db_show_intr)
1916 struct intr_event *ie;
1919 verbose = strchr(modif, 'v') != NULL;
1920 all = strchr(modif, 'a') != NULL;
1921 TAILQ_FOREACH(ie, &event_list, ie_list) {
1922 if (!all && TAILQ_EMPTY(&ie->ie_handlers))
1924 db_dump_intr_event(ie, verbose);
1932 * Start standard software interrupt threads
1935 start_softintr(void *dummy)
1938 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1939 panic("died while creating vm swi ithread");
1941 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1945 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1946 * The data for this machine dependent, and the declarations are in machine
1947 * dependent code. The layout of intrnames and intrcnt however is machine
1950 * We do not know the length of intrcnt and intrnames at compile time, so
1951 * calculate things at run time.
1954 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1956 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1959 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1960 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1963 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1966 uint32_t *intrcnt32;
1970 if (req->flags & SCTL_MASK32) {
1972 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1973 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1974 if (intrcnt32 == NULL)
1976 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1977 intrcnt32[i] = intrcnt[i];
1978 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1979 free(intrcnt32, M_TEMP);
1983 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1986 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1987 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1991 * DDB command to dump the interrupt statistics.
1993 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
2001 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
2006 db_printf("%s\t%lu\n", cp, *i);
2007 cp += strlen(cp) + 1;