This patch moves the interrupt timer logic to kvm.c and cleans it up.
Signed-off-by: Pekka Enberg <penberg@kernel.org>
#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;
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);
#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.
};
}
+#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;
#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;
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;
blk_virtio__init(kvm);
- setup_timer();
+ kvm__start_timer(kvm);
tty_set_canon_flag(fileno(stdin), 1);