/*
* Copyright (c) 2008-2010, 2015 Antti Kantee. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
/* generate dummy clockframe for hardclock to consume */
clkframe = rump_cpu_makeclockframe();
for (;;) {
int lbolt_ticks = 0;
hardclock(clkframe);
if (CPU_IS_PRIMARY(ci)) {
if (++lbolt_ticks >= hz) {
lbolt_ticks = 0;
cv_broadcast(&lbolt);
}
}
error = rumpuser_clock_sleep(RUMPUSER_CLOCK_ABSMONO,
curclock.tv_sec, curclock.tv_nsec);
if (error) {
panic("rumpuser_clock_sleep failed with error %d",
error);
}
timespecadd(&curclock, &thetick, &curclock);
}
}
/*
* Soft interrupt execution thread. This thread is pinned to the
* same CPU that scheduled the interrupt, so we don't need to do
* lock against si_lvl.
*/
static void
sithread(void *arg)
{
struct softint_percpu *sip;
struct softint *si;
void (*func)(void *) = NULL;
void *funarg;
bool mpsafe;
int mylevel = (uintptr_t)arg;
struct softint_lev *si_lvlp, *si_lvl;
struct cpu_data *cd = &curcpu()->ci_data;
/*
* ok, now figure out which cpu we need the softint to
* be handled on
*/
si = sip->sip_parent;
cidx = sip - si->si_entry;
ci = cpu_lookup(cidx);
me->l_target_cpu = ci;
/* schedule ourselves there, and then schedule the softint */
rump_schedule();
KASSERT(curcpu() == ci);
softint_schedule(si);
rump_unschedule();
}
panic("sithread_cpu_bouncer unreasonable");
}
static kmutex_t sithr_emtx;
static unsigned int sithr_est;
static int sithr_canest;
/*
* Create softint handler threads when the softint for each respective
* level is established for the first time. Most rump kernels don't
* need at least half of the softint levels, so on-demand saves bootstrap
* time and memory resources. Note, though, that this routine may be
* called before it's possible to call kthread_create(). Creation of
* those softints (SOFTINT_CLOCK, as of writing this) will be deferred
* to until softint_init() is called for the main CPU.
*/
static void
sithread_establish(int level)
{
int docreate, rv;
int lvlbit = 1<<level;
int i;
KASSERT((level & ~SOFTINT_LVLMASK) == 0);
if (__predict_true(sithr_est & lvlbit))
return;
slev = kmem_alloc(sizeof(struct softint_lev) * SOFTINT_COUNT, KM_SLEEP);
for (i = 0; i < SOFTINT_COUNT; i++) {
rumpuser_cv_init(&slev[i].si_cv);
TAILQ_INIT(&slev[i].si_pending);
}
cd->cpu_softcpu = slev;
/* overloaded global init ... */
/* XXX: should be done the last time we are called */
if (ci->ci_index == 0) {
int sithr_swap;
/* pretend that we have our own for these */
stathz = 1;
schedhz = 1;
profhz = 1;
initclocks();
/* create deferred softint threads */
mutex_enter(&sithr_emtx);
sithr_swap = sithr_est;
sithr_est = 0;
sithr_canest = 1;
mutex_exit(&sithr_emtx);
for (i = 0; i < SOFTINT_COUNT; i++) {
if (sithr_swap & (1<<i))
sithread_establish(i);
}
}
/* well, not really a "soft" interrupt ... */
if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
ci, doclock, NULL, NULL, "rumpclk%d", ci->ci_index)) != 0)
panic("clock thread creation failed: %d", rv);
/* not one either, but at least a softint helper */
if (CPU_IS_PRIMARY(ci)) {
rumpuser_mutex_init(&sicpumtx, RUMPUSER_MTX_SPIN);
rumpuser_cv_init(&sicpucv);
if ((rv = kthread_create(PRI_NONE, KTHREAD_MPSAFE,
NULL, sithread_cpu_bouncer, NULL, NULL, "sipbnc")) != 0)
panic("softint cpu bouncer creation failed: %d", rv);
}
}
if (!rump_threads) {
si->si_func(si->si_arg);
} else {
if (!sip->sip_onlist) {
TAILQ_INSERT_TAIL(&si_lvl[si->si_level].si_pending,
sip, sip_entries);
sip->sip_onlist = true;
}
}
}
/*
* Like softint_schedule(), except schedule softint to be handled on
* the core designated by ci_tgt instead of the core the call is made on.
*
* Unlike softint_schedule(), the performance is not important
* (unless ci_tgt == curcpu): high-performance rump kernel I/O stacks
* should arrange data to already be on the right core at the driver
* layer.
*/
void
softint_schedule_cpu(void *arg, struct cpu_info *ci_tgt)
{
struct softint *si = arg;
struct cpu_info *ci_cur = curcpu();
struct softint_percpu *sip;
KASSERT(rump_threads);
/* preferred case (which can be optimized some day) */
if (ci_cur == ci_tgt) {
softint_schedule(si);
return;
}
/*
* no? then it's softint turtles all the way down
*/