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" },
38 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
40 GlobalProperty hw_compat_3_1
[] = {
41 { "pcie-root-port", "x-speed", "2_5" },
42 { "pcie-root-port", "x-width", "1" },
43 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
44 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
45 { "tpm-crb", "ppi", "false" },
46 { "tpm-tis", "ppi", "false" },
47 { "usb-kbd", "serial", "42" },
48 { "usb-mouse", "serial", "42" },
49 { "usb-tablet", "serial", "42" },
50 { "virtio-blk-device", "discard", "false" },
51 { "virtio-blk-device", "write-zeroes", "false" },
53 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
55 GlobalProperty hw_compat_3_0
[] = {};
56 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
58 GlobalProperty hw_compat_2_12
[] = {
59 { "migration", "decompress-error-check", "off" },
60 { "hda-audio", "use-timer", "false" },
61 { "cirrus-vga", "global-vmstate", "true" },
62 { "VGA", "global-vmstate", "true" },
63 { "vmware-svga", "global-vmstate", "true" },
64 { "qxl-vga", "global-vmstate", "true" },
66 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
68 GlobalProperty hw_compat_2_11
[] = {
69 { "hpet", "hpet-offset-saved", "false" },
70 { "virtio-blk-pci", "vectors", "2" },
71 { "vhost-user-blk-pci", "vectors", "2" },
72 { "e1000", "migrate_tso_props", "off" },
74 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
76 GlobalProperty hw_compat_2_10
[] = {
77 { "virtio-mouse-device", "wheel-axis", "false" },
78 { "virtio-tablet-device", "wheel-axis", "false" },
80 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
82 GlobalProperty hw_compat_2_9
[] = {
83 { "pci-bridge", "shpc", "off" },
84 { "intel-iommu", "pt", "off" },
85 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
86 { "pcie-root-port", "x-migrate-msix", "false" },
88 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
90 GlobalProperty hw_compat_2_8
[] = {
91 { "fw_cfg_mem", "x-file-slots", "0x10" },
92 { "fw_cfg_io", "x-file-slots", "0x10" },
93 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
94 { "pci-bridge", "shpc", "on" },
95 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
96 { "virtio-pci", "x-pcie-deverr-init", "off" },
97 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
98 { "virtio-pci", "x-pcie-pm-init", "off" },
99 { "cirrus-vga", "vgamem_mb", "8" },
100 { "isa-cirrus-vga", "vgamem_mb", "8" },
102 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
104 GlobalProperty hw_compat_2_7
[] = {
105 { "virtio-pci", "page-per-vq", "on" },
106 { "virtio-serial-device", "emergency-write", "off" },
107 { "ioapic", "version", "0x11" },
108 { "intel-iommu", "x-buggy-eim", "true" },
109 { "virtio-pci", "x-ignore-backend-features", "on" },
111 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
113 GlobalProperty hw_compat_2_6
[] = {
114 { "virtio-mmio", "format_transport_address", "off" },
116 * don't include devices which are modern-only
117 * ie keyboard, mouse, tablet, gpu, vga & crypto
119 { "virtio-9p-pci", "disable-modern", "on" },
120 { "virtio-9p-pci", "disable-legacy", "off" },
121 { "virtio-balloon-pci", "disable-modern", "on" },
122 { "virtio-balloon-pci", "disable-legacy", "off" },
123 { "virtio-blk-pci", "disable-modern", "on" },
124 { "virtio-blk-pci", "disable-legacy", "off" },
125 { "virtio-input-host-pci", "disable-modern", "on" },
126 { "virtio-input-host-pci", "disable-legacy", "off" },
127 { "virtio-net-pci", "disable-modern", "on" },
128 { "virtio-net-pci", "disable-legacy", "off" },
129 { "virtio-rng-pci", "disable-modern", "on" },
130 { "virtio-rng-pci", "disable-legacy", "off" },
131 { "virtio-scsi-pci", "disable-modern", "on" },
132 { "virtio-scsi-pci", "disable-legacy", "off" },
133 { "virtio-serial-pci", "disable-modern", "on" },
134 { "virtio-serial-pci", "disable-legacy", "off" },
136 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
138 GlobalProperty hw_compat_2_5
[] = {
139 { "isa-fdc", "fallback", "144" },
140 { "pvscsi", "x-old-pci-configuration", "on" },
141 { "pvscsi", "x-disable-pcie", "on" },
142 { "vmxnet3", "x-old-msi-offsets", "on" },
143 { "vmxnet3", "x-disable-pcie", "on" },
145 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
147 GlobalProperty hw_compat_2_4
[] = {
148 { "virtio-blk-device", "scsi", "true" },
149 { "e1000", "extra_mac_registers", "off" },
150 { "virtio-pci", "x-disable-pcie", "on" },
151 { "virtio-pci", "migrate-extra", "off" },
152 { "fw_cfg_mem", "dma_enabled", "off" },
153 { "fw_cfg_io", "dma_enabled", "off" }
155 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
157 GlobalProperty hw_compat_2_3
[] = {
158 { "virtio-blk-pci", "any_layout", "off" },
159 { "virtio-balloon-pci", "any_layout", "off" },
160 { "virtio-serial-pci", "any_layout", "off" },
161 { "virtio-9p-pci", "any_layout", "off" },
162 { "virtio-rng-pci", "any_layout", "off" },
163 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
164 { "migration", "send-configuration", "off" },
165 { "migration", "send-section-footer", "off" },
166 { "migration", "store-global-state", "off" },
168 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
170 GlobalProperty hw_compat_2_2
[] = {};
171 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
173 GlobalProperty hw_compat_2_1
[] = {
174 { "intel-hda", "old_msi_addr", "on" },
175 { "VGA", "qemu-extended-regs", "off" },
176 { "secondary-vga", "qemu-extended-regs", "off" },
177 { "virtio-scsi-pci", "any_layout", "off" },
178 { "usb-mouse", "usb_version", "1" },
179 { "usb-kbd", "usb_version", "1" },
180 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
182 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
184 static char *machine_get_accel(Object
*obj
, Error
**errp
)
186 MachineState
*ms
= MACHINE(obj
);
188 return g_strdup(ms
->accel
);
191 static void machine_set_accel(Object
*obj
, const char *value
, Error
**errp
)
193 MachineState
*ms
= MACHINE(obj
);
196 ms
->accel
= g_strdup(value
);
199 static void machine_set_kernel_irqchip(Object
*obj
, Visitor
*v
,
200 const char *name
, void *opaque
,
204 MachineState
*ms
= MACHINE(obj
);
207 visit_type_OnOffSplit(v
, name
, &mode
, &err
);
209 error_propagate(errp
, err
);
213 case ON_OFF_SPLIT_ON
:
214 ms
->kernel_irqchip_allowed
= true;
215 ms
->kernel_irqchip_required
= true;
216 ms
->kernel_irqchip_split
= false;
218 case ON_OFF_SPLIT_OFF
:
219 ms
->kernel_irqchip_allowed
= false;
220 ms
->kernel_irqchip_required
= false;
221 ms
->kernel_irqchip_split
= false;
223 case ON_OFF_SPLIT_SPLIT
:
224 ms
->kernel_irqchip_allowed
= true;
225 ms
->kernel_irqchip_required
= true;
226 ms
->kernel_irqchip_split
= true;
229 /* The value was checked in visit_type_OnOffSplit() above. If
230 * we get here, then something is wrong in QEMU.
237 static void machine_get_kvm_shadow_mem(Object
*obj
, Visitor
*v
,
238 const char *name
, void *opaque
,
241 MachineState
*ms
= MACHINE(obj
);
242 int64_t value
= ms
->kvm_shadow_mem
;
244 visit_type_int(v
, name
, &value
, errp
);
247 static void machine_set_kvm_shadow_mem(Object
*obj
, Visitor
*v
,
248 const char *name
, void *opaque
,
251 MachineState
*ms
= MACHINE(obj
);
255 visit_type_int(v
, name
, &value
, &error
);
257 error_propagate(errp
, error
);
261 ms
->kvm_shadow_mem
= value
;
264 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
266 MachineState
*ms
= MACHINE(obj
);
268 return g_strdup(ms
->kernel_filename
);
271 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
273 MachineState
*ms
= MACHINE(obj
);
275 g_free(ms
->kernel_filename
);
276 ms
->kernel_filename
= g_strdup(value
);
279 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
281 MachineState
*ms
= MACHINE(obj
);
283 return g_strdup(ms
->initrd_filename
);
286 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
288 MachineState
*ms
= MACHINE(obj
);
290 g_free(ms
->initrd_filename
);
291 ms
->initrd_filename
= g_strdup(value
);
294 static char *machine_get_append(Object
*obj
, Error
**errp
)
296 MachineState
*ms
= MACHINE(obj
);
298 return g_strdup(ms
->kernel_cmdline
);
301 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
303 MachineState
*ms
= MACHINE(obj
);
305 g_free(ms
->kernel_cmdline
);
306 ms
->kernel_cmdline
= g_strdup(value
);
309 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
311 MachineState
*ms
= MACHINE(obj
);
313 return g_strdup(ms
->dtb
);
316 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
318 MachineState
*ms
= MACHINE(obj
);
321 ms
->dtb
= g_strdup(value
);
324 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
326 MachineState
*ms
= MACHINE(obj
);
328 return g_strdup(ms
->dumpdtb
);
331 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
333 MachineState
*ms
= MACHINE(obj
);
336 ms
->dumpdtb
= g_strdup(value
);
339 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
340 const char *name
, void *opaque
,
343 MachineState
*ms
= MACHINE(obj
);
344 int64_t value
= ms
->phandle_start
;
346 visit_type_int(v
, name
, &value
, errp
);
349 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
350 const char *name
, void *opaque
,
353 MachineState
*ms
= MACHINE(obj
);
357 visit_type_int(v
, name
, &value
, &error
);
359 error_propagate(errp
, error
);
363 ms
->phandle_start
= value
;
366 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
368 MachineState
*ms
= MACHINE(obj
);
370 return g_strdup(ms
->dt_compatible
);
373 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
375 MachineState
*ms
= MACHINE(obj
);
377 g_free(ms
->dt_compatible
);
378 ms
->dt_compatible
= g_strdup(value
);
381 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
383 MachineState
*ms
= MACHINE(obj
);
385 return ms
->dump_guest_core
;
388 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
390 MachineState
*ms
= MACHINE(obj
);
392 ms
->dump_guest_core
= value
;
395 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
397 MachineState
*ms
= MACHINE(obj
);
399 return ms
->mem_merge
;
402 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
404 MachineState
*ms
= MACHINE(obj
);
406 ms
->mem_merge
= value
;
409 static bool machine_get_usb(Object
*obj
, Error
**errp
)
411 MachineState
*ms
= MACHINE(obj
);
416 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
418 MachineState
*ms
= MACHINE(obj
);
421 ms
->usb_disabled
= !value
;
424 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
426 MachineState
*ms
= MACHINE(obj
);
428 return ms
->enable_graphics
;
431 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
433 MachineState
*ms
= MACHINE(obj
);
435 ms
->enable_graphics
= value
;
438 static bool machine_get_igd_gfx_passthru(Object
*obj
, Error
**errp
)
440 MachineState
*ms
= MACHINE(obj
);
442 return ms
->igd_gfx_passthru
;
445 static void machine_set_igd_gfx_passthru(Object
*obj
, bool value
, Error
**errp
)
447 MachineState
*ms
= MACHINE(obj
);
449 ms
->igd_gfx_passthru
= value
;
452 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
454 MachineState
*ms
= MACHINE(obj
);
456 return g_strdup(ms
->firmware
);
459 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
461 MachineState
*ms
= MACHINE(obj
);
463 g_free(ms
->firmware
);
464 ms
->firmware
= g_strdup(value
);
467 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
469 MachineState
*ms
= MACHINE(obj
);
471 ms
->suppress_vmdesc
= value
;
474 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
476 MachineState
*ms
= MACHINE(obj
);
478 return ms
->suppress_vmdesc
;
481 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
484 MachineState
*ms
= MACHINE(obj
);
486 warn_report("enforce-config-section is deprecated, please use "
487 "-global migration.send-configuration=on|off instead");
489 ms
->enforce_config_section
= value
;
492 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
494 MachineState
*ms
= MACHINE(obj
);
496 return ms
->enforce_config_section
;
499 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
501 MachineState
*ms
= MACHINE(obj
);
503 return g_strdup(ms
->memory_encryption
);
506 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
509 MachineState
*ms
= MACHINE(obj
);
511 g_free(ms
->memory_encryption
);
512 ms
->memory_encryption
= g_strdup(value
);
515 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
517 MachineState
*ms
= MACHINE(obj
);
519 return ms
->nvdimms_state
->is_enabled
;
522 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
524 MachineState
*ms
= MACHINE(obj
);
526 ms
->nvdimms_state
->is_enabled
= value
;
529 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
531 MachineState
*ms
= MACHINE(obj
);
533 return g_strdup(ms
->nvdimms_state
->persistence_string
);
536 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
539 MachineState
*ms
= MACHINE(obj
);
540 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
542 if (strcmp(value
, "cpu") == 0) {
543 nvdimms_state
->persistence
= 3;
544 } else if (strcmp(value
, "mem-ctrl") == 0) {
545 nvdimms_state
->persistence
= 2;
547 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
552 g_free(nvdimms_state
->persistence_string
);
553 nvdimms_state
->persistence_string
= g_strdup(value
);
556 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
558 strList
*item
= g_new0(strList
, 1);
560 item
->value
= g_strdup(type
);
561 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
562 mc
->allowed_dynamic_sysbus_devices
= item
;
565 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
567 MachineState
*machine
= opaque
;
568 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
569 bool allowed
= false;
572 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
575 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
579 error_report("Option '-device %s' cannot be handled by this machine",
580 object_class_get_name(object_get_class(OBJECT(sbdev
))));
585 static void machine_init_notify(Notifier
*notifier
, void *data
)
587 MachineState
*machine
= MACHINE(qdev_get_machine());
590 * Loop through all dynamically created sysbus devices and check if they are
591 * all allowed. If a device is not allowed, error out.
593 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
596 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
599 HotpluggableCPUList
*head
= NULL
;
600 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
602 /* force board to initialize possible_cpus if it hasn't been done yet */
603 mc
->possible_cpu_arch_ids(machine
);
605 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
607 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
608 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
610 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
611 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
612 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
613 sizeof(*cpu_item
->props
));
615 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
617 cpu_item
->has_qom_path
= true;
618 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
620 list_item
->value
= cpu_item
;
621 list_item
->next
= head
;
628 * machine_set_cpu_numa_node:
629 * @machine: machine object to modify
630 * @props: specifies which cpu objects to assign to
631 * numa node specified by @props.node_id
632 * @errp: if an error occurs, a pointer to an area to store the error
634 * Associate NUMA node specified by @props.node_id with cpu slots that
635 * match socket/core/thread-ids specified by @props. It's recommended to use
636 * query-hotpluggable-cpus.props values to specify affected cpu slots,
637 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
639 * However for CLI convenience it's possible to pass in subset of properties,
640 * which would affect all cpu slots that match it.
642 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
643 * -numa cpu,node-id=0,socket_id=0 \
644 * -numa cpu,node-id=1,socket_id=1
645 * will assign all child cores of socket 0 to node 0 and
646 * of socket 1 to node 1.
648 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
650 * Empty subset is disallowed and function will return with error in this case.
652 void machine_set_cpu_numa_node(MachineState
*machine
,
653 const CpuInstanceProperties
*props
, Error
**errp
)
655 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
659 if (!mc
->possible_cpu_arch_ids
) {
660 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
664 /* disabling node mapping is not supported, forbid it */
665 assert(props
->has_node_id
);
667 /* force board to initialize possible_cpus if it hasn't been done yet */
668 mc
->possible_cpu_arch_ids(machine
);
670 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
671 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
673 /* reject unsupported by board properties */
674 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
675 error_setg(errp
, "thread-id is not supported");
679 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
680 error_setg(errp
, "core-id is not supported");
684 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
685 error_setg(errp
, "socket-id is not supported");
689 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
690 error_setg(errp
, "die-id is not supported");
694 /* skip slots with explicit mismatch */
695 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
699 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
703 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
707 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
711 /* reject assignment if slot is already assigned, for compatibility
712 * of legacy cpu_index mapping with SPAPR core based mapping do not
713 * error out if cpu thread and matched core have the same node-id */
714 if (slot
->props
.has_node_id
&&
715 slot
->props
.node_id
!= props
->node_id
) {
716 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
717 slot
->props
.node_id
);
721 /* assign slot to node as it's matched '-numa cpu' key */
723 slot
->props
.node_id
= props
->node_id
;
724 slot
->props
.has_node_id
= props
->has_node_id
;
728 error_setg(errp
, "no match found");
732 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
735 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
736 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
737 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
738 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
740 /* compute missing values, prefer sockets over cores over threads */
741 if (cpus
== 0 || sockets
== 0) {
742 cores
= cores
> 0 ? cores
: 1;
743 threads
= threads
> 0 ? threads
: 1;
745 sockets
= sockets
> 0 ? sockets
: 1;
746 cpus
= cores
* threads
* sockets
;
749 qemu_opt_get_number(opts
, "maxcpus", cpus
);
750 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
752 } else if (cores
== 0) {
753 threads
= threads
> 0 ? threads
: 1;
754 cores
= cpus
/ (sockets
* threads
);
755 cores
= cores
> 0 ? cores
: 1;
756 } else if (threads
== 0) {
757 threads
= cpus
/ (cores
* sockets
);
758 threads
= threads
> 0 ? threads
: 1;
759 } else if (sockets
* cores
* threads
< cpus
) {
760 error_report("cpu topology: "
761 "sockets (%u) * cores (%u) * threads (%u) < "
763 sockets
, cores
, threads
, cpus
);
768 qemu_opt_get_number(opts
, "maxcpus", cpus
);
770 if (ms
->smp
.max_cpus
< cpus
) {
771 error_report("maxcpus must be equal to or greater than smp");
775 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
776 error_report("cpu topology: "
777 "sockets (%u) * cores (%u) * threads (%u) > "
779 sockets
, cores
, threads
,
784 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
785 warn_report("Invalid CPU topology deprecated: "
786 "sockets (%u) * cores (%u) * threads (%u) "
788 sockets
, cores
, threads
,
793 ms
->smp
.cores
= cores
;
794 ms
->smp
.threads
= threads
;
797 if (ms
->smp
.cpus
> 1) {
798 Error
*blocker
= NULL
;
799 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
800 replay_add_blocker(blocker
);
804 static void machine_class_init(ObjectClass
*oc
, void *data
)
806 MachineClass
*mc
= MACHINE_CLASS(oc
);
808 /* Default 128 MB as guest ram size */
809 mc
->default_ram_size
= 128 * MiB
;
810 mc
->rom_file_has_mr
= true;
811 mc
->smp_parse
= smp_parse
;
813 /* numa node memory size aligned on 8MB by default.
814 * On Linux, each node's border has to be 8MB aligned
816 mc
->numa_mem_align_shift
= 23;
817 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
819 object_class_property_add_str(oc
, "accel",
820 machine_get_accel
, machine_set_accel
, &error_abort
);
821 object_class_property_set_description(oc
, "accel",
822 "Accelerator list", &error_abort
);
824 object_class_property_add(oc
, "kernel-irqchip", "on|off|split",
825 NULL
, machine_set_kernel_irqchip
,
826 NULL
, NULL
, &error_abort
);
827 object_class_property_set_description(oc
, "kernel-irqchip",
828 "Configure KVM in-kernel irqchip", &error_abort
);
830 object_class_property_add(oc
, "kvm-shadow-mem", "int",
831 machine_get_kvm_shadow_mem
, machine_set_kvm_shadow_mem
,
832 NULL
, NULL
, &error_abort
);
833 object_class_property_set_description(oc
, "kvm-shadow-mem",
834 "KVM shadow MMU size", &error_abort
);
836 object_class_property_add_str(oc
, "kernel",
837 machine_get_kernel
, machine_set_kernel
, &error_abort
);
838 object_class_property_set_description(oc
, "kernel",
839 "Linux kernel image file", &error_abort
);
841 object_class_property_add_str(oc
, "initrd",
842 machine_get_initrd
, machine_set_initrd
, &error_abort
);
843 object_class_property_set_description(oc
, "initrd",
844 "Linux initial ramdisk file", &error_abort
);
846 object_class_property_add_str(oc
, "append",
847 machine_get_append
, machine_set_append
, &error_abort
);
848 object_class_property_set_description(oc
, "append",
849 "Linux kernel command line", &error_abort
);
851 object_class_property_add_str(oc
, "dtb",
852 machine_get_dtb
, machine_set_dtb
, &error_abort
);
853 object_class_property_set_description(oc
, "dtb",
854 "Linux kernel device tree file", &error_abort
);
856 object_class_property_add_str(oc
, "dumpdtb",
857 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
858 object_class_property_set_description(oc
, "dumpdtb",
859 "Dump current dtb to a file and quit", &error_abort
);
861 object_class_property_add(oc
, "phandle-start", "int",
862 machine_get_phandle_start
, machine_set_phandle_start
,
863 NULL
, NULL
, &error_abort
);
864 object_class_property_set_description(oc
, "phandle-start",
865 "The first phandle ID we may generate dynamically", &error_abort
);
867 object_class_property_add_str(oc
, "dt-compatible",
868 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
869 object_class_property_set_description(oc
, "dt-compatible",
870 "Overrides the \"compatible\" property of the dt root node",
873 object_class_property_add_bool(oc
, "dump-guest-core",
874 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
875 object_class_property_set_description(oc
, "dump-guest-core",
876 "Include guest memory in a core dump", &error_abort
);
878 object_class_property_add_bool(oc
, "mem-merge",
879 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
880 object_class_property_set_description(oc
, "mem-merge",
881 "Enable/disable memory merge support", &error_abort
);
883 object_class_property_add_bool(oc
, "usb",
884 machine_get_usb
, machine_set_usb
, &error_abort
);
885 object_class_property_set_description(oc
, "usb",
886 "Set on/off to enable/disable usb", &error_abort
);
888 object_class_property_add_bool(oc
, "graphics",
889 machine_get_graphics
, machine_set_graphics
, &error_abort
);
890 object_class_property_set_description(oc
, "graphics",
891 "Set on/off to enable/disable graphics emulation", &error_abort
);
893 object_class_property_add_bool(oc
, "igd-passthru",
894 machine_get_igd_gfx_passthru
, machine_set_igd_gfx_passthru
,
896 object_class_property_set_description(oc
, "igd-passthru",
897 "Set on/off to enable/disable igd passthrou", &error_abort
);
899 object_class_property_add_str(oc
, "firmware",
900 machine_get_firmware
, machine_set_firmware
,
902 object_class_property_set_description(oc
, "firmware",
903 "Firmware image", &error_abort
);
905 object_class_property_add_bool(oc
, "suppress-vmdesc",
906 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
908 object_class_property_set_description(oc
, "suppress-vmdesc",
909 "Set on to disable self-describing migration", &error_abort
);
911 object_class_property_add_bool(oc
, "enforce-config-section",
912 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
914 object_class_property_set_description(oc
, "enforce-config-section",
915 "Set on to enforce configuration section migration", &error_abort
);
917 object_class_property_add_str(oc
, "memory-encryption",
918 machine_get_memory_encryption
, machine_set_memory_encryption
,
920 object_class_property_set_description(oc
, "memory-encryption",
921 "Set memory encryption object to use", &error_abort
);
924 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
926 if (!object_class_is_abstract(oc
)) {
927 MachineClass
*mc
= MACHINE_CLASS(oc
);
928 const char *cname
= object_class_get_name(oc
);
929 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
930 mc
->name
= g_strndup(cname
,
931 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
932 mc
->compat_props
= g_ptr_array_new();
936 static void machine_initfn(Object
*obj
)
938 MachineState
*ms
= MACHINE(obj
);
939 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
941 ms
->kernel_irqchip_allowed
= true;
942 ms
->kernel_irqchip_split
= mc
->default_kernel_irqchip_split
;
943 ms
->kvm_shadow_mem
= -1;
944 ms
->dump_guest_core
= true;
945 ms
->mem_merge
= true;
946 ms
->enable_graphics
= true;
948 if (mc
->nvdimm_supported
) {
949 Object
*obj
= OBJECT(ms
);
951 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
952 object_property_add_bool(obj
, "nvdimm",
953 machine_get_nvdimm
, machine_set_nvdimm
,
955 object_property_set_description(obj
, "nvdimm",
956 "Set on/off to enable/disable "
957 "NVDIMM instantiation", NULL
);
959 object_property_add_str(obj
, "nvdimm-persistence",
960 machine_get_nvdimm_persistence
,
961 machine_set_nvdimm_persistence
,
963 object_property_set_description(obj
, "nvdimm-persistence",
964 "Set NVDIMM persistence"
965 "Valid values are cpu, mem-ctrl",
970 /* Register notifier when init is done for sysbus sanity checks */
971 ms
->sysbus_notifier
.notify
= machine_init_notify
;
972 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
975 static void machine_finalize(Object
*obj
)
977 MachineState
*ms
= MACHINE(obj
);
980 g_free(ms
->kernel_filename
);
981 g_free(ms
->initrd_filename
);
982 g_free(ms
->kernel_cmdline
);
985 g_free(ms
->dt_compatible
);
986 g_free(ms
->firmware
);
987 g_free(ms
->device_memory
);
988 g_free(ms
->nvdimms_state
);
991 bool machine_usb(MachineState
*machine
)
996 bool machine_kernel_irqchip_allowed(MachineState
*machine
)
998 return machine
->kernel_irqchip_allowed
;
1001 bool machine_kernel_irqchip_required(MachineState
*machine
)
1003 return machine
->kernel_irqchip_required
;
1006 bool machine_kernel_irqchip_split(MachineState
*machine
)
1008 return machine
->kernel_irqchip_split
;
1011 int machine_kvm_shadow_mem(MachineState
*machine
)
1013 return machine
->kvm_shadow_mem
;
1016 int machine_phandle_start(MachineState
*machine
)
1018 return machine
->phandle_start
;
1021 bool machine_dump_guest_core(MachineState
*machine
)
1023 return machine
->dump_guest_core
;
1026 bool machine_mem_merge(MachineState
*machine
)
1028 return machine
->mem_merge
;
1031 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
1033 GString
*s
= g_string_new(NULL
);
1034 if (cpu
->props
.has_socket_id
) {
1035 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
1037 if (cpu
->props
.has_die_id
) {
1038 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
1040 if (cpu
->props
.has_core_id
) {
1042 g_string_append_printf(s
, ", ");
1044 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
1046 if (cpu
->props
.has_thread_id
) {
1048 g_string_append_printf(s
, ", ");
1050 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
1052 return g_string_free(s
, false);
1055 static void machine_numa_finish_cpu_init(MachineState
*machine
)
1058 bool default_mapping
;
1059 GString
*s
= g_string_new(NULL
);
1060 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
1061 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
1063 assert(nb_numa_nodes
);
1064 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1065 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
1069 default_mapping
= (i
== possible_cpus
->len
);
1071 for (i
= 0; i
< possible_cpus
->len
; i
++) {
1072 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
1074 if (!cpu_slot
->props
.has_node_id
) {
1075 /* fetch default mapping from board and enable it */
1076 CpuInstanceProperties props
= cpu_slot
->props
;
1078 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
1079 if (!default_mapping
) {
1080 /* record slots with not set mapping,
1081 * TODO: make it hard error in future */
1082 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
1083 g_string_append_printf(s
, "%sCPU %d [%s]",
1084 s
->len
? ", " : "", i
, cpu_str
);
1087 /* non mapped cpus used to fallback to node 0 */
1091 props
.has_node_id
= true;
1092 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
1095 if (s
->len
&& !qtest_enabled()) {
1096 warn_report("CPU(s) not present in any NUMA nodes: %s",
1098 warn_report("All CPU(s) up to maxcpus should be described "
1099 "in NUMA config, ability to start up with partial NUMA "
1100 "mappings is obsoleted and will be removed in future");
1102 g_string_free(s
, true);
1105 void machine_run_board_init(MachineState
*machine
)
1107 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
1109 numa_complete_configuration(machine
);
1110 if (nb_numa_nodes
) {
1111 machine_numa_finish_cpu_init(machine
);
1114 /* If the machine supports the valid_cpu_types check and the user
1115 * specified a CPU with -cpu check here that the user CPU is supported.
1117 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
1118 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
1121 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
1122 if (object_class_dynamic_cast(class,
1123 machine_class
->valid_cpu_types
[i
])) {
1124 /* The user specificed CPU is in the valid field, we are
1131 if (!machine_class
->valid_cpu_types
[i
]) {
1132 /* The user specified CPU is not valid */
1133 error_report("Invalid CPU type: %s", machine
->cpu_type
);
1134 error_printf("The valid types are: %s",
1135 machine_class
->valid_cpu_types
[0]);
1136 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
1137 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1145 machine_class
->init(machine
);
1148 static const TypeInfo machine_info
= {
1149 .name
= TYPE_MACHINE
,
1150 .parent
= TYPE_OBJECT
,
1152 .class_size
= sizeof(MachineClass
),
1153 .class_init
= machine_class_init
,
1154 .class_base_init
= machine_class_base_init
,
1155 .instance_size
= sizeof(MachineState
),
1156 .instance_init
= machine_initfn
,
1157 .instance_finalize
= machine_finalize
,
1160 static void machine_register_types(void)
1162 type_register_static(&machine_info
);
1165 type_init(machine_register_types
)