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