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