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