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