Home | History | Annotate | Line # | Download | only in kern
kern_softint.c revision 1.1.2.8
      1 /*	$NetBSD: kern_softint.c,v 1.1.2.8 2007/07/18 10:13:59 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 in the bottom half of the kernel must
     90  *	take care to ensure that any lock that may be taken from a
     91  *	software interrupt can not be held for more than a short period
     92  *	of time.
     93  *
     94  *	The kernel does not allow software interrupts to use facilities
     95  *	or perform actions that may block for a significant amount of
     96  *	time.  This means that it's not valid for a software interrupt
     97  *	to: sleep on condition variables, use the lockmgr() facility,
     98  *	or wait for resources to become available (for example,
     99  *	memory).
    100  *
    101  * Per-CPU operation
    102  *
    103  *	If a soft interrupt is triggered on a CPU, it can only be
    104  *	dispatched on the same CPU.  Each LWP dedicated to handling a
    105  *	soft interrupt is bound to its home CPU, so if the LWP blocks
    106  *	and needs to run again, it can only run there.  Nearly all data
    107  *	structures used to manage software interrupts are per-CPU.
    108  *
    109  *	The per-CPU requirement is intended to reduce "ping-pong" of
    110  *	cache lines between CPUs: lines occupied by data structures
    111  *	used to manage the soft interrupts, and lines occupied by data
    112  *	items being passed down to the soft interrupt.  As a positive
    113  *	side effect, this also means that the soft interrupt dispatch
    114  *	code does not need to to use spinlocks to synchronize with the
    115  *	upper half.
    116  *
    117  * Generic implementation
    118  *
    119  *	A generic, low performance implementation is provided that
    120  *	works across all architectures, with no machine-dependent
    121  *	modifications needed.  This implementation uses the scheduler,
    122  *	and so has a number of restrictions:
    123  *
    124  *	1) Since software interrupts can be triggered from any priority
    125  *	level, on architectures where the generic implementation is
    126  *	used IPL_SCHED must be equal to IPL_HIGH (it must block all
    127  *	interrupts).
    128  *
    129  *	2) The software interrupts are not currently preemptive, so
    130  *	must wait for the currently executing LWP to yield the CPU.
    131  *	This can introduce latency.
    132  *
    133  *	3) A context switch is required for each soft interrupt to be
    134  *	handled, which can be quite expensive.
    135  *
    136  * 'Fast' software interrupts
    137  *
    138  *	If an architectures defines __HAVE_FAST_SOFTINTS, it implements
    139  *	the fast mechanism.  Threads running either in the kernel or in
    140  *	userspace will be interrupted, but will not be preempted.  When
    141  *	the soft interrupt completes execution, the interrupted LWP
    142  *	is resumed.  Interrupt dispatch code must provide the minimum
    143  *	level of context necessary for the soft interrupt to block and
    144  *	be resumed at a later time.  The machine-dependent dispatch
    145  *	path looks something like the following:
    146  *
    147  *	softintr()
    148  *	{
    149  *		go to IPL_HIGH if necessary for switch;
    150  *		save any necessary registers in a format that can be
    151  *		    restored by cpu_switchto if the softint blocks;
    152  *		arrange for cpu_switchto() to restore into the
    153  *		    trampoline function;
    154  *		identify LWP to handle this interrupt;
    155  *		switch to the LWP's stack;
    156  *		switch register stacks, if necessary;
    157  *		assign new value of curlwp;
    158  *		call MI softint_dispatch, passing old curlwp and IPL
    159  *		    to execute interrupt at;
    160  *		switch back to old stack;
    161  *		switch back to old register stack, if necessary;
    162  *		restore curlwp;
    163  *		return to interrupted LWP;
    164  *	}
    165  *
    166  *	If the soft interrupt blocks, a trampoline function is returned
    167  *	to in the context of the interrupted LWP, as arranged for by
    168  *	softint():
    169  *
    170  *	softint_ret()
    171  *	{
    172  *		unlock soft interrupt LWP;
    173  *		resume interrupt processing, likely returning to
    174  *		    interrupted LWP or dispatching another, different
    175  *		    interrupt;
    176  *	}
    177  *
    178  *	Once the soft interrupt has fired (and even if it has blocked),
    179  *	no further soft interrupts at that level will be triggered by
    180  *	MI code until the soft interrupt handler has ceased execution.
    181  *	If a soft interrupt handler blocks and is resumed, it resumes
    182  *	execution as a normal LWP (kthread) and gains VM context.  Only
    183  *	when it has completed and is ready to fire again will it
    184  *	interrupt other threads.
    185  */
    186 
    187 #include <sys/cdefs.h>
    188 __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.1.2.8 2007/07/18 10:13:59 ad Exp $");
    189 
    190 #include <sys/param.h>
    191 #include <sys/malloc.h>
    192 #include <sys/proc.h>
    193 #include <sys/intr.h>
    194 #include <sys/mutex.h>
    195 #include <sys/kthread.h>
    196 #include <sys/evcnt.h>
    197 #include <sys/cpu.h>
    198 
    199 #include <net/netisr.h>
    200 
    201 #include <uvm/uvm_extern.h>
    202 
    203 #define	PRI_SOFTSERIAL	(PRI_COUNT - 1)
    204 #define	PRI_SOFTNET	(PRI_SOFTSERIAL - schedppq * 1)
    205 #define	PRI_SOFTBIO	(PRI_SOFTSERIAL - schedppq * 2)
    206 #define	PRI_SOFTCLOCK	(PRI_SOFTSERIAL - schedppq * 3)
    207 
    208 /* This could overlap with signal info in struct lwp. */
    209 typedef struct softint {
    210 	SIMPLEQ_HEAD(, softhand) si_q;
    211 	struct lwp		*si_lwp;
    212 	struct cpu_info		*si_cpu;
    213 	uintptr_t		si_machdep;
    214 	struct evcnt		si_evcnt;
    215 	int			si_active;
    216 	char			si_name[8];
    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 struct evcnt	softint_block;
    243 
    244 /*
    245  * softint_init_isr:
    246  *
    247  *	Initialize a single interrupt level for a single CPU.
    248  */
    249 static void
    250 softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level)
    251 {
    252 	struct cpu_info *ci;
    253 	softint_t *si;
    254 	int error;
    255 
    256 	si = &sc->sc_int[level];
    257 	ci = sc->sc_cpu;
    258 	si->si_cpu = ci;
    259 
    260 	SIMPLEQ_INIT(&si->si_q);
    261 
    262 	error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR |
    263 	    KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp,
    264 	    "soft%s/%d", desc, (int)ci->ci_cpuid);
    265 	if (error != 0)
    266 		panic("softint_init_isr: error %d", error);
    267 
    268 	snprintf(si->si_name, sizeof(si->si_name), "%s/%d", desc,
    269 	    (int)ci->ci_cpuid);
    270 	evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_INTR, NULL,
    271 	   "softint", si->si_name);
    272 
    273 	si->si_lwp->l_private = si;
    274 	softint_init_md(si->si_lwp, level, &si->si_machdep);
    275 #ifdef __HAVE_FAST_SOFTINTS
    276 	si->si_lwp->l_mutex = &ci->ci_schedstate.spc_lwplock;
    277 #endif
    278 }
    279 /*
    280  * softint_init:
    281  *
    282  *	Initialize per-CPU data structures.  Called from mi_cpu_attach().
    283  */
    284 void
    285 softint_init(struct cpu_info *ci)
    286 {
    287 	static struct cpu_info *first;
    288 	softcpu_t *sc, *scfirst;
    289 	softhand_t *sh, *shmax;
    290 
    291 	if (first == NULL) {
    292 		/* Boot CPU. */
    293 		first = ci;
    294 		mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE);
    295 		softint_bytes = round_page(softint_bytes);
    296 		softint_max = (softint_bytes - sizeof(softcpu_t)) /
    297 		    sizeof(softhand_t);
    298 		evcnt_attach_dynamic(&softint_block, EVCNT_TYPE_INTR,
    299 		    NULL, "softint", "block");
    300 	}
    301 
    302 	sc = (softcpu_t *)uvm_km_alloc(kernel_map, softint_bytes, 0,
    303 	    UVM_KMF_WIRED | UVM_KMF_ZERO);
    304 	if (sc == NULL)
    305 		panic("softint_init_cpu: cannot allocate memory");
    306 
    307 	ci->ci_data.cpu_softcpu = sc;
    308 	sc->sc_cpu = ci;
    309 
    310 	softint_init_isr(sc, "net", PRI_SOFTNET, SOFTINT_NET);
    311 	softint_init_isr(sc, "bio", PRI_SOFTBIO, SOFTINT_BIO);
    312 	softint_init_isr(sc, "clk", PRI_SOFTCLOCK, SOFTINT_CLOCK);
    313 	softint_init_isr(sc, "ser", PRI_SOFTSERIAL, SOFTINT_SERIAL);
    314 
    315 	if (first != ci) {
    316 		/* Don't lock -- autoconfiguration will prevent reentry. */
    317 		scfirst = first->ci_data.cpu_softcpu;
    318 		sh = sc->sc_hand;
    319 		memcpy(sh, scfirst->sc_hand, sizeof(*sh) * softint_max);
    320 
    321 		/* Update pointers for this CPU. */
    322 		for (shmax = sh + softint_max; sh < shmax; sh++) {
    323 			if (sh->sh_func == NULL)
    324 				continue;
    325 			sh->sh_isr =
    326 			    &sc->sc_int[sh->sh_flags & SOFTINT_LVLMASK];
    327 		}
    328 	} else {
    329 		/* Establish a handler for legacy net interrupts. */
    330 		softint_netisr_sih = softint_establish(SOFTINT_NET,
    331 		    softint_netisr, NULL);
    332 		KASSERT(softint_netisr_sih != NULL);
    333 	}
    334 }
    335 
    336 /*
    337  * softint_establish:
    338  *
    339  *	Register a software interrupt handler.
    340  */
    341 void *
    342 softint_establish(u_int flags, void (*func)(void *), void *arg)
    343 {
    344 	CPU_INFO_ITERATOR cii;
    345 	struct cpu_info *ci;
    346 	softcpu_t *sc;
    347 	softhand_t *sh;
    348 	u_int level, index;
    349 
    350 	level = (flags & SOFTINT_LVLMASK);
    351 	KASSERT(level < SOFTINT_COUNT);
    352 
    353 	mutex_enter(&softint_lock);
    354 
    355 	/* Find a free slot. */
    356 	sc = curcpu()->ci_data.cpu_softcpu;
    357 	for (index = 1; index < softint_max; index++)
    358 		if (sc->sc_hand[index].sh_func == NULL)
    359 			break;
    360 	if (index == softint_max) {
    361 		mutex_exit(&softint_lock);
    362 		printf("WARNING: softint_establish: table full, "
    363 		    "increase softint_bytes\n");
    364 		return NULL;
    365 	}
    366 
    367 	/* Set up the handler on each CPU. */
    368 	for (CPU_INFO_FOREACH(cii, ci)) {
    369 		sc = ci->ci_data.cpu_softcpu;
    370 		sh = &sc->sc_hand[index];
    371 
    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 	/* Set up 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 	/* Find the handler record for this CPU. */
    431 	offset = (uintptr_t)arg;
    432 	KASSERT(offset != 0 && offset < softint_bytes);
    433 	sh = (softhand_t *)((uint8_t *)curcpu()->ci_data.cpu_softcpu + offset);
    434 
    435 	/* If it's already pending there's nothing to do. */
    436 	if (sh->sh_pending)
    437 		return;
    438 
    439 	/*
    440 	 * Enqueue the handler into the LWP's pending list.
    441 	 * If the LWP is completely idle, then make it run.
    442 	 */
    443 	s = splhigh();
    444 	if (!sh->sh_pending) {
    445 		si = sh->sh_isr;
    446 		sh->sh_pending = 1;
    447 		SIMPLEQ_INSERT_TAIL(&si->si_q, sh, sh_q);
    448 		if (si->si_active == 0) {
    449 			si->si_active = 1;
    450 			softint_trigger(si->si_machdep);
    451 		}
    452 	}
    453 	splx(s);
    454 }
    455 
    456 /*
    457  * softint_execute:
    458  *
    459  *	Invoke handlers for the specified soft interrupt.
    460  *	Must be entered at splhigh.  Will drop the priority
    461  *	to the level specified, but returns back at splhigh.
    462  */
    463 static inline void
    464 softint_execute(softint_t *si, lwp_t *l, int s)
    465 {
    466 	softhand_t *sh;
    467 	lwp_t *l2;
    468 
    469 	KASSERT(si->si_lwp == curlwp);
    470 	KASSERT(si->si_cpu == curcpu());
    471 	KASSERT(si->si_lwp->l_wchan == NULL);
    472 	KASSERT(si->si_active);
    473 
    474 	while (!SIMPLEQ_EMPTY(&si->si_q)) {
    475 		/*
    476 		 * If any interrupted LWP has higher priority then we
    477 		 * must yield immediatley.  Note that IPL_HIGH may be
    478 		 * above IPL_SCHED, so we have to drop the interrupt
    479 		 * priority level before yielding.
    480 		 *
    481 		 * XXXAD Optimise this away.
    482 		 */
    483 		for (l2 = l->l_switchto; l2 != NULL; l2 = l2->l_switchto) {
    484 			if (lwp_eprio(l2) > l->l_priority)
    485 				break;
    486 		}
    487 		if (l2 != NULL) {
    488 			splx(s);
    489 			yield();
    490 			(void)splhigh();
    491 			continue;
    492 		}
    493 
    494 		/*
    495 		 * Pick the longest waiting handler to run.  We block
    496 		 * interrupts but do not lock in order to do this, as
    497 		 * we are protecting against the local CPU only.
    498 		 */
    499 		sh = SIMPLEQ_FIRST(&si->si_q);
    500 		SIMPLEQ_REMOVE_HEAD(&si->si_q, sh_q);
    501 		sh->sh_pending = 0;
    502 		splx(s);
    503 
    504 		/* Run the handler. */
    505 		if ((sh->sh_flags & SOFTINT_MPSAFE) == 0) {
    506 			KERNEL_LOCK(1, l);
    507 		}
    508 		(*sh->sh_func)(sh->sh_arg);
    509 		if ((sh->sh_flags & SOFTINT_MPSAFE) == 0) {
    510 			KERNEL_UNLOCK_ONE(l);
    511 		}
    512 
    513 		(void)splhigh();
    514 	}
    515 
    516 	/*
    517 	 * Unlocked, but only for statistics.
    518 	 * Should be per-CPU to prevent cache ping-pong.
    519 	 */
    520 	uvmexp.softs++;
    521 
    522 	si->si_evcnt.ev_count++;
    523 	si->si_active = 0;
    524 }
    525 
    526 /*
    527  * schednetisr:
    528  *
    529  *	Trigger a legacy network interrupt.  XXX Needs to go away.
    530  */
    531 void
    532 schednetisr(int isr)
    533 {
    534 	int s;
    535 
    536 	s = splhigh();
    537 	curcpu()->ci_data.cpu_netisrs |= (1 << isr);
    538 	softint_schedule(softint_netisr_sih);
    539 	splx(s);
    540 }
    541 
    542 /*
    543  * softintr_netisr:
    544  *
    545  *	Dispatch legacy network interrupts.  XXX Needs to go away.
    546  */
    547 static void
    548 softint_netisr(void *cookie)
    549 {
    550 	struct cpu_info *ci;
    551 	int s, bits;
    552 
    553 	ci = curcpu();
    554 
    555 	s = splhigh();
    556 	bits = ci->ci_data.cpu_netisrs;
    557 	ci->ci_data.cpu_netisrs = 0;
    558 	splx(s);
    559 
    560 #define	DONETISR(which, func)				\
    561 	do {						\
    562 		void func(void);			\
    563 		if ((bits & (1 << which)) != 0)		\
    564 			func();				\
    565 	} while(0);
    566 #include <net/netisr_dispatch.h>
    567 #undef DONETISR
    568 }
    569 
    570 #ifndef __HAVE_FAST_SOFTINTS
    571 
    572 /*
    573  * softint_init_md:
    574  *
    575  *	Perform machine-dependent initialization.
    576  */
    577 void
    578 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
    579 {
    580 	softint_t *si;
    581 
    582 	*machdep = (uintptr_t)l;
    583 	si = l->l_private;
    584 
    585 	lwp_lock(l);
    586 	/* Cheat and make the KASSERT in softint_thread() happy. */
    587 	si->si_active = 1;
    588 	l->l_stat = LSRUN;
    589 	sched_enqueue(l, false);
    590 	lwp_unlock(l);
    591 }
    592 
    593 /*
    594  * softint_trigger:
    595  *
    596  *	Cause a soft interrupt handler to begin executing.
    597  */
    598 void
    599 softint_trigger(uintptr_t machdep)
    600 {
    601 	struct cpu_info *ci;
    602 	lwp_t *l;
    603 
    604 	l = (lwp_t *)machdep;
    605 	ci = l->l_cpu;
    606 
    607 	spc_lock(ci);
    608 	l->l_mutex = ci->ci_schedstate.spc_mutex;
    609 	l->l_stat = LSRUN;
    610 	sched_enqueue(l, false);
    611 	cpu_need_resched(ci, 1);
    612 	spc_unlock(ci);
    613 }
    614 
    615 /*
    616  * softint_thread:
    617  *
    618  *	Slow path MI software interrupt dispatch.
    619  */
    620 void
    621 softint_thread(void *cookie)
    622 {
    623 	softint_t *si;
    624 	lwp_t *l;
    625 	int s;
    626 
    627 	l = curlwp;
    628 	si = l->l_private;
    629 	s = splhigh();
    630 
    631 	for (;;) {
    632 		softint_execute(si, l, s);
    633 
    634 		lwp_lock(l);
    635 		l->l_stat = LSIDL;
    636 		mi_switch(l);
    637 	}
    638 }
    639 
    640 #else	/*  !__HAVE_FAST_SOFTINTS */
    641 
    642 /*
    643  * softint_thread:
    644  *
    645  *	In the __HAVE_FAST_SOFTINTS case, the LWP is switched to without
    646  *	restoring any state, so we should not arrive here - there is a
    647  *	direct handoff between the interrupt stub and softint_dispatch().
    648  */
    649 void
    650 softint_thread(void *cookie)
    651 {
    652 
    653 	panic("softint_thread");
    654 }
    655 
    656 /*
    657  * softint_dispatch:
    658  *
    659  *	Entry point from machine-dependent code.
    660  */
    661 void
    662 softint_dispatch(lwp_t *pinned, int s)
    663 {
    664 	struct timeval now;
    665 	softint_t *si;
    666 	u_int timing;
    667 	lwp_t *l;
    668 
    669 	l = curlwp;
    670 	si = l->l_private;
    671 
    672 	/*
    673 	 * Note the interrupted LWP, and mark the current LWP as running
    674 	 * before proceeding.  Although this must as a rule be done with
    675 	 * the LWP locked, at this point no external agents will want to
    676 	 * modify the interrupt LWP's state.
    677 	 */
    678 	timing = (softint_timing ? LW_TIMEINTR : 0);
    679 	l->l_switchto = pinned;
    680 	l->l_stat = LSONPROC;
    681 	l->l_flag |= (LW_RUNNING | timing);
    682 
    683 	/*
    684 	 * Dispatch the interrupt.  If softints are being timed, charge
    685 	 * for it.
    686 	 */
    687 	if (timing)
    688 		microtime(&l->l_stime);
    689 	softint_execute(si, l, s);
    690 	if (timing) {
    691 		microtime(&now);
    692 		updatertime(l, &now);
    693 		l->l_flag &= ~LW_TIMEINTR;
    694 	}
    695 
    696 	/*
    697 	 * If we blocked while handling the interrupt, the pinned LWP is
    698 	 * gone so switch to the idle LWP.  It will select a new LWP to
    699 	 * run.
    700 	 *
    701 	 * We must drop the priority level as switching at IPL_HIGH could
    702 	 * deadlock the system.  We have already set si->si_active = 0,
    703 	 * which means another interrupt at this level can be triggered.
    704 	 * That's not be a problem: we are lowering to level 's' which will
    705 	 * prevent softint_dispatch() from being reentered at level 's',
    706 	 * until the priority is finally dropped to IPL_NONE on entry to
    707 	 * the idle loop.
    708 	 */
    709 	l->l_stat = LSIDL;
    710 	if (l->l_switchto == NULL) {
    711 		splx(s);
    712 		lwp_exit_switchaway(l);
    713 		/* NOTREACHED */
    714 	}
    715 	l->l_switchto = NULL;
    716 	l->l_flag &= ~LW_RUNNING;
    717 }
    718 
    719 #endif	/* !__HAVE_FAST_SOFTINTS */
    720