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