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