Skip to content

Commit 75430c4

Browse files
Dapeng Misean-jc
authored andcommitted
KVM: x86/pmu: Manipulate FIXED_CTR_CTRL MSR with macros
Magic numbers are used to manipulate the bit fields of FIXED_CTR_CTRL MSR. This makes reading code become difficult, so use pre-defined macros to replace these magic numbers. Signed-off-by: Dapeng Mi <[email protected]> Link: https://lore.kernel.org/r/[email protected] [sean: drop unnecessary curly braces] Signed-off-by: Sean Christopherson <[email protected]>
1 parent 0e102ce commit 75430c4

File tree

3 files changed

+17
-11
lines changed

3 files changed

+17
-11
lines changed

arch/x86/kvm/pmu.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -469,11 +469,11 @@ static int reprogram_counter(struct kvm_pmc *pmc)
469469
if (pmc_is_fixed(pmc)) {
470470
fixed_ctr_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl,
471471
pmc->idx - KVM_FIXED_PMC_BASE_IDX);
472-
if (fixed_ctr_ctrl & 0x1)
472+
if (fixed_ctr_ctrl & INTEL_FIXED_0_KERNEL)
473473
eventsel |= ARCH_PERFMON_EVENTSEL_OS;
474-
if (fixed_ctr_ctrl & 0x2)
474+
if (fixed_ctr_ctrl & INTEL_FIXED_0_USER)
475475
eventsel |= ARCH_PERFMON_EVENTSEL_USR;
476-
if (fixed_ctr_ctrl & 0x8)
476+
if (fixed_ctr_ctrl & INTEL_FIXED_0_ENABLE_PMI)
477477
eventsel |= ARCH_PERFMON_EVENTSEL_INT;
478478
new_config = (u64)fixed_ctr_ctrl;
479479
}
@@ -846,8 +846,8 @@ static inline bool cpl_is_matched(struct kvm_pmc *pmc)
846846
} else {
847847
config = fixed_ctrl_field(pmc_to_pmu(pmc)->fixed_ctr_ctrl,
848848
pmc->idx - KVM_FIXED_PMC_BASE_IDX);
849-
select_os = config & 0x1;
850-
select_user = config & 0x2;
849+
select_os = config & INTEL_FIXED_0_KERNEL;
850+
select_user = config & INTEL_FIXED_0_USER;
851851
}
852852

853853
/*

arch/x86/kvm/pmu.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
MSR_IA32_MISC_ENABLE_BTS_UNAVAIL)
1515

1616
/* retrieve the 4 bits for EN and PMI out of IA32_FIXED_CTR_CTRL */
17-
#define fixed_ctrl_field(ctrl_reg, idx) (((ctrl_reg) >> ((idx)*4)) & 0xf)
17+
#define fixed_ctrl_field(ctrl_reg, idx) \
18+
(((ctrl_reg) >> ((idx) * INTEL_FIXED_BITS_STRIDE)) & INTEL_FIXED_BITS_MASK)
1819

1920
#define VMWARE_BACKDOOR_PMC_HOST_TSC 0x10000
2021
#define VMWARE_BACKDOOR_PMC_REAL_TIME 0x10001
@@ -170,7 +171,8 @@ static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)
170171

171172
if (pmc_is_fixed(pmc))
172173
return fixed_ctrl_field(pmu->fixed_ctr_ctrl,
173-
pmc->idx - KVM_FIXED_PMC_BASE_IDX) & 0x3;
174+
pmc->idx - KVM_FIXED_PMC_BASE_IDX) &
175+
(INTEL_FIXED_0_KERNEL | INTEL_FIXED_0_USER);
174176

175177
return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE;
176178
}

arch/x86/kvm/vmx/pmu_intel.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -502,7 +502,12 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
502502
}
503503

504504
for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
505-
pmu->fixed_ctr_ctrl_rsvd &= ~(0xbull << (i * 4));
505+
pmu->fixed_ctr_ctrl_rsvd &=
506+
~intel_fixed_bits_by_idx(i,
507+
INTEL_FIXED_0_KERNEL |
508+
INTEL_FIXED_0_USER |
509+
INTEL_FIXED_0_ENABLE_PMI);
510+
506511
counter_rsvd = ~(((1ull << pmu->nr_arch_gp_counters) - 1) |
507512
(((1ull << pmu->nr_arch_fixed_counters) - 1) << KVM_FIXED_PMC_BASE_IDX));
508513
pmu->global_ctrl_rsvd = counter_rsvd;
@@ -546,10 +551,9 @@ static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
546551
if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
547552
pmu->pebs_enable_rsvd = counter_rsvd;
548553
pmu->reserved_bits &= ~ICL_EVENTSEL_ADAPTIVE;
549-
for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
554+
for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
550555
pmu->fixed_ctr_ctrl_rsvd &=
551-
~(1ULL << (KVM_FIXED_PMC_BASE_IDX + i * 4));
552-
}
556+
~intel_fixed_bits_by_idx(i, ICL_FIXED_0_ADAPTIVE);
553557
pmu->pebs_data_cfg_rsvd = ~0xff00000full;
554558
} else {
555559
pmu->pebs_enable_rsvd =

0 commit comments

Comments
 (0)