Skip to content

Commit 01cde4e

Browse files
committed
KVM: SVM: Add support for expedited writes to the fast MMIO bus
Wire up SVM's #NPF handler to fast MMIO. While SVM doesn't provide a dedicated exit reason, it's trivial to key off PFERR_RSVD_MASK. Like VMX, restrict the fast path to L1 to avoid having to deal with nGPA=>GPA translations. For simplicity, use the fast path if and only if the next RIP is known. While KVM could utilize EMULTYPE_SKIP, doing so would require additional logic to deal with SEV guests, e.g. to go down the slow path if the instruction buffer is empty. All modern CPUs support next RIP, and in practice the next RIP will be available for any guest fast path. Copy+paste the kvm_io_bus_write() + trace_kvm_fast_mmio() logic even though KVM would ideally provide a small helper, as such a helper would need to either be a macro or non-inline to avoid including trace.h in a header (trace.h must not be included by x86.c prior to CREATE_TRACE_POINTS being defined). Link: https://patch.msgid.link/20251113221642.1673023-3-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 737f2a3 commit 01cde4e

1 file changed

Lines changed: 21 additions & 0 deletions

File tree

arch/x86/kvm/svm/svm.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1862,6 +1862,9 @@ static int pf_interception(struct kvm_vcpu *vcpu)
18621862
svm->vmcb->control.insn_len);
18631863
}
18641864

1865+
static int svm_check_emulate_instruction(struct kvm_vcpu *vcpu, int emul_type,
1866+
void *insn, int insn_len);
1867+
18651868
static int npf_interception(struct kvm_vcpu *vcpu)
18661869
{
18671870
struct vcpu_svm *svm = to_svm(vcpu);
@@ -1879,6 +1882,24 @@ static int npf_interception(struct kvm_vcpu *vcpu)
18791882
if (WARN_ON_ONCE(error_code & PFERR_SYNTHETIC_MASK))
18801883
error_code &= ~PFERR_SYNTHETIC_MASK;
18811884

1885+
/*
1886+
* Expedite fast MMIO kicks if the next RIP is known and KVM is allowed
1887+
* emulate a page fault, e.g. skipping the current instruction is wrong
1888+
* if the #NPF occurred while vectoring an event.
1889+
*/
1890+
if ((error_code & PFERR_RSVD_MASK) && !is_guest_mode(vcpu)) {
1891+
const int emul_type = EMULTYPE_PF | EMULTYPE_NO_DECODE;
1892+
1893+
if (svm_check_emulate_instruction(vcpu, emul_type, NULL, 0))
1894+
return 1;
1895+
1896+
if (nrips && svm->vmcb->control.next_rip &&
1897+
!kvm_io_bus_write(vcpu, KVM_FAST_MMIO_BUS, gpa, 0, NULL)) {
1898+
trace_kvm_fast_mmio(gpa);
1899+
return kvm_skip_emulated_instruction(vcpu);
1900+
}
1901+
}
1902+
18821903
if (sev_snp_guest(vcpu->kvm) && (error_code & PFERR_GUEST_ENC_MASK))
18831904
error_code |= PFERR_PRIVATE_ACCESS;
18841905

0 commit comments

Comments
 (0)