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