Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.24
      1  1.24     pooka /*	$NetBSD: intr.c,v 1.24 2010/04/14 10:27:53 pooka Exp $	*/
      2   1.2        ad 
      3   1.5     pooka /*
      4   1.5     pooka  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5   1.2        ad  *
      6   1.2        ad  * Redistribution and use in source and binary forms, with or without
      7   1.2        ad  * modification, are permitted provided that the following conditions
      8   1.2        ad  * are met:
      9   1.2        ad  * 1. Redistributions of source code must retain the above copyright
     10   1.2        ad  *    notice, this list of conditions and the following disclaimer.
     11   1.2        ad  * 2. Redistributions in binary form must reproduce the above copyright
     12   1.2        ad  *    notice, this list of conditions and the following disclaimer in the
     13   1.2        ad  *    documentation and/or other materials provided with the distribution.
     14   1.2        ad  *
     15   1.5     pooka  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16   1.5     pooka  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17   1.5     pooka  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18   1.5     pooka  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19   1.5     pooka  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20   1.5     pooka  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21   1.5     pooka  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22   1.5     pooka  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23   1.5     pooka  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24   1.5     pooka  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25   1.5     pooka  * SUCH DAMAGE.
     26   1.2        ad  */
     27   1.2        ad 
     28  1.11     pooka #include <sys/cdefs.h>
     29  1.24     pooka __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.24 2010/04/14 10:27:53 pooka Exp $");
     30  1.11     pooka 
     31   1.2        ad #include <sys/param.h>
     32   1.5     pooka #include <sys/cpu.h>
     33  1.24     pooka #include <sys/kernel.h>
     34  1.12     pooka #include <sys/kmem.h>
     35   1.5     pooka #include <sys/kthread.h>
     36   1.2        ad #include <sys/intr.h>
     37  1.24     pooka #include <sys/timetc.h>
     38   1.2        ad 
     39   1.5     pooka #include <rump/rumpuser.h>
     40   1.5     pooka 
     41   1.5     pooka #include "rump_private.h"
     42   1.5     pooka 
     43   1.5     pooka /*
     44   1.5     pooka  * Interrupt simulator.  It executes hardclock() and softintrs.
     45   1.5     pooka  */
     46   1.5     pooka 
     47  1.18     pooka #define SI_MPSAFE 0x01
     48  1.18     pooka #define SI_ONLIST 0x02
     49  1.18     pooka #define SI_KILLME 0x04
     50  1.20     pooka 
     51   1.5     pooka struct softint {
     52   1.5     pooka 	void (*si_func)(void *);
     53   1.5     pooka 	void *si_arg;
     54  1.18     pooka 	int si_flags;
     55  1.20     pooka 	int si_level;
     56   1.5     pooka 
     57   1.5     pooka 	LIST_ENTRY(softint) si_entries;
     58   1.2        ad };
     59  1.10     pooka 
     60  1.20     pooka static struct rumpuser_mtx *si_mtx;
     61  1.22     pooka struct softint_lev {
     62  1.20     pooka 	struct rumpuser_cv *si_cv;
     63  1.20     pooka 	LIST_HEAD(, softint) si_pending;
     64  1.22     pooka };
     65  1.10     pooka 
     66  1.14     pooka /* rumpuser structures since we call rumpuser interfaces directly */
     67  1.14     pooka static struct rumpuser_cv *clockcv;
     68  1.14     pooka static struct rumpuser_mtx *clockmtx;
     69  1.16     pooka static struct timespec clockbase, clockup;
     70  1.14     pooka 
     71  1.23     pooka kcondvar_t lbolt; /* Oh Kath Ra */
     72  1.23     pooka 
     73  1.24     pooka static u_int ticks;
     74  1.24     pooka 
     75  1.24     pooka static u_int
     76  1.24     pooka rumptc_get(struct timecounter *tc)
     77  1.14     pooka {
     78  1.14     pooka 
     79  1.24     pooka 	KASSERT(rump_threads);
     80  1.24     pooka 	return ticks;
     81  1.16     pooka }
     82  1.16     pooka 
     83  1.24     pooka static struct timecounter rumptc = {
     84  1.24     pooka 	.tc_get_timecount	= rumptc_get,
     85  1.24     pooka 	.tc_poll_pps 		= NULL,
     86  1.24     pooka 	.tc_counter_mask	= ~0,
     87  1.24     pooka 	.tc_frequency		= 0,
     88  1.24     pooka 	.tc_name		= "rumpclk",
     89  1.24     pooka 	.tc_quality		= 0,
     90  1.24     pooka };
     91  1.14     pooka 
     92  1.10     pooka /*
     93  1.10     pooka  * clock "interrupt"
     94  1.10     pooka  */
     95  1.10     pooka static void
     96  1.10     pooka doclock(void *noarg)
     97  1.10     pooka {
     98  1.24     pooka 	struct timespec thetick, curtime;
     99  1.15     pooka 	uint64_t sec, nsec;
    100  1.24     pooka 	int error;
    101  1.10     pooka 	extern int hz;
    102  1.14     pooka 
    103  1.15     pooka 	rumpuser_gettime(&sec, &nsec, &error);
    104  1.16     pooka 	clockbase.tv_sec = sec;
    105  1.16     pooka 	clockbase.tv_nsec = nsec;
    106  1.16     pooka 	curtime = clockbase;
    107  1.24     pooka 	thetick.tv_sec = 0;
    108  1.24     pooka 	thetick.tv_nsec = 1000000000/hz;
    109  1.14     pooka 
    110  1.14     pooka 	rumpuser_mutex_enter(clockmtx);
    111  1.14     pooka 	rumpuser_cv_signal(clockcv);
    112  1.10     pooka 
    113  1.10     pooka 	for (;;) {
    114   1.5     pooka 		callout_hardclock();
    115   1.5     pooka 
    116  1.22     pooka 		/* wait until the next tick. XXX: what if the clock changes? */
    117  1.22     pooka 		while (rumpuser_cv_timedwait(clockcv, clockmtx,
    118  1.22     pooka 		    curtime.tv_sec, curtime.tv_nsec) == 0)
    119  1.22     pooka 			continue;
    120  1.22     pooka 
    121  1.22     pooka 		/* if !maincpu: continue */
    122  1.22     pooka 
    123  1.24     pooka 		if ((++ticks % hz) == 0) {
    124  1.23     pooka 			cv_broadcast(&lbolt);
    125   1.8     pooka 		}
    126  1.24     pooka 		tc_ticktock();
    127  1.14     pooka 
    128  1.24     pooka 		timespecadd(&clockup, &thetick, &clockup);
    129  1.16     pooka 		timespecadd(&clockup, &clockbase, &curtime);
    130  1.10     pooka 	}
    131  1.10     pooka }
    132   1.8     pooka 
    133  1.10     pooka /*
    134  1.20     pooka  * Soft interrupt execution thread.  Note that we run without a CPU
    135  1.20     pooka  * context until we start processing the interrupt.  This is to avoid
    136  1.20     pooka  * lock recursion.
    137  1.10     pooka  */
    138  1.10     pooka static void
    139  1.10     pooka sithread(void *arg)
    140  1.10     pooka {
    141  1.10     pooka 	struct softint *si;
    142  1.10     pooka 	void (*func)(void *) = NULL;
    143  1.10     pooka 	void *funarg;
    144  1.10     pooka 	bool mpsafe;
    145  1.20     pooka 	int mylevel = (uintptr_t)arg;
    146  1.22     pooka 	struct softint_lev *si_lvlp, *si_lvl;
    147  1.22     pooka 	struct cpu_data *cd = &curcpu()->ci_data;
    148  1.20     pooka 
    149  1.20     pooka 	rump_unschedule();
    150  1.10     pooka 
    151  1.22     pooka 	si_lvlp = cd->cpu_softcpu;
    152  1.22     pooka 	si_lvl = &si_lvlp[mylevel];
    153  1.22     pooka 
    154  1.22     pooka 	/*
    155  1.22     pooka 	 * XXX: si_mtx is unnecessary, and should open an interface
    156  1.22     pooka 	 * which allows to use schedmtx for the cv wait
    157  1.22     pooka 	 */
    158  1.20     pooka 	rumpuser_mutex_enter_nowrap(si_mtx);
    159  1.10     pooka 	for (;;) {
    160  1.20     pooka 		if (!LIST_EMPTY(&si_lvl->si_pending)) {
    161  1.20     pooka 			si = LIST_FIRST(&si_lvl->si_pending);
    162   1.5     pooka 			func = si->si_func;
    163   1.5     pooka 			funarg = si->si_arg;
    164  1.18     pooka 			mpsafe = si->si_flags & SI_MPSAFE;
    165   1.5     pooka 
    166  1.18     pooka 			si->si_flags &= ~SI_ONLIST;
    167   1.5     pooka 			LIST_REMOVE(si, si_entries);
    168  1.20     pooka 			if (si->si_flags & SI_KILLME) {
    169  1.20     pooka 				rumpuser_mutex_exit(si_mtx);
    170  1.20     pooka 				rump_schedule();
    171  1.18     pooka 				softint_disestablish(si);
    172  1.20     pooka 				rump_unschedule();
    173  1.20     pooka 				rumpuser_mutex_enter_nowrap(si_mtx);
    174  1.20     pooka 				continue;
    175  1.20     pooka 			}
    176  1.10     pooka 		} else {
    177  1.20     pooka 			rumpuser_cv_wait_nowrap(si_lvl->si_cv, si_mtx);
    178  1.10     pooka 			continue;
    179   1.5     pooka 		}
    180  1.20     pooka 		rumpuser_mutex_exit(si_mtx);
    181   1.5     pooka 
    182  1.20     pooka 		rump_schedule();
    183  1.10     pooka 		if (!mpsafe)
    184  1.10     pooka 			KERNEL_LOCK(1, curlwp);
    185  1.10     pooka 		func(funarg);
    186  1.10     pooka 		if (!mpsafe)
    187  1.10     pooka 			KERNEL_UNLOCK_ONE(curlwp);
    188  1.20     pooka 		rump_unschedule();
    189  1.10     pooka 
    190  1.20     pooka 		rumpuser_mutex_enter_nowrap(si_mtx);
    191   1.5     pooka 	}
    192  1.20     pooka 
    193  1.20     pooka 	panic("sithread unreachable");
    194   1.5     pooka }
    195   1.2        ad 
    196   1.5     pooka void
    197  1.22     pooka rump_intr_init()
    198  1.22     pooka {
    199  1.22     pooka 
    200  1.22     pooka 	rumpuser_mutex_init(&si_mtx);
    201  1.22     pooka 	rumpuser_cv_init(&clockcv);
    202  1.22     pooka 	rumpuser_mutex_init(&clockmtx);
    203  1.23     pooka 	cv_init(&lbolt, "oh kath ra");
    204  1.22     pooka }
    205  1.22     pooka 
    206  1.22     pooka void
    207   1.5     pooka softint_init(struct cpu_info *ci)
    208   1.2        ad {
    209  1.22     pooka 	struct cpu_data *cd = &ci->ci_data;
    210  1.22     pooka 	struct softint_lev *slev;
    211  1.20     pooka 	int rv, i;
    212   1.5     pooka 
    213  1.22     pooka 	if (!rump_threads)
    214  1.22     pooka 		return;
    215  1.22     pooka 
    216  1.22     pooka 	slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
    217  1.20     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    218  1.22     pooka 		rumpuser_cv_init(&slev[i].si_cv);
    219  1.22     pooka 		LIST_INIT(&slev[i].si_pending);
    220  1.20     pooka 	}
    221  1.22     pooka 	cd->cpu_softcpu = slev;
    222   1.2        ad 
    223  1.22     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    224  1.22     pooka 		rv = kthread_create(PRI_NONE,
    225  1.22     pooka 		    KTHREAD_MPSAFE | KTHREAD_INTR, NULL,
    226  1.22     pooka 		    sithread, (void *)(uintptr_t)i,
    227  1.22     pooka 		    NULL, "rumpsi%d", i);
    228  1.22     pooka 	}
    229  1.14     pooka 
    230  1.22     pooka 	rumpuser_mutex_enter(clockmtx);
    231  1.22     pooka 	for (i = 0; i < ncpu; i++) {
    232  1.22     pooka 		rv = kthread_create(PRI_NONE,
    233  1.22     pooka 		    KTHREAD_MPSAFE | KTHREAD_INTR,
    234  1.22     pooka 		    cpu_lookup(i), doclock, NULL, NULL,
    235  1.22     pooka 		    "rumpclk%d", i);
    236   1.5     pooka 		if (rv)
    237  1.10     pooka 			panic("clock thread creation failed: %d", rv);
    238  1.22     pooka 	}
    239  1.14     pooka 
    240  1.24     pooka 	rumptc.tc_frequency = hz;
    241  1.24     pooka 	tc_init(&rumptc);
    242  1.24     pooka 
    243  1.22     pooka 	/*
    244  1.22     pooka 	 * Make sure we have a clocktime before returning.
    245  1.22     pooka 	 * XXX: mp
    246  1.22     pooka 	 */
    247  1.22     pooka 	rumpuser_cv_wait(clockcv, clockmtx);
    248  1.22     pooka 	rumpuser_mutex_exit(clockmtx);
    249   1.2        ad }
    250   1.2        ad 
    251   1.5     pooka /*
    252   1.5     pooka  * Soft interrupts bring two choices.  If we are running with thread
    253   1.5     pooka  * support enabled, defer execution, otherwise execute in place.
    254   1.5     pooka  * See softint_schedule().
    255   1.5     pooka  *
    256   1.5     pooka  * As there is currently no clear concept of when a thread finishes
    257   1.5     pooka  * work (although rump_clear_curlwp() is close), simply execute all
    258   1.5     pooka  * softints in the timer thread.  This is probably not the most
    259   1.5     pooka  * efficient method, but good enough for now.
    260   1.5     pooka  */
    261   1.5     pooka void *
    262   1.5     pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
    263   1.2        ad {
    264   1.5     pooka 	struct softint *si;
    265   1.2        ad 
    266   1.5     pooka 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
    267   1.5     pooka 	si->si_func = func;
    268   1.5     pooka 	si->si_arg = arg;
    269  1.18     pooka 	si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
    270  1.20     pooka 	si->si_level = flags & SOFTINT_LVLMASK;
    271  1.20     pooka 	KASSERT(si->si_level < SOFTINT_COUNT);
    272   1.5     pooka 
    273   1.5     pooka 	return si;
    274   1.2        ad }
    275   1.2        ad 
    276   1.2        ad void
    277   1.2        ad softint_schedule(void *arg)
    278   1.2        ad {
    279   1.5     pooka 	struct softint *si = arg;
    280  1.22     pooka 	struct cpu_data *cd = &curcpu()->ci_data;
    281  1.22     pooka 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    282   1.2        ad 
    283   1.5     pooka 	if (!rump_threads) {
    284   1.5     pooka 		si->si_func(si->si_arg);
    285   1.5     pooka 	} else {
    286  1.18     pooka 		if (!(si->si_flags & SI_ONLIST)) {
    287  1.22     pooka 			LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending,
    288  1.20     pooka 			    si, si_entries);
    289  1.18     pooka 			si->si_flags |= SI_ONLIST;
    290   1.5     pooka 		}
    291   1.5     pooka 	}
    292   1.2        ad }
    293   1.2        ad 
    294  1.18     pooka /* flimsy disestablish: should wait for softints to finish */
    295  1.18     pooka void
    296  1.18     pooka softint_disestablish(void *cook)
    297  1.18     pooka {
    298  1.18     pooka 	struct softint *si = cook;
    299  1.18     pooka 
    300  1.20     pooka 	rumpuser_mutex_enter(si_mtx);
    301  1.18     pooka 	if (si->si_flags & SI_ONLIST) {
    302  1.18     pooka 		si->si_flags |= SI_KILLME;
    303  1.18     pooka 		return;
    304  1.18     pooka 	}
    305  1.20     pooka 	rumpuser_mutex_exit(si_mtx);
    306  1.18     pooka 	kmem_free(si, sizeof(*si));
    307  1.18     pooka }
    308  1.18     pooka 
    309  1.20     pooka void
    310  1.20     pooka rump_softint_run(struct cpu_info *ci)
    311  1.20     pooka {
    312  1.22     pooka 	struct cpu_data *cd = &ci->ci_data;
    313  1.22     pooka 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    314  1.20     pooka 	int i;
    315  1.20     pooka 
    316  1.22     pooka 	if (!rump_threads)
    317  1.22     pooka 		return;
    318  1.22     pooka 
    319  1.20     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    320  1.22     pooka 		if (!LIST_EMPTY(&si_lvl[i].si_pending))
    321  1.22     pooka 			rumpuser_cv_signal(si_lvl[i].si_cv);
    322  1.20     pooka 	}
    323  1.20     pooka }
    324  1.20     pooka 
    325   1.2        ad bool
    326   1.9  christos cpu_intr_p(void)
    327   1.2        ad {
    328   1.2        ad 
    329   1.2        ad 	return false;
    330   1.2        ad }
    331  1.19     pooka 
    332  1.19     pooka bool
    333  1.19     pooka cpu_softintr_p(void)
    334  1.19     pooka {
    335  1.19     pooka 
    336  1.20     pooka 	return curlwp->l_pflag & LP_INTR;
    337  1.19     pooka }
    338