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