Skip to content

Commit 748bf53

Browse files
Sean Christophersonjfvogel
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: d0164c161923 ("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: Jack Vogel <[email protected]>
1 parent da15121 commit 748bf53

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"
@@ -5777,11 +5778,8 @@ static int handle_task_switch(struct kvm_vcpu *vcpu)
57775778

57785779
static int handle_ept_violation(struct kvm_vcpu *vcpu)
57795780
{
5780-
unsigned long exit_qualification;
5781+
unsigned long exit_qualification = vmx_get_exit_qual(vcpu);
57815782
gpa_t gpa;
5782-
u64 error_code;
5783-
5784-
exit_qualification = vmx_get_exit_qual(vcpu);
57855783

57865784
/*
57875785
* EPT violation happened while executing iret from NMI,
@@ -5797,23 +5795,6 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
57975795
gpa = vmcs_read64(GUEST_PHYSICAL_ADDRESS);
57985796
trace_kvm_page_fault(vcpu, gpa, exit_qualification);
57995797

5800-
/* Is it a read fault? */
5801-
error_code = (exit_qualification & EPT_VIOLATION_ACC_READ)
5802-
? PFERR_USER_MASK : 0;
5803-
/* Is it a write fault? */
5804-
error_code |= (exit_qualification & EPT_VIOLATION_ACC_WRITE)
5805-
? PFERR_WRITE_MASK : 0;
5806-
/* Is it a fetch fault? */
5807-
error_code |= (exit_qualification & EPT_VIOLATION_ACC_INSTR)
5808-
? PFERR_FETCH_MASK : 0;
5809-
/* ept page table entry is present? */
5810-
error_code |= (exit_qualification & EPT_VIOLATION_RWX_MASK)
5811-
? PFERR_PRESENT_MASK : 0;
5812-
5813-
if (error_code & EPT_VIOLATION_GVA_IS_VALID)
5814-
error_code |= (exit_qualification & EPT_VIOLATION_GVA_TRANSLATED) ?
5815-
PFERR_GUEST_FINAL_MASK : PFERR_GUEST_PAGE_MASK;
5816-
58175798
/*
58185799
* Check that the GPA doesn't exceed physical memory limits, as that is
58195800
* a guest page fault. We have to emulate the instruction here, because
@@ -5825,7 +5806,7 @@ static int handle_ept_violation(struct kvm_vcpu *vcpu)
58255806
if (unlikely(allow_smaller_maxphyaddr && !kvm_vcpu_is_legal_gpa(vcpu, gpa)))
58265807
return kvm_emulate_instruction(vcpu, 0);
58275808

5828-
return kvm_mmu_page_fault(vcpu, gpa, error_code, NULL, 0);
5809+
return __vmx_handle_ept_violation(vcpu, gpa, exit_qualification);
58295810
}
58305811

58315812
static int handle_ept_misconfig(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)