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