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