Skip to content

Commit ab94683

Browse files
Shannon ZhaoMarc Zyngier
Shannon Zhao
authored and
Marc Zyngier
committed
arm64: KVM: Add access handler for PMCR register
Add reset handler which gets host value of PMCR_EL0 and make writable bits architecturally UNKNOWN except PMCR.E which is zero. Add an access handler for PMCR. Signed-off-by: Shannon Zhao <[email protected]> Reviewed-by: Andrew Jones <[email protected]> Signed-off-by: Marc Zyngier <[email protected]>
1 parent 04fe472 commit ab94683

File tree

3 files changed

+47
-2
lines changed

3 files changed

+47
-2
lines changed

arch/arm64/include/asm/kvm_host.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,9 @@ enum vcpu_sysreg {
117117
MDSCR_EL1, /* Monitor Debug System Control Register */
118118
MDCCINT_EL1, /* Monitor Debug Comms Channel Interrupt Enable Reg */
119119

120+
/* Performance Monitors Registers */
121+
PMCR_EL0, /* Control Register */
122+
120123
/* 32bit specific registers. Keep them at the end of the range */
121124
DACR32_EL2, /* Domain Access Control Register */
122125
IFSR32_EL2, /* Instruction Fault Status Register */

arch/arm64/kvm/sys_regs.c

Lines changed: 40 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include <asm/kvm_emulate.h>
3535
#include <asm/kvm_host.h>
3636
#include <asm/kvm_mmu.h>
37+
#include <asm/perf_event.h>
3738

3839
#include <trace/events/kvm.h>
3940

@@ -439,6 +440,43 @@ static void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
439440
vcpu_sys_reg(vcpu, MPIDR_EL1) = (1ULL << 31) | mpidr;
440441
}
441442

443+
static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
444+
{
445+
u64 pmcr, val;
446+
447+
asm volatile("mrs %0, pmcr_el0\n" : "=r" (pmcr));
448+
/* Writable bits of PMCR_EL0 (ARMV8_PMU_PMCR_MASK) is reset to UNKNOWN
449+
* except PMCR.E resetting to zero.
450+
*/
451+
val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
452+
| (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
453+
vcpu_sys_reg(vcpu, PMCR_EL0) = val;
454+
}
455+
456+
static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
457+
const struct sys_reg_desc *r)
458+
{
459+
u64 val;
460+
461+
if (!kvm_arm_pmu_v3_ready(vcpu))
462+
return trap_raz_wi(vcpu, p, r);
463+
464+
if (p->is_write) {
465+
/* Only update writeable bits of PMCR */
466+
val = vcpu_sys_reg(vcpu, PMCR_EL0);
467+
val &= ~ARMV8_PMU_PMCR_MASK;
468+
val |= p->regval & ARMV8_PMU_PMCR_MASK;
469+
vcpu_sys_reg(vcpu, PMCR_EL0) = val;
470+
} else {
471+
/* PMCR.P & PMCR.C are RAZ */
472+
val = vcpu_sys_reg(vcpu, PMCR_EL0)
473+
& ~(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C);
474+
p->regval = val;
475+
}
476+
477+
return true;
478+
}
479+
442480
/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
443481
#define DBG_BCR_BVR_WCR_WVR_EL1(n) \
444482
/* DBGBVRn_EL1 */ \
@@ -623,7 +661,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
623661

624662
/* PMCR_EL0 */
625663
{ Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b000),
626-
trap_raz_wi },
664+
access_pmcr, reset_pmcr, },
627665
/* PMCNTENSET_EL0 */
628666
{ Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
629667
trap_raz_wi },
@@ -885,7 +923,7 @@ static const struct sys_reg_desc cp15_regs[] = {
885923
{ Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
886924

887925
/* PMU */
888-
{ Op1( 0), CRn( 9), CRm(12), Op2( 0), trap_raz_wi },
926+
{ Op1( 0), CRn( 9), CRm(12), Op2( 0), access_pmcr },
889927
{ Op1( 0), CRn( 9), CRm(12), Op2( 1), trap_raz_wi },
890928
{ Op1( 0), CRn( 9), CRm(12), Op2( 2), trap_raz_wi },
891929
{ Op1( 0), CRn( 9), CRm(12), Op2( 3), trap_raz_wi },

include/kvm/arm_pmu.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,13 @@ struct kvm_pmu {
3434
struct kvm_pmc pmc[ARMV8_PMU_MAX_COUNTERS];
3535
bool ready;
3636
};
37+
38+
#define kvm_arm_pmu_v3_ready(v) ((v)->arch.pmu.ready)
3739
#else
3840
struct kvm_pmu {
3941
};
42+
43+
#define kvm_arm_pmu_v3_ready(v) (false)
4044
#endif
4145

4246
#endif

0 commit comments

Comments
 (0)