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