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"
45 #include "hw/boards.h"
48 * Aliases were a bad idea from the start. Let's keep them
49 * from spreading further.
51 typedef struct QDevAlias
58 /* default virtio transport per architecture */
59 #define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | QEMU_ARCH_ARM | \
60 QEMU_ARCH_HPPA | QEMU_ARCH_I386 | \
61 QEMU_ARCH_MIPS | QEMU_ARCH_PPC | \
62 QEMU_ARCH_RISCV | QEMU_ARCH_SH4 | \
63 QEMU_ARCH_SPARC | QEMU_ARCH_XTENSA)
64 #define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X)
65 #define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K)
67 /* Please keep this table sorted by typename. */
68 static const QDevAlias qdev_alias_table
[] = {
69 { "AC97", "ac97" }, /* -soundhw name */
70 { "e1000", "e1000-82540em" },
71 { "ES1370", "es1370" }, /* -soundhw name */
72 { "ich9-ahci", "ahci" },
73 { "lsi53c895a", "lsi" },
74 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO
},
75 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW
},
76 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI
},
77 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO
},
78 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW
},
79 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI
},
80 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO
},
81 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW
},
82 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI
},
83 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO
},
84 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW
},
85 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI
},
86 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO
},
87 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW
},
88 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI
},
89 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI
},
90 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO
},
91 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW
},
92 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI
},
93 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO
},
94 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW
},
95 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI
},
96 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO
},
97 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW
},
98 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI
},
99 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO
},
100 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW
},
101 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI
},
102 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO
},
103 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW
},
104 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI
},
105 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO
},
106 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW
},
107 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI
},
108 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO
},
109 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW
},
110 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI
},
114 static const char *qdev_class_get_alias(DeviceClass
*dc
)
116 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
119 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
120 if (qdev_alias_table
[i
].arch_mask
&&
121 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
125 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
126 return qdev_alias_table
[i
].alias
;
133 static bool qdev_class_has_alias(DeviceClass
*dc
)
135 return (qdev_class_get_alias(dc
) != NULL
);
138 static void qdev_print_devinfo(DeviceClass
*dc
)
140 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
142 qemu_printf(", bus %s", dc
->bus_type
);
144 if (qdev_class_has_alias(dc
)) {
145 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
148 qemu_printf(", desc \"%s\"", dc
->desc
);
150 if (!dc
->user_creatable
) {
151 qemu_printf(", no-user");
156 static void qdev_print_devinfos(bool show_no_user
)
158 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
159 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
160 [DEVICE_CATEGORY_USB
] = "USB",
161 [DEVICE_CATEGORY_STORAGE
] = "Storage",
162 [DEVICE_CATEGORY_NETWORK
] = "Network",
163 [DEVICE_CATEGORY_INPUT
] = "Input",
164 [DEVICE_CATEGORY_DISPLAY
] = "Display",
165 [DEVICE_CATEGORY_SOUND
] = "Sound",
166 [DEVICE_CATEGORY_MISC
] = "Misc",
167 [DEVICE_CATEGORY_CPU
] = "CPU",
168 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
174 module_load_qom_all();
175 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
177 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
179 for (elt
= list
; elt
; elt
= elt
->next
) {
180 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
182 if ((i
< DEVICE_CATEGORY_MAX
183 ? !test_bit(i
, dc
->categories
)
184 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
186 && !dc
->user_creatable
)) {
190 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
193 qdev_print_devinfo(dc
);
200 static const char *find_typename_by_alias(const char *alias
)
204 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
205 if (qdev_alias_table
[i
].arch_mask
&&
206 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
210 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
211 return qdev_alias_table
[i
].typename
;
218 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
222 const char *original_name
= *driver
;
224 oc
= module_object_class_by_name(*driver
);
226 const char *typename
= find_typename_by_alias(*driver
);
230 oc
= module_object_class_by_name(*driver
);
234 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
235 if (*driver
!= original_name
) {
236 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
237 " name", original_name
, *driver
);
239 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
244 if (object_class_is_abstract(oc
)) {
245 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
246 "a non-abstract device type");
250 dc
= DEVICE_CLASS(oc
);
251 if (!dc
->user_creatable
||
252 (phase_check(PHASE_MACHINE_READY
) && !dc
->hotpluggable
)) {
253 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
254 "a pluggable device type");
258 if (object_class_dynamic_cast(oc
, TYPE_SYS_BUS_DEVICE
)) {
259 /* sysbus devices need to be allowed by the machine */
260 MachineClass
*mc
= MACHINE_CLASS(object_get_class(qdev_get_machine()));
261 if (!device_type_is_dynamic_sysbus(mc
, *driver
)) {
262 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
263 "a dynamic sysbus device type for the machine");
272 int qdev_device_help(QemuOpts
*opts
)
274 Error
*local_err
= NULL
;
276 ObjectPropertyInfoList
*prop_list
;
277 ObjectPropertyInfoList
*prop
;
281 driver
= qemu_opt_get(opts
, "driver");
282 if (driver
&& is_help_option(driver
)) {
283 qdev_print_devinfos(false);
287 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
291 if (!object_class_by_name(driver
)) {
292 const char *typename
= find_typename_by_alias(driver
);
299 prop_list
= qmp_device_list_properties(driver
, &local_err
);
305 qemu_printf("%s options:\n", driver
);
307 qemu_printf("There are no options for %s.\n", driver
);
309 array
= g_ptr_array_new();
310 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
311 g_ptr_array_add(array
,
312 object_property_help(prop
->value
->name
,
314 prop
->value
->default_value
,
315 prop
->value
->description
));
317 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
318 for (i
= 0; i
< array
->len
; i
++) {
319 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
321 g_ptr_array_set_free_func(array
, g_free
);
322 g_ptr_array_free(array
, true);
323 qapi_free_ObjectPropertyInfoList(prop_list
);
327 error_report_err(local_err
);
331 static Object
*qdev_get_peripheral(void)
336 dev
= container_get(qdev_get_machine(), "/peripheral");
342 static Object
*qdev_get_peripheral_anon(void)
347 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
353 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
357 const char *sep
= " ";
359 error_append_hint(errp
, "child buses at \"%s\":",
360 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
361 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
362 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
365 error_append_hint(errp
, "\n");
368 static void qbus_error_append_dev_list_hint(BusState
*bus
,
372 const char *sep
= " ";
374 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
375 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
376 DeviceState
*dev
= kid
->child
;
377 error_append_hint(errp
, "%s\"%s\"", sep
,
378 object_get_typename(OBJECT(dev
)));
380 error_append_hint(errp
, "/\"%s\"", dev
->id
);
384 error_append_hint(errp
, "\n");
387 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
391 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
392 if (strcmp(child
->name
, elem
) == 0) {
399 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
404 * try to match in order:
405 * (1) instance id, if present
407 * (3) driver alias, if present
409 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
410 DeviceState
*dev
= kid
->child
;
411 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
415 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
416 DeviceState
*dev
= kid
->child
;
417 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
421 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
422 DeviceState
*dev
= kid
->child
;
423 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
425 if (qdev_class_has_alias(dc
) &&
426 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
433 static inline bool qbus_is_full(BusState
*bus
)
440 bus_class
= BUS_GET_CLASS(bus
);
441 return bus_class
->max_dev
&& bus
->num_children
>= bus_class
->max_dev
;
445 * Search the tree rooted at @bus for a bus.
446 * If @name, search for a bus with that name. Note that bus names
447 * need not be unique. Yes, that's screwed up.
448 * Else search for a bus that is a subtype of @bus_typename.
449 * If more than one exists, prefer one that can take another device.
450 * Return the bus if found, else %NULL.
452 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
453 const char *bus_typename
)
456 BusState
*pick
, *child
, *ret
;
459 assert(name
|| bus_typename
);
461 match
= !strcmp(bus
->name
, name
);
463 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
466 if (match
&& !qbus_is_full(bus
)) {
467 return bus
; /* root matches and isn't full */
470 pick
= match
? bus
: NULL
;
472 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
473 DeviceState
*dev
= kid
->child
;
474 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
475 ret
= qbus_find_recursive(child
, name
, bus_typename
);
476 if (ret
&& !qbus_is_full(ret
)) {
477 return ret
; /* a descendant matches and isn't full */
485 /* root or a descendant matches, but is full */
489 static BusState
*qbus_find(const char *path
, Error
**errp
)
496 /* find start element */
497 if (path
[0] == '/') {
498 bus
= sysbus_get_default();
501 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
505 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
507 error_setg(errp
, "Bus '%s' not found", elem
);
514 assert(path
[pos
] == '/' || !path
[pos
]);
515 while (path
[pos
] == '/') {
518 if (path
[pos
] == '\0') {
523 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
524 g_assert_not_reached();
528 dev
= qbus_find_dev(bus
, elem
);
530 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
531 "Device '%s' not found", elem
);
532 qbus_error_append_dev_list_hint(bus
, errp
);
536 assert(path
[pos
] == '/' || !path
[pos
]);
537 while (path
[pos
] == '/') {
540 if (path
[pos
] == '\0') {
541 /* last specified element is a device. If it has exactly
542 * one child bus accept it nevertheless */
543 if (dev
->num_child_bus
== 1) {
544 bus
= QLIST_FIRST(&dev
->child_bus
);
547 if (dev
->num_child_bus
) {
548 error_setg(errp
, "Device '%s' has multiple child buses",
550 qbus_error_append_bus_list_hint(dev
, errp
);
552 error_setg(errp
, "Device '%s' has no child bus", elem
);
558 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
559 g_assert_not_reached();
563 bus
= qbus_find_bus(dev
, elem
);
565 error_setg(errp
, "Bus '%s' not found", elem
);
566 qbus_error_append_bus_list_hint(dev
, errp
);
571 if (qbus_is_full(bus
)) {
572 error_setg(errp
, "Bus '%s' is full", path
);
578 /* Takes ownership of @id, will be freed when deleting the device */
579 const char *qdev_set_id(DeviceState
*dev
, char *id
, Error
**errp
)
581 ObjectProperty
*prop
;
583 assert(!dev
->id
&& !dev
->realized
);
586 * object_property_[try_]add_child() below will assert the device
590 prop
= object_property_try_add_child(qdev_get_peripheral(), id
,
596 error_setg(errp
, "Duplicate device ID '%s'", id
);
600 static int anon_count
;
601 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
602 prop
= object_property_add_child(qdev_get_peripheral_anon(), name
,
610 DeviceState
*qdev_device_add_from_qdict(const QDict
*opts
,
611 bool from_json
, Error
**errp
)
615 const char *driver
, *path
;
617 DeviceState
*dev
= NULL
;
618 BusState
*bus
= NULL
;
620 driver
= qdict_get_try_str(opts
, "driver");
622 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
627 dc
= qdev_get_device_class(&driver
, errp
);
633 path
= qdict_get_try_str(opts
, "bus");
635 bus
= qbus_find(path
, errp
);
639 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
640 error_setg(errp
, "Device '%s' can't go on %s bus",
641 driver
, object_get_typename(OBJECT(bus
)));
644 } else if (dc
->bus_type
!= NULL
) {
645 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
646 if (!bus
|| qbus_is_full(bus
)) {
647 error_setg(errp
, "No '%s' bus found for device '%s'",
648 dc
->bus_type
, driver
);
653 if (qdev_should_hide_device(opts
, from_json
, errp
)) {
654 if (bus
&& !qbus_is_hotpluggable(bus
)) {
655 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
662 if (phase_check(PHASE_MACHINE_READY
) && bus
&& !qbus_is_hotpluggable(bus
)) {
663 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
667 if (!migration_is_idle()) {
668 error_setg(errp
, "device_add not allowed while migrating");
673 dev
= qdev_new(driver
);
675 /* Check whether the hotplug is allowed by the machine */
676 if (phase_check(PHASE_MACHINE_READY
)) {
677 if (!qdev_hotplug_allowed(dev
, errp
)) {
681 if (!bus
&& !qdev_get_machine_hotplug_handler(dev
)) {
682 /* No bus, no machine hotplug handler --> device is not hotpluggable */
683 error_setg(errp
, "Device '%s' can not be hotplugged on this machine",
690 * set dev's parent and register its id.
691 * If it fails it means the id is already taken.
693 id
= g_strdup(qdict_get_try_str(opts
, "id"));
694 if (!qdev_set_id(dev
, id
, errp
)) {
699 dev
->opts
= qdict_clone_shallow(opts
);
700 qdict_del(dev
->opts
, "driver");
701 qdict_del(dev
->opts
, "bus");
702 qdict_del(dev
->opts
, "id");
704 object_set_properties_from_keyval(&dev
->parent_obj
, dev
->opts
, from_json
,
710 if (!qdev_realize(DEVICE(dev
), bus
, errp
)) {
717 object_unparent(OBJECT(dev
));
718 object_unref(OBJECT(dev
));
723 /* Takes ownership of @opts on success */
724 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
726 QDict
*qdict
= qemu_opts_to_qdict(opts
, NULL
);
729 ret
= qdev_device_add_from_qdict(qdict
, false, errp
);
733 qobject_unref(qdict
);
737 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
738 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
740 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
745 for (; props
->name
; props
++) {
747 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
749 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
750 value
= object_property_get_str(OBJECT(dev
), legacy_name
, NULL
);
752 value
= object_property_print(OBJECT(dev
), props
->name
, true,
760 qdev_printf("%s = %s\n", props
->name
,
761 *value
? value
: "<null>");
766 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
768 BusClass
*bc
= BUS_GET_CLASS(bus
);
771 bc
->print_dev(mon
, dev
, indent
);
775 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
782 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
783 dev
->id
? dev
->id
: "");
785 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
787 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
791 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
795 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
796 g_autofree
char *freq_str
= clock_display_freq(ncl
->clock
);
797 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
798 ncl
->output
? "out" : "in",
799 ncl
->alias
? " (alias)" : "",
800 ncl
->name
, freq_str
);
802 class = object_get_class(OBJECT(dev
));
804 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
805 class = object_class_get_parent(class);
806 } while (class != object_class_by_name(TYPE_DEVICE
));
807 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
808 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
809 qbus_print(mon
, child
, indent
);
813 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
817 qdev_printf("bus: %s\n", bus
->name
);
819 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
820 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
821 DeviceState
*dev
= kid
->child
;
822 qdev_print(mon
, dev
, indent
);
827 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
829 if (sysbus_get_default())
830 qbus_print(mon
, sysbus_get_default(), 0);
833 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
835 qdev_print_devinfos(true);
838 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
843 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, errp
);
847 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
851 dev
= qdev_device_add(opts
, errp
);
854 * Drain all pending RCU callbacks. This is done because
855 * some bus related operations can delay a device removal
856 * (in this case this can happen if device is added and then
857 * removed due to a configuration error)
858 * to a RCU callback, but user might expect that this interface
859 * will finish its job completely once qmp command returns result
868 object_unref(OBJECT(dev
));
871 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
877 obj
= object_resolve_path(id
, NULL
);
879 obj
= object_resolve_path_component(qdev_get_peripheral(), id
);
883 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
884 "Device '%s' not found", id
);
888 dev
= (DeviceState
*)object_dynamic_cast(obj
, TYPE_DEVICE
);
890 error_setg(errp
, "%s is not a hotpluggable device", id
);
897 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
899 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
900 HotplugHandler
*hotplug_ctrl
;
901 HotplugHandlerClass
*hdc
;
902 Error
*local_err
= NULL
;
904 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
905 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
909 if (!dc
->hotpluggable
) {
910 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
911 object_get_typename(OBJECT(dev
)));
915 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
916 error_setg(errp
, "device_del not allowed while migrating");
920 qdev_hot_removed
= true;
922 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
923 /* hotpluggable device MUST have HotplugHandler, if it doesn't
924 * then something is very wrong with it */
925 g_assert(hotplug_ctrl
);
927 /* If device supports async unplug just request it to be done,
928 * otherwise just remove it synchronously */
929 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
930 if (hdc
->unplug_request
) {
931 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
933 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
935 object_unparent(OBJECT(dev
));
938 error_propagate(errp
, local_err
);
941 void qmp_device_del(const char *id
, Error
**errp
)
943 DeviceState
*dev
= find_device_state(id
, errp
);
945 if (dev
->pending_deleted_event
) {
946 error_setg(errp
, "Device %s is already in the "
947 "process of unplug", id
);
951 qdev_unplug(dev
, errp
);
955 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
959 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
960 hmp_handle_error(mon
, err
);
963 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
965 const char *id
= qdict_get_str(qdict
, "id");
968 qmp_device_del(id
, &err
);
969 hmp_handle_error(mon
, err
);
972 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
977 dev
= find_device_state(id
, errp
);
982 blk
= blk_by_dev(dev
);
984 error_setg(errp
, "Device does not have a block device backend");
989 QemuOptsList qemu_device_opts
= {
991 .implied_opt_name
= "driver",
992 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
995 * no elements => accept any
996 * sanity checking will happen later
997 * when setting device properties
999 { /* end of list */ }
1003 QemuOptsList qemu_global_opts
= {
1005 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
1009 .type
= QEMU_OPT_STRING
,
1012 .type
= QEMU_OPT_STRING
,
1015 .type
= QEMU_OPT_STRING
,
1017 { /* end of list */ }
1021 int qemu_global_option(const char *str
)
1023 char driver
[64], property
[64];
1027 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
1028 if (rc
== 2 && str
[offset
] == '=') {
1029 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
1030 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
1031 qemu_opt_set(opts
, "property", property
, &error_abort
);
1032 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
1036 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);
1044 bool qmp_command_available(const QmpCommand
*cmd
, Error
**errp
)
1046 if (!phase_check(PHASE_MACHINE_READY
) &&
1047 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
1048 error_setg(errp
, "The command '%s' is permitted only after machine initialization has completed",