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 /* Optional because the 'scsi' property is Linux-only */
152 { "virtio-blk-device", "scsi", "true", .optional
= true },
153 { "e1000", "extra_mac_registers", "off" },
154 { "virtio-pci", "x-disable-pcie", "on" },
155 { "virtio-pci", "migrate-extra", "off" },
156 { "fw_cfg_mem", "dma_enabled", "off" },
157 { "fw_cfg_io", "dma_enabled", "off" }
159 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
161 GlobalProperty hw_compat_2_3
[] = {
162 { "virtio-blk-pci", "any_layout", "off" },
163 { "virtio-balloon-pci", "any_layout", "off" },
164 { "virtio-serial-pci", "any_layout", "off" },
165 { "virtio-9p-pci", "any_layout", "off" },
166 { "virtio-rng-pci", "any_layout", "off" },
167 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
168 { "migration", "send-configuration", "off" },
169 { "migration", "send-section-footer", "off" },
170 { "migration", "store-global-state", "off" },
172 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
174 GlobalProperty hw_compat_2_2
[] = {};
175 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
177 GlobalProperty hw_compat_2_1
[] = {
178 { "intel-hda", "old_msi_addr", "on" },
179 { "VGA", "qemu-extended-regs", "off" },
180 { "secondary-vga", "qemu-extended-regs", "off" },
181 { "virtio-scsi-pci", "any_layout", "off" },
182 { "usb-mouse", "usb_version", "1" },
183 { "usb-kbd", "usb_version", "1" },
184 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
186 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
188 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
190 MachineState
*ms
= MACHINE(obj
);
192 return g_strdup(ms
->kernel_filename
);
195 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
197 MachineState
*ms
= MACHINE(obj
);
199 g_free(ms
->kernel_filename
);
200 ms
->kernel_filename
= g_strdup(value
);
203 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
205 MachineState
*ms
= MACHINE(obj
);
207 return g_strdup(ms
->initrd_filename
);
210 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
212 MachineState
*ms
= MACHINE(obj
);
214 g_free(ms
->initrd_filename
);
215 ms
->initrd_filename
= g_strdup(value
);
218 static char *machine_get_append(Object
*obj
, Error
**errp
)
220 MachineState
*ms
= MACHINE(obj
);
222 return g_strdup(ms
->kernel_cmdline
);
225 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
227 MachineState
*ms
= MACHINE(obj
);
229 g_free(ms
->kernel_cmdline
);
230 ms
->kernel_cmdline
= g_strdup(value
);
233 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
235 MachineState
*ms
= MACHINE(obj
);
237 return g_strdup(ms
->dtb
);
240 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
242 MachineState
*ms
= MACHINE(obj
);
245 ms
->dtb
= g_strdup(value
);
248 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
250 MachineState
*ms
= MACHINE(obj
);
252 return g_strdup(ms
->dumpdtb
);
255 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
257 MachineState
*ms
= MACHINE(obj
);
260 ms
->dumpdtb
= g_strdup(value
);
263 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
264 const char *name
, void *opaque
,
267 MachineState
*ms
= MACHINE(obj
);
268 int64_t value
= ms
->phandle_start
;
270 visit_type_int(v
, name
, &value
, errp
);
273 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
274 const char *name
, void *opaque
,
277 MachineState
*ms
= MACHINE(obj
);
281 visit_type_int(v
, name
, &value
, &error
);
283 error_propagate(errp
, error
);
287 ms
->phandle_start
= value
;
290 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
292 MachineState
*ms
= MACHINE(obj
);
294 return g_strdup(ms
->dt_compatible
);
297 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
299 MachineState
*ms
= MACHINE(obj
);
301 g_free(ms
->dt_compatible
);
302 ms
->dt_compatible
= g_strdup(value
);
305 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
307 MachineState
*ms
= MACHINE(obj
);
309 return ms
->dump_guest_core
;
312 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
314 MachineState
*ms
= MACHINE(obj
);
316 ms
->dump_guest_core
= value
;
319 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
321 MachineState
*ms
= MACHINE(obj
);
323 return ms
->mem_merge
;
326 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
328 MachineState
*ms
= MACHINE(obj
);
330 ms
->mem_merge
= value
;
333 static bool machine_get_usb(Object
*obj
, Error
**errp
)
335 MachineState
*ms
= MACHINE(obj
);
340 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
342 MachineState
*ms
= MACHINE(obj
);
345 ms
->usb_disabled
= !value
;
348 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
350 MachineState
*ms
= MACHINE(obj
);
352 return ms
->enable_graphics
;
355 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
357 MachineState
*ms
= MACHINE(obj
);
359 ms
->enable_graphics
= value
;
362 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
364 MachineState
*ms
= MACHINE(obj
);
366 return g_strdup(ms
->firmware
);
369 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
371 MachineState
*ms
= MACHINE(obj
);
373 g_free(ms
->firmware
);
374 ms
->firmware
= g_strdup(value
);
377 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
379 MachineState
*ms
= MACHINE(obj
);
381 ms
->suppress_vmdesc
= value
;
384 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
386 MachineState
*ms
= MACHINE(obj
);
388 return ms
->suppress_vmdesc
;
391 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
394 MachineState
*ms
= MACHINE(obj
);
396 warn_report("enforce-config-section is deprecated, please use "
397 "-global migration.send-configuration=on|off instead");
399 ms
->enforce_config_section
= value
;
402 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
404 MachineState
*ms
= MACHINE(obj
);
406 return ms
->enforce_config_section
;
409 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
411 MachineState
*ms
= MACHINE(obj
);
413 return g_strdup(ms
->memory_encryption
);
416 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
419 MachineState
*ms
= MACHINE(obj
);
421 g_free(ms
->memory_encryption
);
422 ms
->memory_encryption
= g_strdup(value
);
425 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
427 MachineState
*ms
= MACHINE(obj
);
429 return ms
->nvdimms_state
->is_enabled
;
432 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
434 MachineState
*ms
= MACHINE(obj
);
436 ms
->nvdimms_state
->is_enabled
= value
;
439 static bool machine_get_hmat(Object
*obj
, Error
**errp
)
441 MachineState
*ms
= MACHINE(obj
);
443 return ms
->numa_state
->hmat_enabled
;
446 static void machine_set_hmat(Object
*obj
, bool value
, Error
**errp
)
448 MachineState
*ms
= MACHINE(obj
);
450 ms
->numa_state
->hmat_enabled
= value
;
453 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
455 MachineState
*ms
= MACHINE(obj
);
457 return g_strdup(ms
->nvdimms_state
->persistence_string
);
460 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
463 MachineState
*ms
= MACHINE(obj
);
464 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
466 if (strcmp(value
, "cpu") == 0) {
467 nvdimms_state
->persistence
= 3;
468 } else if (strcmp(value
, "mem-ctrl") == 0) {
469 nvdimms_state
->persistence
= 2;
471 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
476 g_free(nvdimms_state
->persistence_string
);
477 nvdimms_state
->persistence_string
= g_strdup(value
);
480 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
482 strList
*item
= g_new0(strList
, 1);
484 item
->value
= g_strdup(type
);
485 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
486 mc
->allowed_dynamic_sysbus_devices
= item
;
489 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
491 MachineState
*machine
= opaque
;
492 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
493 bool allowed
= false;
496 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
499 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
503 error_report("Option '-device %s' cannot be handled by this machine",
504 object_class_get_name(object_get_class(OBJECT(sbdev
))));
509 static void machine_init_notify(Notifier
*notifier
, void *data
)
511 MachineState
*machine
= MACHINE(qdev_get_machine());
514 * Loop through all dynamically created sysbus devices and check if they are
515 * all allowed. If a device is not allowed, error out.
517 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
520 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
523 HotpluggableCPUList
*head
= NULL
;
524 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
526 /* force board to initialize possible_cpus if it hasn't been done yet */
527 mc
->possible_cpu_arch_ids(machine
);
529 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
531 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
532 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
534 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
535 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
536 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
537 sizeof(*cpu_item
->props
));
539 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
541 cpu_item
->has_qom_path
= true;
542 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
544 list_item
->value
= cpu_item
;
545 list_item
->next
= head
;
552 * machine_set_cpu_numa_node:
553 * @machine: machine object to modify
554 * @props: specifies which cpu objects to assign to
555 * numa node specified by @props.node_id
556 * @errp: if an error occurs, a pointer to an area to store the error
558 * Associate NUMA node specified by @props.node_id with cpu slots that
559 * match socket/core/thread-ids specified by @props. It's recommended to use
560 * query-hotpluggable-cpus.props values to specify affected cpu slots,
561 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
563 * However for CLI convenience it's possible to pass in subset of properties,
564 * which would affect all cpu slots that match it.
566 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
567 * -numa cpu,node-id=0,socket_id=0 \
568 * -numa cpu,node-id=1,socket_id=1
569 * will assign all child cores of socket 0 to node 0 and
570 * of socket 1 to node 1.
572 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
574 * Empty subset is disallowed and function will return with error in this case.
576 void machine_set_cpu_numa_node(MachineState
*machine
,
577 const CpuInstanceProperties
*props
, Error
**errp
)
579 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
580 NodeInfo
*numa_info
= machine
->numa_state
->nodes
;
584 if (!mc
->possible_cpu_arch_ids
) {
585 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
589 /* disabling node mapping is not supported, forbid it */
590 assert(props
->has_node_id
);
592 /* force board to initialize possible_cpus if it hasn't been done yet */
593 mc
->possible_cpu_arch_ids(machine
);
595 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
596 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
598 /* reject unsupported by board properties */
599 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
600 error_setg(errp
, "thread-id is not supported");
604 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
605 error_setg(errp
, "core-id is not supported");
609 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
610 error_setg(errp
, "socket-id is not supported");
614 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
615 error_setg(errp
, "die-id is not supported");
619 /* skip slots with explicit mismatch */
620 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
624 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
628 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
632 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
636 /* reject assignment if slot is already assigned, for compatibility
637 * of legacy cpu_index mapping with SPAPR core based mapping do not
638 * error out if cpu thread and matched core have the same node-id */
639 if (slot
->props
.has_node_id
&&
640 slot
->props
.node_id
!= props
->node_id
) {
641 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
642 slot
->props
.node_id
);
646 /* assign slot to node as it's matched '-numa cpu' key */
648 slot
->props
.node_id
= props
->node_id
;
649 slot
->props
.has_node_id
= props
->has_node_id
;
651 if (machine
->numa_state
->hmat_enabled
) {
652 if ((numa_info
[props
->node_id
].initiator
< MAX_NODES
) &&
653 (props
->node_id
!= numa_info
[props
->node_id
].initiator
)) {
654 error_setg(errp
, "The initiator of CPU NUMA node %" PRId64
655 " should be itself", props
->node_id
);
658 numa_info
[props
->node_id
].has_cpu
= true;
659 numa_info
[props
->node_id
].initiator
= props
->node_id
;
664 error_setg(errp
, "no match found");
668 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
671 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
672 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
673 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
674 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
676 /* compute missing values, prefer sockets over cores over threads */
677 if (cpus
== 0 || sockets
== 0) {
678 cores
= cores
> 0 ? cores
: 1;
679 threads
= threads
> 0 ? threads
: 1;
681 sockets
= sockets
> 0 ? sockets
: 1;
682 cpus
= cores
* threads
* sockets
;
685 qemu_opt_get_number(opts
, "maxcpus", cpus
);
686 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
688 } else if (cores
== 0) {
689 threads
= threads
> 0 ? threads
: 1;
690 cores
= cpus
/ (sockets
* threads
);
691 cores
= cores
> 0 ? cores
: 1;
692 } else if (threads
== 0) {
693 threads
= cpus
/ (cores
* sockets
);
694 threads
= threads
> 0 ? threads
: 1;
695 } else if (sockets
* cores
* threads
< cpus
) {
696 error_report("cpu topology: "
697 "sockets (%u) * cores (%u) * threads (%u) < "
699 sockets
, cores
, threads
, cpus
);
704 qemu_opt_get_number(opts
, "maxcpus", cpus
);
706 if (ms
->smp
.max_cpus
< cpus
) {
707 error_report("maxcpus must be equal to or greater than smp");
711 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
712 error_report("cpu topology: "
713 "sockets (%u) * cores (%u) * threads (%u) > "
715 sockets
, cores
, threads
,
720 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
721 warn_report("Invalid CPU topology deprecated: "
722 "sockets (%u) * cores (%u) * threads (%u) "
724 sockets
, cores
, threads
,
729 ms
->smp
.cores
= cores
;
730 ms
->smp
.threads
= threads
;
733 if (ms
->smp
.cpus
> 1) {
734 Error
*blocker
= NULL
;
735 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
736 replay_add_blocker(blocker
);
740 static void machine_class_init(ObjectClass
*oc
, void *data
)
742 MachineClass
*mc
= MACHINE_CLASS(oc
);
744 /* Default 128 MB as guest ram size */
745 mc
->default_ram_size
= 128 * MiB
;
746 mc
->rom_file_has_mr
= true;
747 mc
->smp_parse
= smp_parse
;
749 /* numa node memory size aligned on 8MB by default.
750 * On Linux, each node's border has to be 8MB aligned
752 mc
->numa_mem_align_shift
= 23;
753 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
755 object_class_property_add_str(oc
, "kernel",
756 machine_get_kernel
, machine_set_kernel
, &error_abort
);
757 object_class_property_set_description(oc
, "kernel",
758 "Linux kernel image file", &error_abort
);
760 object_class_property_add_str(oc
, "initrd",
761 machine_get_initrd
, machine_set_initrd
, &error_abort
);
762 object_class_property_set_description(oc
, "initrd",
763 "Linux initial ramdisk file", &error_abort
);
765 object_class_property_add_str(oc
, "append",
766 machine_get_append
, machine_set_append
, &error_abort
);
767 object_class_property_set_description(oc
, "append",
768 "Linux kernel command line", &error_abort
);
770 object_class_property_add_str(oc
, "dtb",
771 machine_get_dtb
, machine_set_dtb
, &error_abort
);
772 object_class_property_set_description(oc
, "dtb",
773 "Linux kernel device tree file", &error_abort
);
775 object_class_property_add_str(oc
, "dumpdtb",
776 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
777 object_class_property_set_description(oc
, "dumpdtb",
778 "Dump current dtb to a file and quit", &error_abort
);
780 object_class_property_add(oc
, "phandle-start", "int",
781 machine_get_phandle_start
, machine_set_phandle_start
,
782 NULL
, NULL
, &error_abort
);
783 object_class_property_set_description(oc
, "phandle-start",
784 "The first phandle ID we may generate dynamically", &error_abort
);
786 object_class_property_add_str(oc
, "dt-compatible",
787 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
788 object_class_property_set_description(oc
, "dt-compatible",
789 "Overrides the \"compatible\" property of the dt root node",
792 object_class_property_add_bool(oc
, "dump-guest-core",
793 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
794 object_class_property_set_description(oc
, "dump-guest-core",
795 "Include guest memory in a core dump", &error_abort
);
797 object_class_property_add_bool(oc
, "mem-merge",
798 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
799 object_class_property_set_description(oc
, "mem-merge",
800 "Enable/disable memory merge support", &error_abort
);
802 object_class_property_add_bool(oc
, "usb",
803 machine_get_usb
, machine_set_usb
, &error_abort
);
804 object_class_property_set_description(oc
, "usb",
805 "Set on/off to enable/disable usb", &error_abort
);
807 object_class_property_add_bool(oc
, "graphics",
808 machine_get_graphics
, machine_set_graphics
, &error_abort
);
809 object_class_property_set_description(oc
, "graphics",
810 "Set on/off to enable/disable graphics emulation", &error_abort
);
812 object_class_property_add_str(oc
, "firmware",
813 machine_get_firmware
, machine_set_firmware
,
815 object_class_property_set_description(oc
, "firmware",
816 "Firmware image", &error_abort
);
818 object_class_property_add_bool(oc
, "suppress-vmdesc",
819 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
821 object_class_property_set_description(oc
, "suppress-vmdesc",
822 "Set on to disable self-describing migration", &error_abort
);
824 object_class_property_add_bool(oc
, "enforce-config-section",
825 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
827 object_class_property_set_description(oc
, "enforce-config-section",
828 "Set on to enforce configuration section migration", &error_abort
);
830 object_class_property_add_str(oc
, "memory-encryption",
831 machine_get_memory_encryption
, machine_set_memory_encryption
,
833 object_class_property_set_description(oc
, "memory-encryption",
834 "Set memory encryption object to use", &error_abort
);
837 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
839 if (!object_class_is_abstract(oc
)) {
840 MachineClass
*mc
= MACHINE_CLASS(oc
);
841 const char *cname
= object_class_get_name(oc
);
842 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
843 mc
->name
= g_strndup(cname
,
844 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
845 mc
->compat_props
= g_ptr_array_new();
849 static void machine_initfn(Object
*obj
)
851 MachineState
*ms
= MACHINE(obj
);
852 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
854 ms
->dump_guest_core
= true;
855 ms
->mem_merge
= true;
856 ms
->enable_graphics
= true;
858 if (mc
->nvdimm_supported
) {
859 Object
*obj
= OBJECT(ms
);
861 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
862 object_property_add_bool(obj
, "nvdimm",
863 machine_get_nvdimm
, machine_set_nvdimm
,
865 object_property_set_description(obj
, "nvdimm",
866 "Set on/off to enable/disable "
867 "NVDIMM instantiation", NULL
);
869 object_property_add_str(obj
, "nvdimm-persistence",
870 machine_get_nvdimm_persistence
,
871 machine_set_nvdimm_persistence
,
873 object_property_set_description(obj
, "nvdimm-persistence",
874 "Set NVDIMM persistence"
875 "Valid values are cpu, mem-ctrl",
879 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
880 ms
->numa_state
= g_new0(NumaState
, 1);
881 object_property_add_bool(obj
, "hmat",
882 machine_get_hmat
, machine_set_hmat
,
884 object_property_set_description(obj
, "hmat",
885 "Set on/off to enable/disable "
886 "ACPI Heterogeneous Memory Attribute "
887 "Table (HMAT)", NULL
);
890 /* Register notifier when init is done for sysbus sanity checks */
891 ms
->sysbus_notifier
.notify
= machine_init_notify
;
892 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
895 static void machine_finalize(Object
*obj
)
897 MachineState
*ms
= MACHINE(obj
);
899 g_free(ms
->kernel_filename
);
900 g_free(ms
->initrd_filename
);
901 g_free(ms
->kernel_cmdline
);
904 g_free(ms
->dt_compatible
);
905 g_free(ms
->firmware
);
906 g_free(ms
->device_memory
);
907 g_free(ms
->nvdimms_state
);
908 g_free(ms
->numa_state
);
911 bool machine_usb(MachineState
*machine
)
916 int machine_phandle_start(MachineState
*machine
)
918 return machine
->phandle_start
;
921 bool machine_dump_guest_core(MachineState
*machine
)
923 return machine
->dump_guest_core
;
926 bool machine_mem_merge(MachineState
*machine
)
928 return machine
->mem_merge
;
931 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
933 GString
*s
= g_string_new(NULL
);
934 if (cpu
->props
.has_socket_id
) {
935 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
937 if (cpu
->props
.has_die_id
) {
938 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
940 if (cpu
->props
.has_core_id
) {
942 g_string_append_printf(s
, ", ");
944 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
946 if (cpu
->props
.has_thread_id
) {
948 g_string_append_printf(s
, ", ");
950 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
952 return g_string_free(s
, false);
955 static void numa_validate_initiator(NumaState
*numa_state
)
958 NodeInfo
*numa_info
= numa_state
->nodes
;
960 for (i
= 0; i
< numa_state
->num_nodes
; i
++) {
961 if (numa_info
[i
].initiator
== MAX_NODES
) {
962 error_report("The initiator of NUMA node %d is missing, use "
963 "'-numa node,initiator' option to declare it", i
);
967 if (!numa_info
[numa_info
[i
].initiator
].present
) {
968 error_report("NUMA node %" PRIu16
" is missing, use "
969 "'-numa node' option to declare it first",
970 numa_info
[i
].initiator
);
974 if (!numa_info
[numa_info
[i
].initiator
].has_cpu
) {
975 error_report("The initiator of NUMA node %d is invalid", i
);
981 static void machine_numa_finish_cpu_init(MachineState
*machine
)
984 bool default_mapping
;
985 GString
*s
= g_string_new(NULL
);
986 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
987 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
989 assert(machine
->numa_state
->num_nodes
);
990 for (i
= 0; i
< possible_cpus
->len
; i
++) {
991 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
995 default_mapping
= (i
== possible_cpus
->len
);
997 for (i
= 0; i
< possible_cpus
->len
; i
++) {
998 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1000 if (!cpu_slot
->props
.has_node_id
) {
1001 /* fetch default mapping from board and enable it */
1002 CpuInstanceProperties props
= cpu_slot
->props
;
1004 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1005 if (!default_mapping
) {
1006 /* record slots with not set mapping,
1007 * TODO: make it hard error in future */
1008 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1009 g_string_append_printf(s
, "%sCPU %d [%s]",
1010 s
->len
? ", " : "", i
, cpu_str
);
1013 /* non mapped cpus used to fallback to node 0 */
1017 props
.has_node_id
= true;
1018 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1022 if (machine
->numa_state
->hmat_enabled
) {
1023 numa_validate_initiator(machine
->numa_state
);
1026 if (s
->len
&& !qtest_enabled()) {
1027 warn_report("CPU(s) not present in any NUMA nodes: %s",
1029 warn_report("All CPU(s) up to maxcpus should be described "
1030 "in NUMA config, ability to start up with partial NUMA "
1031 "mappings is obsoleted and will be removed in future");
1033 g_string_free(s
, true);
1036 void machine_run_board_init(MachineState
*machine
)
1038 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1040 if (machine
->numa_state
) {
1041 numa_complete_configuration(machine
);
1042 if (machine
->numa_state
->num_nodes
) {
1043 machine_numa_finish_cpu_init(machine
);
1047 /* If the machine supports the valid_cpu_types check and the user
1048 * specified a CPU with -cpu check here that the user CPU is supported.
1050 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1051 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1054 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1055 if (object_class_dynamic_cast(class,
1056 machine_class
->valid_cpu_types
[i
])) {
1057 /* The user specificed CPU is in the valid field, we are
1064 if (!machine_class
->valid_cpu_types
[i
]) {
1065 /* The user specified CPU is not valid */
1066 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1067 error_printf("The valid types are: %s",
1068 machine_class
->valid_cpu_types
[0]);
1069 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1070 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1078 machine_class
->init(machine
);
1081 static const TypeInfo machine_info
= {
1082 .name
= TYPE_MACHINE
,
1083 .parent
= TYPE_OBJECT
,
1085 .class_size
= sizeof(MachineClass
),
1086 .class_init
= machine_class_init
,
1087 .class_base_init
= machine_class_base_init
,
1088 .instance_size
= sizeof(MachineState
),
1089 .instance_init
= machine_initfn
,
1090 .instance_finalize
= machine_finalize
,
1093 static void machine_register_types(void)
1095 type_register_static(&machine_info
);
1098 type_init(machine_register_types
)