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"
30 GlobalProperty hw_compat_4_2
[] = {
31 { "virtio-blk-device", "queue-size", "128"},
32 { "virtio-scsi-device", "virtqueue_size", "128"},
33 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
34 { "virtio-blk-device", "seg-max-adjust", "off"},
35 { "virtio-scsi-device", "seg_max_adjust", "off"},
36 { "vhost-blk-device", "seg_max_adjust", "off"},
37 { "usb-host", "suppress-remote-wake", "off" },
38 { "usb-redir", "suppress-remote-wake", "off" },
39 { "qxl", "revision", "4" },
40 { "qxl-vga", "revision", "4" },
42 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
44 GlobalProperty hw_compat_4_1
[] = {
45 { "virtio-pci", "x-pcie-flr-init", "off" },
46 { "virtio-device", "use-disabled-flag", "false" },
48 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
50 GlobalProperty hw_compat_4_0
[] = {
51 { "VGA", "edid", "false" },
52 { "secondary-vga", "edid", "false" },
53 { "bochs-display", "edid", "false" },
54 { "virtio-vga", "edid", "false" },
55 { "virtio-gpu", "edid", "false" },
56 { "virtio-device", "use-started", "false" },
57 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
58 { "pl031", "migrate-tick-offset", "false" },
60 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
62 GlobalProperty hw_compat_3_1
[] = {
63 { "pcie-root-port", "x-speed", "2_5" },
64 { "pcie-root-port", "x-width", "1" },
65 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
66 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
67 { "tpm-crb", "ppi", "false" },
68 { "tpm-tis", "ppi", "false" },
69 { "usb-kbd", "serial", "42" },
70 { "usb-mouse", "serial", "42" },
71 { "usb-tablet", "serial", "42" },
72 { "virtio-blk-device", "discard", "false" },
73 { "virtio-blk-device", "write-zeroes", "false" },
74 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
75 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
77 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
79 GlobalProperty hw_compat_3_0
[] = {};
80 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
82 GlobalProperty hw_compat_2_12
[] = {
83 { "migration", "decompress-error-check", "off" },
84 { "hda-audio", "use-timer", "false" },
85 { "cirrus-vga", "global-vmstate", "true" },
86 { "VGA", "global-vmstate", "true" },
87 { "vmware-svga", "global-vmstate", "true" },
88 { "qxl-vga", "global-vmstate", "true" },
90 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
92 GlobalProperty hw_compat_2_11
[] = {
93 { "hpet", "hpet-offset-saved", "false" },
94 { "virtio-blk-pci", "vectors", "2" },
95 { "vhost-user-blk-pci", "vectors", "2" },
96 { "e1000", "migrate_tso_props", "off" },
98 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
100 GlobalProperty hw_compat_2_10
[] = {
101 { "virtio-mouse-device", "wheel-axis", "false" },
102 { "virtio-tablet-device", "wheel-axis", "false" },
104 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
106 GlobalProperty hw_compat_2_9
[] = {
107 { "pci-bridge", "shpc", "off" },
108 { "intel-iommu", "pt", "off" },
109 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
110 { "pcie-root-port", "x-migrate-msix", "false" },
112 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
114 GlobalProperty hw_compat_2_8
[] = {
115 { "fw_cfg_mem", "x-file-slots", "0x10" },
116 { "fw_cfg_io", "x-file-slots", "0x10" },
117 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
118 { "pci-bridge", "shpc", "on" },
119 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
120 { "virtio-pci", "x-pcie-deverr-init", "off" },
121 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
122 { "virtio-pci", "x-pcie-pm-init", "off" },
123 { "cirrus-vga", "vgamem_mb", "8" },
124 { "isa-cirrus-vga", "vgamem_mb", "8" },
126 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
128 GlobalProperty hw_compat_2_7
[] = {
129 { "virtio-pci", "page-per-vq", "on" },
130 { "virtio-serial-device", "emergency-write", "off" },
131 { "ioapic", "version", "0x11" },
132 { "intel-iommu", "x-buggy-eim", "true" },
133 { "virtio-pci", "x-ignore-backend-features", "on" },
135 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
137 GlobalProperty hw_compat_2_6
[] = {
138 { "virtio-mmio", "format_transport_address", "off" },
139 /* Optional because not all virtio-pci devices support legacy mode */
140 { "virtio-pci", "disable-modern", "on", .optional
= true },
141 { "virtio-pci", "disable-legacy", "off", .optional
= true },
143 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
145 GlobalProperty hw_compat_2_5
[] = {
146 { "isa-fdc", "fallback", "144" },
147 { "pvscsi", "x-old-pci-configuration", "on" },
148 { "pvscsi", "x-disable-pcie", "on" },
149 { "vmxnet3", "x-old-msi-offsets", "on" },
150 { "vmxnet3", "x-disable-pcie", "on" },
152 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
154 GlobalProperty hw_compat_2_4
[] = {
155 /* Optional because the 'scsi' property is Linux-only */
156 { "virtio-blk-device", "scsi", "true", .optional
= true },
157 { "e1000", "extra_mac_registers", "off" },
158 { "virtio-pci", "x-disable-pcie", "on" },
159 { "virtio-pci", "migrate-extra", "off" },
160 { "fw_cfg_mem", "dma_enabled", "off" },
161 { "fw_cfg_io", "dma_enabled", "off" }
163 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
165 GlobalProperty hw_compat_2_3
[] = {
166 { "virtio-blk-pci", "any_layout", "off" },
167 { "virtio-balloon-pci", "any_layout", "off" },
168 { "virtio-serial-pci", "any_layout", "off" },
169 { "virtio-9p-pci", "any_layout", "off" },
170 { "virtio-rng-pci", "any_layout", "off" },
171 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
172 { "migration", "send-configuration", "off" },
173 { "migration", "send-section-footer", "off" },
174 { "migration", "store-global-state", "off" },
176 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
178 GlobalProperty hw_compat_2_2
[] = {};
179 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
181 GlobalProperty hw_compat_2_1
[] = {
182 { "intel-hda", "old_msi_addr", "on" },
183 { "VGA", "qemu-extended-regs", "off" },
184 { "secondary-vga", "qemu-extended-regs", "off" },
185 { "virtio-scsi-pci", "any_layout", "off" },
186 { "usb-mouse", "usb_version", "1" },
187 { "usb-kbd", "usb_version", "1" },
188 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
190 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
192 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
194 MachineState
*ms
= MACHINE(obj
);
196 return g_strdup(ms
->kernel_filename
);
199 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
201 MachineState
*ms
= MACHINE(obj
);
203 g_free(ms
->kernel_filename
);
204 ms
->kernel_filename
= g_strdup(value
);
207 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
209 MachineState
*ms
= MACHINE(obj
);
211 return g_strdup(ms
->initrd_filename
);
214 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
216 MachineState
*ms
= MACHINE(obj
);
218 g_free(ms
->initrd_filename
);
219 ms
->initrd_filename
= g_strdup(value
);
222 static char *machine_get_append(Object
*obj
, Error
**errp
)
224 MachineState
*ms
= MACHINE(obj
);
226 return g_strdup(ms
->kernel_cmdline
);
229 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
231 MachineState
*ms
= MACHINE(obj
);
233 g_free(ms
->kernel_cmdline
);
234 ms
->kernel_cmdline
= g_strdup(value
);
237 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
239 MachineState
*ms
= MACHINE(obj
);
241 return g_strdup(ms
->dtb
);
244 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
246 MachineState
*ms
= MACHINE(obj
);
249 ms
->dtb
= g_strdup(value
);
252 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
254 MachineState
*ms
= MACHINE(obj
);
256 return g_strdup(ms
->dumpdtb
);
259 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
261 MachineState
*ms
= MACHINE(obj
);
264 ms
->dumpdtb
= g_strdup(value
);
267 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
268 const char *name
, void *opaque
,
271 MachineState
*ms
= MACHINE(obj
);
272 int64_t value
= ms
->phandle_start
;
274 visit_type_int(v
, name
, &value
, errp
);
277 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
278 const char *name
, void *opaque
,
281 MachineState
*ms
= MACHINE(obj
);
285 visit_type_int(v
, name
, &value
, &error
);
287 error_propagate(errp
, error
);
291 ms
->phandle_start
= value
;
294 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
296 MachineState
*ms
= MACHINE(obj
);
298 return g_strdup(ms
->dt_compatible
);
301 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
303 MachineState
*ms
= MACHINE(obj
);
305 g_free(ms
->dt_compatible
);
306 ms
->dt_compatible
= g_strdup(value
);
309 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
311 MachineState
*ms
= MACHINE(obj
);
313 return ms
->dump_guest_core
;
316 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
318 MachineState
*ms
= MACHINE(obj
);
320 ms
->dump_guest_core
= value
;
323 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
325 MachineState
*ms
= MACHINE(obj
);
327 return ms
->mem_merge
;
330 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
332 MachineState
*ms
= MACHINE(obj
);
334 ms
->mem_merge
= value
;
337 static bool machine_get_usb(Object
*obj
, Error
**errp
)
339 MachineState
*ms
= MACHINE(obj
);
344 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
346 MachineState
*ms
= MACHINE(obj
);
349 ms
->usb_disabled
= !value
;
352 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
354 MachineState
*ms
= MACHINE(obj
);
356 return ms
->enable_graphics
;
359 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
361 MachineState
*ms
= MACHINE(obj
);
363 ms
->enable_graphics
= value
;
366 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
368 MachineState
*ms
= MACHINE(obj
);
370 return g_strdup(ms
->firmware
);
373 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
375 MachineState
*ms
= MACHINE(obj
);
377 g_free(ms
->firmware
);
378 ms
->firmware
= g_strdup(value
);
381 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
383 MachineState
*ms
= MACHINE(obj
);
385 ms
->suppress_vmdesc
= value
;
388 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
390 MachineState
*ms
= MACHINE(obj
);
392 return ms
->suppress_vmdesc
;
395 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
398 MachineState
*ms
= MACHINE(obj
);
400 warn_report("enforce-config-section is deprecated, please use "
401 "-global migration.send-configuration=on|off instead");
403 ms
->enforce_config_section
= value
;
406 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
408 MachineState
*ms
= MACHINE(obj
);
410 return ms
->enforce_config_section
;
413 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
415 MachineState
*ms
= MACHINE(obj
);
417 return g_strdup(ms
->memory_encryption
);
420 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
423 MachineState
*ms
= MACHINE(obj
);
425 g_free(ms
->memory_encryption
);
426 ms
->memory_encryption
= g_strdup(value
);
429 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
431 MachineState
*ms
= MACHINE(obj
);
433 return ms
->nvdimms_state
->is_enabled
;
436 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
438 MachineState
*ms
= MACHINE(obj
);
440 ms
->nvdimms_state
->is_enabled
= value
;
443 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
445 MachineState
*ms
= MACHINE(obj
);
447 return ms
->numa_state
->hmat_enabled
;
450 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
452 MachineState
*ms
= MACHINE(obj
);
454 ms
->numa_state
->hmat_enabled
= value
;
457 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
459 MachineState
*ms
= MACHINE(obj
);
461 return g_strdup(ms
->nvdimms_state
->persistence_string
);
464 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
467 MachineState
*ms
= MACHINE(obj
);
468 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
470 if (strcmp(value
, "cpu") == 0) {
471 nvdimms_state
->persistence
= 3;
472 } else if (strcmp(value
, "mem-ctrl") == 0) {
473 nvdimms_state
->persistence
= 2;
475 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
480 g_free(nvdimms_state
->persistence_string
);
481 nvdimms_state
->persistence_string
= g_strdup(value
);
484 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
486 strList
*item
= g_new0(strList
, 1);
488 item
->value
= g_strdup(type
);
489 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
490 mc
->allowed_dynamic_sysbus_devices
= item
;
493 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
495 MachineState
*machine
= opaque
;
496 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
497 bool allowed
= false;
500 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
503 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
507 error_report("Option '-device %s' cannot be handled by this machine",
508 object_class_get_name(object_get_class(OBJECT(sbdev
))));
513 static void machine_init_notify(Notifier
*notifier
, void *data
)
515 MachineState
*machine
= MACHINE(qdev_get_machine());
518 * Loop through all dynamically created sysbus devices and check if they are
519 * all allowed. If a device is not allowed, error out.
521 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
524 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
527 HotpluggableCPUList
*head
= NULL
;
528 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
530 /* force board to initialize possible_cpus if it hasn't been done yet */
531 mc
->possible_cpu_arch_ids(machine
);
533 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
535 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
536 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
538 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
539 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
540 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
541 sizeof(*cpu_item
->props
));
543 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
545 cpu_item
->has_qom_path
= true;
546 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
548 list_item
->value
= cpu_item
;
549 list_item
->next
= head
;
556 * machine_set_cpu_numa_node:
557 * @machine: machine object to modify
558 * @props: specifies which cpu objects to assign to
559 * numa node specified by @props.node_id
560 * @errp: if an error occurs, a pointer to an area to store the error
562 * Associate NUMA node specified by @props.node_id with cpu slots that
563 * match socket/core/thread-ids specified by @props. It's recommended to use
564 * query-hotpluggable-cpus.props values to specify affected cpu slots,
565 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
567 * However for CLI convenience it's possible to pass in subset of properties,
568 * which would affect all cpu slots that match it.
570 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
571 * -numa cpu,node-id=0,socket_id=0 \
572 * -numa cpu,node-id=1,socket_id=1
573 * will assign all child cores of socket 0 to node 0 and
574 * of socket 1 to node 1.
576 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
578 * Empty subset is disallowed and function will return with error in this case.
580 void machine_set_cpu_numa_node(MachineState
*machine
,
581 const CpuInstanceProperties
*props
, Error
**errp
)
583 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
584 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
588 if (!mc
->possible_cpu_arch_ids
) {
589 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
593 /* disabling node mapping is not supported, forbid it */
594 assert(props
->has_node_id
);
596 /* force board to initialize possible_cpus if it hasn't been done yet */
597 mc
->possible_cpu_arch_ids(machine
);
599 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
600 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
602 /* reject unsupported by board properties */
603 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
604 error_setg(errp
, "thread-id is not supported");
608 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
609 error_setg(errp
, "core-id is not supported");
613 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
614 error_setg(errp
, "socket-id is not supported");
618 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
619 error_setg(errp
, "die-id is not supported");
623 /* skip slots with explicit mismatch */
624 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
628 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
632 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
636 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
640 /* reject assignment if slot is already assigned, for compatibility
641 * of legacy cpu_index mapping with SPAPR core based mapping do not
642 * error out if cpu thread and matched core have the same node-id */
643 if (slot
->props
.has_node_id
&&
644 slot
->props
.node_id
!= props
->node_id
) {
645 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
646 slot
->props
.node_id
);
650 /* assign slot to node as it's matched '-numa cpu' key */
652 slot
->props
.node_id
= props
->node_id
;
653 slot
->props
.has_node_id
= props
->has_node_id
;
655 if (machine
->numa_state
->hmat_enabled
) {
656 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
657 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
658 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
659 " should be itself", props
->node_id
);
662 numa_info
[props
->node_id
].has_cpu
= true;
663 numa_info
[props
->node_id
].initiator
= props
->node_id
;
668 error_setg(errp
, "no match found");
672 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
675 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
676 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
677 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
678 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
680 /* compute missing values, prefer sockets over cores over threads */
681 if (cpus
== 0 || sockets
== 0) {
682 cores
= cores
> 0 ? cores
: 1;
683 threads
= threads
> 0 ? threads
: 1;
685 sockets
= sockets
> 0 ? sockets
: 1;
686 cpus
= cores
* threads
* sockets
;
689 qemu_opt_get_number(opts
, "maxcpus", cpus
);
690 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
692 } else if (cores
== 0) {
693 threads
= threads
> 0 ? threads
: 1;
694 cores
= cpus
/ (sockets
* threads
);
695 cores
= cores
> 0 ? cores
: 1;
696 } else if (threads
== 0) {
697 threads
= cpus
/ (cores
* sockets
);
698 threads
= threads
> 0 ? threads
: 1;
699 } else if (sockets
* cores
* threads
< cpus
) {
700 error_report("cpu topology: "
701 "sockets (%u) * cores (%u) * threads (%u) < "
703 sockets
, cores
, threads
, cpus
);
708 qemu_opt_get_number(opts
, "maxcpus", cpus
);
710 if (ms
->smp
.max_cpus
< cpus
) {
711 error_report("maxcpus must be equal to or greater than smp");
715 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
716 error_report("cpu topology: "
717 "sockets (%u) * cores (%u) * threads (%u) > "
719 sockets
, cores
, threads
,
724 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
725 warn_report("Invalid CPU topology deprecated: "
726 "sockets (%u) * cores (%u) * threads (%u) "
728 sockets
, cores
, threads
,
733 ms
->smp
.cores
= cores
;
734 ms
->smp
.threads
= threads
;
737 if (ms
->smp
.cpus
> 1) {
738 Error
*blocker
= NULL
;
739 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
740 replay_add_blocker(blocker
);
744 static void machine_class_init(ObjectClass
*oc
, void *data
)
746 MachineClass
*mc
= MACHINE_CLASS(oc
);
748 /* Default 128 MB as guest ram size */
749 mc
->default_ram_size
= 128 * MiB
;
750 mc
->rom_file_has_mr
= true;
751 mc
->smp_parse
= smp_parse
;
753 /* numa node memory size aligned on 8MB by default.
754 * On Linux, each node's border has to be 8MB aligned
756 mc
->numa_mem_align_shift
= 23;
757 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
759 object_class_property_add_str(oc
, "kernel",
760 machine_get_kernel
, machine_set_kernel
, &error_abort
);
761 object_class_property_set_description(oc
, "kernel",
762 "Linux kernel image file", &error_abort
);
764 object_class_property_add_str(oc
, "initrd",
765 machine_get_initrd
, machine_set_initrd
, &error_abort
);
766 object_class_property_set_description(oc
, "initrd",
767 "Linux initial ramdisk file", &error_abort
);
769 object_class_property_add_str(oc
, "append",
770 machine_get_append
, machine_set_append
, &error_abort
);
771 object_class_property_set_description(oc
, "append",
772 "Linux kernel command line", &error_abort
);
774 object_class_property_add_str(oc
, "dtb",
775 machine_get_dtb
, machine_set_dtb
, &error_abort
);
776 object_class_property_set_description(oc
, "dtb",
777 "Linux kernel device tree file", &error_abort
);
779 object_class_property_add_str(oc
, "dumpdtb",
780 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
781 object_class_property_set_description(oc
, "dumpdtb",
782 "Dump current dtb to a file and quit", &error_abort
);
784 object_class_property_add(oc
, "phandle-start", "int",
785 machine_get_phandle_start
, machine_set_phandle_start
,
786 NULL
, NULL
, &error_abort
);
787 object_class_property_set_description(oc
, "phandle-start",
788 "The first phandle ID we may generate dynamically", &error_abort
);
790 object_class_property_add_str(oc
, "dt-compatible",
791 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
792 object_class_property_set_description(oc
, "dt-compatible",
793 "Overrides the \"compatible\" property of the dt root node",
796 object_class_property_add_bool(oc
, "dump-guest-core",
797 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
798 object_class_property_set_description(oc
, "dump-guest-core",
799 "Include guest memory in a core dump", &error_abort
);
801 object_class_property_add_bool(oc
, "mem-merge",
802 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
803 object_class_property_set_description(oc
, "mem-merge",
804 "Enable/disable memory merge support", &error_abort
);
806 object_class_property_add_bool(oc
, "usb",
807 machine_get_usb
, machine_set_usb
, &error_abort
);
808 object_class_property_set_description(oc
, "usb",
809 "Set on/off to enable/disable usb", &error_abort
);
811 object_class_property_add_bool(oc
, "graphics",
812 machine_get_graphics
, machine_set_graphics
, &error_abort
);
813 object_class_property_set_description(oc
, "graphics",
814 "Set on/off to enable/disable graphics emulation", &error_abort
);
816 object_class_property_add_str(oc
, "firmware",
817 machine_get_firmware
, machine_set_firmware
,
819 object_class_property_set_description(oc
, "firmware",
820 "Firmware image", &error_abort
);
822 object_class_property_add_bool(oc
, "suppress-vmdesc",
823 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
825 object_class_property_set_description(oc
, "suppress-vmdesc",
826 "Set on to disable self-describing migration", &error_abort
);
828 object_class_property_add_bool(oc
, "enforce-config-section",
829 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
831 object_class_property_set_description(oc
, "enforce-config-section",
832 "Set on to enforce configuration section migration", &error_abort
);
834 object_class_property_add_str(oc
, "memory-encryption",
835 machine_get_memory_encryption
, machine_set_memory_encryption
,
837 object_class_property_set_description(oc
, "memory-encryption",
838 "Set memory encryption object to use", &error_abort
);
841 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
843 if (!object_class_is_abstract(oc
)) {
844 MachineClass
*mc
= MACHINE_CLASS(oc
);
845 const char *cname
= object_class_get_name(oc
);
846 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
847 mc
->name
= g_strndup(cname
,
848 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
849 mc
->compat_props
= g_ptr_array_new();
853 static void machine_initfn(Object
*obj
)
855 MachineState
*ms
= MACHINE(obj
);
856 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
858 ms
->dump_guest_core
= true;
859 ms
->mem_merge
= true;
860 ms
->enable_graphics
= true;
862 if (mc
->nvdimm_supported
) {
863 Object
*obj
= OBJECT(ms
);
865 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
866 object_property_add_bool(obj
, "nvdimm",
867 machine_get_nvdimm
, machine_set_nvdimm
,
869 object_property_set_description(obj
, "nvdimm",
870 "Set on/off to enable/disable "
871 "NVDIMM instantiation", NULL
);
873 object_property_add_str(obj
, "nvdimm-persistence",
874 machine_get_nvdimm_persistence
,
875 machine_set_nvdimm_persistence
,
877 object_property_set_description(obj
, "nvdimm-persistence",
878 "Set NVDIMM persistence"
879 "Valid values are cpu, mem-ctrl",
883 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
884 ms
->numa_state
= g_new0(NumaState
, 1);
885 object_property_add_bool(obj
, "hmat",
886 machine_get_hmat
, machine_set_hmat
,
888 object_property_set_description(obj
, "hmat",
889 "Set on/off to enable/disable "
890 "ACPI Heterogeneous Memory Attribute "
891 "Table (HMAT)", NULL
);
894 /* Register notifier when init is done for sysbus sanity checks */
895 ms
->sysbus_notifier
.notify
= machine_init_notify
;
896 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
899 static void machine_finalize(Object
*obj
)
901 MachineState
*ms
= MACHINE(obj
);
903 g_free(ms
->kernel_filename
);
904 g_free(ms
->initrd_filename
);
905 g_free(ms
->kernel_cmdline
);
908 g_free(ms
->dt_compatible
);
909 g_free(ms
->firmware
);
910 g_free(ms
->device_memory
);
911 g_free(ms
->nvdimms_state
);
912 g_free(ms
->numa_state
);
915 bool machine_usb(MachineState
*machine
)
920 int machine_phandle_start(MachineState
*machine
)
922 return machine
->phandle_start
;
925 bool machine_dump_guest_core(MachineState
*machine
)
927 return machine
->dump_guest_core
;
930 bool machine_mem_merge(MachineState
*machine
)
932 return machine
->mem_merge
;
935 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
937 GString
*s
= g_string_new(NULL
);
938 if (cpu
->props
.has_socket_id
) {
939 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
941 if (cpu
->props
.has_die_id
) {
942 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
944 if (cpu
->props
.has_core_id
) {
946 g_string_append_printf(s
, ", ");
948 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
950 if (cpu
->props
.has_thread_id
) {
952 g_string_append_printf(s
, ", ");
954 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
956 return g_string_free(s
, false);
959 static void numa_validate_initiator(NumaState
*numa_state
)
962 NodeInfo
*numa_info
= numa_state
->nodes
;
964 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
965 if (numa_info
[i
].initiator
== MAX_NODES
) {
966 error_report("The initiator of NUMA node %d is missing, use "
967 "'-numa node,initiator' option to declare it", i
);
971 if (!numa_info
[numa_info
[i
].initiator
].present
) {
972 error_report("NUMA node %" PRIu16
" is missing, use "
973 "'-numa node' option to declare it first",
974 numa_info
[i
].initiator
);
978 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
979 error_report("The initiator of NUMA node %d is invalid", i
);
985 static void machine_numa_finish_cpu_init(MachineState
*machine
)
988 bool default_mapping
;
989 GString
*s
= g_string_new(NULL
);
990 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
991 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
993 assert(machine
->numa_state
->num_nodes
);
994 for (i
= 0; i
< possible_cpus
->len
; i
++) {
995 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
999 default_mapping
= (i
== possible_cpus
->len
);
1001 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1002 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1004 if (!cpu_slot
->props
.has_node_id
) {
1005 /* fetch default mapping from board and enable it */
1006 CpuInstanceProperties props
= cpu_slot
->props
;
1008 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1009 if (!default_mapping
) {
1010 /* record slots with not set mapping,
1011 * TODO: make it hard error in future */
1012 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1013 g_string_append_printf(s
, "%sCPU %d [%s]",
1014 s
->len
? ", " : "", i
, cpu_str
);
1017 /* non mapped cpus used to fallback to node 0 */
1021 props
.has_node_id
= true;
1022 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1026 if (machine
->numa_state
->hmat_enabled
) {
1027 numa_validate_initiator(machine
->numa_state
);
1030 if (s
->len
&& !qtest_enabled()) {
1031 warn_report("CPU(s) not present in any NUMA nodes: %s",
1033 warn_report("All CPU(s) up to maxcpus should be described "
1034 "in NUMA config, ability to start up with partial NUMA "
1035 "mappings is obsoleted and will be removed in future");
1037 g_string_free(s
, true);
1040 void machine_run_board_init(MachineState
*machine
)
1042 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1044 if (machine
->numa_state
) {
1045 numa_complete_configuration(machine
);
1046 if (machine
->numa_state
->num_nodes
) {
1047 machine_numa_finish_cpu_init(machine
);
1051 /* If the machine supports the valid_cpu_types check and the user
1052 * specified a CPU with -cpu check here that the user CPU is supported.
1054 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1055 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1058 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1059 if (object_class_dynamic_cast(class,
1060 machine_class
->valid_cpu_types
[i
])) {
1061 /* The user specificed CPU is in the valid field, we are
1068 if (!machine_class
->valid_cpu_types
[i
]) {
1069 /* The user specified CPU is not valid */
1070 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1071 error_printf("The valid types are: %s",
1072 machine_class
->valid_cpu_types
[0]);
1073 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1074 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1082 machine_class
->init(machine
);
1085 static const TypeInfo machine_info
= {
1086 .name
= TYPE_MACHINE
,
1087 .parent
= TYPE_OBJECT
,
1089 .class_size
= sizeof(MachineClass
),
1090 .class_init
= machine_class_init
,
1091 .class_base_init
= machine_class_base_init
,
1092 .instance_size
= sizeof(MachineState
),
1093 .instance_init
= machine_initfn
,
1094 .instance_finalize
= machine_finalize
,
1097 static void machine_register_types(void)
1099 type_register_static(&machine_info
);
1102 type_init(machine_register_types
)