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 /* Please keep this table sorted by typename. */
56 static const QDevAlias qdev_alias_table
[] = {
57 { "AC97", "ac97" }, /* -soundhw name */
58 { "e1000", "e1000-82540em" },
59 { "ES1370", "es1370" }, /* -soundhw name */
60 { "ich9-ahci", "ahci" },
61 { "lsi53c895a", "lsi" },
62 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO
},
63 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW
},
64 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI
},
65 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO
},
66 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW
},
67 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI
},
68 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO
},
69 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW
},
70 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI
},
71 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO
},
72 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW
},
73 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI
},
74 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO
},
75 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW
},
76 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI
},
77 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI
},
78 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO
},
79 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW
},
80 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI
},
81 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO
},
82 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW
},
83 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI
},
84 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO
},
85 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW
},
86 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI
},
87 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO
},
88 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW
},
89 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI
},
90 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO
},
91 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW
},
92 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI
},
93 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO
},
94 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW
},
95 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI
},
96 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO
},
97 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW
},
98 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI
},
102 static const char *qdev_class_get_alias(DeviceClass
*dc
)
104 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
107 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
108 if (qdev_alias_table
[i
].arch_mask
&&
109 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
113 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
114 return qdev_alias_table
[i
].alias
;
121 static bool qdev_class_has_alias(DeviceClass
*dc
)
123 return (qdev_class_get_alias(dc
) != NULL
);
126 static void qdev_print_devinfo(DeviceClass
*dc
)
128 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
130 qemu_printf(", bus %s", dc
->bus_type
);
132 if (qdev_class_has_alias(dc
)) {
133 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
136 qemu_printf(", desc \"%s\"", dc
->desc
);
138 if (!dc
->user_creatable
) {
139 qemu_printf(", no-user");
144 static void qdev_print_devinfos(bool show_no_user
)
146 static const char *cat_name
[DEVICE_CATEGORY_MAX
+ 1] = {
147 [DEVICE_CATEGORY_BRIDGE
] = "Controller/Bridge/Hub",
148 [DEVICE_CATEGORY_USB
] = "USB",
149 [DEVICE_CATEGORY_STORAGE
] = "Storage",
150 [DEVICE_CATEGORY_NETWORK
] = "Network",
151 [DEVICE_CATEGORY_INPUT
] = "Input",
152 [DEVICE_CATEGORY_DISPLAY
] = "Display",
153 [DEVICE_CATEGORY_SOUND
] = "Sound",
154 [DEVICE_CATEGORY_MISC
] = "Misc",
155 [DEVICE_CATEGORY_CPU
] = "CPU",
156 [DEVICE_CATEGORY_MAX
] = "Uncategorized",
162 module_load_qom_all();
163 list
= object_class_get_list_sorted(TYPE_DEVICE
, false);
165 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
167 for (elt
= list
; elt
; elt
= elt
->next
) {
168 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
170 if ((i
< DEVICE_CATEGORY_MAX
171 ? !test_bit(i
, dc
->categories
)
172 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
174 && !dc
->user_creatable
)) {
178 qemu_printf("%s%s devices:\n", i
? "\n" : "", cat_name
[i
]);
181 qdev_print_devinfo(dc
);
188 static int set_property(void *opaque
, const char *name
, const char *value
,
191 Object
*obj
= opaque
;
193 if (strcmp(name
, "driver") == 0)
195 if (strcmp(name
, "bus") == 0)
198 if (!object_property_parse(obj
, name
, value
, errp
)) {
204 static const char *find_typename_by_alias(const char *alias
)
208 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
209 if (qdev_alias_table
[i
].arch_mask
&&
210 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
214 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
215 return qdev_alias_table
[i
].typename
;
222 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
226 const char *original_name
= *driver
;
228 oc
= module_object_class_by_name(*driver
);
230 const char *typename
= find_typename_by_alias(*driver
);
234 oc
= module_object_class_by_name(*driver
);
238 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
239 if (*driver
!= original_name
) {
240 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
241 " name", original_name
, *driver
);
243 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
248 if (object_class_is_abstract(oc
)) {
249 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
250 "a non-abstract device type");
254 dc
= DEVICE_CLASS(oc
);
255 if (!dc
->user_creatable
||
256 (phase_check(PHASE_MACHINE_READY
) && !dc
->hotpluggable
)) {
257 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
258 "a pluggable device type");
266 int qdev_device_help(QemuOpts
*opts
)
268 Error
*local_err
= NULL
;
270 ObjectPropertyInfoList
*prop_list
;
271 ObjectPropertyInfoList
*prop
;
275 driver
= qemu_opt_get(opts
, "driver");
276 if (driver
&& is_help_option(driver
)) {
277 qdev_print_devinfos(false);
281 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
285 if (!object_class_by_name(driver
)) {
286 const char *typename
= find_typename_by_alias(driver
);
293 prop_list
= qmp_device_list_properties(driver
, &local_err
);
299 qemu_printf("%s options:\n", driver
);
301 qemu_printf("There are no options for %s.\n", driver
);
303 array
= g_ptr_array_new();
304 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
305 g_ptr_array_add(array
,
306 object_property_help(prop
->value
->name
,
308 prop
->value
->default_value
,
309 prop
->value
->description
));
311 g_ptr_array_sort(array
, (GCompareFunc
)qemu_pstrcmp0
);
312 for (i
= 0; i
< array
->len
; i
++) {
313 qemu_printf("%s\n", (char *)array
->pdata
[i
]);
315 g_ptr_array_set_free_func(array
, g_free
);
316 g_ptr_array_free(array
, true);
317 qapi_free_ObjectPropertyInfoList(prop_list
);
321 error_report_err(local_err
);
325 static Object
*qdev_get_peripheral(void)
330 dev
= container_get(qdev_get_machine(), "/peripheral");
336 static Object
*qdev_get_peripheral_anon(void)
341 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
347 static void qbus_error_append_bus_list_hint(DeviceState
*dev
,
351 const char *sep
= " ";
353 error_append_hint(errp
, "child buses at \"%s\":",
354 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
355 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
356 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
359 error_append_hint(errp
, "\n");
362 static void qbus_error_append_dev_list_hint(BusState
*bus
,
366 const char *sep
= " ";
368 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
369 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
370 DeviceState
*dev
= kid
->child
;
371 error_append_hint(errp
, "%s\"%s\"", sep
,
372 object_get_typename(OBJECT(dev
)));
374 error_append_hint(errp
, "/\"%s\"", dev
->id
);
378 error_append_hint(errp
, "\n");
381 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
385 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
386 if (strcmp(child
->name
, elem
) == 0) {
393 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
398 * try to match in order:
399 * (1) instance id, if present
401 * (3) driver alias, if present
403 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
404 DeviceState
*dev
= kid
->child
;
405 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
409 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
410 DeviceState
*dev
= kid
->child
;
411 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
415 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
416 DeviceState
*dev
= kid
->child
;
417 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
419 if (qdev_class_has_alias(dc
) &&
420 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
427 static inline bool qbus_is_full(BusState
*bus
)
429 BusClass
*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 void qdev_set_id(DeviceState
*dev
, const char *id
)
574 object_property_add_child(qdev_get_peripheral(), dev
->id
,
577 static int anon_count
;
578 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
579 object_property_add_child(qdev_get_peripheral_anon(), name
,
585 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
588 const char *driver
, *path
;
589 DeviceState
*dev
= NULL
;
590 BusState
*bus
= NULL
;
592 driver
= qemu_opt_get(opts
, "driver");
594 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
599 dc
= qdev_get_device_class(&driver
, errp
);
605 path
= qemu_opt_get(opts
, "bus");
607 bus
= qbus_find(path
, errp
);
611 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
612 error_setg(errp
, "Device '%s' can't go on %s bus",
613 driver
, object_get_typename(OBJECT(bus
)));
616 } else if (dc
->bus_type
!= NULL
) {
617 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
618 if (!bus
|| qbus_is_full(bus
)) {
619 error_setg(errp
, "No '%s' bus found for device '%s'",
620 dc
->bus_type
, driver
);
625 if (qemu_opt_get(opts
, "failover_pair_id")) {
627 error_setg(errp
, "Device with failover_pair_id don't have id");
630 if (qdev_should_hide_device(opts
)) {
631 if (bus
&& !qbus_is_hotpluggable(bus
)) {
632 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
638 if (phase_check(PHASE_MACHINE_READY
) && bus
&& !qbus_is_hotpluggable(bus
)) {
639 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
643 if (!migration_is_idle()) {
644 error_setg(errp
, "device_add not allowed while migrating");
649 dev
= qdev_new(driver
);
651 /* Check whether the hotplug is allowed by the machine */
652 if (phase_check(PHASE_MACHINE_READY
)) {
653 if (!qdev_hotplug_allowed(dev
, errp
)) {
657 if (!bus
&& !qdev_get_machine_hotplug_handler(dev
)) {
658 /* No bus, no machine hotplug handler --> device is not hotpluggable */
659 error_setg(errp
, "Device '%s' can not be hotplugged on this machine",
665 qdev_set_id(dev
, qemu_opts_id(opts
));
668 if (qemu_opt_foreach(opts
, set_property
, dev
, errp
)) {
673 if (!qdev_realize(DEVICE(dev
), bus
, errp
)) {
681 object_unparent(OBJECT(dev
));
682 object_unref(OBJECT(dev
));
688 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
689 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
691 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
696 for (; props
->name
; props
++) {
698 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
700 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
701 value
= object_property_get_str(OBJECT(dev
), legacy_name
, NULL
);
703 value
= object_property_print(OBJECT(dev
), props
->name
, true,
711 qdev_printf("%s = %s\n", props
->name
,
712 *value
? value
: "<null>");
717 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
719 BusClass
*bc
= BUS_GET_CLASS(bus
);
722 bc
->print_dev(mon
, dev
, indent
);
726 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
733 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
734 dev
->id
? dev
->id
: "");
736 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
738 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
742 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
746 QLIST_FOREACH(ncl
, &dev
->clocks
, node
) {
747 g_autofree
char *freq_str
= clock_display_freq(ncl
->clock
);
748 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
749 ncl
->output
? "out" : "in",
750 ncl
->alias
? " (alias)" : "",
751 ncl
->name
, freq_str
);
753 class = object_get_class(OBJECT(dev
));
755 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props_
, indent
);
756 class = object_class_get_parent(class);
757 } while (class != object_class_by_name(TYPE_DEVICE
));
758 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
759 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
760 qbus_print(mon
, child
, indent
);
764 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
768 qdev_printf("bus: %s\n", bus
->name
);
770 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
771 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
772 DeviceState
*dev
= kid
->child
;
773 qdev_print(mon
, dev
, indent
);
778 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
780 if (sysbus_get_default())
781 qbus_print(mon
, sysbus_get_default(), 0);
784 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
786 qdev_print_devinfos(true);
789 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
794 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, errp
);
798 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
802 dev
= qdev_device_add(opts
, errp
);
805 * Drain all pending RCU callbacks. This is done because
806 * some bus related operations can delay a device removal
807 * (in this case this can happen if device is added and then
808 * removed due to a configuration error)
809 * to a RCU callback, but user might expect that this interface
810 * will finish its job completely once qmp command returns result
819 object_unref(OBJECT(dev
));
822 static DeviceState
*find_device_state(const char *id
, Error
**errp
)
827 obj
= object_resolve_path(id
, NULL
);
829 char *root_path
= object_get_canonical_path(qdev_get_peripheral());
830 char *path
= g_strdup_printf("%s/%s", root_path
, id
);
833 obj
= object_resolve_path_type(path
, TYPE_DEVICE
, NULL
);
838 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
839 "Device '%s' not found", id
);
843 if (!object_dynamic_cast(obj
, TYPE_DEVICE
)) {
844 error_setg(errp
, "%s is not a hotpluggable device", id
);
851 void qdev_unplug(DeviceState
*dev
, Error
**errp
)
853 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
854 HotplugHandler
*hotplug_ctrl
;
855 HotplugHandlerClass
*hdc
;
856 Error
*local_err
= NULL
;
858 if (dev
->parent_bus
&& !qbus_is_hotpluggable(dev
->parent_bus
)) {
859 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, dev
->parent_bus
->name
);
863 if (!dc
->hotpluggable
) {
864 error_setg(errp
, QERR_DEVICE_NO_HOTPLUG
,
865 object_get_typename(OBJECT(dev
)));
869 if (!migration_is_idle() && !dev
->allow_unplug_during_migration
) {
870 error_setg(errp
, "device_del not allowed while migrating");
874 qdev_hot_removed
= true;
876 hotplug_ctrl
= qdev_get_hotplug_handler(dev
);
877 /* hotpluggable device MUST have HotplugHandler, if it doesn't
878 * then something is very wrong with it */
879 g_assert(hotplug_ctrl
);
881 /* If device supports async unplug just request it to be done,
882 * otherwise just remove it synchronously */
883 hdc
= HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl
);
884 if (hdc
->unplug_request
) {
885 hotplug_handler_unplug_request(hotplug_ctrl
, dev
, &local_err
);
887 hotplug_handler_unplug(hotplug_ctrl
, dev
, &local_err
);
889 object_unparent(OBJECT(dev
));
892 error_propagate(errp
, local_err
);
895 void qmp_device_del(const char *id
, Error
**errp
)
897 DeviceState
*dev
= find_device_state(id
, errp
);
899 if (dev
->pending_deleted_event
) {
900 error_setg(errp
, "Device %s is already in the "
901 "process of unplug", id
);
905 qdev_unplug(dev
, errp
);
909 void hmp_device_add(Monitor
*mon
, const QDict
*qdict
)
913 qmp_device_add((QDict
*)qdict
, NULL
, &err
);
914 hmp_handle_error(mon
, err
);
917 void hmp_device_del(Monitor
*mon
, const QDict
*qdict
)
919 const char *id
= qdict_get_str(qdict
, "id");
922 qmp_device_del(id
, &err
);
923 hmp_handle_error(mon
, err
);
926 BlockBackend
*blk_by_qdev_id(const char *id
, Error
**errp
)
931 dev
= find_device_state(id
, errp
);
936 blk
= blk_by_dev(dev
);
938 error_setg(errp
, "Device does not have a block device backend");
943 QemuOptsList qemu_device_opts
= {
945 .implied_opt_name
= "driver",
946 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
949 * no elements => accept any
950 * sanity checking will happen later
951 * when setting device properties
953 { /* end of list */ }
957 QemuOptsList qemu_global_opts
= {
959 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
963 .type
= QEMU_OPT_STRING
,
966 .type
= QEMU_OPT_STRING
,
969 .type
= QEMU_OPT_STRING
,
971 { /* end of list */ }
975 int qemu_global_option(const char *str
)
977 char driver
[64], property
[64];
981 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
982 if (rc
== 2 && str
[offset
] == '=') {
983 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
984 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
985 qemu_opt_set(opts
, "property", property
, &error_abort
);
986 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
990 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);
998 bool qmp_command_available(const QmpCommand
*cmd
, Error
**errp
)
1000 if (!phase_check(PHASE_MACHINE_READY
) &&
1001 !(cmd
->options
& QCO_ALLOW_PRECONFIG
)) {
1002 error_setg(errp
, "The command '%s' is permitted only after machine initialization has completed",