]> git.baikalelectronics.ru Git - kernel.git/commitdiff
KVM: SVM: Unwind "speculative" RIP advancement if INTn injection "fails"
authorSean Christopherson <seanjc@google.com>
Sun, 1 May 2022 22:07:27 +0000 (00:07 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 17 Aug 2022 12:23:40 +0000 (14:23 +0200)
[ Upstream commit 84654a9e8e2d3441e7f1d1499f5fdfbde07e0d5d ]

Unwind the RIP advancement done by svm_queue_exception() when injecting
an INT3 ultimately "fails" due to the CPU encountering a VM-Exit while
vectoring the injected event, even if the exception reported by the CPU
isn't the same event that was injected.  If vectoring INT3 encounters an
exception, e.g. #NP, and vectoring the #NP encounters an intercepted
exception, e.g. #PF when KVM is using shadow paging, then the #NP will
be reported as the event that was in-progress.

Note, this is still imperfect, as it will get a false positive if the
INT3 is cleanly injected, no VM-Exit occurs before the IRET from the INT3
handler in the guest, the instruction following the INT3 generates an
exception (directly or indirectly), _and_ vectoring that exception
encounters an exception that is intercepted by KVM.  The false positives
could theoretically be solved by further analyzing the vectoring event,
e.g. by comparing the error code against the expected error code were an
exception to occur when vectoring the original injected exception, but
SVM without NRIPS is a complete disaster, trying to make it 100% correct
is a waste of time.

Reviewed-by: Maxim Levitsky <mlevitsk@redhat.com>
Fixes: 397076fd8c2e ("KVM: SVM: Emulate nRIP feature when reinjecting INT3")
Signed-off-by: Sean Christopherson <seanjc@google.com>
Signed-off-by: Maciej S. Szmigiero <maciej.szmigiero@oracle.com>
Message-Id: <450133cf0a026cb9825a2ff55d02cb136a1cb111.1651440202.git.maciej.szmigiero@oracle.com>
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
Signed-off-by: Sasha Levin <sashal@kernel.org>
arch/x86/kvm/svm/svm.c

index b001f7d94d1d32fa94ba168f5026a8ddc0a7a099..05d76832362dcd2f4d49cbc16dd3e51f56700a65 100644 (file)
@@ -3680,6 +3680,18 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
        vector = exitintinfo & SVM_EXITINTINFO_VEC_MASK;
        type = exitintinfo & SVM_EXITINTINFO_TYPE_MASK;
 
+       /*
+        * If NextRIP isn't enabled, KVM must manually advance RIP prior to
+        * injecting the soft exception/interrupt.  That advancement needs to
+        * be unwound if vectoring didn't complete.  Note, the _new_ event may
+        * not be the injected event, e.g. if KVM injected an INTn, the INTn
+        * hit a #NP in the guest, and the #NP encountered a #PF, the #NP will
+        * be the reported vectored event, but RIP still needs to be unwound.
+        */
+       if (int3_injected && type == SVM_EXITINTINFO_TYPE_EXEPT &&
+          kvm_is_linear_rip(vcpu, svm->int3_rip))
+               kvm_rip_write(vcpu, kvm_rip_read(vcpu) - int3_injected);
+
        switch (type) {
        case SVM_EXITINTINFO_TYPE_NMI:
                vcpu->arch.nmi_injected = true;
@@ -3693,16 +3705,11 @@ static void svm_complete_interrupts(struct kvm_vcpu *vcpu)
 
                /*
                 * In case of software exceptions, do not reinject the vector,
-                * but re-execute the instruction instead. Rewind RIP first
-                * if we emulated INT3 before.
+                * but re-execute the instruction instead.
                 */
-               if (kvm_exception_is_soft(vector)) {
-                       if (vector == BP_VECTOR && int3_injected &&
-                           kvm_is_linear_rip(vcpu, svm->int3_rip))
-                               kvm_rip_write(vcpu,
-                                             kvm_rip_read(vcpu) - int3_injected);
+               if (kvm_exception_is_soft(vector))
                        break;
-               }
+
                if (exitintinfo & SVM_EXITINTINFO_VALID_ERR) {
                        u32 err = svm->vmcb->control.exit_int_info_err;
                        kvm_requeue_exception_e(vcpu, vector, err);