fw_cfg: Allow reboot-timeout=-1 again
[qemu/kevin.git] / qdev-monitor.c
blobe6b112eb0ab0252ecb1d585d3784f858f1adc8ab
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 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/qdict.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "qemu/help_option.h"
33 #include "qemu/option.h"
34 #include "qemu/qemu-print.h"
35 #include "qemu/option_int.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/sysemu.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
42 * Aliases were a bad idea from the start. Let's keep them
43 * from spreading further.
45 typedef struct QDevAlias
47 const char *typename;
48 const char *alias;
49 uint32_t arch_mask;
50 } QDevAlias;
52 /* Please keep this table sorted by typename. */
53 static const QDevAlias qdev_alias_table[] = {
54 { "e1000", "e1000-82540em" },
55 { "ich9-ahci", "ahci" },
56 { "lsi53c895a", "lsi" },
57 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
58 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
59 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
60 { "virtio-balloon-pci", "virtio-balloon",
61 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
62 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
63 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
64 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
65 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
66 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
67 { "virtio-input-host-pci", "virtio-input-host",
68 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
69 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
70 { "virtio-keyboard-pci", "virtio-keyboard",
71 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
73 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
75 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
77 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
78 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
79 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
80 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
81 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
82 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
83 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
84 { }
87 static const char *qdev_class_get_alias(DeviceClass *dc)
89 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
90 int i;
92 for (i = 0; qdev_alias_table[i].typename; i++) {
93 if (qdev_alias_table[i].arch_mask &&
94 !(qdev_alias_table[i].arch_mask & arch_type)) {
95 continue;
98 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
99 return qdev_alias_table[i].alias;
103 return NULL;
106 static bool qdev_class_has_alias(DeviceClass *dc)
108 return (qdev_class_get_alias(dc) != NULL);
111 static void qdev_print_devinfo(DeviceClass *dc)
113 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
114 if (dc->bus_type) {
115 qemu_printf(", bus %s", dc->bus_type);
117 if (qdev_class_has_alias(dc)) {
118 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
120 if (dc->desc) {
121 qemu_printf(", desc \"%s\"", dc->desc);
123 if (!dc->user_creatable) {
124 qemu_printf(", no-user");
126 qemu_printf("\n");
129 static void qdev_print_devinfos(bool show_no_user)
131 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
132 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
133 [DEVICE_CATEGORY_USB] = "USB",
134 [DEVICE_CATEGORY_STORAGE] = "Storage",
135 [DEVICE_CATEGORY_NETWORK] = "Network",
136 [DEVICE_CATEGORY_INPUT] = "Input",
137 [DEVICE_CATEGORY_DISPLAY] = "Display",
138 [DEVICE_CATEGORY_SOUND] = "Sound",
139 [DEVICE_CATEGORY_MISC] = "Misc",
140 [DEVICE_CATEGORY_CPU] = "CPU",
141 [DEVICE_CATEGORY_MAX] = "Uncategorized",
143 GSList *list, *elt;
144 int i;
145 bool cat_printed;
147 list = object_class_get_list_sorted(TYPE_DEVICE, false);
149 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
150 cat_printed = false;
151 for (elt = list; elt; elt = elt->next) {
152 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
153 TYPE_DEVICE);
154 if ((i < DEVICE_CATEGORY_MAX
155 ? !test_bit(i, dc->categories)
156 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
157 || (!show_no_user
158 && !dc->user_creatable)) {
159 continue;
161 if (!cat_printed) {
162 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
163 cat_printed = true;
165 qdev_print_devinfo(dc);
169 g_slist_free(list);
172 static int set_property(void *opaque, const char *name, const char *value,
173 Error **errp)
175 Object *obj = opaque;
176 Error *err = NULL;
178 if (strcmp(name, "driver") == 0)
179 return 0;
180 if (strcmp(name, "bus") == 0)
181 return 0;
183 object_property_parse(obj, value, name, &err);
184 if (err != NULL) {
185 error_propagate(errp, err);
186 return -1;
188 return 0;
191 static const char *find_typename_by_alias(const char *alias)
193 int i;
195 for (i = 0; qdev_alias_table[i].alias; i++) {
196 if (qdev_alias_table[i].arch_mask &&
197 !(qdev_alias_table[i].arch_mask & arch_type)) {
198 continue;
201 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
202 return qdev_alias_table[i].typename;
206 return NULL;
209 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
211 ObjectClass *oc;
212 DeviceClass *dc;
213 const char *original_name = *driver;
215 oc = object_class_by_name(*driver);
216 if (!oc) {
217 const char *typename = find_typename_by_alias(*driver);
219 if (typename) {
220 *driver = typename;
221 oc = object_class_by_name(*driver);
225 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
226 if (*driver != original_name) {
227 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
228 " name", original_name, *driver);
229 } else {
230 error_setg(errp, "'%s' is not a valid device model name", *driver);
232 return NULL;
235 if (object_class_is_abstract(oc)) {
236 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
237 "non-abstract device type");
238 return NULL;
241 dc = DEVICE_CLASS(oc);
242 if (!dc->user_creatable ||
243 (qdev_hotplug && !dc->hotpluggable)) {
244 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
245 "pluggable device type");
246 return NULL;
249 return dc;
253 int qdev_device_help(QemuOpts *opts)
255 Error *local_err = NULL;
256 const char *driver;
257 ObjectPropertyInfoList *prop_list;
258 ObjectPropertyInfoList *prop;
260 driver = qemu_opt_get(opts, "driver");
261 if (driver && is_help_option(driver)) {
262 qdev_print_devinfos(false);
263 return 1;
266 if (!driver || !qemu_opt_has_help_opt(opts)) {
267 return 0;
270 if (!object_class_by_name(driver)) {
271 const char *typename = find_typename_by_alias(driver);
273 if (typename) {
274 driver = typename;
278 prop_list = qmp_device_list_properties(driver, &local_err);
279 if (local_err) {
280 goto error;
283 if (prop_list) {
284 qemu_printf("%s options:\n", driver);
285 } else {
286 qemu_printf("There are no options for %s.\n", driver);
288 for (prop = prop_list; prop; prop = prop->next) {
289 int len;
290 qemu_printf(" %s=<%s>%n", prop->value->name, prop->value->type, &len);
291 if (prop->value->has_description) {
292 if (len < 24) {
293 qemu_printf("%*s", 24 - len, "");
295 qemu_printf(" - %s\n", prop->value->description);
296 } else {
297 qemu_printf("\n");
301 qapi_free_ObjectPropertyInfoList(prop_list);
302 return 1;
304 error:
305 error_report_err(local_err);
306 return 1;
309 static Object *qdev_get_peripheral(void)
311 static Object *dev;
313 if (dev == NULL) {
314 dev = container_get(qdev_get_machine(), "/peripheral");
317 return dev;
320 static Object *qdev_get_peripheral_anon(void)
322 static Object *dev;
324 if (dev == NULL) {
325 dev = container_get(qdev_get_machine(), "/peripheral-anon");
328 return dev;
331 static void qbus_list_bus(DeviceState *dev, Error **errp)
333 BusState *child;
334 const char *sep = " ";
336 error_append_hint(errp, "child buses at \"%s\":",
337 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
338 QLIST_FOREACH(child, &dev->child_bus, sibling) {
339 error_append_hint(errp, "%s\"%s\"", sep, child->name);
340 sep = ", ";
342 error_append_hint(errp, "\n");
345 static void qbus_list_dev(BusState *bus, Error **errp)
347 BusChild *kid;
348 const char *sep = " ";
350 error_append_hint(errp, "devices at \"%s\":", bus->name);
351 QTAILQ_FOREACH(kid, &bus->children, sibling) {
352 DeviceState *dev = kid->child;
353 error_append_hint(errp, "%s\"%s\"", sep,
354 object_get_typename(OBJECT(dev)));
355 if (dev->id) {
356 error_append_hint(errp, "/\"%s\"", dev->id);
358 sep = ", ";
360 error_append_hint(errp, "\n");
363 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
365 BusState *child;
367 QLIST_FOREACH(child, &dev->child_bus, sibling) {
368 if (strcmp(child->name, elem) == 0) {
369 return child;
372 return NULL;
375 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
377 BusChild *kid;
380 * try to match in order:
381 * (1) instance id, if present
382 * (2) driver name
383 * (3) driver alias, if present
385 QTAILQ_FOREACH(kid, &bus->children, sibling) {
386 DeviceState *dev = kid->child;
387 if (dev->id && strcmp(dev->id, elem) == 0) {
388 return dev;
391 QTAILQ_FOREACH(kid, &bus->children, sibling) {
392 DeviceState *dev = kid->child;
393 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
394 return dev;
397 QTAILQ_FOREACH(kid, &bus->children, sibling) {
398 DeviceState *dev = kid->child;
399 DeviceClass *dc = DEVICE_GET_CLASS(dev);
401 if (qdev_class_has_alias(dc) &&
402 strcmp(qdev_class_get_alias(dc), elem) == 0) {
403 return dev;
406 return NULL;
409 static inline bool qbus_is_full(BusState *bus)
411 BusClass *bus_class = BUS_GET_CLASS(bus);
412 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
416 * Search the tree rooted at @bus for a bus.
417 * If @name, search for a bus with that name. Note that bus names
418 * need not be unique. Yes, that's screwed up.
419 * Else search for a bus that is a subtype of @bus_typename.
420 * If more than one exists, prefer one that can take another device.
421 * Return the bus if found, else %NULL.
423 static BusState *qbus_find_recursive(BusState *bus, const char *name,
424 const char *bus_typename)
426 BusChild *kid;
427 BusState *pick, *child, *ret;
428 bool match;
430 assert(name || bus_typename);
431 if (name) {
432 match = !strcmp(bus->name, name);
433 } else {
434 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
437 if (match && !qbus_is_full(bus)) {
438 return bus; /* root matches and isn't full */
441 pick = match ? bus : NULL;
443 QTAILQ_FOREACH(kid, &bus->children, sibling) {
444 DeviceState *dev = kid->child;
445 QLIST_FOREACH(child, &dev->child_bus, sibling) {
446 ret = qbus_find_recursive(child, name, bus_typename);
447 if (ret && !qbus_is_full(ret)) {
448 return ret; /* a descendant matches and isn't full */
450 if (ret && !pick) {
451 pick = ret;
456 /* root or a descendant matches, but is full */
457 return pick;
460 static BusState *qbus_find(const char *path, Error **errp)
462 DeviceState *dev;
463 BusState *bus;
464 char elem[128];
465 int pos, len;
467 /* find start element */
468 if (path[0] == '/') {
469 bus = sysbus_get_default();
470 pos = 0;
471 } else {
472 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
473 assert(!path[0]);
474 elem[0] = len = 0;
476 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
477 if (!bus) {
478 error_setg(errp, "Bus '%s' not found", elem);
479 return NULL;
481 pos = len;
484 for (;;) {
485 assert(path[pos] == '/' || !path[pos]);
486 while (path[pos] == '/') {
487 pos++;
489 if (path[pos] == '\0') {
490 break;
493 /* find device */
494 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
495 g_assert_not_reached();
496 elem[0] = len = 0;
498 pos += len;
499 dev = qbus_find_dev(bus, elem);
500 if (!dev) {
501 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
502 "Device '%s' not found", elem);
503 qbus_list_dev(bus, errp);
504 return NULL;
507 assert(path[pos] == '/' || !path[pos]);
508 while (path[pos] == '/') {
509 pos++;
511 if (path[pos] == '\0') {
512 /* last specified element is a device. If it has exactly
513 * one child bus accept it nevertheless */
514 if (dev->num_child_bus == 1) {
515 bus = QLIST_FIRST(&dev->child_bus);
516 break;
518 if (dev->num_child_bus) {
519 error_setg(errp, "Device '%s' has multiple child buses",
520 elem);
521 qbus_list_bus(dev, errp);
522 } else {
523 error_setg(errp, "Device '%s' has no child bus", elem);
525 return NULL;
528 /* find bus */
529 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
530 g_assert_not_reached();
531 elem[0] = len = 0;
533 pos += len;
534 bus = qbus_find_bus(dev, elem);
535 if (!bus) {
536 error_setg(errp, "Bus '%s' not found", elem);
537 qbus_list_bus(dev, errp);
538 return NULL;
542 if (qbus_is_full(bus)) {
543 error_setg(errp, "Bus '%s' is full", path);
544 return NULL;
546 return bus;
549 void qdev_set_id(DeviceState *dev, const char *id)
551 if (id) {
552 dev->id = id;
555 if (dev->id) {
556 object_property_add_child(qdev_get_peripheral(), dev->id,
557 OBJECT(dev), NULL);
558 } else {
559 static int anon_count;
560 gchar *name = g_strdup_printf("device[%d]", anon_count++);
561 object_property_add_child(qdev_get_peripheral_anon(), name,
562 OBJECT(dev), NULL);
563 g_free(name);
567 static int is_failover_device(void *opaque, const char *name, const char *value,
568 Error **errp)
570 if (strcmp(name, "failover_pair_id") == 0) {
571 QemuOpts *opts = (QemuOpts *)opaque;
573 if (qdev_should_hide_device(opts)) {
574 return 1;
578 return 0;
581 static bool should_hide_device(QemuOpts *opts)
583 if (qemu_opt_foreach(opts, is_failover_device, opts, NULL) == 0) {
584 return false;
586 return true;
589 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
591 DeviceClass *dc;
592 const char *driver, *path;
593 DeviceState *dev = NULL;
594 BusState *bus = NULL;
595 Error *err = NULL;
596 bool hide;
598 driver = qemu_opt_get(opts, "driver");
599 if (!driver) {
600 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
601 return NULL;
604 /* find driver */
605 dc = qdev_get_device_class(&driver, errp);
606 if (!dc) {
607 return NULL;
610 /* find bus */
611 path = qemu_opt_get(opts, "bus");
612 if (path != NULL) {
613 bus = qbus_find(path, errp);
614 if (!bus) {
615 return NULL;
617 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
618 error_setg(errp, "Device '%s' can't go on %s bus",
619 driver, object_get_typename(OBJECT(bus)));
620 return NULL;
622 } else if (dc->bus_type != NULL) {
623 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
624 if (!bus || qbus_is_full(bus)) {
625 error_setg(errp, "No '%s' bus found for device '%s'",
626 dc->bus_type, driver);
627 return NULL;
630 hide = should_hide_device(opts);
632 if ((hide || qdev_hotplug) && bus && !qbus_is_hotpluggable(bus)) {
633 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
634 return NULL;
637 if (hide) {
638 return NULL;
641 if (!migration_is_idle()) {
642 error_setg(errp, "device_add not allowed while migrating");
643 return NULL;
646 /* create device */
647 dev = DEVICE(object_new(driver));
649 /* Check whether the hotplug is allowed by the machine */
650 if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
651 /* Error must be set in the machine hook */
652 assert(err);
653 goto err_del_dev;
656 if (bus) {
657 qdev_set_parent_bus(dev, bus);
658 } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
659 /* No bus, no machine hotplug handler --> device is not hotpluggable */
660 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
661 driver);
662 goto err_del_dev;
665 qdev_set_id(dev, qemu_opts_id(opts));
667 /* set properties */
668 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
669 goto err_del_dev;
672 dev->opts = opts;
673 object_property_set_bool(OBJECT(dev), true, "realized", &err);
674 if (err != NULL) {
675 dev->opts = NULL;
676 goto err_del_dev;
678 return dev;
680 err_del_dev:
681 error_propagate(errp, err);
682 if (dev) {
683 object_unparent(OBJECT(dev));
684 object_unref(OBJECT(dev));
686 return NULL;
690 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
691 static void qbus_print(Monitor *mon, BusState *bus, int indent);
693 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
694 int indent)
696 if (!props)
697 return;
698 for (; props->name; props++) {
699 Error *err = NULL;
700 char *value;
701 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
702 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
703 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
704 } else {
705 value = object_property_print(OBJECT(dev), props->name, true, &err);
707 g_free(legacy_name);
709 if (err) {
710 error_free(err);
711 continue;
713 qdev_printf("%s = %s\n", props->name,
714 value && *value ? value : "<null>");
715 g_free(value);
719 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
721 BusClass *bc = BUS_GET_CLASS(bus);
723 if (bc->print_dev) {
724 bc->print_dev(mon, dev, indent);
728 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
730 ObjectClass *class;
731 BusState *child;
732 NamedGPIOList *ngl;
734 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
735 dev->id ? dev->id : "");
736 indent += 2;
737 QLIST_FOREACH(ngl, &dev->gpios, node) {
738 if (ngl->num_in) {
739 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
740 ngl->num_in);
742 if (ngl->num_out) {
743 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
744 ngl->num_out);
747 class = object_get_class(OBJECT(dev));
748 do {
749 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
750 class = object_class_get_parent(class);
751 } while (class != object_class_by_name(TYPE_DEVICE));
752 bus_print_dev(dev->parent_bus, mon, dev, indent);
753 QLIST_FOREACH(child, &dev->child_bus, sibling) {
754 qbus_print(mon, child, indent);
758 static void qbus_print(Monitor *mon, BusState *bus, int indent)
760 BusChild *kid;
762 qdev_printf("bus: %s\n", bus->name);
763 indent += 2;
764 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
765 QTAILQ_FOREACH(kid, &bus->children, sibling) {
766 DeviceState *dev = kid->child;
767 qdev_print(mon, dev, indent);
770 #undef qdev_printf
772 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
774 if (sysbus_get_default())
775 qbus_print(mon, sysbus_get_default(), 0);
778 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
780 qdev_print_devinfos(true);
783 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
785 Error *local_err = NULL;
786 QemuOpts *opts;
787 DeviceState *dev;
789 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
790 if (local_err) {
791 error_propagate(errp, local_err);
792 return;
794 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
795 qemu_opts_del(opts);
796 return;
798 dev = qdev_device_add(opts, &local_err);
799 if (!dev) {
800 error_propagate(errp, local_err);
801 qemu_opts_del(opts);
802 return;
804 object_unref(OBJECT(dev));
807 static DeviceState *find_device_state(const char *id, Error **errp)
809 Object *obj;
811 if (id[0] == '/') {
812 obj = object_resolve_path(id, NULL);
813 } else {
814 char *root_path = object_get_canonical_path(qdev_get_peripheral());
815 char *path = g_strdup_printf("%s/%s", root_path, id);
817 g_free(root_path);
818 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
819 g_free(path);
822 if (!obj) {
823 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
824 "Device '%s' not found", id);
825 return NULL;
828 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
829 error_setg(errp, "%s is not a hotpluggable device", id);
830 return NULL;
833 return DEVICE(obj);
836 void qdev_unplug(DeviceState *dev, Error **errp)
838 DeviceClass *dc = DEVICE_GET_CLASS(dev);
839 HotplugHandler *hotplug_ctrl;
840 HotplugHandlerClass *hdc;
841 Error *local_err = NULL;
843 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
844 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
845 return;
848 if (!dc->hotpluggable) {
849 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
850 object_get_typename(OBJECT(dev)));
851 return;
854 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
855 error_setg(errp, "device_del not allowed while migrating");
856 return;
859 qdev_hot_removed = true;
861 hotplug_ctrl = qdev_get_hotplug_handler(dev);
862 /* hotpluggable device MUST have HotplugHandler, if it doesn't
863 * then something is very wrong with it */
864 g_assert(hotplug_ctrl);
866 /* If device supports async unplug just request it to be done,
867 * otherwise just remove it synchronously */
868 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
869 if (hdc->unplug_request) {
870 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
871 } else {
872 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
873 if (!local_err) {
874 object_unparent(OBJECT(dev));
877 error_propagate(errp, local_err);
880 void qmp_device_del(const char *id, Error **errp)
882 DeviceState *dev = find_device_state(id, errp);
883 if (dev != NULL) {
884 qdev_unplug(dev, errp);
888 void hmp_device_add(Monitor *mon, const QDict *qdict)
890 Error *err = NULL;
892 qmp_device_add((QDict *)qdict, NULL, &err);
893 hmp_handle_error(mon, &err);
896 void hmp_device_del(Monitor *mon, const QDict *qdict)
898 const char *id = qdict_get_str(qdict, "id");
899 Error *err = NULL;
901 qmp_device_del(id, &err);
902 hmp_handle_error(mon, &err);
905 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
907 DeviceState *dev;
908 BlockBackend *blk;
910 dev = find_device_state(id, errp);
911 if (dev == NULL) {
912 return NULL;
915 blk = blk_by_dev(dev);
916 if (!blk) {
917 error_setg(errp, "Device does not have a block device backend");
919 return blk;
922 void qdev_machine_init(void)
924 qdev_get_peripheral_anon();
925 qdev_get_peripheral();
928 QemuOptsList qemu_device_opts = {
929 .name = "device",
930 .implied_opt_name = "driver",
931 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
932 .desc = {
934 * no elements => accept any
935 * sanity checking will happen later
936 * when setting device properties
938 { /* end of list */ }
942 QemuOptsList qemu_global_opts = {
943 .name = "global",
944 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
945 .desc = {
947 .name = "driver",
948 .type = QEMU_OPT_STRING,
950 .name = "property",
951 .type = QEMU_OPT_STRING,
953 .name = "value",
954 .type = QEMU_OPT_STRING,
956 { /* end of list */ }
960 int qemu_global_option(const char *str)
962 char driver[64], property[64];
963 QemuOpts *opts;
964 int rc, offset;
966 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
967 if (rc == 2 && str[offset] == '=') {
968 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
969 qemu_opt_set(opts, "driver", driver, &error_abort);
970 qemu_opt_set(opts, "property", property, &error_abort);
971 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
972 return 0;
975 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
976 if (!opts) {
977 return -1;
980 return 0;