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