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