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