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