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