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