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