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