xref: /openbmc/qemu/hw/core/machine.c (revision 8466405e)
1 /*
2  * QEMU Machine
3  *
4  * Copyright (C) 2014 Red Hat Inc
5  *
6  * Authors:
7  *   Marcel Apfelbaum <marcel.a@redhat.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qemu/option.h"
15 #include "qapi/qmp/qerror.h"
16 #include "sysemu/replay.h"
17 #include "qemu/units.h"
18 #include "hw/boards.h"
19 #include "hw/loader.h"
20 #include "qapi/error.h"
21 #include "qapi/qapi-visit-common.h"
22 #include "qapi/qapi-visit-machine.h"
23 #include "qapi/visitor.h"
24 #include "qom/object_interfaces.h"
25 #include "hw/sysbus.h"
26 #include "sysemu/cpus.h"
27 #include "sysemu/sysemu.h"
28 #include "sysemu/reset.h"
29 #include "sysemu/runstate.h"
30 #include "sysemu/numa.h"
31 #include "sysemu/xen.h"
32 #include "qemu/error-report.h"
33 #include "sysemu/qtest.h"
34 #include "hw/pci/pci.h"
35 #include "hw/mem/nvdimm.h"
36 #include "migration/global_state.h"
37 #include "migration/vmstate.h"
38 #include "exec/confidential-guest-support.h"
39 #include "hw/virtio/virtio.h"
40 #include "hw/virtio/virtio-pci.h"
41 #include "qom/object_interfaces.h"
42 
43 GlobalProperty hw_compat_7_1[] = {};
44 const size_t hw_compat_7_1_len = G_N_ELEMENTS(hw_compat_7_1);
45 
46 GlobalProperty hw_compat_7_0[] = {
47     { "arm-gicv3-common", "force-8-bit-prio", "on" },
48     { "nvme-ns", "eui64-default", "on"},
49 };
50 const size_t hw_compat_7_0_len = G_N_ELEMENTS(hw_compat_7_0);
51 
52 GlobalProperty hw_compat_6_2[] = {
53     { "PIIX4_PM", "x-not-migrate-acpi-index", "on"},
54 };
55 const size_t hw_compat_6_2_len = G_N_ELEMENTS(hw_compat_6_2);
56 
57 GlobalProperty hw_compat_6_1[] = {
58     { "vhost-user-vsock-device", "seqpacket", "off" },
59     { "nvme-ns", "shared", "off" },
60 };
61 const size_t hw_compat_6_1_len = G_N_ELEMENTS(hw_compat_6_1);
62 
63 GlobalProperty hw_compat_6_0[] = {
64     { "gpex-pcihost", "allow-unmapped-accesses", "false" },
65     { "i8042", "extended-state", "false"},
66     { "nvme-ns", "eui64-default", "off"},
67     { "e1000", "init-vet", "off" },
68     { "e1000e", "init-vet", "off" },
69     { "vhost-vsock-device", "seqpacket", "off" },
70 };
71 const size_t hw_compat_6_0_len = G_N_ELEMENTS(hw_compat_6_0);
72 
73 GlobalProperty hw_compat_5_2[] = {
74     { "ICH9-LPC", "smm-compat", "on"},
75     { "PIIX4_PM", "smm-compat", "on"},
76     { "virtio-blk-device", "report-discard-granularity", "off" },
77     { "virtio-net-pci-base", "vectors", "3"},
78 };
79 const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
80 
81 GlobalProperty hw_compat_5_1[] = {
82     { "vhost-scsi", "num_queues", "1"},
83     { "vhost-user-blk", "num-queues", "1"},
84     { "vhost-user-scsi", "num_queues", "1"},
85     { "virtio-blk-device", "num-queues", "1"},
86     { "virtio-scsi-device", "num_queues", "1"},
87     { "nvme", "use-intel-id", "on"},
88     { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
89     { "pl011", "migrate-clk", "off" },
90     { "virtio-pci", "x-ats-page-aligned", "off"},
91 };
92 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
93 
94 GlobalProperty hw_compat_5_0[] = {
95     { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
96     { "virtio-balloon-device", "page-poison", "false" },
97     { "vmport", "x-read-set-eax", "off" },
98     { "vmport", "x-signal-unsupported-cmd", "off" },
99     { "vmport", "x-report-vmx-type", "off" },
100     { "vmport", "x-cmds-v2", "off" },
101     { "virtio-device", "x-disable-legacy-check", "true" },
102 };
103 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
104 
105 GlobalProperty hw_compat_4_2[] = {
106     { "virtio-blk-device", "queue-size", "128"},
107     { "virtio-scsi-device", "virtqueue_size", "128"},
108     { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
109     { "virtio-blk-device", "seg-max-adjust", "off"},
110     { "virtio-scsi-device", "seg_max_adjust", "off"},
111     { "vhost-blk-device", "seg_max_adjust", "off"},
112     { "usb-host", "suppress-remote-wake", "off" },
113     { "usb-redir", "suppress-remote-wake", "off" },
114     { "qxl", "revision", "4" },
115     { "qxl-vga", "revision", "4" },
116     { "fw_cfg", "acpi-mr-restore", "false" },
117     { "virtio-device", "use-disabled-flag", "false" },
118 };
119 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
120 
121 GlobalProperty hw_compat_4_1[] = {
122     { "virtio-pci", "x-pcie-flr-init", "off" },
123 };
124 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
125 
126 GlobalProperty hw_compat_4_0[] = {
127     { "VGA",            "edid", "false" },
128     { "secondary-vga",  "edid", "false" },
129     { "bochs-display",  "edid", "false" },
130     { "virtio-vga",     "edid", "false" },
131     { "virtio-gpu-device", "edid", "false" },
132     { "virtio-device", "use-started", "false" },
133     { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
134     { "pl031", "migrate-tick-offset", "false" },
135 };
136 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
137 
138 GlobalProperty hw_compat_3_1[] = {
139     { "pcie-root-port", "x-speed", "2_5" },
140     { "pcie-root-port", "x-width", "1" },
141     { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
142     { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
143     { "tpm-crb", "ppi", "false" },
144     { "tpm-tis", "ppi", "false" },
145     { "usb-kbd", "serial", "42" },
146     { "usb-mouse", "serial", "42" },
147     { "usb-tablet", "serial", "42" },
148     { "virtio-blk-device", "discard", "false" },
149     { "virtio-blk-device", "write-zeroes", "false" },
150     { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
151     { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
152 };
153 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
154 
155 GlobalProperty hw_compat_3_0[] = {};
156 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
157 
158 GlobalProperty hw_compat_2_12[] = {
159     { "migration", "decompress-error-check", "off" },
160     { "hda-audio", "use-timer", "false" },
161     { "cirrus-vga", "global-vmstate", "true" },
162     { "VGA", "global-vmstate", "true" },
163     { "vmware-svga", "global-vmstate", "true" },
164     { "qxl-vga", "global-vmstate", "true" },
165 };
166 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
167 
168 GlobalProperty hw_compat_2_11[] = {
169     { "hpet", "hpet-offset-saved", "false" },
170     { "virtio-blk-pci", "vectors", "2" },
171     { "vhost-user-blk-pci", "vectors", "2" },
172     { "e1000", "migrate_tso_props", "off" },
173 };
174 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
175 
176 GlobalProperty hw_compat_2_10[] = {
177     { "virtio-mouse-device", "wheel-axis", "false" },
178     { "virtio-tablet-device", "wheel-axis", "false" },
179 };
180 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
181 
182 GlobalProperty hw_compat_2_9[] = {
183     { "pci-bridge", "shpc", "off" },
184     { "intel-iommu", "pt", "off" },
185     { "virtio-net-device", "x-mtu-bypass-backend", "off" },
186     { "pcie-root-port", "x-migrate-msix", "false" },
187 };
188 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
189 
190 GlobalProperty hw_compat_2_8[] = {
191     { "fw_cfg_mem", "x-file-slots", "0x10" },
192     { "fw_cfg_io", "x-file-slots", "0x10" },
193     { "pflash_cfi01", "old-multiple-chip-handling", "on" },
194     { "pci-bridge", "shpc", "on" },
195     { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
196     { "virtio-pci", "x-pcie-deverr-init", "off" },
197     { "virtio-pci", "x-pcie-lnkctl-init", "off" },
198     { "virtio-pci", "x-pcie-pm-init", "off" },
199     { "cirrus-vga", "vgamem_mb", "8" },
200     { "isa-cirrus-vga", "vgamem_mb", "8" },
201 };
202 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
203 
204 GlobalProperty hw_compat_2_7[] = {
205     { "virtio-pci", "page-per-vq", "on" },
206     { "virtio-serial-device", "emergency-write", "off" },
207     { "ioapic", "version", "0x11" },
208     { "intel-iommu", "x-buggy-eim", "true" },
209     { "virtio-pci", "x-ignore-backend-features", "on" },
210 };
211 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
212 
213 GlobalProperty hw_compat_2_6[] = {
214     { "virtio-mmio", "format_transport_address", "off" },
215     /* Optional because not all virtio-pci devices support legacy mode */
216     { "virtio-pci", "disable-modern", "on",  .optional = true },
217     { "virtio-pci", "disable-legacy", "off", .optional = true },
218 };
219 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
220 
221 GlobalProperty hw_compat_2_5[] = {
222     { "isa-fdc", "fallback", "144" },
223     { "pvscsi", "x-old-pci-configuration", "on" },
224     { "pvscsi", "x-disable-pcie", "on" },
225     { "vmxnet3", "x-old-msi-offsets", "on" },
226     { "vmxnet3", "x-disable-pcie", "on" },
227 };
228 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
229 
230 GlobalProperty hw_compat_2_4[] = {
231     /* Optional because the 'scsi' property is Linux-only */
232     { "virtio-blk-device", "scsi", "true", .optional = true },
233     { "e1000", "extra_mac_registers", "off" },
234     { "virtio-pci", "x-disable-pcie", "on" },
235     { "virtio-pci", "migrate-extra", "off" },
236     { "fw_cfg_mem", "dma_enabled", "off" },
237     { "fw_cfg_io", "dma_enabled", "off" }
238 };
239 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
240 
241 GlobalProperty hw_compat_2_3[] = {
242     { "virtio-blk-pci", "any_layout", "off" },
243     { "virtio-balloon-pci", "any_layout", "off" },
244     { "virtio-serial-pci", "any_layout", "off" },
245     { "virtio-9p-pci", "any_layout", "off" },
246     { "virtio-rng-pci", "any_layout", "off" },
247     { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
248     { "migration", "send-configuration", "off" },
249     { "migration", "send-section-footer", "off" },
250     { "migration", "store-global-state", "off" },
251 };
252 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
253 
254 GlobalProperty hw_compat_2_2[] = {};
255 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
256 
257 GlobalProperty hw_compat_2_1[] = {
258     { "intel-hda", "old_msi_addr", "on" },
259     { "VGA", "qemu-extended-regs", "off" },
260     { "secondary-vga", "qemu-extended-regs", "off" },
261     { "virtio-scsi-pci", "any_layout", "off" },
262     { "usb-mouse", "usb_version", "1" },
263     { "usb-kbd", "usb_version", "1" },
264     { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
265 };
266 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
267 
268 MachineState *current_machine;
269 
270 static char *machine_get_kernel(Object *obj, Error **errp)
271 {
272     MachineState *ms = MACHINE(obj);
273 
274     return g_strdup(ms->kernel_filename);
275 }
276 
277 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
278 {
279     MachineState *ms = MACHINE(obj);
280 
281     g_free(ms->kernel_filename);
282     ms->kernel_filename = g_strdup(value);
283 }
284 
285 static char *machine_get_initrd(Object *obj, Error **errp)
286 {
287     MachineState *ms = MACHINE(obj);
288 
289     return g_strdup(ms->initrd_filename);
290 }
291 
292 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
293 {
294     MachineState *ms = MACHINE(obj);
295 
296     g_free(ms->initrd_filename);
297     ms->initrd_filename = g_strdup(value);
298 }
299 
300 static char *machine_get_append(Object *obj, Error **errp)
301 {
302     MachineState *ms = MACHINE(obj);
303 
304     return g_strdup(ms->kernel_cmdline);
305 }
306 
307 static void machine_set_append(Object *obj, const char *value, Error **errp)
308 {
309     MachineState *ms = MACHINE(obj);
310 
311     g_free(ms->kernel_cmdline);
312     ms->kernel_cmdline = g_strdup(value);
313 }
314 
315 static char *machine_get_dtb(Object *obj, Error **errp)
316 {
317     MachineState *ms = MACHINE(obj);
318 
319     return g_strdup(ms->dtb);
320 }
321 
322 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
323 {
324     MachineState *ms = MACHINE(obj);
325 
326     g_free(ms->dtb);
327     ms->dtb = g_strdup(value);
328 }
329 
330 static char *machine_get_dumpdtb(Object *obj, Error **errp)
331 {
332     MachineState *ms = MACHINE(obj);
333 
334     return g_strdup(ms->dumpdtb);
335 }
336 
337 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
338 {
339     MachineState *ms = MACHINE(obj);
340 
341     g_free(ms->dumpdtb);
342     ms->dumpdtb = g_strdup(value);
343 }
344 
345 static void machine_get_phandle_start(Object *obj, Visitor *v,
346                                       const char *name, void *opaque,
347                                       Error **errp)
348 {
349     MachineState *ms = MACHINE(obj);
350     int64_t value = ms->phandle_start;
351 
352     visit_type_int(v, name, &value, errp);
353 }
354 
355 static void machine_set_phandle_start(Object *obj, Visitor *v,
356                                       const char *name, void *opaque,
357                                       Error **errp)
358 {
359     MachineState *ms = MACHINE(obj);
360     int64_t value;
361 
362     if (!visit_type_int(v, name, &value, errp)) {
363         return;
364     }
365 
366     ms->phandle_start = value;
367 }
368 
369 static char *machine_get_dt_compatible(Object *obj, Error **errp)
370 {
371     MachineState *ms = MACHINE(obj);
372 
373     return g_strdup(ms->dt_compatible);
374 }
375 
376 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
377 {
378     MachineState *ms = MACHINE(obj);
379 
380     g_free(ms->dt_compatible);
381     ms->dt_compatible = g_strdup(value);
382 }
383 
384 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
385 {
386     MachineState *ms = MACHINE(obj);
387 
388     return ms->dump_guest_core;
389 }
390 
391 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
392 {
393     MachineState *ms = MACHINE(obj);
394 
395     ms->dump_guest_core = value;
396 }
397 
398 static bool machine_get_mem_merge(Object *obj, Error **errp)
399 {
400     MachineState *ms = MACHINE(obj);
401 
402     return ms->mem_merge;
403 }
404 
405 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
406 {
407     MachineState *ms = MACHINE(obj);
408 
409     ms->mem_merge = value;
410 }
411 
412 static bool machine_get_usb(Object *obj, Error **errp)
413 {
414     MachineState *ms = MACHINE(obj);
415 
416     return ms->usb;
417 }
418 
419 static void machine_set_usb(Object *obj, bool value, Error **errp)
420 {
421     MachineState *ms = MACHINE(obj);
422 
423     ms->usb = value;
424     ms->usb_disabled = !value;
425 }
426 
427 static bool machine_get_graphics(Object *obj, Error **errp)
428 {
429     MachineState *ms = MACHINE(obj);
430 
431     return ms->enable_graphics;
432 }
433 
434 static void machine_set_graphics(Object *obj, bool value, Error **errp)
435 {
436     MachineState *ms = MACHINE(obj);
437 
438     ms->enable_graphics = value;
439 }
440 
441 static char *machine_get_firmware(Object *obj, Error **errp)
442 {
443     MachineState *ms = MACHINE(obj);
444 
445     return g_strdup(ms->firmware);
446 }
447 
448 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
449 {
450     MachineState *ms = MACHINE(obj);
451 
452     g_free(ms->firmware);
453     ms->firmware = g_strdup(value);
454 }
455 
456 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
457 {
458     MachineState *ms = MACHINE(obj);
459 
460     ms->suppress_vmdesc = value;
461 }
462 
463 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
464 {
465     MachineState *ms = MACHINE(obj);
466 
467     return ms->suppress_vmdesc;
468 }
469 
470 static char *machine_get_memory_encryption(Object *obj, Error **errp)
471 {
472     MachineState *ms = MACHINE(obj);
473 
474     if (ms->cgs) {
475         return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
476     }
477 
478     return NULL;
479 }
480 
481 static void machine_set_memory_encryption(Object *obj, const char *value,
482                                         Error **errp)
483 {
484     Object *cgs =
485         object_resolve_path_component(object_get_objects_root(), value);
486 
487     if (!cgs) {
488         error_setg(errp, "No such memory encryption object '%s'", value);
489         return;
490     }
491 
492     object_property_set_link(obj, "confidential-guest-support", cgs, errp);
493 }
494 
495 static void machine_check_confidential_guest_support(const Object *obj,
496                                                      const char *name,
497                                                      Object *new_target,
498                                                      Error **errp)
499 {
500     /*
501      * So far the only constraint is that the target has the
502      * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
503      * by the QOM core
504      */
505 }
506 
507 static bool machine_get_nvdimm(Object *obj, Error **errp)
508 {
509     MachineState *ms = MACHINE(obj);
510 
511     return ms->nvdimms_state->is_enabled;
512 }
513 
514 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
515 {
516     MachineState *ms = MACHINE(obj);
517 
518     ms->nvdimms_state->is_enabled = value;
519 }
520 
521 static bool machine_get_hmat(Object *obj, Error **errp)
522 {
523     MachineState *ms = MACHINE(obj);
524 
525     return ms->numa_state->hmat_enabled;
526 }
527 
528 static void machine_set_hmat(Object *obj, bool value, Error **errp)
529 {
530     MachineState *ms = MACHINE(obj);
531 
532     ms->numa_state->hmat_enabled = value;
533 }
534 
535 static void machine_get_mem(Object *obj, Visitor *v, const char *name,
536                             void *opaque, Error **errp)
537 {
538     MachineState *ms = MACHINE(obj);
539     MemorySizeConfiguration mem = {
540         .has_size = true,
541         .size = ms->ram_size,
542         .has_max_size = !!ms->ram_slots,
543         .max_size = ms->maxram_size,
544         .has_slots = !!ms->ram_slots,
545         .slots = ms->ram_slots,
546     };
547     MemorySizeConfiguration *p_mem = &mem;
548 
549     visit_type_MemorySizeConfiguration(v, name, &p_mem, &error_abort);
550 }
551 
552 static void machine_set_mem(Object *obj, Visitor *v, const char *name,
553                             void *opaque, Error **errp)
554 {
555     MachineState *ms = MACHINE(obj);
556     MachineClass *mc = MACHINE_GET_CLASS(obj);
557     MemorySizeConfiguration *mem;
558 
559     ERRP_GUARD();
560 
561     if (!visit_type_MemorySizeConfiguration(v, name, &mem, errp)) {
562         return;
563     }
564 
565     if (!mem->has_size) {
566         mem->has_size = true;
567         mem->size = mc->default_ram_size;
568     }
569     mem->size = QEMU_ALIGN_UP(mem->size, 8192);
570     if (mc->fixup_ram_size) {
571         mem->size = mc->fixup_ram_size(mem->size);
572     }
573     if ((ram_addr_t)mem->size != mem->size) {
574         error_setg(errp, "ram size too large");
575         goto out_free;
576     }
577 
578     if (mem->has_max_size) {
579         if (mem->max_size < mem->size) {
580             error_setg(errp, "invalid value of maxmem: "
581                        "maximum memory size (0x%" PRIx64 ") must be at least "
582                        "the initial memory size (0x%" PRIx64 ")",
583                        mem->max_size, mem->size);
584             goto out_free;
585         }
586         if (mem->has_slots && mem->slots && mem->max_size == mem->size) {
587             error_setg(errp, "invalid value of maxmem: "
588                        "memory slots were specified but maximum memory size "
589                        "(0x%" PRIx64 ") is equal to the initial memory size "
590                        "(0x%" PRIx64 ")", mem->max_size, mem->size);
591             goto out_free;
592         }
593         ms->maxram_size = mem->max_size;
594     } else {
595         if (mem->has_slots) {
596             error_setg(errp, "slots specified but no max-size");
597             goto out_free;
598         }
599         ms->maxram_size = mem->size;
600     }
601     ms->ram_size = mem->size;
602     ms->ram_slots = mem->has_slots ? mem->slots : 0;
603 out_free:
604     qapi_free_MemorySizeConfiguration(mem);
605 }
606 
607 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
608 {
609     MachineState *ms = MACHINE(obj);
610 
611     return g_strdup(ms->nvdimms_state->persistence_string);
612 }
613 
614 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
615                                            Error **errp)
616 {
617     MachineState *ms = MACHINE(obj);
618     NVDIMMState *nvdimms_state = ms->nvdimms_state;
619 
620     if (strcmp(value, "cpu") == 0) {
621         nvdimms_state->persistence = 3;
622     } else if (strcmp(value, "mem-ctrl") == 0) {
623         nvdimms_state->persistence = 2;
624     } else {
625         error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
626                    value);
627         return;
628     }
629 
630     g_free(nvdimms_state->persistence_string);
631     nvdimms_state->persistence_string = g_strdup(value);
632 }
633 
634 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
635 {
636     QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
637 }
638 
639 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
640 {
641     Object *obj = OBJECT(dev);
642 
643     if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
644         return false;
645     }
646 
647     return device_type_is_dynamic_sysbus(mc, object_get_typename(obj));
648 }
649 
650 bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type)
651 {
652     bool allowed = false;
653     strList *wl;
654     ObjectClass *klass = object_class_by_name(type);
655 
656     for (wl = mc->allowed_dynamic_sysbus_devices;
657          !allowed && wl;
658          wl = wl->next) {
659         allowed |= !!object_class_dynamic_cast(klass, wl->value);
660     }
661 
662     return allowed;
663 }
664 
665 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
666 {
667     int i;
668     HotpluggableCPUList *head = NULL;
669     MachineClass *mc = MACHINE_GET_CLASS(machine);
670 
671     /* force board to initialize possible_cpus if it hasn't been done yet */
672     mc->possible_cpu_arch_ids(machine);
673 
674     for (i = 0; i < machine->possible_cpus->len; i++) {
675         Object *cpu;
676         HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
677 
678         cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
679         cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
680         cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
681                                    sizeof(*cpu_item->props));
682 
683         cpu = machine->possible_cpus->cpus[i].cpu;
684         if (cpu) {
685             cpu_item->has_qom_path = true;
686             cpu_item->qom_path = object_get_canonical_path(cpu);
687         }
688         QAPI_LIST_PREPEND(head, cpu_item);
689     }
690     return head;
691 }
692 
693 /**
694  * machine_set_cpu_numa_node:
695  * @machine: machine object to modify
696  * @props: specifies which cpu objects to assign to
697  *         numa node specified by @props.node_id
698  * @errp: if an error occurs, a pointer to an area to store the error
699  *
700  * Associate NUMA node specified by @props.node_id with cpu slots that
701  * match socket/core/thread-ids specified by @props. It's recommended to use
702  * query-hotpluggable-cpus.props values to specify affected cpu slots,
703  * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
704  *
705  * However for CLI convenience it's possible to pass in subset of properties,
706  * which would affect all cpu slots that match it.
707  * Ex for pc machine:
708  *    -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
709  *    -numa cpu,node-id=0,socket_id=0 \
710  *    -numa cpu,node-id=1,socket_id=1
711  * will assign all child cores of socket 0 to node 0 and
712  * of socket 1 to node 1.
713  *
714  * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
715  * return error.
716  * Empty subset is disallowed and function will return with error in this case.
717  */
718 void machine_set_cpu_numa_node(MachineState *machine,
719                                const CpuInstanceProperties *props, Error **errp)
720 {
721     MachineClass *mc = MACHINE_GET_CLASS(machine);
722     NodeInfo *numa_info = machine->numa_state->nodes;
723     bool match = false;
724     int i;
725 
726     if (!mc->possible_cpu_arch_ids) {
727         error_setg(errp, "mapping of CPUs to NUMA node is not supported");
728         return;
729     }
730 
731     /* disabling node mapping is not supported, forbid it */
732     assert(props->has_node_id);
733 
734     /* force board to initialize possible_cpus if it hasn't been done yet */
735     mc->possible_cpu_arch_ids(machine);
736 
737     for (i = 0; i < machine->possible_cpus->len; i++) {
738         CPUArchId *slot = &machine->possible_cpus->cpus[i];
739 
740         /* reject unsupported by board properties */
741         if (props->has_thread_id && !slot->props.has_thread_id) {
742             error_setg(errp, "thread-id is not supported");
743             return;
744         }
745 
746         if (props->has_core_id && !slot->props.has_core_id) {
747             error_setg(errp, "core-id is not supported");
748             return;
749         }
750 
751         if (props->has_cluster_id && !slot->props.has_cluster_id) {
752             error_setg(errp, "cluster-id is not supported");
753             return;
754         }
755 
756         if (props->has_socket_id && !slot->props.has_socket_id) {
757             error_setg(errp, "socket-id is not supported");
758             return;
759         }
760 
761         if (props->has_die_id && !slot->props.has_die_id) {
762             error_setg(errp, "die-id is not supported");
763             return;
764         }
765 
766         /* skip slots with explicit mismatch */
767         if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
768                 continue;
769         }
770 
771         if (props->has_core_id && props->core_id != slot->props.core_id) {
772                 continue;
773         }
774 
775         if (props->has_cluster_id &&
776             props->cluster_id != slot->props.cluster_id) {
777                 continue;
778         }
779 
780         if (props->has_die_id && props->die_id != slot->props.die_id) {
781                 continue;
782         }
783 
784         if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
785                 continue;
786         }
787 
788         /* reject assignment if slot is already assigned, for compatibility
789          * of legacy cpu_index mapping with SPAPR core based mapping do not
790          * error out if cpu thread and matched core have the same node-id */
791         if (slot->props.has_node_id &&
792             slot->props.node_id != props->node_id) {
793             error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
794                        slot->props.node_id);
795             return;
796         }
797 
798         /* assign slot to node as it's matched '-numa cpu' key */
799         match = true;
800         slot->props.node_id = props->node_id;
801         slot->props.has_node_id = props->has_node_id;
802 
803         if (machine->numa_state->hmat_enabled) {
804             if ((numa_info[props->node_id].initiator < MAX_NODES) &&
805                 (props->node_id != numa_info[props->node_id].initiator)) {
806                 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
807                            " should be itself (got %" PRIu16 ")",
808                            props->node_id, numa_info[props->node_id].initiator);
809                 return;
810             }
811             numa_info[props->node_id].has_cpu = true;
812             numa_info[props->node_id].initiator = props->node_id;
813         }
814     }
815 
816     if (!match) {
817         error_setg(errp, "no match found");
818     }
819 }
820 
821 static void machine_get_smp(Object *obj, Visitor *v, const char *name,
822                             void *opaque, Error **errp)
823 {
824     MachineState *ms = MACHINE(obj);
825     SMPConfiguration *config = &(SMPConfiguration){
826         .has_cpus = true, .cpus = ms->smp.cpus,
827         .has_sockets = true, .sockets = ms->smp.sockets,
828         .has_dies = true, .dies = ms->smp.dies,
829         .has_clusters = true, .clusters = ms->smp.clusters,
830         .has_cores = true, .cores = ms->smp.cores,
831         .has_threads = true, .threads = ms->smp.threads,
832         .has_maxcpus = true, .maxcpus = ms->smp.max_cpus,
833     };
834 
835     if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) {
836         return;
837     }
838 }
839 
840 static void machine_set_smp(Object *obj, Visitor *v, const char *name,
841                             void *opaque, Error **errp)
842 {
843     MachineState *ms = MACHINE(obj);
844     g_autoptr(SMPConfiguration) config = NULL;
845 
846     if (!visit_type_SMPConfiguration(v, name, &config, errp)) {
847         return;
848     }
849 
850     machine_parse_smp_config(ms, config, errp);
851 }
852 
853 static void machine_get_boot(Object *obj, Visitor *v, const char *name,
854                             void *opaque, Error **errp)
855 {
856     MachineState *ms = MACHINE(obj);
857     BootConfiguration *config = &ms->boot_config;
858     visit_type_BootConfiguration(v, name, &config, &error_abort);
859 }
860 
861 static void machine_free_boot_config(MachineState *ms)
862 {
863     g_free(ms->boot_config.order);
864     g_free(ms->boot_config.once);
865     g_free(ms->boot_config.splash);
866 }
867 
868 static void machine_copy_boot_config(MachineState *ms, BootConfiguration *config)
869 {
870     MachineClass *machine_class = MACHINE_GET_CLASS(ms);
871 
872     machine_free_boot_config(ms);
873     ms->boot_config = *config;
874     if (!config->has_order) {
875         ms->boot_config.has_order = true;
876         ms->boot_config.order = g_strdup(machine_class->default_boot_order);
877     }
878 }
879 
880 static void machine_set_boot(Object *obj, Visitor *v, const char *name,
881                             void *opaque, Error **errp)
882 {
883     ERRP_GUARD();
884     MachineState *ms = MACHINE(obj);
885     BootConfiguration *config = NULL;
886 
887     if (!visit_type_BootConfiguration(v, name, &config, errp)) {
888         return;
889     }
890     if (config->has_order) {
891         validate_bootdevices(config->order, errp);
892         if (*errp) {
893             goto out_free;
894         }
895     }
896     if (config->has_once) {
897         validate_bootdevices(config->once, errp);
898         if (*errp) {
899             goto out_free;
900         }
901     }
902 
903     machine_copy_boot_config(ms, config);
904     /* Strings live in ms->boot_config.  */
905     free(config);
906     return;
907 
908 out_free:
909     qapi_free_BootConfiguration(config);
910 }
911 
912 static void machine_class_init(ObjectClass *oc, void *data)
913 {
914     MachineClass *mc = MACHINE_CLASS(oc);
915 
916     /* Default 128 MB as guest ram size */
917     mc->default_ram_size = 128 * MiB;
918     mc->rom_file_has_mr = true;
919 
920     /* numa node memory size aligned on 8MB by default.
921      * On Linux, each node's border has to be 8MB aligned
922      */
923     mc->numa_mem_align_shift = 23;
924 
925     object_class_property_add_str(oc, "kernel",
926         machine_get_kernel, machine_set_kernel);
927     object_class_property_set_description(oc, "kernel",
928         "Linux kernel image file");
929 
930     object_class_property_add_str(oc, "initrd",
931         machine_get_initrd, machine_set_initrd);
932     object_class_property_set_description(oc, "initrd",
933         "Linux initial ramdisk file");
934 
935     object_class_property_add_str(oc, "append",
936         machine_get_append, machine_set_append);
937     object_class_property_set_description(oc, "append",
938         "Linux kernel command line");
939 
940     object_class_property_add_str(oc, "dtb",
941         machine_get_dtb, machine_set_dtb);
942     object_class_property_set_description(oc, "dtb",
943         "Linux kernel device tree file");
944 
945     object_class_property_add_str(oc, "dumpdtb",
946         machine_get_dumpdtb, machine_set_dumpdtb);
947     object_class_property_set_description(oc, "dumpdtb",
948         "Dump current dtb to a file and quit");
949 
950     object_class_property_add(oc, "boot", "BootConfiguration",
951         machine_get_boot, machine_set_boot,
952         NULL, NULL);
953     object_class_property_set_description(oc, "boot",
954         "Boot configuration");
955 
956     object_class_property_add(oc, "smp", "SMPConfiguration",
957         machine_get_smp, machine_set_smp,
958         NULL, NULL);
959     object_class_property_set_description(oc, "smp",
960         "CPU topology");
961 
962     object_class_property_add(oc, "phandle-start", "int",
963         machine_get_phandle_start, machine_set_phandle_start,
964         NULL, NULL);
965     object_class_property_set_description(oc, "phandle-start",
966         "The first phandle ID we may generate dynamically");
967 
968     object_class_property_add_str(oc, "dt-compatible",
969         machine_get_dt_compatible, machine_set_dt_compatible);
970     object_class_property_set_description(oc, "dt-compatible",
971         "Overrides the \"compatible\" property of the dt root node");
972 
973     object_class_property_add_bool(oc, "dump-guest-core",
974         machine_get_dump_guest_core, machine_set_dump_guest_core);
975     object_class_property_set_description(oc, "dump-guest-core",
976         "Include guest memory in a core dump");
977 
978     object_class_property_add_bool(oc, "mem-merge",
979         machine_get_mem_merge, machine_set_mem_merge);
980     object_class_property_set_description(oc, "mem-merge",
981         "Enable/disable memory merge support");
982 
983     object_class_property_add_bool(oc, "usb",
984         machine_get_usb, machine_set_usb);
985     object_class_property_set_description(oc, "usb",
986         "Set on/off to enable/disable usb");
987 
988     object_class_property_add_bool(oc, "graphics",
989         machine_get_graphics, machine_set_graphics);
990     object_class_property_set_description(oc, "graphics",
991         "Set on/off to enable/disable graphics emulation");
992 
993     object_class_property_add_str(oc, "firmware",
994         machine_get_firmware, machine_set_firmware);
995     object_class_property_set_description(oc, "firmware",
996         "Firmware image");
997 
998     object_class_property_add_bool(oc, "suppress-vmdesc",
999         machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
1000     object_class_property_set_description(oc, "suppress-vmdesc",
1001         "Set on to disable self-describing migration");
1002 
1003     object_class_property_add_link(oc, "confidential-guest-support",
1004                                    TYPE_CONFIDENTIAL_GUEST_SUPPORT,
1005                                    offsetof(MachineState, cgs),
1006                                    machine_check_confidential_guest_support,
1007                                    OBJ_PROP_LINK_STRONG);
1008     object_class_property_set_description(oc, "confidential-guest-support",
1009                                           "Set confidential guest scheme to support");
1010 
1011     /* For compatibility */
1012     object_class_property_add_str(oc, "memory-encryption",
1013         machine_get_memory_encryption, machine_set_memory_encryption);
1014     object_class_property_set_description(oc, "memory-encryption",
1015         "Set memory encryption object to use");
1016 
1017     object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKEND,
1018                                    offsetof(MachineState, memdev), object_property_allow_set_link,
1019                                    OBJ_PROP_LINK_STRONG);
1020     object_class_property_set_description(oc, "memory-backend",
1021                                           "Set RAM backend"
1022                                           "Valid value is ID of hostmem based backend");
1023 
1024     object_class_property_add(oc, "memory", "MemorySizeConfiguration",
1025         machine_get_mem, machine_set_mem,
1026         NULL, NULL);
1027     object_class_property_set_description(oc, "memory",
1028         "Memory size configuration");
1029 }
1030 
1031 static void machine_class_base_init(ObjectClass *oc, void *data)
1032 {
1033     MachineClass *mc = MACHINE_CLASS(oc);
1034     mc->max_cpus = mc->max_cpus ?: 1;
1035     mc->min_cpus = mc->min_cpus ?: 1;
1036     mc->default_cpus = mc->default_cpus ?: 1;
1037 
1038     if (!object_class_is_abstract(oc)) {
1039         const char *cname = object_class_get_name(oc);
1040         assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
1041         mc->name = g_strndup(cname,
1042                             strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
1043         mc->compat_props = g_ptr_array_new();
1044     }
1045 }
1046 
1047 static void machine_initfn(Object *obj)
1048 {
1049     MachineState *ms = MACHINE(obj);
1050     MachineClass *mc = MACHINE_GET_CLASS(obj);
1051 
1052     container_get(obj, "/peripheral");
1053     container_get(obj, "/peripheral-anon");
1054 
1055     ms->dump_guest_core = true;
1056     ms->mem_merge = true;
1057     ms->enable_graphics = true;
1058     ms->kernel_cmdline = g_strdup("");
1059     ms->ram_size = mc->default_ram_size;
1060     ms->maxram_size = mc->default_ram_size;
1061 
1062     if (mc->nvdimm_supported) {
1063         Object *obj = OBJECT(ms);
1064 
1065         ms->nvdimms_state = g_new0(NVDIMMState, 1);
1066         object_property_add_bool(obj, "nvdimm",
1067                                  machine_get_nvdimm, machine_set_nvdimm);
1068         object_property_set_description(obj, "nvdimm",
1069                                         "Set on/off to enable/disable "
1070                                         "NVDIMM instantiation");
1071 
1072         object_property_add_str(obj, "nvdimm-persistence",
1073                                 machine_get_nvdimm_persistence,
1074                                 machine_set_nvdimm_persistence);
1075         object_property_set_description(obj, "nvdimm-persistence",
1076                                         "Set NVDIMM persistence"
1077                                         "Valid values are cpu, mem-ctrl");
1078     }
1079 
1080     if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
1081         ms->numa_state = g_new0(NumaState, 1);
1082         object_property_add_bool(obj, "hmat",
1083                                  machine_get_hmat, machine_set_hmat);
1084         object_property_set_description(obj, "hmat",
1085                                         "Set on/off to enable/disable "
1086                                         "ACPI Heterogeneous Memory Attribute "
1087                                         "Table (HMAT)");
1088     }
1089 
1090     /* default to mc->default_cpus */
1091     ms->smp.cpus = mc->default_cpus;
1092     ms->smp.max_cpus = mc->default_cpus;
1093     ms->smp.sockets = 1;
1094     ms->smp.dies = 1;
1095     ms->smp.clusters = 1;
1096     ms->smp.cores = 1;
1097     ms->smp.threads = 1;
1098 
1099     machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
1100 }
1101 
1102 static void machine_finalize(Object *obj)
1103 {
1104     MachineState *ms = MACHINE(obj);
1105 
1106     machine_free_boot_config(ms);
1107     g_free(ms->kernel_filename);
1108     g_free(ms->initrd_filename);
1109     g_free(ms->kernel_cmdline);
1110     g_free(ms->dtb);
1111     g_free(ms->dumpdtb);
1112     g_free(ms->dt_compatible);
1113     g_free(ms->firmware);
1114     g_free(ms->device_memory);
1115     g_free(ms->nvdimms_state);
1116     g_free(ms->numa_state);
1117 }
1118 
1119 bool machine_usb(MachineState *machine)
1120 {
1121     return machine->usb;
1122 }
1123 
1124 int machine_phandle_start(MachineState *machine)
1125 {
1126     return machine->phandle_start;
1127 }
1128 
1129 bool machine_dump_guest_core(MachineState *machine)
1130 {
1131     return machine->dump_guest_core;
1132 }
1133 
1134 bool machine_mem_merge(MachineState *machine)
1135 {
1136     return machine->mem_merge;
1137 }
1138 
1139 static char *cpu_slot_to_string(const CPUArchId *cpu)
1140 {
1141     GString *s = g_string_new(NULL);
1142     if (cpu->props.has_socket_id) {
1143         g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1144     }
1145     if (cpu->props.has_die_id) {
1146         if (s->len) {
1147             g_string_append_printf(s, ", ");
1148         }
1149         g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1150     }
1151     if (cpu->props.has_cluster_id) {
1152         if (s->len) {
1153             g_string_append_printf(s, ", ");
1154         }
1155         g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
1156     }
1157     if (cpu->props.has_core_id) {
1158         if (s->len) {
1159             g_string_append_printf(s, ", ");
1160         }
1161         g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1162     }
1163     if (cpu->props.has_thread_id) {
1164         if (s->len) {
1165             g_string_append_printf(s, ", ");
1166         }
1167         g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1168     }
1169     return g_string_free(s, false);
1170 }
1171 
1172 static void numa_validate_initiator(NumaState *numa_state)
1173 {
1174     int i;
1175     NodeInfo *numa_info = numa_state->nodes;
1176 
1177     for (i = 0; i < numa_state->num_nodes; i++) {
1178         if (numa_info[i].initiator == MAX_NODES) {
1179             error_report("The initiator of NUMA node %d is missing, use "
1180                          "'-numa node,initiator' option to declare it", i);
1181             exit(1);
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.has_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