Newer kernels may revoke exposure of KVM regs to userspace. This can happen when one notices that some registers were unconditionnally exposed whether they shall be conditionnally exposed for example.
An example of such situation is: TCR2_EL1, PIRE0_EL1, PIR_EL1. Associated kernel commits were: 0fcb4eea5345 KVM: arm64: Hide TCR2_EL1 from userspace when disabled for guests a68cddbe47ef KVM: arm64: Hide S1PIE registers from userspace when disabled for guests Those commits were actual fixes but the cons is that is breaks forward migration on some HW. Indeed when migrating from an old kernel that does not feature those commits to a more recent one, destination qemu detects there are more KVM regs in the input migration stream than exposed by the destination host and the migration fails with: "failed to load cpu:cpreg_vmstate_array_len" This patchs adds the capability to define an array of register indexes that may exist in the migration incoming stream but may be not exposed by KVM on the destination. We provision for extra space in cpreg_vmstate_* arrays during the preload phase to allow the state to be saved without overflow, in case the registers only are in the inbound data. On postload we make sure to ignore them when analyzing potential mismatch between registers. The actual cpreg array is never altered meaning those registers are never accessed nor saved. The array will be populated with a dedicated array property. Signed-off-by: Eric Auger <[email protected]> --- v1 -> v2: - get rid of the enforced/fake terminology - remove the useless array of fake regs. Only the number of missing regs is needed RFC -> v1: - improve comment in target/arm/cpu.h (Connie) --- target/arm/cpu.h | 22 ++++++++++++++++++++++ target/arm/machine.c | 27 ++++++++++++++++++--------- 2 files changed, 40 insertions(+), 9 deletions(-) diff --git a/target/arm/cpu.h b/target/arm/cpu.h index 0a283940be..82fc21dcc5 100644 --- a/target/arm/cpu.h +++ b/target/arm/cpu.h @@ -1056,6 +1056,15 @@ struct ArchCPU { uint64_t *hidden_regs; uint32_t nr_hidden_regs; + /* + * Registers that are likely to be part of the migration + * incoming stream but not exposed on destination. If + * their indexes are stored in this array, it is OK to + * ignore those registers in the inbound data. + */ + uint64_t *mig_safe_missing_regs; + uint32_t nr_mig_safe_missing_regs; + /* Uniprocessor system with MP extensions */ bool mp_is_up; @@ -1207,6 +1216,19 @@ arm_cpu_hidden_reg(ARMCPU *cpu, uint64_t regidx) return false; } + +static inline bool +arm_cpu_safe_missing_reg(ARMCPU *cpu, uint64_t regidx) +{ + for (int i = 0; i < cpu->nr_mig_safe_missing_regs; i++) { + if (regidx == cpu->mig_safe_missing_regs[i]) { + return true; + } + } + return false; +} + + /* Callback functions for the generic timer's timers. */ void arm_gt_ptimer_cb(void *opaque); void arm_gt_vtimer_cb(void *opaque); diff --git a/target/arm/machine.c b/target/arm/machine.c index f06a920aba..f420879134 100644 --- a/target/arm/machine.c +++ b/target/arm/machine.c @@ -991,7 +991,8 @@ static int cpu_pre_load(void *opaque) { ARMCPU *cpu = opaque; CPUARMState *env = &cpu->env; - int arraylen = cpu->cpreg_vmstate_array_len + MAX_CPREG_VMSTATE_ANOMALIES; + int arraylen = cpu->cpreg_vmstate_array_len + + cpu->nr_mig_safe_missing_regs + MAX_CPREG_VMSTATE_ANOMALIES; cpu->cpreg_vmstate_indexes = g_renew(uint64_t, cpu->cpreg_vmstate_indexes, arraylen); @@ -1058,6 +1059,10 @@ static int cpu_post_load(void *opaque, int version_id) * entries with the right slots in our own values array. */ + /* + * at this point cpu->cpreg_vmstate_array_len was migrated with the + * actual length saved on source + */ trace_cpu_post_load_len(cpu->cpreg_array_len, cpu->cpreg_vmstate_array_len); for (; i < cpu->cpreg_array_len && v < cpu->cpreg_vmstate_array_len;) { trace_cpu_post_load(i, v , cpu->cpreg_indexes[i]); @@ -1072,10 +1077,12 @@ static int cpu_post_load(void *opaque, int version_id) } if (cpu->cpreg_vmstate_indexes[v] < cpu->cpreg_indexes[i]) { /* register in their list but not ours: those will fail migration */ - trace_cpu_post_load_unexpected(v, cpu->cpreg_vmstate_indexes[v], i); - if (k < MAX_CPREG_VMSTATE_ANOMALIES) { - cpu->cpreg_vmstate_unexpected_indexes[k++] = - cpu->cpreg_vmstate_indexes[v]; + if (!arm_cpu_safe_missing_reg(cpu, cpu->cpreg_vmstate_indexes[v])) { + trace_cpu_post_load_unexpected(v, cpu->cpreg_vmstate_indexes[v], i); + if (k < MAX_CPREG_VMSTATE_ANOMALIES) { + cpu->cpreg_vmstate_unexpected_indexes[k++] = + cpu->cpreg_vmstate_indexes[v]; + } } v++; continue; @@ -1101,10 +1108,12 @@ static int cpu_post_load(void *opaque, int version_id) * still regs in the input stream, continue parsing the vmstate array */ for ( ; v < cpu->cpreg_vmstate_array_len; v++) { - if (k < MAX_CPREG_VMSTATE_ANOMALIES) { - trace_cpu_post_load_unexpected(v, cpu->cpreg_vmstate_indexes[v], i); - cpu->cpreg_vmstate_unexpected_indexes[k++] = - cpu->cpreg_vmstate_indexes[v]; + if (!arm_cpu_safe_missing_reg(cpu, cpu->cpreg_vmstate_indexes[v])) { + if (k < MAX_CPREG_VMSTATE_ANOMALIES) { + trace_cpu_post_load_unexpected(v, cpu->cpreg_vmstate_indexes[v], i); + cpu->cpreg_vmstate_unexpected_indexes[k++] = + cpu->cpreg_vmstate_indexes[v]; + } } } -- 2.51.1
