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