intr.c revision 1.19 1 1.19 pooka /* $NetBSD: intr.c,v 1.19 2009/11/06 15:22:16 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.19 pooka __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.19 2009/11/06 15:22:16 pooka Exp $");
30 1.11 pooka
31 1.2 ad #include <sys/param.h>
32 1.5 pooka #include <sys/cpu.h>
33 1.12 pooka #include <sys/kmem.h>
34 1.5 pooka #include <sys/kthread.h>
35 1.2 ad #include <sys/intr.h>
36 1.2 ad
37 1.5 pooka #include <rump/rumpuser.h>
38 1.5 pooka
39 1.5 pooka #include "rump_private.h"
40 1.5 pooka
41 1.5 pooka /*
42 1.5 pooka * Interrupt simulator. It executes hardclock() and softintrs.
43 1.5 pooka */
44 1.5 pooka
45 1.14 pooka time_t time_uptime = 0;
46 1.8 pooka
47 1.18 pooka #define SI_MPSAFE 0x01
48 1.18 pooka #define SI_ONLIST 0x02
49 1.18 pooka #define SI_KILLME 0x04
50 1.5 pooka struct softint {
51 1.5 pooka void (*si_func)(void *);
52 1.5 pooka void *si_arg;
53 1.18 pooka int si_flags;
54 1.5 pooka
55 1.5 pooka LIST_ENTRY(softint) si_entries;
56 1.2 ad };
57 1.5 pooka static LIST_HEAD(, softint) si_pending = LIST_HEAD_INITIALIZER(si_pending);
58 1.5 pooka static kmutex_t si_mtx;
59 1.5 pooka static kcondvar_t si_cv;
60 1.5 pooka
61 1.10 pooka #define INTRTHREAD_DEFAULT 2
62 1.10 pooka #define INTRTHREAD_MAX 20
63 1.10 pooka static int wrkidle, wrktotal;
64 1.10 pooka
65 1.10 pooka static void sithread(void *);
66 1.10 pooka
67 1.5 pooka static void
68 1.10 pooka makeworker(bool bootstrap)
69 1.5 pooka {
70 1.10 pooka int rv;
71 1.10 pooka
72 1.10 pooka if (wrktotal > INTRTHREAD_MAX) {
73 1.10 pooka /* XXX: ratecheck */
74 1.10 pooka printf("maximum interrupt threads (%d) reached\n",
75 1.10 pooka INTRTHREAD_MAX);
76 1.10 pooka return;
77 1.10 pooka }
78 1.13 pooka rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_INTR, NULL,
79 1.13 pooka sithread, NULL, NULL, "rumpsi");
80 1.10 pooka if (rv) {
81 1.10 pooka if (bootstrap)
82 1.10 pooka panic("intr thread creation failed %d", rv);
83 1.10 pooka else
84 1.10 pooka printf("intr thread creation failed %d\n", rv);
85 1.10 pooka } else {
86 1.10 pooka wrkidle++;
87 1.10 pooka wrktotal++;
88 1.10 pooka }
89 1.10 pooka }
90 1.10 pooka
91 1.14 pooka /* rumpuser structures since we call rumpuser interfaces directly */
92 1.14 pooka static struct rumpuser_cv *clockcv;
93 1.14 pooka static struct rumpuser_mtx *clockmtx;
94 1.16 pooka static struct timespec clockbase, clockup;
95 1.16 pooka static unsigned clkgen;
96 1.14 pooka
97 1.14 pooka void
98 1.16 pooka rump_getuptime(struct timespec *ts)
99 1.14 pooka {
100 1.17 pooka int startgen, i = 0;
101 1.14 pooka
102 1.14 pooka do {
103 1.16 pooka startgen = clkgen;
104 1.16 pooka if (__predict_false(i++ > 10)) {
105 1.16 pooka yield();
106 1.16 pooka i = 0;
107 1.16 pooka }
108 1.16 pooka *ts = clockup;
109 1.16 pooka } while (startgen != clkgen || clkgen % 2 != 0);
110 1.16 pooka }
111 1.16 pooka
112 1.16 pooka void
113 1.16 pooka rump_gettime(struct timespec *ts)
114 1.16 pooka {
115 1.16 pooka struct timespec ts_up;
116 1.14 pooka
117 1.16 pooka rump_getuptime(&ts_up);
118 1.16 pooka timespecadd(&clockbase, &ts_up, ts);
119 1.14 pooka }
120 1.14 pooka
121 1.10 pooka /*
122 1.10 pooka * clock "interrupt"
123 1.10 pooka */
124 1.10 pooka static void
125 1.10 pooka doclock(void *noarg)
126 1.10 pooka {
127 1.16 pooka struct timespec tick, curtime;
128 1.15 pooka uint64_t sec, nsec;
129 1.16 pooka int ticks = 0, error;
130 1.10 pooka extern int hz;
131 1.14 pooka
132 1.15 pooka rumpuser_gettime(&sec, &nsec, &error);
133 1.16 pooka clockbase.tv_sec = sec;
134 1.16 pooka clockbase.tv_nsec = nsec;
135 1.16 pooka curtime = clockbase;
136 1.14 pooka tick.tv_sec = 0;
137 1.14 pooka tick.tv_nsec = 1000000000/hz;
138 1.14 pooka
139 1.14 pooka rumpuser_mutex_enter(clockmtx);
140 1.14 pooka rumpuser_cv_signal(clockcv);
141 1.10 pooka
142 1.10 pooka for (;;) {
143 1.5 pooka callout_hardclock();
144 1.5 pooka
145 1.10 pooka if (++ticks == hz) {
146 1.8 pooka time_uptime++;
147 1.8 pooka ticks = 0;
148 1.8 pooka }
149 1.14 pooka
150 1.16 pooka /* wait until the next tick. XXX: what if the clock changes? */
151 1.14 pooka while (rumpuser_cv_timedwait(clockcv, clockmtx,
152 1.16 pooka &curtime) != EWOULDBLOCK)
153 1.14 pooka continue;
154 1.16 pooka
155 1.16 pooka clkgen++;
156 1.16 pooka timespecadd(&clockup, &tick, &clockup);
157 1.16 pooka clkgen++;
158 1.16 pooka timespecadd(&clockup, &clockbase, &curtime);
159 1.10 pooka }
160 1.10 pooka }
161 1.8 pooka
162 1.10 pooka /*
163 1.10 pooka * run a scheduled soft interrupt
164 1.10 pooka */
165 1.10 pooka static void
166 1.10 pooka sithread(void *arg)
167 1.10 pooka {
168 1.10 pooka struct softint *si;
169 1.10 pooka void (*func)(void *) = NULL;
170 1.10 pooka void *funarg;
171 1.10 pooka bool mpsafe;
172 1.10 pooka
173 1.10 pooka mutex_enter(&si_mtx);
174 1.10 pooka for (;;) {
175 1.10 pooka if (!LIST_EMPTY(&si_pending)) {
176 1.5 pooka si = LIST_FIRST(&si_pending);
177 1.5 pooka func = si->si_func;
178 1.5 pooka funarg = si->si_arg;
179 1.18 pooka mpsafe = si->si_flags & SI_MPSAFE;
180 1.5 pooka
181 1.18 pooka si->si_flags &= ~SI_ONLIST;
182 1.5 pooka LIST_REMOVE(si, si_entries);
183 1.18 pooka if (si->si_flags & SI_KILLME)
184 1.18 pooka softint_disestablish(si);
185 1.10 pooka } else {
186 1.10 pooka cv_wait(&si_cv, &si_mtx);
187 1.10 pooka continue;
188 1.5 pooka }
189 1.10 pooka wrkidle--;
190 1.10 pooka if (__predict_false(wrkidle == 0))
191 1.10 pooka makeworker(false);
192 1.5 pooka mutex_exit(&si_mtx);
193 1.5 pooka
194 1.10 pooka if (!mpsafe)
195 1.10 pooka KERNEL_LOCK(1, curlwp);
196 1.10 pooka func(funarg);
197 1.10 pooka if (!mpsafe)
198 1.10 pooka KERNEL_UNLOCK_ONE(curlwp);
199 1.10 pooka
200 1.10 pooka mutex_enter(&si_mtx);
201 1.10 pooka wrkidle++;
202 1.5 pooka }
203 1.5 pooka }
204 1.2 ad
205 1.5 pooka void
206 1.5 pooka softint_init(struct cpu_info *ci)
207 1.2 ad {
208 1.5 pooka int rv;
209 1.5 pooka
210 1.5 pooka mutex_init(&si_mtx, MUTEX_DEFAULT, IPL_NONE);
211 1.10 pooka cv_init(&si_cv, "intrw8"); /* cv of temporary w8ness */
212 1.2 ad
213 1.14 pooka rumpuser_cv_init(&clockcv);
214 1.14 pooka rumpuser_mutex_init(&clockmtx);
215 1.14 pooka
216 1.5 pooka /* XXX: should have separate "wanttimer" control */
217 1.5 pooka if (rump_threads) {
218 1.14 pooka rumpuser_mutex_enter(clockmtx);
219 1.13 pooka rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, doclock,
220 1.10 pooka NULL, NULL, "rumpclk");
221 1.5 pooka if (rv)
222 1.10 pooka panic("clock thread creation failed: %d", rv);
223 1.10 pooka mutex_enter(&si_mtx);
224 1.10 pooka while (wrktotal < INTRTHREAD_DEFAULT) {
225 1.10 pooka makeworker(true);
226 1.10 pooka }
227 1.10 pooka mutex_exit(&si_mtx);
228 1.14 pooka
229 1.14 pooka /* make sure we have a clocktime before returning */
230 1.14 pooka rumpuser_cv_wait(clockcv, clockmtx);
231 1.14 pooka rumpuser_mutex_exit(clockmtx);
232 1.2 ad }
233 1.2 ad }
234 1.2 ad
235 1.5 pooka /*
236 1.5 pooka * Soft interrupts bring two choices. If we are running with thread
237 1.5 pooka * support enabled, defer execution, otherwise execute in place.
238 1.5 pooka * See softint_schedule().
239 1.5 pooka *
240 1.5 pooka * As there is currently no clear concept of when a thread finishes
241 1.5 pooka * work (although rump_clear_curlwp() is close), simply execute all
242 1.5 pooka * softints in the timer thread. This is probably not the most
243 1.5 pooka * efficient method, but good enough for now.
244 1.5 pooka */
245 1.5 pooka void *
246 1.5 pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
247 1.2 ad {
248 1.5 pooka struct softint *si;
249 1.2 ad
250 1.5 pooka si = kmem_alloc(sizeof(*si), KM_SLEEP);
251 1.5 pooka si->si_func = func;
252 1.5 pooka si->si_arg = arg;
253 1.18 pooka si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
254 1.5 pooka
255 1.5 pooka return si;
256 1.2 ad }
257 1.2 ad
258 1.2 ad void
259 1.2 ad softint_schedule(void *arg)
260 1.2 ad {
261 1.5 pooka struct softint *si = arg;
262 1.2 ad
263 1.5 pooka if (!rump_threads) {
264 1.5 pooka si->si_func(si->si_arg);
265 1.5 pooka } else {
266 1.5 pooka mutex_enter(&si_mtx);
267 1.18 pooka if (!(si->si_flags & SI_ONLIST)) {
268 1.5 pooka LIST_INSERT_HEAD(&si_pending, si, si_entries);
269 1.18 pooka si->si_flags |= SI_ONLIST;
270 1.5 pooka }
271 1.5 pooka cv_signal(&si_cv);
272 1.5 pooka mutex_exit(&si_mtx);
273 1.5 pooka }
274 1.2 ad }
275 1.2 ad
276 1.18 pooka /* flimsy disestablish: should wait for softints to finish */
277 1.18 pooka void
278 1.18 pooka softint_disestablish(void *cook)
279 1.18 pooka {
280 1.18 pooka struct softint *si = cook;
281 1.18 pooka
282 1.18 pooka if (si->si_flags & SI_ONLIST) {
283 1.18 pooka si->si_flags |= SI_KILLME;
284 1.18 pooka return;
285 1.18 pooka }
286 1.18 pooka kmem_free(si, sizeof(*si));
287 1.18 pooka }
288 1.18 pooka
289 1.2 ad bool
290 1.9 christos cpu_intr_p(void)
291 1.2 ad {
292 1.2 ad
293 1.2 ad return false;
294 1.2 ad }
295 1.19 pooka
296 1.19 pooka /* yea, we lie a bit for now */
297 1.19 pooka bool
298 1.19 pooka cpu_softintr_p(void)
299 1.19 pooka {
300 1.19 pooka
301 1.19 pooka return false;
302 1.19 pooka }
303