virtio-blk-pci: default num_queues to -smp N
[qemu/ar7.git] / hw / core / machine.c
blob7f65fa8743610983686f2502327642ee32f9c0be
1 /*
2 * QEMU Machine
4 * Copyright (C) 2014 Red Hat Inc
6 * Authors:
7 * Marcel Apfelbaum <marcel.a@redhat.com>
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.
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"
31 GlobalProperty hw_compat_5_1[] = {
32 { "vhost-scsi", "num_queues", "1"},
33 { "vhost-user-scsi", "num_queues", "1"},
34 { "virtio-blk-device", "num-queues", "1"},
35 { "virtio-scsi-device", "num_queues", "1"},
37 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
39 GlobalProperty hw_compat_5_0[] = {
40 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
41 { "virtio-balloon-device", "page-poison", "false" },
42 { "vmport", "x-read-set-eax", "off" },
43 { "vmport", "x-signal-unsupported-cmd", "off" },
44 { "vmport", "x-report-vmx-type", "off" },
45 { "vmport", "x-cmds-v2", "off" },
47 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
49 GlobalProperty hw_compat_4_2[] = {
50 { "virtio-blk-device", "queue-size", "128"},
51 { "virtio-scsi-device", "virtqueue_size", "128"},
52 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
53 { "virtio-blk-device", "seg-max-adjust", "off"},
54 { "virtio-scsi-device", "seg_max_adjust", "off"},
55 { "vhost-blk-device", "seg_max_adjust", "off"},
56 { "usb-host", "suppress-remote-wake", "off" },
57 { "usb-redir", "suppress-remote-wake", "off" },
58 { "qxl", "revision", "4" },
59 { "qxl-vga", "revision", "4" },
60 { "fw_cfg", "acpi-mr-restore", "false" },
62 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
64 GlobalProperty hw_compat_4_1[] = {
65 { "virtio-pci", "x-pcie-flr-init", "off" },
66 { "virtio-device", "use-disabled-flag", "false" },
68 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
70 GlobalProperty hw_compat_4_0[] = {
71 { "VGA", "edid", "false" },
72 { "secondary-vga", "edid", "false" },
73 { "bochs-display", "edid", "false" },
74 { "virtio-vga", "edid", "false" },
75 { "virtio-gpu-device", "edid", "false" },
76 { "virtio-device", "use-started", "false" },
77 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
78 { "pl031", "migrate-tick-offset", "false" },
80 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
82 GlobalProperty hw_compat_3_1[] = {
83 { "pcie-root-port", "x-speed", "2_5" },
84 { "pcie-root-port", "x-width", "1" },
85 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
86 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
87 { "tpm-crb", "ppi", "false" },
88 { "tpm-tis", "ppi", "false" },
89 { "usb-kbd", "serial", "42" },
90 { "usb-mouse", "serial", "42" },
91 { "usb-tablet", "serial", "42" },
92 { "virtio-blk-device", "discard", "false" },
93 { "virtio-blk-device", "write-zeroes", "false" },
94 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
95 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
97 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
99 GlobalProperty hw_compat_3_0[] = {};
100 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
102 GlobalProperty hw_compat_2_12[] = {
103 { "migration", "decompress-error-check", "off" },
104 { "hda-audio", "use-timer", "false" },
105 { "cirrus-vga", "global-vmstate", "true" },
106 { "VGA", "global-vmstate", "true" },
107 { "vmware-svga", "global-vmstate", "true" },
108 { "qxl-vga", "global-vmstate", "true" },
110 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
112 GlobalProperty hw_compat_2_11[] = {
113 { "hpet", "hpet-offset-saved", "false" },
114 { "virtio-blk-pci", "vectors", "2" },
115 { "vhost-user-blk-pci", "vectors", "2" },
116 { "e1000", "migrate_tso_props", "off" },
118 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
120 GlobalProperty hw_compat_2_10[] = {
121 { "virtio-mouse-device", "wheel-axis", "false" },
122 { "virtio-tablet-device", "wheel-axis", "false" },
124 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
126 GlobalProperty hw_compat_2_9[] = {
127 { "pci-bridge", "shpc", "off" },
128 { "intel-iommu", "pt", "off" },
129 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
130 { "pcie-root-port", "x-migrate-msix", "false" },
132 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
134 GlobalProperty hw_compat_2_8[] = {
135 { "fw_cfg_mem", "x-file-slots", "0x10" },
136 { "fw_cfg_io", "x-file-slots", "0x10" },
137 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
138 { "pci-bridge", "shpc", "on" },
139 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
140 { "virtio-pci", "x-pcie-deverr-init", "off" },
141 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
142 { "virtio-pci", "x-pcie-pm-init", "off" },
143 { "cirrus-vga", "vgamem_mb", "8" },
144 { "isa-cirrus-vga", "vgamem_mb", "8" },
146 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
148 GlobalProperty hw_compat_2_7[] = {
149 { "virtio-pci", "page-per-vq", "on" },
150 { "virtio-serial-device", "emergency-write", "off" },
151 { "ioapic", "version", "0x11" },
152 { "intel-iommu", "x-buggy-eim", "true" },
153 { "virtio-pci", "x-ignore-backend-features", "on" },
155 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
157 GlobalProperty hw_compat_2_6[] = {
158 { "virtio-mmio", "format_transport_address", "off" },
159 /* Optional because not all virtio-pci devices support legacy mode */
160 { "virtio-pci", "disable-modern", "on", .optional = true },
161 { "virtio-pci", "disable-legacy", "off", .optional = true },
163 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
165 GlobalProperty hw_compat_2_5[] = {
166 { "isa-fdc", "fallback", "144" },
167 { "pvscsi", "x-old-pci-configuration", "on" },
168 { "pvscsi", "x-disable-pcie", "on" },
169 { "vmxnet3", "x-old-msi-offsets", "on" },
170 { "vmxnet3", "x-disable-pcie", "on" },
172 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
174 GlobalProperty hw_compat_2_4[] = {
175 /* Optional because the 'scsi' property is Linux-only */
176 { "virtio-blk-device", "scsi", "true", .optional = true },
177 { "e1000", "extra_mac_registers", "off" },
178 { "virtio-pci", "x-disable-pcie", "on" },
179 { "virtio-pci", "migrate-extra", "off" },
180 { "fw_cfg_mem", "dma_enabled", "off" },
181 { "fw_cfg_io", "dma_enabled", "off" }
183 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
185 GlobalProperty hw_compat_2_3[] = {
186 { "virtio-blk-pci", "any_layout", "off" },
187 { "virtio-balloon-pci", "any_layout", "off" },
188 { "virtio-serial-pci", "any_layout", "off" },
189 { "virtio-9p-pci", "any_layout", "off" },
190 { "virtio-rng-pci", "any_layout", "off" },
191 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
192 { "migration", "send-configuration", "off" },
193 { "migration", "send-section-footer", "off" },
194 { "migration", "store-global-state", "off" },
196 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
198 GlobalProperty hw_compat_2_2[] = {};
199 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
201 GlobalProperty hw_compat_2_1[] = {
202 { "intel-hda", "old_msi_addr", "on" },
203 { "VGA", "qemu-extended-regs", "off" },
204 { "secondary-vga", "qemu-extended-regs", "off" },
205 { "virtio-scsi-pci", "any_layout", "off" },
206 { "usb-mouse", "usb_version", "1" },
207 { "usb-kbd", "usb_version", "1" },
208 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
210 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
212 static char *machine_get_kernel(Object *obj, Error **errp)
214 MachineState *ms = MACHINE(obj);
216 return g_strdup(ms->kernel_filename);
219 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
221 MachineState *ms = MACHINE(obj);
223 g_free(ms->kernel_filename);
224 ms->kernel_filename = g_strdup(value);
227 static char *machine_get_initrd(Object *obj, Error **errp)
229 MachineState *ms = MACHINE(obj);
231 return g_strdup(ms->initrd_filename);
234 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
236 MachineState *ms = MACHINE(obj);
238 g_free(ms->initrd_filename);
239 ms->initrd_filename = g_strdup(value);
242 static char *machine_get_append(Object *obj, Error **errp)
244 MachineState *ms = MACHINE(obj);
246 return g_strdup(ms->kernel_cmdline);
249 static void machine_set_append(Object *obj, const char *value, Error **errp)
251 MachineState *ms = MACHINE(obj);
253 g_free(ms->kernel_cmdline);
254 ms->kernel_cmdline = g_strdup(value);
257 static char *machine_get_dtb(Object *obj, Error **errp)
259 MachineState *ms = MACHINE(obj);
261 return g_strdup(ms->dtb);
264 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
266 MachineState *ms = MACHINE(obj);
268 g_free(ms->dtb);
269 ms->dtb = g_strdup(value);
272 static char *machine_get_dumpdtb(Object *obj, Error **errp)
274 MachineState *ms = MACHINE(obj);
276 return g_strdup(ms->dumpdtb);
279 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
281 MachineState *ms = MACHINE(obj);
283 g_free(ms->dumpdtb);
284 ms->dumpdtb = g_strdup(value);
287 static void machine_get_phandle_start(Object *obj, Visitor *v,
288 const char *name, void *opaque,
289 Error **errp)
291 MachineState *ms = MACHINE(obj);
292 int64_t value = ms->phandle_start;
294 visit_type_int(v, name, &value, errp);
297 static void machine_set_phandle_start(Object *obj, Visitor *v,
298 const char *name, void *opaque,
299 Error **errp)
301 MachineState *ms = MACHINE(obj);
302 int64_t value;
304 if (!visit_type_int(v, name, &value, errp)) {
305 return;
308 ms->phandle_start = value;
311 static char *machine_get_dt_compatible(Object *obj, Error **errp)
313 MachineState *ms = MACHINE(obj);
315 return g_strdup(ms->dt_compatible);
318 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
320 MachineState *ms = MACHINE(obj);
322 g_free(ms->dt_compatible);
323 ms->dt_compatible = g_strdup(value);
326 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
328 MachineState *ms = MACHINE(obj);
330 return ms->dump_guest_core;
333 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
335 MachineState *ms = MACHINE(obj);
337 ms->dump_guest_core = value;
340 static bool machine_get_mem_merge(Object *obj, Error **errp)
342 MachineState *ms = MACHINE(obj);
344 return ms->mem_merge;
347 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
349 MachineState *ms = MACHINE(obj);
351 ms->mem_merge = value;
354 static bool machine_get_usb(Object *obj, Error **errp)
356 MachineState *ms = MACHINE(obj);
358 return ms->usb;
361 static void machine_set_usb(Object *obj, bool value, Error **errp)
363 MachineState *ms = MACHINE(obj);
365 ms->usb = value;
366 ms->usb_disabled = !value;
369 static bool machine_get_graphics(Object *obj, Error **errp)
371 MachineState *ms = MACHINE(obj);
373 return ms->enable_graphics;
376 static void machine_set_graphics(Object *obj, bool value, Error **errp)
378 MachineState *ms = MACHINE(obj);
380 ms->enable_graphics = value;
383 static char *machine_get_firmware(Object *obj, Error **errp)
385 MachineState *ms = MACHINE(obj);
387 return g_strdup(ms->firmware);
390 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
392 MachineState *ms = MACHINE(obj);
394 g_free(ms->firmware);
395 ms->firmware = g_strdup(value);
398 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
400 MachineState *ms = MACHINE(obj);
402 ms->suppress_vmdesc = value;
405 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
407 MachineState *ms = MACHINE(obj);
409 return ms->suppress_vmdesc;
412 static void machine_set_enforce_config_section(Object *obj, bool value,
413 Error **errp)
415 MachineState *ms = MACHINE(obj);
417 warn_report("enforce-config-section is deprecated, please use "
418 "-global migration.send-configuration=on|off instead");
420 ms->enforce_config_section = value;
423 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
425 MachineState *ms = MACHINE(obj);
427 return ms->enforce_config_section;
430 static char *machine_get_memory_encryption(Object *obj, Error **errp)
432 MachineState *ms = MACHINE(obj);
434 return g_strdup(ms->memory_encryption);
437 static void machine_set_memory_encryption(Object *obj, const char *value,
438 Error **errp)
440 MachineState *ms = MACHINE(obj);
442 g_free(ms->memory_encryption);
443 ms->memory_encryption = g_strdup(value);
446 * With memory encryption, the host can't see the real contents of RAM,
447 * so there's no point in it trying to merge areas.
449 if (value) {
450 machine_set_mem_merge(obj, false, errp);
454 static bool machine_get_nvdimm(Object *obj, Error **errp)
456 MachineState *ms = MACHINE(obj);
458 return ms->nvdimms_state->is_enabled;
461 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
463 MachineState *ms = MACHINE(obj);
465 ms->nvdimms_state->is_enabled = value;
468 static bool machine_get_hmat(Object *obj, Error **errp)
470 MachineState *ms = MACHINE(obj);
472 return ms->numa_state->hmat_enabled;
475 static void machine_set_hmat(Object *obj, bool value, Error **errp)
477 MachineState *ms = MACHINE(obj);
479 ms->numa_state->hmat_enabled = value;
482 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
484 MachineState *ms = MACHINE(obj);
486 return g_strdup(ms->nvdimms_state->persistence_string);
489 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
490 Error **errp)
492 MachineState *ms = MACHINE(obj);
493 NVDIMMState *nvdimms_state = ms->nvdimms_state;
495 if (strcmp(value, "cpu") == 0) {
496 nvdimms_state->persistence = 3;
497 } else if (strcmp(value, "mem-ctrl") == 0) {
498 nvdimms_state->persistence = 2;
499 } else {
500 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
501 value);
502 return;
505 g_free(nvdimms_state->persistence_string);
506 nvdimms_state->persistence_string = g_strdup(value);
509 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
511 strList *item = g_new0(strList, 1);
513 item->value = g_strdup(type);
514 item->next = mc->allowed_dynamic_sysbus_devices;
515 mc->allowed_dynamic_sysbus_devices = item;
518 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
520 MachineState *machine = opaque;
521 MachineClass *mc = MACHINE_GET_CLASS(machine);
522 bool allowed = false;
523 strList *wl;
525 for (wl = mc->allowed_dynamic_sysbus_devices;
526 !allowed && wl;
527 wl = wl->next) {
528 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
531 if (!allowed) {
532 error_report("Option '-device %s' cannot be handled by this machine",
533 object_class_get_name(object_get_class(OBJECT(sbdev))));
534 exit(1);
538 static char *machine_get_memdev(Object *obj, Error **errp)
540 MachineState *ms = MACHINE(obj);
542 return g_strdup(ms->ram_memdev_id);
545 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
547 MachineState *ms = MACHINE(obj);
549 g_free(ms->ram_memdev_id);
550 ms->ram_memdev_id = g_strdup(value);
554 static void machine_init_notify(Notifier *notifier, void *data)
556 MachineState *machine = MACHINE(qdev_get_machine());
559 * Loop through all dynamically created sysbus devices and check if they are
560 * all allowed. If a device is not allowed, error out.
562 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
565 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
567 int i;
568 HotpluggableCPUList *head = NULL;
569 MachineClass *mc = MACHINE_GET_CLASS(machine);
571 /* force board to initialize possible_cpus if it hasn't been done yet */
572 mc->possible_cpu_arch_ids(machine);
574 for (i = 0; i < machine->possible_cpus->len; i++) {
575 Object *cpu;
576 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
577 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
579 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
580 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
581 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
582 sizeof(*cpu_item->props));
584 cpu = machine->possible_cpus->cpus[i].cpu;
585 if (cpu) {
586 cpu_item->has_qom_path = true;
587 cpu_item->qom_path = object_get_canonical_path(cpu);
589 list_item->value = cpu_item;
590 list_item->next = head;
591 head = list_item;
593 return head;
597 * machine_set_cpu_numa_node:
598 * @machine: machine object to modify
599 * @props: specifies which cpu objects to assign to
600 * numa node specified by @props.node_id
601 * @errp: if an error occurs, a pointer to an area to store the error
603 * Associate NUMA node specified by @props.node_id with cpu slots that
604 * match socket/core/thread-ids specified by @props. It's recommended to use
605 * query-hotpluggable-cpus.props values to specify affected cpu slots,
606 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
608 * However for CLI convenience it's possible to pass in subset of properties,
609 * which would affect all cpu slots that match it.
610 * Ex for pc machine:
611 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
612 * -numa cpu,node-id=0,socket_id=0 \
613 * -numa cpu,node-id=1,socket_id=1
614 * will assign all child cores of socket 0 to node 0 and
615 * of socket 1 to node 1.
617 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
618 * return error.
619 * Empty subset is disallowed and function will return with error in this case.
621 void machine_set_cpu_numa_node(MachineState *machine,
622 const CpuInstanceProperties *props, Error **errp)
624 MachineClass *mc = MACHINE_GET_CLASS(machine);
625 NodeInfo *numa_info = machine->numa_state->nodes;
626 bool match = false;
627 int i;
629 if (!mc->possible_cpu_arch_ids) {
630 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
631 return;
634 /* disabling node mapping is not supported, forbid it */
635 assert(props->has_node_id);
637 /* force board to initialize possible_cpus if it hasn't been done yet */
638 mc->possible_cpu_arch_ids(machine);
640 for (i = 0; i < machine->possible_cpus->len; i++) {
641 CPUArchId *slot = &machine->possible_cpus->cpus[i];
643 /* reject unsupported by board properties */
644 if (props->has_thread_id && !slot->props.has_thread_id) {
645 error_setg(errp, "thread-id is not supported");
646 return;
649 if (props->has_core_id && !slot->props.has_core_id) {
650 error_setg(errp, "core-id is not supported");
651 return;
654 if (props->has_socket_id && !slot->props.has_socket_id) {
655 error_setg(errp, "socket-id is not supported");
656 return;
659 if (props->has_die_id && !slot->props.has_die_id) {
660 error_setg(errp, "die-id is not supported");
661 return;
664 /* skip slots with explicit mismatch */
665 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
666 continue;
669 if (props->has_core_id && props->core_id != slot->props.core_id) {
670 continue;
673 if (props->has_die_id && props->die_id != slot->props.die_id) {
674 continue;
677 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
678 continue;
681 /* reject assignment if slot is already assigned, for compatibility
682 * of legacy cpu_index mapping with SPAPR core based mapping do not
683 * error out if cpu thread and matched core have the same node-id */
684 if (slot->props.has_node_id &&
685 slot->props.node_id != props->node_id) {
686 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
687 slot->props.node_id);
688 return;
691 /* assign slot to node as it's matched '-numa cpu' key */
692 match = true;
693 slot->props.node_id = props->node_id;
694 slot->props.has_node_id = props->has_node_id;
696 if (machine->numa_state->hmat_enabled) {
697 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
698 (props->node_id != numa_info[props->node_id].initiator)) {
699 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
700 " should be itself", props->node_id);
701 return;
703 numa_info[props->node_id].has_cpu = true;
704 numa_info[props->node_id].initiator = props->node_id;
708 if (!match) {
709 error_setg(errp, "no match found");
713 static void smp_parse(MachineState *ms, QemuOpts *opts)
715 if (opts) {
716 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
717 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
718 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
719 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
721 /* compute missing values, prefer sockets over cores over threads */
722 if (cpus == 0 || sockets == 0) {
723 cores = cores > 0 ? cores : 1;
724 threads = threads > 0 ? threads : 1;
725 if (cpus == 0) {
726 sockets = sockets > 0 ? sockets : 1;
727 cpus = cores * threads * sockets;
728 } else {
729 ms->smp.max_cpus =
730 qemu_opt_get_number(opts, "maxcpus", cpus);
731 sockets = ms->smp.max_cpus / (cores * threads);
733 } else if (cores == 0) {
734 threads = threads > 0 ? threads : 1;
735 cores = cpus / (sockets * threads);
736 cores = cores > 0 ? cores : 1;
737 } else if (threads == 0) {
738 threads = cpus / (cores * sockets);
739 threads = threads > 0 ? threads : 1;
740 } else if (sockets * cores * threads < cpus) {
741 error_report("cpu topology: "
742 "sockets (%u) * cores (%u) * threads (%u) < "
743 "smp_cpus (%u)",
744 sockets, cores, threads, cpus);
745 exit(1);
748 ms->smp.max_cpus =
749 qemu_opt_get_number(opts, "maxcpus", cpus);
751 if (ms->smp.max_cpus < cpus) {
752 error_report("maxcpus must be equal to or greater than smp");
753 exit(1);
756 if (sockets * cores * threads > ms->smp.max_cpus) {
757 error_report("cpu topology: "
758 "sockets (%u) * cores (%u) * threads (%u) > "
759 "maxcpus (%u)",
760 sockets, cores, threads,
761 ms->smp.max_cpus);
762 exit(1);
765 if (sockets * cores * threads != ms->smp.max_cpus) {
766 warn_report("Invalid CPU topology deprecated: "
767 "sockets (%u) * cores (%u) * threads (%u) "
768 "!= maxcpus (%u)",
769 sockets, cores, threads,
770 ms->smp.max_cpus);
773 ms->smp.cpus = cpus;
774 ms->smp.cores = cores;
775 ms->smp.threads = threads;
776 ms->smp.sockets = sockets;
779 if (ms->smp.cpus > 1) {
780 Error *blocker = NULL;
781 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
782 replay_add_blocker(blocker);
786 static void machine_class_init(ObjectClass *oc, void *data)
788 MachineClass *mc = MACHINE_CLASS(oc);
790 /* Default 128 MB as guest ram size */
791 mc->default_ram_size = 128 * MiB;
792 mc->rom_file_has_mr = true;
793 mc->smp_parse = smp_parse;
795 /* numa node memory size aligned on 8MB by default.
796 * On Linux, each node's border has to be 8MB aligned
798 mc->numa_mem_align_shift = 23;
799 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
801 object_class_property_add_str(oc, "kernel",
802 machine_get_kernel, machine_set_kernel);
803 object_class_property_set_description(oc, "kernel",
804 "Linux kernel image file");
806 object_class_property_add_str(oc, "initrd",
807 machine_get_initrd, machine_set_initrd);
808 object_class_property_set_description(oc, "initrd",
809 "Linux initial ramdisk file");
811 object_class_property_add_str(oc, "append",
812 machine_get_append, machine_set_append);
813 object_class_property_set_description(oc, "append",
814 "Linux kernel command line");
816 object_class_property_add_str(oc, "dtb",
817 machine_get_dtb, machine_set_dtb);
818 object_class_property_set_description(oc, "dtb",
819 "Linux kernel device tree file");
821 object_class_property_add_str(oc, "dumpdtb",
822 machine_get_dumpdtb, machine_set_dumpdtb);
823 object_class_property_set_description(oc, "dumpdtb",
824 "Dump current dtb to a file and quit");
826 object_class_property_add(oc, "phandle-start", "int",
827 machine_get_phandle_start, machine_set_phandle_start,
828 NULL, NULL);
829 object_class_property_set_description(oc, "phandle-start",
830 "The first phandle ID we may generate dynamically");
832 object_class_property_add_str(oc, "dt-compatible",
833 machine_get_dt_compatible, machine_set_dt_compatible);
834 object_class_property_set_description(oc, "dt-compatible",
835 "Overrides the \"compatible\" property of the dt root node");
837 object_class_property_add_bool(oc, "dump-guest-core",
838 machine_get_dump_guest_core, machine_set_dump_guest_core);
839 object_class_property_set_description(oc, "dump-guest-core",
840 "Include guest memory in a core dump");
842 object_class_property_add_bool(oc, "mem-merge",
843 machine_get_mem_merge, machine_set_mem_merge);
844 object_class_property_set_description(oc, "mem-merge",
845 "Enable/disable memory merge support");
847 object_class_property_add_bool(oc, "usb",
848 machine_get_usb, machine_set_usb);
849 object_class_property_set_description(oc, "usb",
850 "Set on/off to enable/disable usb");
852 object_class_property_add_bool(oc, "graphics",
853 machine_get_graphics, machine_set_graphics);
854 object_class_property_set_description(oc, "graphics",
855 "Set on/off to enable/disable graphics emulation");
857 object_class_property_add_str(oc, "firmware",
858 machine_get_firmware, machine_set_firmware);
859 object_class_property_set_description(oc, "firmware",
860 "Firmware image");
862 object_class_property_add_bool(oc, "suppress-vmdesc",
863 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
864 object_class_property_set_description(oc, "suppress-vmdesc",
865 "Set on to disable self-describing migration");
867 object_class_property_add_bool(oc, "enforce-config-section",
868 machine_get_enforce_config_section, machine_set_enforce_config_section);
869 object_class_property_set_description(oc, "enforce-config-section",
870 "Set on to enforce configuration section migration");
872 object_class_property_add_str(oc, "memory-encryption",
873 machine_get_memory_encryption, machine_set_memory_encryption);
874 object_class_property_set_description(oc, "memory-encryption",
875 "Set memory encryption object to use");
878 static void machine_class_base_init(ObjectClass *oc, void *data)
880 if (!object_class_is_abstract(oc)) {
881 MachineClass *mc = MACHINE_CLASS(oc);
882 const char *cname = object_class_get_name(oc);
883 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
884 mc->name = g_strndup(cname,
885 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
886 mc->compat_props = g_ptr_array_new();
890 static void machine_initfn(Object *obj)
892 MachineState *ms = MACHINE(obj);
893 MachineClass *mc = MACHINE_GET_CLASS(obj);
895 ms->dump_guest_core = true;
896 ms->mem_merge = true;
897 ms->enable_graphics = true;
899 if (mc->nvdimm_supported) {
900 Object *obj = OBJECT(ms);
902 ms->nvdimms_state = g_new0(NVDIMMState, 1);
903 object_property_add_bool(obj, "nvdimm",
904 machine_get_nvdimm, machine_set_nvdimm);
905 object_property_set_description(obj, "nvdimm",
906 "Set on/off to enable/disable "
907 "NVDIMM instantiation");
909 object_property_add_str(obj, "nvdimm-persistence",
910 machine_get_nvdimm_persistence,
911 machine_set_nvdimm_persistence);
912 object_property_set_description(obj, "nvdimm-persistence",
913 "Set NVDIMM persistence"
914 "Valid values are cpu, mem-ctrl");
917 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
918 ms->numa_state = g_new0(NumaState, 1);
919 object_property_add_bool(obj, "hmat",
920 machine_get_hmat, machine_set_hmat);
921 object_property_set_description(obj, "hmat",
922 "Set on/off to enable/disable "
923 "ACPI Heterogeneous Memory Attribute "
924 "Table (HMAT)");
927 object_property_add_str(obj, "memory-backend",
928 machine_get_memdev, machine_set_memdev);
929 object_property_set_description(obj, "memory-backend",
930 "Set RAM backend"
931 "Valid value is ID of hostmem based backend");
933 /* Register notifier when init is done for sysbus sanity checks */
934 ms->sysbus_notifier.notify = machine_init_notify;
935 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
938 static void machine_finalize(Object *obj)
940 MachineState *ms = MACHINE(obj);
942 g_free(ms->kernel_filename);
943 g_free(ms->initrd_filename);
944 g_free(ms->kernel_cmdline);
945 g_free(ms->dtb);
946 g_free(ms->dumpdtb);
947 g_free(ms->dt_compatible);
948 g_free(ms->firmware);
949 g_free(ms->device_memory);
950 g_free(ms->nvdimms_state);
951 g_free(ms->numa_state);
954 bool machine_usb(MachineState *machine)
956 return machine->usb;
959 int machine_phandle_start(MachineState *machine)
961 return machine->phandle_start;
964 bool machine_dump_guest_core(MachineState *machine)
966 return machine->dump_guest_core;
969 bool machine_mem_merge(MachineState *machine)
971 return machine->mem_merge;
974 static char *cpu_slot_to_string(const CPUArchId *cpu)
976 GString *s = g_string_new(NULL);
977 if (cpu->props.has_socket_id) {
978 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
980 if (cpu->props.has_die_id) {
981 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
983 if (cpu->props.has_core_id) {
984 if (s->len) {
985 g_string_append_printf(s, ", ");
987 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
989 if (cpu->props.has_thread_id) {
990 if (s->len) {
991 g_string_append_printf(s, ", ");
993 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
995 return g_string_free(s, false);
998 static void numa_validate_initiator(NumaState *numa_state)
1000 int i;
1001 NodeInfo *numa_info = numa_state->nodes;
1003 for (i = 0; i < numa_state->num_nodes; i++) {
1004 if (numa_info[i].initiator == MAX_NODES) {
1005 error_report("The initiator of NUMA node %d is missing, use "
1006 "'-numa node,initiator' option to declare it", i);
1007 exit(1);
1010 if (!numa_info[numa_info[i].initiator].present) {
1011 error_report("NUMA node %" PRIu16 " is missing, use "
1012 "'-numa node' option to declare it first",
1013 numa_info[i].initiator);
1014 exit(1);
1017 if (!numa_info[numa_info[i].initiator].has_cpu) {
1018 error_report("The initiator of NUMA node %d is invalid", i);
1019 exit(1);
1024 static void machine_numa_finish_cpu_init(MachineState *machine)
1026 int i;
1027 bool default_mapping;
1028 GString *s = g_string_new(NULL);
1029 MachineClass *mc = MACHINE_GET_CLASS(machine);
1030 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1032 assert(machine->numa_state->num_nodes);
1033 for (i = 0; i < possible_cpus->len; i++) {
1034 if (possible_cpus->cpus[i].props.has_node_id) {
1035 break;
1038 default_mapping = (i == possible_cpus->len);
1040 for (i = 0; i < possible_cpus->len; i++) {
1041 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1043 if (!cpu_slot->props.has_node_id) {
1044 /* fetch default mapping from board and enable it */
1045 CpuInstanceProperties props = cpu_slot->props;
1047 props.node_id = mc->get_default_cpu_node_id(machine, i);
1048 if (!default_mapping) {
1049 /* record slots with not set mapping,
1050 * TODO: make it hard error in future */
1051 char *cpu_str = cpu_slot_to_string(cpu_slot);
1052 g_string_append_printf(s, "%sCPU %d [%s]",
1053 s->len ? ", " : "", i, cpu_str);
1054 g_free(cpu_str);
1056 /* non mapped cpus used to fallback to node 0 */
1057 props.node_id = 0;
1060 props.has_node_id = true;
1061 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1065 if (machine->numa_state->hmat_enabled) {
1066 numa_validate_initiator(machine->numa_state);
1069 if (s->len && !qtest_enabled()) {
1070 warn_report("CPU(s) not present in any NUMA nodes: %s",
1071 s->str);
1072 warn_report("All CPU(s) up to maxcpus should be described "
1073 "in NUMA config, ability to start up with partial NUMA "
1074 "mappings is obsoleted and will be removed in future");
1076 g_string_free(s, true);
1079 MemoryRegion *machine_consume_memdev(MachineState *machine,
1080 HostMemoryBackend *backend)
1082 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1084 if (memory_region_is_mapped(ret)) {
1085 error_report("memory backend %s can't be used multiple times.",
1086 object_get_canonical_path_component(OBJECT(backend)));
1087 exit(EXIT_FAILURE);
1089 host_memory_backend_set_mapped(backend, true);
1090 vmstate_register_ram_global(ret);
1091 return ret;
1094 void machine_run_board_init(MachineState *machine)
1096 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1098 if (machine->ram_memdev_id) {
1099 Object *o;
1100 o = object_resolve_path_type(machine->ram_memdev_id,
1101 TYPE_MEMORY_BACKEND, NULL);
1102 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1105 if (machine->numa_state) {
1106 numa_complete_configuration(machine);
1107 if (machine->numa_state->num_nodes) {
1108 machine_numa_finish_cpu_init(machine);
1112 /* If the machine supports the valid_cpu_types check and the user
1113 * specified a CPU with -cpu check here that the user CPU is supported.
1115 if (machine_class->valid_cpu_types && machine->cpu_type) {
1116 ObjectClass *class = object_class_by_name(machine->cpu_type);
1117 int i;
1119 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1120 if (object_class_dynamic_cast(class,
1121 machine_class->valid_cpu_types[i])) {
1122 /* The user specificed CPU is in the valid field, we are
1123 * good to go.
1125 break;
1129 if (!machine_class->valid_cpu_types[i]) {
1130 /* The user specified CPU is not valid */
1131 error_report("Invalid CPU type: %s", machine->cpu_type);
1132 error_printf("The valid types are: %s",
1133 machine_class->valid_cpu_types[0]);
1134 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1135 error_printf(", %s", machine_class->valid_cpu_types[i]);
1137 error_printf("\n");
1139 exit(1);
1143 machine_class->init(machine);
1146 static const TypeInfo machine_info = {
1147 .name = TYPE_MACHINE,
1148 .parent = TYPE_OBJECT,
1149 .abstract = true,
1150 .class_size = sizeof(MachineClass),
1151 .class_init = machine_class_init,
1152 .class_base_init = machine_class_base_init,
1153 .instance_size = sizeof(MachineState),
1154 .instance_init = machine_initfn,
1155 .instance_finalize = machine_finalize,
1158 static void machine_register_types(void)
1160 type_register_static(&machine_info);
1163 type_init(machine_register_types)