kern_softint.c revision 1.3.6.3 1 1.3.6.3 mjf /* $NetBSD: kern_softint.c,v 1.3.6.3 2007/12/27 00:46:01 mjf Exp $ */
2 1.2 ad
3 1.2 ad /*-
4 1.2 ad * Copyright (c) 2007 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 * 3. All advertising materials mentioning features or use of this software
19 1.2 ad * must display the following acknowledgement:
20 1.2 ad * This product includes software developed by the NetBSD
21 1.2 ad * Foundation, Inc. and its contributors.
22 1.2 ad * 4. Neither the name of The NetBSD Foundation nor the names of its
23 1.2 ad * contributors may be used to endorse or promote products derived
24 1.2 ad * from this software without specific prior written permission.
25 1.2 ad *
26 1.2 ad * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 1.2 ad * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 1.2 ad * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 1.2 ad * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 1.2 ad * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 1.2 ad * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 1.2 ad * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 1.2 ad * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 1.2 ad * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 1.2 ad * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 1.2 ad * POSSIBILITY OF SUCH DAMAGE.
37 1.2 ad */
38 1.2 ad
39 1.2 ad /*
40 1.3.6.2 mjf * Generic software interrupt framework.
41 1.3.6.2 mjf *
42 1.3.6.2 mjf * Overview
43 1.3.6.2 mjf *
44 1.3.6.2 mjf * The soft interrupt framework provides a mechanism to schedule a
45 1.3.6.2 mjf * low priority callback that runs with thread context. It allows
46 1.3.6.2 mjf * for dynamic registration of software interrupts, and for fair
47 1.3.6.2 mjf * queueing and prioritization of those interrupts. The callbacks
48 1.3.6.2 mjf * can be scheduled to run from nearly any point in the kernel: by
49 1.3.6.2 mjf * code running with thread context, by code running from a
50 1.3.6.2 mjf * hardware interrupt handler, and at any interrupt priority
51 1.3.6.2 mjf * level.
52 1.3.6.2 mjf *
53 1.3.6.2 mjf * Priority levels
54 1.3.6.2 mjf *
55 1.3.6.2 mjf * Since soft interrupt dispatch can be tied to the underlying
56 1.3.6.2 mjf * architecture's interrupt dispatch code, it can be limited
57 1.3.6.2 mjf * both by the capabilities of the hardware and the capabilities
58 1.3.6.2 mjf * of the interrupt dispatch code itself. The number of priority
59 1.3.6.2 mjf * levels is restricted to four. In order of priority (lowest to
60 1.3.6.2 mjf * highest) the levels are: clock, bio, net, serial.
61 1.3.6.2 mjf *
62 1.3.6.2 mjf * The names are symbolic and in isolation do not have any direct
63 1.3.6.2 mjf * connection with a particular kind of device activity: they are
64 1.3.6.2 mjf * only meant as a guide.
65 1.3.6.2 mjf *
66 1.3.6.2 mjf * The four priority levels map directly to scheduler priority
67 1.3.6.2 mjf * levels, and where the architecture implements 'fast' software
68 1.3.6.2 mjf * interrupts, they also map onto interrupt priorities. The
69 1.3.6.2 mjf * interrupt priorities are intended to be hidden from machine
70 1.3.6.2 mjf * independent code, which should use thread-safe mechanisms to
71 1.3.6.2 mjf * synchronize with software interrupts (for example: mutexes).
72 1.3.6.2 mjf *
73 1.3.6.2 mjf * Capabilities
74 1.3.6.2 mjf *
75 1.3.6.2 mjf * Software interrupts run with limited machine context. In
76 1.3.6.2 mjf * particular, they do not posess any address space context. They
77 1.3.6.2 mjf * should not try to operate on user space addresses, or to use
78 1.3.6.2 mjf * virtual memory facilities other than those noted as interrupt
79 1.3.6.2 mjf * safe.
80 1.3.6.2 mjf *
81 1.3.6.2 mjf * Unlike hardware interrupts, software interrupts do have thread
82 1.3.6.2 mjf * context. They may block on synchronization objects, sleep, and
83 1.3.6.2 mjf * resume execution at a later time.
84 1.3.6.2 mjf *
85 1.3.6.2 mjf * Since software interrupts are a limited resource and run with
86 1.3.6.2 mjf * higher priority than most other LWPs in the system, all
87 1.3.6.2 mjf * block-and-resume activity by a software interrupt must be kept
88 1.3.6.2 mjf * short to allow futher processing at that level to continue. By
89 1.3.6.2 mjf * extension, code running with process context must take care to
90 1.3.6.2 mjf * ensure that any lock that may be taken from a software interrupt
91 1.3.6.2 mjf * can not be held for more than a short period of time.
92 1.3.6.2 mjf *
93 1.3.6.2 mjf * The kernel does not allow software interrupts to use facilities
94 1.3.6.2 mjf * or perform actions that may block for a significant amount of
95 1.3.6.2 mjf * time. This means that it's not valid for a software interrupt
96 1.3.6.2 mjf * to: sleep on condition variables, use the lockmgr() facility,
97 1.3.6.2 mjf * or wait for resources to become available (for example,
98 1.3.6.2 mjf * memory).
99 1.3.6.2 mjf *
100 1.3.6.2 mjf * Per-CPU operation
101 1.3.6.2 mjf *
102 1.3.6.2 mjf * If a soft interrupt is triggered on a CPU, it can only be
103 1.3.6.2 mjf * dispatched on the same CPU. Each LWP dedicated to handling a
104 1.3.6.2 mjf * soft interrupt is bound to its home CPU, so if the LWP blocks
105 1.3.6.2 mjf * and needs to run again, it can only run there. Nearly all data
106 1.3.6.2 mjf * structures used to manage software interrupts are per-CPU.
107 1.3.6.2 mjf *
108 1.3.6.2 mjf * The per-CPU requirement is intended to reduce "ping-pong" of
109 1.3.6.2 mjf * cache lines between CPUs: lines occupied by data structures
110 1.3.6.2 mjf * used to manage the soft interrupts, and lines occupied by data
111 1.3.6.2 mjf * items being passed down to the soft interrupt. As a positive
112 1.3.6.2 mjf * side effect, this also means that the soft interrupt dispatch
113 1.3.6.2 mjf * code does not need to to use spinlocks to synchronize.
114 1.3.6.2 mjf *
115 1.3.6.2 mjf * Generic implementation
116 1.3.6.2 mjf *
117 1.3.6.2 mjf * A generic, low performance implementation is provided that
118 1.3.6.2 mjf * works across all architectures, with no machine-dependent
119 1.3.6.2 mjf * modifications needed. This implementation uses the scheduler,
120 1.3.6.2 mjf * and so has a number of restrictions:
121 1.3.6.2 mjf *
122 1.3.6.2 mjf * 1) The software interrupts are not currently preemptive, so
123 1.3.6.2 mjf * must wait for the currently executing LWP to yield the CPU.
124 1.3.6.2 mjf * This can introduce latency.
125 1.3.6.2 mjf *
126 1.3.6.2 mjf * 2) An expensive context switch is required for a software
127 1.3.6.2 mjf * interrupt to be handled.
128 1.3.6.2 mjf *
129 1.3.6.2 mjf * 'Fast' software interrupts
130 1.3.6.2 mjf *
131 1.3.6.2 mjf * If an architectures defines __HAVE_FAST_SOFTINTS, it implements
132 1.3.6.2 mjf * the fast mechanism. Threads running either in the kernel or in
133 1.3.6.2 mjf * userspace will be interrupted, but will not be preempted. When
134 1.3.6.2 mjf * the soft interrupt completes execution, the interrupted LWP
135 1.3.6.2 mjf * is resumed. Interrupt dispatch code must provide the minimum
136 1.3.6.2 mjf * level of context necessary for the soft interrupt to block and
137 1.3.6.2 mjf * be resumed at a later time. The machine-dependent dispatch
138 1.3.6.2 mjf * path looks something like the following:
139 1.3.6.2 mjf *
140 1.3.6.2 mjf * softintr()
141 1.3.6.2 mjf * {
142 1.3.6.2 mjf * go to IPL_HIGH if necessary for switch;
143 1.3.6.2 mjf * save any necessary registers in a format that can be
144 1.3.6.2 mjf * restored by cpu_switchto if the softint blocks;
145 1.3.6.2 mjf * arrange for cpu_switchto() to restore into the
146 1.3.6.2 mjf * trampoline function;
147 1.3.6.2 mjf * identify LWP to handle this interrupt;
148 1.3.6.2 mjf * switch to the LWP's stack;
149 1.3.6.2 mjf * switch register stacks, if necessary;
150 1.3.6.2 mjf * assign new value of curlwp;
151 1.3.6.2 mjf * call MI softint_dispatch, passing old curlwp and IPL
152 1.3.6.2 mjf * to execute interrupt at;
153 1.3.6.2 mjf * switch back to old stack;
154 1.3.6.2 mjf * switch back to old register stack, if necessary;
155 1.3.6.2 mjf * restore curlwp;
156 1.3.6.2 mjf * return to interrupted LWP;
157 1.3.6.2 mjf * }
158 1.3.6.2 mjf *
159 1.3.6.2 mjf * If the soft interrupt blocks, a trampoline function is returned
160 1.3.6.2 mjf * to in the context of the interrupted LWP, as arranged for by
161 1.3.6.2 mjf * softint():
162 1.3.6.2 mjf *
163 1.3.6.2 mjf * softint_ret()
164 1.3.6.2 mjf * {
165 1.3.6.2 mjf * unlock soft interrupt LWP;
166 1.3.6.2 mjf * resume interrupt processing, likely returning to
167 1.3.6.2 mjf * interrupted LWP or dispatching another, different
168 1.3.6.2 mjf * interrupt;
169 1.3.6.2 mjf * }
170 1.3.6.2 mjf *
171 1.3.6.2 mjf * Once the soft interrupt has fired (and even if it has blocked),
172 1.3.6.2 mjf * no further soft interrupts at that level will be triggered by
173 1.3.6.2 mjf * MI code until the soft interrupt handler has ceased execution.
174 1.3.6.2 mjf * If a soft interrupt handler blocks and is resumed, it resumes
175 1.3.6.2 mjf * execution as a normal LWP (kthread) and gains VM context. Only
176 1.3.6.2 mjf * when it has completed and is ready to fire again will it
177 1.3.6.2 mjf * interrupt other threads.
178 1.3.6.2 mjf *
179 1.3.6.2 mjf * Future directions
180 1.3.6.2 mjf *
181 1.3.6.2 mjf * Provide a cheap way to direct software interrupts to remote
182 1.3.6.2 mjf * CPUs. Provide a way to enqueue work items into the handler
183 1.3.6.2 mjf * record, removing additional spl calls (see subr_workqueue.c).
184 1.2 ad */
185 1.2 ad
186 1.2 ad #include <sys/cdefs.h>
187 1.3.6.3 mjf __KERNEL_RCSID(0, "$NetBSD: kern_softint.c,v 1.3.6.3 2007/12/27 00:46:01 mjf Exp $");
188 1.2 ad
189 1.2 ad #include <sys/param.h>
190 1.3.6.2 mjf #include <sys/malloc.h>
191 1.3.6.2 mjf #include <sys/proc.h>
192 1.2 ad #include <sys/intr.h>
193 1.3.6.2 mjf #include <sys/mutex.h>
194 1.3.6.2 mjf #include <sys/kthread.h>
195 1.3.6.2 mjf #include <sys/evcnt.h>
196 1.3.6.2 mjf #include <sys/cpu.h>
197 1.3.6.2 mjf
198 1.3.6.2 mjf #include <net/netisr.h>
199 1.3.6.2 mjf
200 1.3.6.2 mjf #include <uvm/uvm_extern.h>
201 1.3.6.2 mjf
202 1.3.6.2 mjf /* This could overlap with signal info in struct lwp. */
203 1.3.6.2 mjf typedef struct softint {
204 1.3.6.2 mjf SIMPLEQ_HEAD(, softhand) si_q;
205 1.3.6.2 mjf struct lwp *si_lwp;
206 1.3.6.2 mjf struct cpu_info *si_cpu;
207 1.3.6.2 mjf uintptr_t si_machdep;
208 1.3.6.2 mjf struct evcnt si_evcnt;
209 1.3.6.2 mjf struct evcnt si_evcnt_block;
210 1.3.6.2 mjf int si_active;
211 1.3.6.2 mjf char si_name[8];
212 1.3.6.2 mjf char si_name_block[8+6];
213 1.3.6.2 mjf } softint_t;
214 1.3.6.2 mjf
215 1.3.6.2 mjf typedef struct softhand {
216 1.3.6.2 mjf SIMPLEQ_ENTRY(softhand) sh_q;
217 1.3.6.2 mjf void (*sh_func)(void *);
218 1.3.6.2 mjf void *sh_arg;
219 1.3.6.2 mjf softint_t *sh_isr;
220 1.3.6.2 mjf u_int sh_pending;
221 1.3.6.2 mjf u_int sh_flags;
222 1.3.6.2 mjf } softhand_t;
223 1.3.6.2 mjf
224 1.3.6.2 mjf typedef struct softcpu {
225 1.3.6.2 mjf struct cpu_info *sc_cpu;
226 1.3.6.2 mjf softint_t sc_int[SOFTINT_COUNT];
227 1.3.6.2 mjf softhand_t sc_hand[1];
228 1.3.6.2 mjf } softcpu_t;
229 1.3.6.2 mjf
230 1.3.6.2 mjf static void softint_thread(void *);
231 1.3.6.2 mjf
232 1.3.6.2 mjf u_int softint_bytes = 8192;
233 1.3.6.2 mjf u_int softint_timing;
234 1.3.6.2 mjf static u_int softint_max;
235 1.3.6.2 mjf static kmutex_t softint_lock;
236 1.3.6.2 mjf static void *softint_netisrs[32];
237 1.2 ad
238 1.3.6.2 mjf /*
239 1.3.6.2 mjf * softint_init_isr:
240 1.3.6.2 mjf *
241 1.3.6.2 mjf * Initialize a single interrupt level for a single CPU.
242 1.3.6.2 mjf */
243 1.3.6.2 mjf static void
244 1.3.6.2 mjf softint_init_isr(softcpu_t *sc, const char *desc, pri_t pri, u_int level)
245 1.3.6.2 mjf {
246 1.3.6.2 mjf struct cpu_info *ci;
247 1.3.6.2 mjf softint_t *si;
248 1.3.6.2 mjf int error;
249 1.3.6.2 mjf
250 1.3.6.2 mjf si = &sc->sc_int[level];
251 1.3.6.2 mjf ci = sc->sc_cpu;
252 1.3.6.2 mjf si->si_cpu = ci;
253 1.3.6.2 mjf
254 1.3.6.2 mjf SIMPLEQ_INIT(&si->si_q);
255 1.3.6.2 mjf
256 1.3.6.2 mjf error = kthread_create(pri, KTHREAD_MPSAFE | KTHREAD_INTR |
257 1.3.6.2 mjf KTHREAD_IDLE, ci, softint_thread, si, &si->si_lwp,
258 1.3.6.2 mjf "soft%s/%d", desc, (int)ci->ci_cpuid);
259 1.3.6.2 mjf if (error != 0)
260 1.3.6.2 mjf panic("softint_init_isr: error %d", error);
261 1.3.6.2 mjf
262 1.3.6.2 mjf snprintf(si->si_name, sizeof(si->si_name), "%s/%d", desc,
263 1.3.6.2 mjf (int)ci->ci_cpuid);
264 1.3.6.2 mjf evcnt_attach_dynamic(&si->si_evcnt, EVCNT_TYPE_INTR, NULL,
265 1.3.6.2 mjf "softint", si->si_name);
266 1.3.6.2 mjf snprintf(si->si_name_block, sizeof(si->si_name_block), "%s block/%d",
267 1.3.6.2 mjf desc, (int)ci->ci_cpuid);
268 1.3.6.2 mjf evcnt_attach_dynamic(&si->si_evcnt_block, EVCNT_TYPE_INTR, NULL,
269 1.3.6.2 mjf "softint", si->si_name_block);
270 1.3 ad
271 1.3.6.2 mjf si->si_lwp->l_private = si;
272 1.3.6.2 mjf softint_init_md(si->si_lwp, level, &si->si_machdep);
273 1.3.6.2 mjf }
274 1.2 ad /*
275 1.2 ad * softint_init:
276 1.2 ad *
277 1.2 ad * Initialize per-CPU data structures. Called from mi_cpu_attach().
278 1.2 ad */
279 1.2 ad void
280 1.2 ad softint_init(struct cpu_info *ci)
281 1.2 ad {
282 1.3.6.2 mjf static struct cpu_info *first;
283 1.3.6.2 mjf softcpu_t *sc, *scfirst;
284 1.3.6.2 mjf softhand_t *sh, *shmax;
285 1.3.6.2 mjf
286 1.3.6.2 mjf if (first == NULL) {
287 1.3.6.2 mjf /* Boot CPU. */
288 1.3.6.2 mjf first = ci;
289 1.3.6.2 mjf mutex_init(&softint_lock, MUTEX_DEFAULT, IPL_NONE);
290 1.3.6.2 mjf softint_bytes = round_page(softint_bytes);
291 1.3.6.2 mjf softint_max = (softint_bytes - sizeof(softcpu_t)) /
292 1.3.6.2 mjf sizeof(softhand_t);
293 1.3.6.2 mjf }
294 1.2 ad
295 1.3.6.2 mjf sc = (softcpu_t *)uvm_km_alloc(kernel_map, softint_bytes, 0,
296 1.3.6.2 mjf UVM_KMF_WIRED | UVM_KMF_ZERO);
297 1.3.6.2 mjf if (sc == NULL)
298 1.3.6.2 mjf panic("softint_init_cpu: cannot allocate memory");
299 1.3.6.2 mjf
300 1.3.6.2 mjf ci->ci_data.cpu_softcpu = sc;
301 1.3.6.2 mjf ci->ci_data.cpu_softints = 0;
302 1.3.6.2 mjf sc->sc_cpu = ci;
303 1.3.6.2 mjf
304 1.3.6.2 mjf softint_init_isr(sc, "net", PRI_SOFTNET, SOFTINT_NET);
305 1.3.6.2 mjf softint_init_isr(sc, "bio", PRI_SOFTBIO, SOFTINT_BIO);
306 1.3.6.2 mjf softint_init_isr(sc, "clk", PRI_SOFTCLOCK, SOFTINT_CLOCK);
307 1.3.6.2 mjf softint_init_isr(sc, "ser", PRI_SOFTSERIAL, SOFTINT_SERIAL);
308 1.3.6.2 mjf
309 1.3.6.2 mjf if (first != ci) {
310 1.3.6.2 mjf mutex_enter(&softint_lock);
311 1.3.6.2 mjf scfirst = first->ci_data.cpu_softcpu;
312 1.3.6.2 mjf sh = sc->sc_hand;
313 1.3.6.2 mjf memcpy(sh, scfirst->sc_hand, sizeof(*sh) * softint_max);
314 1.3.6.2 mjf /* Update pointers for this CPU. */
315 1.3.6.2 mjf for (shmax = sh + softint_max; sh < shmax; sh++) {
316 1.3.6.2 mjf if (sh->sh_func == NULL)
317 1.3.6.2 mjf continue;
318 1.3.6.2 mjf sh->sh_isr =
319 1.3.6.2 mjf &sc->sc_int[sh->sh_flags & SOFTINT_LVLMASK];
320 1.3.6.2 mjf }
321 1.3.6.2 mjf mutex_exit(&softint_lock);
322 1.3.6.2 mjf } else {
323 1.3.6.2 mjf /*
324 1.3.6.2 mjf * Establish handlers for legacy net interrupts.
325 1.3.6.2 mjf * XXX Needs to go away.
326 1.3.6.2 mjf */
327 1.3.6.2 mjf #define DONETISR(n, f) \
328 1.3.6.2 mjf softint_netisrs[(n)] = \
329 1.3.6.2 mjf softint_establish(SOFTINT_NET, (void (*)(void *))(f), NULL)
330 1.3.6.2 mjf #include <net/netisr_dispatch.h>
331 1.3.6.2 mjf }
332 1.2 ad }
333 1.2 ad
334 1.2 ad /*
335 1.2 ad * softint_establish:
336 1.2 ad *
337 1.2 ad * Register a software interrupt handler.
338 1.2 ad */
339 1.2 ad void *
340 1.2 ad softint_establish(u_int flags, void (*func)(void *), void *arg)
341 1.2 ad {
342 1.3.6.2 mjf CPU_INFO_ITERATOR cii;
343 1.3.6.2 mjf struct cpu_info *ci;
344 1.3.6.2 mjf softcpu_t *sc;
345 1.3.6.2 mjf softhand_t *sh;
346 1.3.6.2 mjf u_int level, index;
347 1.2 ad
348 1.2 ad level = (flags & SOFTINT_LVLMASK);
349 1.2 ad KASSERT(level < SOFTINT_COUNT);
350 1.2 ad
351 1.3.6.2 mjf mutex_enter(&softint_lock);
352 1.3.6.2 mjf
353 1.3.6.2 mjf /* Find a free slot. */
354 1.3.6.2 mjf sc = curcpu()->ci_data.cpu_softcpu;
355 1.3.6.2 mjf for (index = 1; index < softint_max; index++)
356 1.3.6.2 mjf if (sc->sc_hand[index].sh_func == NULL)
357 1.3.6.2 mjf break;
358 1.3.6.2 mjf if (index == softint_max) {
359 1.3.6.2 mjf mutex_exit(&softint_lock);
360 1.3.6.2 mjf printf("WARNING: softint_establish: table full, "
361 1.3.6.2 mjf "increase softint_bytes\n");
362 1.3.6.2 mjf return NULL;
363 1.2 ad }
364 1.2 ad
365 1.3.6.2 mjf /* Set up the handler on each CPU. */
366 1.3.6.3 mjf if (ncpu < 2) {
367 1.3.6.3 mjf /* XXX hack for machines with no CPU_INFO_FOREACH() early on */
368 1.3.6.3 mjf sc = curcpu()->ci_data.cpu_softcpu;
369 1.3.6.3 mjf sh = &sc->sc_hand[index];
370 1.3.6.3 mjf sh->sh_isr = &sc->sc_int[level];
371 1.3.6.3 mjf sh->sh_func = func;
372 1.3.6.3 mjf sh->sh_arg = arg;
373 1.3.6.3 mjf sh->sh_flags = flags;
374 1.3.6.3 mjf sh->sh_pending = 0;
375 1.3.6.3 mjf } else for (CPU_INFO_FOREACH(cii, ci)) {
376 1.3.6.2 mjf sc = ci->ci_data.cpu_softcpu;
377 1.3.6.2 mjf sh = &sc->sc_hand[index];
378 1.3.6.2 mjf sh->sh_isr = &sc->sc_int[level];
379 1.3.6.2 mjf sh->sh_func = func;
380 1.3.6.2 mjf sh->sh_arg = arg;
381 1.3.6.2 mjf sh->sh_flags = flags;
382 1.3.6.2 mjf sh->sh_pending = 0;
383 1.3.6.2 mjf }
384 1.3.6.2 mjf
385 1.3.6.2 mjf mutex_exit(&softint_lock);
386 1.3.6.2 mjf
387 1.3.6.2 mjf return (void *)((uint8_t *)&sc->sc_hand[index] - (uint8_t *)sc);
388 1.2 ad }
389 1.2 ad
390 1.2 ad /*
391 1.2 ad * softint_disestablish:
392 1.2 ad *
393 1.2 ad * Unregister a software interrupt handler.
394 1.2 ad */
395 1.2 ad void
396 1.2 ad softint_disestablish(void *arg)
397 1.2 ad {
398 1.3.6.2 mjf CPU_INFO_ITERATOR cii;
399 1.3.6.2 mjf struct cpu_info *ci;
400 1.3.6.2 mjf softcpu_t *sc;
401 1.3.6.2 mjf softhand_t *sh;
402 1.3.6.2 mjf uintptr_t offset;
403 1.3.6.2 mjf
404 1.3.6.2 mjf offset = (uintptr_t)arg;
405 1.3.6.2 mjf KASSERT(offset != 0 && offset < softint_bytes);
406 1.3.6.2 mjf
407 1.3.6.2 mjf mutex_enter(&softint_lock);
408 1.3.6.2 mjf
409 1.3.6.2 mjf /* Clear the handler on each CPU. */
410 1.3.6.2 mjf for (CPU_INFO_FOREACH(cii, ci)) {
411 1.3.6.2 mjf sc = ci->ci_data.cpu_softcpu;
412 1.3.6.2 mjf sh = (softhand_t *)((uint8_t *)sc + offset);
413 1.3.6.2 mjf KASSERT(sh->sh_func != NULL);
414 1.3.6.2 mjf KASSERT(sh->sh_pending == 0);
415 1.3.6.2 mjf sh->sh_func = NULL;
416 1.3.6.2 mjf }
417 1.2 ad
418 1.3.6.2 mjf mutex_exit(&softint_lock);
419 1.2 ad }
420 1.2 ad
421 1.2 ad /*
422 1.2 ad * softint_schedule:
423 1.2 ad *
424 1.2 ad * Trigger a software interrupt. Must be called from a hardware
425 1.2 ad * interrupt handler, or with preemption disabled (since we are
426 1.2 ad * using the value of curcpu()).
427 1.2 ad */
428 1.2 ad void
429 1.2 ad softint_schedule(void *arg)
430 1.2 ad {
431 1.3.6.2 mjf softhand_t *sh;
432 1.3.6.2 mjf softint_t *si;
433 1.3.6.2 mjf uintptr_t offset;
434 1.3.6.2 mjf int s;
435 1.3.6.2 mjf
436 1.3.6.2 mjf /* Find the handler record for this CPU. */
437 1.3.6.2 mjf offset = (uintptr_t)arg;
438 1.3.6.2 mjf KASSERT(offset != 0 && offset < softint_bytes);
439 1.3.6.2 mjf sh = (softhand_t *)((uint8_t *)curcpu()->ci_data.cpu_softcpu + offset);
440 1.3.6.2 mjf
441 1.3.6.2 mjf /* If it's already pending there's nothing to do. */
442 1.3.6.2 mjf if (sh->sh_pending)
443 1.3.6.2 mjf return;
444 1.3.6.2 mjf
445 1.3.6.2 mjf /*
446 1.3.6.2 mjf * Enqueue the handler into the LWP's pending list.
447 1.3.6.2 mjf * If the LWP is completely idle, then make it run.
448 1.3.6.2 mjf */
449 1.3.6.2 mjf s = splhigh();
450 1.3.6.2 mjf if (!sh->sh_pending) {
451 1.3.6.2 mjf si = sh->sh_isr;
452 1.3.6.2 mjf sh->sh_pending = 1;
453 1.3.6.2 mjf SIMPLEQ_INSERT_TAIL(&si->si_q, sh, sh_q);
454 1.3.6.2 mjf if (si->si_active == 0) {
455 1.3.6.2 mjf si->si_active = 1;
456 1.3.6.2 mjf softint_trigger(si->si_machdep);
457 1.3.6.2 mjf }
458 1.3.6.2 mjf }
459 1.3.6.2 mjf splx(s);
460 1.3.6.2 mjf }
461 1.3.6.2 mjf
462 1.3.6.2 mjf /*
463 1.3.6.2 mjf * softint_execute:
464 1.3.6.2 mjf *
465 1.3.6.2 mjf * Invoke handlers for the specified soft interrupt.
466 1.3.6.2 mjf * Must be entered at splhigh. Will drop the priority
467 1.3.6.2 mjf * to the level specified, but returns back at splhigh.
468 1.3.6.2 mjf */
469 1.3.6.2 mjf static inline void
470 1.3.6.2 mjf softint_execute(softint_t *si, lwp_t *l, int s)
471 1.3.6.2 mjf {
472 1.3.6.2 mjf softhand_t *sh;
473 1.3.6.2 mjf bool havelock;
474 1.3.6.2 mjf
475 1.3.6.2 mjf #ifdef __HAVE_FAST_SOFTINTS
476 1.3.6.2 mjf KASSERT(si->si_lwp == curlwp);
477 1.3.6.2 mjf #else
478 1.3.6.2 mjf /* May be running in user context. */
479 1.3.6.2 mjf #endif
480 1.3.6.2 mjf KASSERT(si->si_cpu == curcpu());
481 1.3.6.2 mjf KASSERT(si->si_lwp->l_wchan == NULL);
482 1.3.6.2 mjf KASSERT(si->si_active);
483 1.3.6.2 mjf
484 1.3.6.2 mjf havelock = false;
485 1.3.6.2 mjf
486 1.3.6.2 mjf /*
487 1.3.6.2 mjf * Note: due to priority inheritance we may have interrupted a
488 1.3.6.2 mjf * higher priority LWP. Since the soft interrupt must be quick
489 1.3.6.2 mjf * and is non-preemptable, we don't bother yielding.
490 1.3.6.2 mjf */
491 1.3.6.2 mjf
492 1.3.6.2 mjf while (!SIMPLEQ_EMPTY(&si->si_q)) {
493 1.3.6.2 mjf /*
494 1.3.6.2 mjf * Pick the longest waiting handler to run. We block
495 1.3.6.2 mjf * interrupts but do not lock in order to do this, as
496 1.3.6.2 mjf * we are protecting against the local CPU only.
497 1.3.6.2 mjf */
498 1.3.6.2 mjf sh = SIMPLEQ_FIRST(&si->si_q);
499 1.3.6.2 mjf SIMPLEQ_REMOVE_HEAD(&si->si_q, sh_q);
500 1.3.6.2 mjf sh->sh_pending = 0;
501 1.3.6.2 mjf splx(s);
502 1.3.6.2 mjf
503 1.3.6.2 mjf /* Run the handler. */
504 1.3.6.2 mjf if ((sh->sh_flags & SOFTINT_MPSAFE) == 0 && !havelock) {
505 1.3.6.2 mjf KERNEL_LOCK(1, l);
506 1.3.6.2 mjf havelock = true;
507 1.3.6.2 mjf }
508 1.3.6.2 mjf (*sh->sh_func)(sh->sh_arg);
509 1.3.6.2 mjf
510 1.3.6.2 mjf (void)splhigh();
511 1.3.6.2 mjf }
512 1.3.6.2 mjf
513 1.3.6.2 mjf if (havelock) {
514 1.3.6.2 mjf KERNEL_UNLOCK_ONE(l);
515 1.3.6.2 mjf }
516 1.3.6.2 mjf
517 1.3.6.2 mjf /*
518 1.3.6.2 mjf * Unlocked, but only for statistics.
519 1.3.6.2 mjf * Should be per-CPU to prevent cache ping-pong.
520 1.3.6.2 mjf */
521 1.3.6.2 mjf uvmexp.softs++;
522 1.2 ad
523 1.3.6.2 mjf si->si_evcnt.ev_count++;
524 1.3.6.2 mjf si->si_active = 0;
525 1.2 ad }
526 1.2 ad
527 1.2 ad /*
528 1.2 ad * softint_block:
529 1.2 ad *
530 1.2 ad * Update statistics when the soft interrupt blocks.
531 1.2 ad */
532 1.2 ad void
533 1.2 ad softint_block(lwp_t *l)
534 1.2 ad {
535 1.3.6.2 mjf softint_t *si = l->l_private;
536 1.2 ad
537 1.3.6.2 mjf KASSERT((l->l_pflag & LP_INTR) != 0);
538 1.3.6.2 mjf si->si_evcnt_block.ev_count++;
539 1.3.6.2 mjf }
540 1.3.6.2 mjf
541 1.3.6.2 mjf /*
542 1.3.6.2 mjf * schednetisr:
543 1.3.6.2 mjf *
544 1.3.6.2 mjf * Trigger a legacy network interrupt. XXX Needs to go away.
545 1.3.6.2 mjf */
546 1.3.6.2 mjf void
547 1.3.6.2 mjf schednetisr(int isr)
548 1.3.6.2 mjf {
549 1.3.6.2 mjf
550 1.3.6.2 mjf softint_schedule(softint_netisrs[isr]);
551 1.3.6.2 mjf }
552 1.3.6.2 mjf
553 1.3.6.2 mjf #ifndef __HAVE_FAST_SOFTINTS
554 1.3.6.2 mjf
555 1.3.6.2 mjf /*
556 1.3.6.2 mjf * softint_init_md:
557 1.3.6.2 mjf *
558 1.3.6.2 mjf * Slow path: perform machine-dependent initialization.
559 1.3.6.2 mjf */
560 1.3.6.2 mjf void
561 1.3.6.2 mjf softint_init_md(lwp_t *l, u_int level, uintptr_t *machdep)
562 1.3.6.2 mjf {
563 1.3.6.2 mjf softint_t *si;
564 1.3.6.2 mjf
565 1.3.6.2 mjf *machdep = (1 << level);
566 1.3.6.2 mjf si = l->l_private;
567 1.3.6.2 mjf
568 1.3.6.2 mjf lwp_lock(l);
569 1.3.6.2 mjf lwp_unlock_to(l, l->l_cpu->ci_schedstate.spc_mutex);
570 1.3.6.2 mjf lwp_lock(l);
571 1.3.6.2 mjf /* Cheat and make the KASSERT in softint_thread() happy. */
572 1.3.6.2 mjf si->si_active = 1;
573 1.3.6.2 mjf l->l_stat = LSRUN;
574 1.3.6.2 mjf sched_enqueue(l, false);
575 1.3.6.2 mjf lwp_unlock(l);
576 1.3.6.2 mjf }
577 1.3.6.2 mjf
578 1.3.6.2 mjf /*
579 1.3.6.2 mjf * softint_trigger:
580 1.3.6.2 mjf *
581 1.3.6.2 mjf * Slow path: cause a soft interrupt handler to begin executing.
582 1.3.6.2 mjf * Called at IPL_HIGH.
583 1.3.6.2 mjf */
584 1.3.6.2 mjf void
585 1.3.6.2 mjf softint_trigger(uintptr_t machdep)
586 1.3.6.2 mjf {
587 1.3.6.2 mjf struct cpu_info *ci;
588 1.3.6.2 mjf lwp_t *l;
589 1.3.6.2 mjf
590 1.3.6.2 mjf l = curlwp;
591 1.3.6.2 mjf ci = l->l_cpu;
592 1.3.6.2 mjf ci->ci_data.cpu_softints |= machdep;
593 1.3.6.2 mjf if (l == ci->ci_data.cpu_idlelwp) {
594 1.3.6.2 mjf cpu_need_resched(ci, 0);
595 1.3.6.2 mjf } else {
596 1.3.6.2 mjf /* MI equivalent of aston() */
597 1.3.6.2 mjf cpu_signotify(l);
598 1.3.6.2 mjf }
599 1.3.6.2 mjf }
600 1.3.6.2 mjf
601 1.3.6.2 mjf /*
602 1.3.6.2 mjf * softint_thread:
603 1.3.6.2 mjf *
604 1.3.6.2 mjf * Slow path: MI software interrupt dispatch.
605 1.3.6.2 mjf */
606 1.3.6.2 mjf void
607 1.3.6.2 mjf softint_thread(void *cookie)
608 1.3.6.2 mjf {
609 1.3.6.2 mjf softint_t *si;
610 1.3.6.2 mjf lwp_t *l;
611 1.3.6.2 mjf int s;
612 1.3.6.2 mjf
613 1.3.6.2 mjf l = curlwp;
614 1.3.6.2 mjf si = l->l_private;
615 1.3.6.2 mjf
616 1.3.6.2 mjf for (;;) {
617 1.3.6.2 mjf /*
618 1.3.6.2 mjf * Clear pending status and run it. We must drop the
619 1.3.6.2 mjf * spl before mi_switch(), since IPL_HIGH may be higher
620 1.3.6.2 mjf * than IPL_SCHED (and it is not safe to switch at a
621 1.3.6.2 mjf * higher level).
622 1.3.6.2 mjf */
623 1.3.6.2 mjf s = splhigh();
624 1.3.6.2 mjf l->l_cpu->ci_data.cpu_softints &= ~si->si_machdep;
625 1.3.6.2 mjf softint_execute(si, l, s);
626 1.3.6.2 mjf splx(s);
627 1.3.6.2 mjf
628 1.3.6.2 mjf lwp_lock(l);
629 1.3.6.2 mjf l->l_stat = LSIDL;
630 1.3.6.2 mjf mi_switch(l);
631 1.3.6.2 mjf }
632 1.2 ad }
633 1.3.6.1 mjf
634 1.3.6.1 mjf /*
635 1.3.6.1 mjf * softint_picklwp:
636 1.3.6.1 mjf *
637 1.3.6.1 mjf * Slow path: called from mi_switch() to pick the highest priority
638 1.3.6.1 mjf * soft interrupt LWP that needs to run.
639 1.3.6.1 mjf */
640 1.3.6.1 mjf lwp_t *
641 1.3.6.1 mjf softint_picklwp(void)
642 1.3.6.1 mjf {
643 1.3.6.2 mjf struct cpu_info *ci;
644 1.3.6.2 mjf u_int mask;
645 1.3.6.2 mjf softint_t *si;
646 1.3.6.2 mjf lwp_t *l;
647 1.3.6.2 mjf
648 1.3.6.2 mjf ci = curcpu();
649 1.3.6.2 mjf si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int;
650 1.3.6.2 mjf mask = ci->ci_data.cpu_softints;
651 1.3.6.2 mjf
652 1.3.6.2 mjf if ((mask & (1 << SOFTINT_SERIAL)) != 0) {
653 1.3.6.2 mjf l = si[SOFTINT_SERIAL].si_lwp;
654 1.3.6.2 mjf } else if ((mask & (1 << SOFTINT_NET)) != 0) {
655 1.3.6.2 mjf l = si[SOFTINT_NET].si_lwp;
656 1.3.6.2 mjf } else if ((mask & (1 << SOFTINT_BIO)) != 0) {
657 1.3.6.2 mjf l = si[SOFTINT_BIO].si_lwp;
658 1.3.6.2 mjf } else if ((mask & (1 << SOFTINT_CLOCK)) != 0) {
659 1.3.6.2 mjf l = si[SOFTINT_CLOCK].si_lwp;
660 1.3.6.2 mjf } else {
661 1.3.6.2 mjf panic("softint_picklwp");
662 1.3.6.2 mjf }
663 1.3.6.1 mjf
664 1.3.6.2 mjf return l;
665 1.3.6.1 mjf }
666 1.3.6.1 mjf
667 1.3.6.1 mjf /*
668 1.3.6.1 mjf * softint_overlay:
669 1.3.6.1 mjf *
670 1.3.6.1 mjf * Slow path: called from lwp_userret() to run a soft interrupt
671 1.3.6.2 mjf * within the context of a user thread.
672 1.3.6.1 mjf */
673 1.3.6.1 mjf void
674 1.3.6.1 mjf softint_overlay(void)
675 1.3.6.1 mjf {
676 1.3.6.2 mjf struct cpu_info *ci;
677 1.3.6.2 mjf u_int softints;
678 1.3.6.2 mjf softint_t *si;
679 1.3.6.2 mjf pri_t obase;
680 1.3.6.2 mjf lwp_t *l;
681 1.3.6.2 mjf int s;
682 1.3.6.2 mjf
683 1.3.6.2 mjf l = curlwp;
684 1.3.6.2 mjf ci = l->l_cpu;
685 1.3.6.2 mjf si = ((softcpu_t *)ci->ci_data.cpu_softcpu)->sc_int;
686 1.3.6.2 mjf
687 1.3.6.2 mjf KASSERT((l->l_pflag & LP_INTR) == 0);
688 1.3.6.2 mjf
689 1.3.6.2 mjf /* Arrange to elevate priority if the LWP blocks. */
690 1.3.6.2 mjf obase = l->l_kpribase;
691 1.3.6.2 mjf l->l_kpribase = PRI_KERNEL_RT;
692 1.3.6.2 mjf l->l_pflag |= LP_INTR;
693 1.3.6.2 mjf s = splhigh();
694 1.3.6.2 mjf while ((softints = ci->ci_data.cpu_softints) != 0) {
695 1.3.6.2 mjf if ((softints & (1 << SOFTINT_SERIAL)) != 0) {
696 1.3.6.2 mjf ci->ci_data.cpu_softints &= ~(1 << SOFTINT_SERIAL);
697 1.3.6.2 mjf softint_execute(&si[SOFTINT_SERIAL], l, s);
698 1.3.6.2 mjf continue;
699 1.3.6.2 mjf }
700 1.3.6.2 mjf if ((softints & (1 << SOFTINT_NET)) != 0) {
701 1.3.6.2 mjf ci->ci_data.cpu_softints &= ~(1 << SOFTINT_NET);
702 1.3.6.2 mjf softint_execute(&si[SOFTINT_NET], l, s);
703 1.3.6.2 mjf continue;
704 1.3.6.2 mjf }
705 1.3.6.2 mjf if ((softints & (1 << SOFTINT_BIO)) != 0) {
706 1.3.6.2 mjf ci->ci_data.cpu_softints &= ~(1 << SOFTINT_BIO);
707 1.3.6.2 mjf softint_execute(&si[SOFTINT_BIO], l, s);
708 1.3.6.2 mjf continue;
709 1.3.6.2 mjf }
710 1.3.6.2 mjf if ((softints & (1 << SOFTINT_CLOCK)) != 0) {
711 1.3.6.2 mjf ci->ci_data.cpu_softints &= ~(1 << SOFTINT_CLOCK);
712 1.3.6.2 mjf softint_execute(&si[SOFTINT_CLOCK], l, s);
713 1.3.6.2 mjf continue;
714 1.3.6.2 mjf }
715 1.3.6.2 mjf }
716 1.3.6.2 mjf splx(s);
717 1.3.6.2 mjf l->l_pflag &= ~LP_INTR;
718 1.3.6.2 mjf l->l_kpribase = obase;
719 1.3.6.2 mjf }
720 1.3.6.1 mjf
721 1.3.6.2 mjf #else /* !__HAVE_FAST_SOFTINTS */
722 1.3.6.2 mjf
723 1.3.6.2 mjf /*
724 1.3.6.2 mjf * softint_thread:
725 1.3.6.2 mjf *
726 1.3.6.2 mjf * Fast path: the LWP is switched to without restoring any state,
727 1.3.6.2 mjf * so we should not arrive here - there is a direct handoff between
728 1.3.6.2 mjf * the interrupt stub and softint_dispatch().
729 1.3.6.2 mjf */
730 1.3.6.2 mjf void
731 1.3.6.2 mjf softint_thread(void *cookie)
732 1.3.6.2 mjf {
733 1.3.6.2 mjf
734 1.3.6.2 mjf panic("softint_thread");
735 1.3.6.1 mjf }
736 1.3.6.1 mjf
737 1.3.6.1 mjf /*
738 1.3.6.2 mjf * softint_dispatch:
739 1.3.6.1 mjf *
740 1.3.6.2 mjf * Fast path: entry point from machine-dependent code.
741 1.3.6.1 mjf */
742 1.3.6.2 mjf void
743 1.3.6.2 mjf softint_dispatch(lwp_t *pinned, int s)
744 1.3.6.1 mjf {
745 1.3.6.3 mjf struct bintime now;
746 1.3.6.2 mjf softint_t *si;
747 1.3.6.2 mjf u_int timing;
748 1.3.6.2 mjf lwp_t *l;
749 1.3.6.2 mjf
750 1.3.6.2 mjf l = curlwp;
751 1.3.6.2 mjf si = l->l_private;
752 1.3.6.2 mjf
753 1.3.6.2 mjf /*
754 1.3.6.2 mjf * Note the interrupted LWP, and mark the current LWP as running
755 1.3.6.2 mjf * before proceeding. Although this must as a rule be done with
756 1.3.6.2 mjf * the LWP locked, at this point no external agents will want to
757 1.3.6.2 mjf * modify the interrupt LWP's state.
758 1.3.6.2 mjf */
759 1.3.6.2 mjf timing = (softint_timing ? LW_TIMEINTR : 0);
760 1.3.6.2 mjf l->l_switchto = pinned;
761 1.3.6.2 mjf l->l_stat = LSONPROC;
762 1.3.6.2 mjf l->l_flag |= (LW_RUNNING | timing);
763 1.3.6.2 mjf
764 1.3.6.2 mjf /*
765 1.3.6.2 mjf * Dispatch the interrupt. If softints are being timed, charge
766 1.3.6.2 mjf * for it.
767 1.3.6.2 mjf */
768 1.3.6.2 mjf if (timing)
769 1.3.6.3 mjf bintime(&l->l_stime);
770 1.3.6.2 mjf softint_execute(si, l, s);
771 1.3.6.2 mjf if (timing) {
772 1.3.6.3 mjf bintime(&now);
773 1.3.6.2 mjf updatertime(l, &now);
774 1.3.6.2 mjf l->l_flag &= ~LW_TIMEINTR;
775 1.3.6.2 mjf }
776 1.3.6.1 mjf
777 1.3.6.2 mjf /*
778 1.3.6.2 mjf * If we blocked while handling the interrupt, the pinned LWP is
779 1.3.6.2 mjf * gone so switch to the idle LWP. It will select a new LWP to
780 1.3.6.2 mjf * run.
781 1.3.6.2 mjf *
782 1.3.6.2 mjf * We must drop the priority level as switching at IPL_HIGH could
783 1.3.6.2 mjf * deadlock the system. We have already set si->si_active = 0,
784 1.3.6.2 mjf * which means another interrupt at this level can be triggered.
785 1.3.6.2 mjf * That's not be a problem: we are lowering to level 's' which will
786 1.3.6.2 mjf * prevent softint_dispatch() from being reentered at level 's',
787 1.3.6.2 mjf * until the priority is finally dropped to IPL_NONE on entry to
788 1.3.6.2 mjf * the idle loop.
789 1.3.6.2 mjf */
790 1.3.6.2 mjf l->l_stat = LSIDL;
791 1.3.6.2 mjf if (l->l_switchto == NULL) {
792 1.3.6.2 mjf splx(s);
793 1.3.6.2 mjf pmap_deactivate(l);
794 1.3.6.2 mjf lwp_exit_switchaway(l);
795 1.3.6.2 mjf /* NOTREACHED */
796 1.3.6.2 mjf }
797 1.3.6.2 mjf l->l_switchto = NULL;
798 1.3.6.2 mjf l->l_flag &= ~LW_RUNNING;
799 1.3.6.1 mjf }
800 1.3.6.2 mjf
801 1.3.6.2 mjf #endif /* !__HAVE_FAST_SOFTINTS */
802