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