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