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