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