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