intr.c revision 1.8.2.2 1 1.8.2.2 skrll /* $NetBSD: intr.c,v 1.8.2.2 2009/03/03 18:34:07 skrll 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.8.2.1 skrll #include <sys/cdefs.h>
29 1.8.2.2 skrll __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.8.2.2 2009/03/03 18:34:07 skrll Exp $");
30 1.8.2.1 skrll
31 1.2 ad #include <sys/param.h>
32 1.5 pooka #include <sys/cpu.h>
33 1.8.2.1 skrll #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.8.2.2 skrll time_t time_uptime = 0;
46 1.8 pooka
47 1.5 pooka struct softint {
48 1.5 pooka void (*si_func)(void *);
49 1.5 pooka void *si_arg;
50 1.5 pooka bool si_onlist;
51 1.5 pooka bool si_mpsafe;
52 1.5 pooka
53 1.5 pooka LIST_ENTRY(softint) si_entries;
54 1.2 ad };
55 1.5 pooka static LIST_HEAD(, softint) si_pending = LIST_HEAD_INITIALIZER(si_pending);
56 1.5 pooka static kmutex_t si_mtx;
57 1.5 pooka static kcondvar_t si_cv;
58 1.5 pooka
59 1.8.2.1 skrll #define INTRTHREAD_DEFAULT 2
60 1.8.2.1 skrll #define INTRTHREAD_MAX 20
61 1.8.2.1 skrll static int wrkidle, wrktotal;
62 1.8.2.1 skrll
63 1.8.2.1 skrll static void sithread(void *);
64 1.8.2.1 skrll
65 1.5 pooka static void
66 1.8.2.1 skrll makeworker(bool bootstrap)
67 1.5 pooka {
68 1.8.2.1 skrll int rv;
69 1.8.2.1 skrll
70 1.8.2.1 skrll if (wrktotal > INTRTHREAD_MAX) {
71 1.8.2.1 skrll /* XXX: ratecheck */
72 1.8.2.1 skrll printf("maximum interrupt threads (%d) reached\n",
73 1.8.2.1 skrll INTRTHREAD_MAX);
74 1.8.2.1 skrll return;
75 1.8.2.1 skrll }
76 1.8.2.2 skrll rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_INTR, NULL,
77 1.8.2.2 skrll sithread, NULL, NULL, "rumpsi");
78 1.8.2.1 skrll if (rv) {
79 1.8.2.1 skrll if (bootstrap)
80 1.8.2.1 skrll panic("intr thread creation failed %d", rv);
81 1.8.2.1 skrll else
82 1.8.2.1 skrll printf("intr thread creation failed %d\n", rv);
83 1.8.2.1 skrll } else {
84 1.8.2.1 skrll wrkidle++;
85 1.8.2.1 skrll wrktotal++;
86 1.8.2.1 skrll }
87 1.8.2.1 skrll }
88 1.8.2.1 skrll
89 1.8.2.2 skrll /* rumpuser structures since we call rumpuser interfaces directly */
90 1.8.2.2 skrll static struct rumpuser_cv *clockcv;
91 1.8.2.2 skrll static struct rumpuser_mtx *clockmtx;
92 1.8.2.2 skrll static struct timespec rump_clock;
93 1.8.2.2 skrll
94 1.8.2.2 skrll void
95 1.8.2.2 skrll rump_gettime(struct timespec *ts)
96 1.8.2.2 skrll {
97 1.8.2.2 skrll struct timespec attempt;
98 1.8.2.2 skrll
99 1.8.2.2 skrll /* XXX: this is completely bogus */
100 1.8.2.2 skrll do {
101 1.8.2.2 skrll attempt = rump_clock;
102 1.8.2.2 skrll } while (memcmp(&attempt, &rump_clock, sizeof(struct timespec)) != 0);
103 1.8.2.2 skrll
104 1.8.2.2 skrll *ts = attempt;
105 1.8.2.2 skrll }
106 1.8.2.2 skrll
107 1.8.2.1 skrll /*
108 1.8.2.1 skrll * clock "interrupt"
109 1.8.2.1 skrll */
110 1.8.2.1 skrll static void
111 1.8.2.1 skrll doclock(void *noarg)
112 1.8.2.1 skrll {
113 1.8.2.2 skrll struct timespec tick;
114 1.8.2.2 skrll uint64_t sec, nsec;
115 1.8.2.1 skrll static int ticks = 0;
116 1.8.2.1 skrll extern int hz;
117 1.8.2.2 skrll int error;
118 1.8.2.2 skrll
119 1.8.2.2 skrll rumpuser_gettime(&sec, &nsec, &error);
120 1.8.2.2 skrll rump_clock.tv_sec = sec;
121 1.8.2.2 skrll rump_clock.tv_nsec = nsec;
122 1.8.2.2 skrll tick.tv_sec = 0;
123 1.8.2.2 skrll tick.tv_nsec = 1000000000/hz;
124 1.8.2.2 skrll
125 1.8.2.2 skrll rumpuser_mutex_enter(clockmtx);
126 1.8.2.2 skrll rumpuser_cv_signal(clockcv);
127 1.8.2.1 skrll
128 1.8.2.1 skrll for (;;) {
129 1.5 pooka callout_hardclock();
130 1.5 pooka
131 1.8.2.1 skrll if (++ticks == hz) {
132 1.8 pooka time_uptime++;
133 1.8 pooka ticks = 0;
134 1.8 pooka }
135 1.8.2.2 skrll
136 1.8.2.2 skrll /* wait until the next tick */
137 1.8.2.2 skrll while (rumpuser_cv_timedwait(clockcv, clockmtx,
138 1.8.2.2 skrll &rump_clock) != EWOULDBLOCK)
139 1.8.2.2 skrll continue;
140 1.8.2.2 skrll timespecadd(&rump_clock, &tick, &rump_clock);
141 1.8.2.1 skrll }
142 1.8.2.1 skrll }
143 1.8 pooka
144 1.8.2.1 skrll /*
145 1.8.2.1 skrll * run a scheduled soft interrupt
146 1.8.2.1 skrll */
147 1.8.2.1 skrll static void
148 1.8.2.1 skrll sithread(void *arg)
149 1.8.2.1 skrll {
150 1.8.2.1 skrll struct softint *si;
151 1.8.2.1 skrll void (*func)(void *) = NULL;
152 1.8.2.1 skrll void *funarg;
153 1.8.2.1 skrll bool mpsafe;
154 1.8.2.1 skrll
155 1.8.2.1 skrll mutex_enter(&si_mtx);
156 1.8.2.1 skrll for (;;) {
157 1.8.2.1 skrll if (!LIST_EMPTY(&si_pending)) {
158 1.5 pooka si = LIST_FIRST(&si_pending);
159 1.5 pooka func = si->si_func;
160 1.5 pooka funarg = si->si_arg;
161 1.5 pooka mpsafe = si->si_mpsafe;
162 1.5 pooka
163 1.5 pooka si->si_onlist = false;
164 1.5 pooka LIST_REMOVE(si, si_entries);
165 1.8.2.1 skrll } else {
166 1.8.2.1 skrll cv_wait(&si_cv, &si_mtx);
167 1.8.2.1 skrll continue;
168 1.5 pooka }
169 1.8.2.1 skrll wrkidle--;
170 1.8.2.1 skrll if (__predict_false(wrkidle == 0))
171 1.8.2.1 skrll makeworker(false);
172 1.5 pooka mutex_exit(&si_mtx);
173 1.5 pooka
174 1.8.2.1 skrll if (!mpsafe)
175 1.8.2.1 skrll KERNEL_LOCK(1, curlwp);
176 1.8.2.1 skrll func(funarg);
177 1.8.2.1 skrll if (!mpsafe)
178 1.8.2.1 skrll KERNEL_UNLOCK_ONE(curlwp);
179 1.8.2.1 skrll
180 1.8.2.1 skrll mutex_enter(&si_mtx);
181 1.8.2.1 skrll wrkidle++;
182 1.5 pooka }
183 1.5 pooka }
184 1.2 ad
185 1.5 pooka void
186 1.5 pooka softint_init(struct cpu_info *ci)
187 1.2 ad {
188 1.5 pooka int rv;
189 1.5 pooka
190 1.5 pooka mutex_init(&si_mtx, MUTEX_DEFAULT, IPL_NONE);
191 1.8.2.1 skrll cv_init(&si_cv, "intrw8"); /* cv of temporary w8ness */
192 1.2 ad
193 1.8.2.2 skrll rumpuser_cv_init(&clockcv);
194 1.8.2.2 skrll rumpuser_mutex_init(&clockmtx);
195 1.8.2.2 skrll
196 1.5 pooka /* XXX: should have separate "wanttimer" control */
197 1.5 pooka if (rump_threads) {
198 1.8.2.2 skrll rumpuser_mutex_enter(clockmtx);
199 1.8.2.2 skrll rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, doclock,
200 1.8.2.1 skrll NULL, NULL, "rumpclk");
201 1.5 pooka if (rv)
202 1.8.2.1 skrll panic("clock thread creation failed: %d", rv);
203 1.8.2.1 skrll mutex_enter(&si_mtx);
204 1.8.2.1 skrll while (wrktotal < INTRTHREAD_DEFAULT) {
205 1.8.2.1 skrll makeworker(true);
206 1.8.2.1 skrll }
207 1.8.2.1 skrll mutex_exit(&si_mtx);
208 1.8.2.2 skrll
209 1.8.2.2 skrll /* make sure we have a clocktime before returning */
210 1.8.2.2 skrll rumpuser_cv_wait(clockcv, clockmtx);
211 1.8.2.2 skrll rumpuser_mutex_exit(clockmtx);
212 1.2 ad }
213 1.2 ad }
214 1.2 ad
215 1.5 pooka /*
216 1.5 pooka * Soft interrupts bring two choices. If we are running with thread
217 1.5 pooka * support enabled, defer execution, otherwise execute in place.
218 1.5 pooka * See softint_schedule().
219 1.5 pooka *
220 1.5 pooka * As there is currently no clear concept of when a thread finishes
221 1.5 pooka * work (although rump_clear_curlwp() is close), simply execute all
222 1.5 pooka * softints in the timer thread. This is probably not the most
223 1.5 pooka * efficient method, but good enough for now.
224 1.5 pooka */
225 1.5 pooka void *
226 1.5 pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
227 1.2 ad {
228 1.5 pooka struct softint *si;
229 1.2 ad
230 1.5 pooka si = kmem_alloc(sizeof(*si), KM_SLEEP);
231 1.5 pooka si->si_func = func;
232 1.5 pooka si->si_arg = arg;
233 1.5 pooka si->si_onlist = false;
234 1.5 pooka si->si_mpsafe = flags & SOFTINT_MPSAFE;
235 1.5 pooka
236 1.5 pooka return si;
237 1.2 ad }
238 1.2 ad
239 1.2 ad void
240 1.2 ad softint_schedule(void *arg)
241 1.2 ad {
242 1.5 pooka struct softint *si = arg;
243 1.2 ad
244 1.5 pooka if (!rump_threads) {
245 1.5 pooka si->si_func(si->si_arg);
246 1.5 pooka } else {
247 1.5 pooka mutex_enter(&si_mtx);
248 1.5 pooka if (!si->si_onlist) {
249 1.5 pooka LIST_INSERT_HEAD(&si_pending, si, si_entries);
250 1.5 pooka si->si_onlist = true;
251 1.5 pooka }
252 1.5 pooka cv_signal(&si_cv);
253 1.5 pooka mutex_exit(&si_mtx);
254 1.5 pooka }
255 1.2 ad }
256 1.2 ad
257 1.2 ad bool
258 1.8.2.1 skrll cpu_intr_p(void)
259 1.2 ad {
260 1.2 ad
261 1.2 ad return false;
262 1.2 ad }
263