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