cpu: Avoid latent bug calling cpu_reset() on uninitialized vCPU
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
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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
@@ -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
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 byqemu_init_vcpu(). To avoid bugs similar to the one fixed in commit00d0f7cb66when introducing new architectures, move thecpu_reset()calls afterqemu_init_vcpu().Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- 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@@ -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@@ -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@@ -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@@ -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@@ -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@@ -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
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); }
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 byqemu_init_vcpu(). To avoid bugs similar to the one fixed in commit00d0f7cb66when introducing new architectures, move thecpu_reset()calls afterqemu_init_vcpu().Signed-off-by Philippe Mathieu-Daudé <[email protected]>
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
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 byqemu_init_vcpu(). To avoid bugs similar to the one fixed in commit00d0f7cb66when introducing new architectures, move thecpu_reset()calls afterqemu_init_vcpu().
Quoted 5 lines
Quoted 1 line
Signed-off-by Philippe Mathieu-Daudé <[email protected]>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
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 byqemu_init_vcpu(). To avoid bugs similar to the one fixed in commit00d0f7cb66when introducing new architectures, move thecpu_reset()calls afterqemu_init_vcpu().Signed-off-by Philippe Mathieu-Daudé <[email protected]>
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
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 byqemu_init_vcpu(). To avoid bugs similar to the one fixed in commit00d0f7cb66when introducing new architectures, move thecpu_reset()calls afterqemu_init_vcpu().Signed-off-by Philippe Mathieu-Daudé <[email protected]>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 callscpu_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.cthat 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
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
@@ -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
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 byqemu_init_vcpu(). To avoid bugs similar to the one fixed in commit00d0f7cb66when introducing new architectures, assert a vCPU is created before resetting it.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/core/cpu.c | 1 + 1 file changed, 1 insertion(+)hw/core/cpu.c +0
-0
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
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 byqemu_init_vcpu(). To avoid bugs similar to the one fixed in commit00d0f7cb66when introducing new architectures, assert a vCPU is created before resetting it.Signed-off-by Philippe Mathieu-Daudé <[email protected]>--- hw/core/cpu.c | 1 + 1 file changed, 1 insertion(+)hw/core/cpu.c +0
-0hw/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