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", "x-enable-wce-if-config-wce", "off" },
32 { "virtio-blk-device", "seg-max-adjust", "off"},
33 { "virtio-scsi-device", "seg_max_adjust", "off"},
34 { "vhost-blk-device", "seg_max_adjust", "off"},
35 { "usb-host", "suppress-remote-wake", "off" },
36 { "usb-redir", "suppress-remote-wake", "off" },
38 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
40 GlobalProperty hw_compat_4_1
[] = {
41 { "virtio-pci", "x-pcie-flr-init", "off" },
42 { "virtio-device", "use-disabled-flag", "false" },
44 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
46 GlobalProperty hw_compat_4_0
[] = {
47 { "VGA", "edid", "false" },
48 { "secondary-vga", "edid", "false" },
49 { "bochs-display", "edid", "false" },
50 { "virtio-vga", "edid", "false" },
51 { "virtio-gpu", "edid", "false" },
52 { "virtio-device", "use-started", "false" },
53 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
54 { "pl031", "migrate-tick-offset", "false" },
56 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
58 GlobalProperty hw_compat_3_1
[] = {
59 { "pcie-root-port", "x-speed", "2_5" },
60 { "pcie-root-port", "x-width", "1" },
61 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
62 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
63 { "tpm-crb", "ppi", "false" },
64 { "tpm-tis", "ppi", "false" },
65 { "usb-kbd", "serial", "42" },
66 { "usb-mouse", "serial", "42" },
67 { "usb-tablet", "serial", "42" },
68 { "virtio-blk-device", "discard", "false" },
69 { "virtio-blk-device", "write-zeroes", "false" },
70 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
71 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
73 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
75 GlobalProperty hw_compat_3_0
[] = {};
76 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
78 GlobalProperty hw_compat_2_12
[] = {
79 { "migration", "decompress-error-check", "off" },
80 { "hda-audio", "use-timer", "false" },
81 { "cirrus-vga", "global-vmstate", "true" },
82 { "VGA", "global-vmstate", "true" },
83 { "vmware-svga", "global-vmstate", "true" },
84 { "qxl-vga", "global-vmstate", "true" },
86 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
88 GlobalProperty hw_compat_2_11
[] = {
89 { "hpet", "hpet-offset-saved", "false" },
90 { "virtio-blk-pci", "vectors", "2" },
91 { "vhost-user-blk-pci", "vectors", "2" },
92 { "e1000", "migrate_tso_props", "off" },
94 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
96 GlobalProperty hw_compat_2_10
[] = {
97 { "virtio-mouse-device", "wheel-axis", "false" },
98 { "virtio-tablet-device", "wheel-axis", "false" },
100 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
102 GlobalProperty hw_compat_2_9
[] = {
103 { "pci-bridge", "shpc", "off" },
104 { "intel-iommu", "pt", "off" },
105 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
106 { "pcie-root-port", "x-migrate-msix", "false" },
108 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
110 GlobalProperty hw_compat_2_8
[] = {
111 { "fw_cfg_mem", "x-file-slots", "0x10" },
112 { "fw_cfg_io", "x-file-slots", "0x10" },
113 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
114 { "pci-bridge", "shpc", "on" },
115 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
116 { "virtio-pci", "x-pcie-deverr-init", "off" },
117 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
118 { "virtio-pci", "x-pcie-pm-init", "off" },
119 { "cirrus-vga", "vgamem_mb", "8" },
120 { "isa-cirrus-vga", "vgamem_mb", "8" },
122 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
124 GlobalProperty hw_compat_2_7
[] = {
125 { "virtio-pci", "page-per-vq", "on" },
126 { "virtio-serial-device", "emergency-write", "off" },
127 { "ioapic", "version", "0x11" },
128 { "intel-iommu", "x-buggy-eim", "true" },
129 { "virtio-pci", "x-ignore-backend-features", "on" },
131 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
133 GlobalProperty hw_compat_2_6
[] = {
134 { "virtio-mmio", "format_transport_address", "off" },
135 /* Optional because not all virtio-pci devices support legacy mode */
136 { "virtio-pci", "disable-modern", "on", .optional
= true },
137 { "virtio-pci", "disable-legacy", "off", .optional
= true },
139 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
141 GlobalProperty hw_compat_2_5
[] = {
142 { "isa-fdc", "fallback", "144" },
143 { "pvscsi", "x-old-pci-configuration", "on" },
144 { "pvscsi", "x-disable-pcie", "on" },
145 { "vmxnet3", "x-old-msi-offsets", "on" },
146 { "vmxnet3", "x-disable-pcie", "on" },
148 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
150 GlobalProperty hw_compat_2_4
[] = {
151 { "virtio-blk-device", "scsi", "true" },
152 { "e1000", "extra_mac_registers", "off" },
153 { "virtio-pci", "x-disable-pcie", "on" },
154 { "virtio-pci", "migrate-extra", "off" },
155 { "fw_cfg_mem", "dma_enabled", "off" },
156 { "fw_cfg_io", "dma_enabled", "off" }
158 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
160 GlobalProperty hw_compat_2_3
[] = {
161 { "virtio-blk-pci", "any_layout", "off" },
162 { "virtio-balloon-pci", "any_layout", "off" },
163 { "virtio-serial-pci", "any_layout", "off" },
164 { "virtio-9p-pci", "any_layout", "off" },
165 { "virtio-rng-pci", "any_layout", "off" },
166 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
167 { "migration", "send-configuration", "off" },
168 { "migration", "send-section-footer", "off" },
169 { "migration", "store-global-state", "off" },
171 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
173 GlobalProperty hw_compat_2_2
[] = {};
174 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
176 GlobalProperty hw_compat_2_1
[] = {
177 { "intel-hda", "old_msi_addr", "on" },
178 { "VGA", "qemu-extended-regs", "off" },
179 { "secondary-vga", "qemu-extended-regs", "off" },
180 { "virtio-scsi-pci", "any_layout", "off" },
181 { "usb-mouse", "usb_version", "1" },
182 { "usb-kbd", "usb_version", "1" },
183 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
185 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
187 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
189 MachineState
*ms
= MACHINE(obj
);
191 return g_strdup(ms
->kernel_filename
);
194 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
196 MachineState
*ms
= MACHINE(obj
);
198 g_free(ms
->kernel_filename
);
199 ms
->kernel_filename
= g_strdup(value
);
202 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
204 MachineState
*ms
= MACHINE(obj
);
206 return g_strdup(ms
->initrd_filename
);
209 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
211 MachineState
*ms
= MACHINE(obj
);
213 g_free(ms
->initrd_filename
);
214 ms
->initrd_filename
= g_strdup(value
);
217 static char *machine_get_append(Object
*obj
, Error
**errp
)
219 MachineState
*ms
= MACHINE(obj
);
221 return g_strdup(ms
->kernel_cmdline
);
224 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
226 MachineState
*ms
= MACHINE(obj
);
228 g_free(ms
->kernel_cmdline
);
229 ms
->kernel_cmdline
= g_strdup(value
);
232 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
234 MachineState
*ms
= MACHINE(obj
);
236 return g_strdup(ms
->dtb
);
239 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
241 MachineState
*ms
= MACHINE(obj
);
244 ms
->dtb
= g_strdup(value
);
247 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
249 MachineState
*ms
= MACHINE(obj
);
251 return g_strdup(ms
->dumpdtb
);
254 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
256 MachineState
*ms
= MACHINE(obj
);
259 ms
->dumpdtb
= g_strdup(value
);
262 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
263 const char *name
, void *opaque
,
266 MachineState
*ms
= MACHINE(obj
);
267 int64_t value
= ms
->phandle_start
;
269 visit_type_int(v
, name
, &value
, errp
);
272 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
273 const char *name
, void *opaque
,
276 MachineState
*ms
= MACHINE(obj
);
280 visit_type_int(v
, name
, &value
, &error
);
282 error_propagate(errp
, error
);
286 ms
->phandle_start
= value
;
289 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
291 MachineState
*ms
= MACHINE(obj
);
293 return g_strdup(ms
->dt_compatible
);
296 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
298 MachineState
*ms
= MACHINE(obj
);
300 g_free(ms
->dt_compatible
);
301 ms
->dt_compatible
= g_strdup(value
);
304 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
306 MachineState
*ms
= MACHINE(obj
);
308 return ms
->dump_guest_core
;
311 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
313 MachineState
*ms
= MACHINE(obj
);
315 ms
->dump_guest_core
= value
;
318 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
320 MachineState
*ms
= MACHINE(obj
);
322 return ms
->mem_merge
;
325 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
327 MachineState
*ms
= MACHINE(obj
);
329 ms
->mem_merge
= value
;
332 static bool machine_get_usb(Object
*obj
, Error
**errp
)
334 MachineState
*ms
= MACHINE(obj
);
339 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
341 MachineState
*ms
= MACHINE(obj
);
344 ms
->usb_disabled
= !value
;
347 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
349 MachineState
*ms
= MACHINE(obj
);
351 return ms
->enable_graphics
;
354 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
356 MachineState
*ms
= MACHINE(obj
);
358 ms
->enable_graphics
= value
;
361 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
363 MachineState
*ms
= MACHINE(obj
);
365 return g_strdup(ms
->firmware
);
368 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
370 MachineState
*ms
= MACHINE(obj
);
372 g_free(ms
->firmware
);
373 ms
->firmware
= g_strdup(value
);
376 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
378 MachineState
*ms
= MACHINE(obj
);
380 ms
->suppress_vmdesc
= value
;
383 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
385 MachineState
*ms
= MACHINE(obj
);
387 return ms
->suppress_vmdesc
;
390 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
393 MachineState
*ms
= MACHINE(obj
);
395 warn_report("enforce-config-section is deprecated, please use "
396 "-global migration.send-configuration=on|off instead");
398 ms
->enforce_config_section
= value
;
401 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
403 MachineState
*ms
= MACHINE(obj
);
405 return ms
->enforce_config_section
;
408 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
410 MachineState
*ms
= MACHINE(obj
);
412 return g_strdup(ms
->memory_encryption
);
415 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
418 MachineState
*ms
= MACHINE(obj
);
420 g_free(ms
->memory_encryption
);
421 ms
->memory_encryption
= g_strdup(value
);
424 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
426 MachineState
*ms
= MACHINE(obj
);
428 return ms
->nvdimms_state
->is_enabled
;
431 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
433 MachineState
*ms
= MACHINE(obj
);
435 ms
->nvdimms_state
->is_enabled
= value
;
438 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
440 MachineState
*ms
= MACHINE(obj
);
442 return ms
->numa_state
->hmat_enabled
;
445 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
447 MachineState
*ms
= MACHINE(obj
);
449 ms
->numa_state
->hmat_enabled
= value
;
452 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
454 MachineState
*ms
= MACHINE(obj
);
456 return g_strdup(ms
->nvdimms_state
->persistence_string
);
459 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
462 MachineState
*ms
= MACHINE(obj
);
463 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
465 if (strcmp(value
, "cpu") == 0) {
466 nvdimms_state
->persistence
= 3;
467 } else if (strcmp(value
, "mem-ctrl") == 0) {
468 nvdimms_state
->persistence
= 2;
470 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
475 g_free(nvdimms_state
->persistence_string
);
476 nvdimms_state
->persistence_string
= g_strdup(value
);
479 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
481 strList
*item
= g_new0(strList
, 1);
483 item
->value
= g_strdup(type
);
484 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
485 mc
->allowed_dynamic_sysbus_devices
= item
;
488 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
490 MachineState
*machine
= opaque
;
491 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
492 bool allowed
= false;
495 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
498 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
502 error_report("Option '-device %s' cannot be handled by this machine",
503 object_class_get_name(object_get_class(OBJECT(sbdev
))));
508 static void machine_init_notify(Notifier
*notifier
, void *data
)
510 MachineState
*machine
= MACHINE(qdev_get_machine());
513 * Loop through all dynamically created sysbus devices and check if they are
514 * all allowed. If a device is not allowed, error out.
516 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
519 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
522 HotpluggableCPUList
*head
= NULL
;
523 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
525 /* force board to initialize possible_cpus if it hasn't been done yet */
526 mc
->possible_cpu_arch_ids(machine
);
528 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
530 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
531 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
533 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
534 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
535 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
536 sizeof(*cpu_item
->props
));
538 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
540 cpu_item
->has_qom_path
= true;
541 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
543 list_item
->value
= cpu_item
;
544 list_item
->next
= head
;
551 * machine_set_cpu_numa_node:
552 * @machine: machine object to modify
553 * @props: specifies which cpu objects to assign to
554 * numa node specified by @props.node_id
555 * @errp: if an error occurs, a pointer to an area to store the error
557 * Associate NUMA node specified by @props.node_id with cpu slots that
558 * match socket/core/thread-ids specified by @props. It's recommended to use
559 * query-hotpluggable-cpus.props values to specify affected cpu slots,
560 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
562 * However for CLI convenience it's possible to pass in subset of properties,
563 * which would affect all cpu slots that match it.
565 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
566 * -numa cpu,node-id=0,socket_id=0 \
567 * -numa cpu,node-id=1,socket_id=1
568 * will assign all child cores of socket 0 to node 0 and
569 * of socket 1 to node 1.
571 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
573 * Empty subset is disallowed and function will return with error in this case.
575 void machine_set_cpu_numa_node(MachineState
*machine
,
576 const CpuInstanceProperties
*props
, Error
**errp
)
578 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
579 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
583 if (!mc
->possible_cpu_arch_ids
) {
584 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
588 /* disabling node mapping is not supported, forbid it */
589 assert(props
->has_node_id
);
591 /* force board to initialize possible_cpus if it hasn't been done yet */
592 mc
->possible_cpu_arch_ids(machine
);
594 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
595 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
597 /* reject unsupported by board properties */
598 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
599 error_setg(errp
, "thread-id is not supported");
603 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
604 error_setg(errp
, "core-id is not supported");
608 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
609 error_setg(errp
, "socket-id is not supported");
613 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
614 error_setg(errp
, "die-id is not supported");
618 /* skip slots with explicit mismatch */
619 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
623 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
627 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
631 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
635 /* reject assignment if slot is already assigned, for compatibility
636 * of legacy cpu_index mapping with SPAPR core based mapping do not
637 * error out if cpu thread and matched core have the same node-id */
638 if (slot
->props
.has_node_id
&&
639 slot
->props
.node_id
!= props
->node_id
) {
640 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
641 slot
->props
.node_id
);
645 /* assign slot to node as it's matched '-numa cpu' key */
647 slot
->props
.node_id
= props
->node_id
;
648 slot
->props
.has_node_id
= props
->has_node_id
;
650 if (machine
->numa_state
->hmat_enabled
) {
651 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
652 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
653 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
654 " should be itself", props
->node_id
);
657 numa_info
[props
->node_id
].has_cpu
= true;
658 numa_info
[props
->node_id
].initiator
= props
->node_id
;
663 error_setg(errp
, "no match found");
667 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
670 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
671 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
672 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
673 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
675 /* compute missing values, prefer sockets over cores over threads */
676 if (cpus
== 0 || sockets
== 0) {
677 cores
= cores
> 0 ? cores
: 1;
678 threads
= threads
> 0 ? threads
: 1;
680 sockets
= sockets
> 0 ? sockets
: 1;
681 cpus
= cores
* threads
* sockets
;
684 qemu_opt_get_number(opts
, "maxcpus", cpus
);
685 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
687 } else if (cores
== 0) {
688 threads
= threads
> 0 ? threads
: 1;
689 cores
= cpus
/ (sockets
* threads
);
690 cores
= cores
> 0 ? cores
: 1;
691 } else if (threads
== 0) {
692 threads
= cpus
/ (cores
* sockets
);
693 threads
= threads
> 0 ? threads
: 1;
694 } else if (sockets
* cores
* threads
< cpus
) {
695 error_report("cpu topology: "
696 "sockets (%u) * cores (%u) * threads (%u) < "
698 sockets
, cores
, threads
, cpus
);
703 qemu_opt_get_number(opts
, "maxcpus", cpus
);
705 if (ms
->smp
.max_cpus
< cpus
) {
706 error_report("maxcpus must be equal to or greater than smp");
710 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
711 error_report("cpu topology: "
712 "sockets (%u) * cores (%u) * threads (%u) > "
714 sockets
, cores
, threads
,
719 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
720 warn_report("Invalid CPU topology deprecated: "
721 "sockets (%u) * cores (%u) * threads (%u) "
723 sockets
, cores
, threads
,
728 ms
->smp
.cores
= cores
;
729 ms
->smp
.threads
= threads
;
732 if (ms
->smp
.cpus
> 1) {
733 Error
*blocker
= NULL
;
734 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
735 replay_add_blocker(blocker
);
739 static void machine_class_init(ObjectClass
*oc
, void *data
)
741 MachineClass
*mc
= MACHINE_CLASS(oc
);
743 /* Default 128 MB as guest ram size */
744 mc
->default_ram_size
= 128 * MiB
;
745 mc
->rom_file_has_mr
= true;
746 mc
->smp_parse
= smp_parse
;
748 /* numa node memory size aligned on 8MB by default.
749 * On Linux, each node's border has to be 8MB aligned
751 mc
->numa_mem_align_shift
= 23;
752 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
754 object_class_property_add_str(oc
, "kernel",
755 machine_get_kernel
, machine_set_kernel
, &error_abort
);
756 object_class_property_set_description(oc
, "kernel",
757 "Linux kernel image file", &error_abort
);
759 object_class_property_add_str(oc
, "initrd",
760 machine_get_initrd
, machine_set_initrd
, &error_abort
);
761 object_class_property_set_description(oc
, "initrd",
762 "Linux initial ramdisk file", &error_abort
);
764 object_class_property_add_str(oc
, "append",
765 machine_get_append
, machine_set_append
, &error_abort
);
766 object_class_property_set_description(oc
, "append",
767 "Linux kernel command line", &error_abort
);
769 object_class_property_add_str(oc
, "dtb",
770 machine_get_dtb
, machine_set_dtb
, &error_abort
);
771 object_class_property_set_description(oc
, "dtb",
772 "Linux kernel device tree file", &error_abort
);
774 object_class_property_add_str(oc
, "dumpdtb",
775 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
776 object_class_property_set_description(oc
, "dumpdtb",
777 "Dump current dtb to a file and quit", &error_abort
);
779 object_class_property_add(oc
, "phandle-start", "int",
780 machine_get_phandle_start
, machine_set_phandle_start
,
781 NULL
, NULL
, &error_abort
);
782 object_class_property_set_description(oc
, "phandle-start",
783 "The first phandle ID we may generate dynamically", &error_abort
);
785 object_class_property_add_str(oc
, "dt-compatible",
786 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
787 object_class_property_set_description(oc
, "dt-compatible",
788 "Overrides the \"compatible\" property of the dt root node",
791 object_class_property_add_bool(oc
, "dump-guest-core",
792 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
793 object_class_property_set_description(oc
, "dump-guest-core",
794 "Include guest memory in a core dump", &error_abort
);
796 object_class_property_add_bool(oc
, "mem-merge",
797 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
798 object_class_property_set_description(oc
, "mem-merge",
799 "Enable/disable memory merge support", &error_abort
);
801 object_class_property_add_bool(oc
, "usb",
802 machine_get_usb
, machine_set_usb
, &error_abort
);
803 object_class_property_set_description(oc
, "usb",
804 "Set on/off to enable/disable usb", &error_abort
);
806 object_class_property_add_bool(oc
, "graphics",
807 machine_get_graphics
, machine_set_graphics
, &error_abort
);
808 object_class_property_set_description(oc
, "graphics",
809 "Set on/off to enable/disable graphics emulation", &error_abort
);
811 object_class_property_add_str(oc
, "firmware",
812 machine_get_firmware
, machine_set_firmware
,
814 object_class_property_set_description(oc
, "firmware",
815 "Firmware image", &error_abort
);
817 object_class_property_add_bool(oc
, "suppress-vmdesc",
818 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
820 object_class_property_set_description(oc
, "suppress-vmdesc",
821 "Set on to disable self-describing migration", &error_abort
);
823 object_class_property_add_bool(oc
, "enforce-config-section",
824 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
826 object_class_property_set_description(oc
, "enforce-config-section",
827 "Set on to enforce configuration section migration", &error_abort
);
829 object_class_property_add_str(oc
, "memory-encryption",
830 machine_get_memory_encryption
, machine_set_memory_encryption
,
832 object_class_property_set_description(oc
, "memory-encryption",
833 "Set memory encryption object to use", &error_abort
);
836 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
838 if (!object_class_is_abstract(oc
)) {
839 MachineClass
*mc
= MACHINE_CLASS(oc
);
840 const char *cname
= object_class_get_name(oc
);
841 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
842 mc
->name
= g_strndup(cname
,
843 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
844 mc
->compat_props
= g_ptr_array_new();
848 static void machine_initfn(Object
*obj
)
850 MachineState
*ms
= MACHINE(obj
);
851 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
853 ms
->dump_guest_core
= true;
854 ms
->mem_merge
= true;
855 ms
->enable_graphics
= true;
857 if (mc
->nvdimm_supported
) {
858 Object
*obj
= OBJECT(ms
);
860 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
861 object_property_add_bool(obj
, "nvdimm",
862 machine_get_nvdimm
, machine_set_nvdimm
,
864 object_property_set_description(obj
, "nvdimm",
865 "Set on/off to enable/disable "
866 "NVDIMM instantiation", NULL
);
868 object_property_add_str(obj
, "nvdimm-persistence",
869 machine_get_nvdimm_persistence
,
870 machine_set_nvdimm_persistence
,
872 object_property_set_description(obj
, "nvdimm-persistence",
873 "Set NVDIMM persistence"
874 "Valid values are cpu, mem-ctrl",
878 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
879 ms
->numa_state
= g_new0(NumaState
, 1);
880 object_property_add_bool(obj
, "hmat",
881 machine_get_hmat
, machine_set_hmat
,
883 object_property_set_description(obj
, "hmat",
884 "Set on/off to enable/disable "
885 "ACPI Heterogeneous Memory Attribute "
886 "Table (HMAT)", NULL
);
889 /* Register notifier when init is done for sysbus sanity checks */
890 ms
->sysbus_notifier
.notify
= machine_init_notify
;
891 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
894 static void machine_finalize(Object
*obj
)
896 MachineState
*ms
= MACHINE(obj
);
898 g_free(ms
->kernel_filename
);
899 g_free(ms
->initrd_filename
);
900 g_free(ms
->kernel_cmdline
);
903 g_free(ms
->dt_compatible
);
904 g_free(ms
->firmware
);
905 g_free(ms
->device_memory
);
906 g_free(ms
->nvdimms_state
);
907 g_free(ms
->numa_state
);
910 bool machine_usb(MachineState
*machine
)
915 int machine_phandle_start(MachineState
*machine
)
917 return machine
->phandle_start
;
920 bool machine_dump_guest_core(MachineState
*machine
)
922 return machine
->dump_guest_core
;
925 bool machine_mem_merge(MachineState
*machine
)
927 return machine
->mem_merge
;
930 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
932 GString
*s
= g_string_new(NULL
);
933 if (cpu
->props
.has_socket_id
) {
934 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
936 if (cpu
->props
.has_die_id
) {
937 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
939 if (cpu
->props
.has_core_id
) {
941 g_string_append_printf(s
, ", ");
943 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
945 if (cpu
->props
.has_thread_id
) {
947 g_string_append_printf(s
, ", ");
949 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
951 return g_string_free(s
, false);
954 static void numa_validate_initiator(NumaState
*numa_state
)
957 NodeInfo
*numa_info
= numa_state
->nodes
;
959 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
960 if (numa_info
[i
].initiator
== MAX_NODES
) {
961 error_report("The initiator of NUMA node %d is missing, use "
962 "'-numa node,initiator' option to declare it", i
);
966 if (!numa_info
[numa_info
[i
].initiator
].present
) {
967 error_report("NUMA node %" PRIu16
" is missing, use "
968 "'-numa node' option to declare it first",
969 numa_info
[i
].initiator
);
973 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
974 error_report("The initiator of NUMA node %d is invalid", i
);
980 static void machine_numa_finish_cpu_init(MachineState
*machine
)
983 bool default_mapping
;
984 GString
*s
= g_string_new(NULL
);
985 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
986 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
988 assert(machine
->numa_state
->num_nodes
);
989 for (i
= 0; i
< possible_cpus
->len
; i
++) {
990 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
994 default_mapping
= (i
== possible_cpus
->len
);
996 for (i
= 0; i
< possible_cpus
->len
; i
++) {
997 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
999 if (!cpu_slot
->props
.has_node_id
) {
1000 /* fetch default mapping from board and enable it */
1001 CpuInstanceProperties props
= cpu_slot
->props
;
1003 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1004 if (!default_mapping
) {
1005 /* record slots with not set mapping,
1006 * TODO: make it hard error in future */
1007 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1008 g_string_append_printf(s
, "%sCPU %d [%s]",
1009 s
->len
? ", " : "", i
, cpu_str
);
1012 /* non mapped cpus used to fallback to node 0 */
1016 props
.has_node_id
= true;
1017 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1021 if (machine
->numa_state
->hmat_enabled
) {
1022 numa_validate_initiator(machine
->numa_state
);
1025 if (s
->len
&& !qtest_enabled()) {
1026 warn_report("CPU(s) not present in any NUMA nodes: %s",
1028 warn_report("All CPU(s) up to maxcpus should be described "
1029 "in NUMA config, ability to start up with partial NUMA "
1030 "mappings is obsoleted and will be removed in future");
1032 g_string_free(s
, true);
1035 void machine_run_board_init(MachineState
*machine
)
1037 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1039 if (machine
->numa_state
) {
1040 numa_complete_configuration(machine
);
1041 if (machine
->numa_state
->num_nodes
) {
1042 machine_numa_finish_cpu_init(machine
);
1046 /* If the machine supports the valid_cpu_types check and the user
1047 * specified a CPU with -cpu check here that the user CPU is supported.
1049 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1050 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1053 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1054 if (object_class_dynamic_cast(class,
1055 machine_class
->valid_cpu_types
[i
])) {
1056 /* The user specificed CPU is in the valid field, we are
1063 if (!machine_class
->valid_cpu_types
[i
]) {
1064 /* The user specified CPU is not valid */
1065 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1066 error_printf("The valid types are: %s",
1067 machine_class
->valid_cpu_types
[0]);
1068 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1069 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1077 machine_class
->init(machine
);
1080 static const TypeInfo machine_info
= {
1081 .name
= TYPE_MACHINE
,
1082 .parent
= TYPE_OBJECT
,
1084 .class_size
= sizeof(MachineClass
),
1085 .class_init
= machine_class_init
,
1086 .class_base_init
= machine_class_base_init
,
1087 .instance_size
= sizeof(MachineState
),
1088 .instance_init
= machine_initfn
,
1089 .instance_finalize
= machine_finalize
,
1092 static void machine_register_types(void)
1094 type_register_static(&machine_info
);
1097 type_init(machine_register_types
)