From 8554f5755112209d248145c076d90395ca2a0762 Mon Sep 17 00:00:00 2001 From: Glauber Costa <glommer@redhat.com> Date: Sun, 31 May 2009 09:42:10 -0400 Subject: [PATCH 06/10] make sure kvm_vpu_init is the last thing called in cpu initialization KVM access some state that is only present late in cpu initialization. This happens in kvm_vcpu_init(). APIC is an example of that. So we have to make sure that kvm_vcpu_init is the last thing called in the initialization process. We also have to explicitly call qemu_kvm_load_apic(), since the first call will find the vcpu non initialized. Signed-off-by: Glauber Costa <glommer@redhat.com> RH-Upstream-status: upstream (qemu-kvm/master) Message-Id: <1243777330-8157-4-git-send-email-glommer@redhat.com> Signed-off-by: Eduardo Habkost <ehabkost@redhat.com> Acked-by: Gleb Natapov <gleb@redhat.com> Acked-by: Juan Quintela <quintela@redhat.com> Acked-by: Marcelo Tosatti <mtosatti@redhat.com> Bugzilla: 504237 --- qemu/hw/pc.c | 6 ++++++ qemu/qemu-kvm-x86.c | 2 ++ qemu/target-i386/helper.c | 2 -- 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/qemu/hw/pc.c b/qemu/hw/pc.c index 48476ff..08edb40 100644 --- a/qemu/hw/pc.c +++ b/qemu/hw/pc.c @@ -808,6 +808,12 @@ CPUState *pc_new_cpu(int cpu, const char *cpu_model, int pci_enabled) if (pci_enabled) { apic_init(env); } + + /* kvm needs this to run after the apic is initialized. Otherwise, + * it can access invalid state and crash. + */ + if (kvm_enabled()) + kvm_init_vcpu(env); return env; } diff --git a/qemu/qemu-kvm-x86.c b/qemu/qemu-kvm-x86.c index cbb90f2..9004b10 100644 --- a/qemu/qemu-kvm-x86.c +++ b/qemu/qemu-kvm-x86.c @@ -512,6 +512,8 @@ int kvm_arch_qemu_init_env(CPUState *cenv) CPUState copy; uint32_t i, limit; + qemu_kvm_load_lapic(cenv); + copy = *cenv; #ifdef KVM_CPUID_SIGNATURE diff --git a/qemu/target-i386/helper.c b/qemu/target-i386/helper.c index cda0390..9ac8b0e 100644 --- a/qemu/target-i386/helper.c +++ b/qemu/target-i386/helper.c @@ -1642,7 +1642,5 @@ CPUX86State *cpu_x86_init(const char *cpu_model) #ifdef USE_KQEMU kqemu_init(env); #endif - if (kvm_enabled()) - kvm_init_vcpu(env); return env; } -- 1.6.3.rc4.29.g8146