qdev: Base object creation on QDict rather than QemuOpts
[qemu.git] / softmmu / qdev-monitor.c
blob89c473cb22a2ed90a9350fc1c3e03f1bd61926f2
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 const char *find_typename_by_alias(const char *alias)
201 int i;
203 for (i = 0; qdev_alias_table[i].alias; i++) {
204 if (qdev_alias_table[i].arch_mask &&
205 !(qdev_alias_table[i].arch_mask & arch_type)) {
206 continue;
209 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
210 return qdev_alias_table[i].typename;
214 return NULL;
217 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
219 ObjectClass *oc;
220 DeviceClass *dc;
221 const char *original_name = *driver;
223 oc = module_object_class_by_name(*driver);
224 if (!oc) {
225 const char *typename = find_typename_by_alias(*driver);
227 if (typename) {
228 *driver = typename;
229 oc = module_object_class_by_name(*driver);
233 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
234 if (*driver != original_name) {
235 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
236 " name", original_name, *driver);
237 } else {
238 error_setg(errp, "'%s' is not a valid device model name", *driver);
240 return NULL;
243 if (object_class_is_abstract(oc)) {
244 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
245 "a non-abstract device type");
246 return NULL;
249 dc = DEVICE_CLASS(oc);
250 if (!dc->user_creatable ||
251 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
252 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
253 "a pluggable device type");
254 return NULL;
257 return dc;
261 int qdev_device_help(QemuOpts *opts)
263 Error *local_err = NULL;
264 const char *driver;
265 ObjectPropertyInfoList *prop_list;
266 ObjectPropertyInfoList *prop;
267 GPtrArray *array;
268 int i;
270 driver = qemu_opt_get(opts, "driver");
271 if (driver && is_help_option(driver)) {
272 qdev_print_devinfos(false);
273 return 1;
276 if (!driver || !qemu_opt_has_help_opt(opts)) {
277 return 0;
280 if (!object_class_by_name(driver)) {
281 const char *typename = find_typename_by_alias(driver);
283 if (typename) {
284 driver = typename;
288 prop_list = qmp_device_list_properties(driver, &local_err);
289 if (local_err) {
290 goto error;
293 if (prop_list) {
294 qemu_printf("%s options:\n", driver);
295 } else {
296 qemu_printf("There are no options for %s.\n", driver);
298 array = g_ptr_array_new();
299 for (prop = prop_list; prop; prop = prop->next) {
300 g_ptr_array_add(array,
301 object_property_help(prop->value->name,
302 prop->value->type,
303 prop->value->default_value,
304 prop->value->description));
306 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
307 for (i = 0; i < array->len; i++) {
308 qemu_printf("%s\n", (char *)array->pdata[i]);
310 g_ptr_array_set_free_func(array, g_free);
311 g_ptr_array_free(array, true);
312 qapi_free_ObjectPropertyInfoList(prop_list);
313 return 1;
315 error:
316 error_report_err(local_err);
317 return 1;
320 static Object *qdev_get_peripheral(void)
322 static Object *dev;
324 if (dev == NULL) {
325 dev = container_get(qdev_get_machine(), "/peripheral");
328 return dev;
331 static Object *qdev_get_peripheral_anon(void)
333 static Object *dev;
335 if (dev == NULL) {
336 dev = container_get(qdev_get_machine(), "/peripheral-anon");
339 return dev;
342 static void qbus_error_append_bus_list_hint(DeviceState *dev,
343 Error *const *errp)
345 BusState *child;
346 const char *sep = " ";
348 error_append_hint(errp, "child buses at \"%s\":",
349 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
350 QLIST_FOREACH(child, &dev->child_bus, sibling) {
351 error_append_hint(errp, "%s\"%s\"", sep, child->name);
352 sep = ", ";
354 error_append_hint(errp, "\n");
357 static void qbus_error_append_dev_list_hint(BusState *bus,
358 Error *const *errp)
360 BusChild *kid;
361 const char *sep = " ";
363 error_append_hint(errp, "devices at \"%s\":", bus->name);
364 QTAILQ_FOREACH(kid, &bus->children, sibling) {
365 DeviceState *dev = kid->child;
366 error_append_hint(errp, "%s\"%s\"", sep,
367 object_get_typename(OBJECT(dev)));
368 if (dev->id) {
369 error_append_hint(errp, "/\"%s\"", dev->id);
371 sep = ", ";
373 error_append_hint(errp, "\n");
376 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
378 BusState *child;
380 QLIST_FOREACH(child, &dev->child_bus, sibling) {
381 if (strcmp(child->name, elem) == 0) {
382 return child;
385 return NULL;
388 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
390 BusChild *kid;
393 * try to match in order:
394 * (1) instance id, if present
395 * (2) driver name
396 * (3) driver alias, if present
398 QTAILQ_FOREACH(kid, &bus->children, sibling) {
399 DeviceState *dev = kid->child;
400 if (dev->id && strcmp(dev->id, elem) == 0) {
401 return dev;
404 QTAILQ_FOREACH(kid, &bus->children, sibling) {
405 DeviceState *dev = kid->child;
406 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
407 return dev;
410 QTAILQ_FOREACH(kid, &bus->children, sibling) {
411 DeviceState *dev = kid->child;
412 DeviceClass *dc = DEVICE_GET_CLASS(dev);
414 if (qdev_class_has_alias(dc) &&
415 strcmp(qdev_class_get_alias(dc), elem) == 0) {
416 return dev;
419 return NULL;
422 static inline bool qbus_is_full(BusState *bus)
424 BusClass *bus_class;
426 if (bus->full) {
427 return true;
429 bus_class = BUS_GET_CLASS(bus);
430 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
434 * Search the tree rooted at @bus for a bus.
435 * If @name, search for a bus with that name. Note that bus names
436 * need not be unique. Yes, that's screwed up.
437 * Else search for a bus that is a subtype of @bus_typename.
438 * If more than one exists, prefer one that can take another device.
439 * Return the bus if found, else %NULL.
441 static BusState *qbus_find_recursive(BusState *bus, const char *name,
442 const char *bus_typename)
444 BusChild *kid;
445 BusState *pick, *child, *ret;
446 bool match;
448 assert(name || bus_typename);
449 if (name) {
450 match = !strcmp(bus->name, name);
451 } else {
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 */
468 if (ret && !pick) {
469 pick = ret;
474 /* root or a descendant matches, but is full */
475 return pick;
478 static BusState *qbus_find(const char *path, Error **errp)
480 DeviceState *dev;
481 BusState *bus;
482 char elem[128];
483 int pos, len;
485 /* find start element */
486 if (path[0] == '/') {
487 bus = sysbus_get_default();
488 pos = 0;
489 } else {
490 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
491 assert(!path[0]);
492 elem[0] = len = 0;
494 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
495 if (!bus) {
496 error_setg(errp, "Bus '%s' not found", elem);
497 return NULL;
499 pos = len;
502 for (;;) {
503 assert(path[pos] == '/' || !path[pos]);
504 while (path[pos] == '/') {
505 pos++;
507 if (path[pos] == '\0') {
508 break;
511 /* find device */
512 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
513 g_assert_not_reached();
514 elem[0] = len = 0;
516 pos += len;
517 dev = qbus_find_dev(bus, elem);
518 if (!dev) {
519 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
520 "Device '%s' not found", elem);
521 qbus_error_append_dev_list_hint(bus, errp);
522 return NULL;
525 assert(path[pos] == '/' || !path[pos]);
526 while (path[pos] == '/') {
527 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);
534 break;
536 if (dev->num_child_bus) {
537 error_setg(errp, "Device '%s' has multiple child buses",
538 elem);
539 qbus_error_append_bus_list_hint(dev, errp);
540 } else {
541 error_setg(errp, "Device '%s' has no child bus", elem);
543 return NULL;
546 /* find bus */
547 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
548 g_assert_not_reached();
549 elem[0] = len = 0;
551 pos += len;
552 bus = qbus_find_bus(dev, elem);
553 if (!bus) {
554 error_setg(errp, "Bus '%s' not found", elem);
555 qbus_error_append_bus_list_hint(dev, errp);
556 return NULL;
560 if (qbus_is_full(bus)) {
561 error_setg(errp, "Bus '%s' is full", path);
562 return NULL;
564 return bus;
567 /* Takes ownership of @id, will be freed when deleting the device */
568 const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
570 ObjectProperty *prop;
572 assert(!dev->id && !dev->realized);
575 * object_property_[try_]add_child() below will assert the device
576 * has no parent
578 if (id) {
579 prop = object_property_try_add_child(qdev_get_peripheral(), id,
580 OBJECT(dev), NULL);
581 if (prop) {
582 dev->id = id;
583 } else {
584 g_free(id);
585 error_setg(errp, "Duplicate device ID '%s'", id);
586 return NULL;
588 } else {
589 static int anon_count;
590 gchar *name = g_strdup_printf("device[%d]", anon_count++);
591 prop = object_property_add_child(qdev_get_peripheral_anon(), name,
592 OBJECT(dev));
593 g_free(name);
596 return prop->name;
599 DeviceState *qdev_device_add_from_qdict(const QDict *opts,
600 bool from_json, Error **errp)
602 ERRP_GUARD();
603 DeviceClass *dc;
604 const char *driver, *path;
605 char *id;
606 DeviceState *dev = NULL;
607 BusState *bus = NULL;
609 driver = qdict_get_try_str(opts, "driver");
610 if (!driver) {
611 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
612 return NULL;
615 /* find driver */
616 dc = qdev_get_device_class(&driver, errp);
617 if (!dc) {
618 return NULL;
621 /* find bus */
622 path = qdict_get_try_str(opts, "bus");
623 if (path != NULL) {
624 bus = qbus_find(path, errp);
625 if (!bus) {
626 return NULL;
628 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
629 error_setg(errp, "Device '%s' can't go on %s bus",
630 driver, object_get_typename(OBJECT(bus)));
631 return NULL;
633 } else if (dc->bus_type != NULL) {
634 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
635 if (!bus || qbus_is_full(bus)) {
636 error_setg(errp, "No '%s' bus found for device '%s'",
637 dc->bus_type, driver);
638 return NULL;
642 if (qdict_haskey(opts, "failover_pair_id")) {
643 if (!qdict_haskey(opts, "id")) {
644 error_setg(errp, "Device with failover_pair_id don't have id");
645 return NULL;
647 if (qdev_should_hide_device(opts, from_json, errp)) {
648 if (bus && !qbus_is_hotpluggable(bus)) {
649 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
651 return NULL;
652 } else if (*errp) {
653 return NULL;
657 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
658 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
659 return NULL;
662 if (!migration_is_idle()) {
663 error_setg(errp, "device_add not allowed while migrating");
664 return NULL;
667 /* create device */
668 dev = qdev_new(driver);
670 /* Check whether the hotplug is allowed by the machine */
671 if (phase_check(PHASE_MACHINE_READY)) {
672 if (!qdev_hotplug_allowed(dev, errp)) {
673 goto err_del_dev;
676 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
677 /* No bus, no machine hotplug handler --> device is not hotpluggable */
678 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
679 driver);
680 goto err_del_dev;
685 * set dev's parent and register its id.
686 * If it fails it means the id is already taken.
688 id = g_strdup(qdict_get_try_str(opts, "id"));
689 if (!qdev_set_id(dev, id, errp)) {
690 goto err_del_dev;
693 /* set properties */
694 dev->opts = qdict_clone_shallow(opts);
695 qdict_del(dev->opts, "driver");
696 qdict_del(dev->opts, "bus");
697 qdict_del(dev->opts, "id");
699 object_set_properties_from_keyval(&dev->parent_obj, dev->opts, from_json,
700 errp);
701 if (*errp) {
702 goto err_del_dev;
705 if (!qdev_realize(DEVICE(dev), bus, errp)) {
706 goto err_del_dev;
708 return dev;
710 err_del_dev:
711 if (dev) {
712 object_unparent(OBJECT(dev));
713 object_unref(OBJECT(dev));
715 return NULL;
718 /* Takes ownership of @opts on success */
719 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
721 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
722 DeviceState *ret;
724 ret = qdev_device_add_from_qdict(qdict, false, errp);
725 if (ret) {
726 qemu_opts_del(opts);
728 qobject_unref(qdict);
729 return ret;
732 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
733 static void qbus_print(Monitor *mon, BusState *bus, int indent);
735 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
736 int indent)
738 if (!props)
739 return;
740 for (; props->name; props++) {
741 char *value;
742 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
744 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
745 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
746 } else {
747 value = object_property_print(OBJECT(dev), props->name, true,
748 NULL);
750 g_free(legacy_name);
752 if (!value) {
753 continue;
755 qdev_printf("%s = %s\n", props->name,
756 *value ? value : "<null>");
757 g_free(value);
761 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
763 BusClass *bc = BUS_GET_CLASS(bus);
765 if (bc->print_dev) {
766 bc->print_dev(mon, dev, indent);
770 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
772 ObjectClass *class;
773 BusState *child;
774 NamedGPIOList *ngl;
775 NamedClockList *ncl;
777 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
778 dev->id ? dev->id : "");
779 indent += 2;
780 QLIST_FOREACH(ngl, &dev->gpios, node) {
781 if (ngl->num_in) {
782 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
783 ngl->num_in);
785 if (ngl->num_out) {
786 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
787 ngl->num_out);
790 QLIST_FOREACH(ncl, &dev->clocks, node) {
791 g_autofree char *freq_str = clock_display_freq(ncl->clock);
792 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
793 ncl->output ? "out" : "in",
794 ncl->alias ? " (alias)" : "",
795 ncl->name, freq_str);
797 class = object_get_class(OBJECT(dev));
798 do {
799 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
800 class = object_class_get_parent(class);
801 } while (class != object_class_by_name(TYPE_DEVICE));
802 bus_print_dev(dev->parent_bus, mon, dev, indent);
803 QLIST_FOREACH(child, &dev->child_bus, sibling) {
804 qbus_print(mon, child, indent);
808 static void qbus_print(Monitor *mon, BusState *bus, int indent)
810 BusChild *kid;
812 qdev_printf("bus: %s\n", bus->name);
813 indent += 2;
814 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
815 QTAILQ_FOREACH(kid, &bus->children, sibling) {
816 DeviceState *dev = kid->child;
817 qdev_print(mon, dev, indent);
820 #undef qdev_printf
822 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
824 if (sysbus_get_default())
825 qbus_print(mon, sysbus_get_default(), 0);
828 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
830 qdev_print_devinfos(true);
833 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
835 QemuOpts *opts;
836 DeviceState *dev;
838 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
839 if (!opts) {
840 return;
842 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
843 qemu_opts_del(opts);
844 return;
846 dev = qdev_device_add(opts, errp);
849 * Drain all pending RCU callbacks. This is done because
850 * some bus related operations can delay a device removal
851 * (in this case this can happen if device is added and then
852 * removed due to a configuration error)
853 * to a RCU callback, but user might expect that this interface
854 * will finish its job completely once qmp command returns result
855 * to the user
857 drain_call_rcu();
859 if (!dev) {
860 qemu_opts_del(opts);
861 return;
863 object_unref(OBJECT(dev));
866 static DeviceState *find_device_state(const char *id, Error **errp)
868 Object *obj;
869 DeviceState *dev;
871 if (id[0] == '/') {
872 obj = object_resolve_path(id, NULL);
873 } else {
874 obj = object_resolve_path_component(qdev_get_peripheral(), id);
877 if (!obj) {
878 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
879 "Device '%s' not found", id);
880 return NULL;
883 dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
884 if (!dev) {
885 error_setg(errp, "%s is not a hotpluggable device", id);
886 return NULL;
889 return dev;
892 void qdev_unplug(DeviceState *dev, Error **errp)
894 DeviceClass *dc = DEVICE_GET_CLASS(dev);
895 HotplugHandler *hotplug_ctrl;
896 HotplugHandlerClass *hdc;
897 Error *local_err = NULL;
899 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
900 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
901 return;
904 if (!dc->hotpluggable) {
905 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
906 object_get_typename(OBJECT(dev)));
907 return;
910 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
911 error_setg(errp, "device_del not allowed while migrating");
912 return;
915 qdev_hot_removed = true;
917 hotplug_ctrl = qdev_get_hotplug_handler(dev);
918 /* hotpluggable device MUST have HotplugHandler, if it doesn't
919 * then something is very wrong with it */
920 g_assert(hotplug_ctrl);
922 /* If device supports async unplug just request it to be done,
923 * otherwise just remove it synchronously */
924 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
925 if (hdc->unplug_request) {
926 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
927 } else {
928 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
929 if (!local_err) {
930 object_unparent(OBJECT(dev));
933 error_propagate(errp, local_err);
936 void qmp_device_del(const char *id, Error **errp)
938 DeviceState *dev = find_device_state(id, errp);
939 if (dev != NULL) {
940 if (dev->pending_deleted_event) {
941 error_setg(errp, "Device %s is already in the "
942 "process of unplug", id);
943 return;
946 qdev_unplug(dev, errp);
950 void hmp_device_add(Monitor *mon, const QDict *qdict)
952 Error *err = NULL;
954 qmp_device_add((QDict *)qdict, NULL, &err);
955 hmp_handle_error(mon, err);
958 void hmp_device_del(Monitor *mon, const QDict *qdict)
960 const char *id = qdict_get_str(qdict, "id");
961 Error *err = NULL;
963 qmp_device_del(id, &err);
964 hmp_handle_error(mon, err);
967 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
969 DeviceState *dev;
970 BlockBackend *blk;
972 dev = find_device_state(id, errp);
973 if (dev == NULL) {
974 return NULL;
977 blk = blk_by_dev(dev);
978 if (!blk) {
979 error_setg(errp, "Device does not have a block device backend");
981 return blk;
984 QemuOptsList qemu_device_opts = {
985 .name = "device",
986 .implied_opt_name = "driver",
987 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
988 .desc = {
990 * no elements => accept any
991 * sanity checking will happen later
992 * when setting device properties
994 { /* end of list */ }
998 QemuOptsList qemu_global_opts = {
999 .name = "global",
1000 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
1001 .desc = {
1003 .name = "driver",
1004 .type = QEMU_OPT_STRING,
1006 .name = "property",
1007 .type = QEMU_OPT_STRING,
1009 .name = "value",
1010 .type = QEMU_OPT_STRING,
1012 { /* end of list */ }
1016 int qemu_global_option(const char *str)
1018 char driver[64], property[64];
1019 QemuOpts *opts;
1020 int rc, offset;
1022 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
1023 if (rc == 2 && str[offset] == '=') {
1024 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
1025 qemu_opt_set(opts, "driver", driver, &error_abort);
1026 qemu_opt_set(opts, "property", property, &error_abort);
1027 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
1028 return 0;
1031 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
1032 if (!opts) {
1033 return -1;
1036 return 0;
1039 bool qmp_command_available(const QmpCommand *cmd, Error **errp)
1041 if (!phase_check(PHASE_MACHINE_READY) &&
1042 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1043 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1044 cmd->name);
1045 return false;
1047 return true;