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