xref: /qemu/hw/core/machine.c (revision d0fb9657)
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, QemuOpts *opts)
743 {
744     if (opts) {
745         unsigned cpus    = qemu_opt_get_number(opts, "cpus", 0);
746         unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
747         unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
748         unsigned threads = qemu_opt_get_number(opts, "threads", 0);
749 
750         /* compute missing values, prefer sockets over cores over threads */
751         if (cpus == 0 || sockets == 0) {
752             cores = cores > 0 ? cores : 1;
753             threads = threads > 0 ? threads : 1;
754             if (cpus == 0) {
755                 sockets = sockets > 0 ? sockets : 1;
756                 cpus = cores * threads * sockets;
757             } else {
758                 ms->smp.max_cpus =
759                         qemu_opt_get_number(opts, "maxcpus", cpus);
760                 sockets = ms->smp.max_cpus / (cores * threads);
761             }
762         } else if (cores == 0) {
763             threads = threads > 0 ? threads : 1;
764             cores = cpus / (sockets * threads);
765             cores = cores > 0 ? cores : 1;
766         } else if (threads == 0) {
767             threads = cpus / (cores * sockets);
768             threads = threads > 0 ? threads : 1;
769         } else if (sockets * cores * threads < cpus) {
770             error_report("cpu topology: "
771                          "sockets (%u) * cores (%u) * threads (%u) < "
772                          "smp_cpus (%u)",
773                          sockets, cores, threads, cpus);
774             exit(1);
775         }
776 
777         ms->smp.max_cpus =
778                 qemu_opt_get_number(opts, "maxcpus", cpus);
779 
780         if (ms->smp.max_cpus < cpus) {
781             error_report("maxcpus must be equal to or greater than smp");
782             exit(1);
783         }
784 
785         if (sockets * cores * threads != ms->smp.max_cpus) {
786             error_report("Invalid CPU topology: "
787                          "sockets (%u) * cores (%u) * threads (%u) "
788                          "!= maxcpus (%u)",
789                          sockets, cores, threads,
790                          ms->smp.max_cpus);
791             exit(1);
792         }
793 
794         ms->smp.cpus = cpus;
795         ms->smp.cores = cores;
796         ms->smp.threads = threads;
797         ms->smp.sockets = sockets;
798     }
799 
800     if (ms->smp.cpus > 1) {
801         Error *blocker = NULL;
802         error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
803         replay_add_blocker(blocker);
804     }
805 }
806 
807 static void machine_class_init(ObjectClass *oc, void *data)
808 {
809     MachineClass *mc = MACHINE_CLASS(oc);
810 
811     /* Default 128 MB as guest ram size */
812     mc->default_ram_size = 128 * MiB;
813     mc->rom_file_has_mr = true;
814     mc->smp_parse = smp_parse;
815 
816     /* numa node memory size aligned on 8MB by default.
817      * On Linux, each node's border has to be 8MB aligned
818      */
819     mc->numa_mem_align_shift = 23;
820 
821     object_class_property_add_str(oc, "kernel",
822         machine_get_kernel, machine_set_kernel);
823     object_class_property_set_description(oc, "kernel",
824         "Linux kernel image file");
825 
826     object_class_property_add_str(oc, "initrd",
827         machine_get_initrd, machine_set_initrd);
828     object_class_property_set_description(oc, "initrd",
829         "Linux initial ramdisk file");
830 
831     object_class_property_add_str(oc, "append",
832         machine_get_append, machine_set_append);
833     object_class_property_set_description(oc, "append",
834         "Linux kernel command line");
835 
836     object_class_property_add_str(oc, "dtb",
837         machine_get_dtb, machine_set_dtb);
838     object_class_property_set_description(oc, "dtb",
839         "Linux kernel device tree file");
840 
841     object_class_property_add_str(oc, "dumpdtb",
842         machine_get_dumpdtb, machine_set_dumpdtb);
843     object_class_property_set_description(oc, "dumpdtb",
844         "Dump current dtb to a file and quit");
845 
846     object_class_property_add(oc, "phandle-start", "int",
847         machine_get_phandle_start, machine_set_phandle_start,
848         NULL, NULL);
849     object_class_property_set_description(oc, "phandle-start",
850         "The first phandle ID we may generate dynamically");
851 
852     object_class_property_add_str(oc, "dt-compatible",
853         machine_get_dt_compatible, machine_set_dt_compatible);
854     object_class_property_set_description(oc, "dt-compatible",
855         "Overrides the \"compatible\" property of the dt root node");
856 
857     object_class_property_add_bool(oc, "dump-guest-core",
858         machine_get_dump_guest_core, machine_set_dump_guest_core);
859     object_class_property_set_description(oc, "dump-guest-core",
860         "Include guest memory in a core dump");
861 
862     object_class_property_add_bool(oc, "mem-merge",
863         machine_get_mem_merge, machine_set_mem_merge);
864     object_class_property_set_description(oc, "mem-merge",
865         "Enable/disable memory merge support");
866 
867     object_class_property_add_bool(oc, "usb",
868         machine_get_usb, machine_set_usb);
869     object_class_property_set_description(oc, "usb",
870         "Set on/off to enable/disable usb");
871 
872     object_class_property_add_bool(oc, "graphics",
873         machine_get_graphics, machine_set_graphics);
874     object_class_property_set_description(oc, "graphics",
875         "Set on/off to enable/disable graphics emulation");
876 
877     object_class_property_add_str(oc, "firmware",
878         machine_get_firmware, machine_set_firmware);
879     object_class_property_set_description(oc, "firmware",
880         "Firmware image");
881 
882     object_class_property_add_bool(oc, "suppress-vmdesc",
883         machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
884     object_class_property_set_description(oc, "suppress-vmdesc",
885         "Set on to disable self-describing migration");
886 
887     object_class_property_add_link(oc, "confidential-guest-support",
888                                    TYPE_CONFIDENTIAL_GUEST_SUPPORT,
889                                    offsetof(MachineState, cgs),
890                                    machine_check_confidential_guest_support,
891                                    OBJ_PROP_LINK_STRONG);
892     object_class_property_set_description(oc, "confidential-guest-support",
893                                           "Set confidential guest scheme to support");
894 
895     /* For compatibility */
896     object_class_property_add_str(oc, "memory-encryption",
897         machine_get_memory_encryption, machine_set_memory_encryption);
898     object_class_property_set_description(oc, "memory-encryption",
899         "Set memory encryption object to use");
900 
901     object_class_property_add_str(oc, "memory-backend",
902                                   machine_get_memdev, machine_set_memdev);
903     object_class_property_set_description(oc, "memory-backend",
904                                           "Set RAM backend"
905                                           "Valid value is ID of hostmem based backend");
906 }
907 
908 static void machine_class_base_init(ObjectClass *oc, void *data)
909 {
910     MachineClass *mc = MACHINE_CLASS(oc);
911     mc->max_cpus = mc->max_cpus ?: 1;
912     mc->min_cpus = mc->min_cpus ?: 1;
913     mc->default_cpus = mc->default_cpus ?: 1;
914 
915     if (!object_class_is_abstract(oc)) {
916         const char *cname = object_class_get_name(oc);
917         assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
918         mc->name = g_strndup(cname,
919                             strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
920         mc->compat_props = g_ptr_array_new();
921     }
922 }
923 
924 static void machine_initfn(Object *obj)
925 {
926     MachineState *ms = MACHINE(obj);
927     MachineClass *mc = MACHINE_GET_CLASS(obj);
928 
929     container_get(obj, "/peripheral");
930     container_get(obj, "/peripheral-anon");
931 
932     ms->dump_guest_core = true;
933     ms->mem_merge = true;
934     ms->enable_graphics = true;
935     ms->kernel_cmdline = g_strdup("");
936 
937     if (mc->nvdimm_supported) {
938         Object *obj = OBJECT(ms);
939 
940         ms->nvdimms_state = g_new0(NVDIMMState, 1);
941         object_property_add_bool(obj, "nvdimm",
942                                  machine_get_nvdimm, machine_set_nvdimm);
943         object_property_set_description(obj, "nvdimm",
944                                         "Set on/off to enable/disable "
945                                         "NVDIMM instantiation");
946 
947         object_property_add_str(obj, "nvdimm-persistence",
948                                 machine_get_nvdimm_persistence,
949                                 machine_set_nvdimm_persistence);
950         object_property_set_description(obj, "nvdimm-persistence",
951                                         "Set NVDIMM persistence"
952                                         "Valid values are cpu, mem-ctrl");
953     }
954 
955     if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
956         ms->numa_state = g_new0(NumaState, 1);
957         object_property_add_bool(obj, "hmat",
958                                  machine_get_hmat, machine_set_hmat);
959         object_property_set_description(obj, "hmat",
960                                         "Set on/off to enable/disable "
961                                         "ACPI Heterogeneous Memory Attribute "
962                                         "Table (HMAT)");
963     }
964 
965     /* Register notifier when init is done for sysbus sanity checks */
966     ms->sysbus_notifier.notify = machine_init_notify;
967     qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
968 
969     /* default to mc->default_cpus */
970     ms->smp.cpus = mc->default_cpus;
971     ms->smp.max_cpus = mc->default_cpus;
972     ms->smp.cores = 1;
973     ms->smp.threads = 1;
974     ms->smp.sockets = 1;
975 }
976 
977 static void machine_finalize(Object *obj)
978 {
979     MachineState *ms = MACHINE(obj);
980 
981     g_free(ms->kernel_filename);
982     g_free(ms->initrd_filename);
983     g_free(ms->kernel_cmdline);
984     g_free(ms->dtb);
985     g_free(ms->dumpdtb);
986     g_free(ms->dt_compatible);
987     g_free(ms->firmware);
988     g_free(ms->device_memory);
989     g_free(ms->nvdimms_state);
990     g_free(ms->numa_state);
991 }
992 
993 bool machine_usb(MachineState *machine)
994 {
995     return machine->usb;
996 }
997 
998 int machine_phandle_start(MachineState *machine)
999 {
1000     return machine->phandle_start;
1001 }
1002 
1003 bool machine_dump_guest_core(MachineState *machine)
1004 {
1005     return machine->dump_guest_core;
1006 }
1007 
1008 bool machine_mem_merge(MachineState *machine)
1009 {
1010     return machine->mem_merge;
1011 }
1012 
1013 static char *cpu_slot_to_string(const CPUArchId *cpu)
1014 {
1015     GString *s = g_string_new(NULL);
1016     if (cpu->props.has_socket_id) {
1017         g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1018     }
1019     if (cpu->props.has_die_id) {
1020         g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1021     }
1022     if (cpu->props.has_core_id) {
1023         if (s->len) {
1024             g_string_append_printf(s, ", ");
1025         }
1026         g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1027     }
1028     if (cpu->props.has_thread_id) {
1029         if (s->len) {
1030             g_string_append_printf(s, ", ");
1031         }
1032         g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1033     }
1034     return g_string_free(s, false);
1035 }
1036 
1037 static void numa_validate_initiator(NumaState *numa_state)
1038 {
1039     int i;
1040     NodeInfo *numa_info = numa_state->nodes;
1041 
1042     for (i = 0; i < numa_state->num_nodes; i++) {
1043         if (numa_info[i].initiator == MAX_NODES) {
1044             error_report("The initiator of NUMA node %d is missing, use "
1045                          "'-numa node,initiator' option to declare it", i);
1046             exit(1);
1047         }
1048 
1049         if (!numa_info[numa_info[i].initiator].present) {
1050             error_report("NUMA node %" PRIu16 " is missing, use "
1051                          "'-numa node' option to declare it first",
1052                          numa_info[i].initiator);
1053             exit(1);
1054         }
1055 
1056         if (!numa_info[numa_info[i].initiator].has_cpu) {
1057             error_report("The initiator of NUMA node %d is invalid", i);
1058             exit(1);
1059         }
1060     }
1061 }
1062 
1063 static void machine_numa_finish_cpu_init(MachineState *machine)
1064 {
1065     int i;
1066     bool default_mapping;
1067     GString *s = g_string_new(NULL);
1068     MachineClass *mc = MACHINE_GET_CLASS(machine);
1069     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1070 
1071     assert(machine->numa_state->num_nodes);
1072     for (i = 0; i < possible_cpus->len; i++) {
1073         if (possible_cpus->cpus[i].props.has_node_id) {
1074             break;
1075         }
1076     }
1077     default_mapping = (i == possible_cpus->len);
1078 
1079     for (i = 0; i < possible_cpus->len; i++) {
1080         const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1081 
1082         if (!cpu_slot->props.has_node_id) {
1083             /* fetch default mapping from board and enable it */
1084             CpuInstanceProperties props = cpu_slot->props;
1085 
1086             props.node_id = mc->get_default_cpu_node_id(machine, i);
1087             if (!default_mapping) {
1088                 /* record slots with not set mapping,
1089                  * TODO: make it hard error in future */
1090                 char *cpu_str = cpu_slot_to_string(cpu_slot);
1091                 g_string_append_printf(s, "%sCPU %d [%s]",
1092                                        s->len ? ", " : "", i, cpu_str);
1093                 g_free(cpu_str);
1094 
1095                 /* non mapped cpus used to fallback to node 0 */
1096                 props.node_id = 0;
1097             }
1098 
1099             props.has_node_id = true;
1100             machine_set_cpu_numa_node(machine, &props, &error_fatal);
1101         }
1102     }
1103 
1104     if (machine->numa_state->hmat_enabled) {
1105         numa_validate_initiator(machine->numa_state);
1106     }
1107 
1108     if (s->len && !qtest_enabled()) {
1109         warn_report("CPU(s) not present in any NUMA nodes: %s",
1110                     s->str);
1111         warn_report("All CPU(s) up to maxcpus should be described "
1112                     "in NUMA config, ability to start up with partial NUMA "
1113                     "mappings is obsoleted and will be removed in future");
1114     }
1115     g_string_free(s, true);
1116 }
1117 
1118 MemoryRegion *machine_consume_memdev(MachineState *machine,
1119                                      HostMemoryBackend *backend)
1120 {
1121     MemoryRegion *ret = host_memory_backend_get_memory(backend);
1122 
1123     if (memory_region_is_mapped(ret)) {
1124         error_report("memory backend %s can't be used multiple times.",
1125                      object_get_canonical_path_component(OBJECT(backend)));
1126         exit(EXIT_FAILURE);
1127     }
1128     host_memory_backend_set_mapped(backend, true);
1129     vmstate_register_ram_global(ret);
1130     return ret;
1131 }
1132 
1133 bool machine_smp_parse(MachineState *ms, QemuOpts *opts, Error **errp)
1134 {
1135     MachineClass *mc = MACHINE_GET_CLASS(ms);
1136 
1137     mc->smp_parse(ms, opts);
1138 
1139     /* sanity-check smp_cpus and max_cpus against mc */
1140     if (ms->smp.cpus < mc->min_cpus) {
1141         error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
1142                    "supported by machine '%s' is %d",
1143                    ms->smp.cpus,
1144                    mc->name, mc->min_cpus);
1145         return false;
1146     } else if (ms->smp.max_cpus > mc->max_cpus) {
1147         error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
1148                    "supported by machine '%s' is %d",
1149                    current_machine->smp.max_cpus,
1150                    mc->name, mc->max_cpus);
1151         return false;
1152     }
1153     return true;
1154 }
1155 
1156 void machine_run_board_init(MachineState *machine)
1157 {
1158     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1159     ObjectClass *oc = object_class_by_name(machine->cpu_type);
1160     CPUClass *cc;
1161 
1162     /* This checkpoint is required by replay to separate prior clock
1163        reading from the other reads, because timer polling functions query
1164        clock values from the log. */
1165     replay_checkpoint(CHECKPOINT_INIT);
1166 
1167     if (machine->ram_memdev_id) {
1168         Object *o;
1169         o = object_resolve_path_type(machine->ram_memdev_id,
1170                                      TYPE_MEMORY_BACKEND, NULL);
1171         machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1172     }
1173 
1174     if (machine->numa_state) {
1175         numa_complete_configuration(machine);
1176         if (machine->numa_state->num_nodes) {
1177             machine_numa_finish_cpu_init(machine);
1178         }
1179     }
1180 
1181     /* If the machine supports the valid_cpu_types check and the user
1182      * specified a CPU with -cpu check here that the user CPU is supported.
1183      */
1184     if (machine_class->valid_cpu_types && machine->cpu_type) {
1185         int i;
1186 
1187         for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1188             if (object_class_dynamic_cast(oc,
1189                                           machine_class->valid_cpu_types[i])) {
1190                 /* The user specificed CPU is in the valid field, we are
1191                  * good to go.
1192                  */
1193                 break;
1194             }
1195         }
1196 
1197         if (!machine_class->valid_cpu_types[i]) {
1198             /* The user specified CPU is not valid */
1199             error_report("Invalid CPU type: %s", machine->cpu_type);
1200             error_printf("The valid types are: %s",
1201                          machine_class->valid_cpu_types[0]);
1202             for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1203                 error_printf(", %s", machine_class->valid_cpu_types[i]);
1204             }
1205             error_printf("\n");
1206 
1207             exit(1);
1208         }
1209     }
1210 
1211     /* Check if CPU type is deprecated and warn if so */
1212     cc = CPU_CLASS(oc);
1213     if (cc && cc->deprecation_note) {
1214         warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1215                     cc->deprecation_note);
1216     }
1217 
1218     if (machine->cgs) {
1219         /*
1220          * With confidential guests, the host can't see the real
1221          * contents of RAM, so there's no point in it trying to merge
1222          * areas.
1223          */
1224         machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1225 
1226         /*
1227          * Virtio devices can't count on directly accessing guest
1228          * memory, so they need iommu_platform=on to use normal DMA
1229          * mechanisms.  That requires also disabling legacy virtio
1230          * support for those virtio pci devices which allow it.
1231          */
1232         object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1233                                    "on", true);
1234         object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1235                                    "on", false);
1236     }
1237 
1238     accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1239     machine_class->init(machine);
1240     phase_advance(PHASE_MACHINE_INITIALIZED);
1241 }
1242 
1243 static NotifierList machine_init_done_notifiers =
1244     NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1245 
1246 void qemu_add_machine_init_done_notifier(Notifier *notify)
1247 {
1248     notifier_list_add(&machine_init_done_notifiers, notify);
1249     if (phase_check(PHASE_MACHINE_READY)) {
1250         notify->notify(notify, NULL);
1251     }
1252 }
1253 
1254 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1255 {
1256     notifier_remove(notify);
1257 }
1258 
1259 void qdev_machine_creation_done(void)
1260 {
1261     cpu_synchronize_all_post_init();
1262 
1263     if (current_machine->boot_once) {
1264         qemu_boot_set(current_machine->boot_once, &error_fatal);
1265         qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_order));
1266     }
1267 
1268     /*
1269      * ok, initial machine setup is done, starting from now we can
1270      * only create hotpluggable devices
1271      */
1272     phase_advance(PHASE_MACHINE_READY);
1273     qdev_assert_realized_properly();
1274 
1275     /* TODO: once all bus devices are qdevified, this should be done
1276      * when bus is created by qdev.c */
1277     /*
1278      * TODO: If we had a main 'reset container' that the whole system
1279      * lived in, we could reset that using the multi-phase reset
1280      * APIs. For the moment, we just reset the sysbus, which will cause
1281      * all devices hanging off it (and all their child buses, recursively)
1282      * to be reset. Note that this will *not* reset any Device objects
1283      * which are not attached to some part of the qbus tree!
1284      */
1285     qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1286 
1287     notifier_list_notify(&machine_init_done_notifiers, NULL);
1288 
1289     if (rom_check_and_register_reset() != 0) {
1290         exit(1);
1291     }
1292 
1293     replay_start();
1294 
1295     /* This checkpoint is required by replay to separate prior clock
1296        reading from the other reads, because timer polling functions query
1297        clock values from the log. */
1298     replay_checkpoint(CHECKPOINT_RESET);
1299     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1300     register_global_state();
1301 }
1302 
1303 static const TypeInfo machine_info = {
1304     .name = TYPE_MACHINE,
1305     .parent = TYPE_OBJECT,
1306     .abstract = true,
1307     .class_size = sizeof(MachineClass),
1308     .class_init    = machine_class_init,
1309     .class_base_init = machine_class_base_init,
1310     .instance_size = sizeof(MachineState),
1311     .instance_init = machine_initfn,
1312     .instance_finalize = machine_finalize,
1313 };
1314 
1315 static void machine_register_types(void)
1316 {
1317     type_register_static(&machine_info);
1318 }
1319 
1320 type_init(machine_register_types)
1321