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