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