blob: 0772bad9165c55b09c805caee6287de9df1257bb [file] [log] [blame]
Thomas Gleixner20c8ccb2019-06-04 10:11:32 +02001// SPDX-License-Identifier: GPL-2.0-only
Gleb Natapovf5132b02011-11-10 14:57:22 +02002/*
Guo Chaoc7a70622012-06-28 15:23:08 +08003 * Kernel-based Virtual Machine -- Performance Monitoring Unit support
Gleb Natapovf5132b02011-11-10 14:57:22 +02004 *
Wei Huang25462f72015-06-19 15:45:05 +02005 * Copyright 2015 Red Hat, Inc. and/or its affiliates.
Gleb Natapovf5132b02011-11-10 14:57:22 +02006 *
7 * Authors:
8 * Avi Kivity <avi@redhat.com>
9 * Gleb Natapov <gleb@redhat.com>
Wei Huang25462f72015-06-19 15:45:05 +020010 * Wei Huang <wei@redhat.com>
Gleb Natapovf5132b02011-11-10 14:57:22 +020011 */
12
13#include <linux/types.h>
14#include <linux/kvm_host.h>
15#include <linux/perf_event.h>
Nadav Amitd27aa7f2014-08-20 13:25:52 +030016#include <asm/perf_event.h>
Gleb Natapovf5132b02011-11-10 14:57:22 +020017#include "x86.h"
18#include "cpuid.h"
19#include "lapic.h"
Wei Huang474a5bb2015-06-19 13:54:23 +020020#include "pmu.h"
Gleb Natapovf5132b02011-11-10 14:57:22 +020021
Eric Hankland30cd8602019-07-18 11:38:18 -070022/* This is enough to filter the vast majority of currently defined events. */
23#define KVM_PMU_EVENT_FILTER_MAX_EVENTS 300
Eric Hankland66bb8a02019-07-10 18:25:15 -070024
Wei Huang25462f72015-06-19 15:45:05 +020025/* NOTE:
26 * - Each perf counter is defined as "struct kvm_pmc";
27 * - There are two types of perf counters: general purpose (gp) and fixed.
28 * gp counters are stored in gp_counters[] and fixed counters are stored
29 * in fixed_counters[] respectively. Both of them are part of "struct
30 * kvm_pmu";
31 * - pmu.c understands the difference between gp counters and fixed counters.
32 * However AMD doesn't support fixed-counters;
33 * - There are three types of index to access perf counters (PMC):
34 * 1. MSR (named msr): For example Intel has MSR_IA32_PERFCTRn and AMD
35 * has MSR_K7_PERFCTRn.
36 * 2. MSR Index (named idx): This normally is used by RDPMC instruction.
37 * For instance AMD RDPMC instruction uses 0000_0003h in ECX to access
38 * C001_0007h (MSR_K7_PERCTR3). Intel has a similar mechanism, except
39 * that it also supports fixed counters. idx can be used to as index to
40 * gp and fixed counters.
41 * 3. Global PMC Index (named pmc): pmc is an index specific to PMU
42 * code. Each pmc, stored in kvm_pmc.idx field, is unique across
43 * all perf counters (both gp and fixed). The mapping relationship
44 * between pmc and perf counters is as the following:
45 * * Intel: [0 .. INTEL_PMC_MAX_GENERIC-1] <=> gp counters
46 * [INTEL_PMC_IDX_FIXED .. INTEL_PMC_IDX_FIXED + 2] <=> fixed
47 * * AMD: [0 .. AMD64_NUM_COUNTERS-1] <=> gp counters
48 */
Gleb Natapovf5132b02011-11-10 14:57:22 +020049
Wei Huangc6702c92015-06-19 13:44:45 +020050static void kvm_pmi_trigger_fn(struct irq_work *irq_work)
Gleb Natapovf5132b02011-11-10 14:57:22 +020051{
Wei Huang212dba12015-06-19 14:00:33 +020052 struct kvm_pmu *pmu = container_of(irq_work, struct kvm_pmu, irq_work);
53 struct kvm_vcpu *vcpu = pmu_to_vcpu(pmu);
Gleb Natapovf5132b02011-11-10 14:57:22 +020054
Wei Huangc6702c92015-06-19 13:44:45 +020055 kvm_pmu_deliver_pmi(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +020056}
57
58static void kvm_perf_overflow(struct perf_event *perf_event,
59 struct perf_sample_data *data,
60 struct pt_regs *regs)
61{
62 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
Wei Huang212dba12015-06-19 14:00:33 +020063 struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Wei Huange84cfe42015-06-19 14:15:28 +020064
Like Xu4be94672019-10-21 18:55:04 +080065 if (!test_and_set_bit(pmc->idx, pmu->reprogram_pmi)) {
Nadav Amit671bd992014-04-18 03:35:08 +030066 __set_bit(pmc->idx, (unsigned long *)&pmu->global_status);
67 kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
68 }
Gleb Natapovf5132b02011-11-10 14:57:22 +020069}
70
71static void kvm_perf_overflow_intr(struct perf_event *perf_event,
Wei Huange84cfe42015-06-19 14:15:28 +020072 struct perf_sample_data *data,
73 struct pt_regs *regs)
Gleb Natapovf5132b02011-11-10 14:57:22 +020074{
75 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
Wei Huang212dba12015-06-19 14:00:33 +020076 struct kvm_pmu *pmu = pmc_to_pmu(pmc);
Wei Huange84cfe42015-06-19 14:15:28 +020077
Like Xu4be94672019-10-21 18:55:04 +080078 if (!test_and_set_bit(pmc->idx, pmu->reprogram_pmi)) {
Nadav Amit671bd992014-04-18 03:35:08 +030079 __set_bit(pmc->idx, (unsigned long *)&pmu->global_status);
Gleb Natapovf5132b02011-11-10 14:57:22 +020080 kvm_make_request(KVM_REQ_PMU, pmc->vcpu);
Wei Huange84cfe42015-06-19 14:15:28 +020081
Gleb Natapovf5132b02011-11-10 14:57:22 +020082 /*
83 * Inject PMI. If vcpu was in a guest mode during NMI PMI
84 * can be ejected on a guest mode re-entry. Otherwise we can't
85 * be sure that vcpu wasn't executing hlt instruction at the
Wei Huange84cfe42015-06-19 14:15:28 +020086 * time of vmexit and is not going to re-enter guest mode until
Gleb Natapovf5132b02011-11-10 14:57:22 +020087 * woken up. So we should wake it, but this is impossible from
88 * NMI context. Do it from irq work instead.
89 */
90 if (!kvm_is_in_guest())
Wei Huang212dba12015-06-19 14:00:33 +020091 irq_work_queue(&pmc_to_pmu(pmc)->irq_work);
Gleb Natapovf5132b02011-11-10 14:57:22 +020092 else
93 kvm_make_request(KVM_REQ_PMI, pmc->vcpu);
94 }
95}
96
Wei Huangc6702c92015-06-19 13:44:45 +020097static void pmc_reprogram_counter(struct kvm_pmc *pmc, u32 type,
Wei Huange84cfe42015-06-19 14:15:28 +020098 unsigned config, bool exclude_user,
99 bool exclude_kernel, bool intr,
100 bool in_tx, bool in_tx_cp)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200101{
102 struct perf_event *event;
103 struct perf_event_attr attr = {
104 .type = type,
105 .size = sizeof(attr),
106 .pinned = true,
107 .exclude_idle = true,
108 .exclude_host = 1,
109 .exclude_user = exclude_user,
110 .exclude_kernel = exclude_kernel,
111 .config = config,
112 };
Wei Huange84cfe42015-06-19 14:15:28 +0200113
Eric Hankland168d9182020-02-21 18:34:13 -0800114 attr.sample_period = get_sample_period(pmc, pmc->counter);
Robert O'Callahanbba82fd2017-02-01 17:06:11 +1300115
Andi Kleen103af0a2013-07-18 15:57:02 -0700116 if (in_tx)
117 attr.config |= HSW_IN_TX;
Robert O'Callahanbba82fd2017-02-01 17:06:11 +1300118 if (in_tx_cp) {
119 /*
120 * HSW_IN_TX_CHECKPOINTED is not supported with nonzero
121 * period. Just clear the sample period so at least
122 * allocating the counter doesn't fail.
123 */
124 attr.sample_period = 0;
Andi Kleen103af0a2013-07-18 15:57:02 -0700125 attr.config |= HSW_IN_TX_CHECKPOINTED;
Robert O'Callahanbba82fd2017-02-01 17:06:11 +1300126 }
Gleb Natapovf5132b02011-11-10 14:57:22 +0200127
128 event = perf_event_create_kernel_counter(&attr, -1, current,
129 intr ? kvm_perf_overflow_intr :
130 kvm_perf_overflow, pmc);
131 if (IS_ERR(event)) {
Like Xu6fc39772019-07-18 13:35:14 +0800132 pr_debug_ratelimited("kvm_pmu: event creation failed %ld for pmc->idx = %d\n",
133 PTR_ERR(event), pmc->idx);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200134 return;
135 }
136
137 pmc->perf_event = event;
Like Xub35e5542019-10-27 18:52:43 +0800138 pmc_to_pmu(pmc)->event_count++;
Like Xu4be94672019-10-21 18:55:04 +0800139 clear_bit(pmc->idx, pmc_to_pmu(pmc)->reprogram_pmi);
Like Xue79f49c2021-07-28 20:07:05 +0800140 pmc->is_paused = false;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200141}
142
Like Xua6da0d72019-10-27 18:52:42 +0800143static void pmc_pause_counter(struct kvm_pmc *pmc)
144{
145 u64 counter = pmc->counter;
146
Like Xue79f49c2021-07-28 20:07:05 +0800147 if (!pmc->perf_event || pmc->is_paused)
Like Xua6da0d72019-10-27 18:52:42 +0800148 return;
149
150 /* update counter, reset event value to avoid redundant accumulation */
151 counter += perf_event_pause(pmc->perf_event, true);
152 pmc->counter = counter & pmc_bitmask(pmc);
Like Xue79f49c2021-07-28 20:07:05 +0800153 pmc->is_paused = true;
Like Xua6da0d72019-10-27 18:52:42 +0800154}
155
156static bool pmc_resume_counter(struct kvm_pmc *pmc)
157{
158 if (!pmc->perf_event)
159 return false;
160
161 /* recalibrate sample period and check if it's accepted by perf core */
162 if (perf_event_period(pmc->perf_event,
Eric Hankland168d9182020-02-21 18:34:13 -0800163 get_sample_period(pmc, pmc->counter)))
Like Xua6da0d72019-10-27 18:52:42 +0800164 return false;
165
166 /* reuse perf_event to serve as pmc_reprogram_counter() does*/
167 perf_event_enable(pmc->perf_event);
Like Xue79f49c2021-07-28 20:07:05 +0800168 pmc->is_paused = false;
Like Xua6da0d72019-10-27 18:52:42 +0800169
170 clear_bit(pmc->idx, (unsigned long *)&pmc_to_pmu(pmc)->reprogram_pmi);
171 return true;
172}
173
Wei Huang25462f72015-06-19 15:45:05 +0200174void reprogram_gp_counter(struct kvm_pmc *pmc, u64 eventsel)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200175{
176 unsigned config, type = PERF_TYPE_RAW;
177 u8 event_select, unit_mask;
Eric Hankland66bb8a02019-07-10 18:25:15 -0700178 struct kvm *kvm = pmc->vcpu->kvm;
179 struct kvm_pmu_event_filter *filter;
180 int i;
181 bool allow_event = true;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200182
Gleb Natapova7b9d2c2012-02-26 16:55:40 +0200183 if (eventsel & ARCH_PERFMON_EVENTSEL_PIN_CONTROL)
184 printk_once("kvm pmu: pin control bit is ignored\n");
185
Gleb Natapovf5132b02011-11-10 14:57:22 +0200186 pmc->eventsel = eventsel;
187
Like Xua6da0d72019-10-27 18:52:42 +0800188 pmc_pause_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200189
Wei Huangc6702c92015-06-19 13:44:45 +0200190 if (!(eventsel & ARCH_PERFMON_EVENTSEL_ENABLE) || !pmc_is_enabled(pmc))
Gleb Natapovf5132b02011-11-10 14:57:22 +0200191 return;
192
Eric Hankland66bb8a02019-07-10 18:25:15 -0700193 filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
194 if (filter) {
195 for (i = 0; i < filter->nevents; i++)
196 if (filter->events[i] ==
197 (eventsel & AMD64_RAW_EVENT_MASK_NB))
198 break;
199 if (filter->action == KVM_PMU_EVENT_ALLOW &&
200 i == filter->nevents)
201 allow_event = false;
202 if (filter->action == KVM_PMU_EVENT_DENY &&
203 i < filter->nevents)
204 allow_event = false;
205 }
206 if (!allow_event)
207 return;
208
Gleb Natapovf5132b02011-11-10 14:57:22 +0200209 event_select = eventsel & ARCH_PERFMON_EVENTSEL_EVENT;
210 unit_mask = (eventsel & ARCH_PERFMON_EVENTSEL_UMASK) >> 8;
211
Gleb Natapovfac33682012-02-26 16:55:41 +0200212 if (!(eventsel & (ARCH_PERFMON_EVENTSEL_EDGE |
Wei Huange84cfe42015-06-19 14:15:28 +0200213 ARCH_PERFMON_EVENTSEL_INV |
214 ARCH_PERFMON_EVENTSEL_CMASK |
215 HSW_IN_TX |
216 HSW_IN_TX_CHECKPOINTED))) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700217 config = kvm_x86_ops.pmu_ops->find_arch_event(pmc_to_pmu(pmc),
Wei Huang25462f72015-06-19 15:45:05 +0200218 event_select,
219 unit_mask);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200220 if (config != PERF_COUNT_HW_MAX)
221 type = PERF_TYPE_HARDWARE;
222 }
223
224 if (type == PERF_TYPE_RAW)
225 config = eventsel & X86_RAW_EVENT_MASK;
226
Like Xua6da0d72019-10-27 18:52:42 +0800227 if (pmc->current_config == eventsel && pmc_resume_counter(pmc))
228 return;
229
230 pmc_release_perf_event(pmc);
231
232 pmc->current_config = eventsel;
Wei Huangc6702c92015-06-19 13:44:45 +0200233 pmc_reprogram_counter(pmc, type, config,
Wei Huange84cfe42015-06-19 14:15:28 +0200234 !(eventsel & ARCH_PERFMON_EVENTSEL_USR),
235 !(eventsel & ARCH_PERFMON_EVENTSEL_OS),
236 eventsel & ARCH_PERFMON_EVENTSEL_INT,
237 (eventsel & HSW_IN_TX),
238 (eventsel & HSW_IN_TX_CHECKPOINTED));
Gleb Natapovf5132b02011-11-10 14:57:22 +0200239}
Wei Huang25462f72015-06-19 15:45:05 +0200240EXPORT_SYMBOL_GPL(reprogram_gp_counter);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200241
Wei Huang25462f72015-06-19 15:45:05 +0200242void reprogram_fixed_counter(struct kvm_pmc *pmc, u8 ctrl, int idx)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200243{
Wei Huange84cfe42015-06-19 14:15:28 +0200244 unsigned en_field = ctrl & 0x3;
245 bool pmi = ctrl & 0x8;
Eric Hankland30cd8602019-07-18 11:38:18 -0700246 struct kvm_pmu_event_filter *filter;
247 struct kvm *kvm = pmc->vcpu->kvm;
Gleb Natapovf5132b02011-11-10 14:57:22 +0200248
Like Xua6da0d72019-10-27 18:52:42 +0800249 pmc_pause_counter(pmc);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200250
Wei Huange84cfe42015-06-19 14:15:28 +0200251 if (!en_field || !pmc_is_enabled(pmc))
Gleb Natapovf5132b02011-11-10 14:57:22 +0200252 return;
253
Eric Hankland30cd8602019-07-18 11:38:18 -0700254 filter = srcu_dereference(kvm->arch.pmu_event_filter, &kvm->srcu);
255 if (filter) {
256 if (filter->action == KVM_PMU_EVENT_DENY &&
257 test_bit(idx, (ulong *)&filter->fixed_counter_bitmap))
258 return;
259 if (filter->action == KVM_PMU_EVENT_ALLOW &&
260 !test_bit(idx, (ulong *)&filter->fixed_counter_bitmap))
261 return;
262 }
263
Like Xua6da0d72019-10-27 18:52:42 +0800264 if (pmc->current_config == (u64)ctrl && pmc_resume_counter(pmc))
265 return;
266
267 pmc_release_perf_event(pmc);
268
269 pmc->current_config = (u64)ctrl;
Wei Huangc6702c92015-06-19 13:44:45 +0200270 pmc_reprogram_counter(pmc, PERF_TYPE_HARDWARE,
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700271 kvm_x86_ops.pmu_ops->find_fixed_event(idx),
Wei Huange84cfe42015-06-19 14:15:28 +0200272 !(en_field & 0x2), /* exclude user */
273 !(en_field & 0x1), /* exclude kernel */
274 pmi, false, false);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200275}
Wei Huang25462f72015-06-19 15:45:05 +0200276EXPORT_SYMBOL_GPL(reprogram_fixed_counter);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200277
Wei Huang25462f72015-06-19 15:45:05 +0200278void reprogram_counter(struct kvm_pmu *pmu, int pmc_idx)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200279{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700280 struct kvm_pmc *pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, pmc_idx);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200281
282 if (!pmc)
283 return;
284
285 if (pmc_is_gp(pmc))
286 reprogram_gp_counter(pmc, pmc->eventsel);
287 else {
Wei Huange84cfe42015-06-19 14:15:28 +0200288 int idx = pmc_idx - INTEL_PMC_IDX_FIXED;
289 u8 ctrl = fixed_ctrl_field(pmu->fixed_ctr_ctrl, idx);
290
291 reprogram_fixed_counter(pmc, ctrl, idx);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200292 }
293}
Wei Huang25462f72015-06-19 15:45:05 +0200294EXPORT_SYMBOL_GPL(reprogram_counter);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200295
Wei Huange5af0582015-06-19 15:51:47 +0200296void kvm_pmu_handle_event(struct kvm_vcpu *vcpu)
297{
298 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Wei Huange5af0582015-06-19 15:51:47 +0200299 int bit;
300
Like Xu4be94672019-10-21 18:55:04 +0800301 for_each_set_bit(bit, pmu->reprogram_pmi, X86_PMC_IDX_MAX) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700302 struct kvm_pmc *pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, bit);
Wei Huange5af0582015-06-19 15:51:47 +0200303
304 if (unlikely(!pmc || !pmc->perf_event)) {
Like Xu4be94672019-10-21 18:55:04 +0800305 clear_bit(bit, pmu->reprogram_pmi);
Wei Huange5af0582015-06-19 15:51:47 +0200306 continue;
307 }
308
309 reprogram_counter(pmu, bit);
310 }
Like Xub35e5542019-10-27 18:52:43 +0800311
312 /*
313 * Unused perf_events are only released if the corresponding MSRs
314 * weren't accessed during the last vCPU time slice. kvm_arch_sched_in
315 * triggers KVM_REQ_PMU if cleanup is needed.
316 */
317 if (unlikely(pmu->need_cleanup))
318 kvm_pmu_cleanup(vcpu);
Wei Huange5af0582015-06-19 15:51:47 +0200319}
320
321/* check if idx is a valid index to access PMU */
Like Xu98ff80f2019-10-27 18:52:40 +0800322int kvm_pmu_is_valid_rdpmc_ecx(struct kvm_vcpu *vcpu, unsigned int idx)
Wei Huange5af0582015-06-19 15:51:47 +0200323{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700324 return kvm_x86_ops.pmu_ops->is_valid_rdpmc_ecx(vcpu, idx);
Wei Huang41aac142015-06-19 16:16:59 +0200325}
326
Arbel Moshe2d7921c2018-03-12 13:12:53 +0200327bool is_vmware_backdoor_pmc(u32 pmc_idx)
328{
329 switch (pmc_idx) {
330 case VMWARE_BACKDOOR_PMC_HOST_TSC:
331 case VMWARE_BACKDOOR_PMC_REAL_TIME:
332 case VMWARE_BACKDOOR_PMC_APPARENT_TIME:
333 return true;
334 }
335 return false;
336}
337
338static int kvm_pmu_rdpmc_vmware(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
339{
340 u64 ctr_val;
341
342 switch (idx) {
343 case VMWARE_BACKDOOR_PMC_HOST_TSC:
344 ctr_val = rdtsc();
345 break;
346 case VMWARE_BACKDOOR_PMC_REAL_TIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +0200347 ctr_val = ktime_get_boottime_ns();
Arbel Moshe2d7921c2018-03-12 13:12:53 +0200348 break;
349 case VMWARE_BACKDOOR_PMC_APPARENT_TIME:
Jason A. Donenfeld9285ec42019-06-21 22:32:48 +0200350 ctr_val = ktime_get_boottime_ns() +
Arbel Moshe2d7921c2018-03-12 13:12:53 +0200351 vcpu->kvm->arch.kvmclock_offset;
352 break;
353 default:
354 return 1;
355 }
356
357 *data = ctr_val;
358 return 0;
359}
360
Wei Huang41aac142015-06-19 16:16:59 +0200361int kvm_pmu_rdpmc(struct kvm_vcpu *vcpu, unsigned idx, u64 *data)
362{
363 bool fast_mode = idx & (1u << 31);
Liran Alon672ff6c2019-03-25 21:10:17 +0200364 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Wei Huang41aac142015-06-19 16:16:59 +0200365 struct kvm_pmc *pmc;
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200366 u64 mask = fast_mode ? ~0u : ~0ull;
Wei Huang41aac142015-06-19 16:16:59 +0200367
Liran Alon672ff6c2019-03-25 21:10:17 +0200368 if (!pmu->version)
369 return 1;
370
Arbel Moshe2d7921c2018-03-12 13:12:53 +0200371 if (is_vmware_backdoor_pmc(idx))
372 return kvm_pmu_rdpmc_vmware(vcpu, idx, data);
373
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700374 pmc = kvm_x86_ops.pmu_ops->rdpmc_ecx_to_pmc(vcpu, idx, &mask);
Wei Huang41aac142015-06-19 16:16:59 +0200375 if (!pmc)
376 return 1;
377
Like Xu632a4cf2020-07-08 15:44:09 +0800378 if (!(kvm_read_cr4(vcpu) & X86_CR4_PCE) &&
Jason Baronb36464772021-01-14 22:27:56 -0500379 (static_call(kvm_x86_get_cpl)(vcpu) != 0) &&
Like Xu632a4cf2020-07-08 15:44:09 +0800380 (kvm_read_cr0(vcpu) & X86_CR0_PE))
381 return 1;
382
Paolo Bonzini0e6f4672019-05-20 17:20:40 +0200383 *data = pmc_read_counter(pmc) & mask;
Wei Huange5af0582015-06-19 15:51:47 +0200384 return 0;
385}
386
387void kvm_pmu_deliver_pmi(struct kvm_vcpu *vcpu)
388{
Like Xue6209a32021-02-01 13:10:36 +0800389 if (lapic_in_kernel(vcpu)) {
390 if (kvm_x86_ops.pmu_ops->deliver_pmi)
391 kvm_x86_ops.pmu_ops->deliver_pmi(vcpu);
Wei Huange5af0582015-06-19 15:51:47 +0200392 kvm_apic_local_deliver(vcpu->arch.apic, APIC_LVTPC);
Like Xue6209a32021-02-01 13:10:36 +0800393 }
Wei Huange5af0582015-06-19 15:51:47 +0200394}
395
Wei Huangc6702c92015-06-19 13:44:45 +0200396bool kvm_pmu_is_valid_msr(struct kvm_vcpu *vcpu, u32 msr)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200397{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700398 return kvm_x86_ops.pmu_ops->msr_idx_to_pmc(vcpu, msr) ||
399 kvm_x86_ops.pmu_ops->is_valid_msr(vcpu, msr);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200400}
401
Like Xub35e5542019-10-27 18:52:43 +0800402static void kvm_pmu_mark_pmc_in_use(struct kvm_vcpu *vcpu, u32 msr)
403{
404 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700405 struct kvm_pmc *pmc = kvm_x86_ops.pmu_ops->msr_idx_to_pmc(vcpu, msr);
Like Xub35e5542019-10-27 18:52:43 +0800406
407 if (pmc)
408 __set_bit(pmc->idx, pmu->pmc_in_use);
409}
410
Wei Wangcbd71752020-05-29 15:43:44 +0800411int kvm_pmu_get_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200412{
Wei Wangcbd71752020-05-29 15:43:44 +0800413 return kvm_x86_ops.pmu_ops->get_msr(vcpu, msr_info);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200414}
415
Paolo Bonziniafd80d82013-03-28 17:18:35 +0100416int kvm_pmu_set_msr(struct kvm_vcpu *vcpu, struct msr_data *msr_info)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200417{
Like Xub35e5542019-10-27 18:52:43 +0800418 kvm_pmu_mark_pmc_in_use(vcpu, msr_info->index);
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700419 return kvm_x86_ops.pmu_ops->set_msr(vcpu, msr_info);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200420}
421
Wei Huange84cfe42015-06-19 14:15:28 +0200422/* refresh PMU settings. This function generally is called when underlying
423 * settings are changed (such as changes of PMU CPUID by guest VMs), which
424 * should rarely happen.
425 */
Wei Huangc6702c92015-06-19 13:44:45 +0200426void kvm_pmu_refresh(struct kvm_vcpu *vcpu)
Gleb Natapovf5132b02011-11-10 14:57:22 +0200427{
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700428 kvm_x86_ops.pmu_ops->refresh(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200429}
430
Wei Huange5af0582015-06-19 15:51:47 +0200431void kvm_pmu_reset(struct kvm_vcpu *vcpu)
432{
433 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Wei Huange5af0582015-06-19 15:51:47 +0200434
435 irq_work_sync(&pmu->irq_work);
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700436 kvm_x86_ops.pmu_ops->reset(vcpu);
Wei Huange5af0582015-06-19 15:51:47 +0200437}
438
Gleb Natapovf5132b02011-11-10 14:57:22 +0200439void kvm_pmu_init(struct kvm_vcpu *vcpu)
440{
Wei Huang212dba12015-06-19 14:00:33 +0200441 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200442
443 memset(pmu, 0, sizeof(*pmu));
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700444 kvm_x86_ops.pmu_ops->init(vcpu);
Wei Huangc6702c92015-06-19 13:44:45 +0200445 init_irq_work(&pmu->irq_work, kvm_pmi_trigger_fn);
Like Xub35e5542019-10-27 18:52:43 +0800446 pmu->event_count = 0;
447 pmu->need_cleanup = false;
Wei Huangc6702c92015-06-19 13:44:45 +0200448 kvm_pmu_refresh(vcpu);
Gleb Natapovf5132b02011-11-10 14:57:22 +0200449}
450
Like Xub35e5542019-10-27 18:52:43 +0800451static inline bool pmc_speculative_in_use(struct kvm_pmc *pmc)
452{
453 struct kvm_pmu *pmu = pmc_to_pmu(pmc);
454
455 if (pmc_is_fixed(pmc))
456 return fixed_ctrl_field(pmu->fixed_ctr_ctrl,
457 pmc->idx - INTEL_PMC_IDX_FIXED) & 0x3;
458
459 return pmc->eventsel & ARCH_PERFMON_EVENTSEL_ENABLE;
460}
461
462/* Release perf_events for vPMCs that have been unused for a full time slice. */
463void kvm_pmu_cleanup(struct kvm_vcpu *vcpu)
464{
465 struct kvm_pmu *pmu = vcpu_to_pmu(vcpu);
466 struct kvm_pmc *pmc = NULL;
467 DECLARE_BITMAP(bitmask, X86_PMC_IDX_MAX);
468 int i;
469
470 pmu->need_cleanup = false;
471
472 bitmap_andnot(bitmask, pmu->all_valid_pmc_idx,
473 pmu->pmc_in_use, X86_PMC_IDX_MAX);
474
475 for_each_set_bit(i, bitmask, X86_PMC_IDX_MAX) {
Sean Christophersonafaf0b22020-03-21 13:26:00 -0700476 pmc = kvm_x86_ops.pmu_ops->pmc_idx_to_pmc(pmu, i);
Like Xub35e5542019-10-27 18:52:43 +0800477
478 if (pmc && pmc->perf_event && !pmc_speculative_in_use(pmc))
479 pmc_stop_counter(pmc);
480 }
481
Like Xu9aa4f622021-02-01 13:10:37 +0800482 if (kvm_x86_ops.pmu_ops->cleanup)
483 kvm_x86_ops.pmu_ops->cleanup(vcpu);
484
Like Xub35e5542019-10-27 18:52:43 +0800485 bitmap_zero(pmu->pmc_in_use, X86_PMC_IDX_MAX);
486}
487
Gleb Natapovf5132b02011-11-10 14:57:22 +0200488void kvm_pmu_destroy(struct kvm_vcpu *vcpu)
489{
490 kvm_pmu_reset(vcpu);
491}
Eric Hankland66bb8a02019-07-10 18:25:15 -0700492
493int kvm_vm_ioctl_set_pmu_event_filter(struct kvm *kvm, void __user *argp)
494{
495 struct kvm_pmu_event_filter tmp, *filter;
496 size_t size;
497 int r;
498
499 if (copy_from_user(&tmp, argp, sizeof(tmp)))
500 return -EFAULT;
501
502 if (tmp.action != KVM_PMU_EVENT_ALLOW &&
503 tmp.action != KVM_PMU_EVENT_DENY)
504 return -EINVAL;
505
Eric Hankland30cd8602019-07-18 11:38:18 -0700506 if (tmp.flags != 0)
507 return -EINVAL;
508
Eric Hankland66bb8a02019-07-10 18:25:15 -0700509 if (tmp.nevents > KVM_PMU_EVENT_FILTER_MAX_EVENTS)
510 return -E2BIG;
511
512 size = struct_size(filter, events, tmp.nevents);
513 filter = kmalloc(size, GFP_KERNEL_ACCOUNT);
514 if (!filter)
515 return -ENOMEM;
516
517 r = -EFAULT;
518 if (copy_from_user(filter, argp, size))
519 goto cleanup;
520
521 /* Ensure nevents can't be changed between the user copies. */
522 *filter = tmp;
523
524 mutex_lock(&kvm->lock);
Paul E. McKenney12e78e62019-09-23 15:15:35 -0700525 filter = rcu_replace_pointer(kvm->arch.pmu_event_filter, filter,
526 mutex_is_locked(&kvm->lock));
Eric Hankland66bb8a02019-07-10 18:25:15 -0700527 mutex_unlock(&kvm->lock);
528
529 synchronize_srcu_expedited(&kvm->srcu);
Eric Hankland30cd8602019-07-18 11:38:18 -0700530 r = 0;
Eric Hankland66bb8a02019-07-10 18:25:15 -0700531cleanup:
532 kfree(filter);
Eric Hankland30cd8602019-07-18 11:38:18 -0700533 return r;
Eric Hankland66bb8a02019-07-10 18:25:15 -0700534}