]> git.ipfire.org Git - thirdparty/qemu.git/blob - qdev-monitor.c
block: Avoid memleak on qcow2 image info failure
[thirdparty/qemu.git] / qdev-monitor.c
1 /*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
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.
10 *
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.
15 *
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/>.
18 */
19
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 "qemu/option_int.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/sysemu.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
40 #include "qemu/cutils.h"
41
42 /*
43 * Aliases were a bad idea from the start. Let's keep them
44 * from spreading further.
45 */
46 typedef struct QDevAlias
47 {
48 const char *typename;
49 const char *alias;
50 uint32_t arch_mask;
51 } QDevAlias;
52
53 /* Please keep this table sorted by typename. */
54 static const QDevAlias qdev_alias_table[] = {
55 { "e1000", "e1000-82540em" },
56 { "ich9-ahci", "ahci" },
57 { "lsi53c895a", "lsi" },
58 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
59 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
60 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
61 { "virtio-balloon-pci", "virtio-balloon",
62 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
63 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
64 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
65 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
66 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
67 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
68 { "virtio-input-host-pci", "virtio-input-host",
69 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
71 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
72 { "virtio-keyboard-pci", "virtio-keyboard",
73 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
75 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
76 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
77 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
78 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
79 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
80 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
81 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
82 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
83 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
84 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
85 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
86 { }
87 };
88
89 static const char *qdev_class_get_alias(DeviceClass *dc)
90 {
91 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
92 int i;
93
94 for (i = 0; qdev_alias_table[i].typename; i++) {
95 if (qdev_alias_table[i].arch_mask &&
96 !(qdev_alias_table[i].arch_mask & arch_type)) {
97 continue;
98 }
99
100 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
101 return qdev_alias_table[i].alias;
102 }
103 }
104
105 return NULL;
106 }
107
108 static bool qdev_class_has_alias(DeviceClass *dc)
109 {
110 return (qdev_class_get_alias(dc) != NULL);
111 }
112
113 static void qdev_print_devinfo(DeviceClass *dc)
114 {
115 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
116 if (dc->bus_type) {
117 qemu_printf(", bus %s", dc->bus_type);
118 }
119 if (qdev_class_has_alias(dc)) {
120 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
121 }
122 if (dc->desc) {
123 qemu_printf(", desc \"%s\"", dc->desc);
124 }
125 if (!dc->user_creatable) {
126 qemu_printf(", no-user");
127 }
128 qemu_printf("\n");
129 }
130
131 static void qdev_print_devinfos(bool show_no_user)
132 {
133 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
134 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
135 [DEVICE_CATEGORY_USB] = "USB",
136 [DEVICE_CATEGORY_STORAGE] = "Storage",
137 [DEVICE_CATEGORY_NETWORK] = "Network",
138 [DEVICE_CATEGORY_INPUT] = "Input",
139 [DEVICE_CATEGORY_DISPLAY] = "Display",
140 [DEVICE_CATEGORY_SOUND] = "Sound",
141 [DEVICE_CATEGORY_MISC] = "Misc",
142 [DEVICE_CATEGORY_CPU] = "CPU",
143 [DEVICE_CATEGORY_MAX] = "Uncategorized",
144 };
145 GSList *list, *elt;
146 int i;
147 bool cat_printed;
148
149 list = object_class_get_list_sorted(TYPE_DEVICE, false);
150
151 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
152 cat_printed = false;
153 for (elt = list; elt; elt = elt->next) {
154 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
155 TYPE_DEVICE);
156 if ((i < DEVICE_CATEGORY_MAX
157 ? !test_bit(i, dc->categories)
158 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
159 || (!show_no_user
160 && !dc->user_creatable)) {
161 continue;
162 }
163 if (!cat_printed) {
164 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
165 cat_printed = true;
166 }
167 qdev_print_devinfo(dc);
168 }
169 }
170
171 g_slist_free(list);
172 }
173
174 static int set_property(void *opaque, const char *name, const char *value,
175 Error **errp)
176 {
177 Object *obj = opaque;
178 Error *err = NULL;
179
180 if (strcmp(name, "driver") == 0)
181 return 0;
182 if (strcmp(name, "bus") == 0)
183 return 0;
184
185 object_property_parse(obj, value, name, &err);
186 if (err != NULL) {
187 error_propagate(errp, err);
188 return -1;
189 }
190 return 0;
191 }
192
193 static const char *find_typename_by_alias(const char *alias)
194 {
195 int i;
196
197 for (i = 0; qdev_alias_table[i].alias; i++) {
198 if (qdev_alias_table[i].arch_mask &&
199 !(qdev_alias_table[i].arch_mask & arch_type)) {
200 continue;
201 }
202
203 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
204 return qdev_alias_table[i].typename;
205 }
206 }
207
208 return NULL;
209 }
210
211 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
212 {
213 ObjectClass *oc;
214 DeviceClass *dc;
215 const char *original_name = *driver;
216
217 oc = object_class_by_name(*driver);
218 if (!oc) {
219 const char *typename = find_typename_by_alias(*driver);
220
221 if (typename) {
222 *driver = typename;
223 oc = object_class_by_name(*driver);
224 }
225 }
226
227 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
228 if (*driver != original_name) {
229 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
230 " name", original_name, *driver);
231 } else {
232 error_setg(errp, "'%s' is not a valid device model name", *driver);
233 }
234 return NULL;
235 }
236
237 if (object_class_is_abstract(oc)) {
238 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
239 "non-abstract device type");
240 return NULL;
241 }
242
243 dc = DEVICE_CLASS(oc);
244 if (!dc->user_creatable ||
245 (qdev_hotplug && !dc->hotpluggable)) {
246 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
247 "pluggable device type");
248 return NULL;
249 }
250
251 return dc;
252 }
253
254
255 int qdev_device_help(QemuOpts *opts)
256 {
257 Error *local_err = NULL;
258 const char *driver;
259 ObjectPropertyInfoList *prop_list;
260 ObjectPropertyInfoList *prop;
261 GPtrArray *array;
262 int i;
263
264 driver = qemu_opt_get(opts, "driver");
265 if (driver && is_help_option(driver)) {
266 qdev_print_devinfos(false);
267 return 1;
268 }
269
270 if (!driver || !qemu_opt_has_help_opt(opts)) {
271 return 0;
272 }
273
274 if (!object_class_by_name(driver)) {
275 const char *typename = find_typename_by_alias(driver);
276
277 if (typename) {
278 driver = typename;
279 }
280 }
281
282 prop_list = qmp_device_list_properties(driver, &local_err);
283 if (local_err) {
284 goto error;
285 }
286
287 if (prop_list) {
288 qemu_printf("%s options:\n", driver);
289 } else {
290 qemu_printf("There are no options for %s.\n", driver);
291 }
292 array = g_ptr_array_new();
293 for (prop = prop_list; prop; prop = prop->next) {
294 g_ptr_array_add(array,
295 object_property_help(prop->value->name,
296 prop->value->type,
297 prop->value->default_value,
298 prop->value->description));
299 }
300 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
301 for (i = 0; i < array->len; i++) {
302 printf("%s\n", (char *)array->pdata[i]);
303 }
304 g_ptr_array_set_free_func(array, g_free);
305 g_ptr_array_free(array, true);
306 qapi_free_ObjectPropertyInfoList(prop_list);
307 return 1;
308
309 error:
310 error_report_err(local_err);
311 return 1;
312 }
313
314 static Object *qdev_get_peripheral(void)
315 {
316 static Object *dev;
317
318 if (dev == NULL) {
319 dev = container_get(qdev_get_machine(), "/peripheral");
320 }
321
322 return dev;
323 }
324
325 static Object *qdev_get_peripheral_anon(void)
326 {
327 static Object *dev;
328
329 if (dev == NULL) {
330 dev = container_get(qdev_get_machine(), "/peripheral-anon");
331 }
332
333 return dev;
334 }
335
336 static void qbus_error_append_bus_list_hint(DeviceState *dev,
337 Error *const *errp)
338 {
339 BusState *child;
340 const char *sep = " ";
341
342 error_append_hint(errp, "child buses at \"%s\":",
343 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
344 QLIST_FOREACH(child, &dev->child_bus, sibling) {
345 error_append_hint(errp, "%s\"%s\"", sep, child->name);
346 sep = ", ";
347 }
348 error_append_hint(errp, "\n");
349 }
350
351 static void qbus_error_append_dev_list_hint(BusState *bus,
352 Error *const *errp)
353 {
354 BusChild *kid;
355 const char *sep = " ";
356
357 error_append_hint(errp, "devices at \"%s\":", bus->name);
358 QTAILQ_FOREACH(kid, &bus->children, sibling) {
359 DeviceState *dev = kid->child;
360 error_append_hint(errp, "%s\"%s\"", sep,
361 object_get_typename(OBJECT(dev)));
362 if (dev->id) {
363 error_append_hint(errp, "/\"%s\"", dev->id);
364 }
365 sep = ", ";
366 }
367 error_append_hint(errp, "\n");
368 }
369
370 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
371 {
372 BusState *child;
373
374 QLIST_FOREACH(child, &dev->child_bus, sibling) {
375 if (strcmp(child->name, elem) == 0) {
376 return child;
377 }
378 }
379 return NULL;
380 }
381
382 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
383 {
384 BusChild *kid;
385
386 /*
387 * try to match in order:
388 * (1) instance id, if present
389 * (2) driver name
390 * (3) driver alias, if present
391 */
392 QTAILQ_FOREACH(kid, &bus->children, sibling) {
393 DeviceState *dev = kid->child;
394 if (dev->id && strcmp(dev->id, elem) == 0) {
395 return dev;
396 }
397 }
398 QTAILQ_FOREACH(kid, &bus->children, sibling) {
399 DeviceState *dev = kid->child;
400 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
401 return dev;
402 }
403 }
404 QTAILQ_FOREACH(kid, &bus->children, sibling) {
405 DeviceState *dev = kid->child;
406 DeviceClass *dc = DEVICE_GET_CLASS(dev);
407
408 if (qdev_class_has_alias(dc) &&
409 strcmp(qdev_class_get_alias(dc), elem) == 0) {
410 return dev;
411 }
412 }
413 return NULL;
414 }
415
416 static inline bool qbus_is_full(BusState *bus)
417 {
418 BusClass *bus_class = BUS_GET_CLASS(bus);
419 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
420 }
421
422 /*
423 * Search the tree rooted at @bus for a bus.
424 * If @name, search for a bus with that name. Note that bus names
425 * need not be unique. Yes, that's screwed up.
426 * Else search for a bus that is a subtype of @bus_typename.
427 * If more than one exists, prefer one that can take another device.
428 * Return the bus if found, else %NULL.
429 */
430 static BusState *qbus_find_recursive(BusState *bus, const char *name,
431 const char *bus_typename)
432 {
433 BusChild *kid;
434 BusState *pick, *child, *ret;
435 bool match;
436
437 assert(name || bus_typename);
438 if (name) {
439 match = !strcmp(bus->name, name);
440 } else {
441 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
442 }
443
444 if (match && !qbus_is_full(bus)) {
445 return bus; /* root matches and isn't full */
446 }
447
448 pick = match ? bus : NULL;
449
450 QTAILQ_FOREACH(kid, &bus->children, sibling) {
451 DeviceState *dev = kid->child;
452 QLIST_FOREACH(child, &dev->child_bus, sibling) {
453 ret = qbus_find_recursive(child, name, bus_typename);
454 if (ret && !qbus_is_full(ret)) {
455 return ret; /* a descendant matches and isn't full */
456 }
457 if (ret && !pick) {
458 pick = ret;
459 }
460 }
461 }
462
463 /* root or a descendant matches, but is full */
464 return pick;
465 }
466
467 static BusState *qbus_find(const char *path, Error **errp)
468 {
469 DeviceState *dev;
470 BusState *bus;
471 char elem[128];
472 int pos, len;
473
474 /* find start element */
475 if (path[0] == '/') {
476 bus = sysbus_get_default();
477 pos = 0;
478 } else {
479 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
480 assert(!path[0]);
481 elem[0] = len = 0;
482 }
483 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
484 if (!bus) {
485 error_setg(errp, "Bus '%s' not found", elem);
486 return NULL;
487 }
488 pos = len;
489 }
490
491 for (;;) {
492 assert(path[pos] == '/' || !path[pos]);
493 while (path[pos] == '/') {
494 pos++;
495 }
496 if (path[pos] == '\0') {
497 break;
498 }
499
500 /* find device */
501 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
502 g_assert_not_reached();
503 elem[0] = len = 0;
504 }
505 pos += len;
506 dev = qbus_find_dev(bus, elem);
507 if (!dev) {
508 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
509 "Device '%s' not found", elem);
510 qbus_error_append_dev_list_hint(bus, errp);
511 return NULL;
512 }
513
514 assert(path[pos] == '/' || !path[pos]);
515 while (path[pos] == '/') {
516 pos++;
517 }
518 if (path[pos] == '\0') {
519 /* last specified element is a device. If it has exactly
520 * one child bus accept it nevertheless */
521 if (dev->num_child_bus == 1) {
522 bus = QLIST_FIRST(&dev->child_bus);
523 break;
524 }
525 if (dev->num_child_bus) {
526 error_setg(errp, "Device '%s' has multiple child buses",
527 elem);
528 qbus_error_append_bus_list_hint(dev, errp);
529 } else {
530 error_setg(errp, "Device '%s' has no child bus", elem);
531 }
532 return NULL;
533 }
534
535 /* find bus */
536 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
537 g_assert_not_reached();
538 elem[0] = len = 0;
539 }
540 pos += len;
541 bus = qbus_find_bus(dev, elem);
542 if (!bus) {
543 error_setg(errp, "Bus '%s' not found", elem);
544 qbus_error_append_bus_list_hint(dev, errp);
545 return NULL;
546 }
547 }
548
549 if (qbus_is_full(bus)) {
550 error_setg(errp, "Bus '%s' is full", path);
551 return NULL;
552 }
553 return bus;
554 }
555
556 void qdev_set_id(DeviceState *dev, const char *id)
557 {
558 if (id) {
559 dev->id = id;
560 }
561
562 if (dev->id) {
563 object_property_add_child(qdev_get_peripheral(), dev->id,
564 OBJECT(dev), NULL);
565 } else {
566 static int anon_count;
567 gchar *name = g_strdup_printf("device[%d]", anon_count++);
568 object_property_add_child(qdev_get_peripheral_anon(), name,
569 OBJECT(dev), NULL);
570 g_free(name);
571 }
572 }
573
574 static int is_failover_device(void *opaque, const char *name, const char *value,
575 Error **errp)
576 {
577 if (strcmp(name, "failover_pair_id") == 0) {
578 QemuOpts *opts = (QemuOpts *)opaque;
579
580 if (qdev_should_hide_device(opts)) {
581 return 1;
582 }
583 }
584
585 return 0;
586 }
587
588 static bool should_hide_device(QemuOpts *opts)
589 {
590 if (qemu_opt_foreach(opts, is_failover_device, opts, NULL) == 0) {
591 return false;
592 }
593 return true;
594 }
595
596 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
597 {
598 DeviceClass *dc;
599 const char *driver, *path;
600 DeviceState *dev = NULL;
601 BusState *bus = NULL;
602 Error *err = NULL;
603 bool hide;
604
605 driver = qemu_opt_get(opts, "driver");
606 if (!driver) {
607 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
608 return NULL;
609 }
610
611 /* find driver */
612 dc = qdev_get_device_class(&driver, errp);
613 if (!dc) {
614 return NULL;
615 }
616
617 /* find bus */
618 path = qemu_opt_get(opts, "bus");
619 if (path != NULL) {
620 bus = qbus_find(path, errp);
621 if (!bus) {
622 return NULL;
623 }
624 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
625 error_setg(errp, "Device '%s' can't go on %s bus",
626 driver, object_get_typename(OBJECT(bus)));
627 return NULL;
628 }
629 } else if (dc->bus_type != NULL) {
630 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
631 if (!bus || qbus_is_full(bus)) {
632 error_setg(errp, "No '%s' bus found for device '%s'",
633 dc->bus_type, driver);
634 return NULL;
635 }
636 }
637 hide = should_hide_device(opts);
638
639 if ((hide || qdev_hotplug) && bus && !qbus_is_hotpluggable(bus)) {
640 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
641 return NULL;
642 }
643
644 if (hide) {
645 return NULL;
646 }
647
648 if (!migration_is_idle()) {
649 error_setg(errp, "device_add not allowed while migrating");
650 return NULL;
651 }
652
653 /* create device */
654 dev = DEVICE(object_new(driver));
655
656 /* Check whether the hotplug is allowed by the machine */
657 if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
658 /* Error must be set in the machine hook */
659 assert(err);
660 goto err_del_dev;
661 }
662
663 if (bus) {
664 qdev_set_parent_bus(dev, bus);
665 } else if (qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
666 /* No bus, no machine hotplug handler --> device is not hotpluggable */
667 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
668 driver);
669 goto err_del_dev;
670 }
671
672 qdev_set_id(dev, qemu_opts_id(opts));
673
674 /* set properties */
675 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
676 goto err_del_dev;
677 }
678
679 dev->opts = opts;
680 object_property_set_bool(OBJECT(dev), true, "realized", &err);
681 if (err != NULL) {
682 dev->opts = NULL;
683 goto err_del_dev;
684 }
685 return dev;
686
687 err_del_dev:
688 error_propagate(errp, err);
689 if (dev) {
690 object_unparent(OBJECT(dev));
691 object_unref(OBJECT(dev));
692 }
693 return NULL;
694 }
695
696
697 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
698 static void qbus_print(Monitor *mon, BusState *bus, int indent);
699
700 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
701 int indent)
702 {
703 if (!props)
704 return;
705 for (; props->name; props++) {
706 Error *err = NULL;
707 char *value;
708 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
709 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
710 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
711 } else {
712 value = object_property_print(OBJECT(dev), props->name, true, &err);
713 }
714 g_free(legacy_name);
715
716 if (err) {
717 error_free(err);
718 continue;
719 }
720 qdev_printf("%s = %s\n", props->name,
721 value && *value ? value : "<null>");
722 g_free(value);
723 }
724 }
725
726 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
727 {
728 BusClass *bc = BUS_GET_CLASS(bus);
729
730 if (bc->print_dev) {
731 bc->print_dev(mon, dev, indent);
732 }
733 }
734
735 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
736 {
737 ObjectClass *class;
738 BusState *child;
739 NamedGPIOList *ngl;
740
741 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
742 dev->id ? dev->id : "");
743 indent += 2;
744 QLIST_FOREACH(ngl, &dev->gpios, node) {
745 if (ngl->num_in) {
746 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
747 ngl->num_in);
748 }
749 if (ngl->num_out) {
750 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
751 ngl->num_out);
752 }
753 }
754 class = object_get_class(OBJECT(dev));
755 do {
756 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
757 class = object_class_get_parent(class);
758 } while (class != object_class_by_name(TYPE_DEVICE));
759 bus_print_dev(dev->parent_bus, mon, dev, indent);
760 QLIST_FOREACH(child, &dev->child_bus, sibling) {
761 qbus_print(mon, child, indent);
762 }
763 }
764
765 static void qbus_print(Monitor *mon, BusState *bus, int indent)
766 {
767 BusChild *kid;
768
769 qdev_printf("bus: %s\n", bus->name);
770 indent += 2;
771 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
772 QTAILQ_FOREACH(kid, &bus->children, sibling) {
773 DeviceState *dev = kid->child;
774 qdev_print(mon, dev, indent);
775 }
776 }
777 #undef qdev_printf
778
779 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
780 {
781 if (sysbus_get_default())
782 qbus_print(mon, sysbus_get_default(), 0);
783 }
784
785 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
786 {
787 qdev_print_devinfos(true);
788 }
789
790 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
791 {
792 Error *local_err = NULL;
793 QemuOpts *opts;
794 DeviceState *dev;
795
796 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, &local_err);
797 if (local_err) {
798 error_propagate(errp, local_err);
799 return;
800 }
801 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
802 qemu_opts_del(opts);
803 return;
804 }
805 dev = qdev_device_add(opts, &local_err);
806 if (!dev) {
807 error_propagate(errp, local_err);
808 qemu_opts_del(opts);
809 return;
810 }
811 object_unref(OBJECT(dev));
812 }
813
814 static DeviceState *find_device_state(const char *id, Error **errp)
815 {
816 Object *obj;
817
818 if (id[0] == '/') {
819 obj = object_resolve_path(id, NULL);
820 } else {
821 char *root_path = object_get_canonical_path(qdev_get_peripheral());
822 char *path = g_strdup_printf("%s/%s", root_path, id);
823
824 g_free(root_path);
825 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
826 g_free(path);
827 }
828
829 if (!obj) {
830 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
831 "Device '%s' not found", id);
832 return NULL;
833 }
834
835 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
836 error_setg(errp, "%s is not a hotpluggable device", id);
837 return NULL;
838 }
839
840 return DEVICE(obj);
841 }
842
843 void qdev_unplug(DeviceState *dev, Error **errp)
844 {
845 DeviceClass *dc = DEVICE_GET_CLASS(dev);
846 HotplugHandler *hotplug_ctrl;
847 HotplugHandlerClass *hdc;
848 Error *local_err = NULL;
849
850 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
851 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
852 return;
853 }
854
855 if (!dc->hotpluggable) {
856 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
857 object_get_typename(OBJECT(dev)));
858 return;
859 }
860
861 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
862 error_setg(errp, "device_del not allowed while migrating");
863 return;
864 }
865
866 qdev_hot_removed = true;
867
868 hotplug_ctrl = qdev_get_hotplug_handler(dev);
869 /* hotpluggable device MUST have HotplugHandler, if it doesn't
870 * then something is very wrong with it */
871 g_assert(hotplug_ctrl);
872
873 /* If device supports async unplug just request it to be done,
874 * otherwise just remove it synchronously */
875 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
876 if (hdc->unplug_request) {
877 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
878 } else {
879 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
880 if (!local_err) {
881 object_unparent(OBJECT(dev));
882 }
883 }
884 error_propagate(errp, local_err);
885 }
886
887 void qmp_device_del(const char *id, Error **errp)
888 {
889 DeviceState *dev = find_device_state(id, errp);
890 if (dev != NULL) {
891 if (dev->pending_deleted_event) {
892 error_setg(errp, "Device %s is already in the "
893 "process of unplug", id);
894 return;
895 }
896
897 qdev_unplug(dev, errp);
898 }
899 }
900
901 void hmp_device_add(Monitor *mon, const QDict *qdict)
902 {
903 Error *err = NULL;
904
905 qmp_device_add((QDict *)qdict, NULL, &err);
906 hmp_handle_error(mon, err);
907 }
908
909 void hmp_device_del(Monitor *mon, const QDict *qdict)
910 {
911 const char *id = qdict_get_str(qdict, "id");
912 Error *err = NULL;
913
914 qmp_device_del(id, &err);
915 hmp_handle_error(mon, err);
916 }
917
918 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
919 {
920 DeviceState *dev;
921 BlockBackend *blk;
922
923 dev = find_device_state(id, errp);
924 if (dev == NULL) {
925 return NULL;
926 }
927
928 blk = blk_by_dev(dev);
929 if (!blk) {
930 error_setg(errp, "Device does not have a block device backend");
931 }
932 return blk;
933 }
934
935 void qdev_machine_init(void)
936 {
937 qdev_get_peripheral_anon();
938 qdev_get_peripheral();
939 }
940
941 QemuOptsList qemu_device_opts = {
942 .name = "device",
943 .implied_opt_name = "driver",
944 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
945 .desc = {
946 /*
947 * no elements => accept any
948 * sanity checking will happen later
949 * when setting device properties
950 */
951 { /* end of list */ }
952 },
953 };
954
955 QemuOptsList qemu_global_opts = {
956 .name = "global",
957 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
958 .desc = {
959 {
960 .name = "driver",
961 .type = QEMU_OPT_STRING,
962 },{
963 .name = "property",
964 .type = QEMU_OPT_STRING,
965 },{
966 .name = "value",
967 .type = QEMU_OPT_STRING,
968 },
969 { /* end of list */ }
970 },
971 };
972
973 int qemu_global_option(const char *str)
974 {
975 char driver[64], property[64];
976 QemuOpts *opts;
977 int rc, offset;
978
979 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
980 if (rc == 2 && str[offset] == '=') {
981 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
982 qemu_opt_set(opts, "driver", driver, &error_abort);
983 qemu_opt_set(opts, "property", property, &error_abort);
984 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
985 return 0;
986 }
987
988 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
989 if (!opts) {
990 return -1;
991 }
992
993 return 0;
994 }