diff mbox

[5/8] move kvm_context inside KVMState

Message ID 1247058542-31211-6-git-send-email-glommer@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Glauber Costa July 8, 2009, 1:08 p.m. UTC
To make transition smooth, we still keep a global variable kvm_context
pointing to its position inside the global KVMState. This way we don't
need to hurry about changing all callers.

kvm_init() and kvm_finalize are changed, though, since they have now to
deal with the creation/destruction of a global KVMState

Signed-off-by: Glauber Costa <glommer@redhat.com>
---
 kvm-all.c    |   98 +++++++++++++++++++++++++--------------------------------
 kvm.h        |    3 +-
 libkvm-all.h |    3 +-
 3 files changed, 47 insertions(+), 57 deletions(-)
diff mbox

Patch

diff --git a/kvm-all.c b/kvm-all.c
index 034ae52..15bd429 100644
--- a/kvm-all.c
+++ b/kvm-all.c
@@ -35,23 +35,6 @@ 
 #include "libkvm-all.h"
 #include "libkvm.h"
 
-
-#ifdef CONFIG_KVM
-
-
-/* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
-#define PAGE_SIZE TARGET_PAGE_SIZE
-
-//#define DEBUG_KVM
-
-#ifdef DEBUG_KVM
-#define dprintf(fmt, ...) \
-    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
-#else
-#define dprintf(fmt, ...) \
-    do { } while (0)
-#endif
-
 typedef struct KVMSlot
 {
     target_phys_addr_t start_addr;
@@ -63,8 +46,6 @@  typedef struct KVMSlot
 
 typedef struct kvm_dirty_log KVMDirtyLog;
 
-int kvm_allowed = 0;
-
 struct KVMState
 {
     KVMSlot slots[32];
@@ -76,9 +57,28 @@  struct KVMState
 #ifdef KVM_CAP_SET_GUEST_DEBUG
     struct kvm_sw_breakpoint_head kvm_sw_breakpoints;
 #endif
+    struct kvm_context kvm_context;
 };
 
 static KVMState *kvm_state;
+kvm_context_t kvm_context;
+
+#ifdef CONFIG_KVM
+
+/* KVM uses PAGE_SIZE in it's definition of COALESCED_MMIO_MAX */
+#define PAGE_SIZE TARGET_PAGE_SIZE
+
+//#define DEBUG_KVM
+
+#ifdef DEBUG_KVM
+#define dprintf(fmt, ...) \
+    do { fprintf(stderr, fmt, ## __VA_ARGS__); } while (0)
+#else
+#define dprintf(fmt, ...) \
+    do { } while (0)
+#endif
+
+int kvm_allowed = 0;
 
 static KVMSlot *kvm_alloc_slot(KVMState *s)
 {
@@ -1065,7 +1065,6 @@  int kvm_irqchip = 1;
 int kvm_pit = 1;
 int kvm_pit_reinject = 1;
 int kvm_nested = 0;
-kvm_context_t kvm_context;
 
 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
@@ -1439,16 +1438,15 @@  int kvm_dirty_pages_log_reset(kvm_context_t kvm)
 }
 
 
-kvm_context_t kvm_init(void *opaque)
+int kvm_qemu_init()
 {
 	int fd;
-	kvm_context_t kvm;
 	int r, gsi_count;
 
 	fd = open("/dev/kvm", O_RDWR);
 	if (fd == -1) {
 		perror("open /dev/kvm");
-		return NULL;
+		return -1;
 	}
 	r = ioctl(fd, KVM_GET_API_VERSION, 0);
 	if (r == -1) {
@@ -1469,35 +1467,37 @@  kvm_context_t kvm_init(void *opaque)
 	}
 	kvm_abi = r;
 	kvm_page_size = getpagesize();
-	kvm = qemu_mallocz(sizeof(*kvm));
-	kvm->fd = fd;
-	kvm->vm_fd = -1;
-	kvm->opaque = opaque;
-	kvm->dirty_pages_log_all = 0;
-	kvm->no_irqchip_creation = 0;
-	kvm->no_pit_creation = 0;
-
-	gsi_count = kvm_get_gsi_count(kvm);
+	kvm_state = qemu_mallocz(sizeof(*kvm_state));
+	kvm_context = &kvm_state->kvm_context;
+	kvm_context->fd = fd;
+	kvm_context->vm_fd = -1;
+	kvm_context->opaque = cpu_single_env;
+	kvm_context->dirty_pages_log_all = 0;
+	kvm_context->no_irqchip_creation = 0;
+	kvm_context->no_pit_creation = 0;
+
+	gsi_count = kvm_get_gsi_count(kvm_context);
 	if (gsi_count > 0) {
 		int gsi_bits, i;
 
 		/* Round up so we can search ints using ffs */
 		gsi_bits = ALIGN(gsi_count, 32);
-		kvm->used_gsi_bitmap = qemu_mallocz(gsi_bits / 8);
-		kvm->max_gsi = gsi_bits;
+		kvm_context->used_gsi_bitmap = qemu_mallocz(gsi_bits / 8);
+		kvm_context->max_gsi = gsi_bits;
 
 		/* Mark any over-allocated bits as already in use */
 		for (i = gsi_count; i < gsi_bits; i++)
-			set_gsi(kvm, i);
+			set_gsi(kvm_context, i);
 	}
 
-	return kvm;
- out_close:
+    pthread_mutex_lock(&qemu_mutex);
+	return 0;
+out_close:
 	close(fd);
-	return NULL;
+	return -1;
 }
 
-void kvm_finalize(kvm_context_t kvm)
+void kvm_finalize(KVMState *kvm_state)
 {
 	/* FIXME
 	if (kvm->vcpu_fd[0] != -1)
@@ -1505,8 +1505,8 @@  void kvm_finalize(kvm_context_t kvm)
 	if (kvm->vm_fd != -1)
 		close(kvm->vm_fd);
 	*/
-	close(kvm->fd);
-	free(kvm);
+	close(kvm_state->kvm_context.fd);
+	free(kvm_state);
 }
 
 void kvm_disable_irqchip_creation(kvm_context_t kvm)
@@ -3220,18 +3220,6 @@  int kvm_main_loop(void)
     return 0;
 }
 
-int kvm_qemu_init()
-{
-    /* Try to initialize kvm */
-    kvm_context = kvm_init(cpu_single_env);
-    if (!kvm_context) {
-      	return -1;
-    }
-    pthread_mutex_lock(&qemu_mutex);
-
-    return 0;
-}
-
 #ifdef TARGET_I386
 static int destroy_region_works = 0;
 #endif
@@ -3255,12 +3243,12 @@  int kvm_qemu_create_context(void)
         kvm_disable_pit_creation(kvm_context);
     }
     if (kvm_create(kvm_context, 0, NULL) < 0) {
-       kvm_finalize(kvm_context);
+       kvm_finalize(kvm_state);
        return -1;
     }
     r = kvm_arch_qemu_create_context();
     if(r <0)
-       kvm_finalize(kvm_context);
+       kvm_finalize(kvm_state);
     if (kvm_pit && !kvm_pit_reinject) {
         if (kvm_reinject_control(kvm_context, 0)) {
             fprintf(stderr, "failure to disable in-kernel PIT reinjection\n");
diff --git a/kvm.h b/kvm.h
index 7648c49..31ebce2 100644
--- a/kvm.h
+++ b/kvm.h
@@ -16,11 +16,12 @@ 
 
 #include "config.h"
 #include "sys-queue.h"
-#include "libkvm-all.h"
 
 struct KVMState;
 typedef struct KVMState KVMState;
 
+#include "libkvm-all.h"
+
 #ifdef KVM_UPSTREAM
 
 #ifdef CONFIG_KVM
diff --git a/libkvm-all.h b/libkvm-all.h
index e16646c..7857532 100644
--- a/libkvm-all.h
+++ b/libkvm-all.h
@@ -21,6 +21,7 @@ 
 
 #include <signal.h>
 
+
 /* FIXME: share this number with kvm */
 /* FIXME: or dynamically alloc/realloc regions */
 #ifdef __s390__
@@ -189,7 +190,7 @@  kvm_context_t kvm_init(void *opaque);
  *
  * \param kvm Pointer to the kvm_context that is to be freed
  */
-void kvm_finalize(kvm_context_t kvm);
+void kvm_finalize(KVMState *s);
 
 /*!
  * \brief Disable the in-kernel IRQCHIP creation