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