[5627] Add KVM support to QEMU
Revision: 5627
http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5627 Author: aliguori Date: 2008-11-05 16:04:33 +0000 (Wed, 05 Nov 2008)
Log Message:
-----------
Add KVM support to QEMU
This patch adds very basic KVM support. KVM is a kernel module for Linux that allows userspace programs to make use of hardware virtualization support. It current supports x86 hardware virtualization using Intel VT-x or AMD-V. It also supports IA64 VT-i, PPC 440, and S390.
This patch only implements the bare minimum support to get a guest booting. It has very little impact the rest of QEMU and attempts to integrate nicely with the rest of QEMU.
Even though this implementation is basic, it is significantly faster than TCG. Booting and shutting down a Linux guest:
w/TCG: 1:32.36 elapsed 84% CPU
w/KVM: 0:31.14 elapsed 59% CPU
Right now, KVM is disabled by default and must be explicitly enabled with
-enable-kvm. We can enable it by default later when we have had better
testing.
Modified Paths:
--------------
trunk/Makefile.target
trunk/configure
trunk/cpu-defs.h
trunk/cpu-exec.c
trunk/exec.c
trunk/hw/acpi.c
trunk/monitor.c
trunk/target-i386/cpu.h
trunk/target-i386/helper.c
trunk/vl.cModified: trunk/Makefile.target
===================================================================
Makefile.target +6 -0
@@ -183,6 +183,9 @@ endif endif +kvm.o: CFLAGS+=$(KVM_CFLAGS) +kvm-all.o: CFLAGS+=$(KVM_CFLAGS) + all: $(PROGS) #########################################################
@@ -581,6 +584,9 @@ OBJS=vl.o osdep.o monitor.o pci.o loader.o isa_mmio.o machine.o net-checksum.o OBJS+=fw_cfg.o aio.o buffered_file.o migration.o migration-tcp.o qemu-char.o OBJS+=net.o +ifdef CONFIG_KVM +OBJS+=kvm.o kvm-all.o +endif ifdef CONFIG_WIN32 OBJS+=block-raw-win32.o else
Modified: trunk/configure
===================================================================
configure +48 -0
96 lines not on this page · raw message
Modified: trunk/cpu-defs.h
===================================================================
cpu-defs.h +7 -1
@@ -142,6 +142,9 @@ } icount_decr_u16;#endif +struct kvm_run ; +struct KVMState ; +#define CPU_TEMP_BUF_NLONGS 128#define CPU_COMMON \ struct TranslationBlock *current_tb;/* currently executing TB */ \
@@ -199,6 +202,9 @@/* user data */ \void *opaque; \ \ -const char *cpu_model_str; +const char *cpu_model_str; \ +struct KVMState *kvm_state; \ +struct kvm_run *kvm_run; \ +int kvm_fd;#endif
Modified: trunk/cpu-exec.c
===================================================================
cpu-exec.c +14 -0
@@ -22,6 +22,7 @@#include "exec.h" #include "disas.h" #include "tcg.h" +#include "kvm.h" #if !defined(CONFIG_SOFTMMU )#undef EAX
@@ -371,6 +372,19 @@ }#endif +if (kvm_enabled ()) { +int ret; + ret =kvm_cpu_exec (env); +if ((env->interrupt_request &CPU_INTERRUPT_EXIT )) { + env->interrupt_request &= ~CPU_INTERRUPT_EXIT ; + env->exception_index =EXCP_INTERRUPT ; +cpu_loop_exit (); + }else if (env->halted) { +cpu_loop_exit (); + }else +longjmp (env->jmp_env,1 ); + } + next_tb =0 ;/* force lookup of first TB */ for (;;) { interrupt_request = env->interrupt_request;
Modified: trunk/exec.c
===================================================================
exec.c +4 -0
@@ -39,6 +39,7 @@#include "tcg.h" #include "hw/hw.h" #include "osdep.h" +#include "kvm.h" #if defined(CONFIG_USER_ONLY )#include <qemu.h> #endif
@@ -2212,6 +2213,9 @@ kqemu_set_phys_mem(start_addr, size, phys_offset); }#endif +if (kvm_enabled ()) +kvm_set_phys_mem (start_addr, size, phys_offset); + size = (size +TARGET_PAGE_SIZE -1 ) &TARGET_PAGE_MASK ; end_addr = start_addr + (target_phys_addr_t )size;for (addr = start_addr; addr != end_addr; addr +=TARGET_PAGE_SIZE ) {
Modified: trunk/hw/acpi.c
===================================================================
hw/acpi.c +7 -0
@@ -23,6 +23,7 @@#include "sysemu.h" #include "i2c.h" #include "smbus.h" +#include "kvm.h" //#define DEBUG
@@ -501,6 +502,12 @@register_ioport_write (ACPI_DBG_IO_ADDR ,4 ,4 , acpi_dbg_writel, s); +if (kvm_enabled ()) { +/* Mark SMM as already inited to prevent SMM from running. KVM does not +* support SMM mode. */ + pci_conf[0x5B ] =0x02 ; + } +/* XXX: which specification is used ? The i82731AB has different mappings */ pci_conf[0x5f ] = (parallel_hds[0 ] !=NULL ?0x80 :0 ) |0x10 ;
Modified: trunk/monitor.c
===================================================================
monitor.c +16 -0
@@ -37,6 +37,7 @@#include <dirent.h> #include "qemu-timer.h" #include "migration.h" +#include "kvm.h" //#define DEBUG //#define DEBUG_COMPLETION
@@ -1263,6 +1264,19 @@#endif } +static void do_info_kvm (void ) +{ +#ifdef CONFIG_KVM +term_printf ("kvm support: " ); +if (kvm_enabled ()) +term_printf ("enabled\n" ); +else +term_printf ("disabled\n" ); +#else +term_printf ("kvm support: not compiled\n" ); +#endif +} +#ifdef CONFIG_PROFILER int64_t kqemu_time;
@@ -1497,6 +1511,8 @@ "", "show dynamic compiler info", }, {"kqemu" ,"" , do_info_kqemu, "", "show kqemu information", }, + { "kvm", "", do_info_kvm, + "", "show kvm information", }, { "usb", "", usb_info, "", "show guest USB devices", }, { "usbhost", "", usb_host_info,
Modified: trunk/target-i386/cpu.h
===================================================================
target-i386/cpu.h +6 -0
@@ -587,6 +587,8 @@target_ulong kernelgsbase;#endif +uint64_t tsc; +uint64_t pat;/* exception/interrupt handling */
@@ -617,6 +619,10 @@int kqemu_enabled;int last_io_time;#endif + +/* For KVM */ +uint64_t interrupt_bitmap[256 /64 ]; +/* in order to simplify APIC support, we leave this pointer to the user */ struct APICState *apic_state;
Modified: trunk/target-i386/helper.c
===================================================================
target-i386/helper.c +73 -0
109 lines not on this page · raw message
Modified: trunk/vl.c
===================================================================
vl.c +34 -0
@@ -39,6 +39,7 @@#include "block.h" #include "audio/audio.h" #include "migration.h" +#include "kvm.h" #include <unistd.h> #include <fcntl.h>
@@ -4782,6 +4783,9 @@"-kernel-kqemu enable KQEMU full virtualization (default is user mode only)\n" "-no-kqemu disable KQEMU kernel module usage\n"#endif +#ifdef CONFIG_KVM + "-enable-kvm enable KVM full virtualization support\n" +#endif #ifdef TARGET_I386 "-no-acpi disable ACPI\n"#endif
@@ -4887,6 +4891,7 @@ QEMU_OPTION_pidfile, QEMU_OPTION_no_kqemu, QEMU_OPTION_kernel_kqemu, + QEMU_OPTION_enable_kvm, QEMU_OPTION_win2k_hack, QEMU_OPTION_usb, QEMU_OPTION_usbdevice,
@@ -4973,6 +4978,9 @@ { "no-kqemu", 0, QEMU_OPTION_no_kqemu }, { "kernel-kqemu", 0, QEMU_OPTION_kernel_kqemu }, #endif +#ifdef CONFIG_KVM + { "enable-kvm", 0, QEMU_OPTION_enable_kvm }, +#endif #if defined(TARGET_PPC) || defined(TARGET_SPARC) { "g", 1, QEMU_OPTION_g }, #endif
@@ -5794,6 +5802,14 @@ kqemu_allowed =2 ;break ;#endif +#ifdef CONFIG_KVM +case QEMU_OPTION_enable_kvm: + kvm_allowed =1 ; +#ifdef USE_KQEMU + kqemu_allowed =0 ; +#endif +break ; +#endif case QEMU_OPTION_usb: usb_enabled =1 ;break ;
@@ -5928,6 +5944,14 @@ } } +#if defined(CONFIG_KVM ) && defined(USE_KQEMU ) +if (kvm_allowed && kqemu_allowed) { +fprintf (stderr, +"You can not enable both KVM and kqemu at the same time\n" ); +exit (1 ); + } +#endif + machine->max_cpus = machine->max_cpus ?:1 ;/* Default to UP */ if (smp_cpus > machine->max_cpus) { fprintf(stderr, "Number of SMP cpus requested (%d), exceeds max cpus "
@@ -6229,6 +6253,16 @@ } } +if (kvm_enabled ()) { +int ret; + + ret =kvm_init (smp_cpus); +if (ret <0 ) { +fprintf (stderr,"failed to initialize KVM\n" ); +exit (1 ); + } + } + machine->init (ram_size, vga_ram_size, boot_devices, ds, kernel_filename, kernel_cmdline, initrd_filename, cpu_model);
2008/11/5 Anthony Liguori <[email protected]>:
Quoted 49 lines
Revision: 5627
http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5627 Author: aliguori Date: 2008-11-05 16:04:33 +0000 (Wed, 05 Nov 2008)Log Message:
-----------
Add KVM support to QEMUThis patch adds very basic KVM support. KVM is a kernel module for Linux that allows userspace programs to make use of hardware virtualization support. It current supports x86 hardware virtualization using Intel VT-x or AMD-V. It also supports IA64 VT-i, PPC 440, and S390.
This patch only implements the bare minimum support to get a guest booting. It has very little impact the rest of QEMU and attempts to integrate nicely with the rest of QEMU.
Even though this implementation is basic, it is significantly faster than TCG. Booting and shutting down a Linux guest:
w/TCG: 1:32.36 elapsed 84% CPU
w/KVM: 0:31.14 elapsed 59% CPU
Right now, KVM is disabled by default and must be explicitly enabled with
-enable-kvm. We can enable it by default later when we have had better
testing.Signed-off-by Anthony Liguori <[email protected]>Modified Paths:
--------------
trunk/Makefile.target
trunk/configure
trunk/cpu-defs.h
trunk/cpu-exec.c
trunk/exec.c
trunk/hw/acpi.c
trunk/monitor.c
trunk/target-i386/cpu.h
trunk/target-i386/helper.c
trunk/vl.cModified: trunk/Makefile.target
===================================================================
Makefile.target 2008-11-05 16:04:33 UTC (rev 5627) line 187
Quoted 5 lines
Makefile.target 2008-11-05 16:04:33 UTC (rev 5627) renamed from Makefile.target 2008-11-05 15:34:06 UTC (rev 5626) +2
-0@@ -183,6 +183,9 @@ endif endif +kvm.o: CFLAGS+=$(KVM_CFLAGS) +kvm-all.o: CFLAGS+=$(KVM_CFLAGS)
The source seems to be missing, probably forgotten?
Cheers
Quoted 5 lines · andrzej zaborowski
2008/11/5 Anthony Liguori <[email protected]>:
Quoted 1 line
The source seems to be missing, probably forgotten?
Yeah, sorry about that. I just fixed it.
Regards,
Anthony Liguori
Quoted 4 lines
Cheers
Quoted 9 lines · Anthony Liguori
Revision: 5627
http://svn.sv.gnu.org/viewvc/?view=rev&root=qemu&revision=5627 Author: aliguori Date: 2008-11-05 16:04:33 +0000 (Wed, 05 Nov 2008)Log Message:
-----------
Add KVM support to QEMU
...
Quoted 49 lines
Modified:
trunk/target-i386/helper.c
===================================================================target-i386/helper.c +27
-0@@ -29,6 +29,7 @@#include "exec-all.h" #include "svm.h" #include "qemu-common.h" +#include "kvm.h" //#define DEBUG_MMU @@ -115,6 +116,8 @@#ifdef USE_KQEMU kqemu_init (env);#endif +if (kvm_enabled ()) +kvm_init_vcpu (env);return env; }@@ -1288,6 +1291,40 @@ }#endif /* !CONFIG_USER_ONLY */ +#if defined(CONFIG_KVM ) +static void host_cpuid (uint32_t function,uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) +{ +uint32_t vec[4 ]; + +#ifdef __x86_64__ + asmvolatile ("cpuid" + :"=a" (vec[0 ]),"=b" (vec[1 ]), +"=c" (vec[2 ]),"=d" (vec[3 ]) + :"0" (function) :"cc" ); +#else + asmvolatile ("pusha \n\t" +"cpuid \n\t" +"mov %%eax, 0(%1) \n\t" +"mov %%ebx, 4(%1) \n\t" +"mov %%ecx, 8(%1) \n\t" +"mov %%edx, 12(%1) \n\t" +"popa" + : :"a" (function),"S" (vec) + :"memory" ,"cc" ); +#endif + +if (eax)
target-i386/helper.c line 1327
Quoted 10 lines
target-i386/helper.c +10
-0+ *eax = vec[0 ]; +if (ebx) + *ebx = vec[1 ]; +if (ecx) + *ecx = vec[2 ]; +if (edx) + *edx = vec[3]; +} +#endif +
...and if KVM is disabled, let's keep the compiler happy:
------------>
Fix compiler warning for KVM-disabled case.
--- target-i386/helper.c | 3 +++ 1 file changed, 3 insertions(+)
target-i386/helper.c +3 -0
@@ -1323,6 +1323,9 @@ static void host_cpuid(uint32_t functionif (edx) *edx = vec[3 ]; } +#else +static inline void host_cpuid (uint32_t function,uint32_t *eax,uint32_t *ebx, +uint32_t *ecx,uint32_t *edx) { }#endif void cpu_x86_cpuid(CPUX86State *env, uint32_t index,
configure line 968
Quoted 11 lines · Anthony Liguori
configure +11
-0+# kvm probe +if test "$kvm" = "yes" ; then + cat > $TMPC <<EOF +#include <linux/kvm.h> +#if !defined(KVM_API_VERSION) || \ + KVM_API_VERSION < 12 || \ + KVM_API_VERSION > 12 || \ + !defined(KVM_CAP_USER_MEMORY) || \ + !defined(KVM_CAP_SET_TSS_ADDR) +#error Invalid KVM version +#endif
The installed linux/kvm.h has no relatation at all to the currently
running kernel. Please ship a current max API version kvm.h with qemu
and always build against that one, and handle lower API versions at runtime. Please don't turn mainline qemu kvm support into the same bloody mess as the kvm userspace repository.
Quoted 12 lines · Christoph Hellwig
Quoted 10 lines · Anthony Liguori
configure +10
-0+# kvm probe +if test "$kvm" = "yes" ; then + cat > $TMPC <<EOF +#include <linux/kvm.h> +#if !defined(KVM_API_VERSION) || \ + KVM_API_VERSION < 12 || \ + KVM_API_VERSION > 12 || \ + !defined(KVM_CAP_USER_MEMORY) || \ + !defined(KVM_CAP_SET_TSS_ADDR) +#error Invalid KVM version
Quoted 9 lines
Quoted 2 lines
+#endif
The installed
linux/kvm.hhas no relatation at all to the currently
running kernel. Please ship a current max API versionkvm.hwith qemu
and always build against that one, and handle lower API versions at runtime. Please don't turn mainline qemu kvm support into the same bloody mess as the kvm userspace repository.
There are multiple places in QEMU that depend on kernel headers. I don't want to stick kvm headers in QEMU while depending on external headers for USB pass through. If someone wants to put together a patch to make QEMU no longer depend on external headers, I'd happily review/apply it. I expect it to be tested on various kernel versions though. Some less than wonderful kernel subsystems have not always been good about making interface changes backwards compatible. linux-aio is a good example of an interface that makes it exceedingly difficult to detect whether a new feature is supported on any given kernel version without doing a compile test.
Regards,
Anthony Liguori