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_2
[] = {};
41 const size_t hw_compat_6_2_len
= G_N_ELEMENTS(hw_compat_6_2
);
43 GlobalProperty hw_compat_6_1
[] = {
44 { "vhost-user-vsock-device", "seqpacket", "off" },
45 { "nvme-ns", "shared", "off" },
47 const size_t hw_compat_6_1_len
= G_N_ELEMENTS(hw_compat_6_1
);
49 GlobalProperty hw_compat_6_0
[] = {
50 { "gpex-pcihost", "allow-unmapped-accesses", "false" },
51 { "i8042", "extended-state", "false"},
52 { "nvme-ns", "eui64-default", "off"},
53 { "e1000", "init-vet", "off" },
54 { "e1000e", "init-vet", "off" },
55 { "vhost-vsock-device", "seqpacket", "off" },
57 const size_t hw_compat_6_0_len
= G_N_ELEMENTS(hw_compat_6_0
);
59 GlobalProperty hw_compat_5_2
[] = {
60 { "ICH9-LPC", "smm-compat", "on"},
61 { "PIIX4_PM", "smm-compat", "on"},
62 { "virtio-blk-device", "report-discard-granularity", "off" },
63 { "virtio-net-pci-base", "vectors", "3"},
65 const size_t hw_compat_5_2_len
= G_N_ELEMENTS(hw_compat_5_2
);
67 GlobalProperty hw_compat_5_1
[] = {
68 { "vhost-scsi", "num_queues", "1"},
69 { "vhost-user-blk", "num-queues", "1"},
70 { "vhost-user-scsi", "num_queues", "1"},
71 { "virtio-blk-device", "num-queues", "1"},
72 { "virtio-scsi-device", "num_queues", "1"},
73 { "nvme", "use-intel-id", "on"},
74 { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
75 { "pl011", "migrate-clk", "off" },
76 { "virtio-pci", "x-ats-page-aligned", "off"},
78 const size_t hw_compat_5_1_len
= G_N_ELEMENTS(hw_compat_5_1
);
80 GlobalProperty hw_compat_5_0
[] = {
81 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
82 { "virtio-balloon-device", "page-poison", "false" },
83 { "vmport", "x-read-set-eax", "off" },
84 { "vmport", "x-signal-unsupported-cmd", "off" },
85 { "vmport", "x-report-vmx-type", "off" },
86 { "vmport", "x-cmds-v2", "off" },
87 { "virtio-device", "x-disable-legacy-check", "true" },
89 const size_t hw_compat_5_0_len
= G_N_ELEMENTS(hw_compat_5_0
);
91 GlobalProperty hw_compat_4_2
[] = {
92 { "virtio-blk-device", "queue-size", "128"},
93 { "virtio-scsi-device", "virtqueue_size", "128"},
94 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
95 { "virtio-blk-device", "seg-max-adjust", "off"},
96 { "virtio-scsi-device", "seg_max_adjust", "off"},
97 { "vhost-blk-device", "seg_max_adjust", "off"},
98 { "usb-host", "suppress-remote-wake", "off" },
99 { "usb-redir", "suppress-remote-wake", "off" },
100 { "qxl", "revision", "4" },
101 { "qxl-vga", "revision", "4" },
102 { "fw_cfg", "acpi-mr-restore", "false" },
103 { "virtio-device", "use-disabled-flag", "false" },
105 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
107 GlobalProperty hw_compat_4_1
[] = {
108 { "virtio-pci", "x-pcie-flr-init", "off" },
110 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
112 GlobalProperty hw_compat_4_0
[] = {
113 { "VGA", "edid", "false" },
114 { "secondary-vga", "edid", "false" },
115 { "bochs-display", "edid", "false" },
116 { "virtio-vga", "edid", "false" },
117 { "virtio-gpu-device", "edid", "false" },
118 { "virtio-device", "use-started", "false" },
119 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
120 { "pl031", "migrate-tick-offset", "false" },
122 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
124 GlobalProperty hw_compat_3_1
[] = {
125 { "pcie-root-port", "x-speed", "2_5" },
126 { "pcie-root-port", "x-width", "1" },
127 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
128 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
129 { "tpm-crb", "ppi", "false" },
130 { "tpm-tis", "ppi", "false" },
131 { "usb-kbd", "serial", "42" },
132 { "usb-mouse", "serial", "42" },
133 { "usb-tablet", "serial", "42" },
134 { "virtio-blk-device", "discard", "false" },
135 { "virtio-blk-device", "write-zeroes", "false" },
136 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
137 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
139 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
141 GlobalProperty hw_compat_3_0
[] = {};
142 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
144 GlobalProperty hw_compat_2_12
[] = {
145 { "migration", "decompress-error-check", "off" },
146 { "hda-audio", "use-timer", "false" },
147 { "cirrus-vga", "global-vmstate", "true" },
148 { "VGA", "global-vmstate", "true" },
149 { "vmware-svga", "global-vmstate", "true" },
150 { "qxl-vga", "global-vmstate", "true" },
152 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
154 GlobalProperty hw_compat_2_11
[] = {
155 { "hpet", "hpet-offset-saved", "false" },
156 { "virtio-blk-pci", "vectors", "2" },
157 { "vhost-user-blk-pci", "vectors", "2" },
158 { "e1000", "migrate_tso_props", "off" },
160 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
162 GlobalProperty hw_compat_2_10
[] = {
163 { "virtio-mouse-device", "wheel-axis", "false" },
164 { "virtio-tablet-device", "wheel-axis", "false" },
166 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
168 GlobalProperty hw_compat_2_9
[] = {
169 { "pci-bridge", "shpc", "off" },
170 { "intel-iommu", "pt", "off" },
171 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
172 { "pcie-root-port", "x-migrate-msix", "false" },
174 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
176 GlobalProperty hw_compat_2_8
[] = {
177 { "fw_cfg_mem", "x-file-slots", "0x10" },
178 { "fw_cfg_io", "x-file-slots", "0x10" },
179 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
180 { "pci-bridge", "shpc", "on" },
181 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
182 { "virtio-pci", "x-pcie-deverr-init", "off" },
183 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
184 { "virtio-pci", "x-pcie-pm-init", "off" },
185 { "cirrus-vga", "vgamem_mb", "8" },
186 { "isa-cirrus-vga", "vgamem_mb", "8" },
188 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
190 GlobalProperty hw_compat_2_7
[] = {
191 { "virtio-pci", "page-per-vq", "on" },
192 { "virtio-serial-device", "emergency-write", "off" },
193 { "ioapic", "version", "0x11" },
194 { "intel-iommu", "x-buggy-eim", "true" },
195 { "virtio-pci", "x-ignore-backend-features", "on" },
197 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
199 GlobalProperty hw_compat_2_6
[] = {
200 { "virtio-mmio", "format_transport_address", "off" },
201 /* Optional because not all virtio-pci devices support legacy mode */
202 { "virtio-pci", "disable-modern", "on", .optional
= true },
203 { "virtio-pci", "disable-legacy", "off", .optional
= true },
205 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
207 GlobalProperty hw_compat_2_5
[] = {
208 { "isa-fdc", "fallback", "144" },
209 { "pvscsi", "x-old-pci-configuration", "on" },
210 { "pvscsi", "x-disable-pcie", "on" },
211 { "vmxnet3", "x-old-msi-offsets", "on" },
212 { "vmxnet3", "x-disable-pcie", "on" },
214 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
216 GlobalProperty hw_compat_2_4
[] = {
217 /* Optional because the 'scsi' property is Linux-only */
218 { "virtio-blk-device", "scsi", "true", .optional
= true },
219 { "e1000", "extra_mac_registers", "off" },
220 { "virtio-pci", "x-disable-pcie", "on" },
221 { "virtio-pci", "migrate-extra", "off" },
222 { "fw_cfg_mem", "dma_enabled", "off" },
223 { "fw_cfg_io", "dma_enabled", "off" }
225 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
227 GlobalProperty hw_compat_2_3
[] = {
228 { "virtio-blk-pci", "any_layout", "off" },
229 { "virtio-balloon-pci", "any_layout", "off" },
230 { "virtio-serial-pci", "any_layout", "off" },
231 { "virtio-9p-pci", "any_layout", "off" },
232 { "virtio-rng-pci", "any_layout", "off" },
233 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
234 { "migration", "send-configuration", "off" },
235 { "migration", "send-section-footer", "off" },
236 { "migration", "store-global-state", "off" },
238 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
240 GlobalProperty hw_compat_2_2
[] = {};
241 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
243 GlobalProperty hw_compat_2_1
[] = {
244 { "intel-hda", "old_msi_addr", "on" },
245 { "VGA", "qemu-extended-regs", "off" },
246 { "secondary-vga", "qemu-extended-regs", "off" },
247 { "virtio-scsi-pci", "any_layout", "off" },
248 { "usb-mouse", "usb_version", "1" },
249 { "usb-kbd", "usb_version", "1" },
250 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
252 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
254 MachineState
*current_machine
;
256 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
258 MachineState
*ms
= MACHINE(obj
);
260 return g_strdup(ms
->kernel_filename
);
263 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
265 MachineState
*ms
= MACHINE(obj
);
267 g_free(ms
->kernel_filename
);
268 ms
->kernel_filename
= g_strdup(value
);
271 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
273 MachineState
*ms
= MACHINE(obj
);
275 return g_strdup(ms
->initrd_filename
);
278 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
280 MachineState
*ms
= MACHINE(obj
);
282 g_free(ms
->initrd_filename
);
283 ms
->initrd_filename
= g_strdup(value
);
286 static char *machine_get_append(Object
*obj
, Error
**errp
)
288 MachineState
*ms
= MACHINE(obj
);
290 return g_strdup(ms
->kernel_cmdline
);
293 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
295 MachineState
*ms
= MACHINE(obj
);
297 g_free(ms
->kernel_cmdline
);
298 ms
->kernel_cmdline
= g_strdup(value
);
301 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
303 MachineState
*ms
= MACHINE(obj
);
305 return g_strdup(ms
->dtb
);
308 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
310 MachineState
*ms
= MACHINE(obj
);
313 ms
->dtb
= g_strdup(value
);
316 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
318 MachineState
*ms
= MACHINE(obj
);
320 return g_strdup(ms
->dumpdtb
);
323 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
325 MachineState
*ms
= MACHINE(obj
);
328 ms
->dumpdtb
= g_strdup(value
);
331 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
332 const char *name
, void *opaque
,
335 MachineState
*ms
= MACHINE(obj
);
336 int64_t value
= ms
->phandle_start
;
338 visit_type_int(v
, name
, &value
, errp
);
341 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
342 const char *name
, void *opaque
,
345 MachineState
*ms
= MACHINE(obj
);
348 if (!visit_type_int(v
, name
, &value
, errp
)) {
352 ms
->phandle_start
= value
;
355 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
357 MachineState
*ms
= MACHINE(obj
);
359 return g_strdup(ms
->dt_compatible
);
362 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
364 MachineState
*ms
= MACHINE(obj
);
366 g_free(ms
->dt_compatible
);
367 ms
->dt_compatible
= g_strdup(value
);
370 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
372 MachineState
*ms
= MACHINE(obj
);
374 return ms
->dump_guest_core
;
377 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
379 MachineState
*ms
= MACHINE(obj
);
381 ms
->dump_guest_core
= value
;
384 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
386 MachineState
*ms
= MACHINE(obj
);
388 return ms
->mem_merge
;
391 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
393 MachineState
*ms
= MACHINE(obj
);
395 ms
->mem_merge
= value
;
398 static bool machine_get_usb(Object
*obj
, Error
**errp
)
400 MachineState
*ms
= MACHINE(obj
);
405 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
407 MachineState
*ms
= MACHINE(obj
);
410 ms
->usb_disabled
= !value
;
413 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
415 MachineState
*ms
= MACHINE(obj
);
417 return ms
->enable_graphics
;
420 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
422 MachineState
*ms
= MACHINE(obj
);
424 ms
->enable_graphics
= value
;
427 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
429 MachineState
*ms
= MACHINE(obj
);
431 return g_strdup(ms
->firmware
);
434 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
436 MachineState
*ms
= MACHINE(obj
);
438 g_free(ms
->firmware
);
439 ms
->firmware
= g_strdup(value
);
442 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
444 MachineState
*ms
= MACHINE(obj
);
446 ms
->suppress_vmdesc
= value
;
449 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
451 MachineState
*ms
= MACHINE(obj
);
453 return ms
->suppress_vmdesc
;
456 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
458 MachineState
*ms
= MACHINE(obj
);
461 return g_strdup(object_get_canonical_path_component(OBJECT(ms
->cgs
)));
467 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
471 object_resolve_path_component(object_get_objects_root(), value
);
474 error_setg(errp
, "No such memory encryption object '%s'", value
);
478 object_property_set_link(obj
, "confidential-guest-support", cgs
, errp
);
481 static void machine_check_confidential_guest_support(const Object
*obj
,
487 * So far the only constraint is that the target has the
488 * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
493 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
495 MachineState
*ms
= MACHINE(obj
);
497 return ms
->nvdimms_state
->is_enabled
;
500 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
502 MachineState
*ms
= MACHINE(obj
);
504 ms
->nvdimms_state
->is_enabled
= value
;
507 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
509 MachineState
*ms
= MACHINE(obj
);
511 return ms
->numa_state
->hmat_enabled
;
514 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
516 MachineState
*ms
= MACHINE(obj
);
518 ms
->numa_state
->hmat_enabled
= value
;
521 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
523 MachineState
*ms
= MACHINE(obj
);
525 return g_strdup(ms
->nvdimms_state
->persistence_string
);
528 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
531 MachineState
*ms
= MACHINE(obj
);
532 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
534 if (strcmp(value
, "cpu") == 0) {
535 nvdimms_state
->persistence
= 3;
536 } else if (strcmp(value
, "mem-ctrl") == 0) {
537 nvdimms_state
->persistence
= 2;
539 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
544 g_free(nvdimms_state
->persistence_string
);
545 nvdimms_state
->persistence_string
= g_strdup(value
);
548 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
550 QAPI_LIST_PREPEND(mc
->allowed_dynamic_sysbus_devices
, g_strdup(type
));
553 bool device_is_dynamic_sysbus(MachineClass
*mc
, DeviceState
*dev
)
555 Object
*obj
= OBJECT(dev
);
557 if (!object_dynamic_cast(obj
, TYPE_SYS_BUS_DEVICE
)) {
561 return device_type_is_dynamic_sysbus(mc
, object_get_typename(obj
));
564 bool device_type_is_dynamic_sysbus(MachineClass
*mc
, const char *type
)
566 bool allowed
= false;
568 ObjectClass
*klass
= object_class_by_name(type
);
570 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
573 allowed
|= !!object_class_dynamic_cast(klass
, wl
->value
);
579 static char *machine_get_memdev(Object
*obj
, Error
**errp
)
581 MachineState
*ms
= MACHINE(obj
);
583 return g_strdup(ms
->ram_memdev_id
);
586 static void machine_set_memdev(Object
*obj
, const char *value
, Error
**errp
)
588 MachineState
*ms
= MACHINE(obj
);
590 g_free(ms
->ram_memdev_id
);
591 ms
->ram_memdev_id
= g_strdup(value
);
594 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
597 HotpluggableCPUList
*head
= NULL
;
598 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
600 /* force board to initialize possible_cpus if it hasn't been done yet */
601 mc
->possible_cpu_arch_ids(machine
);
603 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
605 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
607 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
608 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
609 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
610 sizeof(*cpu_item
->props
));
612 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
614 cpu_item
->has_qom_path
= true;
615 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
617 QAPI_LIST_PREPEND(head
, cpu_item
);
623 * machine_set_cpu_numa_node:
624 * @machine: machine object to modify
625 * @props: specifies which cpu objects to assign to
626 * numa node specified by @props.node_id
627 * @errp: if an error occurs, a pointer to an area to store the error
629 * Associate NUMA node specified by @props.node_id with cpu slots that
630 * match socket/core/thread-ids specified by @props. It's recommended to use
631 * query-hotpluggable-cpus.props values to specify affected cpu slots,
632 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
634 * However for CLI convenience it's possible to pass in subset of properties,
635 * which would affect all cpu slots that match it.
637 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
638 * -numa cpu,node-id=0,socket_id=0 \
639 * -numa cpu,node-id=1,socket_id=1
640 * will assign all child cores of socket 0 to node 0 and
641 * of socket 1 to node 1.
643 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
645 * Empty subset is disallowed and function will return with error in this case.
647 void machine_set_cpu_numa_node(MachineState
*machine
,
648 const CpuInstanceProperties
*props
, Error
**errp
)
650 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
651 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
655 if (!mc
->possible_cpu_arch_ids
) {
656 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
660 /* disabling node mapping is not supported, forbid it */
661 assert(props
->has_node_id
);
663 /* force board to initialize possible_cpus if it hasn't been done yet */
664 mc
->possible_cpu_arch_ids(machine
);
666 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
667 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
669 /* reject unsupported by board properties */
670 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
671 error_setg(errp
, "thread-id is not supported");
675 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
676 error_setg(errp
, "core-id is not supported");
680 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
681 error_setg(errp
, "socket-id is not supported");
685 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
686 error_setg(errp
, "die-id is not supported");
690 /* skip slots with explicit mismatch */
691 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
695 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
699 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
703 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
707 /* reject assignment if slot is already assigned, for compatibility
708 * of legacy cpu_index mapping with SPAPR core based mapping do not
709 * error out if cpu thread and matched core have the same node-id */
710 if (slot
->props
.has_node_id
&&
711 slot
->props
.node_id
!= props
->node_id
) {
712 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
713 slot
->props
.node_id
);
717 /* assign slot to node as it's matched '-numa cpu' key */
719 slot
->props
.node_id
= props
->node_id
;
720 slot
->props
.has_node_id
= props
->has_node_id
;
722 if (machine
->numa_state
->hmat_enabled
) {
723 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
724 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
725 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
726 " should be itself (got %" PRIu16
")",
727 props
->node_id
, numa_info
[props
->node_id
].initiator
);
730 numa_info
[props
->node_id
].has_cpu
= true;
731 numa_info
[props
->node_id
].initiator
= props
->node_id
;
736 error_setg(errp
, "no match found");
740 static void machine_get_smp(Object
*obj
, Visitor
*v
, const char *name
,
741 void *opaque
, Error
**errp
)
743 MachineState
*ms
= MACHINE(obj
);
744 SMPConfiguration
*config
= &(SMPConfiguration
){
745 .has_cpus
= true, .cpus
= ms
->smp
.cpus
,
746 .has_sockets
= true, .sockets
= ms
->smp
.sockets
,
747 .has_dies
= true, .dies
= ms
->smp
.dies
,
748 .has_clusters
= true, .clusters
= ms
->smp
.clusters
,
749 .has_cores
= true, .cores
= ms
->smp
.cores
,
750 .has_threads
= true, .threads
= ms
->smp
.threads
,
751 .has_maxcpus
= true, .maxcpus
= ms
->smp
.max_cpus
,
754 if (!visit_type_SMPConfiguration(v
, name
, &config
, &error_abort
)) {
759 static void machine_set_smp(Object
*obj
, Visitor
*v
, const char *name
,
760 void *opaque
, Error
**errp
)
762 MachineState
*ms
= MACHINE(obj
);
763 g_autoptr(SMPConfiguration
) config
= NULL
;
765 if (!visit_type_SMPConfiguration(v
, name
, &config
, errp
)) {
769 machine_parse_smp_config(ms
, config
, errp
);
772 static void machine_class_init(ObjectClass
*oc
, void *data
)
774 MachineClass
*mc
= MACHINE_CLASS(oc
);
776 /* Default 128 MB as guest ram size */
777 mc
->default_ram_size
= 128 * MiB
;
778 mc
->rom_file_has_mr
= true;
780 /* numa node memory size aligned on 8MB by default.
781 * On Linux, each node's border has to be 8MB aligned
783 mc
->numa_mem_align_shift
= 23;
785 object_class_property_add_str(oc
, "kernel",
786 machine_get_kernel
, machine_set_kernel
);
787 object_class_property_set_description(oc
, "kernel",
788 "Linux kernel image file");
790 object_class_property_add_str(oc
, "initrd",
791 machine_get_initrd
, machine_set_initrd
);
792 object_class_property_set_description(oc
, "initrd",
793 "Linux initial ramdisk file");
795 object_class_property_add_str(oc
, "append",
796 machine_get_append
, machine_set_append
);
797 object_class_property_set_description(oc
, "append",
798 "Linux kernel command line");
800 object_class_property_add_str(oc
, "dtb",
801 machine_get_dtb
, machine_set_dtb
);
802 object_class_property_set_description(oc
, "dtb",
803 "Linux kernel device tree file");
805 object_class_property_add_str(oc
, "dumpdtb",
806 machine_get_dumpdtb
, machine_set_dumpdtb
);
807 object_class_property_set_description(oc
, "dumpdtb",
808 "Dump current dtb to a file and quit");
810 object_class_property_add(oc
, "smp", "SMPConfiguration",
811 machine_get_smp
, machine_set_smp
,
813 object_class_property_set_description(oc
, "smp",
816 object_class_property_add(oc
, "phandle-start", "int",
817 machine_get_phandle_start
, machine_set_phandle_start
,
819 object_class_property_set_description(oc
, "phandle-start",
820 "The first phandle ID we may generate dynamically");
822 object_class_property_add_str(oc
, "dt-compatible",
823 machine_get_dt_compatible
, machine_set_dt_compatible
);
824 object_class_property_set_description(oc
, "dt-compatible",
825 "Overrides the \"compatible\" property of the dt root node");
827 object_class_property_add_bool(oc
, "dump-guest-core",
828 machine_get_dump_guest_core
, machine_set_dump_guest_core
);
829 object_class_property_set_description(oc
, "dump-guest-core",
830 "Include guest memory in a core dump");
832 object_class_property_add_bool(oc
, "mem-merge",
833 machine_get_mem_merge
, machine_set_mem_merge
);
834 object_class_property_set_description(oc
, "mem-merge",
835 "Enable/disable memory merge support");
837 object_class_property_add_bool(oc
, "usb",
838 machine_get_usb
, machine_set_usb
);
839 object_class_property_set_description(oc
, "usb",
840 "Set on/off to enable/disable usb");
842 object_class_property_add_bool(oc
, "graphics",
843 machine_get_graphics
, machine_set_graphics
);
844 object_class_property_set_description(oc
, "graphics",
845 "Set on/off to enable/disable graphics emulation");
847 object_class_property_add_str(oc
, "firmware",
848 machine_get_firmware
, machine_set_firmware
);
849 object_class_property_set_description(oc
, "firmware",
852 object_class_property_add_bool(oc
, "suppress-vmdesc",
853 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
);
854 object_class_property_set_description(oc
, "suppress-vmdesc",
855 "Set on to disable self-describing migration");
857 object_class_property_add_link(oc
, "confidential-guest-support",
858 TYPE_CONFIDENTIAL_GUEST_SUPPORT
,
859 offsetof(MachineState
, cgs
),
860 machine_check_confidential_guest_support
,
861 OBJ_PROP_LINK_STRONG
);
862 object_class_property_set_description(oc
, "confidential-guest-support",
863 "Set confidential guest scheme to support");
865 /* For compatibility */
866 object_class_property_add_str(oc
, "memory-encryption",
867 machine_get_memory_encryption
, machine_set_memory_encryption
);
868 object_class_property_set_description(oc
, "memory-encryption",
869 "Set memory encryption object to use");
871 object_class_property_add_str(oc
, "memory-backend",
872 machine_get_memdev
, machine_set_memdev
);
873 object_class_property_set_description(oc
, "memory-backend",
875 "Valid value is ID of hostmem based backend");
878 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
880 MachineClass
*mc
= MACHINE_CLASS(oc
);
881 mc
->max_cpus
= mc
->max_cpus
?: 1;
882 mc
->min_cpus
= mc
->min_cpus
?: 1;
883 mc
->default_cpus
= mc
->default_cpus
?: 1;
885 if (!object_class_is_abstract(oc
)) {
886 const char *cname
= object_class_get_name(oc
);
887 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
888 mc
->name
= g_strndup(cname
,
889 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
890 mc
->compat_props
= g_ptr_array_new();
894 static void machine_initfn(Object
*obj
)
896 MachineState
*ms
= MACHINE(obj
);
897 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
899 container_get(obj
, "/peripheral");
900 container_get(obj
, "/peripheral-anon");
902 ms
->dump_guest_core
= true;
903 ms
->mem_merge
= true;
904 ms
->enable_graphics
= true;
905 ms
->kernel_cmdline
= g_strdup("");
907 if (mc
->nvdimm_supported
) {
908 Object
*obj
= OBJECT(ms
);
910 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
911 object_property_add_bool(obj
, "nvdimm",
912 machine_get_nvdimm
, machine_set_nvdimm
);
913 object_property_set_description(obj
, "nvdimm",
914 "Set on/off to enable/disable "
915 "NVDIMM instantiation");
917 object_property_add_str(obj
, "nvdimm-persistence",
918 machine_get_nvdimm_persistence
,
919 machine_set_nvdimm_persistence
);
920 object_property_set_description(obj
, "nvdimm-persistence",
921 "Set NVDIMM persistence"
922 "Valid values are cpu, mem-ctrl");
925 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
926 ms
->numa_state
= g_new0(NumaState
, 1);
927 object_property_add_bool(obj
, "hmat",
928 machine_get_hmat
, machine_set_hmat
);
929 object_property_set_description(obj
, "hmat",
930 "Set on/off to enable/disable "
931 "ACPI Heterogeneous Memory Attribute "
935 /* default to mc->default_cpus */
936 ms
->smp
.cpus
= mc
->default_cpus
;
937 ms
->smp
.max_cpus
= mc
->default_cpus
;
940 ms
->smp
.clusters
= 1;
945 static void machine_finalize(Object
*obj
)
947 MachineState
*ms
= MACHINE(obj
);
949 g_free(ms
->kernel_filename
);
950 g_free(ms
->initrd_filename
);
951 g_free(ms
->kernel_cmdline
);
954 g_free(ms
->dt_compatible
);
955 g_free(ms
->firmware
);
956 g_free(ms
->device_memory
);
957 g_free(ms
->nvdimms_state
);
958 g_free(ms
->numa_state
);
961 bool machine_usb(MachineState
*machine
)
966 int machine_phandle_start(MachineState
*machine
)
968 return machine
->phandle_start
;
971 bool machine_dump_guest_core(MachineState
*machine
)
973 return machine
->dump_guest_core
;
976 bool machine_mem_merge(MachineState
*machine
)
978 return machine
->mem_merge
;
981 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
983 GString
*s
= g_string_new(NULL
);
984 if (cpu
->props
.has_socket_id
) {
985 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
987 if (cpu
->props
.has_die_id
) {
989 g_string_append_printf(s
, ", ");
991 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
993 if (cpu
->props
.has_core_id
) {
995 g_string_append_printf(s
, ", ");
997 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
999 if (cpu
->props
.has_thread_id
) {
1001 g_string_append_printf(s
, ", ");
1003 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
1005 return g_string_free(s
, false);
1008 static void numa_validate_initiator(NumaState
*numa_state
)
1011 NodeInfo
*numa_info
= numa_state
->nodes
;
1013 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
1014 if (numa_info
[i
].initiator
== MAX_NODES
) {
1015 error_report("The initiator of NUMA node %d is missing, use "
1016 "'-numa node,initiator' option to declare it", i
);
1020 if (!numa_info
[numa_info
[i
].initiator
].present
) {
1021 error_report("NUMA node %" PRIu16
" is missing, use "
1022 "'-numa node' option to declare it first",
1023 numa_info
[i
].initiator
);
1027 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
1028 error_report("The initiator of NUMA node %d is invalid", i
);
1034 static void machine_numa_finish_cpu_init(MachineState
*machine
)
1037 bool default_mapping
;
1038 GString
*s
= g_string_new(NULL
);
1039 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1040 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
1042 assert(machine
->numa_state
->num_nodes
);
1043 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1044 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
1048 default_mapping
= (i
== possible_cpus
->len
);
1050 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1051 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1053 if (!cpu_slot
->props
.has_node_id
) {
1054 /* fetch default mapping from board and enable it */
1055 CpuInstanceProperties props
= cpu_slot
->props
;
1057 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1058 if (!default_mapping
) {
1059 /* record slots with not set mapping,
1060 * TODO: make it hard error in future */
1061 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1062 g_string_append_printf(s
, "%sCPU %d [%s]",
1063 s
->len
? ", " : "", i
, cpu_str
);
1066 /* non mapped cpus used to fallback to node 0 */
1070 props
.has_node_id
= true;
1071 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1075 if (machine
->numa_state
->hmat_enabled
) {
1076 numa_validate_initiator(machine
->numa_state
);
1079 if (s
->len
&& !qtest_enabled()) {
1080 warn_report("CPU(s) not present in any NUMA nodes: %s",
1082 warn_report("All CPU(s) up to maxcpus should be described "
1083 "in NUMA config, ability to start up with partial NUMA "
1084 "mappings is obsoleted and will be removed in future");
1086 g_string_free(s
, true);
1089 MemoryRegion
*machine_consume_memdev(MachineState
*machine
,
1090 HostMemoryBackend
*backend
)
1092 MemoryRegion
*ret
= host_memory_backend_get_memory(backend
);
1094 if (host_memory_backend_is_mapped(backend
)) {
1095 error_report("memory backend %s can't be used multiple times.",
1096 object_get_canonical_path_component(OBJECT(backend
)));
1099 host_memory_backend_set_mapped(backend
, true);
1100 vmstate_register_ram_global(ret
);
1104 void machine_run_board_init(MachineState
*machine
)
1106 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1107 ObjectClass
*oc
= object_class_by_name(machine
->cpu_type
);
1110 /* This checkpoint is required by replay to separate prior clock
1111 reading from the other reads, because timer polling functions query
1112 clock values from the log. */
1113 replay_checkpoint(CHECKPOINT_INIT
);
1115 if (machine
->ram_memdev_id
) {
1117 o
= object_resolve_path_type(machine
->ram_memdev_id
,
1118 TYPE_MEMORY_BACKEND
, NULL
);
1119 machine
->ram
= machine_consume_memdev(machine
, MEMORY_BACKEND(o
));
1122 if (machine
->numa_state
) {
1123 numa_complete_configuration(machine
);
1124 if (machine
->numa_state
->num_nodes
) {
1125 machine_numa_finish_cpu_init(machine
);
1129 /* If the machine supports the valid_cpu_types check and the user
1130 * specified a CPU with -cpu check here that the user CPU is supported.
1132 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1135 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1136 if (object_class_dynamic_cast(oc
,
1137 machine_class
->valid_cpu_types
[i
])) {
1138 /* The user specificed CPU is in the valid field, we are
1145 if (!machine_class
->valid_cpu_types
[i
]) {
1146 /* The user specified CPU is not valid */
1147 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1148 error_printf("The valid types are: %s",
1149 machine_class
->valid_cpu_types
[0]);
1150 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1151 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1159 /* Check if CPU type is deprecated and warn if so */
1161 if (cc
&& cc
->deprecation_note
) {
1162 warn_report("CPU model %s is deprecated -- %s", machine
->cpu_type
,
1163 cc
->deprecation_note
);
1168 * With confidential guests, the host can't see the real
1169 * contents of RAM, so there's no point in it trying to merge
1172 machine_set_mem_merge(OBJECT(machine
), false, &error_abort
);
1175 * Virtio devices can't count on directly accessing guest
1176 * memory, so they need iommu_platform=on to use normal DMA
1177 * mechanisms. That requires also disabling legacy virtio
1178 * support for those virtio pci devices which allow it.
1180 object_register_sugar_prop(TYPE_VIRTIO_PCI
, "disable-legacy",
1182 object_register_sugar_prop(TYPE_VIRTIO_DEVICE
, "iommu_platform",
1186 accel_init_interfaces(ACCEL_GET_CLASS(machine
->accelerator
));
1187 machine_class
->init(machine
);
1188 phase_advance(PHASE_MACHINE_INITIALIZED
);
1191 static NotifierList machine_init_done_notifiers
=
1192 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers
);
1194 void qemu_add_machine_init_done_notifier(Notifier
*notify
)
1196 notifier_list_add(&machine_init_done_notifiers
, notify
);
1197 if (phase_check(PHASE_MACHINE_READY
)) {
1198 notify
->notify(notify
, NULL
);
1202 void qemu_remove_machine_init_done_notifier(Notifier
*notify
)
1204 notifier_remove(notify
);
1207 void qdev_machine_creation_done(void)
1209 cpu_synchronize_all_post_init();
1211 if (current_machine
->boot_once
) {
1212 qemu_boot_set(current_machine
->boot_once
, &error_fatal
);
1213 qemu_register_reset(restore_boot_order
, g_strdup(current_machine
->boot_order
));
1217 * ok, initial machine setup is done, starting from now we can
1218 * only create hotpluggable devices
1220 phase_advance(PHASE_MACHINE_READY
);
1221 qdev_assert_realized_properly();
1223 /* TODO: once all bus devices are qdevified, this should be done
1224 * when bus is created by qdev.c */
1226 * TODO: If we had a main 'reset container' that the whole system
1227 * lived in, we could reset that using the multi-phase reset
1228 * APIs. For the moment, we just reset the sysbus, which will cause
1229 * all devices hanging off it (and all their child buses, recursively)
1230 * to be reset. Note that this will *not* reset any Device objects
1231 * which are not attached to some part of the qbus tree!
1233 qemu_register_reset(resettable_cold_reset_fn
, sysbus_get_default());
1235 notifier_list_notify(&machine_init_done_notifiers
, NULL
);
1237 if (rom_check_and_register_reset() != 0) {
1243 /* This checkpoint is required by replay to separate prior clock
1244 reading from the other reads, because timer polling functions query
1245 clock values from the log. */
1246 replay_checkpoint(CHECKPOINT_RESET
);
1247 qemu_system_reset(SHUTDOWN_CAUSE_NONE
);
1248 register_global_state();
1251 static const TypeInfo machine_info
= {
1252 .name
= TYPE_MACHINE
,
1253 .parent
= TYPE_OBJECT
,
1255 .class_size
= sizeof(MachineClass
),
1256 .class_init
= machine_class_init
,
1257 .class_base_init
= machine_class_base_init
,
1258 .instance_size
= sizeof(MachineState
),
1259 .instance_init
= machine_initfn
,
1260 .instance_finalize
= machine_finalize
,
1263 static void machine_register_types(void)
1265 type_register_static(&machine_info
);
1268 type_init(machine_register_types
)