Merge tag 'pull-target-arm-20220519' of https://git.linaro.org/people/pmaydell/qemu...
[qemu.git] / softmmu / 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.1 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/dispatch.h"
29 #include "qapi/qmp/qdict.h"
30 #include "qapi/qmp/qerror.h"
31 #include "qapi/qmp/qstring.h"
32 #include "qapi/qobject-input-visitor.h"
33 #include "qemu/config-file.h"
34 #include "qemu/error-report.h"
35 #include "qemu/help_option.h"
36 #include "qemu/option.h"
37 #include "qemu/qemu-print.h"
38 #include "qemu/option_int.h"
39 #include "sysemu/block-backend.h"
40 #include "migration/misc.h"
41 #include "migration/migration.h"
42 #include "qemu/cutils.h"
43 #include "hw/qdev-properties.h"
44 #include "hw/clock.h"
45
46 /*
47 * Aliases were a bad idea from the start. Let's keep them
48 * from spreading further.
49 */
50 typedef struct QDevAlias
51 {
52 const char *typename;
53 const char *alias;
54 uint32_t arch_mask;
55 } QDevAlias;
56
57 /* default virtio transport per architecture */
58 #define QEMU_ARCH_VIRTIO_PCI (QEMU_ARCH_ALPHA | QEMU_ARCH_ARM | \
59 QEMU_ARCH_HPPA | QEMU_ARCH_I386 | \
60 QEMU_ARCH_MIPS | QEMU_ARCH_PPC | \
61 QEMU_ARCH_RISCV | QEMU_ARCH_SH4 | \
62 QEMU_ARCH_SPARC | QEMU_ARCH_XTENSA)
63 #define QEMU_ARCH_VIRTIO_CCW (QEMU_ARCH_S390X)
64 #define QEMU_ARCH_VIRTIO_MMIO (QEMU_ARCH_M68K)
65
66 /* Please keep this table sorted by typename. */
67 static const QDevAlias qdev_alias_table[] = {
68 { "AC97", "ac97" }, /* -soundhw name */
69 { "e1000", "e1000-82540em" },
70 { "ES1370", "es1370" }, /* -soundhw name */
71 { "ich9-ahci", "ahci" },
72 { "lsi53c895a", "lsi" },
73 { "virtio-9p-device", "virtio-9p", QEMU_ARCH_VIRTIO_MMIO },
74 { "virtio-9p-ccw", "virtio-9p", QEMU_ARCH_VIRTIO_CCW },
75 { "virtio-9p-pci", "virtio-9p", QEMU_ARCH_VIRTIO_PCI },
76 { "virtio-balloon-device", "virtio-balloon", QEMU_ARCH_VIRTIO_MMIO },
77 { "virtio-balloon-ccw", "virtio-balloon", QEMU_ARCH_VIRTIO_CCW },
78 { "virtio-balloon-pci", "virtio-balloon", QEMU_ARCH_VIRTIO_PCI },
79 { "virtio-blk-device", "virtio-blk", QEMU_ARCH_VIRTIO_MMIO },
80 { "virtio-blk-ccw", "virtio-blk", QEMU_ARCH_VIRTIO_CCW },
81 { "virtio-blk-pci", "virtio-blk", QEMU_ARCH_VIRTIO_PCI },
82 { "virtio-gpu-device", "virtio-gpu", QEMU_ARCH_VIRTIO_MMIO },
83 { "virtio-gpu-ccw", "virtio-gpu", QEMU_ARCH_VIRTIO_CCW },
84 { "virtio-gpu-pci", "virtio-gpu", QEMU_ARCH_VIRTIO_PCI },
85 { "virtio-input-host-device", "virtio-input-host", QEMU_ARCH_VIRTIO_MMIO },
86 { "virtio-input-host-ccw", "virtio-input-host", QEMU_ARCH_VIRTIO_CCW },
87 { "virtio-input-host-pci", "virtio-input-host", QEMU_ARCH_VIRTIO_PCI },
88 { "virtio-iommu-pci", "virtio-iommu", QEMU_ARCH_VIRTIO_PCI },
89 { "virtio-keyboard-device", "virtio-keyboard", QEMU_ARCH_VIRTIO_MMIO },
90 { "virtio-keyboard-ccw", "virtio-keyboard", QEMU_ARCH_VIRTIO_CCW },
91 { "virtio-keyboard-pci", "virtio-keyboard", QEMU_ARCH_VIRTIO_PCI },
92 { "virtio-mouse-device", "virtio-mouse", QEMU_ARCH_VIRTIO_MMIO },
93 { "virtio-mouse-ccw", "virtio-mouse", QEMU_ARCH_VIRTIO_CCW },
94 { "virtio-mouse-pci", "virtio-mouse", QEMU_ARCH_VIRTIO_PCI },
95 { "virtio-net-device", "virtio-net", QEMU_ARCH_VIRTIO_MMIO },
96 { "virtio-net-ccw", "virtio-net", QEMU_ARCH_VIRTIO_CCW },
97 { "virtio-net-pci", "virtio-net", QEMU_ARCH_VIRTIO_PCI },
98 { "virtio-rng-device", "virtio-rng", QEMU_ARCH_VIRTIO_MMIO },
99 { "virtio-rng-ccw", "virtio-rng", QEMU_ARCH_VIRTIO_CCW },
100 { "virtio-rng-pci", "virtio-rng", QEMU_ARCH_VIRTIO_PCI },
101 { "virtio-scsi-device", "virtio-scsi", QEMU_ARCH_VIRTIO_MMIO },
102 { "virtio-scsi-ccw", "virtio-scsi", QEMU_ARCH_VIRTIO_CCW },
103 { "virtio-scsi-pci", "virtio-scsi", QEMU_ARCH_VIRTIO_PCI },
104 { "virtio-serial-device", "virtio-serial", QEMU_ARCH_VIRTIO_MMIO },
105 { "virtio-serial-ccw", "virtio-serial", QEMU_ARCH_VIRTIO_CCW },
106 { "virtio-serial-pci", "virtio-serial", QEMU_ARCH_VIRTIO_PCI},
107 { "virtio-tablet-device", "virtio-tablet", QEMU_ARCH_VIRTIO_MMIO },
108 { "virtio-tablet-ccw", "virtio-tablet", QEMU_ARCH_VIRTIO_CCW },
109 { "virtio-tablet-pci", "virtio-tablet", QEMU_ARCH_VIRTIO_PCI },
110 { }
111 };
112
113 static const char *qdev_class_get_alias(DeviceClass *dc)
114 {
115 const char *typename = object_class_get_name(OBJECT_CLASS(dc));
116 int i;
117
118 for (i = 0; qdev_alias_table[i].typename; i++) {
119 if (qdev_alias_table[i].arch_mask &&
120 !(qdev_alias_table[i].arch_mask & arch_type)) {
121 continue;
122 }
123
124 if (strcmp(qdev_alias_table[i].typename, typename) == 0) {
125 return qdev_alias_table[i].alias;
126 }
127 }
128
129 return NULL;
130 }
131
132 static bool qdev_class_has_alias(DeviceClass *dc)
133 {
134 return (qdev_class_get_alias(dc) != NULL);
135 }
136
137 static void qdev_print_devinfo(DeviceClass *dc)
138 {
139 qemu_printf("name \"%s\"", object_class_get_name(OBJECT_CLASS(dc)));
140 if (dc->bus_type) {
141 qemu_printf(", bus %s", dc->bus_type);
142 }
143 if (qdev_class_has_alias(dc)) {
144 qemu_printf(", alias \"%s\"", qdev_class_get_alias(dc));
145 }
146 if (dc->desc) {
147 qemu_printf(", desc \"%s\"", dc->desc);
148 }
149 if (!dc->user_creatable) {
150 qemu_printf(", no-user");
151 }
152 qemu_printf("\n");
153 }
154
155 static void qdev_print_devinfos(bool show_no_user)
156 {
157 static const char *cat_name[DEVICE_CATEGORY_MAX + 1] = {
158 [DEVICE_CATEGORY_BRIDGE] = "Controller/Bridge/Hub",
159 [DEVICE_CATEGORY_USB] = "USB",
160 [DEVICE_CATEGORY_STORAGE] = "Storage",
161 [DEVICE_CATEGORY_NETWORK] = "Network",
162 [DEVICE_CATEGORY_INPUT] = "Input",
163 [DEVICE_CATEGORY_DISPLAY] = "Display",
164 [DEVICE_CATEGORY_SOUND] = "Sound",
165 [DEVICE_CATEGORY_MISC] = "Misc",
166 [DEVICE_CATEGORY_CPU] = "CPU",
167 [DEVICE_CATEGORY_MAX] = "Uncategorized",
168 };
169 GSList *list, *elt;
170 int i;
171 bool cat_printed;
172
173 module_load_qom_all();
174 list = object_class_get_list_sorted(TYPE_DEVICE, false);
175
176 for (i = 0; i <= DEVICE_CATEGORY_MAX; i++) {
177 cat_printed = false;
178 for (elt = list; elt; elt = elt->next) {
179 DeviceClass *dc = OBJECT_CLASS_CHECK(DeviceClass, elt->data,
180 TYPE_DEVICE);
181 if ((i < DEVICE_CATEGORY_MAX
182 ? !test_bit(i, dc->categories)
183 : !bitmap_empty(dc->categories, DEVICE_CATEGORY_MAX))
184 || (!show_no_user
185 && !dc->user_creatable)) {
186 continue;
187 }
188 if (!cat_printed) {
189 qemu_printf("%s%s devices:\n", i ? "\n" : "", cat_name[i]);
190 cat_printed = true;
191 }
192 qdev_print_devinfo(dc);
193 }
194 }
195
196 g_slist_free(list);
197 }
198
199 static const char *find_typename_by_alias(const char *alias)
200 {
201 int i;
202
203 for (i = 0; qdev_alias_table[i].alias; i++) {
204 if (qdev_alias_table[i].arch_mask &&
205 !(qdev_alias_table[i].arch_mask & arch_type)) {
206 continue;
207 }
208
209 if (strcmp(qdev_alias_table[i].alias, alias) == 0) {
210 return qdev_alias_table[i].typename;
211 }
212 }
213
214 return NULL;
215 }
216
217 static DeviceClass *qdev_get_device_class(const char **driver, Error **errp)
218 {
219 ObjectClass *oc;
220 DeviceClass *dc;
221 const char *original_name = *driver;
222
223 oc = module_object_class_by_name(*driver);
224 if (!oc) {
225 const char *typename = find_typename_by_alias(*driver);
226
227 if (typename) {
228 *driver = typename;
229 oc = module_object_class_by_name(*driver);
230 }
231 }
232
233 if (!object_class_dynamic_cast(oc, TYPE_DEVICE)) {
234 if (*driver != original_name) {
235 error_setg(errp, "'%s' (alias '%s') is not a valid device model"
236 " name", original_name, *driver);
237 } else {
238 error_setg(errp, "'%s' is not a valid device model name", *driver);
239 }
240 return NULL;
241 }
242
243 if (object_class_is_abstract(oc)) {
244 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
245 "a non-abstract device type");
246 return NULL;
247 }
248
249 dc = DEVICE_CLASS(oc);
250 if (!dc->user_creatable ||
251 (phase_check(PHASE_MACHINE_READY) && !dc->hotpluggable)) {
252 error_setg(errp, QERR_INVALID_PARAMETER_VALUE, "driver",
253 "a pluggable device type");
254 return NULL;
255 }
256
257 return dc;
258 }
259
260
261 int qdev_device_help(QemuOpts *opts)
262 {
263 Error *local_err = NULL;
264 const char *driver;
265 ObjectPropertyInfoList *prop_list;
266 ObjectPropertyInfoList *prop;
267 GPtrArray *array;
268 int i;
269
270 driver = qemu_opt_get(opts, "driver");
271 if (driver && is_help_option(driver)) {
272 qdev_print_devinfos(false);
273 return 1;
274 }
275
276 if (!driver || !qemu_opt_has_help_opt(opts)) {
277 return 0;
278 }
279
280 if (!object_class_by_name(driver)) {
281 const char *typename = find_typename_by_alias(driver);
282
283 if (typename) {
284 driver = typename;
285 }
286 }
287
288 prop_list = qmp_device_list_properties(driver, &local_err);
289 if (local_err) {
290 goto error;
291 }
292
293 if (prop_list) {
294 qemu_printf("%s options:\n", driver);
295 } else {
296 qemu_printf("There are no options for %s.\n", driver);
297 }
298 array = g_ptr_array_new();
299 for (prop = prop_list; prop; prop = prop->next) {
300 g_ptr_array_add(array,
301 object_property_help(prop->value->name,
302 prop->value->type,
303 prop->value->default_value,
304 prop->value->description));
305 }
306 g_ptr_array_sort(array, (GCompareFunc)qemu_pstrcmp0);
307 for (i = 0; i < array->len; i++) {
308 qemu_printf("%s\n", (char *)array->pdata[i]);
309 }
310 g_ptr_array_set_free_func(array, g_free);
311 g_ptr_array_free(array, true);
312 qapi_free_ObjectPropertyInfoList(prop_list);
313 return 1;
314
315 error:
316 error_report_err(local_err);
317 return 1;
318 }
319
320 static Object *qdev_get_peripheral(void)
321 {
322 static Object *dev;
323
324 if (dev == NULL) {
325 dev = container_get(qdev_get_machine(), "/peripheral");
326 }
327
328 return dev;
329 }
330
331 static Object *qdev_get_peripheral_anon(void)
332 {
333 static Object *dev;
334
335 if (dev == NULL) {
336 dev = container_get(qdev_get_machine(), "/peripheral-anon");
337 }
338
339 return dev;
340 }
341
342 static void qbus_error_append_bus_list_hint(DeviceState *dev,
343 Error *const *errp)
344 {
345 BusState *child;
346 const char *sep = " ";
347
348 error_append_hint(errp, "child buses at \"%s\":",
349 dev->id ? dev->id : object_get_typename(OBJECT(dev)));
350 QLIST_FOREACH(child, &dev->child_bus, sibling) {
351 error_append_hint(errp, "%s\"%s\"", sep, child->name);
352 sep = ", ";
353 }
354 error_append_hint(errp, "\n");
355 }
356
357 static void qbus_error_append_dev_list_hint(BusState *bus,
358 Error *const *errp)
359 {
360 BusChild *kid;
361 const char *sep = " ";
362
363 error_append_hint(errp, "devices at \"%s\":", bus->name);
364 QTAILQ_FOREACH(kid, &bus->children, sibling) {
365 DeviceState *dev = kid->child;
366 error_append_hint(errp, "%s\"%s\"", sep,
367 object_get_typename(OBJECT(dev)));
368 if (dev->id) {
369 error_append_hint(errp, "/\"%s\"", dev->id);
370 }
371 sep = ", ";
372 }
373 error_append_hint(errp, "\n");
374 }
375
376 static BusState *qbus_find_bus(DeviceState *dev, char *elem)
377 {
378 BusState *child;
379
380 QLIST_FOREACH(child, &dev->child_bus, sibling) {
381 if (strcmp(child->name, elem) == 0) {
382 return child;
383 }
384 }
385 return NULL;
386 }
387
388 static DeviceState *qbus_find_dev(BusState *bus, char *elem)
389 {
390 BusChild *kid;
391
392 /*
393 * try to match in order:
394 * (1) instance id, if present
395 * (2) driver name
396 * (3) driver alias, if present
397 */
398 QTAILQ_FOREACH(kid, &bus->children, sibling) {
399 DeviceState *dev = kid->child;
400 if (dev->id && strcmp(dev->id, elem) == 0) {
401 return dev;
402 }
403 }
404 QTAILQ_FOREACH(kid, &bus->children, sibling) {
405 DeviceState *dev = kid->child;
406 if (strcmp(object_get_typename(OBJECT(dev)), elem) == 0) {
407 return dev;
408 }
409 }
410 QTAILQ_FOREACH(kid, &bus->children, sibling) {
411 DeviceState *dev = kid->child;
412 DeviceClass *dc = DEVICE_GET_CLASS(dev);
413
414 if (qdev_class_has_alias(dc) &&
415 strcmp(qdev_class_get_alias(dc), elem) == 0) {
416 return dev;
417 }
418 }
419 return NULL;
420 }
421
422 static inline bool qbus_is_full(BusState *bus)
423 {
424 BusClass *bus_class;
425
426 if (bus->full) {
427 return true;
428 }
429 bus_class = BUS_GET_CLASS(bus);
430 return bus_class->max_dev && bus->num_children >= bus_class->max_dev;
431 }
432
433 /*
434 * Search the tree rooted at @bus for a bus.
435 * If @name, search for a bus with that name. Note that bus names
436 * need not be unique. Yes, that's screwed up.
437 * Else search for a bus that is a subtype of @bus_typename.
438 * If more than one exists, prefer one that can take another device.
439 * Return the bus if found, else %NULL.
440 */
441 static BusState *qbus_find_recursive(BusState *bus, const char *name,
442 const char *bus_typename)
443 {
444 BusChild *kid;
445 BusState *pick, *child, *ret;
446 bool match;
447
448 assert(name || bus_typename);
449 if (name) {
450 match = !strcmp(bus->name, name);
451 } else {
452 match = !!object_dynamic_cast(OBJECT(bus), bus_typename);
453 }
454
455 if (match && !qbus_is_full(bus)) {
456 return bus; /* root matches and isn't full */
457 }
458
459 pick = match ? bus : NULL;
460
461 QTAILQ_FOREACH(kid, &bus->children, sibling) {
462 DeviceState *dev = kid->child;
463 QLIST_FOREACH(child, &dev->child_bus, sibling) {
464 ret = qbus_find_recursive(child, name, bus_typename);
465 if (ret && !qbus_is_full(ret)) {
466 return ret; /* a descendant matches and isn't full */
467 }
468 if (ret && !pick) {
469 pick = ret;
470 }
471 }
472 }
473
474 /* root or a descendant matches, but is full */
475 return pick;
476 }
477
478 static BusState *qbus_find(const char *path, Error **errp)
479 {
480 DeviceState *dev;
481 BusState *bus;
482 char elem[128];
483 int pos, len;
484
485 /* find start element */
486 if (path[0] == '/') {
487 bus = sysbus_get_default();
488 pos = 0;
489 } else {
490 if (sscanf(path, "%127[^/]%n", elem, &len) != 1) {
491 assert(!path[0]);
492 elem[0] = len = 0;
493 }
494 bus = qbus_find_recursive(sysbus_get_default(), elem, NULL);
495 if (!bus) {
496 error_setg(errp, "Bus '%s' not found", elem);
497 return NULL;
498 }
499 pos = len;
500 }
501
502 for (;;) {
503 assert(path[pos] == '/' || !path[pos]);
504 while (path[pos] == '/') {
505 pos++;
506 }
507 if (path[pos] == '\0') {
508 break;
509 }
510
511 /* find device */
512 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
513 g_assert_not_reached();
514 elem[0] = len = 0;
515 }
516 pos += len;
517 dev = qbus_find_dev(bus, elem);
518 if (!dev) {
519 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
520 "Device '%s' not found", elem);
521 qbus_error_append_dev_list_hint(bus, errp);
522 return NULL;
523 }
524
525 assert(path[pos] == '/' || !path[pos]);
526 while (path[pos] == '/') {
527 pos++;
528 }
529 if (path[pos] == '\0') {
530 /* last specified element is a device. If it has exactly
531 * one child bus accept it nevertheless */
532 if (dev->num_child_bus == 1) {
533 bus = QLIST_FIRST(&dev->child_bus);
534 break;
535 }
536 if (dev->num_child_bus) {
537 error_setg(errp, "Device '%s' has multiple child buses",
538 elem);
539 qbus_error_append_bus_list_hint(dev, errp);
540 } else {
541 error_setg(errp, "Device '%s' has no child bus", elem);
542 }
543 return NULL;
544 }
545
546 /* find bus */
547 if (sscanf(path+pos, "%127[^/]%n", elem, &len) != 1) {
548 g_assert_not_reached();
549 elem[0] = len = 0;
550 }
551 pos += len;
552 bus = qbus_find_bus(dev, elem);
553 if (!bus) {
554 error_setg(errp, "Bus '%s' not found", elem);
555 qbus_error_append_bus_list_hint(dev, errp);
556 return NULL;
557 }
558 }
559
560 if (qbus_is_full(bus)) {
561 error_setg(errp, "Bus '%s' is full", path);
562 return NULL;
563 }
564 return bus;
565 }
566
567 /* Takes ownership of @id, will be freed when deleting the device */
568 const char *qdev_set_id(DeviceState *dev, char *id, Error **errp)
569 {
570 ObjectProperty *prop;
571
572 assert(!dev->id && !dev->realized);
573
574 /*
575 * object_property_[try_]add_child() below will assert the device
576 * has no parent
577 */
578 if (id) {
579 prop = object_property_try_add_child(qdev_get_peripheral(), id,
580 OBJECT(dev), NULL);
581 if (prop) {
582 dev->id = id;
583 } else {
584 g_free(id);
585 error_setg(errp, "Duplicate device ID '%s'", id);
586 return NULL;
587 }
588 } else {
589 static int anon_count;
590 gchar *name = g_strdup_printf("device[%d]", anon_count++);
591 prop = object_property_add_child(qdev_get_peripheral_anon(), name,
592 OBJECT(dev));
593 g_free(name);
594 }
595
596 return prop->name;
597 }
598
599 DeviceState *qdev_device_add_from_qdict(const QDict *opts,
600 bool from_json, Error **errp)
601 {
602 ERRP_GUARD();
603 DeviceClass *dc;
604 const char *driver, *path;
605 char *id;
606 DeviceState *dev = NULL;
607 BusState *bus = NULL;
608
609 driver = qdict_get_try_str(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 = qdict_get_try_str(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
642 if (qdev_should_hide_device(opts, from_json, errp)) {
643 if (bus && !qbus_is_hotpluggable(bus)) {
644 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
645 }
646 return NULL;
647 } else if (*errp) {
648 return NULL;
649 }
650
651 if (phase_check(PHASE_MACHINE_READY) && bus && !qbus_is_hotpluggable(bus)) {
652 error_setg(errp, QERR_BUS_NO_HOTPLUG, bus->name);
653 return NULL;
654 }
655
656 if (!migration_is_idle()) {
657 error_setg(errp, "device_add not allowed while migrating");
658 return NULL;
659 }
660
661 /* create device */
662 dev = qdev_new(driver);
663
664 /* Check whether the hotplug is allowed by the machine */
665 if (phase_check(PHASE_MACHINE_READY)) {
666 if (!qdev_hotplug_allowed(dev, errp)) {
667 goto err_del_dev;
668 }
669
670 if (!bus && !qdev_get_machine_hotplug_handler(dev)) {
671 /* No bus, no machine hotplug handler --> device is not hotpluggable */
672 error_setg(errp, "Device '%s' can not be hotplugged on this machine",
673 driver);
674 goto err_del_dev;
675 }
676 }
677
678 /*
679 * set dev's parent and register its id.
680 * If it fails it means the id is already taken.
681 */
682 id = g_strdup(qdict_get_try_str(opts, "id"));
683 if (!qdev_set_id(dev, id, errp)) {
684 goto err_del_dev;
685 }
686
687 /* set properties */
688 dev->opts = qdict_clone_shallow(opts);
689 qdict_del(dev->opts, "driver");
690 qdict_del(dev->opts, "bus");
691 qdict_del(dev->opts, "id");
692
693 object_set_properties_from_keyval(&dev->parent_obj, dev->opts, from_json,
694 errp);
695 if (*errp) {
696 goto err_del_dev;
697 }
698
699 if (!qdev_realize(DEVICE(dev), bus, errp)) {
700 goto err_del_dev;
701 }
702 return dev;
703
704 err_del_dev:
705 if (dev) {
706 object_unparent(OBJECT(dev));
707 object_unref(OBJECT(dev));
708 }
709 return NULL;
710 }
711
712 /* Takes ownership of @opts on success */
713 DeviceState *qdev_device_add(QemuOpts *opts, Error **errp)
714 {
715 QDict *qdict = qemu_opts_to_qdict(opts, NULL);
716 DeviceState *ret;
717
718 ret = qdev_device_add_from_qdict(qdict, false, errp);
719 if (ret) {
720 qemu_opts_del(opts);
721 }
722 qobject_unref(qdict);
723 return ret;
724 }
725
726 #define qdev_printf(fmt, ...) monitor_printf(mon, "%*s" fmt, indent, "", ## __VA_ARGS__)
727 static void qbus_print(Monitor *mon, BusState *bus, int indent);
728
729 static void qdev_print_props(Monitor *mon, DeviceState *dev, Property *props,
730 int indent)
731 {
732 if (!props)
733 return;
734 for (; props->name; props++) {
735 char *value;
736 char *legacy_name = g_strdup_printf("legacy-%s", props->name);
737
738 if (object_property_get_type(OBJECT(dev), legacy_name, NULL)) {
739 value = object_property_get_str(OBJECT(dev), legacy_name, NULL);
740 } else {
741 value = object_property_print(OBJECT(dev), props->name, true,
742 NULL);
743 }
744 g_free(legacy_name);
745
746 if (!value) {
747 continue;
748 }
749 qdev_printf("%s = %s\n", props->name,
750 *value ? value : "<null>");
751 g_free(value);
752 }
753 }
754
755 static void bus_print_dev(BusState *bus, Monitor *mon, DeviceState *dev, int indent)
756 {
757 BusClass *bc = BUS_GET_CLASS(bus);
758
759 if (bc->print_dev) {
760 bc->print_dev(mon, dev, indent);
761 }
762 }
763
764 static void qdev_print(Monitor *mon, DeviceState *dev, int indent)
765 {
766 ObjectClass *class;
767 BusState *child;
768 NamedGPIOList *ngl;
769 NamedClockList *ncl;
770
771 qdev_printf("dev: %s, id \"%s\"\n", object_get_typename(OBJECT(dev)),
772 dev->id ? dev->id : "");
773 indent += 2;
774 QLIST_FOREACH(ngl, &dev->gpios, node) {
775 if (ngl->num_in) {
776 qdev_printf("gpio-in \"%s\" %d\n", ngl->name ? ngl->name : "",
777 ngl->num_in);
778 }
779 if (ngl->num_out) {
780 qdev_printf("gpio-out \"%s\" %d\n", ngl->name ? ngl->name : "",
781 ngl->num_out);
782 }
783 }
784 QLIST_FOREACH(ncl, &dev->clocks, node) {
785 g_autofree char *freq_str = clock_display_freq(ncl->clock);
786 qdev_printf("clock-%s%s \"%s\" freq_hz=%s\n",
787 ncl->output ? "out" : "in",
788 ncl->alias ? " (alias)" : "",
789 ncl->name, freq_str);
790 }
791 class = object_get_class(OBJECT(dev));
792 do {
793 qdev_print_props(mon, dev, DEVICE_CLASS(class)->props_, indent);
794 class = object_class_get_parent(class);
795 } while (class != object_class_by_name(TYPE_DEVICE));
796 bus_print_dev(dev->parent_bus, mon, dev, indent);
797 QLIST_FOREACH(child, &dev->child_bus, sibling) {
798 qbus_print(mon, child, indent);
799 }
800 }
801
802 static void qbus_print(Monitor *mon, BusState *bus, int indent)
803 {
804 BusChild *kid;
805
806 qdev_printf("bus: %s\n", bus->name);
807 indent += 2;
808 qdev_printf("type %s\n", object_get_typename(OBJECT(bus)));
809 QTAILQ_FOREACH(kid, &bus->children, sibling) {
810 DeviceState *dev = kid->child;
811 qdev_print(mon, dev, indent);
812 }
813 }
814 #undef qdev_printf
815
816 void hmp_info_qtree(Monitor *mon, const QDict *qdict)
817 {
818 if (sysbus_get_default())
819 qbus_print(mon, sysbus_get_default(), 0);
820 }
821
822 void hmp_info_qdm(Monitor *mon, const QDict *qdict)
823 {
824 qdev_print_devinfos(true);
825 }
826
827 void qmp_device_add(QDict *qdict, QObject **ret_data, Error **errp)
828 {
829 QemuOpts *opts;
830 DeviceState *dev;
831
832 opts = qemu_opts_from_qdict(qemu_find_opts("device"), qdict, errp);
833 if (!opts) {
834 return;
835 }
836 if (!monitor_cur_is_qmp() && qdev_device_help(opts)) {
837 qemu_opts_del(opts);
838 return;
839 }
840 dev = qdev_device_add(opts, errp);
841
842 /*
843 * Drain all pending RCU callbacks. This is done because
844 * some bus related operations can delay a device removal
845 * (in this case this can happen if device is added and then
846 * removed due to a configuration error)
847 * to a RCU callback, but user might expect that this interface
848 * will finish its job completely once qmp command returns result
849 * to the user
850 */
851 drain_call_rcu();
852
853 if (!dev) {
854 qemu_opts_del(opts);
855 return;
856 }
857 object_unref(OBJECT(dev));
858 }
859
860 static DeviceState *find_device_state(const char *id, Error **errp)
861 {
862 Object *obj;
863 DeviceState *dev;
864
865 if (id[0] == '/') {
866 obj = object_resolve_path(id, NULL);
867 } else {
868 obj = object_resolve_path_component(qdev_get_peripheral(), id);
869 }
870
871 if (!obj) {
872 error_set(errp, ERROR_CLASS_DEVICE_NOT_FOUND,
873 "Device '%s' not found", id);
874 return NULL;
875 }
876
877 dev = (DeviceState *)object_dynamic_cast(obj, TYPE_DEVICE);
878 if (!dev) {
879 error_setg(errp, "%s is not a hotpluggable device", id);
880 return NULL;
881 }
882
883 return dev;
884 }
885
886 void qdev_unplug(DeviceState *dev, Error **errp)
887 {
888 DeviceClass *dc = DEVICE_GET_CLASS(dev);
889 HotplugHandler *hotplug_ctrl;
890 HotplugHandlerClass *hdc;
891 Error *local_err = NULL;
892
893 if (dev->parent_bus && !qbus_is_hotpluggable(dev->parent_bus)) {
894 error_setg(errp, QERR_BUS_NO_HOTPLUG, dev->parent_bus->name);
895 return;
896 }
897
898 if (!dc->hotpluggable) {
899 error_setg(errp, QERR_DEVICE_NO_HOTPLUG,
900 object_get_typename(OBJECT(dev)));
901 return;
902 }
903
904 if (!migration_is_idle() && !dev->allow_unplug_during_migration) {
905 error_setg(errp, "device_del not allowed while migrating");
906 return;
907 }
908
909 qdev_hot_removed = true;
910
911 hotplug_ctrl = qdev_get_hotplug_handler(dev);
912 /* hotpluggable device MUST have HotplugHandler, if it doesn't
913 * then something is very wrong with it */
914 g_assert(hotplug_ctrl);
915
916 /* If device supports async unplug just request it to be done,
917 * otherwise just remove it synchronously */
918 hdc = HOTPLUG_HANDLER_GET_CLASS(hotplug_ctrl);
919 if (hdc->unplug_request) {
920 hotplug_handler_unplug_request(hotplug_ctrl, dev, &local_err);
921 } else {
922 hotplug_handler_unplug(hotplug_ctrl, dev, &local_err);
923 if (!local_err) {
924 object_unparent(OBJECT(dev));
925 }
926 }
927 error_propagate(errp, local_err);
928 }
929
930 void qmp_device_del(const char *id, Error **errp)
931 {
932 DeviceState *dev = find_device_state(id, errp);
933 if (dev != NULL) {
934 if (dev->pending_deleted_event) {
935 error_setg(errp, "Device %s is already in the "
936 "process of unplug", id);
937 return;
938 }
939
940 qdev_unplug(dev, errp);
941 }
942 }
943
944 void hmp_device_add(Monitor *mon, const QDict *qdict)
945 {
946 Error *err = NULL;
947
948 qmp_device_add((QDict *)qdict, NULL, &err);
949 hmp_handle_error(mon, err);
950 }
951
952 void hmp_device_del(Monitor *mon, const QDict *qdict)
953 {
954 const char *id = qdict_get_str(qdict, "id");
955 Error *err = NULL;
956
957 qmp_device_del(id, &err);
958 hmp_handle_error(mon, err);
959 }
960
961 BlockBackend *blk_by_qdev_id(const char *id, Error **errp)
962 {
963 DeviceState *dev;
964 BlockBackend *blk;
965
966 dev = find_device_state(id, errp);
967 if (dev == NULL) {
968 return NULL;
969 }
970
971 blk = blk_by_dev(dev);
972 if (!blk) {
973 error_setg(errp, "Device does not have a block device backend");
974 }
975 return blk;
976 }
977
978 QemuOptsList qemu_device_opts = {
979 .name = "device",
980 .implied_opt_name = "driver",
981 .head = QTAILQ_HEAD_INITIALIZER(qemu_device_opts.head),
982 .desc = {
983 /*
984 * no elements => accept any
985 * sanity checking will happen later
986 * when setting device properties
987 */
988 { /* end of list */ }
989 },
990 };
991
992 QemuOptsList qemu_global_opts = {
993 .name = "global",
994 .head = QTAILQ_HEAD_INITIALIZER(qemu_global_opts.head),
995 .desc = {
996 {
997 .name = "driver",
998 .type = QEMU_OPT_STRING,
999 },{
1000 .name = "property",
1001 .type = QEMU_OPT_STRING,
1002 },{
1003 .name = "value",
1004 .type = QEMU_OPT_STRING,
1005 },
1006 { /* end of list */ }
1007 },
1008 };
1009
1010 int qemu_global_option(const char *str)
1011 {
1012 char driver[64], property[64];
1013 QemuOpts *opts;
1014 int rc, offset;
1015
1016 rc = sscanf(str, "%63[^.=].%63[^=]%n", driver, property, &offset);
1017 if (rc == 2 && str[offset] == '=') {
1018 opts = qemu_opts_create(&qemu_global_opts, NULL, 0, &error_abort);
1019 qemu_opt_set(opts, "driver", driver, &error_abort);
1020 qemu_opt_set(opts, "property", property, &error_abort);
1021 qemu_opt_set(opts, "value", str + offset + 1, &error_abort);
1022 return 0;
1023 }
1024
1025 opts = qemu_opts_parse_noisily(&qemu_global_opts, str, false);
1026 if (!opts) {
1027 return -1;
1028 }
1029
1030 return 0;
1031 }
1032
1033 bool qmp_command_available(const QmpCommand *cmd, Error **errp)
1034 {
1035 if (!phase_check(PHASE_MACHINE_READY) &&
1036 !(cmd->options & QCO_ALLOW_PRECONFIG)) {
1037 error_setg(errp, "The command '%s' is permitted only after machine initialization has completed",
1038 cmd->name);
1039 return false;
1040 }
1041 return true;
1042 }