xref: /freebsd/sys/kern/kern_intr.c (revision 4d29cb2db96d7ffe8dd025a29787918448894267)
1425f9fdaSStefan Eßer /*
2425f9fdaSStefan Eßer  * Copyright (c) 1997, Stefan Esser <se@freebsd.org>
3425f9fdaSStefan Eßer  * All rights reserved.
4425f9fdaSStefan Eßer  *
5425f9fdaSStefan Eßer  * Redistribution and use in source and binary forms, with or without
6425f9fdaSStefan Eßer  * modification, are permitted provided that the following conditions
7425f9fdaSStefan Eßer  * are met:
8425f9fdaSStefan Eßer  * 1. Redistributions of source code must retain the above copyright
9425f9fdaSStefan Eßer  *    notice unmodified, this list of conditions, and the following
10425f9fdaSStefan Eßer  *    disclaimer.
11425f9fdaSStefan Eßer  * 2. Redistributions in binary form must reproduce the above copyright
12425f9fdaSStefan Eßer  *    notice, this list of conditions and the following disclaimer in the
13425f9fdaSStefan Eßer  *    documentation and/or other materials provided with the distribution.
14425f9fdaSStefan Eßer  *
15425f9fdaSStefan Eßer  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16425f9fdaSStefan Eßer  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17425f9fdaSStefan Eßer  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18425f9fdaSStefan Eßer  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
19425f9fdaSStefan Eßer  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
20425f9fdaSStefan Eßer  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
21425f9fdaSStefan Eßer  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22425f9fdaSStefan Eßer  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23425f9fdaSStefan Eßer  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
24425f9fdaSStefan Eßer  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25425f9fdaSStefan Eßer  *
26c3aac50fSPeter Wemm  * $FreeBSD$
27425f9fdaSStefan Eßer  *
28425f9fdaSStefan Eßer  */
29425f9fdaSStefan Eßer 
303900ddb2SDoug Rabson 
311c5bb3eaSPeter Wemm #include <sys/param.h>
329a94c9c5SJohn Baldwin #include <sys/bus.h>
339a94c9c5SJohn Baldwin #include <sys/rtprio.h>
34425f9fdaSStefan Eßer #include <sys/systm.h>
3568352337SDoug Rabson #include <sys/interrupt.h>
361931cf94SJohn Baldwin #include <sys/kernel.h>
371931cf94SJohn Baldwin #include <sys/kthread.h>
381931cf94SJohn Baldwin #include <sys/ktr.h>
39f34fa851SJohn Baldwin #include <sys/lock.h>
401931cf94SJohn Baldwin #include <sys/malloc.h>
4135e0e5b3SJohn Baldwin #include <sys/mutex.h>
421931cf94SJohn Baldwin #include <sys/proc.h>
433e5da754SJohn Baldwin #include <sys/random.h>
44b4151f71SJohn Baldwin #include <sys/resourcevar.h>
451931cf94SJohn Baldwin #include <sys/unistd.h>
461931cf94SJohn Baldwin #include <sys/vmmeter.h>
471931cf94SJohn Baldwin #include <machine/atomic.h>
481931cf94SJohn Baldwin #include <machine/cpu.h>
498088699fSJohn Baldwin #include <machine/md_var.h>
50b4151f71SJohn Baldwin #include <machine/stdarg.h>
51425f9fdaSStefan Eßer 
528088699fSJohn Baldwin #include <net/netisr.h>		/* prototype for legacy_setsoftnet */
5318c5a6c4SBruce Evans 
543e5da754SJohn Baldwin struct	int_entropy {
553e5da754SJohn Baldwin 	struct	proc *proc;
563e5da754SJohn Baldwin 	int	vector;
573e5da754SJohn Baldwin };
583e5da754SJohn Baldwin 
59b4151f71SJohn Baldwin void	*net_ih;
60b4151f71SJohn Baldwin void	*vm_ih;
61b4151f71SJohn Baldwin void	*softclock_ih;
628088699fSJohn Baldwin struct	ithd *clk_ithd;
638088699fSJohn Baldwin struct	ithd *tty_ithd;
641931cf94SJohn Baldwin 
65b4151f71SJohn Baldwin static MALLOC_DEFINE(M_ITHREAD, "ithread", "Interrupt Threads");
66b4151f71SJohn Baldwin 
67b4151f71SJohn Baldwin static void	ithread_update(struct ithd *);
68b4151f71SJohn Baldwin static void	ithread_loop(void *);
691931cf94SJohn Baldwin static void	start_softintr(void *);
708088699fSJohn Baldwin static void	swi_net(void *);
7118c5a6c4SBruce Evans 
72b4151f71SJohn Baldwin u_char
73b4151f71SJohn Baldwin ithread_priority(enum intr_type flags)
749a94c9c5SJohn Baldwin {
75b4151f71SJohn Baldwin 	u_char pri;
769a94c9c5SJohn Baldwin 
77b4151f71SJohn Baldwin 	flags &= (INTR_TYPE_TTY | INTR_TYPE_BIO | INTR_TYPE_NET |
78b4151f71SJohn Baldwin 	    INTR_TYPE_CAM | INTR_TYPE_MISC | INTR_TYPE_CLK);
799a94c9c5SJohn Baldwin 	switch (flags) {
80b4151f71SJohn Baldwin 	case INTR_TYPE_TTY:
819a94c9c5SJohn Baldwin 		pri = PI_TTYLOW;
829a94c9c5SJohn Baldwin 		break;
839a94c9c5SJohn Baldwin 	case INTR_TYPE_BIO:
849a94c9c5SJohn Baldwin 		/*
859a94c9c5SJohn Baldwin 		 * XXX We need to refine this.  BSD/OS distinguishes
869a94c9c5SJohn Baldwin 		 * between tape and disk priorities.
879a94c9c5SJohn Baldwin 		 */
889a94c9c5SJohn Baldwin 		pri = PI_DISK;
899a94c9c5SJohn Baldwin 		break;
909a94c9c5SJohn Baldwin 	case INTR_TYPE_NET:
919a94c9c5SJohn Baldwin 		pri = PI_NET;
929a94c9c5SJohn Baldwin 		break;
939a94c9c5SJohn Baldwin 	case INTR_TYPE_CAM:
949a94c9c5SJohn Baldwin 		pri = PI_DISK;          /* XXX or PI_CAM? */
959a94c9c5SJohn Baldwin 		break;
96b4151f71SJohn Baldwin 	case INTR_TYPE_CLK:
97b4151f71SJohn Baldwin 		pri = PI_REALTIME;
98b4151f71SJohn Baldwin 		break;
999a94c9c5SJohn Baldwin 	case INTR_TYPE_MISC:
1009a94c9c5SJohn Baldwin 		pri = PI_DULL;          /* don't care */
1019a94c9c5SJohn Baldwin 		break;
1029a94c9c5SJohn Baldwin 	default:
103b4151f71SJohn Baldwin 		/* We didn't specify an interrupt level. */
1049a94c9c5SJohn Baldwin 		panic("ithread_priority: no interrupt type in flags");
1059a94c9c5SJohn Baldwin 	}
1069a94c9c5SJohn Baldwin 
1079a94c9c5SJohn Baldwin 	return pri;
1089a94c9c5SJohn Baldwin }
1099a94c9c5SJohn Baldwin 
110b4151f71SJohn Baldwin /*
111b4151f71SJohn Baldwin  * Regenerate the name (p_comm) and priority for a threaded interrupt thread.
112b4151f71SJohn Baldwin  */
113b4151f71SJohn Baldwin static void
114b4151f71SJohn Baldwin ithread_update(struct ithd *ithd)
115b4151f71SJohn Baldwin {
116b4151f71SJohn Baldwin 	struct intrhand *ih;
117b4151f71SJohn Baldwin 	struct proc *p;
118b4151f71SJohn Baldwin 	int entropy;
1198088699fSJohn Baldwin 
1204d29cb2dSJohn Baldwin 	mtx_assert(&ithd->it_lock, MA_OWNED);
121b4151f71SJohn Baldwin 	p = ithd->it_proc;
122b4151f71SJohn Baldwin 	if (p == NULL)
123b4151f71SJohn Baldwin 		return;
124b4151f71SJohn Baldwin 
125b4151f71SJohn Baldwin 	strncpy(p->p_comm, ithd->it_name, sizeof(ithd->it_name));
126b4151f71SJohn Baldwin 	ih = TAILQ_FIRST(&ithd->it_handlers);
127b4151f71SJohn Baldwin 	if (ih == NULL) {
128d5a08a60SJake Burkholder 		p->p_pri.pri_level = PRI_MAX_ITHD;
129b4151f71SJohn Baldwin 		ithd->it_flags &= ~IT_ENTROPY;
130b4151f71SJohn Baldwin 		return;
131b4151f71SJohn Baldwin 	}
132b4151f71SJohn Baldwin 
133b4151f71SJohn Baldwin 	entropy = 0;
134d5a08a60SJake Burkholder 	p->p_pri.pri_level = ih->ih_pri;
1355b270b2aSJake Burkholder 	p->p_pri.pri_native = ih->ih_pri;
136b4151f71SJohn Baldwin 	TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
137b4151f71SJohn Baldwin 		if (strlen(p->p_comm) + strlen(ih->ih_name) + 1 <
138b4151f71SJohn Baldwin 		    sizeof(p->p_comm)) {
139b4151f71SJohn Baldwin 			strcat(p->p_comm, " ");
140b4151f71SJohn Baldwin 			strcat(p->p_comm, ih->ih_name);
141b4151f71SJohn Baldwin 		} else if (strlen(p->p_comm) + 1 == sizeof(p->p_comm)) {
142b4151f71SJohn Baldwin 			if (p->p_comm[sizeof(p->p_comm) - 2] == '+')
143b4151f71SJohn Baldwin 				p->p_comm[sizeof(p->p_comm) - 2] = '*';
144b4151f71SJohn Baldwin 			else
145b4151f71SJohn Baldwin 				p->p_comm[sizeof(p->p_comm) - 2] = '+';
146b4151f71SJohn Baldwin 		} else
147b4151f71SJohn Baldwin 			strcat(p->p_comm, "+");
148b4151f71SJohn Baldwin 		if (ih->ih_flags & IH_ENTROPY)
149b4151f71SJohn Baldwin 			entropy++;
150b4151f71SJohn Baldwin 	}
151b4151f71SJohn Baldwin 
1523e5da754SJohn Baldwin 	if (entropy)
153b4151f71SJohn Baldwin 		ithd->it_flags |= IT_ENTROPY;
154b4151f71SJohn Baldwin 	else
155b4151f71SJohn Baldwin 		ithd->it_flags &= ~IT_ENTROPY;
156addec20cSJohn Baldwin 
157addec20cSJohn Baldwin 	CTR1(KTR_INTR, __func__ ": updated %s\n", p->p_comm);
158b4151f71SJohn Baldwin }
159b4151f71SJohn Baldwin 
160b4151f71SJohn Baldwin int
161b4151f71SJohn Baldwin ithread_create(struct ithd **ithread, int vector, int flags,
162b4151f71SJohn Baldwin     void (*disable)(int), void (*enable)(int), const char *fmt, ...)
163b4151f71SJohn Baldwin {
164b4151f71SJohn Baldwin 	struct ithd *ithd;
165b4151f71SJohn Baldwin 	struct proc *p;
166b4151f71SJohn Baldwin 	int error;
167b4151f71SJohn Baldwin 	va_list ap;
168b4151f71SJohn Baldwin 
1693e5da754SJohn Baldwin 	/* The only valid flag during creation is IT_SOFT. */
1703e5da754SJohn Baldwin 	if ((flags & ~IT_SOFT) != 0)
1713e5da754SJohn Baldwin 		return (EINVAL);
1723e5da754SJohn Baldwin 
173b4151f71SJohn Baldwin 	ithd = malloc(sizeof(struct ithd), M_ITHREAD, M_WAITOK | M_ZERO);
174b4151f71SJohn Baldwin 	ithd->it_vector = vector;
175b4151f71SJohn Baldwin 	ithd->it_disable = disable;
176b4151f71SJohn Baldwin 	ithd->it_enable = enable;
177b4151f71SJohn Baldwin 	ithd->it_flags = flags;
178b4151f71SJohn Baldwin 	TAILQ_INIT(&ithd->it_handlers);
1794d29cb2dSJohn Baldwin 	mtx_init(&ithd->it_lock, "ithread", MTX_DEF);
1804d29cb2dSJohn Baldwin 	mtx_lock(&ithd->it_lock);
181b4151f71SJohn Baldwin 
182b4151f71SJohn Baldwin 	va_start(ap, fmt);
183b4151f71SJohn Baldwin 	vsnprintf(ithd->it_name, sizeof(ithd->it_name), fmt, ap);
184b4151f71SJohn Baldwin 	va_end(ap);
185b4151f71SJohn Baldwin 
186b4151f71SJohn Baldwin 	error = kthread_create(ithread_loop, ithd, &p, RFSTOPPED | RFHIGHPID,
1871f723035SJohn Baldwin 	    "%s", ithd->it_name);
188b4151f71SJohn Baldwin 	if (error) {
1894d29cb2dSJohn Baldwin 		mtx_destroy(&ithd->it_lock);
190b4151f71SJohn Baldwin 		free(ithd, M_ITHREAD);
191b4151f71SJohn Baldwin 		return (error);
192b4151f71SJohn Baldwin 	}
193d5a08a60SJake Burkholder 	p->p_pri.pri_class = PRI_ITHD;
194d5a08a60SJake Burkholder 	p->p_pri.pri_level = PRI_MAX_ITHD;
195b4151f71SJohn Baldwin 	p->p_stat = SWAIT;
196b4151f71SJohn Baldwin 	ithd->it_proc = p;
197b4151f71SJohn Baldwin 	p->p_ithd = ithd;
198b4151f71SJohn Baldwin 	if (ithread != NULL)
199b4151f71SJohn Baldwin 		*ithread = ithd;
2004d29cb2dSJohn Baldwin 	mtx_unlock(&ithd->it_lock);
201b4151f71SJohn Baldwin 
202addec20cSJohn Baldwin 	CTR1(KTR_INTR, __func__ ": created %s", ithd->it_name);
203b4151f71SJohn Baldwin 	return (0);
204b4151f71SJohn Baldwin }
205b4151f71SJohn Baldwin 
206b4151f71SJohn Baldwin int
207b4151f71SJohn Baldwin ithread_destroy(struct ithd *ithread)
208b4151f71SJohn Baldwin {
209b4151f71SJohn Baldwin 
2104d29cb2dSJohn Baldwin 	if (ithread == NULL)
211b4151f71SJohn Baldwin 		return (EINVAL);
212b4151f71SJohn Baldwin 
2134d29cb2dSJohn Baldwin 	mtx_lock(&ithread->it_lock);
2144d29cb2dSJohn Baldwin 	if (!TAILQ_EMPTY(&ithread->it_handlers)) {
2154d29cb2dSJohn Baldwin 		mtx_unlock(&ithread->it_lock);
2164d29cb2dSJohn Baldwin 		return (EINVAL);
2174d29cb2dSJohn Baldwin 	}
218b4151f71SJohn Baldwin 	ithread->it_flags |= IT_DEAD;
2194d29cb2dSJohn Baldwin 	mtx_lock_spin(&sched_lock);
220b4151f71SJohn Baldwin 	if (ithread->it_proc->p_stat == SWAIT) {
221b4151f71SJohn Baldwin 		ithread->it_proc->p_stat = SRUN;
222b4151f71SJohn Baldwin 		setrunqueue(ithread->it_proc);
223b4151f71SJohn Baldwin 	}
224b4151f71SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
2254d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
226addec20cSJohn Baldwin 	CTR1(KTR_INTR, __func__ ": killing %s", ithread->it_name);
227b4151f71SJohn Baldwin 	return (0);
228b4151f71SJohn Baldwin }
229b4151f71SJohn Baldwin 
230b4151f71SJohn Baldwin int
231b4151f71SJohn Baldwin ithread_add_handler(struct ithd* ithread, const char *name,
232b4151f71SJohn Baldwin     driver_intr_t handler, void *arg, u_char pri, enum intr_type flags,
233b4151f71SJohn Baldwin     void **cookiep)
234b4151f71SJohn Baldwin {
235b4151f71SJohn Baldwin 	struct intrhand *ih, *temp_ih;
236b4151f71SJohn Baldwin 
237b4151f71SJohn Baldwin 	if (ithread == NULL || name == NULL || handler == NULL)
238b4151f71SJohn Baldwin 		return (EINVAL);
239b4151f71SJohn Baldwin 	if ((flags & INTR_FAST) !=0)
240b4151f71SJohn Baldwin 		flags |= INTR_EXCL;
241b4151f71SJohn Baldwin 
242b4151f71SJohn Baldwin 	ih = malloc(sizeof(struct intrhand), M_ITHREAD, M_WAITOK | M_ZERO);
243b4151f71SJohn Baldwin 	ih->ih_handler = handler;
244b4151f71SJohn Baldwin 	ih->ih_argument = arg;
245b4151f71SJohn Baldwin 	ih->ih_name = name;
246b4151f71SJohn Baldwin 	ih->ih_ithread = ithread;
247b4151f71SJohn Baldwin 	ih->ih_pri = pri;
248b4151f71SJohn Baldwin 	if (flags & INTR_FAST)
249b4151f71SJohn Baldwin 		ih->ih_flags = IH_FAST | IH_EXCLUSIVE;
250b4151f71SJohn Baldwin 	else if (flags & INTR_EXCL)
251b4151f71SJohn Baldwin 		ih->ih_flags = IH_EXCLUSIVE;
252b4151f71SJohn Baldwin 	if (flags & INTR_MPSAFE)
253b4151f71SJohn Baldwin 		ih->ih_flags |= IH_MPSAFE;
254b4151f71SJohn Baldwin 	if (flags & INTR_ENTROPY)
255b4151f71SJohn Baldwin 		ih->ih_flags |= IH_ENTROPY;
256b4151f71SJohn Baldwin 
2574d29cb2dSJohn Baldwin 	mtx_lock(&ithread->it_lock);
258b4151f71SJohn Baldwin 	if ((flags & INTR_EXCL) !=0 && !TAILQ_EMPTY(&ithread->it_handlers))
259b4151f71SJohn Baldwin 		goto fail;
260b4151f71SJohn Baldwin 	if (!TAILQ_EMPTY(&ithread->it_handlers) &&
261b4151f71SJohn Baldwin 	    (TAILQ_FIRST(&ithread->it_handlers)->ih_flags & IH_EXCLUSIVE) != 0)
262b4151f71SJohn Baldwin 		goto fail;
263b4151f71SJohn Baldwin 
264b4151f71SJohn Baldwin 	TAILQ_FOREACH(temp_ih, &ithread->it_handlers, ih_next)
265b4151f71SJohn Baldwin 	    if (temp_ih->ih_pri > ih->ih_pri)
266b4151f71SJohn Baldwin 		    break;
267b4151f71SJohn Baldwin 	if (temp_ih == NULL)
268b4151f71SJohn Baldwin 		TAILQ_INSERT_TAIL(&ithread->it_handlers, ih, ih_next);
269b4151f71SJohn Baldwin 	else
270b4151f71SJohn Baldwin 		TAILQ_INSERT_BEFORE(temp_ih, ih, ih_next);
271b4151f71SJohn Baldwin 	ithread_update(ithread);
2724d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
273b4151f71SJohn Baldwin 
274b4151f71SJohn Baldwin 	if (cookiep != NULL)
275b4151f71SJohn Baldwin 		*cookiep = ih;
276addec20cSJohn Baldwin 	CTR2(KTR_INTR, __func__ ": added %s to %s", ih->ih_name,
277addec20cSJohn Baldwin 	    ithread->it_name);
278b4151f71SJohn Baldwin 	return (0);
279b4151f71SJohn Baldwin 
280b4151f71SJohn Baldwin fail:
2814d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
282b4151f71SJohn Baldwin 	free(ih, M_ITHREAD);
283b4151f71SJohn Baldwin 	return (EINVAL);
284b4151f71SJohn Baldwin }
285b4151f71SJohn Baldwin 
286b4151f71SJohn Baldwin int
287b4151f71SJohn Baldwin ithread_remove_handler(void *cookie)
288b4151f71SJohn Baldwin {
289b4151f71SJohn Baldwin 	struct intrhand *handler = (struct intrhand *)cookie;
290b4151f71SJohn Baldwin 	struct ithd *ithread;
291b4151f71SJohn Baldwin #ifdef INVARIANTS
292b4151f71SJohn Baldwin 	struct intrhand *ih;
293b4151f71SJohn Baldwin #endif
294b4151f71SJohn Baldwin 
2953e5da754SJohn Baldwin 	if (handler == NULL)
296b4151f71SJohn Baldwin 		return (EINVAL);
29776bd604eSJohn Baldwin 	ithread = handler->ih_ithread;
29876bd604eSJohn Baldwin 	KASSERT(ithread != NULL,
2993e5da754SJohn Baldwin 	    ("interrupt handler \"%s\" has a NULL interrupt thread",
3003e5da754SJohn Baldwin 		handler->ih_name));
301addec20cSJohn Baldwin 	CTR2(KTR_INTR, __func__ ": removing %s from %s", handler->ih_name,
302addec20cSJohn Baldwin 	    ithread->it_name);
3034d29cb2dSJohn Baldwin 	mtx_lock(&ithread->it_lock);
304b4151f71SJohn Baldwin #ifdef INVARIANTS
305b4151f71SJohn Baldwin 	TAILQ_FOREACH(ih, &ithread->it_handlers, ih_next)
3063e5da754SJohn Baldwin 		if (ih == handler)
3073e5da754SJohn Baldwin 			goto ok;
3084d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
3093e5da754SJohn Baldwin 	panic("interrupt handler \"%s\" not found in interrupt thread \"%s\"",
3103e5da754SJohn Baldwin 	    ih->ih_name, ithread->it_name);
3113e5da754SJohn Baldwin ok:
312b4151f71SJohn Baldwin #endif
313de271f01SJohn Baldwin 	/*
314de271f01SJohn Baldwin 	 * If the interrupt thread is already running, then just mark this
315de271f01SJohn Baldwin 	 * handler as being dead and let the ithread do the actual removal.
316de271f01SJohn Baldwin 	 */
317de271f01SJohn Baldwin 	mtx_lock_spin(&sched_lock);
318de271f01SJohn Baldwin 	if (ithread->it_proc->p_stat != SWAIT) {
319de271f01SJohn Baldwin 		handler->ih_flags |= IH_DEAD;
320de271f01SJohn Baldwin 
321de271f01SJohn Baldwin 		/*
322de271f01SJohn Baldwin 		 * Ensure that the thread will process the handler list
323de271f01SJohn Baldwin 		 * again and remove this handler if it has already passed
324de271f01SJohn Baldwin 		 * it on the list.
325de271f01SJohn Baldwin 		 */
326de271f01SJohn Baldwin 		ithread->it_need = 1;
3274d29cb2dSJohn Baldwin 	} else
328b4151f71SJohn Baldwin 		TAILQ_REMOVE(&ithread->it_handlers, handler, ih_next);
329de271f01SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
3304d29cb2dSJohn Baldwin 	if ((handler->ih_flags & IH_DEAD) != 0)
3314d29cb2dSJohn Baldwin 		msleep(handler, &ithread->it_lock, PUSER, "itrmh", 0);
3324d29cb2dSJohn Baldwin 	ithread_update(ithread);
3334d29cb2dSJohn Baldwin 	mtx_unlock(&ithread->it_lock);
334b4151f71SJohn Baldwin 	free(handler, M_ITHREAD);
335b4151f71SJohn Baldwin 	return (0);
336b4151f71SJohn Baldwin }
337b4151f71SJohn Baldwin 
338b4151f71SJohn Baldwin int
3393e5da754SJohn Baldwin ithread_schedule(struct ithd *ithread, int do_switch)
3403e5da754SJohn Baldwin {
3413e5da754SJohn Baldwin 	struct int_entropy entropy;
3423e5da754SJohn Baldwin 	struct proc *p;
343b944b903SJohn Baldwin 	critical_t savecrit;
3443e5da754SJohn Baldwin 
3453e5da754SJohn Baldwin 	/*
3463e5da754SJohn Baldwin 	 * If no ithread or no handlers, then we have a stray interrupt.
3473e5da754SJohn Baldwin 	 */
3483e5da754SJohn Baldwin 	if ((ithread == NULL) || TAILQ_EMPTY(&ithread->it_handlers))
3493e5da754SJohn Baldwin 		return (EINVAL);
3503e5da754SJohn Baldwin 
3513e5da754SJohn Baldwin 	/*
3523e5da754SJohn Baldwin 	 * If any of the handlers for this ithread claim to be good
3533e5da754SJohn Baldwin 	 * sources of entropy, then gather some.
3543e5da754SJohn Baldwin 	 */
3553e5da754SJohn Baldwin 	if (harvest.interrupt && ithread->it_flags & IT_ENTROPY) {
3563e5da754SJohn Baldwin 		entropy.vector = ithread->it_vector;
3573e5da754SJohn Baldwin 		entropy.proc = CURPROC;
3583e5da754SJohn Baldwin 		random_harvest(&entropy, sizeof(entropy), 2, 0,
3593e5da754SJohn Baldwin 		    RANDOM_INTERRUPT);
3603e5da754SJohn Baldwin 	}
3613e5da754SJohn Baldwin 
3623e5da754SJohn Baldwin 	p = ithread->it_proc;
363653dd8c2SJohn Baldwin 	KASSERT(p != NULL, ("ithread %s has no process", ithread->it_name));
3643e5da754SJohn Baldwin 	CTR3(KTR_INTR, __func__ ": pid %d: (%s) need = %d", p->p_pid, p->p_comm,
3653e5da754SJohn Baldwin 	    ithread->it_need);
3663e5da754SJohn Baldwin 
3673e5da754SJohn Baldwin 	/*
3683e5da754SJohn Baldwin 	 * Set it_need to tell the thread to keep running if it is already
3693e5da754SJohn Baldwin 	 * running.  Then, grab sched_lock and see if we actually need to
3703e5da754SJohn Baldwin 	 * put this thread on the runqueue.  If so and the do_switch flag is
3713e5da754SJohn Baldwin 	 * true, then switch to the ithread immediately.  Otherwise, use
3723e5da754SJohn Baldwin 	 * need_resched() to guarantee that this ithread will run before any
3733e5da754SJohn Baldwin 	 * userland processes.
3743e5da754SJohn Baldwin 	 */
3753e5da754SJohn Baldwin 	ithread->it_need = 1;
3763e5da754SJohn Baldwin 	mtx_lock_spin(&sched_lock);
3773e5da754SJohn Baldwin 	if (p->p_stat == SWAIT) {
3783e5da754SJohn Baldwin 		CTR1(KTR_INTR, __func__ ": setrunqueue %d", p->p_pid);
3793e5da754SJohn Baldwin 		p->p_stat = SRUN;
3803e5da754SJohn Baldwin 		setrunqueue(p);
381653dd8c2SJohn Baldwin 		if (do_switch && curproc->p_stat == SRUN) {
382b944b903SJohn Baldwin 			savecrit = sched_lock.mtx_savecrit;
3833e5da754SJohn Baldwin 			mtx_intr_enable(&sched_lock);
3843e5da754SJohn Baldwin 			if (curproc != PCPU_GET(idleproc))
3853e5da754SJohn Baldwin 				setrunqueue(curproc);
3863e5da754SJohn Baldwin 			curproc->p_stats->p_ru.ru_nvcsw++;
3873e5da754SJohn Baldwin 			mi_switch();
388b944b903SJohn Baldwin 			sched_lock.mtx_savecrit = savecrit;
3893e5da754SJohn Baldwin 		} else
3906caa8a15SJohn Baldwin 			need_resched(curproc);
3913e5da754SJohn Baldwin 	} else {
3923e5da754SJohn Baldwin 		CTR3(KTR_INTR, __func__ ": pid %d: it_need %d, state %d",
3933e5da754SJohn Baldwin 		    p->p_pid, ithread->it_need, p->p_stat);
3943e5da754SJohn Baldwin 	}
3953e5da754SJohn Baldwin 	mtx_unlock_spin(&sched_lock);
3963e5da754SJohn Baldwin 
3973e5da754SJohn Baldwin 	return (0);
3983e5da754SJohn Baldwin }
3993e5da754SJohn Baldwin 
4003e5da754SJohn Baldwin int
401b4151f71SJohn Baldwin swi_add(struct ithd **ithdp, const char *name, driver_intr_t handler,
402b4151f71SJohn Baldwin 	    void *arg, int pri, enum intr_type flags, void **cookiep)
4038088699fSJohn Baldwin {
4048088699fSJohn Baldwin 	struct ithd *ithd;
405b4151f71SJohn Baldwin 	int error;
4068088699fSJohn Baldwin 
4073e5da754SJohn Baldwin 	if (flags & (INTR_FAST | INTR_ENTROPY))
4083e5da754SJohn Baldwin 		return (EINVAL);
4093e5da754SJohn Baldwin 
4108088699fSJohn Baldwin 	ithd = (ithdp != NULL) ? *ithdp : NULL;
4118088699fSJohn Baldwin 
4123e5da754SJohn Baldwin 	if (ithd != NULL) {
4133e5da754SJohn Baldwin 		if ((ithd->it_flags & IT_SOFT) == 0)
4143e5da754SJohn Baldwin 			return(EINVAL);
4153e5da754SJohn Baldwin 	} else {
416b4151f71SJohn Baldwin 		error = ithread_create(&ithd, pri, IT_SOFT, NULL, NULL,
417b4151f71SJohn Baldwin 		    "swi%d:", pri);
4188088699fSJohn Baldwin 		if (error)
419b4151f71SJohn Baldwin 			return (error);
420b4151f71SJohn Baldwin 
4218088699fSJohn Baldwin 		if (ithdp != NULL)
4228088699fSJohn Baldwin 			*ithdp = ithd;
4238088699fSJohn Baldwin 	}
424d5a08a60SJake Burkholder 	return (ithread_add_handler(ithd, name, handler, arg,
425d5a08a60SJake Burkholder 		    (pri * RQ_PPQ) + PI_SOFT, flags, cookiep));
4268088699fSJohn Baldwin }
4278088699fSJohn Baldwin 
4288088699fSJohn Baldwin 
4291931cf94SJohn Baldwin /*
4308088699fSJohn Baldwin  * Schedule a heavyweight software interrupt process.
4311931cf94SJohn Baldwin  */
4321931cf94SJohn Baldwin void
433b4151f71SJohn Baldwin swi_sched(void *cookie, int flags)
4341931cf94SJohn Baldwin {
435b4151f71SJohn Baldwin 	struct intrhand *ih = (struct intrhand *)cookie;
436b4151f71SJohn Baldwin 	struct ithd *it = ih->ih_ithread;
4373e5da754SJohn Baldwin 	int error;
4388088699fSJohn Baldwin 
4391931cf94SJohn Baldwin 	atomic_add_int(&cnt.v_intr, 1); /* one more global interrupt */
4401931cf94SJohn Baldwin 
441b4151f71SJohn Baldwin 	CTR3(KTR_INTR, "swi_sched pid %d(%s) need=%d",
44260f2b032SJohn Baldwin 		it->it_proc->p_pid, it->it_proc->p_comm, it->it_need);
4431931cf94SJohn Baldwin 
4441931cf94SJohn Baldwin 	/*
4453e5da754SJohn Baldwin 	 * Set ih_need for this handler so that if the ithread is already
4463e5da754SJohn Baldwin 	 * running it will execute this handler on the next pass.  Otherwise,
4473e5da754SJohn Baldwin 	 * it will execute it the next time it runs.
4481931cf94SJohn Baldwin 	 */
449b4151f71SJohn Baldwin 	atomic_store_rel_int(&ih->ih_need, 1);
450b4151f71SJohn Baldwin 	if (!(flags & SWI_DELAY)) {
4513e5da754SJohn Baldwin 		error = ithread_schedule(it, !cold && flags & SWI_SWITCH);
4523e5da754SJohn Baldwin 		KASSERT(error == 0, ("stray software interrupt"));
4538088699fSJohn Baldwin 	}
4548088699fSJohn Baldwin }
4558088699fSJohn Baldwin 
4568088699fSJohn Baldwin /*
457b4151f71SJohn Baldwin  * This is the main code for interrupt threads.
4588088699fSJohn Baldwin  */
4598088699fSJohn Baldwin void
460b4151f71SJohn Baldwin ithread_loop(void *arg)
4618088699fSJohn Baldwin {
462b4151f71SJohn Baldwin 	struct ithd *ithd;		/* our thread context */
4638088699fSJohn Baldwin 	struct intrhand *ih;		/* and our interrupt handler chain */
464b4151f71SJohn Baldwin 	struct proc *p;
4658088699fSJohn Baldwin 
466b4151f71SJohn Baldwin 	p = curproc;
467b4151f71SJohn Baldwin 	ithd = (struct ithd *)arg;	/* point to myself */
468b4151f71SJohn Baldwin 	KASSERT(ithd->it_proc == p && p->p_ithd == ithd,
469b4151f71SJohn Baldwin 	    (__func__ ": ithread and proc linkage out of sync"));
4708088699fSJohn Baldwin 
4718088699fSJohn Baldwin 	/*
4728088699fSJohn Baldwin 	 * As long as we have interrupts outstanding, go through the
4738088699fSJohn Baldwin 	 * list of handlers, giving each one a go at it.
4748088699fSJohn Baldwin 	 */
4758088699fSJohn Baldwin 	for (;;) {
476b4151f71SJohn Baldwin 		/*
477b4151f71SJohn Baldwin 		 * If we are an orphaned thread, then just die.
478b4151f71SJohn Baldwin 		 */
479b4151f71SJohn Baldwin 		if (ithd->it_flags & IT_DEAD) {
480b4151f71SJohn Baldwin 			CTR2(KTR_INTR, __func__ ": pid %d: (%s) exiting",
481b4151f71SJohn Baldwin 			    p->p_pid, p->p_comm);
482b4151f71SJohn Baldwin 			p->p_ithd = NULL;
4834d29cb2dSJohn Baldwin 			mtx_destroy(&ithd->it_lock);
484b4151f71SJohn Baldwin 			mtx_lock(&Giant);
485b4151f71SJohn Baldwin 			free(ithd, M_ITHREAD);
486b4151f71SJohn Baldwin 			kthread_exit(0);
487b4151f71SJohn Baldwin 		}
488b4151f71SJohn Baldwin 
489b4151f71SJohn Baldwin 		CTR3(KTR_INTR, __func__ ": pid %d: (%s) need=%d",
490b4151f71SJohn Baldwin 		     p->p_pid, p->p_comm, ithd->it_need);
491b4151f71SJohn Baldwin 		while (ithd->it_need) {
4928088699fSJohn Baldwin 			/*
4938088699fSJohn Baldwin 			 * Service interrupts.  If another interrupt
4948088699fSJohn Baldwin 			 * arrives while we are running, they will set
4958088699fSJohn Baldwin 			 * it_need to denote that we should make
4968088699fSJohn Baldwin 			 * another pass.
4978088699fSJohn Baldwin 			 */
498b4151f71SJohn Baldwin 			atomic_store_rel_int(&ithd->it_need, 0);
499de271f01SJohn Baldwin restart:
500b4151f71SJohn Baldwin 			TAILQ_FOREACH(ih, &ithd->it_handlers, ih_next) {
501b4151f71SJohn Baldwin 				if (ithd->it_flags & IT_SOFT && !ih->ih_need)
5028088699fSJohn Baldwin 					continue;
503b4151f71SJohn Baldwin 				atomic_store_rel_int(&ih->ih_need, 0);
5048088699fSJohn Baldwin 				CTR5(KTR_INTR,
505b4151f71SJohn Baldwin 				    __func__ ": pid %d ih=%p: %p(%p) flg=%x",
5068088699fSJohn Baldwin 				    p->p_pid, (void *)ih,
5078088699fSJohn Baldwin 				    (void *)ih->ih_handler, ih->ih_argument,
5088088699fSJohn Baldwin 				    ih->ih_flags);
5098088699fSJohn Baldwin 
510de271f01SJohn Baldwin 				if ((ih->ih_flags & IH_DEAD) != 0) {
5114d29cb2dSJohn Baldwin 					mtx_lock(&ithd->it_lock);
512de271f01SJohn Baldwin 					TAILQ_REMOVE(&ithd->it_handlers, ih,
513de271f01SJohn Baldwin 					    ih_next);
5144d29cb2dSJohn Baldwin 					wakeup(ih);
5154d29cb2dSJohn Baldwin 					mtx_unlock(&ithd->it_lock);
516de271f01SJohn Baldwin 					goto restart;
517de271f01SJohn Baldwin 				}
5184d29cb2dSJohn Baldwin 				if ((ih->ih_flags & IH_MPSAFE) == 0)
5194d29cb2dSJohn Baldwin 					mtx_lock(&Giant);
5208088699fSJohn Baldwin 				ih->ih_handler(ih->ih_argument);
521b4151f71SJohn Baldwin 				if ((ih->ih_flags & IH_MPSAFE) == 0)
5229ed346baSBosko Milekic 					mtx_unlock(&Giant);
5238088699fSJohn Baldwin 			}
5248088699fSJohn Baldwin 		}
5258088699fSJohn Baldwin 
5268088699fSJohn Baldwin 		/*
5278088699fSJohn Baldwin 		 * Processed all our interrupts.  Now get the sched
5288088699fSJohn Baldwin 		 * lock.  This may take a while and it_need may get
5298088699fSJohn Baldwin 		 * set again, so we have to check it again.
5308088699fSJohn Baldwin 		 */
531896c2303SJohn Baldwin 		mtx_assert(&Giant, MA_NOTOWNED);
5329ed346baSBosko Milekic 		mtx_lock_spin(&sched_lock);
533b4151f71SJohn Baldwin 		if (!ithd->it_need) {
534b4151f71SJohn Baldwin 			/*
535b4151f71SJohn Baldwin 			 * Should we call this earlier in the loop above?
536b4151f71SJohn Baldwin 			 */
537b4151f71SJohn Baldwin 			if (ithd->it_enable != NULL)
538b4151f71SJohn Baldwin 				ithd->it_enable(ithd->it_vector);
5398088699fSJohn Baldwin 			p->p_stat = SWAIT; /* we're idle */
540b4151f71SJohn Baldwin 			CTR1(KTR_INTR, __func__ ": pid %d: done", p->p_pid);
5418088699fSJohn Baldwin 			mi_switch();
542b4151f71SJohn Baldwin 			CTR1(KTR_INTR, __func__ ": pid %d: resumed", p->p_pid);
5438088699fSJohn Baldwin 		}
5449ed346baSBosko Milekic 		mtx_unlock_spin(&sched_lock);
5458088699fSJohn Baldwin 	}
5461931cf94SJohn Baldwin }
5471931cf94SJohn Baldwin 
548b4151f71SJohn Baldwin /*
5498088699fSJohn Baldwin  * Start standard software interrupt threads
5501931cf94SJohn Baldwin  */
5511931cf94SJohn Baldwin static void
552b4151f71SJohn Baldwin start_softintr(void *dummy)
5531931cf94SJohn Baldwin {
554b4151f71SJohn Baldwin 
555b4151f71SJohn Baldwin 	if (swi_add(NULL, "net", swi_net, NULL, SWI_NET, 0, &net_ih) ||
556b4151f71SJohn Baldwin 	    swi_add(&clk_ithd, "clock", softclock, NULL, SWI_CLOCK,
557b4151f71SJohn Baldwin 		INTR_MPSAFE, &softclock_ih) ||
558b4151f71SJohn Baldwin 	    swi_add(NULL, "vm", swi_vm, NULL, SWI_VM, 0, &vm_ih))
559b4151f71SJohn Baldwin 		panic("died while creating standard software ithreads");
5603e5da754SJohn Baldwin 
5613e5da754SJohn Baldwin 	PROC_LOCK(clk_ithd->it_proc);
5623e5da754SJohn Baldwin 	clk_ithd->it_proc->p_flag |= P_NOLOAD;
5633e5da754SJohn Baldwin 	PROC_UNLOCK(clk_ithd->it_proc);
5641931cf94SJohn Baldwin }
565b4151f71SJohn Baldwin SYSINIT(start_softintr, SI_SUB_SOFTINTR, SI_ORDER_FIRST, start_softintr, NULL)
5661931cf94SJohn Baldwin 
5671931cf94SJohn Baldwin void
568b4151f71SJohn Baldwin legacy_setsoftnet(void)
5691931cf94SJohn Baldwin {
570b4151f71SJohn Baldwin 	swi_sched(net_ih, SWI_NOSWITCH);
5711931cf94SJohn Baldwin }
5721931cf94SJohn Baldwin 
5738088699fSJohn Baldwin /*
5748088699fSJohn Baldwin  * XXX: This should really be in the network code somewhere and installed
5758088699fSJohn Baldwin  * via a SI_SUB_SOFINTR, SI_ORDER_MIDDLE sysinit.
5768088699fSJohn Baldwin  */
5778088699fSJohn Baldwin void	(*netisrs[32]) __P((void));
5788088699fSJohn Baldwin u_int	netisr;
5798088699fSJohn Baldwin 
5801eb44f02SJake Burkholder int
5811eb44f02SJake Burkholder register_netisr(num, handler)
5821eb44f02SJake Burkholder 	int num;
5831eb44f02SJake Burkholder 	netisr_t *handler;
5841eb44f02SJake Burkholder {
5851eb44f02SJake Burkholder 
5861eb44f02SJake Burkholder 	if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
5871eb44f02SJake Burkholder 		printf("register_netisr: bad isr number: %d\n", num);
5881eb44f02SJake Burkholder 		return (EINVAL);
5891eb44f02SJake Burkholder 	}
5901eb44f02SJake Burkholder 	netisrs[num] = handler;
5911eb44f02SJake Burkholder 	return (0);
5921eb44f02SJake Burkholder }
5931eb44f02SJake Burkholder 
5941eb44f02SJake Burkholder int
5951eb44f02SJake Burkholder unregister_netisr(num)
5961eb44f02SJake Burkholder 	int num;
5971eb44f02SJake Burkholder {
5981eb44f02SJake Burkholder 
5991eb44f02SJake Burkholder 	if (num < 0 || num >= (sizeof(netisrs)/sizeof(*netisrs)) ) {
6001eb44f02SJake Burkholder 		printf("unregister_netisr: bad isr number: %d\n", num);
6011eb44f02SJake Burkholder 		return (EINVAL);
6021eb44f02SJake Burkholder 	}
6031eb44f02SJake Burkholder 	netisrs[num] = NULL;
6041eb44f02SJake Burkholder 	return (0);
6051eb44f02SJake Burkholder }
6061eb44f02SJake Burkholder 
6078088699fSJohn Baldwin static void
6088088699fSJohn Baldwin swi_net(void *dummy)
6091931cf94SJohn Baldwin {
6108088699fSJohn Baldwin 	u_int bits;
6118088699fSJohn Baldwin 	int i;
6128088699fSJohn Baldwin 
6138088699fSJohn Baldwin 	bits = atomic_readandclear_int(&netisr);
6148088699fSJohn Baldwin 	while ((i = ffs(bits)) != 0) {
6158088699fSJohn Baldwin 		i--;
616338c0bc6SSeigo Tanimura 		if (netisrs[i] != NULL)
6178088699fSJohn Baldwin 			netisrs[i]();
618338c0bc6SSeigo Tanimura 		else
619338c0bc6SSeigo Tanimura 			printf("swi_net: unregistered isr number: %d.\n", i);
6208088699fSJohn Baldwin 		bits &= ~(1 << i);
6218088699fSJohn Baldwin 	}
6221931cf94SJohn Baldwin }
623