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