blob: 1b7456b2177b9f6c46750f03c04c174560212c1c [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Wei Huang25462f72015-06-19 15:45:05 +02002/*
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 Huang25462f72015-06-19 15:45:05 +020010 */
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 Upton03a8871a2019-11-13 16:17:20 -080018#include "nested.h"
Wei Huang25462f72015-06-19 15:45:05 +020019#include "pmu.h"
20
Like Xu27461da32020-05-29 15:43:45 +080021#define MSR_PMC_FULL_WIDTH_BIT (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0)
22
Wei Huang25462f72015-06-19 15:45:05 +020023static 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 Xu98dd2f12020-12-30 16:19:16 +080032 [7] = { 0x00, 0x03, PERF_COUNT_HW_REF_CPU_CYCLES },
Wei Huang25462f72015-06-19 15:45:05 +020033};
34
35/* mapping between fixed pmc index and intel_arch_events array */
36static int fixed_pmc_events[] = {1, 0, 7};
37
38static 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 Xub35e5542019-10-27 18:52:43 +080052 __set_bit(INTEL_PMC_IDX_FIXED + i, pmu->pmc_in_use);
Wei Huang25462f72015-06-19 15:45:05 +020053 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. */
60static 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
71static 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
89static unsigned intel_find_fixed_event(int idx)
90{
Marios Pomonis66061742019-12-11 12:47:53 -080091 u32 event;
92 size_t size = ARRAY_SIZE(fixed_pmc_events);
93
94 if (idx >= size)
Wei Huang25462f72015-06-19 15:45:05 +020095 return PERF_COUNT_HW_MAX;
96
Marios Pomonis66061742019-12-11 12:47:53 -080097 event = fixed_pmc_events[array_index_nospec(idx, size)];
98 return intel_arch_events[event].event_type;
Wei Huang25462f72015-06-19 15:45:05 +020099}
100
Andrea Gelminibb3541f2016-05-21 14:14:44 +0200101/* check if a PMC is enabled by comparing it with globl_ctrl bits. */
Wei Huang25462f72015-06-19 15:45:05 +0200102static 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
109static 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 Mattsone6cd31f2021-11-05 13:20:58 -0700121static bool intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
Wei Huang25462f72015-06-19 15:45:05 +0200122{
123 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
124 bool fixed = idx & (1u << 30);
125
126 idx &= ~(3u << 30);
127
Jim Mattsone6cd31f2021-11-05 13:20:58 -0700128 return fixed ? idx < pmu->nr_arch_fixed_counters
129 : idx < pmu->nr_arch_gp_counters;
Wei Huang25462f72015-06-19 15:45:05 +0200130}
131
Like Xu98ff80f2019-10-27 18:52:40 +0800132static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
133 unsigned int idx, u64 *mask)
Wei Huang25462f72015-06-19 15:45:05 +0200134{
135 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
136 bool fixed = idx & (1u << 30);
137 struct kvm_pmc *counters;
Marios Pomonis66061742019-12-11 12:47:53 -0800138 unsigned int num_counters;
Wei Huang25462f72015-06-19 15:45:05 +0200139
140 idx &= ~(3u << 30);
Marios Pomonis66061742019-12-11 12:47:53 -0800141 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 Huang25462f72015-06-19 15:45:05 +0200149 return NULL;
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200150 *mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
Marios Pomonis66061742019-12-11 12:47:53 -0800151 return &counters[array_index_nospec(idx, num_counters)];
Wei Huang25462f72015-06-19 15:45:05 +0200152}
153
Paolo Bonzinia7557532021-02-02 09:32:35 -0500154static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu)
Like Xu27461da32020-05-29 15:43:45 +0800155{
156 if (!guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
Paolo Bonzinia7557532021-02-02 09:32:35 -0500157 return 0;
Like Xu27461da32020-05-29 15:43:45 +0800158
Paolo Bonzinia7557532021-02-02 09:32:35 -0500159 return vcpu->arch.perf_capabilities;
160}
161
162static inline bool fw_writes_is_enabled(struct kvm_vcpu *vcpu)
163{
164 return (vcpu_get_perf_capabilities(vcpu) & PMU_CAP_FW_WRITES) != 0;
Like Xu27461da32020-05-29 15:43:45 +0800165}
166
167static 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 Bonzini9c9520c2021-02-02 09:36:08 -0500175bool 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 Xuc6462362021-02-01 13:10:31 +0800185bool 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 Xu1b5ac3222021-02-01 13:10:34 +0800192static 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 Huang25462f72015-06-19 15:45:05 +0200210static 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 Xu1b5ac3222021-02-01 13:10:34 +0800225 get_fixed_pmc(pmu, msr) || get_fw_gp_pmc(pmu, msr) ||
226 intel_pmu_is_valid_lbr_msr(vcpu, msr);
Wei Huang25462f72015-06-19 15:45:05 +0200227 break;
228 }
229
230 return ret;
231}
232
Like Xuc900c152019-10-27 18:52:41 +0800233static 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 Xu8e129112021-02-01 13:10:33 +0800245static 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
256int 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 Xu9aa4f622021-02-01 13:10:37 +0800290 if (unlikely(lbr_desc->event)) {
291 __set_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use);
Like Xu8e129112021-02-01 13:10:33 +0800292 return 0;
Like Xu9aa4f622021-02-01 13:10:37 +0800293 }
Like Xu8e129112021-02-01 13:10:33 +0800294
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 Xu67b45af2021-02-23 09:39:57 +0800300 return PTR_ERR(event);
Like Xu8e129112021-02-01 13:10:33 +0800301 }
302 lbr_desc->event = event;
303 pmu->event_count++;
Like Xu9aa4f622021-02-01 13:10:37 +0800304 __set_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use);
Like Xu8e129112021-02-01 13:10:33 +0800305 return 0;
306}
307
Like Xu1b5ac3222021-02-01 13:10:34 +0800308/*
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 */
313static 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 Xu67b45af2021-02-23 09:39:57 +0800322 if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu) < 0)
Like Xu1b5ac3222021-02-01 13:10:34 +0800323 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 Xu9aa4f622021-02-01 13:10:37 +0800337 __set_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use);
Like Xu1b5ac3222021-02-01 13:10:34 +0800338 local_irq_enable();
339 return true;
340 }
Like Xu9aa4f622021-02-01 13:10:37 +0800341 clear_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use);
Like Xu1b5ac3222021-02-01 13:10:34 +0800342 local_irq_enable();
343
344dummy:
345 if (read)
346 msr_info->data = 0;
347 return true;
348}
349
Wei Wangcbd71752020-05-29 15:43:44 +0800350static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Wei Huang25462f72015-06-19 15:45:05 +0200351{
352 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
353 struct kvm_pmc *pmc;
Wei Wangcbd71752020-05-29 15:43:44 +0800354 u32 msr = msr_info->index;
Wei Huang25462f72015-06-19 15:45:05 +0200355
356 switch (msr) {
357 case MSR_CORE_PERF_FIXED_CTR_CTRL:
Wei Wangcbd71752020-05-29 15:43:44 +0800358 msr_info->data = pmu->fixed_ctr_ctrl;
Wei Huang25462f72015-06-19 15:45:05 +0200359 return 0;
360 case MSR_CORE_PERF_GLOBAL_STATUS:
Wei Wangcbd71752020-05-29 15:43:44 +0800361 msr_info->data = pmu->global_status;
Wei Huang25462f72015-06-19 15:45:05 +0200362 return 0;
363 case MSR_CORE_PERF_GLOBAL_CTRL:
Wei Wangcbd71752020-05-29 15:43:44 +0800364 msr_info->data = pmu->global_ctrl;
Wei Huang25462f72015-06-19 15:45:05 +0200365 return 0;
366 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
Wanpeng Li540c7ab2021-10-19 01:12:39 -0700367 msr_info->data = 0;
Wei Huang25462f72015-06-19 15:45:05 +0200368 return 0;
369 default:
Like Xu27461da32020-05-29 15:43:45 +0800370 if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
371 (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200372 u64 val = pmc_read_counter(pmc);
Wei Wangcbd71752020-05-29 15:43:44 +0800373 msr_info->data =
374 val & pmu->counter_bitmask[KVM_PMC_GP];
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200375 return 0;
376 } else if ((pmc = get_fixed_pmc(pmu, msr))) {
377 u64 val = pmc_read_counter(pmc);
Wei Wangcbd71752020-05-29 15:43:44 +0800378 msr_info->data =
379 val & pmu->counter_bitmask[KVM_PMC_FIXED];
Wei Huang25462f72015-06-19 15:45:05 +0200380 return 0;
381 } else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
Wei Wangcbd71752020-05-29 15:43:44 +0800382 msr_info->data = pmc->eventsel;
Wei Huang25462f72015-06-19 15:45:05 +0200383 return 0;
Like Xu1b5ac3222021-02-01 13:10:34 +0800384 } else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, true))
385 return 0;
Wei Huang25462f72015-06-19 15:45:05 +0200386 }
387
388 return 1;
389}
390
391static 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 Upton9477f442019-11-13 16:17:15 -0800416 if (kvm_valid_perf_global_ctrl(pmu, data)) {
Wei Huang25462f72015-06-19 15:45:05 +0200417 global_ctrl_changed(pmu, data);
418 return 0;
419 }
420 break;
421 case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
Luwei Kangc715eb92019-02-18 19:26:08 -0500422 if (!(data & pmu->global_ovf_ctrl_mask)) {
Wei Huang25462f72015-06-19 15:45:05 +0200423 if (!msr_info->host_initiated)
424 pmu->global_status &= ~data;
Wei Huang25462f72015-06-19 15:45:05 +0200425 return 0;
426 }
427 break;
428 default:
Like Xu27461da32020-05-29 15:43:45 +0800429 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 Hankland4400cf52020-01-27 13:22:56 -0800436 data = (s64)(s32)data;
437 pmc->counter += data - pmc_read_counter(pmc);
Like Xue79f49c2021-07-28 20:07:05 +0800438 if (pmc->perf_event && !pmc->is_paused)
Eric Hankland168d9182020-02-21 18:34:13 -0800439 perf_event_period(pmc->perf_event,
440 get_sample_period(pmc, data));
Paolo Bonzini2924b522019-05-20 17:34:30 +0200441 return 0;
442 } else if ((pmc = get_fixed_pmc(pmu, msr))) {
Eric Hankland4400cf52020-01-27 13:22:56 -0800443 pmc->counter += data - pmc_read_counter(pmc);
Like Xue79f49c2021-07-28 20:07:05 +0800444 if (pmc->perf_event && !pmc->is_paused)
Eric Hankland168d9182020-02-21 18:34:13 -0800445 perf_event_period(pmc->perf_event,
446 get_sample_period(pmc, data));
Wei Huang25462f72015-06-19 15:45:05 +0200447 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 Xu1b5ac3222021-02-01 13:10:34 +0800455 } else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, false))
456 return 0;
Wei Huang25462f72015-06-19 15:45:05 +0200457 }
458
459 return 1;
460}
461
462static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
463{
464 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500465 struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
466
Jim Mattsone1fba492019-09-30 16:38:54 -0700467 struct x86_pmu_capability x86_pmu;
Wei Huang25462f72015-06-19 15:45:05 +0200468 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 Mattsone1fba492019-09-30 16:38:54 -0700489 perf_get_x86_pmu_capability(&x86_pmu);
490
Wei Huang25462f72015-06-19 15:45:05 +0200491 pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
Jim Mattsone1fba492019-09-30 16:38:54 -0700492 x86_pmu.num_counters_gp);
Like Xue61ab2a2021-01-18 10:58:00 +0800493 eax.split.bit_width = min_t(int, eax.split.bit_width, x86_pmu.bit_width_gp);
Wei Huang25462f72015-06-19 15:45:05 +0200494 pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1;
Like Xue61ab2a2021-01-18 10:58:00 +0800495 eax.split.mask_length = min_t(int, eax.split.mask_length, x86_pmu.events_mask_len);
Wei Huang25462f72015-06-19 15:45:05 +0200496 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 Mattsone1fba492019-09-30 16:38:54 -0700504 x86_pmu.num_counters_fixed);
Like Xue61ab2a2021-01-18 10:58:00 +0800505 edx.split.bit_width_fixed = min_t(int,
506 edx.split.bit_width_fixed, x86_pmu.bit_width_fixed);
Wei Huang25462f72015-06-19 15:45:05 +0200507 pmu->counter_bitmask[KVM_PMC_FIXED] =
508 ((u64)1 << edx.split.bit_width_fixed) - 1;
509 }
510
Radim Krčmář34b0dad2017-05-18 19:37:31 +0200511 pmu->global_ctrl = ((1ull << pmu->nr_arch_gp_counters) - 1) |
Wei Huang25462f72015-06-19 15:45:05 +0200512 (((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED);
513 pmu->global_ctrl_mask = ~pmu->global_ctrl;
Luwei Kangc715eb92019-02-18 19:26:08 -0500514 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 Christophersona1bead22020-03-02 15:57:00 -0800517 if (vmx_pt_mode_is_host_guest())
Luwei Kangc715eb92019-02-18 19:26:08 -0500518 pmu->global_ovf_ctrl_mask &=
519 ~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
Wei Huang25462f72015-06-19 15:45:05 +0200520
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 Xub35e5542019-10-27 18:52:43 +0800526
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 Upton03a8871a2019-11-13 16:17:20 -0800531
532 nested_vmx_pmu_entry_exit_ctls_update(vcpu);
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500533
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 Xu9aa4f622021-02-01 13:10:37 +0800538
539 if (lbr_desc->records.nr)
540 bitmap_set(pmu->all_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1);
Wei Huang25462f72015-06-19 15:45:05 +0200541}
542
543static void intel_pmu_init(struct kvm_vcpu *vcpu)
544{
545 int i;
546 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500547 struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
Wei Huang25462f72015-06-19 15:45:05 +0200548
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 Xua6da0d72019-10-27 18:52:42 +0800553 pmu->gp_counters[i].current_config = 0;
Wei Huang25462f72015-06-19 15:45:05 +0200554 }
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 Xua6da0d72019-10-27 18:52:42 +0800560 pmu->fixed_counters[i].current_config = 0;
Wei Huang25462f72015-06-19 15:45:05 +0200561 }
Paolo Bonzinia7557532021-02-02 09:32:35 -0500562
563 vcpu->arch.perf_capabilities = vmx_get_perf_capabilities();
Paolo Bonzini9c9520c2021-02-02 09:36:08 -0500564 lbr_desc->records.nr = 0;
Like Xu8e129112021-02-01 13:10:33 +0800565 lbr_desc->event = NULL;
Like Xu9254bea2021-02-01 13:10:35 +0800566 lbr_desc->msr_passthrough = false;
Wei Huang25462f72015-06-19 15:45:05 +0200567}
568
569static void intel_pmu_reset(struct kvm_vcpu *vcpu)
570{
571 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Like Xu4d1a082d2019-07-17 10:51:18 +0800572 struct kvm_pmc *pmc = NULL;
Wei Huang25462f72015-06-19 15:45:05 +0200573 int i;
574
575 for (i = 0; i < INTEL_PMC_MAX_GENERIC; i++) {
Like Xu4d1a082d2019-07-17 10:51:18 +0800576 pmc = &pmu->gp_counters[i];
Wei Huang25462f72015-06-19 15:45:05 +0200577
578 pmc_stop_counter(pmc);
579 pmc->counter = pmc->eventsel = 0;
580 }
581
Like Xu4d1a082d2019-07-17 10:51:18 +0800582 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 Huang25462f72015-06-19 15:45:05 +0200588
Wanpeng Li540c7ab2021-10-19 01:12:39 -0700589 pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = 0;
Like Xu8e129112021-02-01 13:10:33 +0800590
591 intel_pmu_release_guest_lbr_event(vcpu);
Wei Huang25462f72015-06-19 15:45:05 +0200592}
593
Like Xue6209a32021-02-01 13:10:36 +0800594/*
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 */
602static 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
612static 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 Xu1b5ac3222021-02-01 13:10:34 +0800623static 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
639static inline void vmx_disable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu)
640{
Like Xu9254bea2021-02-01 13:10:35 +0800641 struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
642
643 if (!lbr_desc->msr_passthrough)
644 return;
645
Like Xu1b5ac3222021-02-01 13:10:34 +0800646 vmx_update_intercept_for_lbr_msrs(vcpu, true);
Like Xu9254bea2021-02-01 13:10:35 +0800647 lbr_desc->msr_passthrough = false;
Like Xu1b5ac3222021-02-01 13:10:34 +0800648}
649
650static inline void vmx_enable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu)
651{
Like Xu9254bea2021-02-01 13:10:35 +0800652 struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
653
654 if (lbr_desc->msr_passthrough)
655 return;
656
Like Xu1b5ac3222021-02-01 13:10:34 +0800657 vmx_update_intercept_for_lbr_msrs(vcpu, false);
Like Xu9254bea2021-02-01 13:10:35 +0800658 lbr_desc->msr_passthrough = true;
Like Xu1b5ac3222021-02-01 13:10:34 +0800659}
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 */
671void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)
672{
Like Xu9aa4f622021-02-01 13:10:37 +0800673 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Like Xu1b5ac3222021-02-01 13:10:34 +0800674 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 Xu9aa4f622021-02-01 13:10:37 +0800680 if (test_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use))
681 goto warn;
Like Xu1b5ac3222021-02-01 13:10:34 +0800682 return;
683 }
684
685 if (lbr_desc->event->state < PERF_EVENT_STATE_ACTIVE) {
686 vmx_disable_lbr_msrs_passthrough(vcpu);
Like Xu9aa4f622021-02-01 13:10:37 +0800687 __clear_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use);
Like Xu1b5ac3222021-02-01 13:10:34 +0800688 goto warn;
689 } else
690 vmx_enable_lbr_msrs_passthrough(vcpu);
691
692 return;
693
694warn:
695 pr_warn_ratelimited("kvm: vcpu-%d: fail to passthrough LBR.\n",
696 vcpu->vcpu_id);
697}
698
Like Xu9aa4f622021-02-01 13:10:37 +0800699static 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 Huang25462f72015-06-19 15:45:05 +0200705struct 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 Xu98ff80f2019-10-27 18:52:40 +0800710 .rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,
Like Xuc900c152019-10-27 18:52:41 +0800711 .msr_idx_to_pmc = intel_msr_idx_to_pmc,
Like Xu98ff80f2019-10-27 18:52:40 +0800712 .is_valid_rdpmc_ecx = intel_is_valid_rdpmc_ecx,
Wei Huang25462f72015-06-19 15:45:05 +0200713 .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 Xue6209a32021-02-01 13:10:36 +0800719 .deliver_pmi = intel_pmu_deliver_pmi,
Like Xu9aa4f622021-02-01 13:10:37 +0800720 .cleanup = intel_pmu_cleanup,
Wei Huang25462f72015-06-19 15:45:05 +0200721};