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