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