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