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