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