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