1 /* $NetBSD: intr.c,v 1.23 2009/12/05 22:44:08 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.23 2009/12/05 22:44:08 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 #define SI_MPSAFE 0x01 48 #define SI_ONLIST 0x02 49 #define SI_KILLME 0x04 50 51 struct softint { 52 void (*si_func)(void *); 53 void *si_arg; 54 int si_flags; 55 int si_level; 56 57 LIST_ENTRY(softint) si_entries; 58 }; 59 60 static struct rumpuser_mtx *si_mtx; 61 struct softint_lev { 62 struct rumpuser_cv *si_cv; 63 LIST_HEAD(, softint) si_pending; 64 }; 65 66 /* rumpuser structures since we call rumpuser interfaces directly */ 67 static struct rumpuser_cv *clockcv; 68 static struct rumpuser_mtx *clockmtx; 69 static struct timespec clockbase, clockup; 70 static unsigned clkgen; 71 72 kcondvar_t lbolt; /* Oh Kath Ra */ 73 74 void 75 rump_getuptime(struct timespec *ts) 76 { 77 int startgen, i = 0; 78 79 do { 80 startgen = clkgen; 81 if (__predict_false(i++ > 10)) { 82 yield(); 83 i = 0; 84 } 85 *ts = clockup; 86 } while (startgen != clkgen || clkgen % 2 != 0); 87 } 88 89 void 90 rump_gettime(struct timespec *ts) 91 { 92 struct timespec ts_up; 93 94 rump_getuptime(&ts_up); 95 timespecadd(&clockbase, &ts_up, ts); 96 } 97 98 /* 99 * clock "interrupt" 100 */ 101 static void 102 doclock(void *noarg) 103 { 104 struct timespec tick, curtime; 105 uint64_t sec, nsec; 106 int ticks = 0, error; 107 extern int hz; 108 109 rumpuser_gettime(&sec, &nsec, &error); 110 clockbase.tv_sec = sec; 111 clockbase.tv_nsec = nsec; 112 curtime = clockbase; 113 tick.tv_sec = 0; 114 tick.tv_nsec = 1000000000/hz; 115 116 rumpuser_mutex_enter(clockmtx); 117 rumpuser_cv_signal(clockcv); 118 119 for (;;) { 120 callout_hardclock(); 121 122 /* wait until the next tick. XXX: what if the clock changes? */ 123 while (rumpuser_cv_timedwait(clockcv, clockmtx, 124 curtime.tv_sec, curtime.tv_nsec) == 0) 125 continue; 126 127 /* if !maincpu: continue */ 128 129 if (++ticks == hz) { 130 time_uptime++; 131 ticks = 0; 132 cv_broadcast(&lbolt); 133 } 134 135 clkgen++; 136 timespecadd(&clockup, &tick, &clockup); 137 clkgen++; 138 timespecadd(&clockup, &clockbase, &curtime); 139 } 140 } 141 142 /* 143 * Soft interrupt execution thread. Note that we run without a CPU 144 * context until we start processing the interrupt. This is to avoid 145 * lock recursion. 146 */ 147 static void 148 sithread(void *arg) 149 { 150 struct softint *si; 151 void (*func)(void *) = NULL; 152 void *funarg; 153 bool mpsafe; 154 int mylevel = (uintptr_t)arg; 155 struct softint_lev *si_lvlp, *si_lvl; 156 struct cpu_data *cd = &curcpu()->ci_data; 157 158 rump_unschedule(); 159 160 si_lvlp = cd->cpu_softcpu; 161 si_lvl = &si_lvlp[mylevel]; 162 163 /* 164 * XXX: si_mtx is unnecessary, and should open an interface 165 * which allows to use schedmtx for the cv wait 166 */ 167 rumpuser_mutex_enter_nowrap(si_mtx); 168 for (;;) { 169 if (!LIST_EMPTY(&si_lvl->si_pending)) { 170 si = LIST_FIRST(&si_lvl->si_pending); 171 func = si->si_func; 172 funarg = si->si_arg; 173 mpsafe = si->si_flags & SI_MPSAFE; 174 175 si->si_flags &= ~SI_ONLIST; 176 LIST_REMOVE(si, si_entries); 177 if (si->si_flags & SI_KILLME) { 178 rumpuser_mutex_exit(si_mtx); 179 rump_schedule(); 180 softint_disestablish(si); 181 rump_unschedule(); 182 rumpuser_mutex_enter_nowrap(si_mtx); 183 continue; 184 } 185 } else { 186 rumpuser_cv_wait_nowrap(si_lvl->si_cv, si_mtx); 187 continue; 188 } 189 rumpuser_mutex_exit(si_mtx); 190 191 rump_schedule(); 192 if (!mpsafe) 193 KERNEL_LOCK(1, curlwp); 194 func(funarg); 195 if (!mpsafe) 196 KERNEL_UNLOCK_ONE(curlwp); 197 rump_unschedule(); 198 199 rumpuser_mutex_enter_nowrap(si_mtx); 200 } 201 202 panic("sithread unreachable"); 203 } 204 205 void 206 rump_intr_init() 207 { 208 209 rumpuser_mutex_init(&si_mtx); 210 rumpuser_cv_init(&clockcv); 211 rumpuser_mutex_init(&clockmtx); 212 cv_init(&lbolt, "oh kath ra"); 213 } 214 215 void 216 softint_init(struct cpu_info *ci) 217 { 218 struct cpu_data *cd = &ci->ci_data; 219 struct softint_lev *slev; 220 int rv, i; 221 222 if (!rump_threads) 223 return; 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 for (i = 0; i < SOFTINT_COUNT; i++) { 233 rv = kthread_create(PRI_NONE, 234 KTHREAD_MPSAFE | KTHREAD_INTR, NULL, 235 sithread, (void *)(uintptr_t)i, 236 NULL, "rumpsi%d", i); 237 } 238 239 rumpuser_mutex_enter(clockmtx); 240 for (i = 0; i < ncpu; i++) { 241 rv = kthread_create(PRI_NONE, 242 KTHREAD_MPSAFE | KTHREAD_INTR, 243 cpu_lookup(i), doclock, NULL, NULL, 244 "rumpclk%d", i); 245 if (rv) 246 panic("clock thread creation failed: %d", rv); 247 } 248 249 /* 250 * Make sure we have a clocktime before returning. 251 * XXX: mp 252 */ 253 rumpuser_cv_wait(clockcv, clockmtx); 254 rumpuser_mutex_exit(clockmtx); 255 } 256 257 /* 258 * Soft interrupts bring two choices. If we are running with thread 259 * support enabled, defer execution, otherwise execute in place. 260 * See softint_schedule(). 261 * 262 * As there is currently no clear concept of when a thread finishes 263 * work (although rump_clear_curlwp() is close), simply execute all 264 * softints in the timer thread. This is probably not the most 265 * efficient method, but good enough for now. 266 */ 267 void * 268 softint_establish(u_int flags, void (*func)(void *), void *arg) 269 { 270 struct softint *si; 271 272 si = kmem_alloc(sizeof(*si), KM_SLEEP); 273 si->si_func = func; 274 si->si_arg = arg; 275 si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0; 276 si->si_level = flags & SOFTINT_LVLMASK; 277 KASSERT(si->si_level < SOFTINT_COUNT); 278 279 return si; 280 } 281 282 void 283 softint_schedule(void *arg) 284 { 285 struct softint *si = arg; 286 struct cpu_data *cd = &curcpu()->ci_data; 287 struct softint_lev *si_lvl = cd->cpu_softcpu; 288 289 if (!rump_threads) { 290 si->si_func(si->si_arg); 291 } else { 292 if (!(si->si_flags & SI_ONLIST)) { 293 LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending, 294 si, si_entries); 295 si->si_flags |= SI_ONLIST; 296 } 297 } 298 } 299 300 /* flimsy disestablish: should wait for softints to finish */ 301 void 302 softint_disestablish(void *cook) 303 { 304 struct softint *si = cook; 305 306 rumpuser_mutex_enter(si_mtx); 307 if (si->si_flags & SI_ONLIST) { 308 si->si_flags |= SI_KILLME; 309 return; 310 } 311 rumpuser_mutex_exit(si_mtx); 312 kmem_free(si, sizeof(*si)); 313 } 314 315 void 316 rump_softint_run(struct cpu_info *ci) 317 { 318 struct cpu_data *cd = &ci->ci_data; 319 struct softint_lev *si_lvl = cd->cpu_softcpu; 320 int i; 321 322 if (!rump_threads) 323 return; 324 325 for (i = 0; i < SOFTINT_COUNT; i++) { 326 if (!LIST_EMPTY(&si_lvl[i].si_pending)) 327 rumpuser_cv_signal(si_lvl[i].si_cv); 328 } 329 } 330 331 bool 332 cpu_intr_p(void) 333 { 334 335 return false; 336 } 337 338 bool 339 cpu_softintr_p(void) 340 { 341 342 return curlwp->l_pflag & LP_INTR; 343 } 344