[PATCH v2 28/54] KVM: x86/pmu: Add counter MSR and selector MSR index into struct kvm_pmc

Mingwei Zhang
2024-05-06T05:31:21Z

Add the MSR indices for both selector and counter in each kvm_pmc. Giving
convenience to mediated passthrough vPMU in scenarios of querying MSR from
a given pmc. Note that legacy vPMU does not need this because it never
directly accesses PMU MSRs, instead each kvm_pmc is bound to a perf_event.

Signed-off-by: Mingwei Zhang <mizhang@google.com>
---
arch/x86/include/asm/kvm_host.h | 2 ++
arch/x86/kvm/svm/pmu.c | 13 +++++++++++++
arch/x86/kvm/vmx/pmu_intel.c | 13 +++++++++++++
3 files changed, 28 insertions(+)

diff --git a/arch/x86/include/asm/kvm_host.h b/arch/x86/include/asm/kvm_host.h
index 19b924c3bd85..8b4ea9bdcc74 100644
--- a/arch/x86/include/asm/kvm_host.h
+++ b/arch/x86/include/asm/kvm_host.h
@@ -519,6 +519,8 @@ struct kvm_pmc {
*/
u64 emulated_counter;
u64 eventsel;
+ u64 msr_counter;
+ u64 msr_eventsel;
struct perf_event *perf_event;
struct kvm_vcpu *vcpu;
/*
diff --git a/arch/x86/kvm/svm/pmu.c b/arch/x86/kvm/svm/pmu.c
index 6b471b1ec9b8..447657513729 100644
--- a/arch/x86/kvm/svm/pmu.c
+++ b/arch/x86/kvm/svm/pmu.c
@@ -177,6 +177,7 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
{
struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
union cpuid_0x80000022_ebx ebx;
+ int i;

pmu->version = 1;
if (guest_cpuid_has(vcpu, X86_FEATURE_PERFMON_V2)) {
@@ -210,6 +211,18 @@ static void amd_pmu_refresh(struct kvm_vcpu *vcpu)
pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
pmu->nr_arch_fixed_counters = 0;
bitmap_set(pmu->all_valid_pmc_idx, 0, pmu->nr_arch_gp_counters);
+
+ if (guest_cpuid_has(vcpu, X86_FEATURE_PERFCTR_CORE)) {
+ for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ pmu->gp_counters[i].msr_eventsel = MSR_F15H_PERF_CTL0 + 2*i;
+ pmu->gp_counters[i].msr_counter = MSR_F15H_PERF_CTR0 + 2*i;
+ }
+ } else {
+ for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ pmu->gp_counters[i].msr_eventsel = MSR_K7_EVNTSEL0 + i;
+ pmu->gp_counters[i].msr_counter = MSR_K7_PERFCTR0 + i;
+ }
+ }
}

static void amd_pmu_init(struct kvm_vcpu *vcpu)
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 8e8d1f2aa5e5..7852ba25a240 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -562,6 +562,19 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
~((1ull << pmu->nr_arch_gp_counters) - 1);
}
}
+
+ for (i = 0; i < pmu->nr_arch_gp_counters; i++) {
+ pmu->gp_counters[i].msr_eventsel = MSR_P6_EVNTSEL0 + i;
+ if (fw_writes_is_enabled(vcpu))
+ pmu->gp_counters[i].msr_counter = MSR_IA32_PMC0 + i;
+ else
+ pmu->gp_counters[i].msr_counter = MSR_IA32_PERFCTR0 + i;
+ }
+
+ for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
+ pmu->fixed_counters[i].msr_eventsel = MSR_CORE_PERF_FIXED_CTR_CTRL;
+ pmu->fixed_counters[i].msr_counter = MSR_CORE_PERF_FIXED_CTR0 + i;
+ }
}

static void intel_pmu_init(struct kvm_vcpu *vcpu)
--
2.45.0.rc1.225.g2a3ae87e7f-goog