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