Thomas Gleixner | 20c8ccb | 2019-06-04 10:11:32 +0200 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 2 | /* |
| 3 | * KVM PMU support for Intel CPUs |
| 4 | * |
| 5 | * Copyright 2011 Red Hat, Inc. and/or its affiliates. |
| 6 | * |
| 7 | * Authors: |
| 8 | * Avi Kivity <avi@redhat.com> |
| 9 | * Gleb Natapov <gleb@redhat.com> |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 10 | */ |
| 11 | #include <linux/types.h> |
| 12 | #include <linux/kvm_host.h> |
| 13 | #include <linux/perf_event.h> |
| 14 | #include <asm/perf_event.h> |
| 15 | #include "x86.h" |
| 16 | #include "cpuid.h" |
| 17 | #include "lapic.h" |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 18 | #include "nested.h" |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 19 | #include "pmu.h" |
| 20 | |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 21 | #define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0) |
| 22 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 23 | static struct kvm_event_hw_type_mapping intel_arch_events[] = { |
| 24 | /* Index must match CPUID 0x0A.EBX bit vector */ |
| 25 | [0] = { 0x3c, 0x00, PERF_COUNT_HW_CPU_CYCLES }, |
| 26 | [1] = { 0xc0, 0x00, PERF_COUNT_HW_INSTRUCTIONS }, |
| 27 | [2] = { 0x3c, 0x01, PERF_COUNT_HW_BUS_CYCLES }, |
| 28 | [3] = { 0x2e, 0x4f, PERF_COUNT_HW_CACHE_REFERENCES }, |
| 29 | [4] = { 0x2e, 0x41, PERF_COUNT_HW_CACHE_MISSES }, |
| 30 | [5] = { 0xc4, 0x00, PERF_COUNT_HW_BRANCH_INSTRUCTIONS }, |
| 31 | [6] = { 0xc5, 0x00, PERF_COUNT_HW_BRANCH_MISSES }, |
Like Xu | 98dd2f1 | 2020-12-30 16:19:16 +0800 | [diff] [blame] | 32 | [7] = { 0x00, 0x03, PERF_COUNT_HW_REF_CPU_CYCLES }, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 33 | }; |
| 34 | |
| 35 | /* mapping between fixed pmc index and intel_arch_events array */ |
| 36 | static int fixed_pmc_events[] = {1, 0, 7}; |
| 37 | |
| 38 | static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data) |
| 39 | { |
| 40 | int i; |
| 41 | |
| 42 | for (i = 0; i < pmu->nr_arch_fixed_counters; i++) { |
| 43 | u8 new_ctrl = fixed_ctrl_field(data, i); |
| 44 | u8 old_ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, i); |
| 45 | struct kvm_pmc *pmc; |
| 46 | |
| 47 | pmc = get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + i); |
| 48 | |
| 49 | if (old_ctrl == new_ctrl) |
| 50 | continue; |
| 51 | |
Like Xu | b35e554 | 2019-10-27 18:52:43 +0800 | [diff] [blame] | 52 | __set_bit(INTEL_PMC_IDX_FIXED + i, pmu->pmc_in_use); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 53 | reprogram_fixed_counter(pmc, new_ctrl, i); |
| 54 | } |
| 55 | |
| 56 | pmu->fixed_ctr_ctrl = data; |
| 57 | } |
| 58 | |
| 59 | /* function is called when global control register has been updated. */ |
| 60 | static void global_ctrl_changed(struct kvm_pmu *pmu, u64 data) |
| 61 | { |
| 62 | int bit; |
| 63 | u64 diff = pmu->global_ctrl ^ data; |
| 64 | |
| 65 | pmu->global_ctrl = data; |
| 66 | |
| 67 | for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX) |
| 68 | reprogram_counter(pmu, bit); |
| 69 | } |
| 70 | |
| 71 | static unsigned intel_find_arch_event(struct kvm_pmu *pmu, |
| 72 | u8 event_select, |
| 73 | u8 unit_mask) |
| 74 | { |
| 75 | int i; |
| 76 | |
| 77 | for (i = 0; i < ARRAY_SIZE(intel_arch_events); i++) |
| 78 | if (intel_arch_events[i].eventsel == event_select |
| 79 | && intel_arch_events[i].unit_mask == unit_mask |
| 80 | && (pmu->available_event_types & (1 << i))) |
| 81 | break; |
| 82 | |
| 83 | if (i == ARRAY_SIZE(intel_arch_events)) |
| 84 | return PERF_COUNT_HW_MAX; |
| 85 | |
| 86 | return intel_arch_events[i].event_type; |
| 87 | } |
| 88 | |
| 89 | static unsigned intel_find_fixed_event(int idx) |
| 90 | { |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 91 | u32 event; |
| 92 | size_t size = ARRAY_SIZE(fixed_pmc_events); |
| 93 | |
| 94 | if (idx >= size) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 95 | return PERF_COUNT_HW_MAX; |
| 96 | |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 97 | event = fixed_pmc_events[array_index_nospec(idx, size)]; |
| 98 | return intel_arch_events[event].event_type; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 99 | } |
| 100 | |
Andrea Gelmini | bb3541f | 2016-05-21 14:14:44 +0200 | [diff] [blame] | 101 | /* check if a PMC is enabled by comparing it with globl_ctrl bits. */ |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 102 | static bool intel_pmc_is_enabled(struct kvm_pmc *pmc) |
| 103 | { |
| 104 | struct kvm_pmu *pmu = pmc_to_pmu(pmc); |
| 105 | |
| 106 | return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl); |
| 107 | } |
| 108 | |
| 109 | static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx) |
| 110 | { |
| 111 | if (pmc_idx < INTEL_PMC_IDX_FIXED) |
| 112 | return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + pmc_idx, |
| 113 | MSR_P6_EVNTSEL0); |
| 114 | else { |
| 115 | u32 idx = pmc_idx - INTEL_PMC_IDX_FIXED; |
| 116 | |
| 117 | return get_fixed_pmc(pmu, idx + MSR_CORE_PERF_FIXED_CTR0); |
| 118 | } |
| 119 | } |
| 120 | |
Jim Mattson | e6cd31f | 2021-11-05 13:20:58 -0700 | [diff] [blame] | 121 | static bool intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 122 | { |
| 123 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 124 | bool fixed = idx & (1u << 30); |
| 125 | |
| 126 | idx &= ~(3u << 30); |
| 127 | |
Jim Mattson | e6cd31f | 2021-11-05 13:20:58 -0700 | [diff] [blame] | 128 | return fixed ? idx < pmu->nr_arch_fixed_counters |
| 129 | : idx < pmu->nr_arch_gp_counters; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 130 | } |
| 131 | |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 132 | static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu, |
| 133 | unsigned int idx, u64 *mask) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 134 | { |
| 135 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 136 | bool fixed = idx & (1u << 30); |
| 137 | struct kvm_pmc *counters; |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 138 | unsigned int num_counters; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 139 | |
| 140 | idx &= ~(3u << 30); |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 141 | if (fixed) { |
| 142 | counters = pmu->fixed_counters; |
| 143 | num_counters = pmu->nr_arch_fixed_counters; |
| 144 | } else { |
| 145 | counters = pmu->gp_counters; |
| 146 | num_counters = pmu->nr_arch_gp_counters; |
| 147 | } |
| 148 | if (idx >= num_counters) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 149 | return NULL; |
Paolo Bonzini | 0e6f467 | 2019-05-20 17:20:40 +0200 | [diff] [blame] | 150 | *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP]; |
Marios Pomonis | 6606174 | 2019-12-11 12:47:53 -0800 | [diff] [blame] | 151 | return &counters[array_index_nospec(idx, num_counters)]; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 152 | } |
| 153 | |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 154 | static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu) |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 155 | { |
| 156 | if (!guest_cpuid_has(vcpu, X86_FEATURE_PDCM)) |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 157 | return 0; |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 158 | |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 159 | return vcpu->arch.perf_capabilities; |
| 160 | } |
| 161 | |
| 162 | static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu) |
| 163 | { |
| 164 | return (vcpu_get_perf_capabilities(vcpu) & PMU_CAP_FW_WRITES) != 0; |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | static inline struct kvm_pmc *get_fw_gp_pmc(struct kvm_pmu *pmu, u32 msr) |
| 168 | { |
| 169 | if (!fw_writes_is_enabled(pmu_to_vcpu(pmu))) |
| 170 | return NULL; |
| 171 | |
| 172 | return get_gp_pmc(pmu, msr, MSR_IA32_PMC0); |
| 173 | } |
| 174 | |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame] | 175 | bool intel_pmu_lbr_is_compatible(struct kvm_vcpu *vcpu) |
| 176 | { |
| 177 | /* |
| 178 | * As a first step, a guest could only enable LBR feature if its |
| 179 | * cpu model is the same as the host because the LBR registers |
| 180 | * would be pass-through to the guest and they're model specific. |
| 181 | */ |
| 182 | return boot_cpu_data.x86_model == guest_cpuid_model(vcpu); |
| 183 | } |
| 184 | |
Like Xu | c646236 | 2021-02-01 13:10:31 +0800 | [diff] [blame] | 185 | bool intel_pmu_lbr_is_enabled(struct kvm_vcpu *vcpu) |
| 186 | { |
| 187 | struct x86_pmu_lbr *lbr = vcpu_to_lbr_records(vcpu); |
| 188 | |
| 189 | return lbr->nr && (vcpu_get_perf_capabilities(vcpu) & PMU_CAP_LBR_FMT); |
| 190 | } |
| 191 | |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 192 | static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index) |
| 193 | { |
| 194 | struct x86_pmu_lbr *records = vcpu_to_lbr_records(vcpu); |
| 195 | bool ret = false; |
| 196 | |
| 197 | if (!intel_pmu_lbr_is_enabled(vcpu)) |
| 198 | return ret; |
| 199 | |
| 200 | ret = (index == MSR_LBR_SELECT) || (index == MSR_LBR_TOS) || |
| 201 | (index >= records->from && index < records->from + records->nr) || |
| 202 | (index >= records->to && index < records->to + records->nr); |
| 203 | |
| 204 | if (!ret && records->info) |
| 205 | ret = (index >= records->info && index < records->info + records->nr); |
| 206 | |
| 207 | return ret; |
| 208 | } |
| 209 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 210 | static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr) |
| 211 | { |
| 212 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 213 | int ret; |
| 214 | |
| 215 | switch (msr) { |
| 216 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 217 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 218 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 219 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
| 220 | ret = pmu->version > 1; |
| 221 | break; |
| 222 | default: |
| 223 | ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) || |
| 224 | get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) || |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 225 | get_fixed_pmc(pmu, msr) || get_fw_gp_pmc(pmu, msr) || |
| 226 | intel_pmu_is_valid_lbr_msr(vcpu, msr); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 227 | break; |
| 228 | } |
| 229 | |
| 230 | return ret; |
| 231 | } |
| 232 | |
Like Xu | c900c15 | 2019-10-27 18:52:41 +0800 | [diff] [blame] | 233 | static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr) |
| 234 | { |
| 235 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 236 | struct kvm_pmc *pmc; |
| 237 | |
| 238 | pmc = get_fixed_pmc(pmu, msr); |
| 239 | pmc = pmc ? pmc : get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0); |
| 240 | pmc = pmc ? pmc : get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0); |
| 241 | |
| 242 | return pmc; |
| 243 | } |
| 244 | |
Like Xu | 8e12911 | 2021-02-01 13:10:33 +0800 | [diff] [blame] | 245 | static inline void intel_pmu_release_guest_lbr_event(struct kvm_vcpu *vcpu) |
| 246 | { |
| 247 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 248 | |
| 249 | if (lbr_desc->event) { |
| 250 | perf_event_release_kernel(lbr_desc->event); |
| 251 | lbr_desc->event = NULL; |
| 252 | vcpu_to_pmu(vcpu)->event_count--; |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu) |
| 257 | { |
| 258 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 259 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 260 | struct perf_event *event; |
| 261 | |
| 262 | /* |
| 263 | * The perf_event_attr is constructed in the minimum efficient way: |
| 264 | * - set 'pinned = true' to make it task pinned so that if another |
| 265 | * cpu pinned event reclaims LBR, the event->oncpu will be set to -1; |
| 266 | * - set '.exclude_host = true' to record guest branches behavior; |
| 267 | * |
| 268 | * - set '.config = INTEL_FIXED_VLBR_EVENT' to indicates host perf |
| 269 | * schedule the event without a real HW counter but a fake one; |
| 270 | * check is_guest_lbr_event() and __intel_get_event_constraints(); |
| 271 | * |
| 272 | * - set 'sample_type = PERF_SAMPLE_BRANCH_STACK' and |
| 273 | * 'branch_sample_type = PERF_SAMPLE_BRANCH_CALL_STACK | |
| 274 | * PERF_SAMPLE_BRANCH_USER' to configure it as a LBR callstack |
| 275 | * event, which helps KVM to save/restore guest LBR records |
| 276 | * during host context switches and reduces quite a lot overhead, |
| 277 | * check branch_user_callstack() and intel_pmu_lbr_sched_task(); |
| 278 | */ |
| 279 | struct perf_event_attr attr = { |
| 280 | .type = PERF_TYPE_RAW, |
| 281 | .size = sizeof(attr), |
| 282 | .config = INTEL_FIXED_VLBR_EVENT, |
| 283 | .sample_type = PERF_SAMPLE_BRANCH_STACK, |
| 284 | .pinned = true, |
| 285 | .exclude_host = true, |
| 286 | .branch_sample_type = PERF_SAMPLE_BRANCH_CALL_STACK | |
| 287 | PERF_SAMPLE_BRANCH_USER, |
| 288 | }; |
| 289 | |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 290 | if (unlikely(lbr_desc->event)) { |
| 291 | __set_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use); |
Like Xu | 8e12911 | 2021-02-01 13:10:33 +0800 | [diff] [blame] | 292 | return 0; |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 293 | } |
Like Xu | 8e12911 | 2021-02-01 13:10:33 +0800 | [diff] [blame] | 294 | |
| 295 | event = perf_event_create_kernel_counter(&attr, -1, |
| 296 | current, NULL, NULL); |
| 297 | if (IS_ERR(event)) { |
| 298 | pr_debug_ratelimited("%s: failed %ld\n", |
| 299 | __func__, PTR_ERR(event)); |
Like Xu | 67b45af | 2021-02-23 09:39:57 +0800 | [diff] [blame] | 300 | return PTR_ERR(event); |
Like Xu | 8e12911 | 2021-02-01 13:10:33 +0800 | [diff] [blame] | 301 | } |
| 302 | lbr_desc->event = event; |
| 303 | pmu->event_count++; |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 304 | __set_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use); |
Like Xu | 8e12911 | 2021-02-01 13:10:33 +0800 | [diff] [blame] | 305 | return 0; |
| 306 | } |
| 307 | |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 308 | /* |
| 309 | * It's safe to access LBR msrs from guest when they have not |
| 310 | * been passthrough since the host would help restore or reset |
| 311 | * the LBR msrs records when the guest LBR event is scheduled in. |
| 312 | */ |
| 313 | static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu, |
| 314 | struct msr_data *msr_info, bool read) |
| 315 | { |
| 316 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 317 | u32 index = msr_info->index; |
| 318 | |
| 319 | if (!intel_pmu_is_valid_lbr_msr(vcpu, index)) |
| 320 | return false; |
| 321 | |
Like Xu | 67b45af | 2021-02-23 09:39:57 +0800 | [diff] [blame] | 322 | if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu) < 0) |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 323 | goto dummy; |
| 324 | |
| 325 | /* |
| 326 | * Disable irq to ensure the LBR feature doesn't get reclaimed by the |
| 327 | * host at the time the value is read from the msr, and this avoids the |
| 328 | * host LBR value to be leaked to the guest. If LBR has been reclaimed, |
| 329 | * return 0 on guest reads. |
| 330 | */ |
| 331 | local_irq_disable(); |
| 332 | if (lbr_desc->event->state == PERF_EVENT_STATE_ACTIVE) { |
| 333 | if (read) |
| 334 | rdmsrl(index, msr_info->data); |
| 335 | else |
| 336 | wrmsrl(index, msr_info->data); |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 337 | __set_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use); |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 338 | local_irq_enable(); |
| 339 | return true; |
| 340 | } |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 341 | clear_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use); |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 342 | local_irq_enable(); |
| 343 | |
| 344 | dummy: |
| 345 | if (read) |
| 346 | msr_info->data = 0; |
| 347 | return true; |
| 348 | } |
| 349 | |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 350 | static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 351 | { |
| 352 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 353 | struct kvm_pmc *pmc; |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 354 | u32 msr = msr_info->index; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 355 | |
| 356 | switch (msr) { |
| 357 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 358 | msr_info->data = pmu->fixed_ctr_ctrl; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 359 | return 0; |
| 360 | case MSR_CORE_PERF_GLOBAL_STATUS: |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 361 | msr_info->data = pmu->global_status; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 362 | return 0; |
| 363 | case MSR_CORE_PERF_GLOBAL_CTRL: |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 364 | msr_info->data = pmu->global_ctrl; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 365 | return 0; |
| 366 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
Wanpeng Li | 540c7ab | 2021-10-19 01:12:39 -0700 | [diff] [blame] | 367 | msr_info->data = 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 368 | return 0; |
| 369 | default: |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 370 | if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || |
| 371 | (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { |
Paolo Bonzini | 0e6f467 | 2019-05-20 17:20:40 +0200 | [diff] [blame] | 372 | u64 val = pmc_read_counter(pmc); |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 373 | msr_info->data = |
| 374 | val & pmu->counter_bitmask[KVM_PMC_GP]; |
Paolo Bonzini | 0e6f467 | 2019-05-20 17:20:40 +0200 | [diff] [blame] | 375 | return 0; |
| 376 | } else if ((pmc = get_fixed_pmc(pmu, msr))) { |
| 377 | u64 val = pmc_read_counter(pmc); |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 378 | msr_info->data = |
| 379 | val & pmu->counter_bitmask[KVM_PMC_FIXED]; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 380 | return 0; |
| 381 | } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { |
Wei Wang | cbd7175 | 2020-05-29 15:43:44 +0800 | [diff] [blame] | 382 | msr_info->data = pmc->eventsel; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 383 | return 0; |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 384 | } else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, true)) |
| 385 | return 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | return 1; |
| 389 | } |
| 390 | |
| 391 | static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info) |
| 392 | { |
| 393 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
| 394 | struct kvm_pmc *pmc; |
| 395 | u32 msr = msr_info->index; |
| 396 | u64 data = msr_info->data; |
| 397 | |
| 398 | switch (msr) { |
| 399 | case MSR_CORE_PERF_FIXED_CTR_CTRL: |
| 400 | if (pmu->fixed_ctr_ctrl == data) |
| 401 | return 0; |
| 402 | if (!(data & 0xfffffffffffff444ull)) { |
| 403 | reprogram_fixed_counters(pmu, data); |
| 404 | return 0; |
| 405 | } |
| 406 | break; |
| 407 | case MSR_CORE_PERF_GLOBAL_STATUS: |
| 408 | if (msr_info->host_initiated) { |
| 409 | pmu->global_status = data; |
| 410 | return 0; |
| 411 | } |
| 412 | break; /* RO MSR */ |
| 413 | case MSR_CORE_PERF_GLOBAL_CTRL: |
| 414 | if (pmu->global_ctrl == data) |
| 415 | return 0; |
Oliver Upton | 9477f44 | 2019-11-13 16:17:15 -0800 | [diff] [blame] | 416 | if (kvm_valid_perf_global_ctrl(pmu, data)) { |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 417 | global_ctrl_changed(pmu, data); |
| 418 | return 0; |
| 419 | } |
| 420 | break; |
| 421 | case MSR_CORE_PERF_GLOBAL_OVF_CTRL: |
Luwei Kang | c715eb9 | 2019-02-18 19:26:08 -0500 | [diff] [blame] | 422 | if (!(data & pmu->global_ovf_ctrl_mask)) { |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 423 | if (!msr_info->host_initiated) |
| 424 | pmu->global_status &= ~data; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 425 | return 0; |
| 426 | } |
| 427 | break; |
| 428 | default: |
Like Xu | 27461da3 | 2020-05-29 15:43:45 +0800 | [diff] [blame] | 429 | if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) || |
| 430 | (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) { |
| 431 | if ((msr & MSR_PMC_FULL_WIDTH_BIT) && |
| 432 | (data & ~pmu->counter_bitmask[KVM_PMC_GP])) |
| 433 | return 1; |
| 434 | if (!msr_info->host_initiated && |
| 435 | !(msr & MSR_PMC_FULL_WIDTH_BIT)) |
Eric Hankland | 4400cf5 | 2020-01-27 13:22:56 -0800 | [diff] [blame] | 436 | data = (s64)(s32)data; |
| 437 | pmc->counter += data - pmc_read_counter(pmc); |
Like Xu | e79f49c | 2021-07-28 20:07:05 +0800 | [diff] [blame] | 438 | if (pmc->perf_event && !pmc->is_paused) |
Eric Hankland | 168d918 | 2020-02-21 18:34:13 -0800 | [diff] [blame] | 439 | perf_event_period(pmc->perf_event, |
| 440 | get_sample_period(pmc, data)); |
Paolo Bonzini | 2924b52 | 2019-05-20 17:34:30 +0200 | [diff] [blame] | 441 | return 0; |
| 442 | } else if ((pmc = get_fixed_pmc(pmu, msr))) { |
Eric Hankland | 4400cf5 | 2020-01-27 13:22:56 -0800 | [diff] [blame] | 443 | pmc->counter += data - pmc_read_counter(pmc); |
Like Xu | e79f49c | 2021-07-28 20:07:05 +0800 | [diff] [blame] | 444 | if (pmc->perf_event && !pmc->is_paused) |
Eric Hankland | 168d918 | 2020-02-21 18:34:13 -0800 | [diff] [blame] | 445 | perf_event_period(pmc->perf_event, |
| 446 | get_sample_period(pmc, data)); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 447 | return 0; |
| 448 | } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) { |
| 449 | if (data == pmc->eventsel) |
| 450 | return 0; |
| 451 | if (!(data & pmu->reserved_bits)) { |
| 452 | reprogram_gp_counter(pmc, data); |
| 453 | return 0; |
| 454 | } |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 455 | } else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, false)) |
| 456 | return 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | return 1; |
| 460 | } |
| 461 | |
| 462 | static void intel_pmu_refresh(struct kvm_vcpu *vcpu) |
| 463 | { |
| 464 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame] | 465 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 466 | |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 467 | struct x86_pmu_capability x86_pmu; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 468 | struct kvm_cpuid_entry2 *entry; |
| 469 | union cpuid10_eax eax; |
| 470 | union cpuid10_edx edx; |
| 471 | |
| 472 | pmu->nr_arch_gp_counters = 0; |
| 473 | pmu->nr_arch_fixed_counters = 0; |
| 474 | pmu->counter_bitmask[KVM_PMC_GP] = 0; |
| 475 | pmu->counter_bitmask[KVM_PMC_FIXED] = 0; |
| 476 | pmu->version = 0; |
| 477 | pmu->reserved_bits = 0xffffffff00200000ull; |
| 478 | |
| 479 | entry = kvm_find_cpuid_entry(vcpu, 0xa, 0); |
| 480 | if (!entry) |
| 481 | return; |
| 482 | eax.full = entry->eax; |
| 483 | edx.full = entry->edx; |
| 484 | |
| 485 | pmu->version = eax.split.version_id; |
| 486 | if (!pmu->version) |
| 487 | return; |
| 488 | |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 489 | perf_get_x86_pmu_capability(&x86_pmu); |
| 490 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 491 | pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters, |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 492 | x86_pmu.num_counters_gp); |
Like Xu | e61ab2a | 2021-01-18 10:58:00 +0800 | [diff] [blame] | 493 | eax.split.bit_width = min_t(int, eax.split.bit_width, x86_pmu.bit_width_gp); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 494 | pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1; |
Like Xu | e61ab2a | 2021-01-18 10:58:00 +0800 | [diff] [blame] | 495 | eax.split.mask_length = min_t(int, eax.split.mask_length, x86_pmu.events_mask_len); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 496 | pmu->available_event_types = ~entry->ebx & |
| 497 | ((1ull << eax.split.mask_length) - 1); |
| 498 | |
| 499 | if (pmu->version == 1) { |
| 500 | pmu->nr_arch_fixed_counters = 0; |
| 501 | } else { |
| 502 | pmu->nr_arch_fixed_counters = |
| 503 | min_t(int, edx.split.num_counters_fixed, |
Jim Mattson | e1fba49 | 2019-09-30 16:38:54 -0700 | [diff] [blame] | 504 | x86_pmu.num_counters_fixed); |
Like Xu | e61ab2a | 2021-01-18 10:58:00 +0800 | [diff] [blame] | 505 | edx.split.bit_width_fixed = min_t(int, |
| 506 | edx.split.bit_width_fixed, x86_pmu.bit_width_fixed); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 507 | pmu->counter_bitmask[KVM_PMC_FIXED] = |
| 508 | ((u64)1 << edx.split.bit_width_fixed) - 1; |
| 509 | } |
| 510 | |
Radim Krčmář | 34b0dad | 2017-05-18 19:37:31 +0200 | [diff] [blame] | 511 | pmu->global_ctrl = ((1ull << pmu->nr_arch_gp_counters) - 1) | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 512 | (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED); |
| 513 | pmu->global_ctrl_mask = ~pmu->global_ctrl; |
Luwei Kang | c715eb9 | 2019-02-18 19:26:08 -0500 | [diff] [blame] | 514 | pmu->global_ovf_ctrl_mask = pmu->global_ctrl_mask |
| 515 | & ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF | |
| 516 | MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD); |
Sean Christopherson | a1bead2 | 2020-03-02 15:57:00 -0800 | [diff] [blame] | 517 | if (vmx_pt_mode_is_host_guest()) |
Luwei Kang | c715eb9 | 2019-02-18 19:26:08 -0500 | [diff] [blame] | 518 | pmu->global_ovf_ctrl_mask &= |
| 519 | ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 520 | |
| 521 | entry = kvm_find_cpuid_entry(vcpu, 7, 0); |
| 522 | if (entry && |
| 523 | (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) && |
| 524 | (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) |
| 525 | pmu->reserved_bits ^= HSW_IN_TX|HSW_IN_TX_CHECKPOINTED; |
Like Xu | b35e554 | 2019-10-27 18:52:43 +0800 | [diff] [blame] | 526 | |
| 527 | bitmap_set(pmu->all_valid_pmc_idx, |
| 528 | 0, pmu->nr_arch_gp_counters); |
| 529 | bitmap_set(pmu->all_valid_pmc_idx, |
| 530 | INTEL_PMC_MAX_GENERIC, pmu->nr_arch_fixed_counters); |
Oliver Upton | 03a8871a | 2019-11-13 16:17:20 -0800 | [diff] [blame] | 531 | |
| 532 | nested_vmx_pmu_entry_exit_ctls_update(vcpu); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame] | 533 | |
| 534 | if (intel_pmu_lbr_is_compatible(vcpu)) |
| 535 | x86_perf_get_lbr(&lbr_desc->records); |
| 536 | else |
| 537 | lbr_desc->records.nr = 0; |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 538 | |
| 539 | if (lbr_desc->records.nr) |
| 540 | bitmap_set(pmu->all_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 541 | } |
| 542 | |
| 543 | static void intel_pmu_init(struct kvm_vcpu *vcpu) |
| 544 | { |
| 545 | int i; |
| 546 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame] | 547 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 548 | |
| 549 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
| 550 | pmu->gp_counters[i].type = KVM_PMC_GP; |
| 551 | pmu->gp_counters[i].vcpu = vcpu; |
| 552 | pmu->gp_counters[i].idx = i; |
Like Xu | a6da0d7 | 2019-10-27 18:52:42 +0800 | [diff] [blame] | 553 | pmu->gp_counters[i].current_config = 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 554 | } |
| 555 | |
| 556 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) { |
| 557 | pmu->fixed_counters[i].type = KVM_PMC_FIXED; |
| 558 | pmu->fixed_counters[i].vcpu = vcpu; |
| 559 | pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED; |
Like Xu | a6da0d7 | 2019-10-27 18:52:42 +0800 | [diff] [blame] | 560 | pmu->fixed_counters[i].current_config = 0; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 561 | } |
Paolo Bonzini | a755753 | 2021-02-02 09:32:35 -0500 | [diff] [blame] | 562 | |
| 563 | vcpu->arch.perf_capabilities = vmx_get_perf_capabilities(); |
Paolo Bonzini | 9c9520c | 2021-02-02 09:36:08 -0500 | [diff] [blame] | 564 | lbr_desc->records.nr = 0; |
Like Xu | 8e12911 | 2021-02-01 13:10:33 +0800 | [diff] [blame] | 565 | lbr_desc->event = NULL; |
Like Xu | 9254bea | 2021-02-01 13:10:35 +0800 | [diff] [blame] | 566 | lbr_desc->msr_passthrough = false; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 567 | } |
| 568 | |
| 569 | static void intel_pmu_reset(struct kvm_vcpu *vcpu) |
| 570 | { |
| 571 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Like Xu | 4d1a082d | 2019-07-17 10:51:18 +0800 | [diff] [blame] | 572 | struct kvm_pmc *pmc = NULL; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 573 | int i; |
| 574 | |
| 575 | for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) { |
Like Xu | 4d1a082d | 2019-07-17 10:51:18 +0800 | [diff] [blame] | 576 | pmc = &pmu->gp_counters[i]; |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 577 | |
| 578 | pmc_stop_counter(pmc); |
| 579 | pmc->counter = pmc->eventsel = 0; |
| 580 | } |
| 581 | |
Like Xu | 4d1a082d | 2019-07-17 10:51:18 +0800 | [diff] [blame] | 582 | for (i = 0; i < INTEL_PMC_MAX_FIXED; i++) { |
| 583 | pmc = &pmu->fixed_counters[i]; |
| 584 | |
| 585 | pmc_stop_counter(pmc); |
| 586 | pmc->counter = 0; |
| 587 | } |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 588 | |
Wanpeng Li | 540c7ab | 2021-10-19 01:12:39 -0700 | [diff] [blame] | 589 | pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = 0; |
Like Xu | 8e12911 | 2021-02-01 13:10:33 +0800 | [diff] [blame] | 590 | |
| 591 | intel_pmu_release_guest_lbr_event(vcpu); |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 592 | } |
| 593 | |
Like Xu | e6209a3 | 2021-02-01 13:10:36 +0800 | [diff] [blame] | 594 | /* |
| 595 | * Emulate LBR_On_PMI behavior for 1 < pmu.version < 4. |
| 596 | * |
| 597 | * If Freeze_LBR_On_PMI = 1, the LBR is frozen on PMI and |
| 598 | * the KVM emulates to clear the LBR bit (bit 0) in IA32_DEBUGCTL. |
| 599 | * |
| 600 | * Guest needs to re-enable LBR to resume branches recording. |
| 601 | */ |
| 602 | static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu) |
| 603 | { |
| 604 | u64 data = vmcs_read64(GUEST_IA32_DEBUGCTL); |
| 605 | |
| 606 | if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) { |
| 607 | data &= ~DEBUGCTLMSR_LBR; |
| 608 | vmcs_write64(GUEST_IA32_DEBUGCTL, data); |
| 609 | } |
| 610 | } |
| 611 | |
| 612 | static void intel_pmu_deliver_pmi(struct kvm_vcpu *vcpu) |
| 613 | { |
| 614 | u8 version = vcpu_to_pmu(vcpu)->version; |
| 615 | |
| 616 | if (!intel_pmu_lbr_is_enabled(vcpu)) |
| 617 | return; |
| 618 | |
| 619 | if (version > 1 && version < 4) |
| 620 | intel_pmu_legacy_freezing_lbrs_on_pmi(vcpu); |
| 621 | } |
| 622 | |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 623 | static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set) |
| 624 | { |
| 625 | struct x86_pmu_lbr *lbr = vcpu_to_lbr_records(vcpu); |
| 626 | int i; |
| 627 | |
| 628 | for (i = 0; i < lbr->nr; i++) { |
| 629 | vmx_set_intercept_for_msr(vcpu, lbr->from + i, MSR_TYPE_RW, set); |
| 630 | vmx_set_intercept_for_msr(vcpu, lbr->to + i, MSR_TYPE_RW, set); |
| 631 | if (lbr->info) |
| 632 | vmx_set_intercept_for_msr(vcpu, lbr->info + i, MSR_TYPE_RW, set); |
| 633 | } |
| 634 | |
| 635 | vmx_set_intercept_for_msr(vcpu, MSR_LBR_SELECT, MSR_TYPE_RW, set); |
| 636 | vmx_set_intercept_for_msr(vcpu, MSR_LBR_TOS, MSR_TYPE_RW, set); |
| 637 | } |
| 638 | |
| 639 | static inline void vmx_disable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu) |
| 640 | { |
Like Xu | 9254bea | 2021-02-01 13:10:35 +0800 | [diff] [blame] | 641 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 642 | |
| 643 | if (!lbr_desc->msr_passthrough) |
| 644 | return; |
| 645 | |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 646 | vmx_update_intercept_for_lbr_msrs(vcpu, true); |
Like Xu | 9254bea | 2021-02-01 13:10:35 +0800 | [diff] [blame] | 647 | lbr_desc->msr_passthrough = false; |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 648 | } |
| 649 | |
| 650 | static inline void vmx_enable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu) |
| 651 | { |
Like Xu | 9254bea | 2021-02-01 13:10:35 +0800 | [diff] [blame] | 652 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 653 | |
| 654 | if (lbr_desc->msr_passthrough) |
| 655 | return; |
| 656 | |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 657 | vmx_update_intercept_for_lbr_msrs(vcpu, false); |
Like Xu | 9254bea | 2021-02-01 13:10:35 +0800 | [diff] [blame] | 658 | lbr_desc->msr_passthrough = true; |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 659 | } |
| 660 | |
| 661 | /* |
| 662 | * Higher priority host perf events (e.g. cpu pinned) could reclaim the |
| 663 | * pmu resources (e.g. LBR) that were assigned to the guest. This is |
| 664 | * usually done via ipi calls (more details in perf_install_in_context). |
| 665 | * |
| 666 | * Before entering the non-root mode (with irq disabled here), double |
| 667 | * confirm that the pmu features enabled to the guest are not reclaimed |
| 668 | * by higher priority host events. Otherwise, disallow vcpu's access to |
| 669 | * the reclaimed features. |
| 670 | */ |
| 671 | void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu) |
| 672 | { |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 673 | struct kvm_pmu *pmu = vcpu_to_pmu(vcpu); |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 674 | struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu); |
| 675 | |
| 676 | if (!lbr_desc->event) { |
| 677 | vmx_disable_lbr_msrs_passthrough(vcpu); |
| 678 | if (vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR) |
| 679 | goto warn; |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 680 | if (test_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use)) |
| 681 | goto warn; |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 682 | return; |
| 683 | } |
| 684 | |
| 685 | if (lbr_desc->event->state < PERF_EVENT_STATE_ACTIVE) { |
| 686 | vmx_disable_lbr_msrs_passthrough(vcpu); |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 687 | __clear_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use); |
Like Xu | 1b5ac322 | 2021-02-01 13:10:34 +0800 | [diff] [blame] | 688 | goto warn; |
| 689 | } else |
| 690 | vmx_enable_lbr_msrs_passthrough(vcpu); |
| 691 | |
| 692 | return; |
| 693 | |
| 694 | warn: |
| 695 | pr_warn_ratelimited("kvm: vcpu-%d: fail to passthrough LBR.\n", |
| 696 | vcpu->vcpu_id); |
| 697 | } |
| 698 | |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 699 | static void intel_pmu_cleanup(struct kvm_vcpu *vcpu) |
| 700 | { |
| 701 | if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)) |
| 702 | intel_pmu_release_guest_lbr_event(vcpu); |
| 703 | } |
| 704 | |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 705 | struct kvm_pmu_ops intel_pmu_ops = { |
| 706 | .find_arch_event = intel_find_arch_event, |
| 707 | .find_fixed_event = intel_find_fixed_event, |
| 708 | .pmc_is_enabled = intel_pmc_is_enabled, |
| 709 | .pmc_idx_to_pmc = intel_pmc_idx_to_pmc, |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 710 | .rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc, |
Like Xu | c900c15 | 2019-10-27 18:52:41 +0800 | [diff] [blame] | 711 | .msr_idx_to_pmc = intel_msr_idx_to_pmc, |
Like Xu | 98ff80f | 2019-10-27 18:52:40 +0800 | [diff] [blame] | 712 | .is_valid_rdpmc_ecx = intel_is_valid_rdpmc_ecx, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 713 | .is_valid_msr = intel_is_valid_msr, |
| 714 | .get_msr = intel_pmu_get_msr, |
| 715 | .set_msr = intel_pmu_set_msr, |
| 716 | .refresh = intel_pmu_refresh, |
| 717 | .init = intel_pmu_init, |
| 718 | .reset = intel_pmu_reset, |
Like Xu | e6209a3 | 2021-02-01 13:10:36 +0800 | [diff] [blame] | 719 | .deliver_pmi = intel_pmu_deliver_pmi, |
Like Xu | 9aa4f62 | 2021-02-01 13:10:37 +0800 | [diff] [blame] | 720 | .cleanup = intel_pmu_cleanup, |
Wei Huang | 25462f7 | 2015-06-19 15:45:05 +0200 | [diff] [blame] | 721 | }; |