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