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"
22 #include "hw/sysbus.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "qmp-commands.h"
26 #include "sysemu/arch_init.h"
27 #include "qapi/qmp/qerror.h"
28 #include "qemu/config-file.h"
29 #include "qemu/error-report.h"
30 #include "qemu/help_option.h"
33 * Aliases were a bad idea from the start. Let's keep them
34 * from spreading further.
36 typedef struct QDevAlias
43 /* Please keep this table sorted by typename. */
44 static const QDevAlias qdev_alias_table
[] = {
45 { "e1000", "e1000-82540em" },
46 { "ich9-ahci", "ahci" },
47 { "kvm-pci-assign", "pci-assign" },
48 { "lsi53c895a", "lsi" },
49 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X
},
50 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
51 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X
},
52 { "virtio-balloon-pci", "virtio-balloon",
53 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
54 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X
},
55 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
56 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X
},
57 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
58 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X
},
59 { "virtio-input-host-pci", "virtio-input-host",
60 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
61 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X
},
62 { "virtio-keyboard-pci", "virtio-keyboard",
63 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
64 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X
},
65 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
66 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X
},
67 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
68 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X
},
69 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
70 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X
},
71 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
72 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X
},
73 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
74 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X
},
75 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
79 static const char *qdev_class_get_alias(DeviceClass
*dc
)
81 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
84 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
85 if (qdev_alias_table
[i
].arch_mask
&&
86 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
90 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
91 return qdev_alias_table
[i
].alias
;
98 static bool qdev_class_has_alias(DeviceClass
*dc
)
100 return (qdev_class_get_alias(dc
) != NULL
);
103 static void qdev_print_devinfo(DeviceClass
*dc
)
105 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc
)));
107 error_printf(", bus %s", dc
->bus_type
);
109 if (qdev_class_has_alias(dc
)) {
110 error_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
113 error_printf(", desc \"%s\"", dc
->desc
);
115 if (dc
->cannot_instantiate_with_device_add_yet
) {
116 error_printf(", no-user");
121 static gint
devinfo_cmp(gconstpointer a
, gconstpointer b
)
123 return strcasecmp(object_class_get_name((ObjectClass
*)a
),
124 object_class_get_name((ObjectClass
*)b
));
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_MAX
] = "Uncategorized",
144 list
= g_slist_sort(object_class_get_list(TYPE_DEVICE
, false),
147 for (i
= 0; i
<= DEVICE_CATEGORY_MAX
; i
++) {
149 for (elt
= list
; elt
; elt
= elt
->next
) {
150 DeviceClass
*dc
= OBJECT_CLASS_CHECK(DeviceClass
, elt
->data
,
152 if ((i
< DEVICE_CATEGORY_MAX
153 ? !test_bit(i
, dc
->categories
)
154 : !bitmap_empty(dc
->categories
, DEVICE_CATEGORY_MAX
))
156 && dc
->cannot_instantiate_with_device_add_yet
)) {
160 error_printf("%s%s devices:\n", i
? "\n" : "",
164 qdev_print_devinfo(dc
);
171 static int set_property(void *opaque
, const char *name
, const char *value
,
174 Object
*obj
= opaque
;
177 if (strcmp(name
, "driver") == 0)
179 if (strcmp(name
, "bus") == 0)
182 object_property_parse(obj
, value
, name
, &err
);
184 error_propagate(errp
, err
);
190 static const char *find_typename_by_alias(const char *alias
)
194 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
195 if (qdev_alias_table
[i
].arch_mask
&&
196 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
200 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
201 return qdev_alias_table
[i
].typename
;
208 static DeviceClass
*qdev_get_device_class(const char **driver
, Error
**errp
)
212 const char *original_name
= *driver
;
214 oc
= object_class_by_name(*driver
);
216 const char *typename
= find_typename_by_alias(*driver
);
220 oc
= object_class_by_name(*driver
);
224 if (!object_class_dynamic_cast(oc
, TYPE_DEVICE
)) {
225 if (*driver
!= original_name
) {
226 error_setg(errp
, "'%s' (alias '%s') is not a valid device model"
227 " name", original_name
, *driver
);
229 error_setg(errp
, "'%s' is not a valid device model name", *driver
);
234 if (object_class_is_abstract(oc
)) {
235 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
236 "non-abstract device type");
240 dc
= DEVICE_CLASS(oc
);
241 if (dc
->cannot_instantiate_with_device_add_yet
||
242 (qdev_hotplug
&& !dc
->hotpluggable
)) {
243 error_setg(errp
, QERR_INVALID_PARAMETER_VALUE
, "driver",
244 "pluggable device type");
252 int qdev_device_help(QemuOpts
*opts
)
254 Error
*local_err
= NULL
;
256 DevicePropertyInfoList
*prop_list
;
257 DevicePropertyInfoList
*prop
;
259 driver
= qemu_opt_get(opts
, "driver");
260 if (driver
&& is_help_option(driver
)) {
261 qdev_print_devinfos(false);
265 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
269 if (!object_class_by_name(driver
)) {
270 const char *typename
= find_typename_by_alias(driver
);
277 prop_list
= qmp_device_list_properties(driver
, &local_err
);
282 for (prop
= prop_list
; prop
; prop
= prop
->next
) {
283 error_printf("%s.%s=%s", driver
,
286 if (prop
->value
->has_description
) {
287 error_printf(" (%s)\n", prop
->value
->description
);
293 qapi_free_DevicePropertyInfoList(prop_list
);
297 error_report_err(local_err
);
301 static Object
*qdev_get_peripheral(void)
306 dev
= container_get(qdev_get_machine(), "/peripheral");
312 static Object
*qdev_get_peripheral_anon(void)
317 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
323 static void qbus_list_bus(DeviceState
*dev
, Error
**errp
)
326 const char *sep
= " ";
328 error_append_hint(errp
, "child buses at \"%s\":",
329 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
330 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
331 error_append_hint(errp
, "%s\"%s\"", sep
, child
->name
);
334 error_append_hint(errp
, "\n");
337 static void qbus_list_dev(BusState
*bus
, Error
**errp
)
340 const char *sep
= " ";
342 error_append_hint(errp
, "devices at \"%s\":", bus
->name
);
343 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
344 DeviceState
*dev
= kid
->child
;
345 error_append_hint(errp
, "%s\"%s\"", sep
,
346 object_get_typename(OBJECT(dev
)));
348 error_append_hint(errp
, "/\"%s\"", dev
->id
);
352 error_append_hint(errp
, "\n");
355 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
359 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
360 if (strcmp(child
->name
, elem
) == 0) {
367 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
372 * try to match in order:
373 * (1) instance id, if present
375 * (3) driver alias, if present
377 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
378 DeviceState
*dev
= kid
->child
;
379 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
383 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
384 DeviceState
*dev
= kid
->child
;
385 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
389 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
390 DeviceState
*dev
= kid
->child
;
391 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
393 if (qdev_class_has_alias(dc
) &&
394 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
401 static inline bool qbus_is_full(BusState
*bus
)
403 BusClass
*bus_class
= BUS_GET_CLASS(bus
);
404 return bus_class
->max_dev
&& bus
->max_index
>= bus_class
->max_dev
;
408 * Search the tree rooted at @bus for a bus.
409 * If @name, search for a bus with that name. Note that bus names
410 * need not be unique. Yes, that's screwed up.
411 * Else search for a bus that is a subtype of @bus_typename.
412 * If more than one exists, prefer one that can take another device.
413 * Return the bus if found, else %NULL.
415 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
416 const char *bus_typename
)
419 BusState
*pick
, *child
, *ret
;
422 assert(name
|| bus_typename
);
424 match
= !strcmp(bus
->name
, name
);
426 match
= !!object_dynamic_cast(OBJECT(bus
), bus_typename
);
429 if (match
&& !qbus_is_full(bus
)) {
430 return bus
; /* root matches and isn't full */
433 pick
= match
? bus
: NULL
;
435 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
436 DeviceState
*dev
= kid
->child
;
437 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
438 ret
= qbus_find_recursive(child
, name
, bus_typename
);
439 if (ret
&& !qbus_is_full(ret
)) {
440 return ret
; /* a descendant matches and isn't full */
448 /* root or a descendant matches, but is full */
452 static BusState
*qbus_find(const char *path
, Error
**errp
)
459 /* find start element */
460 if (path
[0] == '/') {
461 bus
= sysbus_get_default();
464 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
468 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
470 error_setg(errp
, "Bus '%s' not found", elem
);
477 assert(path
[pos
] == '/' || !path
[pos
]);
478 while (path
[pos
] == '/') {
481 if (path
[pos
] == '\0') {
486 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
487 g_assert_not_reached();
491 dev
= qbus_find_dev(bus
, elem
);
493 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
494 "Device '%s' not found", elem
);
495 qbus_list_dev(bus
, errp
);
499 assert(path
[pos
] == '/' || !path
[pos
]);
500 while (path
[pos
] == '/') {
503 if (path
[pos
] == '\0') {
504 /* last specified element is a device. If it has exactly
505 * one child bus accept it nevertheless */
506 if (dev
->num_child_bus
== 1) {
507 bus
= QLIST_FIRST(&dev
->child_bus
);
510 if (dev
->num_child_bus
) {
511 error_setg(errp
, "Device '%s' has multiple child buses",
513 qbus_list_bus(dev
, errp
);
515 error_setg(errp
, "Device '%s' has no child bus", elem
);
521 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
522 g_assert_not_reached();
526 bus
= qbus_find_bus(dev
, elem
);
528 error_setg(errp
, "Bus '%s' not found", elem
);
529 qbus_list_bus(dev
, errp
);
534 if (qbus_is_full(bus
)) {
535 error_setg(errp
, "Bus '%s' is full", path
);
541 DeviceState
*qdev_device_add(QemuOpts
*opts
, Error
**errp
)
544 const char *driver
, *path
, *id
;
546 BusState
*bus
= NULL
;
549 driver
= qemu_opt_get(opts
, "driver");
551 error_setg(errp
, QERR_MISSING_PARAMETER
, "driver");
556 dc
= qdev_get_device_class(&driver
, errp
);
562 path
= qemu_opt_get(opts
, "bus");
564 bus
= qbus_find(path
, errp
);
568 if (!object_dynamic_cast(OBJECT(bus
), dc
->bus_type
)) {
569 error_setg(errp
, "Device '%s' can't go on %s bus",
570 driver
, object_get_typename(OBJECT(bus
)));
573 } else if (dc
->bus_type
!= NULL
) {
574 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, dc
->bus_type
);
575 if (!bus
|| qbus_is_full(bus
)) {
576 error_setg(errp
, "No '%s' bus found for device '%s'",
577 dc
->bus_type
, driver
);
581 if (qdev_hotplug
&& bus
&& !qbus_is_hotpluggable(bus
)) {
582 error_setg(errp
, QERR_BUS_NO_HOTPLUG
, bus
->name
);
587 dev
= DEVICE(object_new(driver
));
590 qdev_set_parent_bus(dev
, bus
);
593 id
= qemu_opts_id(opts
);
599 object_property_add_child(qdev_get_peripheral(), dev
->id
,
602 static int anon_count
;
603 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
604 object_property_add_child(qdev_get_peripheral_anon(), name
,
610 if (qemu_opt_foreach(opts
, set_property
, dev
, &err
)) {
611 error_propagate(errp
, err
);
612 object_unparent(OBJECT(dev
));
613 object_unref(OBJECT(dev
));
618 object_property_set_bool(OBJECT(dev
), true, "realized", &err
);
620 error_propagate(errp
, err
);
622 object_unparent(OBJECT(dev
));
623 object_unref(OBJECT(dev
));
630 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
631 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
633 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
638 for (; props
->name
; props
++) {
641 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
642 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
643 value
= object_property_get_str(OBJECT(dev
), legacy_name
, &err
);
645 value
= object_property_print(OBJECT(dev
), props
->name
, true, &err
);
653 qdev_printf("%s = %s\n", props
->name
,
654 value
&& *value
? value
: "<null>");
659 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
661 BusClass
*bc
= BUS_GET_CLASS(bus
);
664 bc
->print_dev(mon
, dev
, indent
);
668 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
674 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
675 dev
->id
? dev
->id
: "");
677 QLIST_FOREACH(ngl
, &dev
->gpios
, node
) {
679 qdev_printf("gpio-in \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
683 qdev_printf("gpio-out \"%s\" %d\n", ngl
->name
? ngl
->name
: "",
687 class = object_get_class(OBJECT(dev
));
689 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props
, indent
);
690 class = object_class_get_parent(class);
691 } while (class != object_class_by_name(TYPE_DEVICE
));
692 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
693 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
694 qbus_print(mon
, child
, indent
);
698 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
702 qdev_printf("bus: %s\n", bus
->name
);
704 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
705 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
706 DeviceState
*dev
= kid
->child
;
707 qdev_print(mon
, dev
, indent
);
712 void hmp_info_qtree(Monitor
*mon
, const QDict
*qdict
)
714 if (sysbus_get_default())
715 qbus_print(mon
, sysbus_get_default(), 0);
718 void hmp_info_qdm(Monitor
*mon
, const QDict
*qdict
)
720 qdev_print_devinfos(true);
723 typedef struct QOMCompositionState
{
726 } QOMCompositionState
;
728 static void print_qom_composition(Monitor
*mon
, Object
*obj
, int indent
);
730 static int print_qom_composition_child(Object
*obj
, void *opaque
)
732 QOMCompositionState
*s
= opaque
;
734 print_qom_composition(s
->mon
, obj
, s
->indent
);
739 static void print_qom_composition(Monitor
*mon
, Object
*obj
, int indent
)
741 QOMCompositionState s
= {
743 .indent
= indent
+ 2,
747 if (obj
== object_get_root()) {
750 name
= object_get_canonical_path_component(obj
);
752 monitor_printf(mon
, "%*s/%s (%s)\n", indent
, "", name
,
753 object_get_typename(obj
));
755 object_child_foreach(obj
, print_qom_composition_child
, &s
);
758 void hmp_info_qom_tree(Monitor
*mon
, const QDict
*dict
)
760 const char *path
= qdict_get_try_str(dict
, "path");
762 bool ambiguous
= false;
765 obj
= object_resolve_path(path
, &ambiguous
);
767 monitor_printf(mon
, "Path '%s' could not be resolved.\n", path
);
771 monitor_printf(mon
, "Warning: Path '%s' is ambiguous.\n", path
);
775 obj
= qdev_get_machine();
777 print_qom_composition(mon
, obj
, 0);
780 void qmp_device_add(QDict
*qdict
, QObject
**ret_data
, Error
**errp
)
782 Error
*local_err
= NULL
;
786 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, &local_err
);
788 error_propagate(errp
, local_err
);
791 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
795 dev
= qdev_device_add(opts
, &local_err
);
797 error_propagate(errp
, local_err
);
801 object_unref(OBJECT(dev
));
804 void qmp_device_del(const char *id
, Error
**errp
)
809 obj
= object_resolve_path(id
, NULL
);
811 char *root_path
= object_get_canonical_path(qdev_get_peripheral());
812 char *path
= g_strdup_printf("%s/%s", root_path
, id
);
815 obj
= object_resolve_path_type(path
, TYPE_DEVICE
, NULL
);
820 error_set(errp
, ERROR_CLASS_DEVICE_NOT_FOUND
,
821 "Device '%s' not found", id
);
825 if (!object_dynamic_cast(obj
, TYPE_DEVICE
)) {
826 error_setg(errp
, "%s is not a hotpluggable device", id
);
830 qdev_unplug(DEVICE(obj
), errp
);
833 void qdev_machine_init(void)
835 qdev_get_peripheral_anon();
836 qdev_get_peripheral();
839 QemuOptsList qemu_device_opts
= {
841 .implied_opt_name
= "driver",
842 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
845 * no elements => accept any
846 * sanity checking will happen later
847 * when setting device properties
849 { /* end of list */ }
853 QemuOptsList qemu_global_opts
= {
855 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
859 .type
= QEMU_OPT_STRING
,
862 .type
= QEMU_OPT_STRING
,
865 .type
= QEMU_OPT_STRING
,
867 { /* end of list */ }
871 int qemu_global_option(const char *str
)
873 char driver
[64], property
[64];
877 rc
= sscanf(str
, "%63[^.=].%63[^=]%n", driver
, property
, &offset
);
878 if (rc
== 2 && str
[offset
] == '=') {
879 opts
= qemu_opts_create(&qemu_global_opts
, NULL
, 0, &error_abort
);
880 qemu_opt_set(opts
, "driver", driver
, &error_abort
);
881 qemu_opt_set(opts
, "property", property
, &error_abort
);
882 qemu_opt_set(opts
, "value", str
+ offset
+ 1, &error_abort
);
886 opts
= qemu_opts_parse_noisily(&qemu_global_opts
, str
, false);