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 "qapi/error.h"
20 #include "qapi/qapi-visit-common.h"
21 #include "qapi/visitor.h"
22 #include "hw/sysbus.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/numa.h"
25 #include "qemu/error-report.h"
26 #include "sysemu/qtest.h"
27 #include "hw/pci/pci.h"
28 #include "hw/mem/nvdimm.h"
29 #include "migration/vmstate.h"
31 GlobalProperty hw_compat_5_0
[] = {
32 { "virtio-balloon-device", "page-poison", "false" },
33 { "vmport", "x-read-set-eax", "off" },
34 { "vmport", "x-signal-unsupported-cmd", "off" },
35 { "vmport", "x-report-vmx-type", "off" },
36 { "vmport", "x-cmds-v2", "off" },
38 const size_t hw_compat_5_0_len
= G_N_ELEMENTS(hw_compat_5_0
);
40 GlobalProperty hw_compat_4_2
[] = {
41 { "virtio-blk-device", "queue-size", "128"},
42 { "virtio-scsi-device", "virtqueue_size", "128"},
43 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
44 { "virtio-blk-device", "seg-max-adjust", "off"},
45 { "virtio-scsi-device", "seg_max_adjust", "off"},
46 { "vhost-blk-device", "seg_max_adjust", "off"},
47 { "usb-host", "suppress-remote-wake", "off" },
48 { "usb-redir", "suppress-remote-wake", "off" },
49 { "qxl", "revision", "4" },
50 { "qxl-vga", "revision", "4" },
51 { "fw_cfg", "acpi-mr-restore", "false" },
53 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
55 GlobalProperty hw_compat_4_1
[] = {
56 { "virtio-pci", "x-pcie-flr-init", "off" },
57 { "virtio-device", "use-disabled-flag", "false" },
59 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
61 GlobalProperty hw_compat_4_0
[] = {
62 { "VGA", "edid", "false" },
63 { "secondary-vga", "edid", "false" },
64 { "bochs-display", "edid", "false" },
65 { "virtio-vga", "edid", "false" },
66 { "virtio-gpu-device", "edid", "false" },
67 { "virtio-device", "use-started", "false" },
68 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
69 { "pl031", "migrate-tick-offset", "false" },
71 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
73 GlobalProperty hw_compat_3_1
[] = {
74 { "pcie-root-port", "x-speed", "2_5" },
75 { "pcie-root-port", "x-width", "1" },
76 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
77 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
78 { "tpm-crb", "ppi", "false" },
79 { "tpm-tis", "ppi", "false" },
80 { "usb-kbd", "serial", "42" },
81 { "usb-mouse", "serial", "42" },
82 { "usb-tablet", "serial", "42" },
83 { "virtio-blk-device", "discard", "false" },
84 { "virtio-blk-device", "write-zeroes", "false" },
85 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
86 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
88 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
90 GlobalProperty hw_compat_3_0
[] = {};
91 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
93 GlobalProperty hw_compat_2_12
[] = {
94 { "migration", "decompress-error-check", "off" },
95 { "hda-audio", "use-timer", "false" },
96 { "cirrus-vga", "global-vmstate", "true" },
97 { "VGA", "global-vmstate", "true" },
98 { "vmware-svga", "global-vmstate", "true" },
99 { "qxl-vga", "global-vmstate", "true" },
101 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
103 GlobalProperty hw_compat_2_11
[] = {
104 { "hpet", "hpet-offset-saved", "false" },
105 { "virtio-blk-pci", "vectors", "2" },
106 { "vhost-user-blk-pci", "vectors", "2" },
107 { "e1000", "migrate_tso_props", "off" },
109 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
111 GlobalProperty hw_compat_2_10
[] = {
112 { "virtio-mouse-device", "wheel-axis", "false" },
113 { "virtio-tablet-device", "wheel-axis", "false" },
115 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
117 GlobalProperty hw_compat_2_9
[] = {
118 { "pci-bridge", "shpc", "off" },
119 { "intel-iommu", "pt", "off" },
120 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
121 { "pcie-root-port", "x-migrate-msix", "false" },
123 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
125 GlobalProperty hw_compat_2_8
[] = {
126 { "fw_cfg_mem", "x-file-slots", "0x10" },
127 { "fw_cfg_io", "x-file-slots", "0x10" },
128 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
129 { "pci-bridge", "shpc", "on" },
130 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
131 { "virtio-pci", "x-pcie-deverr-init", "off" },
132 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
133 { "virtio-pci", "x-pcie-pm-init", "off" },
134 { "cirrus-vga", "vgamem_mb", "8" },
135 { "isa-cirrus-vga", "vgamem_mb", "8" },
137 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
139 GlobalProperty hw_compat_2_7
[] = {
140 { "virtio-pci", "page-per-vq", "on" },
141 { "virtio-serial-device", "emergency-write", "off" },
142 { "ioapic", "version", "0x11" },
143 { "intel-iommu", "x-buggy-eim", "true" },
144 { "virtio-pci", "x-ignore-backend-features", "on" },
146 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
148 GlobalProperty hw_compat_2_6
[] = {
149 { "virtio-mmio", "format_transport_address", "off" },
150 /* Optional because not all virtio-pci devices support legacy mode */
151 { "virtio-pci", "disable-modern", "on", .optional
= true },
152 { "virtio-pci", "disable-legacy", "off", .optional
= true },
154 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
156 GlobalProperty hw_compat_2_5
[] = {
157 { "isa-fdc", "fallback", "144" },
158 { "pvscsi", "x-old-pci-configuration", "on" },
159 { "pvscsi", "x-disable-pcie", "on" },
160 { "vmxnet3", "x-old-msi-offsets", "on" },
161 { "vmxnet3", "x-disable-pcie", "on" },
163 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
165 GlobalProperty hw_compat_2_4
[] = {
166 /* Optional because the 'scsi' property is Linux-only */
167 { "virtio-blk-device", "scsi", "true", .optional
= true },
168 { "e1000", "extra_mac_registers", "off" },
169 { "virtio-pci", "x-disable-pcie", "on" },
170 { "virtio-pci", "migrate-extra", "off" },
171 { "fw_cfg_mem", "dma_enabled", "off" },
172 { "fw_cfg_io", "dma_enabled", "off" }
174 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
176 GlobalProperty hw_compat_2_3
[] = {
177 { "virtio-blk-pci", "any_layout", "off" },
178 { "virtio-balloon-pci", "any_layout", "off" },
179 { "virtio-serial-pci", "any_layout", "off" },
180 { "virtio-9p-pci", "any_layout", "off" },
181 { "virtio-rng-pci", "any_layout", "off" },
182 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
183 { "migration", "send-configuration", "off" },
184 { "migration", "send-section-footer", "off" },
185 { "migration", "store-global-state", "off" },
187 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
189 GlobalProperty hw_compat_2_2
[] = {};
190 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
192 GlobalProperty hw_compat_2_1
[] = {
193 { "intel-hda", "old_msi_addr", "on" },
194 { "VGA", "qemu-extended-regs", "off" },
195 { "secondary-vga", "qemu-extended-regs", "off" },
196 { "virtio-scsi-pci", "any_layout", "off" },
197 { "usb-mouse", "usb_version", "1" },
198 { "usb-kbd", "usb_version", "1" },
199 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
201 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
203 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
205 MachineState
*ms
= MACHINE(obj
);
207 return g_strdup(ms
->kernel_filename
);
210 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
212 MachineState
*ms
= MACHINE(obj
);
214 g_free(ms
->kernel_filename
);
215 ms
->kernel_filename
= g_strdup(value
);
218 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
220 MachineState
*ms
= MACHINE(obj
);
222 return g_strdup(ms
->initrd_filename
);
225 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
227 MachineState
*ms
= MACHINE(obj
);
229 g_free(ms
->initrd_filename
);
230 ms
->initrd_filename
= g_strdup(value
);
233 static char *machine_get_append(Object
*obj
, Error
**errp
)
235 MachineState
*ms
= MACHINE(obj
);
237 return g_strdup(ms
->kernel_cmdline
);
240 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
242 MachineState
*ms
= MACHINE(obj
);
244 g_free(ms
->kernel_cmdline
);
245 ms
->kernel_cmdline
= g_strdup(value
);
248 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
250 MachineState
*ms
= MACHINE(obj
);
252 return g_strdup(ms
->dtb
);
255 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
257 MachineState
*ms
= MACHINE(obj
);
260 ms
->dtb
= g_strdup(value
);
263 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
265 MachineState
*ms
= MACHINE(obj
);
267 return g_strdup(ms
->dumpdtb
);
270 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
272 MachineState
*ms
= MACHINE(obj
);
275 ms
->dumpdtb
= g_strdup(value
);
278 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
279 const char *name
, void *opaque
,
282 MachineState
*ms
= MACHINE(obj
);
283 int64_t value
= ms
->phandle_start
;
285 visit_type_int(v
, name
, &value
, errp
);
288 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
289 const char *name
, void *opaque
,
292 MachineState
*ms
= MACHINE(obj
);
295 if (!visit_type_int(v
, name
, &value
, errp
)) {
299 ms
->phandle_start
= value
;
302 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
304 MachineState
*ms
= MACHINE(obj
);
306 return g_strdup(ms
->dt_compatible
);
309 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
311 MachineState
*ms
= MACHINE(obj
);
313 g_free(ms
->dt_compatible
);
314 ms
->dt_compatible
= g_strdup(value
);
317 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
319 MachineState
*ms
= MACHINE(obj
);
321 return ms
->dump_guest_core
;
324 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
326 MachineState
*ms
= MACHINE(obj
);
328 ms
->dump_guest_core
= value
;
331 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
333 MachineState
*ms
= MACHINE(obj
);
335 return ms
->mem_merge
;
338 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
340 MachineState
*ms
= MACHINE(obj
);
342 ms
->mem_merge
= value
;
345 static bool machine_get_usb(Object
*obj
, Error
**errp
)
347 MachineState
*ms
= MACHINE(obj
);
352 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
354 MachineState
*ms
= MACHINE(obj
);
357 ms
->usb_disabled
= !value
;
360 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
362 MachineState
*ms
= MACHINE(obj
);
364 return ms
->enable_graphics
;
367 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
369 MachineState
*ms
= MACHINE(obj
);
371 ms
->enable_graphics
= value
;
374 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
376 MachineState
*ms
= MACHINE(obj
);
378 return g_strdup(ms
->firmware
);
381 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
383 MachineState
*ms
= MACHINE(obj
);
385 g_free(ms
->firmware
);
386 ms
->firmware
= g_strdup(value
);
389 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
391 MachineState
*ms
= MACHINE(obj
);
393 ms
->suppress_vmdesc
= value
;
396 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
398 MachineState
*ms
= MACHINE(obj
);
400 return ms
->suppress_vmdesc
;
403 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
406 MachineState
*ms
= MACHINE(obj
);
408 warn_report("enforce-config-section is deprecated, please use "
409 "-global migration.send-configuration=on|off instead");
411 ms
->enforce_config_section
= value
;
414 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
416 MachineState
*ms
= MACHINE(obj
);
418 return ms
->enforce_config_section
;
421 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
423 MachineState
*ms
= MACHINE(obj
);
425 return g_strdup(ms
->memory_encryption
);
428 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
431 MachineState
*ms
= MACHINE(obj
);
433 g_free(ms
->memory_encryption
);
434 ms
->memory_encryption
= g_strdup(value
);
437 * With memory encryption, the host can't see the real contents of RAM,
438 * so there's no point in it trying to merge areas.
441 machine_set_mem_merge(obj
, false, errp
);
445 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
447 MachineState
*ms
= MACHINE(obj
);
449 return ms
->nvdimms_state
->is_enabled
;
452 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
454 MachineState
*ms
= MACHINE(obj
);
456 ms
->nvdimms_state
->is_enabled
= value
;
459 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
461 MachineState
*ms
= MACHINE(obj
);
463 return ms
->numa_state
->hmat_enabled
;
466 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
468 MachineState
*ms
= MACHINE(obj
);
470 ms
->numa_state
->hmat_enabled
= value
;
473 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
475 MachineState
*ms
= MACHINE(obj
);
477 return g_strdup(ms
->nvdimms_state
->persistence_string
);
480 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
483 MachineState
*ms
= MACHINE(obj
);
484 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
486 if (strcmp(value
, "cpu") == 0) {
487 nvdimms_state
->persistence
= 3;
488 } else if (strcmp(value
, "mem-ctrl") == 0) {
489 nvdimms_state
->persistence
= 2;
491 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
496 g_free(nvdimms_state
->persistence_string
);
497 nvdimms_state
->persistence_string
= g_strdup(value
);
500 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
502 strList
*item
= g_new0(strList
, 1);
504 item
->value
= g_strdup(type
);
505 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
506 mc
->allowed_dynamic_sysbus_devices
= item
;
509 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
511 MachineState
*machine
= opaque
;
512 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
513 bool allowed
= false;
516 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
519 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
523 error_report("Option '-device %s' cannot be handled by this machine",
524 object_class_get_name(object_get_class(OBJECT(sbdev
))));
529 static char *machine_get_memdev(Object
*obj
, Error
**errp
)
531 MachineState
*ms
= MACHINE(obj
);
533 return g_strdup(ms
->ram_memdev_id
);
536 static void machine_set_memdev(Object
*obj
, const char *value
, Error
**errp
)
538 MachineState
*ms
= MACHINE(obj
);
540 g_free(ms
->ram_memdev_id
);
541 ms
->ram_memdev_id
= g_strdup(value
);
545 static void machine_init_notify(Notifier
*notifier
, void *data
)
547 MachineState
*machine
= MACHINE(qdev_get_machine());
550 * Loop through all dynamically created sysbus devices and check if they are
551 * all allowed. If a device is not allowed, error out.
553 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
556 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
559 HotpluggableCPUList
*head
= NULL
;
560 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
562 /* force board to initialize possible_cpus if it hasn't been done yet */
563 mc
->possible_cpu_arch_ids(machine
);
565 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
567 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
568 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
570 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
571 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
572 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
573 sizeof(*cpu_item
->props
));
575 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
577 cpu_item
->has_qom_path
= true;
578 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
580 list_item
->value
= cpu_item
;
581 list_item
->next
= head
;
588 * machine_set_cpu_numa_node:
589 * @machine: machine object to modify
590 * @props: specifies which cpu objects to assign to
591 * numa node specified by @props.node_id
592 * @errp: if an error occurs, a pointer to an area to store the error
594 * Associate NUMA node specified by @props.node_id with cpu slots that
595 * match socket/core/thread-ids specified by @props. It's recommended to use
596 * query-hotpluggable-cpus.props values to specify affected cpu slots,
597 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
599 * However for CLI convenience it's possible to pass in subset of properties,
600 * which would affect all cpu slots that match it.
602 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
603 * -numa cpu,node-id=0,socket_id=0 \
604 * -numa cpu,node-id=1,socket_id=1
605 * will assign all child cores of socket 0 to node 0 and
606 * of socket 1 to node 1.
608 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
610 * Empty subset is disallowed and function will return with error in this case.
612 void machine_set_cpu_numa_node(MachineState
*machine
,
613 const CpuInstanceProperties
*props
, Error
**errp
)
615 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
616 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
620 if (!mc
->possible_cpu_arch_ids
) {
621 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
625 /* disabling node mapping is not supported, forbid it */
626 assert(props
->has_node_id
);
628 /* force board to initialize possible_cpus if it hasn't been done yet */
629 mc
->possible_cpu_arch_ids(machine
);
631 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
632 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
634 /* reject unsupported by board properties */
635 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
636 error_setg(errp
, "thread-id is not supported");
640 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
641 error_setg(errp
, "core-id is not supported");
645 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
646 error_setg(errp
, "socket-id is not supported");
650 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
651 error_setg(errp
, "die-id is not supported");
655 /* skip slots with explicit mismatch */
656 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
660 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
664 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
668 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
672 /* reject assignment if slot is already assigned, for compatibility
673 * of legacy cpu_index mapping with SPAPR core based mapping do not
674 * error out if cpu thread and matched core have the same node-id */
675 if (slot
->props
.has_node_id
&&
676 slot
->props
.node_id
!= props
->node_id
) {
677 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
678 slot
->props
.node_id
);
682 /* assign slot to node as it's matched '-numa cpu' key */
684 slot
->props
.node_id
= props
->node_id
;
685 slot
->props
.has_node_id
= props
->has_node_id
;
687 if (machine
->numa_state
->hmat_enabled
) {
688 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
689 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
690 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
691 " should be itself", props
->node_id
);
694 numa_info
[props
->node_id
].has_cpu
= true;
695 numa_info
[props
->node_id
].initiator
= props
->node_id
;
700 error_setg(errp
, "no match found");
704 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
707 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
708 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
709 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
710 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
712 /* compute missing values, prefer sockets over cores over threads */
713 if (cpus
== 0 || sockets
== 0) {
714 cores
= cores
> 0 ? cores
: 1;
715 threads
= threads
> 0 ? threads
: 1;
717 sockets
= sockets
> 0 ? sockets
: 1;
718 cpus
= cores
* threads
* sockets
;
721 qemu_opt_get_number(opts
, "maxcpus", cpus
);
722 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
724 } else if (cores
== 0) {
725 threads
= threads
> 0 ? threads
: 1;
726 cores
= cpus
/ (sockets
* threads
);
727 cores
= cores
> 0 ? cores
: 1;
728 } else if (threads
== 0) {
729 threads
= cpus
/ (cores
* sockets
);
730 threads
= threads
> 0 ? threads
: 1;
731 } else if (sockets
* cores
* threads
< cpus
) {
732 error_report("cpu topology: "
733 "sockets (%u) * cores (%u) * threads (%u) < "
735 sockets
, cores
, threads
, cpus
);
740 qemu_opt_get_number(opts
, "maxcpus", cpus
);
742 if (ms
->smp
.max_cpus
< cpus
) {
743 error_report("maxcpus must be equal to or greater than smp");
747 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
748 error_report("cpu topology: "
749 "sockets (%u) * cores (%u) * threads (%u) > "
751 sockets
, cores
, threads
,
756 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
757 warn_report("Invalid CPU topology deprecated: "
758 "sockets (%u) * cores (%u) * threads (%u) "
760 sockets
, cores
, threads
,
765 ms
->smp
.cores
= cores
;
766 ms
->smp
.threads
= threads
;
767 ms
->smp
.sockets
= sockets
;
770 if (ms
->smp
.cpus
> 1) {
771 Error
*blocker
= NULL
;
772 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
773 replay_add_blocker(blocker
);
777 static void machine_class_init(ObjectClass
*oc
, void *data
)
779 MachineClass
*mc
= MACHINE_CLASS(oc
);
781 /* Default 128 MB as guest ram size */
782 mc
->default_ram_size
= 128 * MiB
;
783 mc
->rom_file_has_mr
= true;
784 mc
->smp_parse
= smp_parse
;
786 /* numa node memory size aligned on 8MB by default.
787 * On Linux, each node's border has to be 8MB aligned
789 mc
->numa_mem_align_shift
= 23;
790 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
792 object_class_property_add_str(oc
, "kernel",
793 machine_get_kernel
, machine_set_kernel
);
794 object_class_property_set_description(oc
, "kernel",
795 "Linux kernel image file");
797 object_class_property_add_str(oc
, "initrd",
798 machine_get_initrd
, machine_set_initrd
);
799 object_class_property_set_description(oc
, "initrd",
800 "Linux initial ramdisk file");
802 object_class_property_add_str(oc
, "append",
803 machine_get_append
, machine_set_append
);
804 object_class_property_set_description(oc
, "append",
805 "Linux kernel command line");
807 object_class_property_add_str(oc
, "dtb",
808 machine_get_dtb
, machine_set_dtb
);
809 object_class_property_set_description(oc
, "dtb",
810 "Linux kernel device tree file");
812 object_class_property_add_str(oc
, "dumpdtb",
813 machine_get_dumpdtb
, machine_set_dumpdtb
);
814 object_class_property_set_description(oc
, "dumpdtb",
815 "Dump current dtb to a file and quit");
817 object_class_property_add(oc
, "phandle-start", "int",
818 machine_get_phandle_start
, machine_set_phandle_start
,
820 object_class_property_set_description(oc
, "phandle-start",
821 "The first phandle ID we may generate dynamically");
823 object_class_property_add_str(oc
, "dt-compatible",
824 machine_get_dt_compatible
, machine_set_dt_compatible
);
825 object_class_property_set_description(oc
, "dt-compatible",
826 "Overrides the \"compatible\" property of the dt root node");
828 object_class_property_add_bool(oc
, "dump-guest-core",
829 machine_get_dump_guest_core
, machine_set_dump_guest_core
);
830 object_class_property_set_description(oc
, "dump-guest-core",
831 "Include guest memory in a core dump");
833 object_class_property_add_bool(oc
, "mem-merge",
834 machine_get_mem_merge
, machine_set_mem_merge
);
835 object_class_property_set_description(oc
, "mem-merge",
836 "Enable/disable memory merge support");
838 object_class_property_add_bool(oc
, "usb",
839 machine_get_usb
, machine_set_usb
);
840 object_class_property_set_description(oc
, "usb",
841 "Set on/off to enable/disable usb");
843 object_class_property_add_bool(oc
, "graphics",
844 machine_get_graphics
, machine_set_graphics
);
845 object_class_property_set_description(oc
, "graphics",
846 "Set on/off to enable/disable graphics emulation");
848 object_class_property_add_str(oc
, "firmware",
849 machine_get_firmware
, machine_set_firmware
);
850 object_class_property_set_description(oc
, "firmware",
853 object_class_property_add_bool(oc
, "suppress-vmdesc",
854 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
);
855 object_class_property_set_description(oc
, "suppress-vmdesc",
856 "Set on to disable self-describing migration");
858 object_class_property_add_bool(oc
, "enforce-config-section",
859 machine_get_enforce_config_section
, machine_set_enforce_config_section
);
860 object_class_property_set_description(oc
, "enforce-config-section",
861 "Set on to enforce configuration section migration");
863 object_class_property_add_str(oc
, "memory-encryption",
864 machine_get_memory_encryption
, machine_set_memory_encryption
);
865 object_class_property_set_description(oc
, "memory-encryption",
866 "Set memory encryption object to use");
869 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
871 if (!object_class_is_abstract(oc
)) {
872 MachineClass
*mc
= MACHINE_CLASS(oc
);
873 const char *cname
= object_class_get_name(oc
);
874 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
875 mc
->name
= g_strndup(cname
,
876 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
877 mc
->compat_props
= g_ptr_array_new();
881 static void machine_initfn(Object
*obj
)
883 MachineState
*ms
= MACHINE(obj
);
884 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
886 ms
->dump_guest_core
= true;
887 ms
->mem_merge
= true;
888 ms
->enable_graphics
= true;
890 if (mc
->nvdimm_supported
) {
891 Object
*obj
= OBJECT(ms
);
893 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
894 object_property_add_bool(obj
, "nvdimm",
895 machine_get_nvdimm
, machine_set_nvdimm
);
896 object_property_set_description(obj
, "nvdimm",
897 "Set on/off to enable/disable "
898 "NVDIMM instantiation");
900 object_property_add_str(obj
, "nvdimm-persistence",
901 machine_get_nvdimm_persistence
,
902 machine_set_nvdimm_persistence
);
903 object_property_set_description(obj
, "nvdimm-persistence",
904 "Set NVDIMM persistence"
905 "Valid values are cpu, mem-ctrl");
908 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
909 ms
->numa_state
= g_new0(NumaState
, 1);
910 object_property_add_bool(obj
, "hmat",
911 machine_get_hmat
, machine_set_hmat
);
912 object_property_set_description(obj
, "hmat",
913 "Set on/off to enable/disable "
914 "ACPI Heterogeneous Memory Attribute "
918 object_property_add_str(obj
, "memory-backend",
919 machine_get_memdev
, machine_set_memdev
);
920 object_property_set_description(obj
, "memory-backend",
922 "Valid value is ID of hostmem based backend");
924 /* Register notifier when init is done for sysbus sanity checks */
925 ms
->sysbus_notifier
.notify
= machine_init_notify
;
926 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
929 static void machine_finalize(Object
*obj
)
931 MachineState
*ms
= MACHINE(obj
);
933 g_free(ms
->kernel_filename
);
934 g_free(ms
->initrd_filename
);
935 g_free(ms
->kernel_cmdline
);
938 g_free(ms
->dt_compatible
);
939 g_free(ms
->firmware
);
940 g_free(ms
->device_memory
);
941 g_free(ms
->nvdimms_state
);
942 g_free(ms
->numa_state
);
945 bool machine_usb(MachineState
*machine
)
950 int machine_phandle_start(MachineState
*machine
)
952 return machine
->phandle_start
;
955 bool machine_dump_guest_core(MachineState
*machine
)
957 return machine
->dump_guest_core
;
960 bool machine_mem_merge(MachineState
*machine
)
962 return machine
->mem_merge
;
965 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
967 GString
*s
= g_string_new(NULL
);
968 if (cpu
->props
.has_socket_id
) {
969 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
971 if (cpu
->props
.has_die_id
) {
972 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
974 if (cpu
->props
.has_core_id
) {
976 g_string_append_printf(s
, ", ");
978 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
980 if (cpu
->props
.has_thread_id
) {
982 g_string_append_printf(s
, ", ");
984 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
986 return g_string_free(s
, false);
989 static void numa_validate_initiator(NumaState
*numa_state
)
992 NodeInfo
*numa_info
= numa_state
->nodes
;
994 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
995 if (numa_info
[i
].initiator
== MAX_NODES
) {
996 error_report("The initiator of NUMA node %d is missing, use "
997 "'-numa node,initiator' option to declare it", i
);
1001 if (!numa_info
[numa_info
[i
].initiator
].present
) {
1002 error_report("NUMA node %" PRIu16
" is missing, use "
1003 "'-numa node' option to declare it first",
1004 numa_info
[i
].initiator
);
1008 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
1009 error_report("The initiator of NUMA node %d is invalid", i
);
1015 static void machine_numa_finish_cpu_init(MachineState
*machine
)
1018 bool default_mapping
;
1019 GString
*s
= g_string_new(NULL
);
1020 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1021 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
1023 assert(machine
->numa_state
->num_nodes
);
1024 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1025 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
1029 default_mapping
= (i
== possible_cpus
->len
);
1031 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1032 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1034 if (!cpu_slot
->props
.has_node_id
) {
1035 /* fetch default mapping from board and enable it */
1036 CpuInstanceProperties props
= cpu_slot
->props
;
1038 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1039 if (!default_mapping
) {
1040 /* record slots with not set mapping,
1041 * TODO: make it hard error in future */
1042 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1043 g_string_append_printf(s
, "%sCPU %d [%s]",
1044 s
->len
? ", " : "", i
, cpu_str
);
1047 /* non mapped cpus used to fallback to node 0 */
1051 props
.has_node_id
= true;
1052 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1056 if (machine
->numa_state
->hmat_enabled
) {
1057 numa_validate_initiator(machine
->numa_state
);
1060 if (s
->len
&& !qtest_enabled()) {
1061 warn_report("CPU(s) not present in any NUMA nodes: %s",
1063 warn_report("All CPU(s) up to maxcpus should be described "
1064 "in NUMA config, ability to start up with partial NUMA "
1065 "mappings is obsoleted and will be removed in future");
1067 g_string_free(s
, true);
1070 MemoryRegion
*machine_consume_memdev(MachineState
*machine
,
1071 HostMemoryBackend
*backend
)
1073 MemoryRegion
*ret
= host_memory_backend_get_memory(backend
);
1075 if (memory_region_is_mapped(ret
)) {
1076 error_report("memory backend %s can't be used multiple times.",
1077 object_get_canonical_path_component(OBJECT(backend
)));
1080 host_memory_backend_set_mapped(backend
, true);
1081 vmstate_register_ram_global(ret
);
1085 void machine_run_board_init(MachineState
*machine
)
1087 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1089 if (machine
->ram_memdev_id
) {
1091 o
= object_resolve_path_type(machine
->ram_memdev_id
,
1092 TYPE_MEMORY_BACKEND
, NULL
);
1093 machine
->ram
= machine_consume_memdev(machine
, MEMORY_BACKEND(o
));
1096 if (machine
->numa_state
) {
1097 numa_complete_configuration(machine
);
1098 if (machine
->numa_state
->num_nodes
) {
1099 machine_numa_finish_cpu_init(machine
);
1103 /* If the machine supports the valid_cpu_types check and the user
1104 * specified a CPU with -cpu check here that the user CPU is supported.
1106 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1107 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1110 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1111 if (object_class_dynamic_cast(class,
1112 machine_class
->valid_cpu_types
[i
])) {
1113 /* The user specificed CPU is in the valid field, we are
1120 if (!machine_class
->valid_cpu_types
[i
]) {
1121 /* The user specified CPU is not valid */
1122 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1123 error_printf("The valid types are: %s",
1124 machine_class
->valid_cpu_types
[0]);
1125 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1126 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1134 machine_class
->init(machine
);
1137 static const TypeInfo machine_info
= {
1138 .name
= TYPE_MACHINE
,
1139 .parent
= TYPE_OBJECT
,
1141 .class_size
= sizeof(MachineClass
),
1142 .class_init
= machine_class_init
,
1143 .class_base_init
= machine_class_base_init
,
1144 .instance_size
= sizeof(MachineState
),
1145 .instance_init
= machine_initfn
,
1146 .instance_finalize
= machine_finalize
,
1149 static void machine_register_types(void)
1151 type_register_static(&machine_info
);
1154 type_init(machine_register_types
)