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