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_0
[] = {
31 { "VGA", "edid", "false" },
32 { "secondary-vga", "edid", "false" },
33 { "bochs-display", "edid", "false" },
34 { "virtio-vga", "edid", "false" },
35 { "virtio-gpu-pci", "edid", "false" },
36 { "virtio-device", "use-started", "false" },
37 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
38 { "pl031", "migrate-tick-offset", "false" },
40 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
42 GlobalProperty hw_compat_3_1
[] = {
43 { "pcie-root-port", "x-speed", "2_5" },
44 { "pcie-root-port", "x-width", "1" },
45 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
46 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
47 { "tpm-crb", "ppi", "false" },
48 { "tpm-tis", "ppi", "false" },
49 { "usb-kbd", "serial", "42" },
50 { "usb-mouse", "serial", "42" },
51 { "usb-tablet", "serial", "42" },
52 { "virtio-blk-device", "discard", "false" },
53 { "virtio-blk-device", "write-zeroes", "false" },
54 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
56 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
58 GlobalProperty hw_compat_3_0
[] = {};
59 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
61 GlobalProperty hw_compat_2_12
[] = {
62 { "migration", "decompress-error-check", "off" },
63 { "hda-audio", "use-timer", "false" },
64 { "cirrus-vga", "global-vmstate", "true" },
65 { "VGA", "global-vmstate", "true" },
66 { "vmware-svga", "global-vmstate", "true" },
67 { "qxl-vga", "global-vmstate", "true" },
69 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
71 GlobalProperty hw_compat_2_11
[] = {
72 { "hpet", "hpet-offset-saved", "false" },
73 { "virtio-blk-pci", "vectors", "2" },
74 { "vhost-user-blk-pci", "vectors", "2" },
75 { "e1000", "migrate_tso_props", "off" },
77 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
79 GlobalProperty hw_compat_2_10
[] = {
80 { "virtio-mouse-device", "wheel-axis", "false" },
81 { "virtio-tablet-device", "wheel-axis", "false" },
83 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
85 GlobalProperty hw_compat_2_9
[] = {
86 { "pci-bridge", "shpc", "off" },
87 { "intel-iommu", "pt", "off" },
88 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
89 { "pcie-root-port", "x-migrate-msix", "false" },
91 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
93 GlobalProperty hw_compat_2_8
[] = {
94 { "fw_cfg_mem", "x-file-slots", "0x10" },
95 { "fw_cfg_io", "x-file-slots", "0x10" },
96 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
97 { "pci-bridge", "shpc", "on" },
98 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
99 { "virtio-pci", "x-pcie-deverr-init", "off" },
100 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
101 { "virtio-pci", "x-pcie-pm-init", "off" },
102 { "cirrus-vga", "vgamem_mb", "8" },
103 { "isa-cirrus-vga", "vgamem_mb", "8" },
105 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
107 GlobalProperty hw_compat_2_7
[] = {
108 { "virtio-pci", "page-per-vq", "on" },
109 { "virtio-serial-device", "emergency-write", "off" },
110 { "ioapic", "version", "0x11" },
111 { "intel-iommu", "x-buggy-eim", "true" },
112 { "virtio-pci", "x-ignore-backend-features", "on" },
114 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
116 GlobalProperty hw_compat_2_6
[] = {
117 { "virtio-mmio", "format_transport_address", "off" },
118 /* Optional because not all virtio-pci devices support legacy mode */
119 { "virtio-pci", "disable-modern", "on", .optional
= true },
120 { "virtio-pci", "disable-legacy", "off", .optional
= true },
122 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
124 GlobalProperty hw_compat_2_5
[] = {
125 { "isa-fdc", "fallback", "144" },
126 { "pvscsi", "x-old-pci-configuration", "on" },
127 { "pvscsi", "x-disable-pcie", "on" },
128 { "vmxnet3", "x-old-msi-offsets", "on" },
129 { "vmxnet3", "x-disable-pcie", "on" },
131 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
133 GlobalProperty hw_compat_2_4
[] = {
134 { "virtio-blk-device", "scsi", "true" },
135 { "e1000", "extra_mac_registers", "off" },
136 { "virtio-pci", "x-disable-pcie", "on" },
137 { "virtio-pci", "migrate-extra", "off" },
138 { "fw_cfg_mem", "dma_enabled", "off" },
139 { "fw_cfg_io", "dma_enabled", "off" }
141 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
143 GlobalProperty hw_compat_2_3
[] = {
144 { "virtio-blk-pci", "any_layout", "off" },
145 { "virtio-balloon-pci", "any_layout", "off" },
146 { "virtio-serial-pci", "any_layout", "off" },
147 { "virtio-9p-pci", "any_layout", "off" },
148 { "virtio-rng-pci", "any_layout", "off" },
149 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
150 { "migration", "send-configuration", "off" },
151 { "migration", "send-section-footer", "off" },
152 { "migration", "store-global-state", "off" },
154 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
156 GlobalProperty hw_compat_2_2
[] = {};
157 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
159 GlobalProperty hw_compat_2_1
[] = {
160 { "intel-hda", "old_msi_addr", "on" },
161 { "VGA", "qemu-extended-regs", "off" },
162 { "secondary-vga", "qemu-extended-regs", "off" },
163 { "virtio-scsi-pci", "any_layout", "off" },
164 { "usb-mouse", "usb_version", "1" },
165 { "usb-kbd", "usb_version", "1" },
166 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
168 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
170 static char *machine_get_accel(Object
*obj
, Error
**errp
)
172 MachineState
*ms
= MACHINE(obj
);
174 return g_strdup(ms
->accel
);
177 static void machine_set_accel(Object
*obj
, const char *value
, Error
**errp
)
179 MachineState
*ms
= MACHINE(obj
);
182 ms
->accel
= g_strdup(value
);
185 static void machine_set_kernel_irqchip(Object
*obj
, Visitor
*v
,
186 const char *name
, void *opaque
,
190 MachineState
*ms
= MACHINE(obj
);
193 visit_type_OnOffSplit(v
, name
, &mode
, &err
);
195 error_propagate(errp
, err
);
199 case ON_OFF_SPLIT_ON
:
200 ms
->kernel_irqchip_allowed
= true;
201 ms
->kernel_irqchip_required
= true;
202 ms
->kernel_irqchip_split
= false;
204 case ON_OFF_SPLIT_OFF
:
205 ms
->kernel_irqchip_allowed
= false;
206 ms
->kernel_irqchip_required
= false;
207 ms
->kernel_irqchip_split
= false;
209 case ON_OFF_SPLIT_SPLIT
:
210 ms
->kernel_irqchip_allowed
= true;
211 ms
->kernel_irqchip_required
= true;
212 ms
->kernel_irqchip_split
= true;
215 /* The value was checked in visit_type_OnOffSplit() above. If
216 * we get here, then something is wrong in QEMU.
223 static void machine_get_kvm_shadow_mem(Object
*obj
, Visitor
*v
,
224 const char *name
, void *opaque
,
227 MachineState
*ms
= MACHINE(obj
);
228 int64_t value
= ms
->kvm_shadow_mem
;
230 visit_type_int(v
, name
, &value
, errp
);
233 static void machine_set_kvm_shadow_mem(Object
*obj
, Visitor
*v
,
234 const char *name
, void *opaque
,
237 MachineState
*ms
= MACHINE(obj
);
241 visit_type_int(v
, name
, &value
, &error
);
243 error_propagate(errp
, error
);
247 ms
->kvm_shadow_mem
= value
;
250 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
252 MachineState
*ms
= MACHINE(obj
);
254 return g_strdup(ms
->kernel_filename
);
257 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
259 MachineState
*ms
= MACHINE(obj
);
261 g_free(ms
->kernel_filename
);
262 ms
->kernel_filename
= g_strdup(value
);
265 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
267 MachineState
*ms
= MACHINE(obj
);
269 return g_strdup(ms
->initrd_filename
);
272 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
274 MachineState
*ms
= MACHINE(obj
);
276 g_free(ms
->initrd_filename
);
277 ms
->initrd_filename
= g_strdup(value
);
280 static char *machine_get_append(Object
*obj
, Error
**errp
)
282 MachineState
*ms
= MACHINE(obj
);
284 return g_strdup(ms
->kernel_cmdline
);
287 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
289 MachineState
*ms
= MACHINE(obj
);
291 g_free(ms
->kernel_cmdline
);
292 ms
->kernel_cmdline
= g_strdup(value
);
295 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
297 MachineState
*ms
= MACHINE(obj
);
299 return g_strdup(ms
->dtb
);
302 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
304 MachineState
*ms
= MACHINE(obj
);
307 ms
->dtb
= g_strdup(value
);
310 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
312 MachineState
*ms
= MACHINE(obj
);
314 return g_strdup(ms
->dumpdtb
);
317 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
319 MachineState
*ms
= MACHINE(obj
);
322 ms
->dumpdtb
= g_strdup(value
);
325 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
326 const char *name
, void *opaque
,
329 MachineState
*ms
= MACHINE(obj
);
330 int64_t value
= ms
->phandle_start
;
332 visit_type_int(v
, name
, &value
, errp
);
335 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
336 const char *name
, void *opaque
,
339 MachineState
*ms
= MACHINE(obj
);
343 visit_type_int(v
, name
, &value
, &error
);
345 error_propagate(errp
, error
);
349 ms
->phandle_start
= value
;
352 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
354 MachineState
*ms
= MACHINE(obj
);
356 return g_strdup(ms
->dt_compatible
);
359 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
361 MachineState
*ms
= MACHINE(obj
);
363 g_free(ms
->dt_compatible
);
364 ms
->dt_compatible
= g_strdup(value
);
367 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
369 MachineState
*ms
= MACHINE(obj
);
371 return ms
->dump_guest_core
;
374 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
376 MachineState
*ms
= MACHINE(obj
);
378 ms
->dump_guest_core
= value
;
381 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
383 MachineState
*ms
= MACHINE(obj
);
385 return ms
->mem_merge
;
388 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
390 MachineState
*ms
= MACHINE(obj
);
392 ms
->mem_merge
= value
;
395 static bool machine_get_usb(Object
*obj
, Error
**errp
)
397 MachineState
*ms
= MACHINE(obj
);
402 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
404 MachineState
*ms
= MACHINE(obj
);
407 ms
->usb_disabled
= !value
;
410 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
412 MachineState
*ms
= MACHINE(obj
);
414 return ms
->enable_graphics
;
417 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
419 MachineState
*ms
= MACHINE(obj
);
421 ms
->enable_graphics
= value
;
424 static bool machine_get_igd_gfx_passthru(Object
*obj
, Error
**errp
)
426 MachineState
*ms
= MACHINE(obj
);
428 return ms
->igd_gfx_passthru
;
431 static void machine_set_igd_gfx_passthru(Object
*obj
, bool value
, Error
**errp
)
433 MachineState
*ms
= MACHINE(obj
);
435 ms
->igd_gfx_passthru
= value
;
438 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
440 MachineState
*ms
= MACHINE(obj
);
442 return g_strdup(ms
->firmware
);
445 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
447 MachineState
*ms
= MACHINE(obj
);
449 g_free(ms
->firmware
);
450 ms
->firmware
= g_strdup(value
);
453 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
455 MachineState
*ms
= MACHINE(obj
);
457 ms
->suppress_vmdesc
= value
;
460 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
462 MachineState
*ms
= MACHINE(obj
);
464 return ms
->suppress_vmdesc
;
467 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
470 MachineState
*ms
= MACHINE(obj
);
472 warn_report("enforce-config-section is deprecated, please use "
473 "-global migration.send-configuration=on|off instead");
475 ms
->enforce_config_section
= value
;
478 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
480 MachineState
*ms
= MACHINE(obj
);
482 return ms
->enforce_config_section
;
485 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
487 MachineState
*ms
= MACHINE(obj
);
489 return g_strdup(ms
->memory_encryption
);
492 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
495 MachineState
*ms
= MACHINE(obj
);
497 g_free(ms
->memory_encryption
);
498 ms
->memory_encryption
= g_strdup(value
);
501 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
503 MachineState
*ms
= MACHINE(obj
);
505 return ms
->nvdimms_state
->is_enabled
;
508 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
510 MachineState
*ms
= MACHINE(obj
);
512 ms
->nvdimms_state
->is_enabled
= value
;
515 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
517 MachineState
*ms
= MACHINE(obj
);
519 return g_strdup(ms
->nvdimms_state
->persistence_string
);
522 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
525 MachineState
*ms
= MACHINE(obj
);
526 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
528 if (strcmp(value
, "cpu") == 0) {
529 nvdimms_state
->persistence
= 3;
530 } else if (strcmp(value
, "mem-ctrl") == 0) {
531 nvdimms_state
->persistence
= 2;
533 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
538 g_free(nvdimms_state
->persistence_string
);
539 nvdimms_state
->persistence_string
= g_strdup(value
);
542 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
544 strList
*item
= g_new0(strList
, 1);
546 item
->value
= g_strdup(type
);
547 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
548 mc
->allowed_dynamic_sysbus_devices
= item
;
551 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
553 MachineState
*machine
= opaque
;
554 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
555 bool allowed
= false;
558 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
561 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
565 error_report("Option '-device %s' cannot be handled by this machine",
566 object_class_get_name(object_get_class(OBJECT(sbdev
))));
571 static void machine_init_notify(Notifier
*notifier
, void *data
)
573 MachineState
*machine
= MACHINE(qdev_get_machine());
576 * Loop through all dynamically created sysbus devices and check if they are
577 * all allowed. If a device is not allowed, error out.
579 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
582 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
585 HotpluggableCPUList
*head
= NULL
;
586 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
588 /* force board to initialize possible_cpus if it hasn't been done yet */
589 mc
->possible_cpu_arch_ids(machine
);
591 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
593 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
594 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
596 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
597 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
598 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
599 sizeof(*cpu_item
->props
));
601 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
603 cpu_item
->has_qom_path
= true;
604 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
606 list_item
->value
= cpu_item
;
607 list_item
->next
= head
;
614 * machine_set_cpu_numa_node:
615 * @machine: machine object to modify
616 * @props: specifies which cpu objects to assign to
617 * numa node specified by @props.node_id
618 * @errp: if an error occurs, a pointer to an area to store the error
620 * Associate NUMA node specified by @props.node_id with cpu slots that
621 * match socket/core/thread-ids specified by @props. It's recommended to use
622 * query-hotpluggable-cpus.props values to specify affected cpu slots,
623 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
625 * However for CLI convenience it's possible to pass in subset of properties,
626 * which would affect all cpu slots that match it.
628 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
629 * -numa cpu,node-id=0,socket_id=0 \
630 * -numa cpu,node-id=1,socket_id=1
631 * will assign all child cores of socket 0 to node 0 and
632 * of socket 1 to node 1.
634 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
636 * Empty subset is disallowed and function will return with error in this case.
638 void machine_set_cpu_numa_node(MachineState
*machine
,
639 const CpuInstanceProperties
*props
, Error
**errp
)
641 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
645 if (!mc
->possible_cpu_arch_ids
) {
646 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
650 /* disabling node mapping is not supported, forbid it */
651 assert(props
->has_node_id
);
653 /* force board to initialize possible_cpus if it hasn't been done yet */
654 mc
->possible_cpu_arch_ids(machine
);
656 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
657 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
659 /* reject unsupported by board properties */
660 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
661 error_setg(errp
, "thread-id is not supported");
665 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
666 error_setg(errp
, "core-id is not supported");
670 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
671 error_setg(errp
, "socket-id is not supported");
675 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
676 error_setg(errp
, "die-id is not supported");
680 /* skip slots with explicit mismatch */
681 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
685 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
689 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
693 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
697 /* reject assignment if slot is already assigned, for compatibility
698 * of legacy cpu_index mapping with SPAPR core based mapping do not
699 * error out if cpu thread and matched core have the same node-id */
700 if (slot
->props
.has_node_id
&&
701 slot
->props
.node_id
!= props
->node_id
) {
702 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
703 slot
->props
.node_id
);
707 /* assign slot to node as it's matched '-numa cpu' key */
709 slot
->props
.node_id
= props
->node_id
;
710 slot
->props
.has_node_id
= props
->has_node_id
;
714 error_setg(errp
, "no match found");
718 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
721 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
722 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
723 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
724 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
726 /* compute missing values, prefer sockets over cores over threads */
727 if (cpus
== 0 || sockets
== 0) {
728 cores
= cores
> 0 ? cores
: 1;
729 threads
= threads
> 0 ? threads
: 1;
731 sockets
= sockets
> 0 ? sockets
: 1;
732 cpus
= cores
* threads
* sockets
;
735 qemu_opt_get_number(opts
, "maxcpus", cpus
);
736 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
738 } else if (cores
== 0) {
739 threads
= threads
> 0 ? threads
: 1;
740 cores
= cpus
/ (sockets
* threads
);
741 cores
= cores
> 0 ? cores
: 1;
742 } else if (threads
== 0) {
743 threads
= cpus
/ (cores
* sockets
);
744 threads
= threads
> 0 ? threads
: 1;
745 } else if (sockets
* cores
* threads
< cpus
) {
746 error_report("cpu topology: "
747 "sockets (%u) * cores (%u) * threads (%u) < "
749 sockets
, cores
, threads
, cpus
);
754 qemu_opt_get_number(opts
, "maxcpus", cpus
);
756 if (ms
->smp
.max_cpus
< cpus
) {
757 error_report("maxcpus must be equal to or greater than smp");
761 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
762 error_report("cpu topology: "
763 "sockets (%u) * cores (%u) * threads (%u) > "
765 sockets
, cores
, threads
,
770 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
771 warn_report("Invalid CPU topology deprecated: "
772 "sockets (%u) * cores (%u) * threads (%u) "
774 sockets
, cores
, threads
,
779 ms
->smp
.cores
= cores
;
780 ms
->smp
.threads
= threads
;
783 if (ms
->smp
.cpus
> 1) {
784 Error
*blocker
= NULL
;
785 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
786 replay_add_blocker(blocker
);
790 static void machine_class_init(ObjectClass
*oc
, void *data
)
792 MachineClass
*mc
= MACHINE_CLASS(oc
);
794 /* Default 128 MB as guest ram size */
795 mc
->default_ram_size
= 128 * MiB
;
796 mc
->rom_file_has_mr
= true;
797 mc
->smp_parse
= smp_parse
;
799 /* numa node memory size aligned on 8MB by default.
800 * On Linux, each node's border has to be 8MB aligned
802 mc
->numa_mem_align_shift
= 23;
803 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
805 object_class_property_add_str(oc
, "accel",
806 machine_get_accel
, machine_set_accel
, &error_abort
);
807 object_class_property_set_description(oc
, "accel",
808 "Accelerator list", &error_abort
);
810 object_class_property_add(oc
, "kernel-irqchip", "on|off|split",
811 NULL
, machine_set_kernel_irqchip
,
812 NULL
, NULL
, &error_abort
);
813 object_class_property_set_description(oc
, "kernel-irqchip",
814 "Configure KVM in-kernel irqchip", &error_abort
);
816 object_class_property_add(oc
, "kvm-shadow-mem", "int",
817 machine_get_kvm_shadow_mem
, machine_set_kvm_shadow_mem
,
818 NULL
, NULL
, &error_abort
);
819 object_class_property_set_description(oc
, "kvm-shadow-mem",
820 "KVM shadow MMU size", &error_abort
);
822 object_class_property_add_str(oc
, "kernel",
823 machine_get_kernel
, machine_set_kernel
, &error_abort
);
824 object_class_property_set_description(oc
, "kernel",
825 "Linux kernel image file", &error_abort
);
827 object_class_property_add_str(oc
, "initrd",
828 machine_get_initrd
, machine_set_initrd
, &error_abort
);
829 object_class_property_set_description(oc
, "initrd",
830 "Linux initial ramdisk file", &error_abort
);
832 object_class_property_add_str(oc
, "append",
833 machine_get_append
, machine_set_append
, &error_abort
);
834 object_class_property_set_description(oc
, "append",
835 "Linux kernel command line", &error_abort
);
837 object_class_property_add_str(oc
, "dtb",
838 machine_get_dtb
, machine_set_dtb
, &error_abort
);
839 object_class_property_set_description(oc
, "dtb",
840 "Linux kernel device tree file", &error_abort
);
842 object_class_property_add_str(oc
, "dumpdtb",
843 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
844 object_class_property_set_description(oc
, "dumpdtb",
845 "Dump current dtb to a file and quit", &error_abort
);
847 object_class_property_add(oc
, "phandle-start", "int",
848 machine_get_phandle_start
, machine_set_phandle_start
,
849 NULL
, NULL
, &error_abort
);
850 object_class_property_set_description(oc
, "phandle-start",
851 "The first phandle ID we may generate dynamically", &error_abort
);
853 object_class_property_add_str(oc
, "dt-compatible",
854 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
855 object_class_property_set_description(oc
, "dt-compatible",
856 "Overrides the \"compatible\" property of the dt root node",
859 object_class_property_add_bool(oc
, "dump-guest-core",
860 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
861 object_class_property_set_description(oc
, "dump-guest-core",
862 "Include guest memory in a core dump", &error_abort
);
864 object_class_property_add_bool(oc
, "mem-merge",
865 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
866 object_class_property_set_description(oc
, "mem-merge",
867 "Enable/disable memory merge support", &error_abort
);
869 object_class_property_add_bool(oc
, "usb",
870 machine_get_usb
, machine_set_usb
, &error_abort
);
871 object_class_property_set_description(oc
, "usb",
872 "Set on/off to enable/disable usb", &error_abort
);
874 object_class_property_add_bool(oc
, "graphics",
875 machine_get_graphics
, machine_set_graphics
, &error_abort
);
876 object_class_property_set_description(oc
, "graphics",
877 "Set on/off to enable/disable graphics emulation", &error_abort
);
879 object_class_property_add_bool(oc
, "igd-passthru",
880 machine_get_igd_gfx_passthru
, machine_set_igd_gfx_passthru
,
882 object_class_property_set_description(oc
, "igd-passthru",
883 "Set on/off to enable/disable igd passthrou", &error_abort
);
885 object_class_property_add_str(oc
, "firmware",
886 machine_get_firmware
, machine_set_firmware
,
888 object_class_property_set_description(oc
, "firmware",
889 "Firmware image", &error_abort
);
891 object_class_property_add_bool(oc
, "suppress-vmdesc",
892 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
894 object_class_property_set_description(oc
, "suppress-vmdesc",
895 "Set on to disable self-describing migration", &error_abort
);
897 object_class_property_add_bool(oc
, "enforce-config-section",
898 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
900 object_class_property_set_description(oc
, "enforce-config-section",
901 "Set on to enforce configuration section migration", &error_abort
);
903 object_class_property_add_str(oc
, "memory-encryption",
904 machine_get_memory_encryption
, machine_set_memory_encryption
,
906 object_class_property_set_description(oc
, "memory-encryption",
907 "Set memory encryption object to use", &error_abort
);
910 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
912 if (!object_class_is_abstract(oc
)) {
913 MachineClass
*mc
= MACHINE_CLASS(oc
);
914 const char *cname
= object_class_get_name(oc
);
915 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
916 mc
->name
= g_strndup(cname
,
917 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
918 mc
->compat_props
= g_ptr_array_new();
922 static void machine_initfn(Object
*obj
)
924 MachineState
*ms
= MACHINE(obj
);
925 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
927 ms
->kernel_irqchip_allowed
= true;
928 ms
->kernel_irqchip_split
= mc
->default_kernel_irqchip_split
;
929 ms
->kvm_shadow_mem
= -1;
930 ms
->dump_guest_core
= true;
931 ms
->mem_merge
= true;
932 ms
->enable_graphics
= true;
934 if (mc
->nvdimm_supported
) {
935 Object
*obj
= OBJECT(ms
);
937 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
938 object_property_add_bool(obj
, "nvdimm",
939 machine_get_nvdimm
, machine_set_nvdimm
,
941 object_property_set_description(obj
, "nvdimm",
942 "Set on/off to enable/disable "
943 "NVDIMM instantiation", NULL
);
945 object_property_add_str(obj
, "nvdimm-persistence",
946 machine_get_nvdimm_persistence
,
947 machine_set_nvdimm_persistence
,
949 object_property_set_description(obj
, "nvdimm-persistence",
950 "Set NVDIMM persistence"
951 "Valid values are cpu, mem-ctrl",
956 /* Register notifier when init is done for sysbus sanity checks */
957 ms
->sysbus_notifier
.notify
= machine_init_notify
;
958 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
961 static void machine_finalize(Object
*obj
)
963 MachineState
*ms
= MACHINE(obj
);
966 g_free(ms
->kernel_filename
);
967 g_free(ms
->initrd_filename
);
968 g_free(ms
->kernel_cmdline
);
971 g_free(ms
->dt_compatible
);
972 g_free(ms
->firmware
);
973 g_free(ms
->device_memory
);
974 g_free(ms
->nvdimms_state
);
977 bool machine_usb(MachineState
*machine
)
982 bool machine_kernel_irqchip_allowed(MachineState
*machine
)
984 return machine
->kernel_irqchip_allowed
;
987 bool machine_kernel_irqchip_required(MachineState
*machine
)
989 return machine
->kernel_irqchip_required
;
992 bool machine_kernel_irqchip_split(MachineState
*machine
)
994 return machine
->kernel_irqchip_split
;
997 int machine_kvm_shadow_mem(MachineState
*machine
)
999 return machine
->kvm_shadow_mem
;
1002 int machine_phandle_start(MachineState
*machine
)
1004 return machine
->phandle_start
;
1007 bool machine_dump_guest_core(MachineState
*machine
)
1009 return machine
->dump_guest_core
;
1012 bool machine_mem_merge(MachineState
*machine
)
1014 return machine
->mem_merge
;
1017 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
1019 GString
*s
= g_string_new(NULL
);
1020 if (cpu
->props
.has_socket_id
) {
1021 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
1023 if (cpu
->props
.has_die_id
) {
1024 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
1026 if (cpu
->props
.has_core_id
) {
1028 g_string_append_printf(s
, ", ");
1030 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
1032 if (cpu
->props
.has_thread_id
) {
1034 g_string_append_printf(s
, ", ");
1036 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
1038 return g_string_free(s
, false);
1041 static void machine_numa_finish_cpu_init(MachineState
*machine
)
1044 bool default_mapping
;
1045 GString
*s
= g_string_new(NULL
);
1046 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1047 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
1049 assert(nb_numa_nodes
);
1050 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1051 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
1055 default_mapping
= (i
== possible_cpus
->len
);
1057 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1058 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1060 if (!cpu_slot
->props
.has_node_id
) {
1061 /* fetch default mapping from board and enable it */
1062 CpuInstanceProperties props
= cpu_slot
->props
;
1064 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1065 if (!default_mapping
) {
1066 /* record slots with not set mapping,
1067 * TODO: make it hard error in future */
1068 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1069 g_string_append_printf(s
, "%sCPU %d [%s]",
1070 s
->len
? ", " : "", i
, cpu_str
);
1073 /* non mapped cpus used to fallback to node 0 */
1077 props
.has_node_id
= true;
1078 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1081 if (s
->len
&& !qtest_enabled()) {
1082 warn_report("CPU(s) not present in any NUMA nodes: %s",
1084 warn_report("All CPU(s) up to maxcpus should be described "
1085 "in NUMA config, ability to start up with partial NUMA "
1086 "mappings is obsoleted and will be removed in future");
1088 g_string_free(s
, true);
1091 void machine_run_board_init(MachineState
*machine
)
1093 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1095 numa_complete_configuration(machine
);
1096 if (nb_numa_nodes
) {
1097 machine_numa_finish_cpu_init(machine
);
1100 /* If the machine supports the valid_cpu_types check and the user
1101 * specified a CPU with -cpu check here that the user CPU is supported.
1103 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1104 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1107 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1108 if (object_class_dynamic_cast(class,
1109 machine_class
->valid_cpu_types
[i
])) {
1110 /* The user specificed CPU is in the valid field, we are
1117 if (!machine_class
->valid_cpu_types
[i
]) {
1118 /* The user specified CPU is not valid */
1119 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1120 error_printf("The valid types are: %s",
1121 machine_class
->valid_cpu_types
[0]);
1122 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1123 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1131 machine_class
->init(machine
);
1134 static const TypeInfo machine_info
= {
1135 .name
= TYPE_MACHINE
,
1136 .parent
= TYPE_OBJECT
,
1138 .class_size
= sizeof(MachineClass
),
1139 .class_init
= machine_class_init
,
1140 .class_base_init
= machine_class_base_init
,
1141 .instance_size
= sizeof(MachineState
),
1142 .instance_init
= machine_initfn
,
1143 .instance_finalize
= machine_finalize
,
1146 static void machine_register_types(void)
1148 type_register_static(&machine_info
);
1151 type_init(machine_register_types
)