Skip to content

Commit dee281e

Browse files
sean-jcbonzini
authored andcommitted
KVM: x86: Move synthetic PFERR_* sanity checks to SVM's #NPF handler
Move the sanity check that hardware never sets bits that collide with KVM- define synthetic bits from kvm_mmu_page_fault() to npf_interception(), i.e. make the sanity check #NPF specific. The legacy #PF path already WARNs if _any_ of bits 63:32 are set, and the error code that comes from VMX's EPT Violatation and Misconfig is 100% synthesized (KVM morphs VMX's EXIT_QUALIFICATION into error code flags). Add a compile-time assert in the legacy #PF handler to make sure that KVM- define flags are covered by its existing sanity check on the upper bits. Opportunistically add a description of PFERR_IMPLICIT_ACCESS, since we are removing the comment that defined it. Signed-off-by: Sean Christopherson <seanjc@google.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Message-ID: <20240228024147.41573-8-seanjc@google.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 9b62e03 commit dee281e

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

arch/x86/include/asm/kvm_host.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,7 +267,13 @@ enum x86_intercept_stage;
267267
#define PFERR_GUEST_ENC_MASK BIT_ULL(34)
268268
#define PFERR_GUEST_SIZEM_MASK BIT_ULL(35)
269269
#define PFERR_GUEST_VMPL_MASK BIT_ULL(36)
270+
271+
/*
272+
* IMPLICIT_ACCESS is a KVM-defined flag used to correctly perform SMAP checks
273+
* when emulating instructions that triggers implicit access.
274+
*/
270275
#define PFERR_IMPLICIT_ACCESS BIT_ULL(48)
276+
#define PFERR_SYNTHETIC_MASK (PFERR_IMPLICIT_ACCESS)
271277

272278
#define PFERR_NESTED_GUEST_PAGE (PFERR_GUEST_PAGE_MASK | \
273279
PFERR_WRITE_MASK | \

arch/x86/kvm/mmu/mmu.c

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,6 +4501,9 @@ int kvm_handle_page_fault(struct kvm_vcpu *vcpu, u64 error_code,
45014501
return -EFAULT;
45024502
#endif
45034503

4504+
/* Ensure the above sanity check also covers KVM-defined flags. */
4505+
BUILD_BUG_ON(lower_32_bits(PFERR_SYNTHETIC_MASK));
4506+
45044507
vcpu->arch.l1tf_flush_l1d = true;
45054508
if (!flags) {
45064509
trace_kvm_page_fault(vcpu, fault_address, error_code);
@@ -5785,17 +5788,6 @@ int noinline kvm_mmu_page_fault(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa, u64 err
57855788
int r, emulation_type = EMULTYPE_PF;
57865789
bool direct = vcpu->arch.mmu->root_role.direct;
57875790

5788-
/*
5789-
* IMPLICIT_ACCESS is a KVM-defined flag used to correctly perform SMAP
5790-
* checks when emulating instructions that triggers implicit access.
5791-
* WARN if hardware generates a fault with an error code that collides
5792-
* with the KVM-defined value. Clear the flag and continue on, i.e.
5793-
* don't terminate the VM, as KVM can't possibly be relying on a flag
5794-
* that KVM doesn't know about.
5795-
*/
5796-
if (WARN_ON_ONCE(error_code & PFERR_IMPLICIT_ACCESS))
5797-
error_code &= ~PFERR_IMPLICIT_ACCESS;
5798-
57995791
if (WARN_ON_ONCE(!VALID_PAGE(vcpu->arch.mmu->root.hpa)))
58005792
return RET_PF_RETRY;
58015793

arch/x86/kvm/svm/svm.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2047,6 +2047,15 @@ static int npf_interception(struct kvm_vcpu *vcpu)
20472047
u64 fault_address = svm->vmcb->control.exit_info_2;
20482048
u64 error_code = svm->vmcb->control.exit_info_1;
20492049

2050+
/*
2051+
* WARN if hardware generates a fault with an error code that collides
2052+
* with KVM-defined sythentic flags. Clear the flags and continue on,
2053+
* i.e. don't terminate the VM, as KVM can't possibly be relying on a
2054+
* flag that KVM doesn't know about.
2055+
*/
2056+
if (WARN_ON_ONCE(error_code & PFERR_SYNTHETIC_MASK))
2057+
error_code &= ~PFERR_SYNTHETIC_MASK;
2058+
20502059
trace_kvm_page_fault(vcpu, fault_address, error_code);
20512060
return kvm_mmu_page_fault(vcpu, fault_address, error_code,
20522061
static_cpu_has(X86_FEATURE_DECODEASSISTS) ?

0 commit comments

Comments
 (0)