1 /* $NetBSD: kern_softint.c,v 1.40 2013/09/07 03:34:59 matt Exp $ */ 2 3 /*- 4 * Copyright (c) 2007, 2008 The NetBSD Foundation, Inc. 5 * All rights reserved. 6 * 7 * This code is derived from software contributed to The NetBSD Foundation 8 * by Andrew Doran. 9 * 10 * Redistribution and use in source and binary forms, with or without 11 * modification, are permitted provided that the following conditions 12 * are met: 13 * 1. Redistributions of source code must retain the above copyright 14 * notice, this list of conditions and the following disclaimer. 15 * 2. Redistributions in binary form must reproduce the above copyright 16 * notice, this list of conditions and the following disclaimer in the 17 * documentation and/or other materials provided with the distribution. 18 * 19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 29 * POSSIBILITY OF SUCH DAMAGE. 30 */ 31 32 /* 33 * Generic software interrupt framework. 34 * 35 * Overview 36 * 37 * The soft interrupt framework provides a mechanism to schedule a 38 * low priority callback that runs with thread context. It allows 39 * for dynamic registration of software interrupts, and for fair 40 * queueing and prioritization of those interrupts. The callbacks 41 * can be scheduled to run from nearly any point in the kernel: by 42 * code running with thread context, by code running from a 43 * hardware interrupt handler, and at any interrupt priority 44 * level. 45 * 46 * Priority levels 47 * 48 * Since soft interrupt dispatch can be tied to the underlying 49 * architecture's interrupt dispatch code, it can be limited 50 * both by the capabilities of the hardware and the capabilities 51 * of the interrupt dispatch code itself. The number of priority 52 * levels is restricted to four. In order of priority (lowest to 53 * highest) the levels are: clock, bio, net, serial. 54 * 55 * The names are symbolic and in isolation do not have any direct 56 * connection with a particular kind of device activity: they are 57 * only meant as a guide. 58 * 59 * The four priority levels map directly to scheduler priority 60 * levels, and where the architecture implements 'fast' software 61 * interrupts, they also map onto interrupt priorities. The 62 * interrupt priorities are intended to be hidden from machine 63 * independent code, which should use thread-safe mechanisms to 64 * synchronize with software interrupts (for example: mutexes). 65 * 66 * Capabilities 67 * 68 * Software interrupts run with limited machine context. In 69 * particular, they do not posess any address space context. They 70 * should not try to operate on user space addresses, or to use 71 * virtual memory facilities other than those noted as interrupt 72 * safe. 73 * 74 * Unlike hardware interrupts, software interrupts do have thread 75 * context. They may block on synchronization objects, sleep, and 76 * resume execution at a later time. 77 * 78 * Since software interrupts are a limited resource and run with 79 * higher priority than most other LWPs in the system, all 80 * block-and-resume activity by a software interrupt must be kept 81 * short to allow futher processing at that level to continue. By 82 * extension, code running with process context must take care to 83 * ensure that any lock that may be taken from a software interrupt 84 * can not be held for more than a short period of time. 85 * 86 * The kernel does not allow software interrupts to use facilities 87 * or perform actions that may block for a significant amount of 88 * time. This means that it's not valid for a software interrupt 89 * to sleep on condition variables or wait for resources to become 90 * available (for example, memory). 91 * 92 * Per-CPU operation 93 * 94 * If a soft interrupt is triggered on a CPU, it can only be 95 * dispatched on the same CPU. Each LWP dedicated to handling a 96 * soft interrupt is bound to its home CPU, so if the LWP blocks 97 * and needs to run again, it can only run there. Nearly all data 98 * structures used to manage software interrupts are per-CPU. 99 * 100 * The per-CPU requirement is intended to reduce "ping-pong" of 101 * cache lines between CPUs: lines occupied by data structures 102 * used to manage the soft interrupts, and lines occupied by data 103 * items being passed down to the soft interrupt. As a positive 104 * side effect, this also means that the soft interrupt dispatch 105 * code does not need to to use spinlocks to synchronize. 106 * 107 * Generic implementation 108 * 109 * A generic, low performance implementation is provided that 110 * works across all architectures, with no machine-dependent 111 * modifications needed. This implementation uses the scheduler, 112 * and so has a number of restrictions: 113 * 114 * 1) The software interrupts are not currently preemptive, so 115 * must wait for the currently executing LWP to yield the CPU. 116 * This can introduce latency. 117 * 118 * 2) An expensive context switch is required for a software 119 * interrupt to be handled. 120 * 121 * 'Fast' software interrupts 122 * 123 * If an architectures defines __HAVE_FAST_SOFTINTS, it implements 124 * the fast mechanism. Threads running either in the kernel or in 125 * userspace will be interrupted, but will not be preempted. When 126 * the soft interrupt completes execution, the interrupted LWP 127 * is resumed. Interrupt dispatch code must provide the minimum 128 * level of context necessary for the soft interrupt to block and 129 * be resumed at a later time. The machine-dependent dispatch 130 * path looks something like the following: 131 * 132 * softintr() 133 * { 134 * go to IPL_HIGH if necessary for switch; 135 * save any necessary registers in a format that can be 136 * restored by cpu_switchto if the softint blocks; 137 * arrange for cpu_switchto() to restore into the 138 * trampoline function; 139 * identify LWP to handle this interrupt; 140 * switch to the LWP's stack; 141 * switch register stacks, if necessary; 142 * assign new value of curlwp; 143 * call MI softint_dispatch, passing old curlwp and IPL 144 * to execute interrupt at; 145 * switch back to old stack; 146 * switch back to old register stack, if necessary; 147 * restore curlwp; 148 * return to interrupted LWP; 149 * } 150 * 151 * If the soft interrupt blocks, a trampoline function is returned 152 * to in the context of the interrupted LWP, as arranged for by 153 * softint(): 154 * 155 * softint_ret() 156 * { 157 * unlock soft interrupt LWP; 158 * resume interrupt processing, likely returning to 159 * interrupted LWP or dispatching another, different 160 * interrupt; 161 * } 162 * 163 * Once the soft interrupt has fired (and even if it has blocked), 164 * no further soft interrupts at that level will be triggered by 165 * MI code until the soft interrupt handler has ceased execution. 166 * If a soft interrupt handler blocks and is resumed, it resumes 167 * execution as a normal LWP (kthread) and gains VM context. Only 168 * when it has completed and is ready to fire again will it 169 * interrupt other threads. 170 * 171 * Future directions 172 * 173 * Provide a cheap way to direct software interrupts to remote 174 * CPUs. Provide a way to enqueue work items into the handler 175 * record, removing additional spl calls (see subr_workqueue.c). 176 */ 177 178 #include <sys/cdefs.h> 179 __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.40 2013/09/07 03:34:59 matt Exp $"); 180 181 #include <sys/param.h> 182 #include <sys/proc.h> 183 #include <sys/intr.h> 184 #include <sys/mutex.h> 185 #include <sys/kthread.h> 186 #include <sys/evcnt.h> 187 #include <sys/cpu.h> 188 #include <sys/xcall.h> 189 #include <sys/pserialize.h> 190 191 #include <net/netisr.h> 192 193 #include <uvm/uvm_extern.h> 194 195 /* This could overlap with signal info in struct lwp. */ 196 typedef struct softint { 197 SIMPLEQ_HEAD(, softhand) si_q; 198 struct lwp *si_lwp; 199 struct cpu_info *si_cpu; 200 uintptr_t si_machdep; 201 struct evcnt si_evcnt; 202 struct evcnt si_evcnt_block; 203 int si_active; 204 char si_name[8]; 205 char si_name_block[8+6]; 206 } softint_t; 207 208 typedef struct softhand { 209 SIMPLEQ_ENTRY(softhand) sh_q; 210 void (*sh_func)(void *); 211 void *sh_arg; 212 softint_t *sh_isr; 213 u_int sh_flags; 214 } softhand_t; 215 216 typedef struct softcpu { 217 struct cpu_info *sc_cpu; 218 softint_t sc_int[SOFTINT_COUNT]; 219 softhand_t sc_hand[1]; 220 } softcpu_t; 221 222 static void softint_thread(void *); 223 224 u_int softint_bytes = 8192; 225 u_int softint_timing; 226 static u_int softint_max; 227 static kmutex_t softint_lock; 228 static void *softint_netisrs[NETISR_MAX]; 229 230 /* 231 * softint_init_isr: 232 * 233 * Initialize a single interrupt level for a single CPU. 234 */ 235 static void 236 softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level) 237 { 238 struct cpu_info *ci; 239 softint_t *si; 240 int error; 241 242 si = &sc->sc_int[level]; 243 ci = sc->sc_cpu; 244 si->si_cpu = ci; 245 246 SIMPLEQ_INIT(&si->si_q); 247 248 error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR | 249 KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp, 250 "soft%s/%u", desc, ci->ci_index); 251 if (error != 0) 252 panic("softint_init_isr: error %d", error); 253 254 snprintf(si->si_name, sizeof(si->si_name), "%s/%u", desc, 255 ci->ci_index); 256 evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_MISC, NULL, 257 "softint", si->si_name); 258 snprintf(si->si_name_block, sizeof(si->si_name_block), "%s block/%u", 259 desc, ci->ci_index); 260 evcnt_attach_dynamic(&si->si_evcnt_block, EVCNT_TYPE_MISC, NULL, 261 "softint", si->si_name_block); 262 263 si->si_lwp->l_private = si; 264 softint_init_md(si->si_lwp, level, &si->si_machdep); 265 } 266 267 /* 268 * softint_init: 269 * 270 * Initialize per-CPU data structures. Called from mi_cpu_attach(). 271 */ 272 void 273 softint_init(struct cpu_info *ci) 274 { 275 static struct cpu_info *first; 276 softcpu_t *sc, *scfirst; 277 softhand_t *sh, *shmax; 278 279 if (first == NULL) { 280 /* Boot CPU. */ 281 first = ci; 282 mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE); 283 softint_bytes = round_page(softint_bytes); 284 softint_max = (softint_bytes - sizeof(softcpu_t)) / 285 sizeof(softhand_t); 286 } 287 288 /* Use uvm_km(9) for persistent, page-aligned allocation. */ 289 sc = (softcpu_t *)uvm_km_alloc(kernel_map, softint_bytes, 0, 290 UVM_KMF_WIRED | UVM_KMF_ZERO); 291 if (sc == NULL) 292 panic("softint_init_cpu: cannot allocate memory"); 293 294 ci->ci_data.cpu_softcpu = sc; 295 ci->ci_data.cpu_softints = 0; 296 sc->sc_cpu = ci; 297 298 softint_init_isr(sc, "net", PRI_SOFTNET, SOFTINT_NET); 299 softint_init_isr(sc, "bio", PRI_SOFTBIO, SOFTINT_BIO); 300 softint_init_isr(sc, "clk", PRI_SOFTCLOCK, SOFTINT_CLOCK); 301 softint_init_isr(sc, "ser", PRI_SOFTSERIAL, SOFTINT_SERIAL); 302 303 if (first != ci) { 304 mutex_enter(&softint_lock); 305 scfirst = first->ci_data.cpu_softcpu; 306 sh = sc->sc_hand; 307 memcpy(sh, scfirst->sc_hand, sizeof(*sh) * softint_max); 308 /* Update pointers for this CPU. */ 309 for (shmax = sh + softint_max; sh < shmax; sh++) { 310 if (sh->sh_func == NULL) 311 continue; 312 sh->sh_isr = 313 &sc->sc_int[sh->sh_flags & SOFTINT_LVLMASK]; 314 } 315 mutex_exit(&softint_lock); 316 } else { 317 /* 318 * Establish handlers for legacy net interrupts. 319 * XXX Needs to go away. 320 */ 321 #define DONETISR(n, f) \ 322 softint_netisrs[(n)] = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,\ 323 (void (*)(void *))(f), NULL) 324 #include <net/netisr_dispatch.h> 325 } 326 } 327 328 /* 329 * softint_establish: 330 * 331 * Register a software interrupt handler. 332 */ 333 void * 334 softint_establish(u_int flags, void (*func)(void *), void *arg) 335 { 336 CPU_INFO_ITERATOR cii; 337 struct cpu_info *ci; 338 softcpu_t *sc; 339 softhand_t *sh; 340 u_int level, index; 341 342 level = (flags & SOFTINT_LVLMASK); 343 KASSERT(level < SOFTINT_COUNT); 344 KASSERT((flags & SOFTINT_IMPMASK) == 0); 345 346 mutex_enter(&softint_lock); 347 348 /* Find a free slot. */ 349 sc = curcpu()->ci_data.cpu_softcpu; 350 for (index = 1; index < softint_max; index++) { 351 if (sc->sc_hand[index].sh_func == NULL) 352 break; 353 } 354 if (index == softint_max) { 355 mutex_exit(&softint_lock); 356 printf("WARNING: softint_establish: table full, " 357 "increase softint_bytes\n"); 358 return NULL; 359 } 360 361 /* Set up the handler on each CPU. */ 362 if (ncpu < 2) { 363 /* XXX hack for machines with no CPU_INFO_FOREACH() early on */ 364 sc = curcpu()->ci_data.cpu_softcpu; 365 sh = &sc->sc_hand[index]; 366 sh->sh_isr = &sc->sc_int[level]; 367 sh->sh_func = func; 368 sh->sh_arg = arg; 369 sh->sh_flags = flags; 370 } else for (CPU_INFO_FOREACH(cii, ci)) { 371 sc = ci->ci_data.cpu_softcpu; 372 sh = &sc->sc_hand[index]; 373 sh->sh_isr = &sc->sc_int[level]; 374 sh->sh_func = func; 375 sh->sh_arg = arg; 376 sh->sh_flags = flags; 377 } 378 379 mutex_exit(&softint_lock); 380 381 return (void *)((uint8_t *)&sc->sc_hand[index] - (uint8_t *)sc); 382 } 383 384 /* 385 * softint_disestablish: 386 * 387 * Unregister a software interrupt handler. The soft interrupt could 388 * still be active at this point, but the caller commits not to try 389 * and trigger it again once this call is made. The caller must not 390 * hold any locks that could be taken from soft interrupt context, 391 * because we will wait for the softint to complete if it's still 392 * running. 393 */ 394 void 395 softint_disestablish(void *arg) 396 { 397 CPU_INFO_ITERATOR cii; 398 struct cpu_info *ci; 399 softcpu_t *sc; 400 softhand_t *sh; 401 uintptr_t offset; 402 uint64_t where; 403 u_int flags; 404 405 offset = (uintptr_t)arg; 406 KASSERTMSG(offset != 0 && offset < softint_bytes, "%"PRIuPTR" %u", 407 offset, softint_bytes); 408 409 /* 410 * Run a cross call so we see up to date values of sh_flags from 411 * all CPUs. Once softint_disestablish() is called, the caller 412 * commits to not trigger the interrupt and set SOFTINT_ACTIVE on 413 * it again. So, we are only looking for handler records with 414 * SOFTINT_ACTIVE already set. 415 */ 416 where = xc_broadcast(0, (xcfunc_t)nullop, NULL, NULL); 417 xc_wait(where); 418 419 for (;;) { 420 /* Collect flag values from each CPU. */ 421 flags = 0; 422 for (CPU_INFO_FOREACH(cii, ci)) { 423 sc = ci->ci_data.cpu_softcpu; 424 sh = (softhand_t *)((uint8_t *)sc + offset); 425 KASSERT(sh->sh_func != NULL); 426 flags |= sh->sh_flags; 427 } 428 /* Inactive on all CPUs? */ 429 if ((flags & SOFTINT_ACTIVE) == 0) { 430 break; 431 } 432 /* Oops, still active. Wait for it to clear. */ 433 (void)kpause("softdis", false, 1, NULL); 434 } 435 436 /* Clear the handler on each CPU. */ 437 mutex_enter(&softint_lock); 438 for (CPU_INFO_FOREACH(cii, ci)) { 439 sc = ci->ci_data.cpu_softcpu; 440 sh = (softhand_t *)((uint8_t *)sc + offset); 441 KASSERT(sh->sh_func != NULL); 442 sh->sh_func = NULL; 443 } 444 mutex_exit(&softint_lock); 445 } 446 447 /* 448 * softint_schedule: 449 * 450 * Trigger a software interrupt. Must be called from a hardware 451 * interrupt handler, or with preemption disabled (since we are 452 * using the value of curcpu()). 453 */ 454 void 455 softint_schedule(void *arg) 456 { 457 softhand_t *sh; 458 softint_t *si; 459 uintptr_t offset; 460 int s; 461 462 KASSERT(kpreempt_disabled()); 463 464 /* Find the handler record for this CPU. */ 465 offset = (uintptr_t)arg; 466 KASSERTMSG(offset != 0 && offset < softint_bytes, "%"PRIuPTR" %u", 467 offset, softint_bytes); 468 sh = (softhand_t *)((uint8_t *)curcpu()->ci_data.cpu_softcpu + offset); 469 470 /* If it's already pending there's nothing to do. */ 471 if ((sh->sh_flags & SOFTINT_PENDING) != 0) { 472 return; 473 } 474 475 /* 476 * Enqueue the handler into the LWP's pending list. 477 * If the LWP is completely idle, then make it run. 478 */ 479 s = splhigh(); 480 if ((sh->sh_flags & SOFTINT_PENDING) == 0) { 481 si = sh->sh_isr; 482 sh->sh_flags |= SOFTINT_PENDING; 483 SIMPLEQ_INSERT_TAIL(&si->si_q, sh, sh_q); 484 if (si->si_active == 0) { 485 si->si_active = 1; 486 softint_trigger(si->si_machdep); 487 } 488 } 489 splx(s); 490 } 491 492 /* 493 * softint_execute: 494 * 495 * Invoke handlers for the specified soft interrupt. 496 * Must be entered at splhigh. Will drop the priority 497 * to the level specified, but returns back at splhigh. 498 */ 499 static inline void 500 softint_execute(softint_t *si, lwp_t *l, int s) 501 { 502 softhand_t *sh; 503 bool havelock; 504 505 #ifdef __HAVE_FAST_SOFTINTS 506 KASSERT(si->si_lwp == curlwp); 507 #else 508 /* May be running in user context. */ 509 #endif 510 KASSERT(si->si_cpu == curcpu()); 511 KASSERT(si->si_lwp->l_wchan == NULL); 512 KASSERT(si->si_active); 513 514 havelock = false; 515 516 /* 517 * Note: due to priority inheritance we may have interrupted a 518 * higher priority LWP. Since the soft interrupt must be quick 519 * and is non-preemptable, we don't bother yielding. 520 */ 521 522 while (!SIMPLEQ_EMPTY(&si->si_q)) { 523 /* 524 * Pick the longest waiting handler to run. We block 525 * interrupts but do not lock in order to do this, as 526 * we are protecting against the local CPU only. 527 */ 528 sh = SIMPLEQ_FIRST(&si->si_q); 529 SIMPLEQ_REMOVE_HEAD(&si->si_q, sh_q); 530 KASSERT((sh->sh_flags & SOFTINT_PENDING) != 0); 531 KASSERT((sh->sh_flags & SOFTINT_ACTIVE) == 0); 532 sh->sh_flags ^= (SOFTINT_PENDING | SOFTINT_ACTIVE); 533 splx(s); 534 535 /* Run the handler. */ 536 if (sh->sh_flags & SOFTINT_MPSAFE) { 537 if (havelock) { 538 KERNEL_UNLOCK_ONE(l); 539 havelock = false; 540 } 541 } else if (!havelock) { 542 KERNEL_LOCK(1, l); 543 havelock = true; 544 } 545 (*sh->sh_func)(sh->sh_arg); 546 547 /* Diagnostic: check that spin-locks have not leaked. */ 548 KASSERTMSG(curcpu()->ci_mtx_count == 0, 549 "%s: ci_mtx_count (%d) != 0, sh_func %p\n", 550 __func__, curcpu()->ci_mtx_count, sh->sh_func); 551 552 (void)splhigh(); 553 KASSERT((sh->sh_flags & SOFTINT_ACTIVE) != 0); 554 sh->sh_flags ^= SOFTINT_ACTIVE; 555 } 556 557 if (havelock) { 558 KERNEL_UNLOCK_ONE(l); 559 } 560 561 /* 562 * Unlocked, but only for statistics. 563 * Should be per-CPU to prevent cache ping-pong. 564 */ 565 curcpu()->ci_data.cpu_nsoft++; 566 567 KASSERT(si->si_cpu == curcpu()); 568 KASSERT(si->si_lwp->l_wchan == NULL); 569 KASSERT(si->si_active); 570 si->si_evcnt.ev_count++; 571 si->si_active = 0; 572 } 573 574 /* 575 * softint_block: 576 * 577 * Update statistics when the soft interrupt blocks. 578 */ 579 void 580 softint_block(lwp_t *l) 581 { 582 softint_t *si = l->l_private; 583 584 KASSERT((l->l_pflag & LP_INTR) != 0); 585 si->si_evcnt_block.ev_count++; 586 } 587 588 /* 589 * schednetisr: 590 * 591 * Trigger a legacy network interrupt. XXX Needs to go away. 592 */ 593 void 594 schednetisr(int isr) 595 { 596 597 softint_schedule(softint_netisrs[isr]); 598 } 599 600 #ifndef __HAVE_FAST_SOFTINTS 601 602 #ifdef __HAVE_PREEMPTION 603 #error __HAVE_PREEMPTION requires __HAVE_FAST_SOFTINTS 604 #endif 605 606 /* 607 * softint_init_md: 608 * 609 * Slow path: perform machine-dependent initialization. 610 */ 611 void 612 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep) 613 { 614 softint_t *si; 615 616 *machdep = (1 << level); 617 si = l->l_private; 618 619 lwp_lock(l); 620 lwp_unlock_to(l, l->l_cpu->ci_schedstate.spc_mutex); 621 lwp_lock(l); 622 /* Cheat and make the KASSERT in softint_thread() happy. */ 623 si->si_active = 1; 624 l->l_stat = LSRUN; 625 sched_enqueue(l, false); 626 lwp_unlock(l); 627 } 628 629 /* 630 * softint_trigger: 631 * 632 * Slow path: cause a soft interrupt handler to begin executing. 633 * Called at IPL_HIGH. 634 */ 635 void 636 softint_trigger(uintptr_t machdep) 637 { 638 struct cpu_info *ci; 639 lwp_t *l; 640 641 l = curlwp; 642 ci = l->l_cpu; 643 ci->ci_data.cpu_softints |= machdep; 644 if (l == ci->ci_data.cpu_idlelwp) { 645 cpu_need_resched(ci, 0); 646 } else { 647 /* MI equivalent of aston() */ 648 cpu_signotify(l); 649 } 650 } 651 652 /* 653 * softint_thread: 654 * 655 * Slow path: MI software interrupt dispatch. 656 */ 657 void 658 softint_thread(void *cookie) 659 { 660 softint_t *si; 661 lwp_t *l; 662 int s; 663 664 l = curlwp; 665 si = l->l_private; 666 667 for (;;) { 668 /* 669 * Clear pending status and run it. We must drop the 670 * spl before mi_switch(), since IPL_HIGH may be higher 671 * than IPL_SCHED (and it is not safe to switch at a 672 * higher level). 673 */ 674 s = splhigh(); 675 l->l_cpu->ci_data.cpu_softints &= ~si->si_machdep; 676 softint_execute(si, l, s); 677 splx(s); 678 679 lwp_lock(l); 680 l->l_stat = LSIDL; 681 mi_switch(l); 682 } 683 } 684 685 /* 686 * softint_picklwp: 687 * 688 * Slow path: called from mi_switch() to pick the highest priority 689 * soft interrupt LWP that needs to run. 690 */ 691 lwp_t * 692 softint_picklwp(void) 693 { 694 struct cpu_info *ci; 695 u_int mask; 696 softint_t *si; 697 lwp_t *l; 698 699 ci = curcpu(); 700 si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int; 701 mask = ci->ci_data.cpu_softints; 702 703 if ((mask & (1 << SOFTINT_SERIAL)) != 0) { 704 l = si[SOFTINT_SERIAL].si_lwp; 705 } else if ((mask & (1 << SOFTINT_NET)) != 0) { 706 l = si[SOFTINT_NET].si_lwp; 707 } else if ((mask & (1 << SOFTINT_BIO)) != 0) { 708 l = si[SOFTINT_BIO].si_lwp; 709 } else if ((mask & (1 << SOFTINT_CLOCK)) != 0) { 710 l = si[SOFTINT_CLOCK].si_lwp; 711 } else { 712 panic("softint_picklwp"); 713 } 714 715 return l; 716 } 717 718 /* 719 * softint_overlay: 720 * 721 * Slow path: called from lwp_userret() to run a soft interrupt 722 * within the context of a user thread. 723 */ 724 void 725 softint_overlay(void) 726 { 727 struct cpu_info *ci; 728 u_int softints, oflag; 729 softint_t *si; 730 pri_t obase; 731 lwp_t *l; 732 int s; 733 734 l = curlwp; 735 KASSERT((l->l_pflag & LP_INTR) == 0); 736 737 /* 738 * Arrange to elevate priority if the LWP blocks. Also, bind LWP 739 * to the CPU. Note: disable kernel preemption before doing that. 740 */ 741 s = splhigh(); 742 ci = l->l_cpu; 743 si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int; 744 745 obase = l->l_kpribase; 746 l->l_kpribase = PRI_KERNEL_RT; 747 oflag = l->l_pflag; 748 l->l_pflag = oflag | LP_INTR | LP_BOUND; 749 750 while ((softints = ci->ci_data.cpu_softints) != 0) { 751 if ((softints & (1 << SOFTINT_SERIAL)) != 0) { 752 ci->ci_data.cpu_softints &= ~(1 << SOFTINT_SERIAL); 753 softint_execute(&si[SOFTINT_SERIAL], l, s); 754 continue; 755 } 756 if ((softints & (1 << SOFTINT_NET)) != 0) { 757 ci->ci_data.cpu_softints &= ~(1 << SOFTINT_NET); 758 softint_execute(&si[SOFTINT_NET], l, s); 759 continue; 760 } 761 if ((softints & (1 << SOFTINT_BIO)) != 0) { 762 ci->ci_data.cpu_softints &= ~(1 << SOFTINT_BIO); 763 softint_execute(&si[SOFTINT_BIO], l, s); 764 continue; 765 } 766 if ((softints & (1 << SOFTINT_CLOCK)) != 0) { 767 ci->ci_data.cpu_softints &= ~(1 << SOFTINT_CLOCK); 768 softint_execute(&si[SOFTINT_CLOCK], l, s); 769 continue; 770 } 771 } 772 l->l_pflag = oflag; 773 l->l_kpribase = obase; 774 splx(s); 775 } 776 777 #else /* !__HAVE_FAST_SOFTINTS */ 778 779 /* 780 * softint_thread: 781 * 782 * Fast path: the LWP is switched to without restoring any state, 783 * so we should not arrive here - there is a direct handoff between 784 * the interrupt stub and softint_dispatch(). 785 */ 786 void 787 softint_thread(void *cookie) 788 { 789 790 panic("softint_thread"); 791 } 792 793 /* 794 * softint_dispatch: 795 * 796 * Fast path: entry point from machine-dependent code. 797 */ 798 void 799 softint_dispatch(lwp_t *pinned, int s) 800 { 801 struct bintime now; 802 softint_t *si; 803 u_int timing; 804 lwp_t *l; 805 806 KASSERT((pinned->l_pflag & LP_RUNNING) != 0); 807 l = curlwp; 808 si = l->l_private; 809 810 /* 811 * Note the interrupted LWP, and mark the current LWP as running 812 * before proceeding. Although this must as a rule be done with 813 * the LWP locked, at this point no external agents will want to 814 * modify the interrupt LWP's state. 815 */ 816 timing = (softint_timing ? LP_TIMEINTR : 0); 817 l->l_switchto = pinned; 818 l->l_stat = LSONPROC; 819 l->l_pflag |= (LP_RUNNING | timing); 820 821 /* 822 * Dispatch the interrupt. If softints are being timed, charge 823 * for it. 824 */ 825 if (timing) 826 binuptime(&l->l_stime); 827 softint_execute(si, l, s); 828 if (timing) { 829 binuptime(&now); 830 updatertime(l, &now); 831 l->l_pflag &= ~LP_TIMEINTR; 832 } 833 834 /* Indicate a soft-interrupt switch. */ 835 pserialize_switchpoint(); 836 837 /* 838 * If we blocked while handling the interrupt, the pinned LWP is 839 * gone so switch to the idle LWP. It will select a new LWP to 840 * run. 841 * 842 * We must drop the priority level as switching at IPL_HIGH could 843 * deadlock the system. We have already set si->si_active = 0, 844 * which means another interrupt at this level can be triggered. 845 * That's not be a problem: we are lowering to level 's' which will 846 * prevent softint_dispatch() from being reentered at level 's', 847 * until the priority is finally dropped to IPL_NONE on entry to 848 * the LWP chosen by lwp_exit_switchaway(). 849 */ 850 l->l_stat = LSIDL; 851 if (l->l_switchto == NULL) { 852 splx(s); 853 pmap_deactivate(l); 854 lwp_exit_switchaway(l); 855 /* NOTREACHED */ 856 } 857 l->l_switchto = NULL; 858 l->l_pflag &= ~LP_RUNNING; 859 } 860 861 #endif /* !__HAVE_FAST_SOFTINTS */ 862