Skip to contentSkip to conversation

exec/cpu: Call cpu_exec_realizefn() once in cpu_common_realize()

PATCH In review Philippe Mathieu-Daudé 5 participants 52 messages Sep 18, 2023 – Jan 22, 2025

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover exec/cpu: Call cpu_exec_realizefn() once in cpu_common_realize()

Hi,

TL;DR: This series factor duplicated common code in CPUs DeviceRealize() handlers out, moving as a single call in cpu_common_realize().

In an effort to have most of:
- CPU core code independant of accelerators
- CPU core code target agnostic
- CPU target code independant of accelerators
- and cpu_reset() called automatically without having to rely on global QEMUResetHandler,
I'm working on the core CPU code, unfortunately touching files in all targets.

I suppose them term "exec" used in various areas of QEMU started from what we call today "accel[erators]" [*]. So cpu_exec_realizefn() can be read as cpu_accel_realize(), or "generic code where an accelerator realizes its internal fields on an abstract (target independent) CPU".

This series moves a common pattern used in all target's cpu_realize() handlers to the common cpu_exec_realizefn().

Some optional code is used to check CPU requested features are compatible with the accelerator possibilities. We extracted this code as CPUClass::verify_accel_features() handler. Better name welcomed :)

Some targets were calling cpu_reset() *before* cpu_common_realizefn(), we moved it *after* (since RESET shouldn't happen before REALIZE). I still have to audit each target to confirm there are no side effects. Besides this cpu_reset() change, the rest should be relatively trivial to review, still I'd like feedback from the respective target maintainers for the "move HW creation after vCPU one" patches.

Regards,

Phil.

Follow-up: Make cpu_reset() accel-agnostic and move it
to cpu_common_realize() (not trivial due to KVM run_on_cpu() calls).

[*] If Paolo/Richard confirm, I might post series renaming
various APIs s/exec/accel/, because various headers meaning aren't clear to me.

Philippe Mathieu-Daudé (21):
target/i386: Only realize existing APIC device hw/intc/apic: Pass CPU using QOM link property target/i386/kvm: Correct comment in kvm_cpu_realize() exec/cpu: Never call cpu_reset() before cpu_realize() exec/cpu: Call qemu_init_vcpu() once in cpu_common_realize() exec/cpu: Call cpu_remove_sync() once in cpu_common_unrealize() exec/cpu: RFC Destroy vCPU address spaces in cpu_common_unrealize() target/arm: Create timers *after* accelerator vCPU is realized target/hppa: Create timer *after* accelerator vCPU is realized target/nios2: Create IRQs *after* accelerator vCPU is realized target/mips: Create clock *after* accelerator vCPU is realized target/xtensa: Create IRQs *after* accelerator vCPU is realized target/sparc: Init CPU environment *after* accelerator vCPU is
realized
exec/cpu: Introduce CPUClass::verify_accel_features() target/arm: Extract verify_accel_features() from cpu_realize() target/i386: Extract verify_accel_features() from cpu_realize() target/s390x: Call s390_cpu_realize_sysemu from s390_realize_cpu_model target/s390x: Have s390_realize_cpu_model() return a boolean target/s390x: Use s390_realize_cpu_model() as verify_accel_features() exec/cpu: Have cpu_exec_realize() return a boolean exec/cpu: Call cpu_exec_realizefn() once in cpu_common_realize()

xianglai li (1):
exec/cpu: Introduce the CPU address space destruction function

 include/exec/cpu-common.h     |  8 ++++
 include/hw/core/cpu.h         |  7 +++-
 target/s390x/s390x-internal.h |  4 +-
 cpu.c                         | 11 ++++-
 hw/core/cpu-common.c          | 15 +++++++
 hw/intc/apic_common.c         |  2 +
 softmmu/physmem.c             | 24 +++++++++++
 target/alpha/cpu.c            | 10 -----
 target/arm/cpu.c              | 53 ++++++++++++------------
 target/avr/cpu.c              | 10 +----
 target/cris/cpu.c             | 11 +----
 target/hexagon/cpu.c          | 11 +----
 target/hppa/cpu.c             | 20 ++-------
 target/i386/cpu-sysemu.c      | 20 ++++-----
 target/i386/cpu.c             | 77 ++++++++++++++++++-----------------
 target/i386/kvm/kvm-cpu.c     |  3 +-
 target/loongarch/cpu.c        | 11 +----
 target/m68k/cpu.c             | 11 +----
 target/microblaze/cpu.c       |  9 ----
 target/mips/cpu.c             | 27 ++++--------
 target/nios2/cpu.c            | 20 +++------
 target/openrisc/cpu.c         | 11 +----
 target/ppc/cpu_init.c         |  8 ----
 target/riscv/cpu.c            | 10 +----
 target/rx/cpu.c               | 11 +----
 target/s390x/cpu-sysemu.c     |  3 +-
 target/s390x/cpu.c            | 21 +---------
 target/s390x/cpu_models.c     | 16 +++++---
 target/sh4/cpu.c              | 11 +----
 target/sparc/cpu.c            | 10 -----
 target/tricore/cpu.c          | 10 +----
 target/xtensa/cpu.c           | 13 +-----
 32 files changed, 189 insertions(+), 299 deletions(-)
Signature · 1 line
2.41.0
#1 lore raw

PATCH 1/22 target/i386: Only realize existing APIC device

APIC state is created under a certain condition,
use the same condition to realize it.
Having a NULL APIC state is a bug: use assert().

---
 target/i386/cpu-sysemu.c | 9 +++------
 target/i386/cpu.c        | 8 +++++---
 2 files changed, 8 insertions(+), 9 deletions(-)
target/i386/cpu-sysemu.c +3 -6
index 2375e48178..6a164d3769 100644
@@ -272,9 +272,7 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
     APICCommonState *apic;
     APICCommonClass *apic_class = apic_get_class(errp);
 
-    if (!apic_class) {
-        return;
-    }
+    assert(apic_class);
 
     cpu->apic_state = DEVICE(object_new_with_class(OBJECT_CLASS(apic_class)));
     object_property_add_child(OBJECT(cpu), "lapic",
@@ -293,9 +291,8 @@ void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
     APICCommonState *apic;
     static bool apic_mmio_map_once;
 
-    if (cpu->apic_state == NULL) {
-        return;
-    }
+    assert(cpu->apic_state);
+
     qdev_realize(DEVICE(cpu->apic_state), NULL, errp);
 
     /* Map APIC MMIO area */
target/i386/cpu.c +5 -3
index b2a20365e1..a23d4795e0 100644
@@ -7448,9 +7448,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
 #ifndef CONFIG_USER_ONLY
-    x86_cpu_apic_realize(cpu, &local_err);
-    if (local_err != NULL) {
-        goto out;
+    if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || ms->smp.cpus > 1) {
+        x86_cpu_apic_realize(cpu, &local_err);
+        if (local_err != NULL) {
+            goto out;
+        }
     }
 #endif /* !CONFIG_USER_ONLY */
     cpu_reset(cs);
Signature · 1 line
2.41.0
#2 lore raw

Re: [PATCH 01/22] target/i386: Only realize existing APIC device

Quoted 15 lines · Philippe Mathieu-Daudé

APIC state is created under a certain condition,
use the same condition to realize it.
Having a NULL APIC state is a bug: use assert().

---
  target/i386/cpu-sysemu.c | 9 +++------
  target/i386/cpu.c        | 8 +++++---
  2 files changed, 8 insertions(+), 9 deletions(-)
target/i386/cpu-sysemu.c +0 -0
index 2375e48178..6a164d3769 100644

target/i386/cpu-sysemu.c void x86_cpu_apic_create(X86CPU *cpu, Error **errp) · line 275

Quoted 7 lines
target/i386/cpu-sysemu.c +1 -3
@@ -272,9 +272,7 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
      APICCommonState *apic;
      APICCommonClass *apic_class = apic_get_class(errp);
 
-    if (!apic_class) {
-        return;
-    }
+    assert(apic_class);

Ok.

Quoted 20 lines
target/i386/cpu-sysemu.c +2 -3
      cpu->apic_state = DEVICE(object_new_with_class(OBJECT_CLASS(apic_class)));
      object_property_add_child(OBJECT(cpu), "lapic",
@@ -293,9 +291,8 @@ void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
      APICCommonState *apic;
      static bool apic_mmio_map_once;
 
-    if (cpu->apic_state == NULL) {
-        return;
-    }
+    assert(cpu->apic_state);
+
      qdev_realize(DEVICE(cpu->apic_state), NULL, errp);
 
      /* Map APIC MMIO area */
target/i386/cpu.c +0 -0
index b2a20365e1..a23d4795e0 100644
@@ -7448,9 +7448,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
      }

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7455

Quoted 10 lines
target/i386/cpu.c +5 -3
 
  #ifndef CONFIG_USER_ONLY
-    x86_cpu_apic_realize(cpu, &local_err);
-    if (local_err != NULL) {
-        goto out;
+    if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || ms->smp.cpus > 1) {
+        x86_cpu_apic_realize(cpu, &local_err);
+        if (local_err != NULL) {
+            goto out;
+        }

I'm not keen on the replication of the condition. Testing whether the apic object was created seems reasonable. It probably is clearer with the IF in the caller, as you do.

r~

#3 lore raw

Re: [PATCH 01/22] target/i386: Only realize existing APIC device

In reply to Philippe Mathieu-Daudé
Quoted 15 lines · On Mon, 18 Sep 2023 18:02:34 +0200 Philippe Mathieu-Daudé <[email protected]>…

APIC state is created under a certain condition,
use the same condition to realize it.
Having a NULL APIC state is a bug: use assert().

---
 target/i386/cpu-sysemu.c | 9 +++------
 target/i386/cpu.c        | 8 +++++---
 2 files changed, 8 insertions(+), 9 deletions(-)
target/i386/cpu-sysemu.c +0 -0
index 2375e48178..6a164d3769 100644

target/i386/cpu-sysemu.c void x86_cpu_apic_create(X86CPU *cpu, Error **errp) · line 275

Quoted 7 lines
target/i386/cpu-sysemu.c +1 -3
@@ -272,9 +272,7 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
     APICCommonState *apic;
     APICCommonClass *apic_class = apic_get_class(errp);
 
-    if (!apic_class) {
-        return;
-    }
+    assert(apic_class);

if errp doesn't lead to error_fatal/abort, wouldn't that effectively mask following error
apic_get_class():

      error_setg(errp, "KVM does not support userspace APIC");
      return NULL;

???

target/i386/cpu-sysemu.c void x86_cpu_apic_realize(X86CPU *cpu, Error **errp) · line 294

Quoted 11 lines
target/i386/cpu-sysemu.c +1 -3
     cpu->apic_state = DEVICE(object_new_with_class(OBJECT_CLASS(apic_class)));
     object_property_add_child(OBJECT(cpu), "lapic",
@@ -293,9 +291,8 @@ void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
     APICCommonState *apic;
     static bool apic_mmio_map_once;
 
-    if (cpu->apic_state == NULL) {
-        return;
-    }
+    assert(cpu->apic_state);

it would be better to explode in one place only inside apic_get_class(), provided !kvm_irqchip_in_kernel() case is dealt with properly.

Quoted 9 lines
target/i386/cpu-sysemu.c +0 -0
     qdev_realize(DEVICE(cpu->apic_state), NULL, errp);
 
     /* Map APIC MMIO area */
target/i386/cpu.c +0 -0
index b2a20365e1..a23d4795e0 100644
@@ -7448,9 +7448,11 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
     }

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7455

Quoted 10 lines
target/i386/cpu.c +5 -3
 
 #ifndef CONFIG_USER_ONLY
-    x86_cpu_apic_realize(cpu, &local_err);
-    if (local_err != NULL) {
-        goto out;
+    if (cpu->env.features[FEAT_1_EDX] & CPUID_APIC || ms->smp.cpus > 1) {
+        x86_cpu_apic_realize(cpu, &local_err);
+        if (local_err != NULL) {
+            goto out;
+        }

better move 'if (cpu->apic_state == NULL) {' from x86_cpu_apic_realize() to the caller, instead of repeating test again.

Quoted 3 lines
target/i386/cpu.c +0 -0
     }
 #endif /* !CONFIG_USER_ONLY */
     cpu_reset(cs);
#4 lore raw

PATCH 2/22 hw/intc/apic: Pass CPU using QOM link property

In reply to Philippe Mathieu-Daudé

QOM objects shouldn't access each other internals fields except using the QOM API.

Declare the 'cpu' and 'base-addr' properties, set them using object_property_set_link() and qdev_prop_set_uint32() respectively.

---
 hw/intc/apic_common.c    |  2 ++
 target/i386/cpu-sysemu.c | 11 ++++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)
hw/intc/apic_common.c +2 -0
index 68ad30e2f5..e28f7402ab 100644
@@ -394,6 +394,8 @@ static Property apic_properties_common[] = {
                     true),
     DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
                      false),
+    DEFINE_PROP_LINK("cpu", APICCommonState, cpu, TYPE_X86_CPU, X86CPU *),
+    DEFINE_PROP_UINT32("base-addr", APICCommonState, apicbase, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
target/i386/cpu-sysemu.c +6 -5
index 6a164d3769..6edfb7e2af 100644
@@ -269,7 +269,6 @@ APICCommonClass *apic_get_class(Error **errp)
 
 void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
 {
-    APICCommonState *apic;
     APICCommonClass *apic_class = apic_get_class(errp);
 
     assert(apic_class);
@@ -279,11 +278,13 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
                               OBJECT(cpu->apic_state));
     object_unref(OBJECT(cpu->apic_state));
 
+    if (!object_property_set_link(OBJECT(cpu->apic_state), "cpu",
+                                  OBJECT(cpu), errp)) {
+        return;
+    }
     qdev_prop_set_uint32(cpu->apic_state, "id", cpu->apic_id);
-    /* TODO: convert to link<> */
-    apic = APIC_COMMON(cpu->apic_state);
-    apic->cpu = cpu;
-    apic->apicbase = APIC_DEFAULT_ADDRESS | MSR_IA32_APICBASE_ENABLE;
+    qdev_prop_set_uint32(cpu->apic_state, "base-addr",
+                         APIC_DEFAULT_ADDRESS | MSR_IA32_APICBASE_ENABLE);
 }
 
 void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
Signature · 1 line
2.41.0
Reviewed-by on v1 2/22
#5 lore raw

Re: [PATCH 02/22] hw/intc/apic: Pass CPU using QOM link property

Quoted 7 lines · Philippe Mathieu-Daudé

QOM objects shouldn't access each other internals fields except using the QOM API.

Declare the 'cpu' and 'base-addr' properties, set them using object_property_set_link() and qdev_prop_set_uint32() respectively.

Quoted 5 lines
---
  hw/intc/apic_common.c    |  2 ++
  target/i386/cpu-sysemu.c | 11 ++++++-----
  2 files changed, 8 insertions(+), 5 deletions(-)

r~

Reviewed-by on v1 2/22
#6 lore raw

Re: [PATCH 02/22] hw/intc/apic: Pass CPU using QOM link property

In reply to Philippe Mathieu-Daudé
Quoted 8 lines · On Mon, 18 Sep 2023 18:02:35 +0200 Philippe Mathieu-Daudé <[email protected]>…

QOM objects shouldn't access each other internals fields except using the QOM API.

Declare the 'cpu' and 'base-addr' properties, set them using object_property_set_link() and qdev_prop_set_uint32() respectively.

Quoted 48 lines
---
 hw/intc/apic_common.c    |  2 ++
 target/i386/cpu-sysemu.c | 11 ++++++-----
 2 files changed, 8 insertions(+), 5 deletions(-)
hw/intc/apic_common.c +2 -0
index 68ad30e2f5..e28f7402ab 100644
@@ -394,6 +394,8 @@ static Property apic_properties_common[] = {
                     true),
     DEFINE_PROP_BOOL("legacy-instance-id", APICCommonState, legacy_instance_id,
                      false),
+    DEFINE_PROP_LINK("cpu", APICCommonState, cpu, TYPE_X86_CPU, X86CPU *),
+    DEFINE_PROP_UINT32("base-addr", APICCommonState, apicbase, 0),
     DEFINE_PROP_END_OF_LIST(),
 };
 
target/i386/cpu-sysemu.c +6 -5
index 6a164d3769..6edfb7e2af 100644
@@ -269,7 +269,6 @@ APICCommonClass *apic_get_class(Error **errp)
 
 void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
 {
-    APICCommonState *apic;
     APICCommonClass *apic_class = apic_get_class(errp);
 
     assert(apic_class);
@@ -279,11 +278,13 @@ void x86_cpu_apic_create(X86CPU *cpu, Error **errp)
                               OBJECT(cpu->apic_state));
     object_unref(OBJECT(cpu->apic_state));
 
+    if (!object_property_set_link(OBJECT(cpu->apic_state), "cpu",
+                                  OBJECT(cpu), errp)) {
+        return;
+    }
     qdev_prop_set_uint32(cpu->apic_state, "id", cpu->apic_id);
-    /* TODO: convert to link<> */
-    apic = APIC_COMMON(cpu->apic_state);
-    apic->cpu = cpu;
-    apic->apicbase = APIC_DEFAULT_ADDRESS | MSR_IA32_APICBASE_ENABLE;
+    qdev_prop_set_uint32(cpu->apic_state, "base-addr",
+                         APIC_DEFAULT_ADDRESS | MSR_IA32_APICBASE_ENABLE);
 }
 
 void x86_cpu_apic_realize(X86CPU *cpu, Error **errp)
#7 lore raw

PATCH 3/22 target/i386/kvm: Correct comment in kvm_cpu_realize()

In reply to Philippe Mathieu-Daudé
---
 target/i386/kvm/kvm-cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/i386/kvm/kvm-cpu.c +1 -0
index 7237378a7d..1fe62ce176 100644
@@ -37,6 +37,7 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
      *  -> cpu_exec_realizefn():
      *            -> accel_cpu_realizefn()
      *               kvm_cpu_realizefn() -> host_cpu_realizefn()
+     *  -> cpu_common_realizefn()
      *  -> check/update ucode_rev, phys_bits, mwait
      */
     if (cpu->max_features) {
Signature · 1 line
2.41.0
Reviewed-by on v1 3/22
#9 lore raw

Re: [PATCH 03/22] target/i386/kvm: Correct comment in kvm_cpu_realize()

In reply to Philippe Mathieu-Daudé
Quoted 1 line · On Mon, 18 Sep 2023 18:02:36 +0200 Philippe Mathieu-Daudé <[email protected]>…
Quoted 16 lines
---
 target/i386/kvm/kvm-cpu.c | 1 +
 1 file changed, 1 insertion(+)
target/i386/kvm/kvm-cpu.c +1 -0
index 7237378a7d..1fe62ce176 100644
@@ -37,6 +37,7 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
      *  -> cpu_exec_realizefn():
      *            -> accel_cpu_realizefn()
      *               kvm_cpu_realizefn() -> host_cpu_realizefn()
+     *  -> cpu_common_realizefn()
      *  -> check/update ucode_rev, phys_bits, mwait
      */
     if (cpu->max_features) {
#10 lore raw

RFC PATCH 4/22 exec/cpu: Never call cpu_reset() before cpu_realize()

In reply to Philippe Mathieu-Daudé

QDev instance is expected to be in an unknown state until full object realization. Thus we shouldn't call DeviceReset() on an unrealized instance. Move the cpu_reset() call from *before* the parent realize() handler (effectively cpu_common_realizefn) to *after* it.

---
RFC:
I haven't audited all the call sites, but plan to do it, amending the result to this patch description. This used to be a problem on some targets, but as of today's master this series pass make check-{qtest,avocado}.

---
 target/arm/cpu.c       | 2 +-
 target/avr/cpu.c       | 2 +-
 target/cris/cpu.c      | 2 +-
 target/hexagon/cpu.c   | 3 +--
 target/i386/cpu.c      | 2 +-
 target/loongarch/cpu.c | 2 +-
 target/m68k/cpu.c      | 2 +-
 target/mips/cpu.c      | 2 +-
 target/nios2/cpu.c     | 2 +-
 target/openrisc/cpu.c  | 2 +-
 target/riscv/cpu.c     | 2 +-
 target/rx/cpu.c        | 2 +-
 target/s390x/cpu.c     | 2 +-
 target/sh4/cpu.c       | 2 +-
 target/tricore/cpu.c   | 2 +-
 15 files changed, 15 insertions(+), 16 deletions(-)
target/arm/cpu.c +1 -1
index b9e09a702d..6aca036b85 100644
@@ -2278,9 +2278,9 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     acc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
target/avr/cpu.c +1 -1
index 8f741f258c..84d353f30e 100644
@@ -120,9 +120,9 @@ static void avr_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void avr_cpu_set_int(void *opaque, int irq, int level)
target/cris/cpu.c +1 -1
index a6a93c2359..079872a5cc 100644
@@ -152,10 +152,10 @@ static void cris_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     ccc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/hexagon/cpu.c +1 -2
index f155936289..7edc32701f 100644
@@ -346,9 +346,8 @@ static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
                              "hexagon-hvx.xml", 0);
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
-
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void hexagon_cpu_init(Object *obj)
target/i386/cpu.c +1 -1
index a23d4795e0..7faaa6915f 100644
@@ -7455,9 +7455,9 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
         }
     }
 #endif /* !CONFIG_USER_ONLY */
-    cpu_reset(cs);
 
     xcc->parent_realize(dev, &local_err);
+    cpu_reset(cs);
 
 out:
     if (local_err != NULL) {
target/loongarch/cpu.c +1 -1
index 65f9320e34..8029e70e76 100644
@@ -565,10 +565,10 @@ static void loongarch_cpu_realizefn(DeviceState *dev, Error **errp)
 
     loongarch_cpu_register_gdb_regs_for_features(cs);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     lacc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/m68k/cpu.c +1 -1
index 70d58471dc..2bc0a62f0e 100644
@@ -321,10 +321,10 @@ static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
 
     m68k_cpu_init_gdb(cpu);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void m68k_cpu_initfn(Object *obj)
target/mips/cpu.c +1 -1
index 63da1948fd..8d6f633f72 100644
@@ -492,10 +492,10 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     fpu_init(env, env->cpu_model);
     mvp_init(env);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void mips_cpu_initfn(Object *obj)
target/nios2/cpu.c +1 -1
index bc5cbf81c2..876a6dcad2 100644
@@ -217,12 +217,12 @@ static void nios2_cpu_realizefn(DeviceState *dev, Error **errp)
 
     realize_cr_status(cs);
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     /* We have reserved storage for cpuid; might as well use it. */
     cpu->env.ctrl[CR_CPUID] = cs->cpu_index;
 
     ncc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/openrisc/cpu.c +1 -1
index 61d748cfdc..cd25f1e9d5 100644
@@ -142,9 +142,9 @@ static void openrisc_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     occ->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void openrisc_cpu_initfn(Object *obj)
target/riscv/cpu.c +1 -1
index f227c7664e..7566757346 100644
@@ -1532,9 +1532,9 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
 #endif
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/rx/cpu.c +1 -1
index 157e57da0f..c9c8443cbd 100644
@@ -138,9 +138,9 @@ static void rx_cpu_realize(DeviceState *dev, Error **errp)
     }
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     rcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void rx_cpu_set_irq(void *opaque, int no, int request)
target/s390x/cpu.c +1 -1
index df167493c3..0f0b11fd73 100644
@@ -254,6 +254,7 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     s390_cpu_gdb_init(cs);
     qemu_init_vcpu(cs);
 
+    scc->parent_realize(dev, &err);
     /*
      * KVM requires the initial CPU reset ioctl to be executed on the target
      * CPU thread. CPU hotplug under single-threaded TCG will not work with
@@ -266,7 +267,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
         cpu_reset(cs);
     }
 
-    scc->parent_realize(dev, &err);
 out:
     error_propagate(errp, err);
 }
target/sh4/cpu.c +1 -1
index 61769ffdfa..656d71f74a 100644
@@ -228,10 +228,10 @@ static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     scc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void superh_cpu_initfn(Object *obj)
target/tricore/cpu.c +1 -1
index 133a9ac70e..a3610aecca 100644
@@ -118,10 +118,10 @@ static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
     if (tricore_has_feature(env, TRICORE_FEATURE_131)) {
         set_feature(env, TRICORE_FEATURE_13);
     }
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     tcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 
Signature · 1 line
2.41.0
Reviewed-by on v1 4/22
#11 lore raw

Re: [RFC PATCH 04/22] exec/cpu: Never call cpu_reset() before cpu_realize()

Quoted 22 lines · Philippe Mathieu-Daudé

QDev instance is expected to be in an unknown state until full object realization. Thus we shouldn't call DeviceReset() on an unrealized instance. Move the cpu_reset() call from*before* the parent realize() handler (effectively cpu_common_realizefn) to*after* it.

---
RFC:
I haven't audited all the call sites, but plan to do it, amending the result to this patch description. This used to be a problem on some targets, but as of today's master this series pass make check-{qtest,avocado}.

---
  target/arm/cpu.c       | 2 +-
  target/avr/cpu.c       | 2 +-
  target/cris/cpu.c      | 2 +-
  target/hexagon/cpu.c   | 3 +--
  target/i386/cpu.c      | 2 +-
  target/loongarch/cpu.c | 2 +-
  target/m68k/cpu.c      | 2 +-
  target/mips/cpu.c      | 2 +-
Quoted 8 lines
  target/nios2/cpu.c     | 2 +-
  target/openrisc/cpu.c  | 2 +-
  target/riscv/cpu.c     | 2 +-
  target/rx/cpu.c        | 2 +-
  target/s390x/cpu.c     | 2 +-
  target/sh4/cpu.c       | 2 +-
  target/tricore/cpu.c   | 2 +-
  15 files changed, 15 insertions(+), 16 deletions(-)

r~

#12 lore raw

Re: [RFC PATCH 04/22] exec/cpu: Never call cpu_reset() before cpu_realize()

In reply to Philippe Mathieu-Daudé
Quoted 5 lines · On Mon, 18 Sep 2023 18:02:37 +0200 Philippe Mathieu-Daudé <[email protected]>…

QDev instance is expected to be in an unknown state until full object realization. Thus we shouldn't call DeviceReset() on an unrealized instance. Move the cpu_reset() call from *before* the parent realize() handler (effectively cpu_common_realizefn) to *after* it.

looking at:

--cpu_reset() <- brings cpu to known (after reset|poweron) state
cpu_common_realizefn()

       if (dev->hotplugged) {                                                       
           cpu_synchronize_post_init(cpu);                                          
           cpu_resume(cpu);                                                         
       }

++cpu_reset() <-- looks to be too late, we already told hypervisor to run undefined CPU, didn't we?

Quoted 265 lines

---
RFC:
I haven't audited all the call sites, but plan to do it, amending the result to this patch description. This used to be a problem on some targets, but as of today's master this series pass make check-{qtest,avocado}.

---
 target/arm/cpu.c       | 2 +-
 target/avr/cpu.c       | 2 +-
 target/cris/cpu.c      | 2 +-
 target/hexagon/cpu.c   | 3 +--
 target/i386/cpu.c      | 2 +-
 target/loongarch/cpu.c | 2 +-
 target/m68k/cpu.c      | 2 +-
 target/mips/cpu.c      | 2 +-
 target/nios2/cpu.c     | 2 +-
 target/openrisc/cpu.c  | 2 +-
 target/riscv/cpu.c     | 2 +-
 target/rx/cpu.c        | 2 +-
 target/s390x/cpu.c     | 2 +-
 target/sh4/cpu.c       | 2 +-
 target/tricore/cpu.c   | 2 +-
 15 files changed, 15 insertions(+), 16 deletions(-)
target/arm/cpu.c +1 -1
index b9e09a702d..6aca036b85 100644
@@ -2278,9 +2278,9 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     acc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static ObjectClass *arm_cpu_class_by_name(const char *cpu_model)
target/avr/cpu.c +1 -1
index 8f741f258c..84d353f30e 100644
@@ -120,9 +120,9 @@ static void avr_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void avr_cpu_set_int(void *opaque, int irq, int level)
target/cris/cpu.c +1 -1
index a6a93c2359..079872a5cc 100644
@@ -152,10 +152,10 @@ static void cris_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     ccc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/hexagon/cpu.c +1 -2
index f155936289..7edc32701f 100644
@@ -346,9 +346,8 @@ static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
                              "hexagon-hvx.xml", 0);
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
-
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void hexagon_cpu_init(Object *obj)
target/i386/cpu.c +1 -1
index a23d4795e0..7faaa6915f 100644
@@ -7455,9 +7455,9 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
         }
     }
 #endif /* !CONFIG_USER_ONLY */
-    cpu_reset(cs);
 
     xcc->parent_realize(dev, &local_err);
+    cpu_reset(cs);
 
 out:
     if (local_err != NULL) {
target/loongarch/cpu.c +1 -1
index 65f9320e34..8029e70e76 100644
@@ -565,10 +565,10 @@ static void loongarch_cpu_realizefn(DeviceState *dev, Error **errp)
 
     loongarch_cpu_register_gdb_regs_for_features(cs);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     lacc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/m68k/cpu.c +1 -1
index 70d58471dc..2bc0a62f0e 100644
@@ -321,10 +321,10 @@ static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
 
     m68k_cpu_init_gdb(cpu);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void m68k_cpu_initfn(Object *obj)
target/mips/cpu.c +1 -1
index 63da1948fd..8d6f633f72 100644
@@ -492,10 +492,10 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     fpu_init(env, env->cpu_model);
     mvp_init(env);
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void mips_cpu_initfn(Object *obj)
target/nios2/cpu.c +1 -1
index bc5cbf81c2..876a6dcad2 100644
@@ -217,12 +217,12 @@ static void nios2_cpu_realizefn(DeviceState *dev, Error **errp)
 
     realize_cr_status(cs);
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     /* We have reserved storage for cpuid; might as well use it. */
     cpu->env.ctrl[CR_CPUID] = cs->cpu_index;
 
     ncc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/openrisc/cpu.c +1 -1
index 61d748cfdc..cd25f1e9d5 100644
@@ -142,9 +142,9 @@ static void openrisc_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     occ->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void openrisc_cpu_initfn(Object *obj)
target/riscv/cpu.c +1 -1
index f227c7664e..7566757346 100644
@@ -1532,9 +1532,9 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
 #endif
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     mcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 #ifndef CONFIG_USER_ONLY
target/rx/cpu.c +1 -1
index 157e57da0f..c9c8443cbd 100644
@@ -138,9 +138,9 @@ static void rx_cpu_realize(DeviceState *dev, Error **errp)
     }
 
     qemu_init_vcpu(cs);
-    cpu_reset(cs);
 
     rcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void rx_cpu_set_irq(void *opaque, int no, int request)
target/s390x/cpu.c +1 -1
index df167493c3..0f0b11fd73 100644
@@ -254,6 +254,7 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     s390_cpu_gdb_init(cs);
     qemu_init_vcpu(cs);
 
+    scc->parent_realize(dev, &err);
     /*
      * KVM requires the initial CPU reset ioctl to be executed on the target
      * CPU thread. CPU hotplug under single-threaded TCG will not work with
@@ -266,7 +267,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
         cpu_reset(cs);
     }
 
-    scc->parent_realize(dev, &err);
 out:
     error_propagate(errp, err);
 }
target/sh4/cpu.c +1 -1
index 61769ffdfa..656d71f74a 100644
@@ -228,10 +228,10 @@ static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     scc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 static void superh_cpu_initfn(Object *obj)
target/tricore/cpu.c +1 -1
index 133a9ac70e..a3610aecca 100644
@@ -118,10 +118,10 @@ static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
     if (tricore_has_feature(env, TRICORE_FEATURE_131)) {
         set_feature(env, TRICORE_FEATURE_13);
     }
-    cpu_reset(cs);
     qemu_init_vcpu(cs);
 
     tcc->parent_realize(dev, errp);
+    cpu_reset(cs);
 }
 
 
#13 lore raw

PATCH 5/22 exec/cpu: Call qemu_init_vcpu() once in cpu_common_realize()

In reply to Philippe Mathieu-Daudé

qemu_init_vcpu() is called in each ${target}_cpu_realize() before the call to parent_realize(), which is cpu_common_realizefn(). Call it once there.

---
 hw/core/cpu-common.c    | 3 +++
 target/alpha/cpu.c      | 2 --
 target/arm/cpu.c        | 2 --
 target/avr/cpu.c        | 1 -
 target/cris/cpu.c       | 2 --
 target/hexagon/cpu.c    | 1 -
 target/hppa/cpu.c       | 1 -
 target/i386/cpu.c       | 4 +---
 target/loongarch/cpu.c  | 2 --
 target/m68k/cpu.c       | 2 --
 target/microblaze/cpu.c | 2 --
 target/mips/cpu.c       | 2 --
 target/nios2/cpu.c      | 1 -
 target/openrisc/cpu.c   | 2 --
 target/ppc/cpu_init.c   | 1 -
 target/riscv/cpu.c      | 2 --
 target/rx/cpu.c         | 2 --
 target/s390x/cpu.c      | 1 -
 target/sh4/cpu.c        | 2 --
 target/sparc/cpu.c      | 2 --
 target/tricore/cpu.c    | 1 -
 target/xtensa/cpu.c     | 2 --
 22 files changed, 4 insertions(+), 36 deletions(-)
hw/core/cpu-common.c +3 -0
index ced66c2b34..a3b8de7054 100644
@@ -204,6 +204,9 @@ static void cpu_common_realizefn(DeviceState *dev, Error **errp)
         }
     }
 
+    /* Create CPU address space and vCPU thread */
+    qemu_init_vcpu(cpu);
+
     if (dev->hotplugged) {
         cpu_synchronize_post_init(cpu);
         cpu_resume(cpu);
target/alpha/cpu.c +0 -2
index 270ae787b1..eb78318bb8 100644
@@ -82,8 +82,6 @@ static void alpha_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     acc->parent_realize(dev, errp);
 }
 
target/arm/cpu.c +0 -2
index 6aca036b85..fc3772025c 100644
@@ -2277,8 +2277,6 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         }
     }
 
-    qemu_init_vcpu(cs);
-
     acc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/avr/cpu.c +0 -1
index 84d353f30e..d3460b3960 100644
@@ -119,7 +119,6 @@ static void avr_cpu_realizefn(DeviceState *dev, Error **errp)
         error_propagate(errp, local_err);
         return;
     }
-    qemu_init_vcpu(cs);
 
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
target/cris/cpu.c +0 -2
index 079872a5cc..671693a362 100644
@@ -152,8 +152,6 @@ static void cris_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     ccc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/hexagon/cpu.c +0 -1
index 7edc32701f..5b9bb3fe83 100644
@@ -345,7 +345,6 @@ static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
                              NUM_VREGS + NUM_QREGS,
                              "hexagon-hvx.xml", 0);
 
-    qemu_init_vcpu(cs);
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/hppa/cpu.c +0 -1
index 11022f9c99..49082bd2ba 100644
@@ -131,7 +131,6 @@ static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
     acc->parent_realize(dev, errp);
 
 #ifndef CONFIG_USER_ONLY
target/i386/cpu.c +1 -3
index 7faaa6915f..cb41d30aab 100644
@@ -7425,15 +7425,13 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 
     mce_init(cpu);
 
-    qemu_init_vcpu(cs);
-
     /*
      * Most Intel and certain AMD CPUs support hyperthreading. Even though QEMU
      * fixes this issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX
      * based on inputs (sockets,cores,threads), it is still better to give
      * users a warning.
      *
-     * NOTE: the following code has to follow qemu_init_vcpu(). Otherwise
+     * NOTE: the following code has to follow cpu_common_realize(). Otherwise
      * cs->nr_threads hasn't be populated yet and the checking is incorrect.
      */
     if (IS_AMD_CPU(env) &&
target/loongarch/cpu.c +0 -2
index 8029e70e76..dc0ac39833 100644
@@ -565,8 +565,6 @@ static void loongarch_cpu_realizefn(DeviceState *dev, Error **errp)
 
     loongarch_cpu_register_gdb_regs_for_features(cs);
 
-    qemu_init_vcpu(cs);
-
     lacc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/m68k/cpu.c +0 -2
index 2bc0a62f0e..3da316bc30 100644
@@ -321,8 +321,6 @@ static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
 
     m68k_cpu_init_gdb(cpu);
 
-    qemu_init_vcpu(cs);
-
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/microblaze/cpu.c +0 -2
index 03c2c4db1f..1f19a6e07d 100644
@@ -221,8 +221,6 @@ static void mb_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     version = cpu->cfg.version ? cpu->cfg.version : DEFAULT_CPU_VERSION;
     for (i = 0; mb_cpu_lookup[i].name && version; i++) {
         if (strcmp(mb_cpu_lookup[i].name, version) == 0) {
target/mips/cpu.c +0 -2
index 8d6f633f72..0aea69aaf9 100644
@@ -492,8 +492,6 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     fpu_init(env, env->cpu_model);
     mvp_init(env);
 
-    qemu_init_vcpu(cs);
-
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/nios2/cpu.c +0 -1
index 876a6dcad2..7a92fc5f2c 100644
@@ -216,7 +216,6 @@ static void nios2_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
     realize_cr_status(cs);
-    qemu_init_vcpu(cs);
 
     /* We have reserved storage for cpuid; might as well use it. */
     cpu->env.ctrl[CR_CPUID] = cs->cpu_index;
target/openrisc/cpu.c +0 -2
index cd25f1e9d5..e4ec95ca7f 100644
@@ -141,8 +141,6 @@ static void openrisc_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     occ->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/ppc/cpu_init.c +0 -1
index 7ab5ee92d9..e2c06c1f32 100644
@@ -6833,7 +6833,6 @@ static void ppc_cpu_realize(DeviceState *dev, Error **errp)
     init_ppc_proc(cpu);
 
     ppc_gdb_init(cs, pcc);
-    qemu_init_vcpu(cs);
 
     pcc->parent_realize(dev, errp);
 
target/riscv/cpu.c +0 -2
index 7566757346..4f7ae55359 100644
@@ -1531,8 +1531,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
     }
 #endif
 
-    qemu_init_vcpu(cs);
-
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/rx/cpu.c +0 -2
index c9c8443cbd..089df61790 100644
@@ -137,8 +137,6 @@ static void rx_cpu_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     rcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/s390x/cpu.c +0 -1
index 0f0b11fd73..416ac6c4e0 100644
@@ -252,7 +252,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev));
 #endif
     s390_cpu_gdb_init(cs);
-    qemu_init_vcpu(cs);
 
     scc->parent_realize(dev, &err);
     /*
target/sh4/cpu.c +0 -2
index 656d71f74a..e6690daf9a 100644
@@ -228,8 +228,6 @@ static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     scc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/sparc/cpu.c +0 -2
index 130ab8f578..2fdc95eda9 100644
@@ -782,8 +782,6 @@ static void sparc_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     scc->parent_realize(dev, errp);
 }
 
target/tricore/cpu.c +0 -1
index a3610aecca..0142cf556d 100644
@@ -118,7 +118,6 @@ static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
     if (tricore_has_feature(env, TRICORE_FEATURE_131)) {
         set_feature(env, TRICORE_FEATURE_13);
     }
-    qemu_init_vcpu(cs);
 
     tcc->parent_realize(dev, errp);
     cpu_reset(cs);
target/xtensa/cpu.c +0 -2
index acaf8c905f..300d19d45c 100644
@@ -174,8 +174,6 @@ static void xtensa_cpu_realizefn(DeviceState *dev, Error **errp)
 
     cs->gdb_num_regs = xcc->config->gdb_regmap.num_regs;
 
-    qemu_init_vcpu(cs);
-
     xcc->parent_realize(dev, errp);
 }
 
Signature · 1 line
2.41.0
Reviewed-by on v1 5/22
#14 lore raw

Re: [PATCH 05/22] exec/cpu: Call qemu_init_vcpu() once in cpu_common_realize()

Quoted 21 lines · Philippe Mathieu-Daudé

qemu_init_vcpu() is called in each ${target}_cpu_realize() before the call to parent_realize(), which is cpu_common_realizefn(). Call it once there.

---
  hw/core/cpu-common.c    | 3 +++
  target/alpha/cpu.c      | 2 --
  target/arm/cpu.c        | 2 --
  target/avr/cpu.c        | 1 -
  target/cris/cpu.c       | 2 --
  target/hexagon/cpu.c    | 1 -
  target/hppa/cpu.c       | 1 -
  target/i386/cpu.c       | 4 +---
  target/loongarch/cpu.c  | 2 --
  target/m68k/cpu.c       | 2 --
  target/microblaze/cpu.c | 2 --
  target/mips/cpu.c       | 2 --
  target/nios2/cpu.c      | 1 -
  target/openrisc/cpu.c   | 2 --
  target/ppc/cpu_init.c   | 1 -
Quoted 8 lines
  target/riscv/cpu.c      | 2 --
  target/rx/cpu.c         | 2 --
  target/s390x/cpu.c      | 1 -
  target/sh4/cpu.c        | 2 --
  target/sparc/cpu.c      | 2 --
  target/tricore/cpu.c    | 1 -
  target/xtensa/cpu.c     | 2 --
  22 files changed, 4 insertions(+), 36 deletions(-)

r~

#15 lore raw

Re: [PATCH 05/22] exec/cpu: Call qemu_init_vcpu() once in cpu_common_realize()

In reply to Philippe Mathieu-Daudé
Quoted 127 lines · On Mon, 18 Sep 2023 18:02:38 +0200 Philippe Mathieu-Daudé <[email protected]>…

qemu_init_vcpu() is called in each ${target}_cpu_realize() before the call to parent_realize(), which is cpu_common_realizefn(). Call it once there.

---
 hw/core/cpu-common.c    | 3 +++
 target/alpha/cpu.c      | 2 --
 target/arm/cpu.c        | 2 --
 target/avr/cpu.c        | 1 -
 target/cris/cpu.c       | 2 --
 target/hexagon/cpu.c    | 1 -
 target/hppa/cpu.c       | 1 -
 target/i386/cpu.c       | 4 +---
 target/loongarch/cpu.c  | 2 --
 target/m68k/cpu.c       | 2 --
 target/microblaze/cpu.c | 2 --
 target/mips/cpu.c       | 2 --
 target/nios2/cpu.c      | 1 -
 target/openrisc/cpu.c   | 2 --
 target/ppc/cpu_init.c   | 1 -
 target/riscv/cpu.c      | 2 --
 target/rx/cpu.c         | 2 --
 target/s390x/cpu.c      | 1 -
 target/sh4/cpu.c        | 2 --
 target/sparc/cpu.c      | 2 --
 target/tricore/cpu.c    | 1 -
 target/xtensa/cpu.c     | 2 --
 22 files changed, 4 insertions(+), 36 deletions(-)
hw/core/cpu-common.c +3 -0
index ced66c2b34..a3b8de7054 100644
@@ -204,6 +204,9 @@ static void cpu_common_realizefn(DeviceState *dev, Error **errp)
         }
     }
 
+    /* Create CPU address space and vCPU thread */
+    qemu_init_vcpu(cpu);
+
     if (dev->hotplugged) {
         cpu_synchronize_post_init(cpu);
         cpu_resume(cpu);
target/alpha/cpu.c +0 -2
index 270ae787b1..eb78318bb8 100644
@@ -82,8 +82,6 @@ static void alpha_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     acc->parent_realize(dev, errp);
 }
 
target/arm/cpu.c +0 -2
index 6aca036b85..fc3772025c 100644
@@ -2277,8 +2277,6 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         }
     }
 
-    qemu_init_vcpu(cs);
-
     acc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/avr/cpu.c +0 -1
index 84d353f30e..d3460b3960 100644
@@ -119,7 +119,6 @@ static void avr_cpu_realizefn(DeviceState *dev, Error **errp)
         error_propagate(errp, local_err);
         return;
     }
-    qemu_init_vcpu(cs);
 
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
target/cris/cpu.c +0 -2
index 079872a5cc..671693a362 100644
@@ -152,8 +152,6 @@ static void cris_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     ccc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/hexagon/cpu.c +0 -1
index 7edc32701f..5b9bb3fe83 100644
@@ -345,7 +345,6 @@ static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
                              NUM_VREGS + NUM_QREGS,
                              "hexagon-hvx.xml", 0);
 
-    qemu_init_vcpu(cs);
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/hppa/cpu.c +0 -1
index 11022f9c99..49082bd2ba 100644
@@ -131,7 +131,6 @@ static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
     acc->parent_realize(dev, errp);
 
 #ifndef CONFIG_USER_ONLY
target/i386/cpu.c +0 -0
index 7faaa6915f..cb41d30aab 100644
@@ -7425,15 +7425,13 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 
     mce_init(cpu);
 

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7434

Quoted 10 lines
target/i386/cpu.c +1 -3
-    qemu_init_vcpu(cs);
-
     /*
      * Most Intel and certain AMD CPUs support hyperthreading. Even though QEMU
      * fixes this issue by adjusting CPUID_0000_0001_EBX and CPUID_8000_0008_ECX
      * based on inputs (sockets,cores,threads), it is still better to give
      * users a warning.
      *
-     * NOTE: the following code has to follow qemu_init_vcpu(). Otherwise
+     * NOTE: the following code has to follow cpu_common_realize(). Otherwise

Does moving qemu_init_vcpu() past check and altering comment here makes cs->nr_threads somehow correct for following if()?

target/i386/cpu.c static void x86_cpu_realizefn(DeviceState *dev, Error **errp) · line 7437

Quoted 3 lines
target/i386/cpu.c +0 -0
      * cs->nr_threads hasn't be populated yet and the checking is incorrect.
      */
     if (IS_AMD_CPU(env) &&

missing context:

       if (IS_AMD_CPU(env) &&
           !(env->features[FEAT_8000_0001_ECX] & CPUID_EXT3_TOPOEXT) &&
           cs->nr_threads > 1 && !ht_warned) {
Quoted 178 lines
target/loongarch/cpu.c +0 -2
index 8029e70e76..dc0ac39833 100644
@@ -565,8 +565,6 @@ static void loongarch_cpu_realizefn(DeviceState *dev, Error **errp)
 
     loongarch_cpu_register_gdb_regs_for_features(cs);
 
-    qemu_init_vcpu(cs);
-
     lacc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/m68k/cpu.c +0 -2
index 2bc0a62f0e..3da316bc30 100644
@@ -321,8 +321,6 @@ static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
 
     m68k_cpu_init_gdb(cpu);
 
-    qemu_init_vcpu(cs);
-
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/microblaze/cpu.c +0 -2
index 03c2c4db1f..1f19a6e07d 100644
@@ -221,8 +221,6 @@ static void mb_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     version = cpu->cfg.version ? cpu->cfg.version : DEFAULT_CPU_VERSION;
     for (i = 0; mb_cpu_lookup[i].name && version; i++) {
         if (strcmp(mb_cpu_lookup[i].name, version) == 0) {
target/mips/cpu.c +0 -2
index 8d6f633f72..0aea69aaf9 100644
@@ -492,8 +492,6 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     fpu_init(env, env->cpu_model);
     mvp_init(env);
 
-    qemu_init_vcpu(cs);
-
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/nios2/cpu.c +0 -1
index 876a6dcad2..7a92fc5f2c 100644
@@ -216,7 +216,6 @@ static void nios2_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 
     realize_cr_status(cs);
-    qemu_init_vcpu(cs);
 
     /* We have reserved storage for cpuid; might as well use it. */
     cpu->env.ctrl[CR_CPUID] = cs->cpu_index;
target/openrisc/cpu.c +0 -2
index cd25f1e9d5..e4ec95ca7f 100644
@@ -141,8 +141,6 @@ static void openrisc_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     occ->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/ppc/cpu_init.c +0 -1
index 7ab5ee92d9..e2c06c1f32 100644
@@ -6833,7 +6833,6 @@ static void ppc_cpu_realize(DeviceState *dev, Error **errp)
     init_ppc_proc(cpu);
 
     ppc_gdb_init(cs, pcc);
-    qemu_init_vcpu(cs);
 
     pcc->parent_realize(dev, errp);
 
target/riscv/cpu.c +0 -2
index 7566757346..4f7ae55359 100644
@@ -1531,8 +1531,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
     }
 #endif
 
-    qemu_init_vcpu(cs);
-
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/rx/cpu.c +0 -2
index c9c8443cbd..089df61790 100644
@@ -137,8 +137,6 @@ static void rx_cpu_realize(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     rcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/s390x/cpu.c +0 -1
index 0f0b11fd73..416ac6c4e0 100644
@@ -252,7 +252,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev));
 #endif
     s390_cpu_gdb_init(cs);
-    qemu_init_vcpu(cs);
 
     scc->parent_realize(dev, &err);
     /*
target/sh4/cpu.c +0 -2
index 656d71f74a..e6690daf9a 100644
@@ -228,8 +228,6 @@ static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     scc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
target/sparc/cpu.c +0 -2
index 130ab8f578..2fdc95eda9 100644
@@ -782,8 +782,6 @@ static void sparc_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    qemu_init_vcpu(cs);
-
     scc->parent_realize(dev, errp);
 }
 
target/tricore/cpu.c +0 -1
index a3610aecca..0142cf556d 100644
@@ -118,7 +118,6 @@ static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
     if (tricore_has_feature(env, TRICORE_FEATURE_131)) {
         set_feature(env, TRICORE_FEATURE_13);
     }
-    qemu_init_vcpu(cs);
 
     tcc->parent_realize(dev, errp);
     cpu_reset(cs);
target/xtensa/cpu.c +0 -2
index acaf8c905f..300d19d45c 100644
@@ -174,8 +174,6 @@ static void xtensa_cpu_realizefn(DeviceState *dev, Error **errp)
 
     cs->gdb_num_regs = xcc->config->gdb_regmap.num_regs;
 
-    qemu_init_vcpu(cs);
-
     xcc->parent_realize(dev, errp);
 }
 
#16 lore raw

PATCH 6/22 exec/cpu: Call cpu_remove_sync() once in cpu_common_unrealize()

In reply to Philippe Mathieu-Daudé

While create_vcpu_thread() creates a vCPU thread, its counterpart is cpu_remove_sync(), which join and destroy the thread.

create_vcpu_thread() is called in qemu_init_vcpu(), itself called in cpu_common_realizefn(). Since we don't have qemu_deinit_vcpu() helper (we probably don't need any), simply destroy the thread in cpu_common_unrealizefn().

Note: only the PPC and X86 targets were calling cpu_remove_sync(), meaning all other targets were leaking the thread when the vCPU was unrealized (mostly when vCPU are hot-unplugged).

---
 hw/core/cpu-common.c  | 3 +++
 target/i386/cpu.c     | 1 -
 target/ppc/cpu_init.c | 2 --
 3 files changed, 3 insertions(+), 3 deletions(-)
hw/core/cpu-common.c +3 -0
index a3b8de7054..e5841c59df 100644
@@ -221,6 +221,9 @@ static void cpu_common_unrealizefn(DeviceState *dev)
 
     /* NOTE: latest generic point before the cpu is fully unrealized */
     cpu_exec_unrealizefn(cpu);
+
+    /* Destroy vCPU thread */
+    cpu_remove_sync(cpu);
 }
 
 static void cpu_common_initfn(Object *obj)
target/i386/cpu.c +0 -1
index cb41d30aab..d79797d963 100644
@@ -7470,7 +7470,6 @@ static void x86_cpu_unrealizefn(DeviceState *dev)
     X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
 
 #ifndef CONFIG_USER_ONLY
-    cpu_remove_sync(CPU(dev));
     qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);
 #endif
 
target/ppc/cpu_init.c +0 -2
index e2c06c1f32..24d4e8fa7e 100644
@@ -6853,8 +6853,6 @@ static void ppc_cpu_unrealize(DeviceState *dev)
 
     pcc->parent_unrealize(dev);
 
-    cpu_remove_sync(CPU(cpu));
-
     destroy_ppc_opcodes(cpu);
 }
 
Signature · 1 line
2.41.0
Reviewed-by on v1 6/22
#17 lore raw

Re: [PATCH 06/22] exec/cpu: Call cpu_remove_sync() once in cpu_common_unrealize()

Quoted 12 lines · Philippe Mathieu-Daudé

While create_vcpu_thread() creates a vCPU thread, its counterpart is cpu_remove_sync(), which join and destroy the thread.

create_vcpu_thread() is called in qemu_init_vcpu(), itself called in cpu_common_realizefn(). Since we don't have qemu_deinit_vcpu() helper (we probably don't need any), simply destroy the thread in cpu_common_unrealizefn().

Note: only the PPC and X86 targets were calling cpu_remove_sync(), meaning all other targets were leaking the thread when the vCPU was unrealized (mostly when vCPU are hot-unplugged).

Quoted 6 lines
---
  hw/core/cpu-common.c  | 3 +++
  target/i386/cpu.c     | 1 -
  target/ppc/cpu_init.c | 2 --
  3 files changed, 3 insertions(+), 3 deletions(-)

r~

#18 lore raw

Re: [PATCH 06/22] exec/cpu: Call cpu_remove_sync() once in cpu_common_unrealize()

In reply to Philippe Mathieu-Daudé
Quoted 38 lines · On Mon, 18 Sep 2023 18:02:39 +0200 Philippe Mathieu-Daudé <[email protected]>…

While create_vcpu_thread() creates a vCPU thread, its counterpart is cpu_remove_sync(), which join and destroy the thread.

create_vcpu_thread() is called in qemu_init_vcpu(), itself called in cpu_common_realizefn(). Since we don't have qemu_deinit_vcpu() helper (we probably don't need any), simply destroy the thread in cpu_common_unrealizefn().

Note: only the PPC and X86 targets were calling cpu_remove_sync(), meaning all other targets were leaking the thread when the vCPU was unrealized (mostly when vCPU are hot-unplugged).

---
 hw/core/cpu-common.c  | 3 +++
 target/i386/cpu.c     | 1 -
 target/ppc/cpu_init.c | 2 --
 3 files changed, 3 insertions(+), 3 deletions(-)
hw/core/cpu-common.c +3 -0
index a3b8de7054..e5841c59df 100644
@@ -221,6 +221,9 @@ static void cpu_common_unrealizefn(DeviceState *dev)
 
     /* NOTE: latest generic point before the cpu is fully unrealized */
     cpu_exec_unrealizefn(cpu);
+
+    /* Destroy vCPU thread */
+    cpu_remove_sync(cpu);
 }
 
 static void cpu_common_initfn(Object *obj)
target/i386/cpu.c +0 -0
index cb41d30aab..d79797d963 100644

target/i386/cpu.c static void x86_cpu_unrealizefn(DeviceState *dev) · line 7474

Quoted 6 lines
target/i386/cpu.c +0 -1
@@ -7470,7 +7470,6 @@ static void x86_cpu_unrealizefn(DeviceState *dev)
     X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
 
 #ifndef CONFIG_USER_ONLY
-    cpu_remove_sync(CPU(dev));
     qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);
 #endif

missing followup context:

    ...
    xcc->parent_unrealize(dev); 

Before the patch, vcpu thread is stopped and onnly then clean up happens.

After the patch we have cleanup while vcpu thread is still running.

Even if it doesn't explode, such ordering still seems to be wrong.

target/ppc/cpu_init.c static void ppc_cpu_unrealize(DeviceState *dev) · line 6856

Quoted 9 lines
target/ppc/cpu_init.c +0 -1
index e2c06c1f32..24d4e8fa7e 100644
@@ -6853,8 +6853,6 @@ static void ppc_cpu_unrealize(DeviceState *dev)
 
     pcc->parent_unrealize(dev);
 
-    cpu_remove_sync(CPU(cpu));

bug in current code?

Quoted 4 lines
target/ppc/cpu_init.c +0 -1
-
     destroy_ppc_opcodes(cpu);
 }
 
#19 lore raw

Re: [PATCH 06/22] exec/cpu: Call cpu_remove_sync() once in cpu_common_unrealize()

Quoted 52 lines · Igor Mammedov
Quoted 38 lines · On Mon, 18 Sep 2023 18:02:39 +0200 Philippe Mathieu-Daudé <[email protected]>…

While create_vcpu_thread() creates a vCPU thread, its counterpart is cpu_remove_sync(), which join and destroy the thread.

create_vcpu_thread() is called in qemu_init_vcpu(), itself called in cpu_common_realizefn(). Since we don't have qemu_deinit_vcpu() helper (we probably don't need any), simply destroy the thread in cpu_common_unrealizefn().

Note: only the PPC and X86 targets were calling cpu_remove_sync(), meaning all other targets were leaking the thread when the vCPU was unrealized (mostly when vCPU are hot-unplugged).

---
  hw/core/cpu-common.c  | 3 +++
  target/i386/cpu.c     | 1 -
  target/ppc/cpu_init.c | 2 --
  3 files changed, 3 insertions(+), 3 deletions(-)
hw/core/cpu-common.c +3 -0
index a3b8de7054..e5841c59df 100644
@@ -221,6 +221,9 @@ static void cpu_common_unrealizefn(DeviceState *dev)
 
      /* NOTE: latest generic point before the cpu is fully unrealized */
      cpu_exec_unrealizefn(cpu);
+
+    /* Destroy vCPU thread */
+    cpu_remove_sync(cpu);
  }
 
  static void cpu_common_initfn(Object *obj)
target/i386/cpu.c +0 -0
index cb41d30aab..d79797d963 100644

target/i386/cpu.c static void x86_cpu_unrealizefn(DeviceState *dev) · line 7474

Quoted 6 lines
target/i386/cpu.c +0 -1
@@ -7470,7 +7470,6 @@ static void x86_cpu_unrealizefn(DeviceState *dev)
      X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
 
  #ifndef CONFIG_USER_ONLY
-    cpu_remove_sync(CPU(dev));
      qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);
  #endif

missing followup context:

     ...
     xcc->parent_unrealize(dev);
Quoted 6 lines

Before the patch, vcpu thread is stopped and onnly then clean up happens.

After the patch we have cleanup while vcpu thread is still running.

Even if it doesn't explode, such ordering still seems to be wrong.

OK.

Quoted 11 lines

target/ppc/cpu_init.c static void ppc_cpu_unrealize(DeviceState *dev) · line 6856

Quoted 9 lines
target/ppc/cpu_init.c +0 -1
index e2c06c1f32..24d4e8fa7e 100644
@@ -6853,8 +6853,6 @@ static void ppc_cpu_unrealize(DeviceState *dev)
 
      pcc->parent_unrealize(dev);
 
-    cpu_remove_sync(CPU(cpu));

bug in current code?

Plausibly. See:

commit f1023d21e81b7bf523ddf2ac91a48117f20ef9d7

Author: Greg Kurz <[email protected]>
Date:   Thu Oct 15 23:18:32 2020 +0200

spapr: Unrealize vCPUs with qdev_unrealize()

Since we introduced CPU hot-unplug in sPAPR, we don't unrealize the vCPU objects explicitly. Instead, we let QOM handle that for us under object_property_del_all() when the CPU core object is finalized. The only thing we do is calling cpu_remove_sync() to tear the vCPU thread down.

This happens to work but it is ugly because:
- we call qdev_realize() but the corresponding qdev_unrealize() is buried deep in the QOM code
- we call cpu_remove_sync() to undo qemu_init_vcpu() called by ppc_cpu_realize() in target/ppc/translate_init.c.inc
- the CPU init and teardown paths aren't really symmetrical

The latter didn't bite us so far but a future patch that greatly simplifies the CPU core realize path needs it to avoid a crash in QOM.

For all these reasons, have ppc_cpu_unrealize() to undo the changes of ppc_cpu_realize() by calling cpu_remove_sync() at the right place, and have the sPAPR CPU core code to call qdev_unrealize().

This requires to add a missing stub because translate_init.c.inc is also compiled for user mode.

Quoted 5 lines
Quoted 5 lines
target/ppc/cpu_init.c +0 -1
-
      destroy_ppc_opcodes(cpu);
  }
 
#20 lore raw

Re: [PATCH 06/22] exec/cpu: Call cpu_remove_sync() once in cpu_common_unrealize()

Quoted 94 lines · Philippe Mathieu-Daudé
Quoted 49 lines · Igor Mammedov
Quoted 36 lines · On Mon, 18 Sep 2023 18:02:39 +0200 Philippe Mathieu-Daudé <[email protected]>…

While create_vcpu_thread() creates a vCPU thread, its counterpart is cpu_remove_sync(), which join and destroy the thread.

create_vcpu_thread() is called in qemu_init_vcpu(), itself called in cpu_common_realizefn(). Since we don't have qemu_deinit_vcpu() helper (we probably don't need any), simply destroy the thread in cpu_common_unrealizefn().

Note: only the PPC and X86 targets were calling cpu_remove_sync(), meaning all other targets were leaking the thread when the vCPU was unrealized (mostly when vCPU are hot-unplugged).

---
  hw/core/cpu-common.c  | 3 +++
  target/i386/cpu.c     | 1 -
  target/ppc/cpu_init.c | 2 --

  3 files changed, 3 insertions(+), 3 deletions(-)
hw/core/cpu-common.c +3 -0
index a3b8de7054..e5841c59df 100644
@@ -221,6 +221,9 @@ static void cpu_common_unrealizefn(DeviceState *dev)
       /* NOTE: latest generic point before the cpu is fully unrealized */
       cpu_exec_unrealizefn(cpu);
+
+    /* Destroy vCPU thread */
+    cpu_remove_sync(cpu);
   }
   static void cpu_common_initfn(Object *obj)
target/i386/cpu.c +0 -0
index cb41d30aab..d79797d963 100644

target/i386/cpu.c static void x86_cpu_unrealizefn(DeviceState *dev) · line 7473

Quoted 5 lines
target/i386/cpu.c +0 -1
@@ -7470,7 +7470,6 @@ static void x86_cpu_unrealizefn(DeviceState *dev)
       X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
   #ifndef CONFIG_USER_ONLY
-    cpu_remove_sync(CPU(dev));
       qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);
   #endif

missing  followup context:
     ...
     xcc->parent_unrealize(dev);

Quoted 6 lines

Before the patch, vcpu thread is stopped and onnly then clean up happens.

After the patch we have cleanup while vcpu thread is still running.

Even if it doesn't explode, such ordering still seems to be wrong.

OK.

Quoted 9 lines

target/ppc/cpu_init.c static void ppc_cpu_unrealize(DeviceState *dev) · line 6854

Quoted 7 lines
target/ppc/cpu_init.c +0 -1
index e2c06c1f32..24d4e8fa7e 100644
@@ -6853,8 +6853,6 @@ static void ppc_cpu_unrealize(DeviceState *dev)
       pcc->parent_unrealize(dev);
-    cpu_remove_sync(CPU(cpu));

bug in current code?

Plausibly. See:

commit f1023d21e81b7bf523ddf2ac91a48117f20ef9d7
Author: Greg Kurz <[email protected]>
Date:   Thu Oct 15 23:18:32 2020 +0200

    spapr: Unrealize vCPUs with qdev_unrealize()

    Since we introduced CPU hot-unplug in sPAPR, we don't unrealize the vCPU objects explicitly. Instead, we let QOM handle that for us under object_property_del_all() when the CPU core object is finalized. The only thing we do is calling cpu_remove_sync() to tear the vCPU thread down.

    This happens to work but it is ugly because:
    - we call qdev_realize() but the corresponding qdev_unrealize() is buried deep in the QOM code
    - we call cpu_remove_sync() to undo qemu_init_vcpu() called by ppc_cpu_realize() in target/ppc/translate_init.c.inc
    - the CPU init and teardown paths aren't really symmetrical

    The latter didn't bite us so far but a future patch that greatly simplifies the CPU core realize path needs it to avoid a crash in QOM.

Quoted 6 lines

    For all these reasons, have ppc_cpu_unrealize() to undo the changes of ppc_cpu_realize() by calling cpu_remove_sync() at the right place, and have the sPAPR CPU core code to call qdev_unrealize().

    This requires to add a missing stub because translate_init.c.inc is also compiled for user mode.

See also:

commit 5e22e29201d80124bca0124f2034e72b698cbb6f

Author: David Gibson <[email protected]>
Date:   Wed Jun 13 12:08:42 2018 +1000

pnv: Add cpu unrealize path

Currently we don't have any unrealize path for pnv cpu cores. We get away with this because we don't yet support cpu hotplug for pnv.

However, we're going to want it eventually, and in the meantime, it makes it non-obvious why there are a bunch of allocations on the realize() path that don't have matching frees.

So, implement the missing unrealize path.

hw/ppc/pnv_core.c +7 -0
index f4c41d89d6d..f7cf33f547a 100644
@@ -186,6 +186,26 @@ err:
     error_propagate(errp, local_err);
 }
 
+static void pnv_unrealize_vcpu(PowerPCCPU *cpu)
+{
+    qemu_unregister_reset(pnv_cpu_reset, cpu);
+    object_unparent(cpu->intc);
+    cpu_remove_sync(CPU(cpu));
+    object_unparent(OBJECT(cpu));
+}
 
#21 lore raw

Re: [PATCH 06/22] exec/cpu: Call cpu_remove_sync() once in cpu_common_unrealize()

In reply to Philippe Mathieu-Daudé
Quoted 53 lines · On Thu, 16 Jan 2025 19:05:46 +0100 Philippe Mathieu-Daudé <[email protected]>…
Quoted 52 lines · Igor Mammedov
Quoted 38 lines · On Mon, 18 Sep 2023 18:02:39 +0200 Philippe Mathieu-Daudé <[email protected]>…

While create_vcpu_thread() creates a vCPU thread, its counterpart is cpu_remove_sync(), which join and destroy the thread.

create_vcpu_thread() is called in qemu_init_vcpu(), itself called in cpu_common_realizefn(). Since we don't have qemu_deinit_vcpu() helper (we probably don't need any), simply destroy the thread in cpu_common_unrealizefn().

Note: only the PPC and X86 targets were calling cpu_remove_sync(), meaning all other targets were leaking the thread when the vCPU was unrealized (mostly when vCPU are hot-unplugged).

---
  hw/core/cpu-common.c  | 3 +++
  target/i386/cpu.c     | 1 -
  target/ppc/cpu_init.c | 2 --
  3 files changed, 3 insertions(+), 3 deletions(-)
hw/core/cpu-common.c +3 -0
index a3b8de7054..e5841c59df 100644
@@ -221,6 +221,9 @@ static void cpu_common_unrealizefn(DeviceState *dev)
 
      /* NOTE: latest generic point before the cpu is fully unrealized */
      cpu_exec_unrealizefn(cpu);
+
+    /* Destroy vCPU thread */
+    cpu_remove_sync(cpu);
  }
 
  static void cpu_common_initfn(Object *obj)
target/i386/cpu.c +0 -0
index cb41d30aab..d79797d963 100644

target/i386/cpu.c static void x86_cpu_unrealizefn(DeviceState *dev) · line 7474

Quoted 6 lines
target/i386/cpu.c +0 -1
@@ -7470,7 +7470,6 @@ static void x86_cpu_unrealizefn(DeviceState *dev)
      X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
 
  #ifndef CONFIG_USER_ONLY
-    cpu_remove_sync(CPU(dev));
      qemu_unregister_reset(x86_cpu_machine_reset_cb, dev);
  #endif  

missing followup context:

     ...
     xcc->parent_unrealize(dev);
Quoted 8 lines
Quoted 6 lines

Before the patch, vcpu thread is stopped and onnly then clean up happens.

After the patch we have cleanup while vcpu thread is still running.

Even if it doesn't explode, such ordering still seems to be wrong.

OK.

looking at all users, some do stop vcpu thread before tearing down vcpu object and interrupt controller, while some do it other way around or mix of both.

It's probably safe to stop vcpu thread wrt intc cleanup.
Can you check what would happen if there were a pending interrupt, but then flowing would happen:
1. destroying intc
2. can vcpu thread just kicked out from KVM_RUN,
trip over missing/invalid intc state while thread runs towards its exit point?

If above can't crash then,
I'd prefer to stop vcpu at least before vcpu cleanup is run.
i.e. put cpu_remove_sync() as the very 1st call inside of cpu_common_unrealizefn()

Quoted 50 lines
Quoted 11 lines

target/ppc/cpu_init.c static void ppc_cpu_unrealize(DeviceState *dev) · line 6856

Quoted 9 lines
target/ppc/cpu_init.c +0 -1
index e2c06c1f32..24d4e8fa7e 100644
@@ -6853,8 +6853,6 @@ static void ppc_cpu_unrealize(DeviceState *dev)
 
      pcc->parent_unrealize(dev);
 
-    cpu_remove_sync(CPU(cpu));  

bug in current code?

Plausibly. See:

commit f1023d21e81b7bf523ddf2ac91a48117f20ef9d7

Author: Greg Kurz <[email protected]>
Date:   Thu Oct 15 23:18:32 2020 +0200

spapr: Unrealize vCPUs with qdev_unrealize()

Since we introduced CPU hot-unplug in sPAPR, we don't unrealize the vCPU objects explicitly. Instead, we let QOM handle that for us under object_property_del_all() when the CPU core object is finalized. The only thing we do is calling cpu_remove_sync() to tear the vCPU thread down.

This happens to work but it is ugly because:
- we call qdev_realize() but the corresponding qdev_unrealize() is buried deep in the QOM code
- we call cpu_remove_sync() to undo qemu_init_vcpu() called by ppc_cpu_realize() in target/ppc/translate_init.c.inc
- the CPU init and teardown paths aren't really symmetrical

The latter didn't bite us so far but a future patch that greatly simplifies the CPU core realize path needs it to avoid a crash in QOM.

For all these reasons, have ppc_cpu_unrealize() to undo the changes of ppc_cpu_realize() by calling cpu_remove_sync() at the right place, and have the sPAPR CPU core code to call qdev_unrealize().

This requires to add a missing stub because translate_init.c.inc is also compiled for user mode.

Quoted 5 lines
Quoted 5 lines
target/ppc/cpu_init.c +0 -1
-
      destroy_ppc_opcodes(cpu);
  }
 
#22 lore raw

PATCH 7/22 exec/cpu: Introduce the CPU address space destruction function

In reply to Philippe Mathieu-Daudé

From: xianglai li <[email protected]>

Introduce new function to destroy CPU address space resources for cpu hot-(un)plug.

Cc "Salil Mehta" <[email protected]>
Cc Xiaojuan Yang <[email protected]>
Cc Song Gao <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Ani Sinha <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
Cc Yanan Wang <[email protected]>
Cc "Daniel P. Berrangé" <[email protected]>
Cc Peter Xu <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Bibo Mao <[email protected]>

Message-ID: <3a4fc2a3df4b767c3c296a7da3bc15ca9c251316.1694433326.git.lixianglai@loongson.cn>

---
 include/exec/cpu-common.h |  8 ++++++++
 include/hw/core/cpu.h     |  1 +
 softmmu/physmem.c         | 24 ++++++++++++++++++++++++
 3 files changed, 33 insertions(+)
include/exec/cpu-common.h +8 -0
index 41788c0bdd..eb56a228a2 100644
@@ -120,6 +120,14 @@ size_t qemu_ram_pagesize_largest(void);
  */
 void cpu_address_space_init(CPUState *cpu, int asidx,
                             const char *prefix, MemoryRegion *mr);
+/**
+ * cpu_address_space_destroy:
+ * @cpu: CPU for which address space needs to be destroyed
+ * @asidx: integer index of this address space
+ *
+ * Note that with KVM only one address space is supported.
+ */
+void cpu_address_space_destroy(CPUState *cpu, int asidx);
 
 void cpu_physical_memory_rw(hwaddr addr, void *buf,
                             hwaddr len, bool is_write);
include/hw/core/cpu.h +1 -0
index 92a4234439..c90cf3a162 100644
@@ -366,6 +366,7 @@ struct CPUState {
     QSIMPLEQ_HEAD(, qemu_work_item) work_list;
 
     CPUAddressSpace *cpu_ases;
+    int cpu_ases_ref_count;
     int num_ases;
     AddressSpace *as;
     MemoryRegion *memory;
softmmu/physmem.c +24 -0
index 18277ddd67..c75e3e8042 100644
@@ -761,6 +761,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
 
     if (!cpu->cpu_ases) {
         cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
+        cpu->cpu_ases_ref_count = cpu->num_ases;
     }
 
     newas = &cpu->cpu_ases[asidx];
@@ -774,6 +775,29 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
     }
 }
 
+void cpu_address_space_destroy(CPUState *cpu, int asidx)
+{
+    CPUAddressSpace *cpuas;
+
+    assert(asidx < cpu->num_ases);
+    assert(asidx == 0 || !kvm_enabled());
+    assert(cpu->cpu_ases);
+
+    cpuas = &cpu->cpu_ases[asidx];
+    if (tcg_enabled()) {
+        memory_listener_unregister(&cpuas->tcg_as_listener);
+    }
+
+    address_space_destroy(cpuas->as);
+
+    cpu->cpu_ases_ref_count--;
+    if (cpu->cpu_ases_ref_count == 0) {
+        g_free(cpu->cpu_ases);
+        cpu->cpu_ases = NULL;
+    }
+
+}
+
 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx)
 {
     /* Return the AddressSpace corresponding to the specified index */
Signature · 1 line
2.41.0
#23 lore raw

Re: [PATCH 07/22] exec/cpu: Introduce the CPU address space destruction function

Quoted 9 lines · Philippe Mathieu-Daudé
softmmu/physmem.c +1 -0
@@ -761,6 +761,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
 
      if (!cpu->cpu_ases) {
          cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
+        cpu->cpu_ases_ref_count = cpu->num_ases;
      }
 

...

Quoted 13 lines
softmmu/physmem.c +12 -0
+void cpu_address_space_destroy(CPUState *cpu, int asidx)
+{
+    CPUAddressSpace *cpuas;
+
+    assert(asidx < cpu->num_ases);
+    assert(asidx == 0 || !kvm_enabled());
+    assert(cpu->cpu_ases);
+
+    cpuas = &cpu->cpu_ases[asidx];
+    if (tcg_enabled()) {
+        memory_listener_unregister(&cpuas->tcg_as_listener);
+    }

softmmu/physmem.c void cpu_address_space_init(CPUState *cpu, int asidx, · line 799

Quoted 10 lines
softmmu/physmem.c +10 -0
+
+    address_space_destroy(cpuas->as);
+
+    cpu->cpu_ases_ref_count--;
+    if (cpu->cpu_ases_ref_count == 0) {
+        g_free(cpu->cpu_ases);
+        cpu->cpu_ases = NULL;
+    }
+
+}

I think it would be better to destroy all address spaces at once, so that you don't need to invent a reference count that isn't used for anything else.

r~

#24 lore raw

RE: [PATCH 07/22] exec/cpu: Introduce the CPU address space destruction function

In reply to Philippe Mathieu-Daudé

Hi Phillipe,
This patch has been taken as it is from ARM's RFC and the common patch-set mentioned below. So SOBs are all wrong everywhere.

Original RFC posted in the year 2020.
[1] https://lore.kernel.org/qemu-devel/[email protected]/

Recently posted RFC V2
[2] https://lore.kernel.org/qemu-devel/[email protected]/T/#m5f5ae40b091d69d01012880d7500d96874a9d39c

Recent common patch-set for Virtual CPU Hotplug
[3] https://lore.kernel.org/qemu-devel/[email protected]/

[4] https://lore.kernel.org/qemu-devel/[email protected]/

Beside my original patch-set had bug which you have inherited in this patch-set.

Thanks
Salil

Quoted 151 lines

From: [email protected] <qemu-arm- [email protected]> On Behalf Of Philippe Mathieu- Daudé
Sent: Monday, September 18, 2023 5:03 PM
To: [email protected]
Cc: Laurent Vivier <[email protected]>; Paolo Bonzini <[email protected]>; Max Filippov <[email protected]>; David Hildenbrand <[email protected]>; Peter Xu <[email protected]>; Anton Johansson <[email protected]>; Peter Maydell <[email protected]>; [email protected]; Marek Vasut <[email protected]>; David Gibson <[email protected]>; Brian Cain <[email protected]>; Yoshinori Sato <[email protected]>; Edgar E . Iglesias <[email protected]>; Claudio Fontana <[email protected]>; Daniel Henrique Barboza <[email protected]>; Artyom Tarasenko <[email protected]>; Marcelo Tosatti <[email protected]>; qemu- [email protected]; Liu Zhiwei <[email protected]>; Aurelien Jarno <[email protected]>; Ilya Leoshkevich <[email protected]>; Daniel Henrique Barboza <[email protected]>; Bastian Koppelmann <[email protected]>; Cédric Le Goater <[email protected]>; Alistair Francis <[email protected]>; Alessandro Di Federico <[email protected]>; Song Gao <[email protected]>; Marcel Apfelbaum <[email protected]>; Chris Wulff <[email protected]>; Michael S. Tsirkin <[email protected]>; Alistair Francis <[email protected]>; Fabiano Rosas <[email protected]>; [email protected]; wangyanan (Y) <[email protected]>; Luc Michel <[email protected]>; Weiwei Li <[email protected]>; Bin Meng <[email protected]>; Stafford Horne <[email protected]>; Xiaojuan Yang <[email protected]>; Daniel P . Berrange <[email protected]>; Thomas Huth <[email protected]>; Philippe Mathieu-Daudé <[email protected]>; [email protected]; Jiaxun Yang <[email protected]>; Richard Henderson <[email protected]>; Aleksandar Rikalo <[email protected]>; Bernhard Beschow <[email protected]>; Mark Cave-Ayland <[email protected]>; [email protected]; Alex Bennée <[email protected]>; Nicholas Piggin <[email protected]>; Greg Kurz <[email protected]>; Michael Rolnik <[email protected]>; Eduardo Habkost <[email protected]>; Markus Armbruster <[email protected]>; Palmer Dabbelt <[email protected]>; xianglai li <[email protected]>; Salil Mehta <[email protected]>; Igor Mammedov <[email protected]>; Ani Sinha <[email protected]>; Bibo Mao <[email protected]>
Subject: [PATCH 07/22] exec/cpu: Introduce the CPU address space destruction function

From: xianglai li <[email protected]>

Introduce new function to destroy CPU address space resources for cpu hot-(un)plug.

Cc "Salil Mehta" <[email protected]>
Cc Xiaojuan Yang <[email protected]>
Cc Song Gao <[email protected]>
Cc "Michael S. Tsirkin" <[email protected]>
Cc Igor Mammedov <[email protected]>
Cc Ani Sinha <[email protected]>
Cc Paolo Bonzini <[email protected]>
Cc Richard Henderson <[email protected]>
Cc Eduardo Habkost <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc "Philippe Mathieu-Daudé" <[email protected]>
Cc Yanan Wang <[email protected]>
Cc "Daniel P. Berrangé" <[email protected]>
Cc Peter Xu <[email protected]>
Cc David Hildenbrand <[email protected]>
Cc Bibo Mao <[email protected]>

Message-ID:
<3a4fc2a3df4b767c3c296a7da3bc15ca9c251316.1694433326.git.lixianglai@loongso n.cn>

---
 include/exec/cpu-common.h |  8 ++++++++
 include/hw/core/cpu.h     |  1 +
 softmmu/physmem.c         | 24 ++++++++++++++++++++++++
 3 files changed, 33 insertions(+)
include/exec/cpu-common.h +8 -0
index 41788c0bdd..eb56a228a2 100644
@@ -120,6 +120,14 @@ size_t qemu_ram_pagesize_largest(void);
  */
 void cpu_address_space_init(CPUState *cpu, int asidx,
                             const char *prefix, MemoryRegion *mr);
+/**
+ * cpu_address_space_destroy:
+ * @cpu: CPU for which address space needs to be destroyed
+ * @asidx: integer index of this address space
+ *
+ * Note that with KVM only one address space is supported.
+ */
+void cpu_address_space_destroy(CPUState *cpu, int asidx);
 
 void cpu_physical_memory_rw(hwaddr addr, void *buf,
                             hwaddr len, bool is_write);
include/hw/core/cpu.h +1 -0
index 92a4234439..c90cf3a162 100644
@@ -366,6 +366,7 @@ struct CPUState {
     QSIMPLEQ_HEAD(, qemu_work_item) work_list;
 
     CPUAddressSpace *cpu_ases;
+    int cpu_ases_ref_count;
     int num_ases;
     AddressSpace *as;
     MemoryRegion *memory;
softmmu/physmem.c +24 -1
index 18277ddd67..c75e3e8042 100644
@@ -761,6 +761,7 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
 
     if (!cpu->cpu_ases) {
         cpu->cpu_ases = g_new0(CPUAddressSpace, cpu->num_ases);
+        cpu->cpu_ases_ref_count = cpu->num_ases;
     }
 
     newas = &cpu->cpu_ases[asidx];
@@ -774,6 +775,29 @@ void cpu_address_space_init(CPUState *cpu, int asidx,
     }
 }
 
+void cpu_address_space_destroy(CPUState *cpu, int asidx)
+{
+    CPUAddressSpace *cpuas;
+
+    assert(asidx < cpu->num_ases);
+    assert(asidx == 0 || !kvm_enabled());
+    assert(cpu->cpu_ases);
+
+    cpuas = &cpu->cpu_ases[asidx];
+    if (tcg_enabled()) {
+        memory_listener_unregister(&cpuas->tcg_as_listener);
+    }
+
+    address_space_destroy(cpuas->as);
+
+    cpu->cpu_ases_ref_count--;
+    if (cpu->cpu_ases_ref_count == 0) {
+        g_free(cpu->cpu_ases);
+        cpu->cpu_ases = NULL;
+    }
+
+}
+
 AddressSpace *cpu_get_address_space(CPUState *cpu, int asidx)
 {
     /* Return the AddressSpace corresponding to the specified index */
--

2.41.0

#25 lore raw

PATCH 8/22 exec/cpu: RFC Destroy vCPU address spaces in cpu_common_unrealize()

In reply to Philippe Mathieu-Daudé

We create at least one vCPU address space by default in qemu_init_vcpu(), itself called in cpu_common_realizefn(). Since we don't have qemu_deinit_vcpu() helper (we probably don't need any), simply destroy all the address spaces in cpu_common_unrealizefn(), *after* the thread is destroyed.

Note: all targets were leaking the vCPU address spaces upon vCPU unrealize (like hot-unplugged actions).

---
 hw/core/cpu-common.c | 5 +++++
 1 file changed, 5 insertions(+)
hw/core/cpu-common.c +5 -0
index e5841c59df..35c0cc4dad 100644
@@ -224,6 +224,11 @@ static void cpu_common_unrealizefn(DeviceState *dev)
 
     /* Destroy vCPU thread */
     cpu_remove_sync(cpu);
+
+    /* Destroy CPU address space */
+    for (unsigned idx = 0; idx < cpu->num_ases; idx++) {
+        cpu_address_space_destroy(cpu, idx);
+    }
 }
 
 static void cpu_common_initfn(Object *obj)
Signature · 1 line
2.41.0
#26 lore raw

Re: [PATCH 08/22] exec/cpu: RFC Destroy vCPU address spaces in cpu_common_unrealize()

hw/core/cpu-common.c static void cpu_common_unrealizefn(DeviceState *dev) · line 231

Quoted 10 lines · Philippe Mathieu-Daudé

+++ b/hw/core/cpu-common.c

hw/core/cpu-common.c +5 -0
@@ -224,6 +224,11 @@ static void cpu_common_unrealizefn(DeviceState *dev)
 
      /* Destroy vCPU thread */
      cpu_remove_sync(cpu);
+
+    /* Destroy CPU address space */
+    for (unsigned idx = 0; idx < cpu->num_ases; idx++) {
+        cpu_address_space_destroy(cpu, idx);
+    }

Merged with the previous patch, with the single call.

r~

#27 lore raw

PATCH 9/22 target/arm: Create timers *after* accelerator vCPU is realized

In reply to Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized *after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
 target/arm/cpu.c | 14 ++++++++------
 1 file changed, 8 insertions(+), 6 deletions(-)
target/arm/cpu.c +8 -6
index fc3772025c..46d3f70d63 100644
@@ -1748,7 +1748,15 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
             return;
         }
     }
+#endif
 
+    cpu_exec_realizefn(cs, &local_err);
+    if (local_err != NULL) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
+#ifndef CONFIG_USER_ONLY
     {
         uint64_t scale;
 
@@ -1776,12 +1784,6 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     }
 #endif
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
     arm_cpu_finalize_features(cpu, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
Signature · 1 line
2.41.0
Reviewed-by on v1 9/22
#28 lore raw

Re: [PATCH 09/22] target/arm: Create timers *after* accelerator vCPU is realized

Quoted 9 lines · Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized*after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
  target/arm/cpu.c | 14 ++++++++------
  1 file changed, 8 insertions(+), 6 deletions(-)

Hmm. I suppose so. Considering we have no error return from timer_new().

r~

#29 lore raw

PATCH 10/22 target/hppa: Create timer *after* accelerator vCPU is realized

In reply to Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized *after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
 target/hppa/cpu.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
target/hppa/cpu.c +2 -2
index 49082bd2ba..b0d106b6c7 100644
@@ -131,8 +131,6 @@ static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    acc->parent_realize(dev, errp);
-
 #ifndef CONFIG_USER_ONLY
     {
         HPPACPU *cpu = HPPA_CPU(cs);
@@ -140,6 +138,8 @@ static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
                                         hppa_cpu_alarm_timer, cpu);
     }
 #endif
+
+    acc->parent_realize(dev, errp);
 }
 
 static void hppa_cpu_initfn(Object *obj)
Signature · 1 line
2.41.0
#30 lore raw

Re: [PATCH 10/22] target/hppa: Create timer *after* accelerator vCPU is realized

Quoted 24 lines · Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized *after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
  target/hppa/cpu.c | 4 ++--
  1 file changed, 2 insertions(+), 2 deletions(-)
target/hppa/cpu.c +0 -2
index 49082bd2ba..b0d106b6c7 100644
@@ -131,8 +131,6 @@ static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
          return;
      }
 
-    acc->parent_realize(dev, errp);
-
  #ifndef CONFIG_USER_ONLY
      {
          HPPACPU *cpu = HPPA_CPU(cs);

target/hppa/cpu.c static void hppa_cpu_realizefn(DeviceState *dev, Error **errp) · line 145

Quoted 8 lines
target/hppa/cpu.c +2 -0
@@ -140,6 +138,8 @@ static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
                                          hppa_cpu_alarm_timer, cpu);
      }
  #endif
+
+    acc->parent_realize(dev, errp);
  }
 
  static void hppa_cpu_initfn(Object *obj)

This appears to delay final realization of the vcpu, not advance it...

r~

#31 lore raw

PATCH 11/22 target/nios2: Create IRQs *after* accelerator vCPU is realized

In reply to Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized *after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
 target/nios2/cpu.c | 16 ++++++++--------
 1 file changed, 8 insertions(+), 8 deletions(-)
target/nios2/cpu.c +8 -8
index 7a92fc5f2c..f500ca7ba2 100644
@@ -201,14 +201,6 @@ static void nios2_cpu_realizefn(DeviceState *dev, Error **errp)
     Nios2CPUClass *ncc = NIOS2_CPU_GET_CLASS(dev);
     Error *local_err = NULL;
 
-#ifndef CONFIG_USER_ONLY
-    if (cpu->eic_present) {
-        qdev_init_gpio_in_named(DEVICE(cpu), eic_set_irq, "EIC", 1);
-    } else {
-        qdev_init_gpio_in_named(DEVICE(cpu), iic_set_irq, "IRQ", 32);
-    }
-#endif
-
     cpu_exec_realizefn(cs, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
@@ -220,6 +212,14 @@ static void nios2_cpu_realizefn(DeviceState *dev, Error **errp)
     /* We have reserved storage for cpuid; might as well use it. */
     cpu->env.ctrl[CR_CPUID] = cs->cpu_index;
 
+#ifndef CONFIG_USER_ONLY
+    if (cpu->eic_present) {
+        qdev_init_gpio_in_named(DEVICE(cpu), eic_set_irq, "EIC", 1);
+    } else {
+        qdev_init_gpio_in_named(DEVICE(cpu), iic_set_irq, "IRQ", 32);
+    }
+#endif
+
     ncc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
Signature · 1 line
2.41.0
Reviewed-by on v1 11/22
#32 lore raw

Re: [PATCH 11/22] target/nios2: Create IRQs *after* accelerator vCPU is realized

Quoted 9 lines · Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized*after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
  target/nios2/cpu.c | 16 ++++++++--------
  1 file changed, 8 insertions(+), 8 deletions(-)

r~

#33 lore raw

PATCH 12/22 target/mips: Create clock *after* accelerator vCPU is realized

In reply to Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized *after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
 target/mips/cpu.c | 28 ++++++++++++++--------------
 1 file changed, 14 insertions(+), 14 deletions(-)
target/mips/cpu.c +14 -14
index 0aea69aaf9..7c81e6c356 100644
@@ -464,20 +464,6 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(dev);
     Error *local_err = NULL;
 
-    if (!clock_get(cpu->clock)) {
-#ifndef CONFIG_USER_ONLY
-        if (!qtest_enabled()) {
-            g_autofree char *cpu_freq_str = freq_to_str(CPU_FREQ_HZ_DEFAULT);
-
-            warn_report("CPU input clock is not connected to any output clock, "
-                        "using default frequency of %s.", cpu_freq_str);
-        }
-#endif
-        /* Initialize the frequency in case the clock remains unconnected. */
-        clock_set_hz(cpu->clock, CPU_FREQ_HZ_DEFAULT);
-    }
-    mips_cp0_period_set(cpu);
-
     cpu_exec_realizefn(cs, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
@@ -492,6 +478,20 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     fpu_init(env, env->cpu_model);
     mvp_init(env);
 
+    if (!clock_get(cpu->clock)) {
+#ifndef CONFIG_USER_ONLY
+        if (!qtest_enabled()) {
+            g_autofree char *cpu_freq_str = freq_to_str(CPU_FREQ_HZ_DEFAULT);
+
+            warn_report("CPU input clock is not connected to any output clock, "
+                        "using default frequency of %s.", cpu_freq_str);
+        }
+#endif
+        /* Initialize the frequency in case the clock remains unconnected. */
+        clock_set_hz(cpu->clock, CPU_FREQ_HZ_DEFAULT);
+    }
+    mips_cp0_period_set(cpu);
+
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
 }
Signature · 1 line
2.41.0
Reviewed-by on v1 12/22
#34 lore raw

Re: [PATCH 12/22] target/mips: Create clock *after* accelerator vCPU is realized

Quoted 9 lines · Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized*after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
  target/mips/cpu.c | 28 ++++++++++++++--------------
  1 file changed, 14 insertions(+), 14 deletions(-)

r~

#35 lore raw

PATCH 13/22 target/xtensa: Create IRQs *after* accelerator vCPU is realized

In reply to Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized *after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
 target/xtensa/cpu.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
target/xtensa/cpu.c +4 -4
index 300d19d45c..bbfd2d42a8 100644
@@ -162,10 +162,6 @@ static void xtensa_cpu_realizefn(DeviceState *dev, Error **errp)
     XtensaCPUClass *xcc = XTENSA_CPU_GET_CLASS(dev);
     Error *local_err = NULL;
 
-#ifndef CONFIG_USER_ONLY
-    xtensa_irq_init(&XTENSA_CPU(dev)->env);
-#endif
-
     cpu_exec_realizefn(cs, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
@@ -174,6 +170,10 @@ static void xtensa_cpu_realizefn(DeviceState *dev, Error **errp)
 
     cs->gdb_num_regs = xcc->config->gdb_regmap.num_regs;
 
+#ifndef CONFIG_USER_ONLY
+    xtensa_irq_init(&XTENSA_CPU(dev)->env);
+#endif
+
     xcc->parent_realize(dev, errp);
 }
 
Signature · 1 line
2.41.0
Reviewed-by on v1 13/22
#36 lore raw

Re: [PATCH 13/22] target/xtensa: Create IRQs *after* accelerator vCPU is realized

Quoted 9 lines · Philippe Mathieu-Daudé

Architecture specific hardware doesn't have a particular dependency on the accelerator vCPU (created with cpu_exec_realizefn), and can be initialized*after* the vCPU is realized. Doing so allows further generic API simplification (in few commits).

---
  target/xtensa/cpu.c | 8 ++++----
  1 file changed, 4 insertions(+), 4 deletions(-)

r~

#37 lore raw

PATCH 14/22 target/sparc: Init CPU environment *after* accelerator vCPU is realized

In reply to Philippe Mathieu-Daudé

These fields from the environment don't affect how accelerators create their vCPU thread. We can safely reorder them *after* the cpu_exec_realizefn() call. Doing so allows further generic API simplification (in the next commit).

---
 target/sparc/cpu.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
target/sparc/cpu.c +6 -6
index 2fdc95eda9..88157fcd33 100644
@@ -756,6 +756,12 @@ static void sparc_cpu_realizefn(DeviceState *dev, Error **errp)
     SPARCCPU *cpu = SPARC_CPU(dev);
     CPUSPARCState *env = &cpu->env;
 
+    cpu_exec_realizefn(cs, &local_err);
+    if (local_err != NULL) {
+        error_propagate(errp, local_err);
+        return;
+    }
+
 #if defined(CONFIG_USER_ONLY)
     if ((env->def.features & CPU_FEATURE_FLOAT)) {
         env->def.features |= CPU_FEATURE_FLOAT128;
@@ -776,12 +782,6 @@ static void sparc_cpu_realizefn(DeviceState *dev, Error **errp)
     env->version |= env->def.nwindows - 1;
 #endif
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
     scc->parent_realize(dev, errp);
 }
 
Signature · 1 line
2.41.0
Reviewed-by on v1 14/22
#38 lore raw

Re: [PATCH 14/22] target/sparc: Init CPU environment *after* accelerator vCPU is realized

Quoted 9 lines · Philippe Mathieu-Daudé

These fields from the environment don't affect how accelerators create their vCPU thread. We can safely reorder them*after* the cpu_exec_realizefn() call. Doing so allows further generic API simplification (in the next commit).

---
  target/sparc/cpu.c | 12 ++++++------
  1 file changed, 6 insertions(+), 6 deletions(-)

r~

#39 lore raw

PATCH 15/22 exec/cpu: Introduce CPUClass::verify_accel_features()

In reply to Philippe Mathieu-Daudé

Some CPUs need to check the requested features are compatible with the requested accelerator. This has to be done *before* the accelerator realizes a vCPU.
Introduce the verify_accel_features() handler and call it just before accel_cpu_realizefn().

---
 include/hw/core/cpu.h | 4 ++++
 cpu.c                 | 5 +++++
 2 files changed, 9 insertions(+)
include/hw/core/cpu.h +4 -0
index c90cf3a162..1e940f6bb5 100644
@@ -103,6 +103,9 @@ struct SysemuCPUOps;
  * @class_by_name: Callback to map -cpu command line model name to an
  * instantiatable CPU type.
  * @parse_features: Callback to parse command line arguments.
+ * @verify_accel_features: Callback to verify if all requested CPU are
+ *        compatible with the requested accelerator. Called before the
+ *        accelerator realize a vCPU.
  * @reset_dump_flags: #CPUDumpFlags to use for reset logging.
  * @has_work: Callback for checking if there is work to do.
  * @memory_rw_debug: Callback for GDB memory access.
@@ -183,6 +186,7 @@ struct CPUClass {
      * class data that depends on the accelerator, see accel/accel-common.c.
      */
     void (*init_accel_cpu)(struct AccelCPUClass *accel_cpu, CPUClass *cc);
+    bool (*verify_accel_features)(CPUState *cs, Error **errp);
 
     /*
      * Keep non-pointer data at the end to minimize holes.
cpu.c +5 -0
index 0769b0b153..84b03c09ac 100644
@@ -136,6 +136,11 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
     /* cache the cpu class for the hotpath */
     cpu->cc = CPU_GET_CLASS(cpu);
 
+    if (cpu->cc->verify_accel_features
+        && !cpu->cc->verify_accel_features(cpu, errp)) {
+        return false;
+    }
+
     if (!accel_cpu_realizefn(cpu, errp)) {
         return;
     }
Signature · 1 line
2.41.0
Reviewed-by on v1 15/22
#40 lore raw

Re: [PATCH 15/22] exec/cpu: Introduce CPUClass::verify_accel_features()

Quoted 11 lines · Philippe Mathieu-Daudé

Some CPUs need to check the requested features are compatible with the requested accelerator. This has to be done*before* the accelerator realizes a vCPU.
Introduce the verify_accel_features() handler and call it just before accel_cpu_realizefn().

---
  include/hw/core/cpu.h | 4 ++++
  cpu.c                 | 5 +++++
  2 files changed, 9 insertions(+)

r~

#41 lore raw

PATCH 16/22 target/arm: Extract verify_accel_features() from cpu_realize()

In reply to Philippe Mathieu-Daudé

When looking at the arm_cpu_realizefn() method, most of the code run before the cpu_exec_realizefn() call checks whether the requested CPU features are compatible with the requested accelerator. Extract this code to a dedicated handler matching our recently added CPUClass::verify_accel_features() handler.

---
 target/arm/cpu.c | 41 ++++++++++++++++++++++++-----------------
 1 file changed, 24 insertions(+), 17 deletions(-)
target/arm/cpu.c +24 -17
index 46d3f70d63..a551383fd3 100644
@@ -1675,19 +1675,10 @@ void arm_cpu_finalize_features(ARMCPU *cpu, Error **errp)
     }
 }
 
-static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
+static bool arm_cpu_verify_accel_features(CPUState *cs, Error **errp)
 {
-    CPUState *cs = CPU(dev);
-    ARMCPU *cpu = ARM_CPU(dev);
-    ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
+    ARMCPU *cpu = ARM_CPU(cs);
     CPUARMState *env = &cpu->env;
-    int pagebits;
-    Error *local_err = NULL;
-
-    /* Use pc-relative instructions in system-mode */
-#ifndef CONFIG_USER_ONLY
-    cs->tcg_cflags |= CF_PCREL;
-#endif
 
     /* If we needed to query the host kernel for the CPU features
      * then it's possible that might have failed in the initfn, but
@@ -1699,10 +1690,13 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
         } else {
             error_setg(errp, "Failed to retrieve host CPU features");
         }
-        return;
+        return false;
     }
 
 #ifndef CONFIG_USER_ONLY
+    /* Use pc-relative instructions in system-mode */
+    cs->tcg_cflags |= CF_PCREL;
+
     /* The NVIC and M-profile CPU are two halves of a single piece of
      * hardware; trying to use one without the other is a command line
      * error and will result in segfaults if not caught here.
@@ -1710,12 +1704,12 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     if (arm_feature(env, ARM_FEATURE_M)) {
         if (!env->nvic) {
             error_setg(errp, "This board cannot be used with Cortex-M CPUs");
-            return;
+            return false;
         }
     } else {
         if (env->nvic) {
             error_setg(errp, "This board can only be used with Cortex-M CPUs");
-            return;
+            return false;
         }
     }
 
@@ -1733,23 +1727,35 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
             error_setg(errp,
                        "Cannot enable %s when using an M-profile guest CPU",
                        current_accel_name());
-            return;
+            return false;
         }
         if (cpu->has_el3) {
             error_setg(errp,
                        "Cannot enable %s when guest CPU has EL3 enabled",
                        current_accel_name());
-            return;
+            return false;
         }
         if (cpu->tag_memory) {
             error_setg(errp,
                        "Cannot enable %s when guest CPUs has MTE enabled",
                        current_accel_name());
-            return;
+            return false;
         }
     }
 #endif
 
+    return true;
+}
+
+static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
+{
+    CPUState *cs = CPU(dev);
+    ARMCPU *cpu = ARM_CPU(dev);
+    ARMCPUClass *acc = ARM_CPU_GET_CLASS(dev);
+    CPUARMState *env = &cpu->env;
+    int pagebits;
+    Error *local_err = NULL;
+
     cpu_exec_realizefn(cs, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
@@ -2383,6 +2389,7 @@ static void arm_cpu_class_init(ObjectClass *oc, void *data)
                                        &acc->parent_phases);
 
     cc->class_by_name = arm_cpu_class_by_name;
+    cc->verify_accel_features = arm_cpu_verify_accel_features;
     cc->has_work = arm_cpu_has_work;
     cc->dump_state = arm_cpu_dump_state;
     cc->set_pc = arm_cpu_set_pc;
Signature · 1 line
2.41.0
#42 lore raw

Re: [PATCH 16/22] target/arm: Extract verify_accel_features() from cpu_realize()

Quoted 5 lines · Philippe Mathieu-Daudé

When looking at the arm_cpu_realizefn() method, most of the code run before the cpu_exec_realizefn() call checks whether the requested CPU features are compatible with the requested accelerator. Extract this code to a dedicated handler matching our recently added CPUClass::verify_accel_features() handler.

How much verification is this intended to do?
Can realize now be required to succeed?

For instance, the error for unset gt_cntfrq_hz just before timer_new could be moved to within this new function.

r~

#43 lore raw

PATCH 17/22 target/i386: Extract verify_accel_features() from cpu_realize()

In reply to Philippe Mathieu-Daudé

When looking at the x86_cpu_realizefn() method, most of the code run before the cpu_exec_realizefn() call checks whether the requested CPU features are compatible with the requested accelerator. Extract this code to a dedicated handler matching our recently added CPUClass::verify_accel_features() handler.

---
 target/i386/cpu.c | 62 +++++++++++++++++++++++++++--------------------
 1 file changed, 36 insertions(+), 26 deletions(-)
target/i386/cpu.c +36 -26
index d79797d963..2884733397 100644
@@ -7208,26 +7208,19 @@ static void x86_cpu_hyperv_realize(X86CPU *cpu)
     cpu->hyperv_limits[2] = 0;
 }
 
-static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
+/*
+ * note: the call to the framework needs to happen after feature expansion,
+ * but before the checks/modifications to ucode_rev, mwait, phys_bits.
+ * These may be set by the accel-specific code,
+ * and the results are subsequently checked / assumed in x86_cpu_realizefn().
+ */
+static bool x86_cpu_verify_accel_features(CPUState *cs, Error **errp)
 {
-    CPUState *cs = CPU(dev);
-    X86CPU *cpu = X86_CPU(dev);
-    X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
+    X86CPU *cpu = X86_CPU(cs);
     CPUX86State *env = &cpu->env;
     Error *local_err = NULL;
-    static bool ht_warned;
     unsigned requested_lbr_fmt;
 
-    /* Use pc-relative instructions in system-mode */
-#ifndef CONFIG_USER_ONLY
-    cs->tcg_cflags |= CF_PCREL;
-#endif
-
-    if (cpu->apic_id == UNASSIGNED_APIC_ID) {
-        error_setg(errp, "apic-id property was not initialized properly");
-        return;
-    }
-
     /*
      * Process Hyper-V enlightenments.
      * Note: this currently has to happen before the expansion of CPU features.
@@ -7236,7 +7229,7 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 
     x86_cpu_expand_features(cpu, &local_err);
     if (local_err) {
-        goto out;
+        return false;
     }
 
     /*
@@ -7246,7 +7239,7 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
     if (cpu->lbr_fmt != ~PERF_CAP_LBR_FMT) {
         if ((cpu->lbr_fmt & PERF_CAP_LBR_FMT) != cpu->lbr_fmt) {
             error_setg(errp, "invalid lbr-fmt");
-            return;
+            return false;
         }
         env->features[FEAT_PERF_CAPABILITIES] &= ~PERF_CAP_LBR_FMT;
         env->features[FEAT_PERF_CAPABILITIES] |= cpu->lbr_fmt;
@@ -7265,13 +7258,13 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 
         if (!cpu->enable_pmu) {
             error_setg(errp, "vPMU: LBR is unsupported without pmu=on");
-            return;
+            return false;
         }
         if (requested_lbr_fmt != host_lbr_fmt) {
             error_setg(errp, "vPMU: the lbr-fmt value (0x%x) does not match "
                         "the host value (0x%x).",
                         requested_lbr_fmt, host_lbr_fmt);
-            return;
+            return false;
         }
     }
 
@@ -7282,7 +7275,7 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
                    accel_uses_host_cpuid() ?
                        "Host doesn't support requested features" :
                        "TCG doesn't support requested features");
-        goto out;
+        return false;
     }
 
     /* On AMD CPUs, some CPUID[8000_0001].EDX bits must match the bits on
@@ -7296,12 +7289,28 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
 
     x86_cpu_set_sgxlepubkeyhash(env);
 
-    /*
-     * note: the call to the framework needs to happen after feature expansion,
-     * but before the checks/modifications to ucode_rev, mwait, phys_bits.
-     * These may be set by the accel-specific code,
-     * and the results are subsequently checked / assumed in this function.
-     */
+    return true;
+}
+
+static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
+{
+    CPUState *cs = CPU(dev);
+    X86CPU *cpu = X86_CPU(dev);
+    X86CPUClass *xcc = X86_CPU_GET_CLASS(dev);
+    CPUX86State *env = &cpu->env;
+    Error *local_err = NULL;
+    static bool ht_warned;
+
+    /* Use pc-relative instructions in system-mode */
+#ifndef CONFIG_USER_ONLY
+    cs->tcg_cflags |= CF_PCREL;
+#endif
+
+    if (cpu->apic_id == UNASSIGNED_APIC_ID) {
+        error_setg(errp, "apic-id property was not initialized properly");
+        return;
+    }
+
     cpu_exec_realizefn(cs, &local_err);
     if (local_err != NULL) {
         error_propagate(errp, local_err);
@@ -7950,6 +7959,7 @@ static void x86_cpu_common_class_init(ObjectClass *oc, void *data)
 
     cc->class_by_name = x86_cpu_class_by_name;
     cc->parse_features = x86_cpu_parse_featurestr;
+    cc->verify_accel_features = x86_cpu_verify_accel_features;
     cc->has_work = x86_cpu_has_work;
     cc->dump_state = x86_cpu_dump_state;
     cc->set_pc = x86_cpu_set_pc;
Signature · 1 line
2.41.0
#44 lore raw

PATCH 18/22 target/s390x: Call s390_cpu_realize_sysemu from s390_realize_cpu_model

In reply to Philippe Mathieu-Daudé

s390_cpu_realize_sysemu() runs some checks for the TCG accelerator, previous to creating the vCPU. s390_realize_cpu_model() also does run some checks for KVM.
Move the sysemu call to s390_realize_cpu_model(). Having a single call before cpu_exec_realizefn() will allow us to factor a verify_accel_features() handler out in a pair of commits.

Directly pass a S390CPU* to s390_cpu_realize_sysemu() to simplify.

---
 target/s390x/s390x-internal.h | 2 +-
 target/s390x/cpu-sysemu.c     | 3 +--
 target/s390x/cpu.c            | 6 ------
 target/s390x/cpu_models.c     | 4 ++++
 4 files changed, 6 insertions(+), 9 deletions(-)
target/s390x/s390x-internal.h +1 -1
index 825252d728..781ac08458 100644
@@ -241,7 +241,7 @@ uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
 unsigned int s390_cpu_halt(S390CPU *cpu);
 void s390_cpu_unhalt(S390CPU *cpu);
 void s390_cpu_init_sysemu(Object *obj);
-bool s390_cpu_realize_sysemu(DeviceState *dev, Error **errp);
+bool s390_cpu_realize_sysemu(S390CPU *cpu, Error **errp);
 void s390_cpu_finalize(Object *obj);
 void s390_cpu_class_init_sysemu(CPUClass *cc);
 void s390_cpu_machine_reset_cb(void *opaque);
target/s390x/cpu-sysemu.c +1 -2
index 8112561e5e..5178736c46 100644
@@ -122,9 +122,8 @@ void s390_cpu_init_sysemu(Object *obj)
     s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
 }
 
-bool s390_cpu_realize_sysemu(DeviceState *dev, Error **errp)
+bool s390_cpu_realize_sysemu(S390CPU *cpu, Error **errp)
 {
-    S390CPU *cpu = S390_CPU(dev);
     MachineState *ms = MACHINE(qdev_get_machine());
     unsigned int max_cpus = ms->smp.max_cpus;
 
target/s390x/cpu.c +0 -6
index 416ac6c4e0..7257d4bc19 100644
@@ -237,12 +237,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
         goto out;
     }
 
-#if !defined(CONFIG_USER_ONLY)
-    if (!s390_cpu_realize_sysemu(dev, &err)) {
-        goto out;
-    }
-#endif
-
     cpu_exec_realizefn(cs, &err);
     if (err != NULL) {
         goto out;
target/s390x/cpu_models.c +4 -0
index 98f14c09c2..f030be0d55 100644
@@ -612,6 +612,10 @@ void s390_realize_cpu_model(CPUState *cs, Error **errp)
         cpu->env.cpuid = deposit64(cpu->env.cpuid, CPU_PHYS_ADDR_SHIFT,
                                    CPU_PHYS_ADDR_BITS, cpu->env.core_id);
     }
+
+    if (!s390_cpu_realize_sysemu(cpu, &err)) {
+        return;
+    }
 #endif
 }
 
Signature · 1 line
2.41.0
#45 lore raw

Re: [PATCH 18/22] target/s390x: Call s390_cpu_realize_sysemu from s390_realize_cpu_model

Quoted 67 lines · Philippe Mathieu-Daudé

s390_cpu_realize_sysemu() runs some checks for the TCG accelerator, previous to creating the vCPU. s390_realize_cpu_model() also does run some checks for KVM.
Move the sysemu call to s390_realize_cpu_model(). Having a single call before cpu_exec_realizefn() will allow us to factor a verify_accel_features() handler out in a pair of commits.

Directly pass a S390CPU* to s390_cpu_realize_sysemu() to simplify.

---
  target/s390x/s390x-internal.h | 2 +-
  target/s390x/cpu-sysemu.c     | 3 +--
  target/s390x/cpu.c            | 6 ------
  target/s390x/cpu_models.c     | 4 ++++
  4 files changed, 6 insertions(+), 9 deletions(-)
target/s390x/s390x-internal.h +1 -1
index 825252d728..781ac08458 100644
@@ -241,7 +241,7 @@ uint32_t calc_cc(CPUS390XState *env, uint32_t cc_op, uint64_t src, uint64_t dst,
  unsigned int s390_cpu_halt(S390CPU *cpu);
  void s390_cpu_unhalt(S390CPU *cpu);
  void s390_cpu_init_sysemu(Object *obj);
-bool s390_cpu_realize_sysemu(DeviceState *dev, Error **errp);
+bool s390_cpu_realize_sysemu(S390CPU *cpu, Error **errp);
  void s390_cpu_finalize(Object *obj);
  void s390_cpu_class_init_sysemu(CPUClass *cc);
  void s390_cpu_machine_reset_cb(void *opaque);
target/s390x/cpu-sysemu.c +1 -2
index 8112561e5e..5178736c46 100644
@@ -122,9 +122,8 @@ void s390_cpu_init_sysemu(Object *obj)
      s390_cpu_set_state(S390_CPU_STATE_STOPPED, cpu);
  }
 
-bool s390_cpu_realize_sysemu(DeviceState *dev, Error **errp)
+bool s390_cpu_realize_sysemu(S390CPU *cpu, Error **errp)
  {
-    S390CPU *cpu = S390_CPU(dev);
      MachineState *ms = MACHINE(qdev_get_machine());
      unsigned int max_cpus = ms->smp.max_cpus;
 
target/s390x/cpu.c +0 -6
index 416ac6c4e0..7257d4bc19 100644
@@ -237,12 +237,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
          goto out;
      }
 
-#if !defined(CONFIG_USER_ONLY)
-    if (!s390_cpu_realize_sysemu(dev, &err)) {
-        goto out;
-    }
-#endif
-
      cpu_exec_realizefn(cs, &err);
      if (err != NULL) {
          goto out;
target/s390x/cpu_models.c +0 -0
index 98f14c09c2..f030be0d55 100644

target/s390x/cpu_models.c void s390_realize_cpu_model(CPUState *cs, Error **errp) · line 621

Quoted 10 lines
target/s390x/cpu_models.c +4 -0
@@ -612,6 +612,10 @@ void s390_realize_cpu_model(CPUState *cs, Error **errp)
          cpu->env.cpuid = deposit64(cpu->env.cpuid, CPU_PHYS_ADDR_SHIFT,
                                     CPU_PHYS_ADDR_BITS, cpu->env.core_id);
      }
+
+    if (!s390_cpu_realize_sysemu(cpu, &err)) {
+        return;
+    }
  #endif
  }
 

That has nothing to do with CPU models and is, therefore, completely misplaced ... :/

Or what am I missing?

Signature · 3 lines
Cheers,

David / dhildenb
#46 lore raw

PATCH 19/22 target/s390x: Have s390_realize_cpu_model() return a boolean

In reply to Philippe Mathieu-Daudé

Following the example documented since commit e3fe3988d7 ("error:
Document Error API usage rules"), have s390_realize_cpu_model() return a boolean indicating whether an error is set or not.

---
 target/s390x/s390x-internal.h |  2 +-
 target/s390x/cpu.c            |  3 +--
 target/s390x/cpu_models.c     | 12 +++++++-----
 3 files changed, 9 insertions(+), 8 deletions(-)
target/s390x/s390x-internal.h +1 -1
index 781ac08458..67f21f53a9 100644
@@ -260,7 +260,7 @@ static inline void s390_cpu_unhalt(S390CPU *cpu)
 
 /* cpu_models.c */
 void s390_cpu_model_class_register_props(ObjectClass *oc);
-void s390_realize_cpu_model(CPUState *cs, Error **errp);
+bool s390_realize_cpu_model(CPUState *cs, Error **errp);
 S390CPUModel *get_max_cpu_model(Error **errp);
 void apply_cpu_model(const S390CPUModel *model, Error **errp);
 ObjectClass *s390_cpu_class_by_name(const char *name);
target/s390x/cpu.c +1 -2
index 7257d4bc19..1a44a6d2b2 100644
@@ -232,8 +232,7 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     Error *err = NULL;
 
     /* the model has to be realized before qemu_init_vcpu() due to kvm */
-    s390_realize_cpu_model(cs, &err);
-    if (err) {
+    if (!s390_realize_cpu_model(cs, &err)) {
         goto out;
     }
 
target/s390x/cpu_models.c +7 -5
index f030be0d55..0605073dc3 100644
@@ -567,7 +567,7 @@ S390CPUModel *get_max_cpu_model(Error **errp)
     return &max_model;
 }
 
-void s390_realize_cpu_model(CPUState *cs, Error **errp)
+bool s390_realize_cpu_model(CPUState *cs, Error **errp)
 {
     Error *err = NULL;
     S390CPUClass *xcc = S390_CPU_GET_CLASS(cs);
@@ -576,19 +576,19 @@ void s390_realize_cpu_model(CPUState *cs, Error **errp)
 
     if (xcc->kvm_required && !kvm_enabled()) {
         error_setg(errp, "CPU definition requires KVM");
-        return;
+        return false;
     }
 
     if (!cpu->model) {
         /* no host model support -> perform compatibility stuff */
         apply_cpu_model(NULL, errp);
-        return;
+        return false;
     }
 
     max_model = get_max_cpu_model(errp);
     if (!max_model) {
         error_prepend(errp, "CPU models are not available: ");
-        return;
+        return false;
     }
 
     /* copy over properties that can vary */
@@ -601,7 +601,7 @@ void s390_realize_cpu_model(CPUState *cs, Error **errp)
     check_compatibility(max_model, cpu->model, &err);
     if (err) {
         error_propagate(errp, err);
-        return;
+        return false;
     }
 
     apply_cpu_model(cpu->model, errp);
@@ -617,6 +617,8 @@ void s390_realize_cpu_model(CPUState *cs, Error **errp)
         return;
     }
 #endif
+
+    return true;
 }
 
 static void get_feature(Object *obj, Visitor *v, const char *name,
Signature · 1 line
2.41.0
#47 lore raw

PATCH 20/22 target/s390x: Use s390_realize_cpu_model() as verify_accel_features()

In reply to Philippe Mathieu-Daudé

s390_realize_cpu_model() checks if CPU model and definitions are compatible with the KVM / TCG accelerators, before realizing the vCPU. Use it directly as CPUClass::verify_accel_features() handler (called from cpu_exec_realizefn()).

---
 target/s390x/cpu.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)
target/s390x/cpu.c +1 -5
index 1a44a6d2b2..983dbfe563 100644
@@ -231,11 +231,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
     Error *err = NULL;
 
-    /* the model has to be realized before qemu_init_vcpu() due to kvm */
-    if (!s390_realize_cpu_model(cs, &err)) {
-        goto out;
-    }
-
     cpu_exec_realizefn(cs, &err);
     if (err != NULL) {
         goto out;
@@ -329,6 +324,7 @@ static void s390_cpu_class_init(ObjectClass *oc, void *data)
 
     scc->reset = s390_cpu_reset;
     cc->class_by_name = s390_cpu_class_by_name,
+    cc->verify_accel_features = s390_realize_cpu_model;
     cc->has_work = s390_cpu_has_work;
     cc->dump_state = s390_cpu_dump_state;
     cc->query_cpu_fast = s390_query_cpu_fast;
Signature · 1 line
2.41.0
#48 lore raw

PATCH 21/22 exec/cpu: Have cpu_exec_realize() return a boolean

In reply to Philippe Mathieu-Daudé

Following the example documented since commit e3fe3988d7 ("error:
Document Error API usage rules"), have cpu_exec_realizefn() return a boolean indicating whether an error is set or not.

---
 include/hw/core/cpu.h | 2 +-
 cpu.c                 | 6 ++++--
 2 files changed, 5 insertions(+), 3 deletions(-)
include/hw/core/cpu.h +1 -1
index 1e940f6bb5..3dc6968428 100644
@@ -1014,7 +1014,7 @@ G_NORETURN void cpu_abort(CPUState *cpu, const char *fmt, ...)
 /* $(top_srcdir)/cpu.c */
 void cpu_class_init_props(DeviceClass *dc);
 void cpu_exec_initfn(CPUState *cpu);
-void cpu_exec_realizefn(CPUState *cpu, Error **errp);
+bool cpu_exec_realizefn(CPUState *cpu, Error **errp);
 void cpu_exec_unrealizefn(CPUState *cpu);
 
 /**
cpu.c +4 -2
index 84b03c09ac..96ae440b81 100644
@@ -131,7 +131,7 @@ const VMStateDescription vmstate_cpu_common = {
 };
 #endif
 
-void cpu_exec_realizefn(CPUState *cpu, Error **errp)
+bool cpu_exec_realizefn(CPUState *cpu, Error **errp)
 {
     /* cache the cpu class for the hotpath */
     cpu->cc = CPU_GET_CLASS(cpu);
@@ -142,7 +142,7 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
     }
 
     if (!accel_cpu_realizefn(cpu, errp)) {
-        return;
+        return false;
     }
 
     /* NB: errp parameter is unused currently */
@@ -169,6 +169,8 @@ void cpu_exec_realizefn(CPUState *cpu, Error **errp)
         vmstate_register(NULL, cpu->cpu_index, cpu->cc->sysemu_ops->legacy_vmsd, cpu);
     }
 #endif /* CONFIG_USER_ONLY */
+
+    return true;
 }
 
 void cpu_exec_unrealizefn(CPUState *cpu)
Signature · 1 line
2.41.0
Reviewed-by on v1 21/22
#49 lore raw

Re: [PATCH 21/22] exec/cpu: Have cpu_exec_realize() return a boolean

Quoted 9 lines · Philippe Mathieu-Daudé

Following the example documented since commit e3fe3988d7 ("error:
Document Error API usage rules"), have cpu_exec_realizefn() return a boolean indicating whether an error is set or not.

---
  include/hw/core/cpu.h | 2 +-
  cpu.c                 | 6 ++++--
  2 files changed, 5 insertions(+), 3 deletions(-)

r~

#50 lore raw

PATCH 22/22 exec/cpu: Call cpu_exec_realizefn() once in cpu_common_realize()

In reply to Philippe Mathieu-Daudé

cpu_exec_realizefn() is called in each ${target}_cpu_realize(), before calling their parent_realize(), which is simply cpu_common_realizefn(). Directly call it there instead.

---
 hw/core/cpu-common.c      |  4 ++++
 target/alpha/cpu.c        |  8 --------
 target/arm/cpu.c          |  6 ------
 target/avr/cpu.c          |  7 -------
 target/cris/cpu.c         |  7 -------
 target/hexagon/cpu.c      |  7 -------
 target/hppa/cpu.c         | 15 ++-------------
 target/i386/cpu.c         |  6 ------
 target/i386/kvm/kvm-cpu.c |  4 ++--
 target/loongarch/cpu.c    |  7 -------
 target/m68k/cpu.c         |  7 -------
 target/microblaze/cpu.c   |  7 -------
 target/mips/cpu.c         |  7 -------
 target/nios2/cpu.c        |  7 -------
 target/openrisc/cpu.c     |  7 -------
 target/ppc/cpu_init.c     |  5 -----
 target/riscv/cpu.c        |  6 ------
 target/rx/cpu.c           |  7 -------
 target/s390x/cpu.c        |  5 -----
 target/sh4/cpu.c          |  7 -------
 target/sparc/cpu.c        |  8 --------
 target/tricore/cpu.c      |  7 -------
 target/xtensa/cpu.c       |  7 -------
 23 files changed, 8 insertions(+), 150 deletions(-)
hw/core/cpu-common.c +4 -0
index 35c0cc4dad..8901c482a0 100644
@@ -204,6 +204,10 @@ static void cpu_common_realizefn(DeviceState *dev, Error **errp)
         }
     }
 
+    if (!cpu_exec_realizefn(cpu, errp)) {
+        return;
+    }
+
     /* Create CPU address space and vCPU thread */
     qemu_init_vcpu(cpu);
 
target/alpha/cpu.c +0 -8
index eb78318bb8..85834c4d61 100644
@@ -72,15 +72,7 @@ static void alpha_cpu_disas_set_info(CPUState *cpu, disassemble_info *info)
 
 static void alpha_cpu_realizefn(DeviceState *dev, Error **errp)
 {
-    CPUState *cs = CPU(dev);
     AlphaCPUClass *acc = ALPHA_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     acc->parent_realize(dev, errp);
 }
target/arm/cpu.c +0 -6
index a551383fd3..d8eaa186cd 100644
@@ -1756,12 +1756,6 @@ static void arm_cpu_realizefn(DeviceState *dev, Error **errp)
     int pagebits;
     Error *local_err = NULL;
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
 #ifndef CONFIG_USER_ONLY
     {
         uint64_t scale;
target/avr/cpu.c +0 -7
index d3460b3960..e512ad46d3 100644
@@ -112,13 +112,6 @@ static void avr_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     AVRCPUClass *mcc = AVR_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     mcc->parent_realize(dev, errp);
     cpu_reset(cs);
target/cris/cpu.c +0 -7
index 671693a362..9fb69ecda4 100644
@@ -144,13 +144,6 @@ static void cris_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     CRISCPUClass *ccc = CRIS_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     ccc->parent_realize(dev, errp);
     cpu_reset(cs);
target/hexagon/cpu.c +0 -7
index 5b9bb3fe83..17785e2921 100644
@@ -332,13 +332,6 @@ static void hexagon_cpu_realize(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     HexagonCPUClass *mcc = HEXAGON_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     gdb_register_coprocessor(cs, hexagon_hvx_gdb_read_register,
                              hexagon_hvx_gdb_write_register,
target/hppa/cpu.c +2 -13
index b0d106b6c7..a87028b275 100644
@@ -121,22 +121,11 @@ void hppa_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
 
 static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
 {
-    CPUState *cs = CPU(dev);
     HPPACPUClass *acc = HPPA_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
 #ifndef CONFIG_USER_ONLY
-    {
-        HPPACPU *cpu = HPPA_CPU(cs);
-        cpu->alarm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                        hppa_cpu_alarm_timer, cpu);
-    }
+    cpu->alarm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                    hppa_cpu_alarm_timer, HPPA_CPU(dev));
 #endif
 
     acc->parent_realize(dev, errp);
target/i386/cpu.c +0 -6
index 2884733397..c170e2976b 100644
@@ -7311,12 +7311,6 @@ static void x86_cpu_realizefn(DeviceState *dev, Error **errp)
         return;
     }
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
     if (xcc->host_cpuid_required && !accel_uses_host_cpuid()) {
         g_autofree char *name = x86_cpu_class_get_model_name(xcc);
         error_setg(&local_err, "CPU model '%s' requires KVM or HVF", name);
target/i386/kvm/kvm-cpu.c +2 -2
index 1fe62ce176..0f52649779 100644
@@ -34,10 +34,10 @@ static bool kvm_cpu_realizefn(CPUState *cs, Error **errp)
      *
      * x86_cpu_realize():
      *  -> x86_cpu_expand_features()
-     *  -> cpu_exec_realizefn():
+     *  -> cpu_common_realizefn()
+     *      -> cpu_exec_realizefn():
      *            -> accel_cpu_realizefn()
      *               kvm_cpu_realizefn() -> host_cpu_realizefn()
-     *  -> cpu_common_realizefn()
      *  -> check/update ucode_rev, phys_bits, mwait
      */
     if (cpu->max_features) {
target/loongarch/cpu.c +0 -7
index dc0ac39833..d61dcaebca 100644
@@ -555,13 +555,6 @@ static void loongarch_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     LoongArchCPUClass *lacc = LOONGARCH_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     loongarch_cpu_register_gdb_regs_for_features(cs);
 
target/m68k/cpu.c +0 -7
index 3da316bc30..c6740e0e78 100644
@@ -309,16 +309,9 @@ static void m68k_cpu_realizefn(DeviceState *dev, Error **errp)
     CPUState *cs = CPU(dev);
     M68kCPU *cpu = M68K_CPU(dev);
     M68kCPUClass *mcc = M68K_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
 
     register_m68k_insns(&cpu->env);
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
     m68k_cpu_init_gdb(cpu);
 
     mcc->parent_realize(dev, errp);
target/microblaze/cpu.c +0 -7
index 1f19a6e07d..5194911ad4 100644
@@ -207,13 +207,6 @@ static void mb_cpu_realizefn(DeviceState *dev, Error **errp)
     uint8_t version_code = 0;
     const char *version;
     int i = 0;
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     if (cpu->cfg.addr_size < 32 || cpu->cfg.addr_size > 64) {
         error_setg(errp, "addr-size %d is out of range (32 - 64)",
target/mips/cpu.c +0 -7
index 7c81e6c356..4f15dcea44 100644
@@ -462,13 +462,6 @@ static void mips_cpu_realizefn(DeviceState *dev, Error **errp)
     MIPSCPU *cpu = MIPS_CPU(dev);
     CPUMIPSState *env = &cpu->env;
     MIPSCPUClass *mcc = MIPS_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     env->exception_base = (int32_t)0xBFC00000;
 
target/nios2/cpu.c +0 -7
index f500ca7ba2..fc753bb1be 100644
@@ -199,13 +199,6 @@ static void nios2_cpu_realizefn(DeviceState *dev, Error **errp)
     CPUState *cs = CPU(dev);
     Nios2CPU *cpu = NIOS2_CPU(cs);
     Nios2CPUClass *ncc = NIOS2_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     realize_cr_status(cs);
 
target/openrisc/cpu.c +0 -7
index e4ec95ca7f..438146c681 100644
@@ -133,13 +133,6 @@ static void openrisc_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     OpenRISCCPUClass *occ = OPENRISC_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     occ->parent_realize(dev, errp);
     cpu_reset(cs);
target/ppc/cpu_init.c +0 -5
index 24d4e8fa7e..99087ee57c 100644
@@ -6809,11 +6809,6 @@ static void ppc_cpu_realize(DeviceState *dev, Error **errp)
     PowerPCCPUClass *pcc = POWERPC_CPU_GET_CLASS(cpu);
     Error *local_err = NULL;
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
     if (cpu->vcpu_id == UNASSIGNED_CPU_INDEX) {
         cpu->vcpu_id = cs->cpu_index;
     }
target/riscv/cpu.c +0 -6
index 4f7ae55359..62be6d88fc 100644
@@ -1503,12 +1503,6 @@ static void riscv_cpu_realize(DeviceState *dev, Error **errp)
     RISCVCPUClass *mcc = RISCV_CPU_GET_CLASS(dev);
     Error *local_err = NULL;
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
     if (tcg_enabled()) {
         riscv_cpu_realize_tcg(dev, &local_err);
         if (local_err != NULL) {
target/rx/cpu.c +0 -7
index 089df61790..db951ff988 100644
@@ -129,13 +129,6 @@ static void rx_cpu_realize(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     RXCPUClass *rcc = RX_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     rcc->parent_realize(dev, errp);
     cpu_reset(cs);
target/s390x/cpu.c +0 -5
index 983dbfe563..e305928651 100644
@@ -231,11 +231,6 @@ static void s390_cpu_realizefn(DeviceState *dev, Error **errp)
     S390CPUClass *scc = S390_CPU_GET_CLASS(dev);
     Error *err = NULL;
 
-    cpu_exec_realizefn(cs, &err);
-    if (err != NULL) {
-        goto out;
-    }
-
 #if !defined(CONFIG_USER_ONLY)
     qemu_register_reset(s390_cpu_machine_reset_cb, S390_CPU(dev));
 #endif
target/sh4/cpu.c +0 -7
index e6690daf9a..a3fc034ea5 100644
@@ -220,13 +220,6 @@ static void superh_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     SuperHCPUClass *scc = SUPERH_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     scc->parent_realize(dev, errp);
     cpu_reset(cs);
target/sparc/cpu.c +0 -8
index 88157fcd33..f0b2187f3b 100644
@@ -750,18 +750,10 @@ static ObjectClass *sparc_cpu_class_by_name(const char *cpu_model)
 
 static void sparc_cpu_realizefn(DeviceState *dev, Error **errp)
 {
-    CPUState *cs = CPU(dev);
     SPARCCPUClass *scc = SPARC_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
     SPARCCPU *cpu = SPARC_CPU(dev);
     CPUSPARCState *env = &cpu->env;
 
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
-
 #if defined(CONFIG_USER_ONLY)
     if ((env->def.features & CPU_FEATURE_FLOAT)) {
         env->def.features |= CPU_FEATURE_FLOAT128;
target/tricore/cpu.c +0 -7
index 0142cf556d..5319a6841e 100644
@@ -95,13 +95,6 @@ static void tricore_cpu_realizefn(DeviceState *dev, Error **errp)
     TriCoreCPU *cpu = TRICORE_CPU(dev);
     TriCoreCPUClass *tcc = TRICORE_CPU_GET_CLASS(dev);
     CPUTriCoreState *env = &cpu->env;
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     /* Some features automatically imply others */
     if (tricore_has_feature(env, TRICORE_FEATURE_162)) {
target/xtensa/cpu.c +0 -7
index bbfd2d42a8..c7bdd0980a 100644
@@ -160,13 +160,6 @@ static void xtensa_cpu_realizefn(DeviceState *dev, Error **errp)
 {
     CPUState *cs = CPU(dev);
     XtensaCPUClass *xcc = XTENSA_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
     cs->gdb_num_regs = xcc->config->gdb_regmap.num_regs;
 
Signature · 1 line
2.41.0
#51 lore raw
Quoted 16 lines · Philippe Mathieu-Daudé

+++ b/target/hppa/cpu.c

target/hppa/cpu.c +0 -8
@@ -121,22 +121,11 @@ void hppa_cpu_do_unaligned_access(CPUState *cs, vaddr addr,
 
  static void hppa_cpu_realizefn(DeviceState *dev, Error **errp)
  {
-    CPUState *cs = CPU(dev);
      HPPACPUClass *acc = HPPA_CPU_GET_CLASS(dev);
-    Error *local_err = NULL;
-
-    cpu_exec_realizefn(cs, &local_err);
-    if (local_err != NULL) {
-        error_propagate(errp, local_err);
-        return;
-    }
 
  #ifndef CONFIG_USER_ONLY

target/hppa/cpu.c void hppa_cpu_do_unaligned_access(CPUState *cs, vaddr addr, · line 131

Quoted 10 lines
target/hppa/cpu.c +2 -5
-    {
-        HPPACPU *cpu = HPPA_CPU(cs);
-        cpu->alarm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
-                                        hppa_cpu_alarm_timer, cpu);
-    }
+    cpu->alarm_timer = timer_new_ns(QEMU_CLOCK_VIRTUAL,
+                                    hppa_cpu_alarm_timer, HPPA_CPU(dev));
  #endif
 
      acc->parent_realize(dev, errp);

Quoting one example.

In a previous patch you moved cpu_exec_realizefn() earlier, now you're moving it later, into parent_realize(). I think the previous patch needs adjustment.

r~