2 * Dynamic device configuration and creation.
4 * Copyright (c) 2009 CodeSourcery
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
20 #include "qemu/osdep.h"
21 #include "hw/sysbus.h"
22 #include "monitor/hmp.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "sysemu/arch_init.h"
26 #include "qapi/error.h"
27 #include "qapi/qapi-commands-qdev.h"
28 #include "qapi/qmp/dispatch.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qstring.h"
32 #include "qapi/qobject-input-visitor.h"
33 #include "qemu/config-file.h"
34 #include "qemu/error-report.h"
35 #include "qemu/help_option.h"
36 #include "qemu/option.h"
37 #include "qemu/qemu-print.h"
38 #include "qemu/option_int.h"
39 #include "sysemu/block-backend.h"
40 #include "migration/misc.h"
41 #include "migration/migration.h"
42 #include "qemu/cutils.h"
43 #include "hw/qdev-properties.h"
47 * Aliases were a bad idea from the start. Let's keep them
48 * from spreading further.
50 typedef struct QDevAlias
57 /* default virtio transport per architecture */
58 #define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | QEMU_ARCH_ARM | \
59 QEMU_ARCH_HPPA | QEMU_ARCH_I386 | \
60 QEMU_ARCH_MIPS | QEMU_ARCH_PPC | \
61 QEMU_ARCH_RISCV | QEMU_ARCH_SH4 | \
62 QEMU_ARCH_SPARC | QEMU_ARCH_XTENSA)
63 #define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X)
64 #define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K)
66 /* Please keep this table sorted by typename. */
67 static const QDevAlias qdev_alias_table
[] = {
68 { "AC97", "ac97" }, /* -soundhw name */
69 { "e1000", "e1000-82540em" },
70 { "ES1370", "es1370" }, /* -soundhw name */
71 { "ich9-ahci", "ahci" },
72 { "lsi53c895a", "lsi" },
73 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO
},
74 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW
},
75 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI
},
76 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO
},
77 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW
},
78 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI
},
79 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO
},
80 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW
},
81 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI
},
82 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO
},
83 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW
},
84 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI
},
85 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO
},
86 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW
},
87 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI
},
88 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI
},
89 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO
},
90 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW
},
91 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI
},
92 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO
},
93 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW
},
94 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI
},
95 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO
},
96 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW
},
97 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI
},
98 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO
},
99 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW
},
100 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI
},
101 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO
},
102 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW
},
103 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI
},
104 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO
},
105 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW
},
106 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI
},
107 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO
},
108 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW
},
109 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI
},
113 static const char *qdev_class_get_alias(DeviceClass
*dc
)
115 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
118 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
119 if (qdev_alias_table
[i
].arch_mask
&&
120 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
124 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
125 return qdev_alias_table
[i
].alias
;
132 static bool qdev_class_has_alias(DeviceClass
*dc
)
134 return (qdev_class_get_alias(dc
) != NULL
);
137 static void qdev_print_devinfo(DeviceClass
*dc
)
139 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
141 qemu_printf(", bus %s", dc
->bus_type
);
143 if (qdev_class_has_alias(dc
)) {
144 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
147 qemu_printf(", desc \"%s\"", dc
->desc
);
149 if (!dc
->user_creatable
) {
150 qemu_printf(", no-user");
155 static void qdev_print_devinfos(bool show_no_user
)
157 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
158 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
159 [DEVICE_CATEGORY_USB
] = "USB",
160 [DEVICE_CATEGORY_STORAGE
] = "Storage",
161 [DEVICE_CATEGORY_NETWORK
] = "Network",
162 [DEVICE_CATEGORY_INPUT
] = "Input",
163 [DEVICE_CATEGORY_DISPLAY
] = "Display",
164 [DEVICE_CATEGORY_SOUND
] = "Sound",
165 [DEVICE_CATEGORY_MISC
] = "Misc",
166 [DEVICE_CATEGORY_CPU
] = "CPU",
167 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
173 module_load_qom_all();
174 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
176 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
178 for (elt
= list
; elt
; elt
= elt
->next
) {
179 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
181 if ((i
< DEVICE_CATEGORY_MAX
182 ? !test_bit(i
, dc
->categories
)
183 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
185 && !dc
->user_creatable
)) {
189 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
192 qdev_print_devinfo(dc
);
199 static const char *find_typename_by_alias(const char *alias
)
203 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
204 if (qdev_alias_table
[i
].arch_mask
&&
205 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
209 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
210 return qdev_alias_table
[i
].typename
;
217 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
221 const char *original_name
= *driver
;
223 oc
= module_object_class_by_name(*driver
);
225 const char *typename
= find_typename_by_alias(*driver
);
229 oc
= module_object_class_by_name(*driver
);
233 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
234 if (*driver
!= original_name
) {
235 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
236 " name", original_name
, *driver
);
238 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
243 if (object_class_is_abstract(oc
)) {
244 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
245 "a non-abstract device type");
249 dc
= DEVICE_CLASS(oc
);
250 if (!dc
->user_creatable
||
251 (phase_check(PHASE_MACHINE_READY
) && !dc
->hotpluggable
)) {
252 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
253 "a pluggable device type");
261 int qdev_device_help(QemuOpts
*opts
)
263 Error
*local_err
= NULL
;
265 ObjectPropertyInfoList
*prop_list
;
266 ObjectPropertyInfoList
*prop
;
270 driver
= qemu_opt_get(opts
, "driver");
271 if (driver
&& is_help_option(driver
)) {
272 qdev_print_devinfos(false);
276 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
280 if (!object_class_by_name(driver
)) {
281 const char *typename
= find_typename_by_alias(driver
);
288 prop_list
= qmp_device_list_properties(driver
, &local_err
);
294 qemu_printf("%s options:\n", driver
);
296 qemu_printf("There are no options for %s.\n", driver
);
298 array
= g_ptr_array_new();
299 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
300 g_ptr_array_add(array
,
301 object_property_help(prop
->value
->name
,
303 prop
->value
->default_value
,
304 prop
->value
->description
));
306 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
307 for (i
= 0; i
< array
->len
; i
++) {
308 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
310 g_ptr_array_set_free_func(array
, g_free
);
311 g_ptr_array_free(array
, true);
312 qapi_free_ObjectPropertyInfoList(prop_list
);
316 error_report_err(local_err
);
320 static Object
*qdev_get_peripheral(void)
325 dev
= container_get(qdev_get_machine(), "/peripheral");
331 static Object
*qdev_get_peripheral_anon(void)
336 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
342 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
346 const char *sep
= " ";
348 error_append_hint(errp
, "child buses at \"%s\":",
349 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
350 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
351 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
354 error_append_hint(errp
, "\n");
357 static void qbus_error_append_dev_list_hint(BusState
*bus
,
361 const char *sep
= " ";
363 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
364 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
365 DeviceState
*dev
= kid
->child
;
366 error_append_hint(errp
, "%s\"%s\"", sep
,
367 object_get_typename(OBJECT(dev
)));
369 error_append_hint(errp
, "/\"%s\"", dev
->id
);
373 error_append_hint(errp
, "\n");
376 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
380 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
381 if (strcmp(child
->name
, elem
) == 0) {
388 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
393 * try to match in order:
394 * (1) instance id, if present
396 * (3) driver alias, if present
398 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
399 DeviceState
*dev
= kid
->child
;
400 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
404 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
405 DeviceState
*dev
= kid
->child
;
406 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
410 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
411 DeviceState
*dev
= kid
->child
;
412 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
414 if (qdev_class_has_alias(dc
) &&
415 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
422 static inline bool qbus_is_full(BusState
*bus
)
429 bus_class
= BUS_GET_CLASS(bus
);
430 return bus_class
->max_dev
&& bus
->num_children
>= bus_class
->max_dev
;
434 * Search the tree rooted at @bus for a bus.
435 * If @name, search for a bus with that name. Note that bus names
436 * need not be unique. Yes, that's screwed up.
437 * Else search for a bus that is a subtype of @bus_typename.
438 * If more than one exists, prefer one that can take another device.
439 * Return the bus if found, else %NULL.
441 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
442 const char *bus_typename
)
445 BusState
*pick
, *child
, *ret
;
448 assert(name
|| bus_typename
);
450 match
= !strcmp(bus
->name
, name
);
452 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
455 if (match
&& !qbus_is_full(bus
)) {
456 return bus
; /* root matches and isn't full */
459 pick
= match
? bus
: NULL
;
461 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
462 DeviceState
*dev
= kid
->child
;
463 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
464 ret
= qbus_find_recursive(child
, name
, bus_typename
);
465 if (ret
&& !qbus_is_full(ret
)) {
466 return ret
; /* a descendant matches and isn't full */
474 /* root or a descendant matches, but is full */
478 static BusState
*qbus_find(const char *path
, Error
**errp
)
485 /* find start element */
486 if (path
[0] == '/') {
487 bus
= sysbus_get_default();
490 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
494 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
496 error_setg(errp
, "Bus '%s' not found", elem
);
503 assert(path
[pos
] == '/' || !path
[pos
]);
504 while (path
[pos
] == '/') {
507 if (path
[pos
] == '\0') {
512 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
513 g_assert_not_reached();
517 dev
= qbus_find_dev(bus
, elem
);
519 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
520 "Device '%s' not found", elem
);
521 qbus_error_append_dev_list_hint(bus
, errp
);
525 assert(path
[pos
] == '/' || !path
[pos
]);
526 while (path
[pos
] == '/') {
529 if (path
[pos
] == '\0') {
530 /* last specified element is a device. If it has exactly
531 * one child bus accept it nevertheless */
532 if (dev
->num_child_bus
== 1) {
533 bus
= QLIST_FIRST(&dev
->child_bus
);
536 if (dev
->num_child_bus
) {
537 error_setg(errp
, "Device '%s' has multiple child buses",
539 qbus_error_append_bus_list_hint(dev
, errp
);
541 error_setg(errp
, "Device '%s' has no child bus", elem
);
547 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
548 g_assert_not_reached();
552 bus
= qbus_find_bus(dev
, elem
);
554 error_setg(errp
, "Bus '%s' not found", elem
);
555 qbus_error_append_bus_list_hint(dev
, errp
);
560 if (qbus_is_full(bus
)) {
561 error_setg(errp
, "Bus '%s' is full", path
);
567 /* Takes ownership of @id, will be freed when deleting the device */
568 const char *qdev_set_id(DeviceState
*dev
, char *id
, Error
**errp
)
570 ObjectProperty
*prop
;
572 assert(!dev
->id
&& !dev
->realized
);
575 * object_property_[try_]add_child() below will assert the device
579 prop
= object_property_try_add_child(qdev_get_peripheral(), id
,
585 error_setg(errp
, "Duplicate device ID '%s'", id
);
589 static int anon_count
;
590 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
591 prop
= object_property_add_child(qdev_get_peripheral_anon(), name
,
599 DeviceState
*qdev_device_add_from_qdict(const QDict
*opts
,
600 bool from_json
, Error
**errp
)
604 const char *driver
, *path
;
606 DeviceState
*dev
= NULL
;
607 BusState
*bus
= NULL
;
609 driver
= qdict_get_try_str(opts
, "driver");
611 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
616 dc
= qdev_get_device_class(&driver
, errp
);
622 path
= qdict_get_try_str(opts
, "bus");
624 bus
= qbus_find(path
, errp
);
628 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
629 error_setg(errp
, "Device '%s' can't go on %s bus",
630 driver
, object_get_typename(OBJECT(bus
)));
633 } else if (dc
->bus_type
!= NULL
) {
634 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
635 if (!bus
|| qbus_is_full(bus
)) {
636 error_setg(errp
, "No '%s' bus found for device '%s'",
637 dc
->bus_type
, driver
);
642 if (qdev_should_hide_device(opts
, from_json
, errp
)) {
643 if (bus
&& !qbus_is_hotpluggable(bus
)) {
644 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
651 if (phase_check(PHASE_MACHINE_READY
) && bus
&& !qbus_is_hotpluggable(bus
)) {
652 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
656 if (!migration_is_idle()) {
657 error_setg(errp
, "device_add not allowed while migrating");
662 dev
= qdev_new(driver
);
664 /* Check whether the hotplug is allowed by the machine */
665 if (phase_check(PHASE_MACHINE_READY
)) {
666 if (!qdev_hotplug_allowed(dev
, errp
)) {
670 if (!bus
&& !qdev_get_machine_hotplug_handler(dev
)) {
671 /* No bus, no machine hotplug handler --> device is not hotpluggable */
672 error_setg(errp
, "Device '%s' can not be hotplugged on this machine",
679 * set dev's parent and register its id.
680 * If it fails it means the id is already taken.
682 id
= g_strdup(qdict_get_try_str(opts
, "id"));
683 if (!qdev_set_id(dev
, id
, errp
)) {
688 dev
->opts
= qdict_clone_shallow(opts
);
689 qdict_del(dev
->opts
, "driver");
690 qdict_del(dev
->opts
, "bus");
691 qdict_del(dev
->opts
, "id");
693 object_set_properties_from_keyval(&dev
->parent_obj
, dev
->opts
, from_json
,
699 if (!qdev_realize(DEVICE(dev
), bus
, errp
)) {
706 object_unparent(OBJECT(dev
));
707 object_unref(OBJECT(dev
));
712 /* Takes ownership of @opts on success */
713 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
715 QDict
*qdict
= qemu_opts_to_qdict(opts
, NULL
);
718 ret
= qdev_device_add_from_qdict(qdict
, false, errp
);
722 qobject_unref(qdict
);
726 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
727 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
729 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
734 for (; props
->name
; props
++) {
736 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
738 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
739 value
= object_property_get_str(OBJECT(dev
), legacy_name
, NULL
);
741 value
= object_property_print(OBJECT(dev
), props
->name
, true,
749 qdev_printf("%s = %s\n", props
->name
,
750 *value
? value
: "<null>");
755 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
757 BusClass
*bc
= BUS_GET_CLASS(bus
);
760 bc
->print_dev(mon
, dev
, indent
);
764 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
771 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
772 dev
->id
? dev
->id
: "");
774 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
776 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
780 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
784 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
785 g_autofree
char *freq_str
= clock_display_freq(ncl
->clock
);
786 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
787 ncl
->output
? "out" : "in",
788 ncl
->alias
? " (alias)" : "",
789 ncl
->name
, freq_str
);
791 class = object_get_class(OBJECT(dev
));
793 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
794 class = object_class_get_parent(class);
795 } while (class != object_class_by_name(TYPE_DEVICE
));
796 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
797 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
798 qbus_print(mon
, child
, indent
);
802 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
806 qdev_printf("bus: %s\n", bus
->name
);
808 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
809 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
810 DeviceState
*dev
= kid
->child
;
811 qdev_print(mon
, dev
, indent
);
816 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
818 if (sysbus_get_default())
819 qbus_print(mon
, sysbus_get_default(), 0);
822 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
824 qdev_print_devinfos(true);
827 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
832 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, errp
);
836 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
840 dev
= qdev_device_add(opts
, errp
);
843 * Drain all pending RCU callbacks. This is done because
844 * some bus related operations can delay a device removal
845 * (in this case this can happen if device is added and then
846 * removed due to a configuration error)
847 * to a RCU callback, but user might expect that this interface
848 * will finish its job completely once qmp command returns result
857 object_unref(OBJECT(dev
));
860 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
866 obj
= object_resolve_path(id
, NULL
);
868 obj
= object_resolve_path_component(qdev_get_peripheral(), id
);
872 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
873 "Device '%s' not found", id
);
877 dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
879 error_setg(errp
, "%s is not a hotpluggable device", id
);
886 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
888 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
889 HotplugHandler
*hotplug_ctrl
;
890 HotplugHandlerClass
*hdc
;
891 Error
*local_err
= NULL
;
893 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
894 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
898 if (!dc
->hotpluggable
) {
899 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
900 object_get_typename(OBJECT(dev
)));
904 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
905 error_setg(errp
, "device_del not allowed while migrating");
909 qdev_hot_removed
= true;
911 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
912 /* hotpluggable device MUST have HotplugHandler, if it doesn't
913 * then something is very wrong with it */
914 g_assert(hotplug_ctrl
);
916 /* If device supports async unplug just request it to be done,
917 * otherwise just remove it synchronously */
918 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
919 if (hdc
->unplug_request
) {
920 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
922 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
924 object_unparent(OBJECT(dev
));
927 error_propagate(errp
, local_err
);
930 void qmp_device_del(const char *id
, Error
**errp
)
932 DeviceState
*dev
= find_device_state(id
, errp
);
934 if (dev
->pending_deleted_event
) {
935 error_setg(errp
, "Device %s is already in the "
936 "process of unplug", id
);
940 qdev_unplug(dev
, errp
);
944 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
948 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
949 hmp_handle_error(mon
, err
);
952 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
954 const char *id
= qdict_get_str(qdict
, "id");
957 qmp_device_del(id
, &err
);
958 hmp_handle_error(mon
, err
);
961 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
966 dev
= find_device_state(id
, errp
);
971 blk
= blk_by_dev(dev
);
973 error_setg(errp
, "Device does not have a block device backend");
978 QemuOptsList qemu_device_opts
= {
980 .implied_opt_name
= "driver",
981 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
984 * no elements => accept any
985 * sanity checking will happen later
986 * when setting device properties
988 { /* end of list */ }
992 QemuOptsList qemu_global_opts
= {
994 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
998 .type
= QEMU_OPT_STRING
,
1001 .type
= QEMU_OPT_STRING
,
1004 .type
= QEMU_OPT_STRING
,
1006 { /* end of list */ }
1010 int qemu_global_option(const char *str
)
1012 char driver
[64], property
[64];
1016 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
1017 if (rc
== 2 && str
[offset
] == '=') {
1018 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
1019 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
1020 qemu_opt_set(opts
, "property", property
, &error_abort
);
1021 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
1025 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);
1033 bool qmp_command_available(const QmpCommand
*cmd
, Error
**errp
)
1035 if (!phase_check(PHASE_MACHINE_READY
) &&
1036 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
1037 error_setg(errp
, "The command '%s' is permitted only after machine initialization has completed",