Home | History | Annotate | Line # | Download | only in kern
kern_softint.c revision 1.18
      1 /*	$NetBSD: kern_softint.c,v 1.18 2008/04/28 20:24:03 martin 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 "opt_preemption.h"
    179 
    180 #include <sys/cdefs.h>
    181 __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.18 2008/04/28 20:24:03 martin Exp $");
    182 
    183 #include <sys/param.h>
    184 #include <sys/malloc.h>
    185 #include <sys/proc.h>
    186 #include <sys/intr.h>
    187 #include <sys/mutex.h>
    188 #include <sys/kthread.h>
    189 #include <sys/evcnt.h>
    190 #include <sys/cpu.h>
    191 
    192 #include <net/netisr.h>
    193 
    194 #include <uvm/uvm_extern.h>
    195 
    196 /* This could overlap with signal info in struct lwp. */
    197 typedef struct softint {
    198 	SIMPLEQ_HEAD(, softhand) si_q;
    199 	struct lwp		*si_lwp;
    200 	struct cpu_info		*si_cpu;
    201 	uintptr_t		si_machdep;
    202 	struct evcnt		si_evcnt;
    203 	struct evcnt		si_evcnt_block;
    204 	int			si_active;
    205 	char			si_name[8];
    206 	char			si_name_block[8+6];
    207 } softint_t;
    208 
    209 typedef struct softhand {
    210 	SIMPLEQ_ENTRY(softhand)	sh_q;
    211 	void			(*sh_func)(void *);
    212 	void			*sh_arg;
    213 	softint_t		*sh_isr;
    214 	u_int			sh_pending;
    215 	u_int			sh_flags;
    216 } softhand_t;
    217 
    218 typedef struct softcpu {
    219 	struct cpu_info		*sc_cpu;
    220 	softint_t		sc_int[SOFTINT_COUNT];
    221 	softhand_t		sc_hand[1];
    222 } softcpu_t;
    223 
    224 static void	softint_thread(void *);
    225 
    226 u_int		softint_bytes = 8192;
    227 u_int		softint_timing;
    228 static u_int	softint_max;
    229 static kmutex_t	softint_lock;
    230 static void	*softint_netisrs[32];
    231 
    232 /*
    233  * softint_init_isr:
    234  *
    235  *	Initialize a single interrupt level for a single CPU.
    236  */
    237 static void
    238 softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level)
    239 {
    240 	struct cpu_info *ci;
    241 	softint_t *si;
    242 	int error;
    243 
    244 	si = &sc->sc_int[level];
    245 	ci = sc->sc_cpu;
    246 	si->si_cpu = ci;
    247 
    248 	SIMPLEQ_INIT(&si->si_q);
    249 
    250 	error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR |
    251 	    KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp,
    252 	    "soft%s/%u", desc, ci->ci_index);
    253 	if (error != 0)
    254 		panic("softint_init_isr: error %d", error);
    255 
    256 	snprintf(si->si_name, sizeof(si->si_name), "%s/%u", desc,
    257 	    ci->ci_index);
    258 	evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_INTR, NULL,
    259 	   "softint", si->si_name);
    260 	snprintf(si->si_name_block, sizeof(si->si_name_block), "%s block/%u",
    261 	    desc, ci->ci_index);
    262 	evcnt_attach_dynamic(&si->si_evcnt_block, EVCNT_TYPE_INTR, NULL,
    263 	   "softint", si->si_name_block);
    264 
    265 	si->si_lwp->l_private = si;
    266 	softint_init_md(si->si_lwp, level, &si->si_machdep);
    267 }
    268 /*
    269  * softint_init:
    270  *
    271  *	Initialize per-CPU data structures.  Called from mi_cpu_attach().
    272  */
    273 void
    274 softint_init(struct cpu_info *ci)
    275 {
    276 	static struct cpu_info *first;
    277 	softcpu_t *sc, *scfirst;
    278 	softhand_t *sh, *shmax;
    279 
    280 	if (first == NULL) {
    281 		/* Boot CPU. */
    282 		first = ci;
    283 		mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE);
    284 		softint_bytes = round_page(softint_bytes);
    285 		softint_max = (softint_bytes - sizeof(softcpu_t)) /
    286 		    sizeof(softhand_t);
    287 	}
    288 
    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 
    345 	mutex_enter(&softint_lock);
    346 
    347 	/* Find a free slot. */
    348 	sc = curcpu()->ci_data.cpu_softcpu;
    349 	for (index = 1; index < softint_max; index++)
    350 		if (sc->sc_hand[index].sh_func == NULL)
    351 			break;
    352 	if (index == softint_max) {
    353 		mutex_exit(&softint_lock);
    354 		printf("WARNING: softint_establish: table full, "
    355 		    "increase softint_bytes\n");
    356 		return NULL;
    357 	}
    358 
    359 	/* Set up the handler on each CPU. */
    360 	if (ncpu < 2) {
    361 		/* XXX hack for machines with no CPU_INFO_FOREACH() early on */
    362 		sc = curcpu()->ci_data.cpu_softcpu;
    363 		sh = &sc->sc_hand[index];
    364 		sh->sh_isr = &sc->sc_int[level];
    365 		sh->sh_func = func;
    366 		sh->sh_arg = arg;
    367 		sh->sh_flags = flags;
    368 		sh->sh_pending = 0;
    369 	} else for (CPU_INFO_FOREACH(cii, ci)) {
    370 		sc = ci->ci_data.cpu_softcpu;
    371 		sh = &sc->sc_hand[index];
    372 		sh->sh_isr = &sc->sc_int[level];
    373 		sh->sh_func = func;
    374 		sh->sh_arg = arg;
    375 		sh->sh_flags = flags;
    376 		sh->sh_pending = 0;
    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.
    388  */
    389 void
    390 softint_disestablish(void *arg)
    391 {
    392 	CPU_INFO_ITERATOR cii;
    393 	struct cpu_info *ci;
    394 	softcpu_t *sc;
    395 	softhand_t *sh;
    396 	uintptr_t offset;
    397 
    398 	offset = (uintptr_t)arg;
    399 	KASSERT(offset != 0 && offset < softint_bytes);
    400 
    401 	mutex_enter(&softint_lock);
    402 
    403 	/* Clear the handler on each CPU. */
    404 	for (CPU_INFO_FOREACH(cii, ci)) {
    405 		sc = ci->ci_data.cpu_softcpu;
    406 		sh = (softhand_t *)((uint8_t *)sc + offset);
    407 		KASSERT(sh->sh_func != NULL);
    408 		KASSERT(sh->sh_pending == 0);
    409 		sh->sh_func = NULL;
    410 	}
    411 
    412 	mutex_exit(&softint_lock);
    413 }
    414 
    415 /*
    416  * softint_schedule:
    417  *
    418  *	Trigger a software interrupt.  Must be called from a hardware
    419  *	interrupt handler, or with preemption disabled (since we are
    420  *	using the value of curcpu()).
    421  */
    422 void
    423 softint_schedule(void *arg)
    424 {
    425 	softhand_t *sh;
    426 	softint_t *si;
    427 	uintptr_t offset;
    428 	int s;
    429 
    430 	KASSERT(kpreempt_disabled());
    431 
    432 	/* Find the handler record for this CPU. */
    433 	offset = (uintptr_t)arg;
    434 	KASSERT(offset != 0 && offset < softint_bytes);
    435 	sh = (softhand_t *)((uint8_t *)curcpu()->ci_data.cpu_softcpu + offset);
    436 
    437 	/* If it's already pending there's nothing to do. */
    438 	if (sh->sh_pending)
    439 		return;
    440 
    441 	/*
    442 	 * Enqueue the handler into the LWP's pending list.
    443 	 * If the LWP is completely idle, then make it run.
    444 	 */
    445 	s = splhigh();
    446 	if (!sh->sh_pending) {
    447 		si = sh->sh_isr;
    448 		sh->sh_pending = 1;
    449 		SIMPLEQ_INSERT_TAIL(&si->si_q, sh, sh_q);
    450 		if (si->si_active == 0) {
    451 			si->si_active = 1;
    452 			softint_trigger(si->si_machdep);
    453 		}
    454 	}
    455 	splx(s);
    456 }
    457 
    458 /*
    459  * softint_execute:
    460  *
    461  *	Invoke handlers for the specified soft interrupt.
    462  *	Must be entered at splhigh.  Will drop the priority
    463  *	to the level specified, but returns back at splhigh.
    464  */
    465 static inline void
    466 softint_execute(softint_t *si, lwp_t *l, int s)
    467 {
    468 	softhand_t *sh;
    469 	bool havelock;
    470 
    471 #ifdef __HAVE_FAST_SOFTINTS
    472 	KASSERT(si->si_lwp == curlwp);
    473 #else
    474 	/* May be running in user context. */
    475 #endif
    476 	KASSERT(si->si_cpu == curcpu());
    477 	KASSERT(si->si_lwp->l_wchan == NULL);
    478 	KASSERT(si->si_active);
    479 
    480 	havelock = false;
    481 
    482 	/*
    483 	 * Note: due to priority inheritance we may have interrupted a
    484 	 * higher priority LWP.  Since the soft interrupt must be quick
    485 	 * and is non-preemptable, we don't bother yielding.
    486 	 */
    487 
    488 	while (!SIMPLEQ_EMPTY(&si->si_q)) {
    489 		/*
    490 		 * Pick the longest waiting handler to run.  We block
    491 		 * interrupts but do not lock in order to do this, as
    492 		 * we are protecting against the local CPU only.
    493 		 */
    494 		sh = SIMPLEQ_FIRST(&si->si_q);
    495 		SIMPLEQ_REMOVE_HEAD(&si->si_q, sh_q);
    496 		sh->sh_pending = 0;
    497 		splx(s);
    498 
    499 		/* Run the handler. */
    500 		if ((sh->sh_flags & SOFTINT_MPSAFE) == 0 && !havelock) {
    501 			KERNEL_LOCK(1, l);
    502 			havelock = true;
    503 		}
    504 		(*sh->sh_func)(sh->sh_arg);
    505 
    506 		(void)splhigh();
    507 	}
    508 
    509 	if (havelock) {
    510 		KERNEL_UNLOCK_ONE(l);
    511 	}
    512 
    513 	/*
    514 	 * Unlocked, but only for statistics.
    515 	 * Should be per-CPU to prevent cache ping-pong.
    516 	 */
    517 	uvmexp.softs++;
    518 
    519 	KASSERT(si->si_cpu == curcpu());
    520 	KASSERT(si->si_lwp->l_wchan == NULL);
    521 	KASSERT(si->si_active);
    522 	si->si_evcnt.ev_count++;
    523 	si->si_active = 0;
    524 }
    525 
    526 /*
    527  * softint_block:
    528  *
    529  *	Update statistics when the soft interrupt blocks.
    530  */
    531 void
    532 softint_block(lwp_t *l)
    533 {
    534 	softint_t *si = l->l_private;
    535 
    536 	KASSERT((l->l_pflag & LP_INTR) != 0);
    537 	si->si_evcnt_block.ev_count++;
    538 }
    539 
    540 /*
    541  * schednetisr:
    542  *
    543  *	Trigger a legacy network interrupt.  XXX Needs to go away.
    544  */
    545 void
    546 schednetisr(int isr)
    547 {
    548 
    549 	softint_schedule(softint_netisrs[isr]);
    550 }
    551 
    552 #ifndef __HAVE_FAST_SOFTINTS
    553 
    554 #ifdef PREEMPTION
    555 #error PREEMPTION requires __HAVE_FAST_SOFTINTS
    556 #endif
    557 
    558 /*
    559  * softint_init_md:
    560  *
    561  *	Slow path: perform machine-dependent initialization.
    562  */
    563 void
    564 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
    565 {
    566 	softint_t *si;
    567 
    568 	*machdep = (1 << level);
    569 	si = l->l_private;
    570 
    571 	lwp_lock(l);
    572 	lwp_unlock_to(l, l->l_cpu->ci_schedstate.spc_mutex);
    573 	lwp_lock(l);
    574 	/* Cheat and make the KASSERT in softint_thread() happy. */
    575 	si->si_active = 1;
    576 	l->l_stat = LSRUN;
    577 	sched_enqueue(l, false);
    578 	lwp_unlock(l);
    579 }
    580 
    581 /*
    582  * softint_trigger:
    583  *
    584  *	Slow path: cause a soft interrupt handler to begin executing.
    585  *	Called at IPL_HIGH.
    586  */
    587 void
    588 softint_trigger(uintptr_t machdep)
    589 {
    590 	struct cpu_info *ci;
    591 	lwp_t *l;
    592 
    593 	l = curlwp;
    594 	ci = l->l_cpu;
    595 	ci->ci_data.cpu_softints |= machdep;
    596 	if (l == ci->ci_data.cpu_idlelwp) {
    597 		cpu_need_resched(ci, 0);
    598 	} else {
    599 		/* MI equivalent of aston() */
    600 		cpu_signotify(l);
    601 	}
    602 }
    603 
    604 /*
    605  * softint_thread:
    606  *
    607  *	Slow path: MI software interrupt dispatch.
    608  */
    609 void
    610 softint_thread(void *cookie)
    611 {
    612 	softint_t *si;
    613 	lwp_t *l;
    614 	int s;
    615 
    616 	l = curlwp;
    617 	si = l->l_private;
    618 
    619 	for (;;) {
    620 		/*
    621 		 * Clear pending status and run it.  We must drop the
    622 		 * spl before mi_switch(), since IPL_HIGH may be higher
    623 		 * than IPL_SCHED (and it is not safe to switch at a
    624 		 * higher level).
    625 		 */
    626 		s = splhigh();
    627 		l->l_cpu->ci_data.cpu_softints &= ~si->si_machdep;
    628 		softint_execute(si, l, s);
    629 		splx(s);
    630 
    631 		lwp_lock(l);
    632 		l->l_stat = LSIDL;
    633 		mi_switch(l);
    634 	}
    635 }
    636 
    637 /*
    638  * softint_picklwp:
    639  *
    640  *	Slow path: called from mi_switch() to pick the highest priority
    641  *	soft interrupt LWP that needs to run.
    642  */
    643 lwp_t *
    644 softint_picklwp(void)
    645 {
    646 	struct cpu_info *ci;
    647 	u_int mask;
    648 	softint_t *si;
    649 	lwp_t *l;
    650 
    651 	ci = curcpu();
    652 	si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int;
    653 	mask = ci->ci_data.cpu_softints;
    654 
    655 	if ((mask & (1 << SOFTINT_SERIAL)) != 0) {
    656 		l = si[SOFTINT_SERIAL].si_lwp;
    657 	} else if ((mask & (1 << SOFTINT_NET)) != 0) {
    658 		l = si[SOFTINT_NET].si_lwp;
    659 	} else if ((mask & (1 << SOFTINT_BIO)) != 0) {
    660 		l = si[SOFTINT_BIO].si_lwp;
    661 	} else if ((mask & (1 << SOFTINT_CLOCK)) != 0) {
    662 		l = si[SOFTINT_CLOCK].si_lwp;
    663 	} else {
    664 		panic("softint_picklwp");
    665 	}
    666 
    667 	return l;
    668 }
    669 
    670 /*
    671  * softint_overlay:
    672  *
    673  *	Slow path: called from lwp_userret() to run a soft interrupt
    674  *	within the context of a user thread.
    675  */
    676 void
    677 softint_overlay(void)
    678 {
    679 	struct cpu_info *ci;
    680 	u_int softints, oflag;
    681 	softint_t *si;
    682 	pri_t obase;
    683 	lwp_t *l;
    684 	int s;
    685 
    686 	l = curlwp;
    687 	ci = l->l_cpu;
    688 	si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int;
    689 
    690 	KASSERT((l->l_pflag & LP_INTR) == 0);
    691 
    692 	/* Arrange to elevate priority if the LWP blocks. */
    693 	s = splhigh();
    694 	obase = l->l_kpribase;
    695 	l->l_kpribase = PRI_KERNEL_RT;
    696 	oflag = l->l_pflag;
    697 	l->l_pflag = oflag | LP_INTR | LP_BOUND;
    698 	while ((softints = ci->ci_data.cpu_softints) != 0) {
    699 		if ((softints & (1 << SOFTINT_SERIAL)) != 0) {
    700 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_SERIAL);
    701 			softint_execute(&si[SOFTINT_SERIAL], l, s);
    702 			continue;
    703 		}
    704 		if ((softints & (1 << SOFTINT_NET)) != 0) {
    705 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_NET);
    706 			softint_execute(&si[SOFTINT_NET], l, s);
    707 			continue;
    708 		}
    709 		if ((softints & (1 << SOFTINT_BIO)) != 0) {
    710 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_BIO);
    711 			softint_execute(&si[SOFTINT_BIO], l, s);
    712 			continue;
    713 		}
    714 		if ((softints & (1 << SOFTINT_CLOCK)) != 0) {
    715 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_CLOCK);
    716 			softint_execute(&si[SOFTINT_CLOCK], l, s);
    717 			continue;
    718 		}
    719 	}
    720 	l->l_pflag = oflag;
    721 	l->l_kpribase = obase;
    722 	splx(s);
    723 }
    724 
    725 #else	/*  !__HAVE_FAST_SOFTINTS */
    726 
    727 /*
    728  * softint_thread:
    729  *
    730  *	Fast path: the LWP is switched to without restoring any state,
    731  *	so we should not arrive here - there is a direct handoff between
    732  *	the interrupt stub and softint_dispatch().
    733  */
    734 void
    735 softint_thread(void *cookie)
    736 {
    737 
    738 	panic("softint_thread");
    739 }
    740 
    741 /*
    742  * softint_dispatch:
    743  *
    744  *	Fast path: entry point from machine-dependent code.
    745  */
    746 void
    747 softint_dispatch(lwp_t *pinned, int s)
    748 {
    749 	struct bintime now;
    750 	softint_t *si;
    751 	u_int timing;
    752 	lwp_t *l;
    753 
    754 	l = curlwp;
    755 	si = l->l_private;
    756 
    757 	/*
    758 	 * Note the interrupted LWP, and mark the current LWP as running
    759 	 * before proceeding.  Although this must as a rule be done with
    760 	 * the LWP locked, at this point no external agents will want to
    761 	 * modify the interrupt LWP's state.
    762 	 */
    763 	timing = (softint_timing ? LW_TIMEINTR : 0);
    764 	l->l_switchto = pinned;
    765 	l->l_stat = LSONPROC;
    766 	l->l_flag |= (LW_RUNNING | timing);
    767 
    768 	/*
    769 	 * Dispatch the interrupt.  If softints are being timed, charge
    770 	 * for it.
    771 	 */
    772 	if (timing)
    773 		binuptime(&l->l_stime);
    774 	softint_execute(si, l, s);
    775 	if (timing) {
    776 		binuptime(&now);
    777 		updatertime(l, &now);
    778 		l->l_flag &= ~LW_TIMEINTR;
    779 	}
    780 
    781 	/*
    782 	 * If we blocked while handling the interrupt, the pinned LWP is
    783 	 * gone so switch to the idle LWP.  It will select a new LWP to
    784 	 * run.
    785 	 *
    786 	 * We must drop the priority level as switching at IPL_HIGH could
    787 	 * deadlock the system.  We have already set si->si_active = 0,
    788 	 * which means another interrupt at this level can be triggered.
    789 	 * That's not be a problem: we are lowering to level 's' which will
    790 	 * prevent softint_dispatch() from being reentered at level 's',
    791 	 * until the priority is finally dropped to IPL_NONE on entry to
    792 	 * the idle loop.
    793 	 */
    794 	l->l_stat = LSIDL;
    795 	if (l->l_switchto == NULL) {
    796 		splx(s);
    797 		pmap_deactivate(l);
    798 		lwp_exit_switchaway(l);
    799 		/* NOTREACHED */
    800 	}
    801 	l->l_switchto = NULL;
    802 	l->l_flag &= ~LW_RUNNING;
    803 }
    804 
    805 #endif	/* !__HAVE_FAST_SOFTINTS */
    806