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