qdev: Add Error parameter to hide_device() callbacks
[qemu.git] / softmmu / qdev-monitor.c
blobea737db0285f8a24dcda7c11b9e9aad351963616
1 /*
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"
44 #include "hw/clock.h"
47 * Aliases were a bad idea from the start. Let's keep them
48 * from spreading further.
50 typedef struct QDevAlias
52 const char *typename;
53 const char *alias;
54 uint32_t arch_mask;
55 } 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));
116 int i;
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)) {
121 continue;
124 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
125 return qdev_alias_table[i].alias;
129 return NULL;
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)));
140 if (dc->bus_type) {
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));
146 if (dc->desc) {
147 qemu_printf(", desc \"%s\"", dc->desc);
149 if (!dc->user_creatable) {
150 qemu_printf(", no-user");
152 qemu_printf("\n");
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",
169 GSList *list, *elt;
170 int i;
171 bool cat_printed;
173 module_load_qom_all();
174 list = object_class_get_list_sorted(TYPE_DEVICE, false);
176 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
177 cat_printed = false;
178 for (elt = list; elt; elt = elt->next) {
179 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
180 TYPE_DEVICE);
181 if ((i < DEVICE_CATEGORY_MAX
182 ? !test_bit(i, dc->categories)
183 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
184 || (!show_no_user
185 && !dc->user_creatable)) {
186 continue;
188 if (!cat_printed) {
189 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
190 cat_printed = true;
192 qdev_print_devinfo(dc);
196 g_slist_free(list);
199 static int set_property(void *opaque, const char *name, const char *value,
200 Error **errp)
202 Object *obj = opaque;
203 QString *val;
204 Visitor *v;
205 int ret;
207 if (strcmp(name, "driver") == 0)
208 return 0;
209 if (strcmp(name, "bus") == 0)
210 return 0;
212 val = qstring_from_str(value);
213 v = qobject_input_visitor_new_keyval(QOBJECT(val));
215 if (!object_property_set(obj, name, v, errp)) {
216 ret = -1;
217 goto out;
220 ret = 0;
221 out:
222 visit_free(v);
223 qobject_unref(val);
224 return ret;
227 static const char *find_typename_by_alias(const char *alias)
229 int i;
231 for (i = 0; qdev_alias_table[i].alias; i++) {
232 if (qdev_alias_table[i].arch_mask &&
233 !(qdev_alias_table[i].arch_mask & arch_type)) {
234 continue;
237 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
238 return qdev_alias_table[i].typename;
242 return NULL;
245 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
247 ObjectClass *oc;
248 DeviceClass *dc;
249 const char *original_name = *driver;
251 oc = module_object_class_by_name(*driver);
252 if (!oc) {
253 const char *typename = find_typename_by_alias(*driver);
255 if (typename) {
256 *driver = typename;
257 oc = module_object_class_by_name(*driver);
261 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
262 if (*driver != original_name) {
263 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
264 " name", original_name, *driver);
265 } else {
266 error_setg(errp, "'%s' is not a valid device model name", *driver);
268 return NULL;
271 if (object_class_is_abstract(oc)) {
272 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
273 "a non-abstract device type");
274 return NULL;
277 dc = DEVICE_CLASS(oc);
278 if (!dc->user_creatable ||
279 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
280 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
281 "a pluggable device type");
282 return NULL;
285 return dc;
289 int qdev_device_help(QemuOpts *opts)
291 Error *local_err = NULL;
292 const char *driver;
293 ObjectPropertyInfoList *prop_list;
294 ObjectPropertyInfoList *prop;
295 GPtrArray *array;
296 int i;
298 driver = qemu_opt_get(opts, "driver");
299 if (driver && is_help_option(driver)) {
300 qdev_print_devinfos(false);
301 return 1;
304 if (!driver || !qemu_opt_has_help_opt(opts)) {
305 return 0;
308 if (!object_class_by_name(driver)) {
309 const char *typename = find_typename_by_alias(driver);
311 if (typename) {
312 driver = typename;
316 prop_list = qmp_device_list_properties(driver, &local_err);
317 if (local_err) {
318 goto error;
321 if (prop_list) {
322 qemu_printf("%s options:\n", driver);
323 } else {
324 qemu_printf("There are no options for %s.\n", driver);
326 array = g_ptr_array_new();
327 for (prop = prop_list; prop; prop = prop->next) {
328 g_ptr_array_add(array,
329 object_property_help(prop->value->name,
330 prop->value->type,
331 prop->value->default_value,
332 prop->value->description));
334 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
335 for (i = 0; i < array->len; i++) {
336 qemu_printf("%s\n", (char *)array->pdata[i]);
338 g_ptr_array_set_free_func(array, g_free);
339 g_ptr_array_free(array, true);
340 qapi_free_ObjectPropertyInfoList(prop_list);
341 return 1;
343 error:
344 error_report_err(local_err);
345 return 1;
348 static Object *qdev_get_peripheral(void)
350 static Object *dev;
352 if (dev == NULL) {
353 dev = container_get(qdev_get_machine(), "/peripheral");
356 return dev;
359 static Object *qdev_get_peripheral_anon(void)
361 static Object *dev;
363 if (dev == NULL) {
364 dev = container_get(qdev_get_machine(), "/peripheral-anon");
367 return dev;
370 static void qbus_error_append_bus_list_hint(DeviceState *dev,
371 Error *const *errp)
373 BusState *child;
374 const char *sep = " ";
376 error_append_hint(errp, "child buses at \"%s\":",
377 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
378 QLIST_FOREACH(child, &dev->child_bus, sibling) {
379 error_append_hint(errp, "%s\"%s\"", sep, child->name);
380 sep = ", ";
382 error_append_hint(errp, "\n");
385 static void qbus_error_append_dev_list_hint(BusState *bus,
386 Error *const *errp)
388 BusChild *kid;
389 const char *sep = " ";
391 error_append_hint(errp, "devices at \"%s\":", bus->name);
392 QTAILQ_FOREACH(kid, &bus->children, sibling) {
393 DeviceState *dev = kid->child;
394 error_append_hint(errp, "%s\"%s\"", sep,
395 object_get_typename(OBJECT(dev)));
396 if (dev->id) {
397 error_append_hint(errp, "/\"%s\"", dev->id);
399 sep = ", ";
401 error_append_hint(errp, "\n");
404 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
406 BusState *child;
408 QLIST_FOREACH(child, &dev->child_bus, sibling) {
409 if (strcmp(child->name, elem) == 0) {
410 return child;
413 return NULL;
416 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
418 BusChild *kid;
421 * try to match in order:
422 * (1) instance id, if present
423 * (2) driver name
424 * (3) driver alias, if present
426 QTAILQ_FOREACH(kid, &bus->children, sibling) {
427 DeviceState *dev = kid->child;
428 if (dev->id && strcmp(dev->id, elem) == 0) {
429 return dev;
432 QTAILQ_FOREACH(kid, &bus->children, sibling) {
433 DeviceState *dev = kid->child;
434 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
435 return dev;
438 QTAILQ_FOREACH(kid, &bus->children, sibling) {
439 DeviceState *dev = kid->child;
440 DeviceClass *dc = DEVICE_GET_CLASS(dev);
442 if (qdev_class_has_alias(dc) &&
443 strcmp(qdev_class_get_alias(dc), elem) == 0) {
444 return dev;
447 return NULL;
450 static inline bool qbus_is_full(BusState *bus)
452 BusClass *bus_class;
454 if (bus->full) {
455 return true;
457 bus_class = BUS_GET_CLASS(bus);
458 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
462 * Search the tree rooted at @bus for a bus.
463 * If @name, search for a bus with that name. Note that bus names
464 * need not be unique. Yes, that's screwed up.
465 * Else search for a bus that is a subtype of @bus_typename.
466 * If more than one exists, prefer one that can take another device.
467 * Return the bus if found, else %NULL.
469 static BusState *qbus_find_recursive(BusState *bus, const char *name,
470 const char *bus_typename)
472 BusChild *kid;
473 BusState *pick, *child, *ret;
474 bool match;
476 assert(name || bus_typename);
477 if (name) {
478 match = !strcmp(bus->name, name);
479 } else {
480 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
483 if (match && !qbus_is_full(bus)) {
484 return bus; /* root matches and isn't full */
487 pick = match ? bus : NULL;
489 QTAILQ_FOREACH(kid, &bus->children, sibling) {
490 DeviceState *dev = kid->child;
491 QLIST_FOREACH(child, &dev->child_bus, sibling) {
492 ret = qbus_find_recursive(child, name, bus_typename);
493 if (ret && !qbus_is_full(ret)) {
494 return ret; /* a descendant matches and isn't full */
496 if (ret && !pick) {
497 pick = ret;
502 /* root or a descendant matches, but is full */
503 return pick;
506 static BusState *qbus_find(const char *path, Error **errp)
508 DeviceState *dev;
509 BusState *bus;
510 char elem[128];
511 int pos, len;
513 /* find start element */
514 if (path[0] == '/') {
515 bus = sysbus_get_default();
516 pos = 0;
517 } else {
518 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
519 assert(!path[0]);
520 elem[0] = len = 0;
522 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
523 if (!bus) {
524 error_setg(errp, "Bus '%s' not found", elem);
525 return NULL;
527 pos = len;
530 for (;;) {
531 assert(path[pos] == '/' || !path[pos]);
532 while (path[pos] == '/') {
533 pos++;
535 if (path[pos] == '\0') {
536 break;
539 /* find device */
540 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
541 g_assert_not_reached();
542 elem[0] = len = 0;
544 pos += len;
545 dev = qbus_find_dev(bus, elem);
546 if (!dev) {
547 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
548 "Device '%s' not found", elem);
549 qbus_error_append_dev_list_hint(bus, errp);
550 return NULL;
553 assert(path[pos] == '/' || !path[pos]);
554 while (path[pos] == '/') {
555 pos++;
557 if (path[pos] == '\0') {
558 /* last specified element is a device. If it has exactly
559 * one child bus accept it nevertheless */
560 if (dev->num_child_bus == 1) {
561 bus = QLIST_FIRST(&dev->child_bus);
562 break;
564 if (dev->num_child_bus) {
565 error_setg(errp, "Device '%s' has multiple child buses",
566 elem);
567 qbus_error_append_bus_list_hint(dev, errp);
568 } else {
569 error_setg(errp, "Device '%s' has no child bus", elem);
571 return NULL;
574 /* find bus */
575 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
576 g_assert_not_reached();
577 elem[0] = len = 0;
579 pos += len;
580 bus = qbus_find_bus(dev, elem);
581 if (!bus) {
582 error_setg(errp, "Bus '%s' not found", elem);
583 qbus_error_append_bus_list_hint(dev, errp);
584 return NULL;
588 if (qbus_is_full(bus)) {
589 error_setg(errp, "Bus '%s' is full", path);
590 return NULL;
592 return bus;
595 /* Takes ownership of @id, will be freed when deleting the device */
596 const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
598 ObjectProperty *prop;
600 assert(!dev->id && !dev->realized);
603 * object_property_[try_]add_child() below will assert the device
604 * has no parent
606 if (id) {
607 prop = object_property_try_add_child(qdev_get_peripheral(), id,
608 OBJECT(dev), NULL);
609 if (prop) {
610 dev->id = id;
611 } else {
612 g_free(id);
613 error_setg(errp, "Duplicate device ID '%s'", id);
614 return NULL;
616 } else {
617 static int anon_count;
618 gchar *name = g_strdup_printf("device[%d]", anon_count++);
619 prop = object_property_add_child(qdev_get_peripheral_anon(), name,
620 OBJECT(dev));
621 g_free(name);
624 return prop->name;
627 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
629 ERRP_GUARD();
630 DeviceClass *dc;
631 const char *driver, *path;
632 DeviceState *dev = NULL;
633 BusState *bus = NULL;
635 driver = qemu_opt_get(opts, "driver");
636 if (!driver) {
637 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
638 return NULL;
641 /* find driver */
642 dc = qdev_get_device_class(&driver, errp);
643 if (!dc) {
644 return NULL;
647 /* find bus */
648 path = qemu_opt_get(opts, "bus");
649 if (path != NULL) {
650 bus = qbus_find(path, errp);
651 if (!bus) {
652 return NULL;
654 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
655 error_setg(errp, "Device '%s' can't go on %s bus",
656 driver, object_get_typename(OBJECT(bus)));
657 return NULL;
659 } else if (dc->bus_type != NULL) {
660 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
661 if (!bus || qbus_is_full(bus)) {
662 error_setg(errp, "No '%s' bus found for device '%s'",
663 dc->bus_type, driver);
664 return NULL;
668 if (qemu_opt_get(opts, "failover_pair_id")) {
669 if (!opts->id) {
670 error_setg(errp, "Device with failover_pair_id don't have id");
671 return NULL;
673 if (qdev_should_hide_device(opts, errp)) {
674 if (bus && !qbus_is_hotpluggable(bus)) {
675 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
677 return NULL;
678 } else if (*errp) {
679 return NULL;
683 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
684 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
685 return NULL;
688 if (!migration_is_idle()) {
689 error_setg(errp, "device_add not allowed while migrating");
690 return NULL;
693 /* create device */
694 dev = qdev_new(driver);
696 /* Check whether the hotplug is allowed by the machine */
697 if (phase_check(PHASE_MACHINE_READY)) {
698 if (!qdev_hotplug_allowed(dev, errp)) {
699 goto err_del_dev;
702 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
703 /* No bus, no machine hotplug handler --> device is not hotpluggable */
704 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
705 driver);
706 goto err_del_dev;
711 * set dev's parent and register its id.
712 * If it fails it means the id is already taken.
714 if (!qdev_set_id(dev, g_strdup(qemu_opts_id(opts)), errp)) {
715 goto err_del_dev;
718 /* set properties */
719 if (qemu_opt_foreach(opts, set_property, dev, errp)) {
720 goto err_del_dev;
723 dev->opts = opts;
724 if (!qdev_realize(DEVICE(dev), bus, errp)) {
725 dev->opts = NULL;
726 goto err_del_dev;
728 return dev;
730 err_del_dev:
731 if (dev) {
732 object_unparent(OBJECT(dev));
733 object_unref(OBJECT(dev));
735 return NULL;
739 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
740 static void qbus_print(Monitor *mon, BusState *bus, int indent);
742 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
743 int indent)
745 if (!props)
746 return;
747 for (; props->name; props++) {
748 char *value;
749 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
751 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
752 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
753 } else {
754 value = object_property_print(OBJECT(dev), props->name, true,
755 NULL);
757 g_free(legacy_name);
759 if (!value) {
760 continue;
762 qdev_printf("%s = %s\n", props->name,
763 *value ? value : "<null>");
764 g_free(value);
768 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
770 BusClass *bc = BUS_GET_CLASS(bus);
772 if (bc->print_dev) {
773 bc->print_dev(mon, dev, indent);
777 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
779 ObjectClass *class;
780 BusState *child;
781 NamedGPIOList *ngl;
782 NamedClockList *ncl;
784 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
785 dev->id ? dev->id : "");
786 indent += 2;
787 QLIST_FOREACH(ngl, &dev->gpios, node) {
788 if (ngl->num_in) {
789 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
790 ngl->num_in);
792 if (ngl->num_out) {
793 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
794 ngl->num_out);
797 QLIST_FOREACH(ncl, &dev->clocks, node) {
798 g_autofree char *freq_str = clock_display_freq(ncl->clock);
799 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
800 ncl->output ? "out" : "in",
801 ncl->alias ? " (alias)" : "",
802 ncl->name, freq_str);
804 class = object_get_class(OBJECT(dev));
805 do {
806 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
807 class = object_class_get_parent(class);
808 } while (class != object_class_by_name(TYPE_DEVICE));
809 bus_print_dev(dev->parent_bus, mon, dev, indent);
810 QLIST_FOREACH(child, &dev->child_bus, sibling) {
811 qbus_print(mon, child, indent);
815 static void qbus_print(Monitor *mon, BusState *bus, int indent)
817 BusChild *kid;
819 qdev_printf("bus: %s\n", bus->name);
820 indent += 2;
821 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
822 QTAILQ_FOREACH(kid, &bus->children, sibling) {
823 DeviceState *dev = kid->child;
824 qdev_print(mon, dev, indent);
827 #undef qdev_printf
829 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
831 if (sysbus_get_default())
832 qbus_print(mon, sysbus_get_default(), 0);
835 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
837 qdev_print_devinfos(true);
840 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
842 QemuOpts *opts;
843 DeviceState *dev;
845 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
846 if (!opts) {
847 return;
849 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
850 qemu_opts_del(opts);
851 return;
853 dev = qdev_device_add(opts, errp);
856 * Drain all pending RCU callbacks. This is done because
857 * some bus related operations can delay a device removal
858 * (in this case this can happen if device is added and then
859 * removed due to a configuration error)
860 * to a RCU callback, but user might expect that this interface
861 * will finish its job completely once qmp command returns result
862 * to the user
864 drain_call_rcu();
866 if (!dev) {
867 qemu_opts_del(opts);
868 return;
870 object_unref(OBJECT(dev));
873 static DeviceState *find_device_state(const char *id, Error **errp)
875 Object *obj;
876 DeviceState *dev;
878 if (id[0] == '/') {
879 obj = object_resolve_path(id, NULL);
880 } else {
881 obj = object_resolve_path_component(qdev_get_peripheral(), id);
884 if (!obj) {
885 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
886 "Device '%s' not found", id);
887 return NULL;
890 dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
891 if (!dev) {
892 error_setg(errp, "%s is not a hotpluggable device", id);
893 return NULL;
896 return dev;
899 void qdev_unplug(DeviceState *dev, Error **errp)
901 DeviceClass *dc = DEVICE_GET_CLASS(dev);
902 HotplugHandler *hotplug_ctrl;
903 HotplugHandlerClass *hdc;
904 Error *local_err = NULL;
906 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
907 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
908 return;
911 if (!dc->hotpluggable) {
912 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
913 object_get_typename(OBJECT(dev)));
914 return;
917 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
918 error_setg(errp, "device_del not allowed while migrating");
919 return;
922 qdev_hot_removed = true;
924 hotplug_ctrl = qdev_get_hotplug_handler(dev);
925 /* hotpluggable device MUST have HotplugHandler, if it doesn't
926 * then something is very wrong with it */
927 g_assert(hotplug_ctrl);
929 /* If device supports async unplug just request it to be done,
930 * otherwise just remove it synchronously */
931 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
932 if (hdc->unplug_request) {
933 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
934 } else {
935 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
936 if (!local_err) {
937 object_unparent(OBJECT(dev));
940 error_propagate(errp, local_err);
943 void qmp_device_del(const char *id, Error **errp)
945 DeviceState *dev = find_device_state(id, errp);
946 if (dev != NULL) {
947 if (dev->pending_deleted_event) {
948 error_setg(errp, "Device %s is already in the "
949 "process of unplug", id);
950 return;
953 qdev_unplug(dev, errp);
957 void hmp_device_add(Monitor *mon, const QDict *qdict)
959 Error *err = NULL;
961 qmp_device_add((QDict *)qdict, NULL, &err);
962 hmp_handle_error(mon, err);
965 void hmp_device_del(Monitor *mon, const QDict *qdict)
967 const char *id = qdict_get_str(qdict, "id");
968 Error *err = NULL;
970 qmp_device_del(id, &err);
971 hmp_handle_error(mon, err);
974 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
976 DeviceState *dev;
977 BlockBackend *blk;
979 dev = find_device_state(id, errp);
980 if (dev == NULL) {
981 return NULL;
984 blk = blk_by_dev(dev);
985 if (!blk) {
986 error_setg(errp, "Device does not have a block device backend");
988 return blk;
991 QemuOptsList qemu_device_opts = {
992 .name = "device",
993 .implied_opt_name = "driver",
994 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
995 .desc = {
997 * no elements => accept any
998 * sanity checking will happen later
999 * when setting device properties
1001 { /* end of list */ }
1005 QemuOptsList qemu_global_opts = {
1006 .name = "global",
1007 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
1008 .desc = {
1010 .name = "driver",
1011 .type = QEMU_OPT_STRING,
1013 .name = "property",
1014 .type = QEMU_OPT_STRING,
1016 .name = "value",
1017 .type = QEMU_OPT_STRING,
1019 { /* end of list */ }
1023 int qemu_global_option(const char *str)
1025 char driver[64], property[64];
1026 QemuOpts *opts;
1027 int rc, offset;
1029 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
1030 if (rc == 2 && str[offset] == '=') {
1031 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
1032 qemu_opt_set(opts, "driver", driver, &error_abort);
1033 qemu_opt_set(opts, "property", property, &error_abort);
1034 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
1035 return 0;
1038 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
1039 if (!opts) {
1040 return -1;
1043 return 0;
1046 bool qmp_command_available(const QmpCommand *cmd, Error **errp)
1048 if (!phase_check(PHASE_MACHINE_READY) &&
1049 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1050 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1051 cmd->name);
1052 return false;
1054 return true;