Skip to contentSkip to conversation

cpu: Avoid latent bug calling cpu_reset() on uninitialized vCPU

PATCH In review Philippe Mathieu-Daudé 3 participants 10 messages Mar 9, 2020

lore
Share & tools
lore
#0 lore raw

cover cpu: Avoid latent bug calling cpu_reset() on uninitialized vCPU

Two trivial patches to avoid each (new) architecture to track a bug already resolved once on ARM: cpu_reset() modify fields in the CpuState while the state is not yet allocated.

Philippe Mathieu-Daudé (2):
cpu: Do not reset a vCPU before it is created
cpu: Assert a vCPU is created before resetting it

 hw/core/cpu.c        | 1 +
 target/cris/cpu.c    | 2 +-
 target/lm32/cpu.c    | 3 +--
 target/m68k/cpu.c    | 2 +-
 target/mips/cpu.c    | 2 +-
 target/sh4/cpu.c     | 2 +-
 target/tilegx/cpu.c  | 2 +-
 target/tricore/cpu.c | 2 +-
 8 files changed, 8 insertions(+), 8 deletions(-)
Signature · 1 line
2.21.1
#1 lore raw

PATCH 1/2 cpu: Do not reset a vCPU before it is created

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, move the cpu_reset() calls after qemu_init_vcpu().

---
 target/cris/cpu.c    | 2 +-
 target/lm32/cpu.c    | 3 +--
 target/m68k/cpu.c    | 2 +-
 target/mips/cpu.c    | 2 +-
 target/sh4/cpu.c     | 2 +-
 target/tilegx/cpu.c  | 2 +-
 target/tricore/cpu.c | 2 +-
 7 files changed, 7 insertions(+), 8 deletions(-)
target/cris/cpu.c +1 -1
index 17c6712e29..9b8b99840d 100644
@@ -134,8 +134,8 @@ static void cris_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     ccc->parent_realize(dev, errp);
 }
target/lm32/cpu.c +1 -2
index 687bf35e65..56f7b97c8f 100644
@@ -132,9 +132,8 @@ static void lm32_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
-
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     lcc->parent_realize(dev, errp);
 }
target/m68k/cpu.c +1 -1
index f0653cda2f..51ca62694e 100644
@@ -247,8 +247,8 @@ static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
 
     m68k_cpu_init_gdb(cpu);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
 }
target/mips/cpu.c +1 -1
index 6cd6b9650b..553945005f 100644
@@ -149,8 +149,8 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
 
     cpu_mips_realize_env(&cpu->env);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
 }
target/sh4/cpu.c +1 -1
index 70c8d8170f..2564436719 100644
@@ -184,8 +184,8 @@ static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     scc->parent_realize(dev, errp);
 }
target/tilegx/cpu.c +1 -1
index cd422a0467..7e9982197f 100644
@@ -91,8 +91,8 @@ static void tilegx_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     tcc->parent_realize(dev, errp);
 }
target/tricore/cpu.c +1 -1
index 85bc9f03a1..c5a5d54569 100644
@@ -94,8 +94,8 @@ static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
     if (tricore_feature(env, TRICORE_FEATURE_131)) {
         set_feature(env, TRICORE_FEATURE_13);
     }
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     tcc->parent_realize(dev, errp);
 }
Signature · 1 line
2.21.1
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] cpu: Do not reset a vCPU before it is created

Le 09/03/2020 à 13:11, Philippe Mathieu-Daudé a écrit :

Quoted 107 lines

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, move the cpu_reset() calls after qemu_init_vcpu().

---
 target/cris/cpu.c    | 2 +-
 target/lm32/cpu.c    | 3 +--
 target/m68k/cpu.c    | 2 +-
 target/mips/cpu.c    | 2 +-
 target/sh4/cpu.c     | 2 +-
 target/tilegx/cpu.c  | 2 +-
 target/tricore/cpu.c | 2 +-
 7 files changed, 7 insertions(+), 8 deletions(-)
target/cris/cpu.c +1 -1
index 17c6712e29..9b8b99840d 100644
@@ -134,8 +134,8 @@ static void cris_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     ccc->parent_realize(dev, errp);
 }
target/lm32/cpu.c +1 -2
index 687bf35e65..56f7b97c8f 100644
@@ -132,9 +132,8 @@ static void lm32_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
-
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     lcc->parent_realize(dev, errp);
 }
target/m68k/cpu.c +1 -1
index f0653cda2f..51ca62694e 100644
@@ -247,8 +247,8 @@ static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
 
     m68k_cpu_init_gdb(cpu);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
 }
target/mips/cpu.c +1 -1
index 6cd6b9650b..553945005f 100644
@@ -149,8 +149,8 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
 
     cpu_mips_realize_env(&cpu->env);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
 }
target/sh4/cpu.c +1 -1
index 70c8d8170f..2564436719 100644
@@ -184,8 +184,8 @@ static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     scc->parent_realize(dev, errp);
 }
target/tilegx/cpu.c +1 -1
index cd422a0467..7e9982197f 100644
@@ -91,8 +91,8 @@ static void tilegx_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     tcc->parent_realize(dev, errp);
 }
target/tricore/cpu.c +0 -0
index 85bc9f03a1..c5a5d54569 100644

target/tricore/cpu.c static void tricore_cpu_realizefn(DeviceState *dev, Error **errp) · line 101

Quoted 9 lines
target/tricore/cpu.c +1 -1
@@ -94,8 +94,8 @@ static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
     if (tricore_feature(env, TRICORE_FEATURE_131)) {
         set_feature(env, TRICORE_FEATURE_13);
     }
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
+    cpu_reset(cs);
 
     tcc->parent_realize(dev, errp);
 }
#3 lore raw

Re: [PATCH 1/2] cpu: Do not reset a vCPU before it is created

In reply to Philippe Mathieu-Daudé
Quoted 7 lines · Philippe Mathieu-Daudé

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, move the cpu_reset() calls after qemu_init_vcpu().

Why do we need to call cpu_reset() from realize anyway? Generally for devices this is incorrect as they should be being reset by some other mechanism.

Obviously actually determining that dropping the cpu_reset() call is safe would require some tedious auditing.

If we do do a cpu_reset() in realize, should it be after the call to the parent realize function ?

thanks
-- PMM

#4 lore raw

Re: [PATCH 1/2] cpu: Do not reset a vCPU before it is created

Le 09/03/2020 à 14:09, Peter Maydell a écrit :

Quoted 7 lines · Philippe Mathieu-Daudé
Quoted 6 lines

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, move the cpu_reset() calls after qemu_init_vcpu().

Quoted 5 lines
Quoted 1 line

Why do we need to call cpu_reset() from realize anyway? Generally for devices this is incorrect as they should be being reset by some other mechanism.

I have this same change in my branch for q800 to set the initial PC and stack pointer read from memory on cold boot.

Do we have another way to do that?

Thanks,
Laurent

#5 lore raw

Re: [PATCH 1/2] cpu: Do not reset a vCPU before it is created

Quoted 11 lines · Laurent Vivier
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Philippe Mathieu-Daudé

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, move the cpu_reset() calls after qemu_init_vcpu().

Quoted 8 lines
Quoted 3 lines

Why do we need to call cpu_reset() from realize anyway? Generally for devices this is incorrect as they should be being reset by some other mechanism.

I have this same change in my branch for q800 to set the initial PC and stack pointer read from memory on cold boot.

Do we have another way to do that?

The expectation at the moment is that the board code should register a reset function with qemu_register_reset() which calls cpu_reset(). Relying on doing a reset in realize won't work for the case where there's a QEMU system reset, because we don't re-init/realize everything, we just call all the reset hooks.

If m68k reads pc/sp from memory on reset you'll probably run into the same reset-ordering vs hw/cpu/loader.c that Arm M-profile has; we currently work around that in the arm reset function. I had hoped that 3-phase reset would resolve this reset order issue, but it has not turned out to be so easy.

thanks
-- PMM

#6 lore raw

Re: [PATCH 1/2] cpu: Do not reset a vCPU before it is created

Le 09/03/2020 à 14:18, Peter Maydell a écrit :

Quoted 28 lines · Laurent Vivier
Quoted 11 lines · Peter Maydell
Quoted 9 lines · Philippe Mathieu-Daudé
Quoted 7 lines

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, move the cpu_reset() calls after qemu_init_vcpu().

Quoted 8 lines
Quoted 3 lines

Why do we need to call cpu_reset() from realize anyway? Generally for devices this is incorrect as they should be being reset by some other mechanism.

I have this same change in my branch for q800 to set the initial PC and stack pointer read from memory on cold boot.

Do we have another way to do that?

The expectation at the moment is that the board code should register a reset function with qemu_register_reset() which calls cpu_reset(). Relying on doing a reset in realize won't work for the case where there's a QEMU system reset, because we don't re-init/realize everything, we just call all the reset hooks.

Quoted 5 lines

If m68k reads pc/sp from memory on reset you'll probably run into the same reset-ordering vs hw/cpu/loader.c that Arm M-profile has; we currently work around that in the arm reset function. I had hoped that 3-phase reset would resolve this reset order issue, but it has not turned out to be so easy.

Thank you for the hint, I'll have a look.

Laurent

#7 lore raw

PATCH 2/2 cpu: Assert a vCPU is created before resetting it

In reply to Philippe Mathieu-Daudé

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, assert a vCPU is created before resetting it.

---
 hw/core/cpu.c | 1 +
 1 file changed, 1 insertion(+)
hw/core/cpu.c +1 -0
index fe65ca62ac..09e49f8d6a 100644
@@ -251,6 +251,7 @@ void cpu_reset(CPUState *cpu)
 {
     CPUClass *klass = CPU_GET_CLASS(cpu);
 
+    assert(cpu->created);
     if (klass->reset != NULL) {
         (*klass->reset)(cpu);
     }
Signature · 1 line
2.21.1
#8 lore raw

Re: [PATCH 2/2] cpu: Assert a vCPU is created before resetting it

Quoted 16 lines · Philippe Mathieu-Daudé

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, assert a vCPU is created before resetting it.

---
 hw/core/cpu.c | 1 +
 1 file changed, 1 insertion(+)
hw/core/cpu.c +0 -0
index fe65ca62ac..09e49f8d6a 100644

hw/core/cpu.c void cpu_reset(CPUState *cpu) · line 257

Quoted 7 lines
hw/core/cpu.c +1 -0
@@ -251,6 +251,7 @@ void cpu_reset(CPUState *cpu)
 {
     CPUClass *klass = CPU_GET_CLASS(cpu);
 
+    assert(cpu->created);
     if (klass->reset != NULL) {
         (*klass->reset)(cpu);
     }

This will conflict with the change to use DeviceClass::reset.

Ideally we should do an equivalent assert in the DeviceClass (and flush out all the bugs where we forgot to realize the device before using it).

thanks
-- PMM

#9 lore raw

Re: [PATCH 2/2] cpu: Assert a vCPU is created before resetting it

Quoted 27 lines · Peter Maydell
Quoted 16 lines · Philippe Mathieu-Daudé

cpu_reset() might modify architecture-specific fields allocated by qemu_init_vcpu(). To avoid bugs similar to the one fixed in commit 00d0f7cb66 when introducing new architectures, assert a vCPU is created before resetting it.

---
  hw/core/cpu.c | 1 +
  1 file changed, 1 insertion(+)
hw/core/cpu.c +0 -0
index fe65ca62ac..09e49f8d6a 100644

hw/core/cpu.c void cpu_reset(CPUState *cpu) · line 257

Quoted 7 lines
hw/core/cpu.c +1 -0
@@ -251,6 +251,7 @@ void cpu_reset(CPUState *cpu)
  {
      CPUClass *klass = CPU_GET_CLASS(cpu);
 
+    assert(cpu->created);
      if (klass->reset != NULL) {
          (*klass->reset)(cpu);
      }

This will conflict with the change to use DeviceClass::reset.

Quoted 3 lines

Ideally we should do an equivalent assert in the DeviceClass (and flush out all the bugs where we forgot to realize the device before using it).

OK (I should have sent as RFC probably).

Anyway this fails the ppc64le/s390x linux-user tests on Travis-CI:

qemu-ppc64le: hw/core/cpu.c:254: cpu_reset: Assertion `cpu->created' failed.

qemu-s390x: hw/core/cpu.c:254: cpu_reset: Assertion `cpu->created' failed.

Quoted 4 lines

thanks
-- PMM