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