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