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 const size_t hw_compat_5_0_len
= G_N_ELEMENTS(hw_compat_5_0
);
34 GlobalProperty hw_compat_4_2
[] = {
35 { "virtio-blk-device", "queue-size", "128"},
36 { "virtio-scsi-device", "virtqueue_size", "128"},
37 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
38 { "virtio-blk-device", "seg-max-adjust", "off"},
39 { "virtio-scsi-device", "seg_max_adjust", "off"},
40 { "vhost-blk-device", "seg_max_adjust", "off"},
41 { "usb-host", "suppress-remote-wake", "off" },
42 { "usb-redir", "suppress-remote-wake", "off" },
43 { "qxl", "revision", "4" },
44 { "qxl-vga", "revision", "4" },
45 { "fw_cfg", "acpi-mr-restore", "false" },
47 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
49 GlobalProperty hw_compat_4_1
[] = {
50 { "virtio-pci", "x-pcie-flr-init", "off" },
51 { "virtio-device", "use-disabled-flag", "false" },
53 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
55 GlobalProperty hw_compat_4_0
[] = {
56 { "VGA", "edid", "false" },
57 { "secondary-vga", "edid", "false" },
58 { "bochs-display", "edid", "false" },
59 { "virtio-vga", "edid", "false" },
60 { "virtio-gpu-device", "edid", "false" },
61 { "virtio-device", "use-started", "false" },
62 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
63 { "pl031", "migrate-tick-offset", "false" },
65 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
67 GlobalProperty hw_compat_3_1
[] = {
68 { "pcie-root-port", "x-speed", "2_5" },
69 { "pcie-root-port", "x-width", "1" },
70 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
71 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
72 { "tpm-crb", "ppi", "false" },
73 { "tpm-tis", "ppi", "false" },
74 { "usb-kbd", "serial", "42" },
75 { "usb-mouse", "serial", "42" },
76 { "usb-tablet", "serial", "42" },
77 { "virtio-blk-device", "discard", "false" },
78 { "virtio-blk-device", "write-zeroes", "false" },
79 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
80 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
82 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
84 GlobalProperty hw_compat_3_0
[] = {};
85 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
87 GlobalProperty hw_compat_2_12
[] = {
88 { "migration", "decompress-error-check", "off" },
89 { "hda-audio", "use-timer", "false" },
90 { "cirrus-vga", "global-vmstate", "true" },
91 { "VGA", "global-vmstate", "true" },
92 { "vmware-svga", "global-vmstate", "true" },
93 { "qxl-vga", "global-vmstate", "true" },
95 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
97 GlobalProperty hw_compat_2_11
[] = {
98 { "hpet", "hpet-offset-saved", "false" },
99 { "virtio-blk-pci", "vectors", "2" },
100 { "vhost-user-blk-pci", "vectors", "2" },
101 { "e1000", "migrate_tso_props", "off" },
103 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
105 GlobalProperty hw_compat_2_10
[] = {
106 { "virtio-mouse-device", "wheel-axis", "false" },
107 { "virtio-tablet-device", "wheel-axis", "false" },
109 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
111 GlobalProperty hw_compat_2_9
[] = {
112 { "pci-bridge", "shpc", "off" },
113 { "intel-iommu", "pt", "off" },
114 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
115 { "pcie-root-port", "x-migrate-msix", "false" },
117 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
119 GlobalProperty hw_compat_2_8
[] = {
120 { "fw_cfg_mem", "x-file-slots", "0x10" },
121 { "fw_cfg_io", "x-file-slots", "0x10" },
122 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
123 { "pci-bridge", "shpc", "on" },
124 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
125 { "virtio-pci", "x-pcie-deverr-init", "off" },
126 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
127 { "virtio-pci", "x-pcie-pm-init", "off" },
128 { "cirrus-vga", "vgamem_mb", "8" },
129 { "isa-cirrus-vga", "vgamem_mb", "8" },
131 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
133 GlobalProperty hw_compat_2_7
[] = {
134 { "virtio-pci", "page-per-vq", "on" },
135 { "virtio-serial-device", "emergency-write", "off" },
136 { "ioapic", "version", "0x11" },
137 { "intel-iommu", "x-buggy-eim", "true" },
138 { "virtio-pci", "x-ignore-backend-features", "on" },
140 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
142 GlobalProperty hw_compat_2_6
[] = {
143 { "virtio-mmio", "format_transport_address", "off" },
144 /* Optional because not all virtio-pci devices support legacy mode */
145 { "virtio-pci", "disable-modern", "on", .optional
= true },
146 { "virtio-pci", "disable-legacy", "off", .optional
= true },
148 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
150 GlobalProperty hw_compat_2_5
[] = {
151 { "isa-fdc", "fallback", "144" },
152 { "pvscsi", "x-old-pci-configuration", "on" },
153 { "pvscsi", "x-disable-pcie", "on" },
154 { "vmxnet3", "x-old-msi-offsets", "on" },
155 { "vmxnet3", "x-disable-pcie", "on" },
157 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
159 GlobalProperty hw_compat_2_4
[] = {
160 /* Optional because the 'scsi' property is Linux-only */
161 { "virtio-blk-device", "scsi", "true", .optional
= true },
162 { "e1000", "extra_mac_registers", "off" },
163 { "virtio-pci", "x-disable-pcie", "on" },
164 { "virtio-pci", "migrate-extra", "off" },
165 { "fw_cfg_mem", "dma_enabled", "off" },
166 { "fw_cfg_io", "dma_enabled", "off" }
168 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
170 GlobalProperty hw_compat_2_3
[] = {
171 { "virtio-blk-pci", "any_layout", "off" },
172 { "virtio-balloon-pci", "any_layout", "off" },
173 { "virtio-serial-pci", "any_layout", "off" },
174 { "virtio-9p-pci", "any_layout", "off" },
175 { "virtio-rng-pci", "any_layout", "off" },
176 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
177 { "migration", "send-configuration", "off" },
178 { "migration", "send-section-footer", "off" },
179 { "migration", "store-global-state", "off" },
181 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
183 GlobalProperty hw_compat_2_2
[] = {};
184 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
186 GlobalProperty hw_compat_2_1
[] = {
187 { "intel-hda", "old_msi_addr", "on" },
188 { "VGA", "qemu-extended-regs", "off" },
189 { "secondary-vga", "qemu-extended-regs", "off" },
190 { "virtio-scsi-pci", "any_layout", "off" },
191 { "usb-mouse", "usb_version", "1" },
192 { "usb-kbd", "usb_version", "1" },
193 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
195 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
197 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
199 MachineState
*ms
= MACHINE(obj
);
201 return g_strdup(ms
->kernel_filename
);
204 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
206 MachineState
*ms
= MACHINE(obj
);
208 g_free(ms
->kernel_filename
);
209 ms
->kernel_filename
= g_strdup(value
);
212 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
214 MachineState
*ms
= MACHINE(obj
);
216 return g_strdup(ms
->initrd_filename
);
219 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
221 MachineState
*ms
= MACHINE(obj
);
223 g_free(ms
->initrd_filename
);
224 ms
->initrd_filename
= g_strdup(value
);
227 static char *machine_get_append(Object
*obj
, Error
**errp
)
229 MachineState
*ms
= MACHINE(obj
);
231 return g_strdup(ms
->kernel_cmdline
);
234 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
236 MachineState
*ms
= MACHINE(obj
);
238 g_free(ms
->kernel_cmdline
);
239 ms
->kernel_cmdline
= g_strdup(value
);
242 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
244 MachineState
*ms
= MACHINE(obj
);
246 return g_strdup(ms
->dtb
);
249 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
251 MachineState
*ms
= MACHINE(obj
);
254 ms
->dtb
= g_strdup(value
);
257 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
259 MachineState
*ms
= MACHINE(obj
);
261 return g_strdup(ms
->dumpdtb
);
264 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
266 MachineState
*ms
= MACHINE(obj
);
269 ms
->dumpdtb
= g_strdup(value
);
272 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
273 const char *name
, void *opaque
,
276 MachineState
*ms
= MACHINE(obj
);
277 int64_t value
= ms
->phandle_start
;
279 visit_type_int(v
, name
, &value
, errp
);
282 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
283 const char *name
, void *opaque
,
286 MachineState
*ms
= MACHINE(obj
);
290 visit_type_int(v
, name
, &value
, &error
);
292 error_propagate(errp
, error
);
296 ms
->phandle_start
= value
;
299 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
301 MachineState
*ms
= MACHINE(obj
);
303 return g_strdup(ms
->dt_compatible
);
306 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
308 MachineState
*ms
= MACHINE(obj
);
310 g_free(ms
->dt_compatible
);
311 ms
->dt_compatible
= g_strdup(value
);
314 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
316 MachineState
*ms
= MACHINE(obj
);
318 return ms
->dump_guest_core
;
321 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
323 MachineState
*ms
= MACHINE(obj
);
325 ms
->dump_guest_core
= value
;
328 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
330 MachineState
*ms
= MACHINE(obj
);
332 return ms
->mem_merge
;
335 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
337 MachineState
*ms
= MACHINE(obj
);
339 ms
->mem_merge
= value
;
342 static bool machine_get_usb(Object
*obj
, Error
**errp
)
344 MachineState
*ms
= MACHINE(obj
);
349 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
351 MachineState
*ms
= MACHINE(obj
);
354 ms
->usb_disabled
= !value
;
357 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
359 MachineState
*ms
= MACHINE(obj
);
361 return ms
->enable_graphics
;
364 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
366 MachineState
*ms
= MACHINE(obj
);
368 ms
->enable_graphics
= value
;
371 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
373 MachineState
*ms
= MACHINE(obj
);
375 return g_strdup(ms
->firmware
);
378 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
380 MachineState
*ms
= MACHINE(obj
);
382 g_free(ms
->firmware
);
383 ms
->firmware
= g_strdup(value
);
386 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
388 MachineState
*ms
= MACHINE(obj
);
390 ms
->suppress_vmdesc
= value
;
393 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
395 MachineState
*ms
= MACHINE(obj
);
397 return ms
->suppress_vmdesc
;
400 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
403 MachineState
*ms
= MACHINE(obj
);
405 warn_report("enforce-config-section is deprecated, please use "
406 "-global migration.send-configuration=on|off instead");
408 ms
->enforce_config_section
= value
;
411 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
413 MachineState
*ms
= MACHINE(obj
);
415 return ms
->enforce_config_section
;
418 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
420 MachineState
*ms
= MACHINE(obj
);
422 return g_strdup(ms
->memory_encryption
);
425 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
428 MachineState
*ms
= MACHINE(obj
);
430 g_free(ms
->memory_encryption
);
431 ms
->memory_encryption
= g_strdup(value
);
434 * With memory encryption, the host can't see the real contents of RAM,
435 * so there's no point in it trying to merge areas.
438 machine_set_mem_merge(obj
, false, errp
);
442 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
444 MachineState
*ms
= MACHINE(obj
);
446 return ms
->nvdimms_state
->is_enabled
;
449 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
451 MachineState
*ms
= MACHINE(obj
);
453 ms
->nvdimms_state
->is_enabled
= value
;
456 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
458 MachineState
*ms
= MACHINE(obj
);
460 return ms
->numa_state
->hmat_enabled
;
463 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
465 MachineState
*ms
= MACHINE(obj
);
467 ms
->numa_state
->hmat_enabled
= value
;
470 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
472 MachineState
*ms
= MACHINE(obj
);
474 return g_strdup(ms
->nvdimms_state
->persistence_string
);
477 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
480 MachineState
*ms
= MACHINE(obj
);
481 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
483 if (strcmp(value
, "cpu") == 0) {
484 nvdimms_state
->persistence
= 3;
485 } else if (strcmp(value
, "mem-ctrl") == 0) {
486 nvdimms_state
->persistence
= 2;
488 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
493 g_free(nvdimms_state
->persistence_string
);
494 nvdimms_state
->persistence_string
= g_strdup(value
);
497 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
499 strList
*item
= g_new0(strList
, 1);
501 item
->value
= g_strdup(type
);
502 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
503 mc
->allowed_dynamic_sysbus_devices
= item
;
506 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
508 MachineState
*machine
= opaque
;
509 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
510 bool allowed
= false;
513 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
516 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
520 error_report("Option '-device %s' cannot be handled by this machine",
521 object_class_get_name(object_get_class(OBJECT(sbdev
))));
526 static char *machine_get_memdev(Object
*obj
, Error
**errp
)
528 MachineState
*ms
= MACHINE(obj
);
530 return g_strdup(ms
->ram_memdev_id
);
533 static void machine_set_memdev(Object
*obj
, const char *value
, Error
**errp
)
535 MachineState
*ms
= MACHINE(obj
);
537 g_free(ms
->ram_memdev_id
);
538 ms
->ram_memdev_id
= g_strdup(value
);
542 static void machine_init_notify(Notifier
*notifier
, void *data
)
544 MachineState
*machine
= MACHINE(qdev_get_machine());
547 * Loop through all dynamically created sysbus devices and check if they are
548 * all allowed. If a device is not allowed, error out.
550 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
553 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
556 HotpluggableCPUList
*head
= NULL
;
557 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
559 /* force board to initialize possible_cpus if it hasn't been done yet */
560 mc
->possible_cpu_arch_ids(machine
);
562 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
564 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
565 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
567 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
568 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
569 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
570 sizeof(*cpu_item
->props
));
572 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
574 cpu_item
->has_qom_path
= true;
575 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
577 list_item
->value
= cpu_item
;
578 list_item
->next
= head
;
585 * machine_set_cpu_numa_node:
586 * @machine: machine object to modify
587 * @props: specifies which cpu objects to assign to
588 * numa node specified by @props.node_id
589 * @errp: if an error occurs, a pointer to an area to store the error
591 * Associate NUMA node specified by @props.node_id with cpu slots that
592 * match socket/core/thread-ids specified by @props. It's recommended to use
593 * query-hotpluggable-cpus.props values to specify affected cpu slots,
594 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
596 * However for CLI convenience it's possible to pass in subset of properties,
597 * which would affect all cpu slots that match it.
599 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
600 * -numa cpu,node-id=0,socket_id=0 \
601 * -numa cpu,node-id=1,socket_id=1
602 * will assign all child cores of socket 0 to node 0 and
603 * of socket 1 to node 1.
605 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
607 * Empty subset is disallowed and function will return with error in this case.
609 void machine_set_cpu_numa_node(MachineState
*machine
,
610 const CpuInstanceProperties
*props
, Error
**errp
)
612 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
613 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
617 if (!mc
->possible_cpu_arch_ids
) {
618 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
622 /* disabling node mapping is not supported, forbid it */
623 assert(props
->has_node_id
);
625 /* force board to initialize possible_cpus if it hasn't been done yet */
626 mc
->possible_cpu_arch_ids(machine
);
628 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
629 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
631 /* reject unsupported by board properties */
632 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
633 error_setg(errp
, "thread-id is not supported");
637 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
638 error_setg(errp
, "core-id is not supported");
642 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
643 error_setg(errp
, "socket-id is not supported");
647 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
648 error_setg(errp
, "die-id is not supported");
652 /* skip slots with explicit mismatch */
653 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
657 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
661 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
665 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
669 /* reject assignment if slot is already assigned, for compatibility
670 * of legacy cpu_index mapping with SPAPR core based mapping do not
671 * error out if cpu thread and matched core have the same node-id */
672 if (slot
->props
.has_node_id
&&
673 slot
->props
.node_id
!= props
->node_id
) {
674 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
675 slot
->props
.node_id
);
679 /* assign slot to node as it's matched '-numa cpu' key */
681 slot
->props
.node_id
= props
->node_id
;
682 slot
->props
.has_node_id
= props
->has_node_id
;
684 if (machine
->numa_state
->hmat_enabled
) {
685 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
686 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
687 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
688 " should be itself", props
->node_id
);
691 numa_info
[props
->node_id
].has_cpu
= true;
692 numa_info
[props
->node_id
].initiator
= props
->node_id
;
697 error_setg(errp
, "no match found");
701 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
704 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
705 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
706 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
707 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
709 /* compute missing values, prefer sockets over cores over threads */
710 if (cpus
== 0 || sockets
== 0) {
711 cores
= cores
> 0 ? cores
: 1;
712 threads
= threads
> 0 ? threads
: 1;
714 sockets
= sockets
> 0 ? sockets
: 1;
715 cpus
= cores
* threads
* sockets
;
718 qemu_opt_get_number(opts
, "maxcpus", cpus
);
719 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
721 } else if (cores
== 0) {
722 threads
= threads
> 0 ? threads
: 1;
723 cores
= cpus
/ (sockets
* threads
);
724 cores
= cores
> 0 ? cores
: 1;
725 } else if (threads
== 0) {
726 threads
= cpus
/ (cores
* sockets
);
727 threads
= threads
> 0 ? threads
: 1;
728 } else if (sockets
* cores
* threads
< cpus
) {
729 error_report("cpu topology: "
730 "sockets (%u) * cores (%u) * threads (%u) < "
732 sockets
, cores
, threads
, cpus
);
737 qemu_opt_get_number(opts
, "maxcpus", cpus
);
739 if (ms
->smp
.max_cpus
< cpus
) {
740 error_report("maxcpus must be equal to or greater than smp");
744 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
745 error_report("cpu topology: "
746 "sockets (%u) * cores (%u) * threads (%u) > "
748 sockets
, cores
, threads
,
753 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
754 warn_report("Invalid CPU topology deprecated: "
755 "sockets (%u) * cores (%u) * threads (%u) "
757 sockets
, cores
, threads
,
762 ms
->smp
.cores
= cores
;
763 ms
->smp
.threads
= threads
;
764 ms
->smp
.sockets
= sockets
;
767 if (ms
->smp
.cpus
> 1) {
768 Error
*blocker
= NULL
;
769 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
770 replay_add_blocker(blocker
);
774 static void machine_class_init(ObjectClass
*oc
, void *data
)
776 MachineClass
*mc
= MACHINE_CLASS(oc
);
778 /* Default 128 MB as guest ram size */
779 mc
->default_ram_size
= 128 * MiB
;
780 mc
->rom_file_has_mr
= true;
781 mc
->smp_parse
= smp_parse
;
783 /* numa node memory size aligned on 8MB by default.
784 * On Linux, each node's border has to be 8MB aligned
786 mc
->numa_mem_align_shift
= 23;
787 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
789 object_class_property_add_str(oc
, "kernel",
790 machine_get_kernel
, machine_set_kernel
);
791 object_class_property_set_description(oc
, "kernel",
792 "Linux kernel image file");
794 object_class_property_add_str(oc
, "initrd",
795 machine_get_initrd
, machine_set_initrd
);
796 object_class_property_set_description(oc
, "initrd",
797 "Linux initial ramdisk file");
799 object_class_property_add_str(oc
, "append",
800 machine_get_append
, machine_set_append
);
801 object_class_property_set_description(oc
, "append",
802 "Linux kernel command line");
804 object_class_property_add_str(oc
, "dtb",
805 machine_get_dtb
, machine_set_dtb
);
806 object_class_property_set_description(oc
, "dtb",
807 "Linux kernel device tree file");
809 object_class_property_add_str(oc
, "dumpdtb",
810 machine_get_dumpdtb
, machine_set_dumpdtb
);
811 object_class_property_set_description(oc
, "dumpdtb",
812 "Dump current dtb to a file and quit");
814 object_class_property_add(oc
, "phandle-start", "int",
815 machine_get_phandle_start
, machine_set_phandle_start
,
817 object_class_property_set_description(oc
, "phandle-start",
818 "The first phandle ID we may generate dynamically");
820 object_class_property_add_str(oc
, "dt-compatible",
821 machine_get_dt_compatible
, machine_set_dt_compatible
);
822 object_class_property_set_description(oc
, "dt-compatible",
823 "Overrides the \"compatible\" property of the dt root node");
825 object_class_property_add_bool(oc
, "dump-guest-core",
826 machine_get_dump_guest_core
, machine_set_dump_guest_core
);
827 object_class_property_set_description(oc
, "dump-guest-core",
828 "Include guest memory in a core dump");
830 object_class_property_add_bool(oc
, "mem-merge",
831 machine_get_mem_merge
, machine_set_mem_merge
);
832 object_class_property_set_description(oc
, "mem-merge",
833 "Enable/disable memory merge support");
835 object_class_property_add_bool(oc
, "usb",
836 machine_get_usb
, machine_set_usb
);
837 object_class_property_set_description(oc
, "usb",
838 "Set on/off to enable/disable usb");
840 object_class_property_add_bool(oc
, "graphics",
841 machine_get_graphics
, machine_set_graphics
);
842 object_class_property_set_description(oc
, "graphics",
843 "Set on/off to enable/disable graphics emulation");
845 object_class_property_add_str(oc
, "firmware",
846 machine_get_firmware
, machine_set_firmware
);
847 object_class_property_set_description(oc
, "firmware",
850 object_class_property_add_bool(oc
, "suppress-vmdesc",
851 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
);
852 object_class_property_set_description(oc
, "suppress-vmdesc",
853 "Set on to disable self-describing migration");
855 object_class_property_add_bool(oc
, "enforce-config-section",
856 machine_get_enforce_config_section
, machine_set_enforce_config_section
);
857 object_class_property_set_description(oc
, "enforce-config-section",
858 "Set on to enforce configuration section migration");
860 object_class_property_add_str(oc
, "memory-encryption",
861 machine_get_memory_encryption
, machine_set_memory_encryption
);
862 object_class_property_set_description(oc
, "memory-encryption",
863 "Set memory encryption object to use");
866 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
868 if (!object_class_is_abstract(oc
)) {
869 MachineClass
*mc
= MACHINE_CLASS(oc
);
870 const char *cname
= object_class_get_name(oc
);
871 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
872 mc
->name
= g_strndup(cname
,
873 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
874 mc
->compat_props
= g_ptr_array_new();
878 static void machine_initfn(Object
*obj
)
880 MachineState
*ms
= MACHINE(obj
);
881 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
883 ms
->dump_guest_core
= true;
884 ms
->mem_merge
= true;
885 ms
->enable_graphics
= true;
887 if (mc
->nvdimm_supported
) {
888 Object
*obj
= OBJECT(ms
);
890 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
891 object_property_add_bool(obj
, "nvdimm",
892 machine_get_nvdimm
, machine_set_nvdimm
);
893 object_property_set_description(obj
, "nvdimm",
894 "Set on/off to enable/disable "
895 "NVDIMM instantiation");
897 object_property_add_str(obj
, "nvdimm-persistence",
898 machine_get_nvdimm_persistence
,
899 machine_set_nvdimm_persistence
);
900 object_property_set_description(obj
, "nvdimm-persistence",
901 "Set NVDIMM persistence"
902 "Valid values are cpu, mem-ctrl");
905 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
906 ms
->numa_state
= g_new0(NumaState
, 1);
907 object_property_add_bool(obj
, "hmat",
908 machine_get_hmat
, machine_set_hmat
);
909 object_property_set_description(obj
, "hmat",
910 "Set on/off to enable/disable "
911 "ACPI Heterogeneous Memory Attribute "
915 object_property_add_str(obj
, "memory-backend",
916 machine_get_memdev
, machine_set_memdev
);
917 object_property_set_description(obj
, "memory-backend",
919 "Valid value is ID of hostmem based backend");
921 /* Register notifier when init is done for sysbus sanity checks */
922 ms
->sysbus_notifier
.notify
= machine_init_notify
;
923 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
926 static void machine_finalize(Object
*obj
)
928 MachineState
*ms
= MACHINE(obj
);
930 g_free(ms
->kernel_filename
);
931 g_free(ms
->initrd_filename
);
932 g_free(ms
->kernel_cmdline
);
935 g_free(ms
->dt_compatible
);
936 g_free(ms
->firmware
);
937 g_free(ms
->device_memory
);
938 g_free(ms
->nvdimms_state
);
939 g_free(ms
->numa_state
);
942 bool machine_usb(MachineState
*machine
)
947 int machine_phandle_start(MachineState
*machine
)
949 return machine
->phandle_start
;
952 bool machine_dump_guest_core(MachineState
*machine
)
954 return machine
->dump_guest_core
;
957 bool machine_mem_merge(MachineState
*machine
)
959 return machine
->mem_merge
;
962 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
964 GString
*s
= g_string_new(NULL
);
965 if (cpu
->props
.has_socket_id
) {
966 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
968 if (cpu
->props
.has_die_id
) {
969 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
971 if (cpu
->props
.has_core_id
) {
973 g_string_append_printf(s
, ", ");
975 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
977 if (cpu
->props
.has_thread_id
) {
979 g_string_append_printf(s
, ", ");
981 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
983 return g_string_free(s
, false);
986 static void numa_validate_initiator(NumaState
*numa_state
)
989 NodeInfo
*numa_info
= numa_state
->nodes
;
991 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
992 if (numa_info
[i
].initiator
== MAX_NODES
) {
993 error_report("The initiator of NUMA node %d is missing, use "
994 "'-numa node,initiator' option to declare it", i
);
998 if (!numa_info
[numa_info
[i
].initiator
].present
) {
999 error_report("NUMA node %" PRIu16
" is missing, use "
1000 "'-numa node' option to declare it first",
1001 numa_info
[i
].initiator
);
1005 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
1006 error_report("The initiator of NUMA node %d is invalid", i
);
1012 static void machine_numa_finish_cpu_init(MachineState
*machine
)
1015 bool default_mapping
;
1016 GString
*s
= g_string_new(NULL
);
1017 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1018 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
1020 assert(machine
->numa_state
->num_nodes
);
1021 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1022 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
1026 default_mapping
= (i
== possible_cpus
->len
);
1028 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1029 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1031 if (!cpu_slot
->props
.has_node_id
) {
1032 /* fetch default mapping from board and enable it */
1033 CpuInstanceProperties props
= cpu_slot
->props
;
1035 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1036 if (!default_mapping
) {
1037 /* record slots with not set mapping,
1038 * TODO: make it hard error in future */
1039 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1040 g_string_append_printf(s
, "%sCPU %d [%s]",
1041 s
->len
? ", " : "", i
, cpu_str
);
1044 /* non mapped cpus used to fallback to node 0 */
1048 props
.has_node_id
= true;
1049 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1053 if (machine
->numa_state
->hmat_enabled
) {
1054 numa_validate_initiator(machine
->numa_state
);
1057 if (s
->len
&& !qtest_enabled()) {
1058 warn_report("CPU(s) not present in any NUMA nodes: %s",
1060 warn_report("All CPU(s) up to maxcpus should be described "
1061 "in NUMA config, ability to start up with partial NUMA "
1062 "mappings is obsoleted and will be removed in future");
1064 g_string_free(s
, true);
1067 MemoryRegion
*machine_consume_memdev(MachineState
*machine
,
1068 HostMemoryBackend
*backend
)
1070 MemoryRegion
*ret
= host_memory_backend_get_memory(backend
);
1072 if (memory_region_is_mapped(ret
)) {
1073 char *path
= object_get_canonical_path_component(OBJECT(backend
));
1074 error_report("memory backend %s can't be used multiple times.", path
);
1078 host_memory_backend_set_mapped(backend
, true);
1079 vmstate_register_ram_global(ret
);
1083 void machine_run_board_init(MachineState
*machine
)
1085 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1087 if (machine
->ram_memdev_id
) {
1089 o
= object_resolve_path_type(machine
->ram_memdev_id
,
1090 TYPE_MEMORY_BACKEND
, NULL
);
1091 machine
->ram
= machine_consume_memdev(machine
, MEMORY_BACKEND(o
));
1094 if (machine
->numa_state
) {
1095 numa_complete_configuration(machine
);
1096 if (machine
->numa_state
->num_nodes
) {
1097 machine_numa_finish_cpu_init(machine
);
1101 /* If the machine supports the valid_cpu_types check and the user
1102 * specified a CPU with -cpu check here that the user CPU is supported.
1104 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1105 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1108 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1109 if (object_class_dynamic_cast(class,
1110 machine_class
->valid_cpu_types
[i
])) {
1111 /* The user specificed CPU is in the valid field, we are
1118 if (!machine_class
->valid_cpu_types
[i
]) {
1119 /* The user specified CPU is not valid */
1120 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1121 error_printf("The valid types are: %s",
1122 machine_class
->valid_cpu_types
[0]);
1123 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1124 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1132 machine_class
->init(machine
);
1135 static const TypeInfo machine_info
= {
1136 .name
= TYPE_MACHINE
,
1137 .parent
= TYPE_OBJECT
,
1139 .class_size
= sizeof(MachineClass
),
1140 .class_init
= machine_class_init
,
1141 .class_base_init
= machine_class_base_init
,
1142 .instance_size
= sizeof(MachineState
),
1143 .instance_init
= machine_initfn
,
1144 .instance_finalize
= machine_finalize
,
1147 static void machine_register_types(void)
1149 type_register_static(&machine_info
);
1152 type_init(machine_register_types
)