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