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