block/qcow2.h: Avoid "1LL << 63" (shifts into sign bit)
[qemu/ar7.git] / qdev-monitor.c
blob410cdcbe97bc7510a6a4fe75e63f95a89c1100cf
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 "hw/qdev.h"
21 #include "hw/sysbus.h"
22 #include "monitor/monitor.h"
23 #include "monitor/qdev.h"
24 #include "qmp-commands.h"
25 #include "sysemu/arch_init.h"
26 #include "qemu/config-file.h"
29 * Aliases were a bad idea from the start. Let's keep them
30 * from spreading further.
32 typedef struct QDevAlias
34 const char *typename;
35 const char *alias;
36 uint32_t arch_mask;
37 } QDevAlias;
39 static const QDevAlias qdev_alias_table[] = {
40 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
41 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
42 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
43 { "virtio-balloon-pci", "virtio-balloon",
44 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
45 { "virtio-blk-s390", "virtio-blk", QEMU_ARCH_S390X },
46 { "virtio-net-s390", "virtio-net", QEMU_ARCH_S390X },
47 { "virtio-serial-s390", "virtio-serial", QEMU_ARCH_S390X },
48 { "lsi53c895a", "lsi" },
49 { "ich9-ahci", "ahci" },
50 { "kvm-pci-assign", "pci-assign" },
51 { }
54 static const char *qdev_class_get_alias(DeviceClass *dc)
56 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
57 int i;
59 for (i = 0; qdev_alias_table[i].typename; i++) {
60 if (qdev_alias_table[i].arch_mask &&
61 !(qdev_alias_table[i].arch_mask & arch_type)) {
62 continue;
65 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
66 return qdev_alias_table[i].alias;
70 return NULL;
73 static bool qdev_class_has_alias(DeviceClass *dc)
75 return (qdev_class_get_alias(dc) != NULL);
78 static void qdev_print_class_devinfo(DeviceClass *dc)
80 DeviceCategory category;
82 if (!dc) {
83 return;
86 error_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
87 if (dc->bus_type) {
88 error_printf(", bus %s", dc->bus_type);
90 if (qdev_class_has_alias(dc)) {
91 error_printf(", alias \"%s\"", qdev_class_get_alias(dc));
93 error_printf(", categories");
94 for (category = 0; category < DEVICE_CATEGORY_MAX; ++category) {
95 if (test_bit(category, dc->categories)) {
96 error_printf(" \"%s\"", qdev_category_get_name(category));
99 if (dc->desc) {
100 error_printf(", desc \"%s\"", dc->desc);
102 if (dc->no_user) {
103 error_printf(", no-user");
105 error_printf("\n");
108 static void qdev_print_devinfo(ObjectClass *klass, void *opaque)
110 DeviceClass *dc;
112 dc = (DeviceClass *)object_class_dynamic_cast(klass, TYPE_DEVICE);
114 qdev_print_class_devinfo(dc);
117 static int set_property(const char *name, const char *value, void *opaque)
119 DeviceState *dev = opaque;
120 Error *err = NULL;
122 if (strcmp(name, "driver") == 0)
123 return 0;
124 if (strcmp(name, "bus") == 0)
125 return 0;
127 qdev_prop_parse(dev, name, value, &err);
128 if (err != NULL) {
129 qerror_report_err(err);
130 error_free(err);
131 return -1;
133 return 0;
136 static const char *find_typename_by_alias(const char *alias)
138 int i;
140 for (i = 0; qdev_alias_table[i].alias; i++) {
141 if (qdev_alias_table[i].arch_mask &&
142 !(qdev_alias_table[i].arch_mask & arch_type)) {
143 continue;
146 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
147 return qdev_alias_table[i].typename;
151 return NULL;
154 static void qdev_print_category_devices(DeviceCategory category)
156 DeviceClass *dc;
157 GSList *list, *curr;
159 list = object_class_get_list(TYPE_DEVICE, false);
160 for (curr = list; curr; curr = g_slist_next(curr)) {
161 dc = (DeviceClass *)object_class_dynamic_cast(curr->data, TYPE_DEVICE);
162 if (!dc->no_user && test_bit(category, dc->categories)) {
163 qdev_print_class_devinfo(dc);
166 g_slist_free(list);
169 int qdev_device_help(QemuOpts *opts)
171 const char *driver;
172 Property *prop;
173 ObjectClass *klass;
175 driver = qemu_opt_get(opts, "driver");
176 if (driver && is_help_option(driver)) {
177 DeviceCategory category;
178 for (category = 0; category < DEVICE_CATEGORY_MAX; ++category) {
179 qdev_print_category_devices(category);
182 return 1;
185 if (!driver || !qemu_opt_has_help_opt(opts)) {
186 return 0;
189 klass = object_class_by_name(driver);
190 if (!klass) {
191 const char *typename = find_typename_by_alias(driver);
193 if (typename) {
194 driver = typename;
195 klass = object_class_by_name(driver);
199 if (!klass) {
200 return 0;
202 do {
203 for (prop = DEVICE_CLASS(klass)->props; prop && prop->name; prop++) {
205 * TODO Properties without a parser are just for dirty hacks.
206 * qdev_prop_ptr is the only such PropertyInfo. It's marked
207 * for removal. This conditional should be removed along with
208 * it.
210 if (!prop->info->set) {
211 continue; /* no way to set it, don't show */
213 error_printf("%s.%s=%s\n", driver, prop->name,
214 prop->info->legacy_name ?: prop->info->name);
216 klass = object_class_get_parent(klass);
217 } while (klass != object_class_by_name(TYPE_DEVICE));
218 return 1;
221 static Object *qdev_get_peripheral(void)
223 static Object *dev;
225 if (dev == NULL) {
226 dev = container_get(qdev_get_machine(), "/peripheral");
229 return dev;
232 static Object *qdev_get_peripheral_anon(void)
234 static Object *dev;
236 if (dev == NULL) {
237 dev = container_get(qdev_get_machine(), "/peripheral-anon");
240 return dev;
243 static void qbus_list_bus(DeviceState *dev)
245 BusState *child;
246 const char *sep = " ";
248 error_printf("child busses at \"%s\":",
249 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
250 QLIST_FOREACH(child, &dev->child_bus, sibling) {
251 error_printf("%s\"%s\"", sep, child->name);
252 sep = ", ";
254 error_printf("\n");
257 static void qbus_list_dev(BusState *bus)
259 BusChild *kid;
260 const char *sep = " ";
262 error_printf("devices at \"%s\":", bus->name);
263 QTAILQ_FOREACH(kid, &bus->children, sibling) {
264 DeviceState *dev = kid->child;
265 error_printf("%s\"%s\"", sep, object_get_typename(OBJECT(dev)));
266 if (dev->id)
267 error_printf("/\"%s\"", dev->id);
268 sep = ", ";
270 error_printf("\n");
273 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
275 BusState *child;
277 QLIST_FOREACH(child, &dev->child_bus, sibling) {
278 if (strcmp(child->name, elem) == 0) {
279 return child;
282 return NULL;
285 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
287 BusChild *kid;
290 * try to match in order:
291 * (1) instance id, if present
292 * (2) driver name
293 * (3) driver alias, if present
295 QTAILQ_FOREACH(kid, &bus->children, sibling) {
296 DeviceState *dev = kid->child;
297 if (dev->id && strcmp(dev->id, elem) == 0) {
298 return dev;
301 QTAILQ_FOREACH(kid, &bus->children, sibling) {
302 DeviceState *dev = kid->child;
303 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
304 return dev;
307 QTAILQ_FOREACH(kid, &bus->children, sibling) {
308 DeviceState *dev = kid->child;
309 DeviceClass *dc = DEVICE_GET_CLASS(dev);
311 if (qdev_class_has_alias(dc) &&
312 strcmp(qdev_class_get_alias(dc), elem) == 0) {
313 return dev;
316 return NULL;
319 static BusState *qbus_find_recursive(BusState *bus, const char *name,
320 const char *bus_typename)
322 BusClass *bus_class = BUS_GET_CLASS(bus);
323 BusChild *kid;
324 BusState *child, *ret;
325 int match = 1;
327 if (name && (strcmp(bus->name, name) != 0)) {
328 match = 0;
329 } else if (bus_typename && !object_dynamic_cast(OBJECT(bus), bus_typename)) {
330 match = 0;
331 } else if ((bus_class->max_dev != 0) && (bus_class->max_dev <= bus->max_index)) {
332 if (name != NULL) {
333 /* bus was explicitly specified: return an error. */
334 qerror_report(ERROR_CLASS_GENERIC_ERROR, "Bus '%s' is full",
335 bus->name);
336 return NULL;
337 } else {
338 /* bus was not specified: try to find another one. */
339 match = 0;
342 if (match) {
343 return bus;
346 QTAILQ_FOREACH(kid, &bus->children, sibling) {
347 DeviceState *dev = kid->child;
348 QLIST_FOREACH(child, &dev->child_bus, sibling) {
349 ret = qbus_find_recursive(child, name, bus_typename);
350 if (ret) {
351 return ret;
355 return NULL;
358 static BusState *qbus_find(const char *path)
360 DeviceState *dev;
361 BusState *bus;
362 char elem[128];
363 int pos, len;
365 /* find start element */
366 if (path[0] == '/') {
367 bus = sysbus_get_default();
368 pos = 0;
369 } else {
370 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
371 assert(!path[0]);
372 elem[0] = len = 0;
374 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
375 if (!bus) {
376 qerror_report(QERR_BUS_NOT_FOUND, elem);
377 return NULL;
379 pos = len;
382 for (;;) {
383 assert(path[pos] == '/' || !path[pos]);
384 while (path[pos] == '/') {
385 pos++;
387 if (path[pos] == '\0') {
388 return bus;
391 /* find device */
392 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
393 g_assert_not_reached();
394 elem[0] = len = 0;
396 pos += len;
397 dev = qbus_find_dev(bus, elem);
398 if (!dev) {
399 qerror_report(QERR_DEVICE_NOT_FOUND, elem);
400 if (!monitor_cur_is_qmp()) {
401 qbus_list_dev(bus);
403 return NULL;
406 assert(path[pos] == '/' || !path[pos]);
407 while (path[pos] == '/') {
408 pos++;
410 if (path[pos] == '\0') {
411 /* last specified element is a device. If it has exactly
412 * one child bus accept it nevertheless */
413 switch (dev->num_child_bus) {
414 case 0:
415 qerror_report(QERR_DEVICE_NO_BUS, elem);
416 return NULL;
417 case 1:
418 return QLIST_FIRST(&dev->child_bus);
419 default:
420 qerror_report(QERR_DEVICE_MULTIPLE_BUSSES, elem);
421 if (!monitor_cur_is_qmp()) {
422 qbus_list_bus(dev);
424 return NULL;
428 /* find bus */
429 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
430 g_assert_not_reached();
431 elem[0] = len = 0;
433 pos += len;
434 bus = qbus_find_bus(dev, elem);
435 if (!bus) {
436 qerror_report(QERR_BUS_NOT_FOUND, elem);
437 if (!monitor_cur_is_qmp()) {
438 qbus_list_bus(dev);
440 return NULL;
445 DeviceState *qdev_device_add(QemuOpts *opts)
447 ObjectClass *obj;
448 DeviceClass *k;
449 const char *driver, *path, *id;
450 DeviceState *qdev;
451 BusState *bus = NULL;
453 driver = qemu_opt_get(opts, "driver");
454 if (!driver) {
455 qerror_report(QERR_MISSING_PARAMETER, "driver");
456 return NULL;
459 /* find driver */
460 obj = object_class_by_name(driver);
461 if (!obj) {
462 const char *typename = find_typename_by_alias(driver);
464 if (typename) {
465 driver = typename;
466 obj = object_class_by_name(driver);
470 if (!obj) {
471 qerror_report(QERR_INVALID_PARAMETER_VALUE, "driver", "device type");
472 return NULL;
475 k = DEVICE_CLASS(obj);
477 /* find bus */
478 path = qemu_opt_get(opts, "bus");
479 if (path != NULL) {
480 bus = qbus_find(path);
481 if (!bus) {
482 return NULL;
484 if (!object_dynamic_cast(OBJECT(bus), k->bus_type)) {
485 qerror_report(QERR_BAD_BUS_FOR_DEVICE,
486 driver, object_get_typename(OBJECT(bus)));
487 return NULL;
489 } else if (k->bus_type != NULL) {
490 bus = qbus_find_recursive(sysbus_get_default(), NULL, k->bus_type);
491 if (!bus) {
492 qerror_report(QERR_NO_BUS_FOR_DEVICE,
493 k->bus_type, driver);
494 return NULL;
497 if (qdev_hotplug && bus && !bus->allow_hotplug) {
498 qerror_report(QERR_BUS_NO_HOTPLUG, bus->name);
499 return NULL;
502 /* create device, set properties */
503 qdev = DEVICE(object_new(driver));
505 if (bus) {
506 qdev_set_parent_bus(qdev, bus);
509 id = qemu_opts_id(opts);
510 if (id) {
511 qdev->id = id;
513 if (qemu_opt_foreach(opts, set_property, qdev, 1) != 0) {
514 qdev_free(qdev);
515 return NULL;
517 if (qdev->id) {
518 object_property_add_child(qdev_get_peripheral(), qdev->id,
519 OBJECT(qdev), NULL);
520 } else {
521 static int anon_count;
522 gchar *name = g_strdup_printf("device[%d]", anon_count++);
523 object_property_add_child(qdev_get_peripheral_anon(), name,
524 OBJECT(qdev), NULL);
525 g_free(name);
527 if (qdev_init(qdev) < 0) {
528 qerror_report(QERR_DEVICE_INIT_FAILED, driver);
529 return NULL;
531 qdev->opts = opts;
532 return qdev;
536 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
537 static void qbus_print(Monitor *mon, BusState *bus, int indent);
539 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
540 int indent)
542 if (!props)
543 return;
544 for (; props->name; props++) {
545 Error *err = NULL;
546 char *value;
547 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
548 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
549 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
550 } else {
551 value = object_property_print(OBJECT(dev), props->name, &err);
553 g_free(legacy_name);
555 if (err) {
556 error_free(err);
557 continue;
559 qdev_printf("%s = %s\n", props->name,
560 value && *value ? value : "<null>");
561 g_free(value);
565 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
567 BusClass *bc = BUS_GET_CLASS(bus);
569 if (bc->print_dev) {
570 bc->print_dev(mon, dev, indent);
574 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
576 ObjectClass *class;
577 BusState *child;
578 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
579 dev->id ? dev->id : "");
580 indent += 2;
581 if (dev->num_gpio_in) {
582 qdev_printf("gpio-in %d\n", dev->num_gpio_in);
584 if (dev->num_gpio_out) {
585 qdev_printf("gpio-out %d\n", dev->num_gpio_out);
587 class = object_get_class(OBJECT(dev));
588 do {
589 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props, indent);
590 class = object_class_get_parent(class);
591 } while (class != object_class_by_name(TYPE_DEVICE));
592 bus_print_dev(dev->parent_bus, mon, dev, indent);
593 QLIST_FOREACH(child, &dev->child_bus, sibling) {
594 qbus_print(mon, child, indent);
598 static void qbus_print(Monitor *mon, BusState *bus, int indent)
600 BusChild *kid;
602 qdev_printf("bus: %s\n", bus->name);
603 indent += 2;
604 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
605 QTAILQ_FOREACH(kid, &bus->children, sibling) {
606 DeviceState *dev = kid->child;
607 qdev_print(mon, dev, indent);
610 #undef qdev_printf
612 void do_info_qtree(Monitor *mon, const QDict *qdict)
614 if (sysbus_get_default())
615 qbus_print(mon, sysbus_get_default(), 0);
618 void do_info_qdm(Monitor *mon, const QDict *qdict)
620 object_class_foreach(qdev_print_devinfo, TYPE_DEVICE, false, NULL);
623 int do_device_add(Monitor *mon, const QDict *qdict, QObject **ret_data)
625 Error *local_err = NULL;
626 QemuOpts *opts;
627 DeviceState *dev;
629 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
630 if (error_is_set(&local_err)) {
631 qerror_report_err(local_err);
632 error_free(local_err);
633 return -1;
635 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
636 qemu_opts_del(opts);
637 return 0;
639 dev = qdev_device_add(opts);
640 if (!dev) {
641 qemu_opts_del(opts);
642 return -1;
644 object_unref(OBJECT(dev));
645 return 0;
648 void qmp_device_del(const char *id, Error **errp)
650 DeviceState *dev;
652 dev = qdev_find_recursive(sysbus_get_default(), id);
653 if (NULL == dev) {
654 error_set(errp, QERR_DEVICE_NOT_FOUND, id);
655 return;
658 qdev_unplug(dev, errp);
661 void qdev_machine_init(void)
663 qdev_get_peripheral_anon();
664 qdev_get_peripheral();
667 QemuOptsList qemu_device_opts = {
668 .name = "device",
669 .implied_opt_name = "driver",
670 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
671 .desc = {
673 * no elements => accept any
674 * sanity checking will happen later
675 * when setting device properties
677 { /* end of list */ }
681 QemuOptsList qemu_global_opts = {
682 .name = "global",
683 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
684 .desc = {
686 .name = "driver",
687 .type = QEMU_OPT_STRING,
689 .name = "property",
690 .type = QEMU_OPT_STRING,
692 .name = "value",
693 .type = QEMU_OPT_STRING,
695 { /* end of list */ }
699 int qemu_global_option(const char *str)
701 char driver[64], property[64];
702 QemuOpts *opts;
703 int rc, offset;
705 rc = sscanf(str, "%63[^.].%63[^=]%n", driver, property, &offset);
706 if (rc < 2 || str[offset] != '=') {
707 error_report("can't parse: \"%s\"", str);
708 return -1;
711 opts = qemu_opts_create_nofail(&qemu_global_opts);
712 qemu_opt_set(opts, "driver", driver);
713 qemu_opt_set(opts, "property", property);
714 qemu_opt_set(opts, "value", str+offset+1);
715 return 0;