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