public inbox for kvm@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] Clean up KVM/QEMU interaction
@ 2008-01-29 22:46 Anthony Liguori
       [not found] ` <1201646815-5071-1-git-send-email-aliguori-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
  0 siblings, 1 reply; 7+ messages in thread
From: Anthony Liguori @ 2008-01-29 22:46 UTC (permalink / raw)
  To: kvm-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f
  Cc: kvm-ppc-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f, Jerone Young,
	kvm-ia64-devel-5NWGOfrQmneRv+LV9MX5uipxlwaOVQ5f, Avi Kivity,
	Anthony Liguori

This patch attempts to clean up the interactions between KVM and QEMU.  Sorry
for such a big patch, but I don't think there's a better way to approach this
such that it's still bisect friendly.  I think this is most of what's needed to
get basic KVM support into QEMU though.

Right now, there's a mix of #ifdef USE_KVM, if (kvm_allowed), and various
extern declarations.  It's all pretty fugly and there's a lot of mistakes due
to it.

The following patch eliminates almost all uses of #ifdef USE_KVM by introducing
a kvm_enabled() macro.  If USE_KVM is set, this macro evaluates to kvm_allowed.
If USE_KVM isn't set, the macro evaluates to 0.

Since GCC eliminates if (0) blocks, this is just as good as using #ifdef.  By
making sure that we never call into libkvm directly from QEMU, we can also just
not link qemu-kvm when USE_KVM isn't set instead of having the entire file
wrapped in a USE_KVM.

We also change the --enable-kvm configure option to --disable-kvm since KVM is
enabled by default.

I've tested this patch on x86 with 32-bit and 64-bit Linux guests and a 32-bit
Windows guest.  I've also tested with USE_KVM not set.  Jerone has also
verified that it doesn't PPC.  My apologies if it breaks ia64 but I have no
way of testing that.

Signed-off-by: Anthony Liguori <aliguori-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>

diff --git a/configure b/configure
index a000c62..6b20c2f 100755
--- a/configure
+++ b/configure
@@ -115,7 +115,7 @@ fi
     --disable-kqemu \
     --extra-cflags="-I $PWD/../libkvm $qemu_cflags" \
     --extra-ldflags="-L $PWD/../libkvm $qemu_ldflags" \
-    --enable-kvm --kernel-path="$libkvm_kerneldir" \
+    --kernel-path="$libkvm_kerneldir" \
     --prefix="$prefix" \
     ${qemu_cc:+"--cc=$qemu_cc"} \
     ${cross_prefix:+"--cross-prefix=$cross_prefix"} \
diff --git a/qemu/Makefile.target b/qemu/Makefile.target
index df43716..4458971 100644
--- a/qemu/Makefile.target
+++ b/qemu/Makefile.target
@@ -295,8 +295,12 @@ SRCS:= $(OBJS:.o=.c)
 OBJS+= libqemu.a
 
 # cpu emulator library
-LIBOBJS=exec.o kqemu.o qemu-kvm.o translate-op.o translate-all.o cpu-exec.o\
+LIBOBJS=exec.o kqemu.o translate-op.o translate-all.o cpu-exec.o\
         translate.o op.o host-utils.o
+ifeq ($(USE_KVM), 1)
+LIBOBJS+=qemu-kvm.o
+endif
+
 ifdef CONFIG_SOFTFLOAT
 LIBOBJS+=fpu/softfloat.o
 else
@@ -306,20 +310,26 @@ CPPFLAGS+=-I$(SRC_PATH)/fpu
 
 ifeq ($(TARGET_ARCH), i386)
 LIBOBJS+=helper.o helper2.o
+ifeq ($(USE_KVM), 1)
 LIBOBJS+=qemu-kvm-x86.o kvm-tpr-opt.o
 LIBOBJS+=qemu-kvm-helper.o
 endif
+endif
 
 ifeq ($(TARGET_ARCH), x86_64)
 LIBOBJS+=helper.o helper2.o
+ifeq ($(USE_KVM), 1)
 LIBOBJS+=qemu-kvm-x86.o kvm-tpr-opt.o
 LIBOBJS+=qemu-kvm-helper.o
 endif
+endif
 
 ifeq ($(TARGET_BASE_ARCH), ppc)
 LIBOBJS+= op_helper.o helper.o
+ifeq ($(USE_KVM), 1)
 LIBOBJS+= qemu-kvm-powerpc.o
 endif
+endif
 
 ifeq ($(TARGET_BASE_ARCH), mips)
 LIBOBJS+= op_helper.o helper.o
@@ -347,8 +357,10 @@ endif
 
 ifeq ($(TARGET_BASE_ARCH), ia64)
 LIBOBJS+=op_helper.o firmware.o
+ifeq ($(USE_KVM), 1)
 LIBOBJS+=qemu-kvm-ia64.o
 endif
+endif
 
 ifeq ($(TARGET_BASE_ARCH), cris)
 LIBOBJS+= op_helper.o helper.o
diff --git a/qemu/block-raw-posix.c b/qemu/block-raw-posix.c
index 7e0c39e..74657fb 100644
--- a/qemu/block-raw-posix.c
+++ b/qemu/block-raw-posix.c
@@ -346,8 +346,8 @@ void qemu_aio_wait_start(void)
 
     if (!aio_initialized)
         qemu_aio_init();
-#ifdef USE_KVM
-    if (kvm_allowed) {
+#ifndef QEMU_IMG
+    if (kvm_enabled()) {
         qemu_kvm_aio_wait_start();
         return;
     }
@@ -365,9 +365,7 @@ void qemu_aio_wait(void)
 #ifndef QEMU_IMG
     if (qemu_bh_poll())
         return;
-#endif
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
         qemu_kvm_aio_wait();
         qemu_aio_poll();
         return;
@@ -381,8 +379,8 @@ void qemu_aio_wait(void)
 
 void qemu_aio_wait_end(void)
 {
-#ifdef USE_KVM
-    if (kvm_allowed) {
+#ifndef QEMU_IMG
+    if (kvm_enabled()) {
         qemu_kvm_aio_wait_end();
         return;
     }
diff --git a/qemu/configure b/qemu/configure
index 78c35a4..bf624ed 100755
--- a/qemu/configure
+++ b/qemu/configure
@@ -300,7 +300,7 @@ for opt do
   ;;
   --disable-kqemu) kqemu="no"
   ;;
-  --enable-kvm) kvm="yes"
+  --disable-kvm) kvm="no"
   ;;
   --enable-profiler) profiler="yes"
   ;;
@@ -415,7 +415,7 @@ echo ""
 echo "kqemu kernel acceleration support:"
 echo "  --disable-kqemu          disable kqemu support"
 echo "  --kernel-path=PATH       set the kernel path (configure probes it)"
-echo "  --enable-kvm             enable kernel virtual machine support"
+echo "  --disable-kvm            disable kernel virtual machine support"
 echo ""
 echo "Advanced options (experts only):"
 echo "  --source-path=PATH       path of source code [$source_path]"
@@ -1100,6 +1100,7 @@ configure_kvm() {
   if test $kvm = "yes" -a "$target_softmmu" = "yes" -a \
           \( "$cpu" = "i386" -o "$cpu" = "x86_64" -o "$cpu" = "ia64" -o "$cpu" = "powerpc" \); then
     echo "#define USE_KVM 1" >> $config_h
+    echo "USE_KVM=1" >> $config_mak
     echo "CONFIG_KVM_KERNEL_INC=$kernel_path/include" >> $config_mak
   fi
 }
diff --git a/qemu/cpu-exec.c b/qemu/cpu-exec.c
index 42be8ec..9d05ef9 100644
--- a/qemu/cpu-exec.c
+++ b/qemu/cpu-exec.c
@@ -36,10 +36,7 @@
 #include <sys/ucontext.h>
 #endif
 
-#ifdef USE_KVM
 #include "qemu-kvm.h"
-extern int kvm_allowed;
-#endif
 
 int tb_invalidated_flag;
 
@@ -487,12 +484,10 @@ int cpu_exec(CPUState *env1)
             }
 #endif
 
-#ifdef USE_KVM
-            if (kvm_allowed) {
+            if (kvm_enabled()) {
                 kvm_cpu_exec(env);
                 longjmp(env->jmp_env, 1);
             }
-#endif
             T0 = 0; /* force lookup of first TB */
             for(;;) {
                 SAVE_GLOBALS();
diff --git a/qemu/exec.c b/qemu/exec.c
index f8e6713..06eaf62 100644
--- a/qemu/exec.c
+++ b/qemu/exec.c
@@ -35,10 +35,8 @@
 
 #include "cpu.h"
 #include "exec-all.h"
-#ifdef USE_KVM
 #include "dyngen.h"
 #include "qemu-kvm.h"
-#endif
 #if defined(CONFIG_USER_ONLY)
 #include <qemu.h>
 #endif
@@ -88,11 +86,6 @@
 #define TARGET_PHYS_ADDR_SPACE_BITS 32
 #endif
 
-#ifdef USE_KVM
-extern int kvm_allowed;
-extern kvm_context_t kvm_context;
-#endif
-
 TranslationBlock tbs[CODE_GEN_MAX_BLOCKS];
 TranslationBlock *tb_phys_hash[CODE_GEN_PHYS_HASH_SIZE];
 int nb_tbs;
@@ -1147,10 +1140,8 @@ int cpu_breakpoint_insert(CPUState *env, target_ulong pc)
         return -1;
     env->breakpoints[env->nb_breakpoints++] = pc;
 
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
 	kvm_update_debugger(env);
-#endif
 
     breakpoint_invalidate(env, pc);
     return 0;
@@ -1174,10 +1165,8 @@ int cpu_breakpoint_remove(CPUState *env, target_ulong pc)
     if (i < env->nb_breakpoints)
       env->breakpoints[i] = env->breakpoints[env->nb_breakpoints];
 
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
 	kvm_update_debugger(env);
-#endif
     
     breakpoint_invalidate(env, pc);
     return 0;
@@ -1197,11 +1186,9 @@ void cpu_single_step(CPUState *env, int enabled)
         /* XXX: only flush what is necessary */
         tb_flush(env);
     }
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
 	kvm_update_debugger(env);
 #endif
-#endif
 }
 
 /* enable or disable low levels log */
@@ -1248,10 +1235,9 @@ void cpu_interrupt(CPUState *env, int mask)
     static int interrupt_lock;
 
     env->interrupt_request |= mask;
-#ifdef USE_KVM
-    if (kvm_allowed && !kvm_irqchip_in_kernel(kvm_context))
+    if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
 	kvm_update_interrupt_request(env);
-#endif
+
     /* if the cpu is currently executing code, we must unlink it and
        all the potentially executing TB */
     tb = env->current_tb;
@@ -1599,9 +1585,8 @@ int cpu_physical_memory_set_dirty_tracking(int enable)
 {
     int r=0;
 
-#ifdef USE_KVM
-    r = kvm_physical_memory_set_dirty_tracking(enable);
-#endif
+    if (kvm_enabled())
+	r = kvm_physical_memory_set_dirty_tracking(enable);
     in_migration = enable;
     return r;
 }
@@ -2662,11 +2647,11 @@ void cpu_physical_memory_rw(target_phys_addr_t addr, uint8_t *buf,
                     phys_ram_dirty[addr1 >> TARGET_PAGE_BITS] |=
                         (0xff & ~CODE_DIRTY_FLAG);
                 }
-#ifdef USE_KVM
 		/* qemu doesn't execute guest code directly, but kvm does
 		   therefore fluch instruction caches */
-		flush_icache_range((unsigned long)ptr, ((unsigned long)ptr)+l);
-#endif
+		if (kvm_enabled())
+		    flush_icache_range((unsigned long)ptr,
+				       ((unsigned long)ptr)+l);
             }
         } else {
             if ((pd & ~TARGET_PAGE_MASK) > IO_MEM_ROM &&
diff --git a/qemu/gdbstub.c b/qemu/gdbstub.c
index 38b699f..2252084 100644
--- a/qemu/gdbstub.c
+++ b/qemu/gdbstub.c
@@ -894,9 +894,8 @@ static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             env->eip = addr;
-#ifdef USE_KVM
-	    kvm_load_registers(env);
-#endif
+	    if (kvm_enabled())
+		kvm_load_registers(env);
 #elif defined (TARGET_PPC)
             env->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -923,9 +922,8 @@ static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
             addr = strtoull(p, (char **)&p, 16);
 #if defined(TARGET_I386)
             env->eip = addr;
-#ifdef USE_KVM
-	    kvm_load_registers(env);
-#endif
+	    if (kvm_enabled())
+		kvm_load_registers(env);
 #elif defined (TARGET_PPC)
             env->nip = addr;
 #elif defined (TARGET_SPARC)
@@ -977,9 +975,8 @@ static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
         }
         break;
     case 'g':
-#ifdef USE_KVM
-	kvm_save_registers(env);
-#endif
+	if (kvm_enabled())
+	    kvm_save_registers(env);
         reg_size = cpu_gdb_read_registers(env, mem_buf);
         memtohex(buf, mem_buf, reg_size);
         put_packet(s, buf);
@@ -989,9 +986,8 @@ static int gdb_handle_packet(GDBState *s, CPUState *env, const char *line_buf)
         len = strlen(p) / 2;
         hextomem((uint8_t *)registers, p, len);
         cpu_gdb_write_registers(env, mem_buf, len);
-#ifdef USE_KVM
-	kvm_load_registers(env);
-#endif
+	if (kvm_enabled())
+	    kvm_load_registers(env);
         put_packet(s, "OK");
         break;
     case 'm':
diff --git a/qemu/hw/apic.c b/qemu/hw/apic.c
index a47c366..c26a18d 100644
--- a/qemu/hw/apic.c
+++ b/qemu/hw/apic.c
@@ -21,11 +21,7 @@
 #include "pc.h"
 #include "qemu-timer.h"
 
-#ifdef USE_KVM
 #include "qemu-kvm.h"
-extern int kvm_allowed;
-extern kvm_context_t kvm_context;
-#endif
 
 //#define DEBUG_APIC
 //#define DEBUG_IOAPIC
@@ -407,11 +403,10 @@ static void apic_init_ipi(APICState *s)
     s->initial_count = 0;
     s->initial_count_load_time = 0;
     s->next_time = 0;
-#ifdef USE_KVM
-    if (kvm_allowed && !kvm_irqchip_in_kernel(kvm_context))
+
+    if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
 	if (s->cpu_env)
 	    kvm_apic_init(s->cpu_env);
-#endif
 }
 
 /* send a SIPI message to the CPU to start it */
@@ -424,10 +419,8 @@ static void apic_startup(APICState *s, int vector_num)
     cpu_x86_load_seg_cache(env, R_CS, vector_num << 8, vector_num << 12,
                            0xffff, 0);
     env->hflags &= ~HF_HALTED_MASK;
-#if USE_KVM
-    if (kvm_allowed && !kvm_irqchip_in_kernel(kvm_context))
+    if (kvm_enabled() && !qemu_kvm_irqchip_in_kernel())
 	kvm_update_after_sipi(env);
-#endif
 }
 
 static void apic_deliver(APICState *s, uint8_t dest, uint8_t dest_mode,
@@ -753,8 +746,6 @@ static void apic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t val)
     }
 }
 
-#ifdef USE_KVM
-
 #ifdef KVM_CAP_IRQCHIP
 
 static inline uint32_t kapic_reg(struct kvm_lapic_state *kapic, int reg_id)
@@ -832,20 +823,16 @@ static void kvm_kernel_lapic_load_from_user(APICState *s)
 
 #endif
 
-#endif
-
 static void apic_save(QEMUFile *f, void *opaque)
 {
     APICState *s = opaque;
     int i;
 
-#ifdef USE_KVM
 #ifdef KVM_CAP_IRQCHIP
-    if (kvm_allowed && kvm_irqchip_in_kernel(kvm_context)) {
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
         kvm_kernel_lapic_save_to_user(s);
     }
 #endif
-#endif
 
     qemu_put_be32s(f, &s->apicbase);
     qemu_put_8s(f, &s->id);
@@ -910,13 +897,11 @@ static int apic_load(QEMUFile *f, void *opaque, int version_id)
     if (version_id >= 2)
         qemu_get_timer(f, s->timer);
 
-#ifdef USE_KVM
 #ifdef KVM_CAP_IRQCHIP
-    if (kvm_allowed && kvm_irqchip_in_kernel(kvm_context)) {
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
         kvm_kernel_lapic_load_from_user(s);
     }
 #endif
-#endif
 
     return 0;
 }
@@ -932,13 +917,11 @@ static void apic_reset(void *opaque)
      * processor when local APIC is enabled.
      */
     s->lvt[APIC_LVT_LINT0] = 0x700;
-#ifdef USE_KVM
 #ifdef KVM_CAP_IRQCHIP
-    if (kvm_allowed && kvm_irqchip_in_kernel(kvm_context)) {
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
         kvm_kernel_lapic_load_from_user(s);
     }
 #endif
-#endif
 }
 
 static CPUReadMemoryFunc *apic_mem_read[3] = {
@@ -1131,7 +1114,6 @@ static void ioapic_mem_writel(void *opaque, target_phys_addr_t addr, uint32_t va
     }
 }
 
-#ifdef USE_KVM
 static void kvm_kernel_ioapic_save_to_user(IOAPICState *s)
 {
 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
@@ -1170,18 +1152,15 @@ static void kvm_kernel_ioapic_load_from_user(IOAPICState *s)
     kvm_set_irqchip(kvm_context, &chip);
 #endif
 }
-#endif
 
 static void ioapic_save(QEMUFile *f, void *opaque)
 {
     IOAPICState *s = opaque;
     int i;
 
-#ifdef USE_KVM
-    if (kvm_allowed && kvm_irqchip_in_kernel(kvm_context)) {
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
         kvm_kernel_ioapic_save_to_user(s);
     }
-#endif
 
     qemu_put_8s(f, &s->id);
     qemu_put_8s(f, &s->ioregsel);
@@ -1204,11 +1183,9 @@ static int ioapic_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_be64s(f, &s->ioredtbl[i]);
     }
 
-#ifdef USE_KVM
-    if (kvm_allowed && kvm_irqchip_in_kernel(kvm_context)) {
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
         kvm_kernel_ioapic_load_from_user(s);
     }
-#endif
 
     return 0;
 }
diff --git a/qemu/hw/cirrus_vga.c b/qemu/hw/cirrus_vga.c
index ee7ec1c..1915c73 100644
--- a/qemu/hw/cirrus_vga.c
+++ b/qemu/hw/cirrus_vga.c
@@ -34,6 +34,7 @@
 #ifndef _WIN32
 #include <sys/mman.h>
 #endif
+#include "qemu-kvm.h"
 
 /*
  * TODO:
@@ -237,13 +238,11 @@ typedef struct CirrusVGAState {
     int cirrus_linear_io_addr;
     int cirrus_linear_bitblt_io_addr;
     int cirrus_mmio_io_addr;
-#ifdef USE_KVM
     unsigned long cirrus_lfb_addr;
     unsigned long cirrus_lfb_end;
     int aliases_enabled;
     uint32_t aliased_bank_base[2];
     uint32_t aliased_bank_limit[2];
-#endif
     uint32_t cirrus_addr_mask;
     uint32_t linear_mmio_mask;
     uint8_t cirrus_shadow_gr0;
@@ -2601,12 +2600,6 @@ static CPUWriteMemoryFunc *cirrus_linear_bitblt_write[3] = {
     cirrus_linear_bitblt_writel,
 };
 
-#ifdef USE_KVM
-
-#include "qemu-kvm.h"
-
-extern kvm_context_t kvm_context;
-
 void *set_vram_mapping(unsigned long begin, unsigned long end)
 {
     void *vram_pointer = NULL;
@@ -2616,8 +2609,8 @@ void *set_vram_mapping(unsigned long begin, unsigned long end)
     end = begin + VGA_RAM_SIZE;
     end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;
 
-    vram_pointer = kvm_create_phys_mem(kvm_context, begin, end - begin,
-				       1, 1);
+    if (kvm_enabled())
+	vram_pointer = kvm_cpu_create_phys_mem(begin, end - begin, 1, 1);
 
     if (vram_pointer == NULL) {
         printf("set_vram_mapping: cannot allocate memory: %m\n");
@@ -2636,7 +2629,8 @@ int unset_vram_mapping(unsigned long begin, unsigned long end)
     begin = begin & TARGET_PAGE_MASK;
     end = (end + TARGET_PAGE_SIZE -1 ) & TARGET_PAGE_MASK;
 
-    kvm_destroy_phys_mem(kvm_context, begin, end - begin);
+    if (kvm_enabled())
+	kvm_cpu_destroy_phys_mem(begin, end - begin);
 
     return 0;
 }
@@ -2669,20 +2663,19 @@ static void kvm_update_vga_alias(CirrusVGAState *s, int ok, int bank,
 
 static void kvm_update_vga_aliases(CirrusVGAState *s, int ok)
 {
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
 	kvm_update_vga_alias(s, ok, 0, 0xc0000);
 	kvm_update_vga_alias(s, ok, 1, s->map_addr);
     }
     s->aliases_enabled = ok;
 }
 #endif
-#endif
 
 /* Compute the memory access functions */
 static void cirrus_update_memory_access(CirrusVGAState *s)
 {
     unsigned mode;
-#if defined(USE_KVM) && defined(CONFIG_X86)
+#ifdef CONFIG_X86
     int want_vga_alias = 0;
 #endif
 
@@ -2699,8 +2692,7 @@ static void cirrus_update_memory_access(CirrusVGAState *s)
 
 	mode = s->gr[0x05] & 0x7;
 	if (mode < 4 || mode > 5 || ((s->gr[0x0B] & 0x4) == 0)) {
-#ifdef USE_KVM
-            if (kvm_allowed && s->cirrus_lfb_addr && s->cirrus_lfb_end &&
+            if (kvm_enabled() && s->cirrus_lfb_addr && s->cirrus_lfb_end &&
 		!s->map_addr) {
                 void *vram_pointer, *old_vram;
 
@@ -2717,21 +2709,19 @@ static void cirrus_update_memory_access(CirrusVGAState *s)
                 s->map_end = s->cirrus_lfb_end;
             }
 #ifdef CONFIG_X86
-	    if (kvm_allowed
+	    if (kvm_enabled()
 		&& !(s->cirrus_srcptr != s->cirrus_srcptr_end)
 		&& !((s->sr[0x07] & 0x01) == 0)
 		&& !((s->gr[0x0B] & 0x14) == 0x14)
 		&& !(s->gr[0x0B] & 0x02))
 		want_vga_alias = 1;
 #endif
-#endif
             s->cirrus_linear_write[0] = cirrus_linear_mem_writeb;
             s->cirrus_linear_write[1] = cirrus_linear_mem_writew;
             s->cirrus_linear_write[2] = cirrus_linear_mem_writel;
         } else {
         generic_io:
-#ifdef USE_KVM
-            if (kvm_allowed && s->cirrus_lfb_addr && s->cirrus_lfb_end &&
+            if (kvm_enabled() && s->cirrus_lfb_addr && s->cirrus_lfb_end &&
 		s->map_addr) {
 		int error;
                 void *old_vram = NULL;
@@ -2745,13 +2735,12 @@ static void cirrus_update_memory_access(CirrusVGAState *s)
                     munmap(old_vram, s->map_end - s->map_addr);
                 s->map_addr = s->map_end = 0;
             }
-#endif
             s->cirrus_linear_write[0] = cirrus_linear_writeb;
             s->cirrus_linear_write[1] = cirrus_linear_writew;
             s->cirrus_linear_write[2] = cirrus_linear_writel;
         }
     }
-#if defined(USE_KVM) && defined(CONFIG_X86)
+#if defined(CONFIG_X86)
     kvm_update_vga_aliases(s, want_vga_alias);
 #endif
 
@@ -3149,12 +3138,10 @@ static void cirrus_vga_save(QEMUFile *f, void *opaque)
     /* XXX: we do not save the bitblt state - we assume we do not save
        the state when the blitter is active */
 
-#ifdef USE_KVM
-    if (kvm_allowed) { /* XXX: KVM images ought to be loadable in QEMU */
+    if (kvm_enabled()) { /* XXX: KVM images ought to be loadable in QEMU */
 	qemu_put_be32s(f, &s->real_vram_size);
 	qemu_put_buffer(f, s->vram_ptr, s->real_vram_size);
     }
-#endif
 }
 
 static int cirrus_vga_load(QEMUFile *f, void *opaque, int version_id)
@@ -3205,8 +3192,7 @@ static int cirrus_vga_load(QEMUFile *f, void *opaque, int version_id)
     qemu_get_be32s(f, &s->hw_cursor_x);
     qemu_get_be32s(f, &s->hw_cursor_y);
 
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
         int real_vram_size;
         qemu_get_be32s(f, &real_vram_size);
         if (real_vram_size != s->real_vram_size) {
@@ -3218,7 +3204,6 @@ static int cirrus_vga_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_buffer(f, s->vram_ptr, real_vram_size);
         cirrus_update_memory_access(s);
     }
-#endif
 
 
     /* force refresh */
@@ -3376,8 +3361,7 @@ static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
     /* XXX: add byte swapping apertures */
     cpu_register_physical_memory(addr, s->vram_size,
 				 s->cirrus_linear_io_addr);
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
 	s->cirrus_lfb_addr = addr;
 	s->cirrus_lfb_end = addr + VGA_RAM_SIZE;
 
@@ -3385,7 +3369,6 @@ static void cirrus_pci_lfb_map(PCIDevice *d, int region_num,
 	    (s->cirrus_lfb_end != s->map_end))
 	    printf("cirrus vga map change while on lfb mode\n");
     }
-#endif
 
     cpu_register_physical_memory(addr + 0x1000000, 0x400000,
 				 s->cirrus_linear_bitblt_io_addr);
diff --git a/qemu/hw/i8259.c b/qemu/hw/i8259.c
index 071f9f1..b266119 100644
--- a/qemu/hw/i8259.c
+++ b/qemu/hw/i8259.c
@@ -26,9 +26,7 @@
 #include "isa.h"
 #include "console.h"
 
-#ifdef USE_KVM
 #include "qemu-kvm.h"
-#endif
 
 /* debug PIC */
 //#define DEBUG_PIC
@@ -185,15 +183,11 @@ int64_t irq_time[16];
 static void i8259_set_irq(void *opaque, int irq, int level)
 {
     PicState2 *s = opaque;
-#ifdef USE_KVM
 #ifdef KVM_CAP_IRQCHIP
-    extern int kvm_set_irq(int irq, int level);
-
-    if (kvm_allowed)
+    if (kvm_enabled())
 	if (kvm_set_irq(irq, level))
 	    return;
 #endif
-#endif
 #if defined(DEBUG_PIC) || defined(DEBUG_IRQ_COUNT)
     if (level != irq_level[irq]) {
 #if defined(DEBUG_PIC)
@@ -477,11 +471,6 @@ static uint32_t elcr_ioport_read(void *opaque, uint32_t addr1)
     return s->elcr;
 }
 
-#ifdef USE_KVM
-#include "qemu-kvm.h"
-extern int kvm_allowed;
-extern kvm_context_t kvm_context;
-
 static void kvm_kernel_pic_save_to_user(PicState *s)
 {
 #if defined(KVM_CAP_IRQCHIP) && defined(TARGET_I386)
@@ -544,17 +533,14 @@ static void kvm_kernel_pic_load_from_user(PicState *s)
     kvm_set_irqchip(kvm_context, &chip);
 #endif
 }
-#endif
 
 static void pic_save(QEMUFile *f, void *opaque)
 {
     PicState *s = opaque;
 
-#ifdef USE_KVM
-    if (kvm_allowed && kvm_irqchip_in_kernel(kvm_context)) {
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
         kvm_kernel_pic_save_to_user(s);
     }
-#endif
 
     qemu_put_8s(f, &s->last_irr);
     qemu_put_8s(f, &s->irr);
@@ -598,11 +584,9 @@ static int pic_load(QEMUFile *f, void *opaque, int version_id)
     qemu_get_8s(f, &s->single_mode);
     qemu_get_8s(f, &s->elcr);
 
-#ifdef USE_KVM
-    if (kvm_allowed && kvm_irqchip_in_kernel(kvm_context)) {
+    if (kvm_enabled() && qemu_kvm_irqchip_in_kernel()) {
         kvm_kernel_pic_load_from_user(s);
     }
-#endif
 
     return 0;
 }
diff --git a/qemu/hw/ipf.c b/qemu/hw/ipf.c
index b1c1389..b5b48ac 100644
--- a/qemu/hw/ipf.c
+++ b/qemu/hw/ipf.c
@@ -40,10 +40,7 @@
 #include "dyngen.h"
 #include <unistd.h>
 
-#ifdef USE_KVM
 #include "qemu-kvm.h"
-extern int kvm_allowed;
-#endif
 
 #define FW_FILENAME "Flash.fd"
 
@@ -309,11 +306,6 @@ static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
     nb_ne2k++;
 }
 
-#ifdef USE_KVM
-extern kvm_context_t kvm_context;
-extern int kvm_allowed;
-#endif
-
 /* Itanium hardware initialisation */
 static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,
                      const char *boot_device, DisplayState *ds,
@@ -367,9 +359,8 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,
     }
 
 	/* allocate RAM */
-#ifdef USE_KVM
 #ifdef KVM_CAP_USER_MEMORY
-	if (kvm_allowed && kvm_qemu_check_extension(KVM_CAP_USER_MEMORY)) {
+	if (kvm_enabled() && kvm_qemu_check_extension(KVM_CAP_USER_MEMORY)) {
 		ram_addr = qemu_ram_alloc(0xa0000);
 		cpu_register_physical_memory(0, 0xa0000, ram_addr);
 		kvm_cpu_register_physical_memory(0, 0xa0000, ram_addr);
@@ -386,7 +377,6 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,
 				ram_addr);
 	} else
 #endif
-#endif
     {
         ram_addr = qemu_ram_alloc(ram_size);
         cpu_register_physical_memory(0, ram_size, ram_addr);
@@ -398,16 +388,13 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,
 	if (above_4g_mem_size > 0) {
 		ram_addr = qemu_ram_alloc(above_4g_mem_size);
 		cpu_register_physical_memory(0x100000000, above_4g_mem_size, ram_addr);
-#ifdef USE_KVM
-	if (kvm_allowed)
+	if (kvm_enabled())
 		kvm_cpu_register_physical_memory(0x100000000, above_4g_mem_size,
 					ram_addr);
-#endif
 	}
 
 	/*Load firware to its proper position.*/
-#ifdef USE_KVM
-	if (kvm_allowed) {
+	if (kvm_enabled()) {
 		int r;
 		unsigned long  image_size;
 		char *image = NULL;
@@ -446,7 +433,6 @@ static void ipf_init1(ram_addr_t ram_size, int vga_ram_size,
 			(unsigned long)fw_image_start + image_size);
 		kvm_ia64_build_hob(ram_size + above_4g_mem_size, smp_cpus, fw_start);
 	}
-#endif
 
     cpu_irq = qemu_allocate_irqs(pic_irq_request, first_cpu, 1);
     i8259 = i8259_init(cpu_irq[0]);
diff --git a/qemu/hw/pc.c b/qemu/hw/pc.c
index 652b263..0e0d051 100644
--- a/qemu/hw/pc.c
+++ b/qemu/hw/pc.c
@@ -32,10 +32,7 @@
 #include "smbus.h"
 #include "boards.h"
 
-#ifdef USE_KVM
 #include "qemu-kvm.h"
-extern int kvm_allowed;
-#endif
 
 /* output Bochs bios info messages */
 //#define DEBUG_BIOS
@@ -711,11 +708,6 @@ static void pc_init_ne2k_isa(NICInfo *nd, qemu_irq *pic)
     nb_ne2k++;
 }
 
-#ifdef USE_KVM
-extern kvm_context_t kvm_context;
-extern int kvm_allowed;
-#endif
-
 static int load_option_rom(const char *filename, int offset)
 {
     ram_addr_t option_rom_offset;
@@ -738,12 +730,10 @@ static int load_option_rom(const char *filename, int offset)
     size = (size + 4095) & ~4095;
     cpu_register_physical_memory(0xd0000 + offset,
 				 size, option_rom_offset | IO_MEM_ROM);
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
 	    kvm_cpu_register_physical_memory(0xd0000 + offset,
                                              size, option_rom_offset |
                                              IO_MEM_ROM);
-#endif
     return size;
 }
 
@@ -806,9 +796,8 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
     }
 
     /* allocate RAM */
-#ifdef USE_KVM
- #ifdef KVM_CAP_USER_MEMORY
-    if (kvm_allowed && kvm_qemu_check_extension(KVM_CAP_USER_MEMORY)) {
+#ifdef KVM_CAP_USER_MEMORY
+    if (kvm_enabled() && kvm_qemu_check_extension(KVM_CAP_USER_MEMORY)) {
         ram_addr = qemu_ram_alloc(0xa0000);
         cpu_register_physical_memory(0, 0xa0000, ram_addr);
         kvm_cpu_register_physical_memory(0, 0xa0000, ram_addr);
@@ -819,7 +808,6 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
         kvm_cpu_register_physical_memory(0x100000, ram_size - 0x100000,
                                          ram_addr);
     } else
- #endif
 #endif
     {
         ram_addr = qemu_ram_alloc(ram_size);
@@ -867,21 +855,19 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
     if (above_4g_mem_size > 0) {
         ram_addr = qemu_ram_alloc(above_4g_mem_size);
         cpu_register_physical_memory(0x100000000, above_4g_mem_size, ram_addr);
-#ifdef USE_KVM
-    if (kvm_allowed)
-        kvm_cpu_register_physical_memory(0x100000000, above_4g_mem_size,
-                                         ram_addr);
-#endif
+
+	if (kvm_enabled())
+	    kvm_cpu_register_physical_memory(0x100000000,
+					     above_4g_mem_size,
+					     ram_addr);
     }
 
     /* setup basic memory access */
     cpu_register_physical_memory(0xc0000, 0x10000,
                                  vga_bios_offset | IO_MEM_ROM);
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
         kvm_cpu_register_physical_memory(0xc0000, 0x10000,
                                          vga_bios_offset | IO_MEM_ROM);
-#endif
 
     /* map the last 128KB of the BIOS in ISA space */
     isa_bios_size = bios_size;
@@ -893,12 +879,10 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
     cpu_register_physical_memory(0x100000 - isa_bios_size,
                                  isa_bios_size,
                                  (bios_offset + bios_size - isa_bios_size) /* | IO_MEM_ROM */);
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
         kvm_cpu_register_physical_memory(0x100000 - isa_bios_size,
                                          isa_bios_size,
                                          (bios_offset + bios_size - isa_bios_size) | IO_MEM_ROM);
-#endif
 
     opt_rom_offset = 0;
     for (i = 0; i < nb_option_roms; i++)
@@ -912,8 +896,7 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
     /* map all the bios at the top of memory */
     cpu_register_physical_memory((uint32_t)(-bios_size),
                                  bios_size, bios_offset | IO_MEM_ROM);
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
         int r;
 #ifdef KVM_CAP_USER_MEMORY
         r = kvm_qemu_check_extension(KVM_CAP_USER_MEMORY);
@@ -923,14 +906,13 @@ static void pc_init1(ram_addr_t ram_size, int vga_ram_size,
         else
 #endif
         {
-            bios_mem = kvm_create_phys_mem(kvm_context, (uint32_t)(-bios_size),
-                                           bios_size, 0, 1);
+            bios_mem = kvm_cpu_create_phys_mem((uint32_t)(-bios_size),
+					       bios_size, 0, 1);
 	    if (!bios_mem)
                 exit(1);
             memcpy(bios_mem, phys_ram_base + bios_offset, bios_size);
         }
     }
-#endif
 
     bochs_bios_init();
 
diff --git a/qemu/hw/ppc440_bamboo.c b/qemu/hw/ppc440_bamboo.c
index 29c2efc..d3c2bfd 100644
--- a/qemu/hw/ppc440_bamboo.c
+++ b/qemu/hw/ppc440_bamboo.c
@@ -12,9 +12,7 @@
 
 #define KERNEL_LOAD_ADDR 0x400000 /* uboot loader puts kernel at 4MB */
 
-#if USE_KVM
 #include "qemu-kvm.h"
-#endif
 
 /* PPC 440 refrence demo board
  *
@@ -75,9 +73,9 @@ void bamboo_init(ram_addr_t ram_size, int vga_ram_size,
 
 	/* Register mem */
 	cpu_register_physical_memory(0, ram_size, 0);
-#if USE_KVM
-	kvm_cpu_register_physical_memory(0, ram_size, 0);
-#endif
+	if (kvm_enabled())
+	    kvm_cpu_register_physical_memory(0, ram_size, 0);
+
 	/* load kernel with uboot loader */
 	printf("%s: load kernel\n", __func__);
 	kernel_size = load_uboot(kernel_filename, &ep, &is_linux);
@@ -101,18 +99,18 @@ void bamboo_init(ram_addr_t ram_size, int vga_ram_size,
 		}
 	}
 
-#if USE_KVM
-	/* XXX insert TLB entries */
-	env->gpr[1] = (16<<20) - 8;
-	env->gpr[4] = initrd_base;
-	env->gpr[5] = initrd_size;
+	if (kvm_enabled()) {
+	    /* XXX insert TLB entries */
+	    env->gpr[1] = (16<<20) - 8;
+	    env->gpr[4] = initrd_base;
+	    env->gpr[5] = initrd_size;
 
-	env->nip = ep;
+	    env->nip = ep;
 
-	env->cpu_index = 0;
-	printf("%s: loading kvm registers\n", __func__);
-	kvm_load_registers(env);
-#endif
+	    env->cpu_index = 0;
+	    printf("%s: loading kvm registers\n", __func__);
+	    kvm_load_registers(env);
+	}
 
 	printf("%s: DONE\n", __func__);
 }
diff --git a/qemu/hw/vga.c b/qemu/hw/vga.c
index 44e6834..222a39c 100644
--- a/qemu/hw/vga.c
+++ b/qemu/hw/vga.c
@@ -27,6 +27,7 @@
 #include "pci.h"
 #include "vga_int.h"
 #include "pixel_ops.h"
+#include "qemu-kvm.h"
 
 #include <sys/mman.h>
 
@@ -1414,11 +1415,6 @@ void vga_invalidate_scanlines(VGAState *s, int y1, int y2)
     }
 }
 
-#ifdef USE_KVM
-
-#include "libkvm.h"
-extern kvm_context_t kvm_context;
-
 static int bitmap_get_dirty(unsigned long *bitmap, unsigned nr)
 {
     unsigned word = nr / ((sizeof bitmap[0]) * 8);
@@ -1428,11 +1424,6 @@ static int bitmap_get_dirty(unsigned long *bitmap, unsigned nr)
     return (bitmap[word] >> bit) & 1;
 }
 
-#endif
-
-#ifdef USE_KVM
-extern int kvm_allowed;
-#endif
 
 /*
  * graphic modes
@@ -1446,24 +1437,20 @@ static void vga_draw_graphic(VGAState *s, int full_update)
     uint32_t v, addr1, addr;
     long page0, page1, page_min, page_max;
     vga_draw_line_func *vga_draw_line;
-
-#ifdef USE_KVM
-
     /* HACK ALERT */
-#define BITMAP_SIZE ((8*1024*1024) / 4096 / 8 / sizeof(long))
-    unsigned long bitmap[BITMAP_SIZE];
+#define VGA_BITMAP_SIZE ((8*1024*1024) / 4096 / 8 / sizeof(long))
+    unsigned long bitmap[VGA_BITMAP_SIZE];
 #ifndef TARGET_IA64
     int r;
-    if (kvm_allowed) {
-	    r = kvm_get_dirty_pages(kvm_context, s->map_addr, &bitmap);
+    if (kvm_enabled()) {
+	    r = qemu_kvm_get_dirty_pages(s->map_addr, &bitmap);
 	    if (r < 0)
 		    fprintf(stderr, "kvm: get_dirty_pages returned %d\n", r);
     }
 #else
-    memset(bitmap, 0xff, BITMAP_SIZE*sizeof(long));
+    memset(bitmap, 0xff, VGA_BITMAP_SIZE*sizeof(long));
    //FIXME:Always flush full screen before log dirty ready!!
 #endif
-#endif
 
     full_update |= update_basic_params(s);
 
@@ -1571,20 +1558,17 @@ static void vga_draw_graphic(VGAState *s, int full_update)
         update = full_update |
             cpu_physical_memory_get_dirty(page0, VGA_DIRTY_FLAG) |
             cpu_physical_memory_get_dirty(page1, VGA_DIRTY_FLAG);
-#ifdef USE_KVM
-	if (kvm_allowed) {
+	if (kvm_enabled()) {
 		update |= bitmap_get_dirty(bitmap, (page0 - s->vram_offset) >> TARGET_PAGE_BITS);
 		update |= bitmap_get_dirty(bitmap, (page1 - s->vram_offset) >> TARGET_PAGE_BITS);
 	}
-#endif
+
         if ((page1 - page0) > TARGET_PAGE_SIZE) {
             /* if wide line, can use another page */
             update |= cpu_physical_memory_get_dirty(page0 + TARGET_PAGE_SIZE,
                                                     VGA_DIRTY_FLAG);
-#ifdef USE_KVM
-	    if (kvm_allowed)
-		    update |= bitmap_get_dirty(bitmap, (page0 - s->vram_offset) >> TARGET_PAGE_BITS);
-#endif
+	    if (kvm_enabled())
+		update |= bitmap_get_dirty(bitmap, (page0 - s->vram_offset) >> TARGET_PAGE_BITS);
         }
         /* explicit invalidation for the hardware cursor */
         update |= (s->invalidated_y_table[y >> 5] >> (y & 0x1f)) & 1;
@@ -1838,8 +1822,7 @@ static void vga_map(PCIDevice *pci_dev, int region_num,
         cpu_register_physical_memory(addr, s->bios_size, s->bios_offset);
     } else {
         cpu_register_physical_memory(addr, s->vram_size, s->vram_offset);
-#ifdef USE_KVM
-        if (kvm_allowed) {
+        if (kvm_enabled()) {
             unsigned long vga_ram_begin, vga_ram_end;
             void *vram_pointer, *old_vram;
 
@@ -1870,7 +1853,6 @@ static void vga_map(PCIDevice *pci_dev, int region_num,
                 s->map_end  = vga_ram_end;
             }
         }
-#endif
     }
 }
 
@@ -2037,14 +2019,10 @@ void vga_common_init(VGAState *s, DisplayState *ds, uint8_t *vga_ram_base,
 
     vga_reset(s);
 
-#ifndef USE_KVM
-    s->vram_ptr = vga_ram_base;
-#else
-    if (kvm_allowed)
-	    s->vram_ptr = qemu_malloc(vga_ram_size);
+    if (kvm_enabled())
+	s->vram_ptr = qemu_malloc(vga_ram_size);
     else
-	    s->vram_ptr = vga_ram_base;
-#endif
+	s->vram_ptr = vga_ram_base;
     s->vram_offset = vga_ram_offset;
     s->vram_size = vga_ram_size;
     s->ds = ds;
diff --git a/qemu/hw/vga_int.h b/qemu/hw/vga_int.h
index f08700e..912d977 100644
--- a/qemu/hw/vga_int.h
+++ b/qemu/hw/vga_int.h
@@ -79,14 +79,6 @@
 #define CH_ATTR_SIZE (160 * 100)
 #define VGA_MAX_HEIGHT 2048
 
-#ifdef USE_KVM
-#define VGA_KVM_STATE				\
-    unsigned long map_addr;			\
-    unsigned long map_end;
-#else
-#define VGA_KVM_STATE
-#endif
-
 #define VGA_STATE_COMMON                                                \
     uint8_t *vram_ptr;                                                  \
     unsigned long vram_offset;                                          \
@@ -154,18 +146,17 @@
     /* tell for each page if it has been updated since the last time */ \
     uint32_t last_palette[256];                                         \
     uint32_t last_ch_attr[CH_ATTR_SIZE]; /* XXX: make it dynamic */	\
-    VGA_KVM_STATE
+    unsigned long map_addr;						\
+    unsigned long map_end;
 
 
 typedef struct VGAState {
     VGA_STATE_COMMON
 
-#ifdef USE_KVM
     int32_t  aliases_enabled;
     int32_t  pad1;
     uint32_t aliased_bank_base[2];
     uint32_t aliased_bank_limit[2];
-#endif
 
 
 } VGAState;
@@ -200,11 +191,9 @@ void vga_draw_cursor_line_32(uint8_t *d1, const uint8_t *src1,
                              unsigned int color0, unsigned int color1,
                              unsigned int color_xor);
 
-#ifdef USE_KVM
 /* let kvm create vga memory */
 void *set_vram_mapping(unsigned long begin, unsigned long end);
 int unset_vram_mapping(unsigned long begin, unsigned long end);
-#endif
 
 void *vga_update_vram(VGAState *s, void *vga_ram_base, int vga_ram_size);
 extern const uint8_t sr_mask[8];
diff --git a/qemu/hw/vmport.c b/qemu/hw/vmport.c
index c225308..c09227d 100644
--- a/qemu/hw/vmport.c
+++ b/qemu/hw/vmport.c
@@ -26,7 +26,6 @@
 #include "isa.h"
 #include "pc.h"
 #include "sysemu.h"
-#include "libkvm.h"
 #include "qemu-kvm.h"
 
 #define VMPORT_CMD_GETVERSION 0x0a
@@ -60,16 +59,8 @@ static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
     uint32_t eax;
     uint32_t ret;
 
-#ifdef USE_KVM
-    struct kvm_regs regs;
-    extern kvm_context_t kvm_context;
-    if (kvm_allowed) {
-        kvm_get_regs(kvm_context, s->env->cpu_index, &regs);
-        s->env->regs[R_EAX] = regs.rax; s->env->regs[R_EBX] = regs.rbx;
-        s->env->regs[R_ECX] = regs.rcx; s->env->regs[R_EDX] = regs.rdx;
-        s->env->regs[R_ESI] = regs.rsi; s->env->regs[R_EDI] = regs.rdi;
-    }
-#endif
+    if (kvm_enabled())
+	kvm_save_registers(s->env);
 
     eax = s->env->regs[R_EAX];
     if (eax != VMPORT_MAGIC)
@@ -86,14 +77,8 @@ static uint32_t vmport_ioport_read(void *opaque, uint32_t addr)
 
     ret = s->func[command](s->opaque[command], addr);
 
-#ifdef USE_KVM
-    if (kvm_allowed) {
-        regs.rax = s->env->regs[R_EAX]; regs.rbx = s->env->regs[R_EBX];
-        regs.rcx = s->env->regs[R_ECX]; regs.rdx = s->env->regs[R_EDX];
-        regs.rsi = s->env->regs[R_ESI]; regs.rdi = s->env->regs[R_EDI];
-        kvm_set_regs(kvm_context, s->env->cpu_index, &regs);
-    }
-#endif
+    if (kvm_enabled())
+	kvm_load_registers(s->env);
 
     return ret;
 }
diff --git a/qemu/migration.c b/qemu/migration.c
index df0acf9..23cff1e 100644
--- a/qemu/migration.c
+++ b/qemu/migration.c
@@ -29,9 +29,7 @@
 #include "qemu-timer.h"
 #include "migration.h"
 #include "qemu_socket.h"
-#ifdef USE_KVM
 #include "qemu-kvm.h"
-#endif
 
 #include <sys/wait.h>
 
@@ -185,10 +183,10 @@ static void migrate_finish(MigrationState *s)
             qemu_aio_flush();
         } while (qemu_bh_poll());
         bdrv_flush_all();
-#ifdef USE_KVM
-	if (kvm_allowed && !*s->has_error && kvm_update_dirty_pages_log())
+
+	if (kvm_enabled() && !*s->has_error && kvm_update_dirty_pages_log())
 	    *s->has_error = MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED;
-#endif
+
         qemu_put_be32(f, 1);
         ret = qemu_live_savevm_state(f);
 #ifdef MIGRATION_VERIFY
@@ -260,10 +258,8 @@ static int migrate_check_convergence(MigrationState *s)
     }
 
     for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
             continue;
-#endif
 	if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
 	    dirty_count++;
     }
@@ -319,10 +315,8 @@ static void migrate_write(void *opaque)
     if (migrate_write_buffer(s))
 	return;
 
-#ifdef USE_KVM
-    if (kvm_allowed && !*s->has_error && kvm_update_dirty_pages_log())
+    if (kvm_enabled() && !*s->has_error && kvm_update_dirty_pages_log())
         *s->has_error = MIG_STAT_KVM_UPDATE_DIRTY_PAGES_LOG_FAILED;
-#endif
 
     if (migrate_check_convergence(s) || *s->has_error) {
 	qemu_del_timer(s->timer);
@@ -333,10 +327,8 @@ static void migrate_write(void *opaque)
     }	
 
     while (s->addr < phys_ram_size) {
-#ifdef USE_KVM
-        if (kvm_allowed && (s->addr>=0xa0000) && (s->addr<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (s->addr>=0xa0000) && (s->addr<0xc0000)) /* do not access video-addresses */
             s->addr = 0xc0000;
-#endif
 
 	if (cpu_physical_memory_get_dirty(s->addr, MIGRATION_DIRTY_FLAG)) {
             migrate_prepare_page(s);
@@ -404,11 +396,10 @@ static int start_migration(MigrationState *s)
     target_phys_addr_t addr;
     int r;
     unsigned char running = vm_running?2:1; /* 1 + vm_running */
-
-#ifdef USE_KVM
     int n = 0;
     unsigned char *phys_ram_page_exist_bitmap = NULL;
-    if (kvm_allowed) {
+
+    if (kvm_enabled()) {
         n = BITMAP_SIZE(phys_ram_size);
         phys_ram_page_exist_bitmap = qemu_malloc(n);
         if (!phys_ram_page_exist_bitmap) {
@@ -422,7 +413,6 @@ static int start_migration(MigrationState *s)
             goto out;
         }
     }
-#endif
 	
     r = MIG_STAT_WRITE_FAILED;
     if (write_whole_buffer(s->fd, &running, sizeof(running))) {
@@ -434,8 +424,7 @@ static int start_migration(MigrationState *s)
         goto out;
     }
 
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
         value = cpu_to_be32(n);
         if (write_whole_buffer(s->fd, &value, sizeof(value))) {
             perror("phys_ram_size_bitmap size write failed");
@@ -446,18 +435,16 @@ static int start_migration(MigrationState *s)
             goto out;
 	}
     }
-#endif
+
     fcntl(s->fd, F_SETFL, O_NONBLOCK);
 
     for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && !bit_is_set(addr>>TARGET_PAGE_BITS, phys_ram_page_exist_bitmap)) {
+        if (kvm_enabled() && !bit_is_set(addr>>TARGET_PAGE_BITS, phys_ram_page_exist_bitmap)) {
             cpu_physical_memory_reset_dirty(addr, 
                                             addr + TARGET_PAGE_SIZE, 
                                             MIGRATION_DIRTY_FLAG);
             continue;
         }
-#endif
 	if (!cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG))
 	    cpu_physical_memory_set_dirty(addr);
     }
@@ -482,10 +469,8 @@ static int start_migration(MigrationState *s)
     r = 0;
 
  out:
-#ifdef USE_KVM
-    if (phys_ram_page_exist_bitmap)
+    if (kvm_enabled() && phys_ram_page_exist_bitmap)
         qemu_free(phys_ram_page_exist_bitmap);
-#endif
     return r;
 }
 
@@ -825,8 +810,7 @@ static int migrate_incoming_fd(int fd)
 	return MIG_STAT_DST_MEM_SIZE_MISMATCH;
     }
 
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
         int n, m;
         unsigned char *phys_ram_page_exist_bitmap = NULL;
 
@@ -848,7 +832,6 @@ static int migrate_incoming_fd(int fd)
 
         qemu_free(phys_ram_page_exist_bitmap);
     }
-#endif
 
     do {
 	addr = qemu_get_be32(f);
@@ -1118,10 +1101,8 @@ static int save_verify_memory(QEMUFile *f, void *opaque)
     unsigned int sum;
 
     for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
             continue;
-#endif
         sum = calc_page_checksum(addr);
         qemu_put_be32(f, addr);
         qemu_put_be32(f, sum);
@@ -1136,10 +1117,8 @@ static int load_verify_memory(QEMUFile *f, void *opaque, int version_id)
     int num_errors = 0;
     
     for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
             continue;
-#endif
         sum = calc_page_checksum(addr);
         raddr = qemu_get_be32(f);
         rsum  = qemu_get_be32(f);
diff --git a/qemu/monitor.c b/qemu/monitor.c
index e03c473..e8022c8 100644
--- a/qemu/monitor.c
+++ b/qemu/monitor.c
@@ -286,9 +286,8 @@ static CPUState *mon_get_cpu(void)
         mon_set_cpu(0);
     }
 
-#ifdef USE_KVM
-    kvm_save_registers(mon_cpu);
-#endif
+    if (kvm_enabled())
+	kvm_save_registers(mon_cpu);
 
     return mon_cpu;
 }
diff --git a/qemu/osdep.c b/qemu/osdep.c
index 6131438..0ec6446 100644
--- a/qemu/osdep.c
+++ b/qemu/osdep.c
@@ -113,7 +113,7 @@ static void *kqemu_vmalloc(size_t size)
             int64_t free_space;
             int ram_mb;
 
-            extern int ram_size;
+            extern int64_t ram_size;
             free_space = (int64_t)stfs.f_bavail * stfs.f_bsize;
             if ((ram_size + 8192 * 1024) >= free_space) {
                 ram_mb = (ram_size / (1024 * 1024));
diff --git a/qemu/qemu-kvm-ia64.c b/qemu/qemu-kvm-ia64.c
index d3d6ac7..a17e032 100644
--- a/qemu/qemu-kvm-ia64.c
+++ b/qemu/qemu-kvm-ia64.c
@@ -2,11 +2,6 @@
 #include "config.h"
 #include "config-host.h"
 
-extern int kvm_allowed;
-extern int kvm_irqchip;
-
-#ifdef USE_KVM
-
 #include <string.h>
 
 #include "hw/hw.h"
@@ -68,4 +63,3 @@ int kvm_arch_try_push_interrupts(void *opaque)
 void kvm_arch_update_regs_for_sipi(CPUState *env)
 {
 }
-#endif
diff --git a/qemu/qemu-kvm-powerpc.c b/qemu/qemu-kvm-powerpc.c
index 92aeada..60b0d87 100644
--- a/qemu/qemu-kvm-powerpc.c
+++ b/qemu/qemu-kvm-powerpc.c
@@ -2,11 +2,6 @@
 #include "config.h"
 #include "config-host.h"
 
-extern int kvm_allowed;
-extern int kvm_irqchip;
-
-#ifdef USE_KVM
-
 #include <string.h>
 #include "hw/hw.h"
 #include "sysemu.h"
@@ -194,5 +189,3 @@ int handle_powerpc_dcr_write(int vcpu, uint32_t dcrn, uint32_t data)
     ppc_dcr_write(env->dcr_env, dcrn, data);
     return 0; /* XXX ignore failed DCR ops */
 }
-
-#endif
diff --git a/qemu/qemu-kvm-x86.c b/qemu/qemu-kvm-x86.c
index d1838f1..1880290 100644
--- a/qemu/qemu-kvm-x86.c
+++ b/qemu/qemu-kvm-x86.c
@@ -2,11 +2,6 @@
 #include "config.h"
 #include "config-host.h"
 
-extern int kvm_allowed;
-extern int kvm_irqchip;
-
-#ifdef USE_KVM
-
 #include <string.h>
 #include "hw/hw.h"
 
@@ -631,5 +626,3 @@ int handle_tpr_access(void *opaque, int vcpu,
     kvm_tpr_access_report(cpu_single_env, rip, is_write);
     return 0;
 }
-
-#endif
diff --git a/qemu/qemu-kvm.c b/qemu/qemu-kvm.c
index ec05027..3f12eda 100644
--- a/qemu/qemu-kvm.c
+++ b/qemu/qemu-kvm.c
@@ -2,17 +2,9 @@
 #include "config.h"
 #include "config-host.h"
 
-#ifdef USE_KVM
- #define KVM_ALLOWED_DEFAULT 1
-#else
- #define KVM_ALLOWED_DEFAULT 0
-#endif
-
-int kvm_allowed = KVM_ALLOWED_DEFAULT;
+int kvm_allowed = 1;
 int kvm_irqchip = 1;
 
-#ifdef USE_KVM
-
 #include <string.h>
 #include "hw/hw.h"
 #include "sysemu.h"
@@ -112,13 +104,13 @@ static int pre_kvm_run(void *opaque, int vcpu)
 
 void kvm_load_registers(CPUState *env)
 {
-    if (kvm_allowed)
+    if (kvm_enabled())
 	kvm_arch_load_regs(env);
 }
 
 void kvm_save_registers(CPUState *env)
 {
-    if (kvm_allowed)
+    if (kvm_enabled())
 	kvm_arch_save_regs(env);
 }
 
@@ -647,7 +639,7 @@ int kvm_physical_memory_set_dirty_tracking(int enable)
 {
     int r = 0;
 
-    if (!kvm_allowed)
+    if (!kvm_enabled())
         return 0;
 
     if (enable) {
@@ -767,4 +759,19 @@ void qemu_kvm_aio_wait_end(void)
 {
 }
 
-#endif
+int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf)
+{
+    return kvm_get_dirty_pages(kvm_context, phys_addr, buf);
+}
+
+void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
+			      unsigned long size, int log, int writable)
+{
+    return kvm_create_phys_mem(kvm_context, start_addr, size, log, writable);
+}
+
+void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
+			      unsigned long size)
+{
+    kvm_destroy_phys_mem(kvm_context, start_addr, size);
+}
diff --git a/qemu/qemu-kvm.h b/qemu/qemu-kvm.h
index 9b96951..a48efa3 100644
--- a/qemu/qemu-kvm.h
+++ b/qemu/qemu-kvm.h
@@ -2,7 +2,6 @@
 #define QEMU_KVM_H
 
 #include "cpu.h"
-#include "libkvm.h"
 
 int kvm_main_loop(void);
 int kvm_qemu_init(void);
@@ -16,6 +15,7 @@ int kvm_update_debugger(CPUState *env);
 int kvm_qemu_init_env(CPUState *env);
 int kvm_qemu_check_extension(int ext);
 void kvm_apic_init(CPUState *env);
+int kvm_set_irq(int irq, int level);
 
 int kvm_physical_memory_set_dirty_tracking(int enable);
 int kvm_update_dirty_pages_log(void);
@@ -28,6 +28,12 @@ void kvm_update_interrupt_request(CPUState *env);
 void kvm_cpu_register_physical_memory(target_phys_addr_t start_addr,
                                       unsigned long size,
                                       unsigned long phys_offset);
+void *kvm_cpu_create_phys_mem(target_phys_addr_t start_addr,
+			      unsigned long size, int log, int writable);
+
+void kvm_cpu_destroy_phys_mem(target_phys_addr_t start_addr,
+			      unsigned long size);
+
 int kvm_arch_qemu_create_context(void);
 
 void kvm_arch_save_regs(CPUState *env);
@@ -46,14 +52,13 @@ void qemu_kvm_aio_wait_start(void);
 void qemu_kvm_aio_wait(void);
 void qemu_kvm_aio_wait_end(void);
 
-extern int kvm_allowed;
-extern int kvm_irqchip;
-
 void kvm_tpr_opt_setup(CPUState *env);
 void kvm_tpr_access_report(CPUState *env, uint64_t rip, int is_write);
 int handle_tpr_access(void *opaque, int vcpu,
 			     uint64_t rip, int is_write);
 
+int qemu_kvm_get_dirty_pages(unsigned long phys_addr, void *buf);
+
 #ifdef TARGET_PPC
 int handle_powerpc_dcr_read(int vcpu, uint32_t dcrn, uint32_t *data);
 int handle_powerpc_dcr_write(int vcpu,uint32_t dcrn, uint32_t data);
@@ -62,5 +67,17 @@ int handle_powerpc_dcr_write(int vcpu,uint32_t dcrn, uint32_t data);
 #define ALIGN(x, y)  (((x)+(y)-1) & ~((y)-1))
 #define BITMAP_SIZE(m) (ALIGN(((m)>>TARGET_PAGE_BITS), HOST_LONG_BITS) / 8)
 
+#ifdef USE_KVM
+#include "libkvm.h"
+
+extern int kvm_allowed;
+extern kvm_context_t kvm_context;
+
+#define kvm_enabled() (kvm_allowed)
+#define qemu_kvm_irqchip_in_kernel() kvm_irqchip_in_kernel(kvm_context)
+#else
+#define kvm_enabled() (0)
+#define qemu_kvm_irqchip_in_kernel() (0)
+#endif
 
 #endif
diff --git a/qemu/target-i386/cpu.h b/qemu/target-i386/cpu.h
index 7143ab3..600464c 100644
--- a/qemu/target-i386/cpu.h
+++ b/qemu/target-i386/cpu.h
@@ -555,10 +555,8 @@ typedef struct CPUX86State {
     target_ulong kernelgsbase;
 #endif
 
-#ifdef USE_KVM
     uint64_t tsc; /* time stamp counter */
     uint8_t ready_for_interrupt_injection;
-#endif
     uint64_t pat;
 
     /* exception/interrupt handling */
@@ -594,11 +592,9 @@ typedef struct CPUX86State {
     int last_io_time;
 #endif
 
-#ifdef USE_KVM
 #define BITS_PER_LONG (8 * sizeof (uint32_t))
 #define NR_IRQ_WORDS (256/ BITS_PER_LONG)
     uint32_t kvm_interrupt_bitmap[NR_IRQ_WORDS];
-#endif
 
     /* in order to simplify APIC support, we leave this pointer to the
        user */
diff --git a/qemu/target-i386/helper2.c b/qemu/target-i386/helper2.c
index ac663aa..3ada676 100644
--- a/qemu/target-i386/helper2.c
+++ b/qemu/target-i386/helper2.c
@@ -29,9 +29,7 @@
 #include "exec-all.h"
 #include "svm.h"
 
-#ifdef USE_KVM
-#include "../qemu-kvm.h"
-#endif
+#include "qemu-kvm.h"
 
 //#define DEBUG_MMU
 
diff --git a/qemu/target-ia64/cpu.h b/qemu/target-ia64/cpu.h
index 2d91cb9..97358ee 100644
--- a/qemu/target-ia64/cpu.h
+++ b/qemu/target-ia64/cpu.h
@@ -51,9 +51,7 @@ typedef struct CPUIA64State {
    int user_mode_only;
    uint32_t hflags;
 
-#ifdef USE_KVM
-      uint8_t ready_for_interrupt_injection;
-#endif
+    uint8_t ready_for_interrupt_injection;
 
 } CPUIA64State;
 
diff --git a/qemu/target-ia64/op_helper.c b/qemu/target-ia64/op_helper.c
index 5138af5..8660b17 100644
--- a/qemu/target-ia64/op_helper.c
+++ b/qemu/target-ia64/op_helper.c
@@ -24,7 +24,7 @@
 #include "cpu.h"
 #include "exec-all.h"
 
-extern int kvm_allowed;
+#include "qemu-kvm.h"
 
 CPUState *cpu_ia64_init(char *cpu_model){
     CPUState *env;
@@ -33,14 +33,10 @@ CPUState *cpu_ia64_init(char *cpu_model){
         return NULL;
     cpu_exec_init(env);
     cpu_reset(env);
-#ifdef USE_KVM
-    {
-        if (kvm_allowed) {
-            kvm_qemu_init_env(env);
-            env->ready_for_interrupt_injection = 1;
-        }
+    if (kvm_enabled()) {
+	kvm_qemu_init_env(env);
+	env->ready_for_interrupt_injection = 1;
     }
-#endif
     return env;
 }
 
@@ -74,12 +70,10 @@ void switch_mode(CPUState *env, int mode)
 /* Handle a CPU exception.  */
 void do_interrupt(CPUIA64State *env)
 {
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
 	printf("%s: unexpect\n", __FUNCTION__);
 	exit(-1);
     }
-#endif
 }
 
 
diff --git a/qemu/target-ppc/cpu.h b/qemu/target-ppc/cpu.h
index 020f6de..53df331 100644
--- a/qemu/target-ppc/cpu.h
+++ b/qemu/target-ppc/cpu.h
@@ -574,9 +574,7 @@ struct CPUPPCState {
     /* temporary general purpose registers */
     ppc_gpr_t tgpr[4]; /* Used to speed-up TLB assist handlers */
 
-#ifdef USE_KVM
     uint8_t ready_for_interrupt_injection;
-#endif
 
     /* Floating point execution context */
     /* temporary float registers */
diff --git a/qemu/vl.c b/qemu/vl.c
index 4b44312..5f7f8d1 100644
--- a/qemu/vl.c
+++ b/qemu/vl.c
@@ -133,9 +133,7 @@ int inet_aton(const char *cp, struct in_addr *ia);
 
 #include "exec-all.h"
 
-#if USE_KVM
 #include "qemu-kvm.h"
-#endif
 
 #define DEFAULT_NETWORK_SCRIPT "/etc/qemu-ifup"
 #define DEFAULT_NETWORK_DOWN_SCRIPT "/etc/qemu-ifdown"
@@ -265,15 +263,13 @@ extern char *logfilename;
 
 void decorate_application_name(char *appname, int max_len)
 {
-#if USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
     {
         int remain = max_len - strlen(appname) - 1;
 
         if (remain > 0)
             strncat(appname, "/KVM", remain);
     }
-#endif
 }
 
 /***********************************************************/
@@ -6548,10 +6544,8 @@ void cpu_save(QEMUFile *f, void *opaque)
     uint32_t hflags;
     int i;
 
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
         kvm_save_registers(env);
-#endif
 
     for(i = 0; i < CPU_NB_REGS; i++)
         qemu_put_betls(f, &env->regs[i]);
@@ -6638,15 +6632,12 @@ void cpu_save(QEMUFile *f, void *opaque)
 #endif
     qemu_put_be32s(f, &env->smbase);
 
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
         for (i = 0; i < NR_IRQ_WORDS ; i++) {
             qemu_put_be32s(f, &env->kvm_interrupt_bitmap[i]);
         }
         qemu_put_be64s(f, &env->tsc);
     }
-#endif
-
 }
 
 #ifdef USE_X86LDOUBLE
@@ -6789,8 +6780,7 @@ int cpu_load(QEMUFile *f, void *opaque, int version_id)
     /* XXX: compute hflags from scratch, except for CPL and IIF */
     env->hflags = hflags;
     tlb_flush(env, 1);
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
         /* when in-kernel irqchip is used, HF_HALTED_MASK causes deadlock
            because no userspace IRQs will ever clear this flag */
         env->hflags &= ~HF_HALTED_MASK;
@@ -6800,7 +6790,6 @@ int cpu_load(QEMUFile *f, void *opaque, int version_id)
         qemu_get_be64s(f, &env->tsc);
         kvm_load_registers(env);
     }
-#endif
     return 0;
 }
 
@@ -7137,10 +7126,8 @@ static int ram_load_v1(QEMUFile *f, void *opaque)
     if (qemu_get_be32(f) != phys_ram_size)
         return -EINVAL;
     for(i = 0; i < phys_ram_size; i+= TARGET_PAGE_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
             continue;
-#endif
         ret = ram_get_page(f, phys_ram_base + i, TARGET_PAGE_SIZE);
         if (ret)
             return ret;
@@ -7275,10 +7262,8 @@ static void ram_save_live(QEMUFile *f, void *opaque)
     target_ulong addr;
 
     for (addr = 0; addr < phys_ram_size; addr += TARGET_PAGE_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (addr>=0xa0000) && (addr<0xc0000)) /* do not access video-addresses */
             continue;
-#endif
 	if (cpu_physical_memory_get_dirty(addr, MIGRATION_DIRTY_FLAG)) {
 	    qemu_put_be32(f, addr);
 	    qemu_put_buffer(f, phys_ram_base + addr, TARGET_PAGE_SIZE);
@@ -7297,10 +7282,8 @@ static void ram_save_static(QEMUFile *f, void *opaque)
     if (ram_compress_open(s, f) < 0)
         return;
     for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
             continue;
-#endif
 #if 0
         if (tight_savevm_enabled) {
             int64_t sector_num;
@@ -7372,10 +7355,8 @@ static int ram_load_static(QEMUFile *f, void *opaque)
     if (ram_decompress_open(s, f) < 0)
         return -EINVAL;
     for(i = 0; i < phys_ram_size; i+= BDRV_HASH_BLOCK_SIZE) {
-#ifdef USE_KVM
-        if (kvm_allowed && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
+        if (kvm_enabled() && (i>=0xa0000) && (i<0xc0000)) /* do not access video-addresses */
             continue;
-#endif
         if (ram_decompress_buf(s, buf, 1) < 0) {
             fprintf(stderr, "Error while reading ram block header\n");
             goto error;
@@ -7871,13 +7852,12 @@ static int main_loop(void)
     CPUState *env;
 
 
-#ifdef USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
 	kvm_main_loop();
 	cpu_disable_ticks();
 	return 0;
     }
-#endif
+
     cur_cpu = first_cpu;
     next_cpu = cur_cpu->next_cpu ?: first_cpu;
     for(;;) {
@@ -7919,10 +7899,8 @@ static int main_loop(void)
             if (reset_requested) {
                 reset_requested = 0;
                 qemu_system_reset();
-#ifdef USE_KVM
-		if (kvm_allowed)
+		if (kvm_enabled())
 			kvm_load_registers(env);
-#endif
                 ret = EXCP_INTERRUPT;
             }
             if (powerdown_requested) {
@@ -9118,9 +9096,11 @@ int main(int argc, char **argv)
 	    case QEMU_OPTION_no_kvm:
 		kvm_allowed = 0;
 		break;
-	    case QEMU_OPTION_no_kvm_irqchip:
+	    case QEMU_OPTION_no_kvm_irqchip: {
+		extern int kvm_irqchip;
 		kvm_irqchip = 0;
 		break;
+	    }
 #endif
             case QEMU_OPTION_usb:
                 usb_enabled = 1;
@@ -9301,8 +9281,9 @@ int main(int argc, char **argv)
 #endif
 
 #if USE_KVM
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
 	if (kvm_qemu_init() < 0) {
+	    extern int kvm_allowed;
 	    fprintf(stderr, "Could not initialize KVM, will disable KVM support\n");
 	    kvm_allowed = 0;
 	}
@@ -9403,14 +9384,13 @@ int main(int argc, char **argv)
     /* init the memory */
     phys_ram_size = ram_size + vga_ram_size + MAX_BIOS_SIZE;
 
-#if USE_KVM
     /* Initialize kvm */
 #if defined(TARGET_I386) || defined(TARGET_X86_64)
 #define KVM_EXTRA_PAGES 3
 #else
 #define KVM_EXTRA_PAGES 0
 #endif
-    if (kvm_allowed) {
+    if (kvm_enabled()) {
 	    phys_ram_size += KVM_EXTRA_PAGES * TARGET_PAGE_SIZE;
 	    if (kvm_qemu_create_context() < 0) {
 		    fprintf(stderr, "Could not create KVM context\n");
@@ -9437,13 +9417,6 @@ int main(int argc, char **argv)
 		    exit(1);
 	    }
     }
-#else
-    phys_ram_base = qemu_vmalloc(phys_ram_size);
-    if (!phys_ram_base) {
-        fprintf(stderr, "Could not allocate physical memory\n");
-        exit(1);
-    }
-#endif
 
     bdrv_init();
 
@@ -9585,10 +9558,8 @@ int main(int argc, char **argv)
         qemu_mod_timer(display_state.gui_timer, qemu_get_clock(rt_clock));
     }
 
-#ifdef USE_KVM
-    if (kvm_allowed)
+    if (kvm_enabled())
 	kvm_init_ap();
-#endif
 
 #ifdef CONFIG_GDBSTUB
     if (use_gdbstub) {

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/

^ permalink raw reply related	[flat|nested] 7+ messages in thread

end of thread, other threads:[~2008-01-30 14:42 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2008-01-29 22:46 [PATCH] Clean up KVM/QEMU interaction Anthony Liguori
     [not found] ` <1201646815-5071-1-git-send-email-aliguori-r/Jw6+rmf7HQT0dZR+AlfA@public.gmane.org>
2008-01-29 23:19   ` [kvm-ppc-devel] " Hollis Blanchard
2008-01-30  2:36   ` Zhang, Xiantao
2008-01-30  5:38   ` Jerone Young
2008-01-30  9:47   ` Avi Kivity
     [not found]     ` <47A047BC.10101-atKUWr5tajBWk0Htik3J/w@public.gmane.org>
2008-01-30 10:16       ` Avi Kivity
2008-01-30 14:42       ` Anthony Liguori

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox