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