Skip to content

Commit 96b0eeb

Browse files
Shannon ZhaoMarc Zyngier
authored andcommitted
arm64: KVM: Add access handler for PMCNTENSET and PMCNTENCLR register
Since the reset value of PMCNTENSET and PMCNTENCLR is UNKNOWN, use reset_unknown for its reset handler. Add a handler to emulate writing PMCNTENSET or PMCNTENCLR register. When writing to PMCNTENSET, call perf_event_enable to enable the perf event. When writing to PMCNTENCLR, call perf_event_disable to disable the perf event. Signed-off-by: Shannon Zhao <shannon.zhao@linaro.org> Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 051ff58 commit 96b0eeb

4 files changed

Lines changed: 107 additions & 4 deletions

File tree

‎arch/arm64/include/asm/kvm_host.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ enum vcpu_sysreg {
123123
PMEVCNTR0_EL0, /* Event Counter Register (0-30) */
124124
PMEVCNTR30_EL0 = PMEVCNTR0_EL0 + 30,
125125
PMCCNTR_EL0, /* Cycle Counter Register */
126+
PMCNTENSET_EL0, /* Count Enable Set Register */
126127

127128
/* 32bit specific registers. Keep them at the end of the range */
128129
DACR32_EL2, /* Domain Access Control Register */

‎arch/arm64/kvm/sys_regs.c‎

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -563,6 +563,33 @@ static bool access_pmu_evcntr(struct kvm_vcpu *vcpu,
563563
return true;
564564
}
565565

566+
static bool access_pmcnten(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
567+
const struct sys_reg_desc *r)
568+
{
569+
u64 val, mask;
570+
571+
if (!kvm_arm_pmu_v3_ready(vcpu))
572+
return trap_raz_wi(vcpu, p, r);
573+
574+
mask = kvm_pmu_valid_counter_mask(vcpu);
575+
if (p->is_write) {
576+
val = p->regval & mask;
577+
if (r->Op2 & 0x1) {
578+
/* accessing PMCNTENSET_EL0 */
579+
vcpu_sys_reg(vcpu, PMCNTENSET_EL0) |= val;
580+
kvm_pmu_enable_counter(vcpu, val);
581+
} else {
582+
/* accessing PMCNTENCLR_EL0 */
583+
vcpu_sys_reg(vcpu, PMCNTENSET_EL0) &= ~val;
584+
kvm_pmu_disable_counter(vcpu, val);
585+
}
586+
} else {
587+
p->regval = vcpu_sys_reg(vcpu, PMCNTENSET_EL0) & mask;
588+
}
589+
590+
return true;
591+
}
592+
566593
/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
567594
#define DBG_BCR_BVR_WCR_WVR_EL1(n) \
568595
/* DBGBVRn_EL1 */ \
@@ -757,10 +784,10 @@ static const struct sys_reg_desc sys_reg_descs[] = {
757784
access_pmcr, reset_pmcr, },
758785
/* PMCNTENSET_EL0 */
759786
{ Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
760-
trap_raz_wi },
787+
access_pmcnten, reset_unknown, PMCNTENSET_EL0 },
761788
/* PMCNTENCLR_EL0 */
762789
{ Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b010),
763-
trap_raz_wi },
790+
access_pmcnten, NULL, PMCNTENSET_EL0 },
764791
/* PMOVSCLR_EL0 */
765792
{ Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b011),
766793
trap_raz_wi },
@@ -1057,8 +1084,8 @@ static const struct sys_reg_desc cp15_regs[] = {
10571084

10581085
/* PMU */
10591086
{ Op1( 0), CRn( 9), CRm(12), Op2( 0), access_pmcr },
1060-
{ Op1( 0), CRn( 9), CRm(12), Op2( 1), trap_raz_wi },
1061-
{ Op1( 0), CRn( 9), CRm(12), Op2( 2), trap_raz_wi },
1087+
{ Op1( 0), CRn( 9), CRm(12), Op2( 1), access_pmcnten },
1088+
{ Op1( 0), CRn( 9), CRm(12), Op2( 2), access_pmcnten },
10621089
{ Op1( 0), CRn( 9), CRm(12), Op2( 3), trap_raz_wi },
10631090
{ Op1( 0), CRn( 9), CRm(12), Op2( 5), access_pmselr },
10641091
{ Op1( 0), CRn( 9), CRm(12), Op2( 6), access_pmceid },

‎include/kvm/arm_pmu.h‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ struct kvm_pmu {
4040
#define kvm_arm_pmu_v3_ready(v) ((v)->arch.pmu.ready)
4141
u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu, u64 select_idx);
4242
void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val);
43+
u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu);
44+
void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val);
45+
void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val);
4346
#else
4447
struct kvm_pmu {
4548
};
@@ -52,6 +55,12 @@ static inline u64 kvm_pmu_get_counter_value(struct kvm_vcpu *vcpu,
5255
}
5356
static inline void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu,
5457
u64 select_idx, u64 val) {}
58+
static inline u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
59+
{
60+
return 0;
61+
}
62+
static inline void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val) {}
63+
static inline void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val) {}
5564
#endif
5665

5766
#endif

‎virt/kvm/arm/pmu.c‎

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,69 @@ void kvm_pmu_set_counter_value(struct kvm_vcpu *vcpu, u64 select_idx, u64 val)
6161
? PMCCNTR_EL0 : PMEVCNTR0_EL0 + select_idx;
6262
vcpu_sys_reg(vcpu, reg) += (s64)val - kvm_pmu_get_counter_value(vcpu, select_idx);
6363
}
64+
65+
u64 kvm_pmu_valid_counter_mask(struct kvm_vcpu *vcpu)
66+
{
67+
u64 val = vcpu_sys_reg(vcpu, PMCR_EL0) >> ARMV8_PMU_PMCR_N_SHIFT;
68+
69+
val &= ARMV8_PMU_PMCR_N_MASK;
70+
if (val == 0)
71+
return BIT(ARMV8_PMU_CYCLE_IDX);
72+
else
73+
return GENMASK(val - 1, 0) | BIT(ARMV8_PMU_CYCLE_IDX);
74+
}
75+
76+
/**
77+
* kvm_pmu_enable_counter - enable selected PMU counter
78+
* @vcpu: The vcpu pointer
79+
* @val: the value guest writes to PMCNTENSET register
80+
*
81+
* Call perf_event_enable to start counting the perf event
82+
*/
83+
void kvm_pmu_enable_counter(struct kvm_vcpu *vcpu, u64 val)
84+
{
85+
int i;
86+
struct kvm_pmu *pmu = &vcpu->arch.pmu;
87+
struct kvm_pmc *pmc;
88+
89+
if (!(vcpu_sys_reg(vcpu, PMCR_EL0) & ARMV8_PMU_PMCR_E) || !val)
90+
return;
91+
92+
for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
93+
if (!(val & BIT(i)))
94+
continue;
95+
96+
pmc = &pmu->pmc[i];
97+
if (pmc->perf_event) {
98+
perf_event_enable(pmc->perf_event);
99+
if (pmc->perf_event->state != PERF_EVENT_STATE_ACTIVE)
100+
kvm_debug("fail to enable perf event\n");
101+
}
102+
}
103+
}
104+
105+
/**
106+
* kvm_pmu_disable_counter - disable selected PMU counter
107+
* @vcpu: The vcpu pointer
108+
* @val: the value guest writes to PMCNTENCLR register
109+
*
110+
* Call perf_event_disable to stop counting the perf event
111+
*/
112+
void kvm_pmu_disable_counter(struct kvm_vcpu *vcpu, u64 val)
113+
{
114+
int i;
115+
struct kvm_pmu *pmu = &vcpu->arch.pmu;
116+
struct kvm_pmc *pmc;
117+
118+
if (!val)
119+
return;
120+
121+
for (i = 0; i < ARMV8_PMU_MAX_COUNTERS; i++) {
122+
if (!(val & BIT(i)))
123+
continue;
124+
125+
pmc = &pmu->pmc[i];
126+
if (pmc->perf_event)
127+
perf_event_disable(pmc->perf_event);
128+
}
129+
}

0 commit comments

Comments
 (0)