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 "qemu/config-file.h"
32 #include "qemu/error-report.h"
33 #include "qemu/help_option.h"
34 #include "qemu/option.h"
35 #include "qemu/qemu-print.h"
36 #include "qemu/option_int.h"
37 #include "sysemu/block-backend.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
40 #include "qemu/cutils.h"
41 #include "hw/qdev-properties.h"
45 * Aliases were a bad idea from the start. Let's keep them
46 * from spreading further.
48 typedef struct QDevAlias
55 /* default virtio transport per architecture */
56 #define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | QEMU_ARCH_ARM | \
57 QEMU_ARCH_HPPA | QEMU_ARCH_I386 | \
58 QEMU_ARCH_MIPS | QEMU_ARCH_PPC | \
59 QEMU_ARCH_RISCV | QEMU_ARCH_SH4 | \
60 QEMU_ARCH_SPARC | QEMU_ARCH_XTENSA)
61 #define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X)
62 #define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K)
64 /* Please keep this table sorted by typename. */
65 static const QDevAlias qdev_alias_table
[] = {
66 { "AC97", "ac97" }, /* -soundhw name */
67 { "e1000", "e1000-82540em" },
68 { "ES1370", "es1370" }, /* -soundhw name */
69 { "ich9-ahci", "ahci" },
70 { "lsi53c895a", "lsi" },
71 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO
},
72 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW
},
73 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI
},
74 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO
},
75 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW
},
76 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI
},
77 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO
},
78 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW
},
79 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI
},
80 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO
},
81 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW
},
82 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI
},
83 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO
},
84 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW
},
85 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI
},
86 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI
},
87 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO
},
88 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW
},
89 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI
},
90 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO
},
91 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW
},
92 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI
},
93 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO
},
94 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW
},
95 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI
},
96 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO
},
97 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW
},
98 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI
},
99 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO
},
100 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW
},
101 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI
},
102 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO
},
103 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW
},
104 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI
},
105 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO
},
106 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW
},
107 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI
},
111 static const char *qdev_class_get_alias(DeviceClass
*dc
)
113 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
116 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
117 if (qdev_alias_table
[i
].arch_mask
&&
118 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
122 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
123 return qdev_alias_table
[i
].alias
;
130 static bool qdev_class_has_alias(DeviceClass
*dc
)
132 return (qdev_class_get_alias(dc
) != NULL
);
135 static void qdev_print_devinfo(DeviceClass
*dc
)
137 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
139 qemu_printf(", bus %s", dc
->bus_type
);
141 if (qdev_class_has_alias(dc
)) {
142 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
145 qemu_printf(", desc \"%s\"", dc
->desc
);
147 if (!dc
->user_creatable
) {
148 qemu_printf(", no-user");
153 static void qdev_print_devinfos(bool show_no_user
)
155 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
156 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
157 [DEVICE_CATEGORY_USB
] = "USB",
158 [DEVICE_CATEGORY_STORAGE
] = "Storage",
159 [DEVICE_CATEGORY_NETWORK
] = "Network",
160 [DEVICE_CATEGORY_INPUT
] = "Input",
161 [DEVICE_CATEGORY_DISPLAY
] = "Display",
162 [DEVICE_CATEGORY_SOUND
] = "Sound",
163 [DEVICE_CATEGORY_MISC
] = "Misc",
164 [DEVICE_CATEGORY_CPU
] = "CPU",
165 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
171 module_load_qom_all();
172 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
174 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
176 for (elt
= list
; elt
; elt
= elt
->next
) {
177 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
179 if ((i
< DEVICE_CATEGORY_MAX
180 ? !test_bit(i
, dc
->categories
)
181 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
183 && !dc
->user_creatable
)) {
187 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
190 qdev_print_devinfo(dc
);
197 static int set_property(void *opaque
, const char *name
, const char *value
,
200 Object
*obj
= opaque
;
202 if (strcmp(name
, "driver") == 0)
204 if (strcmp(name
, "bus") == 0)
207 if (!object_property_parse(obj
, name
, value
, errp
)) {
213 static const char *find_typename_by_alias(const char *alias
)
217 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
218 if (qdev_alias_table
[i
].arch_mask
&&
219 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
223 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
224 return qdev_alias_table
[i
].typename
;
231 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
235 const char *original_name
= *driver
;
237 oc
= module_object_class_by_name(*driver
);
239 const char *typename
= find_typename_by_alias(*driver
);
243 oc
= module_object_class_by_name(*driver
);
247 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
248 if (*driver
!= original_name
) {
249 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
250 " name", original_name
, *driver
);
252 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
257 if (object_class_is_abstract(oc
)) {
258 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
259 "a non-abstract device type");
263 dc
= DEVICE_CLASS(oc
);
264 if (!dc
->user_creatable
||
265 (phase_check(PHASE_MACHINE_READY
) && !dc
->hotpluggable
)) {
266 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
267 "a pluggable device type");
275 int qdev_device_help(QemuOpts
*opts
)
277 Error
*local_err
= NULL
;
279 ObjectPropertyInfoList
*prop_list
;
280 ObjectPropertyInfoList
*prop
;
284 driver
= qemu_opt_get(opts
, "driver");
285 if (driver
&& is_help_option(driver
)) {
286 qdev_print_devinfos(false);
290 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
294 if (!object_class_by_name(driver
)) {
295 const char *typename
= find_typename_by_alias(driver
);
302 prop_list
= qmp_device_list_properties(driver
, &local_err
);
308 qemu_printf("%s options:\n", driver
);
310 qemu_printf("There are no options for %s.\n", driver
);
312 array
= g_ptr_array_new();
313 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
314 g_ptr_array_add(array
,
315 object_property_help(prop
->value
->name
,
317 prop
->value
->default_value
,
318 prop
->value
->description
));
320 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
321 for (i
= 0; i
< array
->len
; i
++) {
322 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
324 g_ptr_array_set_free_func(array
, g_free
);
325 g_ptr_array_free(array
, true);
326 qapi_free_ObjectPropertyInfoList(prop_list
);
330 error_report_err(local_err
);
334 static Object
*qdev_get_peripheral(void)
339 dev
= container_get(qdev_get_machine(), "/peripheral");
345 static Object
*qdev_get_peripheral_anon(void)
350 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
356 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
360 const char *sep
= " ";
362 error_append_hint(errp
, "child buses at \"%s\":",
363 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
364 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
365 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
368 error_append_hint(errp
, "\n");
371 static void qbus_error_append_dev_list_hint(BusState
*bus
,
375 const char *sep
= " ";
377 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
378 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
379 DeviceState
*dev
= kid
->child
;
380 error_append_hint(errp
, "%s\"%s\"", sep
,
381 object_get_typename(OBJECT(dev
)));
383 error_append_hint(errp
, "/\"%s\"", dev
->id
);
387 error_append_hint(errp
, "\n");
390 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
394 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
395 if (strcmp(child
->name
, elem
) == 0) {
402 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
407 * try to match in order:
408 * (1) instance id, if present
410 * (3) driver alias, if present
412 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
413 DeviceState
*dev
= kid
->child
;
414 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
418 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
419 DeviceState
*dev
= kid
->child
;
420 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
424 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
425 DeviceState
*dev
= kid
->child
;
426 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
428 if (qdev_class_has_alias(dc
) &&
429 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
436 static inline bool qbus_is_full(BusState
*bus
)
438 BusClass
*bus_class
= BUS_GET_CLASS(bus
);
439 return bus_class
->max_dev
&& bus
->num_children
>= bus_class
->max_dev
;
443 * Search the tree rooted at @bus for a bus.
444 * If @name, search for a bus with that name. Note that bus names
445 * need not be unique. Yes, that's screwed up.
446 * Else search for a bus that is a subtype of @bus_typename.
447 * If more than one exists, prefer one that can take another device.
448 * Return the bus if found, else %NULL.
450 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
451 const char *bus_typename
)
454 BusState
*pick
, *child
, *ret
;
457 assert(name
|| bus_typename
);
459 match
= !strcmp(bus
->name
, name
);
461 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
464 if (match
&& !qbus_is_full(bus
)) {
465 return bus
; /* root matches and isn't full */
468 pick
= match
? bus
: NULL
;
470 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
471 DeviceState
*dev
= kid
->child
;
472 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
473 ret
= qbus_find_recursive(child
, name
, bus_typename
);
474 if (ret
&& !qbus_is_full(ret
)) {
475 return ret
; /* a descendant matches and isn't full */
483 /* root or a descendant matches, but is full */
487 static BusState
*qbus_find(const char *path
, Error
**errp
)
494 /* find start element */
495 if (path
[0] == '/') {
496 bus
= sysbus_get_default();
499 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
503 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
505 error_setg(errp
, "Bus '%s' not found", elem
);
512 assert(path
[pos
] == '/' || !path
[pos
]);
513 while (path
[pos
] == '/') {
516 if (path
[pos
] == '\0') {
521 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
522 g_assert_not_reached();
526 dev
= qbus_find_dev(bus
, elem
);
528 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
529 "Device '%s' not found", elem
);
530 qbus_error_append_dev_list_hint(bus
, errp
);
534 assert(path
[pos
] == '/' || !path
[pos
]);
535 while (path
[pos
] == '/') {
538 if (path
[pos
] == '\0') {
539 /* last specified element is a device. If it has exactly
540 * one child bus accept it nevertheless */
541 if (dev
->num_child_bus
== 1) {
542 bus
= QLIST_FIRST(&dev
->child_bus
);
545 if (dev
->num_child_bus
) {
546 error_setg(errp
, "Device '%s' has multiple child buses",
548 qbus_error_append_bus_list_hint(dev
, errp
);
550 error_setg(errp
, "Device '%s' has no child bus", elem
);
556 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
557 g_assert_not_reached();
561 bus
= qbus_find_bus(dev
, elem
);
563 error_setg(errp
, "Bus '%s' not found", elem
);
564 qbus_error_append_bus_list_hint(dev
, errp
);
569 if (qbus_is_full(bus
)) {
570 error_setg(errp
, "Bus '%s' is full", path
);
576 void qdev_set_id(DeviceState
*dev
, const char *id
)
583 object_property_add_child(qdev_get_peripheral(), dev
->id
,
586 static int anon_count
;
587 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
588 object_property_add_child(qdev_get_peripheral_anon(), name
,
594 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
597 const char *driver
, *path
;
598 DeviceState
*dev
= NULL
;
599 BusState
*bus
= NULL
;
601 driver
= qemu_opt_get(opts
, "driver");
603 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
608 dc
= qdev_get_device_class(&driver
, errp
);
614 path
= qemu_opt_get(opts
, "bus");
616 bus
= qbus_find(path
, errp
);
620 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
621 error_setg(errp
, "Device '%s' can't go on %s bus",
622 driver
, object_get_typename(OBJECT(bus
)));
625 } else if (dc
->bus_type
!= NULL
) {
626 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
627 if (!bus
|| qbus_is_full(bus
)) {
628 error_setg(errp
, "No '%s' bus found for device '%s'",
629 dc
->bus_type
, driver
);
634 if (qemu_opt_get(opts
, "failover_pair_id")) {
636 error_setg(errp
, "Device with failover_pair_id don't have id");
639 if (qdev_should_hide_device(opts
)) {
640 if (bus
&& !qbus_is_hotpluggable(bus
)) {
641 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
647 if (phase_check(PHASE_MACHINE_READY
) && bus
&& !qbus_is_hotpluggable(bus
)) {
648 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
652 if (!migration_is_idle()) {
653 error_setg(errp
, "device_add not allowed while migrating");
658 dev
= qdev_new(driver
);
660 /* Check whether the hotplug is allowed by the machine */
661 if (phase_check(PHASE_MACHINE_READY
)) {
662 if (!qdev_hotplug_allowed(dev
, errp
)) {
666 if (!bus
&& !qdev_get_machine_hotplug_handler(dev
)) {
667 /* No bus, no machine hotplug handler --> device is not hotpluggable */
668 error_setg(errp
, "Device '%s' can not be hotplugged on this machine",
674 qdev_set_id(dev
, qemu_opts_id(opts
));
677 if (qemu_opt_foreach(opts
, set_property
, dev
, errp
)) {
682 if (!qdev_realize(DEVICE(dev
), bus
, errp
)) {
690 object_unparent(OBJECT(dev
));
691 object_unref(OBJECT(dev
));
697 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
698 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
700 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
705 for (; props
->name
; props
++) {
707 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
709 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
710 value
= object_property_get_str(OBJECT(dev
), legacy_name
, NULL
);
712 value
= object_property_print(OBJECT(dev
), props
->name
, true,
720 qdev_printf("%s = %s\n", props
->name
,
721 *value
? value
: "<null>");
726 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
728 BusClass
*bc
= BUS_GET_CLASS(bus
);
731 bc
->print_dev(mon
, dev
, indent
);
735 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
742 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
743 dev
->id
? dev
->id
: "");
745 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
747 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
751 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
755 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
756 g_autofree
char *freq_str
= clock_display_freq(ncl
->clock
);
757 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
758 ncl
->output
? "out" : "in",
759 ncl
->alias
? " (alias)" : "",
760 ncl
->name
, freq_str
);
762 class = object_get_class(OBJECT(dev
));
764 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
765 class = object_class_get_parent(class);
766 } while (class != object_class_by_name(TYPE_DEVICE
));
767 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
768 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
769 qbus_print(mon
, child
, indent
);
773 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
777 qdev_printf("bus: %s\n", bus
->name
);
779 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
780 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
781 DeviceState
*dev
= kid
->child
;
782 qdev_print(mon
, dev
, indent
);
787 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
789 if (sysbus_get_default())
790 qbus_print(mon
, sysbus_get_default(), 0);
793 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
795 qdev_print_devinfos(true);
798 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
803 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, errp
);
807 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
811 dev
= qdev_device_add(opts
, errp
);
814 * Drain all pending RCU callbacks. This is done because
815 * some bus related operations can delay a device removal
816 * (in this case this can happen if device is added and then
817 * removed due to a configuration error)
818 * to a RCU callback, but user might expect that this interface
819 * will finish its job completely once qmp command returns result
828 object_unref(OBJECT(dev
));
831 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
836 obj
= object_resolve_path(id
, NULL
);
838 char *root_path
= object_get_canonical_path(qdev_get_peripheral());
839 char *path
= g_strdup_printf("%s/%s", root_path
, id
);
842 obj
= object_resolve_path_type(path
, TYPE_DEVICE
, NULL
);
847 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
848 "Device '%s' not found", id
);
852 if (!object_dynamic_cast(obj
, TYPE_DEVICE
)) {
853 error_setg(errp
, "%s is not a hotpluggable device", id
);
860 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
862 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
863 HotplugHandler
*hotplug_ctrl
;
864 HotplugHandlerClass
*hdc
;
865 Error
*local_err
= NULL
;
867 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
868 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
872 if (!dc
->hotpluggable
) {
873 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
874 object_get_typename(OBJECT(dev
)));
878 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
879 error_setg(errp
, "device_del not allowed while migrating");
883 qdev_hot_removed
= true;
885 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
886 /* hotpluggable device MUST have HotplugHandler, if it doesn't
887 * then something is very wrong with it */
888 g_assert(hotplug_ctrl
);
890 /* If device supports async unplug just request it to be done,
891 * otherwise just remove it synchronously */
892 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
893 if (hdc
->unplug_request
) {
894 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
896 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
898 object_unparent(OBJECT(dev
));
901 error_propagate(errp
, local_err
);
904 void qmp_device_del(const char *id
, Error
**errp
)
906 DeviceState
*dev
= find_device_state(id
, errp
);
908 if (dev
->pending_deleted_event
) {
909 error_setg(errp
, "Device %s is already in the "
910 "process of unplug", id
);
914 qdev_unplug(dev
, errp
);
918 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
922 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
923 hmp_handle_error(mon
, err
);
926 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
928 const char *id
= qdict_get_str(qdict
, "id");
931 qmp_device_del(id
, &err
);
932 hmp_handle_error(mon
, err
);
935 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
940 dev
= find_device_state(id
, errp
);
945 blk
= blk_by_dev(dev
);
947 error_setg(errp
, "Device does not have a block device backend");
952 QemuOptsList qemu_device_opts
= {
954 .implied_opt_name
= "driver",
955 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
958 * no elements => accept any
959 * sanity checking will happen later
960 * when setting device properties
962 { /* end of list */ }
966 QemuOptsList qemu_global_opts
= {
968 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
972 .type
= QEMU_OPT_STRING
,
975 .type
= QEMU_OPT_STRING
,
978 .type
= QEMU_OPT_STRING
,
980 { /* end of list */ }
984 int qemu_global_option(const char *str
)
986 char driver
[64], property
[64];
990 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
991 if (rc
== 2 && str
[offset
] == '=') {
992 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
993 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
994 qemu_opt_set(opts
, "property", property
, &error_abort
);
995 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
999 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);
1007 bool qmp_command_available(const QmpCommand
*cmd
, Error
**errp
)
1009 if (!phase_check(PHASE_MACHINE_READY
) &&
1010 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
1011 error_setg(errp
, "The command '%s' is permitted only after machine initialization has completed",