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