Skip to content

Commit ff8071e

Browse files
committed
KVM: VMX: Always reflect SGX EPCM #PFs back into the guest
When handling intercepted #PFs, reflect EPCM (Enclave Page Cache Map) violations, i.e. #PFs with the SGX flag set, back into the guest. KVM doesn't shadow EPCM entries (the EPCM deals only with virtual/linear addresses), and so EPCM violation cannot be due to KVM interference, and more importantly can't be resolved by KVM. On pre-SGX2 hardware, EPCM violations are delivered as #GP(0) faults, but on SGX2+ hardware, they are delivered as #PF(SGX). Failure to account for the SGX2 behavior could put a vCPU into an infinite loop due to KVM not realizing the #PF is the guest's responsibility. Take care to deliver the EPCM violation as a #GP(0) if the _guest_ CPU model is only SGX1. Fixes: 72add91 ("KVM: VMX: Enable SGX virtualization for SGX1, SGX2 and LC") Cc: Kai Huang <kai.huang@intel.com> Reviewed-by: Richard Lyu <richard.lyu@suse.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Link: https://patch.msgid.link/20251121222018.348987-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent 9ace475 commit ff8071e

1 file changed

Lines changed: 44 additions & 14 deletions

File tree

arch/x86/kvm/vmx/vmx.c

Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -5303,12 +5303,53 @@ static bool is_xfd_nm_fault(struct kvm_vcpu *vcpu)
53035303
!kvm_is_cr0_bit_set(vcpu, X86_CR0_TS);
53045304
}
53055305

5306+
static int vmx_handle_page_fault(struct kvm_vcpu *vcpu, u32 error_code)
5307+
{
5308+
unsigned long cr2 = vmx_get_exit_qual(vcpu);
5309+
5310+
if (vcpu->arch.apf.host_apf_flags)
5311+
goto handle_pf;
5312+
5313+
/* When using EPT, KVM intercepts #PF only to detect illegal GPAs. */
5314+
WARN_ON_ONCE(enable_ept && !allow_smaller_maxphyaddr);
5315+
5316+
/*
5317+
* On SGX2 hardware, EPCM violations are delivered as #PF with the SGX
5318+
* flag set in the error code (SGX1 hardware generates #GP(0)). EPCM
5319+
* violations have nothing to do with shadow paging and can never be
5320+
* resolved by KVM; always reflect them into the guest.
5321+
*/
5322+
if (error_code & PFERR_SGX_MASK) {
5323+
WARN_ON_ONCE(!IS_ENABLED(CONFIG_X86_SGX_KVM) ||
5324+
!cpu_feature_enabled(X86_FEATURE_SGX2));
5325+
5326+
if (guest_cpu_cap_has(vcpu, X86_FEATURE_SGX2))
5327+
kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
5328+
else
5329+
kvm_inject_gp(vcpu, 0);
5330+
return 1;
5331+
}
5332+
5333+
/*
5334+
* If EPT is enabled, fixup and inject the #PF. KVM intercepts #PFs
5335+
* only to set PFERR_RSVD as appropriate (hardware won't set RSVD due
5336+
* to the GPA being legal with respect to host.MAXPHYADDR).
5337+
*/
5338+
if (enable_ept) {
5339+
kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
5340+
return 1;
5341+
}
5342+
5343+
handle_pf:
5344+
return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
5345+
}
5346+
53065347
static int handle_exception_nmi(struct kvm_vcpu *vcpu)
53075348
{
53085349
struct vcpu_vmx *vmx = to_vmx(vcpu);
53095350
struct kvm_run *kvm_run = vcpu->run;
53105351
u32 intr_info, ex_no, error_code;
5311-
unsigned long cr2, dr6;
5352+
unsigned long dr6;
53125353
u32 vect_info;
53135354

53145355
vect_info = vmx->idt_vectoring_info;
@@ -5383,19 +5424,8 @@ static int handle_exception_nmi(struct kvm_vcpu *vcpu)
53835424
return 0;
53845425
}
53855426

5386-
if (is_page_fault(intr_info)) {
5387-
cr2 = vmx_get_exit_qual(vcpu);
5388-
if (enable_ept && !vcpu->arch.apf.host_apf_flags) {
5389-
/*
5390-
* EPT will cause page fault only if we need to
5391-
* detect illegal GPAs.
5392-
*/
5393-
WARN_ON_ONCE(!allow_smaller_maxphyaddr);
5394-
kvm_fixup_and_inject_pf_error(vcpu, cr2, error_code);
5395-
return 1;
5396-
} else
5397-
return kvm_handle_page_fault(vcpu, error_code, cr2, NULL, 0);
5398-
}
5427+
if (is_page_fault(intr_info))
5428+
return vmx_handle_page_fault(vcpu, error_code);
53995429

54005430
ex_no = intr_info & INTR_INFO_VECTOR_MASK;
54015431

0 commit comments

Comments
 (0)