Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.10
      1  1.10     pooka /*	$NetBSD: intr.c,v 1.10 2008/12/18 00:12:00 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.2        ad #include <sys/param.h>
     29   1.5     pooka #include <sys/cpu.h>
     30   1.5     pooka #include <sys/kthread.h>
     31   1.2        ad #include <sys/intr.h>
     32   1.2        ad 
     33   1.5     pooka #include <rump/rumpuser.h>
     34   1.5     pooka 
     35   1.5     pooka #include "rump_private.h"
     36   1.5     pooka 
     37   1.5     pooka /*
     38   1.5     pooka  * Interrupt simulator.  It executes hardclock() and softintrs.
     39   1.5     pooka  */
     40   1.5     pooka 
     41   1.8     pooka time_t time_uptime = 1;
     42   1.8     pooka 
     43   1.5     pooka struct softint {
     44   1.5     pooka 	void (*si_func)(void *);
     45   1.5     pooka 	void *si_arg;
     46   1.5     pooka 	bool si_onlist;
     47   1.5     pooka 	bool si_mpsafe;
     48   1.5     pooka 
     49   1.5     pooka 	LIST_ENTRY(softint) si_entries;
     50   1.2        ad };
     51   1.5     pooka static LIST_HEAD(, softint) si_pending = LIST_HEAD_INITIALIZER(si_pending);
     52   1.5     pooka static kmutex_t si_mtx;
     53   1.5     pooka static kcondvar_t si_cv;
     54   1.5     pooka 
     55  1.10     pooka #define INTRTHREAD_DEFAULT	2
     56  1.10     pooka #define INTRTHREAD_MAX		20
     57  1.10     pooka static int wrkidle, wrktotal;
     58  1.10     pooka 
     59  1.10     pooka static void sithread(void *);
     60  1.10     pooka 
     61   1.5     pooka static void
     62  1.10     pooka makeworker(bool bootstrap)
     63   1.5     pooka {
     64  1.10     pooka 	int rv;
     65  1.10     pooka 
     66  1.10     pooka 	if (wrktotal > INTRTHREAD_MAX) {
     67  1.10     pooka 		/* XXX: ratecheck */
     68  1.10     pooka 		printf("maximum interrupt threads (%d) reached\n",
     69  1.10     pooka 		    INTRTHREAD_MAX);
     70  1.10     pooka 		return;
     71  1.10     pooka 	}
     72  1.10     pooka 	rv = kthread_create(PRI_NONE, 0, NULL, sithread,
     73  1.10     pooka 	    NULL, NULL, "rumpsi");
     74  1.10     pooka 	if (rv) {
     75  1.10     pooka 		if (bootstrap)
     76  1.10     pooka 			panic("intr thread creation failed %d", rv);
     77  1.10     pooka 		else
     78  1.10     pooka 			printf("intr thread creation failed %d\n", rv);
     79  1.10     pooka 	} else {
     80  1.10     pooka 		wrkidle++;
     81  1.10     pooka 		wrktotal++;
     82  1.10     pooka 	}
     83  1.10     pooka }
     84  1.10     pooka 
     85  1.10     pooka /*
     86  1.10     pooka  * clock "interrupt"
     87  1.10     pooka  */
     88  1.10     pooka static void
     89  1.10     pooka doclock(void *noarg)
     90  1.10     pooka {
     91  1.10     pooka 	static int ticks = 0;
     92  1.10     pooka 	extern int hz;
     93  1.10     pooka 
     94  1.10     pooka 	for (;;) {
     95   1.5     pooka 		callout_hardclock();
     96   1.5     pooka 
     97  1.10     pooka 		/* XXX: will drift */
     98  1.10     pooka 		if (++ticks == hz) {
     99   1.8     pooka 			time_uptime++;
    100   1.8     pooka 			ticks = 0;
    101   1.8     pooka 		}
    102  1.10     pooka 		kpause("tickw8", false, 1, NULL);
    103  1.10     pooka 	}
    104  1.10     pooka }
    105   1.8     pooka 
    106  1.10     pooka /*
    107  1.10     pooka  * run a scheduled soft interrupt
    108  1.10     pooka  */
    109  1.10     pooka static void
    110  1.10     pooka sithread(void *arg)
    111  1.10     pooka {
    112  1.10     pooka 	struct softint *si;
    113  1.10     pooka 	void (*func)(void *) = NULL;
    114  1.10     pooka 	void *funarg;
    115  1.10     pooka 	bool mpsafe;
    116  1.10     pooka 
    117  1.10     pooka 	mutex_enter(&si_mtx);
    118  1.10     pooka 	for (;;) {
    119  1.10     pooka 		if (!LIST_EMPTY(&si_pending)) {
    120   1.5     pooka 			si = LIST_FIRST(&si_pending);
    121   1.5     pooka 			func = si->si_func;
    122   1.5     pooka 			funarg = si->si_arg;
    123   1.5     pooka 			mpsafe = si->si_mpsafe;
    124   1.5     pooka 
    125   1.5     pooka 			si->si_onlist = false;
    126   1.5     pooka 			LIST_REMOVE(si, si_entries);
    127  1.10     pooka 		} else {
    128  1.10     pooka 			cv_wait(&si_cv, &si_mtx);
    129  1.10     pooka 			continue;
    130   1.5     pooka 		}
    131  1.10     pooka 		wrkidle--;
    132  1.10     pooka 		if (__predict_false(wrkidle == 0))
    133  1.10     pooka 			makeworker(false);
    134   1.5     pooka 		mutex_exit(&si_mtx);
    135   1.5     pooka 
    136  1.10     pooka 		if (!mpsafe)
    137  1.10     pooka 			KERNEL_LOCK(1, curlwp);
    138  1.10     pooka 		func(funarg);
    139  1.10     pooka 		if (!mpsafe)
    140  1.10     pooka 			KERNEL_UNLOCK_ONE(curlwp);
    141  1.10     pooka 
    142  1.10     pooka 		mutex_enter(&si_mtx);
    143  1.10     pooka 		wrkidle++;
    144   1.5     pooka 	}
    145   1.5     pooka }
    146   1.2        ad 
    147   1.5     pooka void
    148   1.5     pooka softint_init(struct cpu_info *ci)
    149   1.2        ad {
    150   1.5     pooka 	int rv;
    151   1.5     pooka 
    152   1.5     pooka 	mutex_init(&si_mtx, MUTEX_DEFAULT, IPL_NONE);
    153  1.10     pooka 	cv_init(&si_cv, "intrw8"); /* cv of temporary w8ness */
    154   1.2        ad 
    155   1.5     pooka 	/* XXX: should have separate "wanttimer" control */
    156   1.5     pooka 	if (rump_threads) {
    157  1.10     pooka 		rv = kthread_create(PRI_NONE, 0, NULL, doclock,
    158  1.10     pooka 		    NULL, NULL, "rumpclk");
    159   1.5     pooka 		if (rv)
    160  1.10     pooka 			panic("clock thread creation failed: %d", rv);
    161  1.10     pooka 		mutex_enter(&si_mtx);
    162  1.10     pooka 		while (wrktotal < INTRTHREAD_DEFAULT) {
    163  1.10     pooka 			makeworker(true);
    164  1.10     pooka 		}
    165  1.10     pooka 		mutex_exit(&si_mtx);
    166   1.2        ad 	}
    167   1.2        ad }
    168   1.2        ad 
    169   1.5     pooka /*
    170   1.5     pooka  * Soft interrupts bring two choices.  If we are running with thread
    171   1.5     pooka  * support enabled, defer execution, otherwise execute in place.
    172   1.5     pooka  * See softint_schedule().
    173   1.5     pooka  *
    174   1.5     pooka  * As there is currently no clear concept of when a thread finishes
    175   1.5     pooka  * work (although rump_clear_curlwp() is close), simply execute all
    176   1.5     pooka  * softints in the timer thread.  This is probably not the most
    177   1.5     pooka  * efficient method, but good enough for now.
    178   1.5     pooka  */
    179   1.5     pooka void *
    180   1.5     pooka softint_establish(u_int flags, void (*func)(void *), void *arg)
    181   1.2        ad {
    182   1.5     pooka 	struct softint *si;
    183   1.2        ad 
    184   1.5     pooka 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
    185   1.5     pooka 	si->si_func = func;
    186   1.5     pooka 	si->si_arg = arg;
    187   1.5     pooka 	si->si_onlist = false;
    188   1.5     pooka 	si->si_mpsafe = flags & SOFTINT_MPSAFE;
    189   1.5     pooka 
    190   1.5     pooka 	return si;
    191   1.2        ad }
    192   1.2        ad 
    193   1.2        ad void
    194   1.2        ad softint_schedule(void *arg)
    195   1.2        ad {
    196   1.5     pooka 	struct softint *si = arg;
    197   1.2        ad 
    198   1.5     pooka 	if (!rump_threads) {
    199   1.5     pooka 		si->si_func(si->si_arg);
    200   1.5     pooka 	} else {
    201   1.5     pooka 		mutex_enter(&si_mtx);
    202   1.5     pooka 		if (!si->si_onlist) {
    203   1.5     pooka 			LIST_INSERT_HEAD(&si_pending, si, si_entries);
    204   1.5     pooka 			si->si_onlist = true;
    205   1.5     pooka 		}
    206   1.5     pooka 		cv_signal(&si_cv);
    207   1.5     pooka 		mutex_exit(&si_mtx);
    208   1.5     pooka 	}
    209   1.2        ad }
    210   1.2        ad 
    211   1.2        ad bool
    212   1.9  christos cpu_intr_p(void)
    213   1.2        ad {
    214   1.2        ad 
    215   1.2        ad 	return false;
    216   1.2        ad }
    217