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