include/ui/console.h: Delete is_surface_bgr()
[qemu/ar7.git] / hw / core / machine.c
blob257a664ea2ed4f41b256d379d0b56defe2886769
1 /*
2 * QEMU Machine
4 * Copyright (C) 2014 Red Hat Inc
6 * Authors:
7 * Marcel Apfelbaum <marcel.a@redhat.com>
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.
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/visitor.h"
23 #include "hw/sysbus.h"
24 #include "sysemu/cpus.h"
25 #include "sysemu/sysemu.h"
26 #include "sysemu/reset.h"
27 #include "sysemu/runstate.h"
28 #include "sysemu/numa.h"
29 #include "qemu/error-report.h"
30 #include "sysemu/qtest.h"
31 #include "hw/pci/pci.h"
32 #include "hw/mem/nvdimm.h"
33 #include "migration/global_state.h"
34 #include "migration/vmstate.h"
35 #include "exec/confidential-guest-support.h"
36 #include "hw/virtio/virtio.h"
37 #include "hw/virtio/virtio-pci.h"
39 GlobalProperty hw_compat_5_2[] = {
40 { "ICH9-LPC", "smm-compat", "on"},
41 { "PIIX4_PM", "smm-compat", "on"},
42 { "virtio-blk-device", "report-discard-granularity", "off" },
43 { "virtio-net-pci", "vectors", "3"},
45 const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
47 GlobalProperty hw_compat_5_1[] = {
48 { "vhost-scsi", "num_queues", "1"},
49 { "vhost-user-blk", "num-queues", "1"},
50 { "vhost-user-scsi", "num_queues", "1"},
51 { "virtio-blk-device", "num-queues", "1"},
52 { "virtio-scsi-device", "num_queues", "1"},
53 { "nvme", "use-intel-id", "on"},
54 { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
56 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
58 GlobalProperty hw_compat_5_0[] = {
59 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
60 { "virtio-balloon-device", "page-poison", "false" },
61 { "vmport", "x-read-set-eax", "off" },
62 { "vmport", "x-signal-unsupported-cmd", "off" },
63 { "vmport", "x-report-vmx-type", "off" },
64 { "vmport", "x-cmds-v2", "off" },
65 { "virtio-device", "x-disable-legacy-check", "true" },
67 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
69 GlobalProperty hw_compat_4_2[] = {
70 { "virtio-blk-device", "queue-size", "128"},
71 { "virtio-scsi-device", "virtqueue_size", "128"},
72 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
73 { "virtio-blk-device", "seg-max-adjust", "off"},
74 { "virtio-scsi-device", "seg_max_adjust", "off"},
75 { "vhost-blk-device", "seg_max_adjust", "off"},
76 { "usb-host", "suppress-remote-wake", "off" },
77 { "usb-redir", "suppress-remote-wake", "off" },
78 { "qxl", "revision", "4" },
79 { "qxl-vga", "revision", "4" },
80 { "fw_cfg", "acpi-mr-restore", "false" },
81 { "virtio-device", "use-disabled-flag", "false" },
83 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
85 GlobalProperty hw_compat_4_1[] = {
86 { "virtio-pci", "x-pcie-flr-init", "off" },
88 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
90 GlobalProperty hw_compat_4_0[] = {
91 { "VGA", "edid", "false" },
92 { "secondary-vga", "edid", "false" },
93 { "bochs-display", "edid", "false" },
94 { "virtio-vga", "edid", "false" },
95 { "virtio-gpu-device", "edid", "false" },
96 { "virtio-device", "use-started", "false" },
97 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
98 { "pl031", "migrate-tick-offset", "false" },
100 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
102 GlobalProperty hw_compat_3_1[] = {
103 { "pcie-root-port", "x-speed", "2_5" },
104 { "pcie-root-port", "x-width", "1" },
105 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
106 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
107 { "tpm-crb", "ppi", "false" },
108 { "tpm-tis", "ppi", "false" },
109 { "usb-kbd", "serial", "42" },
110 { "usb-mouse", "serial", "42" },
111 { "usb-tablet", "serial", "42" },
112 { "virtio-blk-device", "discard", "false" },
113 { "virtio-blk-device", "write-zeroes", "false" },
114 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
115 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
117 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
119 GlobalProperty hw_compat_3_0[] = {};
120 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
122 GlobalProperty hw_compat_2_12[] = {
123 { "migration", "decompress-error-check", "off" },
124 { "hda-audio", "use-timer", "false" },
125 { "cirrus-vga", "global-vmstate", "true" },
126 { "VGA", "global-vmstate", "true" },
127 { "vmware-svga", "global-vmstate", "true" },
128 { "qxl-vga", "global-vmstate", "true" },
130 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
132 GlobalProperty hw_compat_2_11[] = {
133 { "hpet", "hpet-offset-saved", "false" },
134 { "virtio-blk-pci", "vectors", "2" },
135 { "vhost-user-blk-pci", "vectors", "2" },
136 { "e1000", "migrate_tso_props", "off" },
138 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
140 GlobalProperty hw_compat_2_10[] = {
141 { "virtio-mouse-device", "wheel-axis", "false" },
142 { "virtio-tablet-device", "wheel-axis", "false" },
144 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
146 GlobalProperty hw_compat_2_9[] = {
147 { "pci-bridge", "shpc", "off" },
148 { "intel-iommu", "pt", "off" },
149 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
150 { "pcie-root-port", "x-migrate-msix", "false" },
152 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
154 GlobalProperty hw_compat_2_8[] = {
155 { "fw_cfg_mem", "x-file-slots", "0x10" },
156 { "fw_cfg_io", "x-file-slots", "0x10" },
157 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
158 { "pci-bridge", "shpc", "on" },
159 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
160 { "virtio-pci", "x-pcie-deverr-init", "off" },
161 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
162 { "virtio-pci", "x-pcie-pm-init", "off" },
163 { "cirrus-vga", "vgamem_mb", "8" },
164 { "isa-cirrus-vga", "vgamem_mb", "8" },
166 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
168 GlobalProperty hw_compat_2_7[] = {
169 { "virtio-pci", "page-per-vq", "on" },
170 { "virtio-serial-device", "emergency-write", "off" },
171 { "ioapic", "version", "0x11" },
172 { "intel-iommu", "x-buggy-eim", "true" },
173 { "virtio-pci", "x-ignore-backend-features", "on" },
175 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
177 GlobalProperty hw_compat_2_6[] = {
178 { "virtio-mmio", "format_transport_address", "off" },
179 /* Optional because not all virtio-pci devices support legacy mode */
180 { "virtio-pci", "disable-modern", "on", .optional = true },
181 { "virtio-pci", "disable-legacy", "off", .optional = true },
183 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
185 GlobalProperty hw_compat_2_5[] = {
186 { "isa-fdc", "fallback", "144" },
187 { "pvscsi", "x-old-pci-configuration", "on" },
188 { "pvscsi", "x-disable-pcie", "on" },
189 { "vmxnet3", "x-old-msi-offsets", "on" },
190 { "vmxnet3", "x-disable-pcie", "on" },
192 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
194 GlobalProperty hw_compat_2_4[] = {
195 /* Optional because the 'scsi' property is Linux-only */
196 { "virtio-blk-device", "scsi", "true", .optional = true },
197 { "e1000", "extra_mac_registers", "off" },
198 { "virtio-pci", "x-disable-pcie", "on" },
199 { "virtio-pci", "migrate-extra", "off" },
200 { "fw_cfg_mem", "dma_enabled", "off" },
201 { "fw_cfg_io", "dma_enabled", "off" }
203 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
205 GlobalProperty hw_compat_2_3[] = {
206 { "virtio-blk-pci", "any_layout", "off" },
207 { "virtio-balloon-pci", "any_layout", "off" },
208 { "virtio-serial-pci", "any_layout", "off" },
209 { "virtio-9p-pci", "any_layout", "off" },
210 { "virtio-rng-pci", "any_layout", "off" },
211 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
212 { "migration", "send-configuration", "off" },
213 { "migration", "send-section-footer", "off" },
214 { "migration", "store-global-state", "off" },
216 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
218 GlobalProperty hw_compat_2_2[] = {};
219 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
221 GlobalProperty hw_compat_2_1[] = {
222 { "intel-hda", "old_msi_addr", "on" },
223 { "VGA", "qemu-extended-regs", "off" },
224 { "secondary-vga", "qemu-extended-regs", "off" },
225 { "virtio-scsi-pci", "any_layout", "off" },
226 { "usb-mouse", "usb_version", "1" },
227 { "usb-kbd", "usb_version", "1" },
228 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
230 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
232 MachineState *current_machine;
234 static char *machine_get_kernel(Object *obj, Error **errp)
236 MachineState *ms = MACHINE(obj);
238 return g_strdup(ms->kernel_filename);
241 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
243 MachineState *ms = MACHINE(obj);
245 g_free(ms->kernel_filename);
246 ms->kernel_filename = g_strdup(value);
249 static char *machine_get_initrd(Object *obj, Error **errp)
251 MachineState *ms = MACHINE(obj);
253 return g_strdup(ms->initrd_filename);
256 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
258 MachineState *ms = MACHINE(obj);
260 g_free(ms->initrd_filename);
261 ms->initrd_filename = g_strdup(value);
264 static char *machine_get_append(Object *obj, Error **errp)
266 MachineState *ms = MACHINE(obj);
268 return g_strdup(ms->kernel_cmdline);
271 static void machine_set_append(Object *obj, const char *value, Error **errp)
273 MachineState *ms = MACHINE(obj);
275 g_free(ms->kernel_cmdline);
276 ms->kernel_cmdline = g_strdup(value);
279 static char *machine_get_dtb(Object *obj, Error **errp)
281 MachineState *ms = MACHINE(obj);
283 return g_strdup(ms->dtb);
286 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
288 MachineState *ms = MACHINE(obj);
290 g_free(ms->dtb);
291 ms->dtb = g_strdup(value);
294 static char *machine_get_dumpdtb(Object *obj, Error **errp)
296 MachineState *ms = MACHINE(obj);
298 return g_strdup(ms->dumpdtb);
301 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
303 MachineState *ms = MACHINE(obj);
305 g_free(ms->dumpdtb);
306 ms->dumpdtb = g_strdup(value);
309 static void machine_get_phandle_start(Object *obj, Visitor *v,
310 const char *name, void *opaque,
311 Error **errp)
313 MachineState *ms = MACHINE(obj);
314 int64_t value = ms->phandle_start;
316 visit_type_int(v, name, &value, errp);
319 static void machine_set_phandle_start(Object *obj, Visitor *v,
320 const char *name, void *opaque,
321 Error **errp)
323 MachineState *ms = MACHINE(obj);
324 int64_t value;
326 if (!visit_type_int(v, name, &value, errp)) {
327 return;
330 ms->phandle_start = value;
333 static char *machine_get_dt_compatible(Object *obj, Error **errp)
335 MachineState *ms = MACHINE(obj);
337 return g_strdup(ms->dt_compatible);
340 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
342 MachineState *ms = MACHINE(obj);
344 g_free(ms->dt_compatible);
345 ms->dt_compatible = g_strdup(value);
348 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
350 MachineState *ms = MACHINE(obj);
352 return ms->dump_guest_core;
355 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
357 MachineState *ms = MACHINE(obj);
359 ms->dump_guest_core = value;
362 static bool machine_get_mem_merge(Object *obj, Error **errp)
364 MachineState *ms = MACHINE(obj);
366 return ms->mem_merge;
369 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
371 MachineState *ms = MACHINE(obj);
373 ms->mem_merge = value;
376 static bool machine_get_usb(Object *obj, Error **errp)
378 MachineState *ms = MACHINE(obj);
380 return ms->usb;
383 static void machine_set_usb(Object *obj, bool value, Error **errp)
385 MachineState *ms = MACHINE(obj);
387 ms->usb = value;
388 ms->usb_disabled = !value;
391 static bool machine_get_graphics(Object *obj, Error **errp)
393 MachineState *ms = MACHINE(obj);
395 return ms->enable_graphics;
398 static void machine_set_graphics(Object *obj, bool value, Error **errp)
400 MachineState *ms = MACHINE(obj);
402 ms->enable_graphics = value;
405 static char *machine_get_firmware(Object *obj, Error **errp)
407 MachineState *ms = MACHINE(obj);
409 return g_strdup(ms->firmware);
412 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
414 MachineState *ms = MACHINE(obj);
416 g_free(ms->firmware);
417 ms->firmware = g_strdup(value);
420 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
422 MachineState *ms = MACHINE(obj);
424 ms->suppress_vmdesc = value;
427 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
429 MachineState *ms = MACHINE(obj);
431 return ms->suppress_vmdesc;
434 static char *machine_get_memory_encryption(Object *obj, Error **errp)
436 MachineState *ms = MACHINE(obj);
438 if (ms->cgs) {
439 return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
442 return NULL;
445 static void machine_set_memory_encryption(Object *obj, const char *value,
446 Error **errp)
448 Object *cgs =
449 object_resolve_path_component(object_get_objects_root(), value);
451 if (!cgs) {
452 error_setg(errp, "No such memory encryption object '%s'", value);
453 return;
456 object_property_set_link(obj, "confidential-guest-support", cgs, errp);
459 static void machine_check_confidential_guest_support(const Object *obj,
460 const char *name,
461 Object *new_target,
462 Error **errp)
465 * So far the only constraint is that the target has the
466 * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
467 * by the QOM core
471 static bool machine_get_nvdimm(Object *obj, Error **errp)
473 MachineState *ms = MACHINE(obj);
475 return ms->nvdimms_state->is_enabled;
478 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
480 MachineState *ms = MACHINE(obj);
482 ms->nvdimms_state->is_enabled = value;
485 static bool machine_get_hmat(Object *obj, Error **errp)
487 MachineState *ms = MACHINE(obj);
489 return ms->numa_state->hmat_enabled;
492 static void machine_set_hmat(Object *obj, bool value, Error **errp)
494 MachineState *ms = MACHINE(obj);
496 ms->numa_state->hmat_enabled = value;
499 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
501 MachineState *ms = MACHINE(obj);
503 return g_strdup(ms->nvdimms_state->persistence_string);
506 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
507 Error **errp)
509 MachineState *ms = MACHINE(obj);
510 NVDIMMState *nvdimms_state = ms->nvdimms_state;
512 if (strcmp(value, "cpu") == 0) {
513 nvdimms_state->persistence = 3;
514 } else if (strcmp(value, "mem-ctrl") == 0) {
515 nvdimms_state->persistence = 2;
516 } else {
517 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
518 value);
519 return;
522 g_free(nvdimms_state->persistence_string);
523 nvdimms_state->persistence_string = g_strdup(value);
526 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
528 QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
531 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
533 MachineState *machine = opaque;
534 MachineClass *mc = MACHINE_GET_CLASS(machine);
535 bool allowed = false;
536 strList *wl;
538 for (wl = mc->allowed_dynamic_sysbus_devices;
539 !allowed && wl;
540 wl = wl->next) {
541 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
544 if (!allowed) {
545 error_report("Option '-device %s' cannot be handled by this machine",
546 object_class_get_name(object_get_class(OBJECT(sbdev))));
547 exit(1);
551 static char *machine_get_memdev(Object *obj, Error **errp)
553 MachineState *ms = MACHINE(obj);
555 return g_strdup(ms->ram_memdev_id);
558 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
560 MachineState *ms = MACHINE(obj);
562 g_free(ms->ram_memdev_id);
563 ms->ram_memdev_id = g_strdup(value);
567 static void machine_init_notify(Notifier *notifier, void *data)
569 MachineState *machine = MACHINE(qdev_get_machine());
572 * Loop through all dynamically created sysbus devices and check if they are
573 * all allowed. If a device is not allowed, error out.
575 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
578 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
580 int i;
581 HotpluggableCPUList *head = NULL;
582 MachineClass *mc = MACHINE_GET_CLASS(machine);
584 /* force board to initialize possible_cpus if it hasn't been done yet */
585 mc->possible_cpu_arch_ids(machine);
587 for (i = 0; i < machine->possible_cpus->len; i++) {
588 Object *cpu;
589 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
591 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
592 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
593 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
594 sizeof(*cpu_item->props));
596 cpu = machine->possible_cpus->cpus[i].cpu;
597 if (cpu) {
598 cpu_item->has_qom_path = true;
599 cpu_item->qom_path = object_get_canonical_path(cpu);
601 QAPI_LIST_PREPEND(head, cpu_item);
603 return head;
607 * machine_set_cpu_numa_node:
608 * @machine: machine object to modify
609 * @props: specifies which cpu objects to assign to
610 * numa node specified by @props.node_id
611 * @errp: if an error occurs, a pointer to an area to store the error
613 * Associate NUMA node specified by @props.node_id with cpu slots that
614 * match socket/core/thread-ids specified by @props. It's recommended to use
615 * query-hotpluggable-cpus.props values to specify affected cpu slots,
616 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
618 * However for CLI convenience it's possible to pass in subset of properties,
619 * which would affect all cpu slots that match it.
620 * Ex for pc machine:
621 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
622 * -numa cpu,node-id=0,socket_id=0 \
623 * -numa cpu,node-id=1,socket_id=1
624 * will assign all child cores of socket 0 to node 0 and
625 * of socket 1 to node 1.
627 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
628 * return error.
629 * Empty subset is disallowed and function will return with error in this case.
631 void machine_set_cpu_numa_node(MachineState *machine,
632 const CpuInstanceProperties *props, Error **errp)
634 MachineClass *mc = MACHINE_GET_CLASS(machine);
635 NodeInfo *numa_info = machine->numa_state->nodes;
636 bool match = false;
637 int i;
639 if (!mc->possible_cpu_arch_ids) {
640 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
641 return;
644 /* disabling node mapping is not supported, forbid it */
645 assert(props->has_node_id);
647 /* force board to initialize possible_cpus if it hasn't been done yet */
648 mc->possible_cpu_arch_ids(machine);
650 for (i = 0; i < machine->possible_cpus->len; i++) {
651 CPUArchId *slot = &machine->possible_cpus->cpus[i];
653 /* reject unsupported by board properties */
654 if (props->has_thread_id && !slot->props.has_thread_id) {
655 error_setg(errp, "thread-id is not supported");
656 return;
659 if (props->has_core_id && !slot->props.has_core_id) {
660 error_setg(errp, "core-id is not supported");
661 return;
664 if (props->has_socket_id && !slot->props.has_socket_id) {
665 error_setg(errp, "socket-id is not supported");
666 return;
669 if (props->has_die_id && !slot->props.has_die_id) {
670 error_setg(errp, "die-id is not supported");
671 return;
674 /* skip slots with explicit mismatch */
675 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
676 continue;
679 if (props->has_core_id && props->core_id != slot->props.core_id) {
680 continue;
683 if (props->has_die_id && props->die_id != slot->props.die_id) {
684 continue;
687 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
688 continue;
691 /* reject assignment if slot is already assigned, for compatibility
692 * of legacy cpu_index mapping with SPAPR core based mapping do not
693 * error out if cpu thread and matched core have the same node-id */
694 if (slot->props.has_node_id &&
695 slot->props.node_id != props->node_id) {
696 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
697 slot->props.node_id);
698 return;
701 /* assign slot to node as it's matched '-numa cpu' key */
702 match = true;
703 slot->props.node_id = props->node_id;
704 slot->props.has_node_id = props->has_node_id;
706 if (machine->numa_state->hmat_enabled) {
707 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
708 (props->node_id != numa_info[props->node_id].initiator)) {
709 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
710 " should be itself", props->node_id);
711 return;
713 numa_info[props->node_id].has_cpu = true;
714 numa_info[props->node_id].initiator = props->node_id;
718 if (!match) {
719 error_setg(errp, "no match found");
723 static void smp_parse(MachineState *ms, QemuOpts *opts)
725 if (opts) {
726 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
727 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
728 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
729 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
731 /* compute missing values, prefer sockets over cores over threads */
732 if (cpus == 0 || sockets == 0) {
733 cores = cores > 0 ? cores : 1;
734 threads = threads > 0 ? threads : 1;
735 if (cpus == 0) {
736 sockets = sockets > 0 ? sockets : 1;
737 cpus = cores * threads * sockets;
738 } else {
739 ms->smp.max_cpus =
740 qemu_opt_get_number(opts, "maxcpus", cpus);
741 sockets = ms->smp.max_cpus / (cores * threads);
743 } else if (cores == 0) {
744 threads = threads > 0 ? threads : 1;
745 cores = cpus / (sockets * threads);
746 cores = cores > 0 ? cores : 1;
747 } else if (threads == 0) {
748 threads = cpus / (cores * sockets);
749 threads = threads > 0 ? threads : 1;
750 } else if (sockets * cores * threads < cpus) {
751 error_report("cpu topology: "
752 "sockets (%u) * cores (%u) * threads (%u) < "
753 "smp_cpus (%u)",
754 sockets, cores, threads, cpus);
755 exit(1);
758 ms->smp.max_cpus =
759 qemu_opt_get_number(opts, "maxcpus", cpus);
761 if (ms->smp.max_cpus < cpus) {
762 error_report("maxcpus must be equal to or greater than smp");
763 exit(1);
766 if (sockets * cores * threads != ms->smp.max_cpus) {
767 error_report("Invalid CPU topology: "
768 "sockets (%u) * cores (%u) * threads (%u) "
769 "!= maxcpus (%u)",
770 sockets, cores, threads,
771 ms->smp.max_cpus);
772 exit(1);
775 ms->smp.cpus = cpus;
776 ms->smp.cores = cores;
777 ms->smp.threads = threads;
778 ms->smp.sockets = sockets;
781 if (ms->smp.cpus > 1) {
782 Error *blocker = NULL;
783 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
784 replay_add_blocker(blocker);
788 static void machine_class_init(ObjectClass *oc, void *data)
790 MachineClass *mc = MACHINE_CLASS(oc);
792 /* Default 128 MB as guest ram size */
793 mc->default_ram_size = 128 * MiB;
794 mc->rom_file_has_mr = true;
795 mc->smp_parse = smp_parse;
797 /* numa node memory size aligned on 8MB by default.
798 * On Linux, each node's border has to be 8MB aligned
800 mc->numa_mem_align_shift = 23;
802 object_class_property_add_str(oc, "kernel",
803 machine_get_kernel, machine_set_kernel);
804 object_class_property_set_description(oc, "kernel",
805 "Linux kernel image file");
807 object_class_property_add_str(oc, "initrd",
808 machine_get_initrd, machine_set_initrd);
809 object_class_property_set_description(oc, "initrd",
810 "Linux initial ramdisk file");
812 object_class_property_add_str(oc, "append",
813 machine_get_append, machine_set_append);
814 object_class_property_set_description(oc, "append",
815 "Linux kernel command line");
817 object_class_property_add_str(oc, "dtb",
818 machine_get_dtb, machine_set_dtb);
819 object_class_property_set_description(oc, "dtb",
820 "Linux kernel device tree file");
822 object_class_property_add_str(oc, "dumpdtb",
823 machine_get_dumpdtb, machine_set_dumpdtb);
824 object_class_property_set_description(oc, "dumpdtb",
825 "Dump current dtb to a file and quit");
827 object_class_property_add(oc, "phandle-start", "int",
828 machine_get_phandle_start, machine_set_phandle_start,
829 NULL, NULL);
830 object_class_property_set_description(oc, "phandle-start",
831 "The first phandle ID we may generate dynamically");
833 object_class_property_add_str(oc, "dt-compatible",
834 machine_get_dt_compatible, machine_set_dt_compatible);
835 object_class_property_set_description(oc, "dt-compatible",
836 "Overrides the \"compatible\" property of the dt root node");
838 object_class_property_add_bool(oc, "dump-guest-core",
839 machine_get_dump_guest_core, machine_set_dump_guest_core);
840 object_class_property_set_description(oc, "dump-guest-core",
841 "Include guest memory in a core dump");
843 object_class_property_add_bool(oc, "mem-merge",
844 machine_get_mem_merge, machine_set_mem_merge);
845 object_class_property_set_description(oc, "mem-merge",
846 "Enable/disable memory merge support");
848 object_class_property_add_bool(oc, "usb",
849 machine_get_usb, machine_set_usb);
850 object_class_property_set_description(oc, "usb",
851 "Set on/off to enable/disable usb");
853 object_class_property_add_bool(oc, "graphics",
854 machine_get_graphics, machine_set_graphics);
855 object_class_property_set_description(oc, "graphics",
856 "Set on/off to enable/disable graphics emulation");
858 object_class_property_add_str(oc, "firmware",
859 machine_get_firmware, machine_set_firmware);
860 object_class_property_set_description(oc, "firmware",
861 "Firmware image");
863 object_class_property_add_bool(oc, "suppress-vmdesc",
864 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
865 object_class_property_set_description(oc, "suppress-vmdesc",
866 "Set on to disable self-describing migration");
868 object_class_property_add_link(oc, "confidential-guest-support",
869 TYPE_CONFIDENTIAL_GUEST_SUPPORT,
870 offsetof(MachineState, cgs),
871 machine_check_confidential_guest_support,
872 OBJ_PROP_LINK_STRONG);
873 object_class_property_set_description(oc, "confidential-guest-support",
874 "Set confidential guest scheme to support");
876 /* For compatibility */
877 object_class_property_add_str(oc, "memory-encryption",
878 machine_get_memory_encryption, machine_set_memory_encryption);
879 object_class_property_set_description(oc, "memory-encryption",
880 "Set memory encryption object to use");
882 object_class_property_add_str(oc, "memory-backend",
883 machine_get_memdev, machine_set_memdev);
884 object_class_property_set_description(oc, "memory-backend",
885 "Set RAM backend"
886 "Valid value is ID of hostmem based backend");
889 static void machine_class_base_init(ObjectClass *oc, void *data)
891 MachineClass *mc = MACHINE_CLASS(oc);
892 mc->max_cpus = mc->max_cpus ?: 1;
893 mc->min_cpus = mc->min_cpus ?: 1;
894 mc->default_cpus = mc->default_cpus ?: 1;
896 if (!object_class_is_abstract(oc)) {
897 const char *cname = object_class_get_name(oc);
898 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
899 mc->name = g_strndup(cname,
900 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
901 mc->compat_props = g_ptr_array_new();
905 static void machine_initfn(Object *obj)
907 MachineState *ms = MACHINE(obj);
908 MachineClass *mc = MACHINE_GET_CLASS(obj);
910 container_get(obj, "/peripheral");
911 container_get(obj, "/peripheral-anon");
913 ms->dump_guest_core = true;
914 ms->mem_merge = true;
915 ms->enable_graphics = true;
916 ms->kernel_cmdline = g_strdup("");
918 if (mc->nvdimm_supported) {
919 Object *obj = OBJECT(ms);
921 ms->nvdimms_state = g_new0(NVDIMMState, 1);
922 object_property_add_bool(obj, "nvdimm",
923 machine_get_nvdimm, machine_set_nvdimm);
924 object_property_set_description(obj, "nvdimm",
925 "Set on/off to enable/disable "
926 "NVDIMM instantiation");
928 object_property_add_str(obj, "nvdimm-persistence",
929 machine_get_nvdimm_persistence,
930 machine_set_nvdimm_persistence);
931 object_property_set_description(obj, "nvdimm-persistence",
932 "Set NVDIMM persistence"
933 "Valid values are cpu, mem-ctrl");
936 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
937 ms->numa_state = g_new0(NumaState, 1);
938 object_property_add_bool(obj, "hmat",
939 machine_get_hmat, machine_set_hmat);
940 object_property_set_description(obj, "hmat",
941 "Set on/off to enable/disable "
942 "ACPI Heterogeneous Memory Attribute "
943 "Table (HMAT)");
946 /* Register notifier when init is done for sysbus sanity checks */
947 ms->sysbus_notifier.notify = machine_init_notify;
948 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
950 /* default to mc->default_cpus */
951 ms->smp.cpus = mc->default_cpus;
952 ms->smp.max_cpus = mc->default_cpus;
953 ms->smp.cores = 1;
954 ms->smp.threads = 1;
955 ms->smp.sockets = 1;
958 static void machine_finalize(Object *obj)
960 MachineState *ms = MACHINE(obj);
962 g_free(ms->kernel_filename);
963 g_free(ms->initrd_filename);
964 g_free(ms->kernel_cmdline);
965 g_free(ms->dtb);
966 g_free(ms->dumpdtb);
967 g_free(ms->dt_compatible);
968 g_free(ms->firmware);
969 g_free(ms->device_memory);
970 g_free(ms->nvdimms_state);
971 g_free(ms->numa_state);
974 bool machine_usb(MachineState *machine)
976 return machine->usb;
979 int machine_phandle_start(MachineState *machine)
981 return machine->phandle_start;
984 bool machine_dump_guest_core(MachineState *machine)
986 return machine->dump_guest_core;
989 bool machine_mem_merge(MachineState *machine)
991 return machine->mem_merge;
994 static char *cpu_slot_to_string(const CPUArchId *cpu)
996 GString *s = g_string_new(NULL);
997 if (cpu->props.has_socket_id) {
998 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1000 if (cpu->props.has_die_id) {
1001 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1003 if (cpu->props.has_core_id) {
1004 if (s->len) {
1005 g_string_append_printf(s, ", ");
1007 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1009 if (cpu->props.has_thread_id) {
1010 if (s->len) {
1011 g_string_append_printf(s, ", ");
1013 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1015 return g_string_free(s, false);
1018 static void numa_validate_initiator(NumaState *numa_state)
1020 int i;
1021 NodeInfo *numa_info = numa_state->nodes;
1023 for (i = 0; i < numa_state->num_nodes; i++) {
1024 if (numa_info[i].initiator == MAX_NODES) {
1025 error_report("The initiator of NUMA node %d is missing, use "
1026 "'-numa node,initiator' option to declare it", i);
1027 exit(1);
1030 if (!numa_info[numa_info[i].initiator].present) {
1031 error_report("NUMA node %" PRIu16 " is missing, use "
1032 "'-numa node' option to declare it first",
1033 numa_info[i].initiator);
1034 exit(1);
1037 if (!numa_info[numa_info[i].initiator].has_cpu) {
1038 error_report("The initiator of NUMA node %d is invalid", i);
1039 exit(1);
1044 static void machine_numa_finish_cpu_init(MachineState *machine)
1046 int i;
1047 bool default_mapping;
1048 GString *s = g_string_new(NULL);
1049 MachineClass *mc = MACHINE_GET_CLASS(machine);
1050 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1052 assert(machine->numa_state->num_nodes);
1053 for (i = 0; i < possible_cpus->len; i++) {
1054 if (possible_cpus->cpus[i].props.has_node_id) {
1055 break;
1058 default_mapping = (i == possible_cpus->len);
1060 for (i = 0; i < possible_cpus->len; i++) {
1061 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1063 if (!cpu_slot->props.has_node_id) {
1064 /* fetch default mapping from board and enable it */
1065 CpuInstanceProperties props = cpu_slot->props;
1067 props.node_id = mc->get_default_cpu_node_id(machine, i);
1068 if (!default_mapping) {
1069 /* record slots with not set mapping,
1070 * TODO: make it hard error in future */
1071 char *cpu_str = cpu_slot_to_string(cpu_slot);
1072 g_string_append_printf(s, "%sCPU %d [%s]",
1073 s->len ? ", " : "", i, cpu_str);
1074 g_free(cpu_str);
1076 /* non mapped cpus used to fallback to node 0 */
1077 props.node_id = 0;
1080 props.has_node_id = true;
1081 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1085 if (machine->numa_state->hmat_enabled) {
1086 numa_validate_initiator(machine->numa_state);
1089 if (s->len && !qtest_enabled()) {
1090 warn_report("CPU(s) not present in any NUMA nodes: %s",
1091 s->str);
1092 warn_report("All CPU(s) up to maxcpus should be described "
1093 "in NUMA config, ability to start up with partial NUMA "
1094 "mappings is obsoleted and will be removed in future");
1096 g_string_free(s, true);
1099 MemoryRegion *machine_consume_memdev(MachineState *machine,
1100 HostMemoryBackend *backend)
1102 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1104 if (memory_region_is_mapped(ret)) {
1105 error_report("memory backend %s can't be used multiple times.",
1106 object_get_canonical_path_component(OBJECT(backend)));
1107 exit(EXIT_FAILURE);
1109 host_memory_backend_set_mapped(backend, true);
1110 vmstate_register_ram_global(ret);
1111 return ret;
1114 bool machine_smp_parse(MachineState *ms, QemuOpts *opts, Error **errp)
1116 MachineClass *mc = MACHINE_GET_CLASS(ms);
1118 mc->smp_parse(ms, opts);
1120 /* sanity-check smp_cpus and max_cpus against mc */
1121 if (ms->smp.cpus < mc->min_cpus) {
1122 error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
1123 "supported by machine '%s' is %d",
1124 ms->smp.cpus,
1125 mc->name, mc->min_cpus);
1126 return false;
1127 } else if (ms->smp.max_cpus > mc->max_cpus) {
1128 error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
1129 "supported by machine '%s' is %d",
1130 current_machine->smp.max_cpus,
1131 mc->name, mc->max_cpus);
1132 return false;
1134 return true;
1137 void machine_run_board_init(MachineState *machine)
1139 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1140 ObjectClass *oc = object_class_by_name(machine->cpu_type);
1141 CPUClass *cc;
1143 /* This checkpoint is required by replay to separate prior clock
1144 reading from the other reads, because timer polling functions query
1145 clock values from the log. */
1146 replay_checkpoint(CHECKPOINT_INIT);
1148 if (machine->ram_memdev_id) {
1149 Object *o;
1150 o = object_resolve_path_type(machine->ram_memdev_id,
1151 TYPE_MEMORY_BACKEND, NULL);
1152 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1155 if (machine->numa_state) {
1156 numa_complete_configuration(machine);
1157 if (machine->numa_state->num_nodes) {
1158 machine_numa_finish_cpu_init(machine);
1162 /* If the machine supports the valid_cpu_types check and the user
1163 * specified a CPU with -cpu check here that the user CPU is supported.
1165 if (machine_class->valid_cpu_types && machine->cpu_type) {
1166 int i;
1168 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1169 if (object_class_dynamic_cast(oc,
1170 machine_class->valid_cpu_types[i])) {
1171 /* The user specificed CPU is in the valid field, we are
1172 * good to go.
1174 break;
1178 if (!machine_class->valid_cpu_types[i]) {
1179 /* The user specified CPU is not valid */
1180 error_report("Invalid CPU type: %s", machine->cpu_type);
1181 error_printf("The valid types are: %s",
1182 machine_class->valid_cpu_types[0]);
1183 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1184 error_printf(", %s", machine_class->valid_cpu_types[i]);
1186 error_printf("\n");
1188 exit(1);
1192 /* Check if CPU type is deprecated and warn if so */
1193 cc = CPU_CLASS(oc);
1194 if (cc && cc->deprecation_note) {
1195 warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1196 cc->deprecation_note);
1199 if (machine->cgs) {
1201 * With confidential guests, the host can't see the real
1202 * contents of RAM, so there's no point in it trying to merge
1203 * areas.
1205 machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1208 * Virtio devices can't count on directly accessing guest
1209 * memory, so they need iommu_platform=on to use normal DMA
1210 * mechanisms. That requires also disabling legacy virtio
1211 * support for those virtio pci devices which allow it.
1213 object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1214 "on", true);
1215 object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1216 "on", false);
1219 machine_class->init(machine);
1220 phase_advance(PHASE_MACHINE_INITIALIZED);
1223 static NotifierList machine_init_done_notifiers =
1224 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1226 void qemu_add_machine_init_done_notifier(Notifier *notify)
1228 notifier_list_add(&machine_init_done_notifiers, notify);
1229 if (phase_check(PHASE_MACHINE_READY)) {
1230 notify->notify(notify, NULL);
1234 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1236 notifier_remove(notify);
1239 void qdev_machine_creation_done(void)
1241 cpu_synchronize_all_post_init();
1243 if (current_machine->boot_once) {
1244 qemu_boot_set(current_machine->boot_once, &error_fatal);
1245 qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_order));
1249 * ok, initial machine setup is done, starting from now we can
1250 * only create hotpluggable devices
1252 phase_advance(PHASE_MACHINE_READY);
1253 qdev_assert_realized_properly();
1255 /* TODO: once all bus devices are qdevified, this should be done
1256 * when bus is created by qdev.c */
1258 * TODO: If we had a main 'reset container' that the whole system
1259 * lived in, we could reset that using the multi-phase reset
1260 * APIs. For the moment, we just reset the sysbus, which will cause
1261 * all devices hanging off it (and all their child buses, recursively)
1262 * to be reset. Note that this will *not* reset any Device objects
1263 * which are not attached to some part of the qbus tree!
1265 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1267 notifier_list_notify(&machine_init_done_notifiers, NULL);
1269 if (rom_check_and_register_reset() != 0) {
1270 exit(1);
1273 replay_start();
1275 /* This checkpoint is required by replay to separate prior clock
1276 reading from the other reads, because timer polling functions query
1277 clock values from the log. */
1278 replay_checkpoint(CHECKPOINT_RESET);
1279 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1280 register_global_state();
1283 static const TypeInfo machine_info = {
1284 .name = TYPE_MACHINE,
1285 .parent = TYPE_OBJECT,
1286 .abstract = true,
1287 .class_size = sizeof(MachineClass),
1288 .class_init = machine_class_init,
1289 .class_base_init = machine_class_base_init,
1290 .instance_size = sizeof(MachineState),
1291 .instance_init = machine_initfn,
1292 .instance_finalize = machine_finalize,
1295 static void machine_register_types(void)
1297 type_register_static(&machine_info);
1300 type_init(machine_register_types)