Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.36.4.1
      1  1.36.4.1      yamt /*	$NetBSD: intr.c,v 1.36.4.1 2014/05/22 11:41:15 yamt 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.36.4.1      yamt __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.36.4.1 2014/05/22 11:41:15 yamt 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.36.4.1      yamt 	struct timespec thetick, curclock;
    103  1.36.4.1      yamt 	int64_t sec;
    104  1.36.4.1      yamt 	long nsec;
    105      1.24     pooka 	int error;
    106  1.36.4.1      yamt 	int cpuindx = curcpu()->ci_index;
    107      1.10     pooka 	extern int hz;
    108      1.14     pooka 
    109  1.36.4.1      yamt 	error = rumpuser_clock_gettime(RUMPUSER_CLOCK_ABSMONO, &sec, &nsec);
    110  1.36.4.1      yamt 	if (error)
    111  1.36.4.1      yamt 		panic("clock: cannot get monotonic time");
    112  1.36.4.1      yamt 
    113  1.36.4.1      yamt 	curclock.tv_sec = sec;
    114  1.36.4.1      yamt 	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.36.4.1      yamt 		error = rumpuser_clock_sleep(RUMPUSER_CLOCK_ABSMONO,
    122  1.36.4.1      yamt 		    curclock.tv_sec, curclock.tv_nsec);
    123  1.36.4.1      yamt 		KASSERT(!error);
    124  1.36.4.1      yamt 		timespecadd(&curclock, &thetick, &curclock);
    125      1.26     pooka 
    126  1.36.4.1      yamt 		if (cpuindx != 0)
    127      1.25     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.36.4.1      yamt static kmutex_t sithr_emtx;
    187  1.36.4.1      yamt static unsigned int sithr_est;
    188  1.36.4.1      yamt static int sithr_canest;
    189  1.36.4.1      yamt 
    190  1.36.4.1      yamt /*
    191  1.36.4.1      yamt  * Create softint handler threads when the softint for each respective
    192  1.36.4.1      yamt  * level is established for the first time.  Most rump kernels don't
    193  1.36.4.1      yamt  * need at least half of the softint levels, so on-demand saves bootstrap
    194  1.36.4.1      yamt  * time and memory resources.  Note, though, that this routine may be
    195  1.36.4.1      yamt  * called before it's possible to call kthread_create().  Creation of
    196  1.36.4.1      yamt  * those softints (SOFTINT_CLOCK, as of writing this) will be deferred
    197  1.36.4.1      yamt  * to until softint_init() is called for the main CPU.
    198  1.36.4.1      yamt  */
    199  1.36.4.1      yamt static void
    200  1.36.4.1      yamt sithread_establish(int level)
    201  1.36.4.1      yamt {
    202  1.36.4.1      yamt 	int docreate, rv;
    203  1.36.4.1      yamt 	int lvlbit = 1<<level;
    204  1.36.4.1      yamt 	int i;
    205  1.36.4.1      yamt 
    206  1.36.4.1      yamt 	KASSERT((level & ~SOFTINT_LVLMASK) == 0);
    207  1.36.4.1      yamt 	if (__predict_true(sithr_est & lvlbit))
    208  1.36.4.1      yamt 		return;
    209  1.36.4.1      yamt 
    210  1.36.4.1      yamt 	mutex_enter(&sithr_emtx);
    211  1.36.4.1      yamt 	docreate = (sithr_est & lvlbit) == 0 && sithr_canest;
    212  1.36.4.1      yamt 	sithr_est |= lvlbit;
    213  1.36.4.1      yamt 	mutex_exit(&sithr_emtx);
    214  1.36.4.1      yamt 
    215  1.36.4.1      yamt 	if (docreate) {
    216  1.36.4.1      yamt 		for (i = 0; i < ncpu_final; i++) {
    217  1.36.4.1      yamt 			if ((rv = kthread_create(PRI_NONE,
    218  1.36.4.1      yamt 			    KTHREAD_MPSAFE | KTHREAD_INTR,
    219  1.36.4.1      yamt 			    cpu_lookup(i), sithread, (void *)(uintptr_t)level,
    220  1.36.4.1      yamt 			    NULL, "rsi%d/%d", i, level)) != 0)
    221  1.36.4.1      yamt 				panic("softint thread create failed: %d", rv);
    222  1.36.4.1      yamt 		}
    223  1.36.4.1      yamt 	}
    224  1.36.4.1      yamt }
    225  1.36.4.1      yamt 
    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.36.4.1      yamt 	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.36.4.1      yamt 	/* overloaded global init ... */
    246      1.25     pooka 	if (ci->ci_index == 0) {
    247  1.36.4.1      yamt 		int sithr_swap;
    248  1.36.4.1      yamt 
    249      1.25     pooka 		rumptc.tc_frequency = hz;
    250      1.25     pooka 		tc_init(&rumptc);
    251  1.36.4.1      yamt 
    252  1.36.4.1      yamt 		/* create deferred softint threads */
    253  1.36.4.1      yamt 		mutex_enter(&sithr_emtx);
    254  1.36.4.1      yamt 		sithr_swap = sithr_est;
    255  1.36.4.1      yamt 		sithr_est = 0;
    256  1.36.4.1      yamt 		sithr_canest = 1;
    257  1.36.4.1      yamt 		mutex_exit(&sithr_emtx);
    258  1.36.4.1      yamt 		for (i = 0; i < SOFTINT_COUNT; i++) {
    259  1.36.4.1      yamt 			if (sithr_swap & (1<<i))
    260  1.36.4.1      yamt 				sithread_establish(i);
    261  1.36.4.1      yamt 		}
    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.36.4.1      yamt 	/* well, not really a "soft" interrupt ... */
    272  1.36.4.1      yamt 	if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
    273  1.36.4.1      yamt 	    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.36.4.1      yamt 	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.36.4.1      yamt 	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.36.4.1      yamt 	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.36.4.1      yamt 	struct cpu_info *ci = curcpu();
    312  1.36.4.1      yamt 	struct softint_percpu *sip = &si->si_entry[ci->ci_index];
    313  1.36.4.1      yamt 	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.32     pooka /*
    328      1.32     pooka  * flimsy disestablish: should wait for softints to finish.
    329      1.32     pooka  */
    330      1.18     pooka void
    331      1.18     pooka softint_disestablish(void *cook)
    332      1.18     pooka {
    333      1.18     pooka 	struct softint *si = cook;
    334      1.32     pooka 	int i;
    335      1.18     pooka 
    336      1.34     pooka 	for (i = 0; i < ncpu_final; i++) {
    337      1.32     pooka 		struct softint_percpu *sip;
    338      1.32     pooka 
    339      1.32     pooka 		sip = &si->si_entry[i];
    340      1.32     pooka 		if (sip->sip_onlist) {
    341      1.32     pooka 			si->si_flags |= SI_KILLME;
    342      1.32     pooka 			return;
    343      1.32     pooka 		}
    344      1.18     pooka 	}
    345      1.32     pooka 	free(si->si_entry, M_TEMP);
    346      1.28     pooka 	free(si, M_TEMP);
    347      1.18     pooka }
    348      1.18     pooka 
    349      1.20     pooka void
    350      1.20     pooka rump_softint_run(struct cpu_info *ci)
    351      1.20     pooka {
    352      1.22     pooka 	struct cpu_data *cd = &ci->ci_data;
    353      1.22     pooka 	struct softint_lev *si_lvl = cd->cpu_softcpu;
    354      1.20     pooka 	int i;
    355      1.20     pooka 
    356      1.22     pooka 	if (!rump_threads)
    357      1.22     pooka 		return;
    358      1.22     pooka 
    359      1.20     pooka 	for (i = 0; i < SOFTINT_COUNT; i++) {
    360      1.22     pooka 		if (!LIST_EMPTY(&si_lvl[i].si_pending))
    361      1.22     pooka 			rumpuser_cv_signal(si_lvl[i].si_cv);
    362      1.20     pooka 	}
    363      1.20     pooka }
    364      1.20     pooka 
    365       1.2        ad bool
    366       1.9  christos cpu_intr_p(void)
    367       1.2        ad {
    368       1.2        ad 
    369       1.2        ad 	return false;
    370       1.2        ad }
    371      1.19     pooka 
    372      1.19     pooka bool
    373      1.19     pooka cpu_softintr_p(void)
    374      1.19     pooka {
    375      1.19     pooka 
    376      1.20     pooka 	return curlwp->l_pflag & LP_INTR;
    377      1.19     pooka }
    378