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/>.
21 #include "monitor/monitor.h"
22 #include "qmp-commands.h"
23 #include "sysemu/arch_init.h"
24 #include "qemu/config-file.h"
27 * Aliases were a bad idea from the start. Let's keep them
28 * from spreading further.
30 typedef struct QDevAlias
37 static const QDevAlias qdev_alias_table
[] = {
38 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
39 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
40 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
41 { "virtio-balloon-pci", "virtio-balloon",
42 QEMU_ARCH_ALL
& ~QEMU_ARCH_S390X
},
43 { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X
},
44 { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X
},
45 { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X
},
46 { "lsi53c895a", "lsi" },
47 { "ich9-ahci", "ahci" },
48 { "kvm-pci-assign", "pci-assign" },
52 static const char *qdev_class_get_alias(DeviceClass
*dc
)
54 const char *typename
= object_class_get_name(OBJECT_CLASS(dc
));
57 for (i
= 0; qdev_alias_table
[i
].typename
; i
++) {
58 if (qdev_alias_table
[i
].arch_mask
&&
59 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
63 if (strcmp(qdev_alias_table
[i
].typename
, typename
) == 0) {
64 return qdev_alias_table
[i
].alias
;
71 static bool qdev_class_has_alias(DeviceClass
*dc
)
73 return (qdev_class_get_alias(dc
) != NULL
);
76 static void qdev_print_devinfo(ObjectClass
*klass
, void *opaque
)
79 bool *show_no_user
= opaque
;
81 dc
= (DeviceClass
*)object_class_dynamic_cast(klass
, TYPE_DEVICE
);
83 if (!dc
|| (show_no_user
&& !*show_no_user
&& dc
->no_user
)) {
87 error_printf("name \"%s\"", object_class_get_name(klass
));
89 error_printf(", bus %s", dc
->bus_type
);
91 if (qdev_class_has_alias(dc
)) {
92 error_printf(", alias \"%s\"", qdev_class_get_alias(dc
));
95 error_printf(", desc \"%s\"", dc
->desc
);
98 error_printf(", no-user");
103 static int set_property(const char *name
, const char *value
, void *opaque
)
105 DeviceState
*dev
= opaque
;
107 if (strcmp(name
, "driver") == 0)
109 if (strcmp(name
, "bus") == 0)
112 if (qdev_prop_parse(dev
, name
, value
) == -1) {
118 static const char *find_typename_by_alias(const char *alias
)
122 for (i
= 0; qdev_alias_table
[i
].alias
; i
++) {
123 if (qdev_alias_table
[i
].arch_mask
&&
124 !(qdev_alias_table
[i
].arch_mask
& arch_type
)) {
128 if (strcmp(qdev_alias_table
[i
].alias
, alias
) == 0) {
129 return qdev_alias_table
[i
].typename
;
136 int qdev_device_help(QemuOpts
*opts
)
142 driver
= qemu_opt_get(opts
, "driver");
143 if (driver
&& is_help_option(driver
)) {
144 bool show_no_user
= false;
145 object_class_foreach(qdev_print_devinfo
, TYPE_DEVICE
, false, &show_no_user
);
149 if (!driver
|| !qemu_opt_has_help_opt(opts
)) {
153 klass
= object_class_by_name(driver
);
155 const char *typename
= find_typename_by_alias(driver
);
159 klass
= object_class_by_name(driver
);
167 for (prop
= DEVICE_CLASS(klass
)->props
; prop
&& prop
->name
; prop
++) {
169 * TODO Properties without a parser are just for dirty hacks.
170 * qdev_prop_ptr is the only such PropertyInfo. It's marked
171 * for removal. This conditional should be removed along with
174 if (!prop
->info
->set
) {
175 continue; /* no way to set it, don't show */
177 error_printf("%s.%s=%s\n", driver
, prop
->name
,
178 prop
->info
->legacy_name
?: prop
->info
->name
);
180 klass
= object_class_get_parent(klass
);
181 } while (klass
!= object_class_by_name(TYPE_DEVICE
));
185 static Object
*qdev_get_peripheral(void)
190 dev
= container_get(qdev_get_machine(), "/peripheral");
196 static Object
*qdev_get_peripheral_anon(void)
201 dev
= container_get(qdev_get_machine(), "/peripheral-anon");
207 static void qbus_list_bus(DeviceState
*dev
)
210 const char *sep
= " ";
212 error_printf("child busses at \"%s\":",
213 dev
->id
? dev
->id
: object_get_typename(OBJECT(dev
)));
214 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
215 error_printf("%s\"%s\"", sep
, child
->name
);
221 static void qbus_list_dev(BusState
*bus
)
224 const char *sep
= " ";
226 error_printf("devices at \"%s\":", bus
->name
);
227 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
228 DeviceState
*dev
= kid
->child
;
229 error_printf("%s\"%s\"", sep
, object_get_typename(OBJECT(dev
)));
231 error_printf("/\"%s\"", dev
->id
);
237 static BusState
*qbus_find_bus(DeviceState
*dev
, char *elem
)
241 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
242 if (strcmp(child
->name
, elem
) == 0) {
249 static DeviceState
*qbus_find_dev(BusState
*bus
, char *elem
)
254 * try to match in order:
255 * (1) instance id, if present
257 * (3) driver alias, if present
259 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
260 DeviceState
*dev
= kid
->child
;
261 if (dev
->id
&& strcmp(dev
->id
, elem
) == 0) {
265 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
266 DeviceState
*dev
= kid
->child
;
267 if (strcmp(object_get_typename(OBJECT(dev
)), elem
) == 0) {
271 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
272 DeviceState
*dev
= kid
->child
;
273 DeviceClass
*dc
= DEVICE_GET_CLASS(dev
);
275 if (qdev_class_has_alias(dc
) &&
276 strcmp(qdev_class_get_alias(dc
), elem
) == 0) {
283 static BusState
*qbus_find_recursive(BusState
*bus
, const char *name
,
284 const char *bus_typename
)
286 BusClass
*bus_class
= BUS_GET_CLASS(bus
);
288 BusState
*child
, *ret
;
291 if (name
&& (strcmp(bus
->name
, name
) != 0)) {
294 if (bus_typename
&& !object_dynamic_cast(OBJECT(bus
), bus_typename
)) {
297 if ((bus_class
->max_dev
!= 0) && (bus_class
->max_dev
<= bus
->max_index
)) {
299 /* bus was explicitly specified: return an error. */
300 qerror_report(ERROR_CLASS_GENERIC_ERROR
, "Bus '%s' is full",
304 /* bus was not specified: try to find another one. */
312 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
313 DeviceState
*dev
= kid
->child
;
314 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
315 ret
= qbus_find_recursive(child
, name
, bus_typename
);
324 static BusState
*qbus_find(const char *path
)
331 /* find start element */
332 if (path
[0] == '/') {
333 bus
= sysbus_get_default();
336 if (sscanf(path
, "%127[^/]%n", elem
, &len
) != 1) {
340 bus
= qbus_find_recursive(sysbus_get_default(), elem
, NULL
);
342 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
349 assert(path
[pos
] == '/' || !path
[pos
]);
350 while (path
[pos
] == '/') {
353 if (path
[pos
] == '\0') {
358 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
363 dev
= qbus_find_dev(bus
, elem
);
365 qerror_report(QERR_DEVICE_NOT_FOUND
, elem
);
366 if (!monitor_cur_is_qmp()) {
372 assert(path
[pos
] == '/' || !path
[pos
]);
373 while (path
[pos
] == '/') {
376 if (path
[pos
] == '\0') {
377 /* last specified element is a device. If it has exactly
378 * one child bus accept it nevertheless */
379 switch (dev
->num_child_bus
) {
381 qerror_report(QERR_DEVICE_NO_BUS
, elem
);
384 return QLIST_FIRST(&dev
->child_bus
);
386 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES
, elem
);
387 if (!monitor_cur_is_qmp()) {
395 if (sscanf(path
+pos
, "%127[^/]%n", elem
, &len
) != 1) {
400 bus
= qbus_find_bus(dev
, elem
);
402 qerror_report(QERR_BUS_NOT_FOUND
, elem
);
403 if (!monitor_cur_is_qmp()) {
411 DeviceState
*qdev_device_add(QemuOpts
*opts
)
415 const char *driver
, *path
, *id
;
419 driver
= qemu_opt_get(opts
, "driver");
421 qerror_report(QERR_MISSING_PARAMETER
, "driver");
426 obj
= object_class_by_name(driver
);
428 const char *typename
= find_typename_by_alias(driver
);
432 obj
= object_class_by_name(driver
);
437 qerror_report(QERR_INVALID_PARAMETER_VALUE
, "driver", "device type");
441 k
= DEVICE_CLASS(obj
);
444 path
= qemu_opt_get(opts
, "bus");
446 bus
= qbus_find(path
);
450 if (!object_dynamic_cast(OBJECT(bus
), k
->bus_type
)) {
451 qerror_report(QERR_BAD_BUS_FOR_DEVICE
,
452 driver
, object_get_typename(OBJECT(bus
)));
456 bus
= qbus_find_recursive(sysbus_get_default(), NULL
, k
->bus_type
);
458 qerror_report(QERR_NO_BUS_FOR_DEVICE
,
459 k
->bus_type
, driver
);
463 if (qdev_hotplug
&& !bus
->allow_hotplug
) {
464 qerror_report(QERR_BUS_NO_HOTPLUG
, bus
->name
);
469 bus
= sysbus_get_default();
472 /* create device, set properties */
473 qdev
= DEVICE(object_new(driver
));
474 qdev_set_parent_bus(qdev
, bus
);
476 id
= qemu_opts_id(opts
);
480 if (qemu_opt_foreach(opts
, set_property
, qdev
, 1) != 0) {
485 object_property_add_child(qdev_get_peripheral(), qdev
->id
,
488 static int anon_count
;
489 gchar
*name
= g_strdup_printf("device[%d]", anon_count
++);
490 object_property_add_child(qdev_get_peripheral_anon(), name
,
494 if (qdev_init(qdev
) < 0) {
495 qerror_report(QERR_DEVICE_INIT_FAILED
, driver
);
503 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
504 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
);
506 static void qdev_print_props(Monitor
*mon
, DeviceState
*dev
, Property
*props
,
511 for (; props
->name
; props
++) {
514 char *legacy_name
= g_strdup_printf("legacy-%s", props
->name
);
515 if (object_property_get_type(OBJECT(dev
), legacy_name
, NULL
)) {
516 value
= object_property_get_str(OBJECT(dev
), legacy_name
, &err
);
518 value
= object_property_print(OBJECT(dev
), props
->name
, &err
);
526 qdev_printf("%s = %s\n", props
->name
,
527 value
&& *value
? value
: "<null>");
532 static void bus_print_dev(BusState
*bus
, Monitor
*mon
, DeviceState
*dev
, int indent
)
534 BusClass
*bc
= BUS_GET_CLASS(bus
);
537 bc
->print_dev(mon
, dev
, indent
);
541 static void qdev_print(Monitor
*mon
, DeviceState
*dev
, int indent
)
545 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev
)),
546 dev
->id
? dev
->id
: "");
548 if (dev
->num_gpio_in
) {
549 qdev_printf("gpio-in %d\n", dev
->num_gpio_in
);
551 if (dev
->num_gpio_out
) {
552 qdev_printf("gpio-out %d\n", dev
->num_gpio_out
);
554 class = object_get_class(OBJECT(dev
));
556 qdev_print_props(mon
, dev
, DEVICE_CLASS(class)->props
, indent
);
557 class = object_class_get_parent(class);
558 } while (class != object_class_by_name(TYPE_DEVICE
));
559 bus_print_dev(dev
->parent_bus
, mon
, dev
, indent
);
560 QLIST_FOREACH(child
, &dev
->child_bus
, sibling
) {
561 qbus_print(mon
, child
, indent
);
565 static void qbus_print(Monitor
*mon
, BusState
*bus
, int indent
)
569 qdev_printf("bus: %s\n", bus
->name
);
571 qdev_printf("type %s\n", object_get_typename(OBJECT(bus
)));
572 QTAILQ_FOREACH(kid
, &bus
->children
, sibling
) {
573 DeviceState
*dev
= kid
->child
;
574 qdev_print(mon
, dev
, indent
);
579 void do_info_qtree(Monitor
*mon
, const QDict
*qdict
)
581 if (sysbus_get_default())
582 qbus_print(mon
, sysbus_get_default(), 0);
585 void do_info_qdm(Monitor
*mon
, const QDict
*qdict
)
587 object_class_foreach(qdev_print_devinfo
, TYPE_DEVICE
, false, NULL
);
590 int do_device_add(Monitor
*mon
, const QDict
*qdict
, QObject
**ret_data
)
592 Error
*local_err
= NULL
;
595 opts
= qemu_opts_from_qdict(qemu_find_opts("device"), qdict
, &local_err
);
596 if (error_is_set(&local_err
)) {
597 qerror_report_err(local_err
);
598 error_free(local_err
);
601 if (!monitor_cur_is_qmp() && qdev_device_help(opts
)) {
605 if (!qdev_device_add(opts
)) {
612 void qmp_device_del(const char *id
, Error
**errp
)
616 dev
= qdev_find_recursive(sysbus_get_default(), id
);
618 error_set(errp
, QERR_DEVICE_NOT_FOUND
, id
);
622 qdev_unplug(dev
, errp
);
625 void qdev_machine_init(void)
627 qdev_get_peripheral_anon();
628 qdev_get_peripheral();
631 QemuOptsList qemu_device_opts
= {
633 .implied_opt_name
= "driver",
634 .head
= QTAILQ_HEAD_INITIALIZER(qemu_device_opts
.head
),
637 * no elements => accept any
638 * sanity checking will happen later
639 * when setting device properties
641 { /* end of list */ }
645 QemuOptsList qemu_global_opts
= {
647 .head
= QTAILQ_HEAD_INITIALIZER(qemu_global_opts
.head
),
651 .type
= QEMU_OPT_STRING
,
654 .type
= QEMU_OPT_STRING
,
657 .type
= QEMU_OPT_STRING
,
659 { /* end of list */ }
663 int qemu_global_option(const char *str
)
665 char driver
[64], property
[64];
669 rc
= sscanf(str
, "%63[^.].%63[^=]%n", driver
, property
, &offset
);
670 if (rc
< 2 || str
[offset
] != '=') {
671 error_report("can't parse: \"%s\"", str
);
675 opts
= qemu_opts_create_nofail(&qemu_global_opts
);
676 qemu_opt_set(opts
, "driver", driver
);
677 qemu_opt_set(opts
, "property", property
);
678 qemu_opt_set(opts
, "value", str
+offset
+1);