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