xref: /openbmc/qemu/hw/core/machine.c (revision daa55f3e)
1 /*
2  * QEMU Machine
3  *
4  * Copyright (C) 2014 Red Hat Inc
5  *
6  * Authors:
7  *   Marcel Apfelbaum <marcel.a@redhat.com>
8  *
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.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/option.h"
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/replay.h"
17 #include "qemu/units.h"
18 #include "hw/boards.h"
19 #include "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 "hw/cxl/cxl.h"
37 #include "migration/global_state.h"
38 #include "migration/vmstate.h"
39 #include "exec/confidential-guest-support.h"
40 #include "hw/virtio/virtio.h"
41 #include "hw/virtio/virtio-pci.h"
42 #include "qom/object_interfaces.h"
43 
44 GlobalProperty hw_compat_7_0[] = {
45     { "arm-gicv3-common", "force-8-bit-prio", "on" },
46 };
47 const size_t hw_compat_7_0_len = G_N_ELEMENTS(hw_compat_7_0);
48 
49 GlobalProperty hw_compat_6_2[] = {
50     { "PIIX4_PM", "x-not-migrate-acpi-index", "on"},
51 };
52 const size_t hw_compat_6_2_len = G_N_ELEMENTS(hw_compat_6_2);
53 
54 GlobalProperty hw_compat_6_1[] = {
55     { "vhost-user-vsock-device", "seqpacket", "off" },
56     { "nvme-ns", "shared", "off" },
57 };
58 const size_t hw_compat_6_1_len = G_N_ELEMENTS(hw_compat_6_1);
59 
60 GlobalProperty hw_compat_6_0[] = {
61     { "gpex-pcihost", "allow-unmapped-accesses", "false" },
62     { "i8042", "extended-state", "false"},
63     { "nvme-ns", "eui64-default", "off"},
64     { "e1000", "init-vet", "off" },
65     { "e1000e", "init-vet", "off" },
66     { "vhost-vsock-device", "seqpacket", "off" },
67 };
68 const size_t hw_compat_6_0_len = G_N_ELEMENTS(hw_compat_6_0);
69 
70 GlobalProperty hw_compat_5_2[] = {
71     { "ICH9-LPC", "smm-compat", "on"},
72     { "PIIX4_PM", "smm-compat", "on"},
73     { "virtio-blk-device", "report-discard-granularity", "off" },
74     { "virtio-net-pci-base", "vectors", "3"},
75 };
76 const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
77 
78 GlobalProperty hw_compat_5_1[] = {
79     { "vhost-scsi", "num_queues", "1"},
80     { "vhost-user-blk", "num-queues", "1"},
81     { "vhost-user-scsi", "num_queues", "1"},
82     { "virtio-blk-device", "num-queues", "1"},
83     { "virtio-scsi-device", "num_queues", "1"},
84     { "nvme", "use-intel-id", "on"},
85     { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
86     { "pl011", "migrate-clk", "off" },
87     { "virtio-pci", "x-ats-page-aligned", "off"},
88 };
89 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
90 
91 GlobalProperty hw_compat_5_0[] = {
92     { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
93     { "virtio-balloon-device", "page-poison", "false" },
94     { "vmport", "x-read-set-eax", "off" },
95     { "vmport", "x-signal-unsupported-cmd", "off" },
96     { "vmport", "x-report-vmx-type", "off" },
97     { "vmport", "x-cmds-v2", "off" },
98     { "virtio-device", "x-disable-legacy-check", "true" },
99 };
100 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
101 
102 GlobalProperty hw_compat_4_2[] = {
103     { "virtio-blk-device", "queue-size", "128"},
104     { "virtio-scsi-device", "virtqueue_size", "128"},
105     { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
106     { "virtio-blk-device", "seg-max-adjust", "off"},
107     { "virtio-scsi-device", "seg_max_adjust", "off"},
108     { "vhost-blk-device", "seg_max_adjust", "off"},
109     { "usb-host", "suppress-remote-wake", "off" },
110     { "usb-redir", "suppress-remote-wake", "off" },
111     { "qxl", "revision", "4" },
112     { "qxl-vga", "revision", "4" },
113     { "fw_cfg", "acpi-mr-restore", "false" },
114     { "virtio-device", "use-disabled-flag", "false" },
115 };
116 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
117 
118 GlobalProperty hw_compat_4_1[] = {
119     { "virtio-pci", "x-pcie-flr-init", "off" },
120 };
121 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
122 
123 GlobalProperty hw_compat_4_0[] = {
124     { "VGA",            "edid", "false" },
125     { "secondary-vga",  "edid", "false" },
126     { "bochs-display",  "edid", "false" },
127     { "virtio-vga",     "edid", "false" },
128     { "virtio-gpu-device", "edid", "false" },
129     { "virtio-device", "use-started", "false" },
130     { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
131     { "pl031", "migrate-tick-offset", "false" },
132 };
133 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
134 
135 GlobalProperty hw_compat_3_1[] = {
136     { "pcie-root-port", "x-speed", "2_5" },
137     { "pcie-root-port", "x-width", "1" },
138     { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
139     { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
140     { "tpm-crb", "ppi", "false" },
141     { "tpm-tis", "ppi", "false" },
142     { "usb-kbd", "serial", "42" },
143     { "usb-mouse", "serial", "42" },
144     { "usb-tablet", "serial", "42" },
145     { "virtio-blk-device", "discard", "false" },
146     { "virtio-blk-device", "write-zeroes", "false" },
147     { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
148     { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
149 };
150 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
151 
152 GlobalProperty hw_compat_3_0[] = {};
153 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
154 
155 GlobalProperty hw_compat_2_12[] = {
156     { "migration", "decompress-error-check", "off" },
157     { "hda-audio", "use-timer", "false" },
158     { "cirrus-vga", "global-vmstate", "true" },
159     { "VGA", "global-vmstate", "true" },
160     { "vmware-svga", "global-vmstate", "true" },
161     { "qxl-vga", "global-vmstate", "true" },
162 };
163 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
164 
165 GlobalProperty hw_compat_2_11[] = {
166     { "hpet", "hpet-offset-saved", "false" },
167     { "virtio-blk-pci", "vectors", "2" },
168     { "vhost-user-blk-pci", "vectors", "2" },
169     { "e1000", "migrate_tso_props", "off" },
170 };
171 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
172 
173 GlobalProperty hw_compat_2_10[] = {
174     { "virtio-mouse-device", "wheel-axis", "false" },
175     { "virtio-tablet-device", "wheel-axis", "false" },
176 };
177 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
178 
179 GlobalProperty hw_compat_2_9[] = {
180     { "pci-bridge", "shpc", "off" },
181     { "intel-iommu", "pt", "off" },
182     { "virtio-net-device", "x-mtu-bypass-backend", "off" },
183     { "pcie-root-port", "x-migrate-msix", "false" },
184 };
185 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
186 
187 GlobalProperty hw_compat_2_8[] = {
188     { "fw_cfg_mem", "x-file-slots", "0x10" },
189     { "fw_cfg_io", "x-file-slots", "0x10" },
190     { "pflash_cfi01", "old-multiple-chip-handling", "on" },
191     { "pci-bridge", "shpc", "on" },
192     { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
193     { "virtio-pci", "x-pcie-deverr-init", "off" },
194     { "virtio-pci", "x-pcie-lnkctl-init", "off" },
195     { "virtio-pci", "x-pcie-pm-init", "off" },
196     { "cirrus-vga", "vgamem_mb", "8" },
197     { "isa-cirrus-vga", "vgamem_mb", "8" },
198 };
199 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
200 
201 GlobalProperty hw_compat_2_7[] = {
202     { "virtio-pci", "page-per-vq", "on" },
203     { "virtio-serial-device", "emergency-write", "off" },
204     { "ioapic", "version", "0x11" },
205     { "intel-iommu", "x-buggy-eim", "true" },
206     { "virtio-pci", "x-ignore-backend-features", "on" },
207 };
208 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
209 
210 GlobalProperty hw_compat_2_6[] = {
211     { "virtio-mmio", "format_transport_address", "off" },
212     /* Optional because not all virtio-pci devices support legacy mode */
213     { "virtio-pci", "disable-modern", "on",  .optional = true },
214     { "virtio-pci", "disable-legacy", "off", .optional = true },
215 };
216 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
217 
218 GlobalProperty hw_compat_2_5[] = {
219     { "isa-fdc", "fallback", "144" },
220     { "pvscsi", "x-old-pci-configuration", "on" },
221     { "pvscsi", "x-disable-pcie", "on" },
222     { "vmxnet3", "x-old-msi-offsets", "on" },
223     { "vmxnet3", "x-disable-pcie", "on" },
224 };
225 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
226 
227 GlobalProperty hw_compat_2_4[] = {
228     /* Optional because the 'scsi' property is Linux-only */
229     { "virtio-blk-device", "scsi", "true", .optional = true },
230     { "e1000", "extra_mac_registers", "off" },
231     { "virtio-pci", "x-disable-pcie", "on" },
232     { "virtio-pci", "migrate-extra", "off" },
233     { "fw_cfg_mem", "dma_enabled", "off" },
234     { "fw_cfg_io", "dma_enabled", "off" }
235 };
236 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
237 
238 GlobalProperty hw_compat_2_3[] = {
239     { "virtio-blk-pci", "any_layout", "off" },
240     { "virtio-balloon-pci", "any_layout", "off" },
241     { "virtio-serial-pci", "any_layout", "off" },
242     { "virtio-9p-pci", "any_layout", "off" },
243     { "virtio-rng-pci", "any_layout", "off" },
244     { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
245     { "migration", "send-configuration", "off" },
246     { "migration", "send-section-footer", "off" },
247     { "migration", "store-global-state", "off" },
248 };
249 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
250 
251 GlobalProperty hw_compat_2_2[] = {};
252 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
253 
254 GlobalProperty hw_compat_2_1[] = {
255     { "intel-hda", "old_msi_addr", "on" },
256     { "VGA", "qemu-extended-regs", "off" },
257     { "secondary-vga", "qemu-extended-regs", "off" },
258     { "virtio-scsi-pci", "any_layout", "off" },
259     { "usb-mouse", "usb_version", "1" },
260     { "usb-kbd", "usb_version", "1" },
261     { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
262 };
263 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
264 
265 MachineState *current_machine;
266 
267 static char *machine_get_kernel(Object *obj, Error **errp)
268 {
269     MachineState *ms = MACHINE(obj);
270 
271     return g_strdup(ms->kernel_filename);
272 }
273 
274 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
275 {
276     MachineState *ms = MACHINE(obj);
277 
278     g_free(ms->kernel_filename);
279     ms->kernel_filename = g_strdup(value);
280 }
281 
282 static char *machine_get_initrd(Object *obj, Error **errp)
283 {
284     MachineState *ms = MACHINE(obj);
285 
286     return g_strdup(ms->initrd_filename);
287 }
288 
289 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
290 {
291     MachineState *ms = MACHINE(obj);
292 
293     g_free(ms->initrd_filename);
294     ms->initrd_filename = g_strdup(value);
295 }
296 
297 static char *machine_get_append(Object *obj, Error **errp)
298 {
299     MachineState *ms = MACHINE(obj);
300 
301     return g_strdup(ms->kernel_cmdline);
302 }
303 
304 static void machine_set_append(Object *obj, const char *value, Error **errp)
305 {
306     MachineState *ms = MACHINE(obj);
307 
308     g_free(ms->kernel_cmdline);
309     ms->kernel_cmdline = g_strdup(value);
310 }
311 
312 static char *machine_get_dtb(Object *obj, Error **errp)
313 {
314     MachineState *ms = MACHINE(obj);
315 
316     return g_strdup(ms->dtb);
317 }
318 
319 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
320 {
321     MachineState *ms = MACHINE(obj);
322 
323     g_free(ms->dtb);
324     ms->dtb = g_strdup(value);
325 }
326 
327 static char *machine_get_dumpdtb(Object *obj, Error **errp)
328 {
329     MachineState *ms = MACHINE(obj);
330 
331     return g_strdup(ms->dumpdtb);
332 }
333 
334 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
335 {
336     MachineState *ms = MACHINE(obj);
337 
338     g_free(ms->dumpdtb);
339     ms->dumpdtb = g_strdup(value);
340 }
341 
342 static void machine_get_phandle_start(Object *obj, Visitor *v,
343                                       const char *name, void *opaque,
344                                       Error **errp)
345 {
346     MachineState *ms = MACHINE(obj);
347     int64_t value = ms->phandle_start;
348 
349     visit_type_int(v, name, &value, errp);
350 }
351 
352 static void machine_set_phandle_start(Object *obj, Visitor *v,
353                                       const char *name, void *opaque,
354                                       Error **errp)
355 {
356     MachineState *ms = MACHINE(obj);
357     int64_t value;
358 
359     if (!visit_type_int(v, name, &value, errp)) {
360         return;
361     }
362 
363     ms->phandle_start = value;
364 }
365 
366 static char *machine_get_dt_compatible(Object *obj, Error **errp)
367 {
368     MachineState *ms = MACHINE(obj);
369 
370     return g_strdup(ms->dt_compatible);
371 }
372 
373 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
374 {
375     MachineState *ms = MACHINE(obj);
376 
377     g_free(ms->dt_compatible);
378     ms->dt_compatible = g_strdup(value);
379 }
380 
381 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
382 {
383     MachineState *ms = MACHINE(obj);
384 
385     return ms->dump_guest_core;
386 }
387 
388 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
389 {
390     MachineState *ms = MACHINE(obj);
391 
392     ms->dump_guest_core = value;
393 }
394 
395 static bool machine_get_mem_merge(Object *obj, Error **errp)
396 {
397     MachineState *ms = MACHINE(obj);
398 
399     return ms->mem_merge;
400 }
401 
402 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
403 {
404     MachineState *ms = MACHINE(obj);
405 
406     ms->mem_merge = value;
407 }
408 
409 static bool machine_get_usb(Object *obj, Error **errp)
410 {
411     MachineState *ms = MACHINE(obj);
412 
413     return ms->usb;
414 }
415 
416 static void machine_set_usb(Object *obj, bool value, Error **errp)
417 {
418     MachineState *ms = MACHINE(obj);
419 
420     ms->usb = value;
421     ms->usb_disabled = !value;
422 }
423 
424 static bool machine_get_graphics(Object *obj, Error **errp)
425 {
426     MachineState *ms = MACHINE(obj);
427 
428     return ms->enable_graphics;
429 }
430 
431 static void machine_set_graphics(Object *obj, bool value, Error **errp)
432 {
433     MachineState *ms = MACHINE(obj);
434 
435     ms->enable_graphics = value;
436 }
437 
438 static char *machine_get_firmware(Object *obj, Error **errp)
439 {
440     MachineState *ms = MACHINE(obj);
441 
442     return g_strdup(ms->firmware);
443 }
444 
445 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
446 {
447     MachineState *ms = MACHINE(obj);
448 
449     g_free(ms->firmware);
450     ms->firmware = g_strdup(value);
451 }
452 
453 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
454 {
455     MachineState *ms = MACHINE(obj);
456 
457     ms->suppress_vmdesc = value;
458 }
459 
460 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
461 {
462     MachineState *ms = MACHINE(obj);
463 
464     return ms->suppress_vmdesc;
465 }
466 
467 static char *machine_get_memory_encryption(Object *obj, Error **errp)
468 {
469     MachineState *ms = MACHINE(obj);
470 
471     if (ms->cgs) {
472         return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
473     }
474 
475     return NULL;
476 }
477 
478 static void machine_set_memory_encryption(Object *obj, const char *value,
479                                         Error **errp)
480 {
481     Object *cgs =
482         object_resolve_path_component(object_get_objects_root(), value);
483 
484     if (!cgs) {
485         error_setg(errp, "No such memory encryption object '%s'", value);
486         return;
487     }
488 
489     object_property_set_link(obj, "confidential-guest-support", cgs, errp);
490 }
491 
492 static void machine_check_confidential_guest_support(const Object *obj,
493                                                      const char *name,
494                                                      Object *new_target,
495                                                      Error **errp)
496 {
497     /*
498      * So far the only constraint is that the target has the
499      * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
500      * by the QOM core
501      */
502 }
503 
504 static bool machine_get_nvdimm(Object *obj, Error **errp)
505 {
506     MachineState *ms = MACHINE(obj);
507 
508     return ms->nvdimms_state->is_enabled;
509 }
510 
511 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
512 {
513     MachineState *ms = MACHINE(obj);
514 
515     ms->nvdimms_state->is_enabled = value;
516 }
517 
518 static bool machine_get_hmat(Object *obj, Error **errp)
519 {
520     MachineState *ms = MACHINE(obj);
521 
522     return ms->numa_state->hmat_enabled;
523 }
524 
525 static void machine_set_hmat(Object *obj, bool value, Error **errp)
526 {
527     MachineState *ms = MACHINE(obj);
528 
529     ms->numa_state->hmat_enabled = value;
530 }
531 
532 static void machine_get_mem(Object *obj, Visitor *v, const char *name,
533                             void *opaque, Error **errp)
534 {
535     MachineState *ms = MACHINE(obj);
536     MemorySizeConfiguration mem = {
537         .has_size = true,
538         .size = ms->ram_size,
539         .has_max_size = !!ms->ram_slots,
540         .max_size = ms->maxram_size,
541         .has_slots = !!ms->ram_slots,
542         .slots = ms->ram_slots,
543     };
544     MemorySizeConfiguration *p_mem = &mem;
545 
546     visit_type_MemorySizeConfiguration(v, name, &p_mem, &error_abort);
547 }
548 
549 static void machine_set_mem(Object *obj, Visitor *v, const char *name,
550                             void *opaque, Error **errp)
551 {
552     MachineState *ms = MACHINE(obj);
553     MachineClass *mc = MACHINE_GET_CLASS(obj);
554     MemorySizeConfiguration *mem;
555 
556     ERRP_GUARD();
557 
558     if (!visit_type_MemorySizeConfiguration(v, name, &mem, errp)) {
559         return;
560     }
561 
562     if (!mem->has_size) {
563         mem->has_size = true;
564         mem->size = mc->default_ram_size;
565     }
566     mem->size = QEMU_ALIGN_UP(mem->size, 8192);
567     if (mc->fixup_ram_size) {
568         mem->size = mc->fixup_ram_size(mem->size);
569     }
570     if ((ram_addr_t)mem->size != mem->size) {
571         error_setg(errp, "ram size too large");
572         goto out_free;
573     }
574 
575     if (mem->has_max_size) {
576         if (mem->max_size < mem->size) {
577             error_setg(errp, "invalid value of maxmem: "
578                        "maximum memory size (0x%" PRIx64 ") must be at least "
579                        "the initial memory size (0x%" PRIx64 ")",
580                        mem->max_size, mem->size);
581             goto out_free;
582         }
583         if (mem->has_slots && mem->slots && mem->max_size == mem->size) {
584             error_setg(errp, "invalid value of maxmem: "
585                        "memory slots were specified but maximum memory size "
586                        "(0x%" PRIx64 ") is equal to the initial memory size "
587                        "(0x%" PRIx64 ")", mem->max_size, mem->size);
588             goto out_free;
589         }
590         ms->maxram_size = mem->max_size;
591     } else {
592         if (mem->has_slots) {
593             error_setg(errp, "slots specified but no max-size");
594             goto out_free;
595         }
596         ms->maxram_size = mem->size;
597     }
598     ms->ram_size = mem->size;
599     ms->ram_slots = mem->has_slots ? mem->slots : 0;
600 out_free:
601     qapi_free_MemorySizeConfiguration(mem);
602 }
603 
604 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
605 {
606     MachineState *ms = MACHINE(obj);
607 
608     return g_strdup(ms->nvdimms_state->persistence_string);
609 }
610 
611 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
612                                            Error **errp)
613 {
614     MachineState *ms = MACHINE(obj);
615     NVDIMMState *nvdimms_state = ms->nvdimms_state;
616 
617     if (strcmp(value, "cpu") == 0) {
618         nvdimms_state->persistence = 3;
619     } else if (strcmp(value, "mem-ctrl") == 0) {
620         nvdimms_state->persistence = 2;
621     } else {
622         error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
623                    value);
624         return;
625     }
626 
627     g_free(nvdimms_state->persistence_string);
628     nvdimms_state->persistence_string = g_strdup(value);
629 }
630 
631 static bool machine_get_cxl(Object *obj, Error **errp)
632 {
633     MachineState *ms = MACHINE(obj);
634 
635     return ms->cxl_devices_state->is_enabled;
636 }
637 
638 static void machine_set_cxl(Object *obj, bool value, Error **errp)
639 {
640     MachineState *ms = MACHINE(obj);
641 
642     ms->cxl_devices_state->is_enabled = value;
643 }
644 
645 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
646 {
647     QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
648 }
649 
650 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
651 {
652     Object *obj = OBJECT(dev);
653 
654     if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
655         return false;
656     }
657 
658     return device_type_is_dynamic_sysbus(mc, object_get_typename(obj));
659 }
660 
661 bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type)
662 {
663     bool allowed = false;
664     strList *wl;
665     ObjectClass *klass = object_class_by_name(type);
666 
667     for (wl = mc->allowed_dynamic_sysbus_devices;
668          !allowed && wl;
669          wl = wl->next) {
670         allowed |= !!object_class_dynamic_cast(klass, wl->value);
671     }
672 
673     return allowed;
674 }
675 
676 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
677 {
678     int i;
679     HotpluggableCPUList *head = NULL;
680     MachineClass *mc = MACHINE_GET_CLASS(machine);
681 
682     /* force board to initialize possible_cpus if it hasn't been done yet */
683     mc->possible_cpu_arch_ids(machine);
684 
685     for (i = 0; i < machine->possible_cpus->len; i++) {
686         Object *cpu;
687         HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
688 
689         cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
690         cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
691         cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
692                                    sizeof(*cpu_item->props));
693 
694         cpu = machine->possible_cpus->cpus[i].cpu;
695         if (cpu) {
696             cpu_item->has_qom_path = true;
697             cpu_item->qom_path = object_get_canonical_path(cpu);
698         }
699         QAPI_LIST_PREPEND(head, cpu_item);
700     }
701     return head;
702 }
703 
704 /**
705  * machine_set_cpu_numa_node:
706  * @machine: machine object to modify
707  * @props: specifies which cpu objects to assign to
708  *         numa node specified by @props.node_id
709  * @errp: if an error occurs, a pointer to an area to store the error
710  *
711  * Associate NUMA node specified by @props.node_id with cpu slots that
712  * match socket/core/thread-ids specified by @props. It's recommended to use
713  * query-hotpluggable-cpus.props values to specify affected cpu slots,
714  * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
715  *
716  * However for CLI convenience it's possible to pass in subset of properties,
717  * which would affect all cpu slots that match it.
718  * Ex for pc machine:
719  *    -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
720  *    -numa cpu,node-id=0,socket_id=0 \
721  *    -numa cpu,node-id=1,socket_id=1
722  * will assign all child cores of socket 0 to node 0 and
723  * of socket 1 to node 1.
724  *
725  * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
726  * return error.
727  * Empty subset is disallowed and function will return with error in this case.
728  */
729 void machine_set_cpu_numa_node(MachineState *machine,
730                                const CpuInstanceProperties *props, Error **errp)
731 {
732     MachineClass *mc = MACHINE_GET_CLASS(machine);
733     NodeInfo *numa_info = machine->numa_state->nodes;
734     bool match = false;
735     int i;
736 
737     if (!mc->possible_cpu_arch_ids) {
738         error_setg(errp, "mapping of CPUs to NUMA node is not supported");
739         return;
740     }
741 
742     /* disabling node mapping is not supported, forbid it */
743     assert(props->has_node_id);
744 
745     /* force board to initialize possible_cpus if it hasn't been done yet */
746     mc->possible_cpu_arch_ids(machine);
747 
748     for (i = 0; i < machine->possible_cpus->len; i++) {
749         CPUArchId *slot = &machine->possible_cpus->cpus[i];
750 
751         /* reject unsupported by board properties */
752         if (props->has_thread_id && !slot->props.has_thread_id) {
753             error_setg(errp, "thread-id is not supported");
754             return;
755         }
756 
757         if (props->has_core_id && !slot->props.has_core_id) {
758             error_setg(errp, "core-id is not supported");
759             return;
760         }
761 
762         if (props->has_cluster_id && !slot->props.has_cluster_id) {
763             error_setg(errp, "cluster-id is not supported");
764             return;
765         }
766 
767         if (props->has_socket_id && !slot->props.has_socket_id) {
768             error_setg(errp, "socket-id is not supported");
769             return;
770         }
771 
772         if (props->has_die_id && !slot->props.has_die_id) {
773             error_setg(errp, "die-id is not supported");
774             return;
775         }
776 
777         /* skip slots with explicit mismatch */
778         if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
779                 continue;
780         }
781 
782         if (props->has_core_id && props->core_id != slot->props.core_id) {
783                 continue;
784         }
785 
786         if (props->has_cluster_id &&
787             props->cluster_id != slot->props.cluster_id) {
788                 continue;
789         }
790 
791         if (props->has_die_id && props->die_id != slot->props.die_id) {
792                 continue;
793         }
794 
795         if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
796                 continue;
797         }
798 
799         /* reject assignment if slot is already assigned, for compatibility
800          * of legacy cpu_index mapping with SPAPR core based mapping do not
801          * error out if cpu thread and matched core have the same node-id */
802         if (slot->props.has_node_id &&
803             slot->props.node_id != props->node_id) {
804             error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
805                        slot->props.node_id);
806             return;
807         }
808 
809         /* assign slot to node as it's matched '-numa cpu' key */
810         match = true;
811         slot->props.node_id = props->node_id;
812         slot->props.has_node_id = props->has_node_id;
813 
814         if (machine->numa_state->hmat_enabled) {
815             if ((numa_info[props->node_id].initiator < MAX_NODES) &&
816                 (props->node_id != numa_info[props->node_id].initiator)) {
817                 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
818                            " should be itself (got %" PRIu16 ")",
819                            props->node_id, numa_info[props->node_id].initiator);
820                 return;
821             }
822             numa_info[props->node_id].has_cpu = true;
823             numa_info[props->node_id].initiator = props->node_id;
824         }
825     }
826 
827     if (!match) {
828         error_setg(errp, "no match found");
829     }
830 }
831 
832 static void machine_get_smp(Object *obj, Visitor *v, const char *name,
833                             void *opaque, Error **errp)
834 {
835     MachineState *ms = MACHINE(obj);
836     SMPConfiguration *config = &(SMPConfiguration){
837         .has_cpus = true, .cpus = ms->smp.cpus,
838         .has_sockets = true, .sockets = ms->smp.sockets,
839         .has_dies = true, .dies = ms->smp.dies,
840         .has_clusters = true, .clusters = ms->smp.clusters,
841         .has_cores = true, .cores = ms->smp.cores,
842         .has_threads = true, .threads = ms->smp.threads,
843         .has_maxcpus = true, .maxcpus = ms->smp.max_cpus,
844     };
845 
846     if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) {
847         return;
848     }
849 }
850 
851 static void machine_set_smp(Object *obj, Visitor *v, const char *name,
852                             void *opaque, Error **errp)
853 {
854     MachineState *ms = MACHINE(obj);
855     g_autoptr(SMPConfiguration) config = NULL;
856 
857     if (!visit_type_SMPConfiguration(v, name, &config, errp)) {
858         return;
859     }
860 
861     machine_parse_smp_config(ms, config, errp);
862 }
863 
864 static void machine_get_boot(Object *obj, Visitor *v, const char *name,
865                             void *opaque, Error **errp)
866 {
867     MachineState *ms = MACHINE(obj);
868     BootConfiguration *config = &ms->boot_config;
869     visit_type_BootConfiguration(v, name, &config, &error_abort);
870 }
871 
872 static void machine_free_boot_config(MachineState *ms)
873 {
874     g_free(ms->boot_config.order);
875     g_free(ms->boot_config.once);
876     g_free(ms->boot_config.splash);
877 }
878 
879 static void machine_copy_boot_config(MachineState *ms, BootConfiguration *config)
880 {
881     MachineClass *machine_class = MACHINE_GET_CLASS(ms);
882 
883     machine_free_boot_config(ms);
884     ms->boot_config = *config;
885     if (!config->has_order) {
886         ms->boot_config.has_order = true;
887         ms->boot_config.order = g_strdup(machine_class->default_boot_order);
888     }
889 }
890 
891 static void machine_set_boot(Object *obj, Visitor *v, const char *name,
892                             void *opaque, Error **errp)
893 {
894     ERRP_GUARD();
895     MachineState *ms = MACHINE(obj);
896     BootConfiguration *config = NULL;
897 
898     if (!visit_type_BootConfiguration(v, name, &config, errp)) {
899         return;
900     }
901     if (config->has_order) {
902         validate_bootdevices(config->order, errp);
903         if (*errp) {
904             goto out_free;
905         }
906     }
907     if (config->has_once) {
908         validate_bootdevices(config->once, errp);
909         if (*errp) {
910             goto out_free;
911         }
912     }
913 
914     machine_copy_boot_config(ms, config);
915     /* Strings live in ms->boot_config.  */
916     free(config);
917     return;
918 
919 out_free:
920     qapi_free_BootConfiguration(config);
921 }
922 
923 static void machine_class_init(ObjectClass *oc, void *data)
924 {
925     MachineClass *mc = MACHINE_CLASS(oc);
926 
927     /* Default 128 MB as guest ram size */
928     mc->default_ram_size = 128 * MiB;
929     mc->rom_file_has_mr = true;
930 
931     /* Few machines support CXL, so default to off */
932     mc->cxl_supported = false;
933     /* numa node memory size aligned on 8MB by default.
934      * On Linux, each node's border has to be 8MB aligned
935      */
936     mc->numa_mem_align_shift = 23;
937 
938     object_class_property_add_str(oc, "kernel",
939         machine_get_kernel, machine_set_kernel);
940     object_class_property_set_description(oc, "kernel",
941         "Linux kernel image file");
942 
943     object_class_property_add_str(oc, "initrd",
944         machine_get_initrd, machine_set_initrd);
945     object_class_property_set_description(oc, "initrd",
946         "Linux initial ramdisk file");
947 
948     object_class_property_add_str(oc, "append",
949         machine_get_append, machine_set_append);
950     object_class_property_set_description(oc, "append",
951         "Linux kernel command line");
952 
953     object_class_property_add_str(oc, "dtb",
954         machine_get_dtb, machine_set_dtb);
955     object_class_property_set_description(oc, "dtb",
956         "Linux kernel device tree file");
957 
958     object_class_property_add_str(oc, "dumpdtb",
959         machine_get_dumpdtb, machine_set_dumpdtb);
960     object_class_property_set_description(oc, "dumpdtb",
961         "Dump current dtb to a file and quit");
962 
963     object_class_property_add(oc, "boot", "BootConfiguration",
964         machine_get_boot, machine_set_boot,
965         NULL, NULL);
966     object_class_property_set_description(oc, "boot",
967         "Boot configuration");
968 
969     object_class_property_add(oc, "smp", "SMPConfiguration",
970         machine_get_smp, machine_set_smp,
971         NULL, NULL);
972     object_class_property_set_description(oc, "smp",
973         "CPU topology");
974 
975     object_class_property_add(oc, "phandle-start", "int",
976         machine_get_phandle_start, machine_set_phandle_start,
977         NULL, NULL);
978     object_class_property_set_description(oc, "phandle-start",
979         "The first phandle ID we may generate dynamically");
980 
981     object_class_property_add_str(oc, "dt-compatible",
982         machine_get_dt_compatible, machine_set_dt_compatible);
983     object_class_property_set_description(oc, "dt-compatible",
984         "Overrides the \"compatible\" property of the dt root node");
985 
986     object_class_property_add_bool(oc, "dump-guest-core",
987         machine_get_dump_guest_core, machine_set_dump_guest_core);
988     object_class_property_set_description(oc, "dump-guest-core",
989         "Include guest memory in a core dump");
990 
991     object_class_property_add_bool(oc, "mem-merge",
992         machine_get_mem_merge, machine_set_mem_merge);
993     object_class_property_set_description(oc, "mem-merge",
994         "Enable/disable memory merge support");
995 
996     object_class_property_add_bool(oc, "usb",
997         machine_get_usb, machine_set_usb);
998     object_class_property_set_description(oc, "usb",
999         "Set on/off to enable/disable usb");
1000 
1001     object_class_property_add_bool(oc, "graphics",
1002         machine_get_graphics, machine_set_graphics);
1003     object_class_property_set_description(oc, "graphics",
1004         "Set on/off to enable/disable graphics emulation");
1005 
1006     object_class_property_add_str(oc, "firmware",
1007         machine_get_firmware, machine_set_firmware);
1008     object_class_property_set_description(oc, "firmware",
1009         "Firmware image");
1010 
1011     object_class_property_add_bool(oc, "suppress-vmdesc",
1012         machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
1013     object_class_property_set_description(oc, "suppress-vmdesc",
1014         "Set on to disable self-describing migration");
1015 
1016     object_class_property_add_link(oc, "confidential-guest-support",
1017                                    TYPE_CONFIDENTIAL_GUEST_SUPPORT,
1018                                    offsetof(MachineState, cgs),
1019                                    machine_check_confidential_guest_support,
1020                                    OBJ_PROP_LINK_STRONG);
1021     object_class_property_set_description(oc, "confidential-guest-support",
1022                                           "Set confidential guest scheme to support");
1023 
1024     /* For compatibility */
1025     object_class_property_add_str(oc, "memory-encryption",
1026         machine_get_memory_encryption, machine_set_memory_encryption);
1027     object_class_property_set_description(oc, "memory-encryption",
1028         "Set memory encryption object to use");
1029 
1030     object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKEND,
1031                                    offsetof(MachineState, memdev), object_property_allow_set_link,
1032                                    OBJ_PROP_LINK_STRONG);
1033     object_class_property_set_description(oc, "memory-backend",
1034                                           "Set RAM backend"
1035                                           "Valid value is ID of hostmem based backend");
1036 
1037     object_class_property_add(oc, "memory", "MemorySizeConfiguration",
1038         machine_get_mem, machine_set_mem,
1039         NULL, NULL);
1040     object_class_property_set_description(oc, "memory",
1041         "Memory size configuration");
1042 }
1043 
1044 static void machine_class_base_init(ObjectClass *oc, void *data)
1045 {
1046     MachineClass *mc = MACHINE_CLASS(oc);
1047     mc->max_cpus = mc->max_cpus ?: 1;
1048     mc->min_cpus = mc->min_cpus ?: 1;
1049     mc->default_cpus = mc->default_cpus ?: 1;
1050 
1051     if (!object_class_is_abstract(oc)) {
1052         const char *cname = object_class_get_name(oc);
1053         assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
1054         mc->name = g_strndup(cname,
1055                             strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
1056         mc->compat_props = g_ptr_array_new();
1057     }
1058 }
1059 
1060 static void machine_initfn(Object *obj)
1061 {
1062     MachineState *ms = MACHINE(obj);
1063     MachineClass *mc = MACHINE_GET_CLASS(obj);
1064 
1065     container_get(obj, "/peripheral");
1066     container_get(obj, "/peripheral-anon");
1067 
1068     ms->dump_guest_core = true;
1069     ms->mem_merge = true;
1070     ms->enable_graphics = true;
1071     ms->kernel_cmdline = g_strdup("");
1072     ms->ram_size = mc->default_ram_size;
1073     ms->maxram_size = mc->default_ram_size;
1074 
1075     if (mc->nvdimm_supported) {
1076         Object *obj = OBJECT(ms);
1077 
1078         ms->nvdimms_state = g_new0(NVDIMMState, 1);
1079         object_property_add_bool(obj, "nvdimm",
1080                                  machine_get_nvdimm, machine_set_nvdimm);
1081         object_property_set_description(obj, "nvdimm",
1082                                         "Set on/off to enable/disable "
1083                                         "NVDIMM instantiation");
1084 
1085         object_property_add_str(obj, "nvdimm-persistence",
1086                                 machine_get_nvdimm_persistence,
1087                                 machine_set_nvdimm_persistence);
1088         object_property_set_description(obj, "nvdimm-persistence",
1089                                         "Set NVDIMM persistence"
1090                                         "Valid values are cpu, mem-ctrl");
1091     }
1092 
1093     if (mc->cxl_supported) {
1094         Object *obj = OBJECT(ms);
1095 
1096         ms->cxl_devices_state = g_new0(CXLState, 1);
1097         object_property_add_bool(obj, "cxl", machine_get_cxl, machine_set_cxl);
1098         object_property_set_description(obj, "cxl",
1099                                         "Set on/off to enable/disable "
1100                                         "CXL instantiation");
1101     }
1102 
1103     if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
1104         ms->numa_state = g_new0(NumaState, 1);
1105         object_property_add_bool(obj, "hmat",
1106                                  machine_get_hmat, machine_set_hmat);
1107         object_property_set_description(obj, "hmat",
1108                                         "Set on/off to enable/disable "
1109                                         "ACPI Heterogeneous Memory Attribute "
1110                                         "Table (HMAT)");
1111     }
1112 
1113     /* default to mc->default_cpus */
1114     ms->smp.cpus = mc->default_cpus;
1115     ms->smp.max_cpus = mc->default_cpus;
1116     ms->smp.sockets = 1;
1117     ms->smp.dies = 1;
1118     ms->smp.clusters = 1;
1119     ms->smp.cores = 1;
1120     ms->smp.threads = 1;
1121 
1122     machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
1123 }
1124 
1125 static void machine_finalize(Object *obj)
1126 {
1127     MachineState *ms = MACHINE(obj);
1128 
1129     machine_free_boot_config(ms);
1130     g_free(ms->kernel_filename);
1131     g_free(ms->initrd_filename);
1132     g_free(ms->kernel_cmdline);
1133     g_free(ms->dtb);
1134     g_free(ms->dumpdtb);
1135     g_free(ms->dt_compatible);
1136     g_free(ms->firmware);
1137     g_free(ms->device_memory);
1138     g_free(ms->nvdimms_state);
1139     g_free(ms->numa_state);
1140     g_free(ms->cxl_devices_state);
1141 }
1142 
1143 bool machine_usb(MachineState *machine)
1144 {
1145     return machine->usb;
1146 }
1147 
1148 int machine_phandle_start(MachineState *machine)
1149 {
1150     return machine->phandle_start;
1151 }
1152 
1153 bool machine_dump_guest_core(MachineState *machine)
1154 {
1155     return machine->dump_guest_core;
1156 }
1157 
1158 bool machine_mem_merge(MachineState *machine)
1159 {
1160     return machine->mem_merge;
1161 }
1162 
1163 static char *cpu_slot_to_string(const CPUArchId *cpu)
1164 {
1165     GString *s = g_string_new(NULL);
1166     if (cpu->props.has_socket_id) {
1167         g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1168     }
1169     if (cpu->props.has_die_id) {
1170         if (s->len) {
1171             g_string_append_printf(s, ", ");
1172         }
1173         g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1174     }
1175     if (cpu->props.has_cluster_id) {
1176         if (s->len) {
1177             g_string_append_printf(s, ", ");
1178         }
1179         g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
1180     }
1181     if (cpu->props.has_core_id) {
1182         if (s->len) {
1183             g_string_append_printf(s, ", ");
1184         }
1185         g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1186     }
1187     if (cpu->props.has_thread_id) {
1188         if (s->len) {
1189             g_string_append_printf(s, ", ");
1190         }
1191         g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1192     }
1193     return g_string_free(s, false);
1194 }
1195 
1196 static void numa_validate_initiator(NumaState *numa_state)
1197 {
1198     int i;
1199     NodeInfo *numa_info = numa_state->nodes;
1200 
1201     for (i = 0; i < numa_state->num_nodes; i++) {
1202         if (numa_info[i].initiator == MAX_NODES) {
1203             error_report("The initiator of NUMA node %d is missing, use "
1204                          "'-numa node,initiator' option to declare it", i);
1205             exit(1);
1206         }
1207 
1208         if (!numa_info[numa_info[i].initiator].present) {
1209             error_report("NUMA node %" PRIu16 " is missing, use "
1210                          "'-numa node' option to declare it first",
1211                          numa_info[i].initiator);
1212             exit(1);
1213         }
1214 
1215         if (!numa_info[numa_info[i].initiator].has_cpu) {
1216             error_report("The initiator of NUMA node %d is invalid", i);
1217             exit(1);
1218         }
1219     }
1220 }
1221 
1222 static void machine_numa_finish_cpu_init(MachineState *machine)
1223 {
1224     int i;
1225     bool default_mapping;
1226     GString *s = g_string_new(NULL);
1227     MachineClass *mc = MACHINE_GET_CLASS(machine);
1228     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1229 
1230     assert(machine->numa_state->num_nodes);
1231     for (i = 0; i < possible_cpus->len; i++) {
1232         if (possible_cpus->cpus[i].props.has_node_id) {
1233             break;
1234         }
1235     }
1236     default_mapping = (i == possible_cpus->len);
1237 
1238     for (i = 0; i < possible_cpus->len; i++) {
1239         const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1240 
1241         if (!cpu_slot->props.has_node_id) {
1242             /* fetch default mapping from board and enable it */
1243             CpuInstanceProperties props = cpu_slot->props;
1244 
1245             props.node_id = mc->get_default_cpu_node_id(machine, i);
1246             if (!default_mapping) {
1247                 /* record slots with not set mapping,
1248                  * TODO: make it hard error in future */
1249                 char *cpu_str = cpu_slot_to_string(cpu_slot);
1250                 g_string_append_printf(s, "%sCPU %d [%s]",
1251                                        s->len ? ", " : "", i, cpu_str);
1252                 g_free(cpu_str);
1253 
1254                 /* non mapped cpus used to fallback to node 0 */
1255                 props.node_id = 0;
1256             }
1257 
1258             props.has_node_id = true;
1259             machine_set_cpu_numa_node(machine, &props, &error_fatal);
1260         }
1261     }
1262 
1263     if (machine->numa_state->hmat_enabled) {
1264         numa_validate_initiator(machine->numa_state);
1265     }
1266 
1267     if (s->len && !qtest_enabled()) {
1268         warn_report("CPU(s) not present in any NUMA nodes: %s",
1269                     s->str);
1270         warn_report("All CPU(s) up to maxcpus should be described "
1271                     "in NUMA config, ability to start up with partial NUMA "
1272                     "mappings is obsoleted and will be removed in future");
1273     }
1274     g_string_free(s, true);
1275 }
1276 
1277 MemoryRegion *machine_consume_memdev(MachineState *machine,
1278                                      HostMemoryBackend *backend)
1279 {
1280     MemoryRegion *ret = host_memory_backend_get_memory(backend);
1281 
1282     if (host_memory_backend_is_mapped(backend)) {
1283         error_report("memory backend %s can't be used multiple times.",
1284                      object_get_canonical_path_component(OBJECT(backend)));
1285         exit(EXIT_FAILURE);
1286     }
1287     host_memory_backend_set_mapped(backend, true);
1288     vmstate_register_ram_global(ret);
1289     return ret;
1290 }
1291 
1292 static bool create_default_memdev(MachineState *ms, const char *path, Error **errp)
1293 {
1294     Object *obj;
1295     MachineClass *mc = MACHINE_GET_CLASS(ms);
1296     bool r = false;
1297 
1298     obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
1299     if (path) {
1300         if (!object_property_set_str(obj, "mem-path", path, errp)) {
1301             goto out;
1302         }
1303     }
1304     if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
1305         goto out;
1306     }
1307     object_property_add_child(object_get_objects_root(), mc->default_ram_id,
1308                               obj);
1309     /* Ensure backend's memory region name is equal to mc->default_ram_id */
1310     if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
1311                              false, errp)) {
1312         goto out;
1313     }
1314     if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
1315         goto out;
1316     }
1317     r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
1318 
1319 out:
1320     object_unref(obj);
1321     return r;
1322 }
1323 
1324 
1325 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
1326 {
1327     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1328     ObjectClass *oc = object_class_by_name(machine->cpu_type);
1329     CPUClass *cc;
1330 
1331     /* This checkpoint is required by replay to separate prior clock
1332        reading from the other reads, because timer polling functions query
1333        clock values from the log. */
1334     replay_checkpoint(CHECKPOINT_INIT);
1335 
1336     if (!xen_enabled()) {
1337         /* On 32-bit hosts, QEMU is limited by virtual address space */
1338         if (machine->ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1339             error_setg(errp, "at most 2047 MB RAM can be simulated");
1340             return;
1341         }
1342     }
1343 
1344     if (machine->memdev) {
1345         ram_addr_t backend_size = object_property_get_uint(OBJECT(machine->memdev),
1346                                                            "size",  &error_abort);
1347         if (backend_size != machine->ram_size) {
1348             error_setg(errp, "Machine memory size does not match the size of the memory backend");
1349             return;
1350         }
1351     } else if (machine_class->default_ram_id && machine->ram_size &&
1352                numa_uses_legacy_mem()) {
1353         if (!create_default_memdev(current_machine, mem_path, errp)) {
1354             return;
1355         }
1356     }
1357 
1358     if (machine->numa_state) {
1359         numa_complete_configuration(machine);
1360         if (machine->numa_state->num_nodes) {
1361             machine_numa_finish_cpu_init(machine);
1362         }
1363     }
1364 
1365     if (!machine->ram && machine->memdev) {
1366         machine->ram = machine_consume_memdev(machine, machine->memdev);
1367     }
1368 
1369     /* If the machine supports the valid_cpu_types check and the user
1370      * specified a CPU with -cpu check here that the user CPU is supported.
1371      */
1372     if (machine_class->valid_cpu_types && machine->cpu_type) {
1373         int i;
1374 
1375         for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1376             if (object_class_dynamic_cast(oc,
1377                                           machine_class->valid_cpu_types[i])) {
1378                 /* The user specificed CPU is in the valid field, we are
1379                  * good to go.
1380                  */
1381                 break;
1382             }
1383         }
1384 
1385         if (!machine_class->valid_cpu_types[i]) {
1386             /* The user specified CPU is not valid */
1387             error_report("Invalid CPU type: %s", machine->cpu_type);
1388             error_printf("The valid types are: %s",
1389                          machine_class->valid_cpu_types[0]);
1390             for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1391                 error_printf(", %s", machine_class->valid_cpu_types[i]);
1392             }
1393             error_printf("\n");
1394 
1395             exit(1);
1396         }
1397     }
1398 
1399     /* Check if CPU type is deprecated and warn if so */
1400     cc = CPU_CLASS(oc);
1401     if (cc && cc->deprecation_note) {
1402         warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1403                     cc->deprecation_note);
1404     }
1405 
1406     if (machine->cgs) {
1407         /*
1408          * With confidential guests, the host can't see the real
1409          * contents of RAM, so there's no point in it trying to merge
1410          * areas.
1411          */
1412         machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1413 
1414         /*
1415          * Virtio devices can't count on directly accessing guest
1416          * memory, so they need iommu_platform=on to use normal DMA
1417          * mechanisms.  That requires also disabling legacy virtio
1418          * support for those virtio pci devices which allow it.
1419          */
1420         object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1421                                    "on", true);
1422         object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1423                                    "on", false);
1424     }
1425 
1426     accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1427     machine_class->init(machine);
1428     phase_advance(PHASE_MACHINE_INITIALIZED);
1429 }
1430 
1431 static NotifierList machine_init_done_notifiers =
1432     NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1433 
1434 void qemu_add_machine_init_done_notifier(Notifier *notify)
1435 {
1436     notifier_list_add(&machine_init_done_notifiers, notify);
1437     if (phase_check(PHASE_MACHINE_READY)) {
1438         notify->notify(notify, NULL);
1439     }
1440 }
1441 
1442 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1443 {
1444     notifier_remove(notify);
1445 }
1446 
1447 void qdev_machine_creation_done(void)
1448 {
1449     cpu_synchronize_all_post_init();
1450 
1451     if (current_machine->boot_config.has_once) {
1452         qemu_boot_set(current_machine->boot_config.once, &error_fatal);
1453         qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_config.order));
1454     }
1455 
1456     /*
1457      * ok, initial machine setup is done, starting from now we can
1458      * only create hotpluggable devices
1459      */
1460     phase_advance(PHASE_MACHINE_READY);
1461     qdev_assert_realized_properly();
1462 
1463     /* TODO: once all bus devices are qdevified, this should be done
1464      * when bus is created by qdev.c */
1465     /*
1466      * TODO: If we had a main 'reset container' that the whole system
1467      * lived in, we could reset that using the multi-phase reset
1468      * APIs. For the moment, we just reset the sysbus, which will cause
1469      * all devices hanging off it (and all their child buses, recursively)
1470      * to be reset. Note that this will *not* reset any Device objects
1471      * which are not attached to some part of the qbus tree!
1472      */
1473     qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1474 
1475     notifier_list_notify(&machine_init_done_notifiers, NULL);
1476 
1477     if (rom_check_and_register_reset() != 0) {
1478         exit(1);
1479     }
1480 
1481     replay_start();
1482 
1483     /* This checkpoint is required by replay to separate prior clock
1484        reading from the other reads, because timer polling functions query
1485        clock values from the log. */
1486     replay_checkpoint(CHECKPOINT_RESET);
1487     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1488     register_global_state();
1489 }
1490 
1491 static const TypeInfo machine_info = {
1492     .name = TYPE_MACHINE,
1493     .parent = TYPE_OBJECT,
1494     .abstract = true,
1495     .class_size = sizeof(MachineClass),
1496     .class_init    = machine_class_init,
1497     .class_base_init = machine_class_base_init,
1498     .instance_size = sizeof(MachineState),
1499     .instance_init = machine_initfn,
1500     .instance_finalize = machine_finalize,
1501 };
1502 
1503 static void machine_register_types(void)
1504 {
1505     type_register_static(&machine_info);
1506 }
1507 
1508 type_init(machine_register_types)
1509