diff --git a/kvm.h b/kvm.h
index 553c03e..32af545 100644
--- a/kvm.h
+++ b/kvm.h
@@ -16,7 +16,53 @@
 
 #include "config.h"
 #include "sys-queue.h"
-#include "libkvm-all.h"
+
+/**
+ * \brief The KVM context
+ *
+ * The verbose KVM context
+ */
+
+struct kvm_context {
+	/// Filedescriptor to /dev/kvm
+	int fd;
+	int vm_fd;
+	/// Callbacks that KVM uses to emulate various unvirtualizable functionality
+	struct kvm_callbacks *callbacks;
+	void *opaque;
+	/// is dirty pages logging enabled for all regions or not
+	int dirty_pages_log_all;
+	/// do not create in-kernel irqchip if set
+	int no_irqchip_creation;
+	/// in-kernel irqchip status
+	int irqchip_in_kernel;
+	/// ioctl to use to inject interrupts
+	int irqchip_inject_ioctl;
+	/// do not create in-kernel pit if set
+	int no_pit_creation;
+	/// in-kernel pit status
+	int pit_in_kernel;
+	/// in-kernel coalesced mmio
+	int coalesced_mmio;
+#ifdef KVM_CAP_IRQ_ROUTING
+	struct kvm_irq_routing *irq_routes;
+	int nr_allocated_irq_routes;
+#endif
+	void *used_gsi_bitmap;
+	int max_gsi;
+};
+
+struct kvm_vcpu_context
+{
+	int fd;
+	struct kvm_run *run;
+	struct kvm_context *kvm;
+	uint32_t id;
+};
+
+typedef struct kvm_context *kvm_context_t;
+typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
+
 
 typedef struct KVMSlot
 {
@@ -47,8 +93,13 @@ struct KVMState
 #ifdef KVM_CAP_SET_GUEST_DEBUG
     struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
 #endif
+#ifdef USE_KVM
+    struct kvm_context kvm_context;
+#endif
 };
 
+typedef struct KVMState KVMState;
+
 #ifdef KVM_UPSTREAM
 
 #ifdef CONFIG_KVM
@@ -97,8 +148,6 @@ int kvm_update_guest_debug(CPUState *env, unsigned long reinject_trap);
 
 /* internal API */
 
-struct KVMState;
-typedef struct KVMState KVMState;
 
 int kvm_ioctl(KVMState *s, int type, ...);
 
diff --git a/libkvm-all.c b/libkvm-all.c
index 45679fb..32fdf96 100644
--- a/libkvm-all.c
+++ b/libkvm-all.c
@@ -288,11 +288,12 @@ int kvm_dirty_pages_log_reset(kvm_context_t kvm)
 }
 
 
-kvm_context_t kvm_init(struct kvm_callbacks *callbacks,
+KVMState *kvm_init(struct kvm_callbacks *callbacks,
 		       void *opaque)
 {
 	int fd;
 	kvm_context_t kvm;
+    KVMState *s;
 	int r, gsi_count;
 
 	fd = open("/dev/kvm", O_RDWR);
@@ -319,10 +320,9 @@ kvm_context_t kvm_init(struct kvm_callbacks *callbacks,
 	}
 	kvm_abi = r;
 	kvm_page_size = getpagesize();
-	kvm = malloc(sizeof(*kvm));
-	if (kvm == NULL)
-		goto out_close;
-	memset(kvm, 0, sizeof(*kvm));
+	s = qemu_malloc(sizeof(*s));
+	memset(s, 0, sizeof(*s));
+    kvm = &s->kvm_context;
 	kvm->fd = fd;
 	kvm->vm_fd = -1;
 	kvm->callbacks = callbacks;
@@ -348,10 +348,11 @@ kvm_context_t kvm_init(struct kvm_callbacks *callbacks,
 			set_gsi(kvm, i);
 	}
 
-	return kvm;
- out_close:
-	close(fd);
-	return NULL;
+	return s;
+
+out_close:
+    close(fd);
+    return NULL;
 }
 
 void kvm_finalize(kvm_context_t kvm)
diff --git a/libkvm-all.h b/libkvm-all.h
index d647ef1..251dede 100644
--- a/libkvm-all.h
+++ b/libkvm-all.h
@@ -36,51 +36,6 @@
 /* kvm abi verison variable */
 extern int kvm_abi;
 
-/**
- * \brief The KVM context
- *
- * The verbose KVM context
- */
-
-struct kvm_context {
-	/// Filedescriptor to /dev/kvm
-	int fd;
-	int vm_fd;
-	/// Callbacks that KVM uses to emulate various unvirtualizable functionality
-	struct kvm_callbacks *callbacks;
-	void *opaque;
-	/// is dirty pages logging enabled for all regions or not
-	int dirty_pages_log_all;
-	/// do not create in-kernel irqchip if set
-	int no_irqchip_creation;
-	/// in-kernel irqchip status
-	int irqchip_in_kernel;
-	/// ioctl to use to inject interrupts
-	int irqchip_inject_ioctl;
-	/// do not create in-kernel pit if set
-	int no_pit_creation;
-	/// in-kernel pit status
-	int pit_in_kernel;
-	/// in-kernel coalesced mmio
-	int coalesced_mmio;
-#ifdef KVM_CAP_IRQ_ROUTING
-	struct kvm_irq_routing *irq_routes;
-	int nr_allocated_irq_routes;
-#endif
-	void *used_gsi_bitmap;
-	int max_gsi;
-};
-
-struct kvm_vcpu_context
-{
-	int fd;
-	struct kvm_run *run;
-	struct kvm_context *kvm;
-	uint32_t id;
-};
-
-typedef struct kvm_context *kvm_context_t;
-typedef struct kvm_vcpu_context *kvm_vcpu_context_t;
 
 #include "kvm.h"
 int kvm_alloc_kernel_memory(kvm_context_t kvm, unsigned long memory,
@@ -181,7 +136,7 @@ struct kvm_callbacks {
  * \param opaque Not used
  * \return NULL on failure
  */
-kvm_context_t kvm_init(struct kvm_callbacks *callbacks,
+KVMState *kvm_init(struct kvm_callbacks *callbacks,
 		       void *opaque);
 
 /*!
diff --git a/qemu-kvm.c b/qemu-kvm.c
index 2aeb17c..b441970 100644
--- a/qemu-kvm.c
+++ b/qemu-kvm.c
@@ -33,6 +33,8 @@ int kvm_irqchip = 1;
 int kvm_pit = 1;
 int kvm_pit_reinject = 1;
 int kvm_nested = 0;
+KVMState *kvm_state;
+
 kvm_context_t kvm_context;
 
 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
@@ -780,10 +782,9 @@ static struct kvm_callbacks qemu_kvm_ops = {
 int kvm_qemu_init()
 {
     /* Try to initialize kvm */
-    kvm_context = kvm_init(&qemu_kvm_ops, cpu_single_env);
-    if (!kvm_context) {
-      	return -1;
-    }
+    kvm_state = kvm_init(&qemu_kvm_ops, cpu_single_env);
+    kvm_context = &kvm_state->kvm_context;
+
     pthread_mutex_lock(&qemu_mutex);
 
     return 0;
