]> git.karo-electronics.de Git - karo-tx-linux.git/commitdiff
kvm: Cleanup interrupt timer logic
authorPekka Enberg <penberg@kernel.org>
Tue, 11 Jan 2011 21:56:22 +0000 (23:56 +0200)
committerPekka Enberg <penberg@kernel.org>
Tue, 11 Jan 2011 21:56:22 +0000 (23:56 +0200)
This patch moves the interrupt timer logic to kvm.c and cleans it up.

Signed-off-by: Pekka Enberg <penberg@kernel.org>
tools/kvm/include/kvm/kvm.h
tools/kvm/kvm.c
tools/kvm/main.c

index 48f837306dc016d9eeb9b63ee2ea14b06f2cc368..34fcc9618ee209e443f8a5608a52813f660bfaf3 100644 (file)
@@ -7,11 +7,13 @@
 
 #include <stdbool.h>
 #include <stdint.h>
+#include <time.h>
 
 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);
index a2d8f5b7ac7edc8e6816d7a3114fbc3776c81c55..ce87ed7c6233b85d7cb34ee83a7ea130bcfb55c6 100644 (file)
@@ -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"
 
 #include <sys/ioctl.h>
 #include <inttypes.h>
 #include <sys/mman.h>
+#include <sys/stat.h>
 #include <stdbool.h>
 #include <assert.h>
 #include <limits.h>
+#include <signal.h>
 #include <stdarg.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <stdio.h>
 #include <fcntl.h>
-#include <sys/stat.h>
+#include <time.h>
 
 /*
  * 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;
index 58d63f7a994289e32d09d05a3024bd1046afb659..42857cdf018b8c63a5eda5561c83648058de3a49 100644 (file)
@@ -8,13 +8,12 @@
 
 #include <inttypes.h>
 #include <termios.h>
-#include <unistd.h>
 #include <signal.h>
 #include <stdint.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 #include <stdio.h>
-#include <time.h>
 
 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);