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