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