qemu-option: Simplify around find_default_by_name()
[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 object_property_parse(obj, value, name, &err);
190 if (err != NULL) {
191 error_propagate(errp, err);
192 return -1;
193 }
194 return 0;
195 }
196
197 static const char *find_typename_by_alias(const char *alias)
198 {
199 int i;
200
201 for (i = 0; qdev_alias_table[i].alias; i++) {
202 if (qdev_alias_table[i].arch_mask &&
203 !(qdev_alias_table[i].arch_mask & arch_type)) {
204 continue;
205 }
206
207 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
208 return qdev_alias_table[i].typename;
209 }
210 }
211
212 return NULL;
213 }
214
215 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
216 {
217 ObjectClass *oc;
218 DeviceClass *dc;
219 const char *original_name = *driver;
220
221 oc = module_object_class_by_name(*driver);
222 if (!oc) {
223 const char *typename = find_typename_by_alias(*driver);
224
225 if (typename) {
226 *driver = typename;
227 oc = module_object_class_by_name(*driver);
228 }
229 }
230
231 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
232 if (*driver != original_name) {
233 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
234 " name", original_name, *driver);
235 } else {
236 error_setg(errp, "'%s' is not a valid device model name", *driver);
237 }
238 return NULL;
239 }
240
241 if (object_class_is_abstract(oc)) {
242 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
243 "non-abstract device type");
244 return NULL;
245 }
246
247 dc = DEVICE_CLASS(oc);
248 if (!dc->user_creatable ||
249 (qdev_hotplug && !dc->hotpluggable)) {
250 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
251 "pluggable device type");
252 return NULL;
253 }
254
255 return dc;
256 }
257
258
259 int qdev_device_help(QemuOpts *opts)
260 {
261 Error *local_err = NULL;
262 const char *driver;
263 ObjectPropertyInfoList *prop_list;
264 ObjectPropertyInfoList *prop;
265 GPtrArray *array;
266 int i;
267
268 driver = qemu_opt_get(opts, "driver");
269 if (driver && is_help_option(driver)) {
270 qdev_print_devinfos(false);
271 return 1;
272 }
273
274 if (!driver || !qemu_opt_has_help_opt(opts)) {
275 return 0;
276 }
277
278 if (!object_class_by_name(driver)) {
279 const char *typename = find_typename_by_alias(driver);
280
281 if (typename) {
282 driver = typename;
283 }
284 }
285
286 prop_list = qmp_device_list_properties(driver, &local_err);
287 if (local_err) {
288 goto error;
289 }
290
291 if (prop_list) {
292 qemu_printf("%s options:\n", driver);
293 } else {
294 qemu_printf("There are no options for %s.\n", driver);
295 }
296 array = g_ptr_array_new();
297 for (prop = prop_list; prop; prop = prop->next) {
298 g_ptr_array_add(array,
299 object_property_help(prop->value->name,
300 prop->value->type,
301 prop->value->default_value,
302 prop->value->description));
303 }
304 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
305 for (i = 0; i < array->len; i++) {
306 printf("%s\n", (char *)array->pdata[i]);
307 }
308 g_ptr_array_set_free_func(array, g_free);
309 g_ptr_array_free(array, true);
310 qapi_free_ObjectPropertyInfoList(prop_list);
311 return 1;
312
313 error:
314 error_report_err(local_err);
315 return 1;
316 }
317
318 static Object *qdev_get_peripheral(void)
319 {
320 static Object *dev;
321
322 if (dev == NULL) {
323 dev = container_get(qdev_get_machine(), "/peripheral");
324 }
325
326 return dev;
327 }
328
329 static Object *qdev_get_peripheral_anon(void)
330 {
331 static Object *dev;
332
333 if (dev == NULL) {
334 dev = container_get(qdev_get_machine(), "/peripheral-anon");
335 }
336
337 return dev;
338 }
339
340 static void qbus_error_append_bus_list_hint(DeviceState *dev,
341 Error *const *errp)
342 {
343 BusState *child;
344 const char *sep = " ";
345
346 error_append_hint(errp, "child buses at \"%s\":",
347 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
348 QLIST_FOREACH(child, &dev->child_bus, sibling) {
349 error_append_hint(errp, "%s\"%s\"", sep, child->name);
350 sep = ", ";
351 }
352 error_append_hint(errp, "\n");
353 }
354
355 static void qbus_error_append_dev_list_hint(BusState *bus,
356 Error *const *errp)
357 {
358 BusChild *kid;
359 const char *sep = " ";
360
361 error_append_hint(errp, "devices at \"%s\":", bus->name);
362 QTAILQ_FOREACH(kid, &bus->children, sibling) {
363 DeviceState *dev = kid->child;
364 error_append_hint(errp, "%s\"%s\"", sep,
365 object_get_typename(OBJECT(dev)));
366 if (dev->id) {
367 error_append_hint(errp, "/\"%s\"", dev->id);
368 }
369 sep = ", ";
370 }
371 error_append_hint(errp, "\n");
372 }
373
374 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
375 {
376 BusState *child;
377
378 QLIST_FOREACH(child, &dev->child_bus, sibling) {
379 if (strcmp(child->name, elem) == 0) {
380 return child;
381 }
382 }
383 return NULL;
384 }
385
386 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
387 {
388 BusChild *kid;
389
390 /*
391 * try to match in order:
392 * (1) instance id, if present
393 * (2) driver name
394 * (3) driver alias, if present
395 */
396 QTAILQ_FOREACH(kid, &bus->children, sibling) {
397 DeviceState *dev = kid->child;
398 if (dev->id && strcmp(dev->id, elem) == 0) {
399 return dev;
400 }
401 }
402 QTAILQ_FOREACH(kid, &bus->children, sibling) {
403 DeviceState *dev = kid->child;
404 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
405 return dev;
406 }
407 }
408 QTAILQ_FOREACH(kid, &bus->children, sibling) {
409 DeviceState *dev = kid->child;
410 DeviceClass *dc = DEVICE_GET_CLASS(dev);
411
412 if (qdev_class_has_alias(dc) &&
413 strcmp(qdev_class_get_alias(dc), elem) == 0) {
414 return dev;
415 }
416 }
417 return NULL;
418 }
419
420 static inline bool qbus_is_full(BusState *bus)
421 {
422 BusClass *bus_class = BUS_GET_CLASS(bus);
423 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
424 }
425
426 /*
427 * Search the tree rooted at @bus for a bus.
428 * If @name, search for a bus with that name. Note that bus names
429 * need not be unique. Yes, that's screwed up.
430 * Else search for a bus that is a subtype of @bus_typename.
431 * If more than one exists, prefer one that can take another device.
432 * Return the bus if found, else %NULL.
433 */
434 static BusState *qbus_find_recursive(BusState *bus, const char *name,
435 const char *bus_typename)
436 {
437 BusChild *kid;
438 BusState *pick, *child, *ret;
439 bool match;
440
441 assert(name || bus_typename);
442 if (name) {
443 match = !strcmp(bus->name, name);
444 } else {
445 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
446 }
447
448 if (match && !qbus_is_full(bus)) {
449 return bus; /* root matches and isn't full */
450 }
451
452 pick = match ? bus : NULL;
453
454 QTAILQ_FOREACH(kid, &bus->children, sibling) {
455 DeviceState *dev = kid->child;
456 QLIST_FOREACH(child, &dev->child_bus, sibling) {
457 ret = qbus_find_recursive(child, name, bus_typename);
458 if (ret && !qbus_is_full(ret)) {
459 return ret; /* a descendant matches and isn't full */
460 }
461 if (ret && !pick) {
462 pick = ret;
463 }
464 }
465 }
466
467 /* root or a descendant matches, but is full */
468 return pick;
469 }
470
471 static BusState *qbus_find(const char *path, Error **errp)
472 {
473 DeviceState *dev;
474 BusState *bus;
475 char elem[128];
476 int pos, len;
477
478 /* find start element */
479 if (path[0] == '/') {
480 bus = sysbus_get_default();
481 pos = 0;
482 } else {
483 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
484 assert(!path[0]);
485 elem[0] = len = 0;
486 }
487 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
488 if (!bus) {
489 error_setg(errp, "Bus '%s' not found", elem);
490 return NULL;
491 }
492 pos = len;
493 }
494
495 for (;;) {
496 assert(path[pos] == '/' || !path[pos]);
497 while (path[pos] == '/') {
498 pos++;
499 }
500 if (path[pos] == '\0') {
501 break;
502 }
503
504 /* find device */
505 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
506 g_assert_not_reached();
507 elem[0] = len = 0;
508 }
509 pos += len;
510 dev = qbus_find_dev(bus, elem);
511 if (!dev) {
512 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
513 "Device '%s' not found", elem);
514 qbus_error_append_dev_list_hint(bus, errp);
515 return NULL;
516 }
517
518 assert(path[pos] == '/' || !path[pos]);
519 while (path[pos] == '/') {
520 pos++;
521 }
522 if (path[pos] == '\0') {
523 /* last specified element is a device. If it has exactly
524 * one child bus accept it nevertheless */
525 if (dev->num_child_bus == 1) {
526 bus = QLIST_FIRST(&dev->child_bus);
527 break;
528 }
529 if (dev->num_child_bus) {
530 error_setg(errp, "Device '%s' has multiple child buses",
531 elem);
532 qbus_error_append_bus_list_hint(dev, errp);
533 } else {
534 error_setg(errp, "Device '%s' has no child bus", elem);
535 }
536 return NULL;
537 }
538
539 /* find bus */
540 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
541 g_assert_not_reached();
542 elem[0] = len = 0;
543 }
544 pos += len;
545 bus = qbus_find_bus(dev, elem);
546 if (!bus) {
547 error_setg(errp, "Bus '%s' not found", elem);
548 qbus_error_append_bus_list_hint(dev, errp);
549 return NULL;
550 }
551 }
552
553 if (qbus_is_full(bus)) {
554 error_setg(errp, "Bus '%s' is full", path);
555 return NULL;
556 }
557 return bus;
558 }
559
560 void qdev_set_id(DeviceState *dev, const char *id)
561 {
562 if (id) {
563 dev->id = id;
564 }
565
566 if (dev->id) {
567 object_property_add_child(qdev_get_peripheral(), dev->id,
568 OBJECT(dev));
569 } else {
570 static int anon_count;
571 gchar *name = g_strdup_printf("device[%d]", anon_count++);
572 object_property_add_child(qdev_get_peripheral_anon(), name,
573 OBJECT(dev));
574 g_free(name);
575 }
576 }
577
578 static int is_failover_device(void *opaque, const char *name, const char *value,
579 Error **errp)
580 {
581 if (strcmp(name, "failover_pair_id") == 0) {
582 QemuOpts *opts = (QemuOpts *)opaque;
583
584 if (qdev_should_hide_device(opts)) {
585 return 1;
586 }
587 }
588
589 return 0;
590 }
591
592 static bool should_hide_device(QemuOpts *opts)
593 {
594 if (qemu_opt_foreach(opts, is_failover_device, opts, NULL) == 0) {
595 return false;
596 }
597 return true;
598 }
599
600 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
601 {
602 DeviceClass *dc;
603 const char *driver, *path;
604 DeviceState *dev = NULL;
605 BusState *bus = NULL;
606 Error *err = NULL;
607 bool hide;
608
609 driver = qemu_opt_get(opts, "driver");
610 if (!driver) {
611 error_setg(errp, QERR_MISSING_PARAMETER, "driver");
612 return NULL;
613 }
614
615 /* find driver */
616 dc = qdev_get_device_class(&driver, errp);
617 if (!dc) {
618 return NULL;
619 }
620
621 /* find bus */
622 path = qemu_opt_get(opts, "bus");
623 if (path != NULL) {
624 bus = qbus_find(path, errp);
625 if (!bus) {
626 return NULL;
627 }
628 if (!object_dynamic_cast(OBJECT(bus), dc->bus_type)) {
629 error_setg(errp, "Device '%s' can't go on %s bus",
630 driver, object_get_typename(OBJECT(bus)));
631 return NULL;
632 }
633 } else if (dc->bus_type != NULL) {
634 bus = qbus_find_recursive(sysbus_get_default(), NULL, dc->bus_type);
635 if (!bus || qbus_is_full(bus)) {
636 error_setg(errp, "No '%s' bus found for device '%s'",
637 dc->bus_type, driver);
638 return NULL;
639 }
640 }
641 hide = should_hide_device(opts);
642
643 if ((hide || qdev_hotplug) && bus && !qbus_is_hotpluggable(bus)) {
644 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
645 return NULL;
646 }
647
648 if (hide) {
649 return NULL;
650 }
651
652 if (!migration_is_idle()) {
653 error_setg(errp, "device_add not allowed while migrating");
654 return NULL;
655 }
656
657 /* create device */
658 dev = qdev_new(driver);
659
660 /* Check whether the hotplug is allowed by the machine */
661 if (qdev_hotplug && !qdev_hotplug_allowed(dev, &err)) {
662 /* Error must be set in the machine hook */
663 assert(err);
664 goto err_del_dev;
665 }
666
667 if (!bus && qdev_hotplug && !qdev_get_machine_hotplug_handler(dev)) {
668 /* No bus, no machine hotplug handler --> device is not hotpluggable */
669 error_setg(&err, "Device '%s' can not be hotplugged on this machine",
670 driver);
671 goto err_del_dev;
672 }
673
674 qdev_set_id(dev, qemu_opts_id(opts));
675
676 /* set properties */
677 if (qemu_opt_foreach(opts, set_property, dev, &err)) {
678 goto err_del_dev;
679 }
680
681 dev->opts = opts;
682 if (!qdev_realize(DEVICE(dev), bus, &err)) {
683 dev->opts = NULL;
684 goto err_del_dev;
685 }
686 return dev;
687
688 err_del_dev:
689 error_propagate(errp, err);
690 if (dev) {
691 object_unparent(OBJECT(dev));
692 object_unref(OBJECT(dev));
693 }
694 return NULL;
695 }
696
697
698 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
699 static void qbus_print(Monitor *mon, BusState *bus, int indent);
700
701 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
702 int indent)
703 {
704 if (!props)
705 return;
706 for (; props->name; props++) {
707 Error *err = NULL;
708 char *value;
709 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
710 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
711 value = object_property_get_str(OBJECT(dev), legacy_name, &err);
712 } else {
713 value = object_property_print(OBJECT(dev), props->name, true, &err);
714 }
715 g_free(legacy_name);
716
717 if (err) {
718 error_free(err);
719 continue;
720 }
721 qdev_printf("%s = %s\n", props->name,
722 value && *value ? value : "<null>");
723 g_free(value);
724 }
725 }
726
727 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
728 {
729 BusClass *bc = BUS_GET_CLASS(bus);
730
731 if (bc->print_dev) {
732 bc->print_dev(mon, dev, indent);
733 }
734 }
735
736 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
737 {
738 ObjectClass *class;
739 BusState *child;
740 NamedGPIOList *ngl;
741 NamedClockList *ncl;
742
743 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
744 dev->id ? dev->id : "");
745 indent += 2;
746 QLIST_FOREACH(ngl, &dev->gpios, node) {
747 if (ngl->num_in) {
748 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
749 ngl->num_in);
750 }
751 if (ngl->num_out) {
752 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
753 ngl->num_out);
754 }
755 }
756 QLIST_FOREACH(ncl, &dev->clocks, node) {
757 qdev_printf("clock-%s%s \"%s\" freq_hz=%e\n",
758 ncl->output ? "out" : "in",
759 ncl->alias ? " (alias)" : "",
760 ncl->name,
761 CLOCK_PERIOD_TO_HZ(1.0 * clock_get(ncl->clock)));
762 }
763 class = object_get_class(OBJECT(dev));
764 do {
765 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
766 class = object_class_get_parent(class);
767 } while (class != object_class_by_name(TYPE_DEVICE));
768 bus_print_dev(dev->parent_bus, mon, dev, indent);
769 QLIST_FOREACH(child, &dev->child_bus, sibling) {
770 qbus_print(mon, child, indent);
771 }
772 }
773
774 static void qbus_print(Monitor *mon, BusState *bus, int indent)
775 {
776 BusChild *kid;
777
778 qdev_printf("bus: %s\n", bus->name);
779 indent += 2;
780 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
781 QTAILQ_FOREACH(kid, &bus->children, sibling) {
782 DeviceState *dev = kid->child;
783 qdev_print(mon, dev, indent);
784 }
785 }
786 #undef qdev_printf
787
788 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
789 {
790 if (sysbus_get_default())
791 qbus_print(mon, sysbus_get_default(), 0);
792 }
793
794 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
795 {
796 qdev_print_devinfos(true);
797 }
798
799 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
800 {
801 Error *local_err = NULL;
802 QemuOpts *opts;
803 DeviceState *dev;
804
805 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
806 if (!opts) {
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 }