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