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"},
36 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
38 GlobalProperty hw_compat_4_1
[] = {
39 { "virtio-pci", "x-pcie-flr-init", "off" },
40 { "virtio-device", "use-disabled-flag", "false" },
42 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
44 GlobalProperty hw_compat_4_0
[] = {
45 { "VGA", "edid", "false" },
46 { "secondary-vga", "edid", "false" },
47 { "bochs-display", "edid", "false" },
48 { "virtio-vga", "edid", "false" },
49 { "virtio-gpu", "edid", "false" },
50 { "virtio-device", "use-started", "false" },
51 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
52 { "pl031", "migrate-tick-offset", "false" },
54 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
56 GlobalProperty hw_compat_3_1
[] = {
57 { "pcie-root-port", "x-speed", "2_5" },
58 { "pcie-root-port", "x-width", "1" },
59 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
60 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
61 { "tpm-crb", "ppi", "false" },
62 { "tpm-tis", "ppi", "false" },
63 { "usb-kbd", "serial", "42" },
64 { "usb-mouse", "serial", "42" },
65 { "usb-tablet", "serial", "42" },
66 { "virtio-blk-device", "discard", "false" },
67 { "virtio-blk-device", "write-zeroes", "false" },
68 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
69 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
71 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
73 GlobalProperty hw_compat_3_0
[] = {};
74 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
76 GlobalProperty hw_compat_2_12
[] = {
77 { "migration", "decompress-error-check", "off" },
78 { "hda-audio", "use-timer", "false" },
79 { "cirrus-vga", "global-vmstate", "true" },
80 { "VGA", "global-vmstate", "true" },
81 { "vmware-svga", "global-vmstate", "true" },
82 { "qxl-vga", "global-vmstate", "true" },
84 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
86 GlobalProperty hw_compat_2_11
[] = {
87 { "hpet", "hpet-offset-saved", "false" },
88 { "virtio-blk-pci", "vectors", "2" },
89 { "vhost-user-blk-pci", "vectors", "2" },
90 { "e1000", "migrate_tso_props", "off" },
92 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
94 GlobalProperty hw_compat_2_10
[] = {
95 { "virtio-mouse-device", "wheel-axis", "false" },
96 { "virtio-tablet-device", "wheel-axis", "false" },
98 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
100 GlobalProperty hw_compat_2_9
[] = {
101 { "pci-bridge", "shpc", "off" },
102 { "intel-iommu", "pt", "off" },
103 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
104 { "pcie-root-port", "x-migrate-msix", "false" },
106 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
108 GlobalProperty hw_compat_2_8
[] = {
109 { "fw_cfg_mem", "x-file-slots", "0x10" },
110 { "fw_cfg_io", "x-file-slots", "0x10" },
111 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
112 { "pci-bridge", "shpc", "on" },
113 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
114 { "virtio-pci", "x-pcie-deverr-init", "off" },
115 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
116 { "virtio-pci", "x-pcie-pm-init", "off" },
117 { "cirrus-vga", "vgamem_mb", "8" },
118 { "isa-cirrus-vga", "vgamem_mb", "8" },
120 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
122 GlobalProperty hw_compat_2_7
[] = {
123 { "virtio-pci", "page-per-vq", "on" },
124 { "virtio-serial-device", "emergency-write", "off" },
125 { "ioapic", "version", "0x11" },
126 { "intel-iommu", "x-buggy-eim", "true" },
127 { "virtio-pci", "x-ignore-backend-features", "on" },
129 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
131 GlobalProperty hw_compat_2_6
[] = {
132 { "virtio-mmio", "format_transport_address", "off" },
133 /* Optional because not all virtio-pci devices support legacy mode */
134 { "virtio-pci", "disable-modern", "on", .optional
= true },
135 { "virtio-pci", "disable-legacy", "off", .optional
= true },
137 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
139 GlobalProperty hw_compat_2_5
[] = {
140 { "isa-fdc", "fallback", "144" },
141 { "pvscsi", "x-old-pci-configuration", "on" },
142 { "pvscsi", "x-disable-pcie", "on" },
143 { "vmxnet3", "x-old-msi-offsets", "on" },
144 { "vmxnet3", "x-disable-pcie", "on" },
146 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
148 GlobalProperty hw_compat_2_4
[] = {
149 { "virtio-blk-device", "scsi", "true" },
150 { "e1000", "extra_mac_registers", "off" },
151 { "virtio-pci", "x-disable-pcie", "on" },
152 { "virtio-pci", "migrate-extra", "off" },
153 { "fw_cfg_mem", "dma_enabled", "off" },
154 { "fw_cfg_io", "dma_enabled", "off" }
156 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
158 GlobalProperty hw_compat_2_3
[] = {
159 { "virtio-blk-pci", "any_layout", "off" },
160 { "virtio-balloon-pci", "any_layout", "off" },
161 { "virtio-serial-pci", "any_layout", "off" },
162 { "virtio-9p-pci", "any_layout", "off" },
163 { "virtio-rng-pci", "any_layout", "off" },
164 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
165 { "migration", "send-configuration", "off" },
166 { "migration", "send-section-footer", "off" },
167 { "migration", "store-global-state", "off" },
169 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
171 GlobalProperty hw_compat_2_2
[] = {};
172 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
174 GlobalProperty hw_compat_2_1
[] = {
175 { "intel-hda", "old_msi_addr", "on" },
176 { "VGA", "qemu-extended-regs", "off" },
177 { "secondary-vga", "qemu-extended-regs", "off" },
178 { "virtio-scsi-pci", "any_layout", "off" },
179 { "usb-mouse", "usb_version", "1" },
180 { "usb-kbd", "usb_version", "1" },
181 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
183 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
185 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
187 MachineState
*ms
= MACHINE(obj
);
189 return g_strdup(ms
->kernel_filename
);
192 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
194 MachineState
*ms
= MACHINE(obj
);
196 g_free(ms
->kernel_filename
);
197 ms
->kernel_filename
= g_strdup(value
);
200 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
202 MachineState
*ms
= MACHINE(obj
);
204 return g_strdup(ms
->initrd_filename
);
207 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
209 MachineState
*ms
= MACHINE(obj
);
211 g_free(ms
->initrd_filename
);
212 ms
->initrd_filename
= g_strdup(value
);
215 static char *machine_get_append(Object
*obj
, Error
**errp
)
217 MachineState
*ms
= MACHINE(obj
);
219 return g_strdup(ms
->kernel_cmdline
);
222 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
224 MachineState
*ms
= MACHINE(obj
);
226 g_free(ms
->kernel_cmdline
);
227 ms
->kernel_cmdline
= g_strdup(value
);
230 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
232 MachineState
*ms
= MACHINE(obj
);
234 return g_strdup(ms
->dtb
);
237 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
239 MachineState
*ms
= MACHINE(obj
);
242 ms
->dtb
= g_strdup(value
);
245 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
247 MachineState
*ms
= MACHINE(obj
);
249 return g_strdup(ms
->dumpdtb
);
252 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
254 MachineState
*ms
= MACHINE(obj
);
257 ms
->dumpdtb
= g_strdup(value
);
260 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
261 const char *name
, void *opaque
,
264 MachineState
*ms
= MACHINE(obj
);
265 int64_t value
= ms
->phandle_start
;
267 visit_type_int(v
, name
, &value
, errp
);
270 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
271 const char *name
, void *opaque
,
274 MachineState
*ms
= MACHINE(obj
);
278 visit_type_int(v
, name
, &value
, &error
);
280 error_propagate(errp
, error
);
284 ms
->phandle_start
= value
;
287 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
289 MachineState
*ms
= MACHINE(obj
);
291 return g_strdup(ms
->dt_compatible
);
294 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
296 MachineState
*ms
= MACHINE(obj
);
298 g_free(ms
->dt_compatible
);
299 ms
->dt_compatible
= g_strdup(value
);
302 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
304 MachineState
*ms
= MACHINE(obj
);
306 return ms
->dump_guest_core
;
309 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
311 MachineState
*ms
= MACHINE(obj
);
313 ms
->dump_guest_core
= value
;
316 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
318 MachineState
*ms
= MACHINE(obj
);
320 return ms
->mem_merge
;
323 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
325 MachineState
*ms
= MACHINE(obj
);
327 ms
->mem_merge
= value
;
330 static bool machine_get_usb(Object
*obj
, Error
**errp
)
332 MachineState
*ms
= MACHINE(obj
);
337 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
339 MachineState
*ms
= MACHINE(obj
);
342 ms
->usb_disabled
= !value
;
345 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
347 MachineState
*ms
= MACHINE(obj
);
349 return ms
->enable_graphics
;
352 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
354 MachineState
*ms
= MACHINE(obj
);
356 ms
->enable_graphics
= value
;
359 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
361 MachineState
*ms
= MACHINE(obj
);
363 return g_strdup(ms
->firmware
);
366 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
368 MachineState
*ms
= MACHINE(obj
);
370 g_free(ms
->firmware
);
371 ms
->firmware
= g_strdup(value
);
374 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
376 MachineState
*ms
= MACHINE(obj
);
378 ms
->suppress_vmdesc
= value
;
381 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
383 MachineState
*ms
= MACHINE(obj
);
385 return ms
->suppress_vmdesc
;
388 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
391 MachineState
*ms
= MACHINE(obj
);
393 warn_report("enforce-config-section is deprecated, please use "
394 "-global migration.send-configuration=on|off instead");
396 ms
->enforce_config_section
= value
;
399 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
401 MachineState
*ms
= MACHINE(obj
);
403 return ms
->enforce_config_section
;
406 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
408 MachineState
*ms
= MACHINE(obj
);
410 return g_strdup(ms
->memory_encryption
);
413 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
416 MachineState
*ms
= MACHINE(obj
);
418 g_free(ms
->memory_encryption
);
419 ms
->memory_encryption
= g_strdup(value
);
422 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
424 MachineState
*ms
= MACHINE(obj
);
426 return ms
->nvdimms_state
->is_enabled
;
429 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
431 MachineState
*ms
= MACHINE(obj
);
433 ms
->nvdimms_state
->is_enabled
= value
;
436 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
438 MachineState
*ms
= MACHINE(obj
);
440 return ms
->numa_state
->hmat_enabled
;
443 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
445 MachineState
*ms
= MACHINE(obj
);
447 ms
->numa_state
->hmat_enabled
= value
;
450 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
452 MachineState
*ms
= MACHINE(obj
);
454 return g_strdup(ms
->nvdimms_state
->persistence_string
);
457 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
460 MachineState
*ms
= MACHINE(obj
);
461 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
463 if (strcmp(value
, "cpu") == 0) {
464 nvdimms_state
->persistence
= 3;
465 } else if (strcmp(value
, "mem-ctrl") == 0) {
466 nvdimms_state
->persistence
= 2;
468 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
473 g_free(nvdimms_state
->persistence_string
);
474 nvdimms_state
->persistence_string
= g_strdup(value
);
477 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
479 strList
*item
= g_new0(strList
, 1);
481 item
->value
= g_strdup(type
);
482 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
483 mc
->allowed_dynamic_sysbus_devices
= item
;
486 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
488 MachineState
*machine
= opaque
;
489 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
490 bool allowed
= false;
493 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
496 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
500 error_report("Option '-device %s' cannot be handled by this machine",
501 object_class_get_name(object_get_class(OBJECT(sbdev
))));
506 static void machine_init_notify(Notifier
*notifier
, void *data
)
508 MachineState
*machine
= MACHINE(qdev_get_machine());
511 * Loop through all dynamically created sysbus devices and check if they are
512 * all allowed. If a device is not allowed, error out.
514 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
517 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
520 HotpluggableCPUList
*head
= NULL
;
521 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
523 /* force board to initialize possible_cpus if it hasn't been done yet */
524 mc
->possible_cpu_arch_ids(machine
);
526 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
528 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
529 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
531 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
532 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
533 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
534 sizeof(*cpu_item
->props
));
536 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
538 cpu_item
->has_qom_path
= true;
539 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
541 list_item
->value
= cpu_item
;
542 list_item
->next
= head
;
549 * machine_set_cpu_numa_node:
550 * @machine: machine object to modify
551 * @props: specifies which cpu objects to assign to
552 * numa node specified by @props.node_id
553 * @errp: if an error occurs, a pointer to an area to store the error
555 * Associate NUMA node specified by @props.node_id with cpu slots that
556 * match socket/core/thread-ids specified by @props. It's recommended to use
557 * query-hotpluggable-cpus.props values to specify affected cpu slots,
558 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
560 * However for CLI convenience it's possible to pass in subset of properties,
561 * which would affect all cpu slots that match it.
563 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
564 * -numa cpu,node-id=0,socket_id=0 \
565 * -numa cpu,node-id=1,socket_id=1
566 * will assign all child cores of socket 0 to node 0 and
567 * of socket 1 to node 1.
569 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
571 * Empty subset is disallowed and function will return with error in this case.
573 void machine_set_cpu_numa_node(MachineState
*machine
,
574 const CpuInstanceProperties
*props
, Error
**errp
)
576 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
577 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
581 if (!mc
->possible_cpu_arch_ids
) {
582 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
586 /* disabling node mapping is not supported, forbid it */
587 assert(props
->has_node_id
);
589 /* force board to initialize possible_cpus if it hasn't been done yet */
590 mc
->possible_cpu_arch_ids(machine
);
592 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
593 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
595 /* reject unsupported by board properties */
596 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
597 error_setg(errp
, "thread-id is not supported");
601 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
602 error_setg(errp
, "core-id is not supported");
606 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
607 error_setg(errp
, "socket-id is not supported");
611 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
612 error_setg(errp
, "die-id is not supported");
616 /* skip slots with explicit mismatch */
617 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
621 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
625 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
629 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
633 /* reject assignment if slot is already assigned, for compatibility
634 * of legacy cpu_index mapping with SPAPR core based mapping do not
635 * error out if cpu thread and matched core have the same node-id */
636 if (slot
->props
.has_node_id
&&
637 slot
->props
.node_id
!= props
->node_id
) {
638 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
639 slot
->props
.node_id
);
643 /* assign slot to node as it's matched '-numa cpu' key */
645 slot
->props
.node_id
= props
->node_id
;
646 slot
->props
.has_node_id
= props
->has_node_id
;
648 if (machine
->numa_state
->hmat_enabled
) {
649 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
650 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
651 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
652 " should be itself", props
->node_id
);
655 numa_info
[props
->node_id
].has_cpu
= true;
656 numa_info
[props
->node_id
].initiator
= props
->node_id
;
661 error_setg(errp
, "no match found");
665 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
668 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
669 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
670 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
671 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
673 /* compute missing values, prefer sockets over cores over threads */
674 if (cpus
== 0 || sockets
== 0) {
675 cores
= cores
> 0 ? cores
: 1;
676 threads
= threads
> 0 ? threads
: 1;
678 sockets
= sockets
> 0 ? sockets
: 1;
679 cpus
= cores
* threads
* sockets
;
682 qemu_opt_get_number(opts
, "maxcpus", cpus
);
683 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
685 } else if (cores
== 0) {
686 threads
= threads
> 0 ? threads
: 1;
687 cores
= cpus
/ (sockets
* threads
);
688 cores
= cores
> 0 ? cores
: 1;
689 } else if (threads
== 0) {
690 threads
= cpus
/ (cores
* sockets
);
691 threads
= threads
> 0 ? threads
: 1;
692 } else if (sockets
* cores
* threads
< cpus
) {
693 error_report("cpu topology: "
694 "sockets (%u) * cores (%u) * threads (%u) < "
696 sockets
, cores
, threads
, cpus
);
701 qemu_opt_get_number(opts
, "maxcpus", cpus
);
703 if (ms
->smp
.max_cpus
< cpus
) {
704 error_report("maxcpus must be equal to or greater than smp");
708 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
709 error_report("cpu topology: "
710 "sockets (%u) * cores (%u) * threads (%u) > "
712 sockets
, cores
, threads
,
717 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
718 warn_report("Invalid CPU topology deprecated: "
719 "sockets (%u) * cores (%u) * threads (%u) "
721 sockets
, cores
, threads
,
726 ms
->smp
.cores
= cores
;
727 ms
->smp
.threads
= threads
;
730 if (ms
->smp
.cpus
> 1) {
731 Error
*blocker
= NULL
;
732 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
733 replay_add_blocker(blocker
);
737 static void machine_class_init(ObjectClass
*oc
, void *data
)
739 MachineClass
*mc
= MACHINE_CLASS(oc
);
741 /* Default 128 MB as guest ram size */
742 mc
->default_ram_size
= 128 * MiB
;
743 mc
->rom_file_has_mr
= true;
744 mc
->smp_parse
= smp_parse
;
746 /* numa node memory size aligned on 8MB by default.
747 * On Linux, each node's border has to be 8MB aligned
749 mc
->numa_mem_align_shift
= 23;
750 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
752 object_class_property_add_str(oc
, "kernel",
753 machine_get_kernel
, machine_set_kernel
, &error_abort
);
754 object_class_property_set_description(oc
, "kernel",
755 "Linux kernel image file", &error_abort
);
757 object_class_property_add_str(oc
, "initrd",
758 machine_get_initrd
, machine_set_initrd
, &error_abort
);
759 object_class_property_set_description(oc
, "initrd",
760 "Linux initial ramdisk file", &error_abort
);
762 object_class_property_add_str(oc
, "append",
763 machine_get_append
, machine_set_append
, &error_abort
);
764 object_class_property_set_description(oc
, "append",
765 "Linux kernel command line", &error_abort
);
767 object_class_property_add_str(oc
, "dtb",
768 machine_get_dtb
, machine_set_dtb
, &error_abort
);
769 object_class_property_set_description(oc
, "dtb",
770 "Linux kernel device tree file", &error_abort
);
772 object_class_property_add_str(oc
, "dumpdtb",
773 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
774 object_class_property_set_description(oc
, "dumpdtb",
775 "Dump current dtb to a file and quit", &error_abort
);
777 object_class_property_add(oc
, "phandle-start", "int",
778 machine_get_phandle_start
, machine_set_phandle_start
,
779 NULL
, NULL
, &error_abort
);
780 object_class_property_set_description(oc
, "phandle-start",
781 "The first phandle ID we may generate dynamically", &error_abort
);
783 object_class_property_add_str(oc
, "dt-compatible",
784 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
785 object_class_property_set_description(oc
, "dt-compatible",
786 "Overrides the \"compatible\" property of the dt root node",
789 object_class_property_add_bool(oc
, "dump-guest-core",
790 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
791 object_class_property_set_description(oc
, "dump-guest-core",
792 "Include guest memory in a core dump", &error_abort
);
794 object_class_property_add_bool(oc
, "mem-merge",
795 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
796 object_class_property_set_description(oc
, "mem-merge",
797 "Enable/disable memory merge support", &error_abort
);
799 object_class_property_add_bool(oc
, "usb",
800 machine_get_usb
, machine_set_usb
, &error_abort
);
801 object_class_property_set_description(oc
, "usb",
802 "Set on/off to enable/disable usb", &error_abort
);
804 object_class_property_add_bool(oc
, "graphics",
805 machine_get_graphics
, machine_set_graphics
, &error_abort
);
806 object_class_property_set_description(oc
, "graphics",
807 "Set on/off to enable/disable graphics emulation", &error_abort
);
809 object_class_property_add_str(oc
, "firmware",
810 machine_get_firmware
, machine_set_firmware
,
812 object_class_property_set_description(oc
, "firmware",
813 "Firmware image", &error_abort
);
815 object_class_property_add_bool(oc
, "suppress-vmdesc",
816 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
818 object_class_property_set_description(oc
, "suppress-vmdesc",
819 "Set on to disable self-describing migration", &error_abort
);
821 object_class_property_add_bool(oc
, "enforce-config-section",
822 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
824 object_class_property_set_description(oc
, "enforce-config-section",
825 "Set on to enforce configuration section migration", &error_abort
);
827 object_class_property_add_str(oc
, "memory-encryption",
828 machine_get_memory_encryption
, machine_set_memory_encryption
,
830 object_class_property_set_description(oc
, "memory-encryption",
831 "Set memory encryption object to use", &error_abort
);
834 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
836 if (!object_class_is_abstract(oc
)) {
837 MachineClass
*mc
= MACHINE_CLASS(oc
);
838 const char *cname
= object_class_get_name(oc
);
839 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
840 mc
->name
= g_strndup(cname
,
841 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
842 mc
->compat_props
= g_ptr_array_new();
846 static void machine_initfn(Object
*obj
)
848 MachineState
*ms
= MACHINE(obj
);
849 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
851 ms
->dump_guest_core
= true;
852 ms
->mem_merge
= true;
853 ms
->enable_graphics
= true;
855 if (mc
->nvdimm_supported
) {
856 Object
*obj
= OBJECT(ms
);
858 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
859 object_property_add_bool(obj
, "nvdimm",
860 machine_get_nvdimm
, machine_set_nvdimm
,
862 object_property_set_description(obj
, "nvdimm",
863 "Set on/off to enable/disable "
864 "NVDIMM instantiation", NULL
);
866 object_property_add_str(obj
, "nvdimm-persistence",
867 machine_get_nvdimm_persistence
,
868 machine_set_nvdimm_persistence
,
870 object_property_set_description(obj
, "nvdimm-persistence",
871 "Set NVDIMM persistence"
872 "Valid values are cpu, mem-ctrl",
876 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
877 ms
->numa_state
= g_new0(NumaState
, 1);
878 object_property_add_bool(obj
, "hmat",
879 machine_get_hmat
, machine_set_hmat
,
881 object_property_set_description(obj
, "hmat",
882 "Set on/off to enable/disable "
883 "ACPI Heterogeneous Memory Attribute "
884 "Table (HMAT)", NULL
);
887 /* Register notifier when init is done for sysbus sanity checks */
888 ms
->sysbus_notifier
.notify
= machine_init_notify
;
889 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
892 static void machine_finalize(Object
*obj
)
894 MachineState
*ms
= MACHINE(obj
);
896 g_free(ms
->kernel_filename
);
897 g_free(ms
->initrd_filename
);
898 g_free(ms
->kernel_cmdline
);
901 g_free(ms
->dt_compatible
);
902 g_free(ms
->firmware
);
903 g_free(ms
->device_memory
);
904 g_free(ms
->nvdimms_state
);
905 g_free(ms
->numa_state
);
908 bool machine_usb(MachineState
*machine
)
913 int machine_phandle_start(MachineState
*machine
)
915 return machine
->phandle_start
;
918 bool machine_dump_guest_core(MachineState
*machine
)
920 return machine
->dump_guest_core
;
923 bool machine_mem_merge(MachineState
*machine
)
925 return machine
->mem_merge
;
928 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
930 GString
*s
= g_string_new(NULL
);
931 if (cpu
->props
.has_socket_id
) {
932 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
934 if (cpu
->props
.has_die_id
) {
935 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
937 if (cpu
->props
.has_core_id
) {
939 g_string_append_printf(s
, ", ");
941 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
943 if (cpu
->props
.has_thread_id
) {
945 g_string_append_printf(s
, ", ");
947 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
949 return g_string_free(s
, false);
952 static void numa_validate_initiator(NumaState
*numa_state
)
955 NodeInfo
*numa_info
= numa_state
->nodes
;
957 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
958 if (numa_info
[i
].initiator
== MAX_NODES
) {
959 error_report("The initiator of NUMA node %d is missing, use "
960 "'-numa node,initiator' option to declare it", i
);
964 if (!numa_info
[numa_info
[i
].initiator
].present
) {
965 error_report("NUMA node %" PRIu16
" is missing, use "
966 "'-numa node' option to declare it first",
967 numa_info
[i
].initiator
);
971 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
972 error_report("The initiator of NUMA node %d is invalid", i
);
978 static void machine_numa_finish_cpu_init(MachineState
*machine
)
981 bool default_mapping
;
982 GString
*s
= g_string_new(NULL
);
983 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
984 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
986 assert(machine
->numa_state
->num_nodes
);
987 for (i
= 0; i
< possible_cpus
->len
; i
++) {
988 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
992 default_mapping
= (i
== possible_cpus
->len
);
994 for (i
= 0; i
< possible_cpus
->len
; i
++) {
995 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
997 if (!cpu_slot
->props
.has_node_id
) {
998 /* fetch default mapping from board and enable it */
999 CpuInstanceProperties props
= cpu_slot
->props
;
1001 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1002 if (!default_mapping
) {
1003 /* record slots with not set mapping,
1004 * TODO: make it hard error in future */
1005 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1006 g_string_append_printf(s
, "%sCPU %d [%s]",
1007 s
->len
? ", " : "", i
, cpu_str
);
1010 /* non mapped cpus used to fallback to node 0 */
1014 props
.has_node_id
= true;
1015 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1019 if (machine
->numa_state
->hmat_enabled
) {
1020 numa_validate_initiator(machine
->numa_state
);
1023 if (s
->len
&& !qtest_enabled()) {
1024 warn_report("CPU(s) not present in any NUMA nodes: %s",
1026 warn_report("All CPU(s) up to maxcpus should be described "
1027 "in NUMA config, ability to start up with partial NUMA "
1028 "mappings is obsoleted and will be removed in future");
1030 g_string_free(s
, true);
1033 void machine_run_board_init(MachineState
*machine
)
1035 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1037 if (machine
->numa_state
) {
1038 numa_complete_configuration(machine
);
1039 if (machine
->numa_state
->num_nodes
) {
1040 machine_numa_finish_cpu_init(machine
);
1044 /* If the machine supports the valid_cpu_types check and the user
1045 * specified a CPU with -cpu check here that the user CPU is supported.
1047 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1048 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1051 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1052 if (object_class_dynamic_cast(class,
1053 machine_class
->valid_cpu_types
[i
])) {
1054 /* The user specificed CPU is in the valid field, we are
1061 if (!machine_class
->valid_cpu_types
[i
]) {
1062 /* The user specified CPU is not valid */
1063 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1064 error_printf("The valid types are: %s",
1065 machine_class
->valid_cpu_types
[0]);
1066 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1067 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1075 machine_class
->init(machine
);
1078 static const TypeInfo machine_info
= {
1079 .name
= TYPE_MACHINE
,
1080 .parent
= TYPE_OBJECT
,
1082 .class_size
= sizeof(MachineClass
),
1083 .class_init
= machine_class_init
,
1084 .class_base_init
= machine_class_base_init
,
1085 .instance_size
= sizeof(MachineState
),
1086 .instance_init
= machine_initfn
,
1087 .instance_finalize
= machine_finalize
,
1090 static void machine_register_types(void)
1092 type_register_static(&machine_info
);
1095 type_init(machine_register_types
)