ipmi: Fix watchdog NMI handling
[qemu/kevin.git] / qdev-monitor.c
blob148df9cacfe319e4d9cd46c918b6e2fa67dae4d0
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 "sysemu/block-backend.h"
36 #include "sysemu/sysemu.h"
37 #include "migration/misc.h"
40 * Aliases were a bad idea from the start. Let's keep them
41 * from spreading further.
43 typedef struct QDevAlias
45 const char *typename;
46 const char *alias;
47 uint32_t arch_mask;
48 } QDevAlias;
50 /* Please keep this table sorted by typename. */
51 static const QDevAlias qdev_alias_table[] = {
52 { "e1000", "e1000-82540em" },
53 { "ich9-ahci", "ahci" },
54 { "lsi53c895a", "lsi" },
55 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
56 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
57 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
58 { "virtio-balloon-pci", "virtio-balloon",
59 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
60 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
61 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
62 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
63 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
64 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
65 { "virtio-input-host-pci", "virtio-input-host",
66 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
67 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
68 { "virtio-keyboard-pci", "virtio-keyboard",
69 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
71 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
73 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
75 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
77 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
78 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
79 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
80 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
81 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
82 { }
85 static const char *qdev_class_get_alias(DeviceClass *dc)
87 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
88 int i;
90 for (i = 0; qdev_alias_table[i].typename; i++) {
91 if (qdev_alias_table[i].arch_mask &&
92 !(qdev_alias_table[i].arch_mask & arch_type)) {
93 continue;
96 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
97 return qdev_alias_table[i].alias;
101 return NULL;
104 static bool qdev_class_has_alias(DeviceClass *dc)
106 return (qdev_class_get_alias(dc) != NULL);
109 static void qdev_print_devinfo(DeviceClass *dc)
111 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
112 if (dc->bus_type) {
113 qemu_printf(", bus %s", dc->bus_type);
115 if (qdev_class_has_alias(dc)) {
116 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
118 if (dc->desc) {
119 qemu_printf(", desc \"%s\"", dc->desc);
121 if (!dc->user_creatable) {
122 qemu_printf(", no-user");
124 qemu_printf("\n");
127 static void qdev_print_devinfos(bool show_no_user)
129 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
130 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
131 [DEVICE_CATEGORY_USB] = "USB",
132 [DEVICE_CATEGORY_STORAGE] = "Storage",
133 [DEVICE_CATEGORY_NETWORK] = "Network",
134 [DEVICE_CATEGORY_INPUT] = "Input",
135 [DEVICE_CATEGORY_DISPLAY] = "Display",
136 [DEVICE_CATEGORY_SOUND] = "Sound",
137 [DEVICE_CATEGORY_MISC] = "Misc",
138 [DEVICE_CATEGORY_CPU] = "CPU",
139 [DEVICE_CATEGORY_MAX] = "Uncategorized",
141 GSList *list, *elt;
142 int i;
143 bool cat_printed;
145 list = object_class_get_list_sorted(TYPE_DEVICE, false);
147 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
148 cat_printed = false;
149 for (elt = list; elt; elt = elt->next) {
150 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
151 TYPE_DEVICE);
152 if ((i < DEVICE_CATEGORY_MAX
153 ? !test_bit(i, dc->categories)
154 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
155 || (!show_no_user
156 && !dc->user_creatable)) {
157 continue;
159 if (!cat_printed) {
160 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
161 cat_printed = true;
163 qdev_print_devinfo(dc);
167 g_slist_free(list);
170 static int set_property(void *opaque, const char *name, const char *value,
171 Error **errp)
173 Object *obj = opaque;
174 Error *err = NULL;
176 if (strcmp(name, "driver") == 0)
177 return 0;
178 if (strcmp(name, "bus") == 0)
179 return 0;
181 object_property_parse(obj, value, name, &err);
182 if (err != NULL) {
183 error_propagate(errp, err);
184 return -1;
186 return 0;
189 static const char *find_typename_by_alias(const char *alias)
191 int i;
193 for (i = 0; qdev_alias_table[i].alias; i++) {
194 if (qdev_alias_table[i].arch_mask &&
195 !(qdev_alias_table[i].arch_mask & arch_type)) {
196 continue;
199 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
200 return qdev_alias_table[i].typename;
204 return NULL;
207 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
209 ObjectClass *oc;
210 DeviceClass *dc;
211 const char *original_name = *driver;
213 oc = object_class_by_name(*driver);
214 if (!oc) {
215 const char *typename = find_typename_by_alias(*driver);
217 if (typename) {
218 *driver = typename;
219 oc = object_class_by_name(*driver);
223 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
224 if (*driver != original_name) {
225 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
226 " name", original_name, *driver);
227 } else {
228 error_setg(errp, "'%s' is not a valid device model name", *driver);
230 return NULL;
233 if (object_class_is_abstract(oc)) {
234 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
235 "non-abstract device type");
236 return NULL;
239 dc = DEVICE_CLASS(oc);
240 if (!dc->user_creatable ||
241 (qdev_hotplug && !dc->hotpluggable)) {
242 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
243 "pluggable device type");
244 return NULL;
247 return dc;
251 int qdev_device_help(QemuOpts *opts)
253 Error *local_err = NULL;
254 const char *driver;
255 ObjectPropertyInfoList *prop_list;
256 ObjectPropertyInfoList *prop;
258 driver = qemu_opt_get(opts, "driver");
259 if (driver && is_help_option(driver)) {
260 qdev_print_devinfos(false);
261 return 1;
264 if (!driver || !qemu_opt_has_help_opt(opts)) {
265 return 0;
268 if (!object_class_by_name(driver)) {
269 const char *typename = find_typename_by_alias(driver);
271 if (typename) {
272 driver = typename;
276 prop_list = qmp_device_list_properties(driver, &local_err);
277 if (local_err) {
278 goto error;
281 if (prop_list) {
282 qemu_printf("%s options:\n", driver);
283 } else {
284 qemu_printf("There are no options for %s.\n", driver);
286 for (prop = prop_list; prop; prop = prop->next) {
287 int len;
288 qemu_printf(" %s=<%s>%n", prop->value->name, prop->value->type, &len);
289 if (prop->value->has_description) {
290 if (len < 24) {
291 qemu_printf("%*s", 24 - len, "");
293 qemu_printf(" - %s\n", prop->value->description);
294 } else {
295 qemu_printf("\n");
299 qapi_free_ObjectPropertyInfoList(prop_list);
300 return 1;
302 error:
303 error_report_err(local_err);
304 return 1;
307 static Object *qdev_get_peripheral(void)
309 static Object *dev;
311 if (dev == NULL) {
312 dev = container_get(qdev_get_machine(), "/peripheral");
315 return dev;
318 static Object *qdev_get_peripheral_anon(void)
320 static Object *dev;
322 if (dev == NULL) {
323 dev = container_get(qdev_get_machine(), "/peripheral-anon");
326 return dev;
329 static void qbus_list_bus(DeviceState *dev, Error **errp)
331 BusState *child;
332 const char *sep = " ";
334 error_append_hint(errp, "child buses at \"%s\":",
335 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
336 QLIST_FOREACH(child, &dev->child_bus, sibling) {
337 error_append_hint(errp, "%s\"%s\"", sep, child->name);
338 sep = ", ";
340 error_append_hint(errp, "\n");
343 static void qbus_list_dev(BusState *bus, Error **errp)
345 BusChild *kid;
346 const char *sep = " ";
348 error_append_hint(errp, "devices at \"%s\":", bus->name);
349 QTAILQ_FOREACH(kid, &bus->children, sibling) {
350 DeviceState *dev = kid->child;
351 error_append_hint(errp, "%s\"%s\"", sep,
352 object_get_typename(OBJECT(dev)));
353 if (dev->id) {
354 error_append_hint(errp, "/\"%s\"", dev->id);
356 sep = ", ";
358 error_append_hint(errp, "\n");
361 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
363 BusState *child;
365 QLIST_FOREACH(child, &dev->child_bus, sibling) {
366 if (strcmp(child->name, elem) == 0) {
367 return child;
370 return NULL;
373 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
375 BusChild *kid;
378 * try to match in order:
379 * (1) instance id, if present
380 * (2) driver name
381 * (3) driver alias, if present
383 QTAILQ_FOREACH(kid, &bus->children, sibling) {
384 DeviceState *dev = kid->child;
385 if (dev->id && strcmp(dev->id, elem) == 0) {
386 return dev;
389 QTAILQ_FOREACH(kid, &bus->children, sibling) {
390 DeviceState *dev = kid->child;
391 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
392 return dev;
395 QTAILQ_FOREACH(kid, &bus->children, sibling) {
396 DeviceState *dev = kid->child;
397 DeviceClass *dc = DEVICE_GET_CLASS(dev);
399 if (qdev_class_has_alias(dc) &&
400 strcmp(qdev_class_get_alias(dc), elem) == 0) {
401 return dev;
404 return NULL;
407 static inline bool qbus_is_full(BusState *bus)
409 BusClass *bus_class = BUS_GET_CLASS(bus);
410 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
414 * Search the tree rooted at @bus for a bus.
415 * If @name, search for a bus with that name. Note that bus names
416 * need not be unique. Yes, that's screwed up.
417 * Else search for a bus that is a subtype of @bus_typename.
418 * If more than one exists, prefer one that can take another device.
419 * Return the bus if found, else %NULL.
421 static BusState *qbus_find_recursive(BusState *bus, const char *name,
422 const char *bus_typename)
424 BusChild *kid;
425 BusState *pick, *child, *ret;
426 bool match;
428 assert(name || bus_typename);
429 if (name) {
430 match = !strcmp(bus->name, name);
431 } else {
432 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
435 if (match && !qbus_is_full(bus)) {
436 return bus; /* root matches and isn't full */
439 pick = match ? bus : NULL;
441 QTAILQ_FOREACH(kid, &bus->children, sibling) {
442 DeviceState *dev = kid->child;
443 QLIST_FOREACH(child, &dev->child_bus, sibling) {
444 ret = qbus_find_recursive(child, name, bus_typename);
445 if (ret && !qbus_is_full(ret)) {
446 return ret; /* a descendant matches and isn't full */
448 if (ret && !pick) {
449 pick = ret;
454 /* root or a descendant matches, but is full */
455 return pick;
458 static BusState *qbus_find(const char *path, Error **errp)
460 DeviceState *dev;
461 BusState *bus;
462 char elem[128];
463 int pos, len;
465 /* find start element */
466 if (path[0] == '/') {
467 bus = sysbus_get_default();
468 pos = 0;
469 } else {
470 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
471 assert(!path[0]);
472 elem[0] = len = 0;
474 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
475 if (!bus) {
476 error_setg(errp, "Bus '%s' not found", elem);
477 return NULL;
479 pos = len;
482 for (;;) {
483 assert(path[pos] == '/' || !path[pos]);
484 while (path[pos] == '/') {
485 pos++;
487 if (path[pos] == '\0') {
488 break;
491 /* find device */
492 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
493 g_assert_not_reached();
494 elem[0] = len = 0;
496 pos += len;
497 dev = qbus_find_dev(bus, elem);
498 if (!dev) {
499 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
500 "Device '%s' not found", elem);
501 qbus_list_dev(bus, errp);
502 return NULL;
505 assert(path[pos] == '/' || !path[pos]);
506 while (path[pos] == '/') {
507 pos++;
509 if (path[pos] == '\0') {
510 /* last specified element is a device. If it has exactly
511 * one child bus accept it nevertheless */
512 if (dev->num_child_bus == 1) {
513 bus = QLIST_FIRST(&dev->child_bus);
514 break;
516 if (dev->num_child_bus) {
517 error_setg(errp, "Device '%s' has multiple child buses",
518 elem);
519 qbus_list_bus(dev, errp);
520 } else {
521 error_setg(errp, "Device '%s' has no child bus", elem);
523 return NULL;
526 /* find bus */
527 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
528 g_assert_not_reached();
529 elem[0] = len = 0;
531 pos += len;
532 bus = qbus_find_bus(dev, elem);
533 if (!bus) {
534 error_setg(errp, "Bus '%s' not found", elem);
535 qbus_list_bus(dev, errp);
536 return NULL;
540 if (qbus_is_full(bus)) {
541 error_setg(errp, "Bus '%s' is full", path);
542 return NULL;
544 return bus;
547 void qdev_set_id(DeviceState *dev, const char *id)
549 if (id) {
550 dev->id = id;
553 if (dev->id) {
554 object_property_add_child(qdev_get_peripheral(), dev->id,
555 OBJECT(dev), NULL);
556 } else {
557 static int anon_count;
558 gchar *name = g_strdup_printf("device[%d]", anon_count++);
559 object_property_add_child(qdev_get_peripheral_anon(), name,
560 OBJECT(dev), NULL);
561 g_free(name);
565 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
567 DeviceClass *dc;
568 const char *driver, *path;
569 DeviceState *dev;
570 BusState *bus = NULL;
571 Error *err = NULL;
573 driver = qemu_opt_get(opts, "driver");
574 if (!driver) {
575 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
576 return NULL;
579 /* find driver */
580 dc = qdev_get_device_class(&driver, errp);
581 if (!dc) {
582 return NULL;
585 /* find bus */
586 path = qemu_opt_get(opts, "bus");
587 if (path != NULL) {
588 bus = qbus_find(path, errp);
589 if (!bus) {
590 return NULL;
592 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
593 error_setg(errp, "Device '%s' can't go on %s bus",
594 driver, object_get_typename(OBJECT(bus)));
595 return NULL;
597 } else if (dc->bus_type != NULL) {
598 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
599 if (!bus || qbus_is_full(bus)) {
600 error_setg(errp, "No '%s' bus found for device '%s'",
601 dc->bus_type, driver);
602 return NULL;
605 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
606 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
607 return NULL;
610 if (!migration_is_idle()) {
611 error_setg(errp, "device_add not allowed while migrating");
612 return NULL;
615 /* create device */
616 dev = DEVICE(object_new(driver));
618 /* Check whether the hotplug is allowed by the machine */
619 if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
620 /* Error must be set in the machine hook */
621 assert(err);
622 goto err_del_dev;
625 if (bus) {
626 qdev_set_parent_bus(dev, bus);
627 } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
628 /* No bus, no machine hotplug handler --> device is not hotpluggable */
629 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
630 driver);
631 goto err_del_dev;
634 qdev_set_id(dev, qemu_opts_id(opts));
636 /* set properties */
637 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
638 goto err_del_dev;
641 dev->opts = opts;
642 object_property_set_bool(OBJECT(dev), true, "realized", &err);
643 if (err != NULL) {
644 dev->opts = NULL;
645 goto err_del_dev;
647 return dev;
649 err_del_dev:
650 error_propagate(errp, err);
651 object_unparent(OBJECT(dev));
652 object_unref(OBJECT(dev));
653 return NULL;
657 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
658 static void qbus_print(Monitor *mon, BusState *bus, int indent);
660 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
661 int indent)
663 if (!props)
664 return;
665 for (; props->name; props++) {
666 Error *err = NULL;
667 char *value;
668 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
669 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
670 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
671 } else {
672 value = object_property_print(OBJECT(dev), props->name, true, &err);
674 g_free(legacy_name);
676 if (err) {
677 error_free(err);
678 continue;
680 qdev_printf("%s = %s\n", props->name,
681 value && *value ? value : "<null>");
682 g_free(value);
686 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
688 BusClass *bc = BUS_GET_CLASS(bus);
690 if (bc->print_dev) {
691 bc->print_dev(mon, dev, indent);
695 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
697 ObjectClass *class;
698 BusState *child;
699 NamedGPIOList *ngl;
701 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
702 dev->id ? dev->id : "");
703 indent += 2;
704 QLIST_FOREACH(ngl, &dev->gpios, node) {
705 if (ngl->num_in) {
706 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
707 ngl->num_in);
709 if (ngl->num_out) {
710 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
711 ngl->num_out);
714 class = object_get_class(OBJECT(dev));
715 do {
716 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
717 class = object_class_get_parent(class);
718 } while (class != object_class_by_name(TYPE_DEVICE));
719 bus_print_dev(dev->parent_bus, mon, dev, indent);
720 QLIST_FOREACH(child, &dev->child_bus, sibling) {
721 qbus_print(mon, child, indent);
725 static void qbus_print(Monitor *mon, BusState *bus, int indent)
727 BusChild *kid;
729 qdev_printf("bus: %s\n", bus->name);
730 indent += 2;
731 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
732 QTAILQ_FOREACH(kid, &bus->children, sibling) {
733 DeviceState *dev = kid->child;
734 qdev_print(mon, dev, indent);
737 #undef qdev_printf
739 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
741 if (sysbus_get_default())
742 qbus_print(mon, sysbus_get_default(), 0);
745 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
747 qdev_print_devinfos(true);
750 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
752 Error *local_err = NULL;
753 QemuOpts *opts;
754 DeviceState *dev;
756 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
757 if (local_err) {
758 error_propagate(errp, local_err);
759 return;
761 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
762 qemu_opts_del(opts);
763 return;
765 dev = qdev_device_add(opts, &local_err);
766 if (!dev) {
767 error_propagate(errp, local_err);
768 qemu_opts_del(opts);
769 return;
771 object_unref(OBJECT(dev));
774 static DeviceState *find_device_state(const char *id, Error **errp)
776 Object *obj;
778 if (id[0] == '/') {
779 obj = object_resolve_path(id, NULL);
780 } else {
781 char *root_path = object_get_canonical_path(qdev_get_peripheral());
782 char *path = g_strdup_printf("%s/%s", root_path, id);
784 g_free(root_path);
785 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
786 g_free(path);
789 if (!obj) {
790 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
791 "Device '%s' not found", id);
792 return NULL;
795 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
796 error_setg(errp, "%s is not a hotpluggable device", id);
797 return NULL;
800 return DEVICE(obj);
803 void qdev_unplug(DeviceState *dev, Error **errp)
805 DeviceClass *dc = DEVICE_GET_CLASS(dev);
806 HotplugHandler *hotplug_ctrl;
807 HotplugHandlerClass *hdc;
808 Error *local_err = NULL;
810 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
811 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
812 return;
815 if (!dc->hotpluggable) {
816 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
817 object_get_typename(OBJECT(dev)));
818 return;
821 if (!migration_is_idle()) {
822 error_setg(errp, "device_del not allowed while migrating");
823 return;
826 qdev_hot_removed = true;
828 hotplug_ctrl = qdev_get_hotplug_handler(dev);
829 /* hotpluggable device MUST have HotplugHandler, if it doesn't
830 * then something is very wrong with it */
831 g_assert(hotplug_ctrl);
833 /* If device supports async unplug just request it to be done,
834 * otherwise just remove it synchronously */
835 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
836 if (hdc->unplug_request) {
837 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
838 } else {
839 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
840 if (!local_err) {
841 object_unparent(OBJECT(dev));
844 error_propagate(errp, local_err);
847 void qmp_device_del(const char *id, Error **errp)
849 DeviceState *dev = find_device_state(id, errp);
850 if (dev != NULL) {
851 qdev_unplug(dev, errp);
855 void hmp_device_add(Monitor *mon, const QDict *qdict)
857 Error *err = NULL;
859 qmp_device_add((QDict *)qdict, NULL, &err);
860 hmp_handle_error(mon, &err);
863 void hmp_device_del(Monitor *mon, const QDict *qdict)
865 const char *id = qdict_get_str(qdict, "id");
866 Error *err = NULL;
868 qmp_device_del(id, &err);
869 hmp_handle_error(mon, &err);
872 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
874 DeviceState *dev;
875 BlockBackend *blk;
877 dev = find_device_state(id, errp);
878 if (dev == NULL) {
879 return NULL;
882 blk = blk_by_dev(dev);
883 if (!blk) {
884 error_setg(errp, "Device does not have a block device backend");
886 return blk;
889 void qdev_machine_init(void)
891 qdev_get_peripheral_anon();
892 qdev_get_peripheral();
895 QemuOptsList qemu_device_opts = {
896 .name = "device",
897 .implied_opt_name = "driver",
898 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
899 .desc = {
901 * no elements => accept any
902 * sanity checking will happen later
903 * when setting device properties
905 { /* end of list */ }
909 QemuOptsList qemu_global_opts = {
910 .name = "global",
911 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
912 .desc = {
914 .name = "driver",
915 .type = QEMU_OPT_STRING,
917 .name = "property",
918 .type = QEMU_OPT_STRING,
920 .name = "value",
921 .type = QEMU_OPT_STRING,
923 { /* end of list */ }
927 int qemu_global_option(const char *str)
929 char driver[64], property[64];
930 QemuOpts *opts;
931 int rc, offset;
933 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
934 if (rc == 2 && str[offset] == '=') {
935 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
936 qemu_opt_set(opts, "driver", driver, &error_abort);
937 qemu_opt_set(opts, "property", property, &error_abort);
938 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
939 return 0;
942 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
943 if (!opts) {
944 return -1;
947 return 0;