KVM: SVM: always update CR3 in VMCB
[sfrench/cifs-2.6.git] / arch / x86 / kvm / svm / svm.c
index 4e9cd2a73ad035fb0ba7e50aee4a61917dfd5de0..feb96a410f2de0548efdd1b760205febe6b7fe17 100644 (file)
@@ -331,17 +331,8 @@ static void svm_queue_exception(struct kvm_vcpu *vcpu)
        struct vcpu_svm *svm = to_svm(vcpu);
        unsigned nr = vcpu->arch.exception.nr;
        bool has_error_code = vcpu->arch.exception.has_error_code;
-       bool reinject = vcpu->arch.exception.injected;
        u32 error_code = vcpu->arch.exception.error_code;
 
-       /*
-        * If we are within a nested VM we'd better #VMEXIT and let the guest
-        * handle the exception
-        */
-       if (!reinject &&
-           nested_svm_check_exception(svm, nr, has_error_code, error_code))
-               return;
-
        kvm_deliver_exception_payload(&svm->vcpu);
 
        if (nr == BP_VECTOR && !nrips) {
@@ -778,7 +769,7 @@ static __init void svm_adjust_mmio_mask(void)
         */
        mask = (mask_bit < 52) ? rsvd_bits(mask_bit, 51) | PT_PRESENT_MASK : 0;
 
-       kvm_mmu_set_mmio_spte_mask(mask, mask, PT_WRITABLE_MASK | PT_USER_MASK);
+       kvm_mmu_set_mmio_spte_mask(mask, PT_WRITABLE_MASK | PT_USER_MASK);
 }
 
 static void svm_hardware_teardown(void)
@@ -2898,13 +2889,6 @@ static int handle_exit(struct kvm_vcpu *vcpu, fastpath_t exit_fastpath)
        if (npt_enabled)
                vcpu->arch.cr3 = svm->vmcb->save.cr3;
 
-       if (unlikely(svm->nested.exit_required)) {
-               nested_svm_vmexit(svm);
-               svm->nested.exit_required = false;
-
-               return 1;
-       }
-
        if (is_guest_mode(vcpu)) {
                int vmexit;
 
@@ -3053,15 +3037,15 @@ bool svm_nmi_blocked(struct kvm_vcpu *vcpu)
        return ret;
 }
 
-static bool svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
+static int svm_nmi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
        if (svm->nested.nested_run_pending)
-               return false;
+               return -EBUSY;
 
        /* An NMI must not be injected into L2 if it's supposed to VM-Exit.  */
        if (for_injection && is_guest_mode(vcpu) && nested_exit_on_nmi(svm))
-               return false;
+               return -EBUSY;
 
        return !svm_nmi_blocked(vcpu);
 }
@@ -3112,18 +3096,18 @@ bool svm_interrupt_blocked(struct kvm_vcpu *vcpu)
        return (vmcb->control.int_state & SVM_INTERRUPT_SHADOW_MASK);
 }
 
-static bool svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
+static int svm_interrupt_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
        if (svm->nested.nested_run_pending)
-               return false;
+               return -EBUSY;
 
        /*
         * An IRQ must not be injected into L2 if it's supposed to VM-Exit,
         * e.g. if the IRQ arrived asynchronously after checking nested events.
         */
        if (for_injection && is_guest_mode(vcpu) && nested_exit_on_intr(svm))
-               return false;
+               return -EBUSY;
 
        return !svm_interrupt_blocked(vcpu);
 }
@@ -3336,13 +3320,6 @@ static fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
        svm->vmcb->save.rsp = vcpu->arch.regs[VCPU_REGS_RSP];
        svm->vmcb->save.rip = vcpu->arch.regs[VCPU_REGS_RIP];
 
-       /*
-        * A vmexit emulation is required before the vcpu can be executed
-        * again.
-        */
-       if (unlikely(svm->nested.exit_required))
-               return EXIT_FASTPATH_NONE;
-
        /*
         * Disable singlestep if we're injecting an interrupt/exception.
         * We don't want our modified rflags to be pushed on the stack where
@@ -3470,7 +3447,6 @@ static fastpath_t svm_vcpu_run(struct kvm_vcpu *vcpu)
 static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long root)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
-       bool update_guest_cr3 = true;
        unsigned long cr3;
 
        cr3 = __sme_set(root);
@@ -3479,18 +3455,13 @@ static void svm_load_mmu_pgd(struct kvm_vcpu *vcpu, unsigned long root)
                mark_dirty(svm->vmcb, VMCB_NPT);
 
                /* Loading L2's CR3 is handled by enter_svm_guest_mode.  */
-               if (is_guest_mode(vcpu))
-                       update_guest_cr3 = false;
-               else if (test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
-                       cr3 = vcpu->arch.cr3;
-               else /* CR3 is already up-to-date.  */
-                       update_guest_cr3 = false;
+               if (!test_bit(VCPU_EXREG_CR3, (ulong *)&vcpu->arch.regs_avail))
+                       return;
+               cr3 = vcpu->arch.cr3;
        }
 
-       if (update_guest_cr3) {
-               svm->vmcb->save.cr3 = cr3;
-               mark_dirty(svm->vmcb, VMCB_CR);
-       }
+       svm->vmcb->save.cr3 = cr3;
+       mark_dirty(svm->vmcb, VMCB_CR);
 }
 
 static int is_disabled(void)
@@ -3525,7 +3496,7 @@ static bool svm_cpu_has_accelerated_tpr(void)
        return false;
 }
 
-static bool svm_has_emulated_msr(int index)
+static bool svm_has_emulated_msr(u32 index)
 {
        switch (index) {
        case MSR_IA32_MCG_EXT_CTL:
@@ -3793,15 +3764,15 @@ bool svm_smi_blocked(struct kvm_vcpu *vcpu)
        return is_smm(vcpu);
 }
 
-static bool svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
+static int svm_smi_allowed(struct kvm_vcpu *vcpu, bool for_injection)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
        if (svm->nested.nested_run_pending)
-               return false;
+               return -EBUSY;
 
        /* An SMI must not be injected into L2 if it's supposed to VM-Exit.  */
        if (for_injection && is_guest_mode(vcpu) && nested_exit_on_smi(svm))
-               return false;
+               return -EBUSY;
 
        return !svm_smi_blocked(vcpu);
 }
@@ -3848,7 +3819,7 @@ static int svm_pre_leave_smm(struct kvm_vcpu *vcpu, const char *smstate)
        return 0;
 }
 
-static int enable_smi_window(struct kvm_vcpu *vcpu)
+static void enable_smi_window(struct kvm_vcpu *vcpu)
 {
        struct vcpu_svm *svm = to_svm(vcpu);
 
@@ -3856,9 +3827,9 @@ static int enable_smi_window(struct kvm_vcpu *vcpu)
                if (vgif_enabled(svm))
                        set_intercept(svm, INTERCEPT_STGI);
                /* STGI will cause a vm exit */
-               return 1;
+       } else {
+               /* We must be in SMM; RSM will cause a vmexit anyway.  */
        }
-       return 0;
 }
 
 static bool svm_need_emulation_on_page_fault(struct kvm_vcpu *vcpu)