util/vfio-helpers: Trace PCI BAR region info
[qemu/ar7.git] / hw / core / machine.c
blob98b87f76cbbef01d1c456ec85fe4e299fdf4944e
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-blk", "num-queues", "1"},
34 { "vhost-user-scsi", "num_queues", "1"},
35 { "virtio-blk-device", "num-queues", "1"},
36 { "virtio-scsi-device", "num_queues", "1"},
37 { "nvme", "use-intel-id", "on"},
39 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
41 GlobalProperty hw_compat_5_0[] = {
42 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
43 { "virtio-balloon-device", "page-poison", "false" },
44 { "vmport", "x-read-set-eax", "off" },
45 { "vmport", "x-signal-unsupported-cmd", "off" },
46 { "vmport", "x-report-vmx-type", "off" },
47 { "vmport", "x-cmds-v2", "off" },
48 { "virtio-device", "x-disable-legacy-check", "true" },
50 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
52 GlobalProperty hw_compat_4_2[] = {
53 { "virtio-blk-device", "queue-size", "128"},
54 { "virtio-scsi-device", "virtqueue_size", "128"},
55 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
56 { "virtio-blk-device", "seg-max-adjust", "off"},
57 { "virtio-scsi-device", "seg_max_adjust", "off"},
58 { "vhost-blk-device", "seg_max_adjust", "off"},
59 { "usb-host", "suppress-remote-wake", "off" },
60 { "usb-redir", "suppress-remote-wake", "off" },
61 { "qxl", "revision", "4" },
62 { "qxl-vga", "revision", "4" },
63 { "fw_cfg", "acpi-mr-restore", "false" },
65 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
67 GlobalProperty hw_compat_4_1[] = {
68 { "virtio-pci", "x-pcie-flr-init", "off" },
69 { "virtio-device", "use-disabled-flag", "false" },
71 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
73 GlobalProperty hw_compat_4_0[] = {
74 { "VGA", "edid", "false" },
75 { "secondary-vga", "edid", "false" },
76 { "bochs-display", "edid", "false" },
77 { "virtio-vga", "edid", "false" },
78 { "virtio-gpu-device", "edid", "false" },
79 { "virtio-device", "use-started", "false" },
80 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
81 { "pl031", "migrate-tick-offset", "false" },
83 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
85 GlobalProperty hw_compat_3_1[] = {
86 { "pcie-root-port", "x-speed", "2_5" },
87 { "pcie-root-port", "x-width", "1" },
88 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
89 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
90 { "tpm-crb", "ppi", "false" },
91 { "tpm-tis", "ppi", "false" },
92 { "usb-kbd", "serial", "42" },
93 { "usb-mouse", "serial", "42" },
94 { "usb-tablet", "serial", "42" },
95 { "virtio-blk-device", "discard", "false" },
96 { "virtio-blk-device", "write-zeroes", "false" },
97 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
98 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
100 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
102 GlobalProperty hw_compat_3_0[] = {};
103 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
105 GlobalProperty hw_compat_2_12[] = {
106 { "migration", "decompress-error-check", "off" },
107 { "hda-audio", "use-timer", "false" },
108 { "cirrus-vga", "global-vmstate", "true" },
109 { "VGA", "global-vmstate", "true" },
110 { "vmware-svga", "global-vmstate", "true" },
111 { "qxl-vga", "global-vmstate", "true" },
113 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
115 GlobalProperty hw_compat_2_11[] = {
116 { "hpet", "hpet-offset-saved", "false" },
117 { "virtio-blk-pci", "vectors", "2" },
118 { "vhost-user-blk-pci", "vectors", "2" },
119 { "e1000", "migrate_tso_props", "off" },
121 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
123 GlobalProperty hw_compat_2_10[] = {
124 { "virtio-mouse-device", "wheel-axis", "false" },
125 { "virtio-tablet-device", "wheel-axis", "false" },
127 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
129 GlobalProperty hw_compat_2_9[] = {
130 { "pci-bridge", "shpc", "off" },
131 { "intel-iommu", "pt", "off" },
132 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
133 { "pcie-root-port", "x-migrate-msix", "false" },
135 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
137 GlobalProperty hw_compat_2_8[] = {
138 { "fw_cfg_mem", "x-file-slots", "0x10" },
139 { "fw_cfg_io", "x-file-slots", "0x10" },
140 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
141 { "pci-bridge", "shpc", "on" },
142 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
143 { "virtio-pci", "x-pcie-deverr-init", "off" },
144 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
145 { "virtio-pci", "x-pcie-pm-init", "off" },
146 { "cirrus-vga", "vgamem_mb", "8" },
147 { "isa-cirrus-vga", "vgamem_mb", "8" },
149 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
151 GlobalProperty hw_compat_2_7[] = {
152 { "virtio-pci", "page-per-vq", "on" },
153 { "virtio-serial-device", "emergency-write", "off" },
154 { "ioapic", "version", "0x11" },
155 { "intel-iommu", "x-buggy-eim", "true" },
156 { "virtio-pci", "x-ignore-backend-features", "on" },
158 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
160 GlobalProperty hw_compat_2_6[] = {
161 { "virtio-mmio", "format_transport_address", "off" },
162 /* Optional because not all virtio-pci devices support legacy mode */
163 { "virtio-pci", "disable-modern", "on", .optional = true },
164 { "virtio-pci", "disable-legacy", "off", .optional = true },
166 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
168 GlobalProperty hw_compat_2_5[] = {
169 { "isa-fdc", "fallback", "144" },
170 { "pvscsi", "x-old-pci-configuration", "on" },
171 { "pvscsi", "x-disable-pcie", "on" },
172 { "vmxnet3", "x-old-msi-offsets", "on" },
173 { "vmxnet3", "x-disable-pcie", "on" },
175 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
177 GlobalProperty hw_compat_2_4[] = {
178 /* Optional because the 'scsi' property is Linux-only */
179 { "virtio-blk-device", "scsi", "true", .optional = true },
180 { "e1000", "extra_mac_registers", "off" },
181 { "virtio-pci", "x-disable-pcie", "on" },
182 { "virtio-pci", "migrate-extra", "off" },
183 { "fw_cfg_mem", "dma_enabled", "off" },
184 { "fw_cfg_io", "dma_enabled", "off" }
186 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
188 GlobalProperty hw_compat_2_3[] = {
189 { "virtio-blk-pci", "any_layout", "off" },
190 { "virtio-balloon-pci", "any_layout", "off" },
191 { "virtio-serial-pci", "any_layout", "off" },
192 { "virtio-9p-pci", "any_layout", "off" },
193 { "virtio-rng-pci", "any_layout", "off" },
194 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
195 { "migration", "send-configuration", "off" },
196 { "migration", "send-section-footer", "off" },
197 { "migration", "store-global-state", "off" },
199 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
201 GlobalProperty hw_compat_2_2[] = {};
202 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
204 GlobalProperty hw_compat_2_1[] = {
205 { "intel-hda", "old_msi_addr", "on" },
206 { "VGA", "qemu-extended-regs", "off" },
207 { "secondary-vga", "qemu-extended-regs", "off" },
208 { "virtio-scsi-pci", "any_layout", "off" },
209 { "usb-mouse", "usb_version", "1" },
210 { "usb-kbd", "usb_version", "1" },
211 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
213 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
215 static char *machine_get_kernel(Object *obj, Error **errp)
217 MachineState *ms = MACHINE(obj);
219 return g_strdup(ms->kernel_filename);
222 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
224 MachineState *ms = MACHINE(obj);
226 g_free(ms->kernel_filename);
227 ms->kernel_filename = g_strdup(value);
230 static char *machine_get_initrd(Object *obj, Error **errp)
232 MachineState *ms = MACHINE(obj);
234 return g_strdup(ms->initrd_filename);
237 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
239 MachineState *ms = MACHINE(obj);
241 g_free(ms->initrd_filename);
242 ms->initrd_filename = g_strdup(value);
245 static char *machine_get_append(Object *obj, Error **errp)
247 MachineState *ms = MACHINE(obj);
249 return g_strdup(ms->kernel_cmdline);
252 static void machine_set_append(Object *obj, const char *value, Error **errp)
254 MachineState *ms = MACHINE(obj);
256 g_free(ms->kernel_cmdline);
257 ms->kernel_cmdline = g_strdup(value);
260 static char *machine_get_dtb(Object *obj, Error **errp)
262 MachineState *ms = MACHINE(obj);
264 return g_strdup(ms->dtb);
267 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
269 MachineState *ms = MACHINE(obj);
271 g_free(ms->dtb);
272 ms->dtb = g_strdup(value);
275 static char *machine_get_dumpdtb(Object *obj, Error **errp)
277 MachineState *ms = MACHINE(obj);
279 return g_strdup(ms->dumpdtb);
282 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
284 MachineState *ms = MACHINE(obj);
286 g_free(ms->dumpdtb);
287 ms->dumpdtb = g_strdup(value);
290 static void machine_get_phandle_start(Object *obj, Visitor *v,
291 const char *name, void *opaque,
292 Error **errp)
294 MachineState *ms = MACHINE(obj);
295 int64_t value = ms->phandle_start;
297 visit_type_int(v, name, &value, errp);
300 static void machine_set_phandle_start(Object *obj, Visitor *v,
301 const char *name, void *opaque,
302 Error **errp)
304 MachineState *ms = MACHINE(obj);
305 int64_t value;
307 if (!visit_type_int(v, name, &value, errp)) {
308 return;
311 ms->phandle_start = value;
314 static char *machine_get_dt_compatible(Object *obj, Error **errp)
316 MachineState *ms = MACHINE(obj);
318 return g_strdup(ms->dt_compatible);
321 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
323 MachineState *ms = MACHINE(obj);
325 g_free(ms->dt_compatible);
326 ms->dt_compatible = g_strdup(value);
329 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
331 MachineState *ms = MACHINE(obj);
333 return ms->dump_guest_core;
336 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
338 MachineState *ms = MACHINE(obj);
340 ms->dump_guest_core = value;
343 static bool machine_get_mem_merge(Object *obj, Error **errp)
345 MachineState *ms = MACHINE(obj);
347 return ms->mem_merge;
350 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
352 MachineState *ms = MACHINE(obj);
354 ms->mem_merge = value;
357 static bool machine_get_usb(Object *obj, Error **errp)
359 MachineState *ms = MACHINE(obj);
361 return ms->usb;
364 static void machine_set_usb(Object *obj, bool value, Error **errp)
366 MachineState *ms = MACHINE(obj);
368 ms->usb = value;
369 ms->usb_disabled = !value;
372 static bool machine_get_graphics(Object *obj, Error **errp)
374 MachineState *ms = MACHINE(obj);
376 return ms->enable_graphics;
379 static void machine_set_graphics(Object *obj, bool value, Error **errp)
381 MachineState *ms = MACHINE(obj);
383 ms->enable_graphics = value;
386 static char *machine_get_firmware(Object *obj, Error **errp)
388 MachineState *ms = MACHINE(obj);
390 return g_strdup(ms->firmware);
393 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
395 MachineState *ms = MACHINE(obj);
397 g_free(ms->firmware);
398 ms->firmware = g_strdup(value);
401 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
403 MachineState *ms = MACHINE(obj);
405 ms->suppress_vmdesc = value;
408 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
410 MachineState *ms = MACHINE(obj);
412 return ms->suppress_vmdesc;
415 static char *machine_get_memory_encryption(Object *obj, Error **errp)
417 MachineState *ms = MACHINE(obj);
419 return g_strdup(ms->memory_encryption);
422 static void machine_set_memory_encryption(Object *obj, const char *value,
423 Error **errp)
425 MachineState *ms = MACHINE(obj);
427 g_free(ms->memory_encryption);
428 ms->memory_encryption = g_strdup(value);
431 * With memory encryption, the host can't see the real contents of RAM,
432 * so there's no point in it trying to merge areas.
434 if (value) {
435 machine_set_mem_merge(obj, false, errp);
439 static bool machine_get_nvdimm(Object *obj, Error **errp)
441 MachineState *ms = MACHINE(obj);
443 return ms->nvdimms_state->is_enabled;
446 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
448 MachineState *ms = MACHINE(obj);
450 ms->nvdimms_state->is_enabled = value;
453 static bool machine_get_hmat(Object *obj, Error **errp)
455 MachineState *ms = MACHINE(obj);
457 return ms->numa_state->hmat_enabled;
460 static void machine_set_hmat(Object *obj, bool value, Error **errp)
462 MachineState *ms = MACHINE(obj);
464 ms->numa_state->hmat_enabled = value;
467 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
469 MachineState *ms = MACHINE(obj);
471 return g_strdup(ms->nvdimms_state->persistence_string);
474 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
475 Error **errp)
477 MachineState *ms = MACHINE(obj);
478 NVDIMMState *nvdimms_state = ms->nvdimms_state;
480 if (strcmp(value, "cpu") == 0) {
481 nvdimms_state->persistence = 3;
482 } else if (strcmp(value, "mem-ctrl") == 0) {
483 nvdimms_state->persistence = 2;
484 } else {
485 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
486 value);
487 return;
490 g_free(nvdimms_state->persistence_string);
491 nvdimms_state->persistence_string = g_strdup(value);
494 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
496 strList *item = g_new0(strList, 1);
498 item->value = g_strdup(type);
499 item->next = mc->allowed_dynamic_sysbus_devices;
500 mc->allowed_dynamic_sysbus_devices = item;
503 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
505 MachineState *machine = opaque;
506 MachineClass *mc = MACHINE_GET_CLASS(machine);
507 bool allowed = false;
508 strList *wl;
510 for (wl = mc->allowed_dynamic_sysbus_devices;
511 !allowed && wl;
512 wl = wl->next) {
513 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
516 if (!allowed) {
517 error_report("Option '-device %s' cannot be handled by this machine",
518 object_class_get_name(object_get_class(OBJECT(sbdev))));
519 exit(1);
523 static char *machine_get_memdev(Object *obj, Error **errp)
525 MachineState *ms = MACHINE(obj);
527 return g_strdup(ms->ram_memdev_id);
530 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
532 MachineState *ms = MACHINE(obj);
534 g_free(ms->ram_memdev_id);
535 ms->ram_memdev_id = g_strdup(value);
539 static void machine_init_notify(Notifier *notifier, void *data)
541 MachineState *machine = MACHINE(qdev_get_machine());
544 * Loop through all dynamically created sysbus devices and check if they are
545 * all allowed. If a device is not allowed, error out.
547 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
550 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
552 int i;
553 HotpluggableCPUList *head = NULL;
554 MachineClass *mc = MACHINE_GET_CLASS(machine);
556 /* force board to initialize possible_cpus if it hasn't been done yet */
557 mc->possible_cpu_arch_ids(machine);
559 for (i = 0; i < machine->possible_cpus->len; i++) {
560 Object *cpu;
561 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
562 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
564 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
565 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
566 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
567 sizeof(*cpu_item->props));
569 cpu = machine->possible_cpus->cpus[i].cpu;
570 if (cpu) {
571 cpu_item->has_qom_path = true;
572 cpu_item->qom_path = object_get_canonical_path(cpu);
574 list_item->value = cpu_item;
575 list_item->next = head;
576 head = list_item;
578 return head;
582 * machine_set_cpu_numa_node:
583 * @machine: machine object to modify
584 * @props: specifies which cpu objects to assign to
585 * numa node specified by @props.node_id
586 * @errp: if an error occurs, a pointer to an area to store the error
588 * Associate NUMA node specified by @props.node_id with cpu slots that
589 * match socket/core/thread-ids specified by @props. It's recommended to use
590 * query-hotpluggable-cpus.props values to specify affected cpu slots,
591 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
593 * However for CLI convenience it's possible to pass in subset of properties,
594 * which would affect all cpu slots that match it.
595 * Ex for pc machine:
596 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
597 * -numa cpu,node-id=0,socket_id=0 \
598 * -numa cpu,node-id=1,socket_id=1
599 * will assign all child cores of socket 0 to node 0 and
600 * of socket 1 to node 1.
602 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
603 * return error.
604 * Empty subset is disallowed and function will return with error in this case.
606 void machine_set_cpu_numa_node(MachineState *machine,
607 const CpuInstanceProperties *props, Error **errp)
609 MachineClass *mc = MACHINE_GET_CLASS(machine);
610 NodeInfo *numa_info = machine->numa_state->nodes;
611 bool match = false;
612 int i;
614 if (!mc->possible_cpu_arch_ids) {
615 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
616 return;
619 /* disabling node mapping is not supported, forbid it */
620 assert(props->has_node_id);
622 /* force board to initialize possible_cpus if it hasn't been done yet */
623 mc->possible_cpu_arch_ids(machine);
625 for (i = 0; i < machine->possible_cpus->len; i++) {
626 CPUArchId *slot = &machine->possible_cpus->cpus[i];
628 /* reject unsupported by board properties */
629 if (props->has_thread_id && !slot->props.has_thread_id) {
630 error_setg(errp, "thread-id is not supported");
631 return;
634 if (props->has_core_id && !slot->props.has_core_id) {
635 error_setg(errp, "core-id is not supported");
636 return;
639 if (props->has_socket_id && !slot->props.has_socket_id) {
640 error_setg(errp, "socket-id is not supported");
641 return;
644 if (props->has_die_id && !slot->props.has_die_id) {
645 error_setg(errp, "die-id is not supported");
646 return;
649 /* skip slots with explicit mismatch */
650 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
651 continue;
654 if (props->has_core_id && props->core_id != slot->props.core_id) {
655 continue;
658 if (props->has_die_id && props->die_id != slot->props.die_id) {
659 continue;
662 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
663 continue;
666 /* reject assignment if slot is already assigned, for compatibility
667 * of legacy cpu_index mapping with SPAPR core based mapping do not
668 * error out if cpu thread and matched core have the same node-id */
669 if (slot->props.has_node_id &&
670 slot->props.node_id != props->node_id) {
671 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
672 slot->props.node_id);
673 return;
676 /* assign slot to node as it's matched '-numa cpu' key */
677 match = true;
678 slot->props.node_id = props->node_id;
679 slot->props.has_node_id = props->has_node_id;
681 if (machine->numa_state->hmat_enabled) {
682 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
683 (props->node_id != numa_info[props->node_id].initiator)) {
684 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
685 " should be itself", props->node_id);
686 return;
688 numa_info[props->node_id].has_cpu = true;
689 numa_info[props->node_id].initiator = props->node_id;
693 if (!match) {
694 error_setg(errp, "no match found");
698 static void smp_parse(MachineState *ms, QemuOpts *opts)
700 if (opts) {
701 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
702 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
703 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
704 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
706 /* compute missing values, prefer sockets over cores over threads */
707 if (cpus == 0 || sockets == 0) {
708 cores = cores > 0 ? cores : 1;
709 threads = threads > 0 ? threads : 1;
710 if (cpus == 0) {
711 sockets = sockets > 0 ? sockets : 1;
712 cpus = cores * threads * sockets;
713 } else {
714 ms->smp.max_cpus =
715 qemu_opt_get_number(opts, "maxcpus", cpus);
716 sockets = ms->smp.max_cpus / (cores * threads);
718 } else if (cores == 0) {
719 threads = threads > 0 ? threads : 1;
720 cores = cpus / (sockets * threads);
721 cores = cores > 0 ? cores : 1;
722 } else if (threads == 0) {
723 threads = cpus / (cores * sockets);
724 threads = threads > 0 ? threads : 1;
725 } else if (sockets * cores * threads < cpus) {
726 error_report("cpu topology: "
727 "sockets (%u) * cores (%u) * threads (%u) < "
728 "smp_cpus (%u)",
729 sockets, cores, threads, cpus);
730 exit(1);
733 ms->smp.max_cpus =
734 qemu_opt_get_number(opts, "maxcpus", cpus);
736 if (ms->smp.max_cpus < cpus) {
737 error_report("maxcpus must be equal to or greater than smp");
738 exit(1);
741 if (sockets * cores * threads != ms->smp.max_cpus) {
742 error_report("Invalid CPU topology: "
743 "sockets (%u) * cores (%u) * threads (%u) "
744 "!= maxcpus (%u)",
745 sockets, cores, threads,
746 ms->smp.max_cpus);
747 exit(1);
750 ms->smp.cpus = cpus;
751 ms->smp.cores = cores;
752 ms->smp.threads = threads;
753 ms->smp.sockets = sockets;
756 if (ms->smp.cpus > 1) {
757 Error *blocker = NULL;
758 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
759 replay_add_blocker(blocker);
763 static void machine_class_init(ObjectClass *oc, void *data)
765 MachineClass *mc = MACHINE_CLASS(oc);
767 /* Default 128 MB as guest ram size */
768 mc->default_ram_size = 128 * MiB;
769 mc->rom_file_has_mr = true;
770 mc->smp_parse = smp_parse;
772 /* numa node memory size aligned on 8MB by default.
773 * On Linux, each node's border has to be 8MB aligned
775 mc->numa_mem_align_shift = 23;
777 object_class_property_add_str(oc, "kernel",
778 machine_get_kernel, machine_set_kernel);
779 object_class_property_set_description(oc, "kernel",
780 "Linux kernel image file");
782 object_class_property_add_str(oc, "initrd",
783 machine_get_initrd, machine_set_initrd);
784 object_class_property_set_description(oc, "initrd",
785 "Linux initial ramdisk file");
787 object_class_property_add_str(oc, "append",
788 machine_get_append, machine_set_append);
789 object_class_property_set_description(oc, "append",
790 "Linux kernel command line");
792 object_class_property_add_str(oc, "dtb",
793 machine_get_dtb, machine_set_dtb);
794 object_class_property_set_description(oc, "dtb",
795 "Linux kernel device tree file");
797 object_class_property_add_str(oc, "dumpdtb",
798 machine_get_dumpdtb, machine_set_dumpdtb);
799 object_class_property_set_description(oc, "dumpdtb",
800 "Dump current dtb to a file and quit");
802 object_class_property_add(oc, "phandle-start", "int",
803 machine_get_phandle_start, machine_set_phandle_start,
804 NULL, NULL);
805 object_class_property_set_description(oc, "phandle-start",
806 "The first phandle ID we may generate dynamically");
808 object_class_property_add_str(oc, "dt-compatible",
809 machine_get_dt_compatible, machine_set_dt_compatible);
810 object_class_property_set_description(oc, "dt-compatible",
811 "Overrides the \"compatible\" property of the dt root node");
813 object_class_property_add_bool(oc, "dump-guest-core",
814 machine_get_dump_guest_core, machine_set_dump_guest_core);
815 object_class_property_set_description(oc, "dump-guest-core",
816 "Include guest memory in a core dump");
818 object_class_property_add_bool(oc, "mem-merge",
819 machine_get_mem_merge, machine_set_mem_merge);
820 object_class_property_set_description(oc, "mem-merge",
821 "Enable/disable memory merge support");
823 object_class_property_add_bool(oc, "usb",
824 machine_get_usb, machine_set_usb);
825 object_class_property_set_description(oc, "usb",
826 "Set on/off to enable/disable usb");
828 object_class_property_add_bool(oc, "graphics",
829 machine_get_graphics, machine_set_graphics);
830 object_class_property_set_description(oc, "graphics",
831 "Set on/off to enable/disable graphics emulation");
833 object_class_property_add_str(oc, "firmware",
834 machine_get_firmware, machine_set_firmware);
835 object_class_property_set_description(oc, "firmware",
836 "Firmware image");
838 object_class_property_add_bool(oc, "suppress-vmdesc",
839 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
840 object_class_property_set_description(oc, "suppress-vmdesc",
841 "Set on to disable self-describing migration");
843 object_class_property_add_str(oc, "memory-encryption",
844 machine_get_memory_encryption, machine_set_memory_encryption);
845 object_class_property_set_description(oc, "memory-encryption",
846 "Set memory encryption object to use");
848 object_class_property_add_str(oc, "memory-backend",
849 machine_get_memdev, machine_set_memdev);
850 object_class_property_set_description(oc, "memory-backend",
851 "Set RAM backend"
852 "Valid value is ID of hostmem based backend");
855 static void machine_class_base_init(ObjectClass *oc, void *data)
857 MachineClass *mc = MACHINE_CLASS(oc);
858 mc->max_cpus = mc->max_cpus ?: 1;
859 mc->min_cpus = mc->min_cpus ?: 1;
860 mc->default_cpus = mc->default_cpus ?: 1;
862 if (!object_class_is_abstract(oc)) {
863 const char *cname = object_class_get_name(oc);
864 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
865 mc->name = g_strndup(cname,
866 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
867 mc->compat_props = g_ptr_array_new();
871 static void machine_initfn(Object *obj)
873 MachineState *ms = MACHINE(obj);
874 MachineClass *mc = MACHINE_GET_CLASS(obj);
876 ms->dump_guest_core = true;
877 ms->mem_merge = true;
878 ms->enable_graphics = true;
880 if (mc->nvdimm_supported) {
881 Object *obj = OBJECT(ms);
883 ms->nvdimms_state = g_new0(NVDIMMState, 1);
884 object_property_add_bool(obj, "nvdimm",
885 machine_get_nvdimm, machine_set_nvdimm);
886 object_property_set_description(obj, "nvdimm",
887 "Set on/off to enable/disable "
888 "NVDIMM instantiation");
890 object_property_add_str(obj, "nvdimm-persistence",
891 machine_get_nvdimm_persistence,
892 machine_set_nvdimm_persistence);
893 object_property_set_description(obj, "nvdimm-persistence",
894 "Set NVDIMM persistence"
895 "Valid values are cpu, mem-ctrl");
898 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
899 ms->numa_state = g_new0(NumaState, 1);
900 object_property_add_bool(obj, "hmat",
901 machine_get_hmat, machine_set_hmat);
902 object_property_set_description(obj, "hmat",
903 "Set on/off to enable/disable "
904 "ACPI Heterogeneous Memory Attribute "
905 "Table (HMAT)");
908 /* Register notifier when init is done for sysbus sanity checks */
909 ms->sysbus_notifier.notify = machine_init_notify;
910 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
912 /* default to mc->default_cpus */
913 ms->smp.cpus = mc->default_cpus;
914 ms->smp.max_cpus = mc->default_cpus;
915 ms->smp.cores = 1;
916 ms->smp.threads = 1;
917 ms->smp.sockets = 1;
920 static void machine_finalize(Object *obj)
922 MachineState *ms = MACHINE(obj);
924 g_free(ms->kernel_filename);
925 g_free(ms->initrd_filename);
926 g_free(ms->kernel_cmdline);
927 g_free(ms->dtb);
928 g_free(ms->dumpdtb);
929 g_free(ms->dt_compatible);
930 g_free(ms->firmware);
931 g_free(ms->device_memory);
932 g_free(ms->nvdimms_state);
933 g_free(ms->numa_state);
936 bool machine_usb(MachineState *machine)
938 return machine->usb;
941 int machine_phandle_start(MachineState *machine)
943 return machine->phandle_start;
946 bool machine_dump_guest_core(MachineState *machine)
948 return machine->dump_guest_core;
951 bool machine_mem_merge(MachineState *machine)
953 return machine->mem_merge;
956 static char *cpu_slot_to_string(const CPUArchId *cpu)
958 GString *s = g_string_new(NULL);
959 if (cpu->props.has_socket_id) {
960 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
962 if (cpu->props.has_die_id) {
963 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
965 if (cpu->props.has_core_id) {
966 if (s->len) {
967 g_string_append_printf(s, ", ");
969 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
971 if (cpu->props.has_thread_id) {
972 if (s->len) {
973 g_string_append_printf(s, ", ");
975 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
977 return g_string_free(s, false);
980 static void numa_validate_initiator(NumaState *numa_state)
982 int i;
983 NodeInfo *numa_info = numa_state->nodes;
985 for (i = 0; i < numa_state->num_nodes; i++) {
986 if (numa_info[i].initiator == MAX_NODES) {
987 error_report("The initiator of NUMA node %d is missing, use "
988 "'-numa node,initiator' option to declare it", i);
989 exit(1);
992 if (!numa_info[numa_info[i].initiator].present) {
993 error_report("NUMA node %" PRIu16 " is missing, use "
994 "'-numa node' option to declare it first",
995 numa_info[i].initiator);
996 exit(1);
999 if (!numa_info[numa_info[i].initiator].has_cpu) {
1000 error_report("The initiator of NUMA node %d is invalid", i);
1001 exit(1);
1006 static void machine_numa_finish_cpu_init(MachineState *machine)
1008 int i;
1009 bool default_mapping;
1010 GString *s = g_string_new(NULL);
1011 MachineClass *mc = MACHINE_GET_CLASS(machine);
1012 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1014 assert(machine->numa_state->num_nodes);
1015 for (i = 0; i < possible_cpus->len; i++) {
1016 if (possible_cpus->cpus[i].props.has_node_id) {
1017 break;
1020 default_mapping = (i == possible_cpus->len);
1022 for (i = 0; i < possible_cpus->len; i++) {
1023 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1025 if (!cpu_slot->props.has_node_id) {
1026 /* fetch default mapping from board and enable it */
1027 CpuInstanceProperties props = cpu_slot->props;
1029 props.node_id = mc->get_default_cpu_node_id(machine, i);
1030 if (!default_mapping) {
1031 /* record slots with not set mapping,
1032 * TODO: make it hard error in future */
1033 char *cpu_str = cpu_slot_to_string(cpu_slot);
1034 g_string_append_printf(s, "%sCPU %d [%s]",
1035 s->len ? ", " : "", i, cpu_str);
1036 g_free(cpu_str);
1038 /* non mapped cpus used to fallback to node 0 */
1039 props.node_id = 0;
1042 props.has_node_id = true;
1043 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1047 if (machine->numa_state->hmat_enabled) {
1048 numa_validate_initiator(machine->numa_state);
1051 if (s->len && !qtest_enabled()) {
1052 warn_report("CPU(s) not present in any NUMA nodes: %s",
1053 s->str);
1054 warn_report("All CPU(s) up to maxcpus should be described "
1055 "in NUMA config, ability to start up with partial NUMA "
1056 "mappings is obsoleted and will be removed in future");
1058 g_string_free(s, true);
1061 MemoryRegion *machine_consume_memdev(MachineState *machine,
1062 HostMemoryBackend *backend)
1064 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1066 if (memory_region_is_mapped(ret)) {
1067 error_report("memory backend %s can't be used multiple times.",
1068 object_get_canonical_path_component(OBJECT(backend)));
1069 exit(EXIT_FAILURE);
1071 host_memory_backend_set_mapped(backend, true);
1072 vmstate_register_ram_global(ret);
1073 return ret;
1076 void machine_run_board_init(MachineState *machine)
1078 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1079 ObjectClass *oc = object_class_by_name(machine->cpu_type);
1080 CPUClass *cc;
1082 if (machine->ram_memdev_id) {
1083 Object *o;
1084 o = object_resolve_path_type(machine->ram_memdev_id,
1085 TYPE_MEMORY_BACKEND, NULL);
1086 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1089 if (machine->numa_state) {
1090 numa_complete_configuration(machine);
1091 if (machine->numa_state->num_nodes) {
1092 machine_numa_finish_cpu_init(machine);
1096 /* If the machine supports the valid_cpu_types check and the user
1097 * specified a CPU with -cpu check here that the user CPU is supported.
1099 if (machine_class->valid_cpu_types && machine->cpu_type) {
1100 int i;
1102 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1103 if (object_class_dynamic_cast(oc,
1104 machine_class->valid_cpu_types[i])) {
1105 /* The user specificed CPU is in the valid field, we are
1106 * good to go.
1108 break;
1112 if (!machine_class->valid_cpu_types[i]) {
1113 /* The user specified CPU is not valid */
1114 error_report("Invalid CPU type: %s", machine->cpu_type);
1115 error_printf("The valid types are: %s",
1116 machine_class->valid_cpu_types[0]);
1117 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1118 error_printf(", %s", machine_class->valid_cpu_types[i]);
1120 error_printf("\n");
1122 exit(1);
1126 /* Check if CPU type is deprecated and warn if so */
1127 cc = CPU_CLASS(oc);
1128 if (cc && cc->deprecation_note) {
1129 warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1130 cc->deprecation_note);
1133 machine_class->init(machine);
1136 static const TypeInfo machine_info = {
1137 .name = TYPE_MACHINE,
1138 .parent = TYPE_OBJECT,
1139 .abstract = true,
1140 .class_size = sizeof(MachineClass),
1141 .class_init = machine_class_init,
1142 .class_base_init = machine_class_base_init,
1143 .instance_size = sizeof(MachineState),
1144 .instance_init = machine_initfn,
1145 .instance_finalize = machine_finalize,
1148 static void machine_register_types(void)
1150 type_register_static(&machine_info);
1153 type_init(machine_register_types)