diff --git a/hw/device-assignment.c b/hw/device-assignment.c
index 0f7f5aa..abc38f8 100644
--- a/hw/device-assignment.c
+++ b/hw/device-assignment.c
@@ -967,7 +967,7 @@ static int assign_device(AssignedDevice *dev)
                 "requests!\n");
     }
 
-    r = kvm_assign_pci_device(kvm_context, &assigned_dev_data);
+    r = kvm_assign_pci_device(kvm_state, &assigned_dev_data);
     if (r < 0) {
         fprintf(stderr, "Failed to assign device \"%s\" : %s\n",
                 dev->dev.qdev.id, strerror(-r));
@@ -1010,7 +1010,7 @@ static int assign_irq(AssignedDevice *dev)
 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
     if (dev->irq_requested_type) {
         assigned_irq_data.flags = dev->irq_requested_type;
-        r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
+        r = kvm_deassign_irq(kvm_state, &assigned_irq_data);
         /* -ENXIO means no assigned irq */
         if (r && r != -ENXIO)
             perror("assign_irq: deassign");
@@ -1024,7 +1024,7 @@ static int assign_irq(AssignedDevice *dev)
         assigned_irq_data.flags |= KVM_DEV_IRQ_HOST_INTX;
 #endif
 
-    r = kvm_assign_irq(kvm_context, &assigned_irq_data);
+    r = kvm_assign_irq(kvm_state, &assigned_irq_data);
     if (r < 0) {
         fprintf(stderr, "Failed to assign irq for \"%s\": %s\n",
                 dev->dev.qdev.id, strerror(-r));
@@ -1048,7 +1048,7 @@ static void deassign_device(AssignedDevice *dev)
     assigned_dev_data.assigned_dev_id  =
 	calc_assigned_dev_id(dev->h_segnr, dev->h_busnr, dev->h_devfn);
 
-    r = kvm_deassign_pci_device(kvm_context, &assigned_dev_data);
+    r = kvm_deassign_pci_device(kvm_state, &assigned_dev_data);
     if (r < 0)
 	fprintf(stderr, "Failed to deassign device \"%s\" : %s\n",
                 dev->dev.qdev.id, strerror(-r));
@@ -1113,7 +1113,7 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev, unsigned int ctrl_pos)
 
         assigned_irq_data.flags = assigned_dev->irq_requested_type;
         free_dev_irq_entries(assigned_dev);
-        r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
+        r = kvm_deassign_irq(kvm_state, &assigned_irq_data);
         /* -ENXIO means no assigned irq */
         if (r && r != -ENXIO)
             perror("assigned_dev_update_msi: deassign irq");
@@ -1147,8 +1147,9 @@ static void assigned_dev_update_msi(PCIDevice *pci_dev, unsigned int ctrl_pos)
 
         assigned_irq_data.guest_irq = assigned_dev->entry->gsi;
 	assigned_irq_data.flags = KVM_DEV_IRQ_HOST_MSI | KVM_DEV_IRQ_GUEST_MSI;
-        if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0)
+        if (kvm_assign_irq(kvm_state, &assigned_irq_data) < 0) {
             perror("assigned_dev_enable_msi: assign irq");
+        }
 
         assigned_dev->girq = -1;
         assigned_dev->irq_requested_type = assigned_irq_data.flags;
@@ -1192,7 +1193,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
     msix_nr.assigned_dev_id = calc_assigned_dev_id(adev->h_segnr, adev->h_busnr,
                                           (uint8_t)adev->h_devfn);
     msix_nr.entry_nr = entries_nr;
-    r = kvm_assign_set_msix_nr(kvm_context, &msix_nr);
+    r = kvm_assign_set_msix_nr(kvm_state, &msix_nr);
     if (r != 0) {
         fprintf(stderr, "fail to set MSI-X entry number for MSIX! %s\n",
 			strerror(-r));
@@ -1231,7 +1232,7 @@ static int assigned_dev_update_msix_mmio(PCIDevice *pci_dev)
 
         msix_entry.gsi = adev->entry[entries_nr].gsi;
         msix_entry.entry = i;
-        r = kvm_assign_set_msix_entry(kvm_context, &msix_entry);
+        r = kvm_assign_set_msix_entry(kvm_state, &msix_entry);
         if (r) {
             fprintf(stderr, "fail to set MSI-X entry! %s\n", strerror(-r));
             break;
@@ -1269,7 +1270,7 @@ static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
 
         assigned_irq_data.flags = assigned_dev->irq_requested_type;
         free_dev_irq_entries(assigned_dev);
-        r = kvm_deassign_irq(kvm_context, &assigned_irq_data);
+        r = kvm_deassign_irq(kvm_state, &assigned_irq_data);
         /* -ENXIO means no assigned irq */
         if (r && r != -ENXIO)
             perror("assigned_dev_update_msix: deassign irq");
@@ -1285,7 +1286,7 @@ static void assigned_dev_update_msix(PCIDevice *pci_dev, unsigned int ctrl_pos)
             perror("assigned_dev_update_msix_mmio");
             return;
         }
-        if (kvm_assign_irq(kvm_context, &assigned_irq_data) < 0) {
+        if (kvm_assign_irq(kvm_state, &assigned_irq_data) < 0) {
             perror("assigned_dev_enable_msix: assign irq");
             return;
         }
diff --git a/hw/i8254-kvm.c b/hw/i8254-kvm.c
index 44ed86c..8b494d0 100644
--- a/hw/i8254-kvm.c
+++ b/hw/i8254-kvm.c
@@ -39,11 +39,11 @@ static void kvm_pit_pre_save(void *opaque)
     int i;
 
     if (kvm_has_pit_state2()) {
-        kvm_get_pit2(kvm_context, &pit2);
+        kvm_get_pit2(kvm_state, &pit2);
         s->flags = pit2.flags;
     } else {
         /* pit2 is superset of pit struct so just cast it and use it */
-        kvm_get_pit(kvm_context, (struct kvm_pit_state *)&pit2);
+        kvm_get_pit(kvm_state, (struct kvm_pit_state *)&pit2);
     }
     for (i = 0; i < 3; i++) {
 	c = &pit2.channels[i];
@@ -92,9 +92,9 @@ static int kvm_pit_post_load(void *opaque, int version_id)
     }
 
     if (kvm_has_pit_state2()) {
-        kvm_set_pit2(kvm_context, &pit2);
+        kvm_set_pit2(kvm_state, &pit2);
     } else {
-        kvm_set_pit(kvm_context, (struct kvm_pit_state *)&pit2);
+        kvm_set_pit(kvm_state, (struct kvm_pit_state *)&pit2);
     }
     return 0;
 }
diff --git a/hw/i8259.c b/hw/i8259.c
index d710ada..b892cee 100644
--- a/hw/i8259.c
+++ b/hw/i8259.c
@@ -590,7 +590,7 @@ static void kvm_kernel_pic_save_to_user(PicState *s)
     chip.chip_id = (&s->pics_state->pics[0] == s) ?
                    KVM_IRQCHIP_PIC_MASTER :
                    KVM_IRQCHIP_PIC_SLAVE;
-    kvm_get_irqchip(kvm_context, &chip);
+    kvm_get_irqchip(kvm_state, &chip);
     kpic = &chip.chip.pic;
 
     s->last_irr = kpic->last_irr;
@@ -640,7 +640,7 @@ static int kvm_kernel_pic_load_from_user(PicState *s)
     kpic->elcr = s->elcr;
     kpic->elcr_mask = s->elcr_mask;
 
-    kvm_set_irqchip(kvm_context, &chip);
+    kvm_set_irqchip(kvm_state, &chip);
 #endif
     return 0;
 }
diff --git a/hw/ioapic.c b/hw/ioapic.c
index 084d37a..20dd8eb 100644
--- a/hw/ioapic.c
+++ b/hw/ioapic.c
@@ -278,7 +278,7 @@ static void kvm_kernel_ioapic_save_to_user(IOAPICState *s)
     int i;
 
     chip.chip_id = KVM_IRQCHIP_IOAPIC;
-    kvm_get_irqchip(kvm_context, &chip);
+    kvm_get_irqchip(kvm_state, &chip);
     kioapic = &chip.chip.ioapic;
 
     s->id = kioapic->id;
@@ -308,7 +308,7 @@ static void kvm_kernel_ioapic_load_from_user(IOAPICState *s)
         kioapic->redirtbl[i].bits = s->ioredtbl[i];
     }
 
-    kvm_set_irqchip(kvm_context, &chip);
+    kvm_set_irqchip(kvm_state, &chip);
 #endif
 }
 
diff --git a/hw/pcspk.c b/hw/pcspk.c
index e8b72c5..c70de53 100644
--- a/hw/pcspk.c
+++ b/hw/pcspk.c
@@ -58,7 +58,7 @@ static void kvm_get_pit_ch2(ISADevice *dev,
     struct kvm_pit_state pit_state;
 
     if (kvm_enabled() && kvm_pit_in_kernel()) {
-        kvm_get_pit(kvm_context, &pit_state);
+        kvm_get_pit(kvm_state, &pit_state);
         pit->channels[2].mode = pit_state.channels[2].mode;
         pit->channels[2].count = pit_state.channels[2].count;
         pit->channels[2].count_load_time = pit_state.channels[2].count_load_time;
@@ -80,7 +80,7 @@ static void kvm_set_pit_ch2(ISADevice *dev,
         inkernel_state->channels[2].count_load_time =
             pit->channels[2].count_load_time;
         inkernel_state->channels[2].gate = pit->channels[2].gate;
-        kvm_set_pit(kvm_context, inkernel_state);
+        kvm_set_pit(kvm_state, inkernel_state);
     }
 }
 #else
diff --git a/kvm.h b/kvm.h
index 7d548ed..75dc7cf 100644
--- a/kvm.h
+++ b/kvm.h
@@ -17,9 +17,6 @@
 #include <errno.h>
 #include "config-host.h"
 #include "qemu-queue.h"
-#ifdef NEED_CPU_H
-#include "qemu-kvm.h"
-#endif
 
 #ifdef CONFIG_KVM
 #include <linux/kvm.h>
@@ -85,6 +82,7 @@ int kvm_pit_in_kernel(void);
 
 int kvm_on_sigbus_vcpu(CPUState *env, int code, void *addr);
 int kvm_on_sigbus(int code, void *addr);
+#endif /* NEED_CPU_H */
 
 /* internal API */
 
@@ -96,6 +94,7 @@ int kvm_ioctl(KVMState *s, int type, ...);
 
 int kvm_vm_ioctl(KVMState *s, int type, ...);
 
+#ifdef NEED_CPU_H
 int kvm_vcpu_ioctl(CPUState *env, int type, ...);
 
 /* Arch specific hooks */
@@ -234,4 +233,8 @@ int kvm_irqchip_in_kernel(void);
 
 int kvm_set_irq(int irq, int level, int *status);
 
+#ifdef NEED_CPU_H
+#include "qemu-kvm.h"
+#endif
+
 #endif
diff --git a/qemu-kvm-x86.c b/qemu-kvm-x86.c
index bbdf218..2aa833f 100644
--- a/qemu-kvm-x86.c
+++ b/qemu-kvm-x86.c
@@ -23,19 +23,19 @@
 #include "kvm.h"
 #include "hw/apic.h"
 
-static int kvm_create_pit(kvm_context_t kvm)
+static int kvm_create_pit(KVMState *s)
 {
 #ifdef KVM_CAP_PIT
     int r;
 
     if (kvm_pit_in_kernel()) {
-        r = kvm_vm_ioctl(kvm_state, KVM_CREATE_PIT);
+        r = kvm_vm_ioctl(s, KVM_CREATE_PIT);
         if (r < 0) {
             fprintf(stderr, "Create kernel PIC irqchip failed\n");
             return r;
         }
         if (!kvm_pit_reinject) {
-            r = kvm_reinject_control(kvm_context, 0);
+            r = kvm_reinject_control(s, 0);
             if (r < 0) {
                 fprintf(stderr,
                         "failure to disable in-kernel PIT reinjection\n");
@@ -142,37 +142,37 @@ int kvm_set_lapic(CPUState *env, struct kvm_lapic_state *s)
 
 #ifdef KVM_CAP_PIT
 
-int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s)
+int kvm_get_pit(KVMState *s, struct kvm_pit_state *pit_state)
 {
     if (!kvm_pit_in_kernel()) {
         return 0;
     }
-    return kvm_vm_ioctl(kvm_state, KVM_GET_PIT, s);
+    return kvm_vm_ioctl(s, KVM_GET_PIT, pit_state);
 }
 
-int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s)
+int kvm_set_pit(KVMState *s, struct kvm_pit_state *pit_state)
 {
     if (!kvm_pit_in_kernel()) {
         return 0;
     }
-    return kvm_vm_ioctl(kvm_state, KVM_SET_PIT, s);
+    return kvm_vm_ioctl(s, KVM_SET_PIT, pit_state);
 }
 
 #ifdef KVM_CAP_PIT_STATE2
-int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2)
+int kvm_get_pit2(KVMState *s, struct kvm_pit_state2 *ps2)
 {
     if (!kvm_pit_in_kernel()) {
         return 0;
     }
-    return kvm_vm_ioctl(kvm_state, KVM_GET_PIT2, ps2);
+    return kvm_vm_ioctl(s, KVM_GET_PIT2, ps2);
 }
 
-int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2)
+int kvm_set_pit2(KVMState *s, struct kvm_pit_state2 *ps2)
 {
     if (!kvm_pit_in_kernel()) {
         return 0;
     }
-    return kvm_vm_ioctl(kvm_state, KVM_SET_PIT2, ps2);
+    return kvm_vm_ioctl(s, KVM_SET_PIT2, ps2);
 }
 
 #endif
diff --git a/qemu-kvm.c b/qemu-kvm.c
index 4aa6b0f..9406fcf 100644
--- a/qemu-kvm.c
+++ b/qemu-kvm.c
@@ -55,7 +55,6 @@ int kvm_nested = 0;
 
 
 KVMState *kvm_state;
-kvm_context_t kvm_context;
 
 pthread_mutex_t qemu_mutex = PTHREAD_MUTEX_INITIALIZER;
 pthread_cond_t qemu_vcpu_cond = PTHREAD_COND_INITIALIZER;
@@ -153,7 +152,6 @@ int kvm_init(void)
         goto out_close;
     }
     kvm_state = qemu_mallocz(sizeof(*kvm_state));
-    kvm_context = &kvm_state->kvm_context;
 
     kvm_state->fd = fd;
     kvm_state->vmfd = -1;
@@ -212,12 +210,13 @@ static int kvm_set_boot_vcpu_id(kvm_context_t kvm, uint32_t id)
 #endif
 }
 
-static int kvm_init_irq_routing(kvm_context_t kvm)
+static int kvm_init_irq_routing(KVMState *s)
 {
 #ifdef KVM_CAP_IRQ_ROUTING
+    kvm_context_t kvm = &s->kvm_context;
     int r, gsi_count;
 
-    gsi_count = kvm_check_extension(kvm_state, KVM_CAP_IRQ_ROUTING);
+    gsi_count = kvm_check_extension(s, KVM_CAP_IRQ_ROUTING);
     if (gsi_count > 0) {
         int gsi_bits, i;
 
@@ -232,7 +231,7 @@ static int kvm_init_irq_routing(kvm_context_t kvm)
         }
     }
 
-    kvm->irq_routes = qemu_mallocz(sizeof(*kvm_context->irq_routes));
+    kvm->irq_routes = qemu_mallocz(sizeof(*kvm->irq_routes));
     kvm->nr_allocated_irq_routes = 0;
 
     r = kvm_arch_init_irq_routing();
@@ -244,16 +243,17 @@ static int kvm_init_irq_routing(kvm_context_t kvm)
     return 0;
 }
 
-int kvm_create_irqchip(kvm_context_t kvm)
+int kvm_create_irqchip(KVMState *s)
 {
 #ifdef KVM_CAP_IRQCHIP
+    kvm_context_t kvm = &s->kvm_context;
     int r;
 
-    if (!kvm_irqchip || !kvm_check_extension(kvm_state, KVM_CAP_IRQCHIP)) {
+    if (!kvm_irqchip || !kvm_check_extension(s, KVM_CAP_IRQCHIP)) {
         return 0;
     }
 
-    r = kvm_vm_ioctl(kvm_state, KVM_CREATE_IRQCHIP);
+    r = kvm_vm_ioctl(s, KVM_CREATE_IRQCHIP);
     if (r < 0) {
         fprintf(stderr, "Create kernel PIC irqchip failed\n");
         return r;
@@ -261,13 +261,13 @@ int kvm_create_irqchip(kvm_context_t kvm)
 
     kvm->irqchip_inject_ioctl = KVM_IRQ_LINE;
 #if defined(KVM_CAP_IRQ_INJECT_STATUS) && defined(KVM_IRQ_LINE_STATUS)
-    if (kvm_check_extension(kvm_state, KVM_CAP_IRQ_INJECT_STATUS)) {
+    if (kvm_check_extension(s, KVM_CAP_IRQ_INJECT_STATUS)) {
         kvm->irqchip_inject_ioctl = KVM_IRQ_LINE_STATUS;
     }
 #endif
-    kvm_state->irqchip_in_kernel = 1;
+    s->irqchip_in_kernel = 1;
 
-    r = kvm_init_irq_routing(kvm);
+    r = kvm_init_irq_routing(s);
     if (r < 0) {
         return r;
     }
@@ -307,28 +307,28 @@ int kvm_set_irq(int irq, int level, int *status)
     return 1;
 }
 
-int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip)
+int kvm_get_irqchip(KVMState *s, struct kvm_irqchip *chip)
 {
     int r;
 
-    if (!kvm_state->irqchip_in_kernel) {
+    if (!s->irqchip_in_kernel) {
         return 0;
     }
-    r = kvm_vm_ioctl(kvm_state, KVM_GET_IRQCHIP, chip);
+    r = kvm_vm_ioctl(s, KVM_GET_IRQCHIP, chip);
     if (r < 0) {
         perror("kvm_get_irqchip\n");
     }
     return r;
 }
 
-int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip)
+int kvm_set_irqchip(KVMState *s, struct kvm_irqchip *chip)
 {
     int r;
 
-    if (!kvm_state->irqchip_in_kernel) {
+    if (!s->irqchip_in_kernel) {
         return 0;
     }
-    r = kvm_vm_ioctl(kvm_state, KVM_SET_IRQCHIP, chip);
+    r = kvm_vm_ioctl(s, KVM_SET_IRQCHIP, chip);
     if (r < 0) {
         perror("kvm_set_irqchip\n");
     }
@@ -521,52 +521,52 @@ int kvm_inject_nmi(CPUState *env)
 }
 
 #ifdef KVM_CAP_DEVICE_ASSIGNMENT
-int kvm_assign_pci_device(kvm_context_t kvm,
+int kvm_assign_pci_device(KVMState *s,
                           struct kvm_assigned_pci_dev *assigned_dev)
 {
-    return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_PCI_DEVICE, assigned_dev);
+    return kvm_vm_ioctl(s, KVM_ASSIGN_PCI_DEVICE, assigned_dev);
 }
 
-static int kvm_old_assign_irq(kvm_context_t kvm,
+static int kvm_old_assign_irq(KVMState *s,
                               struct kvm_assigned_irq *assigned_irq)
 {
-    return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_IRQ, assigned_irq);
+    return kvm_vm_ioctl(s, KVM_ASSIGN_IRQ, assigned_irq);
 }
 
 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
-int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq)
+int kvm_assign_irq(KVMState *s, struct kvm_assigned_irq *assigned_irq)
 {
     int ret;
 
-    ret = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_ASSIGN_DEV_IRQ);
+    ret = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_ASSIGN_DEV_IRQ);
     if (ret > 0) {
-        return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_DEV_IRQ, assigned_irq);
+        return kvm_vm_ioctl(s, KVM_ASSIGN_DEV_IRQ, assigned_irq);
     }
 
-    return kvm_old_assign_irq(kvm, assigned_irq);
+    return kvm_old_assign_irq(s, assigned_irq);
 }
 
-int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq)
+int kvm_deassign_irq(KVMState *s, struct kvm_assigned_irq *assigned_irq)
 {
-    return kvm_vm_ioctl(kvm_state, KVM_DEASSIGN_DEV_IRQ, assigned_irq);
+    return kvm_vm_ioctl(s, KVM_DEASSIGN_DEV_IRQ, assigned_irq);
 }
 #else
-int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq)
+int kvm_assign_irq(KVMState *s, struct kvm_assigned_irq *assigned_irq)
 {
-    return kvm_old_assign_irq(kvm, assigned_irq);
+    return kvm_old_assign_irq(s, assigned_irq);
 }
 #endif
 #endif
 
 #ifdef KVM_CAP_DEVICE_DEASSIGNMENT
-int kvm_deassign_pci_device(kvm_context_t kvm,
+int kvm_deassign_pci_device(KVMState *s,
                             struct kvm_assigned_pci_dev *assigned_dev)
 {
-    return kvm_vm_ioctl(kvm_state, KVM_DEASSIGN_PCI_DEVICE, assigned_dev);
+    return kvm_vm_ioctl(s, KVM_DEASSIGN_PCI_DEVICE, assigned_dev);
 }
 #endif
 
-int kvm_reinject_control(kvm_context_t kvm, int pit_reinject)
+int kvm_reinject_control(KVMState *s, int pit_reinject)
 {
 #ifdef KVM_CAP_REINJECT_CONTROL
     int r;
@@ -574,9 +574,9 @@ int kvm_reinject_control(kvm_context_t kvm, int pit_reinject)
 
     control.pit_reinject = pit_reinject;
 
-    r = kvm_ioctl(kvm_state, KVM_CHECK_EXTENSION, KVM_CAP_REINJECT_CONTROL);
+    r = kvm_ioctl(s, KVM_CHECK_EXTENSION, KVM_CAP_REINJECT_CONTROL);
     if (r > 0) {
-        return kvm_vm_ioctl(kvm_state, KVM_REINJECT_CONTROL, &control);
+        return kvm_vm_ioctl(s, KVM_REINJECT_CONTROL, &control);
     }
 #endif
     return -ENOSYS;
@@ -595,7 +595,7 @@ int kvm_has_gsi_routing(void)
 int kvm_clear_gsi_routes(void)
 {
 #ifdef KVM_CAP_IRQ_ROUTING
-    kvm_context_t kvm = kvm_context;
+    kvm_context_t kvm = &kvm_state->kvm_context;
 
     kvm->irq_routes->nr = 0;
     return 0;
@@ -607,7 +607,7 @@ int kvm_clear_gsi_routes(void)
 int kvm_add_routing_entry(struct kvm_irq_routing_entry *entry)
 {
 #ifdef KVM_CAP_IRQ_ROUTING
-    kvm_context_t kvm = kvm_context;
+    kvm_context_t kvm = &kvm_state->kvm_context;
     struct kvm_irq_routing *z;
     struct kvm_irq_routing_entry *new;
     int n, size;
@@ -661,7 +661,7 @@ int kvm_add_irq_route(int gsi, int irqchip, int pin)
 int kvm_del_routing_entry(struct kvm_irq_routing_entry *entry)
 {
 #ifdef KVM_CAP_IRQ_ROUTING
-    kvm_context_t kvm = kvm_context;
+    kvm_context_t kvm = &kvm_state->kvm_context;
     struct kvm_irq_routing_entry *e, *p;
     int i, gsi, found = 0;
 
@@ -722,7 +722,7 @@ int kvm_update_routing_entry(struct kvm_irq_routing_entry *entry,
                              struct kvm_irq_routing_entry *newentry)
 {
 #ifdef KVM_CAP_IRQ_ROUTING
-    kvm_context_t kvm = kvm_context;
+    kvm_context_t kvm = &kvm_state->kvm_context;
     struct kvm_irq_routing_entry *e;
     int i;
 
@@ -781,7 +781,7 @@ int kvm_del_irq_route(int gsi, int irqchip, int pin)
 int kvm_commit_irq_routes(void)
 {
 #ifdef KVM_CAP_IRQ_ROUTING
-    kvm_context_t kvm = kvm_context;
+    kvm_context_t kvm = &kvm_state->kvm_context;
 
     kvm->irq_routes->flags = 0;
     return kvm_vm_ioctl(kvm_state, KVM_SET_GSI_ROUTING, kvm->irq_routes);
@@ -792,7 +792,7 @@ int kvm_commit_irq_routes(void)
 
 int kvm_get_irq_route_gsi(void)
 {
-    kvm_context_t kvm = kvm_context;
+    kvm_context_t kvm = &kvm_state->kvm_context;
     int i, bit;
     uint32_t *buf = kvm->used_gsi_bitmap;
 
@@ -867,16 +867,15 @@ int kvm_msi_message_update(KVMMsiMessage *old, KVMMsiMessage *new)
 
 
 #ifdef KVM_CAP_DEVICE_MSIX
-int kvm_assign_set_msix_nr(kvm_context_t kvm,
-                           struct kvm_assigned_msix_nr *msix_nr)
+int kvm_assign_set_msix_nr(KVMState *s, struct kvm_assigned_msix_nr *msix_nr)
 {
-    return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_SET_MSIX_NR, msix_nr);
+    return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_NR, msix_nr);
 }
 
-int kvm_assign_set_msix_entry(kvm_context_t kvm,
+int kvm_assign_set_msix_entry(KVMState *s,
                               struct kvm_assigned_msix_entry *entry)
 {
-    return kvm_vm_ioctl(kvm_state, KVM_ASSIGN_SET_MSIX_ENTRY, entry);
+    return kvm_vm_ioctl(s, KVM_ASSIGN_SET_MSIX_ENTRY, entry);
 }
 #endif
 
@@ -1234,18 +1233,18 @@ void kvm_hpet_disable_kpit(void)
 {
     struct kvm_pit_state2 ps2;
 
-    kvm_get_pit2(kvm_context, &ps2);
+    kvm_get_pit2(kvm_state, &ps2);
     ps2.flags |= KVM_PIT_FLAGS_HPET_LEGACY;
-    kvm_set_pit2(kvm_context, &ps2);
+    kvm_set_pit2(kvm_state, &ps2);
 }
 
 void kvm_hpet_enable_kpit(void)
 {
     struct kvm_pit_state2 ps2;
 
-    kvm_get_pit2(kvm_context, &ps2);
+    kvm_get_pit2(kvm_state, &ps2);
     ps2.flags &= ~KVM_PIT_FLAGS_HPET_LEGACY;
-    kvm_set_pit2(kvm_context, &ps2);
+    kvm_set_pit2(kvm_state, &ps2);
 }
 #endif
 
@@ -1397,7 +1396,7 @@ static int kvm_create_context(void)
         return -EINVAL;
     }
 
-    r = kvm_create_irqchip(kvm_context);
+    r = kvm_create_irqchip(kvm_state);
     if (r < 0) {
         return r;
     }
@@ -1500,6 +1499,6 @@ void kvm_ioperm(CPUState *env, void *data)
 
 int kvm_set_boot_cpu_id(uint32_t id)
 {
-    return kvm_set_boot_vcpu_id(kvm_context, id);
+    return kvm_set_boot_vcpu_id(&kvm_state->kvm_context, id);
 }
 
diff --git a/qemu-kvm.h b/qemu-kvm.h
index dd11e32..2c67a28 100644
--- a/qemu-kvm.h
+++ b/qemu-kvm.h
@@ -66,7 +66,7 @@ typedef struct kvm_context *kvm_context_t;
 
 int kvm_arch_run(CPUState *env);
 
-int kvm_create_irqchip(kvm_context_t kvm);
+int kvm_create_irqchip(KVMState *s);
 
 /*!
  * \brief Check if a vcpu is ready for interrupt injection
@@ -103,7 +103,7 @@ int kvm_inject_irq(CPUState *env, unsigned irq);
  * \param kvm Pointer to the current kvm_context
  * \param chip The irq chip device to be dumped
  */
-int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
+int kvm_get_irqchip(KVMState *s, struct kvm_irqchip *chip);
 
 /*!
  * \brief Set in kernel IRQCHIP contents
@@ -115,7 +115,7 @@ int kvm_get_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
  * \param kvm Pointer to the current kvm_context
  * \param chip THe irq chip device to be written
  */
-int kvm_set_irqchip(kvm_context_t kvm, struct kvm_irqchip *chip);
+int kvm_set_irqchip(KVMState *s, struct kvm_irqchip *chip);
 
 #if defined(__i386__) || defined(__x86_64__)
 /*!
@@ -166,7 +166,7 @@ int kvm_inject_nmi(CPUState *env);
  * \param kvm Pointer to the current kvm_context
  * \param s PIT state of the virtual domain
  */
-int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
+int kvm_get_pit(KVMState *s, struct kvm_pit_state *pit_state);
 
 /*!
  * \brief Set in kernel PIT of the virtual domain
@@ -177,9 +177,9 @@ int kvm_get_pit(kvm_context_t kvm, struct kvm_pit_state *s);
  * \param kvm Pointer to the current kvm_context
  * \param s PIT state of the virtual domain
  */
-int kvm_set_pit(kvm_context_t kvm, struct kvm_pit_state *s);
+int kvm_set_pit(KVMState *s, struct kvm_pit_state *pit_state);
 
-int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
+int kvm_reinject_control(KVMState *s, int pit_reinject);
 
 #ifdef KVM_CAP_PIT_STATE2
 /*!
@@ -190,7 +190,7 @@ int kvm_reinject_control(kvm_context_t kvm, int pit_reinject);
  * \param ps2 PIT state2 of the virtual domain
  * \return 0 on success
  */
-int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
+int kvm_set_pit2(KVMState *s, struct kvm_pit_state2 *ps2);
 
 /*!
  * \brief Get in kernel PIT state2 of the virtual domain
@@ -200,7 +200,7 @@ int kvm_set_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
  * \param ps2 PIT state2 of the virtual domain
  * \return 0 on success
  */
-int kvm_get_pit2(kvm_context_t kvm, struct kvm_pit_state2 *ps2);
+int kvm_get_pit2(KVMState *s, struct kvm_pit_state2 *ps2);
 
 #endif
 #endif
@@ -222,7 +222,7 @@ int kvm_enable_vapic(CPUState *env, uint64_t vapic);
  * \param kvm Pointer to the current kvm_context
  * \param assigned_dev Parameters, like bus, devfn number, etc
  */
-int kvm_assign_pci_device(kvm_context_t kvm,
+int kvm_assign_pci_device(KVMState *s,
                           struct kvm_assigned_pci_dev *assigned_dev);
 
 /*!
@@ -234,7 +234,7 @@ int kvm_assign_pci_device(kvm_context_t kvm,
  * \param kvm Pointer to the current kvm_context
  * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
  */
-int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
+int kvm_assign_irq(KVMState *s, struct kvm_assigned_irq *assigned_irq);
 
 #ifdef KVM_CAP_ASSIGN_DEV_IRQ
 /*!
@@ -246,7 +246,7 @@ int kvm_assign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
  * \param kvm Pointer to the current kvm_context
  * \param assigned_irq Parameters, like dev id, host irq, guest irq, etc
  */
-int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
+int kvm_deassign_irq(KVMState *s, struct kvm_assigned_irq *assigned_irq);
 #endif
 #endif
 
@@ -260,7 +260,7 @@ int kvm_deassign_irq(kvm_context_t kvm, struct kvm_assigned_irq *assigned_irq);
  * \param kvm Pointer to the current kvm_context
  * \param assigned_dev Parameters, like bus, devfn number, etc
  */
-int kvm_deassign_pci_device(kvm_context_t kvm,
+int kvm_deassign_pci_device(KVMState *s,
                             struct kvm_assigned_pci_dev *assigned_dev);
 #endif
 
@@ -318,16 +318,13 @@ int kvm_update_routing_entry(struct kvm_irq_routing_entry *entry,
 
 
 #ifdef KVM_CAP_DEVICE_MSIX
-int kvm_assign_set_msix_nr(kvm_context_t kvm,
-                           struct kvm_assigned_msix_nr *msix_nr);
-int kvm_assign_set_msix_entry(kvm_context_t kvm,
+int kvm_assign_set_msix_nr(KVMState *s, struct kvm_assigned_msix_nr *msix_nr);
+int kvm_assign_set_msix_entry(KVMState *s,
                               struct kvm_assigned_msix_entry *entry);
 #endif
 
 #else                           /* !CONFIG_KVM */
 
-typedef struct kvm_context *kvm_context_t;
-
 struct kvm_pit_state {
 };
 
@@ -383,7 +380,6 @@ extern int kvm_irqchip;
 extern int kvm_pit;
 extern int kvm_pit_reinject;
 extern int kvm_nested;
-extern kvm_context_t kvm_context;
 extern unsigned int kvm_shadow_memory;
 
 struct ioperm_data {
diff --git a/target-i386/kvm.c b/target-i386/kvm.c
index a8b5c6d..33239bf 100644
--- a/target-i386/kvm.c
+++ b/target-i386/kvm.c
@@ -601,7 +601,7 @@ static int kvm_get_supported_msrs(KVMState *s)
     return ret;
 }
 
-static int kvm_create_pit(kvm_context_t kvm);
+static int kvm_create_pit(KVMState *s);
 
 int kvm_arch_init(KVMState *s)
 {
@@ -653,7 +653,7 @@ int kvm_arch_init(KVMState *s)
     }
     qemu_register_reset(kvm_unpoison_all, NULL);
 
-    ret = kvm_create_pit(&s->kvm_context);
+    ret = kvm_create_pit(s);
     if (ret < 0) {
         return ret;
     }
