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/option.h" 15 #include "qapi/qmp/qerror.h" 16 #include "sysemu/replay.h" 17 #include "qemu/units.h" 18 #include "hw/boards.h" 19 #include "qapi/error.h" 20 #include "qapi/qapi-visit-common.h" 21 #include "qapi/visitor.h" 22 #include "hw/sysbus.h" 23 #include "sysemu/sysemu.h" 24 #include "sysemu/numa.h" 25 #include "qemu/error-report.h" 26 #include "sysemu/qtest.h" 27 #include "hw/pci/pci.h" 28 #include "hw/mem/nvdimm.h" 29 #include "migration/vmstate.h" 30 31 GlobalProperty hw_compat_5_1[] = {}; 32 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1); 33 34 GlobalProperty hw_compat_5_0[] = { 35 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" }, 36 { "virtio-balloon-device", "page-poison", "false" }, 37 { "vmport", "x-read-set-eax", "off" }, 38 { "vmport", "x-signal-unsupported-cmd", "off" }, 39 { "vmport", "x-report-vmx-type", "off" }, 40 { "vmport", "x-cmds-v2", "off" }, 41 }; 42 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0); 43 44 GlobalProperty hw_compat_4_2[] = { 45 { "virtio-blk-device", "queue-size", "128"}, 46 { "virtio-scsi-device", "virtqueue_size", "128"}, 47 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" }, 48 { "virtio-blk-device", "seg-max-adjust", "off"}, 49 { "virtio-scsi-device", "seg_max_adjust", "off"}, 50 { "vhost-blk-device", "seg_max_adjust", "off"}, 51 { "usb-host", "suppress-remote-wake", "off" }, 52 { "usb-redir", "suppress-remote-wake", "off" }, 53 { "qxl", "revision", "4" }, 54 { "qxl-vga", "revision", "4" }, 55 { "fw_cfg", "acpi-mr-restore", "false" }, 56 }; 57 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2); 58 59 GlobalProperty hw_compat_4_1[] = { 60 { "virtio-pci", "x-pcie-flr-init", "off" }, 61 { "virtio-device", "use-disabled-flag", "false" }, 62 }; 63 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1); 64 65 GlobalProperty hw_compat_4_0[] = { 66 { "VGA", "edid", "false" }, 67 { "secondary-vga", "edid", "false" }, 68 { "bochs-display", "edid", "false" }, 69 { "virtio-vga", "edid", "false" }, 70 { "virtio-gpu-device", "edid", "false" }, 71 { "virtio-device", "use-started", "false" }, 72 { "virtio-balloon-device", "qemu-4-0-config-size", "true" }, 73 { "pl031", "migrate-tick-offset", "false" }, 74 }; 75 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0); 76 77 GlobalProperty hw_compat_3_1[] = { 78 { "pcie-root-port", "x-speed", "2_5" }, 79 { "pcie-root-port", "x-width", "1" }, 80 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" }, 81 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" }, 82 { "tpm-crb", "ppi", "false" }, 83 { "tpm-tis", "ppi", "false" }, 84 { "usb-kbd", "serial", "42" }, 85 { "usb-mouse", "serial", "42" }, 86 { "usb-tablet", "serial", "42" }, 87 { "virtio-blk-device", "discard", "false" }, 88 { "virtio-blk-device", "write-zeroes", "false" }, 89 { "virtio-balloon-device", "qemu-4-0-config-size", "false" }, 90 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */ 91 }; 92 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1); 93 94 GlobalProperty hw_compat_3_0[] = {}; 95 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0); 96 97 GlobalProperty hw_compat_2_12[] = { 98 { "migration", "decompress-error-check", "off" }, 99 { "hda-audio", "use-timer", "false" }, 100 { "cirrus-vga", "global-vmstate", "true" }, 101 { "VGA", "global-vmstate", "true" }, 102 { "vmware-svga", "global-vmstate", "true" }, 103 { "qxl-vga", "global-vmstate", "true" }, 104 }; 105 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12); 106 107 GlobalProperty hw_compat_2_11[] = { 108 { "hpet", "hpet-offset-saved", "false" }, 109 { "virtio-blk-pci", "vectors", "2" }, 110 { "vhost-user-blk-pci", "vectors", "2" }, 111 { "e1000", "migrate_tso_props", "off" }, 112 }; 113 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11); 114 115 GlobalProperty hw_compat_2_10[] = { 116 { "virtio-mouse-device", "wheel-axis", "false" }, 117 { "virtio-tablet-device", "wheel-axis", "false" }, 118 }; 119 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10); 120 121 GlobalProperty hw_compat_2_9[] = { 122 { "pci-bridge", "shpc", "off" }, 123 { "intel-iommu", "pt", "off" }, 124 { "virtio-net-device", "x-mtu-bypass-backend", "off" }, 125 { "pcie-root-port", "x-migrate-msix", "false" }, 126 }; 127 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9); 128 129 GlobalProperty hw_compat_2_8[] = { 130 { "fw_cfg_mem", "x-file-slots", "0x10" }, 131 { "fw_cfg_io", "x-file-slots", "0x10" }, 132 { "pflash_cfi01", "old-multiple-chip-handling", "on" }, 133 { "pci-bridge", "shpc", "on" }, 134 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" }, 135 { "virtio-pci", "x-pcie-deverr-init", "off" }, 136 { "virtio-pci", "x-pcie-lnkctl-init", "off" }, 137 { "virtio-pci", "x-pcie-pm-init", "off" }, 138 { "cirrus-vga", "vgamem_mb", "8" }, 139 { "isa-cirrus-vga", "vgamem_mb", "8" }, 140 }; 141 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8); 142 143 GlobalProperty hw_compat_2_7[] = { 144 { "virtio-pci", "page-per-vq", "on" }, 145 { "virtio-serial-device", "emergency-write", "off" }, 146 { "ioapic", "version", "0x11" }, 147 { "intel-iommu", "x-buggy-eim", "true" }, 148 { "virtio-pci", "x-ignore-backend-features", "on" }, 149 }; 150 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7); 151 152 GlobalProperty hw_compat_2_6[] = { 153 { "virtio-mmio", "format_transport_address", "off" }, 154 /* Optional because not all virtio-pci devices support legacy mode */ 155 { "virtio-pci", "disable-modern", "on", .optional = true }, 156 { "virtio-pci", "disable-legacy", "off", .optional = true }, 157 }; 158 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6); 159 160 GlobalProperty hw_compat_2_5[] = { 161 { "isa-fdc", "fallback", "144" }, 162 { "pvscsi", "x-old-pci-configuration", "on" }, 163 { "pvscsi", "x-disable-pcie", "on" }, 164 { "vmxnet3", "x-old-msi-offsets", "on" }, 165 { "vmxnet3", "x-disable-pcie", "on" }, 166 }; 167 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5); 168 169 GlobalProperty hw_compat_2_4[] = { 170 /* Optional because the 'scsi' property is Linux-only */ 171 { "virtio-blk-device", "scsi", "true", .optional = true }, 172 { "e1000", "extra_mac_registers", "off" }, 173 { "virtio-pci", "x-disable-pcie", "on" }, 174 { "virtio-pci", "migrate-extra", "off" }, 175 { "fw_cfg_mem", "dma_enabled", "off" }, 176 { "fw_cfg_io", "dma_enabled", "off" } 177 }; 178 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4); 179 180 GlobalProperty hw_compat_2_3[] = { 181 { "virtio-blk-pci", "any_layout", "off" }, 182 { "virtio-balloon-pci", "any_layout", "off" }, 183 { "virtio-serial-pci", "any_layout", "off" }, 184 { "virtio-9p-pci", "any_layout", "off" }, 185 { "virtio-rng-pci", "any_layout", "off" }, 186 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" }, 187 { "migration", "send-configuration", "off" }, 188 { "migration", "send-section-footer", "off" }, 189 { "migration", "store-global-state", "off" }, 190 }; 191 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3); 192 193 GlobalProperty hw_compat_2_2[] = {}; 194 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2); 195 196 GlobalProperty hw_compat_2_1[] = { 197 { "intel-hda", "old_msi_addr", "on" }, 198 { "VGA", "qemu-extended-regs", "off" }, 199 { "secondary-vga", "qemu-extended-regs", "off" }, 200 { "virtio-scsi-pci", "any_layout", "off" }, 201 { "usb-mouse", "usb_version", "1" }, 202 { "usb-kbd", "usb_version", "1" }, 203 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" }, 204 }; 205 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1); 206 207 static char *machine_get_kernel(Object *obj, Error **errp) 208 { 209 MachineState *ms = MACHINE(obj); 210 211 return g_strdup(ms->kernel_filename); 212 } 213 214 static void machine_set_kernel(Object *obj, const char *value, Error **errp) 215 { 216 MachineState *ms = MACHINE(obj); 217 218 g_free(ms->kernel_filename); 219 ms->kernel_filename = g_strdup(value); 220 } 221 222 static char *machine_get_initrd(Object *obj, Error **errp) 223 { 224 MachineState *ms = MACHINE(obj); 225 226 return g_strdup(ms->initrd_filename); 227 } 228 229 static void machine_set_initrd(Object *obj, const char *value, Error **errp) 230 { 231 MachineState *ms = MACHINE(obj); 232 233 g_free(ms->initrd_filename); 234 ms->initrd_filename = g_strdup(value); 235 } 236 237 static char *machine_get_append(Object *obj, Error **errp) 238 { 239 MachineState *ms = MACHINE(obj); 240 241 return g_strdup(ms->kernel_cmdline); 242 } 243 244 static void machine_set_append(Object *obj, const char *value, Error **errp) 245 { 246 MachineState *ms = MACHINE(obj); 247 248 g_free(ms->kernel_cmdline); 249 ms->kernel_cmdline = g_strdup(value); 250 } 251 252 static char *machine_get_dtb(Object *obj, Error **errp) 253 { 254 MachineState *ms = MACHINE(obj); 255 256 return g_strdup(ms->dtb); 257 } 258 259 static void machine_set_dtb(Object *obj, const char *value, Error **errp) 260 { 261 MachineState *ms = MACHINE(obj); 262 263 g_free(ms->dtb); 264 ms->dtb = g_strdup(value); 265 } 266 267 static char *machine_get_dumpdtb(Object *obj, Error **errp) 268 { 269 MachineState *ms = MACHINE(obj); 270 271 return g_strdup(ms->dumpdtb); 272 } 273 274 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp) 275 { 276 MachineState *ms = MACHINE(obj); 277 278 g_free(ms->dumpdtb); 279 ms->dumpdtb = g_strdup(value); 280 } 281 282 static void machine_get_phandle_start(Object *obj, Visitor *v, 283 const char *name, void *opaque, 284 Error **errp) 285 { 286 MachineState *ms = MACHINE(obj); 287 int64_t value = ms->phandle_start; 288 289 visit_type_int(v, name, &value, errp); 290 } 291 292 static void machine_set_phandle_start(Object *obj, Visitor *v, 293 const char *name, void *opaque, 294 Error **errp) 295 { 296 MachineState *ms = MACHINE(obj); 297 int64_t value; 298 299 if (!visit_type_int(v, name, &value, errp)) { 300 return; 301 } 302 303 ms->phandle_start = value; 304 } 305 306 static char *machine_get_dt_compatible(Object *obj, Error **errp) 307 { 308 MachineState *ms = MACHINE(obj); 309 310 return g_strdup(ms->dt_compatible); 311 } 312 313 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp) 314 { 315 MachineState *ms = MACHINE(obj); 316 317 g_free(ms->dt_compatible); 318 ms->dt_compatible = g_strdup(value); 319 } 320 321 static bool machine_get_dump_guest_core(Object *obj, Error **errp) 322 { 323 MachineState *ms = MACHINE(obj); 324 325 return ms->dump_guest_core; 326 } 327 328 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp) 329 { 330 MachineState *ms = MACHINE(obj); 331 332 ms->dump_guest_core = value; 333 } 334 335 static bool machine_get_mem_merge(Object *obj, Error **errp) 336 { 337 MachineState *ms = MACHINE(obj); 338 339 return ms->mem_merge; 340 } 341 342 static void machine_set_mem_merge(Object *obj, bool value, Error **errp) 343 { 344 MachineState *ms = MACHINE(obj); 345 346 ms->mem_merge = value; 347 } 348 349 static bool machine_get_usb(Object *obj, Error **errp) 350 { 351 MachineState *ms = MACHINE(obj); 352 353 return ms->usb; 354 } 355 356 static void machine_set_usb(Object *obj, bool value, Error **errp) 357 { 358 MachineState *ms = MACHINE(obj); 359 360 ms->usb = value; 361 ms->usb_disabled = !value; 362 } 363 364 static bool machine_get_graphics(Object *obj, Error **errp) 365 { 366 MachineState *ms = MACHINE(obj); 367 368 return ms->enable_graphics; 369 } 370 371 static void machine_set_graphics(Object *obj, bool value, Error **errp) 372 { 373 MachineState *ms = MACHINE(obj); 374 375 ms->enable_graphics = value; 376 } 377 378 static char *machine_get_firmware(Object *obj, Error **errp) 379 { 380 MachineState *ms = MACHINE(obj); 381 382 return g_strdup(ms->firmware); 383 } 384 385 static void machine_set_firmware(Object *obj, const char *value, Error **errp) 386 { 387 MachineState *ms = MACHINE(obj); 388 389 g_free(ms->firmware); 390 ms->firmware = g_strdup(value); 391 } 392 393 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp) 394 { 395 MachineState *ms = MACHINE(obj); 396 397 ms->suppress_vmdesc = value; 398 } 399 400 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp) 401 { 402 MachineState *ms = MACHINE(obj); 403 404 return ms->suppress_vmdesc; 405 } 406 407 static void machine_set_enforce_config_section(Object *obj, bool value, 408 Error **errp) 409 { 410 MachineState *ms = MACHINE(obj); 411 412 warn_report("enforce-config-section is deprecated, please use " 413 "-global migration.send-configuration=on|off instead"); 414 415 ms->enforce_config_section = value; 416 } 417 418 static bool machine_get_enforce_config_section(Object *obj, Error **errp) 419 { 420 MachineState *ms = MACHINE(obj); 421 422 return ms->enforce_config_section; 423 } 424 425 static char *machine_get_memory_encryption(Object *obj, Error **errp) 426 { 427 MachineState *ms = MACHINE(obj); 428 429 return g_strdup(ms->memory_encryption); 430 } 431 432 static void machine_set_memory_encryption(Object *obj, const char *value, 433 Error **errp) 434 { 435 MachineState *ms = MACHINE(obj); 436 437 g_free(ms->memory_encryption); 438 ms->memory_encryption = g_strdup(value); 439 440 /* 441 * With memory encryption, the host can't see the real contents of RAM, 442 * so there's no point in it trying to merge areas. 443 */ 444 if (value) { 445 machine_set_mem_merge(obj, false, errp); 446 } 447 } 448 449 static bool machine_get_nvdimm(Object *obj, Error **errp) 450 { 451 MachineState *ms = MACHINE(obj); 452 453 return ms->nvdimms_state->is_enabled; 454 } 455 456 static void machine_set_nvdimm(Object *obj, bool value, Error **errp) 457 { 458 MachineState *ms = MACHINE(obj); 459 460 ms->nvdimms_state->is_enabled = value; 461 } 462 463 static bool machine_get_hmat(Object *obj, Error **errp) 464 { 465 MachineState *ms = MACHINE(obj); 466 467 return ms->numa_state->hmat_enabled; 468 } 469 470 static void machine_set_hmat(Object *obj, bool value, Error **errp) 471 { 472 MachineState *ms = MACHINE(obj); 473 474 ms->numa_state->hmat_enabled = value; 475 } 476 477 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp) 478 { 479 MachineState *ms = MACHINE(obj); 480 481 return g_strdup(ms->nvdimms_state->persistence_string); 482 } 483 484 static void machine_set_nvdimm_persistence(Object *obj, const char *value, 485 Error **errp) 486 { 487 MachineState *ms = MACHINE(obj); 488 NVDIMMState *nvdimms_state = ms->nvdimms_state; 489 490 if (strcmp(value, "cpu") == 0) { 491 nvdimms_state->persistence = 3; 492 } else if (strcmp(value, "mem-ctrl") == 0) { 493 nvdimms_state->persistence = 2; 494 } else { 495 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option", 496 value); 497 return; 498 } 499 500 g_free(nvdimms_state->persistence_string); 501 nvdimms_state->persistence_string = g_strdup(value); 502 } 503 504 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type) 505 { 506 strList *item = g_new0(strList, 1); 507 508 item->value = g_strdup(type); 509 item->next = mc->allowed_dynamic_sysbus_devices; 510 mc->allowed_dynamic_sysbus_devices = item; 511 } 512 513 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque) 514 { 515 MachineState *machine = opaque; 516 MachineClass *mc = MACHINE_GET_CLASS(machine); 517 bool allowed = false; 518 strList *wl; 519 520 for (wl = mc->allowed_dynamic_sysbus_devices; 521 !allowed && wl; 522 wl = wl->next) { 523 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value); 524 } 525 526 if (!allowed) { 527 error_report("Option '-device %s' cannot be handled by this machine", 528 object_class_get_name(object_get_class(OBJECT(sbdev)))); 529 exit(1); 530 } 531 } 532 533 static char *machine_get_memdev(Object *obj, Error **errp) 534 { 535 MachineState *ms = MACHINE(obj); 536 537 return g_strdup(ms->ram_memdev_id); 538 } 539 540 static void machine_set_memdev(Object *obj, const char *value, Error **errp) 541 { 542 MachineState *ms = MACHINE(obj); 543 544 g_free(ms->ram_memdev_id); 545 ms->ram_memdev_id = g_strdup(value); 546 } 547 548 549 static void machine_init_notify(Notifier *notifier, void *data) 550 { 551 MachineState *machine = MACHINE(qdev_get_machine()); 552 553 /* 554 * Loop through all dynamically created sysbus devices and check if they are 555 * all allowed. If a device is not allowed, error out. 556 */ 557 foreach_dynamic_sysbus_device(validate_sysbus_device, machine); 558 } 559 560 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine) 561 { 562 int i; 563 HotpluggableCPUList *head = NULL; 564 MachineClass *mc = MACHINE_GET_CLASS(machine); 565 566 /* force board to initialize possible_cpus if it hasn't been done yet */ 567 mc->possible_cpu_arch_ids(machine); 568 569 for (i = 0; i < machine->possible_cpus->len; i++) { 570 Object *cpu; 571 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1); 572 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1); 573 574 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type); 575 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count; 576 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props, 577 sizeof(*cpu_item->props)); 578 579 cpu = machine->possible_cpus->cpus[i].cpu; 580 if (cpu) { 581 cpu_item->has_qom_path = true; 582 cpu_item->qom_path = object_get_canonical_path(cpu); 583 } 584 list_item->value = cpu_item; 585 list_item->next = head; 586 head = list_item; 587 } 588 return head; 589 } 590 591 /** 592 * machine_set_cpu_numa_node: 593 * @machine: machine object to modify 594 * @props: specifies which cpu objects to assign to 595 * numa node specified by @props.node_id 596 * @errp: if an error occurs, a pointer to an area to store the error 597 * 598 * Associate NUMA node specified by @props.node_id with cpu slots that 599 * match socket/core/thread-ids specified by @props. It's recommended to use 600 * query-hotpluggable-cpus.props values to specify affected cpu slots, 601 * which would lead to exact 1:1 mapping of cpu slots to NUMA node. 602 * 603 * However for CLI convenience it's possible to pass in subset of properties, 604 * which would affect all cpu slots that match it. 605 * Ex for pc machine: 606 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \ 607 * -numa cpu,node-id=0,socket_id=0 \ 608 * -numa cpu,node-id=1,socket_id=1 609 * will assign all child cores of socket 0 to node 0 and 610 * of socket 1 to node 1. 611 * 612 * On attempt of reassigning (already assigned) cpu slot to another NUMA node, 613 * return error. 614 * Empty subset is disallowed and function will return with error in this case. 615 */ 616 void machine_set_cpu_numa_node(MachineState *machine, 617 const CpuInstanceProperties *props, Error **errp) 618 { 619 MachineClass *mc = MACHINE_GET_CLASS(machine); 620 NodeInfo *numa_info = machine->numa_state->nodes; 621 bool match = false; 622 int i; 623 624 if (!mc->possible_cpu_arch_ids) { 625 error_setg(errp, "mapping of CPUs to NUMA node is not supported"); 626 return; 627 } 628 629 /* disabling node mapping is not supported, forbid it */ 630 assert(props->has_node_id); 631 632 /* force board to initialize possible_cpus if it hasn't been done yet */ 633 mc->possible_cpu_arch_ids(machine); 634 635 for (i = 0; i < machine->possible_cpus->len; i++) { 636 CPUArchId *slot = &machine->possible_cpus->cpus[i]; 637 638 /* reject unsupported by board properties */ 639 if (props->has_thread_id && !slot->props.has_thread_id) { 640 error_setg(errp, "thread-id is not supported"); 641 return; 642 } 643 644 if (props->has_core_id && !slot->props.has_core_id) { 645 error_setg(errp, "core-id is not supported"); 646 return; 647 } 648 649 if (props->has_socket_id && !slot->props.has_socket_id) { 650 error_setg(errp, "socket-id is not supported"); 651 return; 652 } 653 654 if (props->has_die_id && !slot->props.has_die_id) { 655 error_setg(errp, "die-id is not supported"); 656 return; 657 } 658 659 /* skip slots with explicit mismatch */ 660 if (props->has_thread_id && props->thread_id != slot->props.thread_id) { 661 continue; 662 } 663 664 if (props->has_core_id && props->core_id != slot->props.core_id) { 665 continue; 666 } 667 668 if (props->has_die_id && props->die_id != slot->props.die_id) { 669 continue; 670 } 671 672 if (props->has_socket_id && props->socket_id != slot->props.socket_id) { 673 continue; 674 } 675 676 /* reject assignment if slot is already assigned, for compatibility 677 * of legacy cpu_index mapping with SPAPR core based mapping do not 678 * error out if cpu thread and matched core have the same node-id */ 679 if (slot->props.has_node_id && 680 slot->props.node_id != props->node_id) { 681 error_setg(errp, "CPU is already assigned to node-id: %" PRId64, 682 slot->props.node_id); 683 return; 684 } 685 686 /* assign slot to node as it's matched '-numa cpu' key */ 687 match = true; 688 slot->props.node_id = props->node_id; 689 slot->props.has_node_id = props->has_node_id; 690 691 if (machine->numa_state->hmat_enabled) { 692 if ((numa_info[props->node_id].initiator < MAX_NODES) && 693 (props->node_id != numa_info[props->node_id].initiator)) { 694 error_setg(errp, "The initiator of CPU NUMA node %" PRId64 695 " should be itself", props->node_id); 696 return; 697 } 698 numa_info[props->node_id].has_cpu = true; 699 numa_info[props->node_id].initiator = props->node_id; 700 } 701 } 702 703 if (!match) { 704 error_setg(errp, "no match found"); 705 } 706 } 707 708 static void smp_parse(MachineState *ms, QemuOpts *opts) 709 { 710 if (opts) { 711 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0); 712 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0); 713 unsigned cores = qemu_opt_get_number(opts, "cores", 0); 714 unsigned threads = qemu_opt_get_number(opts, "threads", 0); 715 716 /* compute missing values, prefer sockets over cores over threads */ 717 if (cpus == 0 || sockets == 0) { 718 cores = cores > 0 ? cores : 1; 719 threads = threads > 0 ? threads : 1; 720 if (cpus == 0) { 721 sockets = sockets > 0 ? sockets : 1; 722 cpus = cores * threads * sockets; 723 } else { 724 ms->smp.max_cpus = 725 qemu_opt_get_number(opts, "maxcpus", cpus); 726 sockets = ms->smp.max_cpus / (cores * threads); 727 } 728 } else if (cores == 0) { 729 threads = threads > 0 ? threads : 1; 730 cores = cpus / (sockets * threads); 731 cores = cores > 0 ? cores : 1; 732 } else if (threads == 0) { 733 threads = cpus / (cores * sockets); 734 threads = threads > 0 ? threads : 1; 735 } else if (sockets * cores * threads < cpus) { 736 error_report("cpu topology: " 737 "sockets (%u) * cores (%u) * threads (%u) < " 738 "smp_cpus (%u)", 739 sockets, cores, threads, cpus); 740 exit(1); 741 } 742 743 ms->smp.max_cpus = 744 qemu_opt_get_number(opts, "maxcpus", cpus); 745 746 if (ms->smp.max_cpus < cpus) { 747 error_report("maxcpus must be equal to or greater than smp"); 748 exit(1); 749 } 750 751 if (sockets * cores * threads > ms->smp.max_cpus) { 752 error_report("cpu topology: " 753 "sockets (%u) * cores (%u) * threads (%u) > " 754 "maxcpus (%u)", 755 sockets, cores, threads, 756 ms->smp.max_cpus); 757 exit(1); 758 } 759 760 if (sockets * cores * threads != ms->smp.max_cpus) { 761 warn_report("Invalid CPU topology deprecated: " 762 "sockets (%u) * cores (%u) * threads (%u) " 763 "!= maxcpus (%u)", 764 sockets, cores, threads, 765 ms->smp.max_cpus); 766 } 767 768 ms->smp.cpus = cpus; 769 ms->smp.cores = cores; 770 ms->smp.threads = threads; 771 ms->smp.sockets = sockets; 772 } 773 774 if (ms->smp.cpus > 1) { 775 Error *blocker = NULL; 776 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp"); 777 replay_add_blocker(blocker); 778 } 779 } 780 781 static void machine_class_init(ObjectClass *oc, void *data) 782 { 783 MachineClass *mc = MACHINE_CLASS(oc); 784 785 /* Default 128 MB as guest ram size */ 786 mc->default_ram_size = 128 * MiB; 787 mc->rom_file_has_mr = true; 788 mc->smp_parse = smp_parse; 789 790 /* numa node memory size aligned on 8MB by default. 791 * On Linux, each node's border has to be 8MB aligned 792 */ 793 mc->numa_mem_align_shift = 23; 794 mc->numa_auto_assign_ram = numa_default_auto_assign_ram; 795 796 object_class_property_add_str(oc, "kernel", 797 machine_get_kernel, machine_set_kernel); 798 object_class_property_set_description(oc, "kernel", 799 "Linux kernel image file"); 800 801 object_class_property_add_str(oc, "initrd", 802 machine_get_initrd, machine_set_initrd); 803 object_class_property_set_description(oc, "initrd", 804 "Linux initial ramdisk file"); 805 806 object_class_property_add_str(oc, "append", 807 machine_get_append, machine_set_append); 808 object_class_property_set_description(oc, "append", 809 "Linux kernel command line"); 810 811 object_class_property_add_str(oc, "dtb", 812 machine_get_dtb, machine_set_dtb); 813 object_class_property_set_description(oc, "dtb", 814 "Linux kernel device tree file"); 815 816 object_class_property_add_str(oc, "dumpdtb", 817 machine_get_dumpdtb, machine_set_dumpdtb); 818 object_class_property_set_description(oc, "dumpdtb", 819 "Dump current dtb to a file and quit"); 820 821 object_class_property_add(oc, "phandle-start", "int", 822 machine_get_phandle_start, machine_set_phandle_start, 823 NULL, NULL); 824 object_class_property_set_description(oc, "phandle-start", 825 "The first phandle ID we may generate dynamically"); 826 827 object_class_property_add_str(oc, "dt-compatible", 828 machine_get_dt_compatible, machine_set_dt_compatible); 829 object_class_property_set_description(oc, "dt-compatible", 830 "Overrides the \"compatible\" property of the dt root node"); 831 832 object_class_property_add_bool(oc, "dump-guest-core", 833 machine_get_dump_guest_core, machine_set_dump_guest_core); 834 object_class_property_set_description(oc, "dump-guest-core", 835 "Include guest memory in a core dump"); 836 837 object_class_property_add_bool(oc, "mem-merge", 838 machine_get_mem_merge, machine_set_mem_merge); 839 object_class_property_set_description(oc, "mem-merge", 840 "Enable/disable memory merge support"); 841 842 object_class_property_add_bool(oc, "usb", 843 machine_get_usb, machine_set_usb); 844 object_class_property_set_description(oc, "usb", 845 "Set on/off to enable/disable usb"); 846 847 object_class_property_add_bool(oc, "graphics", 848 machine_get_graphics, machine_set_graphics); 849 object_class_property_set_description(oc, "graphics", 850 "Set on/off to enable/disable graphics emulation"); 851 852 object_class_property_add_str(oc, "firmware", 853 machine_get_firmware, machine_set_firmware); 854 object_class_property_set_description(oc, "firmware", 855 "Firmware image"); 856 857 object_class_property_add_bool(oc, "suppress-vmdesc", 858 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc); 859 object_class_property_set_description(oc, "suppress-vmdesc", 860 "Set on to disable self-describing migration"); 861 862 object_class_property_add_bool(oc, "enforce-config-section", 863 machine_get_enforce_config_section, machine_set_enforce_config_section); 864 object_class_property_set_description(oc, "enforce-config-section", 865 "Set on to enforce configuration section migration"); 866 867 object_class_property_add_str(oc, "memory-encryption", 868 machine_get_memory_encryption, machine_set_memory_encryption); 869 object_class_property_set_description(oc, "memory-encryption", 870 "Set memory encryption object to use"); 871 } 872 873 static void machine_class_base_init(ObjectClass *oc, void *data) 874 { 875 if (!object_class_is_abstract(oc)) { 876 MachineClass *mc = MACHINE_CLASS(oc); 877 const char *cname = object_class_get_name(oc); 878 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX)); 879 mc->name = g_strndup(cname, 880 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX)); 881 mc->compat_props = g_ptr_array_new(); 882 } 883 } 884 885 static void machine_initfn(Object *obj) 886 { 887 MachineState *ms = MACHINE(obj); 888 MachineClass *mc = MACHINE_GET_CLASS(obj); 889 890 ms->dump_guest_core = true; 891 ms->mem_merge = true; 892 ms->enable_graphics = true; 893 894 if (mc->nvdimm_supported) { 895 Object *obj = OBJECT(ms); 896 897 ms->nvdimms_state = g_new0(NVDIMMState, 1); 898 object_property_add_bool(obj, "nvdimm", 899 machine_get_nvdimm, machine_set_nvdimm); 900 object_property_set_description(obj, "nvdimm", 901 "Set on/off to enable/disable " 902 "NVDIMM instantiation"); 903 904 object_property_add_str(obj, "nvdimm-persistence", 905 machine_get_nvdimm_persistence, 906 machine_set_nvdimm_persistence); 907 object_property_set_description(obj, "nvdimm-persistence", 908 "Set NVDIMM persistence" 909 "Valid values are cpu, mem-ctrl"); 910 } 911 912 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) { 913 ms->numa_state = g_new0(NumaState, 1); 914 object_property_add_bool(obj, "hmat", 915 machine_get_hmat, machine_set_hmat); 916 object_property_set_description(obj, "hmat", 917 "Set on/off to enable/disable " 918 "ACPI Heterogeneous Memory Attribute " 919 "Table (HMAT)"); 920 } 921 922 object_property_add_str(obj, "memory-backend", 923 machine_get_memdev, machine_set_memdev); 924 object_property_set_description(obj, "memory-backend", 925 "Set RAM backend" 926 "Valid value is ID of hostmem based backend"); 927 928 /* Register notifier when init is done for sysbus sanity checks */ 929 ms->sysbus_notifier.notify = machine_init_notify; 930 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier); 931 } 932 933 static void machine_finalize(Object *obj) 934 { 935 MachineState *ms = MACHINE(obj); 936 937 g_free(ms->kernel_filename); 938 g_free(ms->initrd_filename); 939 g_free(ms->kernel_cmdline); 940 g_free(ms->dtb); 941 g_free(ms->dumpdtb); 942 g_free(ms->dt_compatible); 943 g_free(ms->firmware); 944 g_free(ms->device_memory); 945 g_free(ms->nvdimms_state); 946 g_free(ms->numa_state); 947 } 948 949 bool machine_usb(MachineState *machine) 950 { 951 return machine->usb; 952 } 953 954 int machine_phandle_start(MachineState *machine) 955 { 956 return machine->phandle_start; 957 } 958 959 bool machine_dump_guest_core(MachineState *machine) 960 { 961 return machine->dump_guest_core; 962 } 963 964 bool machine_mem_merge(MachineState *machine) 965 { 966 return machine->mem_merge; 967 } 968 969 static char *cpu_slot_to_string(const CPUArchId *cpu) 970 { 971 GString *s = g_string_new(NULL); 972 if (cpu->props.has_socket_id) { 973 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id); 974 } 975 if (cpu->props.has_die_id) { 976 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id); 977 } 978 if (cpu->props.has_core_id) { 979 if (s->len) { 980 g_string_append_printf(s, ", "); 981 } 982 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id); 983 } 984 if (cpu->props.has_thread_id) { 985 if (s->len) { 986 g_string_append_printf(s, ", "); 987 } 988 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id); 989 } 990 return g_string_free(s, false); 991 } 992 993 static void numa_validate_initiator(NumaState *numa_state) 994 { 995 int i; 996 NodeInfo *numa_info = numa_state->nodes; 997 998 for (i = 0; i < numa_state->num_nodes; i++) { 999 if (numa_info[i].initiator == MAX_NODES) { 1000 error_report("The initiator of NUMA node %d is missing, use " 1001 "'-numa node,initiator' option to declare it", i); 1002 exit(1); 1003 } 1004 1005 if (!numa_info[numa_info[i].initiator].present) { 1006 error_report("NUMA node %" PRIu16 " is missing, use " 1007 "'-numa node' option to declare it first", 1008 numa_info[i].initiator); 1009 exit(1); 1010 } 1011 1012 if (!numa_info[numa_info[i].initiator].has_cpu) { 1013 error_report("The initiator of NUMA node %d is invalid", i); 1014 exit(1); 1015 } 1016 } 1017 } 1018 1019 static void machine_numa_finish_cpu_init(MachineState *machine) 1020 { 1021 int i; 1022 bool default_mapping; 1023 GString *s = g_string_new(NULL); 1024 MachineClass *mc = MACHINE_GET_CLASS(machine); 1025 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine); 1026 1027 assert(machine->numa_state->num_nodes); 1028 for (i = 0; i < possible_cpus->len; i++) { 1029 if (possible_cpus->cpus[i].props.has_node_id) { 1030 break; 1031 } 1032 } 1033 default_mapping = (i == possible_cpus->len); 1034 1035 for (i = 0; i < possible_cpus->len; i++) { 1036 const CPUArchId *cpu_slot = &possible_cpus->cpus[i]; 1037 1038 if (!cpu_slot->props.has_node_id) { 1039 /* fetch default mapping from board and enable it */ 1040 CpuInstanceProperties props = cpu_slot->props; 1041 1042 props.node_id = mc->get_default_cpu_node_id(machine, i); 1043 if (!default_mapping) { 1044 /* record slots with not set mapping, 1045 * TODO: make it hard error in future */ 1046 char *cpu_str = cpu_slot_to_string(cpu_slot); 1047 g_string_append_printf(s, "%sCPU %d [%s]", 1048 s->len ? ", " : "", i, cpu_str); 1049 g_free(cpu_str); 1050 1051 /* non mapped cpus used to fallback to node 0 */ 1052 props.node_id = 0; 1053 } 1054 1055 props.has_node_id = true; 1056 machine_set_cpu_numa_node(machine, &props, &error_fatal); 1057 } 1058 } 1059 1060 if (machine->numa_state->hmat_enabled) { 1061 numa_validate_initiator(machine->numa_state); 1062 } 1063 1064 if (s->len && !qtest_enabled()) { 1065 warn_report("CPU(s) not present in any NUMA nodes: %s", 1066 s->str); 1067 warn_report("All CPU(s) up to maxcpus should be described " 1068 "in NUMA config, ability to start up with partial NUMA " 1069 "mappings is obsoleted and will be removed in future"); 1070 } 1071 g_string_free(s, true); 1072 } 1073 1074 MemoryRegion *machine_consume_memdev(MachineState *machine, 1075 HostMemoryBackend *backend) 1076 { 1077 MemoryRegion *ret = host_memory_backend_get_memory(backend); 1078 1079 if (memory_region_is_mapped(ret)) { 1080 error_report("memory backend %s can't be used multiple times.", 1081 object_get_canonical_path_component(OBJECT(backend))); 1082 exit(EXIT_FAILURE); 1083 } 1084 host_memory_backend_set_mapped(backend, true); 1085 vmstate_register_ram_global(ret); 1086 return ret; 1087 } 1088 1089 void machine_run_board_init(MachineState *machine) 1090 { 1091 MachineClass *machine_class = MACHINE_GET_CLASS(machine); 1092 1093 if (machine->ram_memdev_id) { 1094 Object *o; 1095 o = object_resolve_path_type(machine->ram_memdev_id, 1096 TYPE_MEMORY_BACKEND, NULL); 1097 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o)); 1098 } 1099 1100 if (machine->numa_state) { 1101 numa_complete_configuration(machine); 1102 if (machine->numa_state->num_nodes) { 1103 machine_numa_finish_cpu_init(machine); 1104 } 1105 } 1106 1107 /* If the machine supports the valid_cpu_types check and the user 1108 * specified a CPU with -cpu check here that the user CPU is supported. 1109 */ 1110 if (machine_class->valid_cpu_types && machine->cpu_type) { 1111 ObjectClass *class = object_class_by_name(machine->cpu_type); 1112 int i; 1113 1114 for (i = 0; machine_class->valid_cpu_types[i]; i++) { 1115 if (object_class_dynamic_cast(class, 1116 machine_class->valid_cpu_types[i])) { 1117 /* The user specificed CPU is in the valid field, we are 1118 * good to go. 1119 */ 1120 break; 1121 } 1122 } 1123 1124 if (!machine_class->valid_cpu_types[i]) { 1125 /* The user specified CPU is not valid */ 1126 error_report("Invalid CPU type: %s", machine->cpu_type); 1127 error_printf("The valid types are: %s", 1128 machine_class->valid_cpu_types[0]); 1129 for (i = 1; machine_class->valid_cpu_types[i]; i++) { 1130 error_printf(", %s", machine_class->valid_cpu_types[i]); 1131 } 1132 error_printf("\n"); 1133 1134 exit(1); 1135 } 1136 } 1137 1138 machine_class->init(machine); 1139 } 1140 1141 static const TypeInfo machine_info = { 1142 .name = TYPE_MACHINE, 1143 .parent = TYPE_OBJECT, 1144 .abstract = true, 1145 .class_size = sizeof(MachineClass), 1146 .class_init = machine_class_init, 1147 .class_base_init = machine_class_base_init, 1148 .instance_size = sizeof(MachineState), 1149 .instance_init = machine_initfn, 1150 .instance_finalize = machine_finalize, 1151 }; 1152 1153 static void machine_register_types(void) 1154 { 1155 type_register_static(&machine_info); 1156 } 1157 1158 type_init(machine_register_types) 1159