]> git.proxmox.com Git - mirror_qemu.git/blob - qdev-monitor.c
qom: Use returned bool to check for failure, Coccinelle part
[mirror_qemu.git] / qdev-monitor.c
1 /*
2 * Dynamic device configuration and creation.
3 *
4 * Copyright (c) 2009 CodeSourcery
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "qemu/osdep.h"
21 #include "hw/sysbus.h"
22 #include "monitor/hmp.h"
23 #include "monitor/monitor.h"
24 #include "monitor/qdev.h"
25 #include "sysemu/arch_init.h"
26 #include "qapi/error.h"
27 #include "qapi/qapi-commands-qdev.h"
28 #include "qapi/qmp/qdict.h"
29 #include "qapi/qmp/qerror.h"
30 #include "qemu/config-file.h"
31 #include "qemu/error-report.h"
32 #include "qemu/help_option.h"
33 #include "qemu/option.h"
34 #include "qemu/qemu-print.h"
35 #include "qemu/option_int.h"
36 #include "sysemu/block-backend.h"
37 #include "sysemu/sysemu.h"
38 #include "migration/misc.h"
39 #include "migration/migration.h"
40 #include "qemu/cutils.h"
41 #include "hw/clock.h"
42
43 /*
44 * Aliases were a bad idea from the start. Let's keep them
45 * from spreading further.
46 */
47 typedef struct QDevAlias
48 {
49 const char *typename;
50 const char *alias;
51 uint32_t arch_mask;
52 } QDevAlias;
53
54 /* Please keep this table sorted by typename. */
55 static const QDevAlias qdev_alias_table[] = {
56 { "AC97", "ac97" }, /* -soundhw name */
57 { "e1000", "e1000-82540em" },
58 { "ES1370", "es1370" }, /* -soundhw name */
59 { "ich9-ahci", "ahci" },
60 { "lsi53c895a", "lsi" },
61 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_S390X },
62 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
63 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_S390X },
64 { "virtio-balloon-pci", "virtio-balloon",
65 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
66 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_S390X },
67 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
68 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_S390X },
69 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
70 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_S390X },
71 { "virtio-input-host-pci", "virtio-input-host",
72 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
73 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
74 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_S390X },
75 { "virtio-keyboard-pci", "virtio-keyboard",
76 QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
77 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_S390X },
78 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
79 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_S390X },
80 { "virtio-net-pci", "virtio-net", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
81 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_S390X },
82 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
83 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_S390X },
84 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
85 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_S390X },
86 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
87 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_S390X },
88 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_ALL & ~QEMU_ARCH_S390X },
89 { }
90 };
91
92 static const char *qdev_class_get_alias(DeviceClass *dc)
93 {
94 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
95 int i;
96
97 for (i = 0; qdev_alias_table[i].typename; i++) {
98 if (qdev_alias_table[i].arch_mask &&
99 !(qdev_alias_table[i].arch_mask & arch_type)) {
100 continue;
101 }
102
103 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
104 return qdev_alias_table[i].alias;
105 }
106 }
107
108 return NULL;
109 }
110
111 static bool qdev_class_has_alias(DeviceClass *dc)
112 {
113 return (qdev_class_get_alias(dc) != NULL);
114 }
115
116 static void qdev_print_devinfo(DeviceClass *dc)
117 {
118 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
119 if (dc->bus_type) {
120 qemu_printf(", bus %s", dc->bus_type);
121 }
122 if (qdev_class_has_alias(dc)) {
123 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
124 }
125 if (dc->desc) {
126 qemu_printf(", desc \"%s\"", dc->desc);
127 }
128 if (!dc->user_creatable) {
129 qemu_printf(", no-user");
130 }
131 qemu_printf("\n");
132 }
133
134 static void qdev_print_devinfos(bool show_no_user)
135 {
136 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
137 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
138 [DEVICE_CATEGORY_USB] = "USB",
139 [DEVICE_CATEGORY_STORAGE] = "Storage",
140 [DEVICE_CATEGORY_NETWORK] = "Network",
141 [DEVICE_CATEGORY_INPUT] = "Input",
142 [DEVICE_CATEGORY_DISPLAY] = "Display",
143 [DEVICE_CATEGORY_SOUND] = "Sound",
144 [DEVICE_CATEGORY_MISC] = "Misc",
145 [DEVICE_CATEGORY_CPU] = "CPU",
146 [DEVICE_CATEGORY_MAX] = "Uncategorized",
147 };
148 GSList *list, *elt;
149 int i;
150 bool cat_printed;
151
152 module_load_qom_all();
153 list = object_class_get_list_sorted(TYPE_DEVICE, false);
154
155 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
156 cat_printed = false;
157 for (elt = list; elt; elt = elt->next) {
158 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
159 TYPE_DEVICE);
160 if ((i < DEVICE_CATEGORY_MAX
161 ? !test_bit(i, dc->categories)
162 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
163 || (!show_no_user
164 && !dc->user_creatable)) {
165 continue;
166 }
167 if (!cat_printed) {
168 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
169 cat_printed = true;
170 }
171 qdev_print_devinfo(dc);
172 }
173 }
174
175 g_slist_free(list);
176 }
177
178 static int set_property(void *opaque, const char *name, const char *value,
179 Error **errp)
180 {
181 Object *obj = opaque;
182 Error *err = NULL;
183
184 if (strcmp(name, "driver") == 0)
185 return 0;
186 if (strcmp(name, "bus") == 0)
187 return 0;
188
189 if (!object_property_parse(obj, name, value, &err)) {
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 = module_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 = module_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 GPtrArray *array;
265 int i;
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 if (prop_list) {
291 qemu_printf("%s options:\n", driver);
292 } else {
293 qemu_printf("There are no options for %s.\n", driver);
294 }
295 array = g_ptr_array_new();
296 for (prop = prop_list; prop; prop = prop->next) {
297 g_ptr_array_add(array,
298 object_property_help(prop->value->name,
299 prop->value->type,
300 prop->value->default_value,
301 prop->value->description));
302 }
303 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
304 for (i = 0; i < array->len; i++) {
305 printf("%s\n", (char *)array->pdata[i]);
306 }
307 g_ptr_array_set_free_func(array, g_free);
308 g_ptr_array_free(array, true);
309 qapi_free_ObjectPropertyInfoList(prop_list);
310 return 1;
311
312 error:
313 error_report_err(local_err);
314 return 1;
315 }
316
317 static Object *qdev_get_peripheral(void)
318 {
319 static Object *dev;
320
321 if (dev == NULL) {
322 dev = container_get(qdev_get_machine(), "/peripheral");
323 }
324
325 return dev;
326 }
327
328 static Object *qdev_get_peripheral_anon(void)
329 {
330 static Object *dev;
331
332 if (dev == NULL) {
333 dev = container_get(qdev_get_machine(), "/peripheral-anon");
334 }
335
336 return dev;
337 }
338
339 static void qbus_error_append_bus_list_hint(DeviceState *dev,
340 Error *const *errp)
341 {
342 BusState *child;
343 const char *sep = " ";
344
345 error_append_hint(errp, "child buses at \"%s\":",
346 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
347 QLIST_FOREACH(child, &dev->child_bus, sibling) {
348 error_append_hint(errp, "%s\"%s\"", sep, child->name);
349 sep = ", ";
350 }
351 error_append_hint(errp, "\n");
352 }
353
354 static void qbus_error_append_dev_list_hint(BusState *bus,
355 Error *const *errp)
356 {
357 BusChild *kid;
358 const char *sep = " ";
359
360 error_append_hint(errp, "devices at \"%s\":", bus->name);
361 QTAILQ_FOREACH(kid, &bus->children, sibling) {
362 DeviceState *dev = kid->child;
363 error_append_hint(errp, "%s\"%s\"", sep,
364 object_get_typename(OBJECT(dev)));
365 if (dev->id) {
366 error_append_hint(errp, "/\"%s\"", dev->id);
367 }
368 sep = ", ";
369 }
370 error_append_hint(errp, "\n");
371 }
372
373 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
374 {
375 BusState *child;
376
377 QLIST_FOREACH(child, &dev->child_bus, sibling) {
378 if (strcmp(child->name, elem) == 0) {
379 return child;
380 }
381 }
382 return NULL;
383 }
384
385 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
386 {
387 BusChild *kid;
388
389 /*
390 * try to match in order:
391 * (1) instance id, if present
392 * (2) driver name
393 * (3) driver alias, if present
394 */
395 QTAILQ_FOREACH(kid, &bus->children, sibling) {
396 DeviceState *dev = kid->child;
397 if (dev->id && strcmp(dev->id, elem) == 0) {
398 return dev;
399 }
400 }
401 QTAILQ_FOREACH(kid, &bus->children, sibling) {
402 DeviceState *dev = kid->child;
403 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
404 return dev;
405 }
406 }
407 QTAILQ_FOREACH(kid, &bus->children, sibling) {
408 DeviceState *dev = kid->child;
409 DeviceClass *dc = DEVICE_GET_CLASS(dev);
410
411 if (qdev_class_has_alias(dc) &&
412 strcmp(qdev_class_get_alias(dc), elem) == 0) {
413 return dev;
414 }
415 }
416 return NULL;
417 }
418
419 static inline bool qbus_is_full(BusState *bus)
420 {
421 BusClass *bus_class = BUS_GET_CLASS(bus);
422 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
423 }
424
425 /*
426 * Search the tree rooted at @bus for a bus.
427 * If @name, search for a bus with that name. Note that bus names
428 * need not be unique. Yes, that's screwed up.
429 * Else search for a bus that is a subtype of @bus_typename.
430 * If more than one exists, prefer one that can take another device.
431 * Return the bus if found, else %NULL.
432 */
433 static BusState *qbus_find_recursive(BusState *bus, const char *name,
434 const char *bus_typename)
435 {
436 BusChild *kid;
437 BusState *pick, *child, *ret;
438 bool match;
439
440 assert(name || bus_typename);
441 if (name) {
442 match = !strcmp(bus->name, name);
443 } else {
444 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
445 }
446
447 if (match && !qbus_is_full(bus)) {
448 return bus; /* root matches and isn't full */
449 }
450
451 pick = match ? bus : NULL;
452
453 QTAILQ_FOREACH(kid, &bus->children, sibling) {
454 DeviceState *dev = kid->child;
455 QLIST_FOREACH(child, &dev->child_bus, sibling) {
456 ret = qbus_find_recursive(child, name, bus_typename);
457 if (ret && !qbus_is_full(ret)) {
458 return ret; /* a descendant matches and isn't full */
459 }
460 if (ret && !pick) {
461 pick = ret;
462 }
463 }
464 }
465
466 /* root or a descendant matches, but is full */
467 return pick;
468 }
469
470 static BusState *qbus_find(const char *path, Error **errp)
471 {
472 DeviceState *dev;
473 BusState *bus;
474 char elem[128];
475 int pos, len;
476
477 /* find start element */
478 if (path[0] == '/') {
479 bus = sysbus_get_default();
480 pos = 0;
481 } else {
482 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
483 assert(!path[0]);
484 elem[0] = len = 0;
485 }
486 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
487 if (!bus) {
488 error_setg(errp, "Bus '%s' not found", elem);
489 return NULL;
490 }
491 pos = len;
492 }
493
494 for (;;) {
495 assert(path[pos] == '/' || !path[pos]);
496 while (path[pos] == '/') {
497 pos++;
498 }
499 if (path[pos] == '\0') {
500 break;
501 }
502
503 /* find device */
504 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
505 g_assert_not_reached();
506 elem[0] = len = 0;
507 }
508 pos += len;
509 dev = qbus_find_dev(bus, elem);
510 if (!dev) {
511 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
512 "Device '%s' not found", elem);
513 qbus_error_append_dev_list_hint(bus, errp);
514 return NULL;
515 }
516
517 assert(path[pos] == '/' || !path[pos]);
518 while (path[pos] == '/') {
519 pos++;
520 }
521 if (path[pos] == '\0') {
522 /* last specified element is a device. If it has exactly
523 * one child bus accept it nevertheless */
524 if (dev->num_child_bus == 1) {
525 bus = QLIST_FIRST(&dev->child_bus);
526 break;
527 }
528 if (dev->num_child_bus) {
529 error_setg(errp, "Device '%s' has multiple child buses",
530 elem);
531 qbus_error_append_bus_list_hint(dev, errp);
532 } else {
533 error_setg(errp, "Device '%s' has no child bus", elem);
534 }
535 return NULL;
536 }
537
538 /* find bus */
539 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
540 g_assert_not_reached();
541 elem[0] = len = 0;
542 }
543 pos += len;
544 bus = qbus_find_bus(dev, elem);
545 if (!bus) {
546 error_setg(errp, "Bus '%s' not found", elem);
547 qbus_error_append_bus_list_hint(dev, errp);
548 return NULL;
549 }
550 }
551
552 if (qbus_is_full(bus)) {
553 error_setg(errp, "Bus '%s' is full", path);
554 return NULL;
555 }
556 return bus;
557 }
558
559 void qdev_set_id(DeviceState *dev, const char *id)
560 {
561 if (id) {
562 dev->id = id;
563 }
564
565 if (dev->id) {
566 object_property_add_child(qdev_get_peripheral(), dev->id,
567 OBJECT(dev));
568 } else {
569 static int anon_count;
570 gchar *name = g_strdup_printf("device[%d]", anon_count++);
571 object_property_add_child(qdev_get_peripheral_anon(), name,
572 OBJECT(dev));
573 g_free(name);
574 }
575 }
576
577 static int is_failover_device(void *opaque, const char *name, const char *value,
578 Error **errp)
579 {
580 if (strcmp(name, "failover_pair_id") == 0) {
581 QemuOpts *opts = (QemuOpts *)opaque;
582
583 if (qdev_should_hide_device(opts)) {
584 return 1;
585 }
586 }
587
588 return 0;
589 }
590
591 static bool should_hide_device(QemuOpts *opts)
592 {
593 if (qemu_opt_foreach(opts, is_failover_device, opts, NULL) == 0) {
594 return false;
595 }
596 return true;
597 }
598
599 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
600 {
601 DeviceClass *dc;
602 const char *driver, *path;
603 DeviceState *dev = NULL;
604 BusState *bus = NULL;
605 Error *err = NULL;
606 bool hide;
607
608 driver = qemu_opt_get(opts, "driver");
609 if (!driver) {
610 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
611 return NULL;
612 }
613
614 /* find driver */
615 dc = qdev_get_device_class(&driver, errp);
616 if (!dc) {
617 return NULL;
618 }
619
620 /* find bus */
621 path = qemu_opt_get(opts, "bus");
622 if (path != NULL) {
623 bus = qbus_find(path, errp);
624 if (!bus) {
625 return NULL;
626 }
627 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
628 error_setg(errp, "Device '%s' can't go on %s bus",
629 driver, object_get_typename(OBJECT(bus)));
630 return NULL;
631 }
632 } else if (dc->bus_type != NULL) {
633 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
634 if (!bus || qbus_is_full(bus)) {
635 error_setg(errp, "No '%s' bus found for device '%s'",
636 dc->bus_type, driver);
637 return NULL;
638 }
639 }
640 hide = should_hide_device(opts);
641
642 if ((hide || qdev_hotplug) && bus && !qbus_is_hotpluggable(bus)) {
643 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
644 return NULL;
645 }
646
647 if (hide) {
648 return NULL;
649 }
650
651 if (!migration_is_idle()) {
652 error_setg(errp, "device_add not allowed while migrating");
653 return NULL;
654 }
655
656 /* create device */
657 dev = qdev_new(driver);
658
659 /* Check whether the hotplug is allowed by the machine */
660 if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
661 /* Error must be set in the machine hook */
662 assert(err);
663 goto err_del_dev;
664 }
665
666 if (!bus && qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
667 /* No bus, no machine hotplug handler --> device is not hotpluggable */
668 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
669 driver);
670 goto err_del_dev;
671 }
672
673 qdev_set_id(dev, qemu_opts_id(opts));
674
675 /* set properties */
676 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
677 goto err_del_dev;
678 }
679
680 dev->opts = opts;
681 if (!qdev_realize(DEVICE(dev), bus, &err)) {
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, errp);
805 if (!opts) {
806 return;
807 }
808 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
809 qemu_opts_del(opts);
810 return;
811 }
812 dev = qdev_device_add(opts, &local_err);
813 if (!dev) {
814 error_propagate(errp, local_err);
815 qemu_opts_del(opts);
816 return;
817 }
818 object_unref(OBJECT(dev));
819 }
820
821 static DeviceState *find_device_state(const char *id, Error **errp)
822 {
823 Object *obj;
824
825 if (id[0] == '/') {
826 obj = object_resolve_path(id, NULL);
827 } else {
828 char *root_path = object_get_canonical_path(qdev_get_peripheral());
829 char *path = g_strdup_printf("%s/%s", root_path, id);
830
831 g_free(root_path);
832 obj = object_resolve_path_type(path, TYPE_DEVICE, NULL);
833 g_free(path);
834 }
835
836 if (!obj) {
837 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
838 "Device '%s' not found", id);
839 return NULL;
840 }
841
842 if (!object_dynamic_cast(obj, TYPE_DEVICE)) {
843 error_setg(errp, "%s is not a hotpluggable device", id);
844 return NULL;
845 }
846
847 return DEVICE(obj);
848 }
849
850 void qdev_unplug(DeviceState *dev, Error **errp)
851 {
852 DeviceClass *dc = DEVICE_GET_CLASS(dev);
853 HotplugHandler *hotplug_ctrl;
854 HotplugHandlerClass *hdc;
855 Error *local_err = NULL;
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() && !dev->allow_unplug_during_migration) {
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, &local_err);
885 } else {
886 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
887 if (!local_err) {
888 object_unparent(OBJECT(dev));
889 }
890 }
891 error_propagate(errp, local_err);
892 }
893
894 void qmp_device_del(const char *id, Error **errp)
895 {
896 DeviceState *dev = find_device_state(id, errp);
897 if (dev != NULL) {
898 if (dev->pending_deleted_event) {
899 error_setg(errp, "Device %s is already in the "
900 "process of unplug", id);
901 return;
902 }
903
904 qdev_unplug(dev, errp);
905 }
906 }
907
908 void hmp_device_add(Monitor *mon, const QDict *qdict)
909 {
910 Error *err = NULL;
911
912 qmp_device_add((QDict *)qdict, NULL, &err);
913 hmp_handle_error(mon, err);
914 }
915
916 void hmp_device_del(Monitor *mon, const QDict *qdict)
917 {
918 const char *id = qdict_get_str(qdict, "id");
919 Error *err = NULL;
920
921 qmp_device_del(id, &err);
922 hmp_handle_error(mon, err);
923 }
924
925 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
926 {
927 DeviceState *dev;
928 BlockBackend *blk;
929
930 dev = find_device_state(id, errp);
931 if (dev == NULL) {
932 return NULL;
933 }
934
935 blk = blk_by_dev(dev);
936 if (!blk) {
937 error_setg(errp, "Device does not have a block device backend");
938 }
939 return blk;
940 }
941
942 void qdev_machine_init(void)
943 {
944 qdev_get_peripheral_anon();
945 qdev_get_peripheral();
946 }
947
948 QemuOptsList qemu_device_opts = {
949 .name = "device",
950 .implied_opt_name = "driver",
951 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
952 .desc = {
953 /*
954 * no elements => accept any
955 * sanity checking will happen later
956 * when setting device properties
957 */
958 { /* end of list */ }
959 },
960 };
961
962 QemuOptsList qemu_global_opts = {
963 .name = "global",
964 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
965 .desc = {
966 {
967 .name = "driver",
968 .type = QEMU_OPT_STRING,
969 },{
970 .name = "property",
971 .type = QEMU_OPT_STRING,
972 },{
973 .name = "value",
974 .type = QEMU_OPT_STRING,
975 },
976 { /* end of list */ }
977 },
978 };
979
980 int qemu_global_option(const char *str)
981 {
982 char driver[64], property[64];
983 QemuOpts *opts;
984 int rc, offset;
985
986 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
987 if (rc == 2 && str[offset] == '=') {
988 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
989 qemu_opt_set(opts, "driver", driver, &error_abort);
990 qemu_opt_set(opts, "property", property, &error_abort);
991 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
992 return 0;
993 }
994
995 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
996 if (!opts) {
997 return -1;
998 }
999
1000 return 0;
1001 }