@samitouri / QOSamiQemu / commits / ab2ddc7b66

target/arm/machine: Use VMSTATE_VARRAY_INT32_ALLOC for cpreg arrays

This removes the need for explicitly allocating cpreg_vmstate arrays. On post save we simply point to cpreg arrays and set the length accordingly. Remove VMSTATE_VARRAY_INT32 for cpreg_vmstate_array_len as now the array is dynamically allocated. Also add a trace point on post_load to trace potential mismatch between the number of incoming cpregs versus current ones. Signed-off-by: Eric Auger <eric.auger@redhat.com> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Message-id: 20260304101625.1962633-3-eric.auger@redhat.com Suggested-by: Peter Maydell <peter.maydell@linaro.org> Reviewed-by: Peter Maydell <peter.maydell@linaro.org> Signed-off-by: Peter Maydell <peter.maydell@linaro.org>

Eric Auger committed Mar 6, 2026 at 09:01 UTC ab2ddc7b662d34c242ddfcfbe35996417b047ce2
5 files changed +35 -30
target/arm/helper.c
-5
@@ -265,15 +265,10 @@ void arm_init_cpreg_list(ARMCPU *cpu)
265 if (arraylen) {
266 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
267 cpu->cpreg_values = g_new(uint64_t, arraylen);
268 - cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
269 - cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
268 } else {
269 cpu->cpreg_indexes = NULL;
270 cpu->cpreg_values = NULL;
273 - cpu->cpreg_vmstate_indexes = NULL;
274 - cpu->cpreg_vmstate_values = NULL;
271 }
276 - cpu->cpreg_vmstate_array_len = arraylen;
272 cpu->cpreg_array_len = 0;
273
274 g_hash_table_foreach(cpu->cp_regs, add_cpreg_to_list, cpu);
target/arm/kvm.c
-5
@@ -864,12 +864,7 @@ static int kvm_arm_init_cpreg_list(ARMCPU *cpu)
864
865 cpu->cpreg_indexes = g_renew(uint64_t, cpu->cpreg_indexes, arraylen);
866 cpu->cpreg_values = g_renew(uint64_t, cpu->cpreg_values, arraylen);
867 - cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes,
868 - arraylen);
869 - cpu->cpreg_vmstate_values = g_renew(uint64_t, cpu->cpreg_vmstate_values,
870 - arraylen);
867 cpu->cpreg_array_len = arraylen;
872 - cpu->cpreg_vmstate_array_len = arraylen;
868
869 for (i = 0, arraylen = 0; i < rlp->n; i++) {
870 uint64_t regidx = rlp->reg[i];
target/arm/machine.c
+32 -13
@@ -1,5 +1,6 @@
1 #include "qemu/osdep.h"
2 #include "cpu.h"
3 +#include "trace.h"
4 #include "qemu/error-report.h"
5 #include "system/kvm.h"
6 #include "system/tcg.h"
@@ -984,11 +985,14 @@ static int cpu_pre_save(void *opaque)
985 }
986 }
987
988 + /*
989 + * On outbound migration, send the data in our cpreg_{values,indexes}
990 + * arrays. The migration code will not allocate anything, but just
991 + * reads the data pointed to by the VMSTATE_VARRAY_INT32_ALLOC() fields.
992 + */
993 + cpu->cpreg_vmstate_indexes = cpu->cpreg_indexes;
994 + cpu->cpreg_vmstate_values = cpu->cpreg_values;
995 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
988 - memcpy(cpu->cpreg_vmstate_indexes, cpu->cpreg_indexes,
989 - cpu->cpreg_array_len * sizeof(uint64_t));
990 - memcpy(cpu->cpreg_vmstate_values, cpu->cpreg_values,
991 - cpu->cpreg_array_len * sizeof(uint64_t));
996
997 return 0;
998 }
@@ -1001,6 +1005,9 @@ static int cpu_post_save(void *opaque)
1005 pmu_op_finish(&cpu->env);
1006 }
1007
1008 + cpu->cpreg_vmstate_indexes = NULL;
1009 + cpu->cpreg_vmstate_values = NULL;
1010 +
1011 return 0;
1012 }
1013
@@ -1034,6 +1041,9 @@ static int cpu_pre_load(void *opaque)
1041 pmu_op_start(env);
1042 }
1043
1044 + g_assert(!cpu->cpreg_vmstate_indexes);
1045 + g_assert(!cpu->cpreg_vmstate_values);
1046 +
1047 return 0;
1048 }
1049
@@ -1043,6 +1053,9 @@ static int cpu_post_load(void *opaque, int version_id)
1053 CPUARMState *env = &cpu->env;
1054 int i, v;
1055
1056 + trace_cpu_post_load(cpu->cpreg_vmstate_array_len,
1057 + cpu->cpreg_array_len);
1058 +
1059 /*
1060 * Handle migration compatibility from old QEMU which didn't
1061 * send the irq-line-state subsection. A QEMU without it did not
@@ -1094,6 +1107,11 @@ static int cpu_post_load(void *opaque, int version_id)
1107 }
1108 }
1109
1110 + g_free(cpu->cpreg_vmstate_indexes);
1111 + g_free(cpu->cpreg_vmstate_values);
1112 + cpu->cpreg_vmstate_indexes = NULL;
1113 + cpu->cpreg_vmstate_values = NULL;
1114 +
1115 /*
1116 * Misaligned thumb pc is architecturally impossible. Fail the
1117 * incoming migration. For TCG it would trigger the assert in
@@ -1167,16 +1185,17 @@ const VMStateDescription vmstate_arm_cpu = {
1185 VMSTATE_UINT32_ARRAY(env.fiq_regs, ARMCPU, 5),
1186 VMSTATE_UINT64_ARRAY(env.elr_el, ARMCPU, 4),
1187 VMSTATE_UINT64_ARRAY(env.sp_el, ARMCPU, 4),
1170 - /* The length-check must come before the arrays to avoid
1171 - * incoming data possibly overflowing the array.
1188 + /*
1189 + * The length must come before the arrays so we can
1190 + * allocate the arrays before their data arrives
1191 */
1173 - VMSTATE_INT32_POSITIVE_LE(cpreg_vmstate_array_len, ARMCPU),
1174 - VMSTATE_VARRAY_INT32(cpreg_vmstate_indexes, ARMCPU,
1175 - cpreg_vmstate_array_len,
1176 - 0, vmstate_info_uint64, uint64_t),
1177 - VMSTATE_VARRAY_INT32(cpreg_vmstate_values, ARMCPU,
1178 - cpreg_vmstate_array_len,
1179 - 0, vmstate_info_uint64, uint64_t),
1192 + VMSTATE_INT32(cpreg_vmstate_array_len, ARMCPU),
1193 + VMSTATE_VARRAY_INT32_ALLOC(cpreg_vmstate_indexes, ARMCPU,
1194 + cpreg_vmstate_array_len,
1195 + 0, vmstate_info_uint64, uint64_t),
1196 + VMSTATE_VARRAY_INT32_ALLOC(cpreg_vmstate_values, ARMCPU,
1197 + cpreg_vmstate_array_len,
1198 + 0, vmstate_info_uint64, uint64_t),
1199 VMSTATE_UINT64(env.exclusive_addr, ARMCPU),
1200 VMSTATE_UINT64(env.exclusive_val, ARMCPU),
1201 VMSTATE_UINT64(env.exclusive_high, ARMCPU),
target/arm/trace-events
+3
@@ -26,3 +26,6 @@ arm_powerctl_reset_cpu(uint64_t mp_aff) "cpu %" PRIu64
26
27 # tcg/psci.c and hvf/hvf.c
28 arm_psci_call(uint64_t x0, uint64_t x1, uint64_t x2, uint64_t x3, uint32_t cpuid) "PSCI Call x0=0x%016"PRIx64" x1=0x%016"PRIx64" x2=0x%016"PRIx64" x3=0x%016"PRIx64" cpuid=0x%x"
29 +
30 +# machine.c
31 +cpu_post_load(uint32_t cpreg_vmstate_array_len, uint32_t cpreg_array_len) "cpreg_vmstate_array_len=%d cpreg_array_len=%d"
target/arm/whpx/whpx-all.c
-7
@@ -783,12 +783,6 @@ int whpx_init_vcpu(CPUState *cpu)
783 sregs_match_len);
784 arm_cpu->cpreg_values = g_renew(uint64_t, arm_cpu->cpreg_values,
785 sregs_match_len);
786 - arm_cpu->cpreg_vmstate_indexes = g_renew(uint64_t,
787 - arm_cpu->cpreg_vmstate_indexes,
788 - sregs_match_len);
789 - arm_cpu->cpreg_vmstate_values = g_renew(uint64_t,
790 - arm_cpu->cpreg_vmstate_values,
791 - sregs_match_len);
786
787 memset(arm_cpu->cpreg_values, 0, sregs_match_len * sizeof(uint64_t));
788
@@ -807,7 +801,6 @@ int whpx_init_vcpu(CPUState *cpu)
801 }
802 }
803 arm_cpu->cpreg_array_len = sregs_cnt;
810 - arm_cpu->cpreg_vmstate_array_len = sregs_cnt;
804
805 assert(write_cpustate_to_list(arm_cpu, false));
806