xref: /linux/arch/x86/kvm/vmx/pmu_intel.c (revision a90e8608eb0ed93d31ac0feb055f77ce59512542)
1 // SPDX-License-Identifier: GPL-2.0-only
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>
10  */
11 #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
12 
13 #include <linux/types.h>
14 #include <linux/kvm_host.h>
15 #include <linux/perf_event.h>
16 #include <asm/perf_event.h>
17 #include "x86.h"
18 #include "cpuid.h"
19 #include "lapic.h"
20 #include "nested.h"
21 #include "pmu.h"
22 
23 #define MSR_PMC_FULL_WIDTH_BIT      (MSR_IA32_PMC0 - MSR_IA32_PERFCTR0)
24 
25 static struct {
26 	u8 eventsel;
27 	u8 unit_mask;
28 } const intel_arch_events[] = {
29 	[0] = { 0x3c, 0x00 },
30 	[1] = { 0xc0, 0x00 },
31 	[2] = { 0x3c, 0x01 },
32 	[3] = { 0x2e, 0x4f },
33 	[4] = { 0x2e, 0x41 },
34 	[5] = { 0xc4, 0x00 },
35 	[6] = { 0xc5, 0x00 },
36 	/* The above index must match CPUID 0x0A.EBX bit vector */
37 	[7] = { 0x00, 0x03 },
38 };
39 
40 /* mapping between fixed pmc index and intel_arch_events array */
41 static int fixed_pmc_events[] = {1, 0, 7};
42 
43 static void reprogram_fixed_counters(struct kvm_pmu *pmu, u64 data)
44 {
45 	struct kvm_pmc *pmc;
46 	u8 old_fixed_ctr_ctrl = pmu->fixed_ctr_ctrl;
47 	int i;
48 
49 	pmu->fixed_ctr_ctrl = data;
50 	for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
51 		u8 new_ctrl = fixed_ctrl_field(data, i);
52 		u8 old_ctrl = fixed_ctrl_field(old_fixed_ctr_ctrl, i);
53 
54 		if (old_ctrl == new_ctrl)
55 			continue;
56 
57 		pmc = get_fixed_pmc(pmu, MSR_CORE_PERF_FIXED_CTR0 + i);
58 
59 		__set_bit(INTEL_PMC_IDX_FIXED + i, pmu->pmc_in_use);
60 		kvm_pmu_request_counter_reprogram(pmc);
61 	}
62 }
63 
64 static struct kvm_pmc *intel_pmc_idx_to_pmc(struct kvm_pmu *pmu, int pmc_idx)
65 {
66 	if (pmc_idx < INTEL_PMC_IDX_FIXED) {
67 		return get_gp_pmc(pmu, MSR_P6_EVNTSEL0 + pmc_idx,
68 				  MSR_P6_EVNTSEL0);
69 	} else {
70 		u32 idx = pmc_idx - INTEL_PMC_IDX_FIXED;
71 
72 		return get_fixed_pmc(pmu, idx + MSR_CORE_PERF_FIXED_CTR0);
73 	}
74 }
75 
76 static void reprogram_counters(struct kvm_pmu *pmu, u64 diff)
77 {
78 	int bit;
79 
80 	if (!diff)
81 		return;
82 
83 	for_each_set_bit(bit, (unsigned long *)&diff, X86_PMC_IDX_MAX)
84 		set_bit(bit, pmu->reprogram_pmi);
85 	kvm_make_request(KVM_REQ_PMU, pmu_to_vcpu(pmu));
86 }
87 
88 static bool intel_hw_event_available(struct kvm_pmc *pmc)
89 {
90 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
91 	u8 event_select = pmc->eventsel & ARCH_PERFMON_EVENTSEL_EVENT;
92 	u8 unit_mask = (pmc->eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
93 	int i;
94 
95 	for (i = 0; i < ARRAY_SIZE(intel_arch_events); i++) {
96 		if (intel_arch_events[i].eventsel != event_select ||
97 		    intel_arch_events[i].unit_mask != unit_mask)
98 			continue;
99 
100 		/* disable event that reported as not present by cpuid */
101 		if ((i < 7) && !(pmu->available_event_types & (1 << i)))
102 			return false;
103 
104 		break;
105 	}
106 
107 	return true;
108 }
109 
110 /* check if a PMC is enabled by comparing it with globl_ctrl bits. */
111 static bool intel_pmc_is_enabled(struct kvm_pmc *pmc)
112 {
113 	struct kvm_pmu *pmu = pmc_to_pmu(pmc);
114 
115 	if (!intel_pmu_has_perf_global_ctrl(pmu))
116 		return true;
117 
118 	return test_bit(pmc->idx, (unsigned long *)&pmu->global_ctrl);
119 }
120 
121 static bool intel_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
122 {
123 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
124 	bool fixed = idx & (1u << 30);
125 
126 	idx &= ~(3u << 30);
127 
128 	return fixed ? idx < pmu->nr_arch_fixed_counters
129 		     : idx < pmu->nr_arch_gp_counters;
130 }
131 
132 static struct kvm_pmc *intel_rdpmc_ecx_to_pmc(struct kvm_vcpu *vcpu,
133 					    unsigned int idx, u64 *mask)
134 {
135 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
136 	bool fixed = idx & (1u << 30);
137 	struct kvm_pmc *counters;
138 	unsigned int num_counters;
139 
140 	idx &= ~(3u << 30);
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)
149 		return NULL;
150 	*mask &= pmu->counter_bitmask[fixed ? KVM_PMC_FIXED : KVM_PMC_GP];
151 	return &counters[array_index_nospec(idx, num_counters)];
152 }
153 
154 static inline u64 vcpu_get_perf_capabilities(struct kvm_vcpu *vcpu)
155 {
156 	if (!guest_cpuid_has(vcpu, X86_FEATURE_PDCM))
157 		return 0;
158 
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;
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 
175 static bool intel_pmu_is_valid_lbr_msr(struct kvm_vcpu *vcpu, u32 index)
176 {
177 	struct x86_pmu_lbr *records = vcpu_to_lbr_records(vcpu);
178 	bool ret = false;
179 
180 	if (!intel_pmu_lbr_is_enabled(vcpu))
181 		return ret;
182 
183 	ret = (index == MSR_LBR_SELECT) || (index == MSR_LBR_TOS) ||
184 		(index >= records->from && index < records->from + records->nr) ||
185 		(index >= records->to && index < records->to + records->nr);
186 
187 	if (!ret && records->info)
188 		ret = (index >= records->info && index < records->info + records->nr);
189 
190 	return ret;
191 }
192 
193 static bool intel_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
194 {
195 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
196 	u64 perf_capabilities;
197 	int ret;
198 
199 	switch (msr) {
200 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
201 	case MSR_CORE_PERF_GLOBAL_STATUS:
202 	case MSR_CORE_PERF_GLOBAL_CTRL:
203 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
204 		return intel_pmu_has_perf_global_ctrl(pmu);
205 		break;
206 	case MSR_IA32_PEBS_ENABLE:
207 		ret = vcpu_get_perf_capabilities(vcpu) & PERF_CAP_PEBS_FORMAT;
208 		break;
209 	case MSR_IA32_DS_AREA:
210 		ret = guest_cpuid_has(vcpu, X86_FEATURE_DS);
211 		break;
212 	case MSR_PEBS_DATA_CFG:
213 		perf_capabilities = vcpu_get_perf_capabilities(vcpu);
214 		ret = (perf_capabilities & PERF_CAP_PEBS_BASELINE) &&
215 			((perf_capabilities & PERF_CAP_PEBS_FORMAT) > 3);
216 		break;
217 	default:
218 		ret = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0) ||
219 			get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0) ||
220 			get_fixed_pmc(pmu, msr) || get_fw_gp_pmc(pmu, msr) ||
221 			intel_pmu_is_valid_lbr_msr(vcpu, msr);
222 		break;
223 	}
224 
225 	return ret;
226 }
227 
228 static struct kvm_pmc *intel_msr_idx_to_pmc(struct kvm_vcpu *vcpu, u32 msr)
229 {
230 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
231 	struct kvm_pmc *pmc;
232 
233 	pmc = get_fixed_pmc(pmu, msr);
234 	pmc = pmc ? pmc : get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0);
235 	pmc = pmc ? pmc : get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0);
236 
237 	return pmc;
238 }
239 
240 static inline void intel_pmu_release_guest_lbr_event(struct kvm_vcpu *vcpu)
241 {
242 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
243 
244 	if (lbr_desc->event) {
245 		perf_event_release_kernel(lbr_desc->event);
246 		lbr_desc->event = NULL;
247 		vcpu_to_pmu(vcpu)->event_count--;
248 	}
249 }
250 
251 int intel_pmu_create_guest_lbr_event(struct kvm_vcpu *vcpu)
252 {
253 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
254 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
255 	struct perf_event *event;
256 
257 	/*
258 	 * The perf_event_attr is constructed in the minimum efficient way:
259 	 * - set 'pinned = true' to make it task pinned so that if another
260 	 *   cpu pinned event reclaims LBR, the event->oncpu will be set to -1;
261 	 * - set '.exclude_host = true' to record guest branches behavior;
262 	 *
263 	 * - set '.config = INTEL_FIXED_VLBR_EVENT' to indicates host perf
264 	 *   schedule the event without a real HW counter but a fake one;
265 	 *   check is_guest_lbr_event() and __intel_get_event_constraints();
266 	 *
267 	 * - set 'sample_type = PERF_SAMPLE_BRANCH_STACK' and
268 	 *   'branch_sample_type = PERF_SAMPLE_BRANCH_CALL_STACK |
269 	 *   PERF_SAMPLE_BRANCH_USER' to configure it as a LBR callstack
270 	 *   event, which helps KVM to save/restore guest LBR records
271 	 *   during host context switches and reduces quite a lot overhead,
272 	 *   check branch_user_callstack() and intel_pmu_lbr_sched_task();
273 	 */
274 	struct perf_event_attr attr = {
275 		.type = PERF_TYPE_RAW,
276 		.size = sizeof(attr),
277 		.config = INTEL_FIXED_VLBR_EVENT,
278 		.sample_type = PERF_SAMPLE_BRANCH_STACK,
279 		.pinned = true,
280 		.exclude_host = true,
281 		.branch_sample_type = PERF_SAMPLE_BRANCH_CALL_STACK |
282 					PERF_SAMPLE_BRANCH_USER,
283 	};
284 
285 	if (unlikely(lbr_desc->event)) {
286 		__set_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use);
287 		return 0;
288 	}
289 
290 	event = perf_event_create_kernel_counter(&attr, -1,
291 						current, NULL, NULL);
292 	if (IS_ERR(event)) {
293 		pr_debug_ratelimited("%s: failed %ld\n",
294 					__func__, PTR_ERR(event));
295 		return PTR_ERR(event);
296 	}
297 	lbr_desc->event = event;
298 	pmu->event_count++;
299 	__set_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use);
300 	return 0;
301 }
302 
303 /*
304  * It's safe to access LBR msrs from guest when they have not
305  * been passthrough since the host would help restore or reset
306  * the LBR msrs records when the guest LBR event is scheduled in.
307  */
308 static bool intel_pmu_handle_lbr_msrs_access(struct kvm_vcpu *vcpu,
309 				     struct msr_data *msr_info, bool read)
310 {
311 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
312 	u32 index = msr_info->index;
313 
314 	if (!intel_pmu_is_valid_lbr_msr(vcpu, index))
315 		return false;
316 
317 	if (!lbr_desc->event && intel_pmu_create_guest_lbr_event(vcpu) < 0)
318 		goto dummy;
319 
320 	/*
321 	 * Disable irq to ensure the LBR feature doesn't get reclaimed by the
322 	 * host at the time the value is read from the msr, and this avoids the
323 	 * host LBR value to be leaked to the guest. If LBR has been reclaimed,
324 	 * return 0 on guest reads.
325 	 */
326 	local_irq_disable();
327 	if (lbr_desc->event->state == PERF_EVENT_STATE_ACTIVE) {
328 		if (read)
329 			rdmsrl(index, msr_info->data);
330 		else
331 			wrmsrl(index, msr_info->data);
332 		__set_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use);
333 		local_irq_enable();
334 		return true;
335 	}
336 	clear_bit(INTEL_PMC_IDX_FIXED_VLBR, vcpu_to_pmu(vcpu)->pmc_in_use);
337 	local_irq_enable();
338 
339 dummy:
340 	if (read)
341 		msr_info->data = 0;
342 	return true;
343 }
344 
345 static int intel_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
346 {
347 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
348 	struct kvm_pmc *pmc;
349 	u32 msr = msr_info->index;
350 
351 	switch (msr) {
352 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
353 		msr_info->data = pmu->fixed_ctr_ctrl;
354 		break;
355 	case MSR_CORE_PERF_GLOBAL_STATUS:
356 		msr_info->data = pmu->global_status;
357 		break;
358 	case MSR_CORE_PERF_GLOBAL_CTRL:
359 		msr_info->data = pmu->global_ctrl;
360 		break;
361 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
362 		msr_info->data = 0;
363 		break;
364 	case MSR_IA32_PEBS_ENABLE:
365 		msr_info->data = pmu->pebs_enable;
366 		break;
367 	case MSR_IA32_DS_AREA:
368 		msr_info->data = pmu->ds_area;
369 		break;
370 	case MSR_PEBS_DATA_CFG:
371 		msr_info->data = pmu->pebs_data_cfg;
372 		break;
373 	default:
374 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
375 		    (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
376 			u64 val = pmc_read_counter(pmc);
377 			msr_info->data =
378 				val & pmu->counter_bitmask[KVM_PMC_GP];
379 			break;
380 		} else if ((pmc = get_fixed_pmc(pmu, msr))) {
381 			u64 val = pmc_read_counter(pmc);
382 			msr_info->data =
383 				val & pmu->counter_bitmask[KVM_PMC_FIXED];
384 			break;
385 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
386 			msr_info->data = pmc->eventsel;
387 			break;
388 		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, true)) {
389 			break;
390 		}
391 		return 1;
392 	}
393 
394 	return 0;
395 }
396 
397 static int intel_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
398 {
399 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
400 	struct kvm_pmc *pmc;
401 	u32 msr = msr_info->index;
402 	u64 data = msr_info->data;
403 	u64 reserved_bits, diff;
404 
405 	switch (msr) {
406 	case MSR_CORE_PERF_FIXED_CTR_CTRL:
407 		if (data & pmu->fixed_ctr_ctrl_mask)
408 			return 1;
409 
410 		if (pmu->fixed_ctr_ctrl != data)
411 			reprogram_fixed_counters(pmu, data);
412 		break;
413 	case MSR_CORE_PERF_GLOBAL_STATUS:
414 		if (!msr_info->host_initiated)
415 			return 1; /* RO MSR */
416 
417 		pmu->global_status = data;
418 		break;
419 	case MSR_CORE_PERF_GLOBAL_CTRL:
420 		if (!kvm_valid_perf_global_ctrl(pmu, data))
421 			return 1;
422 
423 		if (pmu->global_ctrl != data) {
424 			diff = pmu->global_ctrl ^ data;
425 			pmu->global_ctrl = data;
426 			reprogram_counters(pmu, diff);
427 		}
428 		break;
429 	case MSR_CORE_PERF_GLOBAL_OVF_CTRL:
430 		if (data & pmu->global_ovf_ctrl_mask)
431 			return 1;
432 
433 		if (!msr_info->host_initiated)
434 			pmu->global_status &= ~data;
435 		break;
436 	case MSR_IA32_PEBS_ENABLE:
437 		if (data & pmu->pebs_enable_mask)
438 			return 1;
439 
440 		if (pmu->pebs_enable != data) {
441 			diff = pmu->pebs_enable ^ data;
442 			pmu->pebs_enable = data;
443 			reprogram_counters(pmu, diff);
444 		}
445 		break;
446 	case MSR_IA32_DS_AREA:
447 		if (msr_info->host_initiated && data && !guest_cpuid_has(vcpu, X86_FEATURE_DS))
448 			return 1;
449 		if (is_noncanonical_address(data, vcpu))
450 			return 1;
451 
452 		pmu->ds_area = data;
453 		break;
454 	case MSR_PEBS_DATA_CFG:
455 		if (data & pmu->pebs_data_cfg_mask)
456 			return 1;
457 
458 		pmu->pebs_data_cfg = data;
459 		break;
460 	default:
461 		if ((pmc = get_gp_pmc(pmu, msr, MSR_IA32_PERFCTR0)) ||
462 		    (pmc = get_gp_pmc(pmu, msr, MSR_IA32_PMC0))) {
463 			if ((msr & MSR_PMC_FULL_WIDTH_BIT) &&
464 			    (data & ~pmu->counter_bitmask[KVM_PMC_GP]))
465 				return 1;
466 
467 			if (!msr_info->host_initiated &&
468 			    !(msr & MSR_PMC_FULL_WIDTH_BIT))
469 				data = (s64)(s32)data;
470 			pmc->counter += data - pmc_read_counter(pmc);
471 			pmc_update_sample_period(pmc);
472 			break;
473 		} else if ((pmc = get_fixed_pmc(pmu, msr))) {
474 			pmc->counter += data - pmc_read_counter(pmc);
475 			pmc_update_sample_period(pmc);
476 			break;
477 		} else if ((pmc = get_gp_pmc(pmu, msr, MSR_P6_EVNTSEL0))) {
478 			reserved_bits = pmu->reserved_bits;
479 			if ((pmc->idx == 2) &&
480 			    (pmu->raw_event_mask & HSW_IN_TX_CHECKPOINTED))
481 				reserved_bits ^= HSW_IN_TX_CHECKPOINTED;
482 			if (data & reserved_bits)
483 				return 1;
484 
485 			if (data != pmc->eventsel) {
486 				pmc->eventsel = data;
487 				kvm_pmu_request_counter_reprogram(pmc);
488 			}
489 			break;
490 		} else if (intel_pmu_handle_lbr_msrs_access(vcpu, msr_info, false)) {
491 			break;
492 		}
493 		/* Not a known PMU MSR. */
494 		return 1;
495 	}
496 
497 	return 0;
498 }
499 
500 static void setup_fixed_pmc_eventsel(struct kvm_pmu *pmu)
501 {
502 	size_t size = ARRAY_SIZE(fixed_pmc_events);
503 	struct kvm_pmc *pmc;
504 	u32 event;
505 	int i;
506 
507 	for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
508 		pmc = &pmu->fixed_counters[i];
509 		event = fixed_pmc_events[array_index_nospec(i, size)];
510 		pmc->eventsel = (intel_arch_events[event].unit_mask << 8) |
511 			intel_arch_events[event].eventsel;
512 	}
513 }
514 
515 static void intel_pmu_refresh(struct kvm_vcpu *vcpu)
516 {
517 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
518 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
519 	struct kvm_cpuid_entry2 *entry;
520 	union cpuid10_eax eax;
521 	union cpuid10_edx edx;
522 	u64 perf_capabilities;
523 	u64 counter_mask;
524 	int i;
525 
526 	pmu->nr_arch_gp_counters = 0;
527 	pmu->nr_arch_fixed_counters = 0;
528 	pmu->counter_bitmask[KVM_PMC_GP] = 0;
529 	pmu->counter_bitmask[KVM_PMC_FIXED] = 0;
530 	pmu->version = 0;
531 	pmu->reserved_bits = 0xffffffff00200000ull;
532 	pmu->raw_event_mask = X86_RAW_EVENT_MASK;
533 	pmu->global_ctrl_mask = ~0ull;
534 	pmu->global_ovf_ctrl_mask = ~0ull;
535 	pmu->fixed_ctr_ctrl_mask = ~0ull;
536 	pmu->pebs_enable_mask = ~0ull;
537 	pmu->pebs_data_cfg_mask = ~0ull;
538 
539 	memset(&lbr_desc->records, 0, sizeof(lbr_desc->records));
540 
541 	/*
542 	 * Setting passthrough of LBR MSRs is done only in the VM-Entry loop,
543 	 * and PMU refresh is disallowed after the vCPU has run, i.e. this code
544 	 * should never be reached while KVM is passing through MSRs.
545 	 */
546 	if (KVM_BUG_ON(lbr_desc->msr_passthrough, vcpu->kvm))
547 		return;
548 
549 	entry = kvm_find_cpuid_entry(vcpu, 0xa);
550 	if (!entry || !vcpu->kvm->arch.enable_pmu)
551 		return;
552 	eax.full = entry->eax;
553 	edx.full = entry->edx;
554 
555 	pmu->version = eax.split.version_id;
556 	if (!pmu->version)
557 		return;
558 
559 	pmu->nr_arch_gp_counters = min_t(int, eax.split.num_counters,
560 					 kvm_pmu_cap.num_counters_gp);
561 	eax.split.bit_width = min_t(int, eax.split.bit_width,
562 				    kvm_pmu_cap.bit_width_gp);
563 	pmu->counter_bitmask[KVM_PMC_GP] = ((u64)1 << eax.split.bit_width) - 1;
564 	eax.split.mask_length = min_t(int, eax.split.mask_length,
565 				      kvm_pmu_cap.events_mask_len);
566 	pmu->available_event_types = ~entry->ebx &
567 					((1ull << eax.split.mask_length) - 1);
568 
569 	if (pmu->version == 1) {
570 		pmu->nr_arch_fixed_counters = 0;
571 	} else {
572 		pmu->nr_arch_fixed_counters =
573 			min3(ARRAY_SIZE(fixed_pmc_events),
574 			     (size_t) edx.split.num_counters_fixed,
575 			     (size_t)kvm_pmu_cap.num_counters_fixed);
576 		edx.split.bit_width_fixed = min_t(int, edx.split.bit_width_fixed,
577 						  kvm_pmu_cap.bit_width_fixed);
578 		pmu->counter_bitmask[KVM_PMC_FIXED] =
579 			((u64)1 << edx.split.bit_width_fixed) - 1;
580 		setup_fixed_pmc_eventsel(pmu);
581 	}
582 
583 	for (i = 0; i < pmu->nr_arch_fixed_counters; i++)
584 		pmu->fixed_ctr_ctrl_mask &= ~(0xbull << (i * 4));
585 	counter_mask = ~(((1ull << pmu->nr_arch_gp_counters) - 1) |
586 		(((1ull << pmu->nr_arch_fixed_counters) - 1) << INTEL_PMC_IDX_FIXED));
587 	pmu->global_ctrl_mask = counter_mask;
588 	pmu->global_ovf_ctrl_mask = pmu->global_ctrl_mask
589 			& ~(MSR_CORE_PERF_GLOBAL_OVF_CTRL_OVF_BUF |
590 			    MSR_CORE_PERF_GLOBAL_OVF_CTRL_COND_CHGD);
591 	if (vmx_pt_mode_is_host_guest())
592 		pmu->global_ovf_ctrl_mask &=
593 				~MSR_CORE_PERF_GLOBAL_OVF_CTRL_TRACE_TOPA_PMI;
594 
595 	entry = kvm_find_cpuid_entry_index(vcpu, 7, 0);
596 	if (entry &&
597 	    (boot_cpu_has(X86_FEATURE_HLE) || boot_cpu_has(X86_FEATURE_RTM)) &&
598 	    (entry->ebx & (X86_FEATURE_HLE|X86_FEATURE_RTM))) {
599 		pmu->reserved_bits ^= HSW_IN_TX;
600 		pmu->raw_event_mask |= (HSW_IN_TX|HSW_IN_TX_CHECKPOINTED);
601 	}
602 
603 	bitmap_set(pmu->all_valid_pmc_idx,
604 		0, pmu->nr_arch_gp_counters);
605 	bitmap_set(pmu->all_valid_pmc_idx,
606 		INTEL_PMC_MAX_GENERIC, pmu->nr_arch_fixed_counters);
607 
608 	perf_capabilities = vcpu_get_perf_capabilities(vcpu);
609 	if (cpuid_model_is_consistent(vcpu) &&
610 	    (perf_capabilities & PMU_CAP_LBR_FMT))
611 		x86_perf_get_lbr(&lbr_desc->records);
612 	else
613 		lbr_desc->records.nr = 0;
614 
615 	if (lbr_desc->records.nr)
616 		bitmap_set(pmu->all_valid_pmc_idx, INTEL_PMC_IDX_FIXED_VLBR, 1);
617 
618 	if (perf_capabilities & PERF_CAP_PEBS_FORMAT) {
619 		if (perf_capabilities & PERF_CAP_PEBS_BASELINE) {
620 			pmu->pebs_enable_mask = counter_mask;
621 			pmu->reserved_bits &= ~ICL_EVENTSEL_ADAPTIVE;
622 			for (i = 0; i < pmu->nr_arch_fixed_counters; i++) {
623 				pmu->fixed_ctr_ctrl_mask &=
624 					~(1ULL << (INTEL_PMC_IDX_FIXED + i * 4));
625 			}
626 			pmu->pebs_data_cfg_mask = ~0xff00000full;
627 		} else {
628 			pmu->pebs_enable_mask =
629 				~((1ull << pmu->nr_arch_gp_counters) - 1);
630 		}
631 	}
632 }
633 
634 static void intel_pmu_init(struct kvm_vcpu *vcpu)
635 {
636 	int i;
637 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
638 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
639 
640 	for (i = 0; i < KVM_INTEL_PMC_MAX_GENERIC; i++) {
641 		pmu->gp_counters[i].type = KVM_PMC_GP;
642 		pmu->gp_counters[i].vcpu = vcpu;
643 		pmu->gp_counters[i].idx = i;
644 		pmu->gp_counters[i].current_config = 0;
645 	}
646 
647 	for (i = 0; i < KVM_PMC_MAX_FIXED; i++) {
648 		pmu->fixed_counters[i].type = KVM_PMC_FIXED;
649 		pmu->fixed_counters[i].vcpu = vcpu;
650 		pmu->fixed_counters[i].idx = i + INTEL_PMC_IDX_FIXED;
651 		pmu->fixed_counters[i].current_config = 0;
652 	}
653 
654 	lbr_desc->records.nr = 0;
655 	lbr_desc->event = NULL;
656 	lbr_desc->msr_passthrough = false;
657 }
658 
659 static void intel_pmu_reset(struct kvm_vcpu *vcpu)
660 {
661 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
662 	struct kvm_pmc *pmc = NULL;
663 	int i;
664 
665 	for (i = 0; i < KVM_INTEL_PMC_MAX_GENERIC; i++) {
666 		pmc = &pmu->gp_counters[i];
667 
668 		pmc_stop_counter(pmc);
669 		pmc->counter = pmc->prev_counter = pmc->eventsel = 0;
670 	}
671 
672 	for (i = 0; i < KVM_PMC_MAX_FIXED; i++) {
673 		pmc = &pmu->fixed_counters[i];
674 
675 		pmc_stop_counter(pmc);
676 		pmc->counter = pmc->prev_counter = 0;
677 	}
678 
679 	pmu->fixed_ctr_ctrl = pmu->global_ctrl = pmu->global_status = 0;
680 
681 	intel_pmu_release_guest_lbr_event(vcpu);
682 }
683 
684 /*
685  * Emulate LBR_On_PMI behavior for 1 < pmu.version < 4.
686  *
687  * If Freeze_LBR_On_PMI = 1, the LBR is frozen on PMI and
688  * the KVM emulates to clear the LBR bit (bit 0) in IA32_DEBUGCTL.
689  *
690  * Guest needs to re-enable LBR to resume branches recording.
691  */
692 static void intel_pmu_legacy_freezing_lbrs_on_pmi(struct kvm_vcpu *vcpu)
693 {
694 	u64 data = vmcs_read64(GUEST_IA32_DEBUGCTL);
695 
696 	if (data & DEBUGCTLMSR_FREEZE_LBRS_ON_PMI) {
697 		data &= ~DEBUGCTLMSR_LBR;
698 		vmcs_write64(GUEST_IA32_DEBUGCTL, data);
699 	}
700 }
701 
702 static void intel_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
703 {
704 	u8 version = vcpu_to_pmu(vcpu)->version;
705 
706 	if (!intel_pmu_lbr_is_enabled(vcpu))
707 		return;
708 
709 	if (version > 1 && version < 4)
710 		intel_pmu_legacy_freezing_lbrs_on_pmi(vcpu);
711 }
712 
713 static void vmx_update_intercept_for_lbr_msrs(struct kvm_vcpu *vcpu, bool set)
714 {
715 	struct x86_pmu_lbr *lbr = vcpu_to_lbr_records(vcpu);
716 	int i;
717 
718 	for (i = 0; i < lbr->nr; i++) {
719 		vmx_set_intercept_for_msr(vcpu, lbr->from + i, MSR_TYPE_RW, set);
720 		vmx_set_intercept_for_msr(vcpu, lbr->to + i, MSR_TYPE_RW, set);
721 		if (lbr->info)
722 			vmx_set_intercept_for_msr(vcpu, lbr->info + i, MSR_TYPE_RW, set);
723 	}
724 
725 	vmx_set_intercept_for_msr(vcpu, MSR_LBR_SELECT, MSR_TYPE_RW, set);
726 	vmx_set_intercept_for_msr(vcpu, MSR_LBR_TOS, MSR_TYPE_RW, set);
727 }
728 
729 static inline void vmx_disable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu)
730 {
731 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
732 
733 	if (!lbr_desc->msr_passthrough)
734 		return;
735 
736 	vmx_update_intercept_for_lbr_msrs(vcpu, true);
737 	lbr_desc->msr_passthrough = false;
738 }
739 
740 static inline void vmx_enable_lbr_msrs_passthrough(struct kvm_vcpu *vcpu)
741 {
742 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
743 
744 	if (lbr_desc->msr_passthrough)
745 		return;
746 
747 	vmx_update_intercept_for_lbr_msrs(vcpu, false);
748 	lbr_desc->msr_passthrough = true;
749 }
750 
751 /*
752  * Higher priority host perf events (e.g. cpu pinned) could reclaim the
753  * pmu resources (e.g. LBR) that were assigned to the guest. This is
754  * usually done via ipi calls (more details in perf_install_in_context).
755  *
756  * Before entering the non-root mode (with irq disabled here), double
757  * confirm that the pmu features enabled to the guest are not reclaimed
758  * by higher priority host events. Otherwise, disallow vcpu's access to
759  * the reclaimed features.
760  */
761 void vmx_passthrough_lbr_msrs(struct kvm_vcpu *vcpu)
762 {
763 	struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
764 	struct lbr_desc *lbr_desc = vcpu_to_lbr_desc(vcpu);
765 
766 	if (!lbr_desc->event) {
767 		vmx_disable_lbr_msrs_passthrough(vcpu);
768 		if (vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR)
769 			goto warn;
770 		if (test_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use))
771 			goto warn;
772 		return;
773 	}
774 
775 	if (lbr_desc->event->state < PERF_EVENT_STATE_ACTIVE) {
776 		vmx_disable_lbr_msrs_passthrough(vcpu);
777 		__clear_bit(INTEL_PMC_IDX_FIXED_VLBR, pmu->pmc_in_use);
778 		goto warn;
779 	} else
780 		vmx_enable_lbr_msrs_passthrough(vcpu);
781 
782 	return;
783 
784 warn:
785 	pr_warn_ratelimited("vcpu-%d: fail to passthrough LBR.\n", vcpu->vcpu_id);
786 }
787 
788 static void intel_pmu_cleanup(struct kvm_vcpu *vcpu)
789 {
790 	if (!(vmcs_read64(GUEST_IA32_DEBUGCTL) & DEBUGCTLMSR_LBR))
791 		intel_pmu_release_guest_lbr_event(vcpu);
792 }
793 
794 void intel_pmu_cross_mapped_check(struct kvm_pmu *pmu)
795 {
796 	struct kvm_pmc *pmc = NULL;
797 	int bit, hw_idx;
798 
799 	for_each_set_bit(bit, (unsigned long *)&pmu->global_ctrl,
800 			 X86_PMC_IDX_MAX) {
801 		pmc = intel_pmc_idx_to_pmc(pmu, bit);
802 
803 		if (!pmc || !pmc_speculative_in_use(pmc) ||
804 		    !intel_pmc_is_enabled(pmc) || !pmc->perf_event)
805 			continue;
806 
807 		/*
808 		 * A negative index indicates the event isn't mapped to a
809 		 * physical counter in the host, e.g. due to contention.
810 		 */
811 		hw_idx = pmc->perf_event->hw.idx;
812 		if (hw_idx != pmc->idx && hw_idx > -1)
813 			pmu->host_cross_mapped_mask |= BIT_ULL(hw_idx);
814 	}
815 }
816 
817 struct kvm_pmu_ops intel_pmu_ops __initdata = {
818 	.hw_event_available = intel_hw_event_available,
819 	.pmc_is_enabled = intel_pmc_is_enabled,
820 	.pmc_idx_to_pmc = intel_pmc_idx_to_pmc,
821 	.rdpmc_ecx_to_pmc = intel_rdpmc_ecx_to_pmc,
822 	.msr_idx_to_pmc = intel_msr_idx_to_pmc,
823 	.is_valid_rdpmc_ecx = intel_is_valid_rdpmc_ecx,
824 	.is_valid_msr = intel_is_valid_msr,
825 	.get_msr = intel_pmu_get_msr,
826 	.set_msr = intel_pmu_set_msr,
827 	.refresh = intel_pmu_refresh,
828 	.init = intel_pmu_init,
829 	.reset = intel_pmu_reset,
830 	.deliver_pmi = intel_pmu_deliver_pmi,
831 	.cleanup = intel_pmu_cleanup,
832 	.EVENTSEL_EVENT = ARCH_PERFMON_EVENTSEL_EVENT,
833 	.MAX_NR_GP_COUNTERS = KVM_INTEL_PMC_MAX_GENERIC,
834 };
835