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);
104 static int intr_event_schedule_thread(struct intr_event *ie);
105 static struct intr_thread *ithread_create(const char *name);
106 static void ithread_destroy(struct intr_thread *ithread);
107 static void ithread_execute_handlers(struct proc *p,
108 struct intr_event *ie);
109 static void ithread_loop(void *);
110 static void ithread_update(struct intr_thread *ithd);
111 static void start_softintr(void *);
113 /* Map an interrupt type to an ithread priority. */
115 intr_priority(enum intr_type flags)
119 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
120 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV);
141 pri = PI_DULL; /* don't care */
144 /* We didn't specify an interrupt level. */
145 panic("intr_priority: no interrupt type in flags");
152 * Update an ithread based on the associated intr_event.
155 ithread_update(struct intr_thread *ithd)
157 struct intr_event *ie;
162 td = ithd->it_thread;
163 mtx_assert(&ie->ie_lock, MA_OWNED);
165 /* Determine the overall priority of this event. */
166 if (CK_SLIST_EMPTY(&ie->ie_handlers))
169 pri = CK_SLIST_FIRST(&ie->ie_handlers)->ih_pri;
171 /* Update name and priority. */
172 strlcpy(td->td_name, ie->ie_fullname, sizeof(td->td_name));
174 sched_clear_tdname(td);
182 * Regenerate the full name of an interrupt event and update its priority.
185 intr_event_update(struct intr_event *ie)
187 struct intr_handler *ih;
191 /* Start off with no entropy and just the name of the event. */
192 mtx_assert(&ie->ie_lock, MA_OWNED);
193 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
194 ie->ie_flags &= ~IE_ENTROPY;
198 /* Run through all the handlers updating values. */
199 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
200 if (strlen(ie->ie_fullname) + strlen(ih->ih_name) + 1 <
201 sizeof(ie->ie_fullname)) {
202 strcat(ie->ie_fullname, " ");
203 strcat(ie->ie_fullname, ih->ih_name);
207 if (ih->ih_flags & IH_ENTROPY)
208 ie->ie_flags |= IE_ENTROPY;
212 * If the handler names were too long, add +'s to indicate missing
213 * names. If we run out of room and still have +'s to add, change
214 * the last character from a + to a *.
216 last = &ie->ie_fullname[sizeof(ie->ie_fullname) - 2];
217 while (missed-- > 0) {
218 if (strlen(ie->ie_fullname) + 1 == sizeof(ie->ie_fullname)) {
225 strcat(ie->ie_fullname, " +");
228 strcat(ie->ie_fullname, "+");
232 * If this event has an ithread, update it's priority and
235 if (ie->ie_thread != NULL)
236 ithread_update(ie->ie_thread);
237 CTR2(KTR_INTR, "%s: updated %s", __func__, ie->ie_fullname);
241 intr_event_create(struct intr_event **event, void *source, int flags, int irq,
242 void (*pre_ithread)(void *), void (*post_ithread)(void *),
243 void (*post_filter)(void *), int (*assign_cpu)(void *, int),
244 const char *fmt, ...)
246 struct intr_event *ie;
249 /* The only valid flag during creation is IE_SOFT. */
250 if ((flags & ~IE_SOFT) != 0)
252 ie = malloc(sizeof(struct intr_event), M_ITHREAD, M_WAITOK | M_ZERO);
253 ie->ie_source = source;
254 ie->ie_pre_ithread = pre_ithread;
255 ie->ie_post_ithread = post_ithread;
256 ie->ie_post_filter = post_filter;
257 ie->ie_assign_cpu = assign_cpu;
258 ie->ie_flags = flags;
261 CK_SLIST_INIT(&ie->ie_handlers);
262 mtx_init(&ie->ie_lock, "intr event", NULL, MTX_DEF);
265 vsnprintf(ie->ie_name, sizeof(ie->ie_name), fmt, ap);
267 strlcpy(ie->ie_fullname, ie->ie_name, sizeof(ie->ie_fullname));
268 mtx_lock(&event_lock);
269 TAILQ_INSERT_TAIL(&event_list, ie, ie_list);
270 mtx_unlock(&event_lock);
273 CTR2(KTR_INTR, "%s: created %s", __func__, ie->ie_name);
278 * Bind an interrupt event to the specified CPU. Note that not all
279 * platforms support binding an interrupt to a CPU. For those
280 * platforms this request will fail. Using a cpu id of NOCPU unbinds
281 * the interrupt event.
284 _intr_event_bind(struct intr_event *ie, int cpu, bool bindirq, bool bindithread)
289 /* Need a CPU to bind to. */
290 if (cpu != NOCPU && CPU_ABSENT(cpu))
293 if (ie->ie_assign_cpu == NULL)
296 error = priv_check(curthread, PRIV_SCHED_CPUSET_INTR);
301 * If we have any ithreads try to set their mask first to verify
305 mtx_lock(&ie->ie_lock);
306 if (ie->ie_thread != NULL) {
307 id = ie->ie_thread->it_thread->td_tid;
308 mtx_unlock(&ie->ie_lock);
309 error = cpuset_setithread(id, cpu);
313 mtx_unlock(&ie->ie_lock);
316 error = ie->ie_assign_cpu(ie->ie_source, cpu);
319 mtx_lock(&ie->ie_lock);
320 if (ie->ie_thread != NULL) {
322 id = ie->ie_thread->it_thread->td_tid;
323 mtx_unlock(&ie->ie_lock);
324 (void)cpuset_setithread(id, cpu);
326 mtx_unlock(&ie->ie_lock);
332 mtx_lock(&ie->ie_lock);
334 mtx_unlock(&ie->ie_lock);
341 * Bind an interrupt event to the specified CPU. For supported platforms, any
342 * associated ithreads as well as the primary interrupt context will be bound
343 * to the specificed CPU.
346 intr_event_bind(struct intr_event *ie, int cpu)
349 return (_intr_event_bind(ie, cpu, true, true));
353 * Bind an interrupt event to the specified CPU, but do not bind associated
357 intr_event_bind_irqonly(struct intr_event *ie, int cpu)
360 return (_intr_event_bind(ie, cpu, true, false));
364 * Bind an interrupt event's ithread to the specified CPU.
367 intr_event_bind_ithread(struct intr_event *ie, int cpu)
370 return (_intr_event_bind(ie, cpu, false, true));
373 static struct intr_event *
376 struct intr_event *ie;
378 mtx_lock(&event_lock);
379 TAILQ_FOREACH(ie, &event_list, ie_list)
380 if (ie->ie_irq == irq &&
381 (ie->ie_flags & IE_SOFT) == 0 &&
382 CK_SLIST_FIRST(&ie->ie_handlers) != NULL)
384 mtx_unlock(&event_lock);
389 intr_setaffinity(int irq, int mode, void *m)
391 struct intr_event *ie;
398 * If we're setting all cpus we can unbind. Otherwise make sure
399 * only one cpu is in the set.
401 if (CPU_CMP(cpuset_root, mask)) {
402 for (n = 0; n < CPU_SETSIZE; n++) {
403 if (!CPU_ISSET(n, mask))
410 ie = intr_lookup(irq);
415 return (intr_event_bind(ie, cpu));
416 case CPU_WHICH_INTRHANDLER:
417 return (intr_event_bind_irqonly(ie, cpu));
418 case CPU_WHICH_ITHREAD:
419 return (intr_event_bind_ithread(ie, cpu));
426 intr_getaffinity(int irq, int mode, void *m)
428 struct intr_event *ie;
436 ie = intr_lookup(irq);
444 case CPU_WHICH_INTRHANDLER:
445 mtx_lock(&ie->ie_lock);
446 if (ie->ie_cpu == NOCPU)
447 CPU_COPY(cpuset_root, mask);
449 CPU_SET(ie->ie_cpu, mask);
450 mtx_unlock(&ie->ie_lock);
452 case CPU_WHICH_ITHREAD:
453 mtx_lock(&ie->ie_lock);
454 if (ie->ie_thread == NULL) {
455 mtx_unlock(&ie->ie_lock);
456 CPU_COPY(cpuset_root, mask);
458 id = ie->ie_thread->it_thread->td_tid;
459 mtx_unlock(&ie->ie_lock);
460 error = cpuset_which(CPU_WHICH_TID, id, &p, &td, NULL);
463 CPU_COPY(&td->td_cpuset->cs_mask, mask);
473 intr_event_destroy(struct intr_event *ie)
476 mtx_lock(&event_lock);
477 mtx_lock(&ie->ie_lock);
478 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
479 mtx_unlock(&ie->ie_lock);
480 mtx_unlock(&event_lock);
483 TAILQ_REMOVE(&event_list, ie, ie_list);
485 if (ie->ie_thread != NULL) {
486 ithread_destroy(ie->ie_thread);
487 ie->ie_thread = NULL;
490 mtx_unlock(&ie->ie_lock);
491 mtx_unlock(&event_lock);
492 mtx_destroy(&ie->ie_lock);
497 static struct intr_thread *
498 ithread_create(const char *name)
500 struct intr_thread *ithd;
504 ithd = malloc(sizeof(struct intr_thread), M_ITHREAD, M_WAITOK | M_ZERO);
506 error = kproc_kthread_add(ithread_loop, ithd, &intrproc,
507 &td, RFSTOPPED | RFHIGHPID,
508 0, "intr", "%s", name);
510 panic("kproc_create() failed with %d", error);
512 sched_class(td, PRI_ITHD);
515 td->td_pflags |= TDP_ITHREAD;
516 ithd->it_thread = td;
517 CTR2(KTR_INTR, "%s: created %s", __func__, name);
522 ithread_destroy(struct intr_thread *ithread)
526 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_event->ie_name);
527 td = ithread->it_thread;
529 ithread->it_flags |= IT_DEAD;
530 if (TD_AWAITING_INTR(td)) {
532 sched_add(td, SRQ_INTR);
538 intr_event_add_handler(struct intr_event *ie, const char *name,
539 driver_filter_t filter, driver_intr_t handler, void *arg, u_char pri,
540 enum intr_type flags, void **cookiep)
542 struct intr_handler *ih, *temp_ih;
543 struct intr_handler **prevptr;
544 struct intr_thread *it;
546 if (ie == NULL || name == NULL || (handler == NULL && filter == NULL))
549 /* Allocate and populate an interrupt handler structure. */
550 ih = malloc(sizeof(struct intr_handler), M_ITHREAD, M_WAITOK | M_ZERO);
551 ih->ih_filter = filter;
552 ih->ih_handler = handler;
553 ih->ih_argument = arg;
554 strlcpy(ih->ih_name, name, sizeof(ih->ih_name));
557 if (flags & INTR_EXCL)
558 ih->ih_flags = IH_EXCLUSIVE;
559 if (flags & INTR_MPSAFE)
560 ih->ih_flags |= IH_MPSAFE;
561 if (flags & INTR_ENTROPY)
562 ih->ih_flags |= IH_ENTROPY;
564 /* We can only have one exclusive handler in a event. */
565 mtx_lock(&ie->ie_lock);
566 if (!CK_SLIST_EMPTY(&ie->ie_handlers)) {
567 if ((flags & INTR_EXCL) ||
568 (CK_SLIST_FIRST(&ie->ie_handlers)->ih_flags & IH_EXCLUSIVE)) {
569 mtx_unlock(&ie->ie_lock);
575 /* Create a thread if we need one. */
576 while (ie->ie_thread == NULL && handler != NULL) {
577 if (ie->ie_flags & IE_ADDING_THREAD)
578 msleep(ie, &ie->ie_lock, 0, "ithread", 0);
580 ie->ie_flags |= IE_ADDING_THREAD;
581 mtx_unlock(&ie->ie_lock);
582 it = ithread_create("intr: newborn");
583 mtx_lock(&ie->ie_lock);
584 ie->ie_flags &= ~IE_ADDING_THREAD;
592 /* Add the new handler to the event in priority order. */
593 CK_SLIST_FOREACH_PREVPTR(temp_ih, prevptr, &ie->ie_handlers, ih_next) {
594 if (temp_ih->ih_pri > ih->ih_pri)
597 CK_SLIST_INSERT_PREVPTR(prevptr, temp_ih, ih, ih_next);
599 intr_event_update(ie);
601 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name,
603 mtx_unlock(&ie->ie_lock);
611 * Append a description preceded by a ':' to the name of the specified
615 intr_event_describe_handler(struct intr_event *ie, void *cookie,
618 struct intr_handler *ih;
622 mtx_lock(&ie->ie_lock);
624 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
629 mtx_unlock(&ie->ie_lock);
630 panic("handler %p not found in interrupt event %p", cookie, ie);
636 * Look for an existing description by checking for an
637 * existing ":". This assumes device names do not include
638 * colons. If one is found, prepare to insert the new
639 * description at that point. If one is not found, find the
640 * end of the name to use as the insertion point.
642 start = strchr(ih->ih_name, ':');
644 start = strchr(ih->ih_name, 0);
647 * See if there is enough remaining room in the string for the
648 * description + ":". The "- 1" leaves room for the trailing
649 * '\0'. The "+ 1" accounts for the colon.
651 space = sizeof(ih->ih_name) - (start - ih->ih_name) - 1;
652 if (strlen(descr) + 1 > space) {
653 mtx_unlock(&ie->ie_lock);
657 /* Append a colon followed by the description. */
659 strcpy(start + 1, descr);
660 intr_event_update(ie);
661 mtx_unlock(&ie->ie_lock);
666 * Return the ie_source field from the intr_event an intr_handler is
670 intr_handler_source(void *cookie)
672 struct intr_handler *ih;
673 struct intr_event *ie;
675 ih = (struct intr_handler *)cookie;
680 ("interrupt handler \"%s\" has a NULL interrupt event",
682 return (ie->ie_source);
686 * Sleep until an ithread finishes executing an interrupt handler.
688 * XXX Doesn't currently handle interrupt filters or fast interrupt
689 * handlers. This is intended for compatibility with linux drivers
690 * only. Do not use in BSD code.
695 struct intr_event *ie;
696 struct intr_thread *ithd;
699 ie = intr_lookup(irq);
702 if (ie->ie_thread == NULL)
704 ithd = ie->ie_thread;
705 td = ithd->it_thread;
707 * We set the flag and wait for it to be cleared to avoid
708 * long delays with potentially busy interrupt handlers
709 * were we to only sample TD_AWAITING_INTR() every tick.
712 if (!TD_AWAITING_INTR(td)) {
713 ithd->it_flags |= IT_WAIT;
714 while (ithd->it_flags & IT_WAIT) {
725 intr_event_remove_handler(void *cookie)
727 struct intr_handler *handler = (struct intr_handler *)cookie;
728 struct intr_event *ie;
729 struct intr_handler *ih;
730 struct intr_handler **prevptr;
737 ie = handler->ih_event;
739 ("interrupt handler \"%s\" has a NULL interrupt event",
742 mtx_lock(&ie->ie_lock);
743 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name,
745 CK_SLIST_FOREACH_PREVPTR(ih, prevptr, &ie->ie_handlers, ih_next) {
750 panic("interrupt handler \"%s\" not found in "
751 "interrupt event \"%s\"", handler->ih_name, ie->ie_name);
755 * If there is no ithread, then just remove the handler and return.
756 * XXX: Note that an INTR_FAST handler might be running on another
759 if (ie->ie_thread == NULL) {
760 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
761 mtx_unlock(&ie->ie_lock);
762 free(handler, M_ITHREAD);
767 * If the interrupt thread is already running, then just mark this
768 * handler as being dead and let the ithread do the actual removal.
770 * During a cold boot while cold is set, msleep() does not sleep,
771 * so we have to remove the handler here rather than letting the
774 thread_lock(ie->ie_thread->it_thread);
775 if (!TD_AWAITING_INTR(ie->ie_thread->it_thread) && !cold) {
776 handler->ih_flags |= IH_DEAD;
779 * Ensure that the thread will process the handler list
780 * again and remove this handler if it has already passed
783 * The release part of the following store ensures
784 * that the update of ih_flags is ordered before the
785 * it_need setting. See the comment before
786 * atomic_cmpset_acq(&ithd->it_need, ...) operation in
787 * the ithread_execute_handlers().
789 atomic_store_rel_int(&ie->ie_thread->it_need, 1);
791 CK_SLIST_REMOVE_PREVPTR(prevptr, ih, ih_next);
792 thread_unlock(ie->ie_thread->it_thread);
793 while (handler->ih_flags & IH_DEAD)
794 msleep(handler, &ie->ie_lock, 0, "iev_rmh", 0);
795 intr_event_update(ie);
799 * XXX: This could be bad in the case of ppbus(8). Also, I think
800 * this could lead to races of stale data when servicing an
804 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
805 if (ih->ih_handler != NULL) {
811 ithread_destroy(ie->ie_thread);
812 ie->ie_thread = NULL;
815 mtx_unlock(&ie->ie_lock);
816 free(handler, M_ITHREAD);
821 intr_event_schedule_thread(struct intr_event *ie)
823 struct intr_entropy entropy;
824 struct intr_thread *it;
829 * If no ithread or no handlers, then we have a stray interrupt.
831 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers) ||
832 ie->ie_thread == NULL)
840 * If any of the handlers for this ithread claim to be good
841 * sources of entropy, then gather some.
843 if (ie->ie_flags & IE_ENTROPY) {
844 entropy.event = (uintptr_t)ie;
846 random_harvest_queue(&entropy, sizeof(entropy), 2, RANDOM_INTERRUPT);
849 KASSERT(td->td_proc != NULL, ("ithread %s has no process", ie->ie_name));
852 * Set it_need to tell the thread to keep running if it is already
853 * running. Then, lock the thread and see if we actually need to
854 * put it on the runqueue.
856 * Use store_rel to arrange that the store to ih_need in
857 * swi_sched() is before the store to it_need and prepare for
858 * transfer of this order to loads in the ithread.
860 atomic_store_rel_int(&it->it_need, 1);
862 if (TD_AWAITING_INTR(td)) {
863 CTR3(KTR_INTR, "%s: schedule pid %d (%s)", __func__, td->td_proc->p_pid,
866 sched_add(td, SRQ_INTR);
868 CTR5(KTR_INTR, "%s: pid %d (%s): it_need %d, state %d",
869 __func__, td->td_proc->p_pid, td->td_name, it->it_need, td->td_state);
877 * Allow interrupt event binding for software interrupt handlers -- a no-op,
878 * since interrupts are generated in software rather than being directed by
882 swi_assign_cpu(void *arg, int cpu)
889 * Add a software interrupt handler to a specified event. If a given event
890 * is not specified, then a new event is created.
893 swi_add(struct intr_event **eventp, const char *name, driver_intr_t handler,
894 void *arg, int pri, enum intr_type flags, void **cookiep)
896 struct intr_event *ie;
899 if (flags & INTR_ENTROPY)
902 ie = (eventp != NULL) ? *eventp : NULL;
905 if (!(ie->ie_flags & IE_SOFT))
908 error = intr_event_create(&ie, NULL, IE_SOFT, 0,
909 NULL, NULL, NULL, swi_assign_cpu, "swi%d:", pri);
915 error = intr_event_add_handler(ie, name, NULL, handler, arg,
916 PI_SWI(pri), flags, cookiep);
921 * Schedule a software interrupt thread.
924 swi_sched(void *cookie, int flags)
926 struct intr_handler *ih = (struct intr_handler *)cookie;
927 struct intr_event *ie = ih->ih_event;
928 struct intr_entropy entropy;
931 CTR3(KTR_INTR, "swi_sched: %s %s need=%d", ie->ie_name, ih->ih_name,
934 entropy.event = (uintptr_t)ih;
935 entropy.td = curthread;
936 random_harvest_queue(&entropy, sizeof(entropy), 1, RANDOM_SWI);
939 * Set ih_need for this handler so that if the ithread is already
940 * running it will execute this handler on the next pass. Otherwise,
941 * it will execute it the next time it runs.
945 if (!(flags & SWI_DELAY)) {
947 error = intr_event_schedule_thread(ie);
948 KASSERT(error == 0, ("stray software interrupt"));
953 * Remove a software interrupt handler. Currently this code does not
954 * remove the associated interrupt event if it becomes empty. Calling code
955 * may do so manually via intr_event_destroy(), but that's not really
956 * an optimal interface.
959 swi_remove(void *cookie)
962 return (intr_event_remove_handler(cookie));
966 intr_event_execute_handlers(struct proc *p, struct intr_event *ie)
968 struct intr_handler *ih, *ihn, *ihp;
971 CK_SLIST_FOREACH_SAFE(ih, &ie->ie_handlers, ih_next, ihn) {
973 * If this handler is marked for death, remove it from
974 * the list of handlers and wake up the sleeper.
976 if (ih->ih_flags & IH_DEAD) {
977 mtx_lock(&ie->ie_lock);
979 CK_SLIST_REMOVE_HEAD(&ie->ie_handlers, ih_next);
981 CK_SLIST_REMOVE_AFTER(ihp, ih_next);
982 ih->ih_flags &= ~IH_DEAD;
984 mtx_unlock(&ie->ie_lock);
989 * Now that we know that the current element won't be removed
990 * update the previous element.
994 /* Skip filter only handlers */
995 if (ih->ih_handler == NULL)
999 * For software interrupt threads, we only execute
1000 * handlers that have their need flag set. Hardware
1001 * interrupt threads always invoke all of their handlers.
1003 * ih_need can only be 0 or 1. Failed cmpset below
1004 * means that there is no request to execute handlers,
1005 * so a retry of the cmpset is not needed.
1007 if ((ie->ie_flags & IE_SOFT) != 0 &&
1008 atomic_cmpset_int(&ih->ih_need, 1, 0) == 0)
1011 /* Execute this handler. */
1012 CTR6(KTR_INTR, "%s: pid %d exec %p(%p) for %s flg=%x",
1013 __func__, p->p_pid, (void *)ih->ih_handler,
1014 ih->ih_argument, ih->ih_name, ih->ih_flags);
1016 if (!(ih->ih_flags & IH_MPSAFE))
1018 ih->ih_handler(ih->ih_argument);
1019 if (!(ih->ih_flags & IH_MPSAFE))
1025 ithread_execute_handlers(struct proc *p, struct intr_event *ie)
1028 /* Interrupt handlers should not sleep. */
1029 if (!(ie->ie_flags & IE_SOFT))
1030 THREAD_NO_SLEEPING();
1031 intr_event_execute_handlers(p, ie);
1032 if (!(ie->ie_flags & IE_SOFT))
1033 THREAD_SLEEPING_OK();
1036 * Interrupt storm handling:
1038 * If this interrupt source is currently storming, then throttle
1039 * it to only fire the handler once per clock tick.
1041 * If this interrupt source is not currently storming, but the
1042 * number of back to back interrupts exceeds the storm threshold,
1043 * then enter storming mode.
1045 if (intr_storm_threshold != 0 && ie->ie_count >= intr_storm_threshold &&
1046 !(ie->ie_flags & IE_SOFT)) {
1047 /* Report the message only once every second. */
1048 if (ppsratecheck(&ie->ie_warntm, &ie->ie_warncnt, 1)) {
1050 "interrupt storm detected on \"%s\"; throttling interrupt source\n",
1058 * Now that all the handlers have had a chance to run, reenable
1059 * the interrupt source.
1061 if (ie->ie_post_ithread != NULL)
1062 ie->ie_post_ithread(ie->ie_source);
1066 * This is the main code for interrupt threads.
1069 ithread_loop(void *arg)
1071 struct intr_thread *ithd;
1072 struct intr_event *ie;
1079 ithd = (struct intr_thread *)arg;
1080 KASSERT(ithd->it_thread == td,
1081 ("%s: ithread and proc linkage out of sync", __func__));
1082 ie = ithd->it_event;
1087 * As long as we have interrupts outstanding, go through the
1088 * list of handlers, giving each one a go at it.
1092 * If we are an orphaned thread, then just die.
1094 if (ithd->it_flags & IT_DEAD) {
1095 CTR3(KTR_INTR, "%s: pid %d (%s) exiting", __func__,
1096 p->p_pid, td->td_name);
1097 free(ithd, M_ITHREAD);
1102 * Service interrupts. If another interrupt arrives while
1103 * we are running, it will set it_need to note that we
1104 * should make another pass.
1106 * The load_acq part of the following cmpset ensures
1107 * that the load of ih_need in ithread_execute_handlers()
1108 * is ordered after the load of it_need here.
1110 while (atomic_cmpset_acq_int(&ithd->it_need, 1, 0) != 0)
1111 ithread_execute_handlers(p, ie);
1112 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread");
1113 mtx_assert(&Giant, MA_NOTOWNED);
1116 * Processed all our interrupts. Now get the sched
1117 * lock. This may take a while and it_need may get
1118 * set again, so we have to check it again.
1121 if (atomic_load_acq_int(&ithd->it_need) == 0 &&
1122 (ithd->it_flags & (IT_DEAD | IT_WAIT)) == 0) {
1125 mi_switch(SW_VOL | SWT_IWAIT, NULL);
1127 if (ithd->it_flags & IT_WAIT) {
1129 ithd->it_flags &= ~IT_WAIT;
1140 * Main interrupt handling body.
1143 * o ie: the event connected to this interrupt.
1144 * o frame: some archs (i.e. i386) pass a frame to some.
1145 * handlers as their main argument.
1147 * o 0: everything ok.
1148 * o EINVAL: stray interrupt.
1151 intr_event_handle(struct intr_event *ie, struct trapframe *frame)
1153 struct intr_handler *ih;
1154 struct trapframe *oldframe;
1160 #ifdef KSTACK_USAGE_PROF
1161 intr_prof_stack_use(td, frame);
1164 /* An interrupt with no event or handlers is a stray interrupt. */
1165 if (ie == NULL || CK_SLIST_EMPTY(&ie->ie_handlers))
1169 * Execute fast interrupt handlers directly.
1170 * To support clock handlers, if a handler registers
1171 * with a NULL argument, then we pass it a pointer to
1172 * a trapframe as its argument.
1174 td->td_intr_nesting_level++;
1178 oldframe = td->td_intr_frame;
1179 td->td_intr_frame = frame;
1181 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next) {
1182 if (ih->ih_filter == NULL) {
1186 CTR4(KTR_INTR, "%s: exec %p(%p) for %s", __func__,
1187 ih->ih_filter, ih->ih_argument == NULL ? frame :
1188 ih->ih_argument, ih->ih_name);
1189 if (ih->ih_argument == NULL)
1190 ret = ih->ih_filter(frame);
1192 ret = ih->ih_filter(ih->ih_argument);
1193 KASSERT(ret == FILTER_STRAY ||
1194 ((ret & (FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) != 0 &&
1195 (ret & ~(FILTER_SCHEDULE_THREAD | FILTER_HANDLED)) == 0),
1196 ("%s: incorrect return value %#x from %s", __func__, ret,
1200 * Wrapper handler special handling:
1202 * in some particular cases (like pccard and pccbb),
1203 * the _real_ device handler is wrapped in a couple of
1204 * functions - a filter wrapper and an ithread wrapper.
1205 * In this case (and just in this case), the filter wrapper
1206 * could ask the system to schedule the ithread and mask
1207 * the interrupt source if the wrapped handler is composed
1208 * of just an ithread handler.
1210 * TODO: write a generic wrapper to avoid people rolling
1214 if (ret == FILTER_SCHEDULE_THREAD)
1218 td->td_intr_frame = oldframe;
1221 if (ie->ie_pre_ithread != NULL)
1222 ie->ie_pre_ithread(ie->ie_source);
1224 if (ie->ie_post_filter != NULL)
1225 ie->ie_post_filter(ie->ie_source);
1228 /* Schedule the ithread if needed. */
1232 error = intr_event_schedule_thread(ie);
1233 KASSERT(error == 0, ("bad stray interrupt"));
1236 td->td_intr_nesting_level--;
1242 * Dump details about an interrupt handler
1245 db_dump_intrhand(struct intr_handler *ih)
1249 db_printf("\t%-10s ", ih->ih_name);
1250 switch (ih->ih_pri) {
1270 if (ih->ih_pri >= PI_SOFT)
1273 db_printf("%4u", ih->ih_pri);
1277 if (ih->ih_filter != NULL) {
1279 db_printsym((uintptr_t)ih->ih_filter, DB_STGY_PROC);
1281 if (ih->ih_handler != NULL) {
1282 if (ih->ih_filter != NULL)
1285 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC);
1287 db_printf("(%p)", ih->ih_argument);
1289 (ih->ih_flags & (IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD |
1293 if (ih->ih_flags & IH_EXCLUSIVE) {
1299 if (ih->ih_flags & IH_ENTROPY) {
1302 db_printf("ENTROPY");
1305 if (ih->ih_flags & IH_DEAD) {
1311 if (ih->ih_flags & IH_MPSAFE) {
1314 db_printf("MPSAFE");
1328 * Dump details about a event.
1331 db_dump_intr_event(struct intr_event *ie, int handlers)
1333 struct intr_handler *ih;
1334 struct intr_thread *it;
1337 db_printf("%s ", ie->ie_fullname);
1340 db_printf("(pid %d)", it->it_thread->td_proc->p_pid);
1342 db_printf("(no thread)");
1343 if ((ie->ie_flags & (IE_SOFT | IE_ENTROPY | IE_ADDING_THREAD)) != 0 ||
1344 (it != NULL && it->it_need)) {
1347 if (ie->ie_flags & IE_SOFT) {
1351 if (ie->ie_flags & IE_ENTROPY) {
1354 db_printf("ENTROPY");
1357 if (ie->ie_flags & IE_ADDING_THREAD) {
1360 db_printf("ADDING_THREAD");
1363 if (it != NULL && it->it_need) {
1373 CK_SLIST_FOREACH(ih, &ie->ie_handlers, ih_next)
1374 db_dump_intrhand(ih);
1378 * Dump data about interrupt handlers
1380 DB_SHOW_COMMAND(intr, db_show_intr)
1382 struct intr_event *ie;
1385 verbose = strchr(modif, 'v') != NULL;
1386 all = strchr(modif, 'a') != NULL;
1387 TAILQ_FOREACH(ie, &event_list, ie_list) {
1388 if (!all && CK_SLIST_EMPTY(&ie->ie_handlers))
1390 db_dump_intr_event(ie, verbose);
1398 * Start standard software interrupt threads
1401 start_softintr(void *dummy)
1404 if (swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih))
1405 panic("died while creating vm swi ithread");
1407 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr,
1411 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt.
1412 * The data for this machine dependent, and the declarations are in machine
1413 * dependent code. The layout of intrnames and intrcnt however is machine
1416 * We do not know the length of intrcnt and intrnames at compile time, so
1417 * calculate things at run time.
1420 sysctl_intrnames(SYSCTL_HANDLER_ARGS)
1422 return (sysctl_handle_opaque(oidp, intrnames, sintrnames, req));
1425 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD,
1426 NULL, 0, sysctl_intrnames, "", "Interrupt Names");
1429 sysctl_intrcnt(SYSCTL_HANDLER_ARGS)
1432 uint32_t *intrcnt32;
1436 if (req->flags & SCTL_MASK32) {
1438 return (sysctl_handle_opaque(oidp, NULL, sintrcnt / 2, req));
1439 intrcnt32 = malloc(sintrcnt / 2, M_TEMP, M_NOWAIT);
1440 if (intrcnt32 == NULL)
1442 for (i = 0; i < sintrcnt / sizeof (u_long); i++)
1443 intrcnt32[i] = intrcnt[i];
1444 error = sysctl_handle_opaque(oidp, intrcnt32, sintrcnt / 2, req);
1445 free(intrcnt32, M_TEMP);
1449 return (sysctl_handle_opaque(oidp, intrcnt, sintrcnt, req));
1452 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD,
1453 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts");
1457 * DDB command to dump the interrupt statistics.
1459 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt)
1467 for (i = intrcnt; j < (sintrcnt / sizeof(u_long)) && !db_pager_quit;
1472 db_printf("%s\t%lu\n", cp, *i);
1473 cp += strlen(cp) + 1;