block: Fix AioContext switch for drained node
[qemu/ar7.git] / hw / core / machine.c
blob077fbd182adfcbbff5367d3be3ee6dcdf88990de
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[] = {
27 { "pcie-root-port", "x-speed", "2_5" },
28 { "pcie-root-port", "x-width", "1" },
29 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
30 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
31 { "tpm-crb", "ppi", "false" },
32 { "tpm-tis", "ppi", "false" },
33 { "usb-kbd", "serial", "42" },
34 { "usb-mouse", "serial", "42" },
35 { "usb-kbd", "serial", "42" },
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[] = {
43 { "migration", "decompress-error-check", "off" },
44 { "hda-audio", "use-timer", "false" },
45 { "cirrus-vga", "global-vmstate", "true" },
46 { "VGA", "global-vmstate", "true" },
47 { "vmware-svga", "global-vmstate", "true" },
48 { "qxl-vga", "global-vmstate", "true" },
50 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
52 GlobalProperty hw_compat_2_11[] = {
53 { "hpet", "hpet-offset-saved", "false" },
54 { "virtio-blk-pci", "vectors", "2" },
55 { "vhost-user-blk-pci", "vectors", "2" },
56 { "e1000", "migrate_tso_props", "off" },
58 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
60 GlobalProperty hw_compat_2_10[] = {
61 { "virtio-mouse-device", "wheel-axis", "false" },
62 { "virtio-tablet-device", "wheel-axis", "false" },
64 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
66 GlobalProperty hw_compat_2_9[] = {
67 { "pci-bridge", "shpc", "off" },
68 { "intel-iommu", "pt", "off" },
69 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
70 { "pcie-root-port", "x-migrate-msix", "false" },
72 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
74 GlobalProperty hw_compat_2_8[] = {
75 { "fw_cfg_mem", "x-file-slots", "0x10" },
76 { "fw_cfg_io", "x-file-slots", "0x10" },
77 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
78 { "pci-bridge", "shpc", "on" },
79 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
80 { "virtio-pci", "x-pcie-deverr-init", "off" },
81 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
82 { "virtio-pci", "x-pcie-pm-init", "off" },
83 { "cirrus-vga", "vgamem_mb", "8" },
84 { "isa-cirrus-vga", "vgamem_mb", "8" },
86 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
88 GlobalProperty hw_compat_2_7[] = {
89 { "virtio-pci", "page-per-vq", "on" },
90 { "virtio-serial-device", "emergency-write", "off" },
91 { "ioapic", "version", "0x11" },
92 { "intel-iommu", "x-buggy-eim", "true" },
93 { "virtio-pci", "x-ignore-backend-features", "on" },
95 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
97 GlobalProperty hw_compat_2_6[] = {
98 { "virtio-mmio", "format_transport_address", "off" },
99 /* Optional because not all virtio-pci devices support legacy mode */
100 { "virtio-pci", "disable-modern", "on", .optional = true },
101 { "virtio-pci", "disable-legacy", "off", .optional = true },
103 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
105 GlobalProperty hw_compat_2_5[] = {
106 { "isa-fdc", "fallback", "144" },
107 { "pvscsi", "x-old-pci-configuration", "on" },
108 { "pvscsi", "x-disable-pcie", "on" },
109 { "vmxnet3", "x-old-msi-offsets", "on" },
110 { "vmxnet3", "x-disable-pcie", "on" },
112 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
114 GlobalProperty hw_compat_2_4[] = {
115 { "virtio-blk-device", "scsi", "true" },
116 { "e1000", "extra_mac_registers", "off" },
117 { "virtio-pci", "x-disable-pcie", "on" },
118 { "virtio-pci", "migrate-extra", "off" },
119 { "fw_cfg_mem", "dma_enabled", "off" },
120 { "fw_cfg_io", "dma_enabled", "off" }
122 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
124 GlobalProperty hw_compat_2_3[] = {
125 { "virtio-blk-pci", "any_layout", "off" },
126 { "virtio-balloon-pci", "any_layout", "off" },
127 { "virtio-serial-pci", "any_layout", "off" },
128 { "virtio-9p-pci", "any_layout", "off" },
129 { "virtio-rng-pci", "any_layout", "off" },
130 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
131 { "migration", "send-configuration", "off" },
132 { "migration", "send-section-footer", "off" },
133 { "migration", "store-global-state", "off" },
135 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
137 GlobalProperty hw_compat_2_2[] = {};
138 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
140 GlobalProperty hw_compat_2_1[] = {
141 { "intel-hda", "old_msi_addr", "on" },
142 { "VGA", "qemu-extended-regs", "off" },
143 { "secondary-vga", "qemu-extended-regs", "off" },
144 { "virtio-scsi-pci", "any_layout", "off" },
145 { "usb-mouse", "usb_version", "1" },
146 { "usb-kbd", "usb_version", "1" },
147 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
149 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
151 static char *machine_get_accel(Object *obj, Error **errp)
153 MachineState *ms = MACHINE(obj);
155 return g_strdup(ms->accel);
158 static void machine_set_accel(Object *obj, const char *value, Error **errp)
160 MachineState *ms = MACHINE(obj);
162 g_free(ms->accel);
163 ms->accel = g_strdup(value);
166 static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
167 const char *name, void *opaque,
168 Error **errp)
170 Error *err = NULL;
171 MachineState *ms = MACHINE(obj);
172 OnOffSplit mode;
174 visit_type_OnOffSplit(v, name, &mode, &err);
175 if (err) {
176 error_propagate(errp, err);
177 return;
178 } else {
179 switch (mode) {
180 case ON_OFF_SPLIT_ON:
181 ms->kernel_irqchip_allowed = true;
182 ms->kernel_irqchip_required = true;
183 ms->kernel_irqchip_split = false;
184 break;
185 case ON_OFF_SPLIT_OFF:
186 ms->kernel_irqchip_allowed = false;
187 ms->kernel_irqchip_required = false;
188 ms->kernel_irqchip_split = false;
189 break;
190 case ON_OFF_SPLIT_SPLIT:
191 ms->kernel_irqchip_allowed = true;
192 ms->kernel_irqchip_required = true;
193 ms->kernel_irqchip_split = true;
194 break;
195 default:
196 /* The value was checked in visit_type_OnOffSplit() above. If
197 * we get here, then something is wrong in QEMU.
199 abort();
204 static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
205 const char *name, void *opaque,
206 Error **errp)
208 MachineState *ms = MACHINE(obj);
209 int64_t value = ms->kvm_shadow_mem;
211 visit_type_int(v, name, &value, errp);
214 static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
215 const char *name, void *opaque,
216 Error **errp)
218 MachineState *ms = MACHINE(obj);
219 Error *error = NULL;
220 int64_t value;
222 visit_type_int(v, name, &value, &error);
223 if (error) {
224 error_propagate(errp, error);
225 return;
228 ms->kvm_shadow_mem = value;
231 static char *machine_get_kernel(Object *obj, Error **errp)
233 MachineState *ms = MACHINE(obj);
235 return g_strdup(ms->kernel_filename);
238 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
240 MachineState *ms = MACHINE(obj);
242 g_free(ms->kernel_filename);
243 ms->kernel_filename = g_strdup(value);
246 static char *machine_get_initrd(Object *obj, Error **errp)
248 MachineState *ms = MACHINE(obj);
250 return g_strdup(ms->initrd_filename);
253 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
255 MachineState *ms = MACHINE(obj);
257 g_free(ms->initrd_filename);
258 ms->initrd_filename = g_strdup(value);
261 static char *machine_get_append(Object *obj, Error **errp)
263 MachineState *ms = MACHINE(obj);
265 return g_strdup(ms->kernel_cmdline);
268 static void machine_set_append(Object *obj, const char *value, Error **errp)
270 MachineState *ms = MACHINE(obj);
272 g_free(ms->kernel_cmdline);
273 ms->kernel_cmdline = g_strdup(value);
276 static char *machine_get_dtb(Object *obj, Error **errp)
278 MachineState *ms = MACHINE(obj);
280 return g_strdup(ms->dtb);
283 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
285 MachineState *ms = MACHINE(obj);
287 g_free(ms->dtb);
288 ms->dtb = g_strdup(value);
291 static char *machine_get_dumpdtb(Object *obj, Error **errp)
293 MachineState *ms = MACHINE(obj);
295 return g_strdup(ms->dumpdtb);
298 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
300 MachineState *ms = MACHINE(obj);
302 g_free(ms->dumpdtb);
303 ms->dumpdtb = g_strdup(value);
306 static void machine_get_phandle_start(Object *obj, Visitor *v,
307 const char *name, void *opaque,
308 Error **errp)
310 MachineState *ms = MACHINE(obj);
311 int64_t value = ms->phandle_start;
313 visit_type_int(v, name, &value, errp);
316 static void machine_set_phandle_start(Object *obj, Visitor *v,
317 const char *name, void *opaque,
318 Error **errp)
320 MachineState *ms = MACHINE(obj);
321 Error *error = NULL;
322 int64_t value;
324 visit_type_int(v, name, &value, &error);
325 if (error) {
326 error_propagate(errp, error);
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 bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
407 MachineState *ms = MACHINE(obj);
409 return ms->igd_gfx_passthru;
412 static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
414 MachineState *ms = MACHINE(obj);
416 ms->igd_gfx_passthru = value;
419 static char *machine_get_firmware(Object *obj, Error **errp)
421 MachineState *ms = MACHINE(obj);
423 return g_strdup(ms->firmware);
426 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
428 MachineState *ms = MACHINE(obj);
430 g_free(ms->firmware);
431 ms->firmware = g_strdup(value);
434 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
436 MachineState *ms = MACHINE(obj);
438 ms->suppress_vmdesc = value;
441 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
443 MachineState *ms = MACHINE(obj);
445 return ms->suppress_vmdesc;
448 static void machine_set_enforce_config_section(Object *obj, bool value,
449 Error **errp)
451 MachineState *ms = MACHINE(obj);
453 warn_report("enforce-config-section is deprecated, please use "
454 "-global migration.send-configuration=on|off instead");
456 ms->enforce_config_section = value;
459 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
461 MachineState *ms = MACHINE(obj);
463 return ms->enforce_config_section;
466 static char *machine_get_memory_encryption(Object *obj, Error **errp)
468 MachineState *ms = MACHINE(obj);
470 return g_strdup(ms->memory_encryption);
473 static void machine_set_memory_encryption(Object *obj, const char *value,
474 Error **errp)
476 MachineState *ms = MACHINE(obj);
478 g_free(ms->memory_encryption);
479 ms->memory_encryption = g_strdup(value);
482 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
484 strList *item = g_new0(strList, 1);
486 item->value = g_strdup(type);
487 item->next = mc->allowed_dynamic_sysbus_devices;
488 mc->allowed_dynamic_sysbus_devices = item;
491 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
493 MachineState *machine = opaque;
494 MachineClass *mc = MACHINE_GET_CLASS(machine);
495 bool allowed = false;
496 strList *wl;
498 for (wl = mc->allowed_dynamic_sysbus_devices;
499 !allowed && wl;
500 wl = wl->next) {
501 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
504 if (!allowed) {
505 error_report("Option '-device %s' cannot be handled by this machine",
506 object_class_get_name(object_get_class(OBJECT(sbdev))));
507 exit(1);
511 static void machine_init_notify(Notifier *notifier, void *data)
513 MachineState *machine = MACHINE(qdev_get_machine());
516 * Loop through all dynamically created sysbus devices and check if they are
517 * all allowed. If a device is not allowed, error out.
519 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
522 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
524 int i;
525 HotpluggableCPUList *head = NULL;
526 MachineClass *mc = MACHINE_GET_CLASS(machine);
528 /* force board to initialize possible_cpus if it hasn't been done yet */
529 mc->possible_cpu_arch_ids(machine);
531 for (i = 0; i < machine->possible_cpus->len; i++) {
532 Object *cpu;
533 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
534 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
536 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
537 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
538 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
539 sizeof(*cpu_item->props));
541 cpu = machine->possible_cpus->cpus[i].cpu;
542 if (cpu) {
543 cpu_item->has_qom_path = true;
544 cpu_item->qom_path = object_get_canonical_path(cpu);
546 list_item->value = cpu_item;
547 list_item->next = head;
548 head = list_item;
550 return head;
554 * machine_set_cpu_numa_node:
555 * @machine: machine object to modify
556 * @props: specifies which cpu objects to assign to
557 * numa node specified by @props.node_id
558 * @errp: if an error occurs, a pointer to an area to store the error
560 * Associate NUMA node specified by @props.node_id with cpu slots that
561 * match socket/core/thread-ids specified by @props. It's recommended to use
562 * query-hotpluggable-cpus.props values to specify affected cpu slots,
563 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
565 * However for CLI convenience it's possible to pass in subset of properties,
566 * which would affect all cpu slots that match it.
567 * Ex for pc machine:
568 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
569 * -numa cpu,node-id=0,socket_id=0 \
570 * -numa cpu,node-id=1,socket_id=1
571 * will assign all child cores of socket 0 to node 0 and
572 * of socket 1 to node 1.
574 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
575 * return error.
576 * Empty subset is disallowed and function will return with error in this case.
578 void machine_set_cpu_numa_node(MachineState *machine,
579 const CpuInstanceProperties *props, Error **errp)
581 MachineClass *mc = MACHINE_GET_CLASS(machine);
582 bool match = false;
583 int i;
585 if (!mc->possible_cpu_arch_ids) {
586 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
587 return;
590 /* disabling node mapping is not supported, forbid it */
591 assert(props->has_node_id);
593 /* force board to initialize possible_cpus if it hasn't been done yet */
594 mc->possible_cpu_arch_ids(machine);
596 for (i = 0; i < machine->possible_cpus->len; i++) {
597 CPUArchId *slot = &machine->possible_cpus->cpus[i];
599 /* reject unsupported by board properties */
600 if (props->has_thread_id && !slot->props.has_thread_id) {
601 error_setg(errp, "thread-id is not supported");
602 return;
605 if (props->has_core_id && !slot->props.has_core_id) {
606 error_setg(errp, "core-id is not supported");
607 return;
610 if (props->has_socket_id && !slot->props.has_socket_id) {
611 error_setg(errp, "socket-id is not supported");
612 return;
615 /* skip slots with explicit mismatch */
616 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
617 continue;
620 if (props->has_core_id && props->core_id != slot->props.core_id) {
621 continue;
624 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
625 continue;
628 /* reject assignment if slot is already assigned, for compatibility
629 * of legacy cpu_index mapping with SPAPR core based mapping do not
630 * error out if cpu thread and matched core have the same node-id */
631 if (slot->props.has_node_id &&
632 slot->props.node_id != props->node_id) {
633 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
634 slot->props.node_id);
635 return;
638 /* assign slot to node as it's matched '-numa cpu' key */
639 match = true;
640 slot->props.node_id = props->node_id;
641 slot->props.has_node_id = props->has_node_id;
644 if (!match) {
645 error_setg(errp, "no match found");
649 static void machine_class_init(ObjectClass *oc, void *data)
651 MachineClass *mc = MACHINE_CLASS(oc);
653 /* Default 128 MB as guest ram size */
654 mc->default_ram_size = 128 * MiB;
655 mc->rom_file_has_mr = true;
657 /* numa node memory size aligned on 8MB by default.
658 * On Linux, each node's border has to be 8MB aligned
660 mc->numa_mem_align_shift = 23;
661 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
663 object_class_property_add_str(oc, "accel",
664 machine_get_accel, machine_set_accel, &error_abort);
665 object_class_property_set_description(oc, "accel",
666 "Accelerator list", &error_abort);
668 object_class_property_add(oc, "kernel-irqchip", "on|off|split",
669 NULL, machine_set_kernel_irqchip,
670 NULL, NULL, &error_abort);
671 object_class_property_set_description(oc, "kernel-irqchip",
672 "Configure KVM in-kernel irqchip", &error_abort);
674 object_class_property_add(oc, "kvm-shadow-mem", "int",
675 machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem,
676 NULL, NULL, &error_abort);
677 object_class_property_set_description(oc, "kvm-shadow-mem",
678 "KVM shadow MMU size", &error_abort);
680 object_class_property_add_str(oc, "kernel",
681 machine_get_kernel, machine_set_kernel, &error_abort);
682 object_class_property_set_description(oc, "kernel",
683 "Linux kernel image file", &error_abort);
685 object_class_property_add_str(oc, "initrd",
686 machine_get_initrd, machine_set_initrd, &error_abort);
687 object_class_property_set_description(oc, "initrd",
688 "Linux initial ramdisk file", &error_abort);
690 object_class_property_add_str(oc, "append",
691 machine_get_append, machine_set_append, &error_abort);
692 object_class_property_set_description(oc, "append",
693 "Linux kernel command line", &error_abort);
695 object_class_property_add_str(oc, "dtb",
696 machine_get_dtb, machine_set_dtb, &error_abort);
697 object_class_property_set_description(oc, "dtb",
698 "Linux kernel device tree file", &error_abort);
700 object_class_property_add_str(oc, "dumpdtb",
701 machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
702 object_class_property_set_description(oc, "dumpdtb",
703 "Dump current dtb to a file and quit", &error_abort);
705 object_class_property_add(oc, "phandle-start", "int",
706 machine_get_phandle_start, machine_set_phandle_start,
707 NULL, NULL, &error_abort);
708 object_class_property_set_description(oc, "phandle-start",
709 "The first phandle ID we may generate dynamically", &error_abort);
711 object_class_property_add_str(oc, "dt-compatible",
712 machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
713 object_class_property_set_description(oc, "dt-compatible",
714 "Overrides the \"compatible\" property of the dt root node",
715 &error_abort);
717 object_class_property_add_bool(oc, "dump-guest-core",
718 machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
719 object_class_property_set_description(oc, "dump-guest-core",
720 "Include guest memory in a core dump", &error_abort);
722 object_class_property_add_bool(oc, "mem-merge",
723 machine_get_mem_merge, machine_set_mem_merge, &error_abort);
724 object_class_property_set_description(oc, "mem-merge",
725 "Enable/disable memory merge support", &error_abort);
727 object_class_property_add_bool(oc, "usb",
728 machine_get_usb, machine_set_usb, &error_abort);
729 object_class_property_set_description(oc, "usb",
730 "Set on/off to enable/disable usb", &error_abort);
732 object_class_property_add_bool(oc, "graphics",
733 machine_get_graphics, machine_set_graphics, &error_abort);
734 object_class_property_set_description(oc, "graphics",
735 "Set on/off to enable/disable graphics emulation", &error_abort);
737 object_class_property_add_bool(oc, "igd-passthru",
738 machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru,
739 &error_abort);
740 object_class_property_set_description(oc, "igd-passthru",
741 "Set on/off to enable/disable igd passthrou", &error_abort);
743 object_class_property_add_str(oc, "firmware",
744 machine_get_firmware, machine_set_firmware,
745 &error_abort);
746 object_class_property_set_description(oc, "firmware",
747 "Firmware image", &error_abort);
749 object_class_property_add_bool(oc, "suppress-vmdesc",
750 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
751 &error_abort);
752 object_class_property_set_description(oc, "suppress-vmdesc",
753 "Set on to disable self-describing migration", &error_abort);
755 object_class_property_add_bool(oc, "enforce-config-section",
756 machine_get_enforce_config_section, machine_set_enforce_config_section,
757 &error_abort);
758 object_class_property_set_description(oc, "enforce-config-section",
759 "Set on to enforce configuration section migration", &error_abort);
761 object_class_property_add_str(oc, "memory-encryption",
762 machine_get_memory_encryption, machine_set_memory_encryption,
763 &error_abort);
764 object_class_property_set_description(oc, "memory-encryption",
765 "Set memory encryption object to use", &error_abort);
768 static void machine_class_base_init(ObjectClass *oc, void *data)
770 if (!object_class_is_abstract(oc)) {
771 MachineClass *mc = MACHINE_CLASS(oc);
772 const char *cname = object_class_get_name(oc);
773 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
774 mc->name = g_strndup(cname,
775 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
776 mc->compat_props = g_ptr_array_new();
780 static void machine_initfn(Object *obj)
782 MachineState *ms = MACHINE(obj);
783 MachineClass *mc = MACHINE_GET_CLASS(obj);
785 ms->kernel_irqchip_allowed = true;
786 ms->kernel_irqchip_split = mc->default_kernel_irqchip_split;
787 ms->kvm_shadow_mem = -1;
788 ms->dump_guest_core = true;
789 ms->mem_merge = true;
790 ms->enable_graphics = true;
792 /* Register notifier when init is done for sysbus sanity checks */
793 ms->sysbus_notifier.notify = machine_init_notify;
794 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
797 static void machine_finalize(Object *obj)
799 MachineState *ms = MACHINE(obj);
801 g_free(ms->accel);
802 g_free(ms->kernel_filename);
803 g_free(ms->initrd_filename);
804 g_free(ms->kernel_cmdline);
805 g_free(ms->dtb);
806 g_free(ms->dumpdtb);
807 g_free(ms->dt_compatible);
808 g_free(ms->firmware);
809 g_free(ms->device_memory);
812 bool machine_usb(MachineState *machine)
814 return machine->usb;
817 bool machine_kernel_irqchip_allowed(MachineState *machine)
819 return machine->kernel_irqchip_allowed;
822 bool machine_kernel_irqchip_required(MachineState *machine)
824 return machine->kernel_irqchip_required;
827 bool machine_kernel_irqchip_split(MachineState *machine)
829 return machine->kernel_irqchip_split;
832 int machine_kvm_shadow_mem(MachineState *machine)
834 return machine->kvm_shadow_mem;
837 int machine_phandle_start(MachineState *machine)
839 return machine->phandle_start;
842 bool machine_dump_guest_core(MachineState *machine)
844 return machine->dump_guest_core;
847 bool machine_mem_merge(MachineState *machine)
849 return machine->mem_merge;
852 static char *cpu_slot_to_string(const CPUArchId *cpu)
854 GString *s = g_string_new(NULL);
855 if (cpu->props.has_socket_id) {
856 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
858 if (cpu->props.has_core_id) {
859 if (s->len) {
860 g_string_append_printf(s, ", ");
862 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
864 if (cpu->props.has_thread_id) {
865 if (s->len) {
866 g_string_append_printf(s, ", ");
868 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
870 return g_string_free(s, false);
873 static void machine_numa_finish_cpu_init(MachineState *machine)
875 int i;
876 bool default_mapping;
877 GString *s = g_string_new(NULL);
878 MachineClass *mc = MACHINE_GET_CLASS(machine);
879 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
881 assert(nb_numa_nodes);
882 for (i = 0; i < possible_cpus->len; i++) {
883 if (possible_cpus->cpus[i].props.has_node_id) {
884 break;
887 default_mapping = (i == possible_cpus->len);
889 for (i = 0; i < possible_cpus->len; i++) {
890 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
892 if (!cpu_slot->props.has_node_id) {
893 /* fetch default mapping from board and enable it */
894 CpuInstanceProperties props = cpu_slot->props;
896 props.node_id = mc->get_default_cpu_node_id(machine, i);
897 if (!default_mapping) {
898 /* record slots with not set mapping,
899 * TODO: make it hard error in future */
900 char *cpu_str = cpu_slot_to_string(cpu_slot);
901 g_string_append_printf(s, "%sCPU %d [%s]",
902 s->len ? ", " : "", i, cpu_str);
903 g_free(cpu_str);
905 /* non mapped cpus used to fallback to node 0 */
906 props.node_id = 0;
909 props.has_node_id = true;
910 machine_set_cpu_numa_node(machine, &props, &error_fatal);
913 if (s->len && !qtest_enabled()) {
914 warn_report("CPU(s) not present in any NUMA nodes: %s",
915 s->str);
916 warn_report("All CPU(s) up to maxcpus should be described "
917 "in NUMA config, ability to start up with partial NUMA "
918 "mappings is obsoleted and will be removed in future");
920 g_string_free(s, true);
923 void machine_run_board_init(MachineState *machine)
925 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
927 numa_complete_configuration(machine);
928 if (nb_numa_nodes) {
929 machine_numa_finish_cpu_init(machine);
932 /* If the machine supports the valid_cpu_types check and the user
933 * specified a CPU with -cpu check here that the user CPU is supported.
935 if (machine_class->valid_cpu_types && machine->cpu_type) {
936 ObjectClass *class = object_class_by_name(machine->cpu_type);
937 int i;
939 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
940 if (object_class_dynamic_cast(class,
941 machine_class->valid_cpu_types[i])) {
942 /* The user specificed CPU is in the valid field, we are
943 * good to go.
945 break;
949 if (!machine_class->valid_cpu_types[i]) {
950 /* The user specified CPU is not valid */
951 error_report("Invalid CPU type: %s", machine->cpu_type);
952 error_printf("The valid types are: %s",
953 machine_class->valid_cpu_types[0]);
954 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
955 error_printf(", %s", machine_class->valid_cpu_types[i]);
957 error_printf("\n");
959 exit(1);
963 machine_class->init(machine);
966 static const TypeInfo machine_info = {
967 .name = TYPE_MACHINE,
968 .parent = TYPE_OBJECT,
969 .abstract = true,
970 .class_size = sizeof(MachineClass),
971 .class_init = machine_class_init,
972 .class_base_init = machine_class_base_init,
973 .instance_size = sizeof(MachineState),
974 .instance_init = machine_initfn,
975 .instance_finalize = machine_finalize,
978 static void machine_register_types(void)
980 type_register_static(&machine_info);
983 type_init(machine_register_types)