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