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