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