intr.c revision 1.32 1 1.32 pooka /* $NetBSD: intr.c,v 1.32 2010/08/15 21:28:33 pooka Exp $ */
2 1.2 ad
3 1.5 pooka /*
4 1.5 pooka * Copyright (c) 2008 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.32 pooka __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.32 2010/08/15 21:28:33 pooka 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.5 pooka #include <rump/rumpuser.h>
42 1.5 pooka
43 1.5 pooka #include "rump_private.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.32 pooka
66 1.32 pooka LIST_ENTRY(softint_percpu) sip_entries;
67 1.2 ad };
68 1.10 pooka
69 1.22 pooka struct softint_lev {
70 1.20 pooka struct rumpuser_cv *si_cv;
71 1.32 pooka LIST_HEAD(, softint_percpu) si_pending;
72 1.22 pooka };
73 1.10 pooka
74 1.23 pooka kcondvar_t lbolt; /* Oh Kath Ra */
75 1.23 pooka
76 1.24 pooka static u_int ticks;
77 1.24 pooka
78 1.24 pooka static u_int
79 1.24 pooka rumptc_get(struct timecounter *tc)
80 1.14 pooka {
81 1.14 pooka
82 1.24 pooka KASSERT(rump_threads);
83 1.24 pooka return ticks;
84 1.16 pooka }
85 1.16 pooka
86 1.24 pooka static struct timecounter rumptc = {
87 1.24 pooka .tc_get_timecount = rumptc_get,
88 1.24 pooka .tc_poll_pps = NULL,
89 1.24 pooka .tc_counter_mask = ~0,
90 1.24 pooka .tc_frequency = 0,
91 1.24 pooka .tc_name = "rumpclk",
92 1.24 pooka .tc_quality = 0,
93 1.24 pooka };
94 1.14 pooka
95 1.10 pooka /*
96 1.10 pooka * clock "interrupt"
97 1.10 pooka */
98 1.10 pooka static void
99 1.10 pooka doclock(void *noarg)
100 1.10 pooka {
101 1.26 pooka struct timespec clockbase, clockup;
102 1.24 pooka struct timespec thetick, curtime;
103 1.26 pooka struct rumpuser_cv *clockcv;
104 1.26 pooka struct rumpuser_mtx *clockmtx;
105 1.15 pooka uint64_t sec, nsec;
106 1.24 pooka int error;
107 1.10 pooka extern int hz;
108 1.14 pooka
109 1.26 pooka memset(&clockup, 0, sizeof(clockup));
110 1.15 pooka rumpuser_gettime(&sec, &nsec, &error);
111 1.16 pooka clockbase.tv_sec = sec;
112 1.16 pooka clockbase.tv_nsec = nsec;
113 1.16 pooka curtime = clockbase;
114 1.24 pooka thetick.tv_sec = 0;
115 1.24 pooka thetick.tv_nsec = 1000000000/hz;
116 1.14 pooka
117 1.26 pooka /* XXX: dummies */
118 1.26 pooka rumpuser_cv_init(&clockcv);
119 1.26 pooka rumpuser_mutex_init(&clockmtx);
120 1.26 pooka
121 1.14 pooka rumpuser_mutex_enter(clockmtx);
122 1.10 pooka for (;;) {
123 1.5 pooka callout_hardclock();
124 1.5 pooka
125 1.22 pooka /* wait until the next tick. XXX: what if the clock changes? */
126 1.22 pooka while (rumpuser_cv_timedwait(clockcv, clockmtx,
127 1.22 pooka curtime.tv_sec, curtime.tv_nsec) == 0)
128 1.22 pooka continue;
129 1.26 pooka
130 1.26 pooka /* XXX: sync with a) virtual clock b) host clock */
131 1.26 pooka timespecadd(&clockup, &clockbase, &curtime);
132 1.25 pooka timespecadd(&clockup, &thetick, &clockup);
133 1.26 pooka
134 1.26 pooka #if 0
135 1.26 pooka /* CPU_IS_PRIMARY is MD and hence unreliably correct here */
136 1.26 pooka if (!CPU_IS_PRIMARY(curcpu()))
137 1.26 pooka continue;
138 1.26 pooka #else
139 1.27 pooka if (curcpu()->ci_index != 0)
140 1.25 pooka continue;
141 1.26 pooka #endif
142 1.22 pooka
143 1.24 pooka if ((++ticks % hz) == 0) {
144 1.23 pooka cv_broadcast(&lbolt);
145 1.8 pooka }
146 1.24 pooka tc_ticktock();
147 1.10 pooka }
148 1.10 pooka }
149 1.8 pooka
150 1.10 pooka /*
151 1.28 pooka * Soft interrupt execution thread. This thread is pinned to the
152 1.28 pooka * same CPU that scheduled the interrupt, so we don't need to do
153 1.28 pooka * lock against si_lvl.
154 1.10 pooka */
155 1.10 pooka static void
156 1.10 pooka sithread(void *arg)
157 1.10 pooka {
158 1.32 pooka struct softint_percpu *sip;
159 1.10 pooka struct softint *si;
160 1.10 pooka void (*func)(void *) = NULL;
161 1.10 pooka void *funarg;
162 1.10 pooka bool mpsafe;
163 1.20 pooka int mylevel = (uintptr_t)arg;
164 1.22 pooka struct softint_lev *si_lvlp, *si_lvl;
165 1.22 pooka struct cpu_data *cd = &curcpu()->ci_data;
166 1.20 pooka
167 1.22 pooka si_lvlp = cd->cpu_softcpu;
168 1.22 pooka si_lvl = &si_lvlp[mylevel];
169 1.22 pooka
170 1.10 pooka for (;;) {
171 1.20 pooka if (!LIST_EMPTY(&si_lvl->si_pending)) {
172 1.32 pooka sip = LIST_FIRST(&si_lvl->si_pending);
173 1.32 pooka si = sip->sip_parent;
174 1.32 pooka
175 1.5 pooka func = si->si_func;
176 1.5 pooka funarg = si->si_arg;
177 1.18 pooka mpsafe = si->si_flags & SI_MPSAFE;
178 1.5 pooka
179 1.32 pooka sip->sip_onlist = false;
180 1.32 pooka LIST_REMOVE(sip, sip_entries);
181 1.20 pooka if (si->si_flags & SI_KILLME) {
182 1.18 pooka softint_disestablish(si);
183 1.20 pooka continue;
184 1.20 pooka }
185 1.10 pooka } else {
186 1.28 pooka rump_schedlock_cv_wait(si_lvl->si_cv);
187 1.10 pooka continue;
188 1.5 pooka }
189 1.5 pooka
190 1.10 pooka if (!mpsafe)
191 1.10 pooka KERNEL_LOCK(1, curlwp);
192 1.10 pooka func(funarg);
193 1.10 pooka if (!mpsafe)
194 1.10 pooka KERNEL_UNLOCK_ONE(curlwp);
195 1.5 pooka }
196 1.20 pooka
197 1.20 pooka panic("sithread unreachable");
198 1.5 pooka }
199 1.2 ad
200 1.5 pooka void
201 1.22 pooka rump_intr_init()
202 1.22 pooka {
203 1.22 pooka
204 1.23 pooka cv_init(&lbolt, "oh kath ra");
205 1.22 pooka }
206 1.22 pooka
207 1.22 pooka void
208 1.5 pooka softint_init(struct cpu_info *ci)
209 1.2 ad {
210 1.22 pooka struct cpu_data *cd = &ci->ci_data;
211 1.22 pooka struct softint_lev *slev;
212 1.20 pooka int rv, i;
213 1.5 pooka
214 1.22 pooka if (!rump_threads)
215 1.22 pooka return;
216 1.22 pooka
217 1.25 pooka /* XXX */
218 1.25 pooka if (ci->ci_index == 0) {
219 1.25 pooka rumptc.tc_frequency = hz;
220 1.25 pooka tc_init(&rumptc);
221 1.25 pooka }
222 1.25 pooka
223 1.22 pooka slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
224 1.20 pooka for (i = 0; i < SOFTINT_COUNT; i++) {
225 1.22 pooka rumpuser_cv_init(&slev[i].si_cv);
226 1.22 pooka LIST_INIT(&slev[i].si_pending);
227 1.20 pooka }
228 1.22 pooka cd->cpu_softcpu = slev;
229 1.2 ad
230 1.28 pooka /* softint might run on different physical CPU */
231 1.28 pooka membar_sync();
232 1.28 pooka
233 1.22 pooka for (i = 0; i < SOFTINT_COUNT; i++) {
234 1.22 pooka rv = kthread_create(PRI_NONE,
235 1.25 pooka KTHREAD_MPSAFE | KTHREAD_INTR, ci,
236 1.22 pooka sithread, (void *)(uintptr_t)i,
237 1.30 pooka NULL, "rsi%d/%d", ci->ci_index, i);
238 1.22 pooka }
239 1.14 pooka
240 1.31 pooka rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
241 1.30 pooka ci, doclock, NULL, NULL, "rumpclk%d", ci->ci_index);
242 1.25 pooka if (rv)
243 1.25 pooka panic("clock thread creation failed: %d", rv);
244 1.2 ad }
245 1.2 ad
246 1.5 pooka /*
247 1.5 pooka * Soft interrupts bring two choices. If we are running with thread
248 1.5 pooka * support enabled, defer execution, otherwise execute in place.
249 1.5 pooka * See softint_schedule().
250 1.5 pooka *
251 1.5 pooka * As there is currently no clear concept of when a thread finishes
252 1.5 pooka * work (although rump_clear_curlwp() is close), simply execute all
253 1.5 pooka * softints in the timer thread. This is probably not the most
254 1.5 pooka * efficient method, but good enough for now.
255 1.5 pooka */
256 1.5 pooka void *
257 1.5 pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
258 1.2 ad {
259 1.5 pooka struct softint *si;
260 1.32 pooka struct softint_percpu *sip;
261 1.32 pooka int i;
262 1.2 ad
263 1.28 pooka si = malloc(sizeof(*si), M_TEMP, M_WAITOK);
264 1.5 pooka si->si_func = func;
265 1.5 pooka si->si_arg = arg;
266 1.18 pooka si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
267 1.20 pooka si->si_level = flags & SOFTINT_LVLMASK;
268 1.20 pooka KASSERT(si->si_level < SOFTINT_COUNT);
269 1.32 pooka si->si_entry = malloc(sizeof(*si->si_entry) * ncpu,
270 1.32 pooka M_TEMP, M_WAITOK | M_ZERO);
271 1.32 pooka for (i = 0; i < ncpu; i++) {
272 1.32 pooka sip = &si->si_entry[i];
273 1.32 pooka sip->sip_parent = si;
274 1.32 pooka }
275 1.5 pooka
276 1.5 pooka return si;
277 1.2 ad }
278 1.2 ad
279 1.2 ad void
280 1.2 ad softint_schedule(void *arg)
281 1.2 ad {
282 1.5 pooka struct softint *si = arg;
283 1.32 pooka struct softint_percpu *sip = &si->si_entry[curcpu()->ci_index];
284 1.22 pooka struct cpu_data *cd = &curcpu()->ci_data;
285 1.22 pooka struct softint_lev *si_lvl = cd->cpu_softcpu;
286 1.2 ad
287 1.5 pooka if (!rump_threads) {
288 1.5 pooka si->si_func(si->si_arg);
289 1.5 pooka } else {
290 1.32 pooka if (!sip->sip_onlist) {
291 1.22 pooka LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending,
292 1.32 pooka sip, sip_entries);
293 1.32 pooka sip->sip_onlist = true;
294 1.5 pooka }
295 1.5 pooka }
296 1.2 ad }
297 1.2 ad
298 1.32 pooka /*
299 1.32 pooka * flimsy disestablish: should wait for softints to finish.
300 1.32 pooka */
301 1.18 pooka void
302 1.18 pooka softint_disestablish(void *cook)
303 1.18 pooka {
304 1.18 pooka struct softint *si = cook;
305 1.32 pooka int i;
306 1.18 pooka
307 1.32 pooka for (i = 0; i < ncpu; i++) {
308 1.32 pooka struct softint_percpu *sip;
309 1.32 pooka
310 1.32 pooka sip = &si->si_entry[i];
311 1.32 pooka if (sip->sip_onlist) {
312 1.32 pooka si->si_flags |= SI_KILLME;
313 1.32 pooka return;
314 1.32 pooka }
315 1.18 pooka }
316 1.32 pooka free(si->si_entry, M_TEMP);
317 1.28 pooka free(si, M_TEMP);
318 1.18 pooka }
319 1.18 pooka
320 1.20 pooka void
321 1.20 pooka rump_softint_run(struct cpu_info *ci)
322 1.20 pooka {
323 1.22 pooka struct cpu_data *cd = &ci->ci_data;
324 1.22 pooka struct softint_lev *si_lvl = cd->cpu_softcpu;
325 1.20 pooka int i;
326 1.20 pooka
327 1.22 pooka if (!rump_threads)
328 1.22 pooka return;
329 1.22 pooka
330 1.20 pooka for (i = 0; i < SOFTINT_COUNT; i++) {
331 1.22 pooka if (!LIST_EMPTY(&si_lvl[i].si_pending))
332 1.22 pooka rumpuser_cv_signal(si_lvl[i].si_cv);
333 1.20 pooka }
334 1.20 pooka }
335 1.20 pooka
336 1.2 ad bool
337 1.9 christos cpu_intr_p(void)
338 1.2 ad {
339 1.2 ad
340 1.2 ad return false;
341 1.2 ad }
342 1.19 pooka
343 1.19 pooka bool
344 1.19 pooka cpu_softintr_p(void)
345 1.19 pooka {
346 1.19 pooka
347 1.20 pooka return curlwp->l_pflag & LP_INTR;
348 1.19 pooka }
349