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