Home | History | Annotate | Line # | Download | only in rumpkern
intr.c revision 1.2.14.3
      1 /*	$NetBSD: intr.c,v 1.2.14.3 2009/01/17 13:29:36 mjf Exp $	*/
      2 
      3 /*
      4  * Copyright (c) 2008 Antti Kantee.  All Rights Reserved.
      5  *
      6  * Redistribution and use in source and binary forms, with or without
      7  * modification, are permitted provided that the following conditions
      8  * are met:
      9  * 1. Redistributions of source code must retain the above copyright
     10  *    notice, this list of conditions and the following disclaimer.
     11  * 2. Redistributions in binary form must reproduce the above copyright
     12  *    notice, this list of conditions and the following disclaimer in the
     13  *    documentation and/or other materials provided with the distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
     16  * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
     17  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     18  * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
     19  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     20  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
     21  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     22  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     23  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     24  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     25  * SUCH DAMAGE.
     26  */
     27 
     28 #include <sys/cdefs.h>
     29 __KERNEL_RCSID(0, "$NetBSD: intr.c,v 1.2.14.3 2009/01/17 13:29:36 mjf Exp $");
     30 
     31 #include <sys/param.h>
     32 #include <sys/cpu.h>
     33 #include <sys/kmem.h>
     34 #include <sys/kthread.h>
     35 #include <sys/intr.h>
     36 
     37 #include <rump/rumpuser.h>
     38 
     39 #include "rump_private.h"
     40 
     41 /*
     42  * Interrupt simulator.  It executes hardclock() and softintrs.
     43  */
     44 
     45 time_t time_uptime = 1;
     46 
     47 struct softint {
     48 	void (*si_func)(void *);
     49 	void *si_arg;
     50 	bool si_onlist;
     51 	bool si_mpsafe;
     52 
     53 	LIST_ENTRY(softint) si_entries;
     54 };
     55 static LIST_HEAD(, softint) si_pending = LIST_HEAD_INITIALIZER(si_pending);
     56 static kmutex_t si_mtx;
     57 static kcondvar_t si_cv;
     58 
     59 #define INTRTHREAD_DEFAULT	2
     60 #define INTRTHREAD_MAX		20
     61 static int wrkidle, wrktotal;
     62 
     63 static void sithread(void *);
     64 
     65 static void
     66 makeworker(bool bootstrap)
     67 {
     68 	int rv;
     69 
     70 	if (wrktotal > INTRTHREAD_MAX) {
     71 		/* XXX: ratecheck */
     72 		printf("maximum interrupt threads (%d) reached\n",
     73 		    INTRTHREAD_MAX);
     74 		return;
     75 	}
     76 	rv = kthread_create(PRI_NONE, 0, NULL, sithread,
     77 	    NULL, NULL, "rumpsi");
     78 	if (rv) {
     79 		if (bootstrap)
     80 			panic("intr thread creation failed %d", rv);
     81 		else
     82 			printf("intr thread creation failed %d\n", rv);
     83 	} else {
     84 		wrkidle++;
     85 		wrktotal++;
     86 	}
     87 }
     88 
     89 /*
     90  * clock "interrupt"
     91  */
     92 static void
     93 doclock(void *noarg)
     94 {
     95 	static int ticks = 0;
     96 	extern int hz;
     97 
     98 	for (;;) {
     99 		callout_hardclock();
    100 
    101 		/* XXX: will drift */
    102 		if (++ticks == hz) {
    103 			time_uptime++;
    104 			ticks = 0;
    105 		}
    106 		kpause("tickw8", false, 1, NULL);
    107 	}
    108 }
    109 
    110 /*
    111  * run a scheduled soft interrupt
    112  */
    113 static void
    114 sithread(void *arg)
    115 {
    116 	struct softint *si;
    117 	void (*func)(void *) = NULL;
    118 	void *funarg;
    119 	bool mpsafe;
    120 
    121 	mutex_enter(&si_mtx);
    122 	for (;;) {
    123 		if (!LIST_EMPTY(&si_pending)) {
    124 			si = LIST_FIRST(&si_pending);
    125 			func = si->si_func;
    126 			funarg = si->si_arg;
    127 			mpsafe = si->si_mpsafe;
    128 
    129 			si->si_onlist = false;
    130 			LIST_REMOVE(si, si_entries);
    131 		} else {
    132 			cv_wait(&si_cv, &si_mtx);
    133 			continue;
    134 		}
    135 		wrkidle--;
    136 		if (__predict_false(wrkidle == 0))
    137 			makeworker(false);
    138 		mutex_exit(&si_mtx);
    139 
    140 		if (!mpsafe)
    141 			KERNEL_LOCK(1, curlwp);
    142 		func(funarg);
    143 		if (!mpsafe)
    144 			KERNEL_UNLOCK_ONE(curlwp);
    145 
    146 		mutex_enter(&si_mtx);
    147 		wrkidle++;
    148 	}
    149 }
    150 
    151 void
    152 softint_init(struct cpu_info *ci)
    153 {
    154 	int rv;
    155 
    156 	mutex_init(&si_mtx, MUTEX_DEFAULT, IPL_NONE);
    157 	cv_init(&si_cv, "intrw8"); /* cv of temporary w8ness */
    158 
    159 	/* XXX: should have separate "wanttimer" control */
    160 	if (rump_threads) {
    161 		rv = kthread_create(PRI_NONE, 0, NULL, doclock,
    162 		    NULL, NULL, "rumpclk");
    163 		if (rv)
    164 			panic("clock thread creation failed: %d", rv);
    165 		mutex_enter(&si_mtx);
    166 		while (wrktotal < INTRTHREAD_DEFAULT) {
    167 			makeworker(true);
    168 		}
    169 		mutex_exit(&si_mtx);
    170 	}
    171 }
    172 
    173 /*
    174  * Soft interrupts bring two choices.  If we are running with thread
    175  * support enabled, defer execution, otherwise execute in place.
    176  * See softint_schedule().
    177  *
    178  * As there is currently no clear concept of when a thread finishes
    179  * work (although rump_clear_curlwp() is close), simply execute all
    180  * softints in the timer thread.  This is probably not the most
    181  * efficient method, but good enough for now.
    182  */
    183 void *
    184 softint_establish(u_int flags, void (*func)(void *), void *arg)
    185 {
    186 	struct softint *si;
    187 
    188 	si = kmem_alloc(sizeof(*si), KM_SLEEP);
    189 	si->si_func = func;
    190 	si->si_arg = arg;
    191 	si->si_onlist = false;
    192 	si->si_mpsafe = flags & SOFTINT_MPSAFE;
    193 
    194 	return si;
    195 }
    196 
    197 void
    198 softint_schedule(void *arg)
    199 {
    200 	struct softint *si = arg;
    201 
    202 	if (!rump_threads) {
    203 		si->si_func(si->si_arg);
    204 	} else {
    205 		mutex_enter(&si_mtx);
    206 		if (!si->si_onlist) {
    207 			LIST_INSERT_HEAD(&si_pending, si, si_entries);
    208 			si->si_onlist = true;
    209 		}
    210 		cv_signal(&si_cv);
    211 		mutex_exit(&si_mtx);
    212 	}
    213 }
    214 
    215 bool
    216 cpu_intr_p(void)
    217 {
    218 
    219 	return false;
    220 }
    221