Skip to content

Commit d82e0df

Browse files
author
Marc Zyngier
committed
KVM: arm64: PMU: Allow ID_DFR0_EL1.PerfMon to be set from userspace
Allow userspace to write ID_DFR0_EL1, on the condition that only the PerfMon field can be altered and be something that is compatible with what was computed for the AArch64 view of 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 60e651f commit d82e0df

File tree

1 file changed

+56
-1
lines changed

1 file changed

+56
-1
lines changed

arch/arm64/kvm/sys_regs.c

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1070,6 +1070,19 @@ static u8 vcpu_pmuver(const struct kvm_vcpu *vcpu)
10701070
return vcpu->kvm->arch.dfr0_pmuver.unimp;
10711071
}
10721072

1073+
static u8 perfmon_to_pmuver(u8 perfmon)
1074+
{
1075+
switch (perfmon) {
1076+
case ID_DFR0_PERFMON_8_0:
1077+
return ID_AA64DFR0_EL1_PMUVer_IMP;
1078+
case ID_DFR0_PERFMON_IMP_DEF:
1079+
return ID_AA64DFR0_EL1_PMUVer_IMP_DEF;
1080+
default:
1081+
/* Anything ARMv8.1+ and NI have the same value. For now. */
1082+
return perfmon;
1083+
}
1084+
}
1085+
10731086
static u8 pmuver_to_perfmon(u8 pmuver)
10741087
{
10751088
switch (pmuver) {
@@ -1281,6 +1294,46 @@ static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu,
12811294
return 0;
12821295
}
12831296

1297+
static int set_id_dfr0_el1(struct kvm_vcpu *vcpu,
1298+
const struct sys_reg_desc *rd,
1299+
u64 val)
1300+
{
1301+
u8 perfmon, host_perfmon;
1302+
bool valid_pmu;
1303+
1304+
host_perfmon = pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit());
1305+
1306+
/*
1307+
* Allow DFR0_EL1.PerfMon to be set from userspace as long as
1308+
* it doesn't promise more than what the HW gives us on the
1309+
* AArch64 side (as everything is emulated with that), and
1310+
* that this is a PMUv3.
1311+
*/
1312+
perfmon = FIELD_GET(ARM64_FEATURE_MASK(ID_DFR0_PERFMON), val);
1313+
if ((perfmon != ID_DFR0_PERFMON_IMP_DEF && perfmon > host_perfmon) ||
1314+
(perfmon != 0 && perfmon < ID_DFR0_PERFMON_8_0))
1315+
return -EINVAL;
1316+
1317+
valid_pmu = (perfmon != 0 && perfmon != ID_DFR0_PERFMON_IMP_DEF);
1318+
1319+
/* Make sure view register and PMU support do match */
1320+
if (kvm_vcpu_has_pmu(vcpu) != valid_pmu)
1321+
return -EINVAL;
1322+
1323+
/* We can only differ with PerfMon, and anything else is an error */
1324+
val ^= read_id_reg(vcpu, rd);
1325+
val &= ~ARM64_FEATURE_MASK(ID_DFR0_PERFMON);
1326+
if (val)
1327+
return -EINVAL;
1328+
1329+
if (valid_pmu)
1330+
vcpu->kvm->arch.dfr0_pmuver.imp = perfmon_to_pmuver(perfmon);
1331+
else
1332+
vcpu->kvm->arch.dfr0_pmuver.unimp = perfmon_to_pmuver(perfmon);
1333+
1334+
return 0;
1335+
}
1336+
12841337
/*
12851338
* cpufeature ID register user accessors
12861339
*
@@ -1502,7 +1555,9 @@ static const struct sys_reg_desc sys_reg_descs[] = {
15021555
/* CRm=1 */
15031556
AA32_ID_SANITISED(ID_PFR0_EL1),
15041557
AA32_ID_SANITISED(ID_PFR1_EL1),
1505-
AA32_ID_SANITISED(ID_DFR0_EL1),
1558+
{ SYS_DESC(SYS_ID_DFR0_EL1), .access = access_id_reg,
1559+
.get_user = get_id_reg, .set_user = set_id_dfr0_el1,
1560+
.visibility = aa32_id_visibility, },
15061561
ID_HIDDEN(ID_AFR0_EL1),
15071562
AA32_ID_SANITISED(ID_MMFR0_EL1),
15081563
AA32_ID_SANITISED(ID_MMFR1_EL1),

0 commit comments

Comments
 (0)