Skip to content

Commit c8563d1

Browse files
Sean Christophersonbonzini
authored andcommitted
KVM: VMX: Split out guts of EPT violation to common/exposed function
The difference of TDX EPT violation is how to retrieve information, GPA, and exit qualification. To share the code to handle EPT violation, split out the guts of EPT violation handler so that VMX/TDX exit handler can call it after retrieving GPA and exit qualification. Signed-off-by: Sean Christopherson <sean.j.christopherson@intel.com> Co-developed-by: Isaku Yamahata <isaku.yamahata@intel.com> Signed-off-by: Isaku Yamahata <isaku.yamahata@intel.com> Co-developed-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Signed-off-by: Rick Edgecombe <rick.p.edgecombe@intel.com> Signed-off-by: Yan Zhao <yan.y.zhao@intel.com> Reviewed-by: Paolo Bonzini <pbonzini@redhat.com> Reviewed-by: Kai Huang <kai.huang@intel.com> Reviewed-by: Binbin Wu <binbin.wu@linux.intel.com> Message-ID: <20241112073528.22042-1-yan.y.zhao@intel.com> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 6d15a64 commit c8563d1

File tree

2 files changed

+37
-22
lines changed

2 files changed

+37
-22
lines changed

arch/x86/kvm/vmx/common.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
/* SPDX-License-Identifier: GPL-2.0-only */
2+
#ifndef __KVM_X86_VMX_COMMON_H
3+
#define __KVM_X86_VMX_COMMON_H
4+
5+
#include <linux/kvm_host.h>
6+
7+
#include "mmu.h"
8+
9+
static inline int __vmx_handle_ept_violation(struct kvm_vcpu *vcpu, gpa_t gpa,
10+
unsigned long exit_qualification)
11+
{
12+
u64 error_code;
13+
14+
/* Is it a read fault? */
15+
error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
16+
? PFERR_USER_MASK : 0;
17+
/* Is it a write fault? */
18+
error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
19+
? PFERR_WRITE_MASK : 0;
20+
/* Is it a fetch fault? */
21+
error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
22+
? PFERR_FETCH_MASK : 0;
23+
/* ept page table entry is present? */
24+
error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK)
25+
? PFERR_PRESENT_MASK : 0;
26+
27+
if (error_code & EPT_VIOLATION_GVA_IS_VALID)
28+
error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) ?
29+
PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
30+
31+
return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
32+
}
33+
34+
#endif /* __KVM_X86_VMX_COMMON_H */

arch/x86/kvm/vmx/vmx.c

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
#include <trace/events/ipi.h>
5454

5555
#include "capabilities.h"
56+
#include "common.h"
5657
#include "cpuid.h"
5758
#include "hyperv.h"
5859
#include "kvm_onhyperv.h"
@@ -5787,11 +5788,8 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
57875788

57885789
static int handle_ept_violation(struct kvm_vcpu *vcpu)
57895790
{
5790-
unsigned long exit_qualification;
5791+
unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
57915792
gpa_t gpa;
5792-
u64 error_code;
5793-
5794-
exit_qualification = vmx_get_exit_qual(vcpu);
57955793

57965794
/*
57975795
* EPT violation happened while executing iret from NMI,
@@ -5807,23 +5805,6 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
58075805
gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
58085806
trace_kvm_page_fault(vcpu, gpa, exit_qualification);
58095807

5810-
/* Is it a read fault? */
5811-
error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
5812-
? PFERR_USER_MASK : 0;
5813-
/* Is it a write fault? */
5814-
error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
5815-
? PFERR_WRITE_MASK : 0;
5816-
/* Is it a fetch fault? */
5817-
error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
5818-
? PFERR_FETCH_MASK : 0;
5819-
/* ept page table entry is present? */
5820-
error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK)
5821-
? PFERR_PRESENT_MASK : 0;
5822-
5823-
if (error_code & EPT_VIOLATION_GVA_IS_VALID)
5824-
error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) ?
5825-
PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
5826-
58275808
/*
58285809
* Check that the GPA doesn't exceed physical memory limits, as that is
58295810
* a guest page fault. We have to emulate the instruction here, because
@@ -5835,7 +5816,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
58355816
if (unlikely(allow_smaller_maxphyaddr && !kvm_vcpu_is_legal_gpa(vcpu, gpa)))
58365817
return kvm_emulate_instruction(vcpu, 0);
58375818

5838-
return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
5819+
return __vmx_handle_ept_violation(vcpu, gpa, exit_qualification);
58395820
}
58405821

58415822
static int handle_ept_misconfig(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)