1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright (C) 2015 Linaro Ltd.
4 * Author: Shannon Zhao <shannon.zhao@linaro.org>
5 */
6
7 #include <linux/cpu.h>
8 #include <linux/kvm.h>
9 #include <linux/kvm_host.h>
10 #include <linux/list.h>
11 #include <linux/perf_event.h>
12 #include <linux/perf/arm_pmu.h>
13 #include <linux/uaccess.h>
14 #include <asm/kvm_emulate.h>
15 #include <kvm/arm_pmu.h>
16 #include <kvm/arm_vgic.h>
17
18 #define PERF_ATTR_CFG1_COUNTER_64BIT BIT(0)
19
20 DEFINE_STATIC_KEY_FALSE(kvm_arm_pmu_available);
21
22 static LIST_HEAD(arm_pmus);
23 static DEFINE_MUTEX(arm_pmus_lock);
24
25 static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc);
26 static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc);
27
kvm_pmc_to_vcpu(const struct kvm_pmc * pmc)28 static struct kvm_vcpu *kvm_pmc_to_vcpu(const struct kvm_pmc *pmc)
29 {
30 return container_of(pmc, struct kvm_vcpu, arch.pmu.pmc[pmc->idx]);
31 }
32
kvm_vcpu_idx_to_pmc(struct kvm_vcpu * vcpu,int cnt_idx)33 static struct kvm_pmc *kvm_vcpu_idx_to_pmc(struct kvm_vcpu *vcpu, int cnt_idx)
34 {
35 return &vcpu->arch.pmu.pmc[cnt_idx];
36 }
37
__kvm_pmu_event_mask(unsigned int pmuver)38 static u32 __kvm_pmu_event_mask(unsigned int pmuver)
39 {
40 switch (pmuver) {
41 case ID_AA64DFR0_EL1_PMUVer_IMP:
42 return GENMASK(9, 0);
43 case ID_AA64DFR0_EL1_PMUVer_V3P1:
44 case ID_AA64DFR0_EL1_PMUVer_V3P4:
45 case ID_AA64DFR0_EL1_PMUVer_V3P5:
46 case ID_AA64DFR0_EL1_PMUVer_V3P7:
47 return GENMASK(15, 0);
48 default: /* Shouldn't be here, just for sanity */
49 WARN_ONCE(1, "Unknown PMU version %d\n", pmuver);
50 return 0;
51 }
52 }
53
kvm_pmu_event_mask(struct kvm * kvm)54 static u32 kvm_pmu_event_mask(struct kvm *kvm)
55 {
56 u64 dfr0 = kvm_read_vm_id_reg(kvm, SYS_ID_AA64DFR0_EL1);
57 u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, dfr0);
58
59 return __kvm_pmu_event_mask(pmuver);
60 }
61
kvm_pmu_evtyper_mask(struct kvm * kvm)62 u64 kvm_pmu_evtyper_mask(struct kvm *kvm)
63 {
64 u64 mask = ARMV8_PMU_EXCLUDE_EL1 | ARMV8_PMU_EXCLUDE_EL0 |
65 kvm_pmu_event_mask(kvm);
66
67 if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL2, IMP))
68 mask |= ARMV8_PMU_INCLUDE_EL2;
69
70 if (kvm_has_feat(kvm, ID_AA64PFR0_EL1, EL3, IMP))
71 mask |= ARMV8_PMU_EXCLUDE_NS_EL0 |
72 ARMV8_PMU_EXCLUDE_NS_EL1 |
73 ARMV8_PMU_EXCLUDE_EL3;
74
75 return mask;
76 }
77
78 /**
79 * kvm_pmc_is_64bit - determine if counter is 64bit
80 * @pmc: counter context
81 */
kvm_pmc_is_64bit(struct kvm_pmc * pmc)82 static bool kvm_pmc_is_64bit(struct kvm_pmc *pmc)
83 {
84 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
85
86 return (pmc->idx == ARMV8_PMU_CYCLE_IDX ||
87 kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5));
88 }
89
kvm_pmc_has_64bit_overflow(struct kvm_pmc * pmc)90 static bool kvm_pmc_has_64bit_overflow(struct kvm_pmc *pmc)
91 {
92 u64 val = kvm_vcpu_read_pmcr(kvm_pmc_to_vcpu(pmc));
93
94 return (pmc->idx < ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LP)) ||
95 (pmc->idx == ARMV8_PMU_CYCLE_IDX && (val & ARMV8_PMU_PMCR_LC));
96 }
97
kvm_pmu_counter_can_chain(struct kvm_pmc * pmc)98 static bool kvm_pmu_counter_can_chain(struct kvm_pmc *pmc)
99 {
100 return (!(pmc->idx & 1) && (pmc->idx + 1) < ARMV8_PMU_CYCLE_IDX &&
101 !kvm_pmc_has_64bit_overflow(pmc));
102 }
103
counter_index_to_reg(u64 idx)104 static u32 counter_index_to_reg(u64 idx)
105 {
106 return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCNTR_EL0 : PMEVCNTR0_EL0 + idx;
107 }
108
counter_index_to_evtreg(u64 idx)109 static u32 counter_index_to_evtreg(u64 idx)
110 {
111 return (idx == ARMV8_PMU_CYCLE_IDX) ? PMCCFILTR_EL0 : PMEVTYPER0_EL0 + idx;
112 }
113
kvm_pmu_get_pmc_value(struct kvm_pmc * pmc)114 static u64 kvm_pmu_get_pmc_value(struct kvm_pmc *pmc)
115 {
116 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
117 u64 counter, reg, enabled, running;
118
119 reg = counter_index_to_reg(pmc->idx);
120 counter = __vcpu_sys_reg(vcpu, reg);
121
122 /*
123 * The real counter value is equal to the value of counter register plus
124 * the value perf event counts.
125 */
126 if (pmc->perf_event)
127 counter += perf_event_read_value(pmc->perf_event, &enabled,
128 &running);
129
130 if (!kvm_pmc_is_64bit(pmc))
131 counter = lower_32_bits(counter);
132
133 return counter;
134 }
135
136 /**
137 * kvm_pmu_get_counter_value - get PMU counter value
138 * @vcpu: The vcpu pointer
139 * @select_idx: The counter index
140 */
kvm_pmu_get_counter_value(struct kvm_vcpu * vcpu,u64 select_idx)141 u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx)
142 {
143 if (!kvm_vcpu_has_pmu(vcpu))
144 return 0;
145
146 return kvm_pmu_get_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx));
147 }
148
kvm_pmu_set_pmc_value(struct kvm_pmc * pmc,u64 val,bool force)149 static void kvm_pmu_set_pmc_value(struct kvm_pmc *pmc, u64 val, bool force)
150 {
151 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
152 u64 reg;
153
154 kvm_pmu_release_perf_event(pmc);
155
156 reg = counter_index_to_reg(pmc->idx);
157
158 if (vcpu_mode_is_32bit(vcpu) && pmc->idx != ARMV8_PMU_CYCLE_IDX &&
159 !force) {
160 /*
161 * Even with PMUv3p5, AArch32 cannot write to the top
162 * 32bit of the counters. The only possible course of
163 * action is to use PMCR.P, which will reset them to
164 * 0 (the only use of the 'force' parameter).
165 */
166 val = __vcpu_sys_reg(vcpu, reg) & GENMASK(63, 32);
167 val |= lower_32_bits(val);
168 }
169
170 __vcpu_sys_reg(vcpu, reg) = val;
171
172 /* Recreate the perf event to reflect the updated sample_period */
173 kvm_pmu_create_perf_event(pmc);
174 }
175
176 /**
177 * kvm_pmu_set_counter_value - set PMU counter value
178 * @vcpu: The vcpu pointer
179 * @select_idx: The counter index
180 * @val: The counter value
181 */
kvm_pmu_set_counter_value(struct kvm_vcpu * vcpu,u64 select_idx,u64 val)182 void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
183 {
184 if (!kvm_vcpu_has_pmu(vcpu))
185 return;
186
187 kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, select_idx), val, false);
188 }
189
190 /**
191 * kvm_pmu_release_perf_event - remove the perf event
192 * @pmc: The PMU counter pointer
193 */
kvm_pmu_release_perf_event(struct kvm_pmc * pmc)194 static void kvm_pmu_release_perf_event(struct kvm_pmc *pmc)
195 {
196 if (pmc->perf_event) {
197 perf_event_disable(pmc->perf_event);
198 perf_event_release_kernel(pmc->perf_event);
199 pmc->perf_event = NULL;
200 }
201 }
202
203 /**
204 * kvm_pmu_stop_counter - stop PMU counter
205 * @pmc: The PMU counter pointer
206 *
207 * If this counter has been configured to monitor some event, release it here.
208 */
kvm_pmu_stop_counter(struct kvm_pmc * pmc)209 static void kvm_pmu_stop_counter(struct kvm_pmc *pmc)
210 {
211 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
212 u64 reg, val;
213
214 if (!pmc->perf_event)
215 return;
216
217 val = kvm_pmu_get_pmc_value(pmc);
218
219 reg = counter_index_to_reg(pmc->idx);
220
221 __vcpu_sys_reg(vcpu, reg) = val;
222
223 kvm_pmu_release_perf_event(pmc);
224 }
225
226 /**
227 * kvm_pmu_vcpu_init - assign pmu counter idx for cpu
228 * @vcpu: The vcpu pointer
229 *
230 */
kvm_pmu_vcpu_init(struct kvm_vcpu * vcpu)231 void kvm_pmu_vcpu_init(struct kvm_vcpu *vcpu)
232 {
233 int i;
234 struct kvm_pmu *pmu = &vcpu->arch.pmu;
235
236 for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++)
237 pmu->pmc[i].idx = i;
238 }
239
240 /**
241 * kvm_pmu_vcpu_reset - reset pmu state for cpu
242 * @vcpu: The vcpu pointer
243 *
244 */
kvm_pmu_vcpu_reset(struct kvm_vcpu * vcpu)245 void kvm_pmu_vcpu_reset(struct kvm_vcpu *vcpu)
246 {
247 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
248 int i;
249
250 for_each_set_bit(i, &mask, 32)
251 kvm_pmu_stop_counter(kvm_vcpu_idx_to_pmc(vcpu, i));
252 }
253
254 /**
255 * kvm_pmu_vcpu_destroy - free perf event of PMU for cpu
256 * @vcpu: The vcpu pointer
257 *
258 */
kvm_pmu_vcpu_destroy(struct kvm_vcpu * vcpu)259 void kvm_pmu_vcpu_destroy(struct kvm_vcpu *vcpu)
260 {
261 int i;
262
263 for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++)
264 kvm_pmu_release_perf_event(kvm_vcpu_idx_to_pmc(vcpu, i));
265 irq_work_sync(&vcpu->arch.pmu.overflow_work);
266 }
267
kvm_pmu_valid_counter_mask(struct kvm_vcpu * vcpu)268 u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
269 {
270 u64 val = FIELD_GET(ARMV8_PMU_PMCR_N, kvm_vcpu_read_pmcr(vcpu));
271
272 if (val == 0)
273 return BIT(ARMV8_PMU_CYCLE_IDX);
274 else
275 return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
276 }
277
278 /**
279 * kvm_pmu_enable_counter_mask - enable selected PMU counters
280 * @vcpu: The vcpu pointer
281 * @val: the value guest writes to PMCNTENSET register
282 *
283 * Call perf_event_enable to start counting the perf event
284 */
kvm_pmu_enable_counter_mask(struct kvm_vcpu * vcpu,u64 val)285 void kvm_pmu_enable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
286 {
287 int i;
288 if (!kvm_vcpu_has_pmu(vcpu))
289 return;
290
291 if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E) || !val)
292 return;
293
294 for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++) {
295 struct kvm_pmc *pmc;
296
297 if (!(val & BIT(i)))
298 continue;
299
300 pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
301
302 if (!pmc->perf_event) {
303 kvm_pmu_create_perf_event(pmc);
304 } else {
305 perf_event_enable(pmc->perf_event);
306 if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
307 kvm_debug("fail to enable perf event\n");
308 }
309 }
310 }
311
312 /**
313 * kvm_pmu_disable_counter_mask - disable selected PMU counters
314 * @vcpu: The vcpu pointer
315 * @val: the value guest writes to PMCNTENCLR register
316 *
317 * Call perf_event_disable to stop counting the perf event
318 */
kvm_pmu_disable_counter_mask(struct kvm_vcpu * vcpu,u64 val)319 void kvm_pmu_disable_counter_mask(struct kvm_vcpu *vcpu, u64 val)
320 {
321 int i;
322
323 if (!kvm_vcpu_has_pmu(vcpu) || !val)
324 return;
325
326 for (i = 0; i < KVM_ARMV8_PMU_MAX_COUNTERS; i++) {
327 struct kvm_pmc *pmc;
328
329 if (!(val & BIT(i)))
330 continue;
331
332 pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
333
334 if (pmc->perf_event)
335 perf_event_disable(pmc->perf_event);
336 }
337 }
338
kvm_pmu_overflow_status(struct kvm_vcpu * vcpu)339 static u64 kvm_pmu_overflow_status(struct kvm_vcpu *vcpu)
340 {
341 u64 reg = 0;
342
343 if ((kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E)) {
344 reg = __vcpu_sys_reg(vcpu, PMOVSSET_EL0);
345 reg &= __vcpu_sys_reg(vcpu, PMINTENSET_EL1);
346 }
347
348 return reg;
349 }
350
kvm_pmu_update_state(struct kvm_vcpu * vcpu)351 static void kvm_pmu_update_state(struct kvm_vcpu *vcpu)
352 {
353 struct kvm_pmu *pmu = &vcpu->arch.pmu;
354 bool overflow;
355
356 if (!kvm_vcpu_has_pmu(vcpu))
357 return;
358
359 overflow = !!kvm_pmu_overflow_status(vcpu);
360 if (pmu->irq_level == overflow)
361 return;
362
363 pmu->irq_level = overflow;
364
365 if (likely(irqchip_in_kernel(vcpu->kvm))) {
366 int ret = kvm_vgic_inject_irq(vcpu->kvm, vcpu,
367 pmu->irq_num, overflow, pmu);
368 WARN_ON(ret);
369 }
370 }
371
kvm_pmu_should_notify_user(struct kvm_vcpu * vcpu)372 bool kvm_pmu_should_notify_user(struct kvm_vcpu *vcpu)
373 {
374 struct kvm_pmu *pmu = &vcpu->arch.pmu;
375 struct kvm_sync_regs *sregs = &vcpu->run->s.regs;
376 bool run_level = sregs->device_irq_level & KVM_ARM_DEV_PMU;
377
378 if (likely(irqchip_in_kernel(vcpu->kvm)))
379 return false;
380
381 return pmu->irq_level != run_level;
382 }
383
384 /*
385 * Reflect the PMU overflow interrupt output level into the kvm_run structure
386 */
kvm_pmu_update_run(struct kvm_vcpu * vcpu)387 void kvm_pmu_update_run(struct kvm_vcpu *vcpu)
388 {
389 struct kvm_sync_regs *regs = &vcpu->run->s.regs;
390
391 /* Populate the timer bitmap for user space */
392 regs->device_irq_level &= ~KVM_ARM_DEV_PMU;
393 if (vcpu->arch.pmu.irq_level)
394 regs->device_irq_level |= KVM_ARM_DEV_PMU;
395 }
396
397 /**
398 * kvm_pmu_flush_hwstate - flush pmu state to cpu
399 * @vcpu: The vcpu pointer
400 *
401 * Check if the PMU has overflowed while we were running in the host, and inject
402 * an interrupt if that was the case.
403 */
kvm_pmu_flush_hwstate(struct kvm_vcpu * vcpu)404 void kvm_pmu_flush_hwstate(struct kvm_vcpu *vcpu)
405 {
406 kvm_pmu_update_state(vcpu);
407 }
408
409 /**
410 * kvm_pmu_sync_hwstate - sync pmu state from cpu
411 * @vcpu: The vcpu pointer
412 *
413 * Check if the PMU has overflowed while we were running in the guest, and
414 * inject an interrupt if that was the case.
415 */
kvm_pmu_sync_hwstate(struct kvm_vcpu * vcpu)416 void kvm_pmu_sync_hwstate(struct kvm_vcpu *vcpu)
417 {
418 kvm_pmu_update_state(vcpu);
419 }
420
421 /*
422 * When perf interrupt is an NMI, we cannot safely notify the vcpu corresponding
423 * to the event.
424 * This is why we need a callback to do it once outside of the NMI context.
425 */
kvm_pmu_perf_overflow_notify_vcpu(struct irq_work * work)426 static void kvm_pmu_perf_overflow_notify_vcpu(struct irq_work *work)
427 {
428 struct kvm_vcpu *vcpu;
429
430 vcpu = container_of(work, struct kvm_vcpu, arch.pmu.overflow_work);
431 kvm_vcpu_kick(vcpu);
432 }
433
434 /*
435 * Perform an increment on any of the counters described in @mask,
436 * generating the overflow if required, and propagate it as a chained
437 * event if possible.
438 */
kvm_pmu_counter_increment(struct kvm_vcpu * vcpu,unsigned long mask,u32 event)439 static void kvm_pmu_counter_increment(struct kvm_vcpu *vcpu,
440 unsigned long mask, u32 event)
441 {
442 int i;
443
444 if (!(kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E))
445 return;
446
447 /* Weed out disabled counters */
448 mask &= __vcpu_sys_reg(vcpu, PMCNTENSET_EL0);
449
450 for_each_set_bit(i, &mask, ARMV8_PMU_CYCLE_IDX) {
451 struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, i);
452 u64 type, reg;
453
454 /* Filter on event type */
455 type = __vcpu_sys_reg(vcpu, counter_index_to_evtreg(i));
456 type &= kvm_pmu_event_mask(vcpu->kvm);
457 if (type != event)
458 continue;
459
460 /* Increment this counter */
461 reg = __vcpu_sys_reg(vcpu, counter_index_to_reg(i)) + 1;
462 if (!kvm_pmc_is_64bit(pmc))
463 reg = lower_32_bits(reg);
464 __vcpu_sys_reg(vcpu, counter_index_to_reg(i)) = reg;
465
466 /* No overflow? move on */
467 if (kvm_pmc_has_64bit_overflow(pmc) ? reg : lower_32_bits(reg))
468 continue;
469
470 /* Mark overflow */
471 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(i);
472
473 if (kvm_pmu_counter_can_chain(pmc))
474 kvm_pmu_counter_increment(vcpu, BIT(i + 1),
475 ARMV8_PMUV3_PERFCTR_CHAIN);
476 }
477 }
478
479 /* Compute the sample period for a given counter value */
compute_period(struct kvm_pmc * pmc,u64 counter)480 static u64 compute_period(struct kvm_pmc *pmc, u64 counter)
481 {
482 u64 val;
483
484 if (kvm_pmc_is_64bit(pmc) && kvm_pmc_has_64bit_overflow(pmc))
485 val = (-counter) & GENMASK(63, 0);
486 else
487 val = (-counter) & GENMASK(31, 0);
488
489 return val;
490 }
491
492 /*
493 * When the perf event overflows, set the overflow status and inform the vcpu.
494 */
kvm_pmu_perf_overflow(struct perf_event * perf_event,struct perf_sample_data * data,struct pt_regs * regs)495 static void kvm_pmu_perf_overflow(struct perf_event *perf_event,
496 struct perf_sample_data *data,
497 struct pt_regs *regs)
498 {
499 struct kvm_pmc *pmc = perf_event->overflow_handler_context;
500 struct arm_pmu *cpu_pmu = to_arm_pmu(perf_event->pmu);
501 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
502 int idx = pmc->idx;
503 u64 period;
504
505 cpu_pmu->pmu.stop(perf_event, PERF_EF_UPDATE);
506
507 /*
508 * Reset the sample period to the architectural limit,
509 * i.e. the point where the counter overflows.
510 */
511 period = compute_period(pmc, local64_read(&perf_event->count));
512
513 local64_set(&perf_event->hw.period_left, 0);
514 perf_event->attr.sample_period = period;
515 perf_event->hw.sample_period = period;
516
517 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= BIT(idx);
518
519 if (kvm_pmu_counter_can_chain(pmc))
520 kvm_pmu_counter_increment(vcpu, BIT(idx + 1),
521 ARMV8_PMUV3_PERFCTR_CHAIN);
522
523 if (kvm_pmu_overflow_status(vcpu)) {
524 kvm_make_request(KVM_REQ_IRQ_PENDING, vcpu);
525
526 if (!in_nmi())
527 kvm_vcpu_kick(vcpu);
528 else
529 irq_work_queue(&vcpu->arch.pmu.overflow_work);
530 }
531
532 cpu_pmu->pmu.start(perf_event, PERF_EF_RELOAD);
533 }
534
535 /**
536 * kvm_pmu_software_increment - do software increment
537 * @vcpu: The vcpu pointer
538 * @val: the value guest writes to PMSWINC register
539 */
kvm_pmu_software_increment(struct kvm_vcpu * vcpu,u64 val)540 void kvm_pmu_software_increment(struct kvm_vcpu *vcpu, u64 val)
541 {
542 kvm_pmu_counter_increment(vcpu, val, ARMV8_PMUV3_PERFCTR_SW_INCR);
543 }
544
545 /**
546 * kvm_pmu_handle_pmcr - handle PMCR register
547 * @vcpu: The vcpu pointer
548 * @val: the value guest writes to PMCR register
549 */
kvm_pmu_handle_pmcr(struct kvm_vcpu * vcpu,u64 val)550 void kvm_pmu_handle_pmcr(struct kvm_vcpu *vcpu, u64 val)
551 {
552 int i;
553
554 if (!kvm_vcpu_has_pmu(vcpu))
555 return;
556
557 /* Fixup PMCR_EL0 to reconcile the PMU version and the LP bit */
558 if (!kvm_has_feat(vcpu->kvm, ID_AA64DFR0_EL1, PMUVer, V3P5))
559 val &= ~ARMV8_PMU_PMCR_LP;
560
561 /* The reset bits don't indicate any state, and shouldn't be saved. */
562 __vcpu_sys_reg(vcpu, PMCR_EL0) = val & ~(ARMV8_PMU_PMCR_C | ARMV8_PMU_PMCR_P);
563
564 if (val & ARMV8_PMU_PMCR_E) {
565 kvm_pmu_enable_counter_mask(vcpu,
566 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0));
567 } else {
568 kvm_pmu_disable_counter_mask(vcpu,
569 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0));
570 }
571
572 if (val & ARMV8_PMU_PMCR_C)
573 kvm_pmu_set_counter_value(vcpu, ARMV8_PMU_CYCLE_IDX, 0);
574
575 if (val & ARMV8_PMU_PMCR_P) {
576 unsigned long mask = kvm_pmu_valid_counter_mask(vcpu);
577 mask &= ~BIT(ARMV8_PMU_CYCLE_IDX);
578 for_each_set_bit(i, &mask, 32)
579 kvm_pmu_set_pmc_value(kvm_vcpu_idx_to_pmc(vcpu, i), 0, true);
580 }
581 kvm_vcpu_pmu_restore_guest(vcpu);
582 }
583
kvm_pmu_counter_is_enabled(struct kvm_pmc * pmc)584 static bool kvm_pmu_counter_is_enabled(struct kvm_pmc *pmc)
585 {
586 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
587 return (kvm_vcpu_read_pmcr(vcpu) & ARMV8_PMU_PMCR_E) &&
588 (__vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & BIT(pmc->idx));
589 }
590
591 /**
592 * kvm_pmu_create_perf_event - create a perf event for a counter
593 * @pmc: Counter context
594 */
kvm_pmu_create_perf_event(struct kvm_pmc * pmc)595 static void kvm_pmu_create_perf_event(struct kvm_pmc *pmc)
596 {
597 struct kvm_vcpu *vcpu = kvm_pmc_to_vcpu(pmc);
598 struct arm_pmu *arm_pmu = vcpu->kvm->arch.arm_pmu;
599 struct perf_event *event;
600 struct perf_event_attr attr;
601 u64 eventsel, reg, data;
602 bool p, u, nsk, nsu;
603
604 reg = counter_index_to_evtreg(pmc->idx);
605 data = __vcpu_sys_reg(vcpu, reg);
606
607 kvm_pmu_stop_counter(pmc);
608 if (pmc->idx == ARMV8_PMU_CYCLE_IDX)
609 eventsel = ARMV8_PMUV3_PERFCTR_CPU_CYCLES;
610 else
611 eventsel = data & kvm_pmu_event_mask(vcpu->kvm);
612
613 /*
614 * Neither SW increment nor chained events need to be backed
615 * by a perf event.
616 */
617 if (eventsel == ARMV8_PMUV3_PERFCTR_SW_INCR ||
618 eventsel == ARMV8_PMUV3_PERFCTR_CHAIN)
619 return;
620
621 /*
622 * If we have a filter in place and that the event isn't allowed, do
623 * not install a perf event either.
624 */
625 if (vcpu->kvm->arch.pmu_filter &&
626 !test_bit(eventsel, vcpu->kvm->arch.pmu_filter))
627 return;
628
629 p = data & ARMV8_PMU_EXCLUDE_EL1;
630 u = data & ARMV8_PMU_EXCLUDE_EL0;
631 nsk = data & ARMV8_PMU_EXCLUDE_NS_EL1;
632 nsu = data & ARMV8_PMU_EXCLUDE_NS_EL0;
633
634 memset(&attr, 0, sizeof(struct perf_event_attr));
635 attr.type = arm_pmu->pmu.type;
636 attr.size = sizeof(attr);
637 attr.pinned = 1;
638 attr.disabled = !kvm_pmu_counter_is_enabled(pmc);
639 attr.exclude_user = (u != nsu);
640 attr.exclude_kernel = (p != nsk);
641 attr.exclude_hv = 1; /* Don't count EL2 events */
642 attr.exclude_host = 1; /* Don't count host events */
643 attr.config = eventsel;
644
645 /*
646 * If counting with a 64bit counter, advertise it to the perf
647 * code, carefully dealing with the initial sample period
648 * which also depends on the overflow.
649 */
650 if (kvm_pmc_is_64bit(pmc))
651 attr.config1 |= PERF_ATTR_CFG1_COUNTER_64BIT;
652
653 attr.sample_period = compute_period(pmc, kvm_pmu_get_pmc_value(pmc));
654
655 event = perf_event_create_kernel_counter(&attr, -1, current,
656 kvm_pmu_perf_overflow, pmc);
657
658 if (IS_ERR(event)) {
659 pr_err_once("kvm: pmu event creation failed %ld\n",
660 PTR_ERR(event));
661 return;
662 }
663
664 pmc->perf_event = event;
665 }
666
667 /**
668 * kvm_pmu_set_counter_event_type - set selected counter to monitor some event
669 * @vcpu: The vcpu pointer
670 * @data: The data guest writes to PMXEVTYPER_EL0
671 * @select_idx: The number of selected counter
672 *
673 * When OS accesses PMXEVTYPER_EL0, that means it wants to set a PMC to count an
674 * event with given hardware event number. Here we call perf_event API to
675 * emulate this action and create a kernel perf event for it.
676 */
kvm_pmu_set_counter_event_type(struct kvm_vcpu * vcpu,u64 data,u64 select_idx)677 void kvm_pmu_set_counter_event_type(struct kvm_vcpu *vcpu, u64 data,
678 u64 select_idx)
679 {
680 struct kvm_pmc *pmc = kvm_vcpu_idx_to_pmc(vcpu, select_idx);
681 u64 reg;
682
683 if (!kvm_vcpu_has_pmu(vcpu))
684 return;
685
686 reg = counter_index_to_evtreg(pmc->idx);
687 __vcpu_sys_reg(vcpu, reg) = data & kvm_pmu_evtyper_mask(vcpu->kvm);
688
689 kvm_pmu_create_perf_event(pmc);
690 }
691
kvm_host_pmu_init(struct arm_pmu * pmu)692 void kvm_host_pmu_init(struct arm_pmu *pmu)
693 {
694 struct arm_pmu_entry *entry;
695
696 /*
697 * Check the sanitised PMU version for the system, as KVM does not
698 * support implementations where PMUv3 exists on a subset of CPUs.
699 */
700 if (!pmuv3_implemented(kvm_arm_pmu_get_pmuver_limit()))
701 return;
702
703 mutex_lock(&arm_pmus_lock);
704
705 entry = kmalloc(sizeof(*entry), GFP_KERNEL);
706 if (!entry)
707 goto out_unlock;
708
709 entry->arm_pmu = pmu;
710 list_add_tail(&entry->entry, &arm_pmus);
711
712 if (list_is_singular(&arm_pmus))
713 static_branch_enable(&kvm_arm_pmu_available);
714
715 out_unlock:
716 mutex_unlock(&arm_pmus_lock);
717 }
718
kvm_pmu_probe_armpmu(void)719 static struct arm_pmu *kvm_pmu_probe_armpmu(void)
720 {
721 struct arm_pmu *tmp, *pmu = NULL;
722 struct arm_pmu_entry *entry;
723 int cpu;
724
725 mutex_lock(&arm_pmus_lock);
726
727 /*
728 * It is safe to use a stale cpu to iterate the list of PMUs so long as
729 * the same value is used for the entirety of the loop. Given this, and
730 * the fact that no percpu data is used for the lookup there is no need
731 * to disable preemption.
732 *
733 * It is still necessary to get a valid cpu, though, to probe for the
734 * default PMU instance as userspace is not required to specify a PMU
735 * type. In order to uphold the preexisting behavior KVM selects the
736 * PMU instance for the core during vcpu init. A dependent use
737 * case would be a user with disdain of all things big.LITTLE that
738 * affines the VMM to a particular cluster of cores.
739 *
740 * In any case, userspace should just do the sane thing and use the UAPI
741 * to select a PMU type directly. But, be wary of the baggage being
742 * carried here.
743 */
744 cpu = raw_smp_processor_id();
745 list_for_each_entry(entry, &arm_pmus, entry) {
746 tmp = entry->arm_pmu;
747
748 if (cpumask_test_cpu(cpu, &tmp->supported_cpus)) {
749 pmu = tmp;
750 break;
751 }
752 }
753
754 mutex_unlock(&arm_pmus_lock);
755
756 return pmu;
757 }
758
kvm_pmu_get_pmceid(struct kvm_vcpu * vcpu,bool pmceid1)759 u64 kvm_pmu_get_pmceid(struct kvm_vcpu *vcpu, bool pmceid1)
760 {
761 unsigned long *bmap = vcpu->kvm->arch.pmu_filter;
762 u64 val, mask = 0;
763 int base, i, nr_events;
764
765 if (!kvm_vcpu_has_pmu(vcpu))
766 return 0;
767
768 if (!pmceid1) {
769 val = read_sysreg(pmceid0_el0);
770 /* always support CHAIN */
771 val |= BIT(ARMV8_PMUV3_PERFCTR_CHAIN);
772 base = 0;
773 } else {
774 val = read_sysreg(pmceid1_el0);
775 /*
776 * Don't advertise STALL_SLOT*, as PMMIR_EL0 is handled
777 * as RAZ
778 */
779 val &= ~(BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT - 32) |
780 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_FRONTEND - 32) |
781 BIT_ULL(ARMV8_PMUV3_PERFCTR_STALL_SLOT_BACKEND - 32));
782 base = 32;
783 }
784
785 if (!bmap)
786 return val;
787
788 nr_events = kvm_pmu_event_mask(vcpu->kvm) + 1;
789
790 for (i = 0; i < 32; i += 8) {
791 u64 byte;
792
793 byte = bitmap_get_value8(bmap, base + i);
794 mask |= byte << i;
795 if (nr_events >= (0x4000 + base + 32)) {
796 byte = bitmap_get_value8(bmap, 0x4000 + base + i);
797 mask |= byte << (32 + i);
798 }
799 }
800
801 return val & mask;
802 }
803
kvm_vcpu_reload_pmu(struct kvm_vcpu * vcpu)804 void kvm_vcpu_reload_pmu(struct kvm_vcpu *vcpu)
805 {
806 u64 mask = kvm_pmu_valid_counter_mask(vcpu);
807
808 kvm_pmu_handle_pmcr(vcpu, kvm_vcpu_read_pmcr(vcpu));
809
810 __vcpu_sys_reg(vcpu, PMOVSSET_EL0) &= mask;
811 __vcpu_sys_reg(vcpu, PMINTENSET_EL1) &= mask;
812 __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) &= mask;
813 }
814
kvm_arm_pmu_v3_enable(struct kvm_vcpu * vcpu)815 int kvm_arm_pmu_v3_enable(struct kvm_vcpu *vcpu)
816 {
817 if (!kvm_vcpu_has_pmu(vcpu))
818 return 0;
819
820 if (!vcpu->arch.pmu.created)
821 return -EINVAL;
822
823 /*
824 * A valid interrupt configuration for the PMU is either to have a
825 * properly configured interrupt number and using an in-kernel
826 * irqchip, or to not have an in-kernel GIC and not set an IRQ.
827 */
828 if (irqchip_in_kernel(vcpu->kvm)) {
829 int irq = vcpu->arch.pmu.irq_num;
830 /*
831 * If we are using an in-kernel vgic, at this point we know
832 * the vgic will be initialized, so we can check the PMU irq
833 * number against the dimensions of the vgic and make sure
834 * it's valid.
835 */
836 if (!irq_is_ppi(irq) && !vgic_valid_spi(vcpu->kvm, irq))
837 return -EINVAL;
838 } else if (kvm_arm_pmu_irq_initialized(vcpu)) {
839 return -EINVAL;
840 }
841
842 /* One-off reload of the PMU on first run */
843 kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu);
844
845 return 0;
846 }
847
kvm_arm_pmu_v3_init(struct kvm_vcpu * vcpu)848 static int kvm_arm_pmu_v3_init(struct kvm_vcpu *vcpu)
849 {
850 if (irqchip_in_kernel(vcpu->kvm)) {
851 int ret;
852
853 /*
854 * If using the PMU with an in-kernel virtual GIC
855 * implementation, we require the GIC to be already
856 * initialized when initializing the PMU.
857 */
858 if (!vgic_initialized(vcpu->kvm))
859 return -ENODEV;
860
861 if (!kvm_arm_pmu_irq_initialized(vcpu))
862 return -ENXIO;
863
864 ret = kvm_vgic_set_owner(vcpu, vcpu->arch.pmu.irq_num,
865 &vcpu->arch.pmu);
866 if (ret)
867 return ret;
868 }
869
870 init_irq_work(&vcpu->arch.pmu.overflow_work,
871 kvm_pmu_perf_overflow_notify_vcpu);
872
873 vcpu->arch.pmu.created = true;
874 return 0;
875 }
876
877 /*
878 * For one VM the interrupt type must be same for each vcpu.
879 * As a PPI, the interrupt number is the same for all vcpus,
880 * while as an SPI it must be a separate number per vcpu.
881 */
pmu_irq_is_valid(struct kvm * kvm,int irq)882 static bool pmu_irq_is_valid(struct kvm *kvm, int irq)
883 {
884 unsigned long i;
885 struct kvm_vcpu *vcpu;
886
887 kvm_for_each_vcpu(i, vcpu, kvm) {
888 if (!kvm_arm_pmu_irq_initialized(vcpu))
889 continue;
890
891 if (irq_is_ppi(irq)) {
892 if (vcpu->arch.pmu.irq_num != irq)
893 return false;
894 } else {
895 if (vcpu->arch.pmu.irq_num == irq)
896 return false;
897 }
898 }
899
900 return true;
901 }
902
903 /**
904 * kvm_arm_pmu_get_max_counters - Return the max number of PMU counters.
905 * @kvm: The kvm pointer
906 */
kvm_arm_pmu_get_max_counters(struct kvm * kvm)907 u8 kvm_arm_pmu_get_max_counters(struct kvm *kvm)
908 {
909 struct arm_pmu *arm_pmu = kvm->arch.arm_pmu;
910
911 /*
912 * The arm_pmu->cntr_mask considers the fixed counter(s) as well.
913 * Ignore those and return only the general-purpose counters.
914 */
915 return bitmap_weight(arm_pmu->cntr_mask, ARMV8_PMU_MAX_GENERAL_COUNTERS);
916 }
917
kvm_arm_set_pmu(struct kvm * kvm,struct arm_pmu * arm_pmu)918 static void kvm_arm_set_pmu(struct kvm *kvm, struct arm_pmu *arm_pmu)
919 {
920 lockdep_assert_held(&kvm->arch.config_lock);
921
922 kvm->arch.arm_pmu = arm_pmu;
923 kvm->arch.pmcr_n = kvm_arm_pmu_get_max_counters(kvm);
924 }
925
926 /**
927 * kvm_arm_set_default_pmu - No PMU set, get the default one.
928 * @kvm: The kvm pointer
929 *
930 * The observant among you will notice that the supported_cpus
931 * mask does not get updated for the default PMU even though it
932 * is quite possible the selected instance supports only a
933 * subset of cores in the system. This is intentional, and
934 * upholds the preexisting behavior on heterogeneous systems
935 * where vCPUs can be scheduled on any core but the guest
936 * counters could stop working.
937 */
kvm_arm_set_default_pmu(struct kvm * kvm)938 int kvm_arm_set_default_pmu(struct kvm *kvm)
939 {
940 struct arm_pmu *arm_pmu = kvm_pmu_probe_armpmu();
941
942 if (!arm_pmu)
943 return -ENODEV;
944
945 kvm_arm_set_pmu(kvm, arm_pmu);
946 return 0;
947 }
948
kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu * vcpu,int pmu_id)949 static int kvm_arm_pmu_v3_set_pmu(struct kvm_vcpu *vcpu, int pmu_id)
950 {
951 struct kvm *kvm = vcpu->kvm;
952 struct arm_pmu_entry *entry;
953 struct arm_pmu *arm_pmu;
954 int ret = -ENXIO;
955
956 lockdep_assert_held(&kvm->arch.config_lock);
957 mutex_lock(&arm_pmus_lock);
958
959 list_for_each_entry(entry, &arm_pmus, entry) {
960 arm_pmu = entry->arm_pmu;
961 if (arm_pmu->pmu.type == pmu_id) {
962 if (kvm_vm_has_ran_once(kvm) ||
963 (kvm->arch.pmu_filter && kvm->arch.arm_pmu != arm_pmu)) {
964 ret = -EBUSY;
965 break;
966 }
967
968 kvm_arm_set_pmu(kvm, arm_pmu);
969 cpumask_copy(kvm->arch.supported_cpus, &arm_pmu->supported_cpus);
970 ret = 0;
971 break;
972 }
973 }
974
975 mutex_unlock(&arm_pmus_lock);
976 return ret;
977 }
978
kvm_arm_pmu_v3_set_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)979 int kvm_arm_pmu_v3_set_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
980 {
981 struct kvm *kvm = vcpu->kvm;
982
983 lockdep_assert_held(&kvm->arch.config_lock);
984
985 if (!kvm_vcpu_has_pmu(vcpu))
986 return -ENODEV;
987
988 if (vcpu->arch.pmu.created)
989 return -EBUSY;
990
991 switch (attr->attr) {
992 case KVM_ARM_VCPU_PMU_V3_IRQ: {
993 int __user *uaddr = (int __user *)(long)attr->addr;
994 int irq;
995
996 if (!irqchip_in_kernel(kvm))
997 return -EINVAL;
998
999 if (get_user(irq, uaddr))
1000 return -EFAULT;
1001
1002 /* The PMU overflow interrupt can be a PPI or a valid SPI. */
1003 if (!(irq_is_ppi(irq) || irq_is_spi(irq)))
1004 return -EINVAL;
1005
1006 if (!pmu_irq_is_valid(kvm, irq))
1007 return -EINVAL;
1008
1009 if (kvm_arm_pmu_irq_initialized(vcpu))
1010 return -EBUSY;
1011
1012 kvm_debug("Set kvm ARM PMU irq: %d\n", irq);
1013 vcpu->arch.pmu.irq_num = irq;
1014 return 0;
1015 }
1016 case KVM_ARM_VCPU_PMU_V3_FILTER: {
1017 u8 pmuver = kvm_arm_pmu_get_pmuver_limit();
1018 struct kvm_pmu_event_filter __user *uaddr;
1019 struct kvm_pmu_event_filter filter;
1020 int nr_events;
1021
1022 /*
1023 * Allow userspace to specify an event filter for the entire
1024 * event range supported by PMUVer of the hardware, rather
1025 * than the guest's PMUVer for KVM backward compatibility.
1026 */
1027 nr_events = __kvm_pmu_event_mask(pmuver) + 1;
1028
1029 uaddr = (struct kvm_pmu_event_filter __user *)(long)attr->addr;
1030
1031 if (copy_from_user(&filter, uaddr, sizeof(filter)))
1032 return -EFAULT;
1033
1034 if (((u32)filter.base_event + filter.nevents) > nr_events ||
1035 (filter.action != KVM_PMU_EVENT_ALLOW &&
1036 filter.action != KVM_PMU_EVENT_DENY))
1037 return -EINVAL;
1038
1039 if (kvm_vm_has_ran_once(kvm))
1040 return -EBUSY;
1041
1042 if (!kvm->arch.pmu_filter) {
1043 kvm->arch.pmu_filter = bitmap_alloc(nr_events, GFP_KERNEL_ACCOUNT);
1044 if (!kvm->arch.pmu_filter)
1045 return -ENOMEM;
1046
1047 /*
1048 * The default depends on the first applied filter.
1049 * If it allows events, the default is to deny.
1050 * Conversely, if the first filter denies a set of
1051 * events, the default is to allow.
1052 */
1053 if (filter.action == KVM_PMU_EVENT_ALLOW)
1054 bitmap_zero(kvm->arch.pmu_filter, nr_events);
1055 else
1056 bitmap_fill(kvm->arch.pmu_filter, nr_events);
1057 }
1058
1059 if (filter.action == KVM_PMU_EVENT_ALLOW)
1060 bitmap_set(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
1061 else
1062 bitmap_clear(kvm->arch.pmu_filter, filter.base_event, filter.nevents);
1063
1064 return 0;
1065 }
1066 case KVM_ARM_VCPU_PMU_V3_SET_PMU: {
1067 int __user *uaddr = (int __user *)(long)attr->addr;
1068 int pmu_id;
1069
1070 if (get_user(pmu_id, uaddr))
1071 return -EFAULT;
1072
1073 return kvm_arm_pmu_v3_set_pmu(vcpu, pmu_id);
1074 }
1075 case KVM_ARM_VCPU_PMU_V3_INIT:
1076 return kvm_arm_pmu_v3_init(vcpu);
1077 }
1078
1079 return -ENXIO;
1080 }
1081
kvm_arm_pmu_v3_get_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1082 int kvm_arm_pmu_v3_get_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1083 {
1084 switch (attr->attr) {
1085 case KVM_ARM_VCPU_PMU_V3_IRQ: {
1086 int __user *uaddr = (int __user *)(long)attr->addr;
1087 int irq;
1088
1089 if (!irqchip_in_kernel(vcpu->kvm))
1090 return -EINVAL;
1091
1092 if (!kvm_vcpu_has_pmu(vcpu))
1093 return -ENODEV;
1094
1095 if (!kvm_arm_pmu_irq_initialized(vcpu))
1096 return -ENXIO;
1097
1098 irq = vcpu->arch.pmu.irq_num;
1099 return put_user(irq, uaddr);
1100 }
1101 }
1102
1103 return -ENXIO;
1104 }
1105
kvm_arm_pmu_v3_has_attr(struct kvm_vcpu * vcpu,struct kvm_device_attr * attr)1106 int kvm_arm_pmu_v3_has_attr(struct kvm_vcpu *vcpu, struct kvm_device_attr *attr)
1107 {
1108 switch (attr->attr) {
1109 case KVM_ARM_VCPU_PMU_V3_IRQ:
1110 case KVM_ARM_VCPU_PMU_V3_INIT:
1111 case KVM_ARM_VCPU_PMU_V3_FILTER:
1112 case KVM_ARM_VCPU_PMU_V3_SET_PMU:
1113 if (kvm_vcpu_has_pmu(vcpu))
1114 return 0;
1115 }
1116
1117 return -ENXIO;
1118 }
1119
kvm_arm_pmu_get_pmuver_limit(void)1120 u8 kvm_arm_pmu_get_pmuver_limit(void)
1121 {
1122 u64 tmp;
1123
1124 tmp = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1);
1125 tmp = cpuid_feature_cap_perfmon_field(tmp,
1126 ID_AA64DFR0_EL1_PMUVer_SHIFT,
1127 ID_AA64DFR0_EL1_PMUVer_V3P5);
1128 return FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), tmp);
1129 }
1130
1131 /**
1132 * kvm_vcpu_read_pmcr - Read PMCR_EL0 register for the vCPU
1133 * @vcpu: The vcpu pointer
1134 */
kvm_vcpu_read_pmcr(struct kvm_vcpu * vcpu)1135 u64 kvm_vcpu_read_pmcr(struct kvm_vcpu *vcpu)
1136 {
1137 u64 pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0);
1138
1139 return u64_replace_bits(pmcr, vcpu->kvm->arch.pmcr_n, ARMV8_PMU_PMCR_N);
1140 }
1141