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