Skip to content

Commit 558ee1e

Browse files
committed
qdev: Implement qdev_create_fake_machine() for user emulation
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 <[email protected]> Signed-off-by: Philippe Mathieu-Daudé <[email protected]> Acked-by: Peter Xu <[email protected]> Reviewed-by: Richard Henderson <[email protected]> Message-Id: <[email protected]>
1 parent ad1ea5f commit 558ee1e

File tree

4 files changed

+37
-1
lines changed

4 files changed

+37
-1
lines changed

accel/tcg/tcg-all.c

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,9 @@
3535
#include "qemu/atomic.h"
3636
#include "qapi/qapi-builtin-visit.h"
3737
#include "qemu/units.h"
38-
#if !defined(CONFIG_USER_ONLY)
38+
#if defined(CONFIG_USER_ONLY)
39+
#include "hw/qdev-core.h"
40+
#else
3941
#include "hw/boards.h"
4042
#endif
4143
#include "internal-common.h"
@@ -124,6 +126,10 @@ static int tcg_init_machine(MachineState *ms)
124126
tcg_prologue_init();
125127
#endif
126128

129+
#ifdef CONFIG_USER_ONLY
130+
qdev_create_fake_machine();
131+
#endif
132+
127133
return 0;
128134
}
129135

hw/core/meson.build

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,3 +46,4 @@ system_ss.add(files(
4646
'vm-change-state-handler.c',
4747
'clock-vmstate.c',
4848
))
49+
user_ss.add(files('qdev-user.c'))

hw/core/qdev-user.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* QDev helpers specific to user emulation.
3+
*
4+
* Copyright 2025 Linaro, Ltd.
5+
*
6+
* SPDX-License-Identifier: GPL-2.0-or-later
7+
*/
8+
#include "qemu/osdep.h"
9+
#include "qom/object.h"
10+
#include "hw/qdev-core.h"
11+
12+
void qdev_create_fake_machine(void)
13+
{
14+
Object *fake_machine_obj;
15+
16+
fake_machine_obj = object_property_add_new_container(object_get_root(),
17+
"machine");
18+
object_property_add_new_container(fake_machine_obj, "unattached");
19+
}

include/hw/qdev-core.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,16 @@ const char *qdev_fw_name(DeviceState *dev);
10231023
void qdev_assert_realized_properly(void);
10241024
Object *qdev_get_machine(void);
10251025

1026+
/**
1027+
* qdev_create_fake_machine(): Create a fake machine container.
1028+
*
1029+
* .. note::
1030+
* This function is a kludge for user emulation (USER_ONLY)
1031+
* because when thread (TYPE_CPU) are realized, qdev_realize()
1032+
* access a machine container.
1033+
*/
1034+
void qdev_create_fake_machine(void);
1035+
10261036
/**
10271037
* qdev_get_human_name() - Return a human-readable name for a device
10281038
* @dev: The device. Must be a valid and non-NULL pointer.

0 commit comments

Comments
 (0)