docker.py: add --run-as-current-user
[qemu/ar7.git] / hw / core / machine.c
blob83cd1bfeeceb7df4e2084d5c52cbd25e4b457b25
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 "qapi/error.h"
20 #include "qapi/qapi-visit-common.h"
21 #include "qapi/visitor.h"
22 #include "hw/sysbus.h"
23 #include "sysemu/sysemu.h"
24 #include "sysemu/numa.h"
25 #include "qemu/error-report.h"
26 #include "sysemu/qtest.h"
27 #include "hw/pci/pci.h"
28 #include "hw/mem/nvdimm.h"
30 GlobalProperty hw_compat_4_1[] = {};
31 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
33 GlobalProperty hw_compat_4_0[] = {
34 { "VGA", "edid", "false" },
35 { "secondary-vga", "edid", "false" },
36 { "bochs-display", "edid", "false" },
37 { "virtio-vga", "edid", "false" },
38 { "virtio-gpu", "edid", "false" },
39 { "virtio-device", "use-started", "false" },
40 { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
41 { "pl031", "migrate-tick-offset", "false" },
43 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
45 GlobalProperty hw_compat_3_1[] = {
46 { "pcie-root-port", "x-speed", "2_5" },
47 { "pcie-root-port", "x-width", "1" },
48 { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
49 { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
50 { "tpm-crb", "ppi", "false" },
51 { "tpm-tis", "ppi", "false" },
52 { "usb-kbd", "serial", "42" },
53 { "usb-mouse", "serial", "42" },
54 { "usb-tablet", "serial", "42" },
55 { "virtio-blk-device", "discard", "false" },
56 { "virtio-blk-device", "write-zeroes", "false" },
57 { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
58 { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
60 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
62 GlobalProperty hw_compat_3_0[] = {};
63 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
65 GlobalProperty hw_compat_2_12[] = {
66 { "migration", "decompress-error-check", "off" },
67 { "hda-audio", "use-timer", "false" },
68 { "cirrus-vga", "global-vmstate", "true" },
69 { "VGA", "global-vmstate", "true" },
70 { "vmware-svga", "global-vmstate", "true" },
71 { "qxl-vga", "global-vmstate", "true" },
73 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
75 GlobalProperty hw_compat_2_11[] = {
76 { "hpet", "hpet-offset-saved", "false" },
77 { "virtio-blk-pci", "vectors", "2" },
78 { "vhost-user-blk-pci", "vectors", "2" },
79 { "e1000", "migrate_tso_props", "off" },
81 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
83 GlobalProperty hw_compat_2_10[] = {
84 { "virtio-mouse-device", "wheel-axis", "false" },
85 { "virtio-tablet-device", "wheel-axis", "false" },
87 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
89 GlobalProperty hw_compat_2_9[] = {
90 { "pci-bridge", "shpc", "off" },
91 { "intel-iommu", "pt", "off" },
92 { "virtio-net-device", "x-mtu-bypass-backend", "off" },
93 { "pcie-root-port", "x-migrate-msix", "false" },
95 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
97 GlobalProperty hw_compat_2_8[] = {
98 { "fw_cfg_mem", "x-file-slots", "0x10" },
99 { "fw_cfg_io", "x-file-slots", "0x10" },
100 { "pflash_cfi01", "old-multiple-chip-handling", "on" },
101 { "pci-bridge", "shpc", "on" },
102 { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
103 { "virtio-pci", "x-pcie-deverr-init", "off" },
104 { "virtio-pci", "x-pcie-lnkctl-init", "off" },
105 { "virtio-pci", "x-pcie-pm-init", "off" },
106 { "cirrus-vga", "vgamem_mb", "8" },
107 { "isa-cirrus-vga", "vgamem_mb", "8" },
109 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
111 GlobalProperty hw_compat_2_7[] = {
112 { "virtio-pci", "page-per-vq", "on" },
113 { "virtio-serial-device", "emergency-write", "off" },
114 { "ioapic", "version", "0x11" },
115 { "intel-iommu", "x-buggy-eim", "true" },
116 { "virtio-pci", "x-ignore-backend-features", "on" },
118 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
120 GlobalProperty hw_compat_2_6[] = {
121 { "virtio-mmio", "format_transport_address", "off" },
122 /* Optional because not all virtio-pci devices support legacy mode */
123 { "virtio-pci", "disable-modern", "on", .optional = true },
124 { "virtio-pci", "disable-legacy", "off", .optional = true },
126 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
128 GlobalProperty hw_compat_2_5[] = {
129 { "isa-fdc", "fallback", "144" },
130 { "pvscsi", "x-old-pci-configuration", "on" },
131 { "pvscsi", "x-disable-pcie", "on" },
132 { "vmxnet3", "x-old-msi-offsets", "on" },
133 { "vmxnet3", "x-disable-pcie", "on" },
135 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
137 GlobalProperty hw_compat_2_4[] = {
138 { "virtio-blk-device", "scsi", "true" },
139 { "e1000", "extra_mac_registers", "off" },
140 { "virtio-pci", "x-disable-pcie", "on" },
141 { "virtio-pci", "migrate-extra", "off" },
142 { "fw_cfg_mem", "dma_enabled", "off" },
143 { "fw_cfg_io", "dma_enabled", "off" }
145 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
147 GlobalProperty hw_compat_2_3[] = {
148 { "virtio-blk-pci", "any_layout", "off" },
149 { "virtio-balloon-pci", "any_layout", "off" },
150 { "virtio-serial-pci", "any_layout", "off" },
151 { "virtio-9p-pci", "any_layout", "off" },
152 { "virtio-rng-pci", "any_layout", "off" },
153 { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
154 { "migration", "send-configuration", "off" },
155 { "migration", "send-section-footer", "off" },
156 { "migration", "store-global-state", "off" },
158 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
160 GlobalProperty hw_compat_2_2[] = {};
161 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
163 GlobalProperty hw_compat_2_1[] = {
164 { "intel-hda", "old_msi_addr", "on" },
165 { "VGA", "qemu-extended-regs", "off" },
166 { "secondary-vga", "qemu-extended-regs", "off" },
167 { "virtio-scsi-pci", "any_layout", "off" },
168 { "usb-mouse", "usb_version", "1" },
169 { "usb-kbd", "usb_version", "1" },
170 { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
172 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
174 static char *machine_get_accel(Object *obj, Error **errp)
176 MachineState *ms = MACHINE(obj);
178 return g_strdup(ms->accel);
181 static void machine_set_accel(Object *obj, const char *value, Error **errp)
183 MachineState *ms = MACHINE(obj);
185 g_free(ms->accel);
186 ms->accel = g_strdup(value);
189 static void machine_set_kernel_irqchip(Object *obj, Visitor *v,
190 const char *name, void *opaque,
191 Error **errp)
193 Error *err = NULL;
194 MachineState *ms = MACHINE(obj);
195 OnOffSplit mode;
197 visit_type_OnOffSplit(v, name, &mode, &err);
198 if (err) {
199 error_propagate(errp, err);
200 return;
201 } else {
202 switch (mode) {
203 case ON_OFF_SPLIT_ON:
204 ms->kernel_irqchip_allowed = true;
205 ms->kernel_irqchip_required = true;
206 ms->kernel_irqchip_split = false;
207 break;
208 case ON_OFF_SPLIT_OFF:
209 ms->kernel_irqchip_allowed = false;
210 ms->kernel_irqchip_required = false;
211 ms->kernel_irqchip_split = false;
212 break;
213 case ON_OFF_SPLIT_SPLIT:
214 ms->kernel_irqchip_allowed = true;
215 ms->kernel_irqchip_required = true;
216 ms->kernel_irqchip_split = true;
217 break;
218 default:
219 /* The value was checked in visit_type_OnOffSplit() above. If
220 * we get here, then something is wrong in QEMU.
222 abort();
227 static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
228 const char *name, void *opaque,
229 Error **errp)
231 MachineState *ms = MACHINE(obj);
232 int64_t value = ms->kvm_shadow_mem;
234 visit_type_int(v, name, &value, errp);
237 static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
238 const char *name, void *opaque,
239 Error **errp)
241 MachineState *ms = MACHINE(obj);
242 Error *error = NULL;
243 int64_t value;
245 visit_type_int(v, name, &value, &error);
246 if (error) {
247 error_propagate(errp, error);
248 return;
251 ms->kvm_shadow_mem = value;
254 static char *machine_get_kernel(Object *obj, Error **errp)
256 MachineState *ms = MACHINE(obj);
258 return g_strdup(ms->kernel_filename);
261 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
263 MachineState *ms = MACHINE(obj);
265 g_free(ms->kernel_filename);
266 ms->kernel_filename = g_strdup(value);
269 static char *machine_get_initrd(Object *obj, Error **errp)
271 MachineState *ms = MACHINE(obj);
273 return g_strdup(ms->initrd_filename);
276 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
278 MachineState *ms = MACHINE(obj);
280 g_free(ms->initrd_filename);
281 ms->initrd_filename = g_strdup(value);
284 static char *machine_get_append(Object *obj, Error **errp)
286 MachineState *ms = MACHINE(obj);
288 return g_strdup(ms->kernel_cmdline);
291 static void machine_set_append(Object *obj, const char *value, Error **errp)
293 MachineState *ms = MACHINE(obj);
295 g_free(ms->kernel_cmdline);
296 ms->kernel_cmdline = g_strdup(value);
299 static char *machine_get_dtb(Object *obj, Error **errp)
301 MachineState *ms = MACHINE(obj);
303 return g_strdup(ms->dtb);
306 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
308 MachineState *ms = MACHINE(obj);
310 g_free(ms->dtb);
311 ms->dtb = g_strdup(value);
314 static char *machine_get_dumpdtb(Object *obj, Error **errp)
316 MachineState *ms = MACHINE(obj);
318 return g_strdup(ms->dumpdtb);
321 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
323 MachineState *ms = MACHINE(obj);
325 g_free(ms->dumpdtb);
326 ms->dumpdtb = g_strdup(value);
329 static void machine_get_phandle_start(Object *obj, Visitor *v,
330 const char *name, void *opaque,
331 Error **errp)
333 MachineState *ms = MACHINE(obj);
334 int64_t value = ms->phandle_start;
336 visit_type_int(v, name, &value, errp);
339 static void machine_set_phandle_start(Object *obj, Visitor *v,
340 const char *name, void *opaque,
341 Error **errp)
343 MachineState *ms = MACHINE(obj);
344 Error *error = NULL;
345 int64_t value;
347 visit_type_int(v, name, &value, &error);
348 if (error) {
349 error_propagate(errp, error);
350 return;
353 ms->phandle_start = value;
356 static char *machine_get_dt_compatible(Object *obj, Error **errp)
358 MachineState *ms = MACHINE(obj);
360 return g_strdup(ms->dt_compatible);
363 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
365 MachineState *ms = MACHINE(obj);
367 g_free(ms->dt_compatible);
368 ms->dt_compatible = g_strdup(value);
371 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
373 MachineState *ms = MACHINE(obj);
375 return ms->dump_guest_core;
378 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
380 MachineState *ms = MACHINE(obj);
382 ms->dump_guest_core = value;
385 static bool machine_get_mem_merge(Object *obj, Error **errp)
387 MachineState *ms = MACHINE(obj);
389 return ms->mem_merge;
392 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
394 MachineState *ms = MACHINE(obj);
396 ms->mem_merge = value;
399 static bool machine_get_usb(Object *obj, Error **errp)
401 MachineState *ms = MACHINE(obj);
403 return ms->usb;
406 static void machine_set_usb(Object *obj, bool value, Error **errp)
408 MachineState *ms = MACHINE(obj);
410 ms->usb = value;
411 ms->usb_disabled = !value;
414 static bool machine_get_graphics(Object *obj, Error **errp)
416 MachineState *ms = MACHINE(obj);
418 return ms->enable_graphics;
421 static void machine_set_graphics(Object *obj, bool value, Error **errp)
423 MachineState *ms = MACHINE(obj);
425 ms->enable_graphics = value;
428 static bool machine_get_igd_gfx_passthru(Object *obj, Error **errp)
430 MachineState *ms = MACHINE(obj);
432 return ms->igd_gfx_passthru;
435 static void machine_set_igd_gfx_passthru(Object *obj, bool value, Error **errp)
437 MachineState *ms = MACHINE(obj);
439 ms->igd_gfx_passthru = value;
442 static char *machine_get_firmware(Object *obj, Error **errp)
444 MachineState *ms = MACHINE(obj);
446 return g_strdup(ms->firmware);
449 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
451 MachineState *ms = MACHINE(obj);
453 g_free(ms->firmware);
454 ms->firmware = g_strdup(value);
457 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
459 MachineState *ms = MACHINE(obj);
461 ms->suppress_vmdesc = value;
464 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
466 MachineState *ms = MACHINE(obj);
468 return ms->suppress_vmdesc;
471 static void machine_set_enforce_config_section(Object *obj, bool value,
472 Error **errp)
474 MachineState *ms = MACHINE(obj);
476 warn_report("enforce-config-section is deprecated, please use "
477 "-global migration.send-configuration=on|off instead");
479 ms->enforce_config_section = value;
482 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
484 MachineState *ms = MACHINE(obj);
486 return ms->enforce_config_section;
489 static char *machine_get_memory_encryption(Object *obj, Error **errp)
491 MachineState *ms = MACHINE(obj);
493 return g_strdup(ms->memory_encryption);
496 static void machine_set_memory_encryption(Object *obj, const char *value,
497 Error **errp)
499 MachineState *ms = MACHINE(obj);
501 g_free(ms->memory_encryption);
502 ms->memory_encryption = g_strdup(value);
505 static bool machine_get_nvdimm(Object *obj, Error **errp)
507 MachineState *ms = MACHINE(obj);
509 return ms->nvdimms_state->is_enabled;
512 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
514 MachineState *ms = MACHINE(obj);
516 ms->nvdimms_state->is_enabled = value;
519 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
521 MachineState *ms = MACHINE(obj);
523 return g_strdup(ms->nvdimms_state->persistence_string);
526 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
527 Error **errp)
529 MachineState *ms = MACHINE(obj);
530 NVDIMMState *nvdimms_state = ms->nvdimms_state;
532 if (strcmp(value, "cpu") == 0) {
533 nvdimms_state->persistence = 3;
534 } else if (strcmp(value, "mem-ctrl") == 0) {
535 nvdimms_state->persistence = 2;
536 } else {
537 error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
538 value);
539 return;
542 g_free(nvdimms_state->persistence_string);
543 nvdimms_state->persistence_string = g_strdup(value);
546 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
548 strList *item = g_new0(strList, 1);
550 item->value = g_strdup(type);
551 item->next = mc->allowed_dynamic_sysbus_devices;
552 mc->allowed_dynamic_sysbus_devices = item;
555 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
557 MachineState *machine = opaque;
558 MachineClass *mc = MACHINE_GET_CLASS(machine);
559 bool allowed = false;
560 strList *wl;
562 for (wl = mc->allowed_dynamic_sysbus_devices;
563 !allowed && wl;
564 wl = wl->next) {
565 allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
568 if (!allowed) {
569 error_report("Option '-device %s' cannot be handled by this machine",
570 object_class_get_name(object_get_class(OBJECT(sbdev))));
571 exit(1);
575 static void machine_init_notify(Notifier *notifier, void *data)
577 MachineState *machine = MACHINE(qdev_get_machine());
580 * Loop through all dynamically created sysbus devices and check if they are
581 * all allowed. If a device is not allowed, error out.
583 foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
586 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
588 int i;
589 HotpluggableCPUList *head = NULL;
590 MachineClass *mc = MACHINE_GET_CLASS(machine);
592 /* force board to initialize possible_cpus if it hasn't been done yet */
593 mc->possible_cpu_arch_ids(machine);
595 for (i = 0; i < machine->possible_cpus->len; i++) {
596 Object *cpu;
597 HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
598 HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
600 cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
601 cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
602 cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
603 sizeof(*cpu_item->props));
605 cpu = machine->possible_cpus->cpus[i].cpu;
606 if (cpu) {
607 cpu_item->has_qom_path = true;
608 cpu_item->qom_path = object_get_canonical_path(cpu);
610 list_item->value = cpu_item;
611 list_item->next = head;
612 head = list_item;
614 return head;
618 * machine_set_cpu_numa_node:
619 * @machine: machine object to modify
620 * @props: specifies which cpu objects to assign to
621 * numa node specified by @props.node_id
622 * @errp: if an error occurs, a pointer to an area to store the error
624 * Associate NUMA node specified by @props.node_id with cpu slots that
625 * match socket/core/thread-ids specified by @props. It's recommended to use
626 * query-hotpluggable-cpus.props values to specify affected cpu slots,
627 * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
629 * However for CLI convenience it's possible to pass in subset of properties,
630 * which would affect all cpu slots that match it.
631 * Ex for pc machine:
632 * -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
633 * -numa cpu,node-id=0,socket_id=0 \
634 * -numa cpu,node-id=1,socket_id=1
635 * will assign all child cores of socket 0 to node 0 and
636 * of socket 1 to node 1.
638 * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
639 * return error.
640 * Empty subset is disallowed and function will return with error in this case.
642 void machine_set_cpu_numa_node(MachineState *machine,
643 const CpuInstanceProperties *props, Error **errp)
645 MachineClass *mc = MACHINE_GET_CLASS(machine);
646 bool match = false;
647 int i;
649 if (!mc->possible_cpu_arch_ids) {
650 error_setg(errp, "mapping of CPUs to NUMA node is not supported");
651 return;
654 /* disabling node mapping is not supported, forbid it */
655 assert(props->has_node_id);
657 /* force board to initialize possible_cpus if it hasn't been done yet */
658 mc->possible_cpu_arch_ids(machine);
660 for (i = 0; i < machine->possible_cpus->len; i++) {
661 CPUArchId *slot = &machine->possible_cpus->cpus[i];
663 /* reject unsupported by board properties */
664 if (props->has_thread_id && !slot->props.has_thread_id) {
665 error_setg(errp, "thread-id is not supported");
666 return;
669 if (props->has_core_id && !slot->props.has_core_id) {
670 error_setg(errp, "core-id is not supported");
671 return;
674 if (props->has_socket_id && !slot->props.has_socket_id) {
675 error_setg(errp, "socket-id is not supported");
676 return;
679 if (props->has_die_id && !slot->props.has_die_id) {
680 error_setg(errp, "die-id is not supported");
681 return;
684 /* skip slots with explicit mismatch */
685 if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
686 continue;
689 if (props->has_core_id && props->core_id != slot->props.core_id) {
690 continue;
693 if (props->has_die_id && props->die_id != slot->props.die_id) {
694 continue;
697 if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
698 continue;
701 /* reject assignment if slot is already assigned, for compatibility
702 * of legacy cpu_index mapping with SPAPR core based mapping do not
703 * error out if cpu thread and matched core have the same node-id */
704 if (slot->props.has_node_id &&
705 slot->props.node_id != props->node_id) {
706 error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
707 slot->props.node_id);
708 return;
711 /* assign slot to node as it's matched '-numa cpu' key */
712 match = true;
713 slot->props.node_id = props->node_id;
714 slot->props.has_node_id = props->has_node_id;
717 if (!match) {
718 error_setg(errp, "no match found");
722 static void smp_parse(MachineState *ms, QemuOpts *opts)
724 if (opts) {
725 unsigned cpus = qemu_opt_get_number(opts, "cpus", 0);
726 unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
727 unsigned cores = qemu_opt_get_number(opts, "cores", 0);
728 unsigned threads = qemu_opt_get_number(opts, "threads", 0);
730 /* compute missing values, prefer sockets over cores over threads */
731 if (cpus == 0 || sockets == 0) {
732 cores = cores > 0 ? cores : 1;
733 threads = threads > 0 ? threads : 1;
734 if (cpus == 0) {
735 sockets = sockets > 0 ? sockets : 1;
736 cpus = cores * threads * sockets;
737 } else {
738 ms->smp.max_cpus =
739 qemu_opt_get_number(opts, "maxcpus", cpus);
740 sockets = ms->smp.max_cpus / (cores * threads);
742 } else if (cores == 0) {
743 threads = threads > 0 ? threads : 1;
744 cores = cpus / (sockets * threads);
745 cores = cores > 0 ? cores : 1;
746 } else if (threads == 0) {
747 threads = cpus / (cores * sockets);
748 threads = threads > 0 ? threads : 1;
749 } else if (sockets * cores * threads < cpus) {
750 error_report("cpu topology: "
751 "sockets (%u) * cores (%u) * threads (%u) < "
752 "smp_cpus (%u)",
753 sockets, cores, threads, cpus);
754 exit(1);
757 ms->smp.max_cpus =
758 qemu_opt_get_number(opts, "maxcpus", cpus);
760 if (ms->smp.max_cpus < cpus) {
761 error_report("maxcpus must be equal to or greater than smp");
762 exit(1);
765 if (sockets * cores * threads > ms->smp.max_cpus) {
766 error_report("cpu topology: "
767 "sockets (%u) * cores (%u) * threads (%u) > "
768 "maxcpus (%u)",
769 sockets, cores, threads,
770 ms->smp.max_cpus);
771 exit(1);
774 if (sockets * cores * threads != ms->smp.max_cpus) {
775 warn_report("Invalid CPU topology deprecated: "
776 "sockets (%u) * cores (%u) * threads (%u) "
777 "!= maxcpus (%u)",
778 sockets, cores, threads,
779 ms->smp.max_cpus);
782 ms->smp.cpus = cpus;
783 ms->smp.cores = cores;
784 ms->smp.threads = threads;
787 if (ms->smp.cpus > 1) {
788 Error *blocker = NULL;
789 error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
790 replay_add_blocker(blocker);
794 static void machine_class_init(ObjectClass *oc, void *data)
796 MachineClass *mc = MACHINE_CLASS(oc);
798 /* Default 128 MB as guest ram size */
799 mc->default_ram_size = 128 * MiB;
800 mc->rom_file_has_mr = true;
801 mc->smp_parse = smp_parse;
803 /* numa node memory size aligned on 8MB by default.
804 * On Linux, each node's border has to be 8MB aligned
806 mc->numa_mem_align_shift = 23;
807 mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
809 object_class_property_add_str(oc, "accel",
810 machine_get_accel, machine_set_accel, &error_abort);
811 object_class_property_set_description(oc, "accel",
812 "Accelerator list", &error_abort);
814 object_class_property_add(oc, "kernel-irqchip", "on|off|split",
815 NULL, machine_set_kernel_irqchip,
816 NULL, NULL, &error_abort);
817 object_class_property_set_description(oc, "kernel-irqchip",
818 "Configure KVM in-kernel irqchip", &error_abort);
820 object_class_property_add(oc, "kvm-shadow-mem", "int",
821 machine_get_kvm_shadow_mem, machine_set_kvm_shadow_mem,
822 NULL, NULL, &error_abort);
823 object_class_property_set_description(oc, "kvm-shadow-mem",
824 "KVM shadow MMU size", &error_abort);
826 object_class_property_add_str(oc, "kernel",
827 machine_get_kernel, machine_set_kernel, &error_abort);
828 object_class_property_set_description(oc, "kernel",
829 "Linux kernel image file", &error_abort);
831 object_class_property_add_str(oc, "initrd",
832 machine_get_initrd, machine_set_initrd, &error_abort);
833 object_class_property_set_description(oc, "initrd",
834 "Linux initial ramdisk file", &error_abort);
836 object_class_property_add_str(oc, "append",
837 machine_get_append, machine_set_append, &error_abort);
838 object_class_property_set_description(oc, "append",
839 "Linux kernel command line", &error_abort);
841 object_class_property_add_str(oc, "dtb",
842 machine_get_dtb, machine_set_dtb, &error_abort);
843 object_class_property_set_description(oc, "dtb",
844 "Linux kernel device tree file", &error_abort);
846 object_class_property_add_str(oc, "dumpdtb",
847 machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
848 object_class_property_set_description(oc, "dumpdtb",
849 "Dump current dtb to a file and quit", &error_abort);
851 object_class_property_add(oc, "phandle-start", "int",
852 machine_get_phandle_start, machine_set_phandle_start,
853 NULL, NULL, &error_abort);
854 object_class_property_set_description(oc, "phandle-start",
855 "The first phandle ID we may generate dynamically", &error_abort);
857 object_class_property_add_str(oc, "dt-compatible",
858 machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
859 object_class_property_set_description(oc, "dt-compatible",
860 "Overrides the \"compatible\" property of the dt root node",
861 &error_abort);
863 object_class_property_add_bool(oc, "dump-guest-core",
864 machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
865 object_class_property_set_description(oc, "dump-guest-core",
866 "Include guest memory in a core dump", &error_abort);
868 object_class_property_add_bool(oc, "mem-merge",
869 machine_get_mem_merge, machine_set_mem_merge, &error_abort);
870 object_class_property_set_description(oc, "mem-merge",
871 "Enable/disable memory merge support", &error_abort);
873 object_class_property_add_bool(oc, "usb",
874 machine_get_usb, machine_set_usb, &error_abort);
875 object_class_property_set_description(oc, "usb",
876 "Set on/off to enable/disable usb", &error_abort);
878 object_class_property_add_bool(oc, "graphics",
879 machine_get_graphics, machine_set_graphics, &error_abort);
880 object_class_property_set_description(oc, "graphics",
881 "Set on/off to enable/disable graphics emulation", &error_abort);
883 object_class_property_add_bool(oc, "igd-passthru",
884 machine_get_igd_gfx_passthru, machine_set_igd_gfx_passthru,
885 &error_abort);
886 object_class_property_set_description(oc, "igd-passthru",
887 "Set on/off to enable/disable igd passthrou", &error_abort);
889 object_class_property_add_str(oc, "firmware",
890 machine_get_firmware, machine_set_firmware,
891 &error_abort);
892 object_class_property_set_description(oc, "firmware",
893 "Firmware image", &error_abort);
895 object_class_property_add_bool(oc, "suppress-vmdesc",
896 machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
897 &error_abort);
898 object_class_property_set_description(oc, "suppress-vmdesc",
899 "Set on to disable self-describing migration", &error_abort);
901 object_class_property_add_bool(oc, "enforce-config-section",
902 machine_get_enforce_config_section, machine_set_enforce_config_section,
903 &error_abort);
904 object_class_property_set_description(oc, "enforce-config-section",
905 "Set on to enforce configuration section migration", &error_abort);
907 object_class_property_add_str(oc, "memory-encryption",
908 machine_get_memory_encryption, machine_set_memory_encryption,
909 &error_abort);
910 object_class_property_set_description(oc, "memory-encryption",
911 "Set memory encryption object to use", &error_abort);
914 static void machine_class_base_init(ObjectClass *oc, void *data)
916 if (!object_class_is_abstract(oc)) {
917 MachineClass *mc = MACHINE_CLASS(oc);
918 const char *cname = object_class_get_name(oc);
919 assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
920 mc->name = g_strndup(cname,
921 strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
922 mc->compat_props = g_ptr_array_new();
926 static void machine_initfn(Object *obj)
928 MachineState *ms = MACHINE(obj);
929 MachineClass *mc = MACHINE_GET_CLASS(obj);
931 ms->kernel_irqchip_allowed = true;
932 ms->kernel_irqchip_split = mc->default_kernel_irqchip_split;
933 ms->kvm_shadow_mem = -1;
934 ms->dump_guest_core = true;
935 ms->mem_merge = true;
936 ms->enable_graphics = true;
938 if (mc->nvdimm_supported) {
939 Object *obj = OBJECT(ms);
941 ms->nvdimms_state = g_new0(NVDIMMState, 1);
942 object_property_add_bool(obj, "nvdimm",
943 machine_get_nvdimm, machine_set_nvdimm,
944 &error_abort);
945 object_property_set_description(obj, "nvdimm",
946 "Set on/off to enable/disable "
947 "NVDIMM instantiation", NULL);
949 object_property_add_str(obj, "nvdimm-persistence",
950 machine_get_nvdimm_persistence,
951 machine_set_nvdimm_persistence,
952 &error_abort);
953 object_property_set_description(obj, "nvdimm-persistence",
954 "Set NVDIMM persistence"
955 "Valid values are cpu, mem-ctrl",
956 NULL);
960 /* Register notifier when init is done for sysbus sanity checks */
961 ms->sysbus_notifier.notify = machine_init_notify;
962 qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
965 static void machine_finalize(Object *obj)
967 MachineState *ms = MACHINE(obj);
969 g_free(ms->accel);
970 g_free(ms->kernel_filename);
971 g_free(ms->initrd_filename);
972 g_free(ms->kernel_cmdline);
973 g_free(ms->dtb);
974 g_free(ms->dumpdtb);
975 g_free(ms->dt_compatible);
976 g_free(ms->firmware);
977 g_free(ms->device_memory);
978 g_free(ms->nvdimms_state);
981 bool machine_usb(MachineState *machine)
983 return machine->usb;
986 bool machine_kernel_irqchip_allowed(MachineState *machine)
988 return machine->kernel_irqchip_allowed;
991 bool machine_kernel_irqchip_required(MachineState *machine)
993 return machine->kernel_irqchip_required;
996 bool machine_kernel_irqchip_split(MachineState *machine)
998 return machine->kernel_irqchip_split;
1001 int machine_kvm_shadow_mem(MachineState *machine)
1003 return machine->kvm_shadow_mem;
1006 int machine_phandle_start(MachineState *machine)
1008 return machine->phandle_start;
1011 bool machine_dump_guest_core(MachineState *machine)
1013 return machine->dump_guest_core;
1016 bool machine_mem_merge(MachineState *machine)
1018 return machine->mem_merge;
1021 static char *cpu_slot_to_string(const CPUArchId *cpu)
1023 GString *s = g_string_new(NULL);
1024 if (cpu->props.has_socket_id) {
1025 g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
1027 if (cpu->props.has_die_id) {
1028 g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
1030 if (cpu->props.has_core_id) {
1031 if (s->len) {
1032 g_string_append_printf(s, ", ");
1034 g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
1036 if (cpu->props.has_thread_id) {
1037 if (s->len) {
1038 g_string_append_printf(s, ", ");
1040 g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
1042 return g_string_free(s, false);
1045 static void machine_numa_finish_cpu_init(MachineState *machine)
1047 int i;
1048 bool default_mapping;
1049 GString *s = g_string_new(NULL);
1050 MachineClass *mc = MACHINE_GET_CLASS(machine);
1051 const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1053 assert(nb_numa_nodes);
1054 for (i = 0; i < possible_cpus->len; i++) {
1055 if (possible_cpus->cpus[i].props.has_node_id) {
1056 break;
1059 default_mapping = (i == possible_cpus->len);
1061 for (i = 0; i < possible_cpus->len; i++) {
1062 const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1064 if (!cpu_slot->props.has_node_id) {
1065 /* fetch default mapping from board and enable it */
1066 CpuInstanceProperties props = cpu_slot->props;
1068 props.node_id = mc->get_default_cpu_node_id(machine, i);
1069 if (!default_mapping) {
1070 /* record slots with not set mapping,
1071 * TODO: make it hard error in future */
1072 char *cpu_str = cpu_slot_to_string(cpu_slot);
1073 g_string_append_printf(s, "%sCPU %d [%s]",
1074 s->len ? ", " : "", i, cpu_str);
1075 g_free(cpu_str);
1077 /* non mapped cpus used to fallback to node 0 */
1078 props.node_id = 0;
1081 props.has_node_id = true;
1082 machine_set_cpu_numa_node(machine, &props, &error_fatal);
1085 if (s->len && !qtest_enabled()) {
1086 warn_report("CPU(s) not present in any NUMA nodes: %s",
1087 s->str);
1088 warn_report("All CPU(s) up to maxcpus should be described "
1089 "in NUMA config, ability to start up with partial NUMA "
1090 "mappings is obsoleted and will be removed in future");
1092 g_string_free(s, true);
1095 void machine_run_board_init(MachineState *machine)
1097 MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1099 numa_complete_configuration(machine);
1100 if (nb_numa_nodes) {
1101 machine_numa_finish_cpu_init(machine);
1104 /* If the machine supports the valid_cpu_types check and the user
1105 * specified a CPU with -cpu check here that the user CPU is supported.
1107 if (machine_class->valid_cpu_types && machine->cpu_type) {
1108 ObjectClass *class = object_class_by_name(machine->cpu_type);
1109 int i;
1111 for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1112 if (object_class_dynamic_cast(class,
1113 machine_class->valid_cpu_types[i])) {
1114 /* The user specificed CPU is in the valid field, we are
1115 * good to go.
1117 break;
1121 if (!machine_class->valid_cpu_types[i]) {
1122 /* The user specified CPU is not valid */
1123 error_report("Invalid CPU type: %s", machine->cpu_type);
1124 error_printf("The valid types are: %s",
1125 machine_class->valid_cpu_types[0]);
1126 for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1127 error_printf(", %s", machine_class->valid_cpu_types[i]);
1129 error_printf("\n");
1131 exit(1);
1135 machine_class->init(machine);
1138 static const TypeInfo machine_info = {
1139 .name = TYPE_MACHINE,
1140 .parent = TYPE_OBJECT,
1141 .abstract = true,
1142 .class_size = sizeof(MachineClass),
1143 .class_init = machine_class_init,
1144 .class_base_init = machine_class_base_init,
1145 .instance_size = sizeof(MachineState),
1146 .instance_init = machine_initfn,
1147 .instance_finalize = machine_finalize,
1150 static void machine_register_types(void)
1152 type_register_static(&machine_info);
1155 type_init(machine_register_types)