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