From e444c4d141bf16e417f3f861659015c8e04117c7 Mon Sep 17 00:00:00 2001 From: James Morris Date: Thu, 29 Mar 2007 11:50:11 -0400 Subject: [PATCH] lguest: add initial clock event device support (WIP) Add initial support for clock event device, enabling nohz and hrtimer support. NOTE - this is incomplete and probably quite buggy. Signed-off-by: James Morris --- drivers/lguest/core.c | 17 +++++++ drivers/lguest/hypercalls.c | 4 ++ drivers/lguest/interrupts_and_traps.c | 4 -- drivers/lguest/lg.h | 8 +++ drivers/lguest/lguest.c | 77 +++++++++++++++++++++++++++------ drivers/lguest/lguest_user.c | 21 +++++++++ include/linux/lguest.h | 1 7 files changed, 114 insertions(+), 18 deletions(-) diff --git a/drivers/lguest/core.c b/drivers/lguest/core.c index d8f136a..344b455 100644 --- a/drivers/lguest/core.c +++ b/drivers/lguest/core.c @@ -393,6 +393,23 @@ int find_free_guest(void) return -1; } +void guest_clockevent(struct lguest *lg, const ktime_t __user *u) +{ + ktime_t kdelta; + + lgread(lg, &kdelta, (u32)u, sizeof(kdelta)); + + if (ktime_to_ns(kdelta) < LG_CLOCK_MIN_DELTA) { + if (printk_ratelimit()) + printk(KERN_DEBUG "%s: guest %u small delta %Ld ns\n", + __FUNCTION__, lg->guestid, ktime_to_ns(kdelta)); + + /* kick guest timer immediately */ + set_bit(0, lg->irqs_pending); + } else + hrtimer_start(&lg->hrt, kdelta, HRTIMER_MODE_REL); +} + static void adjust_pge(void *on) { if (on) diff --git a/drivers/lguest/hypercalls.c b/drivers/lguest/hypercalls.c index ae11fd0..5ea6550 100644 --- a/drivers/lguest/hypercalls.c +++ b/drivers/lguest/hypercalls.c @@ -23,6 +23,7 @@ #include #include #include +#include #include #include #include @@ -96,6 +97,9 @@ static int do_hcall(struct lguest *lg, s case LHCALL_LOAD_TLS: guest_load_tls(lg, (struct desc_struct __user*)regs->edx); break; + case LHCALL_CLOCKEVENT: + guest_clockevent(lg, (ktime_t __user *)regs->edx); + break; default: kill_guest(lg, "Bad hypercall %i\n", regs->eax); } diff --git a/drivers/lguest/interrupts_and_traps.c b/drivers/lguest/interrupts_and_traps.c index 536fc31..d5dcc08 100644 --- a/drivers/lguest/interrupts_and_traps.c +++ b/drivers/lguest/interrupts_and_traps.c @@ -84,10 +84,6 @@ void maybe_do_interrupt(struct lguest *l if (!lg->lguest_data) return; - /* If timer has changed, set timer interrupt. */ - if (jiffies != lg->last_timer) - set_bit(0, lg->irqs_pending); - /* Mask out any interrupts they have blocked. */ copy_from_user(&irqs, lg->lguest_data->interrupts, sizeof(irqs)); bitmap_andnot(irqs, lg->irqs_pending, irqs, LGUEST_IRQS); diff --git a/drivers/lguest/lg.h b/drivers/lguest/lg.h index 52ed6f3..2a6c1c6 100644 --- a/drivers/lguest/lg.h +++ b/drivers/lguest/lg.h @@ -21,6 +21,9 @@ #include "irq_vectors.h" #define GUEST_DPL 1 +#define LG_CLOCK_MIN_DELTA 100UL +#define LG_CLOCK_MAX_DELTA ULONG_MAX + struct lguest_regs { /* Manually saved part. */ @@ -173,6 +176,9 @@ struct lguest struct desc_struct idt[FIRST_EXTERNAL_VECTOR+LGUEST_IRQS]; struct desc_struct syscall_idt; + /* Virtual clock device */ + struct hrtimer hrt; + /* Pending virtual interrupts */ DECLARE_BITMAP(irqs_pending, LGUEST_IRQS); }; @@ -190,7 +196,7 @@ void lgwrite(struct lguest *lg, u32 addr int find_free_guest(void); int lguest_address_ok(const struct lguest *lg, unsigned long addr); int run_guest(struct lguest *lg, char *__user user); - +void guest_clockevent(struct lguest *lg, const ktime_t __user *u); /* interrupts_and_traps.c: */ void maybe_do_interrupt(struct lguest *lg); diff --git a/drivers/lguest/lguest.c b/drivers/lguest/lguest.c index 62e3149..2bc7165 100644 --- a/drivers/lguest/lguest.c +++ b/drivers/lguest/lguest.c @@ -50,6 +50,8 @@ #include #include #include #include +#include +#include #include #include #include @@ -62,6 +64,7 @@ #include #include #include #include +#include "lg.h" /*G:0010 Welcome to the Guest! * @@ -582,20 +585,16 @@ static unsigned long lguest_get_wallcloc return hcall(LHCALL_GET_WALLCLOCK, 0, 0, 0); } -/* This is the Guest timer interrupt (hardware interrupt 0). The Guest asks - * the Host how many jiffies have passed since last time it asked, and hands - * that to the generic do_timer() architecture. At the moment, the Host and - * Guest have to have the same HZ value or this will give strange results. - * - * The call to update_process_times() is for process accounting. The - * TIMER_READ hypercall returns the jiffies since last we asked, which is what - * do_timer wants. I copied this from some old code which has since been - * bulldozed, but it still works. - */ -static void lguest_time_irq(unsigned int irq, struct irq_desc *desc) +static struct clock_event_device lguest_clockevent; + +/* This is the Guest timer interrupt (hardware interrupt 0). */ +static void fastcall lguest_time_irq(unsigned int irq, struct irq_desc *desc) { - do_timer(hcall(LHCALL_TIMER_READ, 0, 0, 0)); - update_process_times(user_mode_vm(get_irq_regs())); + unsigned long flags; + + local_irq_save(flags); + lguest_clockevent.event_handler(&lguest_clockevent); + local_irq_restore(flags); } static cycle_t lguest_clock_read(void) @@ -621,6 +620,57 @@ static void lguest_setup_clocksource(voi clocksource_register(&lguest_clock); } +/* XXX TODO: kill host timers via hypercall */ +static void lguest_clockevent_shutdown(void) +{ + +} + +/* XXX TODO: return -ETIME if event is in the past */ +static int lguest_clockevent_set_next_event(unsigned long delta, + struct clock_event_device *evt) +{ + ktime_t kdelta = ktime_sub(evt->next_event, ktime_get()); + hcall(LHCALL_CLOCKEVENT, __pa(&kdelta), 0, 0); + return 0; +} + +static void lguest_clockevent_set_mode(enum clock_event_mode mode, + struct clock_event_device *evt) +{ + switch (mode) { + case CLOCK_EVT_MODE_UNUSED: + case CLOCK_EVT_MODE_SHUTDOWN: + lguest_clockevent_shutdown(); + break; + + case CLOCK_EVT_MODE_ONESHOT: + break; + + case CLOCK_EVT_MODE_PERIODIC: + BUG(); + } +} + +static struct clock_event_device lguest_clockevent = { + .name = "lguest", + .features = CLOCK_EVT_FEAT_ONESHOT, + .set_next_event = lguest_clockevent_set_next_event, + .set_mode = lguest_clockevent_set_mode, + .rating = INT_MAX, + .mult = 1, + .shift = 0, + .min_delta_ns = LG_CLOCK_MIN_DELTA, + .max_delta_ns = LG_CLOCK_MAX_DELTA, +}; + +/* TODO: make this per-cpu for SMP */ +static void lguest_setup_clockevent(void) +{ + lguest_clockevent.cpumask = cpumask_of_cpu(0); + clockevents_register_device(&lguest_clockevent); +} + /* At some point in the boot process, we get asked to set up our timing * infrastructure. The kernel doesn't expect timer interrupts before this, but * we cleverly initialized the "interrupts" field of lguest_data so that timer @@ -636,6 +686,7 @@ static void lguest_time_init(void) { set_irq_handler(0, lguest_time_irq); lguest_setup_clocksource(); + lguest_setup_clockevent(); hcall(LHCALL_TIMER_READ, 0, 0, 0); enable_lguest_irq(0); } diff --git a/drivers/lguest/lguest_user.c b/drivers/lguest/lguest_user.c index aedda10..afd6bf7 100644 --- a/drivers/lguest/lguest_user.c +++ b/drivers/lguest/lguest_user.c @@ -79,6 +79,25 @@ static ssize_t read(struct file *file, c return run_guest(lg, user); } +static enum hrtimer_restart clockdev_fn(struct hrtimer *timer) +{ + struct lguest *lg = container_of(timer, struct lguest, hrt); + + set_bit(0, lg->irqs_pending); + return HRTIMER_NORESTART; +} + +static void init_clockdev(struct lguest *lg) +{ + hrtimer_init(&lg->hrt, CLOCK_MONOTONIC, HRTIMER_MODE_REL); + lg->hrt.function = clockdev_fn; +} + +static void kill_clockdev(struct lguest *lg) +{ + hrtimer_cancel(&lg->hrt); +} + /*L:0020 An initialization write supplies 4 32-bit values (in addition to the * 32-bit LHREQ_INITIALIZE value). These are: * @@ -136,6 +155,7 @@ static int initialize(struct file *file, lg->tsk = current; lg->mm = get_task_mm(current); lg->last_pages = NULL; + init_clockdev(lg); mutex_unlock(&lguest_lock); file->private_data = lg; @@ -195,6 +215,7 @@ static int close(struct inode *inode, st return 0; mutex_lock(&lguest_lock); + kill_clockdev(lg); release_all_dma(lg); free_guest_pagetable(lg); mmput(lg->mm); diff --git a/include/linux/lguest.h b/include/linux/lguest.h index 71165f2..24fab08 100644 --- a/include/linux/lguest.h +++ b/include/linux/lguest.h @@ -25,6 +25,7 @@ #define LHCALL_SEND_DMA 13 #define LHCALL_SET_PTE 14 #define LHCALL_SET_PMD 15 #define LHCALL_LOAD_TLS 16 +#define LHCALL_CLOCKEVENT 17 /*G:0031 But how does our Guest contact the Host? There are two ways: the * direct way is to make a "hypercall", similar to userspace using a "syscall" -- 1.4.2.1