Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.45
      1  1.45     rmind /*	$NetBSD: intr.c,v 1.45 2014/06/09 13:03:16 rmind Exp $	*/
      2   1.2        ad 
      3   1.5     pooka /*
      4  1.36     pooka  * Copyright (c) 2008-2010 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.45     rmind __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.45 2014/06/09 13:03:16 rmind Exp $");
     30  1.11     pooka 
     31   1.2        ad #include <sys/param.h>
     32  1.29    martin #include <sys/atomic.h>
     33   1.5     pooka #include <sys/cpu.h>
     34  1.24     pooka #include <sys/kernel.h>
     35  1.12     pooka #include <sys/kmem.h>
     36   1.5     pooka #include <sys/kthread.h>
     37  1.28     pooka #include <sys/malloc.h>
     38   1.2        ad #include <sys/intr.h>
     39  1.24     pooka #include <sys/timetc.h>
     40   1.2        ad 
     41   1.5     pooka #include <rump/rumpuser.h>
     42   1.5     pooka 
     43   1.5     pooka #include "rump_private.h"
     44   1.5     pooka 
     45   1.5     pooka /*
     46   1.5     pooka  * Interrupt simulator.  It executes hardclock() and softintrs.
     47   1.5     pooka  */
     48   1.5     pooka 
     49  1.18     pooka #define SI_MPSAFE 0x01
     50  1.32     pooka #define SI_KILLME 0x02
     51  1.20     pooka 
     52  1.32     pooka struct softint_percpu;
     53   1.5     pooka struct softint {
     54   1.5     pooka 	void (*si_func)(void *);
     55   1.5     pooka 	void *si_arg;
     56  1.18     pooka 	int si_flags;
     57  1.20     pooka 	int si_level;
     58   1.5     pooka 
     59  1.32     pooka 	struct softint_percpu *si_entry; /* [0,ncpu-1] */
     60  1.32     pooka };
     61  1.32     pooka 
     62  1.32     pooka struct softint_percpu {
     63  1.32     pooka 	struct softint *sip_parent;
     64  1.32     pooka 	bool sip_onlist;
     65  1.32     pooka 
     66  1.32     pooka 	LIST_ENTRY(softint_percpu) sip_entries;
     67   1.2        ad };
     68  1.10     pooka 
     69  1.22     pooka struct softint_lev {
     70  1.20     pooka 	struct rumpuser_cv *si_cv;
     71  1.32     pooka 	LIST_HEAD(, softint_percpu) si_pending;
     72  1.22     pooka };
     73  1.10     pooka 
     74  1.23     pooka kcondvar_t lbolt; /* Oh Kath Ra */
     75  1.23     pooka 
     76  1.24     pooka static u_int ticks;
     77  1.34     pooka static int ncpu_final;
     78  1.24     pooka 
     79  1.24     pooka static u_int
     80  1.24     pooka rumptc_get(struct timecounter *tc)
     81  1.14     pooka {
     82  1.14     pooka 
     83  1.24     pooka 	KASSERT(rump_threads);
     84  1.24     pooka 	return ticks;
     85  1.16     pooka }
     86  1.16     pooka 
     87  1.24     pooka static struct timecounter rumptc = {
     88  1.24     pooka 	.tc_get_timecount	= rumptc_get,
     89  1.24     pooka 	.tc_poll_pps 		= NULL,
     90  1.24     pooka 	.tc_counter_mask	= ~0,
     91  1.24     pooka 	.tc_frequency		= 0,
     92  1.24     pooka 	.tc_name		= "rumpclk",
     93  1.24     pooka 	.tc_quality		= 0,
     94  1.24     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.38     pooka 	struct timespec thetick, curclock;
    103  1.40     pooka 	int64_t sec;
    104  1.40     pooka 	long nsec;
    105  1.24     pooka 	int error;
    106  1.42     pooka 	int cpuindx = curcpu()->ci_index;
    107  1.10     pooka 	extern int hz;
    108  1.14     pooka 
    109  1.39     pooka 	error = rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, &sec, &nsec);
    110  1.38     pooka 	if (error)
    111  1.38     pooka 		panic("clock: cannot get monotonic time");
    112  1.38     pooka 
    113  1.38     pooka 	curclock.tv_sec = sec;
    114  1.38     pooka 	curclock.tv_nsec = nsec;
    115  1.24     pooka 	thetick.tv_sec = 0;
    116  1.24     pooka 	thetick.tv_nsec = 1000000000/hz;
    117  1.14     pooka 
    118  1.10     pooka 	for (;;) {
    119   1.5     pooka 		callout_hardclock();
    120   1.5     pooka 
    121  1.39     pooka 		error = rumpuser_clock_sleep(RUMPUSER_CLOCK_ABSMONO,
    122  1.39     pooka 		    curclock.tv_sec, curclock.tv_nsec);
    123  1.38     pooka 		KASSERT(!error);
    124  1.38     pooka 		timespecadd(&curclock, &thetick, &curclock);
    125  1.26     pooka 
    126  1.42     pooka 		if (cpuindx != 0)
    127  1.26     pooka 			continue;
    128  1.22     pooka 
    129  1.24     pooka 		if ((++ticks % hz) == 0) {
    130  1.23     pooka 			cv_broadcast(&lbolt);
    131   1.8     pooka 		}
    132  1.24     pooka 		tc_ticktock();
    133  1.10     pooka 	}
    134  1.10     pooka }
    135   1.8     pooka 
    136  1.10     pooka /*
    137  1.28     pooka  * Soft interrupt execution thread.  This thread is pinned to the
    138  1.28     pooka  * same CPU that scheduled the interrupt, so we don't need to do
    139  1.28     pooka  * lock against si_lvl.
    140  1.10     pooka  */
    141  1.10     pooka static void
    142  1.10     pooka sithread(void *arg)
    143  1.10     pooka {
    144  1.32     pooka 	struct softint_percpu *sip;
    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.22     pooka 	struct softint_lev *si_lvlp, *si_lvl;
    151  1.22     pooka 	struct cpu_data *cd = &curcpu()->ci_data;
    152  1.20     pooka 
    153  1.22     pooka 	si_lvlp = cd->cpu_softcpu;
    154  1.22     pooka 	si_lvl = &si_lvlp[mylevel];
    155  1.22     pooka 
    156  1.10     pooka 	for (;;) {
    157  1.20     pooka 		if (!LIST_EMPTY(&si_lvl->si_pending)) {
    158  1.32     pooka 			sip = LIST_FIRST(&si_lvl->si_pending);
    159  1.32     pooka 			si = sip->sip_parent;
    160  1.32     pooka 
    161   1.5     pooka 			func = si->si_func;
    162   1.5     pooka 			funarg = si->si_arg;
    163  1.18     pooka 			mpsafe = si->si_flags & SI_MPSAFE;
    164   1.5     pooka 
    165  1.32     pooka 			sip->sip_onlist = false;
    166  1.32     pooka 			LIST_REMOVE(sip, sip_entries);
    167  1.20     pooka 			if (si->si_flags & SI_KILLME) {
    168  1.18     pooka 				softint_disestablish(si);
    169  1.20     pooka 				continue;
    170  1.20     pooka 			}
    171  1.10     pooka 		} else {
    172  1.28     pooka 			rump_schedlock_cv_wait(si_lvl->si_cv);
    173  1.10     pooka 			continue;
    174   1.5     pooka 		}
    175   1.5     pooka 
    176  1.10     pooka 		if (!mpsafe)
    177  1.10     pooka 			KERNEL_LOCK(1, curlwp);
    178  1.10     pooka 		func(funarg);
    179  1.10     pooka 		if (!mpsafe)
    180  1.10     pooka 			KERNEL_UNLOCK_ONE(curlwp);
    181   1.5     pooka 	}
    182  1.20     pooka 
    183  1.20     pooka 	panic("sithread unreachable");
    184   1.5     pooka }
    185   1.2        ad 
    186  1.41     pooka static kmutex_t sithr_emtx;
    187  1.41     pooka static unsigned int sithr_est;
    188  1.41     pooka static int sithr_canest;
    189  1.41     pooka 
    190  1.41     pooka /*
    191  1.41     pooka  * Create softint handler threads when the softint for each respective
    192  1.41     pooka  * level is established for the first time.  Most rump kernels don't
    193  1.41     pooka  * need at least half of the softint levels, so on-demand saves bootstrap
    194  1.41     pooka  * time and memory resources.  Note, though, that this routine may be
    195  1.41     pooka  * called before it's possible to call kthread_create().  Creation of
    196  1.41     pooka  * those softints (SOFTINT_CLOCK, as of writing this) will be deferred
    197  1.41     pooka  * to until softint_init() is called for the main CPU.
    198  1.41     pooka  */
    199  1.41     pooka static void
    200  1.41     pooka sithread_establish(int level)
    201  1.41     pooka {
    202  1.41     pooka 	int docreate, rv;
    203  1.41     pooka 	int lvlbit = 1<<level;
    204  1.41     pooka 	int i;
    205  1.41     pooka 
    206  1.41     pooka 	KASSERT((level & ~SOFTINT_LVLMASK) == 0);
    207  1.41     pooka 	if (__predict_true(sithr_est & lvlbit))
    208  1.41     pooka 		return;
    209  1.41     pooka 
    210  1.41     pooka 	mutex_enter(&sithr_emtx);
    211  1.41     pooka 	docreate = (sithr_est & lvlbit) == 0 && sithr_canest;
    212  1.41     pooka 	sithr_est |= lvlbit;
    213  1.41     pooka 	mutex_exit(&sithr_emtx);
    214  1.41     pooka 
    215  1.41     pooka 	if (docreate) {
    216  1.41     pooka 		for (i = 0; i < ncpu_final; i++) {
    217  1.41     pooka 			if ((rv = kthread_create(PRI_NONE,
    218  1.41     pooka 			    KTHREAD_MPSAFE | KTHREAD_INTR,
    219  1.41     pooka 			    cpu_lookup(i), sithread, (void *)(uintptr_t)level,
    220  1.41     pooka 			    NULL, "rsi%d/%d", i, level)) != 0)
    221  1.41     pooka 				panic("softint thread create failed: %d", rv);
    222  1.41     pooka 		}
    223  1.41     pooka 	}
    224  1.41     pooka }
    225  1.41     pooka 
    226   1.5     pooka void
    227  1.34     pooka rump_intr_init(int numcpu)
    228  1.22     pooka {
    229  1.22     pooka 
    230  1.23     pooka 	cv_init(&lbolt, "oh kath ra");
    231  1.41     pooka 	mutex_init(&sithr_emtx, MUTEX_DEFAULT, IPL_NONE);
    232  1.34     pooka 	ncpu_final = numcpu;
    233  1.22     pooka }
    234  1.22     pooka 
    235  1.22     pooka void
    236   1.5     pooka softint_init(struct cpu_info *ci)
    237   1.2        ad {
    238  1.22     pooka 	struct cpu_data *cd = &ci->ci_data;
    239  1.22     pooka 	struct softint_lev *slev;
    240  1.20     pooka 	int rv, i;
    241   1.5     pooka 
    242  1.22     pooka 	if (!rump_threads)
    243  1.22     pooka 		return;
    244  1.22     pooka 
    245  1.41     pooka 	/* overloaded global init ... */
    246  1.25     pooka 	if (ci->ci_index == 0) {
    247  1.41     pooka 		int sithr_swap;
    248  1.41     pooka 
    249  1.25     pooka 		rumptc.tc_frequency = hz;
    250  1.25     pooka 		tc_init(&rumptc);
    251  1.41     pooka 
    252  1.41     pooka 		/* create deferred softint threads */
    253  1.41     pooka 		mutex_enter(&sithr_emtx);
    254  1.41     pooka 		sithr_swap = sithr_est;
    255  1.41     pooka 		sithr_est = 0;
    256  1.41     pooka 		sithr_canest = 1;
    257  1.41     pooka 		mutex_exit(&sithr_emtx);
    258  1.41     pooka 		for (i = 0; i < SOFTINT_COUNT; i++) {
    259  1.41     pooka 			if (sithr_swap & (1<<i))
    260  1.41     pooka 				sithread_establish(i);
    261  1.41     pooka 		}
    262  1.25     pooka 	}
    263  1.25     pooka 
    264  1.22     pooka 	slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
    265  1.20     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    266  1.22     pooka 		rumpuser_cv_init(&slev[i].si_cv);
    267  1.22     pooka 		LIST_INIT(&slev[i].si_pending);
    268  1.20     pooka 	}
    269  1.22     pooka 	cd->cpu_softcpu = slev;
    270   1.2        ad 
    271  1.41     pooka 	/* well, not really a "soft" interrupt ... */
    272  1.41     pooka 	if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
    273  1.41     pooka 	    ci, doclock, NULL, NULL, "rumpclk%d", ci->ci_index)) != 0)
    274  1.25     pooka 		panic("clock thread creation failed: %d", rv);
    275   1.2        ad }
    276   1.2        ad 
    277   1.5     pooka void *
    278   1.5     pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
    279   1.2        ad {
    280   1.5     pooka 	struct softint *si;
    281  1.32     pooka 	struct softint_percpu *sip;
    282  1.41     pooka 	int level = flags & SOFTINT_LVLMASK;
    283  1.32     pooka 	int i;
    284   1.2        ad 
    285  1.28     pooka 	si = malloc(sizeof(*si), M_TEMP, M_WAITOK);
    286   1.5     pooka 	si->si_func = func;
    287   1.5     pooka 	si->si_arg = arg;
    288  1.18     pooka 	si->si_flags = flags & SOFTINT_MPSAFE ? SI_MPSAFE : 0;
    289  1.41     pooka 	si->si_level = level;
    290  1.20     pooka 	KASSERT(si->si_level < SOFTINT_COUNT);
    291  1.34     pooka 	si->si_entry = malloc(sizeof(*si->si_entry) * ncpu_final,
    292  1.32     pooka 	    M_TEMP, M_WAITOK | M_ZERO);
    293  1.34     pooka 	for (i = 0; i < ncpu_final; i++) {
    294  1.32     pooka 		sip = &si->si_entry[i];
    295  1.32     pooka 		sip->sip_parent = si;
    296  1.32     pooka 	}
    297  1.41     pooka 	sithread_establish(level);
    298   1.5     pooka 
    299   1.5     pooka 	return si;
    300   1.2        ad }
    301   1.2        ad 
    302  1.33     pooka /*
    303  1.33     pooka  * Soft interrupts bring two choices.  If we are running with thread
    304  1.33     pooka  * support enabled, defer execution, otherwise execute in place.
    305  1.33     pooka  */
    306  1.33     pooka 
    307   1.2        ad void
    308   1.2        ad softint_schedule(void *arg)
    309   1.2        ad {
    310   1.5     pooka 	struct softint *si = arg;
    311  1.42     pooka 	struct cpu_info *ci = curcpu();
    312  1.42     pooka 	struct softint_percpu *sip = &si->si_entry[ci->ci_index];
    313  1.42     pooka 	struct cpu_data *cd = &ci->ci_data;
    314  1.22     pooka 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    315   1.2        ad 
    316   1.5     pooka 	if (!rump_threads) {
    317   1.5     pooka 		si->si_func(si->si_arg);
    318   1.5     pooka 	} else {
    319  1.32     pooka 		if (!sip->sip_onlist) {
    320  1.22     pooka 			LIST_INSERT_HEAD(&si_lvl[si->si_level].si_pending,
    321  1.32     pooka 			    sip, sip_entries);
    322  1.32     pooka 			sip->sip_onlist = true;
    323   1.5     pooka 		}
    324   1.5     pooka 	}
    325   1.2        ad }
    326   1.2        ad 
    327  1.43     rmind void
    328  1.43     rmind softint_schedule_cpu(void *arg, struct cpu_info *ci)
    329  1.43     rmind {
    330  1.44     rmind 	/*
    331  1.44     rmind 	 * TODO: implement this properly
    332  1.44     rmind 	 */
    333  1.45     rmind 	KASSERT(curcpu() == ci);
    334  1.43     rmind 	softint_schedule(arg);
    335  1.43     rmind }
    336  1.43     rmind 
    337  1.32     pooka /*
    338  1.32     pooka  * flimsy disestablish: should wait for softints to finish.
    339  1.32     pooka  */
    340  1.18     pooka void
    341  1.18     pooka softint_disestablish(void *cook)
    342  1.18     pooka {
    343  1.18     pooka 	struct softint *si = cook;
    344  1.32     pooka 	int i;
    345  1.18     pooka 
    346  1.34     pooka 	for (i = 0; i < ncpu_final; i++) {
    347  1.32     pooka 		struct softint_percpu *sip;
    348  1.32     pooka 
    349  1.32     pooka 		sip = &si->si_entry[i];
    350  1.32     pooka 		if (sip->sip_onlist) {
    351  1.32     pooka 			si->si_flags |= SI_KILLME;
    352  1.32     pooka 			return;
    353  1.32     pooka 		}
    354  1.18     pooka 	}
    355  1.32     pooka 	free(si->si_entry, M_TEMP);
    356  1.28     pooka 	free(si, M_TEMP);
    357  1.18     pooka }
    358  1.18     pooka 
    359  1.20     pooka void
    360  1.20     pooka rump_softint_run(struct cpu_info *ci)
    361  1.20     pooka {
    362  1.22     pooka 	struct cpu_data *cd = &ci->ci_data;
    363  1.22     pooka 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    364  1.20     pooka 	int i;
    365  1.20     pooka 
    366  1.22     pooka 	if (!rump_threads)
    367  1.22     pooka 		return;
    368  1.22     pooka 
    369  1.20     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    370  1.22     pooka 		if (!LIST_EMPTY(&si_lvl[i].si_pending))
    371  1.22     pooka 			rumpuser_cv_signal(si_lvl[i].si_cv);
    372  1.20     pooka 	}
    373  1.20     pooka }
    374  1.20     pooka 
    375   1.2        ad bool
    376   1.9  christos cpu_intr_p(void)
    377   1.2        ad {
    378   1.2        ad 
    379   1.2        ad 	return false;
    380   1.2        ad }
    381  1.19     pooka 
    382  1.19     pooka bool
    383  1.19     pooka cpu_softintr_p(void)
    384  1.19     pooka {
    385  1.19     pooka 
    386  1.20     pooka 	return curlwp->l_pflag & LP_INTR;
    387  1.19     pooka }
    388