[PATCH v2] KVM: SVM: emulate MSR_IA32_PERF_CAPABILITIES

From: Vitaly Kuznetsov
Date: Thu Jun 18 2020 - 07:13:49 EST


state_test/smm_test selftests are failing on AMD with:
"Unexpected result from KVM_GET_MSRS, r: 51 (failed MSR was 0x345)"

MSR_IA32_PERF_CAPABILITIES is an emulated MSR on Intel but it is not
known to AMD code, emulate it there too (by returning 0 and allowing
userspace to write 0). This way the code is better prepared to the
eventual appearance of the feature in AMD hardware.

Fixes: 27461da31089 ("KVM: x86/pmu: Support full width counting")
Suggested-by: Jim Mattson <jmattson@xxxxxxxxxx>
Suggested-by: Paolo Bonzini <pbonzini@xxxxxxxxxx>
Signed-off-by: Vitaly Kuznetsov <vkuznets@xxxxxxxxxx>
---
arch/x86/kvm/svm/pmu.c | 29 ++++++++++++++++++++++++++++-
1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index 035da07500e8..f13ee3cd6d0f 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -200,7 +200,13 @@ static struct kvm_pmc *amd_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,

static bool amd_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
{
- /* All MSRs refer to exactly one PMC, so msr_idx_to_pmc is enough. */
+ switch (msr) {
+ case MSR_IA32_PERF_CAPABILITIES:
+ return true;
+ default:
+ break;
+ }
+
return false;
}

@@ -221,6 +227,14 @@ static int amd_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
struct kvm_pmc *pmc;
u32 msr = msr_info->index;

+ if (msr == MSR_IA32_PERF_CAPABILITIES) {
+ if (!msr_info->host_initiated &&
+ !guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
+ return 1;
+ msr_info->data = vcpu->arch.perf_capabilities;
+ return 0;
+ }
+
/* MSR_PERFCTRn */
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
if (pmc) {
@@ -244,6 +258,18 @@ static int amd_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
u32 msr = msr_info->index;
u64 data = msr_info->data;

+ if (msr == MSR_IA32_PERF_CAPABILITIES) {
+ if (!msr_info->host_initiated)
+ return 1;
+
+ /* No feature bits are currently supported */
+ if (data)
+ return 1;
+
+ vcpu->arch.perf_capabilities = data;
+ return 0;
+ }
+
/* MSR_PERFCTRn */
pmc = get_gp_pmc_amd(pmu, msr, PMU_TYPE_COUNTER);
if (pmc) {
@@ -281,6 +307,7 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
pmu->nr_arch_fixed_counters = 0;
pmu->global_status = 0;
bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
+ vcpu->arch.perf_capabilities = 0;
}

static void amd_pmu_init(struct kvm_vcpu *vcpu)
--
2.25.4