sphinx/qapidoc: Drop code to generate doc for simple union tag
[qemu/kevin.git] / hw / core / machine.c
blobfb5afdcae4cc1f9a289d427e161b1917df11a389
1 /*
2 * QEMU Machine
4 * Copyright (C) 2014 Red Hat Inc
6 * Authors:
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/accel.h"
15 #include "sysemu/replay.h"
16 #include "hw/boards.h"
17 #include "hw/loader.h"
18 #include "qapi/error.h"
19 #include "qapi/qapi-visit-machine.h"
20 #include "qom/object_interfaces.h"
21 #include "sysemu/cpus.h"
22 #include "sysemu/sysemu.h"
23 #include "sysemu/reset.h"
24 #include "sysemu/runstate.h"
25 #include "sysemu/xen.h"
26 #include "sysemu/qtest.h"
27 #include "hw/pci/pci_bridge.h"
28 #include "hw/mem/nvdimm.h"
29 #include "migration/global_state.h"
30 #include "exec/confidential-guest-support.h"
31 #include "hw/virtio/virtio-pci.h"
32 #include "hw/virtio/virtio-net.h"
33 #include "audio/audio.h"
35 GlobalProperty hw_compat_8_2[] = {};
36 const size_t hw_compat_8_2_len = G_N_ELEMENTS(hw_compat_8_2);
38 GlobalProperty hw_compat_8_1[] = {
39 { TYPE_PCI_BRIDGE, "x-pci-express-writeable-slt-bug", "true" },
40 { "ramfb", "x-migrate", "off" },
41 { "vfio-pci-nohotplug", "x-ramfb-migrate", "off" },
42 { "igb", "x-pcie-flr-init", "off" },
44 const size_t hw_compat_8_1_len = G_N_ELEMENTS(hw_compat_8_1);
46 GlobalProperty hw_compat_8_0[] = {
47 { "migration", "multifd-flush-after-each-section", "on"},
48 { TYPE_PCI_DEVICE, "x-pcie-ari-nextfn-1", "on" },
49 { TYPE_VIRTIO_NET, "host_uso", "off"},
50 { TYPE_VIRTIO_NET, "guest_uso4", "off"},
51 { TYPE_VIRTIO_NET, "guest_uso6", "off"},
53 const size_t hw_compat_8_0_len = G_N_ELEMENTS(hw_compat_8_0);
55 GlobalProperty hw_compat_7_2[] = {
56 { "e1000e", "migrate-timadj", "off" },
57 { "virtio-mem", "x-early-migration", "false" },
58 { "migration", "x-preempt-pre-7-2", "true" },
59 { TYPE_PCI_DEVICE, "x-pcie-err-unc-mask", "off" },
61 const size_t hw_compat_7_2_len = G_N_ELEMENTS(hw_compat_7_2);
63 GlobalProperty hw_compat_7_1[] = {
64 { "virtio-device", "queue_reset", "false" },
65 { "virtio-rng-pci", "vectors", "0" },
66 { "virtio-rng-pci-transitional", "vectors", "0" },
67 { "virtio-rng-pci-non-transitional", "vectors", "0" },
69 const size_t hw_compat_7_1_len = G_N_ELEMENTS(hw_compat_7_1);
71 GlobalProperty hw_compat_7_0[] = {
72 { "arm-gicv3-common", "force-8-bit-prio", "on" },
73 { "nvme-ns", "eui64-default", "on"},
75 const size_t hw_compat_7_0_len = G_N_ELEMENTS(hw_compat_7_0);
77 GlobalProperty hw_compat_6_2[] = {
78 { "PIIX4_PM", "x-not-migrate-acpi-index", "on"},
80 const size_t hw_compat_6_2_len = G_N_ELEMENTS(hw_compat_6_2);
82 GlobalProperty hw_compat_6_1[] = {
83 { "vhost-user-vsock-device", "seqpacket", "off" },
84 { "nvme-ns", "shared", "off" },
86 const size_t hw_compat_6_1_len = G_N_ELEMENTS(hw_compat_6_1);
88 GlobalProperty hw_compat_6_0[] = {
89 { "gpex-pcihost", "allow-unmapped-accesses", "false" },
90 { "i8042", "extended-state", "false"},
91 { "nvme-ns", "eui64-default", "off"},
92 { "e1000", "init-vet", "off" },
93 { "e1000e", "init-vet", "off" },
94 { "vhost-vsock-device", "seqpacket", "off" },
96 const size_t hw_compat_6_0_len = G_N_ELEMENTS(hw_compat_6_0);
98 GlobalProperty hw_compat_5_2[] = {
99 { "ICH9-LPC", "smm-compat", "on"},
100 { "PIIX4_PM", "smm-compat", "on"},
101 { "virtio-blk-device", "report-discard-granularity", "off" },
102 { "virtio-net-pci-base", "vectors", "3"},
104 const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
106 GlobalProperty hw_compat_5_1[] = {
107 { "vhost-scsi", "num_queues", "1"},
108 { "vhost-user-blk", "num-queues", "1"},
109 { "vhost-user-scsi", "num_queues", "1"},
110 { "virtio-blk-device", "num-queues", "1"},
111 { "virtio-scsi-device", "num_queues", "1"},
112 { "nvme", "use-intel-id", "on"},
113 { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
114 { "pl011", "migrate-clk", "off" },
115 { "virtio-pci", "x-ats-page-aligned", "off"},
117 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
119 GlobalProperty hw_compat_5_0[] = {
120 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
121 { "virtio-balloon-device", "page-poison", "false" },
122 { "vmport", "x-read-set-eax", "off" },
123 { "vmport", "x-signal-unsupported-cmd", "off" },
124 { "vmport", "x-report-vmx-type", "off" },
125 { "vmport", "x-cmds-v2", "off" },
126 { "virtio-device", "x-disable-legacy-check", "true" },
128 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
130 GlobalProperty hw_compat_4_2[] = {
131 { "virtio-blk-device", "queue-size", "128"},
132 { "virtio-scsi-device", "virtqueue_size", "128"},
133 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
134 { "virtio-blk-device", "seg-max-adjust", "off"},
135 { "virtio-scsi-device", "seg_max_adjust", "off"},
136 { "vhost-blk-device", "seg_max_adjust", "off"},
137 { "usb-host", "suppress-remote-wake", "off" },
138 { "usb-redir", "suppress-remote-wake", "off" },
139 { "qxl", "revision", "4" },
140 { "qxl-vga", "revision", "4" },
141 { "fw_cfg", "acpi-mr-restore", "false" },
142 { "virtio-device", "use-disabled-flag", "false" },
144 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
146 GlobalProperty hw_compat_4_1[] = {
147 { "virtio-pci", "x-pcie-flr-init", "off" },
149 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
151 GlobalProperty hw_compat_4_0[] = {
152 { "VGA", "edid", "false" },
153 { "secondary-vga", "edid", "false" },
154 { "bochs-display", "edid", "false" },
155 { "virtio-vga", "edid", "false" },
156 { "virtio-gpu-device", "edid", "false" },
157 { "virtio-device", "use-started", "false" },
158 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
159 { "pl031", "migrate-tick-offset", "false" },
161 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
163 GlobalProperty hw_compat_3_1[] = {
164 { "pcie-root-port", "x-speed", "2_5" },
165 { "pcie-root-port", "x-width", "1" },
166 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
167 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
168 { "tpm-crb", "ppi", "false" },
169 { "tpm-tis", "ppi", "false" },
170 { "usb-kbd", "serial", "42" },
171 { "usb-mouse", "serial", "42" },
172 { "usb-tablet", "serial", "42" },
173 { "virtio-blk-device", "discard", "false" },
174 { "virtio-blk-device", "write-zeroes", "false" },
175 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
176 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
178 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
180 GlobalProperty hw_compat_3_0[] = {};
181 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
183 GlobalProperty hw_compat_2_12[] = {
184 { "migration", "decompress-error-check", "off" },
185 { "hda-audio", "use-timer", "false" },
186 { "cirrus-vga", "global-vmstate", "true" },
187 { "VGA", "global-vmstate", "true" },
188 { "vmware-svga", "global-vmstate", "true" },
189 { "qxl-vga", "global-vmstate", "true" },
191 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
193 GlobalProperty hw_compat_2_11[] = {
194 { "hpet", "hpet-offset-saved", "false" },
195 { "virtio-blk-pci", "vectors", "2" },
196 { "vhost-user-blk-pci", "vectors", "2" },
197 { "e1000", "migrate_tso_props", "off" },
199 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
201 GlobalProperty hw_compat_2_10[] = {
202 { "virtio-mouse-device", "wheel-axis", "false" },
203 { "virtio-tablet-device", "wheel-axis", "false" },
205 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
207 GlobalProperty hw_compat_2_9[] = {
208 { "pci-bridge", "shpc", "off" },
209 { "intel-iommu", "pt", "off" },
210 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
211 { "pcie-root-port", "x-migrate-msix", "false" },
213 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
215 GlobalProperty hw_compat_2_8[] = {
216 { "fw_cfg_mem", "x-file-slots", "0x10" },
217 { "fw_cfg_io", "x-file-slots", "0x10" },
218 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
219 { "pci-bridge", "shpc", "on" },
220 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
221 { "virtio-pci", "x-pcie-deverr-init", "off" },
222 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
223 { "virtio-pci", "x-pcie-pm-init", "off" },
224 { "cirrus-vga", "vgamem_mb", "8" },
225 { "isa-cirrus-vga", "vgamem_mb", "8" },
227 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
229 GlobalProperty hw_compat_2_7[] = {
230 { "virtio-pci", "page-per-vq", "on" },
231 { "virtio-serial-device", "emergency-write", "off" },
232 { "ioapic", "version", "0x11" },
233 { "intel-iommu", "x-buggy-eim", "true" },
234 { "virtio-pci", "x-ignore-backend-features", "on" },
236 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
238 GlobalProperty hw_compat_2_6[] = {
239 { "virtio-mmio", "format_transport_address", "off" },
240 /* Optional because not all virtio-pci devices support legacy mode */
241 { "virtio-pci", "disable-modern", "on", .optional = true },
242 { "virtio-pci", "disable-legacy", "off", .optional = true },
244 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
246 GlobalProperty hw_compat_2_5[] = {
247 { "isa-fdc", "fallback", "144" },
248 { "pvscsi", "x-old-pci-configuration", "on" },
249 { "pvscsi", "x-disable-pcie", "on" },
250 { "vmxnet3", "x-old-msi-offsets", "on" },
251 { "vmxnet3", "x-disable-pcie", "on" },
253 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
255 GlobalProperty hw_compat_2_4[] = {
256 /* Optional because the 'scsi' property is Linux-only */
257 { "virtio-blk-device", "scsi", "true", .optional = true },
258 { "e1000", "extra_mac_registers", "off" },
259 { "virtio-pci", "x-disable-pcie", "on" },
260 { "virtio-pci", "migrate-extra", "off" },
261 { "fw_cfg_mem", "dma_enabled", "off" },
262 { "fw_cfg_io", "dma_enabled", "off" }
264 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
266 GlobalProperty hw_compat_2_3[] = {
267 { "virtio-blk-pci", "any_layout", "off" },
268 { "virtio-balloon-pci", "any_layout", "off" },
269 { "virtio-serial-pci", "any_layout", "off" },
270 { "virtio-9p-pci", "any_layout", "off" },
271 { "virtio-rng-pci", "any_layout", "off" },
272 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
273 { "migration", "send-configuration", "off" },
274 { "migration", "send-section-footer", "off" },
275 { "migration", "store-global-state", "off" },
277 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
279 GlobalProperty hw_compat_2_2[] = {};
280 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
282 GlobalProperty hw_compat_2_1[] = {
283 { "intel-hda", "old_msi_addr", "on" },
284 { "VGA", "qemu-extended-regs", "off" },
285 { "secondary-vga", "qemu-extended-regs", "off" },
286 { "virtio-scsi-pci", "any_layout", "off" },
287 { "usb-mouse", "usb_version", "1" },
288 { "usb-kbd", "usb_version", "1" },
289 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
291 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
293 MachineState *current_machine;
295 static char *machine_get_kernel(Object *obj, Error **errp)
297 MachineState *ms = MACHINE(obj);
299 return g_strdup(ms->kernel_filename);
302 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
304 MachineState *ms = MACHINE(obj);
306 g_free(ms->kernel_filename);
307 ms->kernel_filename = g_strdup(value);
310 static char *machine_get_initrd(Object *obj, Error **errp)
312 MachineState *ms = MACHINE(obj);
314 return g_strdup(ms->initrd_filename);
317 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
319 MachineState *ms = MACHINE(obj);
321 g_free(ms->initrd_filename);
322 ms->initrd_filename = g_strdup(value);
325 static char *machine_get_append(Object *obj, Error **errp)
327 MachineState *ms = MACHINE(obj);
329 return g_strdup(ms->kernel_cmdline);
332 static void machine_set_append(Object *obj, const char *value, Error **errp)
334 MachineState *ms = MACHINE(obj);
336 g_free(ms->kernel_cmdline);
337 ms->kernel_cmdline = g_strdup(value);
340 static char *machine_get_dtb(Object *obj, Error **errp)
342 MachineState *ms = MACHINE(obj);
344 return g_strdup(ms->dtb);
347 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
349 MachineState *ms = MACHINE(obj);
351 g_free(ms->dtb);
352 ms->dtb = g_strdup(value);
355 static char *machine_get_dumpdtb(Object *obj, Error **errp)
357 MachineState *ms = MACHINE(obj);
359 return g_strdup(ms->dumpdtb);
362 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
364 MachineState *ms = MACHINE(obj);
366 g_free(ms->dumpdtb);
367 ms->dumpdtb = g_strdup(value);
370 static void machine_get_phandle_start(Object *obj, Visitor *v,
371 const char *name, void *opaque,
372 Error **errp)
374 MachineState *ms = MACHINE(obj);
375 int64_t value = ms->phandle_start;
377 visit_type_int(v, name, &value, errp);
380 static void machine_set_phandle_start(Object *obj, Visitor *v,
381 const char *name, void *opaque,
382 Error **errp)
384 MachineState *ms = MACHINE(obj);
385 int64_t value;
387 if (!visit_type_int(v, name, &value, errp)) {
388 return;
391 ms->phandle_start = value;
394 static char *machine_get_dt_compatible(Object *obj, Error **errp)
396 MachineState *ms = MACHINE(obj);
398 return g_strdup(ms->dt_compatible);
401 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
403 MachineState *ms = MACHINE(obj);
405 g_free(ms->dt_compatible);
406 ms->dt_compatible = g_strdup(value);
409 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
411 MachineState *ms = MACHINE(obj);
413 return ms->dump_guest_core;
416 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
418 MachineState *ms = MACHINE(obj);
420 ms->dump_guest_core = value;
423 static bool machine_get_mem_merge(Object *obj, Error **errp)
425 MachineState *ms = MACHINE(obj);
427 return ms->mem_merge;
430 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
432 MachineState *ms = MACHINE(obj);
434 ms->mem_merge = value;
437 static bool machine_get_usb(Object *obj, Error **errp)
439 MachineState *ms = MACHINE(obj);
441 return ms->usb;
444 static void machine_set_usb(Object *obj, bool value, Error **errp)
446 MachineState *ms = MACHINE(obj);
448 ms->usb = value;
449 ms->usb_disabled = !value;
452 static bool machine_get_graphics(Object *obj, Error **errp)
454 MachineState *ms = MACHINE(obj);
456 return ms->enable_graphics;
459 static void machine_set_graphics(Object *obj, bool value, Error **errp)
461 MachineState *ms = MACHINE(obj);
463 ms->enable_graphics = value;
466 static char *machine_get_firmware(Object *obj, Error **errp)
468 MachineState *ms = MACHINE(obj);
470 return g_strdup(ms->firmware);
473 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
475 MachineState *ms = MACHINE(obj);
477 g_free(ms->firmware);
478 ms->firmware = g_strdup(value);
481 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
483 MachineState *ms = MACHINE(obj);
485 ms->suppress_vmdesc = value;
488 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
490 MachineState *ms = MACHINE(obj);
492 return ms->suppress_vmdesc;
495 static char *machine_get_memory_encryption(Object *obj, Error **errp)
497 MachineState *ms = MACHINE(obj);
499 if (ms->cgs) {
500 return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
503 return NULL;
506 static void machine_set_memory_encryption(Object *obj, const char *value,
507 Error **errp)
509 Object *cgs =
510 object_resolve_path_component(object_get_objects_root(), value);
512 if (!cgs) {
513 error_setg(errp, "No such memory encryption object '%s'", value);
514 return;
517 object_property_set_link(obj, "confidential-guest-support", cgs, errp);
520 static void machine_check_confidential_guest_support(const Object *obj,
521 const char *name,
522 Object *new_target,
523 Error **errp)
526 * So far the only constraint is that the target has the
527 * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
528 * by the QOM core
532 static bool machine_get_nvdimm(Object *obj, Error **errp)
534 MachineState *ms = MACHINE(obj);
536 return ms->nvdimms_state->is_enabled;
539 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
541 MachineState *ms = MACHINE(obj);
543 ms->nvdimms_state->is_enabled = value;
546 static bool machine_get_hmat(Object *obj, Error **errp)
548 MachineState *ms = MACHINE(obj);
550 return ms->numa_state->hmat_enabled;
553 static void machine_set_hmat(Object *obj, bool value, Error **errp)
555 MachineState *ms = MACHINE(obj);
557 ms->numa_state->hmat_enabled = value;
560 static void machine_get_mem(Object *obj, Visitor *v, const char *name,
561 void *opaque, Error **errp)
563 MachineState *ms = MACHINE(obj);
564 MemorySizeConfiguration mem = {
565 .has_size = true,
566 .size = ms->ram_size,
567 .has_max_size = !!ms->ram_slots,
568 .max_size = ms->maxram_size,
569 .has_slots = !!ms->ram_slots,
570 .slots = ms->ram_slots,
572 MemorySizeConfiguration *p_mem = &mem;
574 visit_type_MemorySizeConfiguration(v, name, &p_mem, &error_abort);
577 static void machine_set_mem(Object *obj, Visitor *v, const char *name,
578 void *opaque, Error **errp)
580 ERRP_GUARD();
581 MachineState *ms = MACHINE(obj);
582 MachineClass *mc = MACHINE_GET_CLASS(obj);
583 MemorySizeConfiguration *mem;
585 if (!visit_type_MemorySizeConfiguration(v, name, &mem, errp)) {
586 return;
589 if (!mem->has_size) {
590 mem->has_size = true;
591 mem->size = mc->default_ram_size;
593 mem->size = QEMU_ALIGN_UP(mem->size, 8192);
594 if (mc->fixup_ram_size) {
595 mem->size = mc->fixup_ram_size(mem->size);
597 if ((ram_addr_t)mem->size != mem->size) {
598 error_setg(errp, "ram size too large");
599 goto out_free;
602 if (mem->has_max_size) {
603 if (mem->max_size < mem->size) {
604 error_setg(errp, "invalid value of maxmem: "
605 "maximum memory size (0x%" PRIx64 ") must be at least "
606 "the initial memory size (0x%" PRIx64 ")",
607 mem->max_size, mem->size);
608 goto out_free;
610 if (mem->has_slots && mem->slots && mem->max_size == mem->size) {
611 error_setg(errp, "invalid value of maxmem: "
612 "memory slots were specified but maximum memory size "
613 "(0x%" PRIx64 ") is equal to the initial memory size "
614 "(0x%" PRIx64 ")", mem->max_size, mem->size);
615 goto out_free;
617 ms->maxram_size = mem->max_size;
618 } else {
619 if (mem->has_slots) {
620 error_setg(errp, "slots specified but no max-size");
621 goto out_free;
623 ms->maxram_size = mem->size;
625 ms->ram_size = mem->size;
626 ms->ram_slots = mem->has_slots ? mem->slots : 0;
627 out_free:
628 qapi_free_MemorySizeConfiguration(mem);
631 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
633 MachineState *ms = MACHINE(obj);
635 return g_strdup(ms->nvdimms_state->persistence_string);
638 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
639 Error **errp)
641 MachineState *ms = MACHINE(obj);
642 NVDIMMState *nvdimms_state = ms->nvdimms_state;
644 if (strcmp(value, "cpu") == 0) {
645 nvdimms_state->persistence = 3;
646 } else if (strcmp(value, "mem-ctrl") == 0) {
647 nvdimms_state->persistence = 2;
648 } else {
649 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
650 value);
651 return;
654 g_free(nvdimms_state->persistence_string);
655 nvdimms_state->persistence_string = g_strdup(value);
658 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
660 QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
663 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
665 Object *obj = OBJECT(dev);
667 if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
668 return false;
671 return device_type_is_dynamic_sysbus(mc, object_get_typename(obj));
674 bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type)
676 bool allowed = false;
677 strList *wl;
678 ObjectClass *klass = object_class_by_name(type);
680 for (wl = mc->allowed_dynamic_sysbus_devices;
681 !allowed && wl;
682 wl = wl->next) {
683 allowed |= !!object_class_dynamic_cast(klass, wl->value);
686 return allowed;
689 static char *machine_get_audiodev(Object *obj, Error **errp)
691 MachineState *ms = MACHINE(obj);
693 return g_strdup(ms->audiodev);
696 static void machine_set_audiodev(Object *obj, const char *value,
697 Error **errp)
699 MachineState *ms = MACHINE(obj);
701 if (!audio_state_by_name(value, errp)) {
702 return;
705 g_free(ms->audiodev);
706 ms->audiodev = g_strdup(value);
709 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
711 int i;
712 HotpluggableCPUList *head = NULL;
713 MachineClass *mc = MACHINE_GET_CLASS(machine);
715 /* force board to initialize possible_cpus if it hasn't been done yet */
716 mc->possible_cpu_arch_ids(machine);
718 for (i = 0; i < machine->possible_cpus->len; i++) {
719 Object *cpu;
720 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
722 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
723 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
724 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
725 sizeof(*cpu_item->props));
727 cpu = machine->possible_cpus->cpus[i].cpu;
728 if (cpu) {
729 cpu_item->qom_path = object_get_canonical_path(cpu);
731 QAPI_LIST_PREPEND(head, cpu_item);
733 return head;
737 * machine_set_cpu_numa_node:
738 * @machine: machine object to modify
739 * @props: specifies which cpu objects to assign to
740 * numa node specified by @props.node_id
741 * @errp: if an error occurs, a pointer to an area to store the error
743 * Associate NUMA node specified by @props.node_id with cpu slots that
744 * match socket/core/thread-ids specified by @props. It's recommended to use
745 * query-hotpluggable-cpus.props values to specify affected cpu slots,
746 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
748 * However for CLI convenience it's possible to pass in subset of properties,
749 * which would affect all cpu slots that match it.
750 * Ex for pc machine:
751 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
752 * -numa cpu,node-id=0,socket_id=0 \
753 * -numa cpu,node-id=1,socket_id=1
754 * will assign all child cores of socket 0 to node 0 and
755 * of socket 1 to node 1.
757 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
758 * return error.
759 * Empty subset is disallowed and function will return with error in this case.
761 void machine_set_cpu_numa_node(MachineState *machine,
762 const CpuInstanceProperties *props, Error **errp)
764 MachineClass *mc = MACHINE_GET_CLASS(machine);
765 NodeInfo *numa_info = machine->numa_state->nodes;
766 bool match = false;
767 int i;
769 if (!mc->possible_cpu_arch_ids) {
770 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
771 return;
774 /* disabling node mapping is not supported, forbid it */
775 assert(props->has_node_id);
777 /* force board to initialize possible_cpus if it hasn't been done yet */
778 mc->possible_cpu_arch_ids(machine);
780 for (i = 0; i < machine->possible_cpus->len; i++) {
781 CPUArchId *slot = &machine->possible_cpus->cpus[i];
783 /* reject unsupported by board properties */
784 if (props->has_thread_id && !slot->props.has_thread_id) {
785 error_setg(errp, "thread-id is not supported");
786 return;
789 if (props->has_core_id && !slot->props.has_core_id) {
790 error_setg(errp, "core-id is not supported");
791 return;
794 if (props->has_cluster_id && !slot->props.has_cluster_id) {
795 error_setg(errp, "cluster-id is not supported");
796 return;
799 if (props->has_socket_id && !slot->props.has_socket_id) {
800 error_setg(errp, "socket-id is not supported");
801 return;
804 if (props->has_die_id && !slot->props.has_die_id) {
805 error_setg(errp, "die-id is not supported");
806 return;
809 /* skip slots with explicit mismatch */
810 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
811 continue;
814 if (props->has_core_id && props->core_id != slot->props.core_id) {
815 continue;
818 if (props->has_cluster_id &&
819 props->cluster_id != slot->props.cluster_id) {
820 continue;
823 if (props->has_die_id && props->die_id != slot->props.die_id) {
824 continue;
827 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
828 continue;
831 /* reject assignment if slot is already assigned, for compatibility
832 * of legacy cpu_index mapping with SPAPR core based mapping do not
833 * error out if cpu thread and matched core have the same node-id */
834 if (slot->props.has_node_id &&
835 slot->props.node_id != props->node_id) {
836 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
837 slot->props.node_id);
838 return;
841 /* assign slot to node as it's matched '-numa cpu' key */
842 match = true;
843 slot->props.node_id = props->node_id;
844 slot->props.has_node_id = props->has_node_id;
846 if (machine->numa_state->hmat_enabled) {
847 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
848 (props->node_id != numa_info[props->node_id].initiator)) {
849 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
850 " should be itself (got %" PRIu16 ")",
851 props->node_id, numa_info[props->node_id].initiator);
852 return;
854 numa_info[props->node_id].has_cpu = true;
855 numa_info[props->node_id].initiator = props->node_id;
859 if (!match) {
860 error_setg(errp, "no match found");
864 static void machine_get_smp(Object *obj, Visitor *v, const char *name,
865 void *opaque, Error **errp)
867 MachineState *ms = MACHINE(obj);
868 SMPConfiguration *config = &(SMPConfiguration){
869 .has_cpus = true, .cpus = ms->smp.cpus,
870 .has_drawers = true, .drawers = ms->smp.drawers,
871 .has_books = true, .books = ms->smp.books,
872 .has_sockets = true, .sockets = ms->smp.sockets,
873 .has_dies = true, .dies = ms->smp.dies,
874 .has_clusters = true, .clusters = ms->smp.clusters,
875 .has_cores = true, .cores = ms->smp.cores,
876 .has_threads = true, .threads = ms->smp.threads,
877 .has_maxcpus = true, .maxcpus = ms->smp.max_cpus,
880 if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) {
881 return;
885 static void machine_set_smp(Object *obj, Visitor *v, const char *name,
886 void *opaque, Error **errp)
888 MachineState *ms = MACHINE(obj);
889 g_autoptr(SMPConfiguration) config = NULL;
891 if (!visit_type_SMPConfiguration(v, name, &config, errp)) {
892 return;
895 machine_parse_smp_config(ms, config, errp);
898 static void machine_get_boot(Object *obj, Visitor *v, const char *name,
899 void *opaque, Error **errp)
901 MachineState *ms = MACHINE(obj);
902 BootConfiguration *config = &ms->boot_config;
903 visit_type_BootConfiguration(v, name, &config, &error_abort);
906 static void machine_free_boot_config(MachineState *ms)
908 g_free(ms->boot_config.order);
909 g_free(ms->boot_config.once);
910 g_free(ms->boot_config.splash);
913 static void machine_copy_boot_config(MachineState *ms, BootConfiguration *config)
915 MachineClass *machine_class = MACHINE_GET_CLASS(ms);
917 machine_free_boot_config(ms);
918 ms->boot_config = *config;
919 if (!config->order) {
920 ms->boot_config.order = g_strdup(machine_class->default_boot_order);
924 static void machine_set_boot(Object *obj, Visitor *v, const char *name,
925 void *opaque, Error **errp)
927 ERRP_GUARD();
928 MachineState *ms = MACHINE(obj);
929 BootConfiguration *config = NULL;
931 if (!visit_type_BootConfiguration(v, name, &config, errp)) {
932 return;
934 if (config->order) {
935 validate_bootdevices(config->order, errp);
936 if (*errp) {
937 goto out_free;
940 if (config->once) {
941 validate_bootdevices(config->once, errp);
942 if (*errp) {
943 goto out_free;
947 machine_copy_boot_config(ms, config);
948 /* Strings live in ms->boot_config. */
949 free(config);
950 return;
952 out_free:
953 qapi_free_BootConfiguration(config);
956 void machine_add_audiodev_property(MachineClass *mc)
958 ObjectClass *oc = OBJECT_CLASS(mc);
960 object_class_property_add_str(oc, "audiodev",
961 machine_get_audiodev,
962 machine_set_audiodev);
963 object_class_property_set_description(oc, "audiodev",
964 "Audiodev to use for default machine devices");
967 static void machine_class_init(ObjectClass *oc, void *data)
969 MachineClass *mc = MACHINE_CLASS(oc);
971 /* Default 128 MB as guest ram size */
972 mc->default_ram_size = 128 * MiB;
973 mc->rom_file_has_mr = true;
975 /* numa node memory size aligned on 8MB by default.
976 * On Linux, each node's border has to be 8MB aligned
978 mc->numa_mem_align_shift = 23;
980 object_class_property_add_str(oc, "kernel",
981 machine_get_kernel, machine_set_kernel);
982 object_class_property_set_description(oc, "kernel",
983 "Linux kernel image file");
985 object_class_property_add_str(oc, "initrd",
986 machine_get_initrd, machine_set_initrd);
987 object_class_property_set_description(oc, "initrd",
988 "Linux initial ramdisk file");
990 object_class_property_add_str(oc, "append",
991 machine_get_append, machine_set_append);
992 object_class_property_set_description(oc, "append",
993 "Linux kernel command line");
995 object_class_property_add_str(oc, "dtb",
996 machine_get_dtb, machine_set_dtb);
997 object_class_property_set_description(oc, "dtb",
998 "Linux kernel device tree file");
1000 object_class_property_add_str(oc, "dumpdtb",
1001 machine_get_dumpdtb, machine_set_dumpdtb);
1002 object_class_property_set_description(oc, "dumpdtb",
1003 "Dump current dtb to a file and quit");
1005 object_class_property_add(oc, "boot", "BootConfiguration",
1006 machine_get_boot, machine_set_boot,
1007 NULL, NULL);
1008 object_class_property_set_description(oc, "boot",
1009 "Boot configuration");
1011 object_class_property_add(oc, "smp", "SMPConfiguration",
1012 machine_get_smp, machine_set_smp,
1013 NULL, NULL);
1014 object_class_property_set_description(oc, "smp",
1015 "CPU topology");
1017 object_class_property_add(oc, "phandle-start", "int",
1018 machine_get_phandle_start, machine_set_phandle_start,
1019 NULL, NULL);
1020 object_class_property_set_description(oc, "phandle-start",
1021 "The first phandle ID we may generate dynamically");
1023 object_class_property_add_str(oc, "dt-compatible",
1024 machine_get_dt_compatible, machine_set_dt_compatible);
1025 object_class_property_set_description(oc, "dt-compatible",
1026 "Overrides the \"compatible\" property of the dt root node");
1028 object_class_property_add_bool(oc, "dump-guest-core",
1029 machine_get_dump_guest_core, machine_set_dump_guest_core);
1030 object_class_property_set_description(oc, "dump-guest-core",
1031 "Include guest memory in a core dump");
1033 object_class_property_add_bool(oc, "mem-merge",
1034 machine_get_mem_merge, machine_set_mem_merge);
1035 object_class_property_set_description(oc, "mem-merge",
1036 "Enable/disable memory merge support");
1038 object_class_property_add_bool(oc, "usb",
1039 machine_get_usb, machine_set_usb);
1040 object_class_property_set_description(oc, "usb",
1041 "Set on/off to enable/disable usb");
1043 object_class_property_add_bool(oc, "graphics",
1044 machine_get_graphics, machine_set_graphics);
1045 object_class_property_set_description(oc, "graphics",
1046 "Set on/off to enable/disable graphics emulation");
1048 object_class_property_add_str(oc, "firmware",
1049 machine_get_firmware, machine_set_firmware);
1050 object_class_property_set_description(oc, "firmware",
1051 "Firmware image");
1053 object_class_property_add_bool(oc, "suppress-vmdesc",
1054 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
1055 object_class_property_set_description(oc, "suppress-vmdesc",
1056 "Set on to disable self-describing migration");
1058 object_class_property_add_link(oc, "confidential-guest-support",
1059 TYPE_CONFIDENTIAL_GUEST_SUPPORT,
1060 offsetof(MachineState, cgs),
1061 machine_check_confidential_guest_support,
1062 OBJ_PROP_LINK_STRONG);
1063 object_class_property_set_description(oc, "confidential-guest-support",
1064 "Set confidential guest scheme to support");
1066 /* For compatibility */
1067 object_class_property_add_str(oc, "memory-encryption",
1068 machine_get_memory_encryption, machine_set_memory_encryption);
1069 object_class_property_set_description(oc, "memory-encryption",
1070 "Set memory encryption object to use");
1072 object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKEND,
1073 offsetof(MachineState, memdev), object_property_allow_set_link,
1074 OBJ_PROP_LINK_STRONG);
1075 object_class_property_set_description(oc, "memory-backend",
1076 "Set RAM backend"
1077 "Valid value is ID of hostmem based backend");
1079 object_class_property_add(oc, "memory", "MemorySizeConfiguration",
1080 machine_get_mem, machine_set_mem,
1081 NULL, NULL);
1082 object_class_property_set_description(oc, "memory",
1083 "Memory size configuration");
1086 static void machine_class_base_init(ObjectClass *oc, void *data)
1088 MachineClass *mc = MACHINE_CLASS(oc);
1089 mc->max_cpus = mc->max_cpus ?: 1;
1090 mc->min_cpus = mc->min_cpus ?: 1;
1091 mc->default_cpus = mc->default_cpus ?: 1;
1093 if (!object_class_is_abstract(oc)) {
1094 const char *cname = object_class_get_name(oc);
1095 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
1096 mc->name = g_strndup(cname,
1097 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
1098 mc->compat_props = g_ptr_array_new();
1102 static void machine_initfn(Object *obj)
1104 MachineState *ms = MACHINE(obj);
1105 MachineClass *mc = MACHINE_GET_CLASS(obj);
1107 container_get(obj, "/peripheral");
1108 container_get(obj, "/peripheral-anon");
1110 ms->dump_guest_core = true;
1111 ms->mem_merge = true;
1112 ms->enable_graphics = true;
1113 ms->kernel_cmdline = g_strdup("");
1114 ms->ram_size = mc->default_ram_size;
1115 ms->maxram_size = mc->default_ram_size;
1117 if (mc->nvdimm_supported) {
1118 ms->nvdimms_state = g_new0(NVDIMMState, 1);
1119 object_property_add_bool(obj, "nvdimm",
1120 machine_get_nvdimm, machine_set_nvdimm);
1121 object_property_set_description(obj, "nvdimm",
1122 "Set on/off to enable/disable "
1123 "NVDIMM instantiation");
1125 object_property_add_str(obj, "nvdimm-persistence",
1126 machine_get_nvdimm_persistence,
1127 machine_set_nvdimm_persistence);
1128 object_property_set_description(obj, "nvdimm-persistence",
1129 "Set NVDIMM persistence"
1130 "Valid values are cpu, mem-ctrl");
1133 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
1134 ms->numa_state = g_new0(NumaState, 1);
1135 object_property_add_bool(obj, "hmat",
1136 machine_get_hmat, machine_set_hmat);
1137 object_property_set_description(obj, "hmat",
1138 "Set on/off to enable/disable "
1139 "ACPI Heterogeneous Memory Attribute "
1140 "Table (HMAT)");
1143 /* default to mc->default_cpus */
1144 ms->smp.cpus = mc->default_cpus;
1145 ms->smp.max_cpus = mc->default_cpus;
1146 ms->smp.drawers = 1;
1147 ms->smp.books = 1;
1148 ms->smp.sockets = 1;
1149 ms->smp.dies = 1;
1150 ms->smp.clusters = 1;
1151 ms->smp.cores = 1;
1152 ms->smp.threads = 1;
1154 machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
1157 static void machine_finalize(Object *obj)
1159 MachineState *ms = MACHINE(obj);
1161 machine_free_boot_config(ms);
1162 g_free(ms->kernel_filename);
1163 g_free(ms->initrd_filename);
1164 g_free(ms->kernel_cmdline);
1165 g_free(ms->dtb);
1166 g_free(ms->dumpdtb);
1167 g_free(ms->dt_compatible);
1168 g_free(ms->firmware);
1169 g_free(ms->device_memory);
1170 g_free(ms->nvdimms_state);
1171 g_free(ms->numa_state);
1172 g_free(ms->audiodev);
1175 bool machine_usb(MachineState *machine)
1177 return machine->usb;
1180 int machine_phandle_start(MachineState *machine)
1182 return machine->phandle_start;
1185 bool machine_dump_guest_core(MachineState *machine)
1187 return machine->dump_guest_core;
1190 bool machine_mem_merge(MachineState *machine)
1192 return machine->mem_merge;
1195 static char *cpu_slot_to_string(const CPUArchId *cpu)
1197 GString *s = g_string_new(NULL);
1198 if (cpu->props.has_socket_id) {
1199 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1201 if (cpu->props.has_die_id) {
1202 if (s->len) {
1203 g_string_append_printf(s, ", ");
1205 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1207 if (cpu->props.has_cluster_id) {
1208 if (s->len) {
1209 g_string_append_printf(s, ", ");
1211 g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
1213 if (cpu->props.has_core_id) {
1214 if (s->len) {
1215 g_string_append_printf(s, ", ");
1217 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1219 if (cpu->props.has_thread_id) {
1220 if (s->len) {
1221 g_string_append_printf(s, ", ");
1223 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1225 return g_string_free(s, false);
1228 static void numa_validate_initiator(NumaState *numa_state)
1230 int i;
1231 NodeInfo *numa_info = numa_state->nodes;
1233 for (i = 0; i < numa_state->num_nodes; i++) {
1234 if (numa_info[i].initiator == MAX_NODES) {
1235 continue;
1238 if (!numa_info[numa_info[i].initiator].present) {
1239 error_report("NUMA node %" PRIu16 " is missing, use "
1240 "'-numa node' option to declare it first",
1241 numa_info[i].initiator);
1242 exit(1);
1245 if (!numa_info[numa_info[i].initiator].has_cpu) {
1246 error_report("The initiator of NUMA node %d is invalid", i);
1247 exit(1);
1252 static void machine_numa_finish_cpu_init(MachineState *machine)
1254 int i;
1255 bool default_mapping;
1256 GString *s = g_string_new(NULL);
1257 MachineClass *mc = MACHINE_GET_CLASS(machine);
1258 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1260 assert(machine->numa_state->num_nodes);
1261 for (i = 0; i < possible_cpus->len; i++) {
1262 if (possible_cpus->cpus[i].props.has_node_id) {
1263 break;
1266 default_mapping = (i == possible_cpus->len);
1268 for (i = 0; i < possible_cpus->len; i++) {
1269 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1271 if (!cpu_slot->props.has_node_id) {
1272 /* fetch default mapping from board and enable it */
1273 CpuInstanceProperties props = cpu_slot->props;
1275 props.node_id = mc->get_default_cpu_node_id(machine, i);
1276 if (!default_mapping) {
1277 /* record slots with not set mapping,
1278 * TODO: make it hard error in future */
1279 char *cpu_str = cpu_slot_to_string(cpu_slot);
1280 g_string_append_printf(s, "%sCPU %d [%s]",
1281 s->len ? ", " : "", i, cpu_str);
1282 g_free(cpu_str);
1284 /* non mapped cpus used to fallback to node 0 */
1285 props.node_id = 0;
1288 props.has_node_id = true;
1289 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1293 if (machine->numa_state->hmat_enabled) {
1294 numa_validate_initiator(machine->numa_state);
1297 if (s->len && !qtest_enabled()) {
1298 warn_report("CPU(s) not present in any NUMA nodes: %s",
1299 s->str);
1300 warn_report("All CPU(s) up to maxcpus should be described "
1301 "in NUMA config, ability to start up with partial NUMA "
1302 "mappings is obsoleted and will be removed in future");
1304 g_string_free(s, true);
1307 static void validate_cpu_cluster_to_numa_boundary(MachineState *ms)
1309 MachineClass *mc = MACHINE_GET_CLASS(ms);
1310 NumaState *state = ms->numa_state;
1311 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1312 const CPUArchId *cpus = possible_cpus->cpus;
1313 int i, j;
1315 if (qtest_enabled() || state->num_nodes <= 1 || possible_cpus->len <= 1) {
1316 return;
1320 * The Linux scheduling domain can't be parsed when the multiple CPUs
1321 * in one cluster have been associated with different NUMA nodes. However,
1322 * it's fine to associate one NUMA node with CPUs in different clusters.
1324 for (i = 0; i < possible_cpus->len; i++) {
1325 for (j = i + 1; j < possible_cpus->len; j++) {
1326 if (cpus[i].props.has_socket_id &&
1327 cpus[i].props.has_cluster_id &&
1328 cpus[i].props.has_node_id &&
1329 cpus[j].props.has_socket_id &&
1330 cpus[j].props.has_cluster_id &&
1331 cpus[j].props.has_node_id &&
1332 cpus[i].props.socket_id == cpus[j].props.socket_id &&
1333 cpus[i].props.cluster_id == cpus[j].props.cluster_id &&
1334 cpus[i].props.node_id != cpus[j].props.node_id) {
1335 warn_report("CPU-%d and CPU-%d in socket-%" PRId64 "-cluster-%" PRId64
1336 " have been associated with node-%" PRId64 " and node-%" PRId64
1337 " respectively. It can cause OSes like Linux to"
1338 " misbehave", i, j, cpus[i].props.socket_id,
1339 cpus[i].props.cluster_id, cpus[i].props.node_id,
1340 cpus[j].props.node_id);
1346 MemoryRegion *machine_consume_memdev(MachineState *machine,
1347 HostMemoryBackend *backend)
1349 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1351 if (host_memory_backend_is_mapped(backend)) {
1352 error_report("memory backend %s can't be used multiple times.",
1353 object_get_canonical_path_component(OBJECT(backend)));
1354 exit(EXIT_FAILURE);
1356 host_memory_backend_set_mapped(backend, true);
1357 vmstate_register_ram_global(ret);
1358 return ret;
1361 static bool create_default_memdev(MachineState *ms, const char *path, Error **errp)
1363 Object *obj;
1364 MachineClass *mc = MACHINE_GET_CLASS(ms);
1365 bool r = false;
1367 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
1368 if (path) {
1369 if (!object_property_set_str(obj, "mem-path", path, errp)) {
1370 goto out;
1373 if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
1374 goto out;
1376 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
1377 obj);
1378 /* Ensure backend's memory region name is equal to mc->default_ram_id */
1379 if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
1380 false, errp)) {
1381 goto out;
1383 if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
1384 goto out;
1386 r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
1388 out:
1389 object_unref(obj);
1390 return r;
1393 const char *machine_class_default_cpu_type(MachineClass *mc)
1395 if (mc->valid_cpu_types && !mc->valid_cpu_types[1]) {
1396 /* Only a single CPU type allowed: use it as default. */
1397 return mc->valid_cpu_types[0];
1399 return mc->default_cpu_type;
1402 static bool is_cpu_type_supported(const MachineState *machine, Error **errp)
1404 MachineClass *mc = MACHINE_GET_CLASS(machine);
1405 ObjectClass *oc = object_class_by_name(machine->cpu_type);
1406 CPUClass *cc;
1407 int i;
1410 * Check if the user specified CPU type is supported when the valid
1411 * CPU types have been determined. Note that the user specified CPU
1412 * type is provided through '-cpu' option.
1414 if (mc->valid_cpu_types) {
1415 assert(mc->valid_cpu_types[0] != NULL);
1416 for (i = 0; mc->valid_cpu_types[i]; i++) {
1417 if (object_class_dynamic_cast(oc, mc->valid_cpu_types[i])) {
1418 break;
1422 /* The user specified CPU type isn't valid */
1423 if (!mc->valid_cpu_types[i]) {
1424 g_autofree char *requested = cpu_model_from_type(machine->cpu_type);
1425 error_setg(errp, "Invalid CPU model: %s", requested);
1426 if (!mc->valid_cpu_types[1]) {
1427 g_autofree char *model = cpu_model_from_type(
1428 mc->valid_cpu_types[0]);
1429 error_append_hint(errp, "The only valid type is: %s\n", model);
1430 } else {
1431 error_append_hint(errp, "The valid models are: ");
1432 for (i = 0; mc->valid_cpu_types[i]; i++) {
1433 g_autofree char *model = cpu_model_from_type(
1434 mc->valid_cpu_types[i]);
1435 error_append_hint(errp, "%s%s",
1436 model,
1437 mc->valid_cpu_types[i + 1] ? ", " : "");
1439 error_append_hint(errp, "\n");
1442 return false;
1446 /* Check if CPU type is deprecated and warn if so */
1447 cc = CPU_CLASS(oc);
1448 assert(cc != NULL);
1449 if (cc->deprecation_note) {
1450 warn_report("CPU model %s is deprecated -- %s",
1451 machine->cpu_type, cc->deprecation_note);
1454 return true;
1457 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
1459 ERRP_GUARD();
1460 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1462 /* This checkpoint is required by replay to separate prior clock
1463 reading from the other reads, because timer polling functions query
1464 clock values from the log. */
1465 replay_checkpoint(CHECKPOINT_INIT);
1467 if (!xen_enabled()) {
1468 /* On 32-bit hosts, QEMU is limited by virtual address space */
1469 if (machine->ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1470 error_setg(errp, "at most 2047 MB RAM can be simulated");
1471 return;
1475 if (machine->memdev) {
1476 ram_addr_t backend_size = object_property_get_uint(OBJECT(machine->memdev),
1477 "size", &error_abort);
1478 if (backend_size != machine->ram_size) {
1479 error_setg(errp, "Machine memory size does not match the size of the memory backend");
1480 return;
1482 } else if (machine_class->default_ram_id && machine->ram_size &&
1483 numa_uses_legacy_mem()) {
1484 if (object_property_find(object_get_objects_root(),
1485 machine_class->default_ram_id)) {
1486 error_setg(errp, "object's id '%s' is reserved for the default"
1487 " RAM backend, it can't be used for any other purposes",
1488 machine_class->default_ram_id);
1489 error_append_hint(errp,
1490 "Change the object's 'id' to something else or disable"
1491 " automatic creation of the default RAM backend by setting"
1492 " 'memory-backend=%s' with '-machine'.\n",
1493 machine_class->default_ram_id);
1494 return;
1496 if (!create_default_memdev(current_machine, mem_path, errp)) {
1497 return;
1501 if (machine->numa_state) {
1502 numa_complete_configuration(machine);
1503 if (machine->numa_state->num_nodes) {
1504 machine_numa_finish_cpu_init(machine);
1505 if (machine_class->cpu_cluster_has_numa_boundary) {
1506 validate_cpu_cluster_to_numa_boundary(machine);
1511 if (!machine->ram && machine->memdev) {
1512 machine->ram = machine_consume_memdev(machine, machine->memdev);
1515 /* Check if the CPU type is supported */
1516 if (machine->cpu_type && !is_cpu_type_supported(machine, errp)) {
1517 return;
1520 if (machine->cgs) {
1522 * With confidential guests, the host can't see the real
1523 * contents of RAM, so there's no point in it trying to merge
1524 * areas.
1526 machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1529 * Virtio devices can't count on directly accessing guest
1530 * memory, so they need iommu_platform=on to use normal DMA
1531 * mechanisms. That requires also disabling legacy virtio
1532 * support for those virtio pci devices which allow it.
1534 object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1535 "on", true);
1536 object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1537 "on", false);
1540 accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1541 machine_class->init(machine);
1542 phase_advance(PHASE_MACHINE_INITIALIZED);
1545 static NotifierList machine_init_done_notifiers =
1546 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1548 void qemu_add_machine_init_done_notifier(Notifier *notify)
1550 notifier_list_add(&machine_init_done_notifiers, notify);
1551 if (phase_check(PHASE_MACHINE_READY)) {
1552 notify->notify(notify, NULL);
1556 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1558 notifier_remove(notify);
1561 void qdev_machine_creation_done(void)
1563 cpu_synchronize_all_post_init();
1565 if (current_machine->boot_config.once) {
1566 qemu_boot_set(current_machine->boot_config.once, &error_fatal);
1567 qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_config.order));
1571 * ok, initial machine setup is done, starting from now we can
1572 * only create hotpluggable devices
1574 phase_advance(PHASE_MACHINE_READY);
1575 qdev_assert_realized_properly();
1577 /* TODO: once all bus devices are qdevified, this should be done
1578 * when bus is created by qdev.c */
1580 * TODO: If we had a main 'reset container' that the whole system
1581 * lived in, we could reset that using the multi-phase reset
1582 * APIs. For the moment, we just reset the sysbus, which will cause
1583 * all devices hanging off it (and all their child buses, recursively)
1584 * to be reset. Note that this will *not* reset any Device objects
1585 * which are not attached to some part of the qbus tree!
1587 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1589 notifier_list_notify(&machine_init_done_notifiers, NULL);
1591 if (rom_check_and_register_reset() != 0) {
1592 exit(1);
1595 replay_start();
1597 /* This checkpoint is required by replay to separate prior clock
1598 reading from the other reads, because timer polling functions query
1599 clock values from the log. */
1600 replay_checkpoint(CHECKPOINT_RESET);
1601 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1602 register_global_state();
1605 static const TypeInfo machine_info = {
1606 .name = TYPE_MACHINE,
1607 .parent = TYPE_OBJECT,
1608 .abstract = true,
1609 .class_size = sizeof(MachineClass),
1610 .class_init = machine_class_init,
1611 .class_base_init = machine_class_base_init,
1612 .instance_size = sizeof(MachineState),
1613 .instance_init = machine_initfn,
1614 .instance_finalize = machine_finalize,
1617 static void machine_register_types(void)
1619 type_register_static(&machine_info);
1622 type_init(machine_register_types)