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