From 00639432fe03dcdc5b5c833a60321c4dafd4cf0f Mon Sep 17 00:00:00 2001 From: James Morris Date: Mon, 26 Mar 2007 11:45:26 -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 --- arch/i386/lguest/core.c | 22 +++++++++ arch/i386/lguest/hypercalls.c | 3 + arch/i386/lguest/interrupts_and_traps.c | 4 -- arch/i386/lguest/lg.h | 7 +++ arch/i386/lguest/lguest.c | 75 ++++++++++++++++++++++++++----- arch/i386/lguest/lguest_user.c | 21 +++++++++ include/asm-i386/lguest.h | 1 7 files changed, 116 insertions(+), 17 deletions(-) diff --git a/arch/i386/lguest/core.c b/arch/i386/lguest/core.c index bde6275..9c22dff 100644 --- a/arch/i386/lguest/core.c +++ b/arch/i386/lguest/core.c @@ -391,6 +391,28 @@ pending_dma: return sizeof(unsigned long)*2; } +static inline int kdelta_ok(ktime_t kdelta) +{ + return (ktime_to_ns(kdelta) >= LG_CLOCK_MIN_DELTA); +} + +void guest_clockevent(struct lguest *lg, const ktime_t __user *u) +{ + ktime_t kdelta; + + lhread(lg, &kdelta, (u32)u, sizeof(kdelta)); + + if (!kdelta_ok(kdelta)) { + 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/arch/i386/lguest/hypercalls.c b/arch/i386/lguest/hypercalls.c index ee3a3f4..c76ace3 100644 --- a/arch/i386/lguest/hypercalls.c +++ b/arch/i386/lguest/hypercalls.c @@ -112,6 +112,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/arch/i386/lguest/interrupts_and_traps.c b/arch/i386/lguest/interrupts_and_traps.c index 5937db2..b5918ec 100644 --- a/arch/i386/lguest/interrupts_and_traps.c +++ b/arch/i386/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/arch/i386/lguest/lg.h b/arch/i386/lguest/lg.h index 17c7c87..344a4af 100644 --- a/arch/i386/lguest/lg.h +++ b/arch/i386/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,6 +196,7 @@ void lhwrite(struct lguest *lg, u32 addr int lguest_address_ok(const struct lguest *lg, unsigned long addr); int run_guest(struct lguest *lg, char *__user user); int find_free_guest(void); +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/arch/i386/lguest/lguest.c b/arch/i386/lguest/lguest.c index 45ed034..524c34a 100644 --- a/arch/i386/lguest/lguest.c +++ b/arch/i386/lguest/lguest.c @@ -51,6 +51,7 @@ #include #include #include #include +#include #include #include #include @@ -63,6 +64,7 @@ #include #include #include #include +#include "lg.h" /*G:0010 Welcome to the Guest! * @@ -585,20 +587,15 @@ 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 has 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) @@ -624,6 +621,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 @@ -639,6 +687,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/arch/i386/lguest/lguest_user.c b/arch/i386/lguest/lguest_user.c index d71d186..e8ad22e 100644 --- a/arch/i386/lguest/lguest_user.c +++ b/arch/i386/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); +} + /* Take: pfnlimit, pgdir, start, pageoffset. */ static int initialize(struct file *file, const u32 __user *input) { @@ -120,6 +139,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; @@ -169,6 +189,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/asm-i386/lguest.h b/include/asm-i386/lguest.h index 1ea1292..ec0a3db 100644 --- a/include/asm-i386/lguest.h +++ b/include/asm-i386/lguest.h @@ -23,6 +23,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