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 bool allowed = false; 552 strList *wl; 553 Object *obj = OBJECT(dev); 554 555 if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) { 556 return false; 557 } 558 559 for (wl = mc->allowed_dynamic_sysbus_devices; 560 !allowed && wl; 561 wl = wl->next) { 562 allowed |= !!object_dynamic_cast(obj, wl->value); 563 } 564 565 return allowed; 566 } 567 568 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque) 569 { 570 MachineState *machine = opaque; 571 MachineClass *mc = MACHINE_GET_CLASS(machine); 572 573 if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) { 574 error_report("Option '-device %s' cannot be handled by this machine", 575 object_class_get_name(object_get_class(OBJECT(sbdev)))); 576 exit(1); 577 } 578 } 579 580 static char *machine_get_memdev(Object *obj, Error **errp) 581 { 582 MachineState *ms = MACHINE(obj); 583 584 return g_strdup(ms->ram_memdev_id); 585 } 586 587 static void machine_set_memdev(Object *obj, const char *value, Error **errp) 588 { 589 MachineState *ms = MACHINE(obj); 590 591 g_free(ms->ram_memdev_id); 592 ms->ram_memdev_id = g_strdup(value); 593 } 594 595 static void machine_init_notify(Notifier *notifier, void *data) 596 { 597 MachineState *machine = MACHINE(qdev_get_machine()); 598 599 /* 600 * Loop through all dynamically created sysbus devices and check if they are 601 * all allowed. If a device is not allowed, error out. 602 */ 603 foreach_dynamic_sysbus_device(validate_sysbus_device, machine); 604 } 605 606 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine) 607 { 608 int i; 609 HotpluggableCPUList *head = NULL; 610 MachineClass *mc = MACHINE_GET_CLASS(machine); 611 612 /* force board to initialize possible_cpus if it hasn't been done yet */ 613 mc->possible_cpu_arch_ids(machine); 614 615 for (i = 0; i < machine->possible_cpus->len; i++) { 616 Object *cpu; 617 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1); 618 619 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type); 620 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count; 621 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props, 622 sizeof(*cpu_item->props)); 623 624 cpu = machine->possible_cpus->cpus[i].cpu; 625 if (cpu) { 626 cpu_item->has_qom_path = true; 627 cpu_item->qom_path = object_get_canonical_path(cpu); 628 } 629 QAPI_LIST_PREPEND(head, cpu_item); 630 } 631 return head; 632 } 633 634 /** 635 * machine_set_cpu_numa_node: 636 * @machine: machine object to modify 637 * @props: specifies which cpu objects to assign to 638 * numa node specified by @props.node_id 639 * @errp: if an error occurs, a pointer to an area to store the error 640 * 641 * Associate NUMA node specified by @props.node_id with cpu slots that 642 * match socket/core/thread-ids specified by @props. It's recommended to use 643 * query-hotpluggable-cpus.props values to specify affected cpu slots, 644 * which would lead to exact 1:1 mapping of cpu slots to NUMA node. 645 * 646 * However for CLI convenience it's possible to pass in subset of properties, 647 * which would affect all cpu slots that match it. 648 * Ex for pc machine: 649 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \ 650 * -numa cpu,node-id=0,socket_id=0 \ 651 * -numa cpu,node-id=1,socket_id=1 652 * will assign all child cores of socket 0 to node 0 and 653 * of socket 1 to node 1. 654 * 655 * On attempt of reassigning (already assigned) cpu slot to another NUMA node, 656 * return error. 657 * Empty subset is disallowed and function will return with error in this case. 658 */ 659 void machine_set_cpu_numa_node(MachineState *machine, 660 const CpuInstanceProperties *props, Error **errp) 661 { 662 MachineClass *mc = MACHINE_GET_CLASS(machine); 663 NodeInfo *numa_info = machine->numa_state->nodes; 664 bool match = false; 665 int i; 666 667 if (!mc->possible_cpu_arch_ids) { 668 error_setg(errp, "mapping of CPUs to NUMA node is not supported"); 669 return; 670 } 671 672 /* disabling node mapping is not supported, forbid it */ 673 assert(props->has_node_id); 674 675 /* force board to initialize possible_cpus if it hasn't been done yet */ 676 mc->possible_cpu_arch_ids(machine); 677 678 for (i = 0; i < machine->possible_cpus->len; i++) { 679 CPUArchId *slot = &machine->possible_cpus->cpus[i]; 680 681 /* reject unsupported by board properties */ 682 if (props->has_thread_id && !slot->props.has_thread_id) { 683 error_setg(errp, "thread-id is not supported"); 684 return; 685 } 686 687 if (props->has_core_id && !slot->props.has_core_id) { 688 error_setg(errp, "core-id is not supported"); 689 return; 690 } 691 692 if (props->has_socket_id && !slot->props.has_socket_id) { 693 error_setg(errp, "socket-id is not supported"); 694 return; 695 } 696 697 if (props->has_die_id && !slot->props.has_die_id) { 698 error_setg(errp, "die-id is not supported"); 699 return; 700 } 701 702 /* skip slots with explicit mismatch */ 703 if (props->has_thread_id && props->thread_id != slot->props.thread_id) { 704 continue; 705 } 706 707 if (props->has_core_id && props->core_id != slot->props.core_id) { 708 continue; 709 } 710 711 if (props->has_die_id && props->die_id != slot->props.die_id) { 712 continue; 713 } 714 715 if (props->has_socket_id && props->socket_id != slot->props.socket_id) { 716 continue; 717 } 718 719 /* reject assignment if slot is already assigned, for compatibility 720 * of legacy cpu_index mapping with SPAPR core based mapping do not 721 * error out if cpu thread and matched core have the same node-id */ 722 if (slot->props.has_node_id && 723 slot->props.node_id != props->node_id) { 724 error_setg(errp, "CPU is already assigned to node-id: %" PRId64, 725 slot->props.node_id); 726 return; 727 } 728 729 /* assign slot to node as it's matched '-numa cpu' key */ 730 match = true; 731 slot->props.node_id = props->node_id; 732 slot->props.has_node_id = props->has_node_id; 733 734 if (machine->numa_state->hmat_enabled) { 735 if ((numa_info[props->node_id].initiator < MAX_NODES) && 736 (props->node_id != numa_info[props->node_id].initiator)) { 737 error_setg(errp, "The initiator of CPU NUMA node %" PRId64 738 " should be itself (got %" PRIu16 ")", 739 props->node_id, numa_info[props->node_id].initiator); 740 return; 741 } 742 numa_info[props->node_id].has_cpu = true; 743 numa_info[props->node_id].initiator = props->node_id; 744 } 745 } 746 747 if (!match) { 748 error_setg(errp, "no match found"); 749 } 750 } 751 752 static void machine_get_smp(Object *obj, Visitor *v, const char *name, 753 void *opaque, Error **errp) 754 { 755 MachineState *ms = MACHINE(obj); 756 SMPConfiguration *config = &(SMPConfiguration){ 757 .has_cpus = true, .cpus = ms->smp.cpus, 758 .has_sockets = true, .sockets = ms->smp.sockets, 759 .has_dies = true, .dies = ms->smp.dies, 760 .has_cores = true, .cores = ms->smp.cores, 761 .has_threads = true, .threads = ms->smp.threads, 762 .has_maxcpus = true, .maxcpus = ms->smp.max_cpus, 763 }; 764 if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) { 765 return; 766 } 767 } 768 769 static void machine_set_smp(Object *obj, Visitor *v, const char *name, 770 void *opaque, Error **errp) 771 { 772 MachineState *ms = MACHINE(obj); 773 g_autoptr(SMPConfiguration) config = NULL; 774 775 if (!visit_type_SMPConfiguration(v, name, &config, errp)) { 776 return; 777 } 778 779 smp_parse(ms, config, errp); 780 } 781 782 static void machine_class_init(ObjectClass *oc, void *data) 783 { 784 MachineClass *mc = MACHINE_CLASS(oc); 785 786 /* Default 128 MB as guest ram size */ 787 mc->default_ram_size = 128 * MiB; 788 mc->rom_file_has_mr = true; 789 790 /* numa node memory size aligned on 8MB by default. 791 * On Linux, each node's border has to be 8MB aligned 792 */ 793 mc->numa_mem_align_shift = 23; 794 795 object_class_property_add_str(oc, "kernel", 796 machine_get_kernel, machine_set_kernel); 797 object_class_property_set_description(oc, "kernel", 798 "Linux kernel image file"); 799 800 object_class_property_add_str(oc, "initrd", 801 machine_get_initrd, machine_set_initrd); 802 object_class_property_set_description(oc, "initrd", 803 "Linux initial ramdisk file"); 804 805 object_class_property_add_str(oc, "append", 806 machine_get_append, machine_set_append); 807 object_class_property_set_description(oc, "append", 808 "Linux kernel command line"); 809 810 object_class_property_add_str(oc, "dtb", 811 machine_get_dtb, machine_set_dtb); 812 object_class_property_set_description(oc, "dtb", 813 "Linux kernel device tree file"); 814 815 object_class_property_add_str(oc, "dumpdtb", 816 machine_get_dumpdtb, machine_set_dumpdtb); 817 object_class_property_set_description(oc, "dumpdtb", 818 "Dump current dtb to a file and quit"); 819 820 object_class_property_add(oc, "smp", "SMPConfiguration", 821 machine_get_smp, machine_set_smp, 822 NULL, NULL); 823 object_class_property_set_description(oc, "smp", 824 "CPU topology"); 825 826 object_class_property_add(oc, "phandle-start", "int", 827 machine_get_phandle_start, machine_set_phandle_start, 828 NULL, NULL); 829 object_class_property_set_description(oc, "phandle-start", 830 "The first phandle ID we may generate dynamically"); 831 832 object_class_property_add_str(oc, "dt-compatible", 833 machine_get_dt_compatible, machine_set_dt_compatible); 834 object_class_property_set_description(oc, "dt-compatible", 835 "Overrides the \"compatible\" property of the dt root node"); 836 837 object_class_property_add_bool(oc, "dump-guest-core", 838 machine_get_dump_guest_core, machine_set_dump_guest_core); 839 object_class_property_set_description(oc, "dump-guest-core", 840 "Include guest memory in a core dump"); 841 842 object_class_property_add_bool(oc, "mem-merge", 843 machine_get_mem_merge, machine_set_mem_merge); 844 object_class_property_set_description(oc, "mem-merge", 845 "Enable/disable memory merge support"); 846 847 object_class_property_add_bool(oc, "usb", 848 machine_get_usb, machine_set_usb); 849 object_class_property_set_description(oc, "usb", 850 "Set on/off to enable/disable usb"); 851 852 object_class_property_add_bool(oc, "graphics", 853 machine_get_graphics, machine_set_graphics); 854 object_class_property_set_description(oc, "graphics", 855 "Set on/off to enable/disable graphics emulation"); 856 857 object_class_property_add_str(oc, "firmware", 858 machine_get_firmware, machine_set_firmware); 859 object_class_property_set_description(oc, "firmware", 860 "Firmware image"); 861 862 object_class_property_add_bool(oc, "suppress-vmdesc", 863 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc); 864 object_class_property_set_description(oc, "suppress-vmdesc", 865 "Set on to disable self-describing migration"); 866 867 object_class_property_add_link(oc, "confidential-guest-support", 868 TYPE_CONFIDENTIAL_GUEST_SUPPORT, 869 offsetof(MachineState, cgs), 870 machine_check_confidential_guest_support, 871 OBJ_PROP_LINK_STRONG); 872 object_class_property_set_description(oc, "confidential-guest-support", 873 "Set confidential guest scheme to support"); 874 875 /* For compatibility */ 876 object_class_property_add_str(oc, "memory-encryption", 877 machine_get_memory_encryption, machine_set_memory_encryption); 878 object_class_property_set_description(oc, "memory-encryption", 879 "Set memory encryption object to use"); 880 881 object_class_property_add_str(oc, "memory-backend", 882 machine_get_memdev, machine_set_memdev); 883 object_class_property_set_description(oc, "memory-backend", 884 "Set RAM backend" 885 "Valid value is ID of hostmem based backend"); 886 } 887 888 static void machine_class_base_init(ObjectClass *oc, void *data) 889 { 890 MachineClass *mc = MACHINE_CLASS(oc); 891 mc->max_cpus = mc->max_cpus ?: 1; 892 mc->min_cpus = mc->min_cpus ?: 1; 893 mc->default_cpus = mc->default_cpus ?: 1; 894 895 if (!object_class_is_abstract(oc)) { 896 const char *cname = object_class_get_name(oc); 897 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX)); 898 mc->name = g_strndup(cname, 899 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX)); 900 mc->compat_props = g_ptr_array_new(); 901 } 902 } 903 904 static void machine_initfn(Object *obj) 905 { 906 MachineState *ms = MACHINE(obj); 907 MachineClass *mc = MACHINE_GET_CLASS(obj); 908 909 container_get(obj, "/peripheral"); 910 container_get(obj, "/peripheral-anon"); 911 912 ms->dump_guest_core = true; 913 ms->mem_merge = true; 914 ms->enable_graphics = true; 915 ms->kernel_cmdline = g_strdup(""); 916 917 if (mc->nvdimm_supported) { 918 Object *obj = OBJECT(ms); 919 920 ms->nvdimms_state = g_new0(NVDIMMState, 1); 921 object_property_add_bool(obj, "nvdimm", 922 machine_get_nvdimm, machine_set_nvdimm); 923 object_property_set_description(obj, "nvdimm", 924 "Set on/off to enable/disable " 925 "NVDIMM instantiation"); 926 927 object_property_add_str(obj, "nvdimm-persistence", 928 machine_get_nvdimm_persistence, 929 machine_set_nvdimm_persistence); 930 object_property_set_description(obj, "nvdimm-persistence", 931 "Set NVDIMM persistence" 932 "Valid values are cpu, mem-ctrl"); 933 } 934 935 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) { 936 ms->numa_state = g_new0(NumaState, 1); 937 object_property_add_bool(obj, "hmat", 938 machine_get_hmat, machine_set_hmat); 939 object_property_set_description(obj, "hmat", 940 "Set on/off to enable/disable " 941 "ACPI Heterogeneous Memory Attribute " 942 "Table (HMAT)"); 943 } 944 945 /* Register notifier when init is done for sysbus sanity checks */ 946 ms->sysbus_notifier.notify = machine_init_notify; 947 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier); 948 949 /* default to mc->default_cpus */ 950 ms->smp.cpus = mc->default_cpus; 951 ms->smp.max_cpus = mc->default_cpus; 952 ms->smp.sockets = 1; 953 ms->smp.dies = 1; 954 ms->smp.cores = 1; 955 ms->smp.threads = 1; 956 } 957 958 static void machine_finalize(Object *obj) 959 { 960 MachineState *ms = MACHINE(obj); 961 962 g_free(ms->kernel_filename); 963 g_free(ms->initrd_filename); 964 g_free(ms->kernel_cmdline); 965 g_free(ms->dtb); 966 g_free(ms->dumpdtb); 967 g_free(ms->dt_compatible); 968 g_free(ms->firmware); 969 g_free(ms->device_memory); 970 g_free(ms->nvdimms_state); 971 g_free(ms->numa_state); 972 } 973 974 bool machine_usb(MachineState *machine) 975 { 976 return machine->usb; 977 } 978 979 int machine_phandle_start(MachineState *machine) 980 { 981 return machine->phandle_start; 982 } 983 984 bool machine_dump_guest_core(MachineState *machine) 985 { 986 return machine->dump_guest_core; 987 } 988 989 bool machine_mem_merge(MachineState *machine) 990 { 991 return machine->mem_merge; 992 } 993 994 static char *cpu_slot_to_string(const CPUArchId *cpu) 995 { 996 GString *s = g_string_new(NULL); 997 if (cpu->props.has_socket_id) { 998 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id); 999 } 1000 if (cpu->props.has_die_id) { 1001 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id); 1002 } 1003 if (cpu->props.has_core_id) { 1004 if (s->len) { 1005 g_string_append_printf(s, ", "); 1006 } 1007 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id); 1008 } 1009 if (cpu->props.has_thread_id) { 1010 if (s->len) { 1011 g_string_append_printf(s, ", "); 1012 } 1013 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id); 1014 } 1015 return g_string_free(s, false); 1016 } 1017 1018 static void numa_validate_initiator(NumaState *numa_state) 1019 { 1020 int i; 1021 NodeInfo *numa_info = numa_state->nodes; 1022 1023 for (i = 0; i < numa_state->num_nodes; i++) { 1024 if (numa_info[i].initiator == MAX_NODES) { 1025 error_report("The initiator of NUMA node %d is missing, use " 1026 "'-numa node,initiator' option to declare it", i); 1027 exit(1); 1028 } 1029 1030 if (!numa_info[numa_info[i].initiator].present) { 1031 error_report("NUMA node %" PRIu16 " is missing, use " 1032 "'-numa node' option to declare it first", 1033 numa_info[i].initiator); 1034 exit(1); 1035 } 1036 1037 if (!numa_info[numa_info[i].initiator].has_cpu) { 1038 error_report("The initiator of NUMA node %d is invalid", i); 1039 exit(1); 1040 } 1041 } 1042 } 1043 1044 static void machine_numa_finish_cpu_init(MachineState *machine) 1045 { 1046 int i; 1047 bool default_mapping; 1048 GString *s = g_string_new(NULL); 1049 MachineClass *mc = MACHINE_GET_CLASS(machine); 1050 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine); 1051 1052 assert(machine->numa_state->num_nodes); 1053 for (i = 0; i < possible_cpus->len; i++) { 1054 if (possible_cpus->cpus[i].props.has_node_id) { 1055 break; 1056 } 1057 } 1058 default_mapping = (i == possible_cpus->len); 1059 1060 for (i = 0; i < possible_cpus->len; i++) { 1061 const CPUArchId *cpu_slot = &possible_cpus->cpus[i]; 1062 1063 if (!cpu_slot->props.has_node_id) { 1064 /* fetch default mapping from board and enable it */ 1065 CpuInstanceProperties props = cpu_slot->props; 1066 1067 props.node_id = mc->get_default_cpu_node_id(machine, i); 1068 if (!default_mapping) { 1069 /* record slots with not set mapping, 1070 * TODO: make it hard error in future */ 1071 char *cpu_str = cpu_slot_to_string(cpu_slot); 1072 g_string_append_printf(s, "%sCPU %d [%s]", 1073 s->len ? ", " : "", i, cpu_str); 1074 g_free(cpu_str); 1075 1076 /* non mapped cpus used to fallback to node 0 */ 1077 props.node_id = 0; 1078 } 1079 1080 props.has_node_id = true; 1081 machine_set_cpu_numa_node(machine, &props, &error_fatal); 1082 } 1083 } 1084 1085 if (machine->numa_state->hmat_enabled) { 1086 numa_validate_initiator(machine->numa_state); 1087 } 1088 1089 if (s->len && !qtest_enabled()) { 1090 warn_report("CPU(s) not present in any NUMA nodes: %s", 1091 s->str); 1092 warn_report("All CPU(s) up to maxcpus should be described " 1093 "in NUMA config, ability to start up with partial NUMA " 1094 "mappings is obsoleted and will be removed in future"); 1095 } 1096 g_string_free(s, true); 1097 } 1098 1099 MemoryRegion *machine_consume_memdev(MachineState *machine, 1100 HostMemoryBackend *backend) 1101 { 1102 MemoryRegion *ret = host_memory_backend_get_memory(backend); 1103 1104 if (memory_region_is_mapped(ret)) { 1105 error_report("memory backend %s can't be used multiple times.", 1106 object_get_canonical_path_component(OBJECT(backend))); 1107 exit(EXIT_FAILURE); 1108 } 1109 host_memory_backend_set_mapped(backend, true); 1110 vmstate_register_ram_global(ret); 1111 return ret; 1112 } 1113 1114 void machine_run_board_init(MachineState *machine) 1115 { 1116 MachineClass *machine_class = MACHINE_GET_CLASS(machine); 1117 ObjectClass *oc = object_class_by_name(machine->cpu_type); 1118 CPUClass *cc; 1119 1120 /* This checkpoint is required by replay to separate prior clock 1121 reading from the other reads, because timer polling functions query 1122 clock values from the log. */ 1123 replay_checkpoint(CHECKPOINT_INIT); 1124 1125 if (machine->ram_memdev_id) { 1126 Object *o; 1127 o = object_resolve_path_type(machine->ram_memdev_id, 1128 TYPE_MEMORY_BACKEND, NULL); 1129 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o)); 1130 } 1131 1132 if (machine->numa_state) { 1133 numa_complete_configuration(machine); 1134 if (machine->numa_state->num_nodes) { 1135 machine_numa_finish_cpu_init(machine); 1136 } 1137 } 1138 1139 /* If the machine supports the valid_cpu_types check and the user 1140 * specified a CPU with -cpu check here that the user CPU is supported. 1141 */ 1142 if (machine_class->valid_cpu_types && machine->cpu_type) { 1143 int i; 1144 1145 for (i = 0; machine_class->valid_cpu_types[i]; i++) { 1146 if (object_class_dynamic_cast(oc, 1147 machine_class->valid_cpu_types[i])) { 1148 /* The user specificed CPU is in the valid field, we are 1149 * good to go. 1150 */ 1151 break; 1152 } 1153 } 1154 1155 if (!machine_class->valid_cpu_types[i]) { 1156 /* The user specified CPU is not valid */ 1157 error_report("Invalid CPU type: %s", machine->cpu_type); 1158 error_printf("The valid types are: %s", 1159 machine_class->valid_cpu_types[0]); 1160 for (i = 1; machine_class->valid_cpu_types[i]; i++) { 1161 error_printf(", %s", machine_class->valid_cpu_types[i]); 1162 } 1163 error_printf("\n"); 1164 1165 exit(1); 1166 } 1167 } 1168 1169 /* Check if CPU type is deprecated and warn if so */ 1170 cc = CPU_CLASS(oc); 1171 if (cc && cc->deprecation_note) { 1172 warn_report("CPU model %s is deprecated -- %s", machine->cpu_type, 1173 cc->deprecation_note); 1174 } 1175 1176 if (machine->cgs) { 1177 /* 1178 * With confidential guests, the host can't see the real 1179 * contents of RAM, so there's no point in it trying to merge 1180 * areas. 1181 */ 1182 machine_set_mem_merge(OBJECT(machine), false, &error_abort); 1183 1184 /* 1185 * Virtio devices can't count on directly accessing guest 1186 * memory, so they need iommu_platform=on to use normal DMA 1187 * mechanisms. That requires also disabling legacy virtio 1188 * support for those virtio pci devices which allow it. 1189 */ 1190 object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy", 1191 "on", true); 1192 object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform", 1193 "on", false); 1194 } 1195 1196 accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator)); 1197 machine_class->init(machine); 1198 phase_advance(PHASE_MACHINE_INITIALIZED); 1199 } 1200 1201 static NotifierList machine_init_done_notifiers = 1202 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers); 1203 1204 void qemu_add_machine_init_done_notifier(Notifier *notify) 1205 { 1206 notifier_list_add(&machine_init_done_notifiers, notify); 1207 if (phase_check(PHASE_MACHINE_READY)) { 1208 notify->notify(notify, NULL); 1209 } 1210 } 1211 1212 void qemu_remove_machine_init_done_notifier(Notifier *notify) 1213 { 1214 notifier_remove(notify); 1215 } 1216 1217 void qdev_machine_creation_done(void) 1218 { 1219 cpu_synchronize_all_post_init(); 1220 1221 if (current_machine->boot_once) { 1222 qemu_boot_set(current_machine->boot_once, &error_fatal); 1223 qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_order)); 1224 } 1225 1226 /* 1227 * ok, initial machine setup is done, starting from now we can 1228 * only create hotpluggable devices 1229 */ 1230 phase_advance(PHASE_MACHINE_READY); 1231 qdev_assert_realized_properly(); 1232 1233 /* TODO: once all bus devices are qdevified, this should be done 1234 * when bus is created by qdev.c */ 1235 /* 1236 * TODO: If we had a main 'reset container' that the whole system 1237 * lived in, we could reset that using the multi-phase reset 1238 * APIs. For the moment, we just reset the sysbus, which will cause 1239 * all devices hanging off it (and all their child buses, recursively) 1240 * to be reset. Note that this will *not* reset any Device objects 1241 * which are not attached to some part of the qbus tree! 1242 */ 1243 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default()); 1244 1245 notifier_list_notify(&machine_init_done_notifiers, NULL); 1246 1247 if (rom_check_and_register_reset() != 0) { 1248 exit(1); 1249 } 1250 1251 replay_start(); 1252 1253 /* This checkpoint is required by replay to separate prior clock 1254 reading from the other reads, because timer polling functions query 1255 clock values from the log. */ 1256 replay_checkpoint(CHECKPOINT_RESET); 1257 qemu_system_reset(SHUTDOWN_CAUSE_NONE); 1258 register_global_state(); 1259 } 1260 1261 static const TypeInfo machine_info = { 1262 .name = TYPE_MACHINE, 1263 .parent = TYPE_OBJECT, 1264 .abstract = true, 1265 .class_size = sizeof(MachineClass), 1266 .class_init = machine_class_init, 1267 .class_base_init = machine_class_base_init, 1268 .instance_size = sizeof(MachineState), 1269 .instance_init = machine_initfn, 1270 .instance_finalize = machine_finalize, 1271 }; 1272 1273 static void machine_register_types(void) 1274 { 1275 type_register_static(&machine_info); 1276 } 1277 1278 type_init(machine_register_types) 1279