1 /* 2 * QEMU Machine 3 * 4 * Copyright (C) 2014 Red Hat Inc 5 * 6 * Authors: 7 * Marcel Apfelbaum <marcel.a@redhat.com> 8 * 9 * This work is licensed under the terms of the GNU GPL, version 2 or later. 10 * See the COPYING file in the top-level directory. 11 */ 12 13 #include "qemu/osdep.h" 14 #include "qemu/option.h" 15 #include "qapi/qmp/qerror.h" 16 #include "sysemu/replay.h" 17 #include "qemu/units.h" 18 #include "hw/boards.h" 19 #include "hw/loader.h" 20 #include "qapi/error.h" 21 #include "qapi/qapi-visit-common.h" 22 #include "qapi/qapi-visit-machine.h" 23 #include "qapi/visitor.h" 24 #include "hw/sysbus.h" 25 #include "sysemu/cpus.h" 26 #include "sysemu/sysemu.h" 27 #include "sysemu/reset.h" 28 #include "sysemu/runstate.h" 29 #include "sysemu/numa.h" 30 #include "qemu/error-report.h" 31 #include "sysemu/qtest.h" 32 #include "hw/pci/pci.h" 33 #include "hw/mem/nvdimm.h" 34 #include "migration/global_state.h" 35 #include "migration/vmstate.h" 36 #include "exec/confidential-guest-support.h" 37 #include "hw/virtio/virtio.h" 38 #include "hw/virtio/virtio-pci.h" 39 40 GlobalProperty hw_compat_6_1[] = { 41 { "vhost-user-vsock-device", "seqpacket", "off" }, 42 }; 43 const size_t hw_compat_6_1_len = G_N_ELEMENTS(hw_compat_6_1); 44 45 GlobalProperty hw_compat_6_0[] = { 46 { "gpex-pcihost", "allow-unmapped-accesses", "false" }, 47 { "i8042", "extended-state", "false"}, 48 { "nvme-ns", "eui64-default", "off"}, 49 { "e1000", "init-vet", "off" }, 50 { "e1000e", "init-vet", "off" }, 51 { "vhost-vsock-device", "seqpacket", "off" }, 52 }; 53 const size_t hw_compat_6_0_len = G_N_ELEMENTS(hw_compat_6_0); 54 55 GlobalProperty hw_compat_5_2[] = { 56 { "ICH9-LPC", "smm-compat", "on"}, 57 { "PIIX4_PM", "smm-compat", "on"}, 58 { "virtio-blk-device", "report-discard-granularity", "off" }, 59 { "virtio-net-pci", "vectors", "3"}, 60 }; 61 const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2); 62 63 GlobalProperty hw_compat_5_1[] = { 64 { "vhost-scsi", "num_queues", "1"}, 65 { "vhost-user-blk", "num-queues", "1"}, 66 { "vhost-user-scsi", "num_queues", "1"}, 67 { "virtio-blk-device", "num-queues", "1"}, 68 { "virtio-scsi-device", "num_queues", "1"}, 69 { "nvme", "use-intel-id", "on"}, 70 { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */ 71 { "pl011", "migrate-clk", "off" }, 72 { "virtio-pci", "x-ats-page-aligned", "off"}, 73 }; 74 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1); 75 76 GlobalProperty hw_compat_5_0[] = { 77 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" }, 78 { "virtio-balloon-device", "page-poison", "false" }, 79 { "vmport", "x-read-set-eax", "off" }, 80 { "vmport", "x-signal-unsupported-cmd", "off" }, 81 { "vmport", "x-report-vmx-type", "off" }, 82 { "vmport", "x-cmds-v2", "off" }, 83 { "virtio-device", "x-disable-legacy-check", "true" }, 84 }; 85 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0); 86 87 GlobalProperty hw_compat_4_2[] = { 88 { "virtio-blk-device", "queue-size", "128"}, 89 { "virtio-scsi-device", "virtqueue_size", "128"}, 90 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" }, 91 { "virtio-blk-device", "seg-max-adjust", "off"}, 92 { "virtio-scsi-device", "seg_max_adjust", "off"}, 93 { "vhost-blk-device", "seg_max_adjust", "off"}, 94 { "usb-host", "suppress-remote-wake", "off" }, 95 { "usb-redir", "suppress-remote-wake", "off" }, 96 { "qxl", "revision", "4" }, 97 { "qxl-vga", "revision", "4" }, 98 { "fw_cfg", "acpi-mr-restore", "false" }, 99 { "virtio-device", "use-disabled-flag", "false" }, 100 }; 101 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2); 102 103 GlobalProperty hw_compat_4_1[] = { 104 { "virtio-pci", "x-pcie-flr-init", "off" }, 105 }; 106 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1); 107 108 GlobalProperty hw_compat_4_0[] = { 109 { "VGA", "edid", "false" }, 110 { "secondary-vga", "edid", "false" }, 111 { "bochs-display", "edid", "false" }, 112 { "virtio-vga", "edid", "false" }, 113 { "virtio-gpu-device", "edid", "false" }, 114 { "virtio-device", "use-started", "false" }, 115 { "virtio-balloon-device", "qemu-4-0-config-size", "true" }, 116 { "pl031", "migrate-tick-offset", "false" }, 117 }; 118 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0); 119 120 GlobalProperty hw_compat_3_1[] = { 121 { "pcie-root-port", "x-speed", "2_5" }, 122 { "pcie-root-port", "x-width", "1" }, 123 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" }, 124 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" }, 125 { "tpm-crb", "ppi", "false" }, 126 { "tpm-tis", "ppi", "false" }, 127 { "usb-kbd", "serial", "42" }, 128 { "usb-mouse", "serial", "42" }, 129 { "usb-tablet", "serial", "42" }, 130 { "virtio-blk-device", "discard", "false" }, 131 { "virtio-blk-device", "write-zeroes", "false" }, 132 { "virtio-balloon-device", "qemu-4-0-config-size", "false" }, 133 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */ 134 }; 135 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1); 136 137 GlobalProperty hw_compat_3_0[] = {}; 138 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0); 139 140 GlobalProperty hw_compat_2_12[] = { 141 { "migration", "decompress-error-check", "off" }, 142 { "hda-audio", "use-timer", "false" }, 143 { "cirrus-vga", "global-vmstate", "true" }, 144 { "VGA", "global-vmstate", "true" }, 145 { "vmware-svga", "global-vmstate", "true" }, 146 { "qxl-vga", "global-vmstate", "true" }, 147 }; 148 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12); 149 150 GlobalProperty hw_compat_2_11[] = { 151 { "hpet", "hpet-offset-saved", "false" }, 152 { "virtio-blk-pci", "vectors", "2" }, 153 { "vhost-user-blk-pci", "vectors", "2" }, 154 { "e1000", "migrate_tso_props", "off" }, 155 }; 156 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11); 157 158 GlobalProperty hw_compat_2_10[] = { 159 { "virtio-mouse-device", "wheel-axis", "false" }, 160 { "virtio-tablet-device", "wheel-axis", "false" }, 161 }; 162 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10); 163 164 GlobalProperty hw_compat_2_9[] = { 165 { "pci-bridge", "shpc", "off" }, 166 { "intel-iommu", "pt", "off" }, 167 { "virtio-net-device", "x-mtu-bypass-backend", "off" }, 168 { "pcie-root-port", "x-migrate-msix", "false" }, 169 }; 170 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9); 171 172 GlobalProperty hw_compat_2_8[] = { 173 { "fw_cfg_mem", "x-file-slots", "0x10" }, 174 { "fw_cfg_io", "x-file-slots", "0x10" }, 175 { "pflash_cfi01", "old-multiple-chip-handling", "on" }, 176 { "pci-bridge", "shpc", "on" }, 177 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" }, 178 { "virtio-pci", "x-pcie-deverr-init", "off" }, 179 { "virtio-pci", "x-pcie-lnkctl-init", "off" }, 180 { "virtio-pci", "x-pcie-pm-init", "off" }, 181 { "cirrus-vga", "vgamem_mb", "8" }, 182 { "isa-cirrus-vga", "vgamem_mb", "8" }, 183 }; 184 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8); 185 186 GlobalProperty hw_compat_2_7[] = { 187 { "virtio-pci", "page-per-vq", "on" }, 188 { "virtio-serial-device", "emergency-write", "off" }, 189 { "ioapic", "version", "0x11" }, 190 { "intel-iommu", "x-buggy-eim", "true" }, 191 { "virtio-pci", "x-ignore-backend-features", "on" }, 192 }; 193 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7); 194 195 GlobalProperty hw_compat_2_6[] = { 196 { "virtio-mmio", "format_transport_address", "off" }, 197 /* Optional because not all virtio-pci devices support legacy mode */ 198 { "virtio-pci", "disable-modern", "on", .optional = true }, 199 { "virtio-pci", "disable-legacy", "off", .optional = true }, 200 }; 201 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6); 202 203 GlobalProperty hw_compat_2_5[] = { 204 { "isa-fdc", "fallback", "144" }, 205 { "pvscsi", "x-old-pci-configuration", "on" }, 206 { "pvscsi", "x-disable-pcie", "on" }, 207 { "vmxnet3", "x-old-msi-offsets", "on" }, 208 { "vmxnet3", "x-disable-pcie", "on" }, 209 }; 210 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5); 211 212 GlobalProperty hw_compat_2_4[] = { 213 /* Optional because the 'scsi' property is Linux-only */ 214 { "virtio-blk-device", "scsi", "true", .optional = true }, 215 { "e1000", "extra_mac_registers", "off" }, 216 { "virtio-pci", "x-disable-pcie", "on" }, 217 { "virtio-pci", "migrate-extra", "off" }, 218 { "fw_cfg_mem", "dma_enabled", "off" }, 219 { "fw_cfg_io", "dma_enabled", "off" } 220 }; 221 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4); 222 223 GlobalProperty hw_compat_2_3[] = { 224 { "virtio-blk-pci", "any_layout", "off" }, 225 { "virtio-balloon-pci", "any_layout", "off" }, 226 { "virtio-serial-pci", "any_layout", "off" }, 227 { "virtio-9p-pci", "any_layout", "off" }, 228 { "virtio-rng-pci", "any_layout", "off" }, 229 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" }, 230 { "migration", "send-configuration", "off" }, 231 { "migration", "send-section-footer", "off" }, 232 { "migration", "store-global-state", "off" }, 233 }; 234 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3); 235 236 GlobalProperty hw_compat_2_2[] = {}; 237 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2); 238 239 GlobalProperty hw_compat_2_1[] = { 240 { "intel-hda", "old_msi_addr", "on" }, 241 { "VGA", "qemu-extended-regs", "off" }, 242 { "secondary-vga", "qemu-extended-regs", "off" }, 243 { "virtio-scsi-pci", "any_layout", "off" }, 244 { "usb-mouse", "usb_version", "1" }, 245 { "usb-kbd", "usb_version", "1" }, 246 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" }, 247 }; 248 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1); 249 250 MachineState *current_machine; 251 252 static char *machine_get_kernel(Object *obj, Error **errp) 253 { 254 MachineState *ms = MACHINE(obj); 255 256 return g_strdup(ms->kernel_filename); 257 } 258 259 static void machine_set_kernel(Object *obj, const char *value, Error **errp) 260 { 261 MachineState *ms = MACHINE(obj); 262 263 g_free(ms->kernel_filename); 264 ms->kernel_filename = g_strdup(value); 265 } 266 267 static char *machine_get_initrd(Object *obj, Error **errp) 268 { 269 MachineState *ms = MACHINE(obj); 270 271 return g_strdup(ms->initrd_filename); 272 } 273 274 static void machine_set_initrd(Object *obj, const char *value, Error **errp) 275 { 276 MachineState *ms = MACHINE(obj); 277 278 g_free(ms->initrd_filename); 279 ms->initrd_filename = g_strdup(value); 280 } 281 282 static char *machine_get_append(Object *obj, Error **errp) 283 { 284 MachineState *ms = MACHINE(obj); 285 286 return g_strdup(ms->kernel_cmdline); 287 } 288 289 static void machine_set_append(Object *obj, const char *value, Error **errp) 290 { 291 MachineState *ms = MACHINE(obj); 292 293 g_free(ms->kernel_cmdline); 294 ms->kernel_cmdline = g_strdup(value); 295 } 296 297 static char *machine_get_dtb(Object *obj, Error **errp) 298 { 299 MachineState *ms = MACHINE(obj); 300 301 return g_strdup(ms->dtb); 302 } 303 304 static void machine_set_dtb(Object *obj, const char *value, Error **errp) 305 { 306 MachineState *ms = MACHINE(obj); 307 308 g_free(ms->dtb); 309 ms->dtb = g_strdup(value); 310 } 311 312 static char *machine_get_dumpdtb(Object *obj, Error **errp) 313 { 314 MachineState *ms = MACHINE(obj); 315 316 return g_strdup(ms->dumpdtb); 317 } 318 319 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp) 320 { 321 MachineState *ms = MACHINE(obj); 322 323 g_free(ms->dumpdtb); 324 ms->dumpdtb = g_strdup(value); 325 } 326 327 static void machine_get_phandle_start(Object *obj, Visitor *v, 328 const char *name, void *opaque, 329 Error **errp) 330 { 331 MachineState *ms = MACHINE(obj); 332 int64_t value = ms->phandle_start; 333 334 visit_type_int(v, name, &value, errp); 335 } 336 337 static void machine_set_phandle_start(Object *obj, Visitor *v, 338 const char *name, void *opaque, 339 Error **errp) 340 { 341 MachineState *ms = MACHINE(obj); 342 int64_t value; 343 344 if (!visit_type_int(v, name, &value, errp)) { 345 return; 346 } 347 348 ms->phandle_start = value; 349 } 350 351 static char *machine_get_dt_compatible(Object *obj, Error **errp) 352 { 353 MachineState *ms = MACHINE(obj); 354 355 return g_strdup(ms->dt_compatible); 356 } 357 358 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp) 359 { 360 MachineState *ms = MACHINE(obj); 361 362 g_free(ms->dt_compatible); 363 ms->dt_compatible = g_strdup(value); 364 } 365 366 static bool machine_get_dump_guest_core(Object *obj, Error **errp) 367 { 368 MachineState *ms = MACHINE(obj); 369 370 return ms->dump_guest_core; 371 } 372 373 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp) 374 { 375 MachineState *ms = MACHINE(obj); 376 377 ms->dump_guest_core = value; 378 } 379 380 static bool machine_get_mem_merge(Object *obj, Error **errp) 381 { 382 MachineState *ms = MACHINE(obj); 383 384 return ms->mem_merge; 385 } 386 387 static void machine_set_mem_merge(Object *obj, bool value, Error **errp) 388 { 389 MachineState *ms = MACHINE(obj); 390 391 ms->mem_merge = value; 392 } 393 394 static bool machine_get_usb(Object *obj, Error **errp) 395 { 396 MachineState *ms = MACHINE(obj); 397 398 return ms->usb; 399 } 400 401 static void machine_set_usb(Object *obj, bool value, Error **errp) 402 { 403 MachineState *ms = MACHINE(obj); 404 405 ms->usb = value; 406 ms->usb_disabled = !value; 407 } 408 409 static bool machine_get_graphics(Object *obj, Error **errp) 410 { 411 MachineState *ms = MACHINE(obj); 412 413 return ms->enable_graphics; 414 } 415 416 static void machine_set_graphics(Object *obj, bool value, Error **errp) 417 { 418 MachineState *ms = MACHINE(obj); 419 420 ms->enable_graphics = value; 421 } 422 423 static char *machine_get_firmware(Object *obj, Error **errp) 424 { 425 MachineState *ms = MACHINE(obj); 426 427 return g_strdup(ms->firmware); 428 } 429 430 static void machine_set_firmware(Object *obj, const char *value, Error **errp) 431 { 432 MachineState *ms = MACHINE(obj); 433 434 g_free(ms->firmware); 435 ms->firmware = g_strdup(value); 436 } 437 438 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp) 439 { 440 MachineState *ms = MACHINE(obj); 441 442 ms->suppress_vmdesc = value; 443 } 444 445 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp) 446 { 447 MachineState *ms = MACHINE(obj); 448 449 return ms->suppress_vmdesc; 450 } 451 452 static char *machine_get_memory_encryption(Object *obj, Error **errp) 453 { 454 MachineState *ms = MACHINE(obj); 455 456 if (ms->cgs) { 457 return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs))); 458 } 459 460 return NULL; 461 } 462 463 static void machine_set_memory_encryption(Object *obj, const char *value, 464 Error **errp) 465 { 466 Object *cgs = 467 object_resolve_path_component(object_get_objects_root(), value); 468 469 if (!cgs) { 470 error_setg(errp, "No such memory encryption object '%s'", value); 471 return; 472 } 473 474 object_property_set_link(obj, "confidential-guest-support", cgs, errp); 475 } 476 477 static void machine_check_confidential_guest_support(const Object *obj, 478 const char *name, 479 Object *new_target, 480 Error **errp) 481 { 482 /* 483 * So far the only constraint is that the target has the 484 * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked 485 * by the QOM core 486 */ 487 } 488 489 static bool machine_get_nvdimm(Object *obj, Error **errp) 490 { 491 MachineState *ms = MACHINE(obj); 492 493 return ms->nvdimms_state->is_enabled; 494 } 495 496 static void machine_set_nvdimm(Object *obj, bool value, Error **errp) 497 { 498 MachineState *ms = MACHINE(obj); 499 500 ms->nvdimms_state->is_enabled = value; 501 } 502 503 static bool machine_get_hmat(Object *obj, Error **errp) 504 { 505 MachineState *ms = MACHINE(obj); 506 507 return ms->numa_state->hmat_enabled; 508 } 509 510 static void machine_set_hmat(Object *obj, bool value, Error **errp) 511 { 512 MachineState *ms = MACHINE(obj); 513 514 ms->numa_state->hmat_enabled = value; 515 } 516 517 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp) 518 { 519 MachineState *ms = MACHINE(obj); 520 521 return g_strdup(ms->nvdimms_state->persistence_string); 522 } 523 524 static void machine_set_nvdimm_persistence(Object *obj, const char *value, 525 Error **errp) 526 { 527 MachineState *ms = MACHINE(obj); 528 NVDIMMState *nvdimms_state = ms->nvdimms_state; 529 530 if (strcmp(value, "cpu") == 0) { 531 nvdimms_state->persistence = 3; 532 } else if (strcmp(value, "mem-ctrl") == 0) { 533 nvdimms_state->persistence = 2; 534 } else { 535 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option", 536 value); 537 return; 538 } 539 540 g_free(nvdimms_state->persistence_string); 541 nvdimms_state->persistence_string = g_strdup(value); 542 } 543 544 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type) 545 { 546 QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type)); 547 } 548 549 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev) 550 { 551 Object *obj = OBJECT(dev); 552 553 if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) { 554 return false; 555 } 556 557 return device_type_is_dynamic_sysbus(mc, object_get_typename(obj)); 558 } 559 560 bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type) 561 { 562 bool allowed = false; 563 strList *wl; 564 ObjectClass *klass = object_class_by_name(type); 565 566 for (wl = mc->allowed_dynamic_sysbus_devices; 567 !allowed && wl; 568 wl = wl->next) { 569 allowed |= !!object_class_dynamic_cast(klass, wl->value); 570 } 571 572 return allowed; 573 } 574 575 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque) 576 { 577 MachineState *machine = opaque; 578 MachineClass *mc = MACHINE_GET_CLASS(machine); 579 580 if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) { 581 error_report("Option '-device %s' cannot be handled by this machine", 582 object_class_get_name(object_get_class(OBJECT(sbdev)))); 583 exit(1); 584 } 585 } 586 587 static char *machine_get_memdev(Object *obj, Error **errp) 588 { 589 MachineState *ms = MACHINE(obj); 590 591 return g_strdup(ms->ram_memdev_id); 592 } 593 594 static void machine_set_memdev(Object *obj, const char *value, Error **errp) 595 { 596 MachineState *ms = MACHINE(obj); 597 598 g_free(ms->ram_memdev_id); 599 ms->ram_memdev_id = g_strdup(value); 600 } 601 602 static void machine_init_notify(Notifier *notifier, void *data) 603 { 604 MachineState *machine = MACHINE(qdev_get_machine()); 605 606 /* 607 * Loop through all dynamically created sysbus devices and check if they are 608 * all allowed. If a device is not allowed, error out. 609 */ 610 foreach_dynamic_sysbus_device(validate_sysbus_device, machine); 611 } 612 613 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine) 614 { 615 int i; 616 HotpluggableCPUList *head = NULL; 617 MachineClass *mc = MACHINE_GET_CLASS(machine); 618 619 /* force board to initialize possible_cpus if it hasn't been done yet */ 620 mc->possible_cpu_arch_ids(machine); 621 622 for (i = 0; i < machine->possible_cpus->len; i++) { 623 Object *cpu; 624 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1); 625 626 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type); 627 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count; 628 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props, 629 sizeof(*cpu_item->props)); 630 631 cpu = machine->possible_cpus->cpus[i].cpu; 632 if (cpu) { 633 cpu_item->has_qom_path = true; 634 cpu_item->qom_path = object_get_canonical_path(cpu); 635 } 636 QAPI_LIST_PREPEND(head, cpu_item); 637 } 638 return head; 639 } 640 641 /** 642 * machine_set_cpu_numa_node: 643 * @machine: machine object to modify 644 * @props: specifies which cpu objects to assign to 645 * numa node specified by @props.node_id 646 * @errp: if an error occurs, a pointer to an area to store the error 647 * 648 * Associate NUMA node specified by @props.node_id with cpu slots that 649 * match socket/core/thread-ids specified by @props. It's recommended to use 650 * query-hotpluggable-cpus.props values to specify affected cpu slots, 651 * which would lead to exact 1:1 mapping of cpu slots to NUMA node. 652 * 653 * However for CLI convenience it's possible to pass in subset of properties, 654 * which would affect all cpu slots that match it. 655 * Ex for pc machine: 656 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \ 657 * -numa cpu,node-id=0,socket_id=0 \ 658 * -numa cpu,node-id=1,socket_id=1 659 * will assign all child cores of socket 0 to node 0 and 660 * of socket 1 to node 1. 661 * 662 * On attempt of reassigning (already assigned) cpu slot to another NUMA node, 663 * return error. 664 * Empty subset is disallowed and function will return with error in this case. 665 */ 666 void machine_set_cpu_numa_node(MachineState *machine, 667 const CpuInstanceProperties *props, Error **errp) 668 { 669 MachineClass *mc = MACHINE_GET_CLASS(machine); 670 NodeInfo *numa_info = machine->numa_state->nodes; 671 bool match = false; 672 int i; 673 674 if (!mc->possible_cpu_arch_ids) { 675 error_setg(errp, "mapping of CPUs to NUMA node is not supported"); 676 return; 677 } 678 679 /* disabling node mapping is not supported, forbid it */ 680 assert(props->has_node_id); 681 682 /* force board to initialize possible_cpus if it hasn't been done yet */ 683 mc->possible_cpu_arch_ids(machine); 684 685 for (i = 0; i < machine->possible_cpus->len; i++) { 686 CPUArchId *slot = &machine->possible_cpus->cpus[i]; 687 688 /* reject unsupported by board properties */ 689 if (props->has_thread_id && !slot->props.has_thread_id) { 690 error_setg(errp, "thread-id is not supported"); 691 return; 692 } 693 694 if (props->has_core_id && !slot->props.has_core_id) { 695 error_setg(errp, "core-id is not supported"); 696 return; 697 } 698 699 if (props->has_socket_id && !slot->props.has_socket_id) { 700 error_setg(errp, "socket-id is not supported"); 701 return; 702 } 703 704 if (props->has_die_id && !slot->props.has_die_id) { 705 error_setg(errp, "die-id is not supported"); 706 return; 707 } 708 709 /* skip slots with explicit mismatch */ 710 if (props->has_thread_id && props->thread_id != slot->props.thread_id) { 711 continue; 712 } 713 714 if (props->has_core_id && props->core_id != slot->props.core_id) { 715 continue; 716 } 717 718 if (props->has_die_id && props->die_id != slot->props.die_id) { 719 continue; 720 } 721 722 if (props->has_socket_id && props->socket_id != slot->props.socket_id) { 723 continue; 724 } 725 726 /* reject assignment if slot is already assigned, for compatibility 727 * of legacy cpu_index mapping with SPAPR core based mapping do not 728 * error out if cpu thread and matched core have the same node-id */ 729 if (slot->props.has_node_id && 730 slot->props.node_id != props->node_id) { 731 error_setg(errp, "CPU is already assigned to node-id: %" PRId64, 732 slot->props.node_id); 733 return; 734 } 735 736 /* assign slot to node as it's matched '-numa cpu' key */ 737 match = true; 738 slot->props.node_id = props->node_id; 739 slot->props.has_node_id = props->has_node_id; 740 741 if (machine->numa_state->hmat_enabled) { 742 if ((numa_info[props->node_id].initiator < MAX_NODES) && 743 (props->node_id != numa_info[props->node_id].initiator)) { 744 error_setg(errp, "The initiator of CPU NUMA node %" PRId64 745 " should be itself (got %" PRIu16 ")", 746 props->node_id, numa_info[props->node_id].initiator); 747 return; 748 } 749 numa_info[props->node_id].has_cpu = true; 750 numa_info[props->node_id].initiator = props->node_id; 751 } 752 } 753 754 if (!match) { 755 error_setg(errp, "no match found"); 756 } 757 } 758 759 static void machine_get_smp(Object *obj, Visitor *v, const char *name, 760 void *opaque, Error **errp) 761 { 762 MachineState *ms = MACHINE(obj); 763 SMPConfiguration *config = &(SMPConfiguration){ 764 .has_cpus = true, .cpus = ms->smp.cpus, 765 .has_sockets = true, .sockets = ms->smp.sockets, 766 .has_dies = true, .dies = ms->smp.dies, 767 .has_cores = true, .cores = ms->smp.cores, 768 .has_threads = true, .threads = ms->smp.threads, 769 .has_maxcpus = true, .maxcpus = ms->smp.max_cpus, 770 }; 771 if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) { 772 return; 773 } 774 } 775 776 static void machine_set_smp(Object *obj, Visitor *v, const char *name, 777 void *opaque, Error **errp) 778 { 779 MachineState *ms = MACHINE(obj); 780 g_autoptr(SMPConfiguration) config = NULL; 781 782 if (!visit_type_SMPConfiguration(v, name, &config, errp)) { 783 return; 784 } 785 786 smp_parse(ms, config, errp); 787 } 788 789 static void machine_class_init(ObjectClass *oc, void *data) 790 { 791 MachineClass *mc = MACHINE_CLASS(oc); 792 793 /* Default 128 MB as guest ram size */ 794 mc->default_ram_size = 128 * MiB; 795 mc->rom_file_has_mr = true; 796 797 /* numa node memory size aligned on 8MB by default. 798 * On Linux, each node's border has to be 8MB aligned 799 */ 800 mc->numa_mem_align_shift = 23; 801 802 object_class_property_add_str(oc, "kernel", 803 machine_get_kernel, machine_set_kernel); 804 object_class_property_set_description(oc, "kernel", 805 "Linux kernel image file"); 806 807 object_class_property_add_str(oc, "initrd", 808 machine_get_initrd, machine_set_initrd); 809 object_class_property_set_description(oc, "initrd", 810 "Linux initial ramdisk file"); 811 812 object_class_property_add_str(oc, "append", 813 machine_get_append, machine_set_append); 814 object_class_property_set_description(oc, "append", 815 "Linux kernel command line"); 816 817 object_class_property_add_str(oc, "dtb", 818 machine_get_dtb, machine_set_dtb); 819 object_class_property_set_description(oc, "dtb", 820 "Linux kernel device tree file"); 821 822 object_class_property_add_str(oc, "dumpdtb", 823 machine_get_dumpdtb, machine_set_dumpdtb); 824 object_class_property_set_description(oc, "dumpdtb", 825 "Dump current dtb to a file and quit"); 826 827 object_class_property_add(oc, "smp", "SMPConfiguration", 828 machine_get_smp, machine_set_smp, 829 NULL, NULL); 830 object_class_property_set_description(oc, "smp", 831 "CPU topology"); 832 833 object_class_property_add(oc, "phandle-start", "int", 834 machine_get_phandle_start, machine_set_phandle_start, 835 NULL, NULL); 836 object_class_property_set_description(oc, "phandle-start", 837 "The first phandle ID we may generate dynamically"); 838 839 object_class_property_add_str(oc, "dt-compatible", 840 machine_get_dt_compatible, machine_set_dt_compatible); 841 object_class_property_set_description(oc, "dt-compatible", 842 "Overrides the \"compatible\" property of the dt root node"); 843 844 object_class_property_add_bool(oc, "dump-guest-core", 845 machine_get_dump_guest_core, machine_set_dump_guest_core); 846 object_class_property_set_description(oc, "dump-guest-core", 847 "Include guest memory in a core dump"); 848 849 object_class_property_add_bool(oc, "mem-merge", 850 machine_get_mem_merge, machine_set_mem_merge); 851 object_class_property_set_description(oc, "mem-merge", 852 "Enable/disable memory merge support"); 853 854 object_class_property_add_bool(oc, "usb", 855 machine_get_usb, machine_set_usb); 856 object_class_property_set_description(oc, "usb", 857 "Set on/off to enable/disable usb"); 858 859 object_class_property_add_bool(oc, "graphics", 860 machine_get_graphics, machine_set_graphics); 861 object_class_property_set_description(oc, "graphics", 862 "Set on/off to enable/disable graphics emulation"); 863 864 object_class_property_add_str(oc, "firmware", 865 machine_get_firmware, machine_set_firmware); 866 object_class_property_set_description(oc, "firmware", 867 "Firmware image"); 868 869 object_class_property_add_bool(oc, "suppress-vmdesc", 870 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc); 871 object_class_property_set_description(oc, "suppress-vmdesc", 872 "Set on to disable self-describing migration"); 873 874 object_class_property_add_link(oc, "confidential-guest-support", 875 TYPE_CONFIDENTIAL_GUEST_SUPPORT, 876 offsetof(MachineState, cgs), 877 machine_check_confidential_guest_support, 878 OBJ_PROP_LINK_STRONG); 879 object_class_property_set_description(oc, "confidential-guest-support", 880 "Set confidential guest scheme to support"); 881 882 /* For compatibility */ 883 object_class_property_add_str(oc, "memory-encryption", 884 machine_get_memory_encryption, machine_set_memory_encryption); 885 object_class_property_set_description(oc, "memory-encryption", 886 "Set memory encryption object to use"); 887 888 object_class_property_add_str(oc, "memory-backend", 889 machine_get_memdev, machine_set_memdev); 890 object_class_property_set_description(oc, "memory-backend", 891 "Set RAM backend" 892 "Valid value is ID of hostmem based backend"); 893 } 894 895 static void machine_class_base_init(ObjectClass *oc, void *data) 896 { 897 MachineClass *mc = MACHINE_CLASS(oc); 898 mc->max_cpus = mc->max_cpus ?: 1; 899 mc->min_cpus = mc->min_cpus ?: 1; 900 mc->default_cpus = mc->default_cpus ?: 1; 901 902 if (!object_class_is_abstract(oc)) { 903 const char *cname = object_class_get_name(oc); 904 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX)); 905 mc->name = g_strndup(cname, 906 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX)); 907 mc->compat_props = g_ptr_array_new(); 908 } 909 } 910 911 static void machine_initfn(Object *obj) 912 { 913 MachineState *ms = MACHINE(obj); 914 MachineClass *mc = MACHINE_GET_CLASS(obj); 915 916 container_get(obj, "/peripheral"); 917 container_get(obj, "/peripheral-anon"); 918 919 ms->dump_guest_core = true; 920 ms->mem_merge = true; 921 ms->enable_graphics = true; 922 ms->kernel_cmdline = g_strdup(""); 923 924 if (mc->nvdimm_supported) { 925 Object *obj = OBJECT(ms); 926 927 ms->nvdimms_state = g_new0(NVDIMMState, 1); 928 object_property_add_bool(obj, "nvdimm", 929 machine_get_nvdimm, machine_set_nvdimm); 930 object_property_set_description(obj, "nvdimm", 931 "Set on/off to enable/disable " 932 "NVDIMM instantiation"); 933 934 object_property_add_str(obj, "nvdimm-persistence", 935 machine_get_nvdimm_persistence, 936 machine_set_nvdimm_persistence); 937 object_property_set_description(obj, "nvdimm-persistence", 938 "Set NVDIMM persistence" 939 "Valid values are cpu, mem-ctrl"); 940 } 941 942 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) { 943 ms->numa_state = g_new0(NumaState, 1); 944 object_property_add_bool(obj, "hmat", 945 machine_get_hmat, machine_set_hmat); 946 object_property_set_description(obj, "hmat", 947 "Set on/off to enable/disable " 948 "ACPI Heterogeneous Memory Attribute " 949 "Table (HMAT)"); 950 } 951 952 /* Register notifier when init is done for sysbus sanity checks */ 953 ms->sysbus_notifier.notify = machine_init_notify; 954 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier); 955 956 /* default to mc->default_cpus */ 957 ms->smp.cpus = mc->default_cpus; 958 ms->smp.max_cpus = mc->default_cpus; 959 ms->smp.sockets = 1; 960 ms->smp.dies = 1; 961 ms->smp.cores = 1; 962 ms->smp.threads = 1; 963 } 964 965 static void machine_finalize(Object *obj) 966 { 967 MachineState *ms = MACHINE(obj); 968 969 g_free(ms->kernel_filename); 970 g_free(ms->initrd_filename); 971 g_free(ms->kernel_cmdline); 972 g_free(ms->dtb); 973 g_free(ms->dumpdtb); 974 g_free(ms->dt_compatible); 975 g_free(ms->firmware); 976 g_free(ms->device_memory); 977 g_free(ms->nvdimms_state); 978 g_free(ms->numa_state); 979 } 980 981 bool machine_usb(MachineState *machine) 982 { 983 return machine->usb; 984 } 985 986 int machine_phandle_start(MachineState *machine) 987 { 988 return machine->phandle_start; 989 } 990 991 bool machine_dump_guest_core(MachineState *machine) 992 { 993 return machine->dump_guest_core; 994 } 995 996 bool machine_mem_merge(MachineState *machine) 997 { 998 return machine->mem_merge; 999 } 1000 1001 static char *cpu_slot_to_string(const CPUArchId *cpu) 1002 { 1003 GString *s = g_string_new(NULL); 1004 if (cpu->props.has_socket_id) { 1005 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id); 1006 } 1007 if (cpu->props.has_die_id) { 1008 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id); 1009 } 1010 if (cpu->props.has_core_id) { 1011 if (s->len) { 1012 g_string_append_printf(s, ", "); 1013 } 1014 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id); 1015 } 1016 if (cpu->props.has_thread_id) { 1017 if (s->len) { 1018 g_string_append_printf(s, ", "); 1019 } 1020 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id); 1021 } 1022 return g_string_free(s, false); 1023 } 1024 1025 static void numa_validate_initiator(NumaState *numa_state) 1026 { 1027 int i; 1028 NodeInfo *numa_info = numa_state->nodes; 1029 1030 for (i = 0; i < numa_state->num_nodes; i++) { 1031 if (numa_info[i].initiator == MAX_NODES) { 1032 error_report("The initiator of NUMA node %d is missing, use " 1033 "'-numa node,initiator' option to declare it", i); 1034 exit(1); 1035 } 1036 1037 if (!numa_info[numa_info[i].initiator].present) { 1038 error_report("NUMA node %" PRIu16 " is missing, use " 1039 "'-numa node' option to declare it first", 1040 numa_info[i].initiator); 1041 exit(1); 1042 } 1043 1044 if (!numa_info[numa_info[i].initiator].has_cpu) { 1045 error_report("The initiator of NUMA node %d is invalid", i); 1046 exit(1); 1047 } 1048 } 1049 } 1050 1051 static void machine_numa_finish_cpu_init(MachineState *machine) 1052 { 1053 int i; 1054 bool default_mapping; 1055 GString *s = g_string_new(NULL); 1056 MachineClass *mc = MACHINE_GET_CLASS(machine); 1057 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine); 1058 1059 assert(machine->numa_state->num_nodes); 1060 for (i = 0; i < possible_cpus->len; i++) { 1061 if (possible_cpus->cpus[i].props.has_node_id) { 1062 break; 1063 } 1064 } 1065 default_mapping = (i == possible_cpus->len); 1066 1067 for (i = 0; i < possible_cpus->len; i++) { 1068 const CPUArchId *cpu_slot = &possible_cpus->cpus[i]; 1069 1070 if (!cpu_slot->props.has_node_id) { 1071 /* fetch default mapping from board and enable it */ 1072 CpuInstanceProperties props = cpu_slot->props; 1073 1074 props.node_id = mc->get_default_cpu_node_id(machine, i); 1075 if (!default_mapping) { 1076 /* record slots with not set mapping, 1077 * TODO: make it hard error in future */ 1078 char *cpu_str = cpu_slot_to_string(cpu_slot); 1079 g_string_append_printf(s, "%sCPU %d [%s]", 1080 s->len ? ", " : "", i, cpu_str); 1081 g_free(cpu_str); 1082 1083 /* non mapped cpus used to fallback to node 0 */ 1084 props.node_id = 0; 1085 } 1086 1087 props.has_node_id = true; 1088 machine_set_cpu_numa_node(machine, &props, &error_fatal); 1089 } 1090 } 1091 1092 if (machine->numa_state->hmat_enabled) { 1093 numa_validate_initiator(machine->numa_state); 1094 } 1095 1096 if (s->len && !qtest_enabled()) { 1097 warn_report("CPU(s) not present in any NUMA nodes: %s", 1098 s->str); 1099 warn_report("All CPU(s) up to maxcpus should be described " 1100 "in NUMA config, ability to start up with partial NUMA " 1101 "mappings is obsoleted and will be removed in future"); 1102 } 1103 g_string_free(s, true); 1104 } 1105 1106 MemoryRegion *machine_consume_memdev(MachineState *machine, 1107 HostMemoryBackend *backend) 1108 { 1109 MemoryRegion *ret = host_memory_backend_get_memory(backend); 1110 1111 if (memory_region_is_mapped(ret)) { 1112 error_report("memory backend %s can't be used multiple times.", 1113 object_get_canonical_path_component(OBJECT(backend))); 1114 exit(EXIT_FAILURE); 1115 } 1116 host_memory_backend_set_mapped(backend, true); 1117 vmstate_register_ram_global(ret); 1118 return ret; 1119 } 1120 1121 void machine_run_board_init(MachineState *machine) 1122 { 1123 MachineClass *machine_class = MACHINE_GET_CLASS(machine); 1124 ObjectClass *oc = object_class_by_name(machine->cpu_type); 1125 CPUClass *cc; 1126 1127 /* This checkpoint is required by replay to separate prior clock 1128 reading from the other reads, because timer polling functions query 1129 clock values from the log. */ 1130 replay_checkpoint(CHECKPOINT_INIT); 1131 1132 if (machine->ram_memdev_id) { 1133 Object *o; 1134 o = object_resolve_path_type(machine->ram_memdev_id, 1135 TYPE_MEMORY_BACKEND, NULL); 1136 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o)); 1137 } 1138 1139 if (machine->numa_state) { 1140 numa_complete_configuration(machine); 1141 if (machine->numa_state->num_nodes) { 1142 machine_numa_finish_cpu_init(machine); 1143 } 1144 } 1145 1146 /* If the machine supports the valid_cpu_types check and the user 1147 * specified a CPU with -cpu check here that the user CPU is supported. 1148 */ 1149 if (machine_class->valid_cpu_types && machine->cpu_type) { 1150 int i; 1151 1152 for (i = 0; machine_class->valid_cpu_types[i]; i++) { 1153 if (object_class_dynamic_cast(oc, 1154 machine_class->valid_cpu_types[i])) { 1155 /* The user specificed CPU is in the valid field, we are 1156 * good to go. 1157 */ 1158 break; 1159 } 1160 } 1161 1162 if (!machine_class->valid_cpu_types[i]) { 1163 /* The user specified CPU is not valid */ 1164 error_report("Invalid CPU type: %s", machine->cpu_type); 1165 error_printf("The valid types are: %s", 1166 machine_class->valid_cpu_types[0]); 1167 for (i = 1; machine_class->valid_cpu_types[i]; i++) { 1168 error_printf(", %s", machine_class->valid_cpu_types[i]); 1169 } 1170 error_printf("\n"); 1171 1172 exit(1); 1173 } 1174 } 1175 1176 /* Check if CPU type is deprecated and warn if so */ 1177 cc = CPU_CLASS(oc); 1178 if (cc && cc->deprecation_note) { 1179 warn_report("CPU model %s is deprecated -- %s", machine->cpu_type, 1180 cc->deprecation_note); 1181 } 1182 1183 if (machine->cgs) { 1184 /* 1185 * With confidential guests, the host can't see the real 1186 * contents of RAM, so there's no point in it trying to merge 1187 * areas. 1188 */ 1189 machine_set_mem_merge(OBJECT(machine), false, &error_abort); 1190 1191 /* 1192 * Virtio devices can't count on directly accessing guest 1193 * memory, so they need iommu_platform=on to use normal DMA 1194 * mechanisms. That requires also disabling legacy virtio 1195 * support for those virtio pci devices which allow it. 1196 */ 1197 object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy", 1198 "on", true); 1199 object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform", 1200 "on", false); 1201 } 1202 1203 accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator)); 1204 machine_class->init(machine); 1205 phase_advance(PHASE_MACHINE_INITIALIZED); 1206 } 1207 1208 static NotifierList machine_init_done_notifiers = 1209 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers); 1210 1211 void qemu_add_machine_init_done_notifier(Notifier *notify) 1212 { 1213 notifier_list_add(&machine_init_done_notifiers, notify); 1214 if (phase_check(PHASE_MACHINE_READY)) { 1215 notify->notify(notify, NULL); 1216 } 1217 } 1218 1219 void qemu_remove_machine_init_done_notifier(Notifier *notify) 1220 { 1221 notifier_remove(notify); 1222 } 1223 1224 void qdev_machine_creation_done(void) 1225 { 1226 cpu_synchronize_all_post_init(); 1227 1228 if (current_machine->boot_once) { 1229 qemu_boot_set(current_machine->boot_once, &error_fatal); 1230 qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_order)); 1231 } 1232 1233 /* 1234 * ok, initial machine setup is done, starting from now we can 1235 * only create hotpluggable devices 1236 */ 1237 phase_advance(PHASE_MACHINE_READY); 1238 qdev_assert_realized_properly(); 1239 1240 /* TODO: once all bus devices are qdevified, this should be done 1241 * when bus is created by qdev.c */ 1242 /* 1243 * TODO: If we had a main 'reset container' that the whole system 1244 * lived in, we could reset that using the multi-phase reset 1245 * APIs. For the moment, we just reset the sysbus, which will cause 1246 * all devices hanging off it (and all their child buses, recursively) 1247 * to be reset. Note that this will *not* reset any Device objects 1248 * which are not attached to some part of the qbus tree! 1249 */ 1250 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default()); 1251 1252 notifier_list_notify(&machine_init_done_notifiers, NULL); 1253 1254 if (rom_check_and_register_reset() != 0) { 1255 exit(1); 1256 } 1257 1258 replay_start(); 1259 1260 /* This checkpoint is required by replay to separate prior clock 1261 reading from the other reads, because timer polling functions query 1262 clock values from the log. */ 1263 replay_checkpoint(CHECKPOINT_RESET); 1264 qemu_system_reset(SHUTDOWN_CAUSE_NONE); 1265 register_global_state(); 1266 } 1267 1268 static const TypeInfo machine_info = { 1269 .name = TYPE_MACHINE, 1270 .parent = TYPE_OBJECT, 1271 .abstract = true, 1272 .class_size = sizeof(MachineClass), 1273 .class_init = machine_class_init, 1274 .class_base_init = machine_class_base_init, 1275 .instance_size = sizeof(MachineState), 1276 .instance_init = machine_initfn, 1277 .instance_finalize = machine_finalize, 1278 }; 1279 1280 static void machine_register_types(void) 1281 { 1282 type_register_static(&machine_info); 1283 } 1284 1285 type_init(machine_register_types) 1286