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