Skip to content

Commit 4407a79

Browse files
babumogerbonzini
authored andcommitted
KVM: SVM: Enable INVPCID feature on AMD
The following intercept bit has been added to support VMEXIT for INVPCID instruction: Code Name Cause A2h VMEXIT_INVPCID INVPCID instruction The following bit has been added to the VMCB layout control area to control intercept of INVPCID: Byte Offset Bit(s) Function 14h 2 intercept INVPCID Enable the interceptions when the the guest is running with shadow page table enabled and handle the tlbflush based on the invpcid instruction type. For the guests with nested page table (NPT) support, the INVPCID feature works as running it natively. KVM does not need to do any special handling in this case. AMD documentation for INVPCID feature is available at "AMD64 Architecture Programmer’s Manual Volume 2: System Programming, Pub. 24593 Rev. 3.34(or later)" The documentation can be obtained at the links below: Link: https://www.amd.com/system/files/TechDocs/24593.pdf Link: https://bugzilla.kernel.org/show_bug.cgi?id=206537 Signed-off-by: Babu Moger <babu.moger@amd.com> Reviewed-by: Jim Mattson <jmattson@google.com> Message-Id: <159985255929.11252.17346684135277453258.stgit@bmoger-ubuntu> Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
1 parent 9715092 commit 4407a79

2 files changed

Lines changed: 53 additions & 0 deletions

File tree

‎arch/x86/include/uapi/asm/svm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
#define SVM_EXIT_MWAIT_COND 0x08c
7777
#define SVM_EXIT_XSETBV 0x08d
7878
#define SVM_EXIT_RDPRU 0x08e
79+
#define SVM_EXIT_INVPCID 0x0a2
7980
#define SVM_EXIT_NPF 0x400
8081
#define SVM_EXIT_AVIC_INCOMPLETE_IPI 0x401
8182
#define SVM_EXIT_AVIC_UNACCELERATED_ACCESS 0x402
@@ -171,6 +172,7 @@
171172
{ SVM_EXIT_MONITOR, "monitor" }, \
172173
{ SVM_EXIT_MWAIT, "mwait" }, \
173174
{ SVM_EXIT_XSETBV, "xsetbv" }, \
175+
{ SVM_EXIT_INVPCID, "invpcid" }, \
174176
{ SVM_EXIT_NPF, "npf" }, \
175177
{ SVM_EXIT_AVIC_INCOMPLETE_IPI, "avic_incomplete_ipi" }, \
176178
{ SVM_EXIT_AVIC_UNACCELERATED_ACCESS, "avic_unaccelerated_access" }, \

‎arch/x86/kvm/svm/svm.c‎

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -824,6 +824,9 @@ static __init void svm_set_cpu_caps(void)
824824
if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) ||
825825
boot_cpu_has(X86_FEATURE_AMD_SSBD))
826826
kvm_cpu_cap_set(X86_FEATURE_VIRT_SSBD);
827+
828+
/* Enable INVPCID feature */
829+
kvm_cpu_cap_check_and_set(X86_FEATURE_INVPCID);
827830
}
828831

829832
static __init int svm_hardware_setup(void)
@@ -996,6 +999,21 @@ static u64 svm_write_l1_tsc_offset(struct kvm_vcpu *vcpu, u64 offset)
996999
return svm->vmcb->control.tsc_offset;
9971000
}
9981001

1002+
static void svm_check_invpcid(struct vcpu_svm *svm)
1003+
{
1004+
/*
1005+
* Intercept INVPCID instruction only if shadow page table is
1006+
* enabled. Interception is not required with nested page table
1007+
* enabled.
1008+
*/
1009+
if (kvm_cpu_cap_has(X86_FEATURE_INVPCID)) {
1010+
if (!npt_enabled)
1011+
svm_set_intercept(svm, INTERCEPT_INVPCID);
1012+
else
1013+
svm_clr_intercept(svm, INTERCEPT_INVPCID);
1014+
}
1015+
}
1016+
9991017
static void init_vmcb(struct vcpu_svm *svm)
10001018
{
10011019
struct vmcb_control_area *control = &svm->vmcb->control;
@@ -1125,6 +1143,8 @@ static void init_vmcb(struct vcpu_svm *svm)
11251143
svm_clr_intercept(svm, INTERCEPT_PAUSE);
11261144
}
11271145

1146+
svm_check_invpcid(svm);
1147+
11281148
if (kvm_vcpu_apicv_active(&svm->vcpu))
11291149
avic_init_vmcb(svm);
11301150

@@ -2729,6 +2749,33 @@ static int mwait_interception(struct vcpu_svm *svm)
27292749
return nop_interception(svm);
27302750
}
27312751

2752+
static int invpcid_interception(struct vcpu_svm *svm)
2753+
{
2754+
struct kvm_vcpu *vcpu = &svm->vcpu;
2755+
unsigned long type;
2756+
gva_t gva;
2757+
2758+
if (!guest_cpuid_has(vcpu, X86_FEATURE_INVPCID)) {
2759+
kvm_queue_exception(vcpu, UD_VECTOR);
2760+
return 1;
2761+
}
2762+
2763+
/*
2764+
* For an INVPCID intercept:
2765+
* EXITINFO1 provides the linear address of the memory operand.
2766+
* EXITINFO2 provides the contents of the register operand.
2767+
*/
2768+
type = svm->vmcb->control.exit_info_2;
2769+
gva = svm->vmcb->control.exit_info_1;
2770+
2771+
if (type > 3) {
2772+
kvm_inject_gp(vcpu, 0);
2773+
return 1;
2774+
}
2775+
2776+
return kvm_handle_invpcid(vcpu, type, gva);
2777+
}
2778+
27322779
static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
27332780
[SVM_EXIT_READ_CR0] = cr_interception,
27342781
[SVM_EXIT_READ_CR3] = cr_interception,
@@ -2791,6 +2838,7 @@ static int (*const svm_exit_handlers[])(struct vcpu_svm *svm) = {
27912838
[SVM_EXIT_MWAIT] = mwait_interception,
27922839
[SVM_EXIT_XSETBV] = xsetbv_interception,
27932840
[SVM_EXIT_RDPRU] = rdpru_interception,
2841+
[SVM_EXIT_INVPCID] = invpcid_interception,
27942842
[SVM_EXIT_NPF] = npf_interception,
27952843
[SVM_EXIT_RSM] = rsm_interception,
27962844
[SVM_EXIT_AVIC_INCOMPLETE_IPI] = avic_incomplete_ipi_interception,
@@ -3619,6 +3667,9 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu)
36193667
svm->nrips_enabled = kvm_cpu_cap_has(X86_FEATURE_NRIPS) &&
36203668
guest_cpuid_has(&svm->vcpu, X86_FEATURE_NRIPS);
36213669

3670+
/* Check again if INVPCID interception if required */
3671+
svm_check_invpcid(svm);
3672+
36223673
if (!kvm_vcpu_apicv_active(vcpu))
36233674
return;
36243675

0 commit comments

Comments
 (0)