[PATCH 16/16] qemu-kvm: Use upstream kvm_init

Jan Kiszka
2011-05-13T12:14:16Z

Fold remaining qemu-kvm specific initialization bits into upsteam
kvm_init and use that service.

Signed-off-by: Jan Kiszka <jan.kiszka@siemens.com>
---
kvm-all.c | 17 +++---
kvm.h | 4 --
qemu-kvm-x86.c | 2 +-
qemu-kvm.c | 156 +----------------------------------------------------
qemu-kvm.h | 13 -----
target-i386/kvm.c | 2 -
6 files changed, 12 insertions(+), 182 deletions(-)

diff --git a/kvm-all.c b/kvm-all.c
index 8ded95c..6b35ea5 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -93,16 +93,12 @@ struct KVMState

KVMState *kvm_state;

-#ifdef OBSOLETE_KVM_IMPL
-
static const KVMCapabilityInfo kvm_required_capabilites[] = {
KVM_CAP_INFO(USER_MEMORY),
KVM_CAP_INFO(DESTROY_MEMORY_REGION_WORKS),
KVM_CAP_LAST_INFO
};

-#endif
-
static KVMSlot *kvm_alloc_slot(KVMState *s)
{
int i;
@@ -512,7 +508,6 @@ static int kvm_check_many_ioeventfds(void)
#endif
}

-#ifdef OBSOLETE_KVM_IMPL
static const KVMCapabilityInfo *
kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
{
@@ -524,7 +519,6 @@ kvm_check_extension_list(KVMState *s, const KVMCapabilityInfo *list)
}
return NULL;
}
-#endif

static void kvm_set_phys_mem(target_phys_addr_t start_addr, ram_addr_t size,
ram_addr_t phys_offset)
@@ -693,7 +687,6 @@ static void kvm_handle_interrupt(CPUState *env, int mask)
kvm_update_interrupt_request(env);
}

-#ifdef OBSOLETE_KVM_IMPL
int kvm_init(void)
{
static const char upgrade_note[] =
@@ -797,6 +790,8 @@ int kvm_init(void)
s->pit_state2 = kvm_check_extension(s, KVM_CAP_PIT_STATE2);
#endif

+ s->pit_in_kernel = kvm_pit;
+
ret = kvm_arch_init(s);
if (ret < 0) {
goto err;
@@ -807,6 +802,13 @@ int kvm_init(void)

s->many_ioeventfds = kvm_check_many_ioeventfds();

+ ret = kvm_create_irqchip(s);
+ if (ret < 0) {
+ return ret;
+ }
+
+ kvm_init_ap();
+
cpu_interrupt_handler = kvm_handle_interrupt;

return 0;
@@ -824,7 +826,6 @@ err:

return ret;
}
-#endif

static void kvm_handle_io(uint16_t port, void *data, int direction, int size,
uint32_t count)
diff --git a/kvm.h b/kvm.h
index 75dc7cf..288a452 100644
--- a/kvm.h
+++ b/kvm.h
@@ -30,7 +30,6 @@ extern int kvm_allowed;
#define kvm_enabled() (0)
#endif

-#ifdef OBSOLETE_KVM_IMPL
struct kvm_run;

typedef struct KVMCapabilityInfo {
@@ -44,7 +43,6 @@ typedef struct KVMCapabilityInfo {
/* external API */

int kvm_init(void);
-#endif /* OBSOLETE_KVM_IMPL */

int kvm_has_sync_mmu(void);
int kvm_has_vcpu_events(void);
@@ -99,9 +97,7 @@ int kvm_vcpu_ioctl(CPUState *env, int type, ...);

/* Arch specific hooks */

-#ifdef OBSOLETE_KVM_IMPL
extern const KVMCapabilityInfo kvm_arch_required_capabilities[];
-#endif

void kvm_arch_pre_run(CPUState *env, struct kvm_run *run);
void kvm_arch_post_run(CPUState *env, struct kvm_run *run);
diff --git a/qemu-kvm-x86.c b/qemu-kvm-x86.c
index 2aa833f..b009251 100644
--- a/qemu-kvm-x86.c
+++ b/qemu-kvm-x86.c
@@ -28,7 +28,7 @@ static int kvm_create_pit(KVMState *s)
#ifdef KVM_CAP_PIT
int r;

- if (kvm_pit_in_kernel()) {
+ if (kvm_pit) {
r = kvm_vm_ioctl(s, KVM_CREATE_PIT);
if (r < 0) {
fprintf(stderr, "Create kernel PIC irqchip failed\n");
diff --git a/qemu-kvm.c b/qemu-kvm.c
index 5ba5b0a..c9ff5fe 100644
--- a/qemu-kvm.c
+++ b/qemu-kvm.c
@@ -120,82 +120,6 @@ static inline void clear_gsi(KVMState *s, unsigned int gsi)
}
}

-static int kvm_create_context(void);
-
-int kvm_init(void)
-{
- int fd;
- int r, i;
-
-
- fd = open("/dev/kvm", O_RDWR);
- if (fd == -1) {
- perror("open /dev/kvm");
- return -1;
- }
- r = ioctl(fd, KVM_GET_API_VERSION, 0);
- if (r == -1) {
- fprintf(stderr,
- "kvm kernel version too old: "
- "KVM_GET_API_VERSION ioctl not supported\n");
- goto out_close;
- }
- if (r < EXPECTED_KVM_API_VERSION) {
- fprintf(stderr, "kvm kernel version too old: "
- "We expect API version %d or newer, but got "
- "version %d\n", EXPECTED_KVM_API_VERSION, r);
- goto out_close;
- }
- if (r > EXPECTED_KVM_API_VERSION) {
- fprintf(stderr, "kvm userspace version too old\n");
- goto out_close;
- }
- kvm_state = qemu_mallocz(sizeof(*kvm_state));
-
- kvm_state->fd = fd;
- kvm_state->vmfd = -1;
-
-#ifdef KVM_CAP_SET_GUEST_DEBUG
- QTAILQ_INIT(&kvm_state->kvm_sw_breakpoints);
-#endif
-
- for (i = 0; i < ARRAY_SIZE(kvm_state->slots); i++) {
- kvm_state->slots[i].slot = i;
- }
-
-#ifdef KVM_CAP_USER_MEMORY
- r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_USER_MEMORY);
- if (r <= 0) {
- fprintf(stderr,
- "Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported\n");
- goto out_close;
- }
-#else
-#error Hypervisor too old: KVM_CAP_USER_MEMORY extension not supported
-#endif
-
- cpu_register_phys_memory_client(&kvm_cpu_phys_memory_client);
-
- pthread_mutex_lock(&qemu_mutex);
- return kvm_create_context();
-
- out_close:
- close(fd);
- return -1;
-}
-
-static void kvm_finalize(KVMState *s)
-{
- /* FIXME
- if (kvm->vcpu_fd[0] != -1)
- close(kvm->vcpu_fd[0]);
- if (kvm->vm_fd != -1)
- close(kvm->vm_fd);
- */
- close(s->fd);
- free(s);
-}
-
static int kvm_init_irq_routing(KVMState *s)
{
#ifdef KVM_CAP_IRQ_ROUTING
@@ -1225,6 +1149,8 @@ int kvm_init_ap(void)
{
struct sigaction action;

+ pthread_mutex_lock(&qemu_mutex);
+
qemu_add_vm_change_state_handler(kvm_vm_state_change_handler, NULL);

signal(SIG_IPI, sig_ipi_handler);
@@ -1336,84 +1262,6 @@ int kvm_arch_init_irq_routing(void)
}
#endif

-static int kvm_create_context(void)
-{
- static const char upgrade_note[] =
- "Please upgrade to at least kernel 2.6.29 or recent kvm-kmod\n"
- "(see http://sourceforge.net/projects/kvm).\n";
-
- int r;
-
- kvm_state->pit_in_kernel = kvm_pit;
-
- kvm_state->vmfd = kvm_ioctl(kvm_state, KVM_CREATE_VM, 0);
- if (kvm_state->vmfd < 0) {
- fprintf(stderr, "kvm_create_vm: %m\n");
- kvm_finalize(kvm_state);
- return -1;
- }
-
- r = kvm_arch_init(kvm_state);
- if (r < 0) {
- kvm_finalize(kvm_state);
- return r;
- }
-
- /* There was a nasty bug in < kvm-80 that prevents memory slots from being
- * destroyed properly. Since we rely on this capability, refuse to work
- * with any kernel without this capability. */
- if (!kvm_check_extension(kvm_state, KVM_CAP_DESTROY_MEMORY_REGION_WORKS)) {
- fprintf(stderr,
- "KVM kernel module broken (DESTROY_MEMORY_REGION).\n%s",
- upgrade_note);
- return -EINVAL;
- }
-
- r = kvm_create_irqchip(kvm_state);
- if (r < 0) {
- return r;
- }
-
- kvm_state->coalesced_mmio = 0;
-#ifdef KVM_CAP_COALESCED_MMIO
- kvm_state->coalesced_mmio =
- kvm_check_extension(kvm_state, KVM_CAP_COALESCED_MMIO);
-#endif
-
- kvm_state->vcpu_events = 0;
-#ifdef KVM_CAP_VCPU_EVENTS
- kvm_state->vcpu_events = kvm_check_extension(kvm_state, KVM_CAP_VCPU_EVENTS);
-#endif
-
- kvm_state->debugregs = 0;
-#ifdef KVM_CAP_DEBUGREGS
- kvm_state->debugregs = kvm_check_extension(kvm_state, KVM_CAP_DEBUGREGS);
-#endif
-
- kvm_state->xsave = 0;
-#ifdef KVM_CAP_XSAVE
- kvm_state->xsave = kvm_check_extension(kvm_state, KVM_CAP_XSAVE);
-#endif
-
- kvm_state->xcrs = 0;
-#ifdef KVM_CAP_XCRS
- kvm_state->xcrs = kvm_check_extension(kvm_state, KVM_CAP_XCRS);
-#endif
-
- kvm_state->many_ioeventfds = kvm_check_many_ioeventfds();
-
- kvm_state->pit_state2 = 0;
-#ifdef KVM_CAP_PIT_STATE2
- kvm_state->pit_state2 = kvm_check_extension(kvm_state, KVM_CAP_PIT_STATE2);
-#endif
-
- kvm_init_ap();
-
- cpu_interrupt_handler = kvm_handle_interrupt;
-
- return 0;
-}
-
static void kvm_mutex_unlock(void)
{
assert(!cpu_single_env);
diff --git a/qemu-kvm.h b/qemu-kvm.h
index 7386b46..b552e1e 100644
--- a/qemu-kvm.h
+++ b/qemu-kvm.h
@@ -311,19 +311,6 @@ struct kvm_pit_state {

#endif /* !CONFIG_KVM */

-
-/*!
- * \brief Create new KVM context
- *
- * This creates a new kvm_context. A KVM context is a small area of data that
- * holds information about the KVM instance that gets created by this call.\n
- * This should always be your first call to KVM.
- *
- * \param opaque Not used
- * \return NULL on failure
- */
-int kvm_init(void);
-
int kvm_main_loop(void);
int kvm_init_ap(void);
void kvm_save_lapic(CPUState *env);
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index 9b8c460..a4365bf 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -53,14 +53,12 @@
#define BUS_MCEERR_AO 5
#endif

-#ifdef OBSOLETE_KVM_IMPL
const KVMCapabilityInfo kvm_arch_required_capabilities[] = {
KVM_CAP_INFO(SET_TSS_ADDR),
KVM_CAP_INFO(EXT_CPUID),
KVM_CAP_INFO(MP_STATE),
KVM_CAP_LAST_INFO
};
-#endif

static bool has_msr_star;
static bool has_msr_hsave_pa;
--
1.7.1