Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.2.18.2
      1  1.2.18.2  yamt /*	$NetBSD: intr.c,v 1.2.18.2 2009/05/04 08:14:29 yamt Exp $	*/
      2       1.2    ad 
      3  1.2.18.2  yamt /*
      4  1.2.18.2  yamt  * 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.2.18.2  yamt  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  1.2.18.2  yamt  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  1.2.18.2  yamt  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  1.2.18.2  yamt  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  1.2.18.2  yamt  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  1.2.18.2  yamt  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  1.2.18.2  yamt  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  1.2.18.2  yamt  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  1.2.18.2  yamt  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  1.2.18.2  yamt  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  1.2.18.2  yamt  * SUCH DAMAGE.
     26       1.2    ad  */
     27       1.2    ad 
     28  1.2.18.2  yamt #include <sys/cdefs.h>
     29  1.2.18.2  yamt __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.2.18.2 2009/05/04 08:14:29 yamt Exp $");
     30  1.2.18.2  yamt 
     31       1.2    ad #include <sys/param.h>
     32       1.2    ad #include <sys/cpu.h>
     33  1.2.18.2  yamt #include <sys/kmem.h>
     34  1.2.18.2  yamt #include <sys/kthread.h>
     35  1.2.18.2  yamt #include <sys/intr.h>
     36  1.2.18.2  yamt 
     37  1.2.18.2  yamt #include <rump/rumpuser.h>
     38       1.2    ad 
     39  1.2.18.2  yamt #include "rump_private.h"
     40       1.2    ad 
     41  1.2.18.2  yamt /*
     42  1.2.18.2  yamt  * Interrupt simulator.  It executes hardclock() and softintrs.
     43  1.2.18.2  yamt  */
     44  1.2.18.2  yamt 
     45  1.2.18.2  yamt time_t time_uptime = 0;
     46  1.2.18.2  yamt 
     47  1.2.18.2  yamt struct softint {
     48  1.2.18.2  yamt 	void (*si_func)(void *);
     49  1.2.18.2  yamt 	void *si_arg;
     50  1.2.18.2  yamt 	bool si_onlist;
     51  1.2.18.2  yamt 	bool si_mpsafe;
     52  1.2.18.2  yamt 
     53  1.2.18.2  yamt 	LIST_ENTRY(softint) si_entries;
     54       1.2    ad };
     55  1.2.18.2  yamt static LIST_HEAD(, softint) si_pending = LIST_HEAD_INITIALIZER(si_pending);
     56  1.2.18.2  yamt static kmutex_t si_mtx;
     57  1.2.18.2  yamt static kcondvar_t si_cv;
     58  1.2.18.2  yamt 
     59  1.2.18.2  yamt #define INTRTHREAD_DEFAULT	2
     60  1.2.18.2  yamt #define INTRTHREAD_MAX		20
     61  1.2.18.2  yamt static int wrkidle, wrktotal;
     62       1.2    ad 
     63  1.2.18.2  yamt static void sithread(void *);
     64  1.2.18.2  yamt 
     65  1.2.18.2  yamt static void
     66  1.2.18.2  yamt makeworker(bool bootstrap)
     67       1.2    ad {
     68  1.2.18.2  yamt 	int rv;
     69       1.2    ad 
     70  1.2.18.2  yamt 	if (wrktotal > INTRTHREAD_MAX) {
     71  1.2.18.2  yamt 		/* XXX: ratecheck */
     72  1.2.18.2  yamt 		printf("maximum interrupt threads (%d) reached\n",
     73  1.2.18.2  yamt 		    INTRTHREAD_MAX);
     74  1.2.18.2  yamt 		return;
     75  1.2.18.2  yamt 	}
     76  1.2.18.2  yamt 	rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE | KTHREAD_INTR, NULL,
     77  1.2.18.2  yamt 	    sithread, NULL, NULL, "rumpsi");
     78  1.2.18.2  yamt 	if (rv) {
     79  1.2.18.2  yamt 		if (bootstrap)
     80  1.2.18.2  yamt 			panic("intr thread creation failed %d", rv);
     81  1.2.18.2  yamt 		else
     82  1.2.18.2  yamt 			printf("intr thread creation failed %d\n", rv);
     83  1.2.18.2  yamt 	} else {
     84  1.2.18.2  yamt 		wrkidle++;
     85  1.2.18.2  yamt 		wrktotal++;
     86       1.2    ad 	}
     87       1.2    ad }
     88       1.2    ad 
     89  1.2.18.2  yamt /* rumpuser structures since we call rumpuser interfaces directly */
     90  1.2.18.2  yamt static struct rumpuser_cv *clockcv;
     91  1.2.18.2  yamt static struct rumpuser_mtx *clockmtx;
     92  1.2.18.2  yamt static struct timespec clockbase, clockup;
     93  1.2.18.2  yamt static unsigned clkgen;
     94  1.2.18.2  yamt 
     95       1.2    ad void
     96  1.2.18.2  yamt rump_getuptime(struct timespec *ts)
     97       1.2    ad {
     98  1.2.18.2  yamt 	int startgen, i = 0;
     99       1.2    ad 
    100  1.2.18.2  yamt 	do {
    101  1.2.18.2  yamt 		startgen = clkgen;
    102  1.2.18.2  yamt 		if (__predict_false(i++ > 10)) {
    103  1.2.18.2  yamt 			yield();
    104  1.2.18.2  yamt 			i = 0;
    105  1.2.18.2  yamt 		}
    106  1.2.18.2  yamt 		*ts = clockup;
    107  1.2.18.2  yamt 	} while (startgen != clkgen || clkgen % 2 != 0);
    108  1.2.18.2  yamt }
    109  1.2.18.2  yamt 
    110  1.2.18.2  yamt void
    111  1.2.18.2  yamt rump_gettime(struct timespec *ts)
    112  1.2.18.2  yamt {
    113  1.2.18.2  yamt 	struct timespec ts_up;
    114  1.2.18.2  yamt 
    115  1.2.18.2  yamt 	rump_getuptime(&ts_up);
    116  1.2.18.2  yamt 	timespecadd(&clockbase, &ts_up, ts);
    117  1.2.18.2  yamt }
    118  1.2.18.2  yamt 
    119  1.2.18.2  yamt /*
    120  1.2.18.2  yamt  * clock "interrupt"
    121  1.2.18.2  yamt  */
    122  1.2.18.2  yamt static void
    123  1.2.18.2  yamt doclock(void *noarg)
    124  1.2.18.2  yamt {
    125  1.2.18.2  yamt 	struct timespec tick, curtime;
    126  1.2.18.2  yamt 	uint64_t sec, nsec;
    127  1.2.18.2  yamt 	int ticks = 0, error;
    128  1.2.18.2  yamt 	extern int hz;
    129  1.2.18.2  yamt 
    130  1.2.18.2  yamt 	rumpuser_gettime(&sec, &nsec, &error);
    131  1.2.18.2  yamt 	clockbase.tv_sec = sec;
    132  1.2.18.2  yamt 	clockbase.tv_nsec = nsec;
    133  1.2.18.2  yamt 	curtime = clockbase;
    134  1.2.18.2  yamt 	tick.tv_sec = 0;
    135  1.2.18.2  yamt 	tick.tv_nsec = 1000000000/hz;
    136  1.2.18.2  yamt 
    137  1.2.18.2  yamt 	rumpuser_mutex_enter(clockmtx);
    138  1.2.18.2  yamt 	rumpuser_cv_signal(clockcv);
    139  1.2.18.2  yamt 
    140  1.2.18.2  yamt 	for (;;) {
    141  1.2.18.2  yamt 		callout_hardclock();
    142  1.2.18.2  yamt 
    143  1.2.18.2  yamt 		if (++ticks == hz) {
    144  1.2.18.2  yamt 			time_uptime++;
    145  1.2.18.2  yamt 			ticks = 0;
    146  1.2.18.2  yamt 		}
    147  1.2.18.2  yamt 
    148  1.2.18.2  yamt 		/* wait until the next tick. XXX: what if the clock changes? */
    149  1.2.18.2  yamt 		while (rumpuser_cv_timedwait(clockcv, clockmtx,
    150  1.2.18.2  yamt 		    &curtime) != EWOULDBLOCK)
    151  1.2.18.2  yamt 			continue;
    152  1.2.18.2  yamt 
    153  1.2.18.2  yamt 		clkgen++;
    154  1.2.18.2  yamt 		timespecadd(&clockup, &tick, &clockup);
    155  1.2.18.2  yamt 		clkgen++;
    156  1.2.18.2  yamt 		timespecadd(&clockup, &clockbase, &curtime);
    157  1.2.18.2  yamt 	}
    158  1.2.18.2  yamt }
    159  1.2.18.2  yamt 
    160  1.2.18.2  yamt /*
    161  1.2.18.2  yamt  * run a scheduled soft interrupt
    162  1.2.18.2  yamt  */
    163  1.2.18.2  yamt static void
    164  1.2.18.2  yamt sithread(void *arg)
    165  1.2.18.2  yamt {
    166  1.2.18.2  yamt 	struct softint *si;
    167  1.2.18.2  yamt 	void (*func)(void *) = NULL;
    168  1.2.18.2  yamt 	void *funarg;
    169  1.2.18.2  yamt 	bool mpsafe;
    170  1.2.18.2  yamt 
    171  1.2.18.2  yamt 	mutex_enter(&si_mtx);
    172  1.2.18.2  yamt 	for (;;) {
    173  1.2.18.2  yamt 		if (!LIST_EMPTY(&si_pending)) {
    174  1.2.18.2  yamt 			si = LIST_FIRST(&si_pending);
    175  1.2.18.2  yamt 			func = si->si_func;
    176  1.2.18.2  yamt 			funarg = si->si_arg;
    177  1.2.18.2  yamt 			mpsafe = si->si_mpsafe;
    178  1.2.18.2  yamt 
    179  1.2.18.2  yamt 			si->si_onlist = false;
    180  1.2.18.2  yamt 			LIST_REMOVE(si, si_entries);
    181  1.2.18.2  yamt 		} else {
    182  1.2.18.2  yamt 			cv_wait(&si_cv, &si_mtx);
    183  1.2.18.2  yamt 			continue;
    184  1.2.18.2  yamt 		}
    185  1.2.18.2  yamt 		wrkidle--;
    186  1.2.18.2  yamt 		if (__predict_false(wrkidle == 0))
    187  1.2.18.2  yamt 			makeworker(false);
    188  1.2.18.2  yamt 		mutex_exit(&si_mtx);
    189  1.2.18.2  yamt 
    190  1.2.18.2  yamt 		if (!mpsafe)
    191  1.2.18.2  yamt 			KERNEL_LOCK(1, curlwp);
    192  1.2.18.2  yamt 		func(funarg);
    193  1.2.18.2  yamt 		if (!mpsafe)
    194  1.2.18.2  yamt 			KERNEL_UNLOCK_ONE(curlwp);
    195  1.2.18.2  yamt 
    196  1.2.18.2  yamt 		mutex_enter(&si_mtx);
    197  1.2.18.2  yamt 		wrkidle++;
    198  1.2.18.2  yamt 	}
    199  1.2.18.2  yamt }
    200  1.2.18.2  yamt 
    201  1.2.18.2  yamt void
    202  1.2.18.2  yamt softint_init(struct cpu_info *ci)
    203  1.2.18.2  yamt {
    204  1.2.18.2  yamt 	int rv;
    205  1.2.18.2  yamt 
    206  1.2.18.2  yamt 	mutex_init(&si_mtx, MUTEX_DEFAULT, IPL_NONE);
    207  1.2.18.2  yamt 	cv_init(&si_cv, "intrw8"); /* cv of temporary w8ness */
    208  1.2.18.2  yamt 
    209  1.2.18.2  yamt 	rumpuser_cv_init(&clockcv);
    210  1.2.18.2  yamt 	rumpuser_mutex_init(&clockmtx);
    211  1.2.18.2  yamt 
    212  1.2.18.2  yamt 	/* XXX: should have separate "wanttimer" control */
    213  1.2.18.2  yamt 	if (rump_threads) {
    214  1.2.18.2  yamt 		rumpuser_mutex_enter(clockmtx);
    215  1.2.18.2  yamt 		rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE, NULL, doclock,
    216  1.2.18.2  yamt 		    NULL, NULL, "rumpclk");
    217  1.2.18.2  yamt 		if (rv)
    218  1.2.18.2  yamt 			panic("clock thread creation failed: %d", rv);
    219  1.2.18.2  yamt 		mutex_enter(&si_mtx);
    220  1.2.18.2  yamt 		while (wrktotal < INTRTHREAD_DEFAULT) {
    221  1.2.18.2  yamt 			makeworker(true);
    222  1.2.18.2  yamt 		}
    223  1.2.18.2  yamt 		mutex_exit(&si_mtx);
    224  1.2.18.2  yamt 
    225  1.2.18.2  yamt 		/* make sure we have a clocktime before returning */
    226  1.2.18.2  yamt 		rumpuser_cv_wait(clockcv, clockmtx);
    227  1.2.18.2  yamt 		rumpuser_mutex_exit(clockmtx);
    228  1.2.18.2  yamt 	}
    229  1.2.18.2  yamt }
    230  1.2.18.2  yamt 
    231  1.2.18.2  yamt /*
    232  1.2.18.2  yamt  * Soft interrupts bring two choices.  If we are running with thread
    233  1.2.18.2  yamt  * support enabled, defer execution, otherwise execute in place.
    234  1.2.18.2  yamt  * See softint_schedule().
    235  1.2.18.2  yamt  *
    236  1.2.18.2  yamt  * As there is currently no clear concept of when a thread finishes
    237  1.2.18.2  yamt  * work (although rump_clear_curlwp() is close), simply execute all
    238  1.2.18.2  yamt  * softints in the timer thread.  This is probably not the most
    239  1.2.18.2  yamt  * efficient method, but good enough for now.
    240  1.2.18.2  yamt  */
    241  1.2.18.2  yamt void *
    242  1.2.18.2  yamt softint_establish(u_int flags, void (*func)(void *), void *arg)
    243  1.2.18.2  yamt {
    244  1.2.18.2  yamt 	struct softint *si;
    245  1.2.18.2  yamt 
    246  1.2.18.2  yamt 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
    247  1.2.18.2  yamt 	si->si_func = func;
    248  1.2.18.2  yamt 	si->si_arg = arg;
    249  1.2.18.2  yamt 	si->si_onlist = false;
    250  1.2.18.2  yamt 	si->si_mpsafe = flags & SOFTINT_MPSAFE;
    251  1.2.18.2  yamt 
    252  1.2.18.2  yamt 	return si;
    253       1.2    ad }
    254       1.2    ad 
    255       1.2    ad void
    256       1.2    ad softint_schedule(void *arg)
    257       1.2    ad {
    258  1.2.18.2  yamt 	struct softint *si = arg;
    259       1.2    ad 
    260  1.2.18.2  yamt 	if (!rump_threads) {
    261  1.2.18.2  yamt 		si->si_func(si->si_arg);
    262  1.2.18.2  yamt 	} else {
    263  1.2.18.2  yamt 		mutex_enter(&si_mtx);
    264  1.2.18.2  yamt 		if (!si->si_onlist) {
    265  1.2.18.2  yamt 			LIST_INSERT_HEAD(&si_pending, si, si_entries);
    266  1.2.18.2  yamt 			si->si_onlist = true;
    267  1.2.18.2  yamt 		}
    268  1.2.18.2  yamt 		cv_signal(&si_cv);
    269  1.2.18.2  yamt 		mutex_exit(&si_mtx);
    270  1.2.18.2  yamt 	}
    271       1.2    ad }
    272       1.2    ad 
    273       1.2    ad bool
    274       1.2    ad cpu_intr_p(void)
    275       1.2    ad {
    276       1.2    ad 
    277       1.2    ad 	return false;
    278       1.2    ad }
    279