Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.25
      1 /*	$NetBSD: intr.c,v 1.25 2010/04/27 23:30: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.25 2010/04/27 23:30:29 pooka Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/cpu.h>
     33 #include <sys/kernel.h>
     34 #include <sys/kmem.h>
     35 #include <sys/kthread.h>
     36 #include <sys/intr.h>
     37 #include <sys/timetc.h>
     38 
     39 #include <rump/rumpuser.h>
     40 
     41 #include "rump_private.h"
     42 
     43 /*
     44  * Interrupt simulator.  It executes hardclock() and softintrs.
     45  */
     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 
     71 kcondvar_t lbolt; /* Oh Kath Ra */
     72 
     73 static u_int ticks;
     74 
     75 static u_int
     76 rumptc_get(struct timecounter *tc)
     77 {
     78 
     79 	KASSERT(rump_threads);
     80 	return ticks;
     81 }
     82 
     83 static struct timecounter rumptc = {
     84 	.tc_get_timecount	= rumptc_get,
     85 	.tc_poll_pps 		= NULL,
     86 	.tc_counter_mask	= ~0,
     87 	.tc_frequency		= 0,
     88 	.tc_name		= "rumpclk",
     89 	.tc_quality		= 0,
     90 };
     91 
     92 /*
     93  * clock "interrupt"
     94  */
     95 static void
     96 doclock(void *noarg)
     97 {
     98 	struct timespec thetick, curtime;
     99 	uint64_t sec, nsec;
    100 	int error;
    101 	extern int hz;
    102 
    103 	rumpuser_gettime(&sec, &nsec, &error);
    104 	clockbase.tv_sec = sec;
    105 	clockbase.tv_nsec = nsec;
    106 	curtime = clockbase;
    107 	thetick.tv_sec = 0;
    108 	thetick.tv_nsec = 1000000000/hz;
    109 
    110 	rumpuser_mutex_enter(clockmtx);
    111 	for (;;) {
    112 		callout_hardclock();
    113 
    114 		/* wait until the next tick. XXX: what if the clock changes? */
    115 		while (rumpuser_cv_timedwait(clockcv, clockmtx,
    116 		    curtime.tv_sec, curtime.tv_nsec) == 0)
    117 			continue;
    118 		timespecadd(&clockup, &thetick, &clockup);
    119 		timespecadd(&clockup, &clockbase, &curtime);
    120 
    121 		/* if !maincpu: continue */
    122 		if (curcpu()->ci_index != 0)
    123 			continue;
    124 
    125 		if ((++ticks % hz) == 0) {
    126 			cv_broadcast(&lbolt);
    127 		}
    128 		tc_ticktock();
    129 	}
    130 }
    131 
    132 /*
    133  * Soft interrupt execution thread.  Note that we run without a CPU
    134  * context until we start processing the interrupt.  This is to avoid
    135  * lock recursion.
    136  */
    137 static void
    138 sithread(void *arg)
    139 {
    140 	struct softint *si;
    141 	void (*func)(void *) = NULL;
    142 	void *funarg;
    143 	bool mpsafe;
    144 	int mylevel = (uintptr_t)arg;
    145 	struct softint_lev *si_lvlp, *si_lvl;
    146 	struct cpu_data *cd = &curcpu()->ci_data;
    147 
    148 	rump_unschedule();
    149 
    150 	si_lvlp = cd->cpu_softcpu;
    151 	si_lvl = &si_lvlp[mylevel];
    152 
    153 	/*
    154 	 * XXX: si_mtx is unnecessary, and should open an interface
    155 	 * which allows to use schedmtx for the cv wait
    156 	 */
    157 	rumpuser_mutex_enter_nowrap(si_mtx);
    158 	for (;;) {
    159 		if (!LIST_EMPTY(&si_lvl->si_pending)) {
    160 			si = LIST_FIRST(&si_lvl->si_pending);
    161 			func = si->si_func;
    162 			funarg = si->si_arg;
    163 			mpsafe = si->si_flags & SI_MPSAFE;
    164 
    165 			si->si_flags &= ~SI_ONLIST;
    166 			LIST_REMOVE(si, si_entries);
    167 			if (si->si_flags & SI_KILLME) {
    168 				rumpuser_mutex_exit(si_mtx);
    169 				rump_schedule();
    170 				softint_disestablish(si);
    171 				rump_unschedule();
    172 				rumpuser_mutex_enter_nowrap(si_mtx);
    173 				continue;
    174 			}
    175 		} else {
    176 			rumpuser_cv_wait_nowrap(si_lvl->si_cv, si_mtx);
    177 			continue;
    178 		}
    179 		rumpuser_mutex_exit(si_mtx);
    180 
    181 		rump_schedule();
    182 		if (!mpsafe)
    183 			KERNEL_LOCK(1, curlwp);
    184 		func(funarg);
    185 		if (!mpsafe)
    186 			KERNEL_UNLOCK_ONE(curlwp);
    187 		rump_unschedule();
    188 
    189 		rumpuser_mutex_enter_nowrap(si_mtx);
    190 	}
    191 
    192 	panic("sithread unreachable");
    193 }
    194 
    195 void
    196 rump_intr_init()
    197 {
    198 
    199 	rumpuser_mutex_init(&si_mtx);
    200 	rumpuser_cv_init(&clockcv);
    201 	rumpuser_mutex_init(&clockmtx);
    202 	cv_init(&lbolt, "oh kath ra");
    203 }
    204 
    205 void
    206 softint_init(struct cpu_info *ci)
    207 {
    208 	struct cpu_data *cd = &ci->ci_data;
    209 	struct softint_lev *slev;
    210 	int rv, i;
    211 
    212 	if (!rump_threads)
    213 		return;
    214 
    215 	/* XXX */
    216 	if (ci->ci_index == 0) {
    217 		rumptc.tc_frequency = hz;
    218 		tc_init(&rumptc);
    219 	}
    220 
    221 	slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
    222 	for (i = 0; i < SOFTINT_COUNT; i++) {
    223 		rumpuser_cv_init(&slev[i].si_cv);
    224 		LIST_INIT(&slev[i].si_pending);
    225 	}
    226 	cd->cpu_softcpu = slev;
    227 
    228 	for (i = 0; i < SOFTINT_COUNT; i++) {
    229 		rv = kthread_create(PRI_NONE,
    230 		    KTHREAD_MPSAFE | KTHREAD_INTR, ci,
    231 		    sithread, (void *)(uintptr_t)i,
    232 		    NULL, "rumpsi%d", i);
    233 	}
    234 
    235 	rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_INTR,
    236 	    ci, doclock, NULL, NULL, "rumpclk%d", i);
    237 	if (rv)
    238 		panic("clock thread creation failed: %d", rv);
    239 }
    240 
    241 /*
    242  * Soft interrupts bring two choices.  If we are running with thread
    243  * support enabled, defer execution, otherwise execute in place.
    244  * See softint_schedule().
    245  *
    246  * As there is currently no clear concept of when a thread finishes
    247  * work (although rump_clear_curlwp() is close), simply execute all
    248  * softints in the timer thread.  This is probably not the most
    249  * efficient method, but good enough for now.
    250  */
    251 void *
    252 softint_establish(u_int flags, void (*func)(void *), void *arg)
    253 {
    254 	struct softint *si;
    255 
    256 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
    257 	si->si_func = func;
    258 	si->si_arg = arg;
    259 	si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
    260 	si->si_level = flags & SOFTINT_LVLMASK;
    261 	KASSERT(si->si_level < SOFTINT_COUNT);
    262 
    263 	return si;
    264 }
    265 
    266 void
    267 softint_schedule(void *arg)
    268 {
    269 	struct softint *si = arg;
    270 	struct cpu_data *cd = &curcpu()->ci_data;
    271 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    272 
    273 	if (!rump_threads) {
    274 		si->si_func(si->si_arg);
    275 	} else {
    276 		if (!(si->si_flags & SI_ONLIST)) {
    277 			LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending,
    278 			    si, si_entries);
    279 			si->si_flags |= SI_ONLIST;
    280 		}
    281 	}
    282 }
    283 
    284 /* flimsy disestablish: should wait for softints to finish */
    285 void
    286 softint_disestablish(void *cook)
    287 {
    288 	struct softint *si = cook;
    289 
    290 	rumpuser_mutex_enter(si_mtx);
    291 	if (si->si_flags & SI_ONLIST) {
    292 		si->si_flags |= SI_KILLME;
    293 		return;
    294 	}
    295 	rumpuser_mutex_exit(si_mtx);
    296 	kmem_free(si, sizeof(*si));
    297 }
    298 
    299 void
    300 rump_softint_run(struct cpu_info *ci)
    301 {
    302 	struct cpu_data *cd = &ci->ci_data;
    303 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    304 	int i;
    305 
    306 	if (!rump_threads)
    307 		return;
    308 
    309 	for (i = 0; i < SOFTINT_COUNT; i++) {
    310 		if (!LIST_EMPTY(&si_lvl[i].si_pending))
    311 			rumpuser_cv_signal(si_lvl[i].si_cv);
    312 	}
    313 }
    314 
    315 bool
    316 cpu_intr_p(void)
    317 {
    318 
    319 	return false;
    320 }
    321 
    322 bool
    323 cpu_softintr_p(void)
    324 {
    325 
    326 	return curlwp->l_pflag & LP_INTR;
    327 }
    328