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