cpu: Move CPUClass::get_paging_enabled to SysemuCPUOps
[qemu/ar7.git] / hw / core / machine.c
blob1bf0e687b94a20ea7b50b886df67338b14aaf9f6
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_6_0[] = {
40 { "gpex-pcihost", "allow-unmapped-accesses", "false" },
42 const size_t hw_compat_6_0_len = G_N_ELEMENTS(hw_compat_6_0);
44 GlobalProperty hw_compat_5_2[] = {
45 { "ICH9-LPC", "smm-compat", "on"},
46 { "PIIX4_PM", "smm-compat", "on"},
47 { "virtio-blk-device", "report-discard-granularity", "off" },
48 { "virtio-net-pci", "vectors", "3"},
50 const size_t hw_compat_5_2_len = G_N_ELEMENTS(hw_compat_5_2);
52 GlobalProperty hw_compat_5_1[] = {
53 { "vhost-scsi", "num_queues", "1"},
54 { "vhost-user-blk", "num-queues", "1"},
55 { "vhost-user-scsi", "num_queues", "1"},
56 { "virtio-blk-device", "num-queues", "1"},
57 { "virtio-scsi-device", "num_queues", "1"},
58 { "nvme", "use-intel-id", "on"},
59 { "pvpanic", "events", "1"}, /* PVPANIC_PANICKED */
60 { "pl011", "migrate-clk", "off" },
61 { "virtio-pci", "x-ats-page-aligned", "off"},
63 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
65 GlobalProperty hw_compat_5_0[] = {
66 { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
67 { "virtio-balloon-device", "page-poison", "false" },
68 { "vmport", "x-read-set-eax", "off" },
69 { "vmport", "x-signal-unsupported-cmd", "off" },
70 { "vmport", "x-report-vmx-type", "off" },
71 { "vmport", "x-cmds-v2", "off" },
72 { "virtio-device", "x-disable-legacy-check", "true" },
74 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
76 GlobalProperty hw_compat_4_2[] = {
77 { "virtio-blk-device", "queue-size", "128"},
78 { "virtio-scsi-device", "virtqueue_size", "128"},
79 { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
80 { "virtio-blk-device", "seg-max-adjust", "off"},
81 { "virtio-scsi-device", "seg_max_adjust", "off"},
82 { "vhost-blk-device", "seg_max_adjust", "off"},
83 { "usb-host", "suppress-remote-wake", "off" },
84 { "usb-redir", "suppress-remote-wake", "off" },
85 { "qxl", "revision", "4" },
86 { "qxl-vga", "revision", "4" },
87 { "fw_cfg", "acpi-mr-restore", "false" },
88 { "virtio-device", "use-disabled-flag", "false" },
90 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
92 GlobalProperty hw_compat_4_1[] = {
93 { "virtio-pci", "x-pcie-flr-init", "off" },
95 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
97 GlobalProperty hw_compat_4_0[] = {
98 { "VGA", "edid", "false" },
99 { "secondary-vga", "edid", "false" },
100 { "bochs-display", "edid", "false" },
101 { "virtio-vga", "edid", "false" },
102 { "virtio-gpu-device", "edid", "false" },
103 { "virtio-device", "use-started", "false" },
104 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
105 { "pl031", "migrate-tick-offset", "false" },
107 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
109 GlobalProperty hw_compat_3_1[] = {
110 { "pcie-root-port", "x-speed", "2_5" },
111 { "pcie-root-port", "x-width", "1" },
112 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
113 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
114 { "tpm-crb", "ppi", "false" },
115 { "tpm-tis", "ppi", "false" },
116 { "usb-kbd", "serial", "42" },
117 { "usb-mouse", "serial", "42" },
118 { "usb-tablet", "serial", "42" },
119 { "virtio-blk-device", "discard", "false" },
120 { "virtio-blk-device", "write-zeroes", "false" },
121 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
122 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
124 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
126 GlobalProperty hw_compat_3_0[] = {};
127 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
129 GlobalProperty hw_compat_2_12[] = {
130 { "migration", "decompress-error-check", "off" },
131 { "hda-audio", "use-timer", "false" },
132 { "cirrus-vga", "global-vmstate", "true" },
133 { "VGA", "global-vmstate", "true" },
134 { "vmware-svga", "global-vmstate", "true" },
135 { "qxl-vga", "global-vmstate", "true" },
137 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
139 GlobalProperty hw_compat_2_11[] = {
140 { "hpet", "hpet-offset-saved", "false" },
141 { "virtio-blk-pci", "vectors", "2" },
142 { "vhost-user-blk-pci", "vectors", "2" },
143 { "e1000", "migrate_tso_props", "off" },
145 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
147 GlobalProperty hw_compat_2_10[] = {
148 { "virtio-mouse-device", "wheel-axis", "false" },
149 { "virtio-tablet-device", "wheel-axis", "false" },
151 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
153 GlobalProperty hw_compat_2_9[] = {
154 { "pci-bridge", "shpc", "off" },
155 { "intel-iommu", "pt", "off" },
156 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
157 { "pcie-root-port", "x-migrate-msix", "false" },
159 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
161 GlobalProperty hw_compat_2_8[] = {
162 { "fw_cfg_mem", "x-file-slots", "0x10" },
163 { "fw_cfg_io", "x-file-slots", "0x10" },
164 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
165 { "pci-bridge", "shpc", "on" },
166 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
167 { "virtio-pci", "x-pcie-deverr-init", "off" },
168 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
169 { "virtio-pci", "x-pcie-pm-init", "off" },
170 { "cirrus-vga", "vgamem_mb", "8" },
171 { "isa-cirrus-vga", "vgamem_mb", "8" },
173 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
175 GlobalProperty hw_compat_2_7[] = {
176 { "virtio-pci", "page-per-vq", "on" },
177 { "virtio-serial-device", "emergency-write", "off" },
178 { "ioapic", "version", "0x11" },
179 { "intel-iommu", "x-buggy-eim", "true" },
180 { "virtio-pci", "x-ignore-backend-features", "on" },
182 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
184 GlobalProperty hw_compat_2_6[] = {
185 { "virtio-mmio", "format_transport_address", "off" },
186 /* Optional because not all virtio-pci devices support legacy mode */
187 { "virtio-pci", "disable-modern", "on", .optional = true },
188 { "virtio-pci", "disable-legacy", "off", .optional = true },
190 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
192 GlobalProperty hw_compat_2_5[] = {
193 { "isa-fdc", "fallback", "144" },
194 { "pvscsi", "x-old-pci-configuration", "on" },
195 { "pvscsi", "x-disable-pcie", "on" },
196 { "vmxnet3", "x-old-msi-offsets", "on" },
197 { "vmxnet3", "x-disable-pcie", "on" },
199 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
201 GlobalProperty hw_compat_2_4[] = {
202 /* Optional because the 'scsi' property is Linux-only */
203 { "virtio-blk-device", "scsi", "true", .optional = true },
204 { "e1000", "extra_mac_registers", "off" },
205 { "virtio-pci", "x-disable-pcie", "on" },
206 { "virtio-pci", "migrate-extra", "off" },
207 { "fw_cfg_mem", "dma_enabled", "off" },
208 { "fw_cfg_io", "dma_enabled", "off" }
210 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
212 GlobalProperty hw_compat_2_3[] = {
213 { "virtio-blk-pci", "any_layout", "off" },
214 { "virtio-balloon-pci", "any_layout", "off" },
215 { "virtio-serial-pci", "any_layout", "off" },
216 { "virtio-9p-pci", "any_layout", "off" },
217 { "virtio-rng-pci", "any_layout", "off" },
218 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
219 { "migration", "send-configuration", "off" },
220 { "migration", "send-section-footer", "off" },
221 { "migration", "store-global-state", "off" },
223 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
225 GlobalProperty hw_compat_2_2[] = {};
226 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
228 GlobalProperty hw_compat_2_1[] = {
229 { "intel-hda", "old_msi_addr", "on" },
230 { "VGA", "qemu-extended-regs", "off" },
231 { "secondary-vga", "qemu-extended-regs", "off" },
232 { "virtio-scsi-pci", "any_layout", "off" },
233 { "usb-mouse", "usb_version", "1" },
234 { "usb-kbd", "usb_version", "1" },
235 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
237 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
239 MachineState *current_machine;
241 static char *machine_get_kernel(Object *obj, Error **errp)
243 MachineState *ms = MACHINE(obj);
245 return g_strdup(ms->kernel_filename);
248 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
250 MachineState *ms = MACHINE(obj);
252 g_free(ms->kernel_filename);
253 ms->kernel_filename = g_strdup(value);
256 static char *machine_get_initrd(Object *obj, Error **errp)
258 MachineState *ms = MACHINE(obj);
260 return g_strdup(ms->initrd_filename);
263 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
265 MachineState *ms = MACHINE(obj);
267 g_free(ms->initrd_filename);
268 ms->initrd_filename = g_strdup(value);
271 static char *machine_get_append(Object *obj, Error **errp)
273 MachineState *ms = MACHINE(obj);
275 return g_strdup(ms->kernel_cmdline);
278 static void machine_set_append(Object *obj, const char *value, Error **errp)
280 MachineState *ms = MACHINE(obj);
282 g_free(ms->kernel_cmdline);
283 ms->kernel_cmdline = g_strdup(value);
286 static char *machine_get_dtb(Object *obj, Error **errp)
288 MachineState *ms = MACHINE(obj);
290 return g_strdup(ms->dtb);
293 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
295 MachineState *ms = MACHINE(obj);
297 g_free(ms->dtb);
298 ms->dtb = g_strdup(value);
301 static char *machine_get_dumpdtb(Object *obj, Error **errp)
303 MachineState *ms = MACHINE(obj);
305 return g_strdup(ms->dumpdtb);
308 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
310 MachineState *ms = MACHINE(obj);
312 g_free(ms->dumpdtb);
313 ms->dumpdtb = g_strdup(value);
316 static void machine_get_phandle_start(Object *obj, Visitor *v,
317 const char *name, void *opaque,
318 Error **errp)
320 MachineState *ms = MACHINE(obj);
321 int64_t value = ms->phandle_start;
323 visit_type_int(v, name, &value, errp);
326 static void machine_set_phandle_start(Object *obj, Visitor *v,
327 const char *name, void *opaque,
328 Error **errp)
330 MachineState *ms = MACHINE(obj);
331 int64_t value;
333 if (!visit_type_int(v, name, &value, errp)) {
334 return;
337 ms->phandle_start = value;
340 static char *machine_get_dt_compatible(Object *obj, Error **errp)
342 MachineState *ms = MACHINE(obj);
344 return g_strdup(ms->dt_compatible);
347 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
349 MachineState *ms = MACHINE(obj);
351 g_free(ms->dt_compatible);
352 ms->dt_compatible = g_strdup(value);
355 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
357 MachineState *ms = MACHINE(obj);
359 return ms->dump_guest_core;
362 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
364 MachineState *ms = MACHINE(obj);
366 ms->dump_guest_core = value;
369 static bool machine_get_mem_merge(Object *obj, Error **errp)
371 MachineState *ms = MACHINE(obj);
373 return ms->mem_merge;
376 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
378 MachineState *ms = MACHINE(obj);
380 ms->mem_merge = value;
383 static bool machine_get_usb(Object *obj, Error **errp)
385 MachineState *ms = MACHINE(obj);
387 return ms->usb;
390 static void machine_set_usb(Object *obj, bool value, Error **errp)
392 MachineState *ms = MACHINE(obj);
394 ms->usb = value;
395 ms->usb_disabled = !value;
398 static bool machine_get_graphics(Object *obj, Error **errp)
400 MachineState *ms = MACHINE(obj);
402 return ms->enable_graphics;
405 static void machine_set_graphics(Object *obj, bool value, Error **errp)
407 MachineState *ms = MACHINE(obj);
409 ms->enable_graphics = value;
412 static char *machine_get_firmware(Object *obj, Error **errp)
414 MachineState *ms = MACHINE(obj);
416 return g_strdup(ms->firmware);
419 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
421 MachineState *ms = MACHINE(obj);
423 g_free(ms->firmware);
424 ms->firmware = g_strdup(value);
427 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
429 MachineState *ms = MACHINE(obj);
431 ms->suppress_vmdesc = value;
434 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
436 MachineState *ms = MACHINE(obj);
438 return ms->suppress_vmdesc;
441 static char *machine_get_memory_encryption(Object *obj, Error **errp)
443 MachineState *ms = MACHINE(obj);
445 if (ms->cgs) {
446 return g_strdup(object_get_canonical_path_component(OBJECT(ms->cgs)));
449 return NULL;
452 static void machine_set_memory_encryption(Object *obj, const char *value,
453 Error **errp)
455 Object *cgs =
456 object_resolve_path_component(object_get_objects_root(), value);
458 if (!cgs) {
459 error_setg(errp, "No such memory encryption object '%s'", value);
460 return;
463 object_property_set_link(obj, "confidential-guest-support", cgs, errp);
466 static void machine_check_confidential_guest_support(const Object *obj,
467 const char *name,
468 Object *new_target,
469 Error **errp)
472 * So far the only constraint is that the target has the
473 * TYPE_CONFIDENTIAL_GUEST_SUPPORT interface, and that's checked
474 * by the QOM core
478 static bool machine_get_nvdimm(Object *obj, Error **errp)
480 MachineState *ms = MACHINE(obj);
482 return ms->nvdimms_state->is_enabled;
485 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
487 MachineState *ms = MACHINE(obj);
489 ms->nvdimms_state->is_enabled = value;
492 static bool machine_get_hmat(Object *obj, Error **errp)
494 MachineState *ms = MACHINE(obj);
496 return ms->numa_state->hmat_enabled;
499 static void machine_set_hmat(Object *obj, bool value, Error **errp)
501 MachineState *ms = MACHINE(obj);
503 ms->numa_state->hmat_enabled = value;
506 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
508 MachineState *ms = MACHINE(obj);
510 return g_strdup(ms->nvdimms_state->persistence_string);
513 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
514 Error **errp)
516 MachineState *ms = MACHINE(obj);
517 NVDIMMState *nvdimms_state = ms->nvdimms_state;
519 if (strcmp(value, "cpu") == 0) {
520 nvdimms_state->persistence = 3;
521 } else if (strcmp(value, "mem-ctrl") == 0) {
522 nvdimms_state->persistence = 2;
523 } else {
524 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
525 value);
526 return;
529 g_free(nvdimms_state->persistence_string);
530 nvdimms_state->persistence_string = g_strdup(value);
533 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
535 QAPI_LIST_PREPEND(mc->allowed_dynamic_sysbus_devices, g_strdup(type));
538 bool device_is_dynamic_sysbus(MachineClass *mc, DeviceState *dev)
540 bool allowed = false;
541 strList *wl;
542 Object *obj = OBJECT(dev);
544 if (!object_dynamic_cast(obj, TYPE_SYS_BUS_DEVICE)) {
545 return false;
548 for (wl = mc->allowed_dynamic_sysbus_devices;
549 !allowed && wl;
550 wl = wl->next) {
551 allowed |= !!object_dynamic_cast(obj, wl->value);
554 return allowed;
557 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
559 MachineState *machine = opaque;
560 MachineClass *mc = MACHINE_GET_CLASS(machine);
562 if (!device_is_dynamic_sysbus(mc, DEVICE(sbdev))) {
563 error_report("Option '-device %s' cannot be handled by this machine",
564 object_class_get_name(object_get_class(OBJECT(sbdev))));
565 exit(1);
569 static char *machine_get_memdev(Object *obj, Error **errp)
571 MachineState *ms = MACHINE(obj);
573 return g_strdup(ms->ram_memdev_id);
576 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
578 MachineState *ms = MACHINE(obj);
580 g_free(ms->ram_memdev_id);
581 ms->ram_memdev_id = g_strdup(value);
585 static void machine_init_notify(Notifier *notifier, void *data)
587 MachineState *machine = MACHINE(qdev_get_machine());
590 * Loop through all dynamically created sysbus devices and check if they are
591 * all allowed. If a device is not allowed, error out.
593 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
596 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
598 int i;
599 HotpluggableCPUList *head = NULL;
600 MachineClass *mc = MACHINE_GET_CLASS(machine);
602 /* force board to initialize possible_cpus if it hasn't been done yet */
603 mc->possible_cpu_arch_ids(machine);
605 for (i = 0; i < machine->possible_cpus->len; i++) {
606 Object *cpu;
607 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
609 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
610 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
611 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
612 sizeof(*cpu_item->props));
614 cpu = machine->possible_cpus->cpus[i].cpu;
615 if (cpu) {
616 cpu_item->has_qom_path = true;
617 cpu_item->qom_path = object_get_canonical_path(cpu);
619 QAPI_LIST_PREPEND(head, cpu_item);
621 return head;
625 * machine_set_cpu_numa_node:
626 * @machine: machine object to modify
627 * @props: specifies which cpu objects to assign to
628 * numa node specified by @props.node_id
629 * @errp: if an error occurs, a pointer to an area to store the error
631 * Associate NUMA node specified by @props.node_id with cpu slots that
632 * match socket/core/thread-ids specified by @props. It's recommended to use
633 * query-hotpluggable-cpus.props values to specify affected cpu slots,
634 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
636 * However for CLI convenience it's possible to pass in subset of properties,
637 * which would affect all cpu slots that match it.
638 * Ex for pc machine:
639 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
640 * -numa cpu,node-id=0,socket_id=0 \
641 * -numa cpu,node-id=1,socket_id=1
642 * will assign all child cores of socket 0 to node 0 and
643 * of socket 1 to node 1.
645 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
646 * return error.
647 * Empty subset is disallowed and function will return with error in this case.
649 void machine_set_cpu_numa_node(MachineState *machine,
650 const CpuInstanceProperties *props, Error **errp)
652 MachineClass *mc = MACHINE_GET_CLASS(machine);
653 NodeInfo *numa_info = machine->numa_state->nodes;
654 bool match = false;
655 int i;
657 if (!mc->possible_cpu_arch_ids) {
658 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
659 return;
662 /* disabling node mapping is not supported, forbid it */
663 assert(props->has_node_id);
665 /* force board to initialize possible_cpus if it hasn't been done yet */
666 mc->possible_cpu_arch_ids(machine);
668 for (i = 0; i < machine->possible_cpus->len; i++) {
669 CPUArchId *slot = &machine->possible_cpus->cpus[i];
671 /* reject unsupported by board properties */
672 if (props->has_thread_id && !slot->props.has_thread_id) {
673 error_setg(errp, "thread-id is not supported");
674 return;
677 if (props->has_core_id && !slot->props.has_core_id) {
678 error_setg(errp, "core-id is not supported");
679 return;
682 if (props->has_socket_id && !slot->props.has_socket_id) {
683 error_setg(errp, "socket-id is not supported");
684 return;
687 if (props->has_die_id && !slot->props.has_die_id) {
688 error_setg(errp, "die-id is not supported");
689 return;
692 /* skip slots with explicit mismatch */
693 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
694 continue;
697 if (props->has_core_id && props->core_id != slot->props.core_id) {
698 continue;
701 if (props->has_die_id && props->die_id != slot->props.die_id) {
702 continue;
705 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
706 continue;
709 /* reject assignment if slot is already assigned, for compatibility
710 * of legacy cpu_index mapping with SPAPR core based mapping do not
711 * error out if cpu thread and matched core have the same node-id */
712 if (slot->props.has_node_id &&
713 slot->props.node_id != props->node_id) {
714 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
715 slot->props.node_id);
716 return;
719 /* assign slot to node as it's matched '-numa cpu' key */
720 match = true;
721 slot->props.node_id = props->node_id;
722 slot->props.has_node_id = props->has_node_id;
724 if (machine->numa_state->hmat_enabled) {
725 if ((numa_info[props->node_id].initiator < MAX_NODES) &&
726 (props->node_id != numa_info[props->node_id].initiator)) {
727 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
728 " should be itself", props->node_id);
729 return;
731 numa_info[props->node_id].has_cpu = true;
732 numa_info[props->node_id].initiator = props->node_id;
736 if (!match) {
737 error_setg(errp, "no match found");
741 static void smp_parse(MachineState *ms, QemuOpts *opts)
743 if (opts) {
744 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
745 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
746 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
747 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
749 /* compute missing values, prefer sockets over cores over threads */
750 if (cpus == 0 || sockets == 0) {
751 cores = cores > 0 ? cores : 1;
752 threads = threads > 0 ? threads : 1;
753 if (cpus == 0) {
754 sockets = sockets > 0 ? sockets : 1;
755 cpus = cores * threads * sockets;
756 } else {
757 ms->smp.max_cpus =
758 qemu_opt_get_number(opts, "maxcpus", cpus);
759 sockets = ms->smp.max_cpus / (cores * threads);
761 } else if (cores == 0) {
762 threads = threads > 0 ? threads : 1;
763 cores = cpus / (sockets * threads);
764 cores = cores > 0 ? cores : 1;
765 } else if (threads == 0) {
766 threads = cpus / (cores * sockets);
767 threads = threads > 0 ? threads : 1;
768 } else if (sockets * cores * threads < cpus) {
769 error_report("cpu topology: "
770 "sockets (%u) * cores (%u) * threads (%u) < "
771 "smp_cpus (%u)",
772 sockets, cores, threads, cpus);
773 exit(1);
776 ms->smp.max_cpus =
777 qemu_opt_get_number(opts, "maxcpus", cpus);
779 if (ms->smp.max_cpus < cpus) {
780 error_report("maxcpus must be equal to or greater than smp");
781 exit(1);
784 if (sockets * cores * threads != ms->smp.max_cpus) {
785 error_report("Invalid CPU topology: "
786 "sockets (%u) * cores (%u) * threads (%u) "
787 "!= maxcpus (%u)",
788 sockets, cores, threads,
789 ms->smp.max_cpus);
790 exit(1);
793 ms->smp.cpus = cpus;
794 ms->smp.cores = cores;
795 ms->smp.threads = threads;
796 ms->smp.sockets = sockets;
799 if (ms->smp.cpus > 1) {
800 Error *blocker = NULL;
801 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
802 replay_add_blocker(blocker);
806 static void machine_class_init(ObjectClass *oc, void *data)
808 MachineClass *mc = MACHINE_CLASS(oc);
810 /* Default 128 MB as guest ram size */
811 mc->default_ram_size = 128 * MiB;
812 mc->rom_file_has_mr = true;
813 mc->smp_parse = smp_parse;
815 /* numa node memory size aligned on 8MB by default.
816 * On Linux, each node's border has to be 8MB aligned
818 mc->numa_mem_align_shift = 23;
820 object_class_property_add_str(oc, "kernel",
821 machine_get_kernel, machine_set_kernel);
822 object_class_property_set_description(oc, "kernel",
823 "Linux kernel image file");
825 object_class_property_add_str(oc, "initrd",
826 machine_get_initrd, machine_set_initrd);
827 object_class_property_set_description(oc, "initrd",
828 "Linux initial ramdisk file");
830 object_class_property_add_str(oc, "append",
831 machine_get_append, machine_set_append);
832 object_class_property_set_description(oc, "append",
833 "Linux kernel command line");
835 object_class_property_add_str(oc, "dtb",
836 machine_get_dtb, machine_set_dtb);
837 object_class_property_set_description(oc, "dtb",
838 "Linux kernel device tree file");
840 object_class_property_add_str(oc, "dumpdtb",
841 machine_get_dumpdtb, machine_set_dumpdtb);
842 object_class_property_set_description(oc, "dumpdtb",
843 "Dump current dtb to a file and quit");
845 object_class_property_add(oc, "phandle-start", "int",
846 machine_get_phandle_start, machine_set_phandle_start,
847 NULL, NULL);
848 object_class_property_set_description(oc, "phandle-start",
849 "The first phandle ID we may generate dynamically");
851 object_class_property_add_str(oc, "dt-compatible",
852 machine_get_dt_compatible, machine_set_dt_compatible);
853 object_class_property_set_description(oc, "dt-compatible",
854 "Overrides the \"compatible\" property of the dt root node");
856 object_class_property_add_bool(oc, "dump-guest-core",
857 machine_get_dump_guest_core, machine_set_dump_guest_core);
858 object_class_property_set_description(oc, "dump-guest-core",
859 "Include guest memory in a core dump");
861 object_class_property_add_bool(oc, "mem-merge",
862 machine_get_mem_merge, machine_set_mem_merge);
863 object_class_property_set_description(oc, "mem-merge",
864 "Enable/disable memory merge support");
866 object_class_property_add_bool(oc, "usb",
867 machine_get_usb, machine_set_usb);
868 object_class_property_set_description(oc, "usb",
869 "Set on/off to enable/disable usb");
871 object_class_property_add_bool(oc, "graphics",
872 machine_get_graphics, machine_set_graphics);
873 object_class_property_set_description(oc, "graphics",
874 "Set on/off to enable/disable graphics emulation");
876 object_class_property_add_str(oc, "firmware",
877 machine_get_firmware, machine_set_firmware);
878 object_class_property_set_description(oc, "firmware",
879 "Firmware image");
881 object_class_property_add_bool(oc, "suppress-vmdesc",
882 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
883 object_class_property_set_description(oc, "suppress-vmdesc",
884 "Set on to disable self-describing migration");
886 object_class_property_add_link(oc, "confidential-guest-support",
887 TYPE_CONFIDENTIAL_GUEST_SUPPORT,
888 offsetof(MachineState, cgs),
889 machine_check_confidential_guest_support,
890 OBJ_PROP_LINK_STRONG);
891 object_class_property_set_description(oc, "confidential-guest-support",
892 "Set confidential guest scheme to support");
894 /* For compatibility */
895 object_class_property_add_str(oc, "memory-encryption",
896 machine_get_memory_encryption, machine_set_memory_encryption);
897 object_class_property_set_description(oc, "memory-encryption",
898 "Set memory encryption object to use");
900 object_class_property_add_str(oc, "memory-backend",
901 machine_get_memdev, machine_set_memdev);
902 object_class_property_set_description(oc, "memory-backend",
903 "Set RAM backend"
904 "Valid value is ID of hostmem based backend");
907 static void machine_class_base_init(ObjectClass *oc, void *data)
909 MachineClass *mc = MACHINE_CLASS(oc);
910 mc->max_cpus = mc->max_cpus ?: 1;
911 mc->min_cpus = mc->min_cpus ?: 1;
912 mc->default_cpus = mc->default_cpus ?: 1;
914 if (!object_class_is_abstract(oc)) {
915 const char *cname = object_class_get_name(oc);
916 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
917 mc->name = g_strndup(cname,
918 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
919 mc->compat_props = g_ptr_array_new();
923 static void machine_initfn(Object *obj)
925 MachineState *ms = MACHINE(obj);
926 MachineClass *mc = MACHINE_GET_CLASS(obj);
928 container_get(obj, "/peripheral");
929 container_get(obj, "/peripheral-anon");
931 ms->dump_guest_core = true;
932 ms->mem_merge = true;
933 ms->enable_graphics = true;
934 ms->kernel_cmdline = g_strdup("");
936 if (mc->nvdimm_supported) {
937 Object *obj = OBJECT(ms);
939 ms->nvdimms_state = g_new0(NVDIMMState, 1);
940 object_property_add_bool(obj, "nvdimm",
941 machine_get_nvdimm, machine_set_nvdimm);
942 object_property_set_description(obj, "nvdimm",
943 "Set on/off to enable/disable "
944 "NVDIMM instantiation");
946 object_property_add_str(obj, "nvdimm-persistence",
947 machine_get_nvdimm_persistence,
948 machine_set_nvdimm_persistence);
949 object_property_set_description(obj, "nvdimm-persistence",
950 "Set NVDIMM persistence"
951 "Valid values are cpu, mem-ctrl");
954 if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
955 ms->numa_state = g_new0(NumaState, 1);
956 object_property_add_bool(obj, "hmat",
957 machine_get_hmat, machine_set_hmat);
958 object_property_set_description(obj, "hmat",
959 "Set on/off to enable/disable "
960 "ACPI Heterogeneous Memory Attribute "
961 "Table (HMAT)");
964 /* Register notifier when init is done for sysbus sanity checks */
965 ms->sysbus_notifier.notify = machine_init_notify;
966 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
968 /* default to mc->default_cpus */
969 ms->smp.cpus = mc->default_cpus;
970 ms->smp.max_cpus = mc->default_cpus;
971 ms->smp.cores = 1;
972 ms->smp.threads = 1;
973 ms->smp.sockets = 1;
976 static void machine_finalize(Object *obj)
978 MachineState *ms = MACHINE(obj);
980 g_free(ms->kernel_filename);
981 g_free(ms->initrd_filename);
982 g_free(ms->kernel_cmdline);
983 g_free(ms->dtb);
984 g_free(ms->dumpdtb);
985 g_free(ms->dt_compatible);
986 g_free(ms->firmware);
987 g_free(ms->device_memory);
988 g_free(ms->nvdimms_state);
989 g_free(ms->numa_state);
992 bool machine_usb(MachineState *machine)
994 return machine->usb;
997 int machine_phandle_start(MachineState *machine)
999 return machine->phandle_start;
1002 bool machine_dump_guest_core(MachineState *machine)
1004 return machine->dump_guest_core;
1007 bool machine_mem_merge(MachineState *machine)
1009 return machine->mem_merge;
1012 static char *cpu_slot_to_string(const CPUArchId *cpu)
1014 GString *s = g_string_new(NULL);
1015 if (cpu->props.has_socket_id) {
1016 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1018 if (cpu->props.has_die_id) {
1019 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1021 if (cpu->props.has_core_id) {
1022 if (s->len) {
1023 g_string_append_printf(s, ", ");
1025 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1027 if (cpu->props.has_thread_id) {
1028 if (s->len) {
1029 g_string_append_printf(s, ", ");
1031 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1033 return g_string_free(s, false);
1036 static void numa_validate_initiator(NumaState *numa_state)
1038 int i;
1039 NodeInfo *numa_info = numa_state->nodes;
1041 for (i = 0; i < numa_state->num_nodes; i++) {
1042 if (numa_info[i].initiator == MAX_NODES) {
1043 error_report("The initiator of NUMA node %d is missing, use "
1044 "'-numa node,initiator' option to declare it", i);
1045 exit(1);
1048 if (!numa_info[numa_info[i].initiator].present) {
1049 error_report("NUMA node %" PRIu16 " is missing, use "
1050 "'-numa node' option to declare it first",
1051 numa_info[i].initiator);
1052 exit(1);
1055 if (!numa_info[numa_info[i].initiator].has_cpu) {
1056 error_report("The initiator of NUMA node %d is invalid", i);
1057 exit(1);
1062 static void machine_numa_finish_cpu_init(MachineState *machine)
1064 int i;
1065 bool default_mapping;
1066 GString *s = g_string_new(NULL);
1067 MachineClass *mc = MACHINE_GET_CLASS(machine);
1068 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1070 assert(machine->numa_state->num_nodes);
1071 for (i = 0; i < possible_cpus->len; i++) {
1072 if (possible_cpus->cpus[i].props.has_node_id) {
1073 break;
1076 default_mapping = (i == possible_cpus->len);
1078 for (i = 0; i < possible_cpus->len; i++) {
1079 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1081 if (!cpu_slot->props.has_node_id) {
1082 /* fetch default mapping from board and enable it */
1083 CpuInstanceProperties props = cpu_slot->props;
1085 props.node_id = mc->get_default_cpu_node_id(machine, i);
1086 if (!default_mapping) {
1087 /* record slots with not set mapping,
1088 * TODO: make it hard error in future */
1089 char *cpu_str = cpu_slot_to_string(cpu_slot);
1090 g_string_append_printf(s, "%sCPU %d [%s]",
1091 s->len ? ", " : "", i, cpu_str);
1092 g_free(cpu_str);
1094 /* non mapped cpus used to fallback to node 0 */
1095 props.node_id = 0;
1098 props.has_node_id = true;
1099 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1103 if (machine->numa_state->hmat_enabled) {
1104 numa_validate_initiator(machine->numa_state);
1107 if (s->len && !qtest_enabled()) {
1108 warn_report("CPU(s) not present in any NUMA nodes: %s",
1109 s->str);
1110 warn_report("All CPU(s) up to maxcpus should be described "
1111 "in NUMA config, ability to start up with partial NUMA "
1112 "mappings is obsoleted and will be removed in future");
1114 g_string_free(s, true);
1117 MemoryRegion *machine_consume_memdev(MachineState *machine,
1118 HostMemoryBackend *backend)
1120 MemoryRegion *ret = host_memory_backend_get_memory(backend);
1122 if (memory_region_is_mapped(ret)) {
1123 error_report("memory backend %s can't be used multiple times.",
1124 object_get_canonical_path_component(OBJECT(backend)));
1125 exit(EXIT_FAILURE);
1127 host_memory_backend_set_mapped(backend, true);
1128 vmstate_register_ram_global(ret);
1129 return ret;
1132 bool machine_smp_parse(MachineState *ms, QemuOpts *opts, Error **errp)
1134 MachineClass *mc = MACHINE_GET_CLASS(ms);
1136 mc->smp_parse(ms, opts);
1138 /* sanity-check smp_cpus and max_cpus against mc */
1139 if (ms->smp.cpus < mc->min_cpus) {
1140 error_setg(errp, "Invalid SMP CPUs %d. The min CPUs "
1141 "supported by machine '%s' is %d",
1142 ms->smp.cpus,
1143 mc->name, mc->min_cpus);
1144 return false;
1145 } else if (ms->smp.max_cpus > mc->max_cpus) {
1146 error_setg(errp, "Invalid SMP CPUs %d. The max CPUs "
1147 "supported by machine '%s' is %d",
1148 current_machine->smp.max_cpus,
1149 mc->name, mc->max_cpus);
1150 return false;
1152 return true;
1155 void machine_run_board_init(MachineState *machine)
1157 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1158 ObjectClass *oc = object_class_by_name(machine->cpu_type);
1159 CPUClass *cc;
1161 /* This checkpoint is required by replay to separate prior clock
1162 reading from the other reads, because timer polling functions query
1163 clock values from the log. */
1164 replay_checkpoint(CHECKPOINT_INIT);
1166 if (machine->ram_memdev_id) {
1167 Object *o;
1168 o = object_resolve_path_type(machine->ram_memdev_id,
1169 TYPE_MEMORY_BACKEND, NULL);
1170 machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1173 if (machine->numa_state) {
1174 numa_complete_configuration(machine);
1175 if (machine->numa_state->num_nodes) {
1176 machine_numa_finish_cpu_init(machine);
1180 /* If the machine supports the valid_cpu_types check and the user
1181 * specified a CPU with -cpu check here that the user CPU is supported.
1183 if (machine_class->valid_cpu_types && machine->cpu_type) {
1184 int i;
1186 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1187 if (object_class_dynamic_cast(oc,
1188 machine_class->valid_cpu_types[i])) {
1189 /* The user specificed CPU is in the valid field, we are
1190 * good to go.
1192 break;
1196 if (!machine_class->valid_cpu_types[i]) {
1197 /* The user specified CPU is not valid */
1198 error_report("Invalid CPU type: %s", machine->cpu_type);
1199 error_printf("The valid types are: %s",
1200 machine_class->valid_cpu_types[0]);
1201 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1202 error_printf(", %s", machine_class->valid_cpu_types[i]);
1204 error_printf("\n");
1206 exit(1);
1210 /* Check if CPU type is deprecated and warn if so */
1211 cc = CPU_CLASS(oc);
1212 if (cc && cc->deprecation_note) {
1213 warn_report("CPU model %s is deprecated -- %s", machine->cpu_type,
1214 cc->deprecation_note);
1217 if (machine->cgs) {
1219 * With confidential guests, the host can't see the real
1220 * contents of RAM, so there's no point in it trying to merge
1221 * areas.
1223 machine_set_mem_merge(OBJECT(machine), false, &error_abort);
1226 * Virtio devices can't count on directly accessing guest
1227 * memory, so they need iommu_platform=on to use normal DMA
1228 * mechanisms. That requires also disabling legacy virtio
1229 * support for those virtio pci devices which allow it.
1231 object_register_sugar_prop(TYPE_VIRTIO_PCI, "disable-legacy",
1232 "on", true);
1233 object_register_sugar_prop(TYPE_VIRTIO_DEVICE, "iommu_platform",
1234 "on", false);
1237 accel_init_interfaces(ACCEL_GET_CLASS(machine->accelerator));
1238 machine_class->init(machine);
1239 phase_advance(PHASE_MACHINE_INITIALIZED);
1242 static NotifierList machine_init_done_notifiers =
1243 NOTIFIER_LIST_INITIALIZER(machine_init_done_notifiers);
1245 void qemu_add_machine_init_done_notifier(Notifier *notify)
1247 notifier_list_add(&machine_init_done_notifiers, notify);
1248 if (phase_check(PHASE_MACHINE_READY)) {
1249 notify->notify(notify, NULL);
1253 void qemu_remove_machine_init_done_notifier(Notifier *notify)
1255 notifier_remove(notify);
1258 void qdev_machine_creation_done(void)
1260 cpu_synchronize_all_post_init();
1262 if (current_machine->boot_once) {
1263 qemu_boot_set(current_machine->boot_once, &error_fatal);
1264 qemu_register_reset(restore_boot_order, g_strdup(current_machine->boot_order));
1268 * ok, initial machine setup is done, starting from now we can
1269 * only create hotpluggable devices
1271 phase_advance(PHASE_MACHINE_READY);
1272 qdev_assert_realized_properly();
1274 /* TODO: once all bus devices are qdevified, this should be done
1275 * when bus is created by qdev.c */
1277 * TODO: If we had a main 'reset container' that the whole system
1278 * lived in, we could reset that using the multi-phase reset
1279 * APIs. For the moment, we just reset the sysbus, which will cause
1280 * all devices hanging off it (and all their child buses, recursively)
1281 * to be reset. Note that this will *not* reset any Device objects
1282 * which are not attached to some part of the qbus tree!
1284 qemu_register_reset(resettable_cold_reset_fn, sysbus_get_default());
1286 notifier_list_notify(&machine_init_done_notifiers, NULL);
1288 if (rom_check_and_register_reset() != 0) {
1289 exit(1);
1292 replay_start();
1294 /* This checkpoint is required by replay to separate prior clock
1295 reading from the other reads, because timer polling functions query
1296 clock values from the log. */
1297 replay_checkpoint(CHECKPOINT_RESET);
1298 qemu_system_reset(SHUTDOWN_CAUSE_NONE);
1299 register_global_state();
1302 static const TypeInfo machine_info = {
1303 .name = TYPE_MACHINE,
1304 .parent = TYPE_OBJECT,
1305 .abstract = true,
1306 .class_size = sizeof(MachineClass),
1307 .class_init = machine_class_init,
1308 .class_base_init = machine_class_base_init,
1309 .instance_size = sizeof(MachineState),
1310 .instance_init = machine_initfn,
1311 .instance_finalize = machine_finalize,
1314 static void machine_register_types(void)
1316 type_register_static(&machine_info);
1319 type_init(machine_register_types)