Merge remote-tracking branch 'remotes/pmaydell/tags/pull-target-arm-20210406' into...
[qemu.git] / hw / core / machine.c
blob40def78183a7675e60ae81cef4f8fb6e876c931d
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 "hw/loader.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-visit-common.h"
22 #include "qapi/visitor.h"
23 #include "hw/sysbus.h"
24 #include "sysemu/cpus.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/reset.h"
27 #include "sysemu/runstate.h"
28 #include "sysemu/numa.h"
29 #include "qemu/error-report.h"
30 #include "sysemu/qtest.h"
31 #include "hw/pci/pci.h"
32 #include "hw/mem/nvdimm.h"
33 #include "migration/global_state.h"
34 #include "migration/vmstate.h"
35 #include "exec/confidential-guest-support.h"
36 #include "hw/virtio/virtio.h"
37 #include "hw/virtio/virtio-pci.h"
39 GlobalProperty hw_compat_5_2[] = {
40 { "ICH9-LPC", "smm-compat", "on"},
41 { "PIIX4_PM", "smm-compat", "on"},
42 { "virtio-blk-device", "report-discard-granularity", "off" },
43 { "virtio-net-pci", "vectors", "3"},
45 const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
47 GlobalProperty hw_compat_5_1[] = {
48 { "vhost-scsi", "num_queues", "1"},
49 { "vhost-user-blk", "num-queues", "1"},
50 { "vhost-user-scsi", "num_queues", "1"},
51 { "virtio-blk-device", "num-queues", "1"},
52 { "virtio-scsi-device", "num_queues", "1"},
53 { "nvme", "use-intel-id", "on"},
54 { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
55 { "pl011", "migrate-clk", "off" },
56 { "virtio-pci", "x-ats-page-aligned", "off"},
58 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
60 GlobalProperty hw_compat_5_0[] = {
61 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
62 { "virtio-balloon-device", "page-poison", "false" },
63 { "vmport", "x-read-set-eax", "off" },
64 { "vmport", "x-signal-unsupported-cmd", "off" },
65 { "vmport", "x-report-vmx-type", "off" },
66 { "vmport", "x-cmds-v2", "off" },
67 { "virtio-device", "x-disable-legacy-check", "true" },
69 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
71 GlobalProperty hw_compat_4_2[] = {
72 { "virtio-blk-device", "queue-size", "128"},
73 { "virtio-scsi-device", "virtqueue_size", "128"},
74 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
75 { "virtio-blk-device", "seg-max-adjust", "off"},
76 { "virtio-scsi-device", "seg_max_adjust", "off"},
77 { "vhost-blk-device", "seg_max_adjust", "off"},
78 { "usb-host", "suppress-remote-wake", "off" },
79 { "usb-redir", "suppress-remote-wake", "off" },
80 { "qxl", "revision", "4" },
81 { "qxl-vga", "revision", "4" },
82 { "fw_cfg", "acpi-mr-restore", "false" },
83 { "virtio-device", "use-disabled-flag", "false" },
85 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
87 GlobalProperty hw_compat_4_1[] = {
88 { "virtio-pci", "x-pcie-flr-init", "off" },
90 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
92 GlobalProperty hw_compat_4_0[] = {
93 { "VGA", "edid", "false" },
94 { "secondary-vga", "edid", "false" },
95 { "bochs-display", "edid", "false" },
96 { "virtio-vga", "edid", "false" },
97 { "virtio-gpu-device", "edid", "false" },
98 { "virtio-device", "use-started", "false" },
99 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
100 { "pl031", "migrate-tick-offset", "false" },
102 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
104 GlobalProperty hw_compat_3_1[] = {
105 { "pcie-root-port", "x-speed", "2_5" },
106 { "pcie-root-port", "x-width", "1" },
107 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
108 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
109 { "tpm-crb", "ppi", "false" },
110 { "tpm-tis", "ppi", "false" },
111 { "usb-kbd", "serial", "42" },
112 { "usb-mouse", "serial", "42" },
113 { "usb-tablet", "serial", "42" },
114 { "virtio-blk-device", "discard", "false" },
115 { "virtio-blk-device", "write-zeroes", "false" },
116 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
117 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
119 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
121 GlobalProperty hw_compat_3_0[] = {};
122 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
124 GlobalProperty hw_compat_2_12[] = {
125 { "migration", "decompress-error-check", "off" },
126 { "hda-audio", "use-timer", "false" },
127 { "cirrus-vga", "global-vmstate", "true" },
128 { "VGA", "global-vmstate", "true" },
129 { "vmware-svga", "global-vmstate", "true" },
130 { "qxl-vga", "global-vmstate", "true" },
132 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
134 GlobalProperty hw_compat_2_11[] = {
135 { "hpet", "hpet-offset-saved", "false" },
136 { "virtio-blk-pci", "vectors", "2" },
137 { "vhost-user-blk-pci", "vectors", "2" },
138 { "e1000", "migrate_tso_props", "off" },
140 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
142 GlobalProperty hw_compat_2_10[] = {
143 { "virtio-mouse-device", "wheel-axis", "false" },
144 { "virtio-tablet-device", "wheel-axis", "false" },
146 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
148 GlobalProperty hw_compat_2_9[] = {
149 { "pci-bridge", "shpc", "off" },
150 { "intel-iommu", "pt", "off" },
151 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
152 { "pcie-root-port", "x-migrate-msix", "false" },
154 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
156 GlobalProperty hw_compat_2_8[] = {
157 { "fw_cfg_mem", "x-file-slots", "0x10" },
158 { "fw_cfg_io", "x-file-slots", "0x10" },
159 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
160 { "pci-bridge", "shpc", "on" },
161 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
162 { "virtio-pci", "x-pcie-deverr-init", "off" },
163 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
164 { "virtio-pci", "x-pcie-pm-init", "off" },
165 { "cirrus-vga", "vgamem_mb", "8" },
166 { "isa-cirrus-vga", "vgamem_mb", "8" },
168 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
170 GlobalProperty hw_compat_2_7[] = {
171 { "virtio-pci", "page-per-vq", "on" },
172 { "virtio-serial-device", "emergency-write", "off" },
173 { "ioapic", "version", "0x11" },
174 { "intel-iommu", "x-buggy-eim", "true" },
175 { "virtio-pci", "x-ignore-backend-features", "on" },
177 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
179 GlobalProperty hw_compat_2_6[] = {
180 { "virtio-mmio", "format_transport_address", "off" },
181 /* Optional because not all virtio-pci devices support legacy mode */
182 { "virtio-pci", "disable-modern", "on", .optional = true },
183 { "virtio-pci", "disable-legacy", "off", .optional = true },
185 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
187 GlobalProperty hw_compat_2_5[] = {
188 { "isa-fdc", "fallback", "144" },
189 { "pvscsi", "x-old-pci-configuration", "on" },
190 { "pvscsi", "x-disable-pcie", "on" },
191 { "vmxnet3", "x-old-msi-offsets", "on" },
192 { "vmxnet3", "x-disable-pcie", "on" },
194 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
196 GlobalProperty hw_compat_2_4[] = {
197 /* Optional because the 'scsi' property is Linux-only */
198 { "virtio-blk-device", "scsi", "true", .optional = true },
199 { "e1000", "extra_mac_registers", "off" },
200 { "virtio-pci", "x-disable-pcie", "on" },
201 { "virtio-pci", "migrate-extra", "off" },
202 { "fw_cfg_mem", "dma_enabled", "off" },
203 { "fw_cfg_io", "dma_enabled", "off" }
205 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
207 GlobalProperty hw_compat_2_3[] = {
208 { "virtio-blk-pci", "any_layout", "off" },
209 { "virtio-balloon-pci", "any_layout", "off" },
210 { "virtio-serial-pci", "any_layout", "off" },
211 { "virtio-9p-pci", "any_layout", "off" },
212 { "virtio-rng-pci", "any_layout", "off" },
213 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
214 { "migration", "send-configuration", "off" },
215 { "migration", "send-section-footer", "off" },
216 { "migration", "store-global-state", "off" },
218 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
220 GlobalProperty hw_compat_2_2[] = {};
221 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
223 GlobalProperty hw_compat_2_1[] = {
224 { "intel-hda", "old_msi_addr", "on" },
225 { "VGA", "qemu-extended-regs", "off" },
226 { "secondary-vga", "qemu-extended-regs", "off" },
227 { "virtio-scsi-pci", "any_layout", "off" },
228 { "usb-mouse", "usb_version", "1" },
229 { "usb-kbd", "usb_version", "1" },
230 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
232 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
234 MachineState *current_machine;
236 static char *machine_get_kernel(Object *obj, Error **errp)
238 MachineState *ms = MACHINE(obj);
240 return g_strdup(ms->kernel_filename);
243 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
245 MachineState *ms = MACHINE(obj);
247 g_free(ms->kernel_filename);
248 ms->kernel_filename = g_strdup(value);
251 static char *machine_get_initrd(Object *obj, Error **errp)
253 MachineState *ms = MACHINE(obj);
255 return g_strdup(ms->initrd_filename);
258 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
260 MachineState *ms = MACHINE(obj);
262 g_free(ms->initrd_filename);
263 ms->initrd_filename = g_strdup(value);
266 static char *machine_get_append(Object *obj, Error **errp)
268 MachineState *ms = MACHINE(obj);
270 return g_strdup(ms->kernel_cmdline);
273 static void machine_set_append(Object *obj, const char *value, Error **errp)
275 MachineState *ms = MACHINE(obj);
277 g_free(ms->kernel_cmdline);
278 ms->kernel_cmdline = g_strdup(value);
281 static char *machine_get_dtb(Object *obj, Error **errp)
283 MachineState *ms = MACHINE(obj);
285 return g_strdup(ms->dtb);
288 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
290 MachineState *ms = MACHINE(obj);
292 g_free(ms->dtb);
293 ms->dtb = g_strdup(value);
296 static char *machine_get_dumpdtb(Object *obj, Error **errp)
298 MachineState *ms = MACHINE(obj);
300 return g_strdup(ms->dumpdtb);
303 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
305 MachineState *ms = MACHINE(obj);
307 g_free(ms->dumpdtb);
308 ms->dumpdtb = g_strdup(value);
311 static void machine_get_phandle_start(Object *obj, Visitor *v,
312 const char *name, void *opaque,
313 Error **errp)
315 MachineState *ms = MACHINE(obj);
316 int64_t value = ms->phandle_start;
318 visit_type_int(v, name, &value, errp);
321 static void machine_set_phandle_start(Object *obj, Visitor *v,
322 const char *name, void *opaque,
323 Error **errp)
325 MachineState *ms = MACHINE(obj);
326 int64_t value;
328 if (!visit_type_int(v, name, &value, errp)) {
329 return;
332 ms->phandle_start = value;
335 static char *machine_get_dt_compatible(Object *obj, Error **errp)
337 MachineState *ms = MACHINE(obj);
339 return g_strdup(ms->dt_compatible);
342 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
344 MachineState *ms = MACHINE(obj);
346 g_free(ms->dt_compatible);
347 ms->dt_compatible = g_strdup(value);
350 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
352 MachineState *ms = MACHINE(obj);
354 return ms->dump_guest_core;
357 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
359 MachineState *ms = MACHINE(obj);
361 ms->dump_guest_core = value;
364 static bool machine_get_mem_merge(Object *obj, Error **errp)
366 MachineState *ms = MACHINE(obj);
368 return ms->mem_merge;
371 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
373 MachineState *ms = MACHINE(obj);
375 ms->mem_merge = value;
378 static bool machine_get_usb(Object *obj, Error **errp)
380 MachineState *ms = MACHINE(obj);
382 return ms->usb;
385 static void machine_set_usb(Object *obj, bool value, Error **errp)
387 MachineState *ms = MACHINE(obj);
389 ms->usb = value;
390 ms->usb_disabled = !value;
393 static bool machine_get_graphics(Object *obj, Error **errp)
395 MachineState *ms = MACHINE(obj);
397 return ms->enable_graphics;
400 static void machine_set_graphics(Object *obj, bool value, Error **errp)
402 MachineState *ms = MACHINE(obj);
404 ms->enable_graphics = value;
407 static char *machine_get_firmware(Object *obj, Error **errp)
409 MachineState *ms = MACHINE(obj);
411 return g_strdup(ms->firmware);
414 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
416 MachineState *ms = MACHINE(obj);
418 g_free(ms->firmware);
419 ms->firmware = g_strdup(value);
422 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
424 MachineState *ms = MACHINE(obj);
426 ms->suppress_vmdesc = value;
429 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
431 MachineState *ms = MACHINE(obj);
433 return ms->suppress_vmdesc;
436 static char *machine_get_memory_encryption(Object *obj, Error **errp)
438 MachineState *ms = MACHINE(obj);
440 if (ms->cgs) {
441 return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
444 return NULL;
447 static void machine_set_memory_encryption(Object *obj, const char *value,
448 Error **errp)
450 Object *cgs =
451 object_resolve_path_component(object_get_objects_root(), value);
453 if (!cgs) {
454 error_setg(errp, "No such memory encryption object '%s'", value);
455 return;
458 object_property_set_link(obj, "confidential-guest-support", cgs, errp);
461 static void machine_check_confidential_guest_support(const Object *obj,
462 const char *name,
463 Object *new_target,
464 Error **errp)
467 * So far the only constraint is that the target has the
468 * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
469 * by the QOM core
473 static bool machine_get_nvdimm(Object *obj, Error **errp)
475 MachineState *ms = MACHINE(obj);
477 return ms->nvdimms_state->is_enabled;
480 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
482 MachineState *ms = MACHINE(obj);
484 ms->nvdimms_state->is_enabled = value;
487 static bool machine_get_hmat(Object *obj, Error **errp)
489 MachineState *ms = MACHINE(obj);
491 return ms->numa_state->hmat_enabled;
494 static void machine_set_hmat(Object *obj, bool value, Error **errp)
496 MachineState *ms = MACHINE(obj);
498 ms->numa_state->hmat_enabled = value;
501 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
503 MachineState *ms = MACHINE(obj);
505 return g_strdup(ms->nvdimms_state->persistence_string);
508 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
509 Error **errp)
511 MachineState *ms = MACHINE(obj);
512 NVDIMMState *nvdimms_state = ms->nvdimms_state;
514 if (strcmp(value, "cpu") == 0) {
515 nvdimms_state->persistence = 3;
516 } else if (strcmp(value, "mem-ctrl") == 0) {
517 nvdimms_state->persistence = 2;
518 } else {
519 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
520 value);
521 return;
524 g_free(nvdimms_state->persistence_string);
525 nvdimms_state->persistence_string = g_strdup(value);
528 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
530 QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
533 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
535 bool allowed = false;
536 strList *wl;
537 Object *obj = OBJECT(dev);
539 if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
540 return false;
543 for (wl = mc->allowed_dynamic_sysbus_devices;
544 !allowed && wl;
545 wl = wl->next) {
546 allowed |= !!object_dynamic_cast(obj, wl->value);
549 return allowed;
552 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
554 MachineState *machine = opaque;
555 MachineClass *mc = MACHINE_GET_CLASS(machine);
557 if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) {
558 error_report("Option '-device %s' cannot be handled by this machine",
559 object_class_get_name(object_get_class(OBJECT(sbdev))));
560 exit(1);
564 static char *machine_get_memdev(Object *obj, Error **errp)
566 MachineState *ms = MACHINE(obj);
568 return g_strdup(ms->ram_memdev_id);
571 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
573 MachineState *ms = MACHINE(obj);
575 g_free(ms->ram_memdev_id);
576 ms->ram_memdev_id = g_strdup(value);
580 static void machine_init_notify(Notifier *notifier, void *data)
582 MachineState *machine = MACHINE(qdev_get_machine());
585 * Loop through all dynamically created sysbus devices and check if they are
586 * all allowed. If a device is not allowed, error out.
588 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
591 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
593 int i;
594 HotpluggableCPUList *head = NULL;
595 MachineClass *mc = MACHINE_GET_CLASS(machine);
597 /* force board to initialize possible_cpus if it hasn't been done yet */
598 mc->possible_cpu_arch_ids(machine);
600 for (i = 0; i < machine->possible_cpus->len; i++) {
601 Object *cpu;
602 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
604 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
605 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
606 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
607 sizeof(*cpu_item->props));
609 cpu = machine->possible_cpus->cpus[i].cpu;
610 if (cpu) {
611 cpu_item->has_qom_path = true;
612 cpu_item->qom_path = object_get_canonical_path(cpu);
614 QAPI_LIST_PREPEND(head, cpu_item);
616 return head;
620 * machine_set_cpu_numa_node:
621 * @machine: machine object to modify
622 * @props: specifies which cpu objects to assign to
623 * numa node specified by @props.node_id
624 * @errp: if an error occurs, a pointer to an area to store the error
626 * Associate NUMA node specified by @props.node_id with cpu slots that
627 * match socket/core/thread-ids specified by @props. It's recommended to use
628 * query-hotpluggable-cpus.props values to specify affected cpu slots,
629 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
631 * However for CLI convenience it's possible to pass in subset of properties,
632 * which would affect all cpu slots that match it.
633 * Ex for pc machine:
634 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
635 * -numa cpu,node-id=0,socket_id=0 \
636 * -numa cpu,node-id=1,socket_id=1
637 * will assign all child cores of socket 0 to node 0 and
638 * of socket 1 to node 1.
640 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
641 * return error.
642 * Empty subset is disallowed and function will return with error in this case.
644 void machine_set_cpu_numa_node(MachineState *machine,
645 const CpuInstanceProperties *props, Error **errp)
647 MachineClass *mc = MACHINE_GET_CLASS(machine);
648 NodeInfo *numa_info = machine->numa_state->nodes;
649 bool match = false;
650 int i;
652 if (!mc->possible_cpu_arch_ids) {
653 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
654 return;
657 /* disabling node mapping is not supported, forbid it */
658 assert(props->has_node_id);
660 /* force board to initialize possible_cpus if it hasn't been done yet */
661 mc->possible_cpu_arch_ids(machine);
663 for (i = 0; i < machine->possible_cpus->len; i++) {
664 CPUArchId *slot = &machine->possible_cpus->cpus[i];
666 /* reject unsupported by board properties */
667 if (props->has_thread_id && !slot->props.has_thread_id) {
668 error_setg(errp, "thread-id is not supported");
669 return;
672 if (props->has_core_id && !slot->props.has_core_id) {
673 error_setg(errp, "core-id is not supported");
674 return;
677 if (props->has_socket_id && !slot->props.has_socket_id) {
678 error_setg(errp, "socket-id is not supported");
679 return;
682 if (props->has_die_id && !slot->props.has_die_id) {
683 error_setg(errp, "die-id is not supported");
684 return;
687 /* skip slots with explicit mismatch */
688 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
689 continue;
692 if (props->has_core_id && props->core_id != slot->props.core_id) {
693 continue;
696 if (props->has_die_id && props->die_id != slot->props.die_id) {
697 continue;
700 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
701 continue;
704 /* reject assignment if slot is already assigned, for compatibility
705 * of legacy cpu_index mapping with SPAPR core based mapping do not
706 * error out if cpu thread and matched core have the same node-id */
707 if (slot->props.has_node_id &&
708 slot->props.node_id != props->node_id) {
709 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
710 slot->props.node_id);
711 return;
714 /* assign slot to node as it's matched '-numa cpu' key */
715 match = true;
716 slot->props.node_id = props->node_id;
717 slot->props.has_node_id = props->has_node_id;
719 if (machine->numa_state->hmat_enabled) {
720 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
721 (props->node_id != numa_info[props->node_id].initiator)) {
722 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
723 " should be itself", props->node_id);
724 return;
726 numa_info[props->node_id].has_cpu = true;
727 numa_info[props->node_id].initiator = props->node_id;
731 if (!match) {
732 error_setg(errp, "no match found");
736 static void smp_parse(MachineState *ms, QemuOpts *opts)
738 if (opts) {
739 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
740 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
741 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
742 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
744 /* compute missing values, prefer sockets over cores over threads */
745 if (cpus == 0 || sockets == 0) {
746 cores = cores > 0 ? cores : 1;
747 threads = threads > 0 ? threads : 1;
748 if (cpus == 0) {
749 sockets = sockets > 0 ? sockets : 1;
750 cpus = cores * threads * sockets;
751 } else {
752 ms->smp.max_cpus =
753 qemu_opt_get_number(opts, "maxcpus", cpus);
754 sockets = ms->smp.max_cpus / (cores * threads);
756 } else if (cores == 0) {
757 threads = threads > 0 ? threads : 1;
758 cores = cpus / (sockets * threads);
759 cores = cores > 0 ? cores : 1;
760 } else if (threads == 0) {
761 threads = cpus / (cores * sockets);
762 threads = threads > 0 ? threads : 1;
763 } else if (sockets * cores * threads < cpus) {
764 error_report("cpu topology: "
765 "sockets (%u) * cores (%u) * threads (%u) < "
766 "smp_cpus (%u)",
767 sockets, cores, threads, cpus);
768 exit(1);
771 ms->smp.max_cpus =
772 qemu_opt_get_number(opts, "maxcpus", cpus);
774 if (ms->smp.max_cpus < cpus) {
775 error_report("maxcpus must be equal to or greater than smp");
776 exit(1);
779 if (sockets * cores * threads != ms->smp.max_cpus) {
780 error_report("Invalid CPU topology: "
781 "sockets (%u) * cores (%u) * threads (%u) "
782 "!= maxcpus (%u)",
783 sockets, cores, threads,
784 ms->smp.max_cpus);
785 exit(1);
788 ms->smp.cpus = cpus;
789 ms->smp.cores = cores;
790 ms->smp.threads = threads;
791 ms->smp.sockets = sockets;
794 if (ms->smp.cpus > 1) {
795 Error *blocker = NULL;
796 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
797 replay_add_blocker(blocker);
801 static void machine_class_init(ObjectClass *oc, void *data)
803 MachineClass *mc = MACHINE_CLASS(oc);
805 /* Default 128 MB as guest ram size */
806 mc->default_ram_size = 128 * MiB;
807 mc->rom_file_has_mr = true;
808 mc->smp_parse = smp_parse;
810 /* numa node memory size aligned on 8MB by default.
811 * On Linux, each node's border has to be 8MB aligned
813 mc->numa_mem_align_shift = 23;
815 object_class_property_add_str(oc, "kernel",
816 machine_get_kernel, machine_set_kernel);
817 object_class_property_set_description(oc, "kernel",
818 "Linux kernel image file");
820 object_class_property_add_str(oc, "initrd",
821 machine_get_initrd, machine_set_initrd);
822 object_class_property_set_description(oc, "initrd",
823 "Linux initial ramdisk file");
825 object_class_property_add_str(oc, "append",
826 machine_get_append, machine_set_append);
827 object_class_property_set_description(oc, "append",
828 "Linux kernel command line");
830 object_class_property_add_str(oc, "dtb",
831 machine_get_dtb, machine_set_dtb);
832 object_class_property_set_description(oc, "dtb",
833 "Linux kernel device tree file");
835 object_class_property_add_str(oc, "dumpdtb",
836 machine_get_dumpdtb, machine_set_dumpdtb);
837 object_class_property_set_description(oc, "dumpdtb",
838 "Dump current dtb to a file and quit");
840 object_class_property_add(oc, "phandle-start", "int",
841 machine_get_phandle_start, machine_set_phandle_start,
842 NULL, NULL);
843 object_class_property_set_description(oc, "phandle-start",
844 "The first phandle ID we may generate dynamically");
846 object_class_property_add_str(oc, "dt-compatible",
847 machine_get_dt_compatible, machine_set_dt_compatible);
848 object_class_property_set_description(oc, "dt-compatible",
849 "Overrides the \"compatible\" property of the dt root node");
851 object_class_property_add_bool(oc, "dump-guest-core",
852 machine_get_dump_guest_core, machine_set_dump_guest_core);
853 object_class_property_set_description(oc, "dump-guest-core",
854 "Include guest memory in a core dump");
856 object_class_property_add_bool(oc, "mem-merge",
857 machine_get_mem_merge, machine_set_mem_merge);
858 object_class_property_set_description(oc, "mem-merge",
859 "Enable/disable memory merge support");
861 object_class_property_add_bool(oc, "usb",
862 machine_get_usb, machine_set_usb);
863 object_class_property_set_description(oc, "usb",
864 "Set on/off to enable/disable usb");
866 object_class_property_add_bool(oc, "graphics",
867 machine_get_graphics, machine_set_graphics);
868 object_class_property_set_description(oc, "graphics",
869 "Set on/off to enable/disable graphics emulation");
871 object_class_property_add_str(oc, "firmware",
872 machine_get_firmware, machine_set_firmware);
873 object_class_property_set_description(oc, "firmware",
874 "Firmware image");
876 object_class_property_add_bool(oc, "suppress-vmdesc",
877 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
878 object_class_property_set_description(oc, "suppress-vmdesc",
879 "Set on to disable self-describing migration");
881 object_class_property_add_link(oc, "confidential-guest-support",
882 TYPE_CONFIDENTIAL_GUEST_SUPPORT,
883 offsetof(MachineState, cgs),
884 machine_check_confidential_guest_support,
885 OBJ_PROP_LINK_STRONG);
886 object_class_property_set_description(oc, "confidential-guest-support",
887 "Set confidential guest scheme to support");
889 /* For compatibility */
890 object_class_property_add_str(oc, "memory-encryption",
891 machine_get_memory_encryption, machine_set_memory_encryption);
892 object_class_property_set_description(oc, "memory-encryption",
893 "Set memory encryption object to use");
895 object_class_property_add_str(oc, "memory-backend",
896 machine_get_memdev, machine_set_memdev);
897 object_class_property_set_description(oc, "memory-backend",
898 "Set RAM backend"
899 "Valid value is ID of hostmem based backend");
902 static void machine_class_base_init(ObjectClass *oc, void *data)
904 MachineClass *mc = MACHINE_CLASS(oc);
905 mc->max_cpus = mc->max_cpus ?: 1;
906 mc->min_cpus = mc->min_cpus ?: 1;
907 mc->default_cpus = mc->default_cpus ?: 1;
909 if (!object_class_is_abstract(oc)) {
910 const char *cname = object_class_get_name(oc);
911 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
912 mc->name = g_strndup(cname,
913 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
914 mc->compat_props = g_ptr_array_new();
918 static void machine_initfn(Object *obj)
920 MachineState *ms = MACHINE(obj);
921 MachineClass *mc = MACHINE_GET_CLASS(obj);
923 container_get(obj, "/peripheral");
924 container_get(obj, "/peripheral-anon");
926 ms->dump_guest_core = true;
927 ms->mem_merge = true;
928 ms->enable_graphics = true;
929 ms->kernel_cmdline = g_strdup("");
931 if (mc->nvdimm_supported) {
932 Object *obj = OBJECT(ms);
934 ms->nvdimms_state = g_new0(NVDIMMState, 1);
935 object_property_add_bool(obj, "nvdimm",
936 machine_get_nvdimm, machine_set_nvdimm);
937 object_property_set_description(obj, "nvdimm",
938 "Set on/off to enable/disable "
939 "NVDIMM instantiation");
941 object_property_add_str(obj, "nvdimm-persistence",
942 machine_get_nvdimm_persistence,
943 machine_set_nvdimm_persistence);
944 object_property_set_description(obj, "nvdimm-persistence",
945 "Set NVDIMM persistence"
946 "Valid values are cpu, mem-ctrl");
949 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
950 ms->numa_state = g_new0(NumaState, 1);
951 object_property_add_bool(obj, "hmat",
952 machine_get_hmat, machine_set_hmat);
953 object_property_set_description(obj, "hmat",
954 "Set on/off to enable/disable "
955 "ACPI Heterogeneous Memory Attribute "
956 "Table (HMAT)");
959 /* Register notifier when init is done for sysbus sanity checks */
960 ms->sysbus_notifier.notify = machine_init_notify;
961 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
963 /* default to mc->default_cpus */
964 ms->smp.cpus = mc->default_cpus;
965 ms->smp.max_cpus = mc->default_cpus;
966 ms->smp.cores = 1;
967 ms->smp.threads = 1;
968 ms->smp.sockets = 1;
971 static void machine_finalize(Object *obj)
973 MachineState *ms = MACHINE(obj);
975 g_free(ms->kernel_filename);
976 g_free(ms->initrd_filename);
977 g_free(ms->kernel_cmdline);
978 g_free(ms->dtb);
979 g_free(ms->dumpdtb);
980 g_free(ms->dt_compatible);
981 g_free(ms->firmware);
982 g_free(ms->device_memory);
983 g_free(ms->nvdimms_state);
984 g_free(ms->numa_state);
987 bool machine_usb(MachineState *machine)
989 return machine->usb;
992 int machine_phandle_start(MachineState *machine)
994 return machine->phandle_start;
997 bool machine_dump_guest_core(MachineState *machine)
999 return machine->dump_guest_core;
1002 bool machine_mem_merge(MachineState *machine)
1004 return machine->mem_merge;
1007 static char *cpu_slot_to_string(const CPUArchId *cpu)
1009 GString *s = g_string_new(NULL);
1010 if (cpu->props.has_socket_id) {
1011 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1013 if (cpu->props.has_die_id) {
1014 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1016 if (cpu->props.has_core_id) {
1017 if (s->len) {
1018 g_string_append_printf(s, ", ");
1020 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1022 if (cpu->props.has_thread_id) {
1023 if (s->len) {
1024 g_string_append_printf(s, ", ");
1026 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1028 return g_string_free(s, false);
1031 static void numa_validate_initiator(NumaState *numa_state)
1033 int i;
1034 NodeInfo *numa_info = numa_state->nodes;
1036 for (i = 0; i < numa_state->num_nodes; i++) {
1037 if (numa_info[i].initiator == MAX_NODES) {
1038 error_report("The initiator of NUMA node %d is missing, use "
1039 "'-numa node,initiator' option to declare it", i);
1040 exit(1);
1043 if (!numa_info[numa_info[i].initiator].present) {
1044 error_report("NUMA node %" PRIu16 " is missing, use "
1045 "'-numa node' option to declare it first",
1046 numa_info[i].initiator);
1047 exit(1);
1050 if (!numa_info[numa_info[i].initiator].has_cpu) {
1051 error_report("The initiator of NUMA node %d is invalid", i);
1052 exit(1);
1057 static void machine_numa_finish_cpu_init(MachineState *machine)
1059 int i;
1060 bool default_mapping;
1061 GString *s = g_string_new(NULL);
1062 MachineClass *mc = MACHINE_GET_CLASS(machine);
1063 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1065 assert(machine->numa_state->num_nodes);
1066 for (i = 0; i < possible_cpus->len; i++) {
1067 if (possible_cpus->cpus[i].props.has_node_id) {
1068 break;
1071 default_mapping = (i == possible_cpus->len);
1073 for (i = 0; i < possible_cpus->len; i++) {
1074 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1076 if (!cpu_slot->props.has_node_id) {
1077 /* fetch default mapping from board and enable it */
1078 CpuInstanceProperties props = cpu_slot->props;
1080 props.node_id = mc->get_default_cpu_node_id(machine, i);
1081 if (!default_mapping) {
1082 /* record slots with not set mapping,
1083 * TODO: make it hard error in future */
1084 char *cpu_str = cpu_slot_to_string(cpu_slot);
1085 g_string_append_printf(s, "%sCPU %d [%s]",
1086 s->len ? ", " : "", i, cpu_str);
1087 g_free(cpu_str);
1089 /* non mapped cpus used to fallback to node 0 */
1090 props.node_id = 0;
1093 props.has_node_id = true;
1094 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1098 if (machine->numa_state->hmat_enabled) {
1099 numa_validate_initiator(machine->numa_state);
1102 if (s->len && !qtest_enabled()) {
1103 warn_report("CPU(s) not present in any NUMA nodes: %s",
1104 s->str);
1105 warn_report("All CPU(s) up to maxcpus should be described "
1106 "in NUMA config, ability to start up with partial NUMA "
1107 "mappings is obsoleted and will be removed in future");
1109 g_string_free(s, true);
1112 MemoryRegion *machine_consume_memdev(MachineState *machine,
1113 HostMemoryBackend *backend)
1115 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1117 if (memory_region_is_mapped(ret)) {
1118 error_report("memory backend %s can't be used multiple times.",
1119 object_get_canonical_path_component(OBJECT(backend)));
1120 exit(EXIT_FAILURE);
1122 host_memory_backend_set_mapped(backend, true);
1123 vmstate_register_ram_global(ret);
1124 return ret;
1127 bool machine_smp_parse(MachineState *ms, QemuOpts *opts, Error **errp)
1129 MachineClass *mc = MACHINE_GET_CLASS(ms);
1131 mc->smp_parse(ms, opts);
1133 /* sanity-check smp_cpus and max_cpus against mc */
1134 if (ms->smp.cpus < mc->min_cpus) {
1135 error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
1136 "supported by machine '%s' is %d",
1137 ms->smp.cpus,
1138 mc->name, mc->min_cpus);
1139 return false;
1140 } else if (ms->smp.max_cpus > mc->max_cpus) {
1141 error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
1142 "supported by machine '%s' is %d",
1143 current_machine->smp.max_cpus,
1144 mc->name, mc->max_cpus);
1145 return false;
1147 return true;
1150 void machine_run_board_init(MachineState *machine)
1152 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1153 ObjectClass *oc = object_class_by_name(machine->cpu_type);
1154 CPUClass *cc;
1156 /* This checkpoint is required by replay to separate prior clock
1157 reading from the other reads, because timer polling functions query
1158 clock values from the log. */
1159 replay_checkpoint(CHECKPOINT_INIT);
1161 if (machine->ram_memdev_id) {
1162 Object *o;
1163 o = object_resolve_path_type(machine->ram_memdev_id,
1164 TYPE_MEMORY_BACKEND, NULL);
1165 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1168 if (machine->numa_state) {
1169 numa_complete_configuration(machine);
1170 if (machine->numa_state->num_nodes) {
1171 machine_numa_finish_cpu_init(machine);
1175 /* If the machine supports the valid_cpu_types check and the user
1176 * specified a CPU with -cpu check here that the user CPU is supported.
1178 if (machine_class->valid_cpu_types && machine->cpu_type) {
1179 int i;
1181 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1182 if (object_class_dynamic_cast(oc,
1183 machine_class->valid_cpu_types[i])) {
1184 /* The user specificed CPU is in the valid field, we are
1185 * good to go.
1187 break;
1191 if (!machine_class->valid_cpu_types[i]) {
1192 /* The user specified CPU is not valid */
1193 error_report("Invalid CPU type: %s", machine->cpu_type);
1194 error_printf("The valid types are: %s",
1195 machine_class->valid_cpu_types[0]);
1196 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1197 error_printf(", %s", machine_class->valid_cpu_types[i]);
1199 error_printf("\n");
1201 exit(1);
1205 /* Check if CPU type is deprecated and warn if so */
1206 cc = CPU_CLASS(oc);
1207 if (cc && cc->deprecation_note) {
1208 warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1209 cc->deprecation_note);
1212 if (machine->cgs) {
1214 * With confidential guests, the host can't see the real
1215 * contents of RAM, so there's no point in it trying to merge
1216 * areas.
1218 machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1221 * Virtio devices can't count on directly accessing guest
1222 * memory, so they need iommu_platform=on to use normal DMA
1223 * mechanisms. That requires also disabling legacy virtio
1224 * support for those virtio pci devices which allow it.
1226 object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1227 "on", true);
1228 object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1229 "on", false);
1232 machine_class->init(machine);
1233 phase_advance(PHASE_MACHINE_INITIALIZED);
1236 static NotifierList machine_init_done_notifiers =
1237 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1239 void qemu_add_machine_init_done_notifier(Notifier *notify)
1241 notifier_list_add(&machine_init_done_notifiers, notify);
1242 if (phase_check(PHASE_MACHINE_READY)) {
1243 notify->notify(notify, NULL);
1247 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1249 notifier_remove(notify);
1252 void qdev_machine_creation_done(void)
1254 cpu_synchronize_all_post_init();
1256 if (current_machine->boot_once) {
1257 qemu_boot_set(current_machine->boot_once, &error_fatal);
1258 qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_order));
1262 * ok, initial machine setup is done, starting from now we can
1263 * only create hotpluggable devices
1265 phase_advance(PHASE_MACHINE_READY);
1266 qdev_assert_realized_properly();
1268 /* TODO: once all bus devices are qdevified, this should be done
1269 * when bus is created by qdev.c */
1271 * TODO: If we had a main 'reset container' that the whole system
1272 * lived in, we could reset that using the multi-phase reset
1273 * APIs. For the moment, we just reset the sysbus, which will cause
1274 * all devices hanging off it (and all their child buses, recursively)
1275 * to be reset. Note that this will *not* reset any Device objects
1276 * which are not attached to some part of the qbus tree!
1278 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1280 notifier_list_notify(&machine_init_done_notifiers, NULL);
1282 if (rom_check_and_register_reset() != 0) {
1283 exit(1);
1286 replay_start();
1288 /* This checkpoint is required by replay to separate prior clock
1289 reading from the other reads, because timer polling functions query
1290 clock values from the log. */
1291 replay_checkpoint(CHECKPOINT_RESET);
1292 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1293 register_global_state();
1296 static const TypeInfo machine_info = {
1297 .name = TYPE_MACHINE,
1298 .parent = TYPE_OBJECT,
1299 .abstract = true,
1300 .class_size = sizeof(MachineClass),
1301 .class_init = machine_class_init,
1302 .class_base_init = machine_class_base_init,
1303 .instance_size = sizeof(MachineState),
1304 .instance_init = machine_initfn,
1305 .instance_finalize = machine_finalize,
1308 static void machine_register_types(void)
1310 type_register_static(&machine_info);
1313 type_init(machine_register_types)