xref: /openbmc/qemu/hw/core/machine.c (revision 4dba9141)
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         ms->nvdimms_state = g_new0(NVDIMMState, 1);
1086         object_property_add_bool(obj, "nvdimm",
1087                                  machine_get_nvdimm, machine_set_nvdimm);
1088         object_property_set_description(obj, "nvdimm",
1089                                         "Set on/off to enable/disable "
1090                                         "NVDIMM instantiation");
1091 
1092         object_property_add_str(obj, "nvdimm-persistence",
1093                                 machine_get_nvdimm_persistence,
1094                                 machine_set_nvdimm_persistence);
1095         object_property_set_description(obj, "nvdimm-persistence",
1096                                         "Set NVDIMM persistence"
1097                                         "Valid values are cpu, mem-ctrl");
1098     }
1099 
1100     if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
1101         ms->numa_state = g_new0(NumaState, 1);
1102         object_property_add_bool(obj, "hmat",
1103                                  machine_get_hmat, machine_set_hmat);
1104         object_property_set_description(obj, "hmat",
1105                                         "Set on/off to enable/disable "
1106                                         "ACPI Heterogeneous Memory Attribute "
1107                                         "Table (HMAT)");
1108     }
1109 
1110     /* default to mc->default_cpus */
1111     ms->smp.cpus = mc->default_cpus;
1112     ms->smp.max_cpus = mc->default_cpus;
1113     ms->smp.sockets = 1;
1114     ms->smp.dies = 1;
1115     ms->smp.clusters = 1;
1116     ms->smp.cores = 1;
1117     ms->smp.threads = 1;
1118 
1119     machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
1120 }
1121 
1122 static void machine_finalize(Object *obj)
1123 {
1124     MachineState *ms = MACHINE(obj);
1125 
1126     machine_free_boot_config(ms);
1127     g_free(ms->kernel_filename);
1128     g_free(ms->initrd_filename);
1129     g_free(ms->kernel_cmdline);
1130     g_free(ms->dtb);
1131     g_free(ms->dumpdtb);
1132     g_free(ms->dt_compatible);
1133     g_free(ms->firmware);
1134     g_free(ms->device_memory);
1135     g_free(ms->nvdimms_state);
1136     g_free(ms->numa_state);
1137 }
1138 
1139 bool machine_usb(MachineState *machine)
1140 {
1141     return machine->usb;
1142 }
1143 
1144 int machine_phandle_start(MachineState *machine)
1145 {
1146     return machine->phandle_start;
1147 }
1148 
1149 bool machine_dump_guest_core(MachineState *machine)
1150 {
1151     return machine->dump_guest_core;
1152 }
1153 
1154 bool machine_mem_merge(MachineState *machine)
1155 {
1156     return machine->mem_merge;
1157 }
1158 
1159 static char *cpu_slot_to_string(const CPUArchId *cpu)
1160 {
1161     GString *s = g_string_new(NULL);
1162     if (cpu->props.has_socket_id) {
1163         g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1164     }
1165     if (cpu->props.has_die_id) {
1166         if (s->len) {
1167             g_string_append_printf(s, ", ");
1168         }
1169         g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1170     }
1171     if (cpu->props.has_cluster_id) {
1172         if (s->len) {
1173             g_string_append_printf(s, ", ");
1174         }
1175         g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
1176     }
1177     if (cpu->props.has_core_id) {
1178         if (s->len) {
1179             g_string_append_printf(s, ", ");
1180         }
1181         g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1182     }
1183     if (cpu->props.has_thread_id) {
1184         if (s->len) {
1185             g_string_append_printf(s, ", ");
1186         }
1187         g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1188     }
1189     return g_string_free(s, false);
1190 }
1191 
1192 static void numa_validate_initiator(NumaState *numa_state)
1193 {
1194     int i;
1195     NodeInfo *numa_info = numa_state->nodes;
1196 
1197     for (i = 0; i < numa_state->num_nodes; i++) {
1198         if (numa_info[i].initiator == MAX_NODES) {
1199             continue;
1200         }
1201 
1202         if (!numa_info[numa_info[i].initiator].present) {
1203             error_report("NUMA node %" PRIu16 " is missing, use "
1204                          "'-numa node' option to declare it first",
1205                          numa_info[i].initiator);
1206             exit(1);
1207         }
1208 
1209         if (!numa_info[numa_info[i].initiator].has_cpu) {
1210             error_report("The initiator of NUMA node %d is invalid", i);
1211             exit(1);
1212         }
1213     }
1214 }
1215 
1216 static void machine_numa_finish_cpu_init(MachineState *machine)
1217 {
1218     int i;
1219     bool default_mapping;
1220     GString *s = g_string_new(NULL);
1221     MachineClass *mc = MACHINE_GET_CLASS(machine);
1222     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1223 
1224     assert(machine->numa_state->num_nodes);
1225     for (i = 0; i < possible_cpus->len; i++) {
1226         if (possible_cpus->cpus[i].props.has_node_id) {
1227             break;
1228         }
1229     }
1230     default_mapping = (i == possible_cpus->len);
1231 
1232     for (i = 0; i < possible_cpus->len; i++) {
1233         const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1234 
1235         if (!cpu_slot->props.has_node_id) {
1236             /* fetch default mapping from board and enable it */
1237             CpuInstanceProperties props = cpu_slot->props;
1238 
1239             props.node_id = mc->get_default_cpu_node_id(machine, i);
1240             if (!default_mapping) {
1241                 /* record slots with not set mapping,
1242                  * TODO: make it hard error in future */
1243                 char *cpu_str = cpu_slot_to_string(cpu_slot);
1244                 g_string_append_printf(s, "%sCPU %d [%s]",
1245                                        s->len ? ", " : "", i, cpu_str);
1246                 g_free(cpu_str);
1247 
1248                 /* non mapped cpus used to fallback to node 0 */
1249                 props.node_id = 0;
1250             }
1251 
1252             props.has_node_id = true;
1253             machine_set_cpu_numa_node(machine, &props, &error_fatal);
1254         }
1255     }
1256 
1257     if (machine->numa_state->hmat_enabled) {
1258         numa_validate_initiator(machine->numa_state);
1259     }
1260 
1261     if (s->len && !qtest_enabled()) {
1262         warn_report("CPU(s) not present in any NUMA nodes: %s",
1263                     s->str);
1264         warn_report("All CPU(s) up to maxcpus should be described "
1265                     "in NUMA config, ability to start up with partial NUMA "
1266                     "mappings is obsoleted and will be removed in future");
1267     }
1268     g_string_free(s, true);
1269 }
1270 
1271 static void validate_cpu_cluster_to_numa_boundary(MachineState *ms)
1272 {
1273     MachineClass *mc = MACHINE_GET_CLASS(ms);
1274     NumaState *state = ms->numa_state;
1275     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1276     const CPUArchId *cpus = possible_cpus->cpus;
1277     int i, j;
1278 
1279     if (state->num_nodes <= 1 || possible_cpus->len <= 1) {
1280         return;
1281     }
1282 
1283     /*
1284      * The Linux scheduling domain can't be parsed when the multiple CPUs
1285      * in one cluster have been associated with different NUMA nodes. However,
1286      * it's fine to associate one NUMA node with CPUs in different clusters.
1287      */
1288     for (i = 0; i < possible_cpus->len; i++) {
1289         for (j = i + 1; j < possible_cpus->len; j++) {
1290             if (cpus[i].props.has_socket_id &&
1291                 cpus[i].props.has_cluster_id &&
1292                 cpus[i].props.has_node_id &&
1293                 cpus[j].props.has_socket_id &&
1294                 cpus[j].props.has_cluster_id &&
1295                 cpus[j].props.has_node_id &&
1296                 cpus[i].props.socket_id == cpus[j].props.socket_id &&
1297                 cpus[i].props.cluster_id == cpus[j].props.cluster_id &&
1298                 cpus[i].props.node_id != cpus[j].props.node_id) {
1299                 warn_report("CPU-%d and CPU-%d in socket-%" PRId64 "-cluster-%" PRId64
1300                              " have been associated with node-%" PRId64 " and node-%" PRId64
1301                              " respectively. It can cause OSes like Linux to"
1302                              " misbehave", i, j, cpus[i].props.socket_id,
1303                              cpus[i].props.cluster_id, cpus[i].props.node_id,
1304                              cpus[j].props.node_id);
1305             }
1306         }
1307     }
1308 }
1309 
1310 MemoryRegion *machine_consume_memdev(MachineState *machine,
1311                                      HostMemoryBackend *backend)
1312 {
1313     MemoryRegion *ret = host_memory_backend_get_memory(backend);
1314 
1315     if (host_memory_backend_is_mapped(backend)) {
1316         error_report("memory backend %s can't be used multiple times.",
1317                      object_get_canonical_path_component(OBJECT(backend)));
1318         exit(EXIT_FAILURE);
1319     }
1320     host_memory_backend_set_mapped(backend, true);
1321     vmstate_register_ram_global(ret);
1322     return ret;
1323 }
1324 
1325 static bool create_default_memdev(MachineState *ms, const char *path, Error **errp)
1326 {
1327     Object *obj;
1328     MachineClass *mc = MACHINE_GET_CLASS(ms);
1329     bool r = false;
1330 
1331     obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
1332     if (path) {
1333         if (!object_property_set_str(obj, "mem-path", path, errp)) {
1334             goto out;
1335         }
1336     }
1337     if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
1338         goto out;
1339     }
1340     object_property_add_child(object_get_objects_root(), mc->default_ram_id,
1341                               obj);
1342     /* Ensure backend's memory region name is equal to mc->default_ram_id */
1343     if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
1344                              false, errp)) {
1345         goto out;
1346     }
1347     if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
1348         goto out;
1349     }
1350     r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
1351 
1352 out:
1353     object_unref(obj);
1354     return r;
1355 }
1356 
1357 
1358 void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
1359 {
1360     ERRP_GUARD();
1361     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1362     ObjectClass *oc = object_class_by_name(machine->cpu_type);
1363     CPUClass *cc;
1364 
1365     /* This checkpoint is required by replay to separate prior clock
1366        reading from the other reads, because timer polling functions query
1367        clock values from the log. */
1368     replay_checkpoint(CHECKPOINT_INIT);
1369 
1370     if (!xen_enabled()) {
1371         /* On 32-bit hosts, QEMU is limited by virtual address space */
1372         if (machine->ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1373             error_setg(errp, "at most 2047 MB RAM can be simulated");
1374             return;
1375         }
1376     }
1377 
1378     if (machine->memdev) {
1379         ram_addr_t backend_size = object_property_get_uint(OBJECT(machine->memdev),
1380                                                            "size",  &error_abort);
1381         if (backend_size != machine->ram_size) {
1382             error_setg(errp, "Machine memory size does not match the size of the memory backend");
1383             return;
1384         }
1385     } else if (machine_class->default_ram_id && machine->ram_size &&
1386                numa_uses_legacy_mem()) {
1387         if (object_property_find(object_get_objects_root(),
1388                                  machine_class->default_ram_id)) {
1389             error_setg(errp, "object's id '%s' is reserved for the default"
1390                 " RAM backend, it can't be used for any other purposes",
1391                 machine_class->default_ram_id);
1392             error_append_hint(errp,
1393                 "Change the object's 'id' to something else or disable"
1394                 " automatic creation of the default RAM backend by setting"
1395                 " 'memory-backend=%s' with '-machine'.\n",
1396                 machine_class->default_ram_id);
1397             return;
1398         }
1399         if (!create_default_memdev(current_machine, mem_path, errp)) {
1400             return;
1401         }
1402     }
1403 
1404     if (machine->numa_state) {
1405         numa_complete_configuration(machine);
1406         if (machine->numa_state->num_nodes) {
1407             machine_numa_finish_cpu_init(machine);
1408             if (machine_class->cpu_cluster_has_numa_boundary) {
1409                 validate_cpu_cluster_to_numa_boundary(machine);
1410             }
1411         }
1412     }
1413 
1414     if (!machine->ram && machine->memdev) {
1415         machine->ram = machine_consume_memdev(machine, machine->memdev);
1416     }
1417 
1418     /* If the machine supports the valid_cpu_types check and the user
1419      * specified a CPU with -cpu check here that the user CPU is supported.
1420      */
1421     if (machine_class->valid_cpu_types && machine->cpu_type) {
1422         int i;
1423 
1424         for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1425             if (object_class_dynamic_cast(oc,
1426                                           machine_class->valid_cpu_types[i])) {
1427                 /* The user specified CPU is in the valid field, we are
1428                  * good to go.
1429                  */
1430                 break;
1431             }
1432         }
1433 
1434         if (!machine_class->valid_cpu_types[i]) {
1435             /* The user specified CPU is not valid */
1436             error_report("Invalid CPU type: %s", machine->cpu_type);
1437             error_printf("The valid types are: %s",
1438                          machine_class->valid_cpu_types[0]);
1439             for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1440                 error_printf(", %s", machine_class->valid_cpu_types[i]);
1441             }
1442             error_printf("\n");
1443 
1444             exit(1);
1445         }
1446     }
1447 
1448     /* Check if CPU type is deprecated and warn if so */
1449     cc = CPU_CLASS(oc);
1450     if (cc && cc->deprecation_note) {
1451         warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1452                     cc->deprecation_note);
1453     }
1454 
1455     if (machine->cgs) {
1456         /*
1457          * With confidential guests, the host can't see the real
1458          * contents of RAM, so there's no point in it trying to merge
1459          * areas.
1460          */
1461         machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1462 
1463         /*
1464          * Virtio devices can't count on directly accessing guest
1465          * memory, so they need iommu_platform=on to use normal DMA
1466          * mechanisms.  That requires also disabling legacy virtio
1467          * support for those virtio pci devices which allow it.
1468          */
1469         object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1470                                    "on", true);
1471         object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1472                                    "on", false);
1473     }
1474 
1475     accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1476     machine_class->init(machine);
1477     phase_advance(PHASE_MACHINE_INITIALIZED);
1478 }
1479 
1480 static NotifierList machine_init_done_notifiers =
1481     NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1482 
1483 void qemu_add_machine_init_done_notifier(Notifier *notify)
1484 {
1485     notifier_list_add(&machine_init_done_notifiers, notify);
1486     if (phase_check(PHASE_MACHINE_READY)) {
1487         notify->notify(notify, NULL);
1488     }
1489 }
1490 
1491 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1492 {
1493     notifier_remove(notify);
1494 }
1495 
1496 void qdev_machine_creation_done(void)
1497 {
1498     cpu_synchronize_all_post_init();
1499 
1500     if (current_machine->boot_config.once) {
1501         qemu_boot_set(current_machine->boot_config.once, &error_fatal);
1502         qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_config.order));
1503     }
1504 
1505     /*
1506      * ok, initial machine setup is done, starting from now we can
1507      * only create hotpluggable devices
1508      */
1509     phase_advance(PHASE_MACHINE_READY);
1510     qdev_assert_realized_properly();
1511 
1512     /* TODO: once all bus devices are qdevified, this should be done
1513      * when bus is created by qdev.c */
1514     /*
1515      * TODO: If we had a main 'reset container' that the whole system
1516      * lived in, we could reset that using the multi-phase reset
1517      * APIs. For the moment, we just reset the sysbus, which will cause
1518      * all devices hanging off it (and all their child buses, recursively)
1519      * to be reset. Note that this will *not* reset any Device objects
1520      * which are not attached to some part of the qbus tree!
1521      */
1522     qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1523 
1524     notifier_list_notify(&machine_init_done_notifiers, NULL);
1525 
1526     if (rom_check_and_register_reset() != 0) {
1527         exit(1);
1528     }
1529 
1530     replay_start();
1531 
1532     /* This checkpoint is required by replay to separate prior clock
1533        reading from the other reads, because timer polling functions query
1534        clock values from the log. */
1535     replay_checkpoint(CHECKPOINT_RESET);
1536     qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1537     register_global_state();
1538 }
1539 
1540 static const TypeInfo machine_info = {
1541     .name = TYPE_MACHINE,
1542     .parent = TYPE_OBJECT,
1543     .abstract = true,
1544     .class_size = sizeof(MachineClass),
1545     .class_init    = machine_class_init,
1546     .class_base_init = machine_class_base_init,
1547     .instance_size = sizeof(MachineState),
1548     .instance_init = machine_initfn,
1549     .instance_finalize = machine_finalize,
1550 };
1551 
1552 static void machine_register_types(void)
1553 {
1554     type_register_static(&machine_info);
1555 }
1556 
1557 type_init(machine_register_types)
1558