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