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" },
33 const size_t hw_compat_4_2_len
= G_N_ELEMENTS(hw_compat_4_2
);
35 GlobalProperty hw_compat_4_1
[] = {
36 { "virtio-pci", "x-pcie-flr-init", "off" },
38 const size_t hw_compat_4_1_len
= G_N_ELEMENTS(hw_compat_4_1
);
40 GlobalProperty hw_compat_4_0
[] = {
41 { "VGA", "edid", "false" },
42 { "secondary-vga", "edid", "false" },
43 { "bochs-display", "edid", "false" },
44 { "virtio-vga", "edid", "false" },
45 { "virtio-gpu", "edid", "false" },
46 { "virtio-device", "use-started", "false" },
47 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
48 { "pl031", "migrate-tick-offset", "false" },
50 const size_t hw_compat_4_0_len
= G_N_ELEMENTS(hw_compat_4_0
);
52 GlobalProperty hw_compat_3_1
[] = {
53 { "pcie-root-port", "x-speed", "2_5" },
54 { "pcie-root-port", "x-width", "1" },
55 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
56 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
57 { "tpm-crb", "ppi", "false" },
58 { "tpm-tis", "ppi", "false" },
59 { "usb-kbd", "serial", "42" },
60 { "usb-mouse", "serial", "42" },
61 { "usb-tablet", "serial", "42" },
62 { "virtio-blk-device", "discard", "false" },
63 { "virtio-blk-device", "write-zeroes", "false" },
64 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
65 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
67 const size_t hw_compat_3_1_len
= G_N_ELEMENTS(hw_compat_3_1
);
69 GlobalProperty hw_compat_3_0
[] = {};
70 const size_t hw_compat_3_0_len
= G_N_ELEMENTS(hw_compat_3_0
);
72 GlobalProperty hw_compat_2_12
[] = {
73 { "migration", "decompress-error-check", "off" },
74 { "hda-audio", "use-timer", "false" },
75 { "cirrus-vga", "global-vmstate", "true" },
76 { "VGA", "global-vmstate", "true" },
77 { "vmware-svga", "global-vmstate", "true" },
78 { "qxl-vga", "global-vmstate", "true" },
80 const size_t hw_compat_2_12_len
= G_N_ELEMENTS(hw_compat_2_12
);
82 GlobalProperty hw_compat_2_11
[] = {
83 { "hpet", "hpet-offset-saved", "false" },
84 { "virtio-blk-pci", "vectors", "2" },
85 { "vhost-user-blk-pci", "vectors", "2" },
86 { "e1000", "migrate_tso_props", "off" },
88 const size_t hw_compat_2_11_len
= G_N_ELEMENTS(hw_compat_2_11
);
90 GlobalProperty hw_compat_2_10
[] = {
91 { "virtio-mouse-device", "wheel-axis", "false" },
92 { "virtio-tablet-device", "wheel-axis", "false" },
94 const size_t hw_compat_2_10_len
= G_N_ELEMENTS(hw_compat_2_10
);
96 GlobalProperty hw_compat_2_9
[] = {
97 { "pci-bridge", "shpc", "off" },
98 { "intel-iommu", "pt", "off" },
99 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
100 { "pcie-root-port", "x-migrate-msix", "false" },
102 const size_t hw_compat_2_9_len
= G_N_ELEMENTS(hw_compat_2_9
);
104 GlobalProperty hw_compat_2_8
[] = {
105 { "fw_cfg_mem", "x-file-slots", "0x10" },
106 { "fw_cfg_io", "x-file-slots", "0x10" },
107 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
108 { "pci-bridge", "shpc", "on" },
109 { TYPE_PCI_DEVICE
, "x-pcie-extcap-init", "off" },
110 { "virtio-pci", "x-pcie-deverr-init", "off" },
111 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
112 { "virtio-pci", "x-pcie-pm-init", "off" },
113 { "cirrus-vga", "vgamem_mb", "8" },
114 { "isa-cirrus-vga", "vgamem_mb", "8" },
116 const size_t hw_compat_2_8_len
= G_N_ELEMENTS(hw_compat_2_8
);
118 GlobalProperty hw_compat_2_7
[] = {
119 { "virtio-pci", "page-per-vq", "on" },
120 { "virtio-serial-device", "emergency-write", "off" },
121 { "ioapic", "version", "0x11" },
122 { "intel-iommu", "x-buggy-eim", "true" },
123 { "virtio-pci", "x-ignore-backend-features", "on" },
125 const size_t hw_compat_2_7_len
= G_N_ELEMENTS(hw_compat_2_7
);
127 GlobalProperty hw_compat_2_6
[] = {
128 { "virtio-mmio", "format_transport_address", "off" },
129 /* Optional because not all virtio-pci devices support legacy mode */
130 { "virtio-pci", "disable-modern", "on", .optional
= true },
131 { "virtio-pci", "disable-legacy", "off", .optional
= true },
133 const size_t hw_compat_2_6_len
= G_N_ELEMENTS(hw_compat_2_6
);
135 GlobalProperty hw_compat_2_5
[] = {
136 { "isa-fdc", "fallback", "144" },
137 { "pvscsi", "x-old-pci-configuration", "on" },
138 { "pvscsi", "x-disable-pcie", "on" },
139 { "vmxnet3", "x-old-msi-offsets", "on" },
140 { "vmxnet3", "x-disable-pcie", "on" },
142 const size_t hw_compat_2_5_len
= G_N_ELEMENTS(hw_compat_2_5
);
144 GlobalProperty hw_compat_2_4
[] = {
145 { "virtio-blk-device", "scsi", "true" },
146 { "e1000", "extra_mac_registers", "off" },
147 { "virtio-pci", "x-disable-pcie", "on" },
148 { "virtio-pci", "migrate-extra", "off" },
149 { "fw_cfg_mem", "dma_enabled", "off" },
150 { "fw_cfg_io", "dma_enabled", "off" }
152 const size_t hw_compat_2_4_len
= G_N_ELEMENTS(hw_compat_2_4
);
154 GlobalProperty hw_compat_2_3
[] = {
155 { "virtio-blk-pci", "any_layout", "off" },
156 { "virtio-balloon-pci", "any_layout", "off" },
157 { "virtio-serial-pci", "any_layout", "off" },
158 { "virtio-9p-pci", "any_layout", "off" },
159 { "virtio-rng-pci", "any_layout", "off" },
160 { TYPE_PCI_DEVICE
, "x-pcie-lnksta-dllla", "off" },
161 { "migration", "send-configuration", "off" },
162 { "migration", "send-section-footer", "off" },
163 { "migration", "store-global-state", "off" },
165 const size_t hw_compat_2_3_len
= G_N_ELEMENTS(hw_compat_2_3
);
167 GlobalProperty hw_compat_2_2
[] = {};
168 const size_t hw_compat_2_2_len
= G_N_ELEMENTS(hw_compat_2_2
);
170 GlobalProperty hw_compat_2_1
[] = {
171 { "intel-hda", "old_msi_addr", "on" },
172 { "VGA", "qemu-extended-regs", "off" },
173 { "secondary-vga", "qemu-extended-regs", "off" },
174 { "virtio-scsi-pci", "any_layout", "off" },
175 { "usb-mouse", "usb_version", "1" },
176 { "usb-kbd", "usb_version", "1" },
177 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
179 const size_t hw_compat_2_1_len
= G_N_ELEMENTS(hw_compat_2_1
);
181 static char *machine_get_kernel(Object
*obj
, Error
**errp
)
183 MachineState
*ms
= MACHINE(obj
);
185 return g_strdup(ms
->kernel_filename
);
188 static void machine_set_kernel(Object
*obj
, const char *value
, Error
**errp
)
190 MachineState
*ms
= MACHINE(obj
);
192 g_free(ms
->kernel_filename
);
193 ms
->kernel_filename
= g_strdup(value
);
196 static char *machine_get_initrd(Object
*obj
, Error
**errp
)
198 MachineState
*ms
= MACHINE(obj
);
200 return g_strdup(ms
->initrd_filename
);
203 static void machine_set_initrd(Object
*obj
, const char *value
, Error
**errp
)
205 MachineState
*ms
= MACHINE(obj
);
207 g_free(ms
->initrd_filename
);
208 ms
->initrd_filename
= g_strdup(value
);
211 static char *machine_get_append(Object
*obj
, Error
**errp
)
213 MachineState
*ms
= MACHINE(obj
);
215 return g_strdup(ms
->kernel_cmdline
);
218 static void machine_set_append(Object
*obj
, const char *value
, Error
**errp
)
220 MachineState
*ms
= MACHINE(obj
);
222 g_free(ms
->kernel_cmdline
);
223 ms
->kernel_cmdline
= g_strdup(value
);
226 static char *machine_get_dtb(Object
*obj
, Error
**errp
)
228 MachineState
*ms
= MACHINE(obj
);
230 return g_strdup(ms
->dtb
);
233 static void machine_set_dtb(Object
*obj
, const char *value
, Error
**errp
)
235 MachineState
*ms
= MACHINE(obj
);
238 ms
->dtb
= g_strdup(value
);
241 static char *machine_get_dumpdtb(Object
*obj
, Error
**errp
)
243 MachineState
*ms
= MACHINE(obj
);
245 return g_strdup(ms
->dumpdtb
);
248 static void machine_set_dumpdtb(Object
*obj
, const char *value
, Error
**errp
)
250 MachineState
*ms
= MACHINE(obj
);
253 ms
->dumpdtb
= g_strdup(value
);
256 static void machine_get_phandle_start(Object
*obj
, Visitor
*v
,
257 const char *name
, void *opaque
,
260 MachineState
*ms
= MACHINE(obj
);
261 int64_t value
= ms
->phandle_start
;
263 visit_type_int(v
, name
, &value
, errp
);
266 static void machine_set_phandle_start(Object
*obj
, Visitor
*v
,
267 const char *name
, void *opaque
,
270 MachineState
*ms
= MACHINE(obj
);
274 visit_type_int(v
, name
, &value
, &error
);
276 error_propagate(errp
, error
);
280 ms
->phandle_start
= value
;
283 static char *machine_get_dt_compatible(Object
*obj
, Error
**errp
)
285 MachineState
*ms
= MACHINE(obj
);
287 return g_strdup(ms
->dt_compatible
);
290 static void machine_set_dt_compatible(Object
*obj
, const char *value
, Error
**errp
)
292 MachineState
*ms
= MACHINE(obj
);
294 g_free(ms
->dt_compatible
);
295 ms
->dt_compatible
= g_strdup(value
);
298 static bool machine_get_dump_guest_core(Object
*obj
, Error
**errp
)
300 MachineState
*ms
= MACHINE(obj
);
302 return ms
->dump_guest_core
;
305 static void machine_set_dump_guest_core(Object
*obj
, bool value
, Error
**errp
)
307 MachineState
*ms
= MACHINE(obj
);
309 ms
->dump_guest_core
= value
;
312 static bool machine_get_mem_merge(Object
*obj
, Error
**errp
)
314 MachineState
*ms
= MACHINE(obj
);
316 return ms
->mem_merge
;
319 static void machine_set_mem_merge(Object
*obj
, bool value
, Error
**errp
)
321 MachineState
*ms
= MACHINE(obj
);
323 ms
->mem_merge
= value
;
326 static bool machine_get_usb(Object
*obj
, Error
**errp
)
328 MachineState
*ms
= MACHINE(obj
);
333 static void machine_set_usb(Object
*obj
, bool value
, Error
**errp
)
335 MachineState
*ms
= MACHINE(obj
);
338 ms
->usb_disabled
= !value
;
341 static bool machine_get_graphics(Object
*obj
, Error
**errp
)
343 MachineState
*ms
= MACHINE(obj
);
345 return ms
->enable_graphics
;
348 static void machine_set_graphics(Object
*obj
, bool value
, Error
**errp
)
350 MachineState
*ms
= MACHINE(obj
);
352 ms
->enable_graphics
= value
;
355 static char *machine_get_firmware(Object
*obj
, Error
**errp
)
357 MachineState
*ms
= MACHINE(obj
);
359 return g_strdup(ms
->firmware
);
362 static void machine_set_firmware(Object
*obj
, const char *value
, Error
**errp
)
364 MachineState
*ms
= MACHINE(obj
);
366 g_free(ms
->firmware
);
367 ms
->firmware
= g_strdup(value
);
370 static void machine_set_suppress_vmdesc(Object
*obj
, bool value
, Error
**errp
)
372 MachineState
*ms
= MACHINE(obj
);
374 ms
->suppress_vmdesc
= value
;
377 static bool machine_get_suppress_vmdesc(Object
*obj
, Error
**errp
)
379 MachineState
*ms
= MACHINE(obj
);
381 return ms
->suppress_vmdesc
;
384 static void machine_set_enforce_config_section(Object
*obj
, bool value
,
387 MachineState
*ms
= MACHINE(obj
);
389 warn_report("enforce-config-section is deprecated, please use "
390 "-global migration.send-configuration=on|off instead");
392 ms
->enforce_config_section
= value
;
395 static bool machine_get_enforce_config_section(Object
*obj
, Error
**errp
)
397 MachineState
*ms
= MACHINE(obj
);
399 return ms
->enforce_config_section
;
402 static char *machine_get_memory_encryption(Object
*obj
, Error
**errp
)
404 MachineState
*ms
= MACHINE(obj
);
406 return g_strdup(ms
->memory_encryption
);
409 static void machine_set_memory_encryption(Object
*obj
, const char *value
,
412 MachineState
*ms
= MACHINE(obj
);
414 g_free(ms
->memory_encryption
);
415 ms
->memory_encryption
= g_strdup(value
);
418 static bool machine_get_nvdimm(Object
*obj
, Error
**errp
)
420 MachineState
*ms
= MACHINE(obj
);
422 return ms
->nvdimms_state
->is_enabled
;
425 static void machine_set_nvdimm(Object
*obj
, bool value
, Error
**errp
)
427 MachineState
*ms
= MACHINE(obj
);
429 ms
->nvdimms_state
->is_enabled
= value
;
432 static char *machine_get_nvdimm_persistence(Object
*obj
, Error
**errp
)
434 MachineState
*ms
= MACHINE(obj
);
436 return g_strdup(ms
->nvdimms_state
->persistence_string
);
439 static void machine_set_nvdimm_persistence(Object
*obj
, const char *value
,
442 MachineState
*ms
= MACHINE(obj
);
443 NVDIMMState
*nvdimms_state
= ms
->nvdimms_state
;
445 if (strcmp(value
, "cpu") == 0) {
446 nvdimms_state
->persistence
= 3;
447 } else if (strcmp(value
, "mem-ctrl") == 0) {
448 nvdimms_state
->persistence
= 2;
450 error_setg(errp
, "-machine nvdimm-persistence=%s: unsupported option",
455 g_free(nvdimms_state
->persistence_string
);
456 nvdimms_state
->persistence_string
= g_strdup(value
);
459 void machine_class_allow_dynamic_sysbus_dev(MachineClass
*mc
, const char *type
)
461 strList
*item
= g_new0(strList
, 1);
463 item
->value
= g_strdup(type
);
464 item
->next
= mc
->allowed_dynamic_sysbus_devices
;
465 mc
->allowed_dynamic_sysbus_devices
= item
;
468 static void validate_sysbus_device(SysBusDevice
*sbdev
, void *opaque
)
470 MachineState
*machine
= opaque
;
471 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
472 bool allowed
= false;
475 for (wl
= mc
->allowed_dynamic_sysbus_devices
;
478 allowed
|= !!object_dynamic_cast(OBJECT(sbdev
), wl
->value
);
482 error_report("Option '-device %s' cannot be handled by this machine",
483 object_class_get_name(object_get_class(OBJECT(sbdev
))));
488 static void machine_init_notify(Notifier
*notifier
, void *data
)
490 MachineState
*machine
= MACHINE(qdev_get_machine());
493 * Loop through all dynamically created sysbus devices and check if they are
494 * all allowed. If a device is not allowed, error out.
496 foreach_dynamic_sysbus_device(validate_sysbus_device
, machine
);
499 HotpluggableCPUList
*machine_query_hotpluggable_cpus(MachineState
*machine
)
502 HotpluggableCPUList
*head
= NULL
;
503 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
505 /* force board to initialize possible_cpus if it hasn't been done yet */
506 mc
->possible_cpu_arch_ids(machine
);
508 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
510 HotpluggableCPUList
*list_item
= g_new0(typeof(*list_item
), 1);
511 HotpluggableCPU
*cpu_item
= g_new0(typeof(*cpu_item
), 1);
513 cpu_item
->type
= g_strdup(machine
->possible_cpus
->cpus
[i
].type
);
514 cpu_item
->vcpus_count
= machine
->possible_cpus
->cpus
[i
].vcpus_count
;
515 cpu_item
->props
= g_memdup(&machine
->possible_cpus
->cpus
[i
].props
,
516 sizeof(*cpu_item
->props
));
518 cpu
= machine
->possible_cpus
->cpus
[i
].cpu
;
520 cpu_item
->has_qom_path
= true;
521 cpu_item
->qom_path
= object_get_canonical_path(cpu
);
523 list_item
->value
= cpu_item
;
524 list_item
->next
= head
;
531 * machine_set_cpu_numa_node:
532 * @machine: machine object to modify
533 * @props: specifies which cpu objects to assign to
534 * numa node specified by @props.node_id
535 * @errp: if an error occurs, a pointer to an area to store the error
537 * Associate NUMA node specified by @props.node_id with cpu slots that
538 * match socket/core/thread-ids specified by @props. It's recommended to use
539 * query-hotpluggable-cpus.props values to specify affected cpu slots,
540 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
542 * However for CLI convenience it's possible to pass in subset of properties,
543 * which would affect all cpu slots that match it.
545 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
546 * -numa cpu,node-id=0,socket_id=0 \
547 * -numa cpu,node-id=1,socket_id=1
548 * will assign all child cores of socket 0 to node 0 and
549 * of socket 1 to node 1.
551 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
553 * Empty subset is disallowed and function will return with error in this case.
555 void machine_set_cpu_numa_node(MachineState
*machine
,
556 const CpuInstanceProperties
*props
, Error
**errp
)
558 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
562 if (!mc
->possible_cpu_arch_ids
) {
563 error_setg(errp
, "mapping of CPUs to NUMA node is not supported");
567 /* disabling node mapping is not supported, forbid it */
568 assert(props
->has_node_id
);
570 /* force board to initialize possible_cpus if it hasn't been done yet */
571 mc
->possible_cpu_arch_ids(machine
);
573 for (i
= 0; i
< machine
->possible_cpus
->len
; i
++) {
574 CPUArchId
*slot
= &machine
->possible_cpus
->cpus
[i
];
576 /* reject unsupported by board properties */
577 if (props
->has_thread_id
&& !slot
->props
.has_thread_id
) {
578 error_setg(errp
, "thread-id is not supported");
582 if (props
->has_core_id
&& !slot
->props
.has_core_id
) {
583 error_setg(errp
, "core-id is not supported");
587 if (props
->has_socket_id
&& !slot
->props
.has_socket_id
) {
588 error_setg(errp
, "socket-id is not supported");
592 if (props
->has_die_id
&& !slot
->props
.has_die_id
) {
593 error_setg(errp
, "die-id is not supported");
597 /* skip slots with explicit mismatch */
598 if (props
->has_thread_id
&& props
->thread_id
!= slot
->props
.thread_id
) {
602 if (props
->has_core_id
&& props
->core_id
!= slot
->props
.core_id
) {
606 if (props
->has_die_id
&& props
->die_id
!= slot
->props
.die_id
) {
610 if (props
->has_socket_id
&& props
->socket_id
!= slot
->props
.socket_id
) {
614 /* reject assignment if slot is already assigned, for compatibility
615 * of legacy cpu_index mapping with SPAPR core based mapping do not
616 * error out if cpu thread and matched core have the same node-id */
617 if (slot
->props
.has_node_id
&&
618 slot
->props
.node_id
!= props
->node_id
) {
619 error_setg(errp
, "CPU is already assigned to node-id: %" PRId64
,
620 slot
->props
.node_id
);
624 /* assign slot to node as it's matched '-numa cpu' key */
626 slot
->props
.node_id
= props
->node_id
;
627 slot
->props
.has_node_id
= props
->has_node_id
;
631 error_setg(errp
, "no match found");
635 static void smp_parse(MachineState
*ms
, QemuOpts
*opts
)
638 unsigned cpus
= qemu_opt_get_number(opts
, "cpus", 0);
639 unsigned sockets
= qemu_opt_get_number(opts
, "sockets", 0);
640 unsigned cores
= qemu_opt_get_number(opts
, "cores", 0);
641 unsigned threads
= qemu_opt_get_number(opts
, "threads", 0);
643 /* compute missing values, prefer sockets over cores over threads */
644 if (cpus
== 0 || sockets
== 0) {
645 cores
= cores
> 0 ? cores
: 1;
646 threads
= threads
> 0 ? threads
: 1;
648 sockets
= sockets
> 0 ? sockets
: 1;
649 cpus
= cores
* threads
* sockets
;
652 qemu_opt_get_number(opts
, "maxcpus", cpus
);
653 sockets
= ms
->smp
.max_cpus
/ (cores
* threads
);
655 } else if (cores
== 0) {
656 threads
= threads
> 0 ? threads
: 1;
657 cores
= cpus
/ (sockets
* threads
);
658 cores
= cores
> 0 ? cores
: 1;
659 } else if (threads
== 0) {
660 threads
= cpus
/ (cores
* sockets
);
661 threads
= threads
> 0 ? threads
: 1;
662 } else if (sockets
* cores
* threads
< cpus
) {
663 error_report("cpu topology: "
664 "sockets (%u) * cores (%u) * threads (%u) < "
666 sockets
, cores
, threads
, cpus
);
671 qemu_opt_get_number(opts
, "maxcpus", cpus
);
673 if (ms
->smp
.max_cpus
< cpus
) {
674 error_report("maxcpus must be equal to or greater than smp");
678 if (sockets
* cores
* threads
> ms
->smp
.max_cpus
) {
679 error_report("cpu topology: "
680 "sockets (%u) * cores (%u) * threads (%u) > "
682 sockets
, cores
, threads
,
687 if (sockets
* cores
* threads
!= ms
->smp
.max_cpus
) {
688 warn_report("Invalid CPU topology deprecated: "
689 "sockets (%u) * cores (%u) * threads (%u) "
691 sockets
, cores
, threads
,
696 ms
->smp
.cores
= cores
;
697 ms
->smp
.threads
= threads
;
700 if (ms
->smp
.cpus
> 1) {
701 Error
*blocker
= NULL
;
702 error_setg(&blocker
, QERR_REPLAY_NOT_SUPPORTED
, "smp");
703 replay_add_blocker(blocker
);
707 static void machine_class_init(ObjectClass
*oc
, void *data
)
709 MachineClass
*mc
= MACHINE_CLASS(oc
);
711 /* Default 128 MB as guest ram size */
712 mc
->default_ram_size
= 128 * MiB
;
713 mc
->rom_file_has_mr
= true;
714 mc
->smp_parse
= smp_parse
;
716 /* numa node memory size aligned on 8MB by default.
717 * On Linux, each node's border has to be 8MB aligned
719 mc
->numa_mem_align_shift
= 23;
720 mc
->numa_auto_assign_ram
= numa_default_auto_assign_ram
;
722 object_class_property_add_str(oc
, "kernel",
723 machine_get_kernel
, machine_set_kernel
, &error_abort
);
724 object_class_property_set_description(oc
, "kernel",
725 "Linux kernel image file", &error_abort
);
727 object_class_property_add_str(oc
, "initrd",
728 machine_get_initrd
, machine_set_initrd
, &error_abort
);
729 object_class_property_set_description(oc
, "initrd",
730 "Linux initial ramdisk file", &error_abort
);
732 object_class_property_add_str(oc
, "append",
733 machine_get_append
, machine_set_append
, &error_abort
);
734 object_class_property_set_description(oc
, "append",
735 "Linux kernel command line", &error_abort
);
737 object_class_property_add_str(oc
, "dtb",
738 machine_get_dtb
, machine_set_dtb
, &error_abort
);
739 object_class_property_set_description(oc
, "dtb",
740 "Linux kernel device tree file", &error_abort
);
742 object_class_property_add_str(oc
, "dumpdtb",
743 machine_get_dumpdtb
, machine_set_dumpdtb
, &error_abort
);
744 object_class_property_set_description(oc
, "dumpdtb",
745 "Dump current dtb to a file and quit", &error_abort
);
747 object_class_property_add(oc
, "phandle-start", "int",
748 machine_get_phandle_start
, machine_set_phandle_start
,
749 NULL
, NULL
, &error_abort
);
750 object_class_property_set_description(oc
, "phandle-start",
751 "The first phandle ID we may generate dynamically", &error_abort
);
753 object_class_property_add_str(oc
, "dt-compatible",
754 machine_get_dt_compatible
, machine_set_dt_compatible
, &error_abort
);
755 object_class_property_set_description(oc
, "dt-compatible",
756 "Overrides the \"compatible\" property of the dt root node",
759 object_class_property_add_bool(oc
, "dump-guest-core",
760 machine_get_dump_guest_core
, machine_set_dump_guest_core
, &error_abort
);
761 object_class_property_set_description(oc
, "dump-guest-core",
762 "Include guest memory in a core dump", &error_abort
);
764 object_class_property_add_bool(oc
, "mem-merge",
765 machine_get_mem_merge
, machine_set_mem_merge
, &error_abort
);
766 object_class_property_set_description(oc
, "mem-merge",
767 "Enable/disable memory merge support", &error_abort
);
769 object_class_property_add_bool(oc
, "usb",
770 machine_get_usb
, machine_set_usb
, &error_abort
);
771 object_class_property_set_description(oc
, "usb",
772 "Set on/off to enable/disable usb", &error_abort
);
774 object_class_property_add_bool(oc
, "graphics",
775 machine_get_graphics
, machine_set_graphics
, &error_abort
);
776 object_class_property_set_description(oc
, "graphics",
777 "Set on/off to enable/disable graphics emulation", &error_abort
);
779 object_class_property_add_str(oc
, "firmware",
780 machine_get_firmware
, machine_set_firmware
,
782 object_class_property_set_description(oc
, "firmware",
783 "Firmware image", &error_abort
);
785 object_class_property_add_bool(oc
, "suppress-vmdesc",
786 machine_get_suppress_vmdesc
, machine_set_suppress_vmdesc
,
788 object_class_property_set_description(oc
, "suppress-vmdesc",
789 "Set on to disable self-describing migration", &error_abort
);
791 object_class_property_add_bool(oc
, "enforce-config-section",
792 machine_get_enforce_config_section
, machine_set_enforce_config_section
,
794 object_class_property_set_description(oc
, "enforce-config-section",
795 "Set on to enforce configuration section migration", &error_abort
);
797 object_class_property_add_str(oc
, "memory-encryption",
798 machine_get_memory_encryption
, machine_set_memory_encryption
,
800 object_class_property_set_description(oc
, "memory-encryption",
801 "Set memory encryption object to use", &error_abort
);
804 static void machine_class_base_init(ObjectClass
*oc
, void *data
)
806 if (!object_class_is_abstract(oc
)) {
807 MachineClass
*mc
= MACHINE_CLASS(oc
);
808 const char *cname
= object_class_get_name(oc
);
809 assert(g_str_has_suffix(cname
, TYPE_MACHINE_SUFFIX
));
810 mc
->name
= g_strndup(cname
,
811 strlen(cname
) - strlen(TYPE_MACHINE_SUFFIX
));
812 mc
->compat_props
= g_ptr_array_new();
816 static void machine_initfn(Object
*obj
)
818 MachineState
*ms
= MACHINE(obj
);
819 MachineClass
*mc
= MACHINE_GET_CLASS(obj
);
821 ms
->dump_guest_core
= true;
822 ms
->mem_merge
= true;
823 ms
->enable_graphics
= true;
825 if (mc
->nvdimm_supported
) {
826 Object
*obj
= OBJECT(ms
);
828 ms
->nvdimms_state
= g_new0(NVDIMMState
, 1);
829 object_property_add_bool(obj
, "nvdimm",
830 machine_get_nvdimm
, machine_set_nvdimm
,
832 object_property_set_description(obj
, "nvdimm",
833 "Set on/off to enable/disable "
834 "NVDIMM instantiation", NULL
);
836 object_property_add_str(obj
, "nvdimm-persistence",
837 machine_get_nvdimm_persistence
,
838 machine_set_nvdimm_persistence
,
840 object_property_set_description(obj
, "nvdimm-persistence",
841 "Set NVDIMM persistence"
842 "Valid values are cpu, mem-ctrl",
846 if (mc
->cpu_index_to_instance_props
&& mc
->get_default_cpu_node_id
) {
847 ms
->numa_state
= g_new0(NumaState
, 1);
850 /* Register notifier when init is done for sysbus sanity checks */
851 ms
->sysbus_notifier
.notify
= machine_init_notify
;
852 qemu_add_machine_init_done_notifier(&ms
->sysbus_notifier
);
855 static void machine_finalize(Object
*obj
)
857 MachineState
*ms
= MACHINE(obj
);
859 g_free(ms
->kernel_filename
);
860 g_free(ms
->initrd_filename
);
861 g_free(ms
->kernel_cmdline
);
864 g_free(ms
->dt_compatible
);
865 g_free(ms
->firmware
);
866 g_free(ms
->device_memory
);
867 g_free(ms
->nvdimms_state
);
868 g_free(ms
->numa_state
);
871 bool machine_usb(MachineState
*machine
)
876 int machine_phandle_start(MachineState
*machine
)
878 return machine
->phandle_start
;
881 bool machine_dump_guest_core(MachineState
*machine
)
883 return machine
->dump_guest_core
;
886 bool machine_mem_merge(MachineState
*machine
)
888 return machine
->mem_merge
;
891 static char *cpu_slot_to_string(const CPUArchId
*cpu
)
893 GString
*s
= g_string_new(NULL
);
894 if (cpu
->props
.has_socket_id
) {
895 g_string_append_printf(s
, "socket-id: %"PRId64
, cpu
->props
.socket_id
);
897 if (cpu
->props
.has_die_id
) {
898 g_string_append_printf(s
, "die-id: %"PRId64
, cpu
->props
.die_id
);
900 if (cpu
->props
.has_core_id
) {
902 g_string_append_printf(s
, ", ");
904 g_string_append_printf(s
, "core-id: %"PRId64
, cpu
->props
.core_id
);
906 if (cpu
->props
.has_thread_id
) {
908 g_string_append_printf(s
, ", ");
910 g_string_append_printf(s
, "thread-id: %"PRId64
, cpu
->props
.thread_id
);
912 return g_string_free(s
, false);
915 static void machine_numa_finish_cpu_init(MachineState
*machine
)
918 bool default_mapping
;
919 GString
*s
= g_string_new(NULL
);
920 MachineClass
*mc
= MACHINE_GET_CLASS(machine
);
921 const CPUArchIdList
*possible_cpus
= mc
->possible_cpu_arch_ids(machine
);
923 assert(machine
->numa_state
->num_nodes
);
924 for (i
= 0; i
< possible_cpus
->len
; i
++) {
925 if (possible_cpus
->cpus
[i
].props
.has_node_id
) {
929 default_mapping
= (i
== possible_cpus
->len
);
931 for (i
= 0; i
< possible_cpus
->len
; i
++) {
932 const CPUArchId
*cpu_slot
= &possible_cpus
->cpus
[i
];
934 if (!cpu_slot
->props
.has_node_id
) {
935 /* fetch default mapping from board and enable it */
936 CpuInstanceProperties props
= cpu_slot
->props
;
938 props
.node_id
= mc
->get_default_cpu_node_id(machine
, i
);
939 if (!default_mapping
) {
940 /* record slots with not set mapping,
941 * TODO: make it hard error in future */
942 char *cpu_str
= cpu_slot_to_string(cpu_slot
);
943 g_string_append_printf(s
, "%sCPU %d [%s]",
944 s
->len
? ", " : "", i
, cpu_str
);
947 /* non mapped cpus used to fallback to node 0 */
951 props
.has_node_id
= true;
952 machine_set_cpu_numa_node(machine
, &props
, &error_fatal
);
955 if (s
->len
&& !qtest_enabled()) {
956 warn_report("CPU(s) not present in any NUMA nodes: %s",
958 warn_report("All CPU(s) up to maxcpus should be described "
959 "in NUMA config, ability to start up with partial NUMA "
960 "mappings is obsoleted and will be removed in future");
962 g_string_free(s
, true);
965 void machine_run_board_init(MachineState
*machine
)
967 MachineClass
*machine_class
= MACHINE_GET_CLASS(machine
);
969 if (machine
->numa_state
) {
970 numa_complete_configuration(machine
);
971 if (machine
->numa_state
->num_nodes
) {
972 machine_numa_finish_cpu_init(machine
);
976 /* If the machine supports the valid_cpu_types check and the user
977 * specified a CPU with -cpu check here that the user CPU is supported.
979 if (machine_class
->valid_cpu_types
&& machine
->cpu_type
) {
980 ObjectClass
*class = object_class_by_name(machine
->cpu_type
);
983 for (i
= 0; machine_class
->valid_cpu_types
[i
]; i
++) {
984 if (object_class_dynamic_cast(class,
985 machine_class
->valid_cpu_types
[i
])) {
986 /* The user specificed CPU is in the valid field, we are
993 if (!machine_class
->valid_cpu_types
[i
]) {
994 /* The user specified CPU is not valid */
995 error_report("Invalid CPU type: %s", machine
->cpu_type
);
996 error_printf("The valid types are: %s",
997 machine_class
->valid_cpu_types
[0]);
998 for (i
= 1; machine_class
->valid_cpu_types
[i
]; i
++) {
999 error_printf(", %s", machine_class
->valid_cpu_types
[i
]);
1007 machine_class
->init(machine
);
1010 static const TypeInfo machine_info
= {
1011 .name
= TYPE_MACHINE
,
1012 .parent
= TYPE_OBJECT
,
1014 .class_size
= sizeof(MachineClass
),
1015 .class_init
= machine_class_init
,
1016 .class_base_init
= machine_class_base_init
,
1017 .instance_size
= sizeof(MachineState
),
1018 .instance_init
= machine_initfn
,
1019 .instance_finalize
= machine_finalize
,
1022 static void machine_register_types(void)
1024 type_register_static(&machine_info
);
1027 type_init(machine_register_types
)