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