Skip to contentSkip to conversation

target/arm: Fix AArch32 Secure banked reg migration

PATCH In review Peter Maydell 2 participants 17 messages Sep 7 – Sep 23

lore
Share & tools
lore
#0 lore raw

cover target/arm: Fix AArch32 Secure banked reg migration

This patchset fixes a very long standing bug where we didn't migrate the values of the Secure banked copy of an AArch32 banked coprocessor register (https://gitlab.com/qemu-project/qemu/-/work_items/467).
This is something that Tao Ding sent a patch for back in July:
https://lore.kernel.org/qemu-devel/[email protected]/
I didn't think that patch was quite right, but digging into it I found a slightly wider set of problems than I expected, plus testing on a bunch of configs was a bit tedious, so it's taken me a while to get something ready to send out. Sorry for the delay...

The underlying cause of the bug is that we store registers in our cpreg hashtable with a key with one format (32 bit, QEMU specific), but store them in the (index, value) lists for migration with an index of a different format (64 bit, same as KVM uses). Because KVM never supports EL3 its index format doesn't have a way to say "this is the Secure banked copy of this cpreg"; only our QEMU specific index does. The conversion functions kvm_to_cpreg_id() and cpreg_to_kvm_id() were written to assume that the KVM index was always for a NonSecure register.

The current code doesn't touch the CP_REG_AA32_NS_MASK bit when converting from the 32-bit ID format to a KVM ID, and it forces the NS bit to 1 when converting from a KVM ID to a cpreg. The effect of this on migration is that the indexes in cpreg_indexes[] have NS = 0 for the Secure banked registers and NS = 1 for the NS banked registers, but when we read and write the values into the CPUState by finding the register using kvm_to_cpreg_id(cpreg_indexes[i]) we always find the NS register, and so the S register values aren't actually migrated.

This went unnoticed most of the time because (a) most setups don't enable EL3 at all (b) AArch64 is unaffected (c) most users don't use migration (d) the case where we direct boot a Linux kernel in NS and the S regs remain at their reset values happened to work by luck. But it breaks migration of a guest which is running in S, such as Linux or u-boot on the imx boards.

Patch summary:
* Patch 1 is the fix for this bug; I included keeping migration compat from old QEMU here for the benefit of that case (d) that used to work.
* Patch 2 fixes a corner case for AArch32 v8 CPUs where we wouldn't migrate some secure banked registers because we incorrectly assumed an AArch64 regdef would do the job
* Patches 3 and 4 are minor tidyup
* Patches 5, 6, 7 fix some v7R and v8R bugs where we were exposing cpregs and the ATS insns when we should not (noticed because this bug caused the debug check in patch 7 to fire)
* patch 8 adds a debug check when QEMU is compiled in debug that every cpreg that we claimed is an ARM_CP_ALIAS to something else really does have some other cpreg handling the migration of its CPU state struct field. It's a bit heuristic but it did successfully report the missing migration handling for the bug fixed in patch 1, as well as the other things which patches 2-7 fix.

thanks
-- PMM

Peter Maydell (8):
target/arm: Keep the S/NS bit in migration data target/arm: Don't assume secure-banked registers are migrated by

    64-bit regs

target/arm: Drop uses of reset_ignore for banked AArch32 registers target/arm: Don't use arm_cp_reset_ignore when fieldoffset is 0 target/arm: Move ESR_EL1 definition to vmsa_pmsa_cp_reginfo target/arm: Don't set ARM_FEATURE_VAPA for v7 PMSA target/arm: Don't register 64-bit TTBR0, TTBR1 for PMSA target/arm: Add a debug check that all sysregs are migrated

 target/arm/cpregs.h  |  21 +++--
 target/arm/cpu.c     |   6 +-
 target/arm/cpu.h     |   6 ++
 target/arm/helper.c  | 198 ++++++++++++++++++++++++++++++++++++-------
 target/arm/machine.c |  80 +++++++++++++++++
 5 files changed, 273 insertions(+), 38 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/8 target/arm: Keep the S/NS bit in migration data

For Arm, the format we use on the wire for migration of sysregs (cpregs on AArch32) is a set of arrays, where cpreg_indexes[] holds the register IDs and cpreg_values[] holds the corresponding values. The ID format we use is KVM's ID format, which is 64 bits. This is not quite the same as the 32-bit ID format QEMU uses internally for things like the coprocessor register hashtable keys, and so we have conversion functions kvm_to_cpreg_id() and cpreg_to_kvm_id() to swap between them.

This conversion has never handled AArch32 Secure coprocessor registers correctly: the KVM ID format has no way to indicate a Secure register, because KVM VMs on AArch32 were always NonSecure. The current code doesn't touch the CP_REG_AA32_NS_MASK bit when converting from the 32-bit ID format to a KVM ID, and it forces the NS bit to 1 when converting from a KVM ID to a cpreg. The effect of this on migration is that the indexes in cpreg_indexes[] have NS = 0 for the Secure banked registers and NS = 1 for the NS banked registers, but when we read and write the values into the CPUState by finding the register using kvm_to_cpreg_id(cpreg_indexes[i]) we always find the NS register, and so the S register values aren't actually migrated.

This went unnoticed most of the time because a typical Linux guest is running only in NS and doesn't really care about the S register state, but it breaks migration of a guest which is running in S, such as Linux on the imx boards.

This would be more awkward to fix if KVM still supported AArch32 hosts, but luckily that was dropped some years ago, and AArch32 guests on an AArch64 host expose the sysregs as CP_REG_ARM64, not CP_REG_ARM32. So the only place where we use a CP_REG_ARM32 KVM register ID is for TCG migrations. We can therefore (continue to) steal a bit in the KVM format for the NS bit, with the semantics we've given it on the wire in the past. We just need to let kvm_to_cpreg_id() pass it through rather than forcing it to 1.

To retain compatibility with migration from older QEMU versions without this fix, we need to add a subsection to the migration data that tells the destination that it can trust the values for Secure banked registers. For incoming migrations, if we don't see this then we ignore all the values for Secure banked registers, giving the same behaviour as before. This means we continue to work for migrations where the Secure world exists but the guest is basically not using it, such as a direct kernel boot on boards which don't set arm_boot_info::secure_boot to true; this includes the cubieboard, bananapi and orangepi.

Attempting migration from a new QEMU to an older QEMU version will now fail cleanly (because the destination doesn't recognize the new subsection) rather than silently corrupting the Secure banked registers.

Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/467

---
 target/arm/cpregs.h  | 21 ++++++++----
 target/arm/cpu.h     |  6 ++++
 target/arm/machine.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
 3 files changed, 100 insertions(+), 7 deletions(-)
target/arm/cpregs.h +14 -7
index 9f2532fe667..ce651a4faa6 100644
@@ -215,7 +215,20 @@ enum {
 
 /*
  * Convert a full 64 bit KVM register ID to the truncated 32 bit
- * version used as a key for the coprocessor register hashtable
+ * version used as a key for the coprocessor register hashtable.
+ *
+ * Note that we deviate slightly from the KVM register ID format as
+ * used by the kernel for AArch32 cpregs, by using bit 29 as "1
+ * for NonSecure, 0 for Secure". (When KVM still supported AArch32
+ * hosts it didn't set this bit; all sysregs for KVM guests are
+ * NonSecure anyway.) This shouldn't cause any issues as KVM no longer
+ * supports AArch32 hosts (and other accelerators never did), so the
+ * only thing that generates KVM regids for AArch32 cpregs is QEMU
+ * TCG.
+ *
+ * The NS bit being in the KVM ID is implicit in the fact that we
+ * don't mask out CP_REG_AA32_NS_MASK in the conversions to and from
+ * the QEMU hashtable key ID format.
  */
 static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
 {
@@ -226,12 +239,6 @@ static inline uint32_t kvm_to_cpreg_id(uint64_t kvmid)
         if ((kvmid & CP_REG_SIZE_MASK) == CP_REG_SIZE_U64) {
             cpregid |= CP_REG_AA32_64BIT_MASK;
         }
-
-        /*
-         * KVM is always non-secure so add the NS flag on AArch32 register
-         * entries.
-         */
-         cpregid |= CP_REG_AA32_NS_MASK;
     }
     return cpregid;
 }
target/arm/cpu.h +6 -0
index e3f931dba26..231fdf710b7 100644
@@ -1128,6 +1128,12 @@ struct ArchCPU {
     bool prop_pauth_qarma5;
     bool prop_lpa2;
 
+    /*
+     * Used only during migration, to handle back-compat with older QEMU
+     * that mishandled migration of AArch32 banked cpregs.
+     */
+    bool secure_banked_regs_ok;
+
     /* GM blocksize, in log_2(words), ie low 4 bits of GMID_EL0 */
     uint8_t gm_blocksize;
 
target/arm/machine.c +80 -0

122 lines not on this page · raw message

Signature · 1 line
2.43.0
Reviewed-by on v1 1/8
#2 lore raw

Re: [PATCH 1/8] target/arm: Keep the S/NS bit in migration data

Quoted 52 lines · Peter Maydell

For Arm, the format we use on the wire for migration of sysregs (cpregs on AArch32) is a set of arrays, where cpreg_indexes[] holds the register IDs and cpreg_values[] holds the corresponding values. The ID format we use is KVM's ID format, which is 64 bits. This is not quite the same as the 32-bit ID format QEMU uses internally for things like the coprocessor register hashtable keys, and so we have conversion functions kvm_to_cpreg_id() and cpreg_to_kvm_id() to swap between them.

This conversion has never handled AArch32 Secure coprocessor registers correctly: the KVM ID format has no way to indicate a Secure register, because KVM VMs on AArch32 were always NonSecure. The current code doesn't touch the CP_REG_AA32_NS_MASK bit when converting from the 32-bit ID format to a KVM ID, and it forces the NS bit to 1 when converting from a KVM ID to a cpreg. The effect of this on migration is that the indexes in cpreg_indexes[] have NS = 0 for the Secure banked registers and NS = 1 for the NS banked registers, but when we read and write the values into the CPUState by finding the register using kvm_to_cpreg_id(cpreg_indexes[i]) we always find the NS register, and so the S register values aren't actually migrated.

This went unnoticed most of the time because a typical Linux guest is running only in NS and doesn't really care about the S register state, but it breaks migration of a guest which is running in S, such as Linux on the imx boards.

This would be more awkward to fix if KVM still supported AArch32 hosts, but luckily that was dropped some years ago, and AArch32 guests on an AArch64 host expose the sysregs as CP_REG_ARM64, not CP_REG_ARM32. So the only place where we use a CP_REG_ARM32 KVM register ID is for TCG migrations. We can therefore (continue to) steal a bit in the KVM format for the NS bit, with the semantics we've given it on the wire in the past. We just need to let kvm_to_cpreg_id() pass it through rather than forcing it to 1.

To retain compatibility with migration from older QEMU versions without this fix, we need to add a subsection to the migration data that tells the destination that it can trust the values for Secure banked registers. For incoming migrations, if we don't see this then we ignore all the values for Secure banked registers, giving the same behaviour as before. This means we continue to work for migrations where the Secure world exists but the guest is basically not using it, such as a direct kernel boot on boards which don't set arm_boot_info::secure_boot to true; this includes the cubieboard, bananapi and orangepi.

Attempting migration from a new QEMU to an older QEMU version will now fail cleanly (because the destination doesn't recognize the new subsection) rather than silently corrupting the Secure banked registers.

Quoted 7 lines

Resolves:https://gitlab.com/qemu-project/qemu/-/work_items/467

---
  target/arm/cpregs.h  | 21 ++++++++----
  target/arm/cpu.h     |  6 ++++
  target/arm/machine.c | 80 ++++++++++++++++++++++++++++++++++++++++++++
  3 files changed, 100 insertions(+), 7 deletions(-)

r~

#3 lore raw

PATCH 2/8 target/arm: Don't assume secure-banked registers are migrated by 64-bit regs

In reply to Peter Maydell

In add_cpreg_to_hashtable() we have some code that automatically marks the secure version of an AArch32 banked cpreg as ARM_CP_ALIAS under two conditions:

1) If the register has both 32-bit and 64-bit instances then we can count on the 64-bit instance taking care of the non-secure bank.

2) If ARMv8 is enabled then we can count on a 64-bit version taking care of the secure bank. This requires that separate 32 and 64-bit definitions are provided.

Item 1 is correct: if the ARMCPRegInfo has ARM_CP_STATE_BOTH then we will always add it as an AA32 entry (which then splits into an S entry with bank_fieldoffsets[0] and an NS entry with bank_fieldoffsets[1]) and as an AA64 entry (which uses bank_fieldoffsets[1]), so the AA64 entry will always be present to cover the NS banked AA32 entry.

Item 2 is not correct: the idea was that because typically AArch32 Secure banked registers are (non-architecturally) mapped to EL3 AArch64 registers, the AArch64 EL3 register would handle things (and that we would be adding the AArch64 register to the hashtable even if the CPU didn't have AArch64).

However, this fails in several cases:
(a) registers like CSSELR, where there is only a CSSELR_EL1, and no _EL3 version. CSSELR_S's underlying state isn't migrated
(b) registers like TPIDRURW, where the secure banked register isn't mapped to any AArch64 register, so the state isn't migrated
(c) IFAR_S and DFAR_S are odd special cases, because they map to AArch32 HIFAR and HDFAR, which in turn map to AArch64 FAR_EL2. If the CPU doesn't have EL2, we don't register FAR_EL2 and the IFAR_S and DFAR_S registers don't get migrated.

More generally, the architecture dropped the concept of documenting secure AArch32 registers as being "non-architecturally mapped" to EL3 AArch64 registers and relegated it to an implementation detail, which is why TPIDRURW and friends got mishandled.

It also conflates "has 64-bit registers" with "is ARMv8", which is wrong for the qemu-system-arm "max" type and for any AArch64 CPU started with "aarch64=off".

Instead of marking the S-banked register as an alias if ARM_FEATURE_V8, don't put it into the hashtable at all if ARM_FEATURE_AARCH64 is present.

There are not all that many banked registers, and they're easy to find by searching for regdefs that set bank_fieldoffsets:
DACR, IFAR, DFAR, IFSR, DFSR, CSSELR, MAIR0, MAIR1, TPIDRURW, TPIDRURO, TPIDRPRW, TTBR0, TTBR1, TTBCR, TTBCR2, PAR, VBAR, SCTLR

None of these do complicated things on reset, so having both an AArch64 view and an AArch32 view reset the same register state to the same value and migrate the same data twice is harmless. This is better than sometimes not migrating or resetting it at all.

This changes the set of cpregs we will try to migrate for CPUs with ARM_FEATURE_V8 but not ARM_FEATURE_AARCH64, and is a migration break there. However, the only such CPUs are the qemu-system-aarch32 "max" CPU and AArch64 CPUs with "aarch64=off".

In an ideal world we would also not bother putting the S cpreg into the hashtable if the CPU is AArch32 without EL3 (i.e. it doesn't implement Secure state at all), because nothing will ever look up the S version of the register (access_secure_reg() always returns false). But that would be a migration compat break for those CPUs.

---
 target/arm/helper.c | 35 +++++++++++++++++++++++++----------
 1 file changed, 25 insertions(+), 10 deletions(-)
target/arm/helper.c +25 -10
index c3f607e6d6b..1b024adc703 100644
@@ -7787,18 +7787,33 @@ static void add_cpreg_to_hashtable(ARMCPU *cpu, ARMCPRegInfo *r,
         if (state == ARM_CP_STATE_AA32) {
             if (isbanked) {
                 /*
-                 * If the register is banked then we don't need to migrate or
-                 * reset the 32-bit instance in certain cases:
+                 * If this is an AArch64 CPU then AArch32 cpregs are never
+                 * banked, and if we put an NS version into the hash table
+                 * it would never be accessed (compare the condition we test
+                 * in access_secure_reg()). So drop it rather than adding it.
                  *
-                 * 1) If the register has both 32-bit and 64-bit instances
-                 *    then we can count on the 64-bit instance taking care
-                 *    of the non-secure bank.
-                 * 2) If ARMv8 is enabled then we can count on a 64-bit
-                 *    version taking care of the secure bank.  This requires
-                 *    that separate 32 and 64-bit definitions are provided.
+                 * Ideally we would also ignore the S banked register here
+                 * for an AArch32 register without EL3; however, that would
+                 * be a migration compatibility break for those CPUs, so we
+                 * continue with having the cpregs in the hashtable.
                  */
-                if ((r->state == ARM_CP_STATE_BOTH && ns) ||
-                    (arm_feature(env, ARM_FEATURE_V8) && !ns)) {
+                if (!ns && arm_feature(env, ARM_FEATURE_AARCH64)) {
+                    g_free(r);
+                    return;
+                }
+                /*
+                 * If the register is banked then we don't need to migrate or
+                 * reset the 32-bit instance if this is a STATE_BOTH regdef.
+                 * This is because we can know for certain that there's a
+                 * 64-bit regdef that's using bank_fieldoffsets[1] as its
+                 * fieldoffset, because the code that handles STATE_BOTH
+                 * always registers it. In other situations either the NS
+                 * or the S banked register might be aliased (architecturally
+                 * or non-architecturally) to a 64-bit register, but it might
+                 * not be. For those we must manually mark the alias in the
+                 * regdef struct if we care.
+                 */
+                if (r->state == ARM_CP_STATE_BOTH && ns) {
                     r->type |= ARM_CP_ALIAS;
                 }
             } else if ((secstate != r->secure) && !ns) {
Signature · 1 line
2.43.0
Reviewed-by on v1 2/8
#4 lore raw

Re: [PATCH 2/8] target/arm: Don't assume secure-banked registers are migrated by 64-bit regs

Quoted 70 lines · Peter Maydell

In add_cpreg_to_hashtable() we have some code that automatically marks the secure version of an AArch32 banked cpreg as ARM_CP_ALIAS under two conditions:

1) If the register has both 32-bit and 64-bit instances then we can count on the 64-bit instance taking care of the non-secure bank.

2) If ARMv8 is enabled then we can count on a 64-bit version taking care of the secure bank. This requires that separate 32 and 64-bit definitions are provided.

Item 1 is correct: if the ARMCPRegInfo has ARM_CP_STATE_BOTH then we will always add it as an AA32 entry (which then splits into an S entry with bank_fieldoffsets[0] and an NS entry with bank_fieldoffsets[1]) and as an AA64 entry (which uses bank_fieldoffsets[1]), so the AA64 entry will always be present to cover the NS banked AA32 entry.

Item 2 is not correct: the idea was that because typically AArch32 Secure banked registers are (non-architecturally) mapped to EL3 AArch64 registers, the AArch64 EL3 register would handle things (and that we would be adding the AArch64 register to the hashtable even if the CPU didn't have AArch64).

However, this fails in several cases:
(a) registers like CSSELR, where there is only a CSSELR_EL1, and no _EL3 version. CSSELR_S's underlying state isn't migrated
(b) registers like TPIDRURW, where the secure banked register isn't mapped to any AArch64 register, so the state isn't migrated
(c) IFAR_S and DFAR_S are odd special cases, because they map to AArch32 HIFAR and HDFAR, which in turn map to AArch64 FAR_EL2. If the CPU doesn't have EL2, we don't register FAR_EL2 and the IFAR_S and DFAR_S registers don't get migrated.

More generally, the architecture dropped the concept of documenting secure AArch32 registers as being "non-architecturally mapped" to EL3 AArch64 registers and relegated it to an implementation detail, which is why TPIDRURW and friends got mishandled.

It also conflates "has 64-bit registers" with "is ARMv8", which is wrong for the qemu-system-arm "max" type and for any AArch64 CPU started with "aarch64=off".

Instead of marking the S-banked register as an alias if ARM_FEATURE_V8, don't put it into the hashtable at all if ARM_FEATURE_AARCH64 is present.

There are not all that many banked registers, and they're easy to find by searching for regdefs that set bank_fieldoffsets:
DACR, IFAR, DFAR, IFSR, DFSR, CSSELR, MAIR0, MAIR1, TPIDRURW, TPIDRURO, TPIDRPRW, TTBR0, TTBR1, TTBCR, TTBCR2, PAR, VBAR, SCTLR

None of these do complicated things on reset, so having both an AArch64 view and an AArch32 view reset the same register state to the same value and migrate the same data twice is harmless. This is better than sometimes not migrating or resetting it at all.

This changes the set of cpregs we will try to migrate for CPUs with ARM_FEATURE_V8 but not ARM_FEATURE_AARCH64, and is a migration break there. However, the only such CPUs are the qemu-system-aarch32 "max" CPU and AArch64 CPUs with "aarch64=off".

In an ideal world we would also not bother putting the S cpreg into the hashtable if the CPU is AArch32 without EL3 (i.e. it doesn't implement Secure state at all), because nothing will ever look up the S version of the register (access_secure_reg() always returns false). But that would be a migration compat break for those CPUs.

Quoted 4 lines
---
  target/arm/helper.c | 35 +++++++++++++++++++++++++----------
  1 file changed, 25 insertions(+), 10 deletions(-)

r~

#5 lore raw

PATCH 3/8 target/arm: Drop uses of reset_ignore for banked AArch32 registers

In reply to Peter Maydell

Some banked AArch32 cpregs set up a resetfn of arm_cp_reset_ignore. This is presumably in an expectation that some other AArch64 register will handle resetting the underlying state. However this isn't true for some of them:

* TPIDRURO's S bank is in cp15.tpidruro_s and this isn't shared with any AArch64 register
* TPIDRURW's S bank similarly is in cp15.tpidrurw_s, not shared with any AArch64 register

Remove the use of arm_cp_reset_ignore here; this means that the state for the NS versions of these registers will be reset twice, but that is fine because it will always be to the same value (and the cp_reg_check_reset() code will assert if we introduce a bug where the AArch64 and AArch32 regs disagree about the reset value).

For the MAIR0 and MAIR1 banked registers, doing nothing on reset is OK because they are backed by state that is always covered by the MAIR_EL1 and MAIR_EL3 AArch64 registers that will always be present if the AArch32 registers are. But it's not consistent with how we handle all the other banked regs, so drop it here too. Again, the cp_reg_check_reset() checks will catch any mismatches in what the reset value should be.

The lack of reset-to-zero for the S banked versions of TPIDRURO and TPIDRURW is very unlikely to have had any guest visible consquences, because these registers are architecturally UNKNOWN at reset and so guest code should be initializing them itself regardless.

---
 target/arm/helper.c | 12 ++++--------
 1 file changed, 4 insertions(+), 8 deletions(-)
target/arm/helper.c +4 -8
index 1b024adc703..f8185b201be 100644
@@ -1072,14 +1072,12 @@ static const ARMCPRegInfo v7_cp_reginfo[] = {
       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
-                             offsetof(CPUARMState, cp15.mair0_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetof(CPUARMState, cp15.mair0_ns) }, },
     { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
       .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
-                             offsetof(CPUARMState, cp15.mair1_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetof(CPUARMState, cp15.mair1_ns) }, },
     { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
       .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
       .fgt = FGT_ISR_EL1,
@@ -1136,8 +1134,7 @@ static const ARMCPRegInfo v6k_cp_reginfo[] = {
       .access = PL0_RW,
       .fgt = FGT_TPIDR_EL0,
       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
-                             offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, },
     { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
       .access = PL0_R | PL1_W,
@@ -1148,8 +1145,7 @@ static const ARMCPRegInfo v6k_cp_reginfo[] = {
       .access = PL0_R | PL1_W,
       .fgt = FGT_TPIDRRO_EL0,
       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
-                             offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
-      .resetfn = arm_cp_reset_ignore },
+                             offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, },
     { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
       .access = PL1_RW,
Signature · 1 line
2.43.0
Reviewed-by on v1 3/8
#6 lore raw

Re: [PATCH 3/8] target/arm: Drop uses of reset_ignore for banked AArch32 registers

Quoted 29 lines · Peter Maydell

Some banked AArch32 cpregs set up a resetfn of arm_cp_reset_ignore. This is presumably in an expectation that some other AArch64 register will handle resetting the underlying state. However this isn't true for some of them:

* TPIDRURO's S bank is in cp15.tpidruro_s and this isn't shared with any AArch64 register
* TPIDRURW's S bank similarly is in cp15.tpidrurw_s, not shared with any AArch64 register

Remove the use of arm_cp_reset_ignore here; this means that the state for the NS versions of these registers will be reset twice, but that is fine because it will always be to the same value (and the cp_reg_check_reset() code will assert if we introduce a bug where the AArch64 and AArch32 regs disagree about the reset value).

For the MAIR0 and MAIR1 banked registers, doing nothing on reset is OK because they are backed by state that is always covered by the MAIR_EL1 and MAIR_EL3 AArch64 registers that will always be present if the AArch32 registers are. But it's not consistent with how we handle all the other banked regs, so drop it here too. Again, the cp_reg_check_reset() checks will catch any mismatches in what the reset value should be.

The lack of reset-to-zero for the S banked versions of TPIDRURO and TPIDRURW is very unlikely to have had any guest visible consquences, because these registers are architecturally UNKNOWN at reset and so guest code should be initializing them itself regardless.

Quoted 4 lines
---
  target/arm/helper.c | 12 ++++--------
  1 file changed, 4 insertions(+), 8 deletions(-)

r~

#7 lore raw

PATCH 4/8 target/arm: Don't use arm_cp_reset_ignore when fieldoffset is 0

In reply to Peter Maydell

In cp_reg_reset(), we treat an ARMCPRegInfo::fieldoffset value of 0 as "reset is handled elsewhere". So we don't need to explicitly set ri->resetfn to arm_cp_reset_ignore() when we haven't defined a fieldoffset in the cpreg struct.

Remove the setting of resetfn to arm_cp_reset_ignore in the handful of places where we were doing this unnecessarily. These are all timer registers, and doing this brings them in to line with the other timer registers (e.g. CNTPCT_EL0 has no resetfn, and now CNTPCT doesn't either).

---
 target/arm/helper.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/arm/helper.c +4 -4
index f8185b201be..9d8f09f13b4 100644
@@ -2209,7 +2209,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
     { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_pct_access,
-      .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_cnt_read,
     },
     { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
@@ -2219,7 +2219,7 @@ static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
     { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_vct_access,
-      .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_virt_cnt_read,
     },
     { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
@@ -2310,7 +2310,7 @@ static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
     { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_vct_access,
-      .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_virt_cnt_read,
     },
     { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6,
@@ -2320,7 +2320,7 @@ static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = {
     { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8,
       .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
       .accessfn = gt_pct_access,
-      .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
+      .readfn = gt_cnt_read,
     },
     { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64,
       .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5,
Signature · 1 line
2.43.0
Reviewed-by on v1 4/8
#8 lore raw

Re: [PATCH 4/8] target/arm: Don't use arm_cp_reset_ignore when fieldoffset is 0

Quoted 11 lines · Peter Maydell

In cp_reg_reset(), we treat an ARMCPRegInfo::fieldoffset value of 0 as "reset is handled elsewhere". So we don't need to explicitly set ri->resetfn to arm_cp_reset_ignore() when we haven't defined a fieldoffset in the cpreg struct.

Remove the setting of resetfn to arm_cp_reset_ignore in the handful of places where we were doing this unnecessarily. These are all timer registers, and doing this brings them in to line with the other timer registers (e.g. CNTPCT_EL0 has no resetfn, and now CNTPCT doesn't either).

Quoted 4 lines
---
  target/arm/helper.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)

r~

#9 lore raw

PATCH 5/8 target/arm: Move ESR_EL1 definition to vmsa_pmsa_cp_reginfo

In reply to Peter Maydell

Currently we define ESR_EL1 in vmsa_cp_reginfo[], but ESR_EL1 isn't VMSA specific. This doesn't matter for "is the register visible when it should be?", because currently all our AArch64 CPUs are VMSA. But it does matter for migration, because the DFSR definition in vmsa_pmsa_cp_reginfo[] is marked ARM_CP_ALIAS on the assumption that the migration of cp15.dfsr_ns will be handled by migrating esr_el[1], which is the same underlying field in the CPU state.

Move ESR_EL1 to vmsa_pmsa_cp_reginfo[]; this will fix the lack of migration of DFSR for R-profile CPUs. This is a migration break for those CPUs, but none of them are used in versioned machine types.

(The S banked DFSR is already migrated, because we register ESR_EL3 in el3_cp_reginfo[] if EL3 exists, regardless of whether we have VMSA or PMSA.)

---
 target/arm/helper.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
target/arm/helper.c +8 -8
index 9d8f09f13b4..e4028e0eba6 100644
@@ -2894,6 +2894,14 @@ static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
 }
 
 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
+    { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
+      .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
+      .access = PL1_RW, .accessfn = access_tvm_trvm,
+      .fgt = FGT_ESR_EL1,
+      .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
+      .vhe_redir_to_el2 = ENCODE_AA64_CP_REG(3, 4, 5, 2, 0),
+      .vhe_redir_to_el01 = ENCODE_AA64_CP_REG(3, 5, 5, 2, 0),
+      .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
     { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS,
       .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
@@ -2918,14 +2926,6 @@ static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
 };
 
 static const ARMCPRegInfo vmsa_cp_reginfo[] = {
-    { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
-      .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
-      .access = PL1_RW, .accessfn = access_tvm_trvm,
-      .fgt = FGT_ESR_EL1,
-      .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1,
-      .vhe_redir_to_el2 = ENCODE_AA64_CP_REG(3, 4, 5, 2, 0),
-      .vhe_redir_to_el01 = ENCODE_AA64_CP_REG(3, 5, 5, 2, 0),
-      .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
     { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
       .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
Signature · 1 line
2.43.0
Reviewed-by on v1 5/8
#10 lore raw

Re: [PATCH 5/8] target/arm: Move ESR_EL1 definition to vmsa_pmsa_cp_reginfo

Quoted 12 lines · Peter Maydell

Currently we define ESR_EL1 in vmsa_cp_reginfo[], but ESR_EL1 isn't VMSA specific. This doesn't matter for "is the register visible when it should be?", because currently all our AArch64 CPUs are VMSA. But it does matter for migration, because the DFSR definition in vmsa_pmsa_cp_reginfo[] is marked ARM_CP_ALIAS on the assumption that the migration of cp15.dfsr_ns will be handled by migrating esr_el[1], which is the same underlying field in the CPU state.

Move ESR_EL1 to vmsa_pmsa_cp_reginfo[]; this will fix the lack of migration of DFSR for R-profile CPUs. This is a migration break for those CPUs, but none of them are used in versioned machine types.

Quoted 8 lines

(The S banked DFSR is already migrated, because we register ESR_EL3 in el3_cp_reginfo[] if EL3 exists, regardless of whether we have VMSA or PMSA.)

---
  target/arm/helper.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)

r~

#11 lore raw

PATCH 6/8 target/arm: Don't set ARM_FEATURE_VAPA for v7 PMSA

In reply to Peter Maydell

The features VAPA (which gets you the ATS instructions and the PAR register) and LPAE (which gets you a 64-bit PAR and the long-format page table descriptors). were originally defined in v7 as being A-profile only. For v8, the ATS insns and PAR are added to R-profile; although there are no page table descriptors to be long or short, the v8R PAR format is always long-descriptor style, so it makes more sense to set ARM_FEATURE_LPAE than not (and matches how we already return true from regime_using_lpae_format() for v8R PMSA).

Our "feature X implies feature Y" code was only taking account of A-profile; adjust it so that for PMSA:
* V8 implies V7VE, LPAE, V7, and VAPA (no change from currently)
* V7 does not imply VAPA (new)

(v8R wants V7VE because this implies things like the existence of ERET and the PMOVSSET insns.)

This will stop us incorrectly exposing the PAR and ATS insns for v7R CPUs (and it will be a migration break on those CPUs, which is fine as none of them are usable with versioned machine types).

---
 target/arm/cpu.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)
target/arm/cpu.c +5 -1
index 77aa78f00e2..0432888ae34 100644
@@ -1501,7 +1501,11 @@ static void arm_cpu_propagate_feature_implications(ARMCPU *cpu)
         set_feature(env, ARM_FEATURE_V7);
     }
     if (arm_feature(env, ARM_FEATURE_V7)) {
-        set_feature(env, ARM_FEATURE_VAPA);
+        /* VAPA appears in v7A, but not in R profile until v8R */
+        if (arm_feature(env, ARM_FEATURE_V8) ||
+            !arm_feature(env, ARM_FEATURE_PMSA)) {
+            set_feature(env, ARM_FEATURE_VAPA);
+        }
         set_feature(env, ARM_FEATURE_THUMB2);
         set_feature(env, ARM_FEATURE_MPIDR);
         if (!arm_feature(env, ARM_FEATURE_M)) {
Signature · 1 line
2.43.0
Reviewed-by on v1 6/8
#12 lore raw

Re: [PATCH 6/8] target/arm: Don't set ARM_FEATURE_VAPA for v7 PMSA

Quoted 17 lines · Peter Maydell

The features VAPA (which gets you the ATS instructions and the PAR register) and LPAE (which gets you a 64-bit PAR and the long-format page table descriptors). were originally defined in v7 as being A-profile only. For v8, the ATS insns and PAR are added to R-profile; although there are no page table descriptors to be long or short, the v8R PAR format is always long-descriptor style, so it makes more sense to set ARM_FEATURE_LPAE than not (and matches how we already return true from regime_using_lpae_format() for v8R PMSA).

Our "feature X implies feature Y" code was only taking account of A-profile; adjust it so that for PMSA:
* V8 implies V7VE, LPAE, V7, and VAPA (no change from currently)
* V7 does not imply VAPA (new)

(v8R wants V7VE because this implies things like the existence of ERET and the PMOVSSET insns.)

Quoted 8 lines

This will stop us incorrectly exposing the PAR and ATS insns for v7R CPUs (and it will be a migration break on those CPUs, which is fine as none of them are usable with versioned machine types).

---
  target/arm/cpu.c | 6 +++++-
  1 file changed, 5 insertions(+), 1 deletion(-)

r~

#13 lore raw

PATCH 7/8 target/arm: Don't register 64-bit TTBR0, TTBR1 for PMSA

In reply to Peter Maydell

We register the 64-bit versions of TTBR0 and TTBR1 in lpae_cp_reginfo[], marking them as ARM_CP_ALIAS because the AArch64 definitions in vmsa_cp_reginfo[] will handle their migration.

However, this means that we are also defining these registers as present in the Cortex-R52, which is a v8R PMSA CPU. v8R does define AMAIR0, AMAIR1 and the PAR, but does not have TTBR0 or TTBR1.

Split the TTBR0 and TTBR1 definitions into a new lpae_vmsa_cp_reginfo which we only register when we have both LPAE and VMSA.

This is a migration break for Cortex-R52 CPUs (which we don't use in any versioned machine type).

---
Detected because we ended up with the CP_ALIAS TTBR[01] registered but not the TTBR[01]_EL1 cpregs that handled the migration.

---
 target/arm/helper.c | 6 ++++++
 1 file changed, 6 insertions(+)
target/arm/helper.c +6 -0
index e4028e0eba6..b41e07e84b4 100644
@@ -3098,6 +3098,9 @@ static const ARMCPRegInfo lpae_cp_reginfo[] = {
       .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
       .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
                              offsetof(CPUARMState, cp15.par_ns)} },
+};
+
+static const ARMCPRegInfo lpae_vmsa_cp_reginfo[] = {
     { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
       .access = PL1_RW, .accessfn = access_tvm_trvm,
       .type = ARM_CP_64BIT | ARM_CP_ALIAS,
@@ -7226,6 +7229,9 @@ void register_cp_regs_for_features(ARMCPU *cpu)
     }
     if (arm_feature(env, ARM_FEATURE_LPAE)) {
         define_arm_cp_regs(cpu, lpae_cp_reginfo);
+        if (!arm_feature(env, ARM_FEATURE_PMSA)) {
+            define_arm_cp_regs(cpu, lpae_vmsa_cp_reginfo);
+        }
     }
     if (cpu_isar_feature(aa32_jazelle, cpu)) {
         define_arm_cp_regs(cpu, jazelle_regs);
Signature · 1 line
2.43.0
Reviewed-by on v1 7/8
#14 lore raw

Re: [PATCH 7/8] target/arm: Don't register 64-bit TTBR0, TTBR1 for PMSA

Quoted 14 lines · Peter Maydell

We register the 64-bit versions of TTBR0 and TTBR1 in lpae_cp_reginfo[], marking them as ARM_CP_ALIAS because the AArch64 definitions in vmsa_cp_reginfo[] will handle their migration.

However, this means that we are also defining these registers as present in the Cortex-R52, which is a v8R PMSA CPU. v8R does define AMAIR0, AMAIR1 and the PAR, but does not have TTBR0 or TTBR1.

Split the TTBR0 and TTBR1 definitions into a new lpae_vmsa_cp_reginfo which we only register when we have both LPAE and VMSA.

This is a migration break for Cortex-R52 CPUs (which we don't use in any versioned machine type).

Quoted 7 lines

---
Detected because we ended up with the CP_ALIAS TTBR[01] registered but not the TTBR[01]_EL1 cpregs that handled the migration.

---
  target/arm/helper.c | 6 ++++++
  1 file changed, 6 insertions(+)

r~

#15 lore raw

PATCH 8/8 target/arm: Add a debug check that all sysregs are migrated

In reply to Peter Maydell

We mark some sysregs with ARM_CP_ALIAS (either manually or automatically) to indicate that they are accessing underlying guest CPU state whose migration and reset is handled by some other register. (For example, typically we make the 32-bit view of a register an alias and handle migration for the 64-bit view.)

Add a debug check that we actually do handle the migration elsewhere:
for every sysreg in the hashtable (i.e. which might be visible to the guest), check that some register in the migration list has a field offset that matches. This isn't perfect but it can catch some bugs. In particular it did fire for the various secure banked registers that we were not migrating before the fixes in the previous patches.

Because the check is more of a debugging aid than something I'm confident will never produce a false-positive report for some niche combination of CPU and machine options, we only do it for debug builds, and make it a warning printed to the user rather than an assertion failure.

---
 target/arm/helper.c | 121 ++++++++++++++++++++++++++++++++++++++++++++
 1 file changed, 121 insertions(+)
target/arm/helper.c +121 -0
index b41e07e84b4..82e7a20bdb6 100644
@@ -230,6 +230,125 @@ bool write_list_to_cpustate(ARMCPU *cpu)
     return ok;
 }
 
+#ifdef CONFIG_DEBUG_TCG
+/* Data to pass through to check_cpreg() */
+typedef struct CheckCPRegData {
+    ARMCPU *cpu;
+    bool warned;
+} CheckCPRegData;
+
+static void check_cpreg(gpointer key, gpointer value, gpointer opaque)
+{
+    CheckCPRegData *data = opaque;
+    ARMCPU *cpu = data->cpu;
+    CPUARMState *env = &cpu->env;
+    bool match = false;
+    const ARMCPRegInfo *ri = value;
+
+    if ((ri->type & (ARM_CP_NO_RAW | ARM_CP_CONST)) ||
+        ri->fieldoffset == 0) {
+        return;
+    }
+
+    /*
+     * If we don't have EL3 then the guest can't get at S-only
+     * registers even if they're in the hashtable. This avoids
+     * false-positive complaints about e.g. TTBCR_S and DFSR_S: they
+     * are migrated via ESR_EL3 and TCR_EL3, but we only register those
+     * _EL3 regs if ARM_FEATURE_EL3.
+     */
+    if (ri->secure == ARM_CP_SECSTATE_S && !arm_feature(env, ARM_FEATURE_EL3)) {
+        return;
+    }
+
+    /*
+     * Some special cases for sysregs that are backed by fields that we
+     * migrate in the main vmstate, not the cpregs array.
+     */
+    if (ri->fieldoffset >= offsetof(CPUARMState, banked_spsr[BANK_USRSYS]) &&
+        ri->fieldoffset <= offsetof(CPUARMState, banked_spsr[BANK_MON])) {
+        return;
+    }
+    if (ri->fieldoffset >= offsetof(CPUARMState, elr_el[0]) &&
+        ri->fieldoffset <= offsetof(CPUARMState, elr_el[3])) {
+        return;
+    }
+    if (ri->fieldoffset >= offsetof(CPUARMState, sp_el[0]) &&
+        ri->fieldoffset <= offsetof(CPUARMState, sp_el[3])) {
+        return;
+    }
+    if (ri->fieldoffset == offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC])) {
+        return;
+    }
+
+    for (int i = 0; i < cpu->cpreg_array_len; i++) {
+        uint32_t other_regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
+        const ARMCPRegInfo *other_ri = get_arm_cp_reginfo(cpu->cp_regs,
+                                                          other_regidx);
+
+        if (!other_ri || (other_ri->type & (ARM_CP_NO_RAW | ARM_CP_CONST)) ||
+            other_ri->fieldoffset == 0) {
+            continue;
+        }
+
+        /*
+         * If the field offsets match exactly, or this 32-bit cpreg
+         * is in the second half of a 64-bit field, consider it to
+         * be handled.
+         */
+        if (ri->fieldoffset == other_ri->fieldoffset ||
+            (cpreg_field_type(ri) == MO_32 &&
+             cpreg_field_type(other_ri) == MO_64 &&
+             ri->fieldoffset == other_ri->fieldoffset + 4)) {
+            match = true;
+            break;
+        }
+    }
+    if (!match) {
+        warn_report("check_cpreg: no migration entry found for %s", ri->name);
+        data->warned = true;
+    }
+}
+#endif /* CONFIG_DEBUG_TCG */
+
+static void arm_check_cpreg_coverage(ARMCPU *cpu)
+{
+    /*
+     * Try to catch bugs where we don't actually migrate a cpreg.
+     * Specifically, here we check that every cpreg in the hash table
+     * (and thus potentially visible to the guest) which specifies
+     * a fieldoffset has some entry in the cpreg_indexes[] array that
+     * handles that same fieldoffset.
+     * The typical bug that will be flagged up here is if a register
+     * is marked as an alias of something else, but there isn't actually
+     * anything else that handles the field.
+     *
+     * Note that the checks are more of a best-effort, are somewhat
+     * expensive at O(n^2) in the number of sysregs, and need some
+     * cases to be excluded where the sysreg data isn't migrated via
+     * the cpreg arrays. So we only do them in --enable-debug builds,
+     * to avoid potentially causing problems for users.
+     */
+#ifdef CONFIG_DEBUG_TCG
+    CheckCPRegData data = {
+        .cpu = cpu,
+        .warned = false,
+    };
+
+    /* We'll only use the cpregs for migration with TCG */
+    if (!tcg_enabled()) {
+        return;
+    }
+    g_hash_table_foreach(cpu->cp_regs, check_cpreg, &data);
+
+    if (data.warned) {
+        error_printf("check_cpreg errors are a QEMU bug that may cause "
+                     "migration to fail. Please report this with the full "
+                     "QEMU command line and version.\n");
+    }
+#endif
+}
+
 static void add_cpreg_to_list(gpointer key, gpointer value, gpointer opaque)
 {
     ARMCPU *cpu = opaque;
@@ -281,6 +400,8 @@ void arm_init_cpreg_list(ARMCPU *cpu)
     if (arraylen) {
         qsort(cpu->cpreg_indexes, arraylen, sizeof(uint64_t), compare_u64);
     }
+
+    arm_check_cpreg_coverage(cpu);
 }
 
 bool arm_pan_enabled(CPUARMState *env)
Signature · 1 line
2.43.0
Reviewed-by on v1 8/8
#16 lore raw

Re: [PATCH 8/8] target/arm: Add a debug check that all sysregs are migrated

Quoted 20 lines · Peter Maydell

We mark some sysregs with ARM_CP_ALIAS (either manually or automatically) to indicate that they are accessing underlying guest CPU state whose migration and reset is handled by some other register. (For example, typically we make the 32-bit view of a register an alias and handle migration for the 64-bit view.)

Add a debug check that we actually do handle the migration elsewhere:
for every sysreg in the hashtable (i.e. which might be visible to the guest), check that some register in the migration list has a field offset that matches. This isn't perfect but it can catch some bugs. In particular it did fire for the various secure banked registers that we were not migrating before the fixes in the previous patches.

Because the check is more of a debugging aid than something I'm confident will never produce a false-positive report for some niche combination of CPU and machine options, we only do it for debug builds, and make it a warning printed to the user rather than an assertion failure.

Quoted 4 lines
---
  target/arm/helper.c | 121 ++++++++++++++++++++++++++++++++++++++++++++
  1 file changed, 121 insertions(+)

r~