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_4_2
[] = {
32 { "virtio-blk-device", "queue-size", "128"},
33 { "virtio-scsi-device", "virtqueue_size", "128"},
34 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
35 { "virtio-blk-device", "seg-max-adjust", "off"},
36 { "virtio-scsi-device", "seg_max_adjust", "off"},
37 { "vhost-blk-device", "seg_max_adjust", "off"},
38 { "usb-host", "suppress-remote-wake", "off" },
39 { "usb-redir", "suppress-remote-wake", "off" },
40 { "qxl", "revision", "4" },
41 { "qxl-vga", "revision", "4" },
43 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
45 GlobalProperty hw_compat_4_1
[] = {
46 { "virtio-pci", "x-pcie-flr-init", "off" },
47 { "virtio-device", "use-disabled-flag", "false" },
49 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
51 GlobalProperty hw_compat_4_0
[] = {
52 { "VGA", "edid", "false" },
53 { "secondary-vga", "edid", "false" },
54 { "bochs-display", "edid", "false" },
55 { "virtio-vga", "edid", "false" },
56 { "virtio-gpu", "edid", "false" },
57 { "virtio-device", "use-started", "false" },
58 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
59 { "pl031", "migrate-tick-offset", "false" },
61 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
63 GlobalProperty hw_compat_3_1
[] = {
64 { "pcie-root-port", "x-speed", "2_5" },
65 { "pcie-root-port", "x-width", "1" },
66 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
67 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
68 { "tpm-crb", "ppi", "false" },
69 { "tpm-tis", "ppi", "false" },
70 { "usb-kbd", "serial", "42" },
71 { "usb-mouse", "serial", "42" },
72 { "usb-tablet", "serial", "42" },
73 { "virtio-blk-device", "discard", "false" },
74 { "virtio-blk-device", "write-zeroes", "false" },
75 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
76 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
78 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
80 GlobalProperty hw_compat_3_0
[] = {};
81 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
83 GlobalProperty hw_compat_2_12
[] = {
84 { "migration", "decompress-error-check", "off" },
85 { "hda-audio", "use-timer", "false" },
86 { "cirrus-vga", "global-vmstate", "true" },
87 { "VGA", "global-vmstate", "true" },
88 { "vmware-svga", "global-vmstate", "true" },
89 { "qxl-vga", "global-vmstate", "true" },
91 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
93 GlobalProperty hw_compat_2_11
[] = {
94 { "hpet", "hpet-offset-saved", "false" },
95 { "virtio-blk-pci", "vectors", "2" },
96 { "vhost-user-blk-pci", "vectors", "2" },
97 { "e1000", "migrate_tso_props", "off" },
99 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
101 GlobalProperty hw_compat_2_10
[] = {
102 { "virtio-mouse-device", "wheel-axis", "false" },
103 { "virtio-tablet-device", "wheel-axis", "false" },
105 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
107 GlobalProperty hw_compat_2_9
[] = {
108 { "pci-bridge", "shpc", "off" },
109 { "intel-iommu", "pt", "off" },
110 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
111 { "pcie-root-port", "x-migrate-msix", "false" },
113 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
115 GlobalProperty hw_compat_2_8
[] = {
116 { "fw_cfg_mem", "x-file-slots", "0x10" },
117 { "fw_cfg_io", "x-file-slots", "0x10" },
118 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
119 { "pci-bridge", "shpc", "on" },
120 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
121 { "virtio-pci", "x-pcie-deverr-init", "off" },
122 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
123 { "virtio-pci", "x-pcie-pm-init", "off" },
124 { "cirrus-vga", "vgamem_mb", "8" },
125 { "isa-cirrus-vga", "vgamem_mb", "8" },
127 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
129 GlobalProperty hw_compat_2_7
[] = {
130 { "virtio-pci", "page-per-vq", "on" },
131 { "virtio-serial-device", "emergency-write", "off" },
132 { "ioapic", "version", "0x11" },
133 { "intel-iommu", "x-buggy-eim", "true" },
134 { "virtio-pci", "x-ignore-backend-features", "on" },
136 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
138 GlobalProperty hw_compat_2_6
[] = {
139 { "virtio-mmio", "format_transport_address", "off" },
140 /* Optional because not all virtio-pci devices support legacy mode */
141 { "virtio-pci", "disable-modern", "on", .optional
= true },
142 { "virtio-pci", "disable-legacy", "off", .optional
= true },
144 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
146 GlobalProperty hw_compat_2_5
[] = {
147 { "isa-fdc", "fallback", "144" },
148 { "pvscsi", "x-old-pci-configuration", "on" },
149 { "pvscsi", "x-disable-pcie", "on" },
150 { "vmxnet3", "x-old-msi-offsets", "on" },
151 { "vmxnet3", "x-disable-pcie", "on" },
153 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
155 GlobalProperty hw_compat_2_4
[] = {
156 /* Optional because the 'scsi' property is Linux-only */
157 { "virtio-blk-device", "scsi", "true", .optional
= true },
158 { "e1000", "extra_mac_registers", "off" },
159 { "virtio-pci", "x-disable-pcie", "on" },
160 { "virtio-pci", "migrate-extra", "off" },
161 { "fw_cfg_mem", "dma_enabled", "off" },
162 { "fw_cfg_io", "dma_enabled", "off" }
164 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
166 GlobalProperty hw_compat_2_3
[] = {
167 { "virtio-blk-pci", "any_layout", "off" },
168 { "virtio-balloon-pci", "any_layout", "off" },
169 { "virtio-serial-pci", "any_layout", "off" },
170 { "virtio-9p-pci", "any_layout", "off" },
171 { "virtio-rng-pci", "any_layout", "off" },
172 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
173 { "migration", "send-configuration", "off" },
174 { "migration", "send-section-footer", "off" },
175 { "migration", "store-global-state", "off" },
177 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
179 GlobalProperty hw_compat_2_2
[] = {};
180 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
182 GlobalProperty hw_compat_2_1
[] = {
183 { "intel-hda", "old_msi_addr", "on" },
184 { "VGA", "qemu-extended-regs", "off" },
185 { "secondary-vga", "qemu-extended-regs", "off" },
186 { "virtio-scsi-pci", "any_layout", "off" },
187 { "usb-mouse", "usb_version", "1" },
188 { "usb-kbd", "usb_version", "1" },
189 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
191 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
193 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
195 MachineState
*ms
= MACHINE(obj
);
197 return g_strdup(ms
->kernel_filename
);
200 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
202 MachineState
*ms
= MACHINE(obj
);
204 g_free(ms
->kernel_filename
);
205 ms
->kernel_filename
= g_strdup(value
);
208 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
210 MachineState
*ms
= MACHINE(obj
);
212 return g_strdup(ms
->initrd_filename
);
215 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
217 MachineState
*ms
= MACHINE(obj
);
219 g_free(ms
->initrd_filename
);
220 ms
->initrd_filename
= g_strdup(value
);
223 static char *machine_get_append(Object
*obj
, Error
**errp
)
225 MachineState
*ms
= MACHINE(obj
);
227 return g_strdup(ms
->kernel_cmdline
);
230 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
232 MachineState
*ms
= MACHINE(obj
);
234 g_free(ms
->kernel_cmdline
);
235 ms
->kernel_cmdline
= g_strdup(value
);
238 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
240 MachineState
*ms
= MACHINE(obj
);
242 return g_strdup(ms
->dtb
);
245 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
247 MachineState
*ms
= MACHINE(obj
);
250 ms
->dtb
= g_strdup(value
);
253 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
255 MachineState
*ms
= MACHINE(obj
);
257 return g_strdup(ms
->dumpdtb
);
260 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
262 MachineState
*ms
= MACHINE(obj
);
265 ms
->dumpdtb
= g_strdup(value
);
268 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
269 const char *name
, void *opaque
,
272 MachineState
*ms
= MACHINE(obj
);
273 int64_t value
= ms
->phandle_start
;
275 visit_type_int(v
, name
, &value
, errp
);
278 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
279 const char *name
, void *opaque
,
282 MachineState
*ms
= MACHINE(obj
);
286 visit_type_int(v
, name
, &value
, &error
);
288 error_propagate(errp
, error
);
292 ms
->phandle_start
= value
;
295 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
297 MachineState
*ms
= MACHINE(obj
);
299 return g_strdup(ms
->dt_compatible
);
302 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
304 MachineState
*ms
= MACHINE(obj
);
306 g_free(ms
->dt_compatible
);
307 ms
->dt_compatible
= g_strdup(value
);
310 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
312 MachineState
*ms
= MACHINE(obj
);
314 return ms
->dump_guest_core
;
317 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
319 MachineState
*ms
= MACHINE(obj
);
321 ms
->dump_guest_core
= value
;
324 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
326 MachineState
*ms
= MACHINE(obj
);
328 return ms
->mem_merge
;
331 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
333 MachineState
*ms
= MACHINE(obj
);
335 ms
->mem_merge
= value
;
338 static bool machine_get_usb(Object
*obj
, Error
**errp
)
340 MachineState
*ms
= MACHINE(obj
);
345 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
347 MachineState
*ms
= MACHINE(obj
);
350 ms
->usb_disabled
= !value
;
353 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
355 MachineState
*ms
= MACHINE(obj
);
357 return ms
->enable_graphics
;
360 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
362 MachineState
*ms
= MACHINE(obj
);
364 ms
->enable_graphics
= value
;
367 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
369 MachineState
*ms
= MACHINE(obj
);
371 return g_strdup(ms
->firmware
);
374 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
376 MachineState
*ms
= MACHINE(obj
);
378 g_free(ms
->firmware
);
379 ms
->firmware
= g_strdup(value
);
382 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
384 MachineState
*ms
= MACHINE(obj
);
386 ms
->suppress_vmdesc
= value
;
389 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
391 MachineState
*ms
= MACHINE(obj
);
393 return ms
->suppress_vmdesc
;
396 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
399 MachineState
*ms
= MACHINE(obj
);
401 warn_report("enforce-config-section is deprecated, please use "
402 "-global migration.send-configuration=on|off instead");
404 ms
->enforce_config_section
= value
;
407 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
409 MachineState
*ms
= MACHINE(obj
);
411 return ms
->enforce_config_section
;
414 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
416 MachineState
*ms
= MACHINE(obj
);
418 return g_strdup(ms
->memory_encryption
);
421 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
424 MachineState
*ms
= MACHINE(obj
);
426 g_free(ms
->memory_encryption
);
427 ms
->memory_encryption
= g_strdup(value
);
430 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
432 MachineState
*ms
= MACHINE(obj
);
434 return ms
->nvdimms_state
->is_enabled
;
437 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
439 MachineState
*ms
= MACHINE(obj
);
441 ms
->nvdimms_state
->is_enabled
= value
;
444 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
446 MachineState
*ms
= MACHINE(obj
);
448 return ms
->numa_state
->hmat_enabled
;
451 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
453 MachineState
*ms
= MACHINE(obj
);
455 ms
->numa_state
->hmat_enabled
= value
;
458 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
460 MachineState
*ms
= MACHINE(obj
);
462 return g_strdup(ms
->nvdimms_state
->persistence_string
);
465 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
468 MachineState
*ms
= MACHINE(obj
);
469 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
471 if (strcmp(value
, "cpu") == 0) {
472 nvdimms_state
->persistence
= 3;
473 } else if (strcmp(value
, "mem-ctrl") == 0) {
474 nvdimms_state
->persistence
= 2;
476 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
481 g_free(nvdimms_state
->persistence_string
);
482 nvdimms_state
->persistence_string
= g_strdup(value
);
485 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
487 strList
*item
= g_new0(strList
, 1);
489 item
->value
= g_strdup(type
);
490 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
491 mc
->allowed_dynamic_sysbus_devices
= item
;
494 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
496 MachineState
*machine
= opaque
;
497 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
498 bool allowed
= false;
501 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
504 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
508 error_report("Option '-device %s' cannot be handled by this machine",
509 object_class_get_name(object_get_class(OBJECT(sbdev
))));
514 static char *machine_get_memdev(Object
*obj
, Error
**errp
)
516 MachineState
*ms
= MACHINE(obj
);
518 return g_strdup(ms
->ram_memdev_id
);
521 static void machine_set_memdev(Object
*obj
, const char *value
, Error
**errp
)
523 MachineState
*ms
= MACHINE(obj
);
525 g_free(ms
->ram_memdev_id
);
526 ms
->ram_memdev_id
= g_strdup(value
);
530 static void machine_init_notify(Notifier
*notifier
, void *data
)
532 MachineState
*machine
= MACHINE(qdev_get_machine());
535 * Loop through all dynamically created sysbus devices and check if they are
536 * all allowed. If a device is not allowed, error out.
538 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
541 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
544 HotpluggableCPUList
*head
= NULL
;
545 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
547 /* force board to initialize possible_cpus if it hasn't been done yet */
548 mc
->possible_cpu_arch_ids(machine
);
550 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
552 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
553 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
555 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
556 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
557 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
558 sizeof(*cpu_item
->props
));
560 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
562 cpu_item
->has_qom_path
= true;
563 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
565 list_item
->value
= cpu_item
;
566 list_item
->next
= head
;
573 * machine_set_cpu_numa_node:
574 * @machine: machine object to modify
575 * @props: specifies which cpu objects to assign to
576 * numa node specified by @props.node_id
577 * @errp: if an error occurs, a pointer to an area to store the error
579 * Associate NUMA node specified by @props.node_id with cpu slots that
580 * match socket/core/thread-ids specified by @props. It's recommended to use
581 * query-hotpluggable-cpus.props values to specify affected cpu slots,
582 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
584 * However for CLI convenience it's possible to pass in subset of properties,
585 * which would affect all cpu slots that match it.
587 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
588 * -numa cpu,node-id=0,socket_id=0 \
589 * -numa cpu,node-id=1,socket_id=1
590 * will assign all child cores of socket 0 to node 0 and
591 * of socket 1 to node 1.
593 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
595 * Empty subset is disallowed and function will return with error in this case.
597 void machine_set_cpu_numa_node(MachineState
*machine
,
598 const CpuInstanceProperties
*props
, Error
**errp
)
600 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
601 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
605 if (!mc
->possible_cpu_arch_ids
) {
606 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
610 /* disabling node mapping is not supported, forbid it */
611 assert(props
->has_node_id
);
613 /* force board to initialize possible_cpus if it hasn't been done yet */
614 mc
->possible_cpu_arch_ids(machine
);
616 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
617 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
619 /* reject unsupported by board properties */
620 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
621 error_setg(errp
, "thread-id is not supported");
625 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
626 error_setg(errp
, "core-id is not supported");
630 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
631 error_setg(errp
, "socket-id is not supported");
635 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
636 error_setg(errp
, "die-id is not supported");
640 /* skip slots with explicit mismatch */
641 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
645 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
649 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
653 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
657 /* reject assignment if slot is already assigned, for compatibility
658 * of legacy cpu_index mapping with SPAPR core based mapping do not
659 * error out if cpu thread and matched core have the same node-id */
660 if (slot
->props
.has_node_id
&&
661 slot
->props
.node_id
!= props
->node_id
) {
662 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
663 slot
->props
.node_id
);
667 /* assign slot to node as it's matched '-numa cpu' key */
669 slot
->props
.node_id
= props
->node_id
;
670 slot
->props
.has_node_id
= props
->has_node_id
;
672 if (machine
->numa_state
->hmat_enabled
) {
673 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
674 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
675 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
676 " should be itself", props
->node_id
);
679 numa_info
[props
->node_id
].has_cpu
= true;
680 numa_info
[props
->node_id
].initiator
= props
->node_id
;
685 error_setg(errp
, "no match found");
689 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
692 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
693 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
694 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
695 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
697 /* compute missing values, prefer sockets over cores over threads */
698 if (cpus
== 0 || sockets
== 0) {
699 cores
= cores
> 0 ? cores
: 1;
700 threads
= threads
> 0 ? threads
: 1;
702 sockets
= sockets
> 0 ? sockets
: 1;
703 cpus
= cores
* threads
* sockets
;
706 qemu_opt_get_number(opts
, "maxcpus", cpus
);
707 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
709 } else if (cores
== 0) {
710 threads
= threads
> 0 ? threads
: 1;
711 cores
= cpus
/ (sockets
* threads
);
712 cores
= cores
> 0 ? cores
: 1;
713 } else if (threads
== 0) {
714 threads
= cpus
/ (cores
* sockets
);
715 threads
= threads
> 0 ? threads
: 1;
716 } else if (sockets
* cores
* threads
< cpus
) {
717 error_report("cpu topology: "
718 "sockets (%u) * cores (%u) * threads (%u) < "
720 sockets
, cores
, threads
, cpus
);
725 qemu_opt_get_number(opts
, "maxcpus", cpus
);
727 if (ms
->smp
.max_cpus
< cpus
) {
728 error_report("maxcpus must be equal to or greater than smp");
732 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
733 error_report("cpu topology: "
734 "sockets (%u) * cores (%u) * threads (%u) > "
736 sockets
, cores
, threads
,
741 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
742 warn_report("Invalid CPU topology deprecated: "
743 "sockets (%u) * cores (%u) * threads (%u) "
745 sockets
, cores
, threads
,
750 ms
->smp
.cores
= cores
;
751 ms
->smp
.threads
= threads
;
754 if (ms
->smp
.cpus
> 1) {
755 Error
*blocker
= NULL
;
756 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
757 replay_add_blocker(blocker
);
761 static void machine_class_init(ObjectClass
*oc
, void *data
)
763 MachineClass
*mc
= MACHINE_CLASS(oc
);
765 /* Default 128 MB as guest ram size */
766 mc
->default_ram_size
= 128 * MiB
;
767 mc
->rom_file_has_mr
= true;
768 mc
->smp_parse
= smp_parse
;
770 /* numa node memory size aligned on 8MB by default.
771 * On Linux, each node's border has to be 8MB aligned
773 mc
->numa_mem_align_shift
= 23;
774 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
776 object_class_property_add_str(oc
, "kernel",
777 machine_get_kernel
, machine_set_kernel
, &error_abort
);
778 object_class_property_set_description(oc
, "kernel",
779 "Linux kernel image file", &error_abort
);
781 object_class_property_add_str(oc
, "initrd",
782 machine_get_initrd
, machine_set_initrd
, &error_abort
);
783 object_class_property_set_description(oc
, "initrd",
784 "Linux initial ramdisk file", &error_abort
);
786 object_class_property_add_str(oc
, "append",
787 machine_get_append
, machine_set_append
, &error_abort
);
788 object_class_property_set_description(oc
, "append",
789 "Linux kernel command line", &error_abort
);
791 object_class_property_add_str(oc
, "dtb",
792 machine_get_dtb
, machine_set_dtb
, &error_abort
);
793 object_class_property_set_description(oc
, "dtb",
794 "Linux kernel device tree file", &error_abort
);
796 object_class_property_add_str(oc
, "dumpdtb",
797 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
798 object_class_property_set_description(oc
, "dumpdtb",
799 "Dump current dtb to a file and quit", &error_abort
);
801 object_class_property_add(oc
, "phandle-start", "int",
802 machine_get_phandle_start
, machine_set_phandle_start
,
803 NULL
, NULL
, &error_abort
);
804 object_class_property_set_description(oc
, "phandle-start",
805 "The first phandle ID we may generate dynamically", &error_abort
);
807 object_class_property_add_str(oc
, "dt-compatible",
808 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
809 object_class_property_set_description(oc
, "dt-compatible",
810 "Overrides the \"compatible\" property of the dt root node",
813 object_class_property_add_bool(oc
, "dump-guest-core",
814 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
815 object_class_property_set_description(oc
, "dump-guest-core",
816 "Include guest memory in a core dump", &error_abort
);
818 object_class_property_add_bool(oc
, "mem-merge",
819 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
820 object_class_property_set_description(oc
, "mem-merge",
821 "Enable/disable memory merge support", &error_abort
);
823 object_class_property_add_bool(oc
, "usb",
824 machine_get_usb
, machine_set_usb
, &error_abort
);
825 object_class_property_set_description(oc
, "usb",
826 "Set on/off to enable/disable usb", &error_abort
);
828 object_class_property_add_bool(oc
, "graphics",
829 machine_get_graphics
, machine_set_graphics
, &error_abort
);
830 object_class_property_set_description(oc
, "graphics",
831 "Set on/off to enable/disable graphics emulation", &error_abort
);
833 object_class_property_add_str(oc
, "firmware",
834 machine_get_firmware
, machine_set_firmware
,
836 object_class_property_set_description(oc
, "firmware",
837 "Firmware image", &error_abort
);
839 object_class_property_add_bool(oc
, "suppress-vmdesc",
840 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
842 object_class_property_set_description(oc
, "suppress-vmdesc",
843 "Set on to disable self-describing migration", &error_abort
);
845 object_class_property_add_bool(oc
, "enforce-config-section",
846 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
848 object_class_property_set_description(oc
, "enforce-config-section",
849 "Set on to enforce configuration section migration", &error_abort
);
851 object_class_property_add_str(oc
, "memory-encryption",
852 machine_get_memory_encryption
, machine_set_memory_encryption
,
854 object_class_property_set_description(oc
, "memory-encryption",
855 "Set memory encryption object to use", &error_abort
);
858 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
860 if (!object_class_is_abstract(oc
)) {
861 MachineClass
*mc
= MACHINE_CLASS(oc
);
862 const char *cname
= object_class_get_name(oc
);
863 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
864 mc
->name
= g_strndup(cname
,
865 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
866 mc
->compat_props
= g_ptr_array_new();
870 static void machine_initfn(Object
*obj
)
872 MachineState
*ms
= MACHINE(obj
);
873 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
875 ms
->dump_guest_core
= true;
876 ms
->mem_merge
= true;
877 ms
->enable_graphics
= true;
879 if (mc
->nvdimm_supported
) {
880 Object
*obj
= OBJECT(ms
);
882 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
883 object_property_add_bool(obj
, "nvdimm",
884 machine_get_nvdimm
, machine_set_nvdimm
,
886 object_property_set_description(obj
, "nvdimm",
887 "Set on/off to enable/disable "
888 "NVDIMM instantiation", NULL
);
890 object_property_add_str(obj
, "nvdimm-persistence",
891 machine_get_nvdimm_persistence
,
892 machine_set_nvdimm_persistence
,
894 object_property_set_description(obj
, "nvdimm-persistence",
895 "Set NVDIMM persistence"
896 "Valid values are cpu, mem-ctrl",
900 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
901 ms
->numa_state
= g_new0(NumaState
, 1);
902 object_property_add_bool(obj
, "hmat",
903 machine_get_hmat
, machine_set_hmat
,
905 object_property_set_description(obj
, "hmat",
906 "Set on/off to enable/disable "
907 "ACPI Heterogeneous Memory Attribute "
908 "Table (HMAT)", NULL
);
911 object_property_add_str(obj
, "memory-backend",
912 machine_get_memdev
, machine_set_memdev
,
914 object_property_set_description(obj
, "memory-backend",
916 "Valid value is ID of hostmem based backend",
919 /* Register notifier when init is done for sysbus sanity checks */
920 ms
->sysbus_notifier
.notify
= machine_init_notify
;
921 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
924 static void machine_finalize(Object
*obj
)
926 MachineState
*ms
= MACHINE(obj
);
928 g_free(ms
->kernel_filename
);
929 g_free(ms
->initrd_filename
);
930 g_free(ms
->kernel_cmdline
);
933 g_free(ms
->dt_compatible
);
934 g_free(ms
->firmware
);
935 g_free(ms
->device_memory
);
936 g_free(ms
->nvdimms_state
);
937 g_free(ms
->numa_state
);
940 bool machine_usb(MachineState
*machine
)
945 int machine_phandle_start(MachineState
*machine
)
947 return machine
->phandle_start
;
950 bool machine_dump_guest_core(MachineState
*machine
)
952 return machine
->dump_guest_core
;
955 bool machine_mem_merge(MachineState
*machine
)
957 return machine
->mem_merge
;
960 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
962 GString
*s
= g_string_new(NULL
);
963 if (cpu
->props
.has_socket_id
) {
964 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
966 if (cpu
->props
.has_die_id
) {
967 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
969 if (cpu
->props
.has_core_id
) {
971 g_string_append_printf(s
, ", ");
973 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
975 if (cpu
->props
.has_thread_id
) {
977 g_string_append_printf(s
, ", ");
979 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
981 return g_string_free(s
, false);
984 static void numa_validate_initiator(NumaState
*numa_state
)
987 NodeInfo
*numa_info
= numa_state
->nodes
;
989 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
990 if (numa_info
[i
].initiator
== MAX_NODES
) {
991 error_report("The initiator of NUMA node %d is missing, use "
992 "'-numa node,initiator' option to declare it", i
);
996 if (!numa_info
[numa_info
[i
].initiator
].present
) {
997 error_report("NUMA node %" PRIu16
" is missing, use "
998 "'-numa node' option to declare it first",
999 numa_info
[i
].initiator
);
1003 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
1004 error_report("The initiator of NUMA node %d is invalid", i
);
1010 static void machine_numa_finish_cpu_init(MachineState
*machine
)
1013 bool default_mapping
;
1014 GString
*s
= g_string_new(NULL
);
1015 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1016 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
1018 assert(machine
->numa_state
->num_nodes
);
1019 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1020 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
1024 default_mapping
= (i
== possible_cpus
->len
);
1026 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1027 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1029 if (!cpu_slot
->props
.has_node_id
) {
1030 /* fetch default mapping from board and enable it */
1031 CpuInstanceProperties props
= cpu_slot
->props
;
1033 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1034 if (!default_mapping
) {
1035 /* record slots with not set mapping,
1036 * TODO: make it hard error in future */
1037 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1038 g_string_append_printf(s
, "%sCPU %d [%s]",
1039 s
->len
? ", " : "", i
, cpu_str
);
1042 /* non mapped cpus used to fallback to node 0 */
1046 props
.has_node_id
= true;
1047 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1051 if (machine
->numa_state
->hmat_enabled
) {
1052 numa_validate_initiator(machine
->numa_state
);
1055 if (s
->len
&& !qtest_enabled()) {
1056 warn_report("CPU(s) not present in any NUMA nodes: %s",
1058 warn_report("All CPU(s) up to maxcpus should be described "
1059 "in NUMA config, ability to start up with partial NUMA "
1060 "mappings is obsoleted and will be removed in future");
1062 g_string_free(s
, true);
1065 MemoryRegion
*machine_consume_memdev(MachineState
*machine
,
1066 HostMemoryBackend
*backend
)
1068 MemoryRegion
*ret
= host_memory_backend_get_memory(backend
);
1070 if (memory_region_is_mapped(ret
)) {
1071 char *path
= object_get_canonical_path_component(OBJECT(backend
));
1072 error_report("memory backend %s can't be used multiple times.", path
);
1076 host_memory_backend_set_mapped(backend
, true);
1077 vmstate_register_ram_global(ret
);
1081 void machine_run_board_init(MachineState
*machine
)
1083 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1085 if (machine
->ram_memdev_id
) {
1087 o
= object_resolve_path_type(machine
->ram_memdev_id
,
1088 TYPE_MEMORY_BACKEND
, NULL
);
1089 machine
->ram
= machine_consume_memdev(machine
, MEMORY_BACKEND(o
));
1092 if (machine
->numa_state
) {
1093 numa_complete_configuration(machine
);
1094 if (machine
->numa_state
->num_nodes
) {
1095 machine_numa_finish_cpu_init(machine
);
1099 /* If the machine supports the valid_cpu_types check and the user
1100 * specified a CPU with -cpu check here that the user CPU is supported.
1102 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1103 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1106 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1107 if (object_class_dynamic_cast(class,
1108 machine_class
->valid_cpu_types
[i
])) {
1109 /* The user specificed CPU is in the valid field, we are
1116 if (!machine_class
->valid_cpu_types
[i
]) {
1117 /* The user specified CPU is not valid */
1118 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1119 error_printf("The valid types are: %s",
1120 machine_class
->valid_cpu_types
[0]);
1121 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1122 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1130 machine_class
->init(machine
);
1133 static const TypeInfo machine_info
= {
1134 .name
= TYPE_MACHINE
,
1135 .parent
= TYPE_OBJECT
,
1137 .class_size
= sizeof(MachineClass
),
1138 .class_init
= machine_class_init
,
1139 .class_base_init
= machine_class_base_init
,
1140 .instance_size
= sizeof(MachineState
),
1141 .instance_init
= machine_initfn
,
1142 .instance_finalize
= machine_finalize
,
1145 static void machine_register_types(void)
1147 type_register_static(&machine_info
);
1150 type_init(machine_register_types
)