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