Skip to content

Commit 8978614

Browse files
Mohammed Gamalbonzini
authored andcommitted
KVM: x86: Add helper functions for illegal GPA checking and page fault injection
This patch adds two helper functions that will be used to support virtualizing MAXPHYADDR in both kvm-intel.ko and kvm.ko. kvm_fixup_and_inject_pf_error() injects a page fault for a user-specified GVA, while kvm_mmu_is_illegal_gpa() checks whether a GPA exceeds vCPU address limits. Signed-off-by: Mohammed Gamal <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]> Message-Id: <[email protected]> Signed-off-by: Paolo Bonzini <[email protected]>
1 parent fe9304d commit 8978614

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

arch/x86/kvm/mmu.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <linux/kvm_host.h>
66
#include "kvm_cache_regs.h"
7+
#include "cpuid.h"
78

89
#define PT64_PT_BITS 9
910
#define PT64_ENT_PER_PAGE (1 << PT64_PT_BITS)
@@ -150,6 +151,11 @@ static inline bool is_write_protection(struct kvm_vcpu *vcpu)
150151
return kvm_read_cr0_bits(vcpu, X86_CR0_WP);
151152
}
152153

154+
static inline bool kvm_mmu_is_illegal_gpa(struct kvm_vcpu *vcpu, gpa_t gpa)
155+
{
156+
return (gpa >= BIT_ULL(cpuid_maxphyaddr(vcpu)));
157+
}
158+
153159
/*
154160
* Check if a given access (described through the I/D, W/R and U/S bits of a
155161
* page fault error code pfec) causes a permission fault with the given PTE

arch/x86/kvm/x86.c

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10738,6 +10738,27 @@ int kvm_spec_ctrl_test_value(u64 value)
1073810738
}
1073910739
EXPORT_SYMBOL_GPL(kvm_spec_ctrl_test_value);
1074010740

10741+
void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code)
10742+
{
10743+
struct x86_exception fault;
10744+
10745+
if (!(error_code & PFERR_PRESENT_MASK) ||
10746+
vcpu->arch.walk_mmu->gva_to_gpa(vcpu, gva, error_code, &fault) != UNMAPPED_GVA) {
10747+
/*
10748+
* If vcpu->arch.walk_mmu->gva_to_gpa succeeded, the page
10749+
* tables probably do not match the TLB. Just proceed
10750+
* with the error code that the processor gave.
10751+
*/
10752+
fault.vector = PF_VECTOR;
10753+
fault.error_code_valid = true;
10754+
fault.error_code = error_code;
10755+
fault.nested_page_fault = false;
10756+
fault.address = gva;
10757+
}
10758+
vcpu->arch.walk_mmu->inject_page_fault(vcpu, &fault);
10759+
}
10760+
EXPORT_SYMBOL_GPL(kvm_fixup_and_inject_pf_error);
10761+
1074110762
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_exit);
1074210763
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_fast_mmio);
1074310764
EXPORT_TRACEPOINT_SYMBOL_GPL(kvm_inj_virq);

arch/x86/kvm/x86.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,6 +272,7 @@ int kvm_mtrr_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *pdata);
272272
bool kvm_mtrr_check_gfn_range_consistency(struct kvm_vcpu *vcpu, gfn_t gfn,
273273
int page_num);
274274
bool kvm_vector_hashing_enabled(void);
275+
void kvm_fixup_and_inject_pf_error(struct kvm_vcpu *vcpu, gva_t gva, u16 error_code);
275276
int x86_emulate_instruction(struct kvm_vcpu *vcpu, gpa_t cr2_or_gpa,
276277
int emulation_type, void *insn, int insn_len);
277278
fastpath_t handle_fastpath_set_msr_irqoff(struct kvm_vcpu *vcpu);

0 commit comments

Comments
 (0)