Skip to content

Commit 03fdfb2

Browse files
author
Marc Zyngier
committed
KVM: arm64: Don't write junk to sysregs on reset
At the moment, the way we reset system registers is mildly insane: We write junk to them, call the reset functions, and then check that we have something else in them. The "fun" thing is that this can happen while the guest is running (PSCI, for example). If anything in KVM has to evaluate the state of a system register while junk is in there, bad thing may happen. Let's stop doing that. Instead, we track that we have called a reset function for that register, and assume that the reset function has done something. This requires fixing a couple of sysreg refinition in the trap table. In the end, the very need of this reset check is pretty dubious, as it doesn't check everything (a lot of the sysregs leave outside of the sys_regs[] array). It may well be axed in the near future. Tested-by: Zenghui Yu <yuzenghui@huawei.com> Signed-off-by: Marc Zyngier <maz@kernel.org>
1 parent 5eeaf10 commit 03fdfb2

1 file changed

Lines changed: 18 additions & 14 deletions

File tree

‎arch/arm64/kvm/sys_regs.c‎

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -632,7 +632,7 @@ static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
632632
*/
633633
val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
634634
| (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
635-
__vcpu_sys_reg(vcpu, PMCR_EL0) = val;
635+
__vcpu_sys_reg(vcpu, r->reg) = val;
636636
}
637637

638638
static bool check_pmu_access_disabled(struct kvm_vcpu *vcpu, u64 flags)
@@ -981,13 +981,13 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
981981
/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
982982
#define DBG_BCR_BVR_WCR_WVR_EL1(n) \
983983
{ SYS_DESC(SYS_DBGBVRn_EL1(n)), \
984-
trap_bvr, reset_bvr, n, 0, get_bvr, set_bvr }, \
984+
trap_bvr, reset_bvr, 0, 0, get_bvr, set_bvr }, \
985985
{ SYS_DESC(SYS_DBGBCRn_EL1(n)), \
986-
trap_bcr, reset_bcr, n, 0, get_bcr, set_bcr }, \
986+
trap_bcr, reset_bcr, 0, 0, get_bcr, set_bcr }, \
987987
{ SYS_DESC(SYS_DBGWVRn_EL1(n)), \
988-
trap_wvr, reset_wvr, n, 0, get_wvr, set_wvr }, \
988+
trap_wvr, reset_wvr, 0, 0, get_wvr, set_wvr }, \
989989
{ SYS_DESC(SYS_DBGWCRn_EL1(n)), \
990-
trap_wcr, reset_wcr, n, 0, get_wcr, set_wcr }
990+
trap_wcr, reset_wcr, 0, 0, get_wcr, set_wcr }
991991

992992
/* Macro to expand the PMEVCNTRn_EL0 register */
993993
#define PMU_PMEVCNTR_EL0(n) \
@@ -1540,7 +1540,7 @@ static const struct sys_reg_desc sys_reg_descs[] = {
15401540
{ SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 },
15411541
{ SYS_DESC(SYS_CTR_EL0), access_ctr },
15421542

1543-
{ SYS_DESC(SYS_PMCR_EL0), access_pmcr, reset_pmcr, },
1543+
{ SYS_DESC(SYS_PMCR_EL0), access_pmcr, reset_pmcr, PMCR_EL0 },
15441544
{ SYS_DESC(SYS_PMCNTENSET_EL0), access_pmcnten, reset_unknown, PMCNTENSET_EL0 },
15451545
{ SYS_DESC(SYS_PMCNTENCLR_EL0), access_pmcnten, NULL, PMCNTENSET_EL0 },
15461546
{ SYS_DESC(SYS_PMOVSCLR_EL0), access_pmovs, NULL, PMOVSSET_EL0 },
@@ -2254,13 +2254,19 @@ static int emulate_sys_reg(struct kvm_vcpu *vcpu,
22542254
}
22552255

22562256
static void reset_sys_reg_descs(struct kvm_vcpu *vcpu,
2257-
const struct sys_reg_desc *table, size_t num)
2257+
const struct sys_reg_desc *table, size_t num,
2258+
unsigned long *bmap)
22582259
{
22592260
unsigned long i;
22602261

22612262
for (i = 0; i < num; i++)
2262-
if (table[i].reset)
2263+
if (table[i].reset) {
2264+
int reg = table[i].reg;
2265+
22632266
table[i].reset(vcpu, &table[i]);
2267+
if (reg > 0 && reg < NR_SYS_REGS)
2268+
set_bit(reg, bmap);
2269+
}
22642270
}
22652271

22662272
/**
@@ -2774,18 +2780,16 @@ void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
27742780
{
27752781
size_t num;
27762782
const struct sys_reg_desc *table;
2777-
2778-
/* Catch someone adding a register without putting in reset entry. */
2779-
memset(&vcpu->arch.ctxt.sys_regs, 0x42, sizeof(vcpu->arch.ctxt.sys_regs));
2783+
DECLARE_BITMAP(bmap, NR_SYS_REGS) = { 0, };
27802784

27812785
/* Generic chip reset first (so target could override). */
2782-
reset_sys_reg_descs(vcpu, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
2786+
reset_sys_reg_descs(vcpu, sys_reg_descs, ARRAY_SIZE(sys_reg_descs), bmap);
27832787

27842788
table = get_target_table(vcpu->arch.target, true, &num);
2785-
reset_sys_reg_descs(vcpu, table, num);
2789+
reset_sys_reg_descs(vcpu, table, num, bmap);
27862790

27872791
for (num = 1; num < NR_SYS_REGS; num++) {
2788-
if (WARN(__vcpu_sys_reg(vcpu, num) == 0x4242424242424242,
2792+
if (WARN(!test_bit(num, bmap),
27892793
"Didn't reset __vcpu_sys_reg(%zi)\n", num))
27902794
break;
27912795
}

0 commit comments

Comments
 (0)