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