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/rtprio.h> 36 #include <sys/systm.h> 37 #include <sys/interrupt.h> 38 #include <sys/kernel.h> 39 #include <sys/kthread.h> 40 #include <sys/ktr.h> 41 #include <sys/lock.h> 42 #include <sys/malloc.h> 43 #include <sys/mutex.h> 44 #include <sys/proc.h> 45 #include <sys/random.h> 46 #include <sys/resourcevar.h> 47 #include <sys/sysctl.h> 48 #include <sys/unistd.h> 49 #include <sys/vmmeter.h> 50 #include <machine/atomic.h> 51 #include <machine/cpu.h> 52 #include <machine/md_var.h> 53 #include <machine/stdarg.h> 54 #ifdef DDB 55 #include <ddb/ddb.h> 56 #include <ddb/db_sym.h> 57 #endif 58 59 struct int_entropy { 60 struct proc *proc; 61 uintptr_t vector; 62 }; 63 64 void *vm_ih; 65 void *softclock_ih; 66 struct ithd *clk_ithd; 67 struct ithd *tty_ithd; 68 69 static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads"); 70 71 static void ithread_update(struct ithd *); 72 static void ithread_loop(void *); 73 static void start_softintr(void *); 74 75 u_char 76 ithread_priority(enum intr_type flags) 77 { 78 u_char pri; 79 80 flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET | 81 INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK | INTR_TYPE_AV); 82 switch (flags) { 83 case INTR_TYPE_TTY: 84 pri = PI_TTYLOW; 85 break; 86 case INTR_TYPE_BIO: 87 /* 88 * XXX We need to refine this. BSD/OS distinguishes 89 * between tape and disk priorities. 90 */ 91 pri = PI_DISK; 92 break; 93 case INTR_TYPE_NET: 94 pri = PI_NET; 95 break; 96 case INTR_TYPE_CAM: 97 pri = PI_DISK; /* XXX or PI_CAM? */ 98 break; 99 case INTR_TYPE_AV: /* Audio/video */ 100 pri = PI_AV; 101 break; 102 case INTR_TYPE_CLK: 103 pri = PI_REALTIME; 104 break; 105 case INTR_TYPE_MISC: 106 pri = PI_DULL; /* don't care */ 107 break; 108 default: 109 /* We didn't specify an interrupt level. */ 110 panic("ithread_priority: no interrupt type in flags"); 111 } 112 113 return pri; 114 } 115 116 /* 117 * Regenerate the name (p_comm) and priority for a threaded interrupt thread. 118 */ 119 static void 120 ithread_update(struct ithd *ithd) 121 { 122 struct intrhand *ih; 123 struct thread *td; 124 struct proc *p; 125 int entropy; 126 127 mtx_assert(&ithd->it_lock, MA_OWNED); 128 td = ithd->it_td; 129 if (td == NULL) 130 return; 131 p = td->td_proc; 132 133 strlcpy(p->p_comm, ithd->it_name, sizeof(p->p_comm)); 134 135 ih = TAILQ_FIRST(&ithd->it_handlers); 136 if (ih == NULL) { 137 mtx_lock_spin(&sched_lock); 138 td->td_priority = PRI_MAX_ITHD; 139 td->td_base_pri = PRI_MAX_ITHD; 140 mtx_unlock_spin(&sched_lock); 141 ithd->it_flags &= ~IT_ENTROPY; 142 return; 143 } 144 entropy = 0; 145 mtx_lock_spin(&sched_lock); 146 td->td_priority = ih->ih_pri; 147 td->td_base_pri = ih->ih_pri; 148 mtx_unlock_spin(&sched_lock); 149 TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 150 if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 < 151 sizeof(p->p_comm)) { 152 strcat(p->p_comm, " "); 153 strcat(p->p_comm, ih->ih_name); 154 } else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) { 155 if (p->p_comm[sizeof(p->p_comm) - 2] == '+') 156 p->p_comm[sizeof(p->p_comm) - 2] = '*'; 157 else 158 p->p_comm[sizeof(p->p_comm) - 2] = '+'; 159 } else 160 strcat(p->p_comm, "+"); 161 if (ih->ih_flags & IH_ENTROPY) 162 entropy++; 163 } 164 if (entropy) 165 ithd->it_flags |= IT_ENTROPY; 166 else 167 ithd->it_flags &= ~IT_ENTROPY; 168 CTR2(KTR_INTR, "%s: updated %s", __func__, p->p_comm); 169 } 170 171 int 172 ithread_create(struct ithd **ithread, uintptr_t vector, int flags, 173 void (*disable)(uintptr_t), void (*enable)(uintptr_t), const char *fmt, ...) 174 { 175 struct ithd *ithd; 176 struct thread *td; 177 struct proc *p; 178 int error; 179 va_list ap; 180 181 /* The only valid flag during creation is IT_SOFT. */ 182 if ((flags & ~IT_SOFT) != 0) 183 return (EINVAL); 184 185 ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO); 186 ithd->it_vector = vector; 187 ithd->it_disable = disable; 188 ithd->it_enable = enable; 189 ithd->it_flags = flags; 190 TAILQ_INIT(&ithd->it_handlers); 191 mtx_init(&ithd->it_lock, "ithread", NULL, MTX_DEF); 192 193 va_start(ap, fmt); 194 vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap); 195 va_end(ap); 196 197 error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID, 198 0, "%s", ithd->it_name); 199 if (error) { 200 mtx_destroy(&ithd->it_lock); 201 free(ithd, M_ITHREAD); 202 return (error); 203 } 204 td = FIRST_THREAD_IN_PROC(p); /* XXXKSE */ 205 mtx_lock_spin(&sched_lock); 206 td->td_ksegrp->kg_pri_class = PRI_ITHD; 207 td->td_priority = PRI_MAX_ITHD; 208 TD_SET_IWAIT(td); 209 mtx_unlock_spin(&sched_lock); 210 ithd->it_td = td; 211 td->td_ithd = ithd; 212 if (ithread != NULL) 213 *ithread = ithd; 214 CTR2(KTR_INTR, "%s: created %s", __func__, ithd->it_name); 215 return (0); 216 } 217 218 int 219 ithread_destroy(struct ithd *ithread) 220 { 221 222 struct thread *td; 223 if (ithread == NULL) 224 return (EINVAL); 225 226 td = ithread->it_td; 227 mtx_lock(&ithread->it_lock); 228 if (!TAILQ_EMPTY(&ithread->it_handlers)) { 229 mtx_unlock(&ithread->it_lock); 230 return (EINVAL); 231 } 232 ithread->it_flags |= IT_DEAD; 233 mtx_lock_spin(&sched_lock); 234 if (TD_AWAITING_INTR(td)) { 235 TD_CLR_IWAIT(td); 236 setrunqueue(td); 237 } 238 mtx_unlock_spin(&sched_lock); 239 mtx_unlock(&ithread->it_lock); 240 CTR2(KTR_INTR, "%s: killing %s", __func__, ithread->it_name); 241 return (0); 242 } 243 244 int 245 ithread_add_handler(struct ithd* ithread, const char *name, 246 driver_intr_t handler, void *arg, u_char pri, enum intr_type flags, 247 void **cookiep) 248 { 249 struct intrhand *ih, *temp_ih; 250 251 if (ithread == NULL || name == NULL || handler == NULL) 252 return (EINVAL); 253 254 ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO); 255 ih->ih_handler = handler; 256 ih->ih_argument = arg; 257 ih->ih_name = name; 258 ih->ih_ithread = ithread; 259 ih->ih_pri = pri; 260 if (flags & INTR_FAST) 261 ih->ih_flags = IH_FAST; 262 else if (flags & INTR_EXCL) 263 ih->ih_flags = IH_EXCLUSIVE; 264 if (flags & INTR_MPSAFE) 265 ih->ih_flags |= IH_MPSAFE; 266 if (flags & INTR_ENTROPY) 267 ih->ih_flags |= IH_ENTROPY; 268 269 mtx_lock(&ithread->it_lock); 270 if ((flags & INTR_EXCL) != 0 && !TAILQ_EMPTY(&ithread->it_handlers)) 271 goto fail; 272 if (!TAILQ_EMPTY(&ithread->it_handlers)) { 273 temp_ih = TAILQ_FIRST(&ithread->it_handlers); 274 if (temp_ih->ih_flags & IH_EXCLUSIVE) 275 goto fail; 276 if ((ih->ih_flags & IH_FAST) && !(temp_ih->ih_flags & IH_FAST)) 277 goto fail; 278 if (!(ih->ih_flags & IH_FAST) && (temp_ih->ih_flags & IH_FAST)) 279 goto fail; 280 } 281 282 TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next) 283 if (temp_ih->ih_pri > ih->ih_pri) 284 break; 285 if (temp_ih == NULL) 286 TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next); 287 else 288 TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next); 289 ithread_update(ithread); 290 mtx_unlock(&ithread->it_lock); 291 292 if (cookiep != NULL) 293 *cookiep = ih; 294 CTR3(KTR_INTR, "%s: added %s to %s", __func__, ih->ih_name, 295 ithread->it_name); 296 return (0); 297 298 fail: 299 mtx_unlock(&ithread->it_lock); 300 free(ih, M_ITHREAD); 301 return (EINVAL); 302 } 303 304 int 305 ithread_remove_handler(void *cookie) 306 { 307 struct intrhand *handler = (struct intrhand *)cookie; 308 struct ithd *ithread; 309 #ifdef INVARIANTS 310 struct intrhand *ih; 311 #endif 312 313 if (handler == NULL) 314 return (EINVAL); 315 ithread = handler->ih_ithread; 316 KASSERT(ithread != NULL, 317 ("interrupt handler \"%s\" has a NULL interrupt thread", 318 handler->ih_name)); 319 CTR3(KTR_INTR, "%s: removing %s from %s", __func__, handler->ih_name, 320 ithread->it_name); 321 mtx_lock(&ithread->it_lock); 322 #ifdef INVARIANTS 323 TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next) 324 if (ih == handler) 325 goto ok; 326 mtx_unlock(&ithread->it_lock); 327 panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"", 328 ih->ih_name, ithread->it_name); 329 ok: 330 #endif 331 /* 332 * If the interrupt thread is already running, then just mark this 333 * handler as being dead and let the ithread do the actual removal. 334 * 335 * During a cold boot while cold is set, msleep() does not sleep, 336 * so we have to remove the handler here rather than letting the 337 * thread do it. 338 */ 339 mtx_lock_spin(&sched_lock); 340 if (!TD_AWAITING_INTR(ithread->it_td) && !cold) { 341 handler->ih_flags |= IH_DEAD; 342 343 /* 344 * Ensure that the thread will process the handler list 345 * again and remove this handler if it has already passed 346 * it on the list. 347 */ 348 ithread->it_need = 1; 349 } else 350 TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next); 351 mtx_unlock_spin(&sched_lock); 352 if ((handler->ih_flags & IH_DEAD) != 0) 353 msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0); 354 ithread_update(ithread); 355 mtx_unlock(&ithread->it_lock); 356 free(handler, M_ITHREAD); 357 return (0); 358 } 359 360 int 361 ithread_schedule(struct ithd *ithread, int do_switch) 362 { 363 struct int_entropy entropy; 364 struct thread *td; 365 struct thread *ctd; 366 struct proc *p; 367 368 /* 369 * If no ithread or no handlers, then we have a stray interrupt. 370 */ 371 if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers)) 372 return (EINVAL); 373 374 ctd = curthread; 375 /* 376 * If any of the handlers for this ithread claim to be good 377 * sources of entropy, then gather some. 378 */ 379 if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) { 380 entropy.vector = ithread->it_vector; 381 entropy.proc = ctd->td_proc; 382 random_harvest(&entropy, sizeof(entropy), 2, 0, 383 RANDOM_INTERRUPT); 384 } 385 386 td = ithread->it_td; 387 p = td->td_proc; 388 KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name)); 389 CTR4(KTR_INTR, "%s: pid %d: (%s) need = %d", 390 __func__, p->p_pid, p->p_comm, ithread->it_need); 391 392 /* 393 * Set it_need to tell the thread to keep running if it is already 394 * running. Then, grab sched_lock and see if we actually need to 395 * put this thread on the runqueue. If so and the do_switch flag is 396 * true and it is safe to switch, then switch to the ithread 397 * immediately. Otherwise, set the needresched flag to guarantee 398 * that this ithread will run before any userland processes. 399 */ 400 ithread->it_need = 1; 401 mtx_lock_spin(&sched_lock); 402 if (TD_AWAITING_INTR(td)) { 403 CTR2(KTR_INTR, "%s: setrunqueue %d", __func__, p->p_pid); 404 TD_CLR_IWAIT(td); 405 setrunqueue(td); 406 if (do_switch && 407 (ctd->td_critnest == 1) ) { 408 KASSERT((TD_IS_RUNNING(ctd)), 409 ("ithread_schedule: Bad state for curthread.")); 410 if (ctd->td_flags & TDF_IDLETD) 411 ctd->td_state = TDS_CAN_RUN; /* XXXKSE */ 412 mi_switch(SW_INVOL); 413 } else { 414 curthread->td_flags |= TDF_NEEDRESCHED; 415 } 416 } else { 417 CTR4(KTR_INTR, "%s: pid %d: it_need %d, state %d", 418 __func__, p->p_pid, ithread->it_need, td->td_state); 419 } 420 mtx_unlock_spin(&sched_lock); 421 422 return (0); 423 } 424 425 int 426 swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler, 427 void *arg, int pri, enum intr_type flags, void **cookiep) 428 { 429 struct ithd *ithd; 430 int error; 431 432 if (flags & (INTR_FAST | INTR_ENTROPY)) 433 return (EINVAL); 434 435 ithd = (ithdp != NULL) ? *ithdp : NULL; 436 437 if (ithd != NULL) { 438 if ((ithd->it_flags & IT_SOFT) == 0) 439 return(EINVAL); 440 } else { 441 error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL, 442 "swi%d:", pri); 443 if (error) 444 return (error); 445 446 if (ithdp != NULL) 447 *ithdp = ithd; 448 } 449 return (ithread_add_handler(ithd, name, handler, arg, 450 (pri * RQ_PPQ) + PI_SOFT, flags, cookiep)); 451 } 452 453 454 /* 455 * Schedule a heavyweight software interrupt process. 456 */ 457 void 458 swi_sched(void *cookie, int flags) 459 { 460 struct intrhand *ih = (struct intrhand *)cookie; 461 struct ithd *it = ih->ih_ithread; 462 int error; 463 464 atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */ 465 466 CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d", 467 it->it_td->td_proc->p_pid, it->it_td->td_proc->p_comm, it->it_need); 468 469 /* 470 * Set ih_need for this handler so that if the ithread is already 471 * running it will execute this handler on the next pass. Otherwise, 472 * it will execute it the next time it runs. 473 */ 474 atomic_store_rel_int(&ih->ih_need, 1); 475 if (!(flags & SWI_DELAY)) { 476 error = ithread_schedule(it, !cold && !dumping); 477 KASSERT(error == 0, ("stray software interrupt")); 478 } 479 } 480 481 /* 482 * This is the main code for interrupt threads. 483 */ 484 static void 485 ithread_loop(void *arg) 486 { 487 struct ithd *ithd; /* our thread context */ 488 struct intrhand *ih; /* and our interrupt handler chain */ 489 struct thread *td; 490 struct proc *p; 491 492 td = curthread; 493 p = td->td_proc; 494 ithd = (struct ithd *)arg; /* point to myself */ 495 KASSERT(ithd->it_td == td && td->td_ithd == ithd, 496 ("%s: ithread and proc linkage out of sync", __func__)); 497 498 /* 499 * As long as we have interrupts outstanding, go through the 500 * list of handlers, giving each one a go at it. 501 */ 502 for (;;) { 503 /* 504 * If we are an orphaned thread, then just die. 505 */ 506 if (ithd->it_flags & IT_DEAD) { 507 CTR3(KTR_INTR, "%s: pid %d: (%s) exiting", __func__, 508 p->p_pid, p->p_comm); 509 td->td_ithd = NULL; 510 mtx_destroy(&ithd->it_lock); 511 mtx_lock(&Giant); 512 free(ithd, M_ITHREAD); 513 kthread_exit(0); 514 } 515 516 CTR4(KTR_INTR, "%s: pid %d: (%s) need=%d", __func__, 517 p->p_pid, p->p_comm, ithd->it_need); 518 while (ithd->it_need) { 519 /* 520 * Service interrupts. If another interrupt 521 * arrives while we are running, they will set 522 * it_need to denote that we should make 523 * another pass. 524 */ 525 atomic_store_rel_int(&ithd->it_need, 0); 526 restart: 527 TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) { 528 if (ithd->it_flags & IT_SOFT && !ih->ih_need) 529 continue; 530 atomic_store_rel_int(&ih->ih_need, 0); 531 CTR6(KTR_INTR, 532 "%s: pid %d ih=%p: %p(%p) flg=%x", __func__, 533 p->p_pid, (void *)ih, 534 (void *)ih->ih_handler, ih->ih_argument, 535 ih->ih_flags); 536 537 if ((ih->ih_flags & IH_DEAD) != 0) { 538 mtx_lock(&ithd->it_lock); 539 TAILQ_REMOVE(&ithd->it_handlers, ih, 540 ih_next); 541 wakeup(ih); 542 mtx_unlock(&ithd->it_lock); 543 goto restart; 544 } 545 if ((ih->ih_flags & IH_MPSAFE) == 0) 546 mtx_lock(&Giant); 547 ih->ih_handler(ih->ih_argument); 548 if ((ih->ih_flags & IH_MPSAFE) == 0) 549 mtx_unlock(&Giant); 550 } 551 } 552 553 /* 554 * Processed all our interrupts. Now get the sched 555 * lock. This may take a while and it_need may get 556 * set again, so we have to check it again. 557 */ 558 WITNESS_WARN(WARN_PANIC, NULL, "suspending ithread"); 559 mtx_assert(&Giant, MA_NOTOWNED); 560 mtx_lock_spin(&sched_lock); 561 if (!ithd->it_need) { 562 /* 563 * Should we call this earlier in the loop above? 564 */ 565 if (ithd->it_enable != NULL) 566 ithd->it_enable(ithd->it_vector); 567 TD_SET_IWAIT(td); /* we're idle */ 568 CTR2(KTR_INTR, "%s: pid %d: done", __func__, p->p_pid); 569 mi_switch(SW_VOL); 570 CTR2(KTR_INTR, "%s: pid %d: resumed", __func__, p->p_pid); 571 } 572 mtx_unlock_spin(&sched_lock); 573 } 574 } 575 576 #ifdef DDB 577 /* 578 * Dump details about an interrupt handler 579 */ 580 static void 581 db_dump_intrhand(struct intrhand *ih) 582 { 583 int comma; 584 585 db_printf("\t%-10s ", ih->ih_name); 586 switch (ih->ih_pri) { 587 case PI_REALTIME: 588 db_printf("CLK "); 589 break; 590 case PI_AV: 591 db_printf("AV "); 592 break; 593 case PI_TTYHIGH: 594 case PI_TTYLOW: 595 db_printf("TTY "); 596 break; 597 case PI_TAPE: 598 db_printf("TAPE"); 599 break; 600 case PI_NET: 601 db_printf("NET "); 602 break; 603 case PI_DISK: 604 case PI_DISKLOW: 605 db_printf("DISK"); 606 break; 607 case PI_DULL: 608 db_printf("DULL"); 609 break; 610 default: 611 if (ih->ih_pri >= PI_SOFT) 612 db_printf("SWI "); 613 else 614 db_printf("%4u", ih->ih_pri); 615 break; 616 } 617 db_printf(" "); 618 db_printsym((uintptr_t)ih->ih_handler, DB_STGY_PROC); 619 db_printf("(%p)", ih->ih_argument); 620 if (ih->ih_need || 621 (ih->ih_flags & (IH_FAST | IH_EXCLUSIVE | IH_ENTROPY | IH_DEAD | 622 IH_MPSAFE)) != 0) { 623 db_printf(" {"); 624 comma = 0; 625 if (ih->ih_flags & IH_FAST) { 626 db_printf("FAST"); 627 comma = 1; 628 } 629 if (ih->ih_flags & IH_EXCLUSIVE) { 630 if (comma) 631 db_printf(", "); 632 db_printf("EXCL"); 633 comma = 1; 634 } 635 if (ih->ih_flags & IH_ENTROPY) { 636 if (comma) 637 db_printf(", "); 638 db_printf("ENTROPY"); 639 comma = 1; 640 } 641 if (ih->ih_flags & IH_DEAD) { 642 if (comma) 643 db_printf(", "); 644 db_printf("DEAD"); 645 comma = 1; 646 } 647 if (ih->ih_flags & IH_MPSAFE) { 648 if (comma) 649 db_printf(", "); 650 db_printf("MPSAFE"); 651 comma = 1; 652 } 653 if (ih->ih_need) { 654 if (comma) 655 db_printf(", "); 656 db_printf("NEED"); 657 } 658 db_printf("}"); 659 } 660 db_printf("\n"); 661 } 662 663 /* 664 * Dump details about an ithread 665 */ 666 void 667 db_dump_ithread(struct ithd *ithd, int handlers) 668 { 669 struct proc *p; 670 struct intrhand *ih; 671 int comma; 672 673 if (ithd->it_td != NULL) { 674 p = ithd->it_td->td_proc; 675 db_printf("%s (pid %d)", p->p_comm, p->p_pid); 676 } else 677 db_printf("%s: (no thread)", ithd->it_name); 678 if ((ithd->it_flags & (IT_SOFT | IT_ENTROPY | IT_DEAD)) != 0 || 679 ithd->it_need) { 680 db_printf(" {"); 681 comma = 0; 682 if (ithd->it_flags & IT_SOFT) { 683 db_printf("SOFT"); 684 comma = 1; 685 } 686 if (ithd->it_flags & IT_ENTROPY) { 687 if (comma) 688 db_printf(", "); 689 db_printf("ENTROPY"); 690 comma = 1; 691 } 692 if (ithd->it_flags & IT_DEAD) { 693 if (comma) 694 db_printf(", "); 695 db_printf("DEAD"); 696 comma = 1; 697 } 698 if (ithd->it_need) { 699 if (comma) 700 db_printf(", "); 701 db_printf("NEED"); 702 } 703 db_printf("}"); 704 } 705 db_printf("\n"); 706 707 if (handlers) 708 TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) 709 db_dump_intrhand(ih); 710 } 711 #endif /* DDB */ 712 713 /* 714 * Start standard software interrupt threads 715 */ 716 static void 717 start_softintr(void *dummy) 718 { 719 struct proc *p; 720 721 if (swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK, 722 INTR_MPSAFE, &softclock_ih) || 723 swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, INTR_MPSAFE, &vm_ih)) 724 panic("died while creating standard software ithreads"); 725 726 p = clk_ithd->it_td->td_proc; 727 PROC_LOCK(p); 728 p->p_flag |= P_NOLOAD; 729 PROC_UNLOCK(p); 730 } 731 SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL) 732 733 /* 734 * Sysctls used by systat and others: hw.intrnames and hw.intrcnt. 735 * The data for this machine dependent, and the declarations are in machine 736 * dependent code. The layout of intrnames and intrcnt however is machine 737 * independent. 738 * 739 * We do not know the length of intrcnt and intrnames at compile time, so 740 * calculate things at run time. 741 */ 742 static int 743 sysctl_intrnames(SYSCTL_HANDLER_ARGS) 744 { 745 return (sysctl_handle_opaque(oidp, intrnames, eintrnames - intrnames, 746 req)); 747 } 748 749 SYSCTL_PROC(_hw, OID_AUTO, intrnames, CTLTYPE_OPAQUE | CTLFLAG_RD, 750 NULL, 0, sysctl_intrnames, "", "Interrupt Names"); 751 752 static int 753 sysctl_intrcnt(SYSCTL_HANDLER_ARGS) 754 { 755 return (sysctl_handle_opaque(oidp, intrcnt, 756 (char *)eintrcnt - (char *)intrcnt, req)); 757 } 758 759 SYSCTL_PROC(_hw, OID_AUTO, intrcnt, CTLTYPE_OPAQUE | CTLFLAG_RD, 760 NULL, 0, sysctl_intrcnt, "", "Interrupt Counts"); 761 762 #ifdef DDB 763 /* 764 * DDB command to dump the interrupt statistics. 765 */ 766 DB_SHOW_COMMAND(intrcnt, db_show_intrcnt) 767 { 768 u_long *i; 769 char *cp; 770 int quit; 771 772 cp = intrnames; 773 db_setup_paging(db_simple_pager, &quit, DB_LINES_PER_PAGE); 774 for (i = intrcnt, quit = 0; i != eintrcnt && !quit; i++) { 775 if (*cp == '\0') 776 break; 777 if (*i != 0) 778 db_printf("%s\t%lu\n", cp, *i); 779 cp += strlen(cp) + 1; 780 } 781 } 782 #endif 783