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