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