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