Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.28
      1  1.28     pooka /*	$NetBSD: intr.c,v 1.28 2010/05/18 14:58:42 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.28     pooka __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.28 2010/05/18 14:58:42 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.28     pooka #include <sys/malloc.h>
     37   1.2        ad #include <sys/intr.h>
     38  1.24     pooka #include <sys/timetc.h>
     39   1.2        ad 
     40   1.5     pooka #include <rump/rumpuser.h>
     41   1.5     pooka 
     42   1.5     pooka #include "rump_private.h"
     43   1.5     pooka 
     44   1.5     pooka /*
     45   1.5     pooka  * Interrupt simulator.  It executes hardclock() and softintrs.
     46   1.5     pooka  */
     47   1.5     pooka 
     48  1.18     pooka #define SI_MPSAFE 0x01
     49  1.18     pooka #define SI_ONLIST 0x02
     50  1.18     pooka #define SI_KILLME 0x04
     51  1.20     pooka 
     52   1.5     pooka struct softint {
     53   1.5     pooka 	void (*si_func)(void *);
     54   1.5     pooka 	void *si_arg;
     55  1.18     pooka 	int si_flags;
     56  1.20     pooka 	int si_level;
     57   1.5     pooka 
     58   1.5     pooka 	LIST_ENTRY(softint) si_entries;
     59   1.2        ad };
     60  1.10     pooka 
     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.23     pooka kcondvar_t lbolt; /* Oh Kath Ra */
     67  1.23     pooka 
     68  1.24     pooka static u_int ticks;
     69  1.24     pooka 
     70  1.24     pooka static u_int
     71  1.24     pooka rumptc_get(struct timecounter *tc)
     72  1.14     pooka {
     73  1.14     pooka 
     74  1.24     pooka 	KASSERT(rump_threads);
     75  1.24     pooka 	return ticks;
     76  1.16     pooka }
     77  1.16     pooka 
     78  1.24     pooka static struct timecounter rumptc = {
     79  1.24     pooka 	.tc_get_timecount	= rumptc_get,
     80  1.24     pooka 	.tc_poll_pps 		= NULL,
     81  1.24     pooka 	.tc_counter_mask	= ~0,
     82  1.24     pooka 	.tc_frequency		= 0,
     83  1.24     pooka 	.tc_name		= "rumpclk",
     84  1.24     pooka 	.tc_quality		= 0,
     85  1.24     pooka };
     86  1.14     pooka 
     87  1.10     pooka /*
     88  1.10     pooka  * clock "interrupt"
     89  1.10     pooka  */
     90  1.10     pooka static void
     91  1.10     pooka doclock(void *noarg)
     92  1.10     pooka {
     93  1.26     pooka 	struct timespec clockbase, clockup;
     94  1.24     pooka 	struct timespec thetick, curtime;
     95  1.26     pooka 	struct rumpuser_cv *clockcv;
     96  1.26     pooka 	struct rumpuser_mtx *clockmtx;
     97  1.15     pooka 	uint64_t sec, nsec;
     98  1.24     pooka 	int error;
     99  1.10     pooka 	extern int hz;
    100  1.14     pooka 
    101  1.26     pooka 	memset(&clockup, 0, sizeof(clockup));
    102  1.15     pooka 	rumpuser_gettime(&sec, &nsec, &error);
    103  1.16     pooka 	clockbase.tv_sec = sec;
    104  1.16     pooka 	clockbase.tv_nsec = nsec;
    105  1.16     pooka 	curtime = clockbase;
    106  1.24     pooka 	thetick.tv_sec = 0;
    107  1.24     pooka 	thetick.tv_nsec = 1000000000/hz;
    108  1.14     pooka 
    109  1.26     pooka 	/* XXX: dummies */
    110  1.26     pooka 	rumpuser_cv_init(&clockcv);
    111  1.26     pooka 	rumpuser_mutex_init(&clockmtx);
    112  1.26     pooka 
    113  1.14     pooka 	rumpuser_mutex_enter(clockmtx);
    114  1.10     pooka 	for (;;) {
    115   1.5     pooka 		callout_hardclock();
    116   1.5     pooka 
    117  1.22     pooka 		/* wait until the next tick. XXX: what if the clock changes? */
    118  1.22     pooka 		while (rumpuser_cv_timedwait(clockcv, clockmtx,
    119  1.22     pooka 		    curtime.tv_sec, curtime.tv_nsec) == 0)
    120  1.22     pooka 			continue;
    121  1.26     pooka 
    122  1.26     pooka 		/* XXX: sync with a) virtual clock b) host clock */
    123  1.26     pooka 		timespecadd(&clockup, &clockbase, &curtime);
    124  1.25     pooka 		timespecadd(&clockup, &thetick, &clockup);
    125  1.26     pooka 
    126  1.26     pooka #if 0
    127  1.26     pooka 		/* CPU_IS_PRIMARY is MD and hence unreliably correct here */
    128  1.26     pooka 		if (!CPU_IS_PRIMARY(curcpu()))
    129  1.26     pooka 			continue;
    130  1.26     pooka #else
    131  1.27     pooka 		if (curcpu()->ci_index != 0)
    132  1.25     pooka 			continue;
    133  1.26     pooka #endif
    134  1.22     pooka 
    135  1.24     pooka 		if ((++ticks % hz) == 0) {
    136  1.23     pooka 			cv_broadcast(&lbolt);
    137   1.8     pooka 		}
    138  1.24     pooka 		tc_ticktock();
    139  1.10     pooka 	}
    140  1.10     pooka }
    141   1.8     pooka 
    142  1.10     pooka /*
    143  1.28     pooka  * Soft interrupt execution thread.  This thread is pinned to the
    144  1.28     pooka  * same CPU that scheduled the interrupt, so we don't need to do
    145  1.28     pooka  * lock against si_lvl.
    146  1.10     pooka  */
    147  1.10     pooka static void
    148  1.10     pooka sithread(void *arg)
    149  1.10     pooka {
    150  1.10     pooka 	struct softint *si;
    151  1.10     pooka 	void (*func)(void *) = NULL;
    152  1.10     pooka 	void *funarg;
    153  1.10     pooka 	bool mpsafe;
    154  1.20     pooka 	int mylevel = (uintptr_t)arg;
    155  1.22     pooka 	struct softint_lev *si_lvlp, *si_lvl;
    156  1.22     pooka 	struct cpu_data *cd = &curcpu()->ci_data;
    157  1.20     pooka 
    158  1.22     pooka 	si_lvlp = cd->cpu_softcpu;
    159  1.22     pooka 	si_lvl = &si_lvlp[mylevel];
    160  1.22     pooka 
    161  1.10     pooka 	for (;;) {
    162  1.20     pooka 		if (!LIST_EMPTY(&si_lvl->si_pending)) {
    163  1.20     pooka 			si = LIST_FIRST(&si_lvl->si_pending);
    164   1.5     pooka 			func = si->si_func;
    165   1.5     pooka 			funarg = si->si_arg;
    166  1.18     pooka 			mpsafe = si->si_flags & SI_MPSAFE;
    167   1.5     pooka 
    168  1.18     pooka 			si->si_flags &= ~SI_ONLIST;
    169   1.5     pooka 			LIST_REMOVE(si, si_entries);
    170  1.20     pooka 			if (si->si_flags & SI_KILLME) {
    171  1.18     pooka 				softint_disestablish(si);
    172  1.20     pooka 				continue;
    173  1.20     pooka 			}
    174  1.10     pooka 		} else {
    175  1.28     pooka 			rump_schedlock_cv_wait(si_lvl->si_cv);
    176  1.10     pooka 			continue;
    177   1.5     pooka 		}
    178   1.5     pooka 
    179  1.10     pooka 		if (!mpsafe)
    180  1.10     pooka 			KERNEL_LOCK(1, curlwp);
    181  1.10     pooka 		func(funarg);
    182  1.10     pooka 		if (!mpsafe)
    183  1.10     pooka 			KERNEL_UNLOCK_ONE(curlwp);
    184   1.5     pooka 	}
    185  1.20     pooka 
    186  1.20     pooka 	panic("sithread unreachable");
    187   1.5     pooka }
    188   1.2        ad 
    189   1.5     pooka void
    190  1.22     pooka rump_intr_init()
    191  1.22     pooka {
    192  1.22     pooka 
    193  1.23     pooka 	cv_init(&lbolt, "oh kath ra");
    194  1.22     pooka }
    195  1.22     pooka 
    196  1.22     pooka void
    197   1.5     pooka softint_init(struct cpu_info *ci)
    198   1.2        ad {
    199  1.22     pooka 	struct cpu_data *cd = &ci->ci_data;
    200  1.22     pooka 	struct softint_lev *slev;
    201  1.20     pooka 	int rv, i;
    202   1.5     pooka 
    203  1.22     pooka 	if (!rump_threads)
    204  1.22     pooka 		return;
    205  1.22     pooka 
    206  1.25     pooka 	/* XXX */
    207  1.25     pooka 	if (ci->ci_index == 0) {
    208  1.25     pooka 		rumptc.tc_frequency = hz;
    209  1.25     pooka 		tc_init(&rumptc);
    210  1.25     pooka 	}
    211  1.25     pooka 
    212  1.22     pooka 	slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
    213  1.20     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    214  1.22     pooka 		rumpuser_cv_init(&slev[i].si_cv);
    215  1.22     pooka 		LIST_INIT(&slev[i].si_pending);
    216  1.20     pooka 	}
    217  1.22     pooka 	cd->cpu_softcpu = slev;
    218   1.2        ad 
    219  1.28     pooka 	/* softint might run on different physical CPU */
    220  1.28     pooka 	membar_sync();
    221  1.28     pooka 
    222  1.22     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    223  1.22     pooka 		rv = kthread_create(PRI_NONE,
    224  1.25     pooka 		    KTHREAD_MPSAFE | KTHREAD_INTR, ci,
    225  1.22     pooka 		    sithread, (void *)(uintptr_t)i,
    226  1.22     pooka 		    NULL, "rumpsi%d", i);
    227  1.22     pooka 	}
    228  1.14     pooka 
    229  1.25     pooka 	rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_INTR,
    230  1.25     pooka 	    ci, doclock, NULL, NULL, "rumpclk%d", i);
    231  1.25     pooka 	if (rv)
    232  1.25     pooka 		panic("clock thread creation failed: %d", rv);
    233   1.2        ad }
    234   1.2        ad 
    235   1.5     pooka /*
    236   1.5     pooka  * Soft interrupts bring two choices.  If we are running with thread
    237   1.5     pooka  * support enabled, defer execution, otherwise execute in place.
    238   1.5     pooka  * See softint_schedule().
    239   1.5     pooka  *
    240   1.5     pooka  * As there is currently no clear concept of when a thread finishes
    241   1.5     pooka  * work (although rump_clear_curlwp() is close), simply execute all
    242   1.5     pooka  * softints in the timer thread.  This is probably not the most
    243   1.5     pooka  * efficient method, but good enough for now.
    244   1.5     pooka  */
    245   1.5     pooka void *
    246   1.5     pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
    247   1.2        ad {
    248   1.5     pooka 	struct softint *si;
    249   1.2        ad 
    250  1.28     pooka 	si = malloc(sizeof(*si), M_TEMP, M_WAITOK);
    251   1.5     pooka 	si->si_func = func;
    252   1.5     pooka 	si->si_arg = arg;
    253  1.18     pooka 	si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
    254  1.20     pooka 	si->si_level = flags & SOFTINT_LVLMASK;
    255  1.20     pooka 	KASSERT(si->si_level < SOFTINT_COUNT);
    256   1.5     pooka 
    257   1.5     pooka 	return si;
    258   1.2        ad }
    259   1.2        ad 
    260   1.2        ad void
    261   1.2        ad softint_schedule(void *arg)
    262   1.2        ad {
    263   1.5     pooka 	struct softint *si = arg;
    264  1.22     pooka 	struct cpu_data *cd = &curcpu()->ci_data;
    265  1.22     pooka 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    266   1.2        ad 
    267   1.5     pooka 	if (!rump_threads) {
    268   1.5     pooka 		si->si_func(si->si_arg);
    269   1.5     pooka 	} else {
    270  1.18     pooka 		if (!(si->si_flags & SI_ONLIST)) {
    271  1.22     pooka 			LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending,
    272  1.20     pooka 			    si, si_entries);
    273  1.18     pooka 			si->si_flags |= SI_ONLIST;
    274   1.5     pooka 		}
    275   1.5     pooka 	}
    276   1.2        ad }
    277   1.2        ad 
    278  1.18     pooka /* flimsy disestablish: should wait for softints to finish */
    279  1.18     pooka void
    280  1.18     pooka softint_disestablish(void *cook)
    281  1.18     pooka {
    282  1.18     pooka 	struct softint *si = cook;
    283  1.18     pooka 
    284  1.18     pooka 	if (si->si_flags & SI_ONLIST) {
    285  1.18     pooka 		si->si_flags |= SI_KILLME;
    286  1.18     pooka 		return;
    287  1.18     pooka 	}
    288  1.28     pooka 	free(si, M_TEMP);
    289  1.18     pooka }
    290  1.18     pooka 
    291  1.20     pooka void
    292  1.20     pooka rump_softint_run(struct cpu_info *ci)
    293  1.20     pooka {
    294  1.22     pooka 	struct cpu_data *cd = &ci->ci_data;
    295  1.22     pooka 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    296  1.20     pooka 	int i;
    297  1.20     pooka 
    298  1.22     pooka 	if (!rump_threads)
    299  1.22     pooka 		return;
    300  1.22     pooka 
    301  1.20     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    302  1.22     pooka 		if (!LIST_EMPTY(&si_lvl[i].si_pending))
    303  1.22     pooka 			rumpuser_cv_signal(si_lvl[i].si_cv);
    304  1.20     pooka 	}
    305  1.20     pooka }
    306  1.20     pooka 
    307   1.2        ad bool
    308   1.9  christos cpu_intr_p(void)
    309   1.2        ad {
    310   1.2        ad 
    311   1.2        ad 	return false;
    312   1.2        ad }
    313  1.19     pooka 
    314  1.19     pooka bool
    315  1.19     pooka cpu_softintr_p(void)
    316  1.19     pooka {
    317  1.19     pooka 
    318  1.20     pooka 	return curlwp->l_pflag & LP_INTR;
    319  1.19     pooka }
    320