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