Userspace save and restore (KVM_GET_ONE_REG / KVM_SET_ONE_REG) must observe and preserve all VM-implemented PMU counters ([0..nr_pmu_counters-1] plus PMCCNTR_EL0), independent of the vCPU's current exception level or MDCR_EL2.HPMN. Currently, two UAPI callbacks still depend on guest execution context when a nested vCPU (KVM_ARM_VCPU_HAS_EL2) is paused below EL2 (!vcpu_is_el2(vcpu)) with MDCR_EL2.HPMN < nr_pmu_counters: - set_pmreg() and get_pmreg() (backing PMINTEN{SET,CLR}_EL1, PMCNTEN{SET,CLR}_EL0, and PMOVS{SET,CLR}_EL0) mask register values with kvm_pmu_accessible_counter_mask(), which strips L1 hypervisor counter bits [HPMN..N-1]. Saving a vCPU paused in L2 hides those bits, and restoring them while paused in L2 permanently clears them. - get_pmcr() calls kvm_vcpu_read_pmcr(), which substitutes MDCR_EL2.HPMN for PMCR_EL0.N below EL2, corrupting the saved PMCR_EL0.N value. Switch set_pmreg() and get_pmreg() back to kvm_pmu_implemented_counter_mask(), and have get_pmcr() populate PMCR_EL0.N directly from vcpu->kvm->arch.nr_pmu_counters using u64_replace_bits(). Keep kvm_vcpu_read_pmcr() and kvm_pmu_accessible_counter_mask() for guest trap paths. Fixes: 9a1c58cfefb0 ("KVM: arm64: nv: Adjust range of accessible PMCs according to HPMN") Fixes: 600f6fa5c90c ("KVM: arm64: Let kvm_vcpu_read_pmcr() return an EL-dependent value for PMCR_EL0.N") Signed-off-by: Colton Lewis --- arch/arm64/kvm/sys_regs.c | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/arch/arm64/kvm/sys_regs.c b/arch/arm64/kvm/sys_regs.c index 6e8bc4618870d..2d9ae781acff0 100644 --- a/arch/arm64/kvm/sys_regs.c +++ b/arch/arm64/kvm/sys_regs.c @@ -1081,8 +1081,8 @@ static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) pmcr |= ARMV8_PMU_PMCR_LC; /* - * The value of PMCR.N field is included when the - * vCPU register is read via kvm_vcpu_read_pmcr(). + * The value of PMCR.N field is included when the vCPU register + * is read via kvm_vcpu_read_pmcr() or get_pmcr(). */ __vcpu_assign_sys_reg(vcpu, r->reg, pmcr); @@ -1323,7 +1323,7 @@ static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, static int set_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 val) { - u64 mask = kvm_pmu_accessible_counter_mask(vcpu); + u64 mask = kvm_pmu_implemented_counter_mask(vcpu); __vcpu_assign_sys_reg(vcpu, r->reg, val & mask); kvm_make_request(KVM_REQ_RELOAD_PMU, vcpu); @@ -1333,7 +1333,7 @@ static int set_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 va static int get_pmreg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 *val) { - u64 mask = kvm_pmu_accessible_counter_mask(vcpu); + u64 mask = kvm_pmu_implemented_counter_mask(vcpu); *val = __vcpu_sys_reg(vcpu, r->reg) & mask; return 0; @@ -1505,7 +1505,9 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, static int get_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, u64 *val) { - *val = kvm_vcpu_read_pmcr(vcpu); + *val = u64_replace_bits(__vcpu_sys_reg(vcpu, PMCR_EL0), + vcpu->kvm->arch.nr_pmu_counters, + ARMV8_PMU_PMCR_N); return 0; } -- 2.56.1.708.g77570a6a1d-goog