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