hw/scsi/scsi-disk: Disallow block sizes smaller than 512 [CVE-2023-42467]
[qemu/ar7.git] / hw / core / machine.c
blobcb38b8cf4cb4820210b9d6e283f2c857a1fe02ba
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/option.h"
15 #include "qemu/accel.h"
16 #include "sysemu/replay.h"
17 #include "qemu/units.h"
18 #include "hw/boards.h"
19 #include "hw/loader.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-visit-common.h"
22 #include "qapi/qapi-visit-machine.h"
23 #include "qapi/visitor.h"
24 #include "qom/object_interfaces.h"
25 #include "hw/sysbus.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/reset.h"
29 #include "sysemu/runstate.h"
30 #include "sysemu/numa.h"
31 #include "sysemu/xen.h"
32 #include "qemu/error-report.h"
33 #include "sysemu/qtest.h"
34 #include "hw/pci/pci.h"
35 #include "hw/mem/nvdimm.h"
36 #include "migration/global_state.h"
37 #include "migration/vmstate.h"
38 #include "exec/confidential-guest-support.h"
39 #include "hw/virtio/virtio.h"
40 #include "hw/virtio/virtio-pci.h"
41 #include "hw/virtio/virtio-net.h"
43 GlobalProperty hw_compat_8_1[] = {};
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 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
691 int i;
692 HotpluggableCPUList *head = NULL;
693 MachineClass *mc = MACHINE_GET_CLASS(machine);
695 /* force board to initialize possible_cpus if it hasn't been done yet */
696 mc->possible_cpu_arch_ids(machine);
698 for (i = 0; i < machine->possible_cpus->len; i++) {
699 Object *cpu;
700 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
702 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
703 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
704 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
705 sizeof(*cpu_item->props));
707 cpu = machine->possible_cpus->cpus[i].cpu;
708 if (cpu) {
709 cpu_item->qom_path = object_get_canonical_path(cpu);
711 QAPI_LIST_PREPEND(head, cpu_item);
713 return head;
717 * machine_set_cpu_numa_node:
718 * @machine: machine object to modify
719 * @props: specifies which cpu objects to assign to
720 * numa node specified by @props.node_id
721 * @errp: if an error occurs, a pointer to an area to store the error
723 * Associate NUMA node specified by @props.node_id with cpu slots that
724 * match socket/core/thread-ids specified by @props. It's recommended to use
725 * query-hotpluggable-cpus.props values to specify affected cpu slots,
726 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
728 * However for CLI convenience it's possible to pass in subset of properties,
729 * which would affect all cpu slots that match it.
730 * Ex for pc machine:
731 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
732 * -numa cpu,node-id=0,socket_id=0 \
733 * -numa cpu,node-id=1,socket_id=1
734 * will assign all child cores of socket 0 to node 0 and
735 * of socket 1 to node 1.
737 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
738 * return error.
739 * Empty subset is disallowed and function will return with error in this case.
741 void machine_set_cpu_numa_node(MachineState *machine,
742 const CpuInstanceProperties *props, Error **errp)
744 MachineClass *mc = MACHINE_GET_CLASS(machine);
745 NodeInfo *numa_info = machine->numa_state->nodes;
746 bool match = false;
747 int i;
749 if (!mc->possible_cpu_arch_ids) {
750 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
751 return;
754 /* disabling node mapping is not supported, forbid it */
755 assert(props->has_node_id);
757 /* force board to initialize possible_cpus if it hasn't been done yet */
758 mc->possible_cpu_arch_ids(machine);
760 for (i = 0; i < machine->possible_cpus->len; i++) {
761 CPUArchId *slot = &machine->possible_cpus->cpus[i];
763 /* reject unsupported by board properties */
764 if (props->has_thread_id && !slot->props.has_thread_id) {
765 error_setg(errp, "thread-id is not supported");
766 return;
769 if (props->has_core_id && !slot->props.has_core_id) {
770 error_setg(errp, "core-id is not supported");
771 return;
774 if (props->has_cluster_id && !slot->props.has_cluster_id) {
775 error_setg(errp, "cluster-id is not supported");
776 return;
779 if (props->has_socket_id && !slot->props.has_socket_id) {
780 error_setg(errp, "socket-id is not supported");
781 return;
784 if (props->has_die_id && !slot->props.has_die_id) {
785 error_setg(errp, "die-id is not supported");
786 return;
789 /* skip slots with explicit mismatch */
790 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
791 continue;
794 if (props->has_core_id && props->core_id != slot->props.core_id) {
795 continue;
798 if (props->has_cluster_id &&
799 props->cluster_id != slot->props.cluster_id) {
800 continue;
803 if (props->has_die_id && props->die_id != slot->props.die_id) {
804 continue;
807 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
808 continue;
811 /* reject assignment if slot is already assigned, for compatibility
812 * of legacy cpu_index mapping with SPAPR core based mapping do not
813 * error out if cpu thread and matched core have the same node-id */
814 if (slot->props.has_node_id &&
815 slot->props.node_id != props->node_id) {
816 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
817 slot->props.node_id);
818 return;
821 /* assign slot to node as it's matched '-numa cpu' key */
822 match = true;
823 slot->props.node_id = props->node_id;
824 slot->props.has_node_id = props->has_node_id;
826 if (machine->numa_state->hmat_enabled) {
827 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
828 (props->node_id != numa_info[props->node_id].initiator)) {
829 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
830 " should be itself (got %" PRIu16 ")",
831 props->node_id, numa_info[props->node_id].initiator);
832 return;
834 numa_info[props->node_id].has_cpu = true;
835 numa_info[props->node_id].initiator = props->node_id;
839 if (!match) {
840 error_setg(errp, "no match found");
844 static void machine_get_smp(Object *obj, Visitor *v, const char *name,
845 void *opaque, Error **errp)
847 MachineState *ms = MACHINE(obj);
848 SMPConfiguration *config = &(SMPConfiguration){
849 .has_cpus = true, .cpus = ms->smp.cpus,
850 .has_sockets = true, .sockets = ms->smp.sockets,
851 .has_dies = true, .dies = ms->smp.dies,
852 .has_clusters = true, .clusters = ms->smp.clusters,
853 .has_cores = true, .cores = ms->smp.cores,
854 .has_threads = true, .threads = ms->smp.threads,
855 .has_maxcpus = true, .maxcpus = ms->smp.max_cpus,
858 if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) {
859 return;
863 static void machine_set_smp(Object *obj, Visitor *v, const char *name,
864 void *opaque, Error **errp)
866 MachineState *ms = MACHINE(obj);
867 g_autoptr(SMPConfiguration) config = NULL;
869 if (!visit_type_SMPConfiguration(v, name, &config, errp)) {
870 return;
873 machine_parse_smp_config(ms, config, errp);
876 static void machine_get_boot(Object *obj, Visitor *v, const char *name,
877 void *opaque, Error **errp)
879 MachineState *ms = MACHINE(obj);
880 BootConfiguration *config = &ms->boot_config;
881 visit_type_BootConfiguration(v, name, &config, &error_abort);
884 static void machine_free_boot_config(MachineState *ms)
886 g_free(ms->boot_config.order);
887 g_free(ms->boot_config.once);
888 g_free(ms->boot_config.splash);
891 static void machine_copy_boot_config(MachineState *ms, BootConfiguration *config)
893 MachineClass *machine_class = MACHINE_GET_CLASS(ms);
895 machine_free_boot_config(ms);
896 ms->boot_config = *config;
897 if (!config->order) {
898 ms->boot_config.order = g_strdup(machine_class->default_boot_order);
902 static void machine_set_boot(Object *obj, Visitor *v, const char *name,
903 void *opaque, Error **errp)
905 ERRP_GUARD();
906 MachineState *ms = MACHINE(obj);
907 BootConfiguration *config = NULL;
909 if (!visit_type_BootConfiguration(v, name, &config, errp)) {
910 return;
912 if (config->order) {
913 validate_bootdevices(config->order, errp);
914 if (*errp) {
915 goto out_free;
918 if (config->once) {
919 validate_bootdevices(config->once, errp);
920 if (*errp) {
921 goto out_free;
925 machine_copy_boot_config(ms, config);
926 /* Strings live in ms->boot_config. */
927 free(config);
928 return;
930 out_free:
931 qapi_free_BootConfiguration(config);
934 static void machine_class_init(ObjectClass *oc, void *data)
936 MachineClass *mc = MACHINE_CLASS(oc);
938 /* Default 128 MB as guest ram size */
939 mc->default_ram_size = 128 * MiB;
940 mc->rom_file_has_mr = true;
942 /* numa node memory size aligned on 8MB by default.
943 * On Linux, each node's border has to be 8MB aligned
945 mc->numa_mem_align_shift = 23;
947 object_class_property_add_str(oc, "kernel",
948 machine_get_kernel, machine_set_kernel);
949 object_class_property_set_description(oc, "kernel",
950 "Linux kernel image file");
952 object_class_property_add_str(oc, "initrd",
953 machine_get_initrd, machine_set_initrd);
954 object_class_property_set_description(oc, "initrd",
955 "Linux initial ramdisk file");
957 object_class_property_add_str(oc, "append",
958 machine_get_append, machine_set_append);
959 object_class_property_set_description(oc, "append",
960 "Linux kernel command line");
962 object_class_property_add_str(oc, "dtb",
963 machine_get_dtb, machine_set_dtb);
964 object_class_property_set_description(oc, "dtb",
965 "Linux kernel device tree file");
967 object_class_property_add_str(oc, "dumpdtb",
968 machine_get_dumpdtb, machine_set_dumpdtb);
969 object_class_property_set_description(oc, "dumpdtb",
970 "Dump current dtb to a file and quit");
972 object_class_property_add(oc, "boot", "BootConfiguration",
973 machine_get_boot, machine_set_boot,
974 NULL, NULL);
975 object_class_property_set_description(oc, "boot",
976 "Boot configuration");
978 object_class_property_add(oc, "smp", "SMPConfiguration",
979 machine_get_smp, machine_set_smp,
980 NULL, NULL);
981 object_class_property_set_description(oc, "smp",
982 "CPU topology");
984 object_class_property_add(oc, "phandle-start", "int",
985 machine_get_phandle_start, machine_set_phandle_start,
986 NULL, NULL);
987 object_class_property_set_description(oc, "phandle-start",
988 "The first phandle ID we may generate dynamically");
990 object_class_property_add_str(oc, "dt-compatible",
991 machine_get_dt_compatible, machine_set_dt_compatible);
992 object_class_property_set_description(oc, "dt-compatible",
993 "Overrides the \"compatible\" property of the dt root node");
995 object_class_property_add_bool(oc, "dump-guest-core",
996 machine_get_dump_guest_core, machine_set_dump_guest_core);
997 object_class_property_set_description(oc, "dump-guest-core",
998 "Include guest memory in a core dump");
1000 object_class_property_add_bool(oc, "mem-merge",
1001 machine_get_mem_merge, machine_set_mem_merge);
1002 object_class_property_set_description(oc, "mem-merge",
1003 "Enable/disable memory merge support");
1005 object_class_property_add_bool(oc, "usb",
1006 machine_get_usb, machine_set_usb);
1007 object_class_property_set_description(oc, "usb",
1008 "Set on/off to enable/disable usb");
1010 object_class_property_add_bool(oc, "graphics",
1011 machine_get_graphics, machine_set_graphics);
1012 object_class_property_set_description(oc, "graphics",
1013 "Set on/off to enable/disable graphics emulation");
1015 object_class_property_add_str(oc, "firmware",
1016 machine_get_firmware, machine_set_firmware);
1017 object_class_property_set_description(oc, "firmware",
1018 "Firmware image");
1020 object_class_property_add_bool(oc, "suppress-vmdesc",
1021 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
1022 object_class_property_set_description(oc, "suppress-vmdesc",
1023 "Set on to disable self-describing migration");
1025 object_class_property_add_link(oc, "confidential-guest-support",
1026 TYPE_CONFIDENTIAL_GUEST_SUPPORT,
1027 offsetof(MachineState, cgs),
1028 machine_check_confidential_guest_support,
1029 OBJ_PROP_LINK_STRONG);
1030 object_class_property_set_description(oc, "confidential-guest-support",
1031 "Set confidential guest scheme to support");
1033 /* For compatibility */
1034 object_class_property_add_str(oc, "memory-encryption",
1035 machine_get_memory_encryption, machine_set_memory_encryption);
1036 object_class_property_set_description(oc, "memory-encryption",
1037 "Set memory encryption object to use");
1039 object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKEND,
1040 offsetof(MachineState, memdev), object_property_allow_set_link,
1041 OBJ_PROP_LINK_STRONG);
1042 object_class_property_set_description(oc, "memory-backend",
1043 "Set RAM backend"
1044 "Valid value is ID of hostmem based backend");
1046 object_class_property_add(oc, "memory", "MemorySizeConfiguration",
1047 machine_get_mem, machine_set_mem,
1048 NULL, NULL);
1049 object_class_property_set_description(oc, "memory",
1050 "Memory size configuration");
1053 static void machine_class_base_init(ObjectClass *oc, void *data)
1055 MachineClass *mc = MACHINE_CLASS(oc);
1056 mc->max_cpus = mc->max_cpus ?: 1;
1057 mc->min_cpus = mc->min_cpus ?: 1;
1058 mc->default_cpus = mc->default_cpus ?: 1;
1060 if (!object_class_is_abstract(oc)) {
1061 const char *cname = object_class_get_name(oc);
1062 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
1063 mc->name = g_strndup(cname,
1064 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
1065 mc->compat_props = g_ptr_array_new();
1069 static void machine_initfn(Object *obj)
1071 MachineState *ms = MACHINE(obj);
1072 MachineClass *mc = MACHINE_GET_CLASS(obj);
1074 container_get(obj, "/peripheral");
1075 container_get(obj, "/peripheral-anon");
1077 ms->dump_guest_core = true;
1078 ms->mem_merge = true;
1079 ms->enable_graphics = true;
1080 ms->kernel_cmdline = g_strdup("");
1081 ms->ram_size = mc->default_ram_size;
1082 ms->maxram_size = mc->default_ram_size;
1084 if (mc->nvdimm_supported) {
1085 Object *obj = OBJECT(ms);
1087 ms->nvdimms_state = g_new0(NVDIMMState, 1);
1088 object_property_add_bool(obj, "nvdimm",
1089 machine_get_nvdimm, machine_set_nvdimm);
1090 object_property_set_description(obj, "nvdimm",
1091 "Set on/off to enable/disable "
1092 "NVDIMM instantiation");
1094 object_property_add_str(obj, "nvdimm-persistence",
1095 machine_get_nvdimm_persistence,
1096 machine_set_nvdimm_persistence);
1097 object_property_set_description(obj, "nvdimm-persistence",
1098 "Set NVDIMM persistence"
1099 "Valid values are cpu, mem-ctrl");
1102 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
1103 ms->numa_state = g_new0(NumaState, 1);
1104 object_property_add_bool(obj, "hmat",
1105 machine_get_hmat, machine_set_hmat);
1106 object_property_set_description(obj, "hmat",
1107 "Set on/off to enable/disable "
1108 "ACPI Heterogeneous Memory Attribute "
1109 "Table (HMAT)");
1112 /* default to mc->default_cpus */
1113 ms->smp.cpus = mc->default_cpus;
1114 ms->smp.max_cpus = mc->default_cpus;
1115 ms->smp.sockets = 1;
1116 ms->smp.dies = 1;
1117 ms->smp.clusters = 1;
1118 ms->smp.cores = 1;
1119 ms->smp.threads = 1;
1121 machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
1124 static void machine_finalize(Object *obj)
1126 MachineState *ms = MACHINE(obj);
1128 machine_free_boot_config(ms);
1129 g_free(ms->kernel_filename);
1130 g_free(ms->initrd_filename);
1131 g_free(ms->kernel_cmdline);
1132 g_free(ms->dtb);
1133 g_free(ms->dumpdtb);
1134 g_free(ms->dt_compatible);
1135 g_free(ms->firmware);
1136 g_free(ms->device_memory);
1137 g_free(ms->nvdimms_state);
1138 g_free(ms->numa_state);
1141 bool machine_usb(MachineState *machine)
1143 return machine->usb;
1146 int machine_phandle_start(MachineState *machine)
1148 return machine->phandle_start;
1151 bool machine_dump_guest_core(MachineState *machine)
1153 return machine->dump_guest_core;
1156 bool machine_mem_merge(MachineState *machine)
1158 return machine->mem_merge;
1161 static char *cpu_slot_to_string(const CPUArchId *cpu)
1163 GString *s = g_string_new(NULL);
1164 if (cpu->props.has_socket_id) {
1165 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1167 if (cpu->props.has_die_id) {
1168 if (s->len) {
1169 g_string_append_printf(s, ", ");
1171 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1173 if (cpu->props.has_cluster_id) {
1174 if (s->len) {
1175 g_string_append_printf(s, ", ");
1177 g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
1179 if (cpu->props.has_core_id) {
1180 if (s->len) {
1181 g_string_append_printf(s, ", ");
1183 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1185 if (cpu->props.has_thread_id) {
1186 if (s->len) {
1187 g_string_append_printf(s, ", ");
1189 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1191 return g_string_free(s, false);
1194 static void numa_validate_initiator(NumaState *numa_state)
1196 int i;
1197 NodeInfo *numa_info = numa_state->nodes;
1199 for (i = 0; i < numa_state->num_nodes; i++) {
1200 if (numa_info[i].initiator == MAX_NODES) {
1201 continue;
1204 if (!numa_info[numa_info[i].initiator].present) {
1205 error_report("NUMA node %" PRIu16 " is missing, use "
1206 "'-numa node' option to declare it first",
1207 numa_info[i].initiator);
1208 exit(1);
1211 if (!numa_info[numa_info[i].initiator].has_cpu) {
1212 error_report("The initiator of NUMA node %d is invalid", i);
1213 exit(1);
1218 static void machine_numa_finish_cpu_init(MachineState *machine)
1220 int i;
1221 bool default_mapping;
1222 GString *s = g_string_new(NULL);
1223 MachineClass *mc = MACHINE_GET_CLASS(machine);
1224 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1226 assert(machine->numa_state->num_nodes);
1227 for (i = 0; i < possible_cpus->len; i++) {
1228 if (possible_cpus->cpus[i].props.has_node_id) {
1229 break;
1232 default_mapping = (i == possible_cpus->len);
1234 for (i = 0; i < possible_cpus->len; i++) {
1235 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1237 if (!cpu_slot->props.has_node_id) {
1238 /* fetch default mapping from board and enable it */
1239 CpuInstanceProperties props = cpu_slot->props;
1241 props.node_id = mc->get_default_cpu_node_id(machine, i);
1242 if (!default_mapping) {
1243 /* record slots with not set mapping,
1244 * TODO: make it hard error in future */
1245 char *cpu_str = cpu_slot_to_string(cpu_slot);
1246 g_string_append_printf(s, "%sCPU %d [%s]",
1247 s->len ? ", " : "", i, cpu_str);
1248 g_free(cpu_str);
1250 /* non mapped cpus used to fallback to node 0 */
1251 props.node_id = 0;
1254 props.has_node_id = true;
1255 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1259 if (machine->numa_state->hmat_enabled) {
1260 numa_validate_initiator(machine->numa_state);
1263 if (s->len && !qtest_enabled()) {
1264 warn_report("CPU(s) not present in any NUMA nodes: %s",
1265 s->str);
1266 warn_report("All CPU(s) up to maxcpus should be described "
1267 "in NUMA config, ability to start up with partial NUMA "
1268 "mappings is obsoleted and will be removed in future");
1270 g_string_free(s, true);
1273 static void validate_cpu_cluster_to_numa_boundary(MachineState *ms)
1275 MachineClass *mc = MACHINE_GET_CLASS(ms);
1276 NumaState *state = ms->numa_state;
1277 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1278 const CPUArchId *cpus = possible_cpus->cpus;
1279 int i, j;
1281 if (state->num_nodes <= 1 || possible_cpus->len <= 1) {
1282 return;
1286 * The Linux scheduling domain can't be parsed when the multiple CPUs
1287 * in one cluster have been associated with different NUMA nodes. However,
1288 * it's fine to associate one NUMA node with CPUs in different clusters.
1290 for (i = 0; i < possible_cpus->len; i++) {
1291 for (j = i + 1; j < possible_cpus->len; j++) {
1292 if (cpus[i].props.has_socket_id &&
1293 cpus[i].props.has_cluster_id &&
1294 cpus[i].props.has_node_id &&
1295 cpus[j].props.has_socket_id &&
1296 cpus[j].props.has_cluster_id &&
1297 cpus[j].props.has_node_id &&
1298 cpus[i].props.socket_id == cpus[j].props.socket_id &&
1299 cpus[i].props.cluster_id == cpus[j].props.cluster_id &&
1300 cpus[i].props.node_id != cpus[j].props.node_id) {
1301 warn_report("CPU-%d and CPU-%d in socket-%" PRId64 "-cluster-%" PRId64
1302 " have been associated with node-%" PRId64 " and node-%" PRId64
1303 " respectively. It can cause OSes like Linux to"
1304 " misbehave", i, j, cpus[i].props.socket_id,
1305 cpus[i].props.cluster_id, cpus[i].props.node_id,
1306 cpus[j].props.node_id);
1312 MemoryRegion *machine_consume_memdev(MachineState *machine,
1313 HostMemoryBackend *backend)
1315 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1317 if (host_memory_backend_is_mapped(backend)) {
1318 error_report("memory backend %s can't be used multiple times.",
1319 object_get_canonical_path_component(OBJECT(backend)));
1320 exit(EXIT_FAILURE);
1322 host_memory_backend_set_mapped(backend, true);
1323 vmstate_register_ram_global(ret);
1324 return ret;
1327 static bool create_default_memdev(MachineState *ms, const char *path, Error **errp)
1329 Object *obj;
1330 MachineClass *mc = MACHINE_GET_CLASS(ms);
1331 bool r = false;
1333 obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
1334 if (path) {
1335 if (!object_property_set_str(obj, "mem-path", path, errp)) {
1336 goto out;
1339 if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
1340 goto out;
1342 object_property_add_child(object_get_objects_root(), mc->default_ram_id,
1343 obj);
1344 /* Ensure backend's memory region name is equal to mc->default_ram_id */
1345 if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
1346 false, errp)) {
1347 goto out;
1349 if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
1350 goto out;
1352 r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
1354 out:
1355 object_unref(obj);
1356 return r;
1360 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
1362 ERRP_GUARD();
1363 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1364 ObjectClass *oc = object_class_by_name(machine->cpu_type);
1365 CPUClass *cc;
1367 /* This checkpoint is required by replay to separate prior clock
1368 reading from the other reads, because timer polling functions query
1369 clock values from the log. */
1370 replay_checkpoint(CHECKPOINT_INIT);
1372 if (!xen_enabled()) {
1373 /* On 32-bit hosts, QEMU is limited by virtual address space */
1374 if (machine->ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1375 error_setg(errp, "at most 2047 MB RAM can be simulated");
1376 return;
1380 if (machine->memdev) {
1381 ram_addr_t backend_size = object_property_get_uint(OBJECT(machine->memdev),
1382 "size", &error_abort);
1383 if (backend_size != machine->ram_size) {
1384 error_setg(errp, "Machine memory size does not match the size of the memory backend");
1385 return;
1387 } else if (machine_class->default_ram_id && machine->ram_size &&
1388 numa_uses_legacy_mem()) {
1389 if (object_property_find(object_get_objects_root(),
1390 machine_class->default_ram_id)) {
1391 error_setg(errp, "object's id '%s' is reserved for the default"
1392 " RAM backend, it can't be used for any other purposes",
1393 machine_class->default_ram_id);
1394 error_append_hint(errp,
1395 "Change the object's 'id' to something else or disable"
1396 " automatic creation of the default RAM backend by setting"
1397 " 'memory-backend=%s' with '-machine'.\n",
1398 machine_class->default_ram_id);
1399 return;
1401 if (!create_default_memdev(current_machine, mem_path, errp)) {
1402 return;
1406 if (machine->numa_state) {
1407 numa_complete_configuration(machine);
1408 if (machine->numa_state->num_nodes) {
1409 machine_numa_finish_cpu_init(machine);
1410 if (machine_class->cpu_cluster_has_numa_boundary) {
1411 validate_cpu_cluster_to_numa_boundary(machine);
1416 if (!machine->ram && machine->memdev) {
1417 machine->ram = machine_consume_memdev(machine, machine->memdev);
1420 /* If the machine supports the valid_cpu_types check and the user
1421 * specified a CPU with -cpu check here that the user CPU is supported.
1423 if (machine_class->valid_cpu_types && machine->cpu_type) {
1424 int i;
1426 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1427 if (object_class_dynamic_cast(oc,
1428 machine_class->valid_cpu_types[i])) {
1429 /* The user specified CPU is in the valid field, we are
1430 * good to go.
1432 break;
1436 if (!machine_class->valid_cpu_types[i]) {
1437 /* The user specified CPU is not valid */
1438 error_report("Invalid CPU type: %s", machine->cpu_type);
1439 error_printf("The valid types are: %s",
1440 machine_class->valid_cpu_types[0]);
1441 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1442 error_printf(", %s", machine_class->valid_cpu_types[i]);
1444 error_printf("\n");
1446 exit(1);
1450 /* Check if CPU type is deprecated and warn if so */
1451 cc = CPU_CLASS(oc);
1452 if (cc && cc->deprecation_note) {
1453 warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1454 cc->deprecation_note);
1457 if (machine->cgs) {
1459 * With confidential guests, the host can't see the real
1460 * contents of RAM, so there's no point in it trying to merge
1461 * areas.
1463 machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1466 * Virtio devices can't count on directly accessing guest
1467 * memory, so they need iommu_platform=on to use normal DMA
1468 * mechanisms. That requires also disabling legacy virtio
1469 * support for those virtio pci devices which allow it.
1471 object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1472 "on", true);
1473 object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1474 "on", false);
1477 accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1478 machine_class->init(machine);
1479 phase_advance(PHASE_MACHINE_INITIALIZED);
1482 static NotifierList machine_init_done_notifiers =
1483 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1485 void qemu_add_machine_init_done_notifier(Notifier *notify)
1487 notifier_list_add(&machine_init_done_notifiers, notify);
1488 if (phase_check(PHASE_MACHINE_READY)) {
1489 notify->notify(notify, NULL);
1493 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1495 notifier_remove(notify);
1498 void qdev_machine_creation_done(void)
1500 cpu_synchronize_all_post_init();
1502 if (current_machine->boot_config.once) {
1503 qemu_boot_set(current_machine->boot_config.once, &error_fatal);
1504 qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_config.order));
1508 * ok, initial machine setup is done, starting from now we can
1509 * only create hotpluggable devices
1511 phase_advance(PHASE_MACHINE_READY);
1512 qdev_assert_realized_properly();
1514 /* TODO: once all bus devices are qdevified, this should be done
1515 * when bus is created by qdev.c */
1517 * TODO: If we had a main 'reset container' that the whole system
1518 * lived in, we could reset that using the multi-phase reset
1519 * APIs. For the moment, we just reset the sysbus, which will cause
1520 * all devices hanging off it (and all their child buses, recursively)
1521 * to be reset. Note that this will *not* reset any Device objects
1522 * which are not attached to some part of the qbus tree!
1524 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1526 notifier_list_notify(&machine_init_done_notifiers, NULL);
1528 if (rom_check_and_register_reset() != 0) {
1529 exit(1);
1532 replay_start();
1534 /* This checkpoint is required by replay to separate prior clock
1535 reading from the other reads, because timer polling functions query
1536 clock values from the log. */
1537 replay_checkpoint(CHECKPOINT_RESET);
1538 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1539 register_global_state();
1542 static const TypeInfo machine_info = {
1543 .name = TYPE_MACHINE,
1544 .parent = TYPE_OBJECT,
1545 .abstract = true,
1546 .class_size = sizeof(MachineClass),
1547 .class_init = machine_class_init,
1548 .class_base_init = machine_class_base_init,
1549 .instance_size = sizeof(MachineState),
1550 .instance_init = machine_initfn,
1551 .instance_finalize = machine_finalize,
1554 static void machine_register_types(void)
1556 type_register_static(&machine_info);
1559 type_init(machine_register_types)