intr.c revision 1.43
1/* $NetBSD: intr.c,v 1.43 2014/06/05 23:43:04 rmind 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.43 2014/06/05 23:43:04 rmind 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 52struct softint_percpu; 53struct 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 62struct softint_percpu { 63 struct softint *sip_parent; 64 bool sip_onlist; 65 66 LIST_ENTRY(softint_percpu) sip_entries; 67}; 68 69struct softint_lev { 70 struct rumpuser_cv *si_cv; 71 LIST_HEAD(, softint_percpu) si_pending; 72}; 73 74kcondvar_t lbolt; /* Oh Kath Ra */ 75 76static u_int ticks; 77static int ncpu_final; 78 79static u_int 80rumptc_get(struct timecounter *tc) 81{ 82 83 KASSERT(rump_threads); 84 return ticks; 85} 86 87static 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 */ 99static void 100doclock(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 */ 141static void 142sithread(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 186static kmutex_t sithr_emtx; 187static unsigned int sithr_est; 188static 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 */ 199static void 200sithread_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 226void 227rump_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 235void 236softint_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 /* overloaded global init ... */ 246 if (ci->ci_index == 0) { 247 int sithr_swap; 248 249 rumptc.tc_frequency = hz; 250 tc_init(&rumptc); 251 252 /* create deferred softint threads */ 253 mutex_enter(&sithr_emtx); 254 sithr_swap = sithr_est; 255 sithr_est = 0; 256 sithr_canest = 1; 257 mutex_exit(&sithr_emtx); 258 for (i = 0; i < SOFTINT_COUNT; i++) { 259 if (sithr_swap & (1<<i)) 260 sithread_establish(i); 261 } 262 } 263 264 slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP); 265 for (i = 0; i < SOFTINT_COUNT; i++) { 266 rumpuser_cv_init(&slev[i].si_cv); 267 LIST_INIT(&slev[i].si_pending); 268 } 269 cd->cpu_softcpu = slev; 270 271 /* well, not really a "soft" interrupt ... */ 272 if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, 273 ci, doclock, NULL, NULL, "rumpclk%d", ci->ci_index)) != 0) 274 panic("clock thread creation failed: %d", rv); 275} 276 277void * 278softint_establish(u_int flags, void (*func)(void *), void *arg) 279{ 280 struct softint *si; 281 struct softint_percpu *sip; 282 int level = flags & SOFTINT_LVLMASK; 283 int i; 284 285 si = malloc(sizeof(*si), M_TEMP, M_WAITOK); 286 si->si_func = func; 287 si->si_arg = arg; 288 si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0; 289 si->si_level = level; 290 KASSERT(si->si_level < SOFTINT_COUNT); 291 si->si_entry = malloc(sizeof(*si->si_entry) * ncpu_final, 292 M_TEMP, M_WAITOK | M_ZERO); 293 for (i = 0; i < ncpu_final; i++) { 294 sip = &si->si_entry[i]; 295 sip->sip_parent = si; 296 } 297 sithread_establish(level); 298 299 return si; 300} 301 302/* 303 * Soft interrupts bring two choices. If we are running with thread 304 * support enabled, defer execution, otherwise execute in place. 305 */ 306 307void 308softint_schedule(void *arg) 309{ 310 struct softint *si = arg; 311 struct cpu_info *ci = curcpu(); 312 struct softint_percpu *sip = &si->si_entry[ci->ci_index]; 313 struct cpu_data *cd = &ci->ci_data; 314 struct softint_lev *si_lvl = cd->cpu_softcpu; 315 316 if (!rump_threads) { 317 si->si_func(si->si_arg); 318 } else { 319 if (!sip->sip_onlist) { 320 LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending, 321 sip, sip_entries); 322 sip->sip_onlist = true; 323 } 324 } 325} 326 327void 328softint_schedule_cpu(void *arg, struct cpu_info *ci) 329{ 330 /* TODO: implement this properly */ 331 KASSERT(curcpu() == ci); 332 softint_schedule(arg); 333} 334 335/* 336 * flimsy disestablish: should wait for softints to finish. 337 */ 338void 339softint_disestablish(void *cook) 340{ 341 struct softint *si = cook; 342 int i; 343 344 for (i = 0; i < ncpu_final; i++) { 345 struct softint_percpu *sip; 346 347 sip = &si->si_entry[i]; 348 if (sip->sip_onlist) { 349 si->si_flags |= SI_KILLME; 350 return; 351 } 352 } 353 free(si->si_entry, M_TEMP); 354 free(si, M_TEMP); 355} 356 357void 358rump_softint_run(struct cpu_info *ci) 359{ 360 struct cpu_data *cd = &ci->ci_data; 361 struct softint_lev *si_lvl = cd->cpu_softcpu; 362 int i; 363 364 if (!rump_threads) 365 return; 366 367 for (i = 0; i < SOFTINT_COUNT; i++) { 368 if (!LIST_EMPTY(&si_lvl[i].si_pending)) 369 rumpuser_cv_signal(si_lvl[i].si_cv); 370 } 371} 372 373bool 374cpu_intr_p(void) 375{ 376 377 return false; 378} 379 380bool 381cpu_softintr_p(void) 382{ 383 384 return curlwp->l_pflag & LP_INTR; 385} 386