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