intr.c revision 1.55 1 1.55 ad /* $NetBSD: intr.c,v 1.55 2019/12/16 22:47:55 ad Exp $ */
2 1.2 ad
3 1.5 pooka /*
4 1.48 pooka * Copyright (c) 2008-2010, 2015 Antti Kantee. All Rights Reserved.
5 1.2 ad *
6 1.2 ad * Redistribution and use in source and binary forms, with or without
7 1.2 ad * modification, are permitted provided that the following conditions
8 1.2 ad * are met:
9 1.2 ad * 1. Redistributions of source code must retain the above copyright
10 1.2 ad * notice, this list of conditions and the following disclaimer.
11 1.2 ad * 2. Redistributions in binary form must reproduce the above copyright
12 1.2 ad * notice, this list of conditions and the following disclaimer in the
13 1.2 ad * documentation and/or other materials provided with the distribution.
14 1.2 ad *
15 1.5 pooka * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 1.5 pooka * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 1.5 pooka * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 1.5 pooka * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 1.5 pooka * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 1.5 pooka * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 1.5 pooka * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 1.5 pooka * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 1.5 pooka * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 1.5 pooka * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 1.5 pooka * SUCH DAMAGE.
26 1.2 ad */
27 1.2 ad
28 1.11 pooka #include <sys/cdefs.h>
29 1.55 ad __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.55 2019/12/16 22:47:55 ad Exp $");
30 1.11 pooka
31 1.2 ad #include <sys/param.h>
32 1.29 martin #include <sys/atomic.h>
33 1.5 pooka #include <sys/cpu.h>
34 1.24 pooka #include <sys/kernel.h>
35 1.12 pooka #include <sys/kmem.h>
36 1.5 pooka #include <sys/kthread.h>
37 1.28 pooka #include <sys/malloc.h>
38 1.2 ad #include <sys/intr.h>
39 1.24 pooka #include <sys/timetc.h>
40 1.2 ad
41 1.54 pooka #include <rump-sys/kern.h>
42 1.54 pooka
43 1.5 pooka #include <rump/rumpuser.h>
44 1.5 pooka
45 1.5 pooka /*
46 1.5 pooka * Interrupt simulator. It executes hardclock() and softintrs.
47 1.5 pooka */
48 1.5 pooka
49 1.18 pooka #define SI_MPSAFE 0x01
50 1.32 pooka #define SI_KILLME 0x02
51 1.20 pooka
52 1.32 pooka struct softint_percpu;
53 1.5 pooka struct softint {
54 1.5 pooka void (*si_func)(void *);
55 1.5 pooka void *si_arg;
56 1.18 pooka int si_flags;
57 1.20 pooka int si_level;
58 1.5 pooka
59 1.32 pooka struct softint_percpu *si_entry; /* [0,ncpu-1] */
60 1.32 pooka };
61 1.32 pooka
62 1.32 pooka struct softint_percpu {
63 1.32 pooka struct softint *sip_parent;
64 1.32 pooka bool sip_onlist;
65 1.47 pooka bool sip_onlist_cpu;
66 1.32 pooka
67 1.48 pooka TAILQ_ENTRY(softint_percpu) sip_entries; /* scheduled */
68 1.47 pooka TAILQ_ENTRY(softint_percpu) sip_entries_cpu; /* to be scheduled */
69 1.2 ad };
70 1.10 pooka
71 1.22 pooka struct softint_lev {
72 1.20 pooka struct rumpuser_cv *si_cv;
73 1.48 pooka TAILQ_HEAD(, softint_percpu) si_pending;
74 1.22 pooka };
75 1.10 pooka
76 1.47 pooka static TAILQ_HEAD(, softint_percpu) sicpupending \
77 1.47 pooka = TAILQ_HEAD_INITIALIZER(sicpupending);
78 1.47 pooka static struct rumpuser_mtx *sicpumtx;
79 1.47 pooka static struct rumpuser_cv *sicpucv;
80 1.47 pooka
81 1.23 pooka kcondvar_t lbolt; /* Oh Kath Ra */
82 1.23 pooka
83 1.34 pooka static int ncpu_final;
84 1.24 pooka
85 1.51 pooka void noclock(void); void noclock(void) {return;}
86 1.51 pooka __strong_alias(sched_schedclock,noclock);
87 1.51 pooka __strong_alias(cpu_initclocks,noclock);
88 1.51 pooka __strong_alias(addupc_intr,noclock);
89 1.51 pooka __strong_alias(sched_tick,noclock);
90 1.51 pooka __strong_alias(setstatclockrate,noclock);
91 1.14 pooka
92 1.10 pooka /*
93 1.10 pooka * clock "interrupt"
94 1.10 pooka */
95 1.10 pooka static void
96 1.10 pooka doclock(void *noarg)
97 1.10 pooka {
98 1.38 pooka struct timespec thetick, curclock;
99 1.52 pooka struct clockframe *clkframe;
100 1.40 pooka int64_t sec;
101 1.40 pooka long nsec;
102 1.24 pooka int error;
103 1.51 pooka struct cpu_info *ci = curcpu();
104 1.14 pooka
105 1.39 pooka error = rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, &sec, &nsec);
106 1.38 pooka if (error)
107 1.38 pooka panic("clock: cannot get monotonic time");
108 1.38 pooka
109 1.38 pooka curclock.tv_sec = sec;
110 1.38 pooka curclock.tv_nsec = nsec;
111 1.24 pooka thetick.tv_sec = 0;
112 1.24 pooka thetick.tv_nsec = 1000000000/hz;
113 1.14 pooka
114 1.52 pooka /* generate dummy clockframe for hardclock to consume */
115 1.52 pooka clkframe = rump_cpu_makeclockframe();
116 1.51 pooka
117 1.10 pooka for (;;) {
118 1.51 pooka int lbolt_ticks = 0;
119 1.51 pooka
120 1.52 pooka hardclock(clkframe);
121 1.51 pooka if (CPU_IS_PRIMARY(ci)) {
122 1.51 pooka if (++lbolt_ticks >= hz) {
123 1.51 pooka lbolt_ticks = 0;
124 1.51 pooka cv_broadcast(&lbolt);
125 1.51 pooka }
126 1.51 pooka }
127 1.5 pooka
128 1.39 pooka error = rumpuser_clock_sleep(RUMPUSER_CLOCK_ABSMONO,
129 1.39 pooka curclock.tv_sec, curclock.tv_nsec);
130 1.53 pooka if (error) {
131 1.53 pooka panic("rumpuser_clock_sleep failed with error %d",
132 1.53 pooka error);
133 1.53 pooka }
134 1.38 pooka timespecadd(&curclock, &thetick, &curclock);
135 1.10 pooka }
136 1.10 pooka }
137 1.8 pooka
138 1.10 pooka /*
139 1.28 pooka * Soft interrupt execution thread. This thread is pinned to the
140 1.28 pooka * same CPU that scheduled the interrupt, so we don't need to do
141 1.28 pooka * lock against si_lvl.
142 1.10 pooka */
143 1.10 pooka static void
144 1.10 pooka sithread(void *arg)
145 1.10 pooka {
146 1.32 pooka struct softint_percpu *sip;
147 1.10 pooka struct softint *si;
148 1.10 pooka void (*func)(void *) = NULL;
149 1.10 pooka void *funarg;
150 1.10 pooka bool mpsafe;
151 1.20 pooka int mylevel = (uintptr_t)arg;
152 1.22 pooka struct softint_lev *si_lvlp, *si_lvl;
153 1.22 pooka struct cpu_data *cd = &curcpu()->ci_data;
154 1.20 pooka
155 1.22 pooka si_lvlp = cd->cpu_softcpu;
156 1.22 pooka si_lvl = &si_lvlp[mylevel];
157 1.22 pooka
158 1.10 pooka for (;;) {
159 1.48 pooka if (!TAILQ_EMPTY(&si_lvl->si_pending)) {
160 1.48 pooka sip = TAILQ_FIRST(&si_lvl->si_pending);
161 1.32 pooka si = sip->sip_parent;
162 1.32 pooka
163 1.5 pooka func = si->si_func;
164 1.5 pooka funarg = si->si_arg;
165 1.18 pooka mpsafe = si->si_flags & SI_MPSAFE;
166 1.5 pooka
167 1.32 pooka sip->sip_onlist = false;
168 1.48 pooka TAILQ_REMOVE(&si_lvl->si_pending, sip, sip_entries);
169 1.20 pooka if (si->si_flags & SI_KILLME) {
170 1.18 pooka softint_disestablish(si);
171 1.20 pooka continue;
172 1.20 pooka }
173 1.10 pooka } else {
174 1.28 pooka rump_schedlock_cv_wait(si_lvl->si_cv);
175 1.10 pooka continue;
176 1.5 pooka }
177 1.5 pooka
178 1.10 pooka if (!mpsafe)
179 1.10 pooka KERNEL_LOCK(1, curlwp);
180 1.10 pooka func(funarg);
181 1.10 pooka if (!mpsafe)
182 1.10 pooka KERNEL_UNLOCK_ONE(curlwp);
183 1.5 pooka }
184 1.20 pooka
185 1.20 pooka panic("sithread unreachable");
186 1.5 pooka }
187 1.2 ad
188 1.47 pooka /*
189 1.47 pooka * Helper for softint_schedule_cpu()
190 1.47 pooka */
191 1.47 pooka static void
192 1.47 pooka sithread_cpu_bouncer(void *arg)
193 1.47 pooka {
194 1.47 pooka struct lwp *me;
195 1.47 pooka
196 1.47 pooka me = curlwp;
197 1.47 pooka me->l_pflag |= LP_BOUND;
198 1.47 pooka
199 1.47 pooka rump_unschedule();
200 1.47 pooka for (;;) {
201 1.47 pooka struct softint_percpu *sip;
202 1.47 pooka struct softint *si;
203 1.47 pooka struct cpu_info *ci;
204 1.47 pooka unsigned int cidx;
205 1.47 pooka
206 1.47 pooka rumpuser_mutex_enter_nowrap(sicpumtx);
207 1.47 pooka while (TAILQ_EMPTY(&sicpupending)) {
208 1.47 pooka rumpuser_cv_wait_nowrap(sicpucv, sicpumtx);
209 1.47 pooka }
210 1.47 pooka sip = TAILQ_FIRST(&sicpupending);
211 1.47 pooka TAILQ_REMOVE(&sicpupending, sip, sip_entries_cpu);
212 1.47 pooka sip->sip_onlist_cpu = false;
213 1.47 pooka rumpuser_mutex_exit(sicpumtx);
214 1.47 pooka
215 1.47 pooka /*
216 1.47 pooka * ok, now figure out which cpu we need the softint to
217 1.47 pooka * be handled on
218 1.47 pooka */
219 1.47 pooka si = sip->sip_parent;
220 1.47 pooka cidx = sip - si->si_entry;
221 1.47 pooka ci = cpu_lookup(cidx);
222 1.47 pooka me->l_target_cpu = ci;
223 1.47 pooka
224 1.47 pooka /* schedule ourselves there, and then schedule the softint */
225 1.47 pooka rump_schedule();
226 1.47 pooka KASSERT(curcpu() == ci);
227 1.47 pooka softint_schedule(si);
228 1.47 pooka rump_unschedule();
229 1.47 pooka }
230 1.47 pooka panic("sithread_cpu_bouncer unreasonable");
231 1.47 pooka }
232 1.47 pooka
233 1.41 pooka static kmutex_t sithr_emtx;
234 1.41 pooka static unsigned int sithr_est;
235 1.41 pooka static int sithr_canest;
236 1.41 pooka
237 1.41 pooka /*
238 1.41 pooka * Create softint handler threads when the softint for each respective
239 1.41 pooka * level is established for the first time. Most rump kernels don't
240 1.41 pooka * need at least half of the softint levels, so on-demand saves bootstrap
241 1.41 pooka * time and memory resources. Note, though, that this routine may be
242 1.41 pooka * called before it's possible to call kthread_create(). Creation of
243 1.41 pooka * those softints (SOFTINT_CLOCK, as of writing this) will be deferred
244 1.41 pooka * to until softint_init() is called for the main CPU.
245 1.41 pooka */
246 1.41 pooka static void
247 1.41 pooka sithread_establish(int level)
248 1.41 pooka {
249 1.41 pooka int docreate, rv;
250 1.41 pooka int lvlbit = 1<<level;
251 1.41 pooka int i;
252 1.41 pooka
253 1.41 pooka KASSERT((level & ~SOFTINT_LVLMASK) == 0);
254 1.41 pooka if (__predict_true(sithr_est & lvlbit))
255 1.41 pooka return;
256 1.41 pooka
257 1.41 pooka mutex_enter(&sithr_emtx);
258 1.41 pooka docreate = (sithr_est & lvlbit) == 0 && sithr_canest;
259 1.41 pooka sithr_est |= lvlbit;
260 1.41 pooka mutex_exit(&sithr_emtx);
261 1.41 pooka
262 1.41 pooka if (docreate) {
263 1.41 pooka for (i = 0; i < ncpu_final; i++) {
264 1.41 pooka if ((rv = kthread_create(PRI_NONE,
265 1.41 pooka KTHREAD_MPSAFE | KTHREAD_INTR,
266 1.41 pooka cpu_lookup(i), sithread, (void *)(uintptr_t)level,
267 1.41 pooka NULL, "rsi%d/%d", i, level)) != 0)
268 1.41 pooka panic("softint thread create failed: %d", rv);
269 1.41 pooka }
270 1.41 pooka }
271 1.41 pooka }
272 1.41 pooka
273 1.5 pooka void
274 1.34 pooka rump_intr_init(int numcpu)
275 1.22 pooka {
276 1.22 pooka
277 1.23 pooka cv_init(&lbolt, "oh kath ra");
278 1.41 pooka mutex_init(&sithr_emtx, MUTEX_DEFAULT, IPL_NONE);
279 1.34 pooka ncpu_final = numcpu;
280 1.22 pooka }
281 1.22 pooka
282 1.22 pooka void
283 1.5 pooka softint_init(struct cpu_info *ci)
284 1.2 ad {
285 1.22 pooka struct cpu_data *cd = &ci->ci_data;
286 1.22 pooka struct softint_lev *slev;
287 1.20 pooka int rv, i;
288 1.5 pooka
289 1.22 pooka if (!rump_threads)
290 1.22 pooka return;
291 1.22 pooka
292 1.46 pooka slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
293 1.46 pooka for (i = 0; i < SOFTINT_COUNT; i++) {
294 1.46 pooka rumpuser_cv_init(&slev[i].si_cv);
295 1.48 pooka TAILQ_INIT(&slev[i].si_pending);
296 1.46 pooka }
297 1.46 pooka cd->cpu_softcpu = slev;
298 1.46 pooka
299 1.41 pooka /* overloaded global init ... */
300 1.46 pooka /* XXX: should be done the last time we are called */
301 1.25 pooka if (ci->ci_index == 0) {
302 1.41 pooka int sithr_swap;
303 1.41 pooka
304 1.51 pooka /* pretend that we have our own for these */
305 1.51 pooka stathz = 1;
306 1.51 pooka schedhz = 1;
307 1.51 pooka profhz = 1;
308 1.51 pooka
309 1.51 pooka initclocks();
310 1.41 pooka
311 1.41 pooka /* create deferred softint threads */
312 1.41 pooka mutex_enter(&sithr_emtx);
313 1.41 pooka sithr_swap = sithr_est;
314 1.41 pooka sithr_est = 0;
315 1.41 pooka sithr_canest = 1;
316 1.41 pooka mutex_exit(&sithr_emtx);
317 1.41 pooka for (i = 0; i < SOFTINT_COUNT; i++) {
318 1.41 pooka if (sithr_swap & (1<<i))
319 1.41 pooka sithread_establish(i);
320 1.41 pooka }
321 1.25 pooka }
322 1.25 pooka
323 1.41 pooka /* well, not really a "soft" interrupt ... */
324 1.41 pooka if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
325 1.41 pooka ci, doclock, NULL, NULL, "rumpclk%d", ci->ci_index)) != 0)
326 1.25 pooka panic("clock thread creation failed: %d", rv);
327 1.47 pooka
328 1.47 pooka /* not one either, but at least a softint helper */
329 1.47 pooka rumpuser_mutex_init(&sicpumtx, RUMPUSER_MTX_SPIN);
330 1.47 pooka rumpuser_cv_init(&sicpucv);
331 1.47 pooka if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
332 1.47 pooka NULL, sithread_cpu_bouncer, NULL, NULL, "sipbnc")) != 0)
333 1.47 pooka panic("softint cpu bouncer creation failed: %d", rv);
334 1.2 ad }
335 1.2 ad
336 1.5 pooka void *
337 1.5 pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
338 1.2 ad {
339 1.5 pooka struct softint *si;
340 1.32 pooka struct softint_percpu *sip;
341 1.41 pooka int level = flags & SOFTINT_LVLMASK;
342 1.32 pooka int i;
343 1.2 ad
344 1.28 pooka si = malloc(sizeof(*si), M_TEMP, M_WAITOK);
345 1.5 pooka si->si_func = func;
346 1.5 pooka si->si_arg = arg;
347 1.18 pooka si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
348 1.41 pooka si->si_level = level;
349 1.20 pooka KASSERT(si->si_level < SOFTINT_COUNT);
350 1.34 pooka si->si_entry = malloc(sizeof(*si->si_entry) * ncpu_final,
351 1.32 pooka M_TEMP, M_WAITOK | M_ZERO);
352 1.34 pooka for (i = 0; i < ncpu_final; i++) {
353 1.32 pooka sip = &si->si_entry[i];
354 1.32 pooka sip->sip_parent = si;
355 1.32 pooka }
356 1.41 pooka sithread_establish(level);
357 1.5 pooka
358 1.5 pooka return si;
359 1.2 ad }
360 1.2 ad
361 1.47 pooka static struct softint_percpu *
362 1.47 pooka sitosip(struct softint *si, struct cpu_info *ci)
363 1.47 pooka {
364 1.47 pooka
365 1.47 pooka return &si->si_entry[ci->ci_index];
366 1.47 pooka }
367 1.47 pooka
368 1.33 pooka /*
369 1.33 pooka * Soft interrupts bring two choices. If we are running with thread
370 1.33 pooka * support enabled, defer execution, otherwise execute in place.
371 1.33 pooka */
372 1.33 pooka
373 1.2 ad void
374 1.2 ad softint_schedule(void *arg)
375 1.2 ad {
376 1.5 pooka struct softint *si = arg;
377 1.42 pooka struct cpu_info *ci = curcpu();
378 1.47 pooka struct softint_percpu *sip = sitosip(si, ci);
379 1.42 pooka struct cpu_data *cd = &ci->ci_data;
380 1.22 pooka struct softint_lev *si_lvl = cd->cpu_softcpu;
381 1.2 ad
382 1.5 pooka if (!rump_threads) {
383 1.5 pooka si->si_func(si->si_arg);
384 1.5 pooka } else {
385 1.32 pooka if (!sip->sip_onlist) {
386 1.48 pooka TAILQ_INSERT_TAIL(&si_lvl[si->si_level].si_pending,
387 1.32 pooka sip, sip_entries);
388 1.32 pooka sip->sip_onlist = true;
389 1.5 pooka }
390 1.5 pooka }
391 1.2 ad }
392 1.2 ad
393 1.47 pooka /*
394 1.47 pooka * Like softint_schedule(), except schedule softint to be handled on
395 1.47 pooka * the core designated by ci_tgt instead of the core the call is made on.
396 1.47 pooka *
397 1.47 pooka * Unlike softint_schedule(), the performance is not important
398 1.47 pooka * (unless ci_tgt == curcpu): high-performance rump kernel I/O stacks
399 1.47 pooka * should arrange data to already be on the right core at the driver
400 1.47 pooka * layer.
401 1.47 pooka */
402 1.43 rmind void
403 1.47 pooka softint_schedule_cpu(void *arg, struct cpu_info *ci_tgt)
404 1.43 rmind {
405 1.47 pooka struct softint *si = arg;
406 1.47 pooka struct cpu_info *ci_cur = curcpu();
407 1.47 pooka struct softint_percpu *sip;
408 1.47 pooka
409 1.47 pooka KASSERT(rump_threads);
410 1.47 pooka
411 1.47 pooka /* preferred case (which can be optimized some day) */
412 1.47 pooka if (ci_cur == ci_tgt) {
413 1.47 pooka softint_schedule(si);
414 1.47 pooka return;
415 1.47 pooka }
416 1.47 pooka
417 1.44 rmind /*
418 1.47 pooka * no? then it's softint turtles all the way down
419 1.44 rmind */
420 1.47 pooka
421 1.47 pooka sip = sitosip(si, ci_tgt);
422 1.47 pooka rumpuser_mutex_enter_nowrap(sicpumtx);
423 1.47 pooka if (sip->sip_onlist_cpu) {
424 1.47 pooka rumpuser_mutex_exit(sicpumtx);
425 1.47 pooka return;
426 1.47 pooka }
427 1.47 pooka TAILQ_INSERT_TAIL(&sicpupending, sip, sip_entries_cpu);
428 1.47 pooka sip->sip_onlist_cpu = true;
429 1.47 pooka rumpuser_cv_signal(sicpucv);
430 1.47 pooka rumpuser_mutex_exit(sicpumtx);
431 1.43 rmind }
432 1.43 rmind
433 1.32 pooka /*
434 1.32 pooka * flimsy disestablish: should wait for softints to finish.
435 1.32 pooka */
436 1.18 pooka void
437 1.18 pooka softint_disestablish(void *cook)
438 1.18 pooka {
439 1.18 pooka struct softint *si = cook;
440 1.32 pooka int i;
441 1.18 pooka
442 1.34 pooka for (i = 0; i < ncpu_final; i++) {
443 1.32 pooka struct softint_percpu *sip;
444 1.32 pooka
445 1.32 pooka sip = &si->si_entry[i];
446 1.32 pooka if (sip->sip_onlist) {
447 1.32 pooka si->si_flags |= SI_KILLME;
448 1.32 pooka return;
449 1.32 pooka }
450 1.18 pooka }
451 1.32 pooka free(si->si_entry, M_TEMP);
452 1.28 pooka free(si, M_TEMP);
453 1.18 pooka }
454 1.18 pooka
455 1.20 pooka void
456 1.20 pooka rump_softint_run(struct cpu_info *ci)
457 1.20 pooka {
458 1.22 pooka struct cpu_data *cd = &ci->ci_data;
459 1.22 pooka struct softint_lev *si_lvl = cd->cpu_softcpu;
460 1.20 pooka int i;
461 1.20 pooka
462 1.22 pooka if (!rump_threads)
463 1.22 pooka return;
464 1.22 pooka
465 1.20 pooka for (i = 0; i < SOFTINT_COUNT; i++) {
466 1.48 pooka if (!TAILQ_EMPTY(&si_lvl[i].si_pending))
467 1.22 pooka rumpuser_cv_signal(si_lvl[i].si_cv);
468 1.20 pooka }
469 1.20 pooka }
470 1.20 pooka
471 1.2 ad bool
472 1.9 christos cpu_intr_p(void)
473 1.2 ad {
474 1.2 ad
475 1.2 ad return false;
476 1.2 ad }
477