xref: /qemu/hw/core/machine.c (revision a1fadbcf)
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 #include "hw/virtio/virtio-net.h"
42 
43 GlobalProperty hw_compat_8_1[] = {};
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 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
690 {
691     int i;
692     HotpluggableCPUList *head = NULL;
693     MachineClass *mc = MACHINE_GET_CLASS(machine);
694 
695     /* force board to initialize possible_cpus if it hasn't been done yet */
696     mc->possible_cpu_arch_ids(machine);
697 
698     for (i = 0; i < machine->possible_cpus->len; i++) {
699         Object *cpu;
700         HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
701 
702         cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
703         cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
704         cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
705                                    sizeof(*cpu_item->props));
706 
707         cpu = machine->possible_cpus->cpus[i].cpu;
708         if (cpu) {
709             cpu_item->qom_path = object_get_canonical_path(cpu);
710         }
711         QAPI_LIST_PREPEND(head, cpu_item);
712     }
713     return head;
714 }
715 
716 /**
717  * machine_set_cpu_numa_node:
718  * @machine: machine object to modify
719  * @props: specifies which cpu objects to assign to
720  *         numa node specified by @props.node_id
721  * @errp: if an error occurs, a pointer to an area to store the error
722  *
723  * Associate NUMA node specified by @props.node_id with cpu slots that
724  * match socket/core/thread-ids specified by @props. It's recommended to use
725  * query-hotpluggable-cpus.props values to specify affected cpu slots,
726  * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
727  *
728  * However for CLI convenience it's possible to pass in subset of properties,
729  * which would affect all cpu slots that match it.
730  * Ex for pc machine:
731  *    -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
732  *    -numa cpu,node-id=0,socket_id=0 \
733  *    -numa cpu,node-id=1,socket_id=1
734  * will assign all child cores of socket 0 to node 0 and
735  * of socket 1 to node 1.
736  *
737  * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
738  * return error.
739  * Empty subset is disallowed and function will return with error in this case.
740  */
741 void machine_set_cpu_numa_node(MachineState *machine,
742                                const CpuInstanceProperties *props, Error **errp)
743 {
744     MachineClass *mc = MACHINE_GET_CLASS(machine);
745     NodeInfo *numa_info = machine->numa_state->nodes;
746     bool match = false;
747     int i;
748 
749     if (!mc->possible_cpu_arch_ids) {
750         error_setg(errp, "mapping of CPUs to NUMA node is not supported");
751         return;
752     }
753 
754     /* disabling node mapping is not supported, forbid it */
755     assert(props->has_node_id);
756 
757     /* force board to initialize possible_cpus if it hasn't been done yet */
758     mc->possible_cpu_arch_ids(machine);
759 
760     for (i = 0; i < machine->possible_cpus->len; i++) {
761         CPUArchId *slot = &machine->possible_cpus->cpus[i];
762 
763         /* reject unsupported by board properties */
764         if (props->has_thread_id && !slot->props.has_thread_id) {
765             error_setg(errp, "thread-id is not supported");
766             return;
767         }
768 
769         if (props->has_core_id && !slot->props.has_core_id) {
770             error_setg(errp, "core-id is not supported");
771             return;
772         }
773 
774         if (props->has_cluster_id && !slot->props.has_cluster_id) {
775             error_setg(errp, "cluster-id is not supported");
776             return;
777         }
778 
779         if (props->has_socket_id && !slot->props.has_socket_id) {
780             error_setg(errp, "socket-id is not supported");
781             return;
782         }
783 
784         if (props->has_die_id && !slot->props.has_die_id) {
785             error_setg(errp, "die-id is not supported");
786             return;
787         }
788 
789         /* skip slots with explicit mismatch */
790         if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
791                 continue;
792         }
793 
794         if (props->has_core_id && props->core_id != slot->props.core_id) {
795                 continue;
796         }
797 
798         if (props->has_cluster_id &&
799             props->cluster_id != slot->props.cluster_id) {
800                 continue;
801         }
802 
803         if (props->has_die_id && props->die_id != slot->props.die_id) {
804                 continue;
805         }
806 
807         if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
808                 continue;
809         }
810 
811         /* reject assignment if slot is already assigned, for compatibility
812          * of legacy cpu_index mapping with SPAPR core based mapping do not
813          * error out if cpu thread and matched core have the same node-id */
814         if (slot->props.has_node_id &&
815             slot->props.node_id != props->node_id) {
816             error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
817                        slot->props.node_id);
818             return;
819         }
820 
821         /* assign slot to node as it's matched '-numa cpu' key */
822         match = true;
823         slot->props.node_id = props->node_id;
824         slot->props.has_node_id = props->has_node_id;
825 
826         if (machine->numa_state->hmat_enabled) {
827             if ((numa_info[props->node_id].initiator < MAX_NODES) &&
828                 (props->node_id != numa_info[props->node_id].initiator)) {
829                 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
830                            " should be itself (got %" PRIu16 ")",
831                            props->node_id, numa_info[props->node_id].initiator);
832                 return;
833             }
834             numa_info[props->node_id].has_cpu = true;
835             numa_info[props->node_id].initiator = props->node_id;
836         }
837     }
838 
839     if (!match) {
840         error_setg(errp, "no match found");
841     }
842 }
843 
844 static void machine_get_smp(Object *obj, Visitor *v, const char *name,
845                             void *opaque, Error **errp)
846 {
847     MachineState *ms = MACHINE(obj);
848     SMPConfiguration *config = &(SMPConfiguration){
849         .has_cpus = true, .cpus = ms->smp.cpus,
850         .has_sockets = true, .sockets = ms->smp.sockets,
851         .has_dies = true, .dies = ms->smp.dies,
852         .has_clusters = true, .clusters = ms->smp.clusters,
853         .has_cores = true, .cores = ms->smp.cores,
854         .has_threads = true, .threads = ms->smp.threads,
855         .has_maxcpus = true, .maxcpus = ms->smp.max_cpus,
856     };
857 
858     if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) {
859         return;
860     }
861 }
862 
863 static void machine_set_smp(Object *obj, Visitor *v, const char *name,
864                             void *opaque, Error **errp)
865 {
866     MachineState *ms = MACHINE(obj);
867     g_autoptr(SMPConfiguration) config = NULL;
868 
869     if (!visit_type_SMPConfiguration(v, name, &config, errp)) {
870         return;
871     }
872 
873     machine_parse_smp_config(ms, config, errp);
874 }
875 
876 static void machine_get_boot(Object *obj, Visitor *v, const char *name,
877                             void *opaque, Error **errp)
878 {
879     MachineState *ms = MACHINE(obj);
880     BootConfiguration *config = &ms->boot_config;
881     visit_type_BootConfiguration(v, name, &config, &error_abort);
882 }
883 
884 static void machine_free_boot_config(MachineState *ms)
885 {
886     g_free(ms->boot_config.order);
887     g_free(ms->boot_config.once);
888     g_free(ms->boot_config.splash);
889 }
890 
891 static void machine_copy_boot_config(MachineState *ms, BootConfiguration *config)
892 {
893     MachineClass *machine_class = MACHINE_GET_CLASS(ms);
894 
895     machine_free_boot_config(ms);
896     ms->boot_config = *config;
897     if (!config->order) {
898         ms->boot_config.order = g_strdup(machine_class->default_boot_order);
899     }
900 }
901 
902 static void machine_set_boot(Object *obj, Visitor *v, const char *name,
903                             void *opaque, Error **errp)
904 {
905     ERRP_GUARD();
906     MachineState *ms = MACHINE(obj);
907     BootConfiguration *config = NULL;
908 
909     if (!visit_type_BootConfiguration(v, name, &config, errp)) {
910         return;
911     }
912     if (config->order) {
913         validate_bootdevices(config->order, errp);
914         if (*errp) {
915             goto out_free;
916         }
917     }
918     if (config->once) {
919         validate_bootdevices(config->once, errp);
920         if (*errp) {
921             goto out_free;
922         }
923     }
924 
925     machine_copy_boot_config(ms, config);
926     /* Strings live in ms->boot_config.  */
927     free(config);
928     return;
929 
930 out_free:
931     qapi_free_BootConfiguration(config);
932 }
933 
934 static void machine_class_init(ObjectClass *oc, void *data)
935 {
936     MachineClass *mc = MACHINE_CLASS(oc);
937 
938     /* Default 128 MB as guest ram size */
939     mc->default_ram_size = 128 * MiB;
940     mc->rom_file_has_mr = true;
941 
942     /* numa node memory size aligned on 8MB by default.
943      * On Linux, each node's border has to be 8MB aligned
944      */
945     mc->numa_mem_align_shift = 23;
946 
947     object_class_property_add_str(oc, "kernel",
948         machine_get_kernel, machine_set_kernel);
949     object_class_property_set_description(oc, "kernel",
950         "Linux kernel image file");
951 
952     object_class_property_add_str(oc, "initrd",
953         machine_get_initrd, machine_set_initrd);
954     object_class_property_set_description(oc, "initrd",
955         "Linux initial ramdisk file");
956 
957     object_class_property_add_str(oc, "append",
958         machine_get_append, machine_set_append);
959     object_class_property_set_description(oc, "append",
960         "Linux kernel command line");
961 
962     object_class_property_add_str(oc, "dtb",
963         machine_get_dtb, machine_set_dtb);
964     object_class_property_set_description(oc, "dtb",
965         "Linux kernel device tree file");
966 
967     object_class_property_add_str(oc, "dumpdtb",
968         machine_get_dumpdtb, machine_set_dumpdtb);
969     object_class_property_set_description(oc, "dumpdtb",
970         "Dump current dtb to a file and quit");
971 
972     object_class_property_add(oc, "boot", "BootConfiguration",
973         machine_get_boot, machine_set_boot,
974         NULL, NULL);
975     object_class_property_set_description(oc, "boot",
976         "Boot configuration");
977 
978     object_class_property_add(oc, "smp", "SMPConfiguration",
979         machine_get_smp, machine_set_smp,
980         NULL, NULL);
981     object_class_property_set_description(oc, "smp",
982         "CPU topology");
983 
984     object_class_property_add(oc, "phandle-start", "int",
985         machine_get_phandle_start, machine_set_phandle_start,
986         NULL, NULL);
987     object_class_property_set_description(oc, "phandle-start",
988         "The first phandle ID we may generate dynamically");
989 
990     object_class_property_add_str(oc, "dt-compatible",
991         machine_get_dt_compatible, machine_set_dt_compatible);
992     object_class_property_set_description(oc, "dt-compatible",
993         "Overrides the \"compatible\" property of the dt root node");
994 
995     object_class_property_add_bool(oc, "dump-guest-core",
996         machine_get_dump_guest_core, machine_set_dump_guest_core);
997     object_class_property_set_description(oc, "dump-guest-core",
998         "Include guest memory in a core dump");
999 
1000     object_class_property_add_bool(oc, "mem-merge",
1001         machine_get_mem_merge, machine_set_mem_merge);
1002     object_class_property_set_description(oc, "mem-merge",
1003         "Enable/disable memory merge support");
1004 
1005     object_class_property_add_bool(oc, "usb",
1006         machine_get_usb, machine_set_usb);
1007     object_class_property_set_description(oc, "usb",
1008         "Set on/off to enable/disable usb");
1009 
1010     object_class_property_add_bool(oc, "graphics",
1011         machine_get_graphics, machine_set_graphics);
1012     object_class_property_set_description(oc, "graphics",
1013         "Set on/off to enable/disable graphics emulation");
1014 
1015     object_class_property_add_str(oc, "firmware",
1016         machine_get_firmware, machine_set_firmware);
1017     object_class_property_set_description(oc, "firmware",
1018         "Firmware image");
1019 
1020     object_class_property_add_bool(oc, "suppress-vmdesc",
1021         machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
1022     object_class_property_set_description(oc, "suppress-vmdesc",
1023         "Set on to disable self-describing migration");
1024 
1025     object_class_property_add_link(oc, "confidential-guest-support",
1026                                    TYPE_CONFIDENTIAL_GUEST_SUPPORT,
1027                                    offsetof(MachineState, cgs),
1028                                    machine_check_confidential_guest_support,
1029                                    OBJ_PROP_LINK_STRONG);
1030     object_class_property_set_description(oc, "confidential-guest-support",
1031                                           "Set confidential guest scheme to support");
1032 
1033     /* For compatibility */
1034     object_class_property_add_str(oc, "memory-encryption",
1035         machine_get_memory_encryption, machine_set_memory_encryption);
1036     object_class_property_set_description(oc, "memory-encryption",
1037         "Set memory encryption object to use");
1038 
1039     object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKEND,
1040                                    offsetof(MachineState, memdev), object_property_allow_set_link,
1041                                    OBJ_PROP_LINK_STRONG);
1042     object_class_property_set_description(oc, "memory-backend",
1043                                           "Set RAM backend"
1044                                           "Valid value is ID of hostmem based backend");
1045 
1046     object_class_property_add(oc, "memory", "MemorySizeConfiguration",
1047         machine_get_mem, machine_set_mem,
1048         NULL, NULL);
1049     object_class_property_set_description(oc, "memory",
1050         "Memory size configuration");
1051 }
1052 
1053 static void machine_class_base_init(ObjectClass *oc, void *data)
1054 {
1055     MachineClass *mc = MACHINE_CLASS(oc);
1056     mc->max_cpus = mc->max_cpus ?: 1;
1057     mc->min_cpus = mc->min_cpus ?: 1;
1058     mc->default_cpus = mc->default_cpus ?: 1;
1059 
1060     if (!object_class_is_abstract(oc)) {
1061         const char *cname = object_class_get_name(oc);
1062         assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
1063         mc->name = g_strndup(cname,
1064                             strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
1065         mc->compat_props = g_ptr_array_new();
1066     }
1067 }
1068 
1069 static void machine_initfn(Object *obj)
1070 {
1071     MachineState *ms = MACHINE(obj);
1072     MachineClass *mc = MACHINE_GET_CLASS(obj);
1073 
1074     container_get(obj, "/peripheral");
1075     container_get(obj, "/peripheral-anon");
1076 
1077     ms->dump_guest_core = true;
1078     ms->mem_merge = true;
1079     ms->enable_graphics = true;
1080     ms->kernel_cmdline = g_strdup("");
1081     ms->ram_size = mc->default_ram_size;
1082     ms->maxram_size = mc->default_ram_size;
1083 
1084     if (mc->nvdimm_supported) {
1085         Object *obj = OBJECT(ms);
1086 
1087         ms->nvdimms_state = g_new0(NVDIMMState, 1);
1088         object_property_add_bool(obj, "nvdimm",
1089                                  machine_get_nvdimm, machine_set_nvdimm);
1090         object_property_set_description(obj, "nvdimm",
1091                                         "Set on/off to enable/disable "
1092                                         "NVDIMM instantiation");
1093 
1094         object_property_add_str(obj, "nvdimm-persistence",
1095                                 machine_get_nvdimm_persistence,
1096                                 machine_set_nvdimm_persistence);
1097         object_property_set_description(obj, "nvdimm-persistence",
1098                                         "Set NVDIMM persistence"
1099                                         "Valid values are cpu, mem-ctrl");
1100     }
1101 
1102     if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
1103         ms->numa_state = g_new0(NumaState, 1);
1104         object_property_add_bool(obj, "hmat",
1105                                  machine_get_hmat, machine_set_hmat);
1106         object_property_set_description(obj, "hmat",
1107                                         "Set on/off to enable/disable "
1108                                         "ACPI Heterogeneous Memory Attribute "
1109                                         "Table (HMAT)");
1110     }
1111 
1112     /* default to mc->default_cpus */
1113     ms->smp.cpus = mc->default_cpus;
1114     ms->smp.max_cpus = mc->default_cpus;
1115     ms->smp.sockets = 1;
1116     ms->smp.dies = 1;
1117     ms->smp.clusters = 1;
1118     ms->smp.cores = 1;
1119     ms->smp.threads = 1;
1120 
1121     machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
1122 }
1123 
1124 static void machine_finalize(Object *obj)
1125 {
1126     MachineState *ms = MACHINE(obj);
1127 
1128     machine_free_boot_config(ms);
1129     g_free(ms->kernel_filename);
1130     g_free(ms->initrd_filename);
1131     g_free(ms->kernel_cmdline);
1132     g_free(ms->dtb);
1133     g_free(ms->dumpdtb);
1134     g_free(ms->dt_compatible);
1135     g_free(ms->firmware);
1136     g_free(ms->device_memory);
1137     g_free(ms->nvdimms_state);
1138     g_free(ms->numa_state);
1139 }
1140 
1141 bool machine_usb(MachineState *machine)
1142 {
1143     return machine->usb;
1144 }
1145 
1146 int machine_phandle_start(MachineState *machine)
1147 {
1148     return machine->phandle_start;
1149 }
1150 
1151 bool machine_dump_guest_core(MachineState *machine)
1152 {
1153     return machine->dump_guest_core;
1154 }
1155 
1156 bool machine_mem_merge(MachineState *machine)
1157 {
1158     return machine->mem_merge;
1159 }
1160 
1161 static char *cpu_slot_to_string(const CPUArchId *cpu)
1162 {
1163     GString *s = g_string_new(NULL);
1164     if (cpu->props.has_socket_id) {
1165         g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1166     }
1167     if (cpu->props.has_die_id) {
1168         if (s->len) {
1169             g_string_append_printf(s, ", ");
1170         }
1171         g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1172     }
1173     if (cpu->props.has_cluster_id) {
1174         if (s->len) {
1175             g_string_append_printf(s, ", ");
1176         }
1177         g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
1178     }
1179     if (cpu->props.has_core_id) {
1180         if (s->len) {
1181             g_string_append_printf(s, ", ");
1182         }
1183         g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1184     }
1185     if (cpu->props.has_thread_id) {
1186         if (s->len) {
1187             g_string_append_printf(s, ", ");
1188         }
1189         g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1190     }
1191     return g_string_free(s, false);
1192 }
1193 
1194 static void numa_validate_initiator(NumaState *numa_state)
1195 {
1196     int i;
1197     NodeInfo *numa_info = numa_state->nodes;
1198 
1199     for (i = 0; i < numa_state->num_nodes; i++) {
1200         if (numa_info[i].initiator == MAX_NODES) {
1201             continue;
1202         }
1203 
1204         if (!numa_info[numa_info[i].initiator].present) {
1205             error_report("NUMA node %" PRIu16 " is missing, use "
1206                          "'-numa node' option to declare it first",
1207                          numa_info[i].initiator);
1208             exit(1);
1209         }
1210 
1211         if (!numa_info[numa_info[i].initiator].has_cpu) {
1212             error_report("The initiator of NUMA node %d is invalid", i);
1213             exit(1);
1214         }
1215     }
1216 }
1217 
1218 static void machine_numa_finish_cpu_init(MachineState *machine)
1219 {
1220     int i;
1221     bool default_mapping;
1222     GString *s = g_string_new(NULL);
1223     MachineClass *mc = MACHINE_GET_CLASS(machine);
1224     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1225 
1226     assert(machine->numa_state->num_nodes);
1227     for (i = 0; i < possible_cpus->len; i++) {
1228         if (possible_cpus->cpus[i].props.has_node_id) {
1229             break;
1230         }
1231     }
1232     default_mapping = (i == possible_cpus->len);
1233 
1234     for (i = 0; i < possible_cpus->len; i++) {
1235         const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1236 
1237         if (!cpu_slot->props.has_node_id) {
1238             /* fetch default mapping from board and enable it */
1239             CpuInstanceProperties props = cpu_slot->props;
1240 
1241             props.node_id = mc->get_default_cpu_node_id(machine, i);
1242             if (!default_mapping) {
1243                 /* record slots with not set mapping,
1244                  * TODO: make it hard error in future */
1245                 char *cpu_str = cpu_slot_to_string(cpu_slot);
1246                 g_string_append_printf(s, "%sCPU %d [%s]",
1247                                        s->len ? ", " : "", i, cpu_str);
1248                 g_free(cpu_str);
1249 
1250                 /* non mapped cpus used to fallback to node 0 */
1251                 props.node_id = 0;
1252             }
1253 
1254             props.has_node_id = true;
1255             machine_set_cpu_numa_node(machine, &props, &error_fatal);
1256         }
1257     }
1258 
1259     if (machine->numa_state->hmat_enabled) {
1260         numa_validate_initiator(machine->numa_state);
1261     }
1262 
1263     if (s->len && !qtest_enabled()) {
1264         warn_report("CPU(s) not present in any NUMA nodes: %s",
1265                     s->str);
1266         warn_report("All CPU(s) up to maxcpus should be described "
1267                     "in NUMA config, ability to start up with partial NUMA "
1268                     "mappings is obsoleted and will be removed in future");
1269     }
1270     g_string_free(s, true);
1271 }
1272 
1273 static void validate_cpu_cluster_to_numa_boundary(MachineState *ms)
1274 {
1275     MachineClass *mc = MACHINE_GET_CLASS(ms);
1276     NumaState *state = ms->numa_state;
1277     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1278     const CPUArchId *cpus = possible_cpus->cpus;
1279     int i, j;
1280 
1281     if (state->num_nodes <= 1 || possible_cpus->len <= 1) {
1282         return;
1283     }
1284 
1285     /*
1286      * The Linux scheduling domain can't be parsed when the multiple CPUs
1287      * in one cluster have been associated with different NUMA nodes. However,
1288      * it's fine to associate one NUMA node with CPUs in different clusters.
1289      */
1290     for (i = 0; i < possible_cpus->len; i++) {
1291         for (j = i + 1; j < possible_cpus->len; j++) {
1292             if (cpus[i].props.has_socket_id &&
1293                 cpus[i].props.has_cluster_id &&
1294                 cpus[i].props.has_node_id &&
1295                 cpus[j].props.has_socket_id &&
1296                 cpus[j].props.has_cluster_id &&
1297                 cpus[j].props.has_node_id &&
1298                 cpus[i].props.socket_id == cpus[j].props.socket_id &&
1299                 cpus[i].props.cluster_id == cpus[j].props.cluster_id &&
1300                 cpus[i].props.node_id != cpus[j].props.node_id) {
1301                 warn_report("CPU-%d and CPU-%d in socket-%" PRId64 "-cluster-%" PRId64
1302                              " have been associated with node-%" PRId64 " and node-%" PRId64
1303                              " respectively. It can cause OSes like Linux to"
1304                              " misbehave", i, j, cpus[i].props.socket_id,
1305                              cpus[i].props.cluster_id, cpus[i].props.node_id,
1306                              cpus[j].props.node_id);
1307             }
1308         }
1309     }
1310 }
1311 
1312 MemoryRegion *machine_consume_memdev(MachineState *machine,
1313                                      HostMemoryBackend *backend)
1314 {
1315     MemoryRegion *ret = host_memory_backend_get_memory(backend);
1316 
1317     if (host_memory_backend_is_mapped(backend)) {
1318         error_report("memory backend %s can't be used multiple times.",
1319                      object_get_canonical_path_component(OBJECT(backend)));
1320         exit(EXIT_FAILURE);
1321     }
1322     host_memory_backend_set_mapped(backend, true);
1323     vmstate_register_ram_global(ret);
1324     return ret;
1325 }
1326 
1327 static bool create_default_memdev(MachineState *ms, const char *path, Error **errp)
1328 {
1329     Object *obj;
1330     MachineClass *mc = MACHINE_GET_CLASS(ms);
1331     bool r = false;
1332 
1333     obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
1334     if (path) {
1335         if (!object_property_set_str(obj, "mem-path", path, errp)) {
1336             goto out;
1337         }
1338     }
1339     if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
1340         goto out;
1341     }
1342     object_property_add_child(object_get_objects_root(), mc->default_ram_id,
1343                               obj);
1344     /* Ensure backend's memory region name is equal to mc->default_ram_id */
1345     if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
1346                              false, errp)) {
1347         goto out;
1348     }
1349     if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
1350         goto out;
1351     }
1352     r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
1353 
1354 out:
1355     object_unref(obj);
1356     return r;
1357 }
1358 
1359 
1360 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
1361 {
1362     ERRP_GUARD();
1363     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1364     ObjectClass *oc = object_class_by_name(machine->cpu_type);
1365     CPUClass *cc;
1366 
1367     /* This checkpoint is required by replay to separate prior clock
1368        reading from the other reads, because timer polling functions query
1369        clock values from the log. */
1370     replay_checkpoint(CHECKPOINT_INIT);
1371 
1372     if (!xen_enabled()) {
1373         /* On 32-bit hosts, QEMU is limited by virtual address space */
1374         if (machine->ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1375             error_setg(errp, "at most 2047 MB RAM can be simulated");
1376             return;
1377         }
1378     }
1379 
1380     if (machine->memdev) {
1381         ram_addr_t backend_size = object_property_get_uint(OBJECT(machine->memdev),
1382                                                            "size",  &error_abort);
1383         if (backend_size != machine->ram_size) {
1384             error_setg(errp, "Machine memory size does not match the size of the memory backend");
1385             return;
1386         }
1387     } else if (machine_class->default_ram_id && machine->ram_size &&
1388                numa_uses_legacy_mem()) {
1389         if (object_property_find(object_get_objects_root(),
1390                                  machine_class->default_ram_id)) {
1391             error_setg(errp, "object's id '%s' is reserved for the default"
1392                 " RAM backend, it can't be used for any other purposes",
1393                 machine_class->default_ram_id);
1394             error_append_hint(errp,
1395                 "Change the object's 'id' to something else or disable"
1396                 " automatic creation of the default RAM backend by setting"
1397                 " 'memory-backend=%s' with '-machine'.\n",
1398                 machine_class->default_ram_id);
1399             return;
1400         }
1401         if (!create_default_memdev(current_machine, mem_path, errp)) {
1402             return;
1403         }
1404     }
1405 
1406     if (machine->numa_state) {
1407         numa_complete_configuration(machine);
1408         if (machine->numa_state->num_nodes) {
1409             machine_numa_finish_cpu_init(machine);
1410             if (machine_class->cpu_cluster_has_numa_boundary) {
1411                 validate_cpu_cluster_to_numa_boundary(machine);
1412             }
1413         }
1414     }
1415 
1416     if (!machine->ram && machine->memdev) {
1417         machine->ram = machine_consume_memdev(machine, machine->memdev);
1418     }
1419 
1420     /* If the machine supports the valid_cpu_types check and the user
1421      * specified a CPU with -cpu check here that the user CPU is supported.
1422      */
1423     if (machine_class->valid_cpu_types && machine->cpu_type) {
1424         int i;
1425 
1426         for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1427             if (object_class_dynamic_cast(oc,
1428                                           machine_class->valid_cpu_types[i])) {
1429                 /* The user specified CPU is in the valid field, we are
1430                  * good to go.
1431                  */
1432                 break;
1433             }
1434         }
1435 
1436         if (!machine_class->valid_cpu_types[i]) {
1437             /* The user specified CPU is not valid */
1438             error_report("Invalid CPU type: %s", machine->cpu_type);
1439             error_printf("The valid types are: %s",
1440                          machine_class->valid_cpu_types[0]);
1441             for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1442                 error_printf(", %s", machine_class->valid_cpu_types[i]);
1443             }
1444             error_printf("\n");
1445 
1446             exit(1);
1447         }
1448     }
1449 
1450     /* Check if CPU type is deprecated and warn if so */
1451     cc = CPU_CLASS(oc);
1452     if (cc && cc->deprecation_note) {
1453         warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1454                     cc->deprecation_note);
1455     }
1456 
1457     if (machine->cgs) {
1458         /*
1459          * With confidential guests, the host can't see the real
1460          * contents of RAM, so there's no point in it trying to merge
1461          * areas.
1462          */
1463         machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1464 
1465         /*
1466          * Virtio devices can't count on directly accessing guest
1467          * memory, so they need iommu_platform=on to use normal DMA
1468          * mechanisms.  That requires also disabling legacy virtio
1469          * support for those virtio pci devices which allow it.
1470          */
1471         object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1472                                    "on", true);
1473         object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1474                                    "on", false);
1475     }
1476 
1477     accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1478     machine_class->init(machine);
1479     phase_advance(PHASE_MACHINE_INITIALIZED);
1480 }
1481 
1482 static NotifierList machine_init_done_notifiers =
1483     NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1484 
1485 void qemu_add_machine_init_done_notifier(Notifier *notify)
1486 {
1487     notifier_list_add(&machine_init_done_notifiers, notify);
1488     if (phase_check(PHASE_MACHINE_READY)) {
1489         notify->notify(notify, NULL);
1490     }
1491 }
1492 
1493 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1494 {
1495     notifier_remove(notify);
1496 }
1497 
1498 void qdev_machine_creation_done(void)
1499 {
1500     cpu_synchronize_all_post_init();
1501 
1502     if (current_machine->boot_config.once) {
1503         qemu_boot_set(current_machine->boot_config.once, &error_fatal);
1504         qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_config.order));
1505     }
1506 
1507     /*
1508      * ok, initial machine setup is done, starting from now we can
1509      * only create hotpluggable devices
1510      */
1511     phase_advance(PHASE_MACHINE_READY);
1512     qdev_assert_realized_properly();
1513 
1514     /* TODO: once all bus devices are qdevified, this should be done
1515      * when bus is created by qdev.c */
1516     /*
1517      * TODO: If we had a main 'reset container' that the whole system
1518      * lived in, we could reset that using the multi-phase reset
1519      * APIs. For the moment, we just reset the sysbus, which will cause
1520      * all devices hanging off it (and all their child buses, recursively)
1521      * to be reset. Note that this will *not* reset any Device objects
1522      * which are not attached to some part of the qbus tree!
1523      */
1524     qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1525 
1526     notifier_list_notify(&machine_init_done_notifiers, NULL);
1527 
1528     if (rom_check_and_register_reset() != 0) {
1529         exit(1);
1530     }
1531 
1532     replay_start();
1533 
1534     /* This checkpoint is required by replay to separate prior clock
1535        reading from the other reads, because timer polling functions query
1536        clock values from the log. */
1537     replay_checkpoint(CHECKPOINT_RESET);
1538     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1539     register_global_state();
1540 }
1541 
1542 static const TypeInfo machine_info = {
1543     .name = TYPE_MACHINE,
1544     .parent = TYPE_OBJECT,
1545     .abstract = true,
1546     .class_size = sizeof(MachineClass),
1547     .class_init    = machine_class_init,
1548     .class_base_init = machine_class_base_init,
1549     .instance_size = sizeof(MachineState),
1550     .instance_init = machine_initfn,
1551     .instance_finalize = machine_finalize,
1552 };
1553 
1554 static void machine_register_types(void)
1555 {
1556     type_register_static(&machine_info);
1557 }
1558 
1559 type_init(machine_register_types)
1560