Skip to content

Commit f9d60e1

Browse files
Sean Christophersonopsiff
authored andcommitted
KVM: VMX: Split out guts of EPT violation to common/exposed function
[ Upstream commit c8563d1 ] 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 <[email protected]> Co-developed-by: Isaku Yamahata <[email protected]> Signed-off-by: Isaku Yamahata <[email protected]> Co-developed-by: Rick Edgecombe <[email protected]> Signed-off-by: Rick Edgecombe <[email protected]> Signed-off-by: Yan Zhao <[email protected]> Reviewed-by: Paolo Bonzini <[email protected]> Reviewed-by: Kai Huang <[email protected]> Reviewed-by: Binbin Wu <[email protected]> Message-ID: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Stable-dep-of: d0164c1 ("KVM: VMX: Fix check for valid GVA on an EPT violation") Signed-off-by: Sasha Levin <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]> (cherry picked from commit 6db2b0eb3251b45fab7310fd93f233bc22282933) Signed-off-by: Wentao Guan <[email protected]>
1 parent 3e86840 commit f9d60e1

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"
@@ -5822,11 +5823,8 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
58225823

58235824
static int handle_ept_violation(struct kvm_vcpu *vcpu)
58245825
{
5825-
unsigned long exit_qualification;
5826+
unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
58265827
gpa_t gpa;
5827-
u64 error_code;
5828-
5829-
exit_qualification = vmx_get_exit_qual(vcpu);
58305828

58315829
/*
58325830
* EPT violation happened while executing iret from NMI,
@@ -5842,23 +5840,6 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
58425840
gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
58435841
trace_kvm_page_fault(vcpu, gpa, exit_qualification);
58445842

5845-
/* Is it a read fault? */
5846-
error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
5847-
? PFERR_USER_MASK : 0;
5848-
/* Is it a write fault? */
5849-
error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
5850-
? PFERR_WRITE_MASK : 0;
5851-
/* Is it a fetch fault? */
5852-
error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
5853-
? PFERR_FETCH_MASK : 0;
5854-
/* ept page table entry is present? */
5855-
error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK)
5856-
? PFERR_PRESENT_MASK : 0;
5857-
5858-
if (error_code & EPT_VIOLATION_GVA_IS_VALID)
5859-
error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) ?
5860-
PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
5861-
58625843
/*
58635844
* Check that the GPA doesn't exceed physical memory limits, as that is
58645845
* a guest page fault. We have to emulate the instruction here, because
@@ -5870,7 +5851,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
58705851
if (unlikely(allow_smaller_maxphyaddr && !kvm_vcpu_is_legal_gpa(vcpu, gpa)))
58715852
return kvm_emulate_instruction(vcpu, 0);
58725853

5873-
return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
5854+
return __vmx_handle_ept_violation(vcpu, gpa, exit_qualification);
58745855
}
58755856

58765857
static int handle_ept_misconfig(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)