xref: /openbmc/qemu/hw/core/machine.c (revision 712f7150a8fba24c4afdc6bafa035cd702841f4d)
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/units.h"
15  #include "qemu/accel.h"
16  #include "sysemu/replay.h"
17  #include "hw/boards.h"
18  #include "hw/loader.h"
19  #include "qemu/error-report.h"
20  #include "qapi/error.h"
21  #include "qapi/qapi-visit-machine.h"
22  #include "qemu/madvise.h"
23  #include "qom/object_interfaces.h"
24  #include "sysemu/cpus.h"
25  #include "sysemu/sysemu.h"
26  #include "sysemu/reset.h"
27  #include "sysemu/runstate.h"
28  #include "sysemu/xen.h"
29  #include "sysemu/qtest.h"
30  #include "hw/pci/pci_bridge.h"
31  #include "hw/mem/nvdimm.h"
32  #include "migration/global_state.h"
33  #include "exec/confidential-guest-support.h"
34  #include "hw/virtio/virtio-pci.h"
35  #include "hw/virtio/virtio-net.h"
36  #include "hw/virtio/virtio-iommu.h"
37  #include "audio/audio.h"
38  
39  GlobalProperty hw_compat_9_1[] = {
40      { TYPE_PCI_DEVICE, "x-pcie-ext-tag", "false" },
41  };
42  const size_t hw_compat_9_1_len = G_N_ELEMENTS(hw_compat_9_1);
43  
44  GlobalProperty hw_compat_9_0[] = {
45      {"arm-cpu", "backcompat-cntfrq", "true" },
46      { "scsi-hd", "migrate-emulated-scsi-request", "false" },
47      { "scsi-cd", "migrate-emulated-scsi-request", "false" },
48      {"vfio-pci", "skip-vsc-check", "false" },
49      { "virtio-pci", "x-pcie-pm-no-soft-reset", "off" },
50      {"sd-card", "spec_version", "2" },
51  };
52  const size_t hw_compat_9_0_len = G_N_ELEMENTS(hw_compat_9_0);
53  
54  GlobalProperty hw_compat_8_2[] = {
55      { "migration", "zero-page-detection", "legacy"},
56      { TYPE_VIRTIO_IOMMU_PCI, "granule", "4k" },
57      { TYPE_VIRTIO_IOMMU_PCI, "aw-bits", "64" },
58      { "virtio-gpu-device", "x-scanout-vmstate-version", "1" },
59  };
60  const size_t hw_compat_8_2_len = G_N_ELEMENTS(hw_compat_8_2);
61  
62  GlobalProperty hw_compat_8_1[] = {
63      { TYPE_PCI_BRIDGE, "x-pci-express-writeable-slt-bug", "true" },
64      { "ramfb", "x-migrate", "off" },
65      { "vfio-pci-nohotplug", "x-ramfb-migrate", "off" },
66      { "igb", "x-pcie-flr-init", "off" },
67      { TYPE_VIRTIO_NET, "host_uso", "off"},
68      { TYPE_VIRTIO_NET, "guest_uso4", "off"},
69      { TYPE_VIRTIO_NET, "guest_uso6", "off"},
70  };
71  const size_t hw_compat_8_1_len = G_N_ELEMENTS(hw_compat_8_1);
72  
73  GlobalProperty hw_compat_8_0[] = {
74      { "migration", "multifd-flush-after-each-section", "on"},
75      { TYPE_PCI_DEVICE, "x-pcie-ari-nextfn-1", "on" },
76  };
77  const size_t hw_compat_8_0_len = G_N_ELEMENTS(hw_compat_8_0);
78  
79  GlobalProperty hw_compat_7_2[] = {
80      { "e1000e", "migrate-timadj", "off" },
81      { "virtio-mem", "x-early-migration", "false" },
82      { "migration", "x-preempt-pre-7-2", "true" },
83      { TYPE_PCI_DEVICE, "x-pcie-err-unc-mask", "off" },
84  };
85  const size_t hw_compat_7_2_len = G_N_ELEMENTS(hw_compat_7_2);
86  
87  GlobalProperty hw_compat_7_1[] = {
88      { "virtio-device", "queue_reset", "false" },
89      { "virtio-rng-pci", "vectors", "0" },
90      { "virtio-rng-pci-transitional", "vectors", "0" },
91      { "virtio-rng-pci-non-transitional", "vectors", "0" },
92  };
93  const size_t hw_compat_7_1_len = G_N_ELEMENTS(hw_compat_7_1);
94  
95  GlobalProperty hw_compat_7_0[] = {
96      { "arm-gicv3-common", "force-8-bit-prio", "on" },
97      { "nvme-ns", "eui64-default", "on"},
98  };
99  const size_t hw_compat_7_0_len = G_N_ELEMENTS(hw_compat_7_0);
100  
101  GlobalProperty hw_compat_6_2[] = {
102      { "PIIX4_PM", "x-not-migrate-acpi-index", "on"},
103  };
104  const size_t hw_compat_6_2_len = G_N_ELEMENTS(hw_compat_6_2);
105  
106  GlobalProperty hw_compat_6_1[] = {
107      { "vhost-user-vsock-device", "seqpacket", "off" },
108      { "nvme-ns", "shared", "off" },
109  };
110  const size_t hw_compat_6_1_len = G_N_ELEMENTS(hw_compat_6_1);
111  
112  GlobalProperty hw_compat_6_0[] = {
113      { "gpex-pcihost", "allow-unmapped-accesses", "false" },
114      { "i8042", "extended-state", "false"},
115      { "nvme-ns", "eui64-default", "off"},
116      { "e1000", "init-vet", "off" },
117      { "e1000e", "init-vet", "off" },
118      { "vhost-vsock-device", "seqpacket", "off" },
119  };
120  const size_t hw_compat_6_0_len = G_N_ELEMENTS(hw_compat_6_0);
121  
122  GlobalProperty hw_compat_5_2[] = {
123      { "ICH9-LPC", "smm-compat", "on"},
124      { "PIIX4_PM", "smm-compat", "on"},
125      { "virtio-blk-device", "report-discard-granularity", "off" },
126      { "virtio-net-pci-base", "vectors", "3"},
127      { "nvme", "msix-exclusive-bar", "on"},
128  };
129  const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
130  
131  GlobalProperty hw_compat_5_1[] = {
132      { "vhost-scsi", "num_queues", "1"},
133      { "vhost-user-blk", "num-queues", "1"},
134      { "vhost-user-scsi", "num_queues", "1"},
135      { "virtio-blk-device", "num-queues", "1"},
136      { "virtio-scsi-device", "num_queues", "1"},
137      { "nvme", "use-intel-id", "on"},
138      { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
139      { "pl011", "migrate-clk", "off" },
140      { "virtio-pci", "x-ats-page-aligned", "off"},
141  };
142  const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
143  
144  GlobalProperty hw_compat_5_0[] = {
145      { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
146      { "virtio-balloon-device", "page-poison", "false" },
147      { "vmport", "x-read-set-eax", "off" },
148      { "vmport", "x-signal-unsupported-cmd", "off" },
149      { "vmport", "x-report-vmx-type", "off" },
150      { "vmport", "x-cmds-v2", "off" },
151      { "virtio-device", "x-disable-legacy-check", "true" },
152  };
153  const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
154  
155  GlobalProperty hw_compat_4_2[] = {
156      { "virtio-blk-device", "queue-size", "128"},
157      { "virtio-scsi-device", "virtqueue_size", "128"},
158      { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
159      { "virtio-blk-device", "seg-max-adjust", "off"},
160      { "virtio-scsi-device", "seg_max_adjust", "off"},
161      { "vhost-blk-device", "seg_max_adjust", "off"},
162      { "usb-host", "suppress-remote-wake", "off" },
163      { "usb-redir", "suppress-remote-wake", "off" },
164      { "qxl", "revision", "4" },
165      { "qxl-vga", "revision", "4" },
166      { "fw_cfg", "acpi-mr-restore", "false" },
167      { "virtio-device", "use-disabled-flag", "false" },
168  };
169  const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
170  
171  GlobalProperty hw_compat_4_1[] = {
172      { "virtio-pci", "x-pcie-flr-init", "off" },
173  };
174  const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
175  
176  GlobalProperty hw_compat_4_0[] = {
177      { "VGA",            "edid", "false" },
178      { "secondary-vga",  "edid", "false" },
179      { "bochs-display",  "edid", "false" },
180      { "virtio-vga",     "edid", "false" },
181      { "virtio-gpu-device", "edid", "false" },
182      { "virtio-device", "use-started", "false" },
183      { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
184      { "pl031", "migrate-tick-offset", "false" },
185  };
186  const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
187  
188  GlobalProperty hw_compat_3_1[] = {
189      { "pcie-root-port", "x-speed", "2_5" },
190      { "pcie-root-port", "x-width", "1" },
191      { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
192      { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
193      { "tpm-crb", "ppi", "false" },
194      { "tpm-tis", "ppi", "false" },
195      { "usb-kbd", "serial", "42" },
196      { "usb-mouse", "serial", "42" },
197      { "usb-tablet", "serial", "42" },
198      { "virtio-blk-device", "discard", "false" },
199      { "virtio-blk-device", "write-zeroes", "false" },
200      { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
201      { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
202  };
203  const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
204  
205  GlobalProperty hw_compat_3_0[] = {};
206  const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
207  
208  GlobalProperty hw_compat_2_12[] = {
209      { "hda-audio", "use-timer", "false" },
210      { "cirrus-vga", "global-vmstate", "true" },
211      { "VGA", "global-vmstate", "true" },
212      { "vmware-svga", "global-vmstate", "true" },
213      { "qxl-vga", "global-vmstate", "true" },
214  };
215  const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
216  
217  GlobalProperty hw_compat_2_11[] = {
218      { "hpet", "hpet-offset-saved", "false" },
219      { "virtio-blk-pci", "vectors", "2" },
220      { "vhost-user-blk-pci", "vectors", "2" },
221      { "e1000", "migrate_tso_props", "off" },
222  };
223  const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
224  
225  GlobalProperty hw_compat_2_10[] = {
226      { "virtio-mouse-device", "wheel-axis", "false" },
227      { "virtio-tablet-device", "wheel-axis", "false" },
228  };
229  const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
230  
231  GlobalProperty hw_compat_2_9[] = {
232      { "pci-bridge", "shpc", "off" },
233      { "intel-iommu", "pt", "off" },
234      { "virtio-net-device", "x-mtu-bypass-backend", "off" },
235      { "pcie-root-port", "x-migrate-msix", "false" },
236  };
237  const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
238  
239  GlobalProperty hw_compat_2_8[] = {
240      { "fw_cfg_mem", "x-file-slots", "0x10" },
241      { "fw_cfg_io", "x-file-slots", "0x10" },
242      { "pflash_cfi01", "old-multiple-chip-handling", "on" },
243      { "pci-bridge", "shpc", "on" },
244      { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
245      { "virtio-pci", "x-pcie-deverr-init", "off" },
246      { "virtio-pci", "x-pcie-lnkctl-init", "off" },
247      { "virtio-pci", "x-pcie-pm-init", "off" },
248      { "cirrus-vga", "vgamem_mb", "8" },
249      { "isa-cirrus-vga", "vgamem_mb", "8" },
250  };
251  const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
252  
253  GlobalProperty hw_compat_2_7[] = {
254      { "virtio-pci", "page-per-vq", "on" },
255      { "virtio-serial-device", "emergency-write", "off" },
256      { "ioapic", "version", "0x11" },
257      { "intel-iommu", "x-buggy-eim", "true" },
258      { "virtio-pci", "x-ignore-backend-features", "on" },
259  };
260  const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
261  
262  GlobalProperty hw_compat_2_6[] = {
263      { "virtio-mmio", "format_transport_address", "off" },
264      /* Optional because not all virtio-pci devices support legacy mode */
265      { "virtio-pci", "disable-modern", "on",  .optional = true },
266      { "virtio-pci", "disable-legacy", "off", .optional = true },
267  };
268  const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
269  
270  GlobalProperty hw_compat_2_5[] = {
271      { "isa-fdc", "fallback", "144" },
272      { "pvscsi", "x-old-pci-configuration", "on" },
273      { "pvscsi", "x-disable-pcie", "on" },
274      { "vmxnet3", "x-old-msi-offsets", "on" },
275      { "vmxnet3", "x-disable-pcie", "on" },
276  };
277  const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
278  
279  GlobalProperty hw_compat_2_4[] = {
280      { "e1000", "extra_mac_registers", "off" },
281      { "virtio-pci", "x-disable-pcie", "on" },
282      { "virtio-pci", "migrate-extra", "off" },
283      { "fw_cfg_mem", "dma_enabled", "off" },
284      { "fw_cfg_io", "dma_enabled", "off" }
285  };
286  const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
287  
288  MachineState *current_machine;
289  
machine_get_kernel(Object * obj,Error ** errp)290  static char *machine_get_kernel(Object *obj, Error **errp)
291  {
292      MachineState *ms = MACHINE(obj);
293  
294      return g_strdup(ms->kernel_filename);
295  }
296  
machine_set_kernel(Object * obj,const char * value,Error ** errp)297  static void machine_set_kernel(Object *obj, const char *value, Error **errp)
298  {
299      MachineState *ms = MACHINE(obj);
300  
301      g_free(ms->kernel_filename);
302      ms->kernel_filename = g_strdup(value);
303  }
304  
machine_get_initrd(Object * obj,Error ** errp)305  static char *machine_get_initrd(Object *obj, Error **errp)
306  {
307      MachineState *ms = MACHINE(obj);
308  
309      return g_strdup(ms->initrd_filename);
310  }
311  
machine_set_initrd(Object * obj,const char * value,Error ** errp)312  static void machine_set_initrd(Object *obj, const char *value, Error **errp)
313  {
314      MachineState *ms = MACHINE(obj);
315  
316      g_free(ms->initrd_filename);
317      ms->initrd_filename = g_strdup(value);
318  }
319  
machine_get_append(Object * obj,Error ** errp)320  static char *machine_get_append(Object *obj, Error **errp)
321  {
322      MachineState *ms = MACHINE(obj);
323  
324      return g_strdup(ms->kernel_cmdline);
325  }
326  
machine_set_append(Object * obj,const char * value,Error ** errp)327  static void machine_set_append(Object *obj, const char *value, Error **errp)
328  {
329      MachineState *ms = MACHINE(obj);
330  
331      g_free(ms->kernel_cmdline);
332      ms->kernel_cmdline = g_strdup(value);
333  }
334  
machine_get_dtb(Object * obj,Error ** errp)335  static char *machine_get_dtb(Object *obj, Error **errp)
336  {
337      MachineState *ms = MACHINE(obj);
338  
339      return g_strdup(ms->dtb);
340  }
341  
machine_set_dtb(Object * obj,const char * value,Error ** errp)342  static void machine_set_dtb(Object *obj, const char *value, Error **errp)
343  {
344      MachineState *ms = MACHINE(obj);
345  
346      g_free(ms->dtb);
347      ms->dtb = g_strdup(value);
348  }
349  
machine_get_dumpdtb(Object * obj,Error ** errp)350  static char *machine_get_dumpdtb(Object *obj, Error **errp)
351  {
352      MachineState *ms = MACHINE(obj);
353  
354      return g_strdup(ms->dumpdtb);
355  }
356  
machine_set_dumpdtb(Object * obj,const char * value,Error ** errp)357  static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
358  {
359      MachineState *ms = MACHINE(obj);
360  
361      g_free(ms->dumpdtb);
362      ms->dumpdtb = g_strdup(value);
363  }
364  
machine_get_phandle_start(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)365  static void machine_get_phandle_start(Object *obj, Visitor *v,
366                                        const char *name, void *opaque,
367                                        Error **errp)
368  {
369      MachineState *ms = MACHINE(obj);
370      int64_t value = ms->phandle_start;
371  
372      visit_type_int(v, name, &value, errp);
373  }
374  
machine_set_phandle_start(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)375  static void machine_set_phandle_start(Object *obj, Visitor *v,
376                                        const char *name, void *opaque,
377                                        Error **errp)
378  {
379      MachineState *ms = MACHINE(obj);
380      int64_t value;
381  
382      if (!visit_type_int(v, name, &value, errp)) {
383          return;
384      }
385  
386      ms->phandle_start = value;
387  }
388  
machine_get_dt_compatible(Object * obj,Error ** errp)389  static char *machine_get_dt_compatible(Object *obj, Error **errp)
390  {
391      MachineState *ms = MACHINE(obj);
392  
393      return g_strdup(ms->dt_compatible);
394  }
395  
machine_set_dt_compatible(Object * obj,const char * value,Error ** errp)396  static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
397  {
398      MachineState *ms = MACHINE(obj);
399  
400      g_free(ms->dt_compatible);
401      ms->dt_compatible = g_strdup(value);
402  }
403  
machine_get_dump_guest_core(Object * obj,Error ** errp)404  static bool machine_get_dump_guest_core(Object *obj, Error **errp)
405  {
406      MachineState *ms = MACHINE(obj);
407  
408      return ms->dump_guest_core;
409  }
410  
machine_set_dump_guest_core(Object * obj,bool value,Error ** errp)411  static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
412  {
413      MachineState *ms = MACHINE(obj);
414  
415      if (!value && QEMU_MADV_DONTDUMP == QEMU_MADV_INVALID) {
416          error_setg(errp, "Dumping guest memory cannot be disabled on this host");
417          return;
418      }
419      ms->dump_guest_core = value;
420  }
421  
machine_get_mem_merge(Object * obj,Error ** errp)422  static bool machine_get_mem_merge(Object *obj, Error **errp)
423  {
424      MachineState *ms = MACHINE(obj);
425  
426      return ms->mem_merge;
427  }
428  
machine_set_mem_merge(Object * obj,bool value,Error ** errp)429  static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
430  {
431      MachineState *ms = MACHINE(obj);
432  
433      if (value && QEMU_MADV_MERGEABLE == QEMU_MADV_INVALID) {
434          error_setg(errp, "Memory merging is not supported on this host");
435          return;
436      }
437      ms->mem_merge = value;
438  }
439  
machine_get_usb(Object * obj,Error ** errp)440  static bool machine_get_usb(Object *obj, Error **errp)
441  {
442      MachineState *ms = MACHINE(obj);
443  
444      return ms->usb;
445  }
446  
machine_set_usb(Object * obj,bool value,Error ** errp)447  static void machine_set_usb(Object *obj, bool value, Error **errp)
448  {
449      MachineState *ms = MACHINE(obj);
450  
451      ms->usb = value;
452      ms->usb_disabled = !value;
453  }
454  
machine_get_graphics(Object * obj,Error ** errp)455  static bool machine_get_graphics(Object *obj, Error **errp)
456  {
457      MachineState *ms = MACHINE(obj);
458  
459      return ms->enable_graphics;
460  }
461  
machine_set_graphics(Object * obj,bool value,Error ** errp)462  static void machine_set_graphics(Object *obj, bool value, Error **errp)
463  {
464      MachineState *ms = MACHINE(obj);
465  
466      ms->enable_graphics = value;
467  }
468  
machine_get_firmware(Object * obj,Error ** errp)469  static char *machine_get_firmware(Object *obj, Error **errp)
470  {
471      MachineState *ms = MACHINE(obj);
472  
473      return g_strdup(ms->firmware);
474  }
475  
machine_set_firmware(Object * obj,const char * value,Error ** errp)476  static void machine_set_firmware(Object *obj, const char *value, Error **errp)
477  {
478      MachineState *ms = MACHINE(obj);
479  
480      g_free(ms->firmware);
481      ms->firmware = g_strdup(value);
482  }
483  
machine_set_suppress_vmdesc(Object * obj,bool value,Error ** errp)484  static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
485  {
486      MachineState *ms = MACHINE(obj);
487  
488      ms->suppress_vmdesc = value;
489  }
490  
machine_get_suppress_vmdesc(Object * obj,Error ** errp)491  static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
492  {
493      MachineState *ms = MACHINE(obj);
494  
495      return ms->suppress_vmdesc;
496  }
497  
machine_get_memory_encryption(Object * obj,Error ** errp)498  static char *machine_get_memory_encryption(Object *obj, Error **errp)
499  {
500      MachineState *ms = MACHINE(obj);
501  
502      if (ms->cgs) {
503          return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
504      }
505  
506      return NULL;
507  }
508  
machine_set_memory_encryption(Object * obj,const char * value,Error ** errp)509  static void machine_set_memory_encryption(Object *obj, const char *value,
510                                          Error **errp)
511  {
512      Object *cgs =
513          object_resolve_path_component(object_get_objects_root(), value);
514  
515      if (!cgs) {
516          error_setg(errp, "No such memory encryption object '%s'", value);
517          return;
518      }
519  
520      object_property_set_link(obj, "confidential-guest-support", cgs, errp);
521  }
522  
machine_check_confidential_guest_support(const Object * obj,const char * name,Object * new_target,Error ** errp)523  static void machine_check_confidential_guest_support(const Object *obj,
524                                                       const char *name,
525                                                       Object *new_target,
526                                                       Error **errp)
527  {
528      /*
529       * So far the only constraint is that the target has the
530       * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
531       * by the QOM core
532       */
533  }
534  
machine_get_nvdimm(Object * obj,Error ** errp)535  static bool machine_get_nvdimm(Object *obj, Error **errp)
536  {
537      MachineState *ms = MACHINE(obj);
538  
539      return ms->nvdimms_state->is_enabled;
540  }
541  
machine_set_nvdimm(Object * obj,bool value,Error ** errp)542  static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
543  {
544      MachineState *ms = MACHINE(obj);
545  
546      ms->nvdimms_state->is_enabled = value;
547  }
548  
machine_get_hmat(Object * obj,Error ** errp)549  static bool machine_get_hmat(Object *obj, Error **errp)
550  {
551      MachineState *ms = MACHINE(obj);
552  
553      return ms->numa_state->hmat_enabled;
554  }
555  
machine_set_hmat(Object * obj,bool value,Error ** errp)556  static void machine_set_hmat(Object *obj, bool value, Error **errp)
557  {
558      MachineState *ms = MACHINE(obj);
559  
560      ms->numa_state->hmat_enabled = value;
561  }
562  
machine_get_mem(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)563  static void machine_get_mem(Object *obj, Visitor *v, const char *name,
564                              void *opaque, Error **errp)
565  {
566      MachineState *ms = MACHINE(obj);
567      MemorySizeConfiguration mem = {
568          .has_size = true,
569          .size = ms->ram_size,
570          .has_max_size = !!ms->ram_slots,
571          .max_size = ms->maxram_size,
572          .has_slots = !!ms->ram_slots,
573          .slots = ms->ram_slots,
574      };
575      MemorySizeConfiguration *p_mem = &mem;
576  
577      visit_type_MemorySizeConfiguration(v, name, &p_mem, &error_abort);
578  }
579  
machine_set_mem(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)580  static void machine_set_mem(Object *obj, Visitor *v, const char *name,
581                              void *opaque, Error **errp)
582  {
583      ERRP_GUARD();
584      MachineState *ms = MACHINE(obj);
585      MachineClass *mc = MACHINE_GET_CLASS(obj);
586      MemorySizeConfiguration *mem;
587  
588      if (!visit_type_MemorySizeConfiguration(v, name, &mem, errp)) {
589          return;
590      }
591  
592      if (!mem->has_size) {
593          mem->has_size = true;
594          mem->size = mc->default_ram_size;
595      }
596      mem->size = QEMU_ALIGN_UP(mem->size, 8192);
597      if (mc->fixup_ram_size) {
598          mem->size = mc->fixup_ram_size(mem->size);
599      }
600      if ((ram_addr_t)mem->size != mem->size) {
601          error_setg(errp, "ram size %llu exceeds permitted maximum %llu",
602                     (unsigned long long)mem->size,
603                     (unsigned long long)RAM_ADDR_MAX);
604          goto out_free;
605      }
606  
607      if (mem->has_max_size) {
608          if ((ram_addr_t)mem->max_size != mem->max_size) {
609              error_setg(errp, "ram size %llu exceeds permitted maximum %llu",
610                         (unsigned long long)mem->max_size,
611                         (unsigned long long)RAM_ADDR_MAX);
612              goto out_free;
613          }
614          if (mem->max_size < mem->size) {
615              error_setg(errp, "invalid value of maxmem: "
616                         "maximum memory size (0x%" PRIx64 ") must be at least "
617                         "the initial memory size (0x%" PRIx64 ")",
618                         mem->max_size, mem->size);
619              goto out_free;
620          }
621          if (mem->has_slots && mem->slots && mem->max_size == mem->size) {
622              error_setg(errp, "invalid value of maxmem: "
623                         "memory slots were specified but maximum memory size "
624                         "(0x%" PRIx64 ") is equal to the initial memory size "
625                         "(0x%" PRIx64 ")", mem->max_size, mem->size);
626              goto out_free;
627          }
628          ms->maxram_size = mem->max_size;
629      } else {
630          if (mem->has_slots) {
631              error_setg(errp, "slots specified but no max-size");
632              goto out_free;
633          }
634          ms->maxram_size = mem->size;
635      }
636      ms->ram_size = mem->size;
637      ms->ram_slots = mem->has_slots ? mem->slots : 0;
638  out_free:
639      qapi_free_MemorySizeConfiguration(mem);
640  }
641  
machine_get_nvdimm_persistence(Object * obj,Error ** errp)642  static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
643  {
644      MachineState *ms = MACHINE(obj);
645  
646      return g_strdup(ms->nvdimms_state->persistence_string);
647  }
648  
machine_set_nvdimm_persistence(Object * obj,const char * value,Error ** errp)649  static void machine_set_nvdimm_persistence(Object *obj, const char *value,
650                                             Error **errp)
651  {
652      MachineState *ms = MACHINE(obj);
653      NVDIMMState *nvdimms_state = ms->nvdimms_state;
654  
655      if (strcmp(value, "cpu") == 0) {
656          nvdimms_state->persistence = 3;
657      } else if (strcmp(value, "mem-ctrl") == 0) {
658          nvdimms_state->persistence = 2;
659      } else {
660          error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
661                     value);
662          return;
663      }
664  
665      g_free(nvdimms_state->persistence_string);
666      nvdimms_state->persistence_string = g_strdup(value);
667  }
668  
machine_class_allow_dynamic_sysbus_dev(MachineClass * mc,const char * type)669  void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
670  {
671      QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
672  }
673  
device_is_dynamic_sysbus(MachineClass * mc,DeviceState * dev)674  bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
675  {
676      Object *obj = OBJECT(dev);
677  
678      if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
679          return false;
680      }
681  
682      return device_type_is_dynamic_sysbus(mc, object_get_typename(obj));
683  }
684  
device_type_is_dynamic_sysbus(MachineClass * mc,const char * type)685  bool device_type_is_dynamic_sysbus(MachineClass *mc, const char *type)
686  {
687      bool allowed = false;
688      strList *wl;
689      ObjectClass *klass = object_class_by_name(type);
690  
691      for (wl = mc->allowed_dynamic_sysbus_devices;
692           !allowed && wl;
693           wl = wl->next) {
694          allowed |= !!object_class_dynamic_cast(klass, wl->value);
695      }
696  
697      return allowed;
698  }
699  
machine_get_audiodev(Object * obj,Error ** errp)700  static char *machine_get_audiodev(Object *obj, Error **errp)
701  {
702      MachineState *ms = MACHINE(obj);
703  
704      return g_strdup(ms->audiodev);
705  }
706  
machine_set_audiodev(Object * obj,const char * value,Error ** errp)707  static void machine_set_audiodev(Object *obj, const char *value,
708                                   Error **errp)
709  {
710      MachineState *ms = MACHINE(obj);
711  
712      if (!audio_state_by_name(value, errp)) {
713          return;
714      }
715  
716      g_free(ms->audiodev);
717      ms->audiodev = g_strdup(value);
718  }
719  
machine_query_hotpluggable_cpus(MachineState * machine)720  HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
721  {
722      int i;
723      HotpluggableCPUList *head = NULL;
724      MachineClass *mc = MACHINE_GET_CLASS(machine);
725  
726      /* force board to initialize possible_cpus if it hasn't been done yet */
727      mc->possible_cpu_arch_ids(machine);
728  
729      for (i = 0; i < machine->possible_cpus->len; i++) {
730          CPUState *cpu;
731          HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
732  
733          cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
734          cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
735          cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
736                                     sizeof(*cpu_item->props));
737  
738          cpu = machine->possible_cpus->cpus[i].cpu;
739          if (cpu) {
740              cpu_item->qom_path = object_get_canonical_path(OBJECT(cpu));
741          }
742          QAPI_LIST_PREPEND(head, cpu_item);
743      }
744      return head;
745  }
746  
747  /**
748   * machine_set_cpu_numa_node:
749   * @machine: machine object to modify
750   * @props: specifies which cpu objects to assign to
751   *         numa node specified by @props.node_id
752   * @errp: if an error occurs, a pointer to an area to store the error
753   *
754   * Associate NUMA node specified by @props.node_id with cpu slots that
755   * match socket/core/thread-ids specified by @props. It's recommended to use
756   * query-hotpluggable-cpus.props values to specify affected cpu slots,
757   * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
758   *
759   * However for CLI convenience it's possible to pass in subset of properties,
760   * which would affect all cpu slots that match it.
761   * Ex for pc machine:
762   *    -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
763   *    -numa cpu,node-id=0,socket_id=0 \
764   *    -numa cpu,node-id=1,socket_id=1
765   * will assign all child cores of socket 0 to node 0 and
766   * of socket 1 to node 1.
767   *
768   * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
769   * return error.
770   * Empty subset is disallowed and function will return with error in this case.
771   */
machine_set_cpu_numa_node(MachineState * machine,const CpuInstanceProperties * props,Error ** errp)772  void machine_set_cpu_numa_node(MachineState *machine,
773                                 const CpuInstanceProperties *props, Error **errp)
774  {
775      MachineClass *mc = MACHINE_GET_CLASS(machine);
776      NodeInfo *numa_info = machine->numa_state->nodes;
777      bool match = false;
778      int i;
779  
780      if (!mc->possible_cpu_arch_ids) {
781          error_setg(errp, "mapping of CPUs to NUMA node is not supported");
782          return;
783      }
784  
785      /* disabling node mapping is not supported, forbid it */
786      assert(props->has_node_id);
787  
788      /* force board to initialize possible_cpus if it hasn't been done yet */
789      mc->possible_cpu_arch_ids(machine);
790  
791      for (i = 0; i < machine->possible_cpus->len; i++) {
792          CPUArchId *slot = &machine->possible_cpus->cpus[i];
793  
794          /* reject unsupported by board properties */
795          if (props->has_thread_id && !slot->props.has_thread_id) {
796              error_setg(errp, "thread-id is not supported");
797              return;
798          }
799  
800          if (props->has_core_id && !slot->props.has_core_id) {
801              error_setg(errp, "core-id is not supported");
802              return;
803          }
804  
805          if (props->has_module_id && !slot->props.has_module_id) {
806              error_setg(errp, "module-id is not supported");
807              return;
808          }
809  
810          if (props->has_cluster_id && !slot->props.has_cluster_id) {
811              error_setg(errp, "cluster-id is not supported");
812              return;
813          }
814  
815          if (props->has_socket_id && !slot->props.has_socket_id) {
816              error_setg(errp, "socket-id is not supported");
817              return;
818          }
819  
820          if (props->has_die_id && !slot->props.has_die_id) {
821              error_setg(errp, "die-id is not supported");
822              return;
823          }
824  
825          /* skip slots with explicit mismatch */
826          if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
827                  continue;
828          }
829  
830          if (props->has_core_id && props->core_id != slot->props.core_id) {
831                  continue;
832          }
833  
834          if (props->has_module_id &&
835              props->module_id != slot->props.module_id) {
836                  continue;
837          }
838  
839          if (props->has_cluster_id &&
840              props->cluster_id != slot->props.cluster_id) {
841                  continue;
842          }
843  
844          if (props->has_die_id && props->die_id != slot->props.die_id) {
845                  continue;
846          }
847  
848          if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
849                  continue;
850          }
851  
852          /* reject assignment if slot is already assigned, for compatibility
853           * of legacy cpu_index mapping with SPAPR core based mapping do not
854           * error out if cpu thread and matched core have the same node-id */
855          if (slot->props.has_node_id &&
856              slot->props.node_id != props->node_id) {
857              error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
858                         slot->props.node_id);
859              return;
860          }
861  
862          /* assign slot to node as it's matched '-numa cpu' key */
863          match = true;
864          slot->props.node_id = props->node_id;
865          slot->props.has_node_id = props->has_node_id;
866  
867          if (machine->numa_state->hmat_enabled) {
868              if ((numa_info[props->node_id].initiator < MAX_NODES) &&
869                  (props->node_id != numa_info[props->node_id].initiator)) {
870                  error_setg(errp, "The initiator of CPU NUMA node %" PRId64
871                             " should be itself (got %" PRIu16 ")",
872                             props->node_id, numa_info[props->node_id].initiator);
873                  return;
874              }
875              numa_info[props->node_id].has_cpu = true;
876              numa_info[props->node_id].initiator = props->node_id;
877          }
878      }
879  
880      if (!match) {
881          error_setg(errp, "no match found");
882      }
883  }
884  
machine_get_smp(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)885  static void machine_get_smp(Object *obj, Visitor *v, const char *name,
886                              void *opaque, Error **errp)
887  {
888      MachineState *ms = MACHINE(obj);
889      SMPConfiguration *config = &(SMPConfiguration){
890          .has_cpus = true, .cpus = ms->smp.cpus,
891          .has_drawers = true, .drawers = ms->smp.drawers,
892          .has_books = true, .books = ms->smp.books,
893          .has_sockets = true, .sockets = ms->smp.sockets,
894          .has_dies = true, .dies = ms->smp.dies,
895          .has_clusters = true, .clusters = ms->smp.clusters,
896          .has_modules = true, .modules = ms->smp.modules,
897          .has_cores = true, .cores = ms->smp.cores,
898          .has_threads = true, .threads = ms->smp.threads,
899          .has_maxcpus = true, .maxcpus = ms->smp.max_cpus,
900      };
901  
902      if (!visit_type_SMPConfiguration(v, name, &config, &error_abort)) {
903          return;
904      }
905  }
906  
machine_set_smp(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)907  static void machine_set_smp(Object *obj, Visitor *v, const char *name,
908                              void *opaque, Error **errp)
909  {
910      MachineState *ms = MACHINE(obj);
911      g_autoptr(SMPConfiguration) config = NULL;
912  
913      if (!visit_type_SMPConfiguration(v, name, &config, errp)) {
914          return;
915      }
916  
917      machine_parse_smp_config(ms, config, errp);
918  }
919  
machine_get_smp_cache(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)920  static void machine_get_smp_cache(Object *obj, Visitor *v, const char *name,
921                                    void *opaque, Error **errp)
922  {
923      MachineState *ms = MACHINE(obj);
924      SmpCache *cache = &ms->smp_cache;
925      SmpCachePropertiesList *head = NULL;
926      SmpCachePropertiesList **tail = &head;
927  
928      for (int i = 0; i < CACHE_LEVEL_AND_TYPE__MAX; i++) {
929          SmpCacheProperties *node = g_new(SmpCacheProperties, 1);
930  
931          node->cache = cache->props[i].cache;
932          node->topology = cache->props[i].topology;
933          QAPI_LIST_APPEND(tail, node);
934      }
935  
936      visit_type_SmpCachePropertiesList(v, name, &head, errp);
937      qapi_free_SmpCachePropertiesList(head);
938  }
939  
machine_set_smp_cache(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)940  static void machine_set_smp_cache(Object *obj, Visitor *v, const char *name,
941                                    void *opaque, Error **errp)
942  {
943      MachineState *ms = MACHINE(obj);
944      SmpCachePropertiesList *caches;
945  
946      if (!visit_type_SmpCachePropertiesList(v, name, &caches, errp)) {
947          return;
948      }
949  
950      machine_parse_smp_cache(ms, caches, errp);
951      qapi_free_SmpCachePropertiesList(caches);
952  }
953  
machine_get_boot(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)954  static void machine_get_boot(Object *obj, Visitor *v, const char *name,
955                              void *opaque, Error **errp)
956  {
957      MachineState *ms = MACHINE(obj);
958      BootConfiguration *config = &ms->boot_config;
959      visit_type_BootConfiguration(v, name, &config, &error_abort);
960  }
961  
machine_free_boot_config(MachineState * ms)962  static void machine_free_boot_config(MachineState *ms)
963  {
964      g_free(ms->boot_config.order);
965      g_free(ms->boot_config.once);
966      g_free(ms->boot_config.splash);
967  }
968  
machine_copy_boot_config(MachineState * ms,BootConfiguration * config)969  static void machine_copy_boot_config(MachineState *ms, BootConfiguration *config)
970  {
971      MachineClass *machine_class = MACHINE_GET_CLASS(ms);
972  
973      machine_free_boot_config(ms);
974      ms->boot_config = *config;
975      if (!config->order) {
976          ms->boot_config.order = g_strdup(machine_class->default_boot_order);
977      }
978  }
979  
machine_set_boot(Object * obj,Visitor * v,const char * name,void * opaque,Error ** errp)980  static void machine_set_boot(Object *obj, Visitor *v, const char *name,
981                              void *opaque, Error **errp)
982  {
983      ERRP_GUARD();
984      MachineState *ms = MACHINE(obj);
985      BootConfiguration *config = NULL;
986  
987      if (!visit_type_BootConfiguration(v, name, &config, errp)) {
988          return;
989      }
990      if (config->order) {
991          validate_bootdevices(config->order, errp);
992          if (*errp) {
993              goto out_free;
994          }
995      }
996      if (config->once) {
997          validate_bootdevices(config->once, errp);
998          if (*errp) {
999              goto out_free;
1000          }
1001      }
1002  
1003      machine_copy_boot_config(ms, config);
1004      /* Strings live in ms->boot_config.  */
1005      free(config);
1006      return;
1007  
1008  out_free:
1009      qapi_free_BootConfiguration(config);
1010  }
1011  
machine_add_audiodev_property(MachineClass * mc)1012  void machine_add_audiodev_property(MachineClass *mc)
1013  {
1014      ObjectClass *oc = OBJECT_CLASS(mc);
1015  
1016      object_class_property_add_str(oc, "audiodev",
1017                                    machine_get_audiodev,
1018                                    machine_set_audiodev);
1019      object_class_property_set_description(oc, "audiodev",
1020                                            "Audiodev to use for default machine devices");
1021  }
1022  
create_default_memdev(MachineState * ms,const char * path,Error ** errp)1023  static bool create_default_memdev(MachineState *ms, const char *path,
1024                                    Error **errp)
1025  {
1026      Object *obj;
1027      MachineClass *mc = MACHINE_GET_CLASS(ms);
1028      bool r = false;
1029  
1030      obj = object_new(path ? TYPE_MEMORY_BACKEND_FILE : TYPE_MEMORY_BACKEND_RAM);
1031      if (path) {
1032          if (!object_property_set_str(obj, "mem-path", path, errp)) {
1033              goto out;
1034          }
1035      }
1036      if (!object_property_set_int(obj, "size", ms->ram_size, errp)) {
1037          goto out;
1038      }
1039      object_property_add_child(object_get_objects_root(), mc->default_ram_id,
1040                                obj);
1041      /* Ensure backend's memory region name is equal to mc->default_ram_id */
1042      if (!object_property_set_bool(obj, "x-use-canonical-path-for-ramblock-id",
1043                               false, errp)) {
1044          goto out;
1045      }
1046      if (!user_creatable_complete(USER_CREATABLE(obj), errp)) {
1047          goto out;
1048      }
1049      r = object_property_set_link(OBJECT(ms), "memory-backend", obj, errp);
1050  
1051  out:
1052      object_unref(obj);
1053      return r;
1054  }
1055  
machine_class_init(ObjectClass * oc,void * data)1056  static void machine_class_init(ObjectClass *oc, void *data)
1057  {
1058      MachineClass *mc = MACHINE_CLASS(oc);
1059  
1060      /* Default 128 MB as guest ram size */
1061      mc->default_ram_size = 128 * MiB;
1062      mc->rom_file_has_mr = true;
1063      /*
1064       * SMBIOS 3.1.0 7.18.5 Memory Device — Extended Size
1065       * use max possible value that could be encoded into
1066       * 'Extended Size' field (2047Tb).
1067       */
1068      mc->smbios_memory_device_size = 2047 * TiB;
1069  
1070      /* numa node memory size aligned on 8MB by default.
1071       * On Linux, each node's border has to be 8MB aligned
1072       */
1073      mc->numa_mem_align_shift = 23;
1074  
1075      mc->create_default_memdev = create_default_memdev;
1076  
1077      object_class_property_add_str(oc, "kernel",
1078          machine_get_kernel, machine_set_kernel);
1079      object_class_property_set_description(oc, "kernel",
1080          "Linux kernel image file");
1081  
1082      object_class_property_add_str(oc, "initrd",
1083          machine_get_initrd, machine_set_initrd);
1084      object_class_property_set_description(oc, "initrd",
1085          "Linux initial ramdisk file");
1086  
1087      object_class_property_add_str(oc, "append",
1088          machine_get_append, machine_set_append);
1089      object_class_property_set_description(oc, "append",
1090          "Linux kernel command line");
1091  
1092      object_class_property_add_str(oc, "dtb",
1093          machine_get_dtb, machine_set_dtb);
1094      object_class_property_set_description(oc, "dtb",
1095          "Linux kernel device tree file");
1096  
1097      object_class_property_add_str(oc, "dumpdtb",
1098          machine_get_dumpdtb, machine_set_dumpdtb);
1099      object_class_property_set_description(oc, "dumpdtb",
1100          "Dump current dtb to a file and quit");
1101  
1102      object_class_property_add(oc, "boot", "BootConfiguration",
1103          machine_get_boot, machine_set_boot,
1104          NULL, NULL);
1105      object_class_property_set_description(oc, "boot",
1106          "Boot configuration");
1107  
1108      object_class_property_add(oc, "smp", "SMPConfiguration",
1109          machine_get_smp, machine_set_smp,
1110          NULL, NULL);
1111      object_class_property_set_description(oc, "smp",
1112          "CPU topology");
1113  
1114      object_class_property_add(oc, "smp-cache", "SmpCachePropertiesWrapper",
1115          machine_get_smp_cache, machine_set_smp_cache, NULL, NULL);
1116      object_class_property_set_description(oc, "smp-cache",
1117          "Cache properties list for SMP machine");
1118  
1119      object_class_property_add(oc, "phandle-start", "int",
1120          machine_get_phandle_start, machine_set_phandle_start,
1121          NULL, NULL);
1122      object_class_property_set_description(oc, "phandle-start",
1123          "The first phandle ID we may generate dynamically");
1124  
1125      object_class_property_add_str(oc, "dt-compatible",
1126          machine_get_dt_compatible, machine_set_dt_compatible);
1127      object_class_property_set_description(oc, "dt-compatible",
1128          "Overrides the \"compatible\" property of the dt root node");
1129  
1130      object_class_property_add_bool(oc, "dump-guest-core",
1131          machine_get_dump_guest_core, machine_set_dump_guest_core);
1132      object_class_property_set_description(oc, "dump-guest-core",
1133          "Include guest memory in a core dump");
1134  
1135      object_class_property_add_bool(oc, "mem-merge",
1136          machine_get_mem_merge, machine_set_mem_merge);
1137      object_class_property_set_description(oc, "mem-merge",
1138          "Enable/disable memory merge support");
1139  
1140      object_class_property_add_bool(oc, "usb",
1141          machine_get_usb, machine_set_usb);
1142      object_class_property_set_description(oc, "usb",
1143          "Set on/off to enable/disable usb");
1144  
1145      object_class_property_add_bool(oc, "graphics",
1146          machine_get_graphics, machine_set_graphics);
1147      object_class_property_set_description(oc, "graphics",
1148          "Set on/off to enable/disable graphics emulation");
1149  
1150      object_class_property_add_str(oc, "firmware",
1151          machine_get_firmware, machine_set_firmware);
1152      object_class_property_set_description(oc, "firmware",
1153          "Firmware image");
1154  
1155      object_class_property_add_bool(oc, "suppress-vmdesc",
1156          machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
1157      object_class_property_set_description(oc, "suppress-vmdesc",
1158          "Set on to disable self-describing migration");
1159  
1160      object_class_property_add_link(oc, "confidential-guest-support",
1161                                     TYPE_CONFIDENTIAL_GUEST_SUPPORT,
1162                                     offsetof(MachineState, cgs),
1163                                     machine_check_confidential_guest_support,
1164                                     OBJ_PROP_LINK_STRONG);
1165      object_class_property_set_description(oc, "confidential-guest-support",
1166                                            "Set confidential guest scheme to support");
1167  
1168      /* For compatibility */
1169      object_class_property_add_str(oc, "memory-encryption",
1170          machine_get_memory_encryption, machine_set_memory_encryption);
1171      object_class_property_set_description(oc, "memory-encryption",
1172          "Set memory encryption object to use");
1173  
1174      object_class_property_add_link(oc, "memory-backend", TYPE_MEMORY_BACKEND,
1175                                     offsetof(MachineState, memdev), object_property_allow_set_link,
1176                                     OBJ_PROP_LINK_STRONG);
1177      object_class_property_set_description(oc, "memory-backend",
1178                                            "Set RAM backend"
1179                                            "Valid value is ID of hostmem based backend");
1180  
1181      object_class_property_add(oc, "memory", "MemorySizeConfiguration",
1182          machine_get_mem, machine_set_mem,
1183          NULL, NULL);
1184      object_class_property_set_description(oc, "memory",
1185          "Memory size configuration");
1186  }
1187  
machine_class_base_init(ObjectClass * oc,void * data)1188  static void machine_class_base_init(ObjectClass *oc, void *data)
1189  {
1190      MachineClass *mc = MACHINE_CLASS(oc);
1191      mc->max_cpus = mc->max_cpus ?: 1;
1192      mc->min_cpus = mc->min_cpus ?: 1;
1193      mc->default_cpus = mc->default_cpus ?: 1;
1194  
1195      if (!object_class_is_abstract(oc)) {
1196          const char *cname = object_class_get_name(oc);
1197          assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
1198          mc->name = g_strndup(cname,
1199                              strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
1200          mc->compat_props = g_ptr_array_new();
1201      }
1202  }
1203  
machine_initfn(Object * obj)1204  static void machine_initfn(Object *obj)
1205  {
1206      MachineState *ms = MACHINE(obj);
1207      MachineClass *mc = MACHINE_GET_CLASS(obj);
1208  
1209      container_get(obj, "/peripheral");
1210      container_get(obj, "/peripheral-anon");
1211  
1212      ms->dump_guest_core = true;
1213      ms->mem_merge = (QEMU_MADV_MERGEABLE != QEMU_MADV_INVALID);
1214      ms->enable_graphics = true;
1215      ms->kernel_cmdline = g_strdup("");
1216      ms->ram_size = mc->default_ram_size;
1217      ms->maxram_size = mc->default_ram_size;
1218  
1219      if (mc->nvdimm_supported) {
1220          ms->nvdimms_state = g_new0(NVDIMMState, 1);
1221          object_property_add_bool(obj, "nvdimm",
1222                                   machine_get_nvdimm, machine_set_nvdimm);
1223          object_property_set_description(obj, "nvdimm",
1224                                          "Set on/off to enable/disable "
1225                                          "NVDIMM instantiation");
1226  
1227          object_property_add_str(obj, "nvdimm-persistence",
1228                                  machine_get_nvdimm_persistence,
1229                                  machine_set_nvdimm_persistence);
1230          object_property_set_description(obj, "nvdimm-persistence",
1231                                          "Set NVDIMM persistence"
1232                                          "Valid values are cpu, mem-ctrl");
1233      }
1234  
1235      if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
1236          ms->numa_state = g_new0(NumaState, 1);
1237          object_property_add_bool(obj, "hmat",
1238                                   machine_get_hmat, machine_set_hmat);
1239          object_property_set_description(obj, "hmat",
1240                                          "Set on/off to enable/disable "
1241                                          "ACPI Heterogeneous Memory Attribute "
1242                                          "Table (HMAT)");
1243      }
1244  
1245      /* default to mc->default_cpus */
1246      ms->smp.cpus = mc->default_cpus;
1247      ms->smp.max_cpus = mc->default_cpus;
1248      ms->smp.drawers = 1;
1249      ms->smp.books = 1;
1250      ms->smp.sockets = 1;
1251      ms->smp.dies = 1;
1252      ms->smp.clusters = 1;
1253      ms->smp.modules = 1;
1254      ms->smp.cores = 1;
1255      ms->smp.threads = 1;
1256  
1257      for (int i = 0; i < CACHE_LEVEL_AND_TYPE__MAX; i++) {
1258          ms->smp_cache.props[i].cache = (CacheLevelAndType)i;
1259          ms->smp_cache.props[i].topology = CPU_TOPOLOGY_LEVEL_DEFAULT;
1260      }
1261  
1262      machine_copy_boot_config(ms, &(BootConfiguration){ 0 });
1263  }
1264  
machine_finalize(Object * obj)1265  static void machine_finalize(Object *obj)
1266  {
1267      MachineState *ms = MACHINE(obj);
1268  
1269      machine_free_boot_config(ms);
1270      g_free(ms->kernel_filename);
1271      g_free(ms->initrd_filename);
1272      g_free(ms->kernel_cmdline);
1273      g_free(ms->dtb);
1274      g_free(ms->dumpdtb);
1275      g_free(ms->dt_compatible);
1276      g_free(ms->firmware);
1277      g_free(ms->device_memory);
1278      g_free(ms->nvdimms_state);
1279      g_free(ms->numa_state);
1280      g_free(ms->audiodev);
1281  }
1282  
machine_usb(MachineState * machine)1283  bool machine_usb(MachineState *machine)
1284  {
1285      return machine->usb;
1286  }
1287  
machine_phandle_start(MachineState * machine)1288  int machine_phandle_start(MachineState *machine)
1289  {
1290      return machine->phandle_start;
1291  }
1292  
machine_dump_guest_core(MachineState * machine)1293  bool machine_dump_guest_core(MachineState *machine)
1294  {
1295      return machine->dump_guest_core;
1296  }
1297  
machine_mem_merge(MachineState * machine)1298  bool machine_mem_merge(MachineState *machine)
1299  {
1300      return machine->mem_merge;
1301  }
1302  
machine_require_guest_memfd(MachineState * machine)1303  bool machine_require_guest_memfd(MachineState *machine)
1304  {
1305      return machine->cgs && machine->cgs->require_guest_memfd;
1306  }
1307  
cpu_slot_to_string(const CPUArchId * cpu)1308  static char *cpu_slot_to_string(const CPUArchId *cpu)
1309  {
1310      GString *s = g_string_new(NULL);
1311      if (cpu->props.has_socket_id) {
1312          g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1313      }
1314      if (cpu->props.has_die_id) {
1315          if (s->len) {
1316              g_string_append_printf(s, ", ");
1317          }
1318          g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1319      }
1320      if (cpu->props.has_cluster_id) {
1321          if (s->len) {
1322              g_string_append_printf(s, ", ");
1323          }
1324          g_string_append_printf(s, "cluster-id: %"PRId64, cpu->props.cluster_id);
1325      }
1326      if (cpu->props.has_module_id) {
1327          if (s->len) {
1328              g_string_append_printf(s, ", ");
1329          }
1330          g_string_append_printf(s, "module-id: %"PRId64, cpu->props.module_id);
1331      }
1332      if (cpu->props.has_core_id) {
1333          if (s->len) {
1334              g_string_append_printf(s, ", ");
1335          }
1336          g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1337      }
1338      if (cpu->props.has_thread_id) {
1339          if (s->len) {
1340              g_string_append_printf(s, ", ");
1341          }
1342          g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1343      }
1344      return g_string_free(s, false);
1345  }
1346  
numa_validate_initiator(NumaState * numa_state)1347  static void numa_validate_initiator(NumaState *numa_state)
1348  {
1349      int i;
1350      NodeInfo *numa_info = numa_state->nodes;
1351  
1352      for (i = 0; i < numa_state->num_nodes; i++) {
1353          if (numa_info[i].initiator == MAX_NODES) {
1354              continue;
1355          }
1356  
1357          if (!numa_info[numa_info[i].initiator].present) {
1358              error_report("NUMA node %" PRIu16 " is missing, use "
1359                           "'-numa node' option to declare it first",
1360                           numa_info[i].initiator);
1361              exit(1);
1362          }
1363  
1364          if (!numa_info[numa_info[i].initiator].has_cpu) {
1365              error_report("The initiator of NUMA node %d is invalid", i);
1366              exit(1);
1367          }
1368      }
1369  }
1370  
machine_numa_finish_cpu_init(MachineState * machine)1371  static void machine_numa_finish_cpu_init(MachineState *machine)
1372  {
1373      int i;
1374      bool default_mapping;
1375      GString *s = g_string_new(NULL);
1376      MachineClass *mc = MACHINE_GET_CLASS(machine);
1377      const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1378  
1379      assert(machine->numa_state->num_nodes);
1380      for (i = 0; i < possible_cpus->len; i++) {
1381          if (possible_cpus->cpus[i].props.has_node_id) {
1382              break;
1383          }
1384      }
1385      default_mapping = (i == possible_cpus->len);
1386  
1387      for (i = 0; i < possible_cpus->len; i++) {
1388          const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1389  
1390          if (!cpu_slot->props.has_node_id) {
1391              /* fetch default mapping from board and enable it */
1392              CpuInstanceProperties props = cpu_slot->props;
1393  
1394              props.node_id = mc->get_default_cpu_node_id(machine, i);
1395              if (!default_mapping) {
1396                  /* record slots with not set mapping,
1397                   * TODO: make it hard error in future */
1398                  char *cpu_str = cpu_slot_to_string(cpu_slot);
1399                  g_string_append_printf(s, "%sCPU %d [%s]",
1400                                         s->len ? ", " : "", i, cpu_str);
1401                  g_free(cpu_str);
1402  
1403                  /* non mapped cpus used to fallback to node 0 */
1404                  props.node_id = 0;
1405              }
1406  
1407              props.has_node_id = true;
1408              machine_set_cpu_numa_node(machine, &props, &error_fatal);
1409          }
1410      }
1411  
1412      if (machine->numa_state->hmat_enabled) {
1413          numa_validate_initiator(machine->numa_state);
1414      }
1415  
1416      if (s->len && !qtest_enabled()) {
1417          warn_report("CPU(s) not present in any NUMA nodes: %s",
1418                      s->str);
1419          warn_report("All CPU(s) up to maxcpus should be described "
1420                      "in NUMA config, ability to start up with partial NUMA "
1421                      "mappings is obsoleted and will be removed in future");
1422      }
1423      g_string_free(s, true);
1424  }
1425  
validate_cpu_cluster_to_numa_boundary(MachineState * ms)1426  static void validate_cpu_cluster_to_numa_boundary(MachineState *ms)
1427  {
1428      MachineClass *mc = MACHINE_GET_CLASS(ms);
1429      NumaState *state = ms->numa_state;
1430      const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(ms);
1431      const CPUArchId *cpus = possible_cpus->cpus;
1432      int i, j;
1433  
1434      if (qtest_enabled() || state->num_nodes <= 1 || possible_cpus->len <= 1) {
1435          return;
1436      }
1437  
1438      /*
1439       * The Linux scheduling domain can't be parsed when the multiple CPUs
1440       * in one cluster have been associated with different NUMA nodes. However,
1441       * it's fine to associate one NUMA node with CPUs in different clusters.
1442       */
1443      for (i = 0; i < possible_cpus->len; i++) {
1444          for (j = i + 1; j < possible_cpus->len; j++) {
1445              if (cpus[i].props.has_socket_id &&
1446                  cpus[i].props.has_cluster_id &&
1447                  cpus[i].props.has_node_id &&
1448                  cpus[j].props.has_socket_id &&
1449                  cpus[j].props.has_cluster_id &&
1450                  cpus[j].props.has_node_id &&
1451                  cpus[i].props.socket_id == cpus[j].props.socket_id &&
1452                  cpus[i].props.cluster_id == cpus[j].props.cluster_id &&
1453                  cpus[i].props.node_id != cpus[j].props.node_id) {
1454                  warn_report("CPU-%d and CPU-%d in socket-%" PRId64 "-cluster-%" PRId64
1455                               " have been associated with node-%" PRId64 " and node-%" PRId64
1456                               " respectively. It can cause OSes like Linux to"
1457                               " misbehave", i, j, cpus[i].props.socket_id,
1458                               cpus[i].props.cluster_id, cpus[i].props.node_id,
1459                               cpus[j].props.node_id);
1460              }
1461          }
1462      }
1463  }
1464  
machine_consume_memdev(MachineState * machine,HostMemoryBackend * backend)1465  MemoryRegion *machine_consume_memdev(MachineState *machine,
1466                                       HostMemoryBackend *backend)
1467  {
1468      MemoryRegion *ret = host_memory_backend_get_memory(backend);
1469  
1470      if (host_memory_backend_is_mapped(backend)) {
1471          error_report("memory backend %s can't be used multiple times.",
1472                       object_get_canonical_path_component(OBJECT(backend)));
1473          exit(EXIT_FAILURE);
1474      }
1475      host_memory_backend_set_mapped(backend, true);
1476      vmstate_register_ram_global(ret);
1477      return ret;
1478  }
1479  
machine_class_default_cpu_type(MachineClass * mc)1480  const char *machine_class_default_cpu_type(MachineClass *mc)
1481  {
1482      if (mc->valid_cpu_types && !mc->valid_cpu_types[1]) {
1483          /* Only a single CPU type allowed: use it as default. */
1484          return mc->valid_cpu_types[0];
1485      }
1486      return mc->default_cpu_type;
1487  }
1488  
is_cpu_type_supported(const MachineState * machine,Error ** errp)1489  static bool is_cpu_type_supported(const MachineState *machine, Error **errp)
1490  {
1491      MachineClass *mc = MACHINE_GET_CLASS(machine);
1492      ObjectClass *oc = object_class_by_name(machine->cpu_type);
1493      CPUClass *cc;
1494      int i;
1495  
1496      /*
1497       * Check if the user specified CPU type is supported when the valid
1498       * CPU types have been determined. Note that the user specified CPU
1499       * type is provided through '-cpu' option.
1500       */
1501      if (mc->valid_cpu_types) {
1502          assert(mc->valid_cpu_types[0] != NULL);
1503          for (i = 0; mc->valid_cpu_types[i]; i++) {
1504              if (object_class_dynamic_cast(oc, mc->valid_cpu_types[i])) {
1505                  break;
1506              }
1507          }
1508  
1509          /* The user specified CPU type isn't valid */
1510          if (!mc->valid_cpu_types[i]) {
1511              g_autofree char *requested = cpu_model_from_type(machine->cpu_type);
1512              error_setg(errp, "Invalid CPU model: %s", requested);
1513              if (!mc->valid_cpu_types[1]) {
1514                  g_autofree char *model = cpu_model_from_type(
1515                                                   mc->valid_cpu_types[0]);
1516                  error_append_hint(errp, "The only valid type is: %s\n", model);
1517              } else {
1518                  error_append_hint(errp, "The valid models are: ");
1519                  for (i = 0; mc->valid_cpu_types[i]; i++) {
1520                      g_autofree char *model = cpu_model_from_type(
1521                                                   mc->valid_cpu_types[i]);
1522                      error_append_hint(errp, "%s%s",
1523                                        model,
1524                                        mc->valid_cpu_types[i + 1] ? ", " : "");
1525                  }
1526                  error_append_hint(errp, "\n");
1527              }
1528  
1529              return false;
1530          }
1531      }
1532  
1533      /* Check if CPU type is deprecated and warn if so */
1534      cc = CPU_CLASS(oc);
1535      assert(cc != NULL);
1536      if (cc->deprecation_note) {
1537          warn_report("CPU model %s is deprecated -- %s",
1538                      machine->cpu_type, cc->deprecation_note);
1539      }
1540  
1541      return true;
1542  }
1543  
machine_run_board_init(MachineState * machine,const char * mem_path,Error ** errp)1544  void machine_run_board_init(MachineState *machine, const char *mem_path, Error **errp)
1545  {
1546      ERRP_GUARD();
1547      MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1548  
1549      /* This checkpoint is required by replay to separate prior clock
1550         reading from the other reads, because timer polling functions query
1551         clock values from the log. */
1552      replay_checkpoint(CHECKPOINT_INIT);
1553  
1554      if (!xen_enabled()) {
1555          /* On 32-bit hosts, QEMU is limited by virtual address space */
1556          if (machine->ram_size > (2047 << 20) && HOST_LONG_BITS == 32) {
1557              error_setg(errp, "at most 2047 MB RAM can be simulated");
1558              return;
1559          }
1560      }
1561  
1562      if (machine->memdev) {
1563          ram_addr_t backend_size = object_property_get_uint(OBJECT(machine->memdev),
1564                                                             "size",  &error_abort);
1565          if (backend_size != machine->ram_size) {
1566              error_setg(errp, "Machine memory size does not match the size of the memory backend");
1567              return;
1568          }
1569      } else if (machine_class->default_ram_id && machine->ram_size &&
1570                 numa_uses_legacy_mem()) {
1571          if (object_property_find(object_get_objects_root(),
1572                                   machine_class->default_ram_id)) {
1573              error_setg(errp, "object's id '%s' is reserved for the default"
1574                  " RAM backend, it can't be used for any other purposes",
1575                  machine_class->default_ram_id);
1576              error_append_hint(errp,
1577                  "Change the object's 'id' to something else or disable"
1578                  " automatic creation of the default RAM backend by setting"
1579                  " 'memory-backend=%s' with '-machine'.\n",
1580                  machine_class->default_ram_id);
1581              return;
1582          }
1583  
1584          if (!machine_class->create_default_memdev(current_machine, mem_path,
1585                                                    errp)) {
1586              return;
1587          }
1588      }
1589  
1590      if (machine->numa_state) {
1591          numa_complete_configuration(machine);
1592          if (machine->numa_state->num_nodes) {
1593              machine_numa_finish_cpu_init(machine);
1594              if (machine_class->cpu_cluster_has_numa_boundary) {
1595                  validate_cpu_cluster_to_numa_boundary(machine);
1596              }
1597          }
1598      }
1599  
1600      if (!machine->ram && machine->memdev) {
1601          machine->ram = machine_consume_memdev(machine, machine->memdev);
1602      }
1603  
1604      /* Check if the CPU type is supported */
1605      if (machine->cpu_type && !is_cpu_type_supported(machine, errp)) {
1606          return;
1607      }
1608  
1609      if (machine->cgs) {
1610          /*
1611           * With confidential guests, the host can't see the real
1612           * contents of RAM, so there's no point in it trying to merge
1613           * areas.
1614           */
1615          machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1616  
1617          /*
1618           * Virtio devices can't count on directly accessing guest
1619           * memory, so they need iommu_platform=on to use normal DMA
1620           * mechanisms.  That requires also disabling legacy virtio
1621           * support for those virtio pci devices which allow it.
1622           */
1623          object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1624                                     "on", true);
1625          object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1626                                     "on", false);
1627      }
1628  
1629      accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1630      machine_class->init(machine);
1631      phase_advance(PHASE_MACHINE_INITIALIZED);
1632  }
1633  
1634  static NotifierList machine_init_done_notifiers =
1635      NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1636  
qemu_add_machine_init_done_notifier(Notifier * notify)1637  void qemu_add_machine_init_done_notifier(Notifier *notify)
1638  {
1639      notifier_list_add(&machine_init_done_notifiers, notify);
1640      if (phase_check(PHASE_MACHINE_READY)) {
1641          notify->notify(notify, NULL);
1642      }
1643  }
1644  
qemu_remove_machine_init_done_notifier(Notifier * notify)1645  void qemu_remove_machine_init_done_notifier(Notifier *notify)
1646  {
1647      notifier_remove(notify);
1648  }
1649  
qdev_machine_creation_done(void)1650  void qdev_machine_creation_done(void)
1651  {
1652      cpu_synchronize_all_post_init();
1653  
1654      if (current_machine->boot_config.once) {
1655          qemu_boot_set(current_machine->boot_config.once, &error_fatal);
1656          qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_config.order));
1657      }
1658  
1659      /*
1660       * ok, initial machine setup is done, starting from now we can
1661       * only create hotpluggable devices
1662       */
1663      phase_advance(PHASE_MACHINE_READY);
1664      qdev_assert_realized_properly();
1665  
1666      /* TODO: once all bus devices are qdevified, this should be done
1667       * when bus is created by qdev.c */
1668      /*
1669       * This is where we arrange for the sysbus to be reset when the
1670       * whole simulation is reset. In turn, resetting the sysbus will cause
1671       * all devices hanging off it (and all their child buses, recursively)
1672       * to be reset. Note that this will *not* reset any Device objects
1673       * which are not attached to some part of the qbus tree!
1674       */
1675      qemu_register_resettable(OBJECT(sysbus_get_default()));
1676  
1677      notifier_list_notify(&machine_init_done_notifiers, NULL);
1678  
1679      if (rom_check_and_register_reset() != 0) {
1680          exit(1);
1681      }
1682  
1683      replay_start();
1684  
1685      /* This checkpoint is required by replay to separate prior clock
1686         reading from the other reads, because timer polling functions query
1687         clock values from the log. */
1688      replay_checkpoint(CHECKPOINT_RESET);
1689      qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1690      register_global_state();
1691  }
1692  
1693  static const TypeInfo machine_info = {
1694      .name = TYPE_MACHINE,
1695      .parent = TYPE_OBJECT,
1696      .abstract = true,
1697      .class_size = sizeof(MachineClass),
1698      .class_init    = machine_class_init,
1699      .class_base_init = machine_class_base_init,
1700      .instance_size = sizeof(MachineState),
1701      .instance_init = machine_initfn,
1702      .instance_finalize = machine_finalize,
1703  };
1704  
machine_register_types(void)1705  static void machine_register_types(void)
1706  {
1707      type_register_static(&machine_info);
1708  }
1709  
1710  type_init(machine_register_types)
1711