Skip to contentSkip to conversation

Generalize start-powered-off property from ARM

PATCH Superseded Thiago Jung Bauermann 4 participants 32 messages Jul 22, 2020 – Jul 23, 2020

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v2 cover Generalize start-powered-off property from ARM

The ARM code has a start-powered-off property in ARMCPU, which is a subclass of CPUState. This property causes arm_cpu_reset() to set CPUState::halted to 1, signalling that the CPU should start in a halted state. Other architectures also have code which aim to achieve the same effect, but without using a property.

The ppc/spapr version has a bug where QEMU does a KVM_RUN on the vcpu before cs->halted is set to 1, causing the vcpu to run while it's still in an unitialized state (more details in patch 3).

Peter Maydell mentioned the ARM start-powered-off property and Eduardo Habkost suggested making it generic, so this patch series does that, for all cases which I was able to find via grep in the code.

The only problem is that I was only able to test these changes on a ppc64le pseries KVM guest, so except for patches 2 and 3, all others are only build-tested. Also, my grasp of QOM lifecycle is basically non-existant so please be aware of that when reviewing this series.

The last 3 patches I think are good cleanups but I'm even less confident in their correctness compared to the other patches, so I marked them as RFC.

Applies cleanly on yesterday's master.

Thiago Jung Bauermann (9):
target/arm: Move start-powered-off property to generic CPUState target/arm: Move setting of CPU halted state to generic code ppc/spapr: Use start-powered-off CPUState property ppc/e500: Use start-powered-off CPUState property mips/cps: Use start-powered-off CPUState property sparc/sun4m: Use start-powered-off CPUState property sparc/sun4m: Don't set CPUState::halted in cpu_devinit() sparc/sun4m: Use one cpu_reset() function for main and secondary CPUs target/s390x: Use start-powered-off CPUState property

 exec.c                  |  1 +
 hw/core/cpu.c           |  2 +-
 hw/mips/cps.c           |  6 +++---
 hw/ppc/e500.c           | 10 +++++++---
 hw/ppc/spapr_cpu_core.c | 12 +++++++-----
 hw/sparc/sun4m.c        | 23 +++++------------------
 include/hw/core/cpu.h   |  4 ++++
 target/arm/cpu.c        |  4 +---
 target/arm/cpu.h        |  3 ---
 target/arm/kvm32.c      |  2 +-
 target/arm/kvm64.c      |  2 +-
 target/s390x/cpu.c      |  3 ++-
 12 files changed, 33 insertions(+), 39 deletions(-)
#1 lore raw

PATCH v2 1/9 target/arm: Move start-powered-off property to generic CPUState

There are other platforms which also have CPUs that start powered off, so generalize the start-powered-off property so that it can be used by them.

Note that ARMv7MState also has a property of the same name but this patch doesn't change it because that class isn't a subclass of CPUState so it wouldn't be a trivial change.

This change should not cause any change in behavior.

---
 exec.c                | 1 +
 include/hw/core/cpu.h | 4 ++++
 target/arm/cpu.c      | 5 ++---
 target/arm/cpu.h      | 3 ---
 target/arm/kvm32.c    | 2 +-
 target/arm/kvm64.c    | 2 +-
 6 files changed, 9 insertions(+), 8 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

exec.c +1 -0
index 6f381f98e2..82e82fab09 100644
@@ -899,6 +899,7 @@ Property cpu_common_props[] = {
     DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
                      MemoryRegion *),
 #endif
+    DEFINE_PROP_BOOL("start-powered-off", CPUState, start_powered_off, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
include/hw/core/cpu.h +4 -0
index 8f145733ce..9fc2696db5 100644
@@ -374,6 +374,10 @@ struct CPUState {
     bool created;
     bool stop;
     bool stopped;
+
+    /* Should CPU start in powered-off state? */
+    bool start_powered_off;
+
     bool unplug;
     bool crash_occurred;
     bool exit_request;
target/arm/cpu.c +2 -3
index 111579554f..ec65c7653f 100644
@@ -174,8 +174,8 @@ static void arm_cpu_reset(DeviceState *dev)
     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
 
-    cpu->power_state = cpu->start_powered_off ? PSCI_OFF : PSCI_ON;
-    s->halted = cpu->start_powered_off;
+    cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON;
+    s->halted = s->start_powered_off;
 
     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
@@ -2182,7 +2182,6 @@ static const ARMCPUInfo arm_cpus[] = {
 };
 
 static Property arm_cpu_properties[] = {
-    DEFINE_PROP_BOOL("start-powered-off", ARMCPU, start_powered_off, false),
     DEFINE_PROP_UINT32("psci-conduit", ARMCPU, psci_conduit, 0),
     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
target/arm/cpu.h +0 -3
index 9e8ed423ea..a925d26996 100644
@@ -810,9 +810,6 @@ struct ARMCPU {
      */
     uint32_t psci_version;
 
-    /* Should CPU start in PSCI powered-off state? */
-    bool start_powered_off;
-
     /* Current power state, access guarded by BQL */
     ARMPSCIState power_state;
 
target/arm/kvm32.c +1 -1
index 0af46b41c8..1f2b8f8b7a 100644
@@ -218,7 +218,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
 
     /* Determine init features for this CPU */
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
-    if (cpu->start_powered_off) {
+    if (cs->start_powered_off) {
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
     }
     if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
target/arm/kvm64.c +1 -1
index 1169237905..f8a6d905fb 100644
@@ -775,7 +775,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
 
     /* Determine init features for this CPU */
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
-    if (cpu->start_powered_off) {
+    if (cs->start_powered_off) {
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
     }
     if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
Reviewed-by on v2 1/9
#2 lore raw

Re: [PATCH v2 1/9] target/arm: Move start-powered-off property to generic CPUState

Hi Thiago,

Quoted 11 lines · Thiago Jung Bauermann

There are other platforms which also have CPUs that start powered off, so generalize the start-powered-off property so that it can be used by them.

Note that ARMv7MState also has a property of the same name but this patch doesn't change it because that class isn't a subclass of CPUState so it wouldn't be a trivial change.

This change should not cause any change in behavior.

As I participated in reviewing your v1, I'd have appreciated being Cc'ed for v2.

Quoted 8 lines
---
 exec.c                | 1 +
 include/hw/core/cpu.h | 4 ++++
 target/arm/cpu.c      | 5 ++---
 target/arm/cpu.h      | 3 ---
 target/arm/kvm32.c    | 2 +-
 target/arm/kvm64.c    | 2 +-
 6 files changed, 9 insertions(+), 8 deletions(-)
#3 lore raw

Re: [PATCH v2 1/9] target/arm: Move start-powered-off property to generic CPUState

Quoted 10 lines · Philippe Mathieu-Daudé

Hi Thiago,

Quoted 7 lines · Thiago Jung Bauermann

There are other platforms which also have CPUs that start powered off, so generalize the start-powered-off property so that it can be used by them.

Note that ARMv7MState also has a property of the same name but this patch doesn't change it because that class isn't a subclass of CPUState so it wouldn't be a trivial change.

Quoted 7 lines
Quoted 4 lines

This change should not cause any change in behavior.

As I participated in reviewing your v1, I'd have appreciated being Cc'ed for v2.

I'm sorry about this. I fixed the Cc list for the next version.

Quoted 1 line

Thank you very much for your prompt review, and suggestions.

I will post a new version addressing your comments shortly.

Quoted 8 lines
Quoted 8 lines
---
 exec.c                | 1 +
 include/hw/core/cpu.h | 4 ++++
 target/arm/cpu.c      | 5 ++---
 target/arm/cpu.h      | 3 ---
 target/arm/kvm32.c    | 2 +-
 target/arm/kvm64.c    | 2 +-
 6 files changed, 9 insertions(+), 8 deletions(-)

--
Thiago Jung Bauermann
IBM Linux Technology Center

Reviewed-by on v2 1/9
#4 lore raw

Re: [PATCH v2 1/9] target/arm: Move start-powered-off property to generic CPUState

In reply to Thiago Jung Bauermann
Quoted 11 lines · Thiago Jung Bauermann

There are other platforms which also have CPUs that start powered off, so generalize the start-powered-off property so that it can be used by them.

Note that ARMv7MState also has a property of the same name but this patch doesn't change it because that class isn't a subclass of CPUState so it wouldn't be a trivial change.

This change should not cause any change in behavior.

Quoted 103 lines
---
 exec.c                | 1 +
 include/hw/core/cpu.h | 4 ++++
 target/arm/cpu.c      | 5 ++---
 target/arm/cpu.h      | 3 ---
 target/arm/kvm32.c    | 2 +-
 target/arm/kvm64.c    | 2 +-
 6 files changed, 9 insertions(+), 8 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

exec.c +1 -0
index 6f381f98e2..82e82fab09 100644
@@ -899,6 +899,7 @@ Property cpu_common_props[] = {
     DEFINE_PROP_LINK("memory", CPUState, memory, TYPE_MEMORY_REGION,
                      MemoryRegion *),
 #endif
+    DEFINE_PROP_BOOL("start-powered-off", CPUState, start_powered_off, false),
     DEFINE_PROP_END_OF_LIST(),
 };
 
include/hw/core/cpu.h +4 -0
index 8f145733ce..9fc2696db5 100644
@@ -374,6 +374,10 @@ struct CPUState {
     bool created;
     bool stop;
     bool stopped;
+
+    /* Should CPU start in powered-off state? */
+    bool start_powered_off;
+
     bool unplug;
     bool crash_occurred;
     bool exit_request;
target/arm/cpu.c +2 -3
index 111579554f..ec65c7653f 100644
@@ -174,8 +174,8 @@ static void arm_cpu_reset(DeviceState *dev)
     env->vfp.xregs[ARM_VFP_MVFR1] = cpu->isar.mvfr1;
     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
 
-    cpu->power_state = cpu->start_powered_off ? PSCI_OFF : PSCI_ON;
-    s->halted = cpu->start_powered_off;
+    cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON;
+    s->halted = s->start_powered_off;
 
     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
@@ -2182,7 +2182,6 @@ static const ARMCPUInfo arm_cpus[] = {
 };
 
 static Property arm_cpu_properties[] = {
-    DEFINE_PROP_BOOL("start-powered-off", ARMCPU, start_powered_off, false),
     DEFINE_PROP_UINT32("psci-conduit", ARMCPU, psci_conduit, 0),
     DEFINE_PROP_UINT64("midr", ARMCPU, midr, 0),
     DEFINE_PROP_UINT64("mp-affinity", ARMCPU,
target/arm/cpu.h +0 -3
index 9e8ed423ea..a925d26996 100644
@@ -810,9 +810,6 @@ struct ARMCPU {
      */
     uint32_t psci_version;
 
-    /* Should CPU start in PSCI powered-off state? */
-    bool start_powered_off;
-
     /* Current power state, access guarded by BQL */
     ARMPSCIState power_state;
 
target/arm/kvm32.c +1 -1
index 0af46b41c8..1f2b8f8b7a 100644
@@ -218,7 +218,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
 
     /* Determine init features for this CPU */
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
-    if (cpu->start_powered_off) {
+    if (cs->start_powered_off) {
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
     }
     if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
target/arm/kvm64.c +1 -1
index 1169237905..f8a6d905fb 100644
@@ -775,7 +775,7 @@ int kvm_arch_init_vcpu(CPUState *cs)
 
     /* Determine init features for this CPU */
     memset(cpu->kvm_init_features, 0, sizeof(cpu->kvm_init_features));
-    if (cpu->start_powered_off) {
+    if (cs->start_powered_off) {
         cpu->kvm_init_features[0] |= 1 << KVM_ARM_VCPU_POWER_OFF;
     }
     if (kvm_check_extension(cs->kvm_state, KVM_CAP_ARM_PSCI_0_2)) {
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#5 lore raw

Re: [PATCH v2 1/9] target/arm: Move start-powered-off property to generic CPUState

Hello David,

Quoted 8 lines · David Gibson
Quoted 7 lines · Thiago Jung Bauermann

There are other platforms which also have CPUs that start powered off, so generalize the start-powered-off property so that it can be used by them.

Note that ARMv7MState also has a property of the same name but this patch doesn't change it because that class isn't a subclass of CPUState so it wouldn't be a trivial change.

Quoted 6 lines
Quoted 4 lines

This change should not cause any change in behavior.

Thank you very much for your review!

Unfortunately I apparently had a minor email mishap and only got your reviews after I sent the v3 patches, so I wasn't able to put your Reviewed-by's in them. Sorry about that.

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
#6 lore raw

PATCH v2 2/9 target/arm: Move setting of CPU halted state to generic code

In reply to Thiago Jung Bauermann

This change is in a separate patch because it's not so obvious that it won't cause a regression.

---
 hw/core/cpu.c    | 2 +-
 target/arm/cpu.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it on an ARM machine. I did on a ppc64le pseries KVM guest.

hw/core/cpu.c +1 -1
index 594441a150..71bb7859f1 100644
@@ -258,7 +258,7 @@ static void cpu_common_reset(DeviceState *dev)
     }
 
     cpu->interrupt_request = 0;
-    cpu->halted = 0;
+    cpu->halted = cpu->start_powered_off;
     cpu->mem_io_pc = 0;
     cpu->icount_extra = 0;
     atomic_set(&cpu->icount_decr_ptr->u32, 0);
target/arm/cpu.c +0 -1
index ec65c7653f..b6c65e4df6 100644
@@ -175,7 +175,6 @@ static void arm_cpu_reset(DeviceState *dev)
     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
 
     cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON;
-    s->halted = s->start_powered_off;
 
     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
Reviewed-by on v2 2/9
#7 lore raw

Re: [PATCH v2 2/9] target/arm: Move setting of CPU halted state to generic code

Quoted 9 lines · Thiago Jung Bauermann

This change is in a separate patch because it's not so obvious that it won't cause a regression.

---
 hw/core/cpu.c    | 2 +-
 target/arm/cpu.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)
Reviewed-by on v2 2/9
#8 lore raw

Re: [PATCH v2 2/9] target/arm: Move setting of CPU halted state to generic code

In reply to Thiago Jung Bauermann
Quoted 5 lines · Thiago Jung Bauermann

This change is in a separate patch because it's not so obvious that it won't cause a regression.

Quoted 34 lines
---
 hw/core/cpu.c    | 2 +-
 target/arm/cpu.c | 1 -
 2 files changed, 1 insertion(+), 2 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it on an ARM machine. I did on a ppc64le pseries KVM guest.

hw/core/cpu.c +1 -1
index 594441a150..71bb7859f1 100644
@@ -258,7 +258,7 @@ static void cpu_common_reset(DeviceState *dev)
     }
 
     cpu->interrupt_request = 0;
-    cpu->halted = 0;
+    cpu->halted = cpu->start_powered_off;
     cpu->mem_io_pc = 0;
     cpu->icount_extra = 0;
     atomic_set(&cpu->icount_decr_ptr->u32, 0);
target/arm/cpu.c +0 -1
index ec65c7653f..b6c65e4df6 100644
@@ -175,7 +175,6 @@ static void arm_cpu_reset(DeviceState *dev)
     env->vfp.xregs[ARM_VFP_MVFR2] = cpu->isar.mvfr2;
 
     cpu->power_state = s->start_powered_off ? PSCI_OFF : PSCI_ON;
-    s->halted = s->start_powered_off;
 
     if (arm_feature(env, ARM_FEATURE_IWMMXT)) {
         env->iwmmxt.cregs[ARM_IWMMXT_wCID] = 0x69051000 | 'Q';
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#9 lore raw

PATCH v2 3/9 ppc/spapr: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann

PowerPC sPAPR CPUs start in the halted state, and spapr_reset_vcpu() attempts to implement this by setting CPUState::halted to 1. But that's too late for the case of hotplugged CPUs in a machine configure with 2 or more threads per core.

By then, other parts of QEMU have already caused the vCPU to run in an unitialized state a couple of times. For example, ppc_cpu_reset() calls ppc_tlb_invalidate_all(), which ends up calling async_run_on_cpu(). This kicks the new vCPU while it has CPUState::halted = 0, causing QEMU to issue a KVM_RUN ioctl on the new vCPU before the guest is able to make the start-cpu RTAS call to initialize its register state.

This problem doesn't seem to cause visible issues for regular guests, but on a secure guest running under the Ultravisor it does. The Ultravisor relies on being able to snoop on the start-cpu RTAS call to map vCPUs to guests, and this issue causes it to see a stray vCPU that doesn't belong to any guest.

Fix by setting the start-powered-off CPUState property in spapr_create_vcpu(), which makes cpu_common_reset() initialize CPUState::halted to 1 at an earlier moment.

---
 hw/ppc/spapr_cpu_core.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

NB: Tested on ppc64le pseries KVM guest with two threads per core. Hot-plugging additional cores doesn't cause the bug described above anymore.

hw/ppc/spapr_cpu_core.c +7 -5
index c4f47dcc04..09feeb5f8f 100644
@@ -36,11 +36,6 @@ static void spapr_reset_vcpu(PowerPCCPU *cpu)
 
     cpu_reset(cs);
 
-    /* All CPUs start halted.  CPU0 is unhalted from the machine level
-     * reset code and the rest are explicitly started up by the guest
-     * using an RTAS call */
-    cs->halted = 1;
-
     env->spr[SPR_HIOR] = 0;
 
     lpcr = env->spr[SPR_LPCR];
@@ -288,6 +283,13 @@ static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp)
 
     cpu->machine_data = g_new0(SpaprCpuState, 1);
 
+    /*
+     * All CPUs start halted. CPU0 is unhalted from the machine level reset code
+     * and the rest are explicitly started up by the guest using an RTAS call.
+     */
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);
+
     object_unref(obj);
     return cpu;
 
#10 lore raw

Re: [PATCH v2 3/9] ppc/spapr: Use start-powered-off CPUState property

Quoted 49 lines · Thiago Jung Bauermann

PowerPC sPAPR CPUs start in the halted state, and spapr_reset_vcpu() attempts to implement this by setting CPUState::halted to 1. But that's too late for the case of hotplugged CPUs in a machine configure with 2 or more threads per core.

By then, other parts of QEMU have already caused the vCPU to run in an unitialized state a couple of times. For example, ppc_cpu_reset() calls ppc_tlb_invalidate_all(), which ends up calling async_run_on_cpu(). This kicks the new vCPU while it has CPUState::halted = 0, causing QEMU to issue a KVM_RUN ioctl on the new vCPU before the guest is able to make the start-cpu RTAS call to initialize its register state.

This problem doesn't seem to cause visible issues for regular guests, but on a secure guest running under the Ultravisor it does. The Ultravisor relies on being able to snoop on the start-cpu RTAS call to map vCPUs to guests, and this issue causes it to see a stray vCPU that doesn't belong to any guest.

Fix by setting the start-powered-off CPUState property in spapr_create_vcpu(), which makes cpu_common_reset() initialize CPUState::halted to 1 at an earlier moment.

---
 hw/ppc/spapr_cpu_core.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

NB: Tested on ppc64le pseries KVM guest with two threads per core. Hot-plugging additional cores doesn't cause the bug described above anymore.

hw/ppc/spapr_cpu_core.c +0 -5
index c4f47dcc04..09feeb5f8f 100644
@@ -36,11 +36,6 @@ static void spapr_reset_vcpu(PowerPCCPU *cpu)
 
     cpu_reset(cs);
 
-    /* All CPUs start halted.  CPU0 is unhalted from the machine level
-     * reset code and the rest are explicitly started up by the guest
-     * using an RTAS call */
-    cs->halted = 1;
-
     env->spr[SPR_HIOR] = 0;
 
     lpcr = env->spr[SPR_LPCR];

hw/ppc/spapr_cpu_core.c static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp) · line 291

Quoted 9 lines
hw/ppc/spapr_cpu_core.c +6 -0
@@ -288,6 +283,13 @@ static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp)
 
     cpu->machine_data = g_new0(SpaprCpuState, 1);
 
+    /*
+     * All CPUs start halted. CPU0 is unhalted from the machine level reset code
+     * and the rest are explicitly started up by the guest using an RTAS call.
+     */
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);

Since here object_new() is used, it is simpler to set the field before the object is realized, similarly to cs->cpu_index:

-- >8 --
@@ -275,6 +275,11 @@ static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp)

     cs = CPU(obj);
     cpu = POWERPC_CPU(obj);
     cs->cpu_index = cc->core_id + i;

+ /*
+ * All CPUs start halted. CPU0 is unhalted from the machine level
reset code
+ * and the rest are explicitly started up by the guest using an
RTAS call.
+ */

+    cs->start_powered_off = true;
     spapr_set_vcpu_id(cpu, cs->cpu_index, &local_err);
     if (local_err) {
         goto err;

---

Quoted 5 lines
hw/ppc/spapr_cpu_core.c +1 -0
+
     object_unref(obj);
     return cpu;
 
#11 lore raw

Re: [PATCH v2 3/9] ppc/spapr: Use start-powered-off CPUState property

Quoted 74 lines · Philippe Mathieu-Daudé
Quoted 49 lines · Thiago Jung Bauermann

PowerPC sPAPR CPUs start in the halted state, and spapr_reset_vcpu() attempts to implement this by setting CPUState::halted to 1. But that's too late for the case of hotplugged CPUs in a machine configure with 2 or more threads per core.

By then, other parts of QEMU have already caused the vCPU to run in an unitialized state a couple of times. For example, ppc_cpu_reset() calls ppc_tlb_invalidate_all(), which ends up calling async_run_on_cpu(). This kicks the new vCPU while it has CPUState::halted = 0, causing QEMU to issue a KVM_RUN ioctl on the new vCPU before the guest is able to make the start-cpu RTAS call to initialize its register state.

This problem doesn't seem to cause visible issues for regular guests, but on a secure guest running under the Ultravisor it does. The Ultravisor relies on being able to snoop on the start-cpu RTAS call to map vCPUs to guests, and this issue causes it to see a stray vCPU that doesn't belong to any guest.

Fix by setting the start-powered-off CPUState property in spapr_create_vcpu(), which makes cpu_common_reset() initialize CPUState::halted to 1 at an earlier moment.

---
 hw/ppc/spapr_cpu_core.c | 12 +++++++-----
 1 file changed, 7 insertions(+), 5 deletions(-)

NB: Tested on ppc64le pseries KVM guest with two threads per core. Hot-plugging additional cores doesn't cause the bug described above anymore.

hw/ppc/spapr_cpu_core.c +0 -5
index c4f47dcc04..09feeb5f8f 100644
@@ -36,11 +36,6 @@ static void spapr_reset_vcpu(PowerPCCPU *cpu)
 
     cpu_reset(cs);
 
-    /* All CPUs start halted.  CPU0 is unhalted from the machine level
-     * reset code and the rest are explicitly started up by the guest
-     * using an RTAS call */
-    cs->halted = 1;
-
     env->spr[SPR_HIOR] = 0;
 
     lpcr = env->spr[SPR_LPCR];

hw/ppc/spapr_cpu_core.c static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp) · line 291

Quoted 9 lines
hw/ppc/spapr_cpu_core.c +6 -0
@@ -288,6 +283,13 @@ static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore *sc, int i, Error **errp)
 
     cpu->machine_data = g_new0(SpaprCpuState, 1);
 
+    /*
+     * All CPUs start halted. CPU0 is unhalted from the machine level reset code
+     * and the rest are explicitly started up by the guest using an RTAS call.
+     */
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);

Since here object_new() is used, it is simpler to set the field before the object is realized, similarly to cs->cpu_index:

-- >8 --

(quoted diff) +2 -0
@@ -275,6 +275,11 @@ static PowerPCCPU *spapr_create_vcpu(SpaprCpuCore
 *sc, int i, Error **errp)
     cs = CPU(obj);
     cpu = POWERPC_CPU(obj);
     cs->cpu_index = cc->core_id + i;
+    /*
+     * All CPUs start halted. CPU0 is unhalted from the machine level

reset code
+ * and the rest are explicitly started up by the guest using an
RTAS call.

Review

Quoted 6 lines
(quoted diff) +2 -1
+     */
+    cs->start_powered_off = true;
     spapr_set_vcpu_id(cpu, cs->cpu_index, &local_err);
     if (local_err) {
         goto err;
---

Good point. I adopted your suggestion.

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
#12 lore raw

PATCH v2 4/9 ppc/e500: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in ppce500_cpu_reset_sec(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 hw/ppc/e500.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/ppc/e500.c +7 -3
index ab9884e315..dda71bc05d 100644
@@ -704,9 +704,6 @@ static void ppce500_cpu_reset_sec(void *opaque)
 
     cpu_reset(cs);
 
-    /* Secondary CPU starts in halted state for now. Needs to change when
-       implementing non-kernel boot. */
-    cs->halted = 1;
     cs->exception_index = EXCP_HLT;
 }
 
@@ -897,6 +894,13 @@ void ppce500_init(MachineState *machine)
         } else {
             /* Secondary CPUs */
             qemu_register_reset(ppce500_cpu_reset_sec, cpu);
+
+            /*
+             * Secondary CPU starts in halted state for now. Needs to change
+             * when implementing non-kernel boot.
+             */
+            object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                                     &error_abort);
         }
     }
 
Reviewed-by on v2 4/9
#13 lore raw

Re: [PATCH v2 4/9] ppc/e500: Use start-powered-off CPUState property

Quoted 29 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in ppce500_cpu_reset_sec(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 hw/ppc/e500.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/ppc/e500.c +0 -3
index ab9884e315..dda71bc05d 100644
@@ -704,9 +704,6 @@ static void ppce500_cpu_reset_sec(void *opaque)
 
     cpu_reset(cs);
 
-    /* Secondary CPU starts in halted state for now. Needs to change when
-       implementing non-kernel boot. */
-    cs->halted = 1;
     cs->exception_index = EXCP_HLT;
 }
 
@@ -897,6 +894,13 @@ void ppce500_init(MachineState *machine)
         } else {
             /* Secondary CPUs */

hw/ppc/e500.c void ppce500_init(MachineState *machine) · line 905

Quoted 10 lines
hw/ppc/e500.c +7 -0
             qemu_register_reset(ppce500_cpu_reset_sec, cpu);
+
+            /*
+             * Secondary CPU starts in halted state for now. Needs to change
+             * when implementing non-kernel boot.
+             */
+            object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                                     &error_abort);
         }
     }
Reviewed-by on v2 4/9
#14 lore raw

Re: [PATCH v2 4/9] ppc/e500: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann
Quoted 5 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in ppce500_cpu_reset_sec(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

Quoted 36 lines
---
 hw/ppc/e500.c | 10 +++++++---
 1 file changed, 7 insertions(+), 3 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/ppc/e500.c +7 -3
index ab9884e315..dda71bc05d 100644
@@ -704,9 +704,6 @@ static void ppce500_cpu_reset_sec(void *opaque)
 
     cpu_reset(cs);
 
-    /* Secondary CPU starts in halted state for now. Needs to change when
-       implementing non-kernel boot. */
-    cs->halted = 1;
     cs->exception_index = EXCP_HLT;
 }
 
@@ -897,6 +894,13 @@ void ppce500_init(MachineState *machine)
         } else {
             /* Secondary CPUs */
             qemu_register_reset(ppce500_cpu_reset_sec, cpu);
+
+            /*
+             * Secondary CPU starts in halted state for now. Needs to change
+             * when implementing non-kernel boot.
+             */
+            object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                                     &error_abort);
         }
     }
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#15 lore raw

PATCH v2 5/9 mips/cps: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in main_cpu_reset(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 hw/mips/cps.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/mips/cps.c +3 -3
index 615e1a1ad2..d5b6c78019 100644
@@ -52,9 +52,6 @@ static void main_cpu_reset(void *opaque)
     CPUState *cs = CPU(cpu);
 
     cpu_reset(cs);
-
-    /* All VPs are halted on reset. Leave powering up to CPC. */
-    cs->halted = 1;
 }
 
 static bool cpu_mips_itu_supported(CPUMIPSState *env)
@@ -89,6 +86,9 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
             env->itc_tag = mips_itu_get_tag_region(&s->itu);
             env->itu = &s->itu;
         }
+        /* All VPs are halted on reset. Leave powering up to CPC. */
+        object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
+                                 &error_abort);
         qemu_register_reset(main_cpu_reset, cpu);
     }
 
Reviewed-by on v2 5/9
#16 lore raw

Re: [PATCH v2 5/9] mips/cps: Use start-powered-off CPUState property

Quoted 27 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in main_cpu_reset(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 hw/mips/cps.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/mips/cps.c +0 -3
index 615e1a1ad2..d5b6c78019 100644
@@ -52,9 +52,6 @@ static void main_cpu_reset(void *opaque)
     CPUState *cs = CPU(cpu);
 
     cpu_reset(cs);
-
-    /* All VPs are halted on reset. Leave powering up to CPC. */
-    cs->halted = 1;
 }
 
 static bool cpu_mips_itu_supported(CPUMIPSState *env)

hw/mips/cps.c static void mips_cps_realize(DeviceState *dev, Error **errp) · line 91

Quoted 6 lines
hw/mips/cps.c +3 -0
@@ -89,6 +86,9 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
             env->itc_tag = mips_itu_get_tag_region(&s->itu);
             env->itu = &s->itu;
         }
+        /* All VPs are halted on reset. Leave powering up to CPC. */
+        object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
+                                 &error_abort);

This is indeed better as now the property is set once, *after* realize but *before* reset.

Quoted 4 lines
hw/mips/cps.c +0 -0
         qemu_register_reset(main_cpu_reset, cpu);
     }
 
#17 lore raw

Re: [PATCH v2 5/9] mips/cps: Use start-powered-off CPUState property

Quoted 38 lines · Philippe Mathieu-Daudé
Quoted 27 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in main_cpu_reset(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 hw/mips/cps.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/mips/cps.c +0 -3
index 615e1a1ad2..d5b6c78019 100644
@@ -52,9 +52,6 @@ static void main_cpu_reset(void *opaque)
     CPUState *cs = CPU(cpu);
 
     cpu_reset(cs);
-
-    /* All VPs are halted on reset. Leave powering up to CPC. */
-    cs->halted = 1;
 }
 
 static bool cpu_mips_itu_supported(CPUMIPSState *env)

hw/mips/cps.c static void mips_cps_realize(DeviceState *dev, Error **errp) · line 91

Quoted 6 lines
hw/mips/cps.c +3 -0
@@ -89,6 +86,9 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
             env->itc_tag = mips_itu_get_tag_region(&s->itu);
             env->itu = &s->itu;
         }
+        /* All VPs are halted on reset. Leave powering up to CPC. */
+        object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
+                                 &error_abort);

This is indeed better as now the property is set once, *after* realize but *before* reset.

Quoted 1 line

Thanks for confirming!

Quoted 4 lines
Quoted 4 lines
hw/mips/cps.c +0 -0
         qemu_register_reset(main_cpu_reset, cpu);
     }
 
Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
Reviewed-by on v2 5/9
#18 lore raw

Re: [PATCH v2 5/9] mips/cps: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann
Quoted 5 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in main_cpu_reset(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

Quoted 32 lines
---
 hw/mips/cps.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/mips/cps.c +3 -3
index 615e1a1ad2..d5b6c78019 100644
@@ -52,9 +52,6 @@ static void main_cpu_reset(void *opaque)
     CPUState *cs = CPU(cpu);
 
     cpu_reset(cs);
-
-    /* All VPs are halted on reset. Leave powering up to CPC. */
-    cs->halted = 1;
 }
 
 static bool cpu_mips_itu_supported(CPUMIPSState *env)
@@ -89,6 +86,9 @@ static void mips_cps_realize(DeviceState *dev, Error **errp)
             env->itc_tag = mips_itu_get_tag_region(&s->itu);
             env->itu = &s->itu;
         }
+        /* All VPs are halted on reset. Leave powering up to CPC. */
+        object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
+                                 &error_abort);
         qemu_register_reset(main_cpu_reset, cpu);
     }
 
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#19 lore raw

PATCH v2 6/9 sparc/sun4m: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in secondary_cpu_reset(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 hw/sparc/sun4m.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/sparc/sun4m.c +2 -1
index 9be930415f..766e79bb5e 100644
@@ -233,7 +233,6 @@ static void secondary_cpu_reset(void *opaque)
     CPUState *cs = CPU(cpu);
 
     cpu_reset(cs);
-    cs->halted = 1;
 }
 
 static void cpu_halt_signal(void *opaque, int irq, int level)
@@ -833,6 +832,8 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
         qemu_register_reset(secondary_cpu_reset, cpu);
         cs = CPU(cpu);
         cs->halted = 1;
+        object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                                 &error_abort);
     }
     *cpu_irqs = qemu_allocate_irqs(cpu_set_irq, cpu, MAX_PILS);
     env->prom_addr = prom_addr;
Reviewed-by on v2 6/9
#20 lore raw

Re: [PATCH v2 6/9] sparc/sun4m: Use start-powered-off CPUState property

Quoted 5 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in secondary_cpu_reset(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

Quoted 29 lines
---
 hw/sparc/sun4m.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/sparc/sun4m.c +2 -1
index 9be930415f..766e79bb5e 100644
@@ -233,7 +233,6 @@ static void secondary_cpu_reset(void *opaque)
     CPUState *cs = CPU(cpu);
 
     cpu_reset(cs);
-    cs->halted = 1;
 }
 
 static void cpu_halt_signal(void *opaque, int irq, int level)
@@ -833,6 +832,8 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
         qemu_register_reset(secondary_cpu_reset, cpu);
         cs = CPU(cpu);
         cs->halted = 1;
+        object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                                 &error_abort);
     }
     *cpu_irqs = qemu_allocate_irqs(cpu_set_irq, cpu, MAX_PILS);
     env->prom_addr = prom_addr;
Signature · 4 lines
David Gibson			| I'll have my music baroque, and my code
david AT gibson.dropbear.id.au	| minimalist, thank you.  NOT _the_ _other_
				| _way_ _around_!
http://www.ozlabs.org/~dgibson
#21 lore raw

RFC PATCH v2 7/9 sparc/sun4m: Don't set CPUState::halted in cpu_devinit()

In reply to Thiago Jung Bauermann

Remove setting of cs->halted from cpu_devinit(), which seems out of place when compared to similar code in other architectures (e.g., ppce500_init() in hw/ppc/e500.c).

---
 hw/sparc/sun4m.c | 1 -
 1 file changed, 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/sparc/sun4m.c +0 -1
index 766e79bb5e..7b3042a801 100644
@@ -831,7 +831,6 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
     } else {
         qemu_register_reset(secondary_cpu_reset, cpu);
         cs = CPU(cpu);
-        cs->halted = 1;
         object_property_set_bool(OBJECT(cs), "start-powered-off", true,
                                  &error_abort);
     }
#22 lore raw

Re: [RFC PATCH v2 7/9] sparc/sun4m: Don't set CPUState::halted in cpu_devinit()

Quoted 18 lines · Thiago Jung Bauermann

Remove setting of cs->halted from cpu_devinit(), which seems out of place when compared to similar code in other architectures (e.g., ppce500_init() in hw/ppc/e500.c).

---
 hw/sparc/sun4m.c | 1 -
 1 file changed, 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/sparc/sun4m.c +0 -0
index 766e79bb5e..7b3042a801 100644

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 836

Quoted 7 lines
hw/sparc/sun4m.c +0 -1
@@ -831,7 +831,6 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
     } else {
         qemu_register_reset(secondary_cpu_reset, cpu);
         cs = CPU(cpu);
-        cs->halted = 1;
         object_property_set_bool(OBJECT(cs), "start-powered-off", true,
                                  &error_abort);
     }

Why not squash with previous patch?

#23 lore raw

Re: [RFC PATCH v2 7/9] sparc/sun4m: Don't set CPUState::halted in cpu_devinit()

Quoted 13 lines · Philippe Mathieu-Daudé
Quoted 12 lines · Thiago Jung Bauermann

Remove setting of cs->halted from cpu_devinit(), which seems out of place when compared to similar code in other architectures (e.g., ppce500_init() in hw/ppc/e500.c).

---
 hw/sparc/sun4m.c | 1 -
 1 file changed, 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

Quoted 15 lines

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 836

Quoted 13 lines
hw/sparc/sun4m.c +0 -1
index 766e79bb5e..7b3042a801 100644
@@ -831,7 +831,6 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
     } else {
         qemu_register_reset(secondary_cpu_reset, cpu);
         cs = CPU(cpu);
-        cs->halted = 1;
         object_property_set_bool(OBJECT(cs), "start-powered-off", true,
                                  &error_abort);
     }

Why not squash with previous patch?

I wasn't sure about this change, and it's also not strictly necessary for this patch set so I wanted to make it easy for maintainers to not apply it.

I squashed it for v3.

--
Thiago Jung Bauermann
IBM Linux Technology Center

#24 lore raw

RFC PATCH v2 8/9 sparc/sun4m: Use one cpu_reset() function for main and secondary CPUs

In reply to Thiago Jung Bauermann

If we rely on cpu_common_reset() setting CPUState::halted according to the start-powered-off property, both reset functions become equivalent and we can use only one.

---
 hw/sparc/sun4m.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/sparc/sun4m.c +4 -17
index 7b3042a801..deb5e9f027 100644
@@ -218,16 +218,7 @@ static void dummy_cpu_set_irq(void *opaque, int irq, int level)
 {
 }
 
-static void main_cpu_reset(void *opaque)
-{
-    SPARCCPU *cpu = opaque;
-    CPUState *cs = CPU(cpu);
-
-    cpu_reset(cs);
-    cs->halted = 0;
-}
-
-static void secondary_cpu_reset(void *opaque)
+static void sun4m_cpu_reset(void *opaque)
 {
     SPARCCPU *cpu = opaque;
     CPUState *cs = CPU(cpu);
@@ -818,7 +809,6 @@ static const TypeInfo ram_info = {
 static void cpu_devinit(const char *cpu_type, unsigned int id,
                         uint64_t prom_addr, qemu_irq **cpu_irqs)
 {
-    CPUState *cs;
     SPARCCPU *cpu;
     CPUSPARCState *env;
 
@@ -826,12 +816,9 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
     env = &cpu->env;
 
     cpu_sparc_set_id(env, id);
-    if (id == 0) {
-        qemu_register_reset(main_cpu_reset, cpu);
-    } else {
-        qemu_register_reset(secondary_cpu_reset, cpu);
-        cs = CPU(cpu);
-        object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+    qemu_register_reset(sun4m_cpu_reset, cpu);
+    if (id != 0) {
+        object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
                                  &error_abort);
     }
     *cpu_irqs = qemu_allocate_irqs(cpu_set_irq, cpu, MAX_PILS);
#25 lore raw

Re: [RFC PATCH v2 8/9] sparc/sun4m: Use one cpu_reset() function for main and secondary CPUs

Quoted 43 lines · Thiago Jung Bauermann

If we rely on cpu_common_reset() setting CPUState::halted according to the start-powered-off property, both reset functions become equivalent and we can use only one.

---
 hw/sparc/sun4m.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/sparc/sun4m.c +1 -11
index 7b3042a801..deb5e9f027 100644
@@ -218,16 +218,7 @@ static void dummy_cpu_set_irq(void *opaque, int irq, int level)
 {
 }
 
-static void main_cpu_reset(void *opaque)
-{
-    SPARCCPU *cpu = opaque;
-    CPUState *cs = CPU(cpu);
-
-    cpu_reset(cs);
-    cs->halted = 0;
-}
-
-static void secondary_cpu_reset(void *opaque)
+static void sun4m_cpu_reset(void *opaque)
 {
     SPARCCPU *cpu = opaque;
     CPUState *cs = CPU(cpu);
@@ -818,7 +809,6 @@ static const TypeInfo ram_info = {
 static void cpu_devinit(const char *cpu_type, unsigned int id,
                         uint64_t prom_addr, qemu_irq **cpu_irqs)
 {
-    CPUState *cs;
     SPARCCPU *cpu;
     CPUSPARCState *env;
 

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 830

Quoted 5 lines
hw/sparc/sun4m.c +0 -2
@@ -826,12 +816,9 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
     env = &cpu->env;
 
     cpu_sparc_set_id(env, id);
-    if (id == 0) {
-        qemu_register_reset(main_cpu_reset, cpu);

IMO it is easier to review this patch in 2, first drop main_cpu_reset as it is pointless (we rely on cpu_common_reset), then set the "start-powered-off" property and drop secondary_cpu_reset().

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 819

Quoted 5 lines
hw/sparc/sun4m.c +1 -4
-    } else {
-        qemu_register_reset(secondary_cpu_reset, cpu);
-        cs = CPU(cpu);
-        object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+    qemu_register_reset(sun4m_cpu_reset, cpu);

Why do you still keep it?

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 822

Quoted 3 lines
hw/sparc/sun4m.c +2 -0
+    if (id != 0) {
+        object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
                                  &error_abort);

At this point the CPU is realized, so this is correct.

I'd use directly:

object_property_set_bool(OBJECT(cpu), "start-powered-off", !!id,
&error_abort);

Quoted 3 lines
hw/sparc/sun4m.c +0 -0
     }
     *cpu_irqs = qemu_allocate_irqs(cpu_set_irq, cpu, MAX_PILS);
#26 lore raw

Re: [RFC PATCH v2 8/9] sparc/sun4m: Use one cpu_reset() function for main and secondary CPUs

Quoted 46 lines · Philippe Mathieu-Daudé
Quoted 45 lines · Thiago Jung Bauermann

If we rely on cpu_common_reset() setting CPUState::halted according to the start-powered-off property, both reset functions become equivalent and we can use only one.

---
 hw/sparc/sun4m.c | 21 ++++-----------------
 1 file changed, 4 insertions(+), 17 deletions(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

hw/sparc/sun4m.c +1 -11
index 7b3042a801..deb5e9f027 100644
@@ -218,16 +218,7 @@ static void dummy_cpu_set_irq(void *opaque, int irq, int level)
 {
 }
 
-static void main_cpu_reset(void *opaque)
-{
-    SPARCCPU *cpu = opaque;
-    CPUState *cs = CPU(cpu);
-
-    cpu_reset(cs);
-    cs->halted = 0;
-}
-
-static void secondary_cpu_reset(void *opaque)
+static void sun4m_cpu_reset(void *opaque)
 {
     SPARCCPU *cpu = opaque;
     CPUState *cs = CPU(cpu);
@@ -818,7 +809,6 @@ static const TypeInfo ram_info = {
 static void cpu_devinit(const char *cpu_type, unsigned int id,
                         uint64_t prom_addr, qemu_irq **cpu_irqs)
 {
-    CPUState *cs;
     SPARCCPU *cpu;
     CPUSPARCState *env;
 
@@ -826,12 +816,9 @@ static void cpu_devinit(const char *cpu_type, unsigned int id,
     env = &cpu->env;
 
Quoted 7 lines

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 830

Quoted 3 lines
hw/sparc/sun4m.c +0 -2
     cpu_sparc_set_id(env, id);
-    if (id == 0) {
-        qemu_register_reset(main_cpu_reset, cpu);

IMO it is easier to review this patch in 2, first drop main_cpu_reset as it is pointless (we rely on cpu_common_reset), then set the "start-powered-off" property and drop secondary_cpu_reset().

That's a good idea. I made those patches for v3.

Quoted 7 lines

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 819

Quoted 5 lines
hw/sparc/sun4m.c +1 -4
-    } else {
-        qemu_register_reset(secondary_cpu_reset, cpu);
-        cs = CPU(cpu);
-        object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+    qemu_register_reset(sun4m_cpu_reset, cpu);

Why do you still keep it?

I didn't know that not registering a reset function would cause cpu_reset() to be caused.

Quoted 5 lines

hw/sparc/sun4m.c static void cpu_devinit(const char *cpu_type, unsigned int id, · line 822

Quoted 3 lines
hw/sparc/sun4m.c +2 -0
+    if (id != 0) {
+        object_property_set_bool(OBJECT(cpu), "start-powered-off", true,
                                  &error_abort);

At this point the CPU is realized, so this is correct.

Great. Thanks for confirming!

Quoted 4 lines

I'd use directly:

object_property_set_bool(OBJECT(cpu), "start-powered-off", !!id,
&error_abort);

I used a slight variation of your suggestion, with id != 0 instead of !!id because I think it makes the code easier to read.

--
Thiago Jung Bauermann
IBM Linux Technology Center

#27 lore raw

RFC PATCH v2 9/9 target/s390x: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in s390_cpu_initfn(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 target/s390x/cpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

target/s390x/cpu.c +2 -1
index 08eb674d22..d3a14af1d9 100644
@@ -291,7 +291,8 @@ static void s390_cpu_initfn(Object *obj)
     S390CPU *cpu = S390_CPU(obj);
 
     cpu_set_cpustate_pointers(cpu);
-    cs->halted = 1;
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);
     cs->exception_index = EXCP_HLT;
 #if !defined(CONFIG_USER_ONLY)
     object_property_add(obj, "crash-information", "GuestPanicInformation",
#28 lore raw

Re: [RFC PATCH v2 9/9] target/s390x: Use start-powered-off CPUState property

Quoted 17 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in s390_cpu_initfn(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 target/s390x/cpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

target/s390x/cpu.c +0 -0
index 08eb674d22..d3a14af1d9 100644

target/s390x/cpu.c static void s390_cpu_initfn(Object *obj) · line 295

Quoted 6 lines
target/s390x/cpu.c +2 -1
@@ -291,7 +291,8 @@ static void s390_cpu_initfn(Object *obj)
     S390CPU *cpu = S390_CPU(obj);
 
     cpu_set_cpustate_pointers(cpu);
-    cs->halted = 1;
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);

Here this seems overkill since this is the same object, so you can directly do:

+ cs->start_powered_off = true;

Quoted 4 lines
target/s390x/cpu.c +0 -0
     cs->exception_index = EXCP_HLT;
 #if !defined(CONFIG_USER_ONLY)
     object_property_add(obj, "crash-information", "GuestPanicInformation",
#29 lore raw

Re: [RFC PATCH v2 9/9] target/s390x: Use start-powered-off CPUState property

Quoted 28 lines · Philippe Mathieu-Daudé
Quoted 17 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in s390_cpu_initfn(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 target/s390x/cpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

target/s390x/cpu.c +0 -0
index 08eb674d22..d3a14af1d9 100644

target/s390x/cpu.c static void s390_cpu_initfn(Object *obj) · line 295

Quoted 6 lines
target/s390x/cpu.c +2 -1
@@ -291,7 +291,8 @@ static void s390_cpu_initfn(Object *obj)
     S390CPU *cpu = S390_CPU(obj);
 
     cpu_set_cpustate_pointers(cpu);
-    cs->halted = 1;
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);

Here this seems overkill since this is the same object, so you can directly do:

Quoted 1 line

+ cs->start_powered_off = true;

I adopted your suggestion.

Signature · 2 lines
Thiago Jung Bauermann
IBM Linux Technology Center
#30 lore raw

Re: [RFC PATCH v2 9/9] target/s390x: Use start-powered-off CPUState property

In reply to Thiago Jung Bauermann
Quoted 17 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in s390_cpu_initfn(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 target/s390x/cpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

target/s390x/cpu.c +0 -0
index 08eb674d22..d3a14af1d9 100644

target/s390x/cpu.c static void s390_cpu_initfn(Object *obj) · line 295

Quoted 6 lines
target/s390x/cpu.c +2 -1
@@ -291,7 +291,8 @@ static void s390_cpu_initfn(Object *obj)
     S390CPU *cpu = S390_CPU(obj);
 
     cpu_set_cpustate_pointers(cpu);
-    cs->halted = 1;
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);

Is this really OK? s390 CPUs don't seem to set halted=1 on reset today.

Quoted 4 lines
target/s390x/cpu.c +0 -0
     cs->exception_index = EXCP_HLT;
 #if !defined(CONFIG_USER_ONLY)
     object_property_add(obj, "crash-information", "GuestPanicInformation",
Signature · 1 line
Eduardo
#31 lore raw

Re: [RFC PATCH v2 9/9] target/s390x: Use start-powered-off CPUState property

Quoted 20 lines · Eduardo Habkost
Quoted 19 lines · Thiago Jung Bauermann

Instead of setting CPUState::halted to 1 in s390_cpu_initfn(), use the start-powered-off property which makes cpu_common_reset() initialize it to 1 in common code.

---
 target/s390x/cpu.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

NB: I was only able to test that this patch builds. I wasn't able to run it.

target/s390x/cpu.c +0 -0
index 08eb674d22..d3a14af1d9 100644
@@ -291,7 +291,8 @@ static void s390_cpu_initfn(Object *obj)
     S390CPU *cpu = S390_CPU(obj);
 
Quoted 7 lines

target/s390x/cpu.c static void s390_cpu_initfn(Object *obj) · line 295

Quoted 4 lines
target/s390x/cpu.c +2 -1
     cpu_set_cpustate_pointers(cpu);
-    cs->halted = 1;
+    object_property_set_bool(OBJECT(cs), "start-powered-off", true,
+                             &error_abort);

Is this really OK? s390 CPUs don't seem to set halted=1 on reset today.

Hm, good point. That is indeed a behavior change that this patch introduces. I'll point it out in the description for v3, and if it's wrong then this patch can simply be dropped.

--
Thiago Jung Bauermann
IBM Linux Technology Center