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