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