]> git.karo-electronics.de Git - mv-sheeva.git/blobdiff - arch/x86/kvm/x86.c
Merge branch 'x86-asm-for-linus' of git://git.kernel.org/pub/scm/linux/kernel/git...
[mv-sheeva.git] / arch / x86 / kvm / x86.c
index d39d6b25d3e5e6db3f7f5d38fc835e149d851dc6..25f19078b3210424b091fea80bd8c82b0a9ef254 100644 (file)
@@ -733,7 +733,7 @@ static u32 msrs_to_save[] = {
        HV_X64_MSR_GUEST_OS_ID, HV_X64_MSR_HYPERCALL,
        HV_X64_MSR_APIC_ASSIST_PAGE,
        MSR_IA32_SYSENTER_CS, MSR_IA32_SYSENTER_ESP, MSR_IA32_SYSENTER_EIP,
-       MSR_K6_STAR,
+       MSR_STAR,
 #ifdef CONFIG_X86_64
        MSR_CSTAR, MSR_KERNEL_GS_BASE, MSR_SYSCALL_MASK, MSR_LSTAR,
 #endif
@@ -744,6 +744,8 @@ static unsigned num_msrs_to_save;
 
 static u32 emulated_msrs[] = {
        MSR_IA32_MISC_ENABLE,
+       MSR_IA32_MCG_STATUS,
+       MSR_IA32_MCG_CTL,
 };
 
 static int set_efer(struct kvm_vcpu *vcpu, u64 efer)
@@ -1783,8 +1785,28 @@ out:
        return r;
 }
 
+static void wbinvd_ipi(void *garbage)
+{
+       wbinvd();
+}
+
+static bool need_emulate_wbinvd(struct kvm_vcpu *vcpu)
+{
+       return vcpu->kvm->arch.iommu_domain &&
+               !(vcpu->kvm->arch.iommu_flags & KVM_IOMMU_CACHE_COHERENCY);
+}
+
 void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
 {
+       /* Address WBINVD may be executed by guest */
+       if (need_emulate_wbinvd(vcpu)) {
+               if (kvm_x86_ops->has_wbinvd_exit())
+                       cpumask_set_cpu(cpu, vcpu->arch.wbinvd_dirty_mask);
+               else if (vcpu->cpu != -1 && vcpu->cpu != cpu)
+                       smp_call_function_single(vcpu->cpu,
+                                       wbinvd_ipi, NULL, 1);
+       }
+
        kvm_x86_ops->vcpu_load(vcpu, cpu);
        if (unlikely(per_cpu(cpu_tsc_khz, cpu) == 0)) {
                unsigned long khz = cpufreq_quick_get(cpu);
@@ -3540,6 +3562,10 @@ static int emulator_cmpxchg_emulated(unsigned long addr,
                goto emul_write;
 
        page = gfn_to_page(vcpu->kvm, gpa >> PAGE_SHIFT);
+       if (is_error_page(page)) {
+               kvm_release_page_clean(page);
+               goto emul_write;
+       }
 
        kaddr = kmap_atomic(page, KM_USER0);
        kaddr += offset_in_page(gpa);
@@ -3660,6 +3686,21 @@ int emulate_invlpg(struct kvm_vcpu *vcpu, gva_t address)
        return X86EMUL_CONTINUE;
 }
 
+int kvm_emulate_wbinvd(struct kvm_vcpu *vcpu)
+{
+       if (!need_emulate_wbinvd(vcpu))
+               return X86EMUL_CONTINUE;
+
+       if (kvm_x86_ops->has_wbinvd_exit()) {
+               smp_call_function_many(vcpu->arch.wbinvd_dirty_mask,
+                               wbinvd_ipi, NULL, 1);
+               cpumask_clear(vcpu->arch.wbinvd_dirty_mask);
+       }
+       wbinvd();
+       return X86EMUL_CONTINUE;
+}
+EXPORT_SYMBOL_GPL(kvm_emulate_wbinvd);
+
 int emulate_clts(struct kvm_vcpu *vcpu)
 {
        kvm_x86_ops->set_cr0(vcpu, kvm_read_cr0_bits(vcpu, ~X86_CR0_TS));
@@ -3895,6 +3936,32 @@ static int handle_emulation_failure(struct kvm_vcpu *vcpu)
        return EMULATE_FAIL;
 }
 
+static bool reexecute_instruction(struct kvm_vcpu *vcpu, gva_t gva)
+{
+       gpa_t gpa;
+
+       if (tdp_enabled)
+               return false;
+
+       /*
+        * if emulation was due to access to shadowed page table
+        * and it failed try to unshadow page and re-entetr the
+        * guest to let CPU execute the instruction.
+        */
+       if (kvm_mmu_unprotect_page_virt(vcpu, gva))
+               return true;
+
+       gpa = kvm_mmu_gva_to_gpa_system(vcpu, gva, NULL);
+
+       if (gpa == UNMAPPED_GVA)
+               return true; /* let cpu generate fault */
+
+       if (!kvm_is_error_hva(gfn_to_hva(vcpu->kvm, gpa >> PAGE_SHIFT)))
+               return true;
+
+       return false;
+}
+
 int emulate_instruction(struct kvm_vcpu *vcpu,
                        unsigned long cr2,
                        u16 error_code,
@@ -3963,7 +4030,7 @@ int emulate_instruction(struct kvm_vcpu *vcpu,
 
                ++vcpu->stat.insn_emulation;
                if (r)  {
-                       if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
+                       if (reexecute_instruction(vcpu, cr2))
                                return EMULATE_DONE;
                        if (emulation_type & EMULTYPE_SKIP)
                                return EMULATE_FAIL;
@@ -3984,12 +4051,7 @@ restart:
        r = x86_emulate_insn(&vcpu->arch.emulate_ctxt, &emulate_ops);
 
        if (r) { /* emulation failed */
-               /*
-                * if emulation was due to access to shadowed page table
-                * and it failed try to unshadow page and re-entetr the
-                * guest to let CPU execute the instruction.
-                */
-               if (kvm_mmu_unprotect_page_virt(vcpu, cr2))
+               if (reexecute_instruction(vcpu, cr2))
                        return EMULATE_DONE;
 
                return handle_emulation_failure(vcpu);
@@ -4616,15 +4678,9 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
        bool req_int_win = !irqchip_in_kernel(vcpu->kvm) &&
                vcpu->run->request_interrupt_window;
 
-       if (vcpu->requests)
+       if (vcpu->requests) {
                if (kvm_check_request(KVM_REQ_MMU_RELOAD, vcpu))
                        kvm_mmu_unload(vcpu);
-
-       r = kvm_mmu_reload(vcpu);
-       if (unlikely(r))
-               goto out;
-
-       if (vcpu->requests) {
                if (kvm_check_request(KVM_REQ_MIGRATE_TIMER, vcpu))
                        __kvm_migrate_timers(vcpu);
                if (kvm_check_request(KVM_REQ_KVMCLOCK_UPDATE, vcpu))
@@ -4649,6 +4705,10 @@ static int vcpu_enter_guest(struct kvm_vcpu *vcpu)
                }
        }
 
+       r = kvm_mmu_reload(vcpu);
+       if (unlikely(r))
+               goto out;
+
        preempt_disable();
 
        kvm_x86_ops->prepare_guest_switch(vcpu);
@@ -5265,6 +5325,7 @@ void kvm_arch_vcpu_free(struct kvm_vcpu *vcpu)
                vcpu->arch.time_page = NULL;
        }
 
+       free_cpumask_var(vcpu->arch.wbinvd_dirty_mask);
        fx_free(vcpu);
        kvm_x86_ops->vcpu_free(vcpu);
 }
@@ -5394,7 +5455,12 @@ int kvm_arch_vcpu_init(struct kvm_vcpu *vcpu)
        }
        vcpu->arch.mcg_cap = KVM_MAX_MCE_BANKS;
 
+       if (!zalloc_cpumask_var(&vcpu->arch.wbinvd_dirty_mask, GFP_KERNEL))
+               goto fail_free_mce_banks;
+
        return 0;
+fail_free_mce_banks:
+       kfree(vcpu->arch.mce_banks);
 fail_free_lapic:
        kvm_free_lapic(vcpu);
 fail_mmu_destroy:
@@ -5466,12 +5532,12 @@ static void kvm_free_vcpus(struct kvm *kvm)
 void kvm_arch_sync_events(struct kvm *kvm)
 {
        kvm_free_all_assigned_devices(kvm);
+       kvm_free_pit(kvm);
 }
 
 void kvm_arch_destroy_vm(struct kvm *kvm)
 {
        kvm_iommu_unmap_guest(kvm);
-       kvm_free_pit(kvm);
        kfree(kvm->arch.vpic);
        kfree(kvm->arch.vioapic);
        kvm_free_vcpus(kvm);