diff mbox series

[v4,1/8] qdev: Implement qdev_create_fake_machine() for user emulation

Message ID 20250102211800.79235-2-philmd@linaro.org
State New
Headers show
Series QOM: container_get() removal | expand

Commit Message

Philippe Mathieu-Daudé Jan. 2, 2025, 9:17 p.m. UTC
When a QDev instance is realized, qdev_get_machine() ends up called.
In the next commit, qdev_get_machine() will require a "machine"
container to be always present. To satisfy this QOM containers design,
Implement qdev_create_fake_machine() which creates a fake "machine"
container for user emulation.

On system emulation, qemu_create_machine() is called from qemu_init().
For user emulation, since the TCG accelerator always calls
tcg_init_machine(), we use it to hook our fake machine creation.

Suggested-by: Peter Xu <peterx@redhat.com>
Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
---
 include/hw/qdev-core.h | 10 ++++++++++
 accel/tcg/tcg-all.c    |  8 +++++++-
 hw/core/qdev-user.c    | 21 +++++++++++++++++++++
 hw/core/meson.build    |  1 +
 4 files changed, 39 insertions(+), 1 deletion(-)
 create mode 100644 hw/core/qdev-user.c

Comments

Peter Xu Jan. 2, 2025, 10:05 p.m. UTC | #1
On Thu, Jan 02, 2025 at 10:17:53PM +0100, Philippe Mathieu-Daudé wrote:
> When a QDev instance is realized, qdev_get_machine() ends up called.
> In the next commit, qdev_get_machine() will require a "machine"
> container to be always present. To satisfy this QOM containers design,
> Implement qdev_create_fake_machine() which creates a fake "machine"
> container for user emulation.
> 
> On system emulation, qemu_create_machine() is called from qemu_init().
> For user emulation, since the TCG accelerator always calls
> tcg_init_machine(), we use it to hook our fake machine creation.
> 
> Suggested-by: Peter Xu <peterx@redhat.com>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>

Acked-by: Peter Xu <peterx@redhat.com>
Richard Henderson Jan. 3, 2025, 2:24 p.m. UTC | #2
On 1/2/25 13:17, Philippe Mathieu-Daudé wrote:
> When a QDev instance is realized, qdev_get_machine() ends up called.
> In the next commit, qdev_get_machine() will require a "machine"
> container to be always present. To satisfy this QOM containers design,
> Implement qdev_create_fake_machine() which creates a fake "machine"
> container for user emulation.
> 
> On system emulation, qemu_create_machine() is called from qemu_init().
> For user emulation, since the TCG accelerator always calls
> tcg_init_machine(), we use it to hook our fake machine creation.
> 
> Suggested-by: Peter Xu <peterx@redhat.com>
> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
> ---
>   include/hw/qdev-core.h | 10 ++++++++++
>   accel/tcg/tcg-all.c    |  8 +++++++-
>   hw/core/qdev-user.c    | 21 +++++++++++++++++++++
>   hw/core/meson.build    |  1 +
>   4 files changed, 39 insertions(+), 1 deletion(-)
>   create mode 100644 hw/core/qdev-user.c
> 
> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
> index e6ef80b7fd0..b83b1439968 100644
> --- a/include/hw/qdev-core.h
> +++ b/include/hw/qdev-core.h
> @@ -1027,6 +1027,16 @@ const char *qdev_fw_name(DeviceState *dev);
>   void qdev_assert_realized_properly(void);
>   Object *qdev_get_machine(void);
>   
> +/**
> + * qdev_create_fake_machine(): Create a fake machine container.
> + *
> + * .. note::
> + *    This function is a kludge for user emulation (USER_ONLY)
> + *    because when thread (TYPE_CPU) are realized, qdev_realize()
> + *    access a machine container.
> + */
> +Object *qdev_create_fake_machine(void);
> +
>   /**
>    * qdev_get_human_name() - Return a human-readable name for a device
>    * @dev: The device. Must be a valid and non-NULL pointer.
> diff --git a/accel/tcg/tcg-all.c b/accel/tcg/tcg-all.c
> index c2565758876..95adaacee82 100644
> --- a/accel/tcg/tcg-all.c
> +++ b/accel/tcg/tcg-all.c
> @@ -35,7 +35,9 @@
>   #include "qemu/atomic.h"
>   #include "qapi/qapi-builtin-visit.h"
>   #include "qemu/units.h"
> -#if !defined(CONFIG_USER_ONLY)
> +#if defined(CONFIG_USER_ONLY)
> +#include "hw/qdev-core.h"
> +#else
>   #include "hw/boards.h"
>   #endif
>   #include "internal-common.h"
> @@ -124,6 +126,10 @@ static int tcg_init_machine(MachineState *ms)
>       tcg_prologue_init();
>   #endif
>   
> +#ifdef CONFIG_USER_ONLY
> +    qdev_create_fake_machine();
> +#endif

No need to return the fake machine, it seems.  With that,

Reviewed-by: Richard Henderson <richard.henderson@linaro.org>


r~
Philippe Mathieu-Daudé Jan. 3, 2025, 2:37 p.m. UTC | #3
On 3/1/25 15:24, Richard Henderson wrote:
> On 1/2/25 13:17, Philippe Mathieu-Daudé wrote:
>> When a QDev instance is realized, qdev_get_machine() ends up called.
>> In the next commit, qdev_get_machine() will require a "machine"
>> container to be always present. To satisfy this QOM containers design,
>> Implement qdev_create_fake_machine() which creates a fake "machine"
>> container for user emulation.
>>
>> On system emulation, qemu_create_machine() is called from qemu_init().
>> For user emulation, since the TCG accelerator always calls
>> tcg_init_machine(), we use it to hook our fake machine creation.
>>
>> Suggested-by: Peter Xu <peterx@redhat.com>
>> Signed-off-by: Philippe Mathieu-Daudé <philmd@linaro.org>
>> ---
>>   include/hw/qdev-core.h | 10 ++++++++++
>>   accel/tcg/tcg-all.c    |  8 +++++++-
>>   hw/core/qdev-user.c    | 21 +++++++++++++++++++++
>>   hw/core/meson.build    |  1 +
>>   4 files changed, 39 insertions(+), 1 deletion(-)
>>   create mode 100644 hw/core/qdev-user.c
>>
>> diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
>> index e6ef80b7fd0..b83b1439968 100644
>> --- a/include/hw/qdev-core.h
>> +++ b/include/hw/qdev-core.h
>> @@ -1027,6 +1027,16 @@ const char *qdev_fw_name(DeviceState *dev);
>>   void qdev_assert_realized_properly(void);
>>   Object *qdev_get_machine(void);
>> +/**
>> + * qdev_create_fake_machine(): Create a fake machine container.
>> + *
>> + * .. note::
>> + *    This function is a kludge for user emulation (USER_ONLY)
>> + *    because when thread (TYPE_CPU) are realized, qdev_realize()
>> + *    access a machine container.
>> + */
>> +Object *qdev_create_fake_machine(void);
>> +
>>   /**
>>    * qdev_get_human_name() - Return a human-readable name for a device
>>    * @dev: The device. Must be a valid and non-NULL pointer.
>> diff --git a/accel/tcg/tcg-all.c b/accel/tcg/tcg-all.c
>> index c2565758876..95adaacee82 100644
>> --- a/accel/tcg/tcg-all.c
>> +++ b/accel/tcg/tcg-all.c
>> @@ -35,7 +35,9 @@
>>   #include "qemu/atomic.h"
>>   #include "qapi/qapi-builtin-visit.h"
>>   #include "qemu/units.h"
>> -#if !defined(CONFIG_USER_ONLY)
>> +#if defined(CONFIG_USER_ONLY)
>> +#include "hw/qdev-core.h"
>> +#else
>>   #include "hw/boards.h"
>>   #endif
>>   #include "internal-common.h"
>> @@ -124,6 +126,10 @@ static int tcg_init_machine(MachineState *ms)
>>       tcg_prologue_init();
>>   #endif
>> +#ifdef CONFIG_USER_ONLY
>> +    qdev_create_fake_machine();
>> +#endif
> 
> No need to return the fake machine, it seems.

My first reasoning was about avoiding ASan leak warnings, planning
to release that container on exit(), but I'm clearly over-engineering
what is meant to be a kludge.

>  With that,
> 
> Reviewed-by: Richard Henderson <richard.henderson@linaro.org>

I'll remove and merge directly, thanks!
diff mbox series

Patch

diff --git a/include/hw/qdev-core.h b/include/hw/qdev-core.h
index e6ef80b7fd0..b83b1439968 100644
--- a/include/hw/qdev-core.h
+++ b/include/hw/qdev-core.h
@@ -1027,6 +1027,16 @@  const char *qdev_fw_name(DeviceState *dev);
 void qdev_assert_realized_properly(void);
 Object *qdev_get_machine(void);
 
+/**
+ * qdev_create_fake_machine(): Create a fake machine container.
+ *
+ * .. note::
+ *    This function is a kludge for user emulation (USER_ONLY)
+ *    because when thread (TYPE_CPU) are realized, qdev_realize()
+ *    access a machine container.
+ */
+Object *qdev_create_fake_machine(void);
+
 /**
  * qdev_get_human_name() - Return a human-readable name for a device
  * @dev: The device. Must be a valid and non-NULL pointer.
diff --git a/accel/tcg/tcg-all.c b/accel/tcg/tcg-all.c
index c2565758876..95adaacee82 100644
--- a/accel/tcg/tcg-all.c
+++ b/accel/tcg/tcg-all.c
@@ -35,7 +35,9 @@ 
 #include "qemu/atomic.h"
 #include "qapi/qapi-builtin-visit.h"
 #include "qemu/units.h"
-#if !defined(CONFIG_USER_ONLY)
+#if defined(CONFIG_USER_ONLY)
+#include "hw/qdev-core.h"
+#else
 #include "hw/boards.h"
 #endif
 #include "internal-common.h"
@@ -124,6 +126,10 @@  static int tcg_init_machine(MachineState *ms)
     tcg_prologue_init();
 #endif
 
+#ifdef CONFIG_USER_ONLY
+    qdev_create_fake_machine();
+#endif
+
     return 0;
 }
 
diff --git a/hw/core/qdev-user.c b/hw/core/qdev-user.c
new file mode 100644
index 00000000000..f816340db5a
--- /dev/null
+++ b/hw/core/qdev-user.c
@@ -0,0 +1,21 @@ 
+/*
+ * QDev helpers specific to user emulation.
+ *
+ * Copyright 2025 Linaro, Ltd.
+ *
+ * SPDX-License-Identifier: GPL-2.0-or-later
+ */
+#include "qemu/osdep.h"
+#include "qom/object.h"
+#include "hw/qdev-core.h"
+
+Object *qdev_create_fake_machine(void)
+{
+    Object *fake_machine_obj;
+
+    fake_machine_obj = object_property_add_new_container(object_get_root(),
+                                                         "machine");
+    object_property_add_new_container(fake_machine_obj, "unattached");
+
+    return fake_machine_obj;
+}
diff --git a/hw/core/meson.build b/hw/core/meson.build
index ce9dfa3f4bf..65a1698ed1f 100644
--- a/hw/core/meson.build
+++ b/hw/core/meson.build
@@ -46,3 +46,4 @@  system_ss.add(files(
   'vm-change-state-handler.c',
   'clock-vmstate.c',
 ))
+user_ss.add(files('qdev-user.c'))