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