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