exec: introduce tlb_init
[qemu/ar7.git] / qdev-monitor.c
blob802c18a74e1d36eeffdae7b0a490d6543b5e31d4
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/qdev.h"
22 #include "hw/sysbus.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-misc.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 "sysemu/block-backend.h"
35 #include "migration/misc.h"
38 * Aliases were a bad idea from the start. Let's keep them
39 * from spreading further.
41 typedef struct QDevAlias
43 const char *typename;
44 const char *alias;
45 uint32_t arch_mask;
46 } QDevAlias;
48 /* Please keep this table sorted by typename. */
49 static const QDevAlias qdev_alias_table[] = {
50 { "e1000", "e1000-82540em" },
51 { "ich9-ahci", "ahci" },
52 { "lsi53c895a", "lsi" },
53 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
54 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
55 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
56 { "virtio-balloon-pci", "virtio-balloon",
57 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
58 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
59 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
60 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
61 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
62 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
63 { "virtio-input-host-pci", "virtio-input-host",
64 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
65 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
66 { "virtio-keyboard-pci", "virtio-keyboard",
67 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
68 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
69 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
71 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
72 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
73 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
75 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
77 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
78 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
79 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
80 { }
83 static const char *qdev_class_get_alias(DeviceClass *dc)
85 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
86 int i;
88 for (i = 0; qdev_alias_table[i].typename; i++) {
89 if (qdev_alias_table[i].arch_mask &&
90 !(qdev_alias_table[i].arch_mask & arch_type)) {
91 continue;
94 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
95 return qdev_alias_table[i].alias;
99 return NULL;
102 static bool qdev_class_has_alias(DeviceClass *dc)
104 return (qdev_class_get_alias(dc) != NULL);
107 static void out_printf(const char *fmt, ...)
109 va_list ap;
111 va_start(ap, fmt);
112 monitor_vfprintf(stdout, fmt, ap);
113 va_end(ap);
116 static void qdev_print_devinfo(DeviceClass *dc)
118 out_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
119 if (dc->bus_type) {
120 out_printf(", bus %s", dc->bus_type);
122 if (qdev_class_has_alias(dc)) {
123 out_printf(", alias \"%s\"", qdev_class_get_alias(dc));
125 if (dc->desc) {
126 out_printf(", desc \"%s\"", dc->desc);
128 if (!dc->user_creatable) {
129 out_printf(", no-user");
131 out_printf("\n");
134 static void qdev_print_devinfos(bool show_no_user)
136 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
137 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
138 [DEVICE_CATEGORY_USB] = "USB",
139 [DEVICE_CATEGORY_STORAGE] = "Storage",
140 [DEVICE_CATEGORY_NETWORK] = "Network",
141 [DEVICE_CATEGORY_INPUT] = "Input",
142 [DEVICE_CATEGORY_DISPLAY] = "Display",
143 [DEVICE_CATEGORY_SOUND] = "Sound",
144 [DEVICE_CATEGORY_MISC] = "Misc",
145 [DEVICE_CATEGORY_CPU] = "CPU",
146 [DEVICE_CATEGORY_MAX] = "Uncategorized",
148 GSList *list, *elt;
149 int i;
150 bool cat_printed;
152 list = object_class_get_list_sorted(TYPE_DEVICE, false);
154 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
155 cat_printed = false;
156 for (elt = list; elt; elt = elt->next) {
157 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
158 TYPE_DEVICE);
159 if ((i < DEVICE_CATEGORY_MAX
160 ? !test_bit(i, dc->categories)
161 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
162 || (!show_no_user
163 && !dc->user_creatable)) {
164 continue;
166 if (!cat_printed) {
167 out_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
168 cat_printed = true;
170 qdev_print_devinfo(dc);
174 g_slist_free(list);
177 static int set_property(void *opaque, const char *name, const char *value,
178 Error **errp)
180 Object *obj = opaque;
181 Error *err = NULL;
183 if (strcmp(name, "driver") == 0)
184 return 0;
185 if (strcmp(name, "bus") == 0)
186 return 0;
188 object_property_parse(obj, value, name, &err);
189 if (err != NULL) {
190 error_propagate(errp, err);
191 return -1;
193 return 0;
196 static const char *find_typename_by_alias(const char *alias)
198 int i;
200 for (i = 0; qdev_alias_table[i].alias; i++) {
201 if (qdev_alias_table[i].arch_mask &&
202 !(qdev_alias_table[i].arch_mask & arch_type)) {
203 continue;
206 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
207 return qdev_alias_table[i].typename;
211 return NULL;
214 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
216 ObjectClass *oc;
217 DeviceClass *dc;
218 const char *original_name = *driver;
220 oc = object_class_by_name(*driver);
221 if (!oc) {
222 const char *typename = find_typename_by_alias(*driver);
224 if (typename) {
225 *driver = typename;
226 oc = object_class_by_name(*driver);
230 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
231 if (*driver != original_name) {
232 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
233 " name", original_name, *driver);
234 } else {
235 error_setg(errp, "'%s' is not a valid device model name", *driver);
237 return NULL;
240 if (object_class_is_abstract(oc)) {
241 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
242 "non-abstract device type");
243 return NULL;
246 dc = DEVICE_CLASS(oc);
247 if (!dc->user_creatable ||
248 (qdev_hotplug && !dc->hotpluggable)) {
249 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
250 "pluggable device type");
251 return NULL;
254 return dc;
258 int qdev_device_help(QemuOpts *opts)
260 Error *local_err = NULL;
261 const char *driver;
262 ObjectPropertyInfoList *prop_list;
263 ObjectPropertyInfoList *prop;
265 driver = qemu_opt_get(opts, "driver");
266 if (driver && is_help_option(driver)) {
267 qdev_print_devinfos(false);
268 return 1;
271 if (!driver || !qemu_opt_has_help_opt(opts)) {
272 return 0;
275 if (!object_class_by_name(driver)) {
276 const char *typename = find_typename_by_alias(driver);
278 if (typename) {
279 driver = typename;
283 prop_list = qmp_device_list_properties(driver, &local_err);
284 if (local_err) {
285 goto error;
288 for (prop = prop_list; prop; prop = prop->next) {
289 out_printf("%s.%s=%s", driver, prop->value->name, prop->value->type);
290 if (prop->value->has_description) {
291 out_printf(" (%s)\n", prop->value->description);
292 } else {
293 out_printf("\n");
297 qapi_free_ObjectPropertyInfoList(prop_list);
298 return 1;
300 error:
301 error_report_err(local_err);
302 return 1;
305 static Object *qdev_get_peripheral(void)
307 static Object *dev;
309 if (dev == NULL) {
310 dev = container_get(qdev_get_machine(), "/peripheral");
313 return dev;
316 static Object *qdev_get_peripheral_anon(void)
318 static Object *dev;
320 if (dev == NULL) {
321 dev = container_get(qdev_get_machine(), "/peripheral-anon");
324 return dev;
327 static void qbus_list_bus(DeviceState *dev, Error **errp)
329 BusState *child;
330 const char *sep = " ";
332 error_append_hint(errp, "child buses at \"%s\":",
333 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
334 QLIST_FOREACH(child, &dev->child_bus, sibling) {
335 error_append_hint(errp, "%s\"%s\"", sep, child->name);
336 sep = ", ";
338 error_append_hint(errp, "\n");
341 static void qbus_list_dev(BusState *bus, Error **errp)
343 BusChild *kid;
344 const char *sep = " ";
346 error_append_hint(errp, "devices at \"%s\":", bus->name);
347 QTAILQ_FOREACH(kid, &bus->children, sibling) {
348 DeviceState *dev = kid->child;
349 error_append_hint(errp, "%s\"%s\"", sep,
350 object_get_typename(OBJECT(dev)));
351 if (dev->id) {
352 error_append_hint(errp, "/\"%s\"", dev->id);
354 sep = ", ";
356 error_append_hint(errp, "\n");
359 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
361 BusState *child;
363 QLIST_FOREACH(child, &dev->child_bus, sibling) {
364 if (strcmp(child->name, elem) == 0) {
365 return child;
368 return NULL;
371 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
373 BusChild *kid;
376 * try to match in order:
377 * (1) instance id, if present
378 * (2) driver name
379 * (3) driver alias, if present
381 QTAILQ_FOREACH(kid, &bus->children, sibling) {
382 DeviceState *dev = kid->child;
383 if (dev->id && strcmp(dev->id, elem) == 0) {
384 return dev;
387 QTAILQ_FOREACH(kid, &bus->children, sibling) {
388 DeviceState *dev = kid->child;
389 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
390 return dev;
393 QTAILQ_FOREACH(kid, &bus->children, sibling) {
394 DeviceState *dev = kid->child;
395 DeviceClass *dc = DEVICE_GET_CLASS(dev);
397 if (qdev_class_has_alias(dc) &&
398 strcmp(qdev_class_get_alias(dc), elem) == 0) {
399 return dev;
402 return NULL;
405 static inline bool qbus_is_full(BusState *bus)
407 BusClass *bus_class = BUS_GET_CLASS(bus);
408 return bus_class->max_dev && bus->max_index >= bus_class->max_dev;
412 * Search the tree rooted at @bus for a bus.
413 * If @name, search for a bus with that name. Note that bus names
414 * need not be unique. Yes, that's screwed up.
415 * Else search for a bus that is a subtype of @bus_typename.
416 * If more than one exists, prefer one that can take another device.
417 * Return the bus if found, else %NULL.
419 static BusState *qbus_find_recursive(BusState *bus, const char *name,
420 const char *bus_typename)
422 BusChild *kid;
423 BusState *pick, *child, *ret;
424 bool match;
426 assert(name || bus_typename);
427 if (name) {
428 match = !strcmp(bus->name, name);
429 } else {
430 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
433 if (match && !qbus_is_full(bus)) {
434 return bus; /* root matches and isn't full */
437 pick = match ? bus : NULL;
439 QTAILQ_FOREACH(kid, &bus->children, sibling) {
440 DeviceState *dev = kid->child;
441 QLIST_FOREACH(child, &dev->child_bus, sibling) {
442 ret = qbus_find_recursive(child, name, bus_typename);
443 if (ret && !qbus_is_full(ret)) {
444 return ret; /* a descendant matches and isn't full */
446 if (ret && !pick) {
447 pick = ret;
452 /* root or a descendant matches, but is full */
453 return pick;
456 static BusState *qbus_find(const char *path, Error **errp)
458 DeviceState *dev;
459 BusState *bus;
460 char elem[128];
461 int pos, len;
463 /* find start element */
464 if (path[0] == '/') {
465 bus = sysbus_get_default();
466 pos = 0;
467 } else {
468 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
469 assert(!path[0]);
470 elem[0] = len = 0;
472 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
473 if (!bus) {
474 error_setg(errp, "Bus '%s' not found", elem);
475 return NULL;
477 pos = len;
480 for (;;) {
481 assert(path[pos] == '/' || !path[pos]);
482 while (path[pos] == '/') {
483 pos++;
485 if (path[pos] == '\0') {
486 break;
489 /* find device */
490 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
491 g_assert_not_reached();
492 elem[0] = len = 0;
494 pos += len;
495 dev = qbus_find_dev(bus, elem);
496 if (!dev) {
497 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
498 "Device '%s' not found", elem);
499 qbus_list_dev(bus, errp);
500 return NULL;
503 assert(path[pos] == '/' || !path[pos]);
504 while (path[pos] == '/') {
505 pos++;
507 if (path[pos] == '\0') {
508 /* last specified element is a device. If it has exactly
509 * one child bus accept it nevertheless */
510 if (dev->num_child_bus == 1) {
511 bus = QLIST_FIRST(&dev->child_bus);
512 break;
514 if (dev->num_child_bus) {
515 error_setg(errp, "Device '%s' has multiple child buses",
516 elem);
517 qbus_list_bus(dev, errp);
518 } else {
519 error_setg(errp, "Device '%s' has no child bus", elem);
521 return NULL;
524 /* find bus */
525 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
526 g_assert_not_reached();
527 elem[0] = len = 0;
529 pos += len;
530 bus = qbus_find_bus(dev, elem);
531 if (!bus) {
532 error_setg(errp, "Bus '%s' not found", elem);
533 qbus_list_bus(dev, errp);
534 return NULL;
538 if (qbus_is_full(bus)) {
539 error_setg(errp, "Bus '%s' is full", path);
540 return NULL;
542 return bus;
545 void qdev_set_id(DeviceState *dev, const char *id)
547 if (id) {
548 dev->id = id;
551 if (dev->id) {
552 object_property_add_child(qdev_get_peripheral(), dev->id,
553 OBJECT(dev), NULL);
554 } else {
555 static int anon_count;
556 gchar *name = g_strdup_printf("device[%d]", anon_count++);
557 object_property_add_child(qdev_get_peripheral_anon(), name,
558 OBJECT(dev), NULL);
559 g_free(name);
563 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
565 DeviceClass *dc;
566 const char *driver, *path;
567 DeviceState *dev;
568 BusState *bus = NULL;
569 Error *err = NULL;
571 driver = qemu_opt_get(opts, "driver");
572 if (!driver) {
573 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
574 return NULL;
577 /* find driver */
578 dc = qdev_get_device_class(&driver, errp);
579 if (!dc) {
580 return NULL;
583 /* find bus */
584 path = qemu_opt_get(opts, "bus");
585 if (path != NULL) {
586 bus = qbus_find(path, errp);
587 if (!bus) {
588 return NULL;
590 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
591 error_setg(errp, "Device '%s' can't go on %s bus",
592 driver, object_get_typename(OBJECT(bus)));
593 return NULL;
595 } else if (dc->bus_type != NULL) {
596 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
597 if (!bus || qbus_is_full(bus)) {
598 error_setg(errp, "No '%s' bus found for device '%s'",
599 dc->bus_type, driver);
600 return NULL;
603 if (qdev_hotplug && bus && !qbus_is_hotpluggable(bus)) {
604 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
605 return NULL;
608 if (!migration_is_idle()) {
609 error_setg(errp, "device_add not allowed while migrating");
610 return NULL;
613 /* create device */
614 dev = DEVICE(object_new(driver));
616 if (bus) {
617 qdev_set_parent_bus(dev, bus);
618 } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
619 /* No bus, no machine hotplug handler --> device is not hotpluggable */
620 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
621 driver);
622 goto err_del_dev;
625 qdev_set_id(dev, qemu_opts_id(opts));
627 /* set properties */
628 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
629 goto err_del_dev;
632 dev->opts = opts;
633 object_property_set_bool(OBJECT(dev), true, "realized", &err);
634 if (err != NULL) {
635 dev->opts = NULL;
636 goto err_del_dev;
638 return dev;
640 err_del_dev:
641 error_propagate(errp, err);
642 object_unparent(OBJECT(dev));
643 object_unref(OBJECT(dev));
644 return NULL;
648 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
649 static void qbus_print(Monitor *mon, BusState *bus, int indent);
651 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
652 int indent)
654 if (!props)
655 return;
656 for (; props->name; props++) {
657 Error *err = NULL;
658 char *value;
659 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
660 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
661 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
662 } else {
663 value = object_property_print(OBJECT(dev), props->name, true, &err);
665 g_free(legacy_name);
667 if (err) {
668 error_free(err);
669 continue;
671 qdev_printf("%s = %s\n", props->name,
672 value && *value ? value : "<null>");
673 g_free(value);
677 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
679 BusClass *bc = BUS_GET_CLASS(bus);
681 if (bc->print_dev) {
682 bc->print_dev(mon, dev, indent);
686 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
688 ObjectClass *class;
689 BusState *child;
690 NamedGPIOList *ngl;
692 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
693 dev->id ? dev->id : "");
694 indent += 2;
695 QLIST_FOREACH(ngl, &dev->gpios, node) {
696 if (ngl->num_in) {
697 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
698 ngl->num_in);
700 if (ngl->num_out) {
701 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
702 ngl->num_out);
705 class = object_get_class(OBJECT(dev));
706 do {
707 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
708 class = object_class_get_parent(class);
709 } while (class != object_class_by_name(TYPE_DEVICE));
710 bus_print_dev(dev->parent_bus, mon, dev, indent);
711 QLIST_FOREACH(child, &dev->child_bus, sibling) {
712 qbus_print(mon, child, indent);
716 static void qbus_print(Monitor *mon, BusState *bus, int indent)
718 BusChild *kid;
720 qdev_printf("bus: %s\n", bus->name);
721 indent += 2;
722 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
723 QTAILQ_FOREACH(kid, &bus->children, sibling) {
724 DeviceState *dev = kid->child;
725 qdev_print(mon, dev, indent);
728 #undef qdev_printf
730 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
732 if (sysbus_get_default())
733 qbus_print(mon, sysbus_get_default(), 0);
736 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
738 qdev_print_devinfos(true);
741 typedef struct QOMCompositionState {
742 Monitor *mon;
743 int indent;
744 } QOMCompositionState;
746 static void print_qom_composition(Monitor *mon, Object *obj, int indent);
748 static int print_qom_composition_child(Object *obj, void *opaque)
750 QOMCompositionState *s = opaque;
752 print_qom_composition(s->mon, obj, s->indent);
754 return 0;
757 static void print_qom_composition(Monitor *mon, Object *obj, int indent)
759 QOMCompositionState s = {
760 .mon = mon,
761 .indent = indent + 2,
763 char *name;
765 if (obj == object_get_root()) {
766 name = g_strdup("");
767 } else {
768 name = object_get_canonical_path_component(obj);
770 monitor_printf(mon, "%*s/%s (%s)\n", indent, "", name,
771 object_get_typename(obj));
772 g_free(name);
773 object_child_foreach(obj, print_qom_composition_child, &s);
776 void hmp_info_qom_tree(Monitor *mon, const QDict *dict)
778 const char *path = qdict_get_try_str(dict, "path");
779 Object *obj;
780 bool ambiguous = false;
782 if (path) {
783 obj = object_resolve_path(path, &ambiguous);
784 if (!obj) {
785 monitor_printf(mon, "Path '%s' could not be resolved.\n", path);
786 return;
788 if (ambiguous) {
789 monitor_printf(mon, "Warning: Path '%s' is ambiguous.\n", path);
790 return;
792 } else {
793 obj = qdev_get_machine();
795 print_qom_composition(mon, obj, 0);
798 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
800 Error *local_err = NULL;
801 QemuOpts *opts;
802 DeviceState *dev;
804 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
805 if (local_err) {
806 error_propagate(errp, local_err);
807 return;
809 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
810 qemu_opts_del(opts);
811 return;
813 dev = qdev_device_add(opts, &local_err);
814 if (!dev) {
815 error_propagate(errp, local_err);
816 qemu_opts_del(opts);
817 return;
819 object_unref(OBJECT(dev));
822 static DeviceState *find_device_state(const char *id, Error **errp)
824 Object *obj;
826 if (id[0] == '/') {
827 obj = object_resolve_path(id, NULL);
828 } else {
829 char *root_path = object_get_canonical_path(qdev_get_peripheral());
830 char *path = g_strdup_printf("%s/%s", root_path, id);
832 g_free(root_path);
833 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
834 g_free(path);
837 if (!obj) {
838 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
839 "Device '%s' not found", id);
840 return NULL;
843 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
844 error_setg(errp, "%s is not a hotpluggable device", id);
845 return NULL;
848 return DEVICE(obj);
851 void qdev_unplug(DeviceState *dev, Error **errp)
853 DeviceClass *dc = DEVICE_GET_CLASS(dev);
854 HotplugHandler *hotplug_ctrl;
855 HotplugHandlerClass *hdc;
857 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
858 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
859 return;
862 if (!dc->hotpluggable) {
863 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
864 object_get_typename(OBJECT(dev)));
865 return;
868 if (!migration_is_idle()) {
869 error_setg(errp, "device_del not allowed while migrating");
870 return;
873 qdev_hot_removed = true;
875 hotplug_ctrl = qdev_get_hotplug_handler(dev);
876 /* hotpluggable device MUST have HotplugHandler, if it doesn't
877 * then something is very wrong with it */
878 g_assert(hotplug_ctrl);
880 /* If device supports async unplug just request it to be done,
881 * otherwise just remove it synchronously */
882 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
883 if (hdc->unplug_request) {
884 hotplug_handler_unplug_request(hotplug_ctrl, dev, errp);
885 } else {
886 hotplug_handler_unplug(hotplug_ctrl, dev, errp);
890 void qmp_device_del(const char *id, Error **errp)
892 DeviceState *dev = find_device_state(id, errp);
893 if (dev != NULL) {
894 qdev_unplug(dev, errp);
898 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
900 DeviceState *dev;
901 BlockBackend *blk;
903 dev = find_device_state(id, errp);
904 if (dev == NULL) {
905 return NULL;
908 blk = blk_by_dev(dev);
909 if (!blk) {
910 error_setg(errp, "Device does not have a block device backend");
912 return blk;
915 void qdev_machine_init(void)
917 qdev_get_peripheral_anon();
918 qdev_get_peripheral();
921 QemuOptsList qemu_device_opts = {
922 .name = "device",
923 .implied_opt_name = "driver",
924 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
925 .desc = {
927 * no elements => accept any
928 * sanity checking will happen later
929 * when setting device properties
931 { /* end of list */ }
935 QemuOptsList qemu_global_opts = {
936 .name = "global",
937 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
938 .desc = {
940 .name = "driver",
941 .type = QEMU_OPT_STRING,
943 .name = "property",
944 .type = QEMU_OPT_STRING,
946 .name = "value",
947 .type = QEMU_OPT_STRING,
949 { /* end of list */ }
953 int qemu_global_option(const char *str)
955 char driver[64], property[64];
956 QemuOpts *opts;
957 int rc, offset;
959 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
960 if (rc == 2 && str[offset] == '=') {
961 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
962 qemu_opt_set(opts, "driver", driver, &error_abort);
963 qemu_opt_set(opts, "property", property, &error_abort);
964 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
965 return 0;
968 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
969 if (!opts) {
970 return -1;
973 return 0;