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