[PATCH] hw/arm: fsl-imx8mm: Don't call qdev_get_machine in init

Vineet Agarwal posted 1 patch 2 weeks, 5 days ago
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/qemu tags/patchew/20260511115918.32765-1-agarwal.vineet2006@gmail.com
Maintainers: Gaurav Sharma <gaurav.sharma_7@nxp.com>, Peter Maydell <peter.maydell@linaro.org>
hw/arm/fsl-imx8mm.c | 15 ++++++++-------
1 file changed, 8 insertions(+), 7 deletions(-)
[PATCH] hw/arm: fsl-imx8mm: Don't call qdev_get_machine in init
Posted by Vineet Agarwal 2 weeks, 5 days ago
Calling qdev_get_machine() from fsl_imx8mm_init() can trigger
an assertion failure because the machine may not be created yet.

Reproducer:

  ./qemu-system-aarch64 -S -display none \
      -M virt -device fsl-imx8mm,help

This hits:

../hw/core/qdev.c:844: Object *qdev_get_machine(void):
Assertion `dev' failed.

Move the CPU initialization into realize(), where accessing the
machine state is safe.

Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>
---
 hw/arm/fsl-imx8mm.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/hw/arm/fsl-imx8mm.c b/hw/arm/fsl-imx8mm.c
index 97c3f8542c..875e92bb34 100644
--- a/hw/arm/fsl-imx8mm.c
+++ b/hw/arm/fsl-imx8mm.c
@@ -157,16 +157,9 @@ static const struct {
 
 static void fsl_imx8mm_init(Object *obj)
 {
-    MachineState *ms = MACHINE(qdev_get_machine());
     FslImx8mmState *s = FSL_IMX8MM(obj);
-    const char *cpu_type = ms->cpu_type ?: ARM_CPU_TYPE_NAME("cortex-a53");
     int i;
 
-    for (i = 0; i < MIN(ms->smp.cpus, FSL_IMX8MM_NUM_CPUS); i++) {
-        g_autofree char *name = g_strdup_printf("cpu%d", i);
-        object_initialize_child(obj, name, &s->cpu[i], cpu_type);
-    }
-
     object_initialize_child(obj, "gic", &s->gic, gicv3_class_name());
 
     object_initialize_child(obj, "ccm", &s->ccm, TYPE_IMX8MP_CCM);
@@ -229,6 +222,8 @@ static void fsl_imx8mm_realize(DeviceState *dev, Error **errp)
     MachineState *ms = MACHINE(qdev_get_machine());
     FslImx8mmState *s = FSL_IMX8MM(dev);
     DeviceState *gicdev = DEVICE(&s->gic);
+    const char *cpu_type =
+        ms->cpu_type ?: ARM_CPU_TYPE_NAME("cortex-a53");
     int i;
 
     if (ms->smp.cpus > FSL_IMX8MM_NUM_CPUS) {
@@ -237,6 +232,12 @@ static void fsl_imx8mm_realize(DeviceState *dev, Error **errp)
         return;
     }
 
+    for (i = 0; i < ms->smp.cpus; i++) {
+        g_autofree char *name = g_strdup_printf("cpu%d", i);
+        object_initialize_child(OBJECT(dev), name,
+                                &s->cpu[i], cpu_type);
+    }
+
     /* CPUs */
     for (i = 0; i < ms->smp.cpus; i++) {
         /* On uniprocessor, the CBAR is set to 0 */
-- 
2.54.0
Re: [PATCH] hw/arm: fsl-imx8mm: Don't call qdev_get_machine in init
Posted by Peter Maydell 2 weeks, 5 days ago
On Mon, 11 May 2026 at 12:59, Vineet Agarwal
<agarwal.vineet2006@gmail.com> wrote:
>
> Calling qdev_get_machine() from fsl_imx8mm_init() can trigger
> an assertion failure because the machine may not be created yet.
>
> Reproducer:
>
>   ./qemu-system-aarch64 -S -display none \
>       -M virt -device fsl-imx8mm,help
>
> This hits:
>
> ../hw/core/qdev.c:844: Object *qdev_get_machine(void):
> Assertion `dev' failed.
>
> Move the CPU initialization into realize(), where accessing the
> machine state is safe.
>
> Signed-off-by: Vineet Agarwal <agarwal.vineet2006@gmail.com>

This is the same fix we put in for fsl-imx8mp under
commit b67d0bcdd. I've applied it to target-arm.next,
with a note added to the commit message about that
other commit.

thanks
-- PMM