From: Sandipan Das Extend hardware-switched mediated PMU support to SEV-ES and SEV-SNP guests. As the guest PMU state resides in the encrypted VMSA, PMC virtualization has to be negotiated with the guest through a new SEV feature bit, SVM_SEV_FEAT_PERFCTR_VIRT. Don't advertise it yet: a guest with PMC virtualization programs its event-selects directly and would bypass the PMU event filter. The requested VMSA features are only known once the guest has been initialized, so set the KVM_MEDIATED_PMU_CAP_HW_SWITCHED capability in the guest init path, gated on the feature bit. Once the VMSA is encrypted, KVM can no longer access the guest PMU state. Hence: * Don't load or save the guest PMU state in the VMCB, it lives in the VMSA. * Do not intercept RDPMC, as it cannot be emulated after the VMSA is encrypted. Signed-off-by: Sandipan Das [Manali: Keep PMC filtering off and PERFCTR_VIRT unadvertised until KVM can filter the guest's events, don't WARN on a protected vendor state] Signed-off-by: Manali Shukla --- arch/x86/include/asm/svm.h | 1 + arch/x86/kvm/svm/pmu.c | 8 ++++++++ arch/x86/kvm/svm/sev.c | 28 ++++++++++++++++++++++++++-- arch/x86/kvm/svm/svm.c | 3 +++ arch/x86/kvm/svm/svm.h | 1 + 5 files changed, 39 insertions(+), 2 deletions(-) diff --git a/arch/x86/include/asm/svm.h b/arch/x86/include/asm/svm.h index 6272ebc955d32..0e4a01e36e54a 100644 --- a/arch/x86/include/asm/svm.h +++ b/arch/x86/include/asm/svm.h @@ -316,6 +316,7 @@ static_assert((X2AVIC_4K_MAX_PHYSICAL_ID & AVIC_PHYSICAL_MAX_INDEX_MASK) == X2AV #define SVM_SEV_FEAT_ALTERNATE_INJECTION BIT(4) #define SVM_SEV_FEAT_DEBUG_SWAP BIT(5) #define SVM_SEV_FEAT_SECURE_TSC BIT(9) +#define SVM_SEV_FEAT_PERFCTR_VIRT BIT(11) #define VMCB_ALLOWED_SEV_FEATURES_VALID BIT_ULL(63) diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c index 52da247e23050..3ef05b651de21 100644 --- a/arch/x86/kvm/svm/pmu.c +++ b/arch/x86/kvm/svm/pmu.c @@ -198,6 +198,10 @@ static int amd_get_vendor_state(struct kvm_vcpu *vcpu, u32 msr) struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); struct kvm_pmc *pmc; + /* A protected guest's PMU state is in the VMSA, there's nothing to do. */ + if (vcpu->arch.guest_state_protected) + return 0; + /* MSR_PERF_CNTR_GLOBAL_* */ switch (msr) { case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS: @@ -233,6 +237,10 @@ static int amd_set_vendor_state(struct kvm_vcpu *vcpu, u32 msr) struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); struct kvm_pmc *pmc; + /* A protected guest's PMU state is in the VMSA, there's nothing to do. */ + if (vcpu->arch.guest_state_protected) + return 0; + /* MSR_PERF_CNTR_GLOBAL_* */ switch (msr) { case MSR_AMD64_PERF_CNTR_GLOBAL_STATUS: diff --git a/arch/x86/kvm/svm/sev.c b/arch/x86/kvm/svm/sev.c index 28bc15def6a2c..1cf2260423573 100644 --- a/arch/x86/kvm/svm/sev.c +++ b/arch/x86/kvm/svm/sev.c @@ -212,6 +212,14 @@ static bool snp_is_secure_tsc_enabled(struct kvm *kvm) !WARN_ON_ONCE(!sev_snp_guest(kvm)); } +static bool sev_es_is_perfctr_virt_enabled(struct kvm *kvm) +{ + struct kvm_sev_info *sev = to_kvm_sev_info(kvm); + + return (sev->vmsa_features & SVM_SEV_FEAT_PERFCTR_VIRT) && + !WARN_ON_ONCE(!sev_es_guest(kvm)); +} + /* Must be called with the sev_bitmap_lock held */ static bool __sev_recycle_asids(unsigned int min_asid, unsigned int max_asid) { @@ -534,9 +542,11 @@ static int __sev_guest_init(struct kvm *kvm, struct kvm_sev_cmd *argp, if (snp_active) sev->vmsa_features |= SVM_SEV_FEAT_SNP_ACTIVE; + if (es_active && !sev_es_is_perfctr_virt_enabled(kvm)) + kvm_clr_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED); + if (es_active) - kvm_clr_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_SWITCHED | - KVM_MEDIATED_PMU_CAP_HW_FILTERED); + kvm_clr_mediated_pmu_caps(kvm, KVM_MEDIATED_PMU_CAP_HW_FILTERED); ret = sev_asid_new(sev, vm_type); if (ret) @@ -4744,6 +4754,20 @@ void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu) !guest_cpuid_has(vcpu, X86_FEATURE_XSAVES)); } +void sev_es_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) +{ + if (!kvm_vcpu_has_mediated_pmu_caps(vcpu, KVM_MEDIATED_PMU_CAP_HW_SWITCHED)) + return; + + /* + * RDPMC cannot be emulated after the VMSA is encrypted. If a guest has + * fewer counters than the host, the extra counters will continue being + * readable via RDPMC (no #GP) but they will remain in the init state. + * Access to the extra PERF_CTLs and PERF_CTRs remains intercepted. + */ + svm_clr_intercept(to_svm(vcpu), INTERCEPT_RDPMC); +} + void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm) { struct kvm_vcpu *vcpu = &svm->vcpu; diff --git a/arch/x86/kvm/svm/svm.c b/arch/x86/kvm/svm/svm.c index 4d48286faeb50..ac8f06e1f5a10 100644 --- a/arch/x86/kvm/svm/svm.c +++ b/arch/x86/kvm/svm/svm.c @@ -1148,6 +1148,9 @@ static void svm_recalc_instruction_intercepts(struct kvm_vcpu *vcpu) svm_set_intercept(svm, INTERCEPT_RDPMC); else svm_clr_intercept(svm, INTERCEPT_RDPMC); + + if (is_sev_es_guest(vcpu)) + sev_es_recalc_instruction_intercepts(vcpu); } static void svm_recalc_intercepts(struct kvm_vcpu *vcpu) diff --git a/arch/x86/kvm/svm/svm.h b/arch/x86/kvm/svm/svm.h index 3a5e9e4ecaa90..1f3cd12e44c46 100644 --- a/arch/x86/kvm/svm/svm.h +++ b/arch/x86/kvm/svm/svm.h @@ -1001,6 +1001,7 @@ void sev_init_vmcb(struct vcpu_svm *svm, bool init_event); void sev_vcpu_after_set_cpuid(struct vcpu_svm *svm); int sev_es_string_io(struct vcpu_svm *svm, int size, unsigned int port, int in); void sev_es_recalc_msr_intercepts(struct kvm_vcpu *vcpu); +void sev_es_recalc_instruction_intercepts(struct kvm_vcpu *vcpu); void sev_vcpu_deliver_sipi_vector(struct kvm_vcpu *vcpu, u8 vector); void sev_es_prepare_switch_to_guest(struct vcpu_svm *svm, struct sev_es_save_area *hostsa); void sev_es_unmap_ghcb(struct vcpu_svm *svm); -- 2.43.0