compat: replace PC_COMPAT_2_7 & HW_COMPAT_2_7 macros
[qemu.git] / hw / core / machine.c
blobcc92b0cc84bf88ba4c8b4a85ae348746ceaa666a
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/units.h"
15 #include "hw/boards.h"
16 #include "qapi/error.h"
17 #include "qapi/qapi-visit-common.h"
18 #include "qapi/visitor.h"
19 #include "hw/sysbus.h"
20 #include "sysemu/sysemu.h"
21 #include "sysemu/numa.h"
22 #include "qemu/error-report.h"
23 #include "sysemu/qtest.h"
24 #include "hw/pci/pci.h"
26 GlobalProperty hw_compat_3_1[] = {
28 .driver = "pcie-root-port",
29 .property = "x-speed",
30 .value = "2_5",
31 },{
32 .driver = "pcie-root-port",
33 .property = "x-width",
34 .value = "1",
37 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
39 GlobalProperty hw_compat_3_0[] = {};
40 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
42 GlobalProperty hw_compat_2_12[] = {
44 .driver = "migration",
45 .property = "decompress-error-check",
46 .value = "off",
47 },{
48 .driver = "hda-audio",
49 .property = "use-timer",
50 .value = "false",
51 },{
52 .driver = "cirrus-vga",
53 .property = "global-vmstate",
54 .value = "true",
55 },{
56 .driver = "VGA",
57 .property = "global-vmstate",
58 .value = "true",
59 },{
60 .driver = "vmware-svga",
61 .property = "global-vmstate",
62 .value = "true",
63 },{
64 .driver = "qxl-vga",
65 .property = "global-vmstate",
66 .value = "true",
69 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
71 GlobalProperty hw_compat_2_11[] = {
73 .driver = "hpet",
74 .property = "hpet-offset-saved",
75 .value = "false",
76 },{
77 .driver = "virtio-blk-pci",
78 .property = "vectors",
79 .value = "2",
80 },{
81 .driver = "vhost-user-blk-pci",
82 .property = "vectors",
83 .value = "2",
84 },{
85 .driver = "e1000",
86 .property = "migrate_tso_props",
87 .value = "off",
90 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
92 GlobalProperty hw_compat_2_10[] = {
94 .driver = "virtio-mouse-device",
95 .property = "wheel-axis",
96 .value = "false",
97 },{
98 .driver = "virtio-tablet-device",
99 .property = "wheel-axis",
100 .value = "false",
103 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
105 GlobalProperty hw_compat_2_9[] = {
107 .driver = "pci-bridge",
108 .property = "shpc",
109 .value = "off",
111 .driver = "intel-iommu",
112 .property = "pt",
113 .value = "off",
115 .driver = "virtio-net-device",
116 .property = "x-mtu-bypass-backend",
117 .value = "off",
119 .driver = "pcie-root-port",
120 .property = "x-migrate-msix",
121 .value = "false",
124 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
126 GlobalProperty hw_compat_2_8[] = {
128 .driver = "fw_cfg_mem",
129 .property = "x-file-slots",
130 .value = stringify(0x10),
132 .driver = "fw_cfg_io",
133 .property = "x-file-slots",
134 .value = stringify(0x10),
136 .driver = "pflash_cfi01",
137 .property = "old-multiple-chip-handling",
138 .value = "on",
140 .driver = "pci-bridge",
141 .property = "shpc",
142 .value = "on",
144 .driver = TYPE_PCI_DEVICE,
145 .property = "x-pcie-extcap-init",
146 .value = "off",
148 .driver = "virtio-pci",
149 .property = "x-pcie-deverr-init",
150 .value = "off",
152 .driver = "virtio-pci",
153 .property = "x-pcie-lnkctl-init",
154 .value = "off",
156 .driver = "virtio-pci",
157 .property = "x-pcie-pm-init",
158 .value = "off",
160 .driver = "cirrus-vga",
161 .property = "vgamem_mb",
162 .value = "8",
164 .driver = "isa-cirrus-vga",
165 .property = "vgamem_mb",
166 .value = "8",
169 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
171 GlobalProperty hw_compat_2_7[] = {
173 .driver = "virtio-pci",
174 .property = "page-per-vq",
175 .value = "on",
177 .driver = "virtio-serial-device",
178 .property = "emergency-write",
179 .value = "off",
181 .driver = "ioapic",
182 .property = "version",
183 .value = "0x11",
185 .driver = "intel-iommu",
186 .property = "x-buggy-eim",
187 .value = "true",
189 .driver = "virtio-pci",
190 .property = "x-ignore-backend-features",
191 .value = "on",
194 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
196 static char *machine_get_accel(Object *obj, Error **errp)
198 MachineState *ms = MACHINE(obj);
200 return g_strdup(ms->accel);
203 static void machine_set_accel(Object *obj, const char *value, Error **errp)
205 MachineState *ms = MACHINE(obj);
207 g_free(ms->accel);
208 ms->accel = g_strdup(value);
211 static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
212 const char *name, void *opaque,
213 Error **errp)
215 Error *err = NULL;
216 MachineState *ms = MACHINE(obj);
217 OnOffSplit mode;
219 visit_type_OnOffSplit(v, name, &mode, &err);
220 if (err) {
221 error_propagate(errp, err);
222 return;
223 } else {
224 switch (mode) {
225 case ON_OFF_SPLIT_ON:
226 ms->kernel_irqchip_allowed = true;
227 ms->kernel_irqchip_required = true;
228 ms->kernel_irqchip_split = false;
229 break;
230 case ON_OFF_SPLIT_OFF:
231 ms->kernel_irqchip_allowed = false;
232 ms->kernel_irqchip_required = false;
233 ms->kernel_irqchip_split = false;
234 break;
235 case ON_OFF_SPLIT_SPLIT:
236 ms->kernel_irqchip_allowed = true;
237 ms->kernel_irqchip_required = true;
238 ms->kernel_irqchip_split = true;
239 break;
240 default:
241 /* The value was checked in visit_type_OnOffSplit() above. If
242 * we get here, then something is wrong in QEMU.
244 abort();
249 static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
250 const char *name, void *opaque,
251 Error **errp)
253 MachineState *ms = MACHINE(obj);
254 int64_t value = ms->kvm_shadow_mem;
256 visit_type_int(v, name, &value, errp);
259 static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
260 const char *name, void *opaque,
261 Error **errp)
263 MachineState *ms = MACHINE(obj);
264 Error *error = NULL;
265 int64_t value;
267 visit_type_int(v, name, &value, &error);
268 if (error) {
269 error_propagate(errp, error);
270 return;
273 ms->kvm_shadow_mem = value;
276 static char *machine_get_kernel(Object *obj, Error **errp)
278 MachineState *ms = MACHINE(obj);
280 return g_strdup(ms->kernel_filename);
283 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
285 MachineState *ms = MACHINE(obj);
287 g_free(ms->kernel_filename);
288 ms->kernel_filename = g_strdup(value);
291 static char *machine_get_initrd(Object *obj, Error **errp)
293 MachineState *ms = MACHINE(obj);
295 return g_strdup(ms->initrd_filename);
298 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
300 MachineState *ms = MACHINE(obj);
302 g_free(ms->initrd_filename);
303 ms->initrd_filename = g_strdup(value);
306 static char *machine_get_append(Object *obj, Error **errp)
308 MachineState *ms = MACHINE(obj);
310 return g_strdup(ms->kernel_cmdline);
313 static void machine_set_append(Object *obj, const char *value, Error **errp)
315 MachineState *ms = MACHINE(obj);
317 g_free(ms->kernel_cmdline);
318 ms->kernel_cmdline = g_strdup(value);
321 static char *machine_get_dtb(Object *obj, Error **errp)
323 MachineState *ms = MACHINE(obj);
325 return g_strdup(ms->dtb);
328 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
330 MachineState *ms = MACHINE(obj);
332 g_free(ms->dtb);
333 ms->dtb = g_strdup(value);
336 static char *machine_get_dumpdtb(Object *obj, Error **errp)
338 MachineState *ms = MACHINE(obj);
340 return g_strdup(ms->dumpdtb);
343 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
345 MachineState *ms = MACHINE(obj);
347 g_free(ms->dumpdtb);
348 ms->dumpdtb = g_strdup(value);
351 static void machine_get_phandle_start(Object *obj, Visitor *v,
352 const char *name, void *opaque,
353 Error **errp)
355 MachineState *ms = MACHINE(obj);
356 int64_t value = ms->phandle_start;
358 visit_type_int(v, name, &value, errp);
361 static void machine_set_phandle_start(Object *obj, Visitor *v,
362 const char *name, void *opaque,
363 Error **errp)
365 MachineState *ms = MACHINE(obj);
366 Error *error = NULL;
367 int64_t value;
369 visit_type_int(v, name, &value, &error);
370 if (error) {
371 error_propagate(errp, error);
372 return;
375 ms->phandle_start = value;
378 static char *machine_get_dt_compatible(Object *obj, Error **errp)
380 MachineState *ms = MACHINE(obj);
382 return g_strdup(ms->dt_compatible);
385 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
387 MachineState *ms = MACHINE(obj);
389 g_free(ms->dt_compatible);
390 ms->dt_compatible = g_strdup(value);
393 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
395 MachineState *ms = MACHINE(obj);
397 return ms->dump_guest_core;
400 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
402 MachineState *ms = MACHINE(obj);
404 ms->dump_guest_core = value;
407 static bool machine_get_mem_merge(Object *obj, Error **errp)
409 MachineState *ms = MACHINE(obj);
411 return ms->mem_merge;
414 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
416 MachineState *ms = MACHINE(obj);
418 ms->mem_merge = value;
421 static bool machine_get_usb(Object *obj, Error **errp)
423 MachineState *ms = MACHINE(obj);
425 return ms->usb;
428 static void machine_set_usb(Object *obj, bool value, Error **errp)
430 MachineState *ms = MACHINE(obj);
432 ms->usb = value;
433 ms->usb_disabled = !value;
436 static bool machine_get_graphics(Object *obj, Error **errp)
438 MachineState *ms = MACHINE(obj);
440 return ms->enable_graphics;
443 static void machine_set_graphics(Object *obj, bool value, Error **errp)
445 MachineState *ms = MACHINE(obj);
447 ms->enable_graphics = value;
450 static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
452 MachineState *ms = MACHINE(obj);
454 return ms->igd_gfx_passthru;
457 static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
459 MachineState *ms = MACHINE(obj);
461 ms->igd_gfx_passthru = value;
464 static char *machine_get_firmware(Object *obj, Error **errp)
466 MachineState *ms = MACHINE(obj);
468 return g_strdup(ms->firmware);
471 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
473 MachineState *ms = MACHINE(obj);
475 g_free(ms->firmware);
476 ms->firmware = g_strdup(value);
479 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
481 MachineState *ms = MACHINE(obj);
483 ms->suppress_vmdesc = value;
486 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
488 MachineState *ms = MACHINE(obj);
490 return ms->suppress_vmdesc;
493 static void machine_set_enforce_config_section(Object *obj, bool value,
494 Error **errp)
496 MachineState *ms = MACHINE(obj);
498 warn_report("enforce-config-section is deprecated, please use "
499 "-global migration.send-configuration=on|off instead");
501 ms->enforce_config_section = value;
504 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
506 MachineState *ms = MACHINE(obj);
508 return ms->enforce_config_section;
511 static char *machine_get_memory_encryption(Object *obj, Error **errp)
513 MachineState *ms = MACHINE(obj);
515 return g_strdup(ms->memory_encryption);
518 static void machine_set_memory_encryption(Object *obj, const char *value,
519 Error **errp)
521 MachineState *ms = MACHINE(obj);
523 g_free(ms->memory_encryption);
524 ms->memory_encryption = g_strdup(value);
527 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
529 strList *item = g_new0(strList, 1);
531 item->value = g_strdup(type);
532 item->next = mc->allowed_dynamic_sysbus_devices;
533 mc->allowed_dynamic_sysbus_devices = item;
536 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
538 MachineState *machine = opaque;
539 MachineClass *mc = MACHINE_GET_CLASS(machine);
540 bool allowed = false;
541 strList *wl;
543 for (wl = mc->allowed_dynamic_sysbus_devices;
544 !allowed && wl;
545 wl = wl->next) {
546 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
549 if (!allowed) {
550 error_report("Option '-device %s' cannot be handled by this machine",
551 object_class_get_name(object_get_class(OBJECT(sbdev))));
552 exit(1);
556 static void machine_init_notify(Notifier *notifier, void *data)
558 MachineState *machine = MACHINE(qdev_get_machine());
561 * Loop through all dynamically created sysbus devices and check if they are
562 * all allowed. If a device is not allowed, error out.
564 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
567 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
569 int i;
570 HotpluggableCPUList *head = NULL;
571 MachineClass *mc = MACHINE_GET_CLASS(machine);
573 /* force board to initialize possible_cpus if it hasn't been done yet */
574 mc->possible_cpu_arch_ids(machine);
576 for (i = 0; i < machine->possible_cpus->len; i++) {
577 Object *cpu;
578 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
579 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
581 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
582 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
583 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
584 sizeof(*cpu_item->props));
586 cpu = machine->possible_cpus->cpus[i].cpu;
587 if (cpu) {
588 cpu_item->has_qom_path = true;
589 cpu_item->qom_path = object_get_canonical_path(cpu);
591 list_item->value = cpu_item;
592 list_item->next = head;
593 head = list_item;
595 return head;
599 * machine_set_cpu_numa_node:
600 * @machine: machine object to modify
601 * @props: specifies which cpu objects to assign to
602 * numa node specified by @props.node_id
603 * @errp: if an error occurs, a pointer to an area to store the error
605 * Associate NUMA node specified by @props.node_id with cpu slots that
606 * match socket/core/thread-ids specified by @props. It's recommended to use
607 * query-hotpluggable-cpus.props values to specify affected cpu slots,
608 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
610 * However for CLI convenience it's possible to pass in subset of properties,
611 * which would affect all cpu slots that match it.
612 * Ex for pc machine:
613 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
614 * -numa cpu,node-id=0,socket_id=0 \
615 * -numa cpu,node-id=1,socket_id=1
616 * will assign all child cores of socket 0 to node 0 and
617 * of socket 1 to node 1.
619 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
620 * return error.
621 * Empty subset is disallowed and function will return with error in this case.
623 void machine_set_cpu_numa_node(MachineState *machine,
624 const CpuInstanceProperties *props, Error **errp)
626 MachineClass *mc = MACHINE_GET_CLASS(machine);
627 bool match = false;
628 int i;
630 if (!mc->possible_cpu_arch_ids) {
631 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
632 return;
635 /* disabling node mapping is not supported, forbid it */
636 assert(props->has_node_id);
638 /* force board to initialize possible_cpus if it hasn't been done yet */
639 mc->possible_cpu_arch_ids(machine);
641 for (i = 0; i < machine->possible_cpus->len; i++) {
642 CPUArchId *slot = &machine->possible_cpus->cpus[i];
644 /* reject unsupported by board properties */
645 if (props->has_thread_id && !slot->props.has_thread_id) {
646 error_setg(errp, "thread-id is not supported");
647 return;
650 if (props->has_core_id && !slot->props.has_core_id) {
651 error_setg(errp, "core-id is not supported");
652 return;
655 if (props->has_socket_id && !slot->props.has_socket_id) {
656 error_setg(errp, "socket-id is not supported");
657 return;
660 /* skip slots with explicit mismatch */
661 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
662 continue;
665 if (props->has_core_id && props->core_id != slot->props.core_id) {
666 continue;
669 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
670 continue;
673 /* reject assignment if slot is already assigned, for compatibility
674 * of legacy cpu_index mapping with SPAPR core based mapping do not
675 * error out if cpu thread and matched core have the same node-id */
676 if (slot->props.has_node_id &&
677 slot->props.node_id != props->node_id) {
678 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
679 slot->props.node_id);
680 return;
683 /* assign slot to node as it's matched '-numa cpu' key */
684 match = true;
685 slot->props.node_id = props->node_id;
686 slot->props.has_node_id = props->has_node_id;
689 if (!match) {
690 error_setg(errp, "no match found");
694 static void machine_class_init(ObjectClass *oc, void *data)
696 MachineClass *mc = MACHINE_CLASS(oc);
698 /* Default 128 MB as guest ram size */
699 mc->default_ram_size = 128 * MiB;
700 mc->rom_file_has_mr = true;
702 /* numa node memory size aligned on 8MB by default.
703 * On Linux, each node's border has to be 8MB aligned
705 mc->numa_mem_align_shift = 23;
706 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
708 object_class_property_add_str(oc, "accel",
709 machine_get_accel, machine_set_accel, &error_abort);
710 object_class_property_set_description(oc, "accel",
711 "Accelerator list", &error_abort);
713 object_class_property_add(oc, "kernel-irqchip", "on|off|split",
714 NULL, machine_set_kernel_irqchip,
715 NULL, NULL, &error_abort);
716 object_class_property_set_description(oc, "kernel-irqchip",
717 "Configure KVM in-kernel irqchip", &error_abort);
719 object_class_property_add(oc, "kvm-shadow-mem", "int",
720 machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem,
721 NULL, NULL, &error_abort);
722 object_class_property_set_description(oc, "kvm-shadow-mem",
723 "KVM shadow MMU size", &error_abort);
725 object_class_property_add_str(oc, "kernel",
726 machine_get_kernel, machine_set_kernel, &error_abort);
727 object_class_property_set_description(oc, "kernel",
728 "Linux kernel image file", &error_abort);
730 object_class_property_add_str(oc, "initrd",
731 machine_get_initrd, machine_set_initrd, &error_abort);
732 object_class_property_set_description(oc, "initrd",
733 "Linux initial ramdisk file", &error_abort);
735 object_class_property_add_str(oc, "append",
736 machine_get_append, machine_set_append, &error_abort);
737 object_class_property_set_description(oc, "append",
738 "Linux kernel command line", &error_abort);
740 object_class_property_add_str(oc, "dtb",
741 machine_get_dtb, machine_set_dtb, &error_abort);
742 object_class_property_set_description(oc, "dtb",
743 "Linux kernel device tree file", &error_abort);
745 object_class_property_add_str(oc, "dumpdtb",
746 machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
747 object_class_property_set_description(oc, "dumpdtb",
748 "Dump current dtb to a file and quit", &error_abort);
750 object_class_property_add(oc, "phandle-start", "int",
751 machine_get_phandle_start, machine_set_phandle_start,
752 NULL, NULL, &error_abort);
753 object_class_property_set_description(oc, "phandle-start",
754 "The first phandle ID we may generate dynamically", &error_abort);
756 object_class_property_add_str(oc, "dt-compatible",
757 machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
758 object_class_property_set_description(oc, "dt-compatible",
759 "Overrides the \"compatible\" property of the dt root node",
760 &error_abort);
762 object_class_property_add_bool(oc, "dump-guest-core",
763 machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
764 object_class_property_set_description(oc, "dump-guest-core",
765 "Include guest memory in a core dump", &error_abort);
767 object_class_property_add_bool(oc, "mem-merge",
768 machine_get_mem_merge, machine_set_mem_merge, &error_abort);
769 object_class_property_set_description(oc, "mem-merge",
770 "Enable/disable memory merge support", &error_abort);
772 object_class_property_add_bool(oc, "usb",
773 machine_get_usb, machine_set_usb, &error_abort);
774 object_class_property_set_description(oc, "usb",
775 "Set on/off to enable/disable usb", &error_abort);
777 object_class_property_add_bool(oc, "graphics",
778 machine_get_graphics, machine_set_graphics, &error_abort);
779 object_class_property_set_description(oc, "graphics",
780 "Set on/off to enable/disable graphics emulation", &error_abort);
782 object_class_property_add_bool(oc, "igd-passthru",
783 machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru,
784 &error_abort);
785 object_class_property_set_description(oc, "igd-passthru",
786 "Set on/off to enable/disable igd passthrou", &error_abort);
788 object_class_property_add_str(oc, "firmware",
789 machine_get_firmware, machine_set_firmware,
790 &error_abort);
791 object_class_property_set_description(oc, "firmware",
792 "Firmware image", &error_abort);
794 object_class_property_add_bool(oc, "suppress-vmdesc",
795 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
796 &error_abort);
797 object_class_property_set_description(oc, "suppress-vmdesc",
798 "Set on to disable self-describing migration", &error_abort);
800 object_class_property_add_bool(oc, "enforce-config-section",
801 machine_get_enforce_config_section, machine_set_enforce_config_section,
802 &error_abort);
803 object_class_property_set_description(oc, "enforce-config-section",
804 "Set on to enforce configuration section migration", &error_abort);
806 object_class_property_add_str(oc, "memory-encryption",
807 machine_get_memory_encryption, machine_set_memory_encryption,
808 &error_abort);
809 object_class_property_set_description(oc, "memory-encryption",
810 "Set memory encryption object to use", &error_abort);
813 static void machine_class_base_init(ObjectClass *oc, void *data)
815 if (!object_class_is_abstract(oc)) {
816 MachineClass *mc = MACHINE_CLASS(oc);
817 const char *cname = object_class_get_name(oc);
818 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
819 mc->name = g_strndup(cname,
820 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
821 mc->compat_props = g_ptr_array_new();
825 static void machine_initfn(Object *obj)
827 MachineState *ms = MACHINE(obj);
828 MachineClass *mc = MACHINE_GET_CLASS(obj);
830 ms->kernel_irqchip_allowed = true;
831 ms->kernel_irqchip_split = mc->default_kernel_irqchip_split;
832 ms->kvm_shadow_mem = -1;
833 ms->dump_guest_core = true;
834 ms->mem_merge = true;
835 ms->enable_graphics = true;
837 /* Register notifier when init is done for sysbus sanity checks */
838 ms->sysbus_notifier.notify = machine_init_notify;
839 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
842 static void machine_finalize(Object *obj)
844 MachineState *ms = MACHINE(obj);
846 g_free(ms->accel);
847 g_free(ms->kernel_filename);
848 g_free(ms->initrd_filename);
849 g_free(ms->kernel_cmdline);
850 g_free(ms->dtb);
851 g_free(ms->dumpdtb);
852 g_free(ms->dt_compatible);
853 g_free(ms->firmware);
854 g_free(ms->device_memory);
857 bool machine_usb(MachineState *machine)
859 return machine->usb;
862 bool machine_kernel_irqchip_allowed(MachineState *machine)
864 return machine->kernel_irqchip_allowed;
867 bool machine_kernel_irqchip_required(MachineState *machine)
869 return machine->kernel_irqchip_required;
872 bool machine_kernel_irqchip_split(MachineState *machine)
874 return machine->kernel_irqchip_split;
877 int machine_kvm_shadow_mem(MachineState *machine)
879 return machine->kvm_shadow_mem;
882 int machine_phandle_start(MachineState *machine)
884 return machine->phandle_start;
887 bool machine_dump_guest_core(MachineState *machine)
889 return machine->dump_guest_core;
892 bool machine_mem_merge(MachineState *machine)
894 return machine->mem_merge;
897 static char *cpu_slot_to_string(const CPUArchId *cpu)
899 GString *s = g_string_new(NULL);
900 if (cpu->props.has_socket_id) {
901 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
903 if (cpu->props.has_core_id) {
904 if (s->len) {
905 g_string_append_printf(s, ", ");
907 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
909 if (cpu->props.has_thread_id) {
910 if (s->len) {
911 g_string_append_printf(s, ", ");
913 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
915 return g_string_free(s, false);
918 static void machine_numa_finish_cpu_init(MachineState *machine)
920 int i;
921 bool default_mapping;
922 GString *s = g_string_new(NULL);
923 MachineClass *mc = MACHINE_GET_CLASS(machine);
924 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
926 assert(nb_numa_nodes);
927 for (i = 0; i < possible_cpus->len; i++) {
928 if (possible_cpus->cpus[i].props.has_node_id) {
929 break;
932 default_mapping = (i == possible_cpus->len);
934 for (i = 0; i < possible_cpus->len; i++) {
935 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
937 if (!cpu_slot->props.has_node_id) {
938 /* fetch default mapping from board and enable it */
939 CpuInstanceProperties props = cpu_slot->props;
941 props.node_id = mc->get_default_cpu_node_id(machine, i);
942 if (!default_mapping) {
943 /* record slots with not set mapping,
944 * TODO: make it hard error in future */
945 char *cpu_str = cpu_slot_to_string(cpu_slot);
946 g_string_append_printf(s, "%sCPU %d [%s]",
947 s->len ? ", " : "", i, cpu_str);
948 g_free(cpu_str);
950 /* non mapped cpus used to fallback to node 0 */
951 props.node_id = 0;
954 props.has_node_id = true;
955 machine_set_cpu_numa_node(machine, &props, &error_fatal);
958 if (s->len && !qtest_enabled()) {
959 warn_report("CPU(s) not present in any NUMA nodes: %s",
960 s->str);
961 warn_report("All CPU(s) up to maxcpus should be described "
962 "in NUMA config, ability to start up with partial NUMA "
963 "mappings is obsoleted and will be removed in future");
965 g_string_free(s, true);
968 void machine_run_board_init(MachineState *machine)
970 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
972 numa_complete_configuration(machine);
973 if (nb_numa_nodes) {
974 machine_numa_finish_cpu_init(machine);
977 /* If the machine supports the valid_cpu_types check and the user
978 * specified a CPU with -cpu check here that the user CPU is supported.
980 if (machine_class->valid_cpu_types && machine->cpu_type) {
981 ObjectClass *class = object_class_by_name(machine->cpu_type);
982 int i;
984 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
985 if (object_class_dynamic_cast(class,
986 machine_class->valid_cpu_types[i])) {
987 /* The user specificed CPU is in the valid field, we are
988 * good to go.
990 break;
994 if (!machine_class->valid_cpu_types[i]) {
995 /* The user specified CPU is not valid */
996 error_report("Invalid CPU type: %s", machine->cpu_type);
997 error_printf("The valid types are: %s",
998 machine_class->valid_cpu_types[0]);
999 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1000 error_printf(", %s", machine_class->valid_cpu_types[i]);
1002 error_printf("\n");
1004 exit(1);
1008 machine_class->init(machine);
1011 static const TypeInfo machine_info = {
1012 .name = TYPE_MACHINE,
1013 .parent = TYPE_OBJECT,
1014 .abstract = true,
1015 .class_size = sizeof(MachineClass),
1016 .class_init = machine_class_init,
1017 .class_base_init = machine_class_base_init,
1018 .instance_size = sizeof(MachineState),
1019 .instance_init = machine_initfn,
1020 .instance_finalize = machine_finalize,
1023 static void machine_register_types(void)
1025 type_register_static(&machine_info);
1028 type_init(machine_register_types)