Home | History | Annotate | Line # | Download | only in kern
kern_softint.c revision 1.17
      1 /*	$NetBSD: kern_softint.c,v 1.17 2008/04/28 15:36:01 ad 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  * 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	or wait for resources to become
     97  *	available (for example,	memory).
     98  *
     99  * Per-CPU operation
    100  *
    101  *	If a soft interrupt is triggered on a CPU, it can only be
    102  *	dispatched on the same CPU.  Each LWP dedicated to handling a
    103  *	soft interrupt is bound to its home CPU, so if the LWP blocks
    104  *	and needs to run again, it can only run there.  Nearly all data
    105  *	structures used to manage software interrupts are per-CPU.
    106  *
    107  *	The per-CPU requirement is intended to reduce "ping-pong" of
    108  *	cache lines between CPUs: lines occupied by data structures
    109  *	used to manage the soft interrupts, and lines occupied by data
    110  *	items being passed down to the soft interrupt.  As a positive
    111  *	side effect, this also means that the soft interrupt dispatch
    112  *	code does not need to to use spinlocks to synchronize.
    113  *
    114  * Generic implementation
    115  *
    116  *	A generic, low performance implementation is provided that
    117  *	works across all architectures, with no machine-dependent
    118  *	modifications needed.  This implementation uses the scheduler,
    119  *	and so has a number of restrictions:
    120  *
    121  *	1) The software interrupts are not currently preemptive, so
    122  *	must wait for the currently executing LWP to yield the CPU.
    123  *	This can introduce latency.
    124  *
    125  *	2) An expensive context switch is required for a software
    126  *	interrupt to be handled.
    127  *
    128  * 'Fast' software interrupts
    129  *
    130  *	If an architectures defines __HAVE_FAST_SOFTINTS, it implements
    131  *	the fast mechanism.  Threads running either in the kernel or in
    132  *	userspace will be interrupted, but will not be preempted.  When
    133  *	the soft interrupt completes execution, the interrupted LWP
    134  *	is resumed.  Interrupt dispatch code must provide the minimum
    135  *	level of context necessary for the soft interrupt to block and
    136  *	be resumed at a later time.  The machine-dependent dispatch
    137  *	path looks something like the following:
    138  *
    139  *	softintr()
    140  *	{
    141  *		go to IPL_HIGH if necessary for switch;
    142  *		save any necessary registers in a format that can be
    143  *		    restored by cpu_switchto if the softint blocks;
    144  *		arrange for cpu_switchto() to restore into the
    145  *		    trampoline function;
    146  *		identify LWP to handle this interrupt;
    147  *		switch to the LWP's stack;
    148  *		switch register stacks, if necessary;
    149  *		assign new value of curlwp;
    150  *		call MI softint_dispatch, passing old curlwp and IPL
    151  *		    to execute interrupt at;
    152  *		switch back to old stack;
    153  *		switch back to old register stack, if necessary;
    154  *		restore curlwp;
    155  *		return to interrupted LWP;
    156  *	}
    157  *
    158  *	If the soft interrupt blocks, a trampoline function is returned
    159  *	to in the context of the interrupted LWP, as arranged for by
    160  *	softint():
    161  *
    162  *	softint_ret()
    163  *	{
    164  *		unlock soft interrupt LWP;
    165  *		resume interrupt processing, likely returning to
    166  *		    interrupted LWP or dispatching another, different
    167  *		    interrupt;
    168  *	}
    169  *
    170  *	Once the soft interrupt has fired (and even if it has blocked),
    171  *	no further soft interrupts at that level will be triggered by
    172  *	MI code until the soft interrupt handler has ceased execution.
    173  *	If a soft interrupt handler blocks and is resumed, it resumes
    174  *	execution as a normal LWP (kthread) and gains VM context.  Only
    175  *	when it has completed and is ready to fire again will it
    176  *	interrupt other threads.
    177  *
    178  * Future directions
    179  *
    180  *	Provide a cheap way to direct software interrupts to remote
    181  *	CPUs.  Provide a way to enqueue work items into the handler
    182  *	record,	removing additional spl calls (see subr_workqueue.c).
    183  */
    184 
    185 #include "opt_preemption.h"
    186 
    187 #include <sys/cdefs.h>
    188 __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.17 2008/04/28 15:36:01 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 /* This could overlap with signal info in struct lwp. */
    204 typedef struct softint {
    205 	SIMPLEQ_HEAD(, softhand) si_q;
    206 	struct lwp		*si_lwp;
    207 	struct cpu_info		*si_cpu;
    208 	uintptr_t		si_machdep;
    209 	struct evcnt		si_evcnt;
    210 	struct evcnt		si_evcnt_block;
    211 	int			si_active;
    212 	char			si_name[8];
    213 	char			si_name_block[8+6];
    214 } softint_t;
    215 
    216 typedef struct softhand {
    217 	SIMPLEQ_ENTRY(softhand)	sh_q;
    218 	void			(*sh_func)(void *);
    219 	void			*sh_arg;
    220 	softint_t		*sh_isr;
    221 	u_int			sh_pending;
    222 	u_int			sh_flags;
    223 } softhand_t;
    224 
    225 typedef struct softcpu {
    226 	struct cpu_info		*sc_cpu;
    227 	softint_t		sc_int[SOFTINT_COUNT];
    228 	softhand_t		sc_hand[1];
    229 } softcpu_t;
    230 
    231 static void	softint_thread(void *);
    232 
    233 u_int		softint_bytes = 8192;
    234 u_int		softint_timing;
    235 static u_int	softint_max;
    236 static kmutex_t	softint_lock;
    237 static void	*softint_netisrs[32];
    238 
    239 /*
    240  * softint_init_isr:
    241  *
    242  *	Initialize a single interrupt level for a single CPU.
    243  */
    244 static void
    245 softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level)
    246 {
    247 	struct cpu_info *ci;
    248 	softint_t *si;
    249 	int error;
    250 
    251 	si = &sc->sc_int[level];
    252 	ci = sc->sc_cpu;
    253 	si->si_cpu = ci;
    254 
    255 	SIMPLEQ_INIT(&si->si_q);
    256 
    257 	error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR |
    258 	    KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp,
    259 	    "soft%s/%u", desc, ci->ci_index);
    260 	if (error != 0)
    261 		panic("softint_init_isr: error %d", error);
    262 
    263 	snprintf(si->si_name, sizeof(si->si_name), "%s/%u", desc,
    264 	    ci->ci_index);
    265 	evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_INTR, NULL,
    266 	   "softint", si->si_name);
    267 	snprintf(si->si_name_block, sizeof(si->si_name_block), "%s block/%u",
    268 	    desc, ci->ci_index);
    269 	evcnt_attach_dynamic(&si->si_evcnt_block, EVCNT_TYPE_INTR, NULL,
    270 	   "softint", si->si_name_block);
    271 
    272 	si->si_lwp->l_private = si;
    273 	softint_init_md(si->si_lwp, level, &si->si_machdep);
    274 }
    275 /*
    276  * softint_init:
    277  *
    278  *	Initialize per-CPU data structures.  Called from mi_cpu_attach().
    279  */
    280 void
    281 softint_init(struct cpu_info *ci)
    282 {
    283 	static struct cpu_info *first;
    284 	softcpu_t *sc, *scfirst;
    285 	softhand_t *sh, *shmax;
    286 
    287 	if (first == NULL) {
    288 		/* Boot CPU. */
    289 		first = ci;
    290 		mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE);
    291 		softint_bytes = round_page(softint_bytes);
    292 		softint_max = (softint_bytes - sizeof(softcpu_t)) /
    293 		    sizeof(softhand_t);
    294 	}
    295 
    296 	sc = (softcpu_t *)uvm_km_alloc(kernel_map, softint_bytes, 0,
    297 	    UVM_KMF_WIRED | UVM_KMF_ZERO);
    298 	if (sc == NULL)
    299 		panic("softint_init_cpu: cannot allocate memory");
    300 
    301 	ci->ci_data.cpu_softcpu = sc;
    302 	ci->ci_data.cpu_softints = 0;
    303 	sc->sc_cpu = ci;
    304 
    305 	softint_init_isr(sc, "net", PRI_SOFTNET, SOFTINT_NET);
    306 	softint_init_isr(sc, "bio", PRI_SOFTBIO, SOFTINT_BIO);
    307 	softint_init_isr(sc, "clk", PRI_SOFTCLOCK, SOFTINT_CLOCK);
    308 	softint_init_isr(sc, "ser", PRI_SOFTSERIAL, SOFTINT_SERIAL);
    309 
    310 	if (first != ci) {
    311 		mutex_enter(&softint_lock);
    312 		scfirst = first->ci_data.cpu_softcpu;
    313 		sh = sc->sc_hand;
    314 		memcpy(sh, scfirst->sc_hand, sizeof(*sh) * softint_max);
    315 		/* Update pointers for this CPU. */
    316 		for (shmax = sh + softint_max; sh < shmax; sh++) {
    317 			if (sh->sh_func == NULL)
    318 				continue;
    319 			sh->sh_isr =
    320 			    &sc->sc_int[sh->sh_flags & SOFTINT_LVLMASK];
    321 		}
    322 		mutex_exit(&softint_lock);
    323 	} else {
    324 		/*
    325 		 * Establish handlers for legacy net interrupts.
    326 		 * XXX Needs to go away.
    327 		 */
    328 #define DONETISR(n, f)							\
    329     softint_netisrs[(n)] = softint_establish(SOFTINT_NET|SOFTINT_MPSAFE,\
    330         (void (*)(void *))(f), NULL)
    331 #include <net/netisr_dispatch.h>
    332 	}
    333 }
    334 
    335 /*
    336  * softint_establish:
    337  *
    338  *	Register a software interrupt handler.
    339  */
    340 void *
    341 softint_establish(u_int flags, void (*func)(void *), void *arg)
    342 {
    343 	CPU_INFO_ITERATOR cii;
    344 	struct cpu_info *ci;
    345 	softcpu_t *sc;
    346 	softhand_t *sh;
    347 	u_int level, index;
    348 
    349 	level = (flags & SOFTINT_LVLMASK);
    350 	KASSERT(level < SOFTINT_COUNT);
    351 
    352 	mutex_enter(&softint_lock);
    353 
    354 	/* Find a free slot. */
    355 	sc = curcpu()->ci_data.cpu_softcpu;
    356 	for (index = 1; index < softint_max; index++)
    357 		if (sc->sc_hand[index].sh_func == NULL)
    358 			break;
    359 	if (index == softint_max) {
    360 		mutex_exit(&softint_lock);
    361 		printf("WARNING: softint_establish: table full, "
    362 		    "increase softint_bytes\n");
    363 		return NULL;
    364 	}
    365 
    366 	/* Set up the handler on each CPU. */
    367 	if (ncpu < 2) {
    368 		/* XXX hack for machines with no CPU_INFO_FOREACH() early on */
    369 		sc = curcpu()->ci_data.cpu_softcpu;
    370 		sh = &sc->sc_hand[index];
    371 		sh->sh_isr = &sc->sc_int[level];
    372 		sh->sh_func = func;
    373 		sh->sh_arg = arg;
    374 		sh->sh_flags = flags;
    375 		sh->sh_pending = 0;
    376 	} else for (CPU_INFO_FOREACH(cii, ci)) {
    377 		sc = ci->ci_data.cpu_softcpu;
    378 		sh = &sc->sc_hand[index];
    379 		sh->sh_isr = &sc->sc_int[level];
    380 		sh->sh_func = func;
    381 		sh->sh_arg = arg;
    382 		sh->sh_flags = flags;
    383 		sh->sh_pending = 0;
    384 	}
    385 
    386 	mutex_exit(&softint_lock);
    387 
    388 	return (void *)((uint8_t *)&sc->sc_hand[index] - (uint8_t *)sc);
    389 }
    390 
    391 /*
    392  * softint_disestablish:
    393  *
    394  *	Unregister a software interrupt handler.
    395  */
    396 void
    397 softint_disestablish(void *arg)
    398 {
    399 	CPU_INFO_ITERATOR cii;
    400 	struct cpu_info *ci;
    401 	softcpu_t *sc;
    402 	softhand_t *sh;
    403 	uintptr_t offset;
    404 
    405 	offset = (uintptr_t)arg;
    406 	KASSERT(offset != 0 && offset < softint_bytes);
    407 
    408 	mutex_enter(&softint_lock);
    409 
    410 	/* Clear the handler on each CPU. */
    411 	for (CPU_INFO_FOREACH(cii, ci)) {
    412 		sc = ci->ci_data.cpu_softcpu;
    413 		sh = (softhand_t *)((uint8_t *)sc + offset);
    414 		KASSERT(sh->sh_func != NULL);
    415 		KASSERT(sh->sh_pending == 0);
    416 		sh->sh_func = NULL;
    417 	}
    418 
    419 	mutex_exit(&softint_lock);
    420 }
    421 
    422 /*
    423  * softint_schedule:
    424  *
    425  *	Trigger a software interrupt.  Must be called from a hardware
    426  *	interrupt handler, or with preemption disabled (since we are
    427  *	using the value of curcpu()).
    428  */
    429 void
    430 softint_schedule(void *arg)
    431 {
    432 	softhand_t *sh;
    433 	softint_t *si;
    434 	uintptr_t offset;
    435 	int s;
    436 
    437 	KASSERT(kpreempt_disabled());
    438 
    439 	/* Find the handler record for this CPU. */
    440 	offset = (uintptr_t)arg;
    441 	KASSERT(offset != 0 && offset < softint_bytes);
    442 	sh = (softhand_t *)((uint8_t *)curcpu()->ci_data.cpu_softcpu + offset);
    443 
    444 	/* If it's already pending there's nothing to do. */
    445 	if (sh->sh_pending)
    446 		return;
    447 
    448 	/*
    449 	 * Enqueue the handler into the LWP's pending list.
    450 	 * If the LWP is completely idle, then make it run.
    451 	 */
    452 	s = splhigh();
    453 	if (!sh->sh_pending) {
    454 		si = sh->sh_isr;
    455 		sh->sh_pending = 1;
    456 		SIMPLEQ_INSERT_TAIL(&si->si_q, sh, sh_q);
    457 		if (si->si_active == 0) {
    458 			si->si_active = 1;
    459 			softint_trigger(si->si_machdep);
    460 		}
    461 	}
    462 	splx(s);
    463 }
    464 
    465 /*
    466  * softint_execute:
    467  *
    468  *	Invoke handlers for the specified soft interrupt.
    469  *	Must be entered at splhigh.  Will drop the priority
    470  *	to the level specified, but returns back at splhigh.
    471  */
    472 static inline void
    473 softint_execute(softint_t *si, lwp_t *l, int s)
    474 {
    475 	softhand_t *sh;
    476 	bool havelock;
    477 
    478 #ifdef __HAVE_FAST_SOFTINTS
    479 	KASSERT(si->si_lwp == curlwp);
    480 #else
    481 	/* May be running in user context. */
    482 #endif
    483 	KASSERT(si->si_cpu == curcpu());
    484 	KASSERT(si->si_lwp->l_wchan == NULL);
    485 	KASSERT(si->si_active);
    486 
    487 	havelock = false;
    488 
    489 	/*
    490 	 * Note: due to priority inheritance we may have interrupted a
    491 	 * higher priority LWP.  Since the soft interrupt must be quick
    492 	 * and is non-preemptable, we don't bother yielding.
    493 	 */
    494 
    495 	while (!SIMPLEQ_EMPTY(&si->si_q)) {
    496 		/*
    497 		 * Pick the longest waiting handler to run.  We block
    498 		 * interrupts but do not lock in order to do this, as
    499 		 * we are protecting against the local CPU only.
    500 		 */
    501 		sh = SIMPLEQ_FIRST(&si->si_q);
    502 		SIMPLEQ_REMOVE_HEAD(&si->si_q, sh_q);
    503 		sh->sh_pending = 0;
    504 		splx(s);
    505 
    506 		/* Run the handler. */
    507 		if ((sh->sh_flags & SOFTINT_MPSAFE) == 0 && !havelock) {
    508 			KERNEL_LOCK(1, l);
    509 			havelock = true;
    510 		}
    511 		(*sh->sh_func)(sh->sh_arg);
    512 
    513 		(void)splhigh();
    514 	}
    515 
    516 	if (havelock) {
    517 		KERNEL_UNLOCK_ONE(l);
    518 	}
    519 
    520 	/*
    521 	 * Unlocked, but only for statistics.
    522 	 * Should be per-CPU to prevent cache ping-pong.
    523 	 */
    524 	uvmexp.softs++;
    525 
    526 	KASSERT(si->si_cpu == curcpu());
    527 	KASSERT(si->si_lwp->l_wchan == NULL);
    528 	KASSERT(si->si_active);
    529 	si->si_evcnt.ev_count++;
    530 	si->si_active = 0;
    531 }
    532 
    533 /*
    534  * softint_block:
    535  *
    536  *	Update statistics when the soft interrupt blocks.
    537  */
    538 void
    539 softint_block(lwp_t *l)
    540 {
    541 	softint_t *si = l->l_private;
    542 
    543 	KASSERT((l->l_pflag & LP_INTR) != 0);
    544 	si->si_evcnt_block.ev_count++;
    545 }
    546 
    547 /*
    548  * schednetisr:
    549  *
    550  *	Trigger a legacy network interrupt.  XXX Needs to go away.
    551  */
    552 void
    553 schednetisr(int isr)
    554 {
    555 
    556 	softint_schedule(softint_netisrs[isr]);
    557 }
    558 
    559 #ifndef __HAVE_FAST_SOFTINTS
    560 
    561 #ifdef PREEMPTION
    562 #error PREEMPTION requires __HAVE_FAST_SOFTINTS
    563 #endif
    564 
    565 /*
    566  * softint_init_md:
    567  *
    568  *	Slow path: perform machine-dependent initialization.
    569  */
    570 void
    571 softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
    572 {
    573 	softint_t *si;
    574 
    575 	*machdep = (1 << level);
    576 	si = l->l_private;
    577 
    578 	lwp_lock(l);
    579 	lwp_unlock_to(l, l->l_cpu->ci_schedstate.spc_mutex);
    580 	lwp_lock(l);
    581 	/* Cheat and make the KASSERT in softint_thread() happy. */
    582 	si->si_active = 1;
    583 	l->l_stat = LSRUN;
    584 	sched_enqueue(l, false);
    585 	lwp_unlock(l);
    586 }
    587 
    588 /*
    589  * softint_trigger:
    590  *
    591  *	Slow path: cause a soft interrupt handler to begin executing.
    592  *	Called at IPL_HIGH.
    593  */
    594 void
    595 softint_trigger(uintptr_t machdep)
    596 {
    597 	struct cpu_info *ci;
    598 	lwp_t *l;
    599 
    600 	l = curlwp;
    601 	ci = l->l_cpu;
    602 	ci->ci_data.cpu_softints |= machdep;
    603 	if (l == ci->ci_data.cpu_idlelwp) {
    604 		cpu_need_resched(ci, 0);
    605 	} else {
    606 		/* MI equivalent of aston() */
    607 		cpu_signotify(l);
    608 	}
    609 }
    610 
    611 /*
    612  * softint_thread:
    613  *
    614  *	Slow path: MI software interrupt dispatch.
    615  */
    616 void
    617 softint_thread(void *cookie)
    618 {
    619 	softint_t *si;
    620 	lwp_t *l;
    621 	int s;
    622 
    623 	l = curlwp;
    624 	si = l->l_private;
    625 
    626 	for (;;) {
    627 		/*
    628 		 * Clear pending status and run it.  We must drop the
    629 		 * spl before mi_switch(), since IPL_HIGH may be higher
    630 		 * than IPL_SCHED (and it is not safe to switch at a
    631 		 * higher level).
    632 		 */
    633 		s = splhigh();
    634 		l->l_cpu->ci_data.cpu_softints &= ~si->si_machdep;
    635 		softint_execute(si, l, s);
    636 		splx(s);
    637 
    638 		lwp_lock(l);
    639 		l->l_stat = LSIDL;
    640 		mi_switch(l);
    641 	}
    642 }
    643 
    644 /*
    645  * softint_picklwp:
    646  *
    647  *	Slow path: called from mi_switch() to pick the highest priority
    648  *	soft interrupt LWP that needs to run.
    649  */
    650 lwp_t *
    651 softint_picklwp(void)
    652 {
    653 	struct cpu_info *ci;
    654 	u_int mask;
    655 	softint_t *si;
    656 	lwp_t *l;
    657 
    658 	ci = curcpu();
    659 	si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int;
    660 	mask = ci->ci_data.cpu_softints;
    661 
    662 	if ((mask & (1 << SOFTINT_SERIAL)) != 0) {
    663 		l = si[SOFTINT_SERIAL].si_lwp;
    664 	} else if ((mask & (1 << SOFTINT_NET)) != 0) {
    665 		l = si[SOFTINT_NET].si_lwp;
    666 	} else if ((mask & (1 << SOFTINT_BIO)) != 0) {
    667 		l = si[SOFTINT_BIO].si_lwp;
    668 	} else if ((mask & (1 << SOFTINT_CLOCK)) != 0) {
    669 		l = si[SOFTINT_CLOCK].si_lwp;
    670 	} else {
    671 		panic("softint_picklwp");
    672 	}
    673 
    674 	return l;
    675 }
    676 
    677 /*
    678  * softint_overlay:
    679  *
    680  *	Slow path: called from lwp_userret() to run a soft interrupt
    681  *	within the context of a user thread.
    682  */
    683 void
    684 softint_overlay(void)
    685 {
    686 	struct cpu_info *ci;
    687 	u_int softints, oflag;
    688 	softint_t *si;
    689 	pri_t obase;
    690 	lwp_t *l;
    691 	int s;
    692 
    693 	l = curlwp;
    694 	ci = l->l_cpu;
    695 	si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int;
    696 
    697 	KASSERT((l->l_pflag & LP_INTR) == 0);
    698 
    699 	/* Arrange to elevate priority if the LWP blocks. */
    700 	s = splhigh();
    701 	obase = l->l_kpribase;
    702 	l->l_kpribase = PRI_KERNEL_RT;
    703 	oflag = l->l_pflag;
    704 	l->l_pflag = oflag | LP_INTR | LP_BOUND;
    705 	while ((softints = ci->ci_data.cpu_softints) != 0) {
    706 		if ((softints & (1 << SOFTINT_SERIAL)) != 0) {
    707 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_SERIAL);
    708 			softint_execute(&si[SOFTINT_SERIAL], l, s);
    709 			continue;
    710 		}
    711 		if ((softints & (1 << SOFTINT_NET)) != 0) {
    712 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_NET);
    713 			softint_execute(&si[SOFTINT_NET], l, s);
    714 			continue;
    715 		}
    716 		if ((softints & (1 << SOFTINT_BIO)) != 0) {
    717 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_BIO);
    718 			softint_execute(&si[SOFTINT_BIO], l, s);
    719 			continue;
    720 		}
    721 		if ((softints & (1 << SOFTINT_CLOCK)) != 0) {
    722 			ci->ci_data.cpu_softints &= ~(1 << SOFTINT_CLOCK);
    723 			softint_execute(&si[SOFTINT_CLOCK], l, s);
    724 			continue;
    725 		}
    726 	}
    727 	l->l_pflag = oflag;
    728 	l->l_kpribase = obase;
    729 	splx(s);
    730 }
    731 
    732 #else	/*  !__HAVE_FAST_SOFTINTS */
    733 
    734 /*
    735  * softint_thread:
    736  *
    737  *	Fast path: the LWP is switched to without restoring any state,
    738  *	so we should not arrive here - there is a direct handoff between
    739  *	the interrupt stub and softint_dispatch().
    740  */
    741 void
    742 softint_thread(void *cookie)
    743 {
    744 
    745 	panic("softint_thread");
    746 }
    747 
    748 /*
    749  * softint_dispatch:
    750  *
    751  *	Fast path: entry point from machine-dependent code.
    752  */
    753 void
    754 softint_dispatch(lwp_t *pinned, int s)
    755 {
    756 	struct bintime now;
    757 	softint_t *si;
    758 	u_int timing;
    759 	lwp_t *l;
    760 
    761 	l = curlwp;
    762 	si = l->l_private;
    763 
    764 	/*
    765 	 * Note the interrupted LWP, and mark the current LWP as running
    766 	 * before proceeding.  Although this must as a rule be done with
    767 	 * the LWP locked, at this point no external agents will want to
    768 	 * modify the interrupt LWP's state.
    769 	 */
    770 	timing = (softint_timing ? LW_TIMEINTR : 0);
    771 	l->l_switchto = pinned;
    772 	l->l_stat = LSONPROC;
    773 	l->l_flag |= (LW_RUNNING | timing);
    774 
    775 	/*
    776 	 * Dispatch the interrupt.  If softints are being timed, charge
    777 	 * for it.
    778 	 */
    779 	if (timing)
    780 		binuptime(&l->l_stime);
    781 	softint_execute(si, l, s);
    782 	if (timing) {
    783 		binuptime(&now);
    784 		updatertime(l, &now);
    785 		l->l_flag &= ~LW_TIMEINTR;
    786 	}
    787 
    788 	/*
    789 	 * If we blocked while handling the interrupt, the pinned LWP is
    790 	 * gone so switch to the idle LWP.  It will select a new LWP to
    791 	 * run.
    792 	 *
    793 	 * We must drop the priority level as switching at IPL_HIGH could
    794 	 * deadlock the system.  We have already set si->si_active = 0,
    795 	 * which means another interrupt at this level can be triggered.
    796 	 * That's not be a problem: we are lowering to level 's' which will
    797 	 * prevent softint_dispatch() from being reentered at level 's',
    798 	 * until the priority is finally dropped to IPL_NONE on entry to
    799 	 * the idle loop.
    800 	 */
    801 	l->l_stat = LSIDL;
    802 	if (l->l_switchto == NULL) {
    803 		splx(s);
    804 		pmap_deactivate(l);
    805 		lwp_exit_switchaway(l);
    806 		/* NOTREACHED */
    807 	}
    808 	l->l_switchto = NULL;
    809 	l->l_flag &= ~LW_RUNNING;
    810 }
    811 
    812 #endif	/* !__HAVE_FAST_SOFTINTS */
    813