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