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