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