Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.21
      1 /*	$NetBSD: intr.c,v 1.21 2009/11/11 16:46:50 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.21 2009/11/11 16:46:50 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 static struct softint_lev {
     62 	struct rumpuser_cv *si_cv;
     63 	LIST_HEAD(, softint) si_pending;
     64 } softints[SOFTINT_COUNT];
     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 void
     73 rump_getuptime(struct timespec *ts)
     74 {
     75 	int startgen, i = 0;
     76 
     77 	do {
     78 		startgen = clkgen;
     79 		if (__predict_false(i++ > 10)) {
     80 			yield();
     81 			i = 0;
     82 		}
     83 		*ts = clockup;
     84 	} while (startgen != clkgen || clkgen % 2 != 0);
     85 }
     86 
     87 void
     88 rump_gettime(struct timespec *ts)
     89 {
     90 	struct timespec ts_up;
     91 
     92 	rump_getuptime(&ts_up);
     93 	timespecadd(&clockbase, &ts_up, ts);
     94 }
     95 
     96 /*
     97  * clock "interrupt"
     98  */
     99 static void
    100 doclock(void *noarg)
    101 {
    102 	struct timespec tick, curtime;
    103 	uint64_t sec, nsec;
    104 	int ticks = 0, error;
    105 	extern int hz;
    106 
    107 	rumpuser_gettime(&sec, &nsec, &error);
    108 	clockbase.tv_sec = sec;
    109 	clockbase.tv_nsec = nsec;
    110 	curtime = clockbase;
    111 	tick.tv_sec = 0;
    112 	tick.tv_nsec = 1000000000/hz;
    113 
    114 	rumpuser_mutex_enter(clockmtx);
    115 	rumpuser_cv_signal(clockcv);
    116 
    117 	for (;;) {
    118 		callout_hardclock();
    119 
    120 		if (++ticks == hz) {
    121 			time_uptime++;
    122 			ticks = 0;
    123 		}
    124 
    125 		/* wait until the next tick. XXX: what if the clock changes? */
    126 		while (rumpuser_cv_timedwait(clockcv, clockmtx,
    127 		    curtime.tv_sec, curtime.tv_nsec) == 0)
    128 			continue;
    129 
    130 		clkgen++;
    131 		timespecadd(&clockup, &tick, &clockup);
    132 		clkgen++;
    133 		timespecadd(&clockup, &clockbase, &curtime);
    134 	}
    135 }
    136 
    137 /*
    138  * Soft interrupt execution thread.  Note that we run without a CPU
    139  * context until we start processing the interrupt.  This is to avoid
    140  * lock recursion.
    141  */
    142 static void
    143 sithread(void *arg)
    144 {
    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_lvl;
    151 
    152 	rump_unschedule();
    153 
    154 	si_lvl = &softints[mylevel];
    155 	rumpuser_mutex_enter_nowrap(si_mtx);
    156 	for (;;) {
    157 		if (!LIST_EMPTY(&si_lvl->si_pending)) {
    158 			si = LIST_FIRST(&si_lvl->si_pending);
    159 			func = si->si_func;
    160 			funarg = si->si_arg;
    161 			mpsafe = si->si_flags & SI_MPSAFE;
    162 
    163 			si->si_flags &= ~SI_ONLIST;
    164 			LIST_REMOVE(si, si_entries);
    165 			if (si->si_flags & SI_KILLME) {
    166 				rumpuser_mutex_exit(si_mtx);
    167 				rump_schedule();
    168 				softint_disestablish(si);
    169 				rump_unschedule();
    170 				rumpuser_mutex_enter_nowrap(si_mtx);
    171 				continue;
    172 			}
    173 		} else {
    174 			rumpuser_cv_wait_nowrap(si_lvl->si_cv, si_mtx);
    175 			continue;
    176 		}
    177 		rumpuser_mutex_exit(si_mtx);
    178 
    179 		rump_schedule();
    180 		if (!mpsafe)
    181 			KERNEL_LOCK(1, curlwp);
    182 		func(funarg);
    183 		if (!mpsafe)
    184 			KERNEL_UNLOCK_ONE(curlwp);
    185 		rump_unschedule();
    186 
    187 		rumpuser_mutex_enter_nowrap(si_mtx);
    188 	}
    189 
    190 	panic("sithread unreachable");
    191 }
    192 
    193 void
    194 softint_init(struct cpu_info *ci)
    195 {
    196 	int rv, i;
    197 
    198 	rumpuser_mutex_init(&si_mtx);
    199 	for (i = 0; i < SOFTINT_COUNT; i++) {
    200 		rumpuser_cv_init(&softints[i].si_cv);
    201 		LIST_INIT(&softints[i].si_pending);
    202 	}
    203 
    204 	rumpuser_cv_init(&clockcv);
    205 	rumpuser_mutex_init(&clockmtx);
    206 
    207 	/* XXX: should have separate "wanttimer" control */
    208 	if (rump_threads) {
    209 		for (i = 0; i < SOFTINT_COUNT; i++) {
    210 			rv = kthread_create(PRI_NONE,
    211 			    KTHREAD_MPSAFE | KTHREAD_INTR, NULL,
    212 			    sithread, (void *)(uintptr_t)i,
    213 			    NULL, "rumpsi%d", i);
    214 		}
    215 
    216 		rumpuser_mutex_enter(clockmtx);
    217 		rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_INTR,
    218 		    NULL, doclock, NULL, NULL, "rumpclk");
    219 		if (rv)
    220 			panic("clock thread creation failed: %d", rv);
    221 
    222 		/* make sure we have a clocktime before returning */
    223 		rumpuser_cv_wait(clockcv, clockmtx);
    224 		rumpuser_mutex_exit(clockmtx);
    225 	}
    226 }
    227 
    228 /*
    229  * Soft interrupts bring two choices.  If we are running with thread
    230  * support enabled, defer execution, otherwise execute in place.
    231  * See softint_schedule().
    232  *
    233  * As there is currently no clear concept of when a thread finishes
    234  * work (although rump_clear_curlwp() is close), simply execute all
    235  * softints in the timer thread.  This is probably not the most
    236  * efficient method, but good enough for now.
    237  */
    238 void *
    239 softint_establish(u_int flags, void (*func)(void *), void *arg)
    240 {
    241 	struct softint *si;
    242 
    243 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
    244 	si->si_func = func;
    245 	si->si_arg = arg;
    246 	si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
    247 	si->si_level = flags & SOFTINT_LVLMASK;
    248 	KASSERT(si->si_level < SOFTINT_COUNT);
    249 
    250 	return si;
    251 }
    252 
    253 void
    254 softint_schedule(void *arg)
    255 {
    256 	struct softint *si = arg;
    257 
    258 	if (!rump_threads) {
    259 		si->si_func(si->si_arg);
    260 	} else {
    261 		rumpuser_mutex_enter(si_mtx);
    262 		if (!(si->si_flags & SI_ONLIST)) {
    263 			LIST_INSERT_HEAD(&softints[si->si_level].si_pending,
    264 			    si, si_entries);
    265 			si->si_flags |= SI_ONLIST;
    266 		}
    267 		rumpuser_mutex_exit(si_mtx);
    268 	}
    269 }
    270 
    271 /* flimsy disestablish: should wait for softints to finish */
    272 void
    273 softint_disestablish(void *cook)
    274 {
    275 	struct softint *si = cook;
    276 
    277 	rumpuser_mutex_enter(si_mtx);
    278 	if (si->si_flags & SI_ONLIST) {
    279 		si->si_flags |= SI_KILLME;
    280 		return;
    281 	}
    282 	rumpuser_mutex_exit(si_mtx);
    283 	kmem_free(si, sizeof(*si));
    284 }
    285 
    286 void
    287 rump_softint_run(struct cpu_info *ci)
    288 {
    289 	int i;
    290 
    291 	rumpuser_mutex_enter_nowrap(si_mtx);
    292 	for (i = 0; i < SOFTINT_COUNT; i++) {
    293 		if (!LIST_EMPTY(&softints[i].si_pending))
    294 			rumpuser_cv_signal(softints[i].si_cv);
    295 	}
    296 	rumpuser_mutex_exit(si_mtx);
    297 }
    298 
    299 bool
    300 cpu_intr_p(void)
    301 {
    302 
    303 	return false;
    304 }
    305 
    306 bool
    307 cpu_softintr_p(void)
    308 {
    309 
    310 	return curlwp->l_pflag & LP_INTR;
    311 }
    312