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