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