Commit e2ea7400eb for qemu.org

commit e2ea7400eb085a9bf48520b2812b6c494309b2c9
Author: Peter Maydell <peter.maydell@linaro.org>
Date:   Tue Nov 24 10:23:09 2026 +0000

    target/arm: Handle "PMU disabled" in migration validate functions

    In various test functions for VMSTATE_VALIDATE() fields, we check
    whether the migrated region number register is within bounds (i.e.
    less than the total number of configured regions).  These tests fail
    spuriously if there are zero configured regions, because then "x <
    cpu->pmsav7_dregion" is always false.

    When we allowed v6M CPUs to have PMSAv7 in commit 157f6c69edd06, this
    exposed a bug, because we now have a CPU which has PMSAv7 according
    to the pmsav7_needed() function but which is configured with 0 PMSA
    regions: the Cortex-M0.

    We could fix this by having pmsav7_needed() return false if there
    are 0 regions; however, the cpu/m-security vmstate struct does
    migration of the secure equivalents of these fields, and we also do
    a VMSTATE_VALIDATE that checks against the number of MPU regions
    there. Making those fields be migrated only if there is more than
    one region would be awkward (we'd need to pull them out into their
    own vmstate struct), and it's unnecessary -- migrating something
    that's all zeroes and not used isn't a problem.

    So instead we relax the checks to handle the "no configured regions"
    case, insisting then that the rnr field in the CPU state (which
    should never be used) is zero.

    Similarly, we adjust the check in sau_rnr_vmstate_validate against
    sau_sregion; this one is currently theoretical as all our CPUs which
    implement the Security Extension have at least one SAU region, but
    architecturally it is permitted to implemnt no SAU regions.

    Fixes: 157f6c69edd06 ("target/arm: extend PMSAv7 support to v6-M CPUs")
    Resolves: https://gitlab.com/qemu-project/qemu/-/work_items/4905
    Signed-off-by: Peter Maydell <peter.maydell@linaro.org>
    Acked-by: Peter Xu <peterx@redhat.com>
    Reviewed-by: Jim MacArthur <jim.macarthur@linaro.org>
    Message-id: 20261119165546.3050013-1-peter.maydell@linaro.org

diff --git a/target/arm/machine.c b/target/arm/machine.c
index c3aef3f330..02a2b78506 100644
--- a/target/arm/machine.c
+++ b/target/arm/machine.c
@@ -593,7 +593,12 @@ static bool pmsav7_rgnr_vmstate_validate(void *opaque, int version_id)
 {
     ARMCPU *cpu = opaque;

-    return cpu->env.pmsav7.rnr[M_REG_NS] < cpu->pmsav7_dregion;
+    /*
+     * Check MPU_RNR in-bounds. If no MPU, we still migrate this field
+     * but number of regions == 0 and MPU_RNR is RAZ/WI so also 0
+     */
+    return cpu->env.pmsav7.rnr[M_REG_NS] < cpu->pmsav7_dregion ||
+        (cpu->pmsav7_dregion == 0 && cpu->env.pmsav7.rnr[M_REG_NS] == 0);
 }

 static const VMStateDescription vmstate_pmsav7 = {
@@ -693,14 +698,25 @@ static bool s_rnr_vmstate_validate(void *opaque, int version_id)
 {
     ARMCPU *cpu = opaque;

-    return cpu->env.pmsav7.rnr[M_REG_S] < cpu->pmsav7_dregion;
+    /*
+     * Check MPU_RNR in-bounds. If no MPU, we still migrate this field
+     * but number of regions == 0 and MPU_RNR is RAZ/WI so also 0
+     */
+    return cpu->env.pmsav7.rnr[M_REG_S] < cpu->pmsav7_dregion ||
+        (cpu->pmsav7_dregion == 0 && cpu->env.pmsav7.rnr[M_REG_S] == 0);
 }

 static bool sau_rnr_vmstate_validate(void *opaque, int version_id)
 {
     ARMCPU *cpu = opaque;

-    return cpu->env.sau.rnr < cpu->sau_sregion;
+    /*
+     * Check SAU_RNR in-bounds. If no SAU regions are implemented,
+     * we still migrate this field, but number of regions == 0 and
+     * SAU_RNR is RAZ/WI so also 0
+     */
+    return cpu->env.sau.rnr < cpu->sau_sregion ||
+        (cpu->sau_sregion == 0 && cpu->env.sau.rnr == 0);
 }

 static bool m_security_needed(void *opaque)