From: Santosh Shukla IBS virtualization (VIBS) allows a guest to collect Instruction-Based Sampling (IBS) data using hardware-assisted virtualization. With VIBS enabled, the hardware automatically saves and restores guest IBS state during VM-Entry and VM-Exit via the VMCB State Save Area. IBS-generated interrupts are delivered directly to the guest without causing a VMEXIT. VIBS depends on mediated PMU mode and requires either AVIC or NMI virtualization for interrupt delivery. However, since AVIC can be dynamically inhibited, VIBS requires VNMI to be enabled to ensure reliable interrupt delivery. If AVIC is inhibited and VNMI is disabled, the guest can encounter a VMEXIT_INVALID when IBS virtualization is enabled for the guest. Because IBS state is classified as swap type C, the hypervisor must save its own IBS state before VMRUN and restore it after VMEXIT. It must also disable IBS before VMRUN and re-enable it afterward. This will be handled using mediated PMU support in subsequent patches by enabling mediated PMU capability for IBS PMUs. More details about IBS virtualization can be found at [1]. [1]: https://bugzilla.kernel.org/attachment.cgi?id=306250 AMD64 Architecture Programmer’s Manual, Vol 2, Section 15.38 Instruction-Based Sampling Virtualization. Signed-off-by: Santosh Shukla Co-developed-by: Manali Shukla Signed-off-by: Manali Shukla --- arch/x86/include/asm/svm.h | 2 ++ arch/x86/kvm/svm/svm.c | 73 +++++++++++++++++++++++++++++++++++++- 2 files changed, 74 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 4296efc1dafe..17aa6bf76bce 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -226,6 +226,8 @@ struct __attribute__ ((__packed__)) vmcb_control_area { #define SVM_INT_VECTOR_MASK GENMASK(7, 0) +#define SVM_MISC_ENABLE_V_IBS BIT_ULL(2) + #define SVM_INTERRUPT_SHADOW_MASK BIT_ULL(0) #define SVM_GUEST_INTERRUPT_MASK BIT_ULL(1) diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 5af3479cd264..421a929398da 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -161,12 +161,15 @@ module_param(lbrv, int, 0444); static int __ro_after_init tsc_scaling = true; module_param(tsc_scaling, int, 0444); +/* enable/disable IBS virtualization */ +static bool __ro_after_init vibs = true; +module_param(vibs, bool, 0444); + module_param(enable_device_posted_irqs, bool, 0444); bool __read_mostly dump_invalid_vmcb; module_param(dump_invalid_vmcb, bool, 0644); - bool __ro_after_init intercept_smi = true; module_param(intercept_smi, bool, 0444); @@ -779,6 +782,26 @@ static void svm_recalc_pmu_msr_intercepts(struct kvm_vcpu *vcpu) MSR_TYPE_RW, intercept); } +static void svm_recalc_ibs_msr_intercepts(struct kvm_vcpu *vcpu) +{ + bool intercept = !(guest_cpu_cap_has(vcpu, X86_FEATURE_IBS) && + kvm_vcpu_has_mediated_pmu(vcpu)); + + if (!enable_mediated_pmu || !vibs) + return; + + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSFETCHCTL, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSFETCHLINAD, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSOPCTL, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSOPRIP, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSOPDATA, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSOPDATA2, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSOPDATA3, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSDCLINAD, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_IBSBRTARGET, MSR_TYPE_RW, intercept); + svm_set_intercept_for_msr(vcpu, MSR_AMD64_ICIBSEXTDCTL, MSR_TYPE_RW, intercept); +} + static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu) { struct vcpu_svm *svm = to_svm(vcpu); @@ -848,6 +871,7 @@ static void svm_recalc_msr_intercepts(struct kvm_vcpu *vcpu) sev_es_recalc_msr_intercepts(vcpu); svm_recalc_pmu_msr_intercepts(vcpu); + svm_recalc_ibs_msr_intercepts(vcpu); /* * x2APIC intercepts are modified on-demand and cannot be filtered by @@ -2880,6 +2904,27 @@ static int svm_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) case MSR_AMD64_DE_CFG: msr_info->data = svm->msr_decfg; break; + + case MSR_AMD64_IBSCTL: + if (guest_cpu_cap_has(vcpu, X86_FEATURE_IBS)) + msr_info->data = IBSCTL_LVT_OFFSET_VALID; + else + msr_info->data = 0; + break; + + + /* + * When IBS virtualization is enabled, guest reads from + * MSR_AMD64_IBSFETCHPHYSAD and MSR_AMD64_IBSDCPHYSAD must return 0. + * This is done for security reasons, as guests should not be allowed to + * access or infer any information about the system's physical + * addresses. + */ + case MSR_AMD64_IBSDCPHYSAD: + case MSR_AMD64_IBSFETCHPHYSAD: + msr_info->data = 0; + break; + default: return kvm_get_msr_common(vcpu, msr_info); } @@ -3171,6 +3216,16 @@ static int svm_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr) svm->msr_decfg = data; break; } + /* + * When IBS virtualization is enabled, guest writes to + * MSR_AMD64_IBSFETCHPHYSAD and MSR_AMD64_IBSDCPHYSAD must be ignored. + * This is done for security reasons, as guests should not be allowed to + * access or infer any information about the system's physical + * addresses. + */ + case MSR_AMD64_IBSDCPHYSAD: + case MSR_AMD64_IBSFETCHPHYSAD: + return 1; default: return kvm_set_msr_common(vcpu, msr); } @@ -4678,6 +4733,11 @@ static void svm_vcpu_after_set_cpuid(struct kvm_vcpu *vcpu) if (guest_cpuid_is_intel_compatible(vcpu)) guest_cpu_cap_clear(vcpu, X86_FEATURE_V_VMSAVE_VMLOAD); + if (guest_cpu_cap_has(vcpu, X86_FEATURE_IBS)) + svm->vmcb->control.misc_ctl2 |= SVM_MISC_ENABLE_V_IBS; + else + svm->vmcb->control.misc_ctl2 &= ~SVM_MISC_ENABLE_V_IBS; + if (sev_guest(vcpu->kvm)) sev_vcpu_after_set_cpuid(svm); } @@ -5510,6 +5570,11 @@ static __init void svm_set_cpu_caps(void) if (cpu_feature_enabled(X86_FEATURE_EXTAPIC)) kvm_caps.has_extapic = true; + if (vibs) + kvm_cpu_cap_check_and_set(X86_FEATURE_IBS); + else + kvm_cpu_caps[CPUID_8000_001B_EAX] = 0; + /* CPUID 0x80000008 */ if (boot_cpu_has(X86_FEATURE_LS_CFG_SSBD) || boot_cpu_has(X86_FEATURE_AMD_SSBD)) @@ -5698,6 +5763,12 @@ static __init int svm_hardware_setup(void) svm_x86_ops.set_vnmi_pending = NULL; } + vibs = enable_mediated_pmu && vnmi && vibs + && boot_cpu_has(X86_FEATURE_VIBS); + + if (vibs) + pr_info("IBS virtualization supported\n"); + if (!enable_pmu) pr_info("PMU virtualization is disabled\n"); -- 2.43.0