Skip to content

Commit 60e651f

Browse files
author
Marc Zyngier
committed
KVM: arm64: PMU: Allow ID_AA64DFR0_EL1.PMUver to be set from userspace
Allow userspace to write ID_AA64DFR0_EL1, on the condition that only the PMUver field can be altered and be at most the one that was initially computed for the guest. Reviewed-by: Reiji Watanabe <[email protected]> Signed-off-by: Marc Zyngier <[email protected]> Link: https://lore.kernel.org/r/[email protected]
1 parent 3d0dba5 commit 60e651f

File tree

1 file changed

+41
-1
lines changed

1 file changed

+41
-1
lines changed

arch/arm64/kvm/sys_regs.c

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1242,6 +1242,45 @@ static int set_id_aa64pfr0_el1(struct kvm_vcpu *vcpu,
12421242
return 0;
12431243
}
12441244

1245+
static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
1246+
const struct sys_reg_desc *rd,
1247+
u64 val)
1248+
{
1249+
u8 pmuver, host_pmuver;
1250+
bool valid_pmu;
1251+
1252+
host_pmuver = kvm_arm_pmu_get_pmuver_limit();
1253+
1254+
/*
1255+
* Allow AA64DFR0_EL1.PMUver to be set from userspace as long
1256+
* as it doesn't promise more than what the HW gives us. We
1257+
* allow an IMPDEF PMU though, only if no PMU is supported
1258+
* (KVM backward compatibility handling).
1259+
*/
1260+
pmuver = FIELD_GET(ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer), val);
1261+
if ((pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF && pmuver > host_pmuver))
1262+
return -EINVAL;
1263+
1264+
valid_pmu = (pmuver != 0 && pmuver != ID_AA64DFR0_EL1_PMUVer_IMP_DEF);
1265+
1266+
/* Make sure view register and PMU support do match */
1267+
if (kvm_vcpu_has_pmu(vcpu) != valid_pmu)
1268+
return -EINVAL;
1269+
1270+
/* We can only differ with PMUver, and anything else is an error */
1271+
val ^= read_id_reg(vcpu, rd);
1272+
val &= ~ARM64_FEATURE_MASK(ID_AA64DFR0_EL1_PMUVer);
1273+
if (val)
1274+
return -EINVAL;
1275+
1276+
if (valid_pmu)
1277+
vcpu->kvm->arch.dfr0_pmuver.imp = pmuver;
1278+
else
1279+
vcpu->kvm->arch.dfr0_pmuver.unimp = pmuver;
1280+
1281+
return 0;
1282+
}
1283+
12451284
/*
12461285
* cpufeature ID register user accessors
12471286
*
@@ -1503,7 +1542,8 @@ static const struct sys_reg_desc sys_reg_descs[] = {
15031542
ID_UNALLOCATED(4,7),
15041543

15051544
/* CRm=5 */
1506-
ID_SANITISED(ID_AA64DFR0_EL1),
1545+
{ SYS_DESC(SYS_ID_AA64DFR0_EL1), .access = access_id_reg,
1546+
.get_user = get_id_reg, .set_user = set_id_aa64dfr0_el1, },
15071547
ID_SANITISED(ID_AA64DFR1_EL1),
15081548
ID_UNALLOCATED(5,2),
15091549
ID_UNALLOCATED(5,3),

0 commit comments

Comments
 (0)