intr.c revision 1.22 1 /* $NetBSD: intr.c,v 1.22 2009/12/01 09:50:51 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2008 Antti Kantee. All Rights Reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
16 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
19 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
20 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
22 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
23 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
24 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
25 * SUCH DAMAGE.
26 */
27
28 #include <sys/cdefs.h>
29 __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.22 2009/12/01 09:50:51 pooka Exp $");
30
31 #include <sys/param.h>
32 #include <sys/cpu.h>
33 #include <sys/kmem.h>
34 #include <sys/kthread.h>
35 #include <sys/intr.h>
36
37 #include <rump/rumpuser.h>
38
39 #include "rump_private.h"
40
41 /*
42 * Interrupt simulator. It executes hardclock() and softintrs.
43 */
44
45 time_t time_uptime = 0;
46
47 #define SI_MPSAFE 0x01
48 #define SI_ONLIST 0x02
49 #define SI_KILLME 0x04
50
51 struct softint {
52 void (*si_func)(void *);
53 void *si_arg;
54 int si_flags;
55 int si_level;
56
57 LIST_ENTRY(softint) si_entries;
58 };
59
60 static struct rumpuser_mtx *si_mtx;
61 struct softint_lev {
62 struct rumpuser_cv *si_cv;
63 LIST_HEAD(, softint) si_pending;
64 };
65
66 /* rumpuser structures since we call rumpuser interfaces directly */
67 static struct rumpuser_cv *clockcv;
68 static struct rumpuser_mtx *clockmtx;
69 static struct timespec clockbase, clockup;
70 static unsigned clkgen;
71
72 void
73 rump_getuptime(struct timespec *ts)
74 {
75 int startgen, i = 0;
76
77 do {
78 startgen = clkgen;
79 if (__predict_false(i++ > 10)) {
80 yield();
81 i = 0;
82 }
83 *ts = clockup;
84 } while (startgen != clkgen || clkgen % 2 != 0);
85 }
86
87 void
88 rump_gettime(struct timespec *ts)
89 {
90 struct timespec ts_up;
91
92 rump_getuptime(&ts_up);
93 timespecadd(&clockbase, &ts_up, ts);
94 }
95
96 /*
97 * clock "interrupt"
98 */
99 static void
100 doclock(void *noarg)
101 {
102 struct timespec tick, curtime;
103 uint64_t sec, nsec;
104 int ticks = 0, error;
105 extern int hz;
106
107 rumpuser_gettime(&sec, &nsec, &error);
108 clockbase.tv_sec = sec;
109 clockbase.tv_nsec = nsec;
110 curtime = clockbase;
111 tick.tv_sec = 0;
112 tick.tv_nsec = 1000000000/hz;
113
114 rumpuser_mutex_enter(clockmtx);
115 rumpuser_cv_signal(clockcv);
116
117 for (;;) {
118 callout_hardclock();
119
120 /* wait until the next tick. XXX: what if the clock changes? */
121 while (rumpuser_cv_timedwait(clockcv, clockmtx,
122 curtime.tv_sec, curtime.tv_nsec) == 0)
123 continue;
124
125 /* if !maincpu: continue */
126
127 if (++ticks == hz) {
128 time_uptime++;
129 ticks = 0;
130 }
131
132 clkgen++;
133 timespecadd(&clockup, &tick, &clockup);
134 clkgen++;
135 timespecadd(&clockup, &clockbase, &curtime);
136 }
137 }
138
139 /*
140 * Soft interrupt execution thread. Note that we run without a CPU
141 * context until we start processing the interrupt. This is to avoid
142 * lock recursion.
143 */
144 static void
145 sithread(void *arg)
146 {
147 struct softint *si;
148 void (*func)(void *) = NULL;
149 void *funarg;
150 bool mpsafe;
151 int mylevel = (uintptr_t)arg;
152 struct softint_lev *si_lvlp, *si_lvl;
153 struct cpu_data *cd = &curcpu()->ci_data;
154
155 rump_unschedule();
156
157 si_lvlp = cd->cpu_softcpu;
158 si_lvl = &si_lvlp[mylevel];
159
160 /*
161 * XXX: si_mtx is unnecessary, and should open an interface
162 * which allows to use schedmtx for the cv wait
163 */
164 rumpuser_mutex_enter_nowrap(si_mtx);
165 for (;;) {
166 if (!LIST_EMPTY(&si_lvl->si_pending)) {
167 si = LIST_FIRST(&si_lvl->si_pending);
168 func = si->si_func;
169 funarg = si->si_arg;
170 mpsafe = si->si_flags & SI_MPSAFE;
171
172 si->si_flags &= ~SI_ONLIST;
173 LIST_REMOVE(si, si_entries);
174 if (si->si_flags & SI_KILLME) {
175 rumpuser_mutex_exit(si_mtx);
176 rump_schedule();
177 softint_disestablish(si);
178 rump_unschedule();
179 rumpuser_mutex_enter_nowrap(si_mtx);
180 continue;
181 }
182 } else {
183 rumpuser_cv_wait_nowrap(si_lvl->si_cv, si_mtx);
184 continue;
185 }
186 rumpuser_mutex_exit(si_mtx);
187
188 rump_schedule();
189 if (!mpsafe)
190 KERNEL_LOCK(1, curlwp);
191 func(funarg);
192 if (!mpsafe)
193 KERNEL_UNLOCK_ONE(curlwp);
194 rump_unschedule();
195
196 rumpuser_mutex_enter_nowrap(si_mtx);
197 }
198
199 panic("sithread unreachable");
200 }
201
202 void
203 rump_intr_init()
204 {
205
206 rumpuser_mutex_init(&si_mtx);
207 rumpuser_cv_init(&clockcv);
208 rumpuser_mutex_init(&clockmtx);
209 }
210
211 void
212 softint_init(struct cpu_info *ci)
213 {
214 struct cpu_data *cd = &ci->ci_data;
215 struct softint_lev *slev;
216 int rv, i;
217
218 if (!rump_threads)
219 return;
220
221 slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
222 for (i = 0; i < SOFTINT_COUNT; i++) {
223 rumpuser_cv_init(&slev[i].si_cv);
224 LIST_INIT(&slev[i].si_pending);
225 }
226 cd->cpu_softcpu = slev;
227
228 for (i = 0; i < SOFTINT_COUNT; i++) {
229 rv = kthread_create(PRI_NONE,
230 KTHREAD_MPSAFE | KTHREAD_INTR, NULL,
231 sithread, (void *)(uintptr_t)i,
232 NULL, "rumpsi%d", i);
233 }
234
235 rumpuser_mutex_enter(clockmtx);
236 for (i = 0; i < ncpu; i++) {
237 rv = kthread_create(PRI_NONE,
238 KTHREAD_MPSAFE | KTHREAD_INTR,
239 cpu_lookup(i), doclock, NULL, NULL,
240 "rumpclk%d", i);
241 if (rv)
242 panic("clock thread creation failed: %d", rv);
243 }
244
245 /*
246 * Make sure we have a clocktime before returning.
247 * XXX: mp
248 */
249 rumpuser_cv_wait(clockcv, clockmtx);
250 rumpuser_mutex_exit(clockmtx);
251 }
252
253 /*
254 * Soft interrupts bring two choices. If we are running with thread
255 * support enabled, defer execution, otherwise execute in place.
256 * See softint_schedule().
257 *
258 * As there is currently no clear concept of when a thread finishes
259 * work (although rump_clear_curlwp() is close), simply execute all
260 * softints in the timer thread. This is probably not the most
261 * efficient method, but good enough for now.
262 */
263 void *
264 softint_establish(u_int flags, void (*func)(void *), void *arg)
265 {
266 struct softint *si;
267
268 si = kmem_alloc(sizeof(*si), KM_SLEEP);
269 si->si_func = func;
270 si->si_arg = arg;
271 si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
272 si->si_level = flags & SOFTINT_LVLMASK;
273 KASSERT(si->si_level < SOFTINT_COUNT);
274
275 return si;
276 }
277
278 void
279 softint_schedule(void *arg)
280 {
281 struct softint *si = arg;
282 struct cpu_data *cd = &curcpu()->ci_data;
283 struct softint_lev *si_lvl = cd->cpu_softcpu;
284
285 if (!rump_threads) {
286 si->si_func(si->si_arg);
287 } else {
288 if (!(si->si_flags & SI_ONLIST)) {
289 LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending,
290 si, si_entries);
291 si->si_flags |= SI_ONLIST;
292 }
293 }
294 }
295
296 /* flimsy disestablish: should wait for softints to finish */
297 void
298 softint_disestablish(void *cook)
299 {
300 struct softint *si = cook;
301
302 rumpuser_mutex_enter(si_mtx);
303 if (si->si_flags & SI_ONLIST) {
304 si->si_flags |= SI_KILLME;
305 return;
306 }
307 rumpuser_mutex_exit(si_mtx);
308 kmem_free(si, sizeof(*si));
309 }
310
311 void
312 rump_softint_run(struct cpu_info *ci)
313 {
314 struct cpu_data *cd = &ci->ci_data;
315 struct softint_lev *si_lvl = cd->cpu_softcpu;
316 int i;
317
318 if (!rump_threads)
319 return;
320
321 for (i = 0; i < SOFTINT_COUNT; i++) {
322 if (!LIST_EMPTY(&si_lvl[i].si_pending))
323 rumpuser_cv_signal(si_lvl[i].si_cv);
324 }
325 }
326
327 bool
328 cpu_intr_p(void)
329 {
330
331 return false;
332 }
333
334 bool
335 cpu_softintr_p(void)
336 {
337
338 return curlwp->l_pflag & LP_INTR;
339 }
340