@samitouri / QOSamiQemu / commits / b0c9d8c126

hw/riscv: sifive_e: Don't call qdev_get_machine in soc init

Calling qdev_get_machine() in the soc_init function would result in the following assert ../hw/core/qdev.c:858: qdev_get_machine: Assertion `dev' failed. when trying to run ./qemu-system-riscv64 -S -display none -M virt -device riscv.sifive.e.soc,help as the machine wasn't created yet. We call qdev_get_machine() to obtain the number of CPUs in the machine. So instead of setting the CPU num-harts in the init function let's set it in realise where the machine will exist. Signed-off-by: Alistair Francis <alistair.francis@wdc.com> Tested-by: Markus Armbruster <armbru@redhat.com> Message-ID: <20260312043158.4191378-2-alistair.francis@wdc.com> [thuth: Fix a complaint from checkpatch.pl with regards to multi-line comment] Signed-off-by: Thomas Huth <thuth@redhat.com>

Alistair Francis committed Mar 12, 2026 at 14:31 UTC b0c9d8c126538e82f74341cae700d1cd266d64b2
1 file changed +6 -3
hw/riscv/sifive_e.c
+6 -3
@@ -178,12 +178,13 @@ type_init(sifive_e_machine_init_register_types)
178
179 static void sifive_e_soc_init(Object *obj)
180 {
181 - MachineState *ms = MACHINE(qdev_get_machine());
181 SiFiveESoCState *s = RISCV_E_SOC(obj);
182
183 object_initialize_child(obj, "cpus", &s->cpus, TYPE_RISCV_HART_ARRAY);
185 - object_property_set_int(OBJECT(&s->cpus), "num-harts", ms->smp.cpus,
186 - &error_abort);
184 + /*
185 + * Set the `num-harts` property later as the machine is potentially not
186 + * created yet.
187 + */
188 object_property_set_int(OBJECT(&s->cpus), "resetvec", 0x1004, &error_abort);
189 object_initialize_child(obj, "riscv.sifive.e.gpio0", &s->gpio,
190 TYPE_SIFIVE_GPIO);
@@ -200,6 +201,8 @@ static void sifive_e_soc_realize(DeviceState *dev, Error **errp)
201
202 object_property_set_str(OBJECT(&s->cpus), "cpu-type", ms->cpu_type,
203 &error_abort);
204 + object_property_set_int(OBJECT(&s->cpus), "num-harts", ms->smp.cpus,
205 + &error_abort);
206 sysbus_realize(SYS_BUS_DEVICE(&s->cpus), &error_fatal);
207
208 /* Mask ROM */