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