intr.c revision 1.46 1 /* $NetBSD: intr.c,v 1.46 2014/06/22 20:09:19 pooka Exp $ */
2
3 /*
4 * Copyright (c) 2008-2010 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.46 2014/06/22 20:09:19 pooka 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 thetick, curclock;
103 int64_t sec;
104 long nsec;
105 int error;
106 int cpuindx = curcpu()->ci_index;
107 extern int hz;
108
109 error = rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, &sec, &nsec);
110 if (error)
111 panic("clock: cannot get monotonic time");
112
113 curclock.tv_sec = sec;
114 curclock.tv_nsec = nsec;
115 thetick.tv_sec = 0;
116 thetick.tv_nsec = 1000000000/hz;
117
118 for (;;) {
119 callout_hardclock();
120
121 error = rumpuser_clock_sleep(RUMPUSER_CLOCK_ABSMONO,
122 curclock.tv_sec, curclock.tv_nsec);
123 KASSERT(!error);
124 timespecadd(&curclock, &thetick, &curclock);
125
126 if (cpuindx != 0)
127 continue;
128
129 if ((++ticks % hz) == 0) {
130 cv_broadcast(&lbolt);
131 }
132 tc_ticktock();
133 }
134 }
135
136 /*
137 * Soft interrupt execution thread. This thread is pinned to the
138 * same CPU that scheduled the interrupt, so we don't need to do
139 * lock against si_lvl.
140 */
141 static void
142 sithread(void *arg)
143 {
144 struct softint_percpu *sip;
145 struct softint *si;
146 void (*func)(void *) = NULL;
147 void *funarg;
148 bool mpsafe;
149 int mylevel = (uintptr_t)arg;
150 struct softint_lev *si_lvlp, *si_lvl;
151 struct cpu_data *cd = &curcpu()->ci_data;
152
153 si_lvlp = cd->cpu_softcpu;
154 si_lvl = &si_lvlp[mylevel];
155
156 for (;;) {
157 if (!LIST_EMPTY(&si_lvl->si_pending)) {
158 sip = LIST_FIRST(&si_lvl->si_pending);
159 si = sip->sip_parent;
160
161 func = si->si_func;
162 funarg = si->si_arg;
163 mpsafe = si->si_flags & SI_MPSAFE;
164
165 sip->sip_onlist = false;
166 LIST_REMOVE(sip, sip_entries);
167 if (si->si_flags & SI_KILLME) {
168 softint_disestablish(si);
169 continue;
170 }
171 } else {
172 rump_schedlock_cv_wait(si_lvl->si_cv);
173 continue;
174 }
175
176 if (!mpsafe)
177 KERNEL_LOCK(1, curlwp);
178 func(funarg);
179 if (!mpsafe)
180 KERNEL_UNLOCK_ONE(curlwp);
181 }
182
183 panic("sithread unreachable");
184 }
185
186 static kmutex_t sithr_emtx;
187 static unsigned int sithr_est;
188 static int sithr_canest;
189
190 /*
191 * Create softint handler threads when the softint for each respective
192 * level is established for the first time. Most rump kernels don't
193 * need at least half of the softint levels, so on-demand saves bootstrap
194 * time and memory resources. Note, though, that this routine may be
195 * called before it's possible to call kthread_create(). Creation of
196 * those softints (SOFTINT_CLOCK, as of writing this) will be deferred
197 * to until softint_init() is called for the main CPU.
198 */
199 static void
200 sithread_establish(int level)
201 {
202 int docreate, rv;
203 int lvlbit = 1<<level;
204 int i;
205
206 KASSERT((level & ~SOFTINT_LVLMASK) == 0);
207 if (__predict_true(sithr_est & lvlbit))
208 return;
209
210 mutex_enter(&sithr_emtx);
211 docreate = (sithr_est & lvlbit) == 0 && sithr_canest;
212 sithr_est |= lvlbit;
213 mutex_exit(&sithr_emtx);
214
215 if (docreate) {
216 for (i = 0; i < ncpu_final; i++) {
217 if ((rv = kthread_create(PRI_NONE,
218 KTHREAD_MPSAFE | KTHREAD_INTR,
219 cpu_lookup(i), sithread, (void *)(uintptr_t)level,
220 NULL, "rsi%d/%d", i, level)) != 0)
221 panic("softint thread create failed: %d", rv);
222 }
223 }
224 }
225
226 void
227 rump_intr_init(int numcpu)
228 {
229
230 cv_init(&lbolt, "oh kath ra");
231 mutex_init(&sithr_emtx, MUTEX_DEFAULT, IPL_NONE);
232 ncpu_final = numcpu;
233 }
234
235 void
236 softint_init(struct cpu_info *ci)
237 {
238 struct cpu_data *cd = &ci->ci_data;
239 struct softint_lev *slev;
240 int rv, i;
241
242 if (!rump_threads)
243 return;
244
245 slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
246 for (i = 0; i < SOFTINT_COUNT; i++) {
247 rumpuser_cv_init(&slev[i].si_cv);
248 LIST_INIT(&slev[i].si_pending);
249 }
250 cd->cpu_softcpu = slev;
251
252 /* overloaded global init ... */
253 /* XXX: should be done the last time we are called */
254 if (ci->ci_index == 0) {
255 int sithr_swap;
256
257 rumptc.tc_frequency = hz;
258 tc_init(&rumptc);
259
260 /* create deferred softint threads */
261 mutex_enter(&sithr_emtx);
262 sithr_swap = sithr_est;
263 sithr_est = 0;
264 sithr_canest = 1;
265 mutex_exit(&sithr_emtx);
266 for (i = 0; i < SOFTINT_COUNT; i++) {
267 if (sithr_swap & (1<<i))
268 sithread_establish(i);
269 }
270 }
271
272 /* well, not really a "soft" interrupt ... */
273 if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
274 ci, doclock, NULL, NULL, "rumpclk%d", ci->ci_index)) != 0)
275 panic("clock thread creation failed: %d", rv);
276 }
277
278 void *
279 softint_establish(u_int flags, void (*func)(void *), void *arg)
280 {
281 struct softint *si;
282 struct softint_percpu *sip;
283 int level = flags & SOFTINT_LVLMASK;
284 int i;
285
286 si = malloc(sizeof(*si), M_TEMP, M_WAITOK);
287 si->si_func = func;
288 si->si_arg = arg;
289 si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
290 si->si_level = level;
291 KASSERT(si->si_level < SOFTINT_COUNT);
292 si->si_entry = malloc(sizeof(*si->si_entry) * ncpu_final,
293 M_TEMP, M_WAITOK | M_ZERO);
294 for (i = 0; i < ncpu_final; i++) {
295 sip = &si->si_entry[i];
296 sip->sip_parent = si;
297 }
298 sithread_establish(level);
299
300 return si;
301 }
302
303 /*
304 * Soft interrupts bring two choices. If we are running with thread
305 * support enabled, defer execution, otherwise execute in place.
306 */
307
308 void
309 softint_schedule(void *arg)
310 {
311 struct softint *si = arg;
312 struct cpu_info *ci = curcpu();
313 struct softint_percpu *sip = &si->si_entry[ci->ci_index];
314 struct cpu_data *cd = &ci->ci_data;
315 struct softint_lev *si_lvl = cd->cpu_softcpu;
316
317 if (!rump_threads) {
318 si->si_func(si->si_arg);
319 } else {
320 if (!sip->sip_onlist) {
321 LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending,
322 sip, sip_entries);
323 sip->sip_onlist = true;
324 }
325 }
326 }
327
328 void
329 softint_schedule_cpu(void *arg, struct cpu_info *ci)
330 {
331 /*
332 * TODO: implement this properly
333 */
334 KASSERT(curcpu() == ci);
335 softint_schedule(arg);
336 }
337
338 /*
339 * flimsy disestablish: should wait for softints to finish.
340 */
341 void
342 softint_disestablish(void *cook)
343 {
344 struct softint *si = cook;
345 int i;
346
347 for (i = 0; i < ncpu_final; i++) {
348 struct softint_percpu *sip;
349
350 sip = &si->si_entry[i];
351 if (sip->sip_onlist) {
352 si->si_flags |= SI_KILLME;
353 return;
354 }
355 }
356 free(si->si_entry, M_TEMP);
357 free(si, M_TEMP);
358 }
359
360 void
361 rump_softint_run(struct cpu_info *ci)
362 {
363 struct cpu_data *cd = &ci->ci_data;
364 struct softint_lev *si_lvl = cd->cpu_softcpu;
365 int i;
366
367 if (!rump_threads)
368 return;
369
370 for (i = 0; i < SOFTINT_COUNT; i++) {
371 if (!LIST_EMPTY(&si_lvl[i].si_pending))
372 rumpuser_cv_signal(si_lvl[i].si_cv);
373 }
374 }
375
376 bool
377 cpu_intr_p(void)
378 {
379
380 return false;
381 }
382
383 bool
384 cpu_softintr_p(void)
385 {
386
387 return curlwp->l_pflag & LP_INTR;
388 }
389