From ce79f1ca45eedcdb890db2e0c4fe85aad0171898 Mon Sep 17 00:00:00 2001 From: Pekka Enberg Date: Tue, 11 Jan 2011 23:56:22 +0200 Subject: [PATCH] kvm: Cleanup interrupt timer logic This patch moves the interrupt timer logic to kvm.c and cleans it up. Signed-off-by: Pekka Enberg --- include/kvm/kvm.h | 3 +++ kvm.c | 46 ++++++++++++++++++++++++++++++++++++++++++++-- main.c | 45 ++------------------------------------------- 3 files changed, 49 insertions(+), 45 deletions(-) diff --git a/include/kvm/kvm.h b/include/kvm/kvm.h index 48f8373..34fcc96 100644 --- a/include/kvm/kvm.h +++ b/include/kvm/kvm.h @@ -7,11 +7,13 @@ #include #include +#include struct kvm { int sys_fd; /* For system ioctls(), i.e. /dev/kvm */ int vm_fd; /* For VM ioctls() */ int vcpu_fd; /* For VCPU ioctls() */ + timer_t timerid; /* Posix timer for interrupts */ struct kvm_run *kvm_run; struct disk_image *disk_image; @@ -40,6 +42,7 @@ bool kvm__load_kernel(struct kvm *kvm, const char *kernel_filename, const char *initrd_filename, const char *kernel_cmdline); void kvm__reset_vcpu(struct kvm *self); void kvm__setup_mem(struct kvm *self); +void kvm__start_timer(struct kvm *self); void kvm__run(struct kvm *self); void kvm__irq_line(struct kvm *self, int irq, int level); bool kvm__emulate_io(struct kvm *self, uint16_t port, void *data, int direction, int size, uint32_t count); diff --git a/kvm.c b/kvm.c index a2d8f5b..ce87ed7 100644 --- a/kvm.c +++ b/kvm.c @@ -1,7 +1,7 @@ #include "kvm/kvm.h" -#include "kvm/interrupt.h" #include "kvm/cpufeature.h" +#include "kvm/interrupt.h" #include "kvm/e820.h" #include "kvm/util.h" @@ -12,16 +12,18 @@ #include #include #include +#include #include #include #include +#include #include #include #include #include #include #include -#include +#include /* * Compatibility code. Remove this when we move to tools/kvm. @@ -606,6 +608,46 @@ void kvm__setup_mem(struct kvm *self) }; } +#define TIMER_INTERVAL_NS 1000000 /* 1 msec */ + +static void alarm_handler(int sig) +{ +} + +/* + * This function sets up a timer that's used to inject interrupts from the + * userspace hypervisor into the guest at periodical intervals. Please note + * that clock interrupt, for example, is not handled here. + */ +void kvm__start_timer(struct kvm *self) +{ + struct itimerspec its; + struct sigaction sa; + struct sigevent sev; + + sigfillset(&sa.sa_mask); + sa.sa_flags = 0; + sa.sa_handler = alarm_handler; + + sigaction(SIGALRM, &sa, NULL); + + memset(&sev, 0, sizeof(struct sigevent)); + sev.sigev_value.sival_int = 0; + sev.sigev_notify = SIGEV_SIGNAL; + sev.sigev_signo = SIGALRM; + + if (timer_create(CLOCK_REALTIME, &sev, &self->timerid) < 0) + die("timer_create()"); + + its.it_value.tv_sec = TIMER_INTERVAL_NS / 1000000000; + its.it_value.tv_nsec = TIMER_INTERVAL_NS % 1000000000; + its.it_interval.tv_sec = its.it_value.tv_sec; + its.it_interval.tv_nsec = its.it_value.tv_nsec; + + if (timer_settime(self->timerid, 0, &its, NULL) < 0) + die("timer_settime()"); +} + void kvm__run(struct kvm *self) { int err; diff --git a/main.c b/main.c index 58d63f7..42857cd 100644 --- a/main.c +++ b/main.c @@ -8,13 +8,12 @@ #include #include -#include #include #include #include #include +#include #include -#include extern bool ioport_debug; @@ -91,46 +90,6 @@ static bool option_matches(char *arg, const char *option) return !strncmp(arg, option, strlen(option)); } -#define TIMER_INTERVAL_NS 1000000 /* 1 msec */ - -static void alarm_handler(int sig) -{ -} - -/* - * This function sets up a timer that's used to inject interrupts from the - * userspace hypervisor into the guest at periodical intervals. Please note - * that clock interrupt, for example, is not handled here. - */ -static void setup_timer(void) -{ - struct itimerspec its; - struct sigaction sa; - struct sigevent sev; - timer_t timerid; - - sigfillset(&sa.sa_mask); - sa.sa_flags = 0; - sa.sa_handler = alarm_handler; - - sigaction(SIGALRM, &sa, NULL); - - memset(&sev, 0, sizeof(struct sigevent)); - sev.sigev_value.sival_int = 0; - sev.sigev_notify = SIGEV_SIGNAL; - sev.sigev_signo = SIGALRM; - - its.it_value.tv_sec = TIMER_INTERVAL_NS / 1000000000; - its.it_value.tv_nsec = TIMER_INTERVAL_NS % 1000000000; - its.it_interval.tv_sec = its.it_value.tv_sec; - its.it_interval.tv_nsec = its.it_value.tv_nsec; - if (timer_create(CLOCK_MONOTONIC, &sev, &timerid) < 0) - die("timer_create()"); - - if (timer_settime(timerid, 0, &its, NULL) < 0) - die("timer_settime()"); -} - int main(int argc, char *argv[]) { const char *kernel_filename = NULL; @@ -222,7 +181,7 @@ int main(int argc, char *argv[]) blk_virtio__init(kvm); - setup_timer(); + kvm__start_timer(kvm); tty_set_canon_flag(fileno(stdin), 1);