[PATCH v4 16/21] KVM: x86/pmu: Populate CPUID.0AH:ECX fixed-counter bitmap
From: Zide Chen
Date: Thu Nov 12 2026 - 18:53:15 EST
PerfMon v5 adds a fixed-counter support bitmap in CPUID.0AH:ECX.
This is a superset of EDX[4:0] which indicates the contiguous
counters starting from 0. For backward compatibility, it's recommended
to use the following to determine if a fixed counter is supported:
FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i);
Populate CPUID.0AH:ECX with kvm_pmu_cap.fixed_cntr_mask64 when KVM
supports PerfMon v5, and in intel_pmu_refresh(), incorporate the
guest's ECX bits into fixed_mask for PerfMon v5+ guests.
Originally-by: Yang Weijiang <weijiang.yang@xxxxxxxxx>
Signed-off-by: Zide Chen <zide.chen@xxxxxxxxx>
---
v4:
- Check pmu->version directly instead of checking both kvm_pmu_cap and
pmu version, as pmu->version is now clamped. (Yosry, Sean)
- Merge the blanket masking into the fixed_mask assignment. (Yosry)
v3:
- Remove '&' when populating entry->ecx. (Jim)
---
arch/x86/kvm/cpuid.c | 5 ++++-
arch/x86/kvm/vmx/pmu_intel.c | 3 +++
2 files changed, 7 insertions(+), 1 deletion(-)
diff --git a/arch/x86/kvm/cpuid.c b/arch/x86/kvm/cpuid.c
index 427871ec365f..2a4530d000fc 100644
--- a/arch/x86/kvm/cpuid.c
+++ b/arch/x86/kvm/cpuid.c
@@ -1554,7 +1554,10 @@ static inline int __do_cpuid_func(struct kvm_cpuid_array *array, u32 function)
entry->eax = eax.full;
entry->ebx = kvm_pmu_cap.events_mask;
- entry->ecx = 0;
+ if (kvm_pmu_cap.version >= 5)
+ entry->ecx = (u32)kvm_pmu_cap.fixed_cntr_mask64;
+ else
+ entry->ecx = 0;
entry->edx = edx.full;
break;
}
diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
index 0be9956810ae..d259b66abac8 100644
--- a/arch/x86/kvm/vmx/pmu_intel.c
+++ b/arch/x86/kvm/vmx/pmu_intel.c
@@ -593,7 +593,10 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
find_first_zero_bit(kvm_pmu_cap.fixed_cntr_mask,
KVM_MAX_NR_FIXED_COUNTERS));
+ /* FxCtr[i]_is_supported := ECX[i] || (EDX[4:0] > i) */
fixed_mask = BIT_ULL(nr_fixed_counters) - 1;
+ if (pmu->version >= 5)
+ fixed_mask |= (entry->ecx & kvm_pmu_cap.fixed_cntr_mask64);
/*
* CPUID.0AH:EAX[15:8] can only enumerate contiguous counters. Clamp the
--
2.56.1