4 * Copyright (C) 2014 Red Hat Inc
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/qapi-visit-machine.h"
23 #include "qapi/visitor.h"
24 #include "hw/sysbus.h"
25 #include "sysemu/cpus.h"
26 #include "sysemu/sysemu.h"
27 #include "sysemu/reset.h"
28 #include "sysemu/runstate.h"
29 #include "sysemu/numa.h"
30 #include "qemu/error-report.h"
31 #include "sysemu/qtest.h"
32 #include "hw/pci/pci.h"
33 #include "hw/mem/nvdimm.h"
34 #include "migration/global_state.h"
35 #include "migration/vmstate.h"
36 #include "exec/confidential-guest-support.h"
37 #include "hw/virtio/virtio.h"
38 #include "hw/virtio/virtio-pci.h"
40 GlobalProperty hw_compat_6_0
[] = {
41 { "gpex-pcihost", "allow-unmapped-accesses", "false" },
42 { "i8042", "extended-state", "false"},
43 { "nvme-ns", "eui64-default", "off"},
44 { "e1000", "init-vet", "off" },
45 { "e1000e", "init-vet", "off" },
47 const size_t hw_compat_6_0_len
= G_N_ELEMENTS(hw_compat_6_0
);
49 GlobalProperty hw_compat_5_2
[] = {
50 { "ICH9-LPC", "smm-compat", "on"},
51 { "PIIX4_PM", "smm-compat", "on"},
52 { "virtio-blk-device", "report-discard-granularity", "off" },
53 { "virtio-net-pci", "vectors", "3"},
55 const size_t hw_compat_5_2_len
= G_N_ELEMENTS(hw_compat_5_2
);
57 GlobalProperty hw_compat_5_1
[] = {
58 { "vhost-scsi", "num_queues", "1"},
59 { "vhost-user-blk", "num-queues", "1"},
60 { "vhost-user-scsi", "num_queues", "1"},
61 { "virtio-blk-device", "num-queues", "1"},
62 { "virtio-scsi-device", "num_queues", "1"},
63 { "nvme", "use-intel-id", "on"},
64 { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
65 { "pl011", "migrate-clk", "off" },
66 { "virtio-pci", "x-ats-page-aligned", "off"},
68 const size_t hw_compat_5_1_len
= G_N_ELEMENTS(hw_compat_5_1
);
70 GlobalProperty hw_compat_5_0
[] = {
71 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
72 { "virtio-balloon-device", "page-poison", "false" },
73 { "vmport", "x-read-set-eax", "off" },
74 { "vmport", "x-signal-unsupported-cmd", "off" },
75 { "vmport", "x-report-vmx-type", "off" },
76 { "vmport", "x-cmds-v2", "off" },
77 { "virtio-device", "x-disable-legacy-check", "true" },
79 const size_t hw_compat_5_0_len
= G_N_ELEMENTS(hw_compat_5_0
);
81 GlobalProperty hw_compat_4_2
[] = {
82 { "virtio-blk-device", "queue-size", "128"},
83 { "virtio-scsi-device", "virtqueue_size", "128"},
84 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
85 { "virtio-blk-device", "seg-max-adjust", "off"},
86 { "virtio-scsi-device", "seg_max_adjust", "off"},
87 { "vhost-blk-device", "seg_max_adjust", "off"},
88 { "usb-host", "suppress-remote-wake", "off" },
89 { "usb-redir", "suppress-remote-wake", "off" },
90 { "qxl", "revision", "4" },
91 { "qxl-vga", "revision", "4" },
92 { "fw_cfg", "acpi-mr-restore", "false" },
93 { "virtio-device", "use-disabled-flag", "false" },
95 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
97 GlobalProperty hw_compat_4_1
[] = {
98 { "virtio-pci", "x-pcie-flr-init", "off" },
100 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
102 GlobalProperty hw_compat_4_0
[] = {
103 { "VGA", "edid", "false" },
104 { "secondary-vga", "edid", "false" },
105 { "bochs-display", "edid", "false" },
106 { "virtio-vga", "edid", "false" },
107 { "virtio-gpu-device", "edid", "false" },
108 { "virtio-device", "use-started", "false" },
109 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
110 { "pl031", "migrate-tick-offset", "false" },
112 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
114 GlobalProperty hw_compat_3_1
[] = {
115 { "pcie-root-port", "x-speed", "2_5" },
116 { "pcie-root-port", "x-width", "1" },
117 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
118 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
119 { "tpm-crb", "ppi", "false" },
120 { "tpm-tis", "ppi", "false" },
121 { "usb-kbd", "serial", "42" },
122 { "usb-mouse", "serial", "42" },
123 { "usb-tablet", "serial", "42" },
124 { "virtio-blk-device", "discard", "false" },
125 { "virtio-blk-device", "write-zeroes", "false" },
126 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
127 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
129 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
131 GlobalProperty hw_compat_3_0
[] = {};
132 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
134 GlobalProperty hw_compat_2_12
[] = {
135 { "migration", "decompress-error-check", "off" },
136 { "hda-audio", "use-timer", "false" },
137 { "cirrus-vga", "global-vmstate", "true" },
138 { "VGA", "global-vmstate", "true" },
139 { "vmware-svga", "global-vmstate", "true" },
140 { "qxl-vga", "global-vmstate", "true" },
142 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
144 GlobalProperty hw_compat_2_11
[] = {
145 { "hpet", "hpet-offset-saved", "false" },
146 { "virtio-blk-pci", "vectors", "2" },
147 { "vhost-user-blk-pci", "vectors", "2" },
148 { "e1000", "migrate_tso_props", "off" },
150 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
152 GlobalProperty hw_compat_2_10
[] = {
153 { "virtio-mouse-device", "wheel-axis", "false" },
154 { "virtio-tablet-device", "wheel-axis", "false" },
156 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
158 GlobalProperty hw_compat_2_9
[] = {
159 { "pci-bridge", "shpc", "off" },
160 { "intel-iommu", "pt", "off" },
161 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
162 { "pcie-root-port", "x-migrate-msix", "false" },
164 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
166 GlobalProperty hw_compat_2_8
[] = {
167 { "fw_cfg_mem", "x-file-slots", "0x10" },
168 { "fw_cfg_io", "x-file-slots", "0x10" },
169 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
170 { "pci-bridge", "shpc", "on" },
171 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
172 { "virtio-pci", "x-pcie-deverr-init", "off" },
173 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
174 { "virtio-pci", "x-pcie-pm-init", "off" },
175 { "cirrus-vga", "vgamem_mb", "8" },
176 { "isa-cirrus-vga", "vgamem_mb", "8" },
178 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
180 GlobalProperty hw_compat_2_7
[] = {
181 { "virtio-pci", "page-per-vq", "on" },
182 { "virtio-serial-device", "emergency-write", "off" },
183 { "ioapic", "version", "0x11" },
184 { "intel-iommu", "x-buggy-eim", "true" },
185 { "virtio-pci", "x-ignore-backend-features", "on" },
187 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
189 GlobalProperty hw_compat_2_6
[] = {
190 { "virtio-mmio", "format_transport_address", "off" },
191 /* Optional because not all virtio-pci devices support legacy mode */
192 { "virtio-pci", "disable-modern", "on", .optional
= true },
193 { "virtio-pci", "disable-legacy", "off", .optional
= true },
195 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
197 GlobalProperty hw_compat_2_5
[] = {
198 { "isa-fdc", "fallback", "144" },
199 { "pvscsi", "x-old-pci-configuration", "on" },
200 { "pvscsi", "x-disable-pcie", "on" },
201 { "vmxnet3", "x-old-msi-offsets", "on" },
202 { "vmxnet3", "x-disable-pcie", "on" },
204 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
206 GlobalProperty hw_compat_2_4
[] = {
207 /* Optional because the 'scsi' property is Linux-only */
208 { "virtio-blk-device", "scsi", "true", .optional
= true },
209 { "e1000", "extra_mac_registers", "off" },
210 { "virtio-pci", "x-disable-pcie", "on" },
211 { "virtio-pci", "migrate-extra", "off" },
212 { "fw_cfg_mem", "dma_enabled", "off" },
213 { "fw_cfg_io", "dma_enabled", "off" }
215 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
217 GlobalProperty hw_compat_2_3
[] = {
218 { "virtio-blk-pci", "any_layout", "off" },
219 { "virtio-balloon-pci", "any_layout", "off" },
220 { "virtio-serial-pci", "any_layout", "off" },
221 { "virtio-9p-pci", "any_layout", "off" },
222 { "virtio-rng-pci", "any_layout", "off" },
223 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
224 { "migration", "send-configuration", "off" },
225 { "migration", "send-section-footer", "off" },
226 { "migration", "store-global-state", "off" },
228 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
230 GlobalProperty hw_compat_2_2
[] = {};
231 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
233 GlobalProperty hw_compat_2_1
[] = {
234 { "intel-hda", "old_msi_addr", "on" },
235 { "VGA", "qemu-extended-regs", "off" },
236 { "secondary-vga", "qemu-extended-regs", "off" },
237 { "virtio-scsi-pci", "any_layout", "off" },
238 { "usb-mouse", "usb_version", "1" },
239 { "usb-kbd", "usb_version", "1" },
240 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
242 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
244 MachineState
*current_machine
;
246 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
248 MachineState
*ms
= MACHINE(obj
);
250 return g_strdup(ms
->kernel_filename
);
253 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
255 MachineState
*ms
= MACHINE(obj
);
257 g_free(ms
->kernel_filename
);
258 ms
->kernel_filename
= g_strdup(value
);
261 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
263 MachineState
*ms
= MACHINE(obj
);
265 return g_strdup(ms
->initrd_filename
);
268 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
270 MachineState
*ms
= MACHINE(obj
);
272 g_free(ms
->initrd_filename
);
273 ms
->initrd_filename
= g_strdup(value
);
276 static char *machine_get_append(Object
*obj
, Error
**errp
)
278 MachineState
*ms
= MACHINE(obj
);
280 return g_strdup(ms
->kernel_cmdline
);
283 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
285 MachineState
*ms
= MACHINE(obj
);
287 g_free(ms
->kernel_cmdline
);
288 ms
->kernel_cmdline
= g_strdup(value
);
291 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
293 MachineState
*ms
= MACHINE(obj
);
295 return g_strdup(ms
->dtb
);
298 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
300 MachineState
*ms
= MACHINE(obj
);
303 ms
->dtb
= g_strdup(value
);
306 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
308 MachineState
*ms
= MACHINE(obj
);
310 return g_strdup(ms
->dumpdtb
);
313 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
315 MachineState
*ms
= MACHINE(obj
);
318 ms
->dumpdtb
= g_strdup(value
);
321 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
322 const char *name
, void *opaque
,
325 MachineState
*ms
= MACHINE(obj
);
326 int64_t value
= ms
->phandle_start
;
328 visit_type_int(v
, name
, &value
, errp
);
331 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
332 const char *name
, void *opaque
,
335 MachineState
*ms
= MACHINE(obj
);
338 if (!visit_type_int(v
, name
, &value
, errp
)) {
342 ms
->phandle_start
= value
;
345 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
347 MachineState
*ms
= MACHINE(obj
);
349 return g_strdup(ms
->dt_compatible
);
352 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
354 MachineState
*ms
= MACHINE(obj
);
356 g_free(ms
->dt_compatible
);
357 ms
->dt_compatible
= g_strdup(value
);
360 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
362 MachineState
*ms
= MACHINE(obj
);
364 return ms
->dump_guest_core
;
367 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
369 MachineState
*ms
= MACHINE(obj
);
371 ms
->dump_guest_core
= value
;
374 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
376 MachineState
*ms
= MACHINE(obj
);
378 return ms
->mem_merge
;
381 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
383 MachineState
*ms
= MACHINE(obj
);
385 ms
->mem_merge
= value
;
388 static bool machine_get_usb(Object
*obj
, Error
**errp
)
390 MachineState
*ms
= MACHINE(obj
);
395 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
397 MachineState
*ms
= MACHINE(obj
);
400 ms
->usb_disabled
= !value
;
403 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
405 MachineState
*ms
= MACHINE(obj
);
407 return ms
->enable_graphics
;
410 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
412 MachineState
*ms
= MACHINE(obj
);
414 ms
->enable_graphics
= value
;
417 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
419 MachineState
*ms
= MACHINE(obj
);
421 return g_strdup(ms
->firmware
);
424 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
426 MachineState
*ms
= MACHINE(obj
);
428 g_free(ms
->firmware
);
429 ms
->firmware
= g_strdup(value
);
432 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
434 MachineState
*ms
= MACHINE(obj
);
436 ms
->suppress_vmdesc
= value
;
439 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
441 MachineState
*ms
= MACHINE(obj
);
443 return ms
->suppress_vmdesc
;
446 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
448 MachineState
*ms
= MACHINE(obj
);
451 return g_strdup(object_get_canonical_path_component(OBJECT(ms
->cgs
)));
457 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
461 object_resolve_path_component(object_get_objects_root(), value
);
464 error_setg(errp
, "No such memory encryption object '%s'", value
);
468 object_property_set_link(obj
, "confidential-guest-support", cgs
, errp
);
471 static void machine_check_confidential_guest_support(const Object
*obj
,
477 * So far the only constraint is that the target has the
478 * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
483 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
485 MachineState
*ms
= MACHINE(obj
);
487 return ms
->nvdimms_state
->is_enabled
;
490 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
492 MachineState
*ms
= MACHINE(obj
);
494 ms
->nvdimms_state
->is_enabled
= value
;
497 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
499 MachineState
*ms
= MACHINE(obj
);
501 return ms
->numa_state
->hmat_enabled
;
504 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
506 MachineState
*ms
= MACHINE(obj
);
508 ms
->numa_state
->hmat_enabled
= value
;
511 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
513 MachineState
*ms
= MACHINE(obj
);
515 return g_strdup(ms
->nvdimms_state
->persistence_string
);
518 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
521 MachineState
*ms
= MACHINE(obj
);
522 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
524 if (strcmp(value
, "cpu") == 0) {
525 nvdimms_state
->persistence
= 3;
526 } else if (strcmp(value
, "mem-ctrl") == 0) {
527 nvdimms_state
->persistence
= 2;
529 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
534 g_free(nvdimms_state
->persistence_string
);
535 nvdimms_state
->persistence_string
= g_strdup(value
);
538 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
540 QAPI_LIST_PREPEND(mc
->allowed_dynamic_sysbus_devices
, g_strdup(type
));
543 bool device_is_dynamic_sysbus(MachineClass
*mc
, DeviceState
*dev
)
545 bool allowed
= false;
547 Object
*obj
= OBJECT(dev
);
549 if (!object_dynamic_cast(obj
, TYPE_SYS_BUS_DEVICE
)) {
553 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
556 allowed
|= !!object_dynamic_cast(obj
, wl
->value
);
562 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
564 MachineState
*machine
= opaque
;
565 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
567 if (!device_is_dynamic_sysbus(mc
, DEVICE(sbdev
))) {
568 error_report("Option '-device %s' cannot be handled by this machine",
569 object_class_get_name(object_get_class(OBJECT(sbdev
))));
574 static char *machine_get_memdev(Object
*obj
, Error
**errp
)
576 MachineState
*ms
= MACHINE(obj
);
578 return g_strdup(ms
->ram_memdev_id
);
581 static void machine_set_memdev(Object
*obj
, const char *value
, Error
**errp
)
583 MachineState
*ms
= MACHINE(obj
);
585 g_free(ms
->ram_memdev_id
);
586 ms
->ram_memdev_id
= g_strdup(value
);
589 static void machine_init_notify(Notifier
*notifier
, void *data
)
591 MachineState
*machine
= MACHINE(qdev_get_machine());
594 * Loop through all dynamically created sysbus devices and check if they are
595 * all allowed. If a device is not allowed, error out.
597 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
600 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
603 HotpluggableCPUList
*head
= NULL
;
604 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
606 /* force board to initialize possible_cpus if it hasn't been done yet */
607 mc
->possible_cpu_arch_ids(machine
);
609 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
611 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
613 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
614 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
615 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
616 sizeof(*cpu_item
->props
));
618 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
620 cpu_item
->has_qom_path
= true;
621 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
623 QAPI_LIST_PREPEND(head
, cpu_item
);
629 * machine_set_cpu_numa_node:
630 * @machine: machine object to modify
631 * @props: specifies which cpu objects to assign to
632 * numa node specified by @props.node_id
633 * @errp: if an error occurs, a pointer to an area to store the error
635 * Associate NUMA node specified by @props.node_id with cpu slots that
636 * match socket/core/thread-ids specified by @props. It's recommended to use
637 * query-hotpluggable-cpus.props values to specify affected cpu slots,
638 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
640 * However for CLI convenience it's possible to pass in subset of properties,
641 * which would affect all cpu slots that match it.
643 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
644 * -numa cpu,node-id=0,socket_id=0 \
645 * -numa cpu,node-id=1,socket_id=1
646 * will assign all child cores of socket 0 to node 0 and
647 * of socket 1 to node 1.
649 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
651 * Empty subset is disallowed and function will return with error in this case.
653 void machine_set_cpu_numa_node(MachineState
*machine
,
654 const CpuInstanceProperties
*props
, Error
**errp
)
656 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
657 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
661 if (!mc
->possible_cpu_arch_ids
) {
662 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
666 /* disabling node mapping is not supported, forbid it */
667 assert(props
->has_node_id
);
669 /* force board to initialize possible_cpus if it hasn't been done yet */
670 mc
->possible_cpu_arch_ids(machine
);
672 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
673 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
675 /* reject unsupported by board properties */
676 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
677 error_setg(errp
, "thread-id is not supported");
681 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
682 error_setg(errp
, "core-id is not supported");
686 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
687 error_setg(errp
, "socket-id is not supported");
691 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
692 error_setg(errp
, "die-id is not supported");
696 /* skip slots with explicit mismatch */
697 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
701 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
705 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
709 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
713 /* reject assignment if slot is already assigned, for compatibility
714 * of legacy cpu_index mapping with SPAPR core based mapping do not
715 * error out if cpu thread and matched core have the same node-id */
716 if (slot
->props
.has_node_id
&&
717 slot
->props
.node_id
!= props
->node_id
) {
718 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
719 slot
->props
.node_id
);
723 /* assign slot to node as it's matched '-numa cpu' key */
725 slot
->props
.node_id
= props
->node_id
;
726 slot
->props
.has_node_id
= props
->has_node_id
;
728 if (machine
->numa_state
->hmat_enabled
) {
729 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
730 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
731 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
732 " should be itself (got %" PRIu16
")",
733 props
->node_id
, numa_info
[props
->node_id
].initiator
);
736 numa_info
[props
->node_id
].has_cpu
= true;
737 numa_info
[props
->node_id
].initiator
= props
->node_id
;
742 error_setg(errp
, "no match found");
746 static void smp_parse(MachineState
*ms
, SMPConfiguration
*config
, Error
**errp
)
748 unsigned cpus
= config
->has_cpus
? config
->cpus
: 0;
749 unsigned sockets
= config
->has_sockets
? config
->sockets
: 0;
750 unsigned cores
= config
->has_cores
? config
->cores
: 0;
751 unsigned threads
= config
->has_threads
? config
->threads
: 0;
753 if (config
->has_dies
&& config
->dies
!= 0 && config
->dies
!= 1) {
754 error_setg(errp
, "dies not supported by this machine's CPU topology");
757 /* compute missing values, prefer sockets over cores over threads */
758 if (cpus
== 0 || sockets
== 0) {
759 cores
= cores
> 0 ? cores
: 1;
760 threads
= threads
> 0 ? threads
: 1;
762 sockets
= sockets
> 0 ? sockets
: 1;
763 cpus
= cores
* threads
* sockets
;
765 ms
->smp
.max_cpus
= config
->has_maxcpus
? config
->maxcpus
: cpus
;
766 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
768 } else if (cores
== 0) {
769 threads
= threads
> 0 ? threads
: 1;
770 cores
= cpus
/ (sockets
* threads
);
771 cores
= cores
> 0 ? cores
: 1;
772 } else if (threads
== 0) {
773 threads
= cpus
/ (cores
* sockets
);
774 threads
= threads
> 0 ? threads
: 1;
775 } else if (sockets
* cores
* threads
< cpus
) {
776 error_setg(errp
, "cpu topology: "
777 "sockets (%u) * cores (%u) * threads (%u) < "
779 sockets
, cores
, threads
, cpus
);
783 ms
->smp
.max_cpus
= config
->has_maxcpus
? config
->maxcpus
: cpus
;
785 if (ms
->smp
.max_cpus
< cpus
) {
786 error_setg(errp
, "maxcpus must be equal to or greater than smp");
790 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
791 error_setg(errp
, "Invalid CPU topology: "
792 "sockets (%u) * cores (%u) * threads (%u) "
794 sockets
, cores
, threads
,
800 ms
->smp
.cores
= cores
;
801 ms
->smp
.threads
= threads
;
802 ms
->smp
.sockets
= sockets
;
805 static void machine_get_smp(Object
*obj
, Visitor
*v
, const char *name
,
806 void *opaque
, Error
**errp
)
808 MachineState
*ms
= MACHINE(obj
);
809 SMPConfiguration
*config
= &(SMPConfiguration
){
810 .has_cores
= true, .cores
= ms
->smp
.cores
,
811 .has_sockets
= true, .sockets
= ms
->smp
.sockets
,
812 .has_dies
= true, .dies
= ms
->smp
.dies
,
813 .has_threads
= true, .threads
= ms
->smp
.threads
,
814 .has_cpus
= true, .cpus
= ms
->smp
.cpus
,
815 .has_maxcpus
= true, .maxcpus
= ms
->smp
.max_cpus
,
817 if (!visit_type_SMPConfiguration(v
, name
, &config
, &error_abort
)) {
822 static void machine_set_smp(Object
*obj
, Visitor
*v
, const char *name
,
823 void *opaque
, Error
**errp
)
825 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
826 MachineState
*ms
= MACHINE(obj
);
827 SMPConfiguration
*config
;
830 if (!visit_type_SMPConfiguration(v
, name
, &config
, errp
)) {
834 mc
->smp_parse(ms
, config
, errp
);
839 /* sanity-check smp_cpus and max_cpus against mc */
840 if (ms
->smp
.cpus
< mc
->min_cpus
) {
841 error_setg(errp
, "Invalid SMP CPUs %d. The min CPUs "
842 "supported by machine '%s' is %d",
844 mc
->name
, mc
->min_cpus
);
845 } else if (ms
->smp
.max_cpus
> mc
->max_cpus
) {
846 error_setg(errp
, "Invalid SMP CPUs %d. The max CPUs "
847 "supported by machine '%s' is %d",
848 current_machine
->smp
.max_cpus
,
849 mc
->name
, mc
->max_cpus
);
853 qapi_free_SMPConfiguration(config
);
856 static void machine_class_init(ObjectClass
*oc
, void *data
)
858 MachineClass
*mc
= MACHINE_CLASS(oc
);
860 /* Default 128 MB as guest ram size */
861 mc
->default_ram_size
= 128 * MiB
;
862 mc
->rom_file_has_mr
= true;
863 mc
->smp_parse
= smp_parse
;
865 /* numa node memory size aligned on 8MB by default.
866 * On Linux, each node's border has to be 8MB aligned
868 mc
->numa_mem_align_shift
= 23;
870 object_class_property_add_str(oc
, "kernel",
871 machine_get_kernel
, machine_set_kernel
);
872 object_class_property_set_description(oc
, "kernel",
873 "Linux kernel image file");
875 object_class_property_add_str(oc
, "initrd",
876 machine_get_initrd
, machine_set_initrd
);
877 object_class_property_set_description(oc
, "initrd",
878 "Linux initial ramdisk file");
880 object_class_property_add_str(oc
, "append",
881 machine_get_append
, machine_set_append
);
882 object_class_property_set_description(oc
, "append",
883 "Linux kernel command line");
885 object_class_property_add_str(oc
, "dtb",
886 machine_get_dtb
, machine_set_dtb
);
887 object_class_property_set_description(oc
, "dtb",
888 "Linux kernel device tree file");
890 object_class_property_add_str(oc
, "dumpdtb",
891 machine_get_dumpdtb
, machine_set_dumpdtb
);
892 object_class_property_set_description(oc
, "dumpdtb",
893 "Dump current dtb to a file and quit");
895 object_class_property_add(oc
, "smp", "SMPConfiguration",
896 machine_get_smp
, machine_set_smp
,
898 object_class_property_set_description(oc
, "smp",
901 object_class_property_add(oc
, "phandle-start", "int",
902 machine_get_phandle_start
, machine_set_phandle_start
,
904 object_class_property_set_description(oc
, "phandle-start",
905 "The first phandle ID we may generate dynamically");
907 object_class_property_add_str(oc
, "dt-compatible",
908 machine_get_dt_compatible
, machine_set_dt_compatible
);
909 object_class_property_set_description(oc
, "dt-compatible",
910 "Overrides the \"compatible\" property of the dt root node");
912 object_class_property_add_bool(oc
, "dump-guest-core",
913 machine_get_dump_guest_core
, machine_set_dump_guest_core
);
914 object_class_property_set_description(oc
, "dump-guest-core",
915 "Include guest memory in a core dump");
917 object_class_property_add_bool(oc
, "mem-merge",
918 machine_get_mem_merge
, machine_set_mem_merge
);
919 object_class_property_set_description(oc
, "mem-merge",
920 "Enable/disable memory merge support");
922 object_class_property_add_bool(oc
, "usb",
923 machine_get_usb
, machine_set_usb
);
924 object_class_property_set_description(oc
, "usb",
925 "Set on/off to enable/disable usb");
927 object_class_property_add_bool(oc
, "graphics",
928 machine_get_graphics
, machine_set_graphics
);
929 object_class_property_set_description(oc
, "graphics",
930 "Set on/off to enable/disable graphics emulation");
932 object_class_property_add_str(oc
, "firmware",
933 machine_get_firmware
, machine_set_firmware
);
934 object_class_property_set_description(oc
, "firmware",
937 object_class_property_add_bool(oc
, "suppress-vmdesc",
938 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
);
939 object_class_property_set_description(oc
, "suppress-vmdesc",
940 "Set on to disable self-describing migration");
942 object_class_property_add_link(oc
, "confidential-guest-support",
943 TYPE_CONFIDENTIAL_GUEST_SUPPORT
,
944 offsetof(MachineState
, cgs
),
945 machine_check_confidential_guest_support
,
946 OBJ_PROP_LINK_STRONG
);
947 object_class_property_set_description(oc
, "confidential-guest-support",
948 "Set confidential guest scheme to support");
950 /* For compatibility */
951 object_class_property_add_str(oc
, "memory-encryption",
952 machine_get_memory_encryption
, machine_set_memory_encryption
);
953 object_class_property_set_description(oc
, "memory-encryption",
954 "Set memory encryption object to use");
956 object_class_property_add_str(oc
, "memory-backend",
957 machine_get_memdev
, machine_set_memdev
);
958 object_class_property_set_description(oc
, "memory-backend",
960 "Valid value is ID of hostmem based backend");
963 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
965 MachineClass
*mc
= MACHINE_CLASS(oc
);
966 mc
->max_cpus
= mc
->max_cpus
?: 1;
967 mc
->min_cpus
= mc
->min_cpus
?: 1;
968 mc
->default_cpus
= mc
->default_cpus
?: 1;
970 if (!object_class_is_abstract(oc
)) {
971 const char *cname
= object_class_get_name(oc
);
972 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
973 mc
->name
= g_strndup(cname
,
974 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
975 mc
->compat_props
= g_ptr_array_new();
979 static void machine_initfn(Object
*obj
)
981 MachineState
*ms
= MACHINE(obj
);
982 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
984 container_get(obj
, "/peripheral");
985 container_get(obj
, "/peripheral-anon");
987 ms
->dump_guest_core
= true;
988 ms
->mem_merge
= true;
989 ms
->enable_graphics
= true;
990 ms
->kernel_cmdline
= g_strdup("");
992 if (mc
->nvdimm_supported
) {
993 Object
*obj
= OBJECT(ms
);
995 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
996 object_property_add_bool(obj
, "nvdimm",
997 machine_get_nvdimm
, machine_set_nvdimm
);
998 object_property_set_description(obj
, "nvdimm",
999 "Set on/off to enable/disable "
1000 "NVDIMM instantiation");
1002 object_property_add_str(obj
, "nvdimm-persistence",
1003 machine_get_nvdimm_persistence
,
1004 machine_set_nvdimm_persistence
);
1005 object_property_set_description(obj
, "nvdimm-persistence",
1006 "Set NVDIMM persistence"
1007 "Valid values are cpu, mem-ctrl");
1010 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
1011 ms
->numa_state
= g_new0(NumaState
, 1);
1012 object_property_add_bool(obj
, "hmat",
1013 machine_get_hmat
, machine_set_hmat
);
1014 object_property_set_description(obj
, "hmat",
1015 "Set on/off to enable/disable "
1016 "ACPI Heterogeneous Memory Attribute "
1020 /* Register notifier when init is done for sysbus sanity checks */
1021 ms
->sysbus_notifier
.notify
= machine_init_notify
;
1022 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
1024 /* default to mc->default_cpus */
1025 ms
->smp
.cpus
= mc
->default_cpus
;
1026 ms
->smp
.max_cpus
= mc
->default_cpus
;
1029 ms
->smp
.threads
= 1;
1030 ms
->smp
.sockets
= 1;
1033 static void machine_finalize(Object
*obj
)
1035 MachineState
*ms
= MACHINE(obj
);
1037 g_free(ms
->kernel_filename
);
1038 g_free(ms
->initrd_filename
);
1039 g_free(ms
->kernel_cmdline
);
1041 g_free(ms
->dumpdtb
);
1042 g_free(ms
->dt_compatible
);
1043 g_free(ms
->firmware
);
1044 g_free(ms
->device_memory
);
1045 g_free(ms
->nvdimms_state
);
1046 g_free(ms
->numa_state
);
1049 bool machine_usb(MachineState
*machine
)
1051 return machine
->usb
;
1054 int machine_phandle_start(MachineState
*machine
)
1056 return machine
->phandle_start
;
1059 bool machine_dump_guest_core(MachineState
*machine
)
1061 return machine
->dump_guest_core
;
1064 bool machine_mem_merge(MachineState
*machine
)
1066 return machine
->mem_merge
;
1069 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
1071 GString
*s
= g_string_new(NULL
);
1072 if (cpu
->props
.has_socket_id
) {
1073 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
1075 if (cpu
->props
.has_die_id
) {
1076 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
1078 if (cpu
->props
.has_core_id
) {
1080 g_string_append_printf(s
, ", ");
1082 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
1084 if (cpu
->props
.has_thread_id
) {
1086 g_string_append_printf(s
, ", ");
1088 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
1090 return g_string_free(s
, false);
1093 static void numa_validate_initiator(NumaState
*numa_state
)
1096 NodeInfo
*numa_info
= numa_state
->nodes
;
1098 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
1099 if (numa_info
[i
].initiator
== MAX_NODES
) {
1100 error_report("The initiator of NUMA node %d is missing, use "
1101 "'-numa node,initiator' option to declare it", i
);
1105 if (!numa_info
[numa_info
[i
].initiator
].present
) {
1106 error_report("NUMA node %" PRIu16
" is missing, use "
1107 "'-numa node' option to declare it first",
1108 numa_info
[i
].initiator
);
1112 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
1113 error_report("The initiator of NUMA node %d is invalid", i
);
1119 static void machine_numa_finish_cpu_init(MachineState
*machine
)
1122 bool default_mapping
;
1123 GString
*s
= g_string_new(NULL
);
1124 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1125 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
1127 assert(machine
->numa_state
->num_nodes
);
1128 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1129 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
1133 default_mapping
= (i
== possible_cpus
->len
);
1135 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1136 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1138 if (!cpu_slot
->props
.has_node_id
) {
1139 /* fetch default mapping from board and enable it */
1140 CpuInstanceProperties props
= cpu_slot
->props
;
1142 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1143 if (!default_mapping
) {
1144 /* record slots with not set mapping,
1145 * TODO: make it hard error in future */
1146 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1147 g_string_append_printf(s
, "%sCPU %d [%s]",
1148 s
->len
? ", " : "", i
, cpu_str
);
1151 /* non mapped cpus used to fallback to node 0 */
1155 props
.has_node_id
= true;
1156 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1160 if (machine
->numa_state
->hmat_enabled
) {
1161 numa_validate_initiator(machine
->numa_state
);
1164 if (s
->len
&& !qtest_enabled()) {
1165 warn_report("CPU(s) not present in any NUMA nodes: %s",
1167 warn_report("All CPU(s) up to maxcpus should be described "
1168 "in NUMA config, ability to start up with partial NUMA "
1169 "mappings is obsoleted and will be removed in future");
1171 g_string_free(s
, true);
1174 MemoryRegion
*machine_consume_memdev(MachineState
*machine
,
1175 HostMemoryBackend
*backend
)
1177 MemoryRegion
*ret
= host_memory_backend_get_memory(backend
);
1179 if (memory_region_is_mapped(ret
)) {
1180 error_report("memory backend %s can't be used multiple times.",
1181 object_get_canonical_path_component(OBJECT(backend
)));
1184 host_memory_backend_set_mapped(backend
, true);
1185 vmstate_register_ram_global(ret
);
1189 void machine_run_board_init(MachineState
*machine
)
1191 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1192 ObjectClass
*oc
= object_class_by_name(machine
->cpu_type
);
1195 /* This checkpoint is required by replay to separate prior clock
1196 reading from the other reads, because timer polling functions query
1197 clock values from the log. */
1198 replay_checkpoint(CHECKPOINT_INIT
);
1200 if (machine
->ram_memdev_id
) {
1202 o
= object_resolve_path_type(machine
->ram_memdev_id
,
1203 TYPE_MEMORY_BACKEND
, NULL
);
1204 machine
->ram
= machine_consume_memdev(machine
, MEMORY_BACKEND(o
));
1207 if (machine
->numa_state
) {
1208 numa_complete_configuration(machine
);
1209 if (machine
->numa_state
->num_nodes
) {
1210 machine_numa_finish_cpu_init(machine
);
1214 /* If the machine supports the valid_cpu_types check and the user
1215 * specified a CPU with -cpu check here that the user CPU is supported.
1217 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1220 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1221 if (object_class_dynamic_cast(oc
,
1222 machine_class
->valid_cpu_types
[i
])) {
1223 /* The user specificed CPU is in the valid field, we are
1230 if (!machine_class
->valid_cpu_types
[i
]) {
1231 /* The user specified CPU is not valid */
1232 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1233 error_printf("The valid types are: %s",
1234 machine_class
->valid_cpu_types
[0]);
1235 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1236 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1244 /* Check if CPU type is deprecated and warn if so */
1246 if (cc
&& cc
->deprecation_note
) {
1247 warn_report("CPU model %s is deprecated -- %s", machine
->cpu_type
,
1248 cc
->deprecation_note
);
1253 * With confidential guests, the host can't see the real
1254 * contents of RAM, so there's no point in it trying to merge
1257 machine_set_mem_merge(OBJECT(machine
), false, &error_abort
);
1260 * Virtio devices can't count on directly accessing guest
1261 * memory, so they need iommu_platform=on to use normal DMA
1262 * mechanisms. That requires also disabling legacy virtio
1263 * support for those virtio pci devices which allow it.
1265 object_register_sugar_prop(TYPE_VIRTIO_PCI
, "disable-legacy",
1267 object_register_sugar_prop(TYPE_VIRTIO_DEVICE
, "iommu_platform",
1271 accel_init_interfaces(ACCEL_GET_CLASS(machine
->accelerator
));
1272 machine_class
->init(machine
);
1273 phase_advance(PHASE_MACHINE_INITIALIZED
);
1276 static NotifierList machine_init_done_notifiers
=
1277 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
1279 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
1281 notifier_list_add(&machine_init_done_notifiers
, notify
);
1282 if (phase_check(PHASE_MACHINE_READY
)) {
1283 notify
->notify(notify
, NULL
);
1287 void qemu_remove_machine_init_done_notifier(Notifier
*notify
)
1289 notifier_remove(notify
);
1292 void qdev_machine_creation_done(void)
1294 cpu_synchronize_all_post_init();
1296 if (current_machine
->boot_once
) {
1297 qemu_boot_set(current_machine
->boot_once
, &error_fatal
);
1298 qemu_register_reset(restore_boot_order
, g_strdup(current_machine
->boot_order
));
1302 * ok, initial machine setup is done, starting from now we can
1303 * only create hotpluggable devices
1305 phase_advance(PHASE_MACHINE_READY
);
1306 qdev_assert_realized_properly();
1308 /* TODO: once all bus devices are qdevified, this should be done
1309 * when bus is created by qdev.c */
1311 * TODO: If we had a main 'reset container' that the whole system
1312 * lived in, we could reset that using the multi-phase reset
1313 * APIs. For the moment, we just reset the sysbus, which will cause
1314 * all devices hanging off it (and all their child buses, recursively)
1315 * to be reset. Note that this will *not* reset any Device objects
1316 * which are not attached to some part of the qbus tree!
1318 qemu_register_reset(resettable_cold_reset_fn
, sysbus_get_default());
1320 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
1322 if (rom_check_and_register_reset() != 0) {
1328 /* This checkpoint is required by replay to separate prior clock
1329 reading from the other reads, because timer polling functions query
1330 clock values from the log. */
1331 replay_checkpoint(CHECKPOINT_RESET
);
1332 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
1333 register_global_state();
1336 static const TypeInfo machine_info
= {
1337 .name
= TYPE_MACHINE
,
1338 .parent
= TYPE_OBJECT
,
1340 .class_size
= sizeof(MachineClass
),
1341 .class_init
= machine_class_init
,
1342 .class_base_init
= machine_class_base_init
,
1343 .instance_size
= sizeof(MachineState
),
1344 .instance_init
= machine_initfn
,
1345 .instance_finalize
= machine_finalize
,
1348 static void machine_register_types(void)
1350 type_register_static(&machine_info
);
1353 type_init(machine_register_types
)