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