1 From fd822f8bf0f622639f35cb923e0bac6e43684c5e Mon Sep 17 00:00:00 2001
2 From: Paolo Bonzini <pbonzini@redhat.com>
3 Date: Mon, 20 May 2019 17:20:40 +0200
4 Subject: KVM: x86/pmu: mask the result of rdpmc according to the width of the
7 [ Upstream commit 0e6f467ee28ec97f68c7b74e35ec1601bb1368a7 ]
9 This patch will simplify the changes in the next, by enforcing the
10 masking of the counters to RDPMC and RDMSR.
12 Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
13 Signed-off-by: Sasha Levin <sashal@kernel.org>
15 arch/x86/kvm/pmu.c | 10 +++-------
16 arch/x86/kvm/pmu.h | 3 ++-
17 arch/x86/kvm/pmu_amd.c | 2 +-
18 arch/x86/kvm/vmx/pmu_intel.c | 13 +++++++++----
19 4 files changed, 15 insertions(+), 13 deletions(-)
21 diff --git a/arch/x86/kvm/pmu.c b/arch/x86/kvm/pmu.c
22 index e39741997893..dd745b58ffd8 100644
23 --- a/arch/x86/kvm/pmu.c
24 +++ b/arch/x86/kvm/pmu.c
25 @@ -283,7 +283,7 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
26 bool fast_mode = idx & (1u << 31);
27 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
30 + u64 mask = fast_mode ? ~0u : ~0ull;
34 @@ -291,15 +291,11 @@ int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
35 if (is_vmware_backdoor_pmc(idx))
36 return kvm_pmu_rdpmc_vmware(vcpu, idx, data);
38 - pmc = kvm_x86_ops->pmu_ops->msr_idx_to_pmc(vcpu, idx);
39 + pmc = kvm_x86_ops->pmu_ops->msr_idx_to_pmc(vcpu, idx, &mask);
43 - ctr_val = pmc_read_counter(pmc);
45 - ctr_val = (u32)ctr_val;
48 + *data = pmc_read_counter(pmc) & mask;
52 diff --git a/arch/x86/kvm/pmu.h b/arch/x86/kvm/pmu.h
53 index ba8898e1a854..22dff661145a 100644
54 --- a/arch/x86/kvm/pmu.h
55 +++ b/arch/x86/kvm/pmu.h
56 @@ -25,7 +25,8 @@ struct kvm_pmu_ops {
57 unsigned (*find_fixed_event)(int idx);
58 bool (*pmc_is_enabled)(struct kvm_pmc *pmc);
59 struct kvm_pmc *(*pmc_idx_to_pmc)(struct kvm_pmu *pmu, int pmc_idx);
60 - struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, unsigned idx);
61 + struct kvm_pmc *(*msr_idx_to_pmc)(struct kvm_vcpu *vcpu, unsigned idx,
63 int (*is_valid_msr_idx)(struct kvm_vcpu *vcpu, unsigned idx);
64 bool (*is_valid_msr)(struct kvm_vcpu *vcpu, u32 msr);
65 int (*get_msr)(struct kvm_vcpu *vcpu, u32 msr, u64 *data);
66 diff --git a/arch/x86/kvm/pmu_amd.c b/arch/x86/kvm/pmu_amd.c
67 index 50fa9450fcf1..d3118088f1cd 100644
68 --- a/arch/x86/kvm/pmu_amd.c
69 +++ b/arch/x86/kvm/pmu_amd.c
70 @@ -186,7 +186,7 @@ static int amd_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx)
73 /* idx is the ECX register of RDPMC instruction */
74 -static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, unsigned idx)
75 +static struct kvm_pmc *amd_msr_idx_to_pmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *mask)
77 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
78 struct kvm_pmc *counters;
79 diff --git a/arch/x86/kvm/vmx/pmu_intel.c b/arch/x86/kvm/vmx/pmu_intel.c
80 index 5ab4a364348e..ad7ea81fbfbf 100644
81 --- a/arch/x86/kvm/vmx/pmu_intel.c
82 +++ b/arch/x86/kvm/vmx/pmu_intel.c
83 @@ -126,7 +126,7 @@ static int intel_is_valid_msr_idx(struct kvm_vcpu *vcpu, unsigned idx)
86 static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
88 + unsigned idx, u64 *mask)
90 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
91 bool fixed = idx & (1u << 30);
92 @@ -138,6 +138,7 @@ static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu,
93 if (fixed && idx >= pmu->nr_arch_fixed_counters)
95 counters = fixed ? pmu->fixed_counters : pmu->gp_counters;
96 + *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
98 return &counters[idx];
100 @@ -183,9 +184,13 @@ static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, u32 msr, u64 *data)
101 *data = pmu->global_ovf_ctrl;
104 - if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
105 - (pmc = get_fixed_pmc(pmu, msr))) {
106 - *data = pmc_read_counter(pmc);
107 + if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0))) {
108 + u64 val = pmc_read_counter(pmc);
109 + *data = val & pmu->counter_bitmask[KVM_PMC_GP];
111 + } else if ((pmc = get_fixed_pmc(pmu, msr))) {
112 + u64 val = pmc_read_counter(pmc);
113 + *data = val & pmu->counter_bitmask[KVM_PMC_FIXED];
115 } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
116 *data = pmc->eventsel;