xref: /qemu/hw/core/machine.c (revision 9445e1e15e66c19e42bea942ba810db28052cd05)
1 /*
2  * QEMU Machine
3  *
4  * Copyright (C) 2014 Red Hat Inc
5  *
6  * Authors:
7  *   Marcel Apfelbaum <marcel.a@redhat.com>
8  *
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.
11  */
12 
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"
29 #include "migration/vmstate.h"
30 
31 GlobalProperty hw_compat_5_1[] = {
32     { "vhost-scsi", "num_queues", "1"},
33     { "vhost-user-scsi", "num_queues", "1"},
34     { "virtio-blk-device", "num-queues", "1"},
35     { "virtio-scsi-device", "num_queues", "1"},
36 };
37 const size_t hw_compat_5_1_len = G_N_ELEMENTS(hw_compat_5_1);
38 
39 GlobalProperty hw_compat_5_0[] = {
40     { "pci-host-bridge", "x-config-reg-migration-enabled", "off" },
41     { "virtio-balloon-device", "page-poison", "false" },
42     { "vmport", "x-read-set-eax", "off" },
43     { "vmport", "x-signal-unsupported-cmd", "off" },
44     { "vmport", "x-report-vmx-type", "off" },
45     { "vmport", "x-cmds-v2", "off" },
46 };
47 const size_t hw_compat_5_0_len = G_N_ELEMENTS(hw_compat_5_0);
48 
49 GlobalProperty hw_compat_4_2[] = {
50     { "virtio-blk-device", "queue-size", "128"},
51     { "virtio-scsi-device", "virtqueue_size", "128"},
52     { "virtio-blk-device", "x-enable-wce-if-config-wce", "off" },
53     { "virtio-blk-device", "seg-max-adjust", "off"},
54     { "virtio-scsi-device", "seg_max_adjust", "off"},
55     { "vhost-blk-device", "seg_max_adjust", "off"},
56     { "usb-host", "suppress-remote-wake", "off" },
57     { "usb-redir", "suppress-remote-wake", "off" },
58     { "qxl", "revision", "4" },
59     { "qxl-vga", "revision", "4" },
60     { "fw_cfg", "acpi-mr-restore", "false" },
61 };
62 const size_t hw_compat_4_2_len = G_N_ELEMENTS(hw_compat_4_2);
63 
64 GlobalProperty hw_compat_4_1[] = {
65     { "virtio-pci", "x-pcie-flr-init", "off" },
66     { "virtio-device", "use-disabled-flag", "false" },
67 };
68 const size_t hw_compat_4_1_len = G_N_ELEMENTS(hw_compat_4_1);
69 
70 GlobalProperty hw_compat_4_0[] = {
71     { "VGA",            "edid", "false" },
72     { "secondary-vga",  "edid", "false" },
73     { "bochs-display",  "edid", "false" },
74     { "virtio-vga",     "edid", "false" },
75     { "virtio-gpu-device", "edid", "false" },
76     { "virtio-device", "use-started", "false" },
77     { "virtio-balloon-device", "qemu-4-0-config-size", "true" },
78     { "pl031", "migrate-tick-offset", "false" },
79 };
80 const size_t hw_compat_4_0_len = G_N_ELEMENTS(hw_compat_4_0);
81 
82 GlobalProperty hw_compat_3_1[] = {
83     { "pcie-root-port", "x-speed", "2_5" },
84     { "pcie-root-port", "x-width", "1" },
85     { "memory-backend-file", "x-use-canonical-path-for-ramblock-id", "true" },
86     { "memory-backend-memfd", "x-use-canonical-path-for-ramblock-id", "true" },
87     { "tpm-crb", "ppi", "false" },
88     { "tpm-tis", "ppi", "false" },
89     { "usb-kbd", "serial", "42" },
90     { "usb-mouse", "serial", "42" },
91     { "usb-tablet", "serial", "42" },
92     { "virtio-blk-device", "discard", "false" },
93     { "virtio-blk-device", "write-zeroes", "false" },
94     { "virtio-balloon-device", "qemu-4-0-config-size", "false" },
95     { "pcie-root-port-base", "disable-acs", "true" }, /* Added in 4.1 */
96 };
97 const size_t hw_compat_3_1_len = G_N_ELEMENTS(hw_compat_3_1);
98 
99 GlobalProperty hw_compat_3_0[] = {};
100 const size_t hw_compat_3_0_len = G_N_ELEMENTS(hw_compat_3_0);
101 
102 GlobalProperty hw_compat_2_12[] = {
103     { "migration", "decompress-error-check", "off" },
104     { "hda-audio", "use-timer", "false" },
105     { "cirrus-vga", "global-vmstate", "true" },
106     { "VGA", "global-vmstate", "true" },
107     { "vmware-svga", "global-vmstate", "true" },
108     { "qxl-vga", "global-vmstate", "true" },
109 };
110 const size_t hw_compat_2_12_len = G_N_ELEMENTS(hw_compat_2_12);
111 
112 GlobalProperty hw_compat_2_11[] = {
113     { "hpet", "hpet-offset-saved", "false" },
114     { "virtio-blk-pci", "vectors", "2" },
115     { "vhost-user-blk-pci", "vectors", "2" },
116     { "e1000", "migrate_tso_props", "off" },
117 };
118 const size_t hw_compat_2_11_len = G_N_ELEMENTS(hw_compat_2_11);
119 
120 GlobalProperty hw_compat_2_10[] = {
121     { "virtio-mouse-device", "wheel-axis", "false" },
122     { "virtio-tablet-device", "wheel-axis", "false" },
123 };
124 const size_t hw_compat_2_10_len = G_N_ELEMENTS(hw_compat_2_10);
125 
126 GlobalProperty hw_compat_2_9[] = {
127     { "pci-bridge", "shpc", "off" },
128     { "intel-iommu", "pt", "off" },
129     { "virtio-net-device", "x-mtu-bypass-backend", "off" },
130     { "pcie-root-port", "x-migrate-msix", "false" },
131 };
132 const size_t hw_compat_2_9_len = G_N_ELEMENTS(hw_compat_2_9);
133 
134 GlobalProperty hw_compat_2_8[] = {
135     { "fw_cfg_mem", "x-file-slots", "0x10" },
136     { "fw_cfg_io", "x-file-slots", "0x10" },
137     { "pflash_cfi01", "old-multiple-chip-handling", "on" },
138     { "pci-bridge", "shpc", "on" },
139     { TYPE_PCI_DEVICE, "x-pcie-extcap-init", "off" },
140     { "virtio-pci", "x-pcie-deverr-init", "off" },
141     { "virtio-pci", "x-pcie-lnkctl-init", "off" },
142     { "virtio-pci", "x-pcie-pm-init", "off" },
143     { "cirrus-vga", "vgamem_mb", "8" },
144     { "isa-cirrus-vga", "vgamem_mb", "8" },
145 };
146 const size_t hw_compat_2_8_len = G_N_ELEMENTS(hw_compat_2_8);
147 
148 GlobalProperty hw_compat_2_7[] = {
149     { "virtio-pci", "page-per-vq", "on" },
150     { "virtio-serial-device", "emergency-write", "off" },
151     { "ioapic", "version", "0x11" },
152     { "intel-iommu", "x-buggy-eim", "true" },
153     { "virtio-pci", "x-ignore-backend-features", "on" },
154 };
155 const size_t hw_compat_2_7_len = G_N_ELEMENTS(hw_compat_2_7);
156 
157 GlobalProperty hw_compat_2_6[] = {
158     { "virtio-mmio", "format_transport_address", "off" },
159     /* Optional because not all virtio-pci devices support legacy mode */
160     { "virtio-pci", "disable-modern", "on",  .optional = true },
161     { "virtio-pci", "disable-legacy", "off", .optional = true },
162 };
163 const size_t hw_compat_2_6_len = G_N_ELEMENTS(hw_compat_2_6);
164 
165 GlobalProperty hw_compat_2_5[] = {
166     { "isa-fdc", "fallback", "144" },
167     { "pvscsi", "x-old-pci-configuration", "on" },
168     { "pvscsi", "x-disable-pcie", "on" },
169     { "vmxnet3", "x-old-msi-offsets", "on" },
170     { "vmxnet3", "x-disable-pcie", "on" },
171 };
172 const size_t hw_compat_2_5_len = G_N_ELEMENTS(hw_compat_2_5);
173 
174 GlobalProperty hw_compat_2_4[] = {
175     /* Optional because the 'scsi' property is Linux-only */
176     { "virtio-blk-device", "scsi", "true", .optional = true },
177     { "e1000", "extra_mac_registers", "off" },
178     { "virtio-pci", "x-disable-pcie", "on" },
179     { "virtio-pci", "migrate-extra", "off" },
180     { "fw_cfg_mem", "dma_enabled", "off" },
181     { "fw_cfg_io", "dma_enabled", "off" }
182 };
183 const size_t hw_compat_2_4_len = G_N_ELEMENTS(hw_compat_2_4);
184 
185 GlobalProperty hw_compat_2_3[] = {
186     { "virtio-blk-pci", "any_layout", "off" },
187     { "virtio-balloon-pci", "any_layout", "off" },
188     { "virtio-serial-pci", "any_layout", "off" },
189     { "virtio-9p-pci", "any_layout", "off" },
190     { "virtio-rng-pci", "any_layout", "off" },
191     { TYPE_PCI_DEVICE, "x-pcie-lnksta-dllla", "off" },
192     { "migration", "send-configuration", "off" },
193     { "migration", "send-section-footer", "off" },
194     { "migration", "store-global-state", "off" },
195 };
196 const size_t hw_compat_2_3_len = G_N_ELEMENTS(hw_compat_2_3);
197 
198 GlobalProperty hw_compat_2_2[] = {};
199 const size_t hw_compat_2_2_len = G_N_ELEMENTS(hw_compat_2_2);
200 
201 GlobalProperty hw_compat_2_1[] = {
202     { "intel-hda", "old_msi_addr", "on" },
203     { "VGA", "qemu-extended-regs", "off" },
204     { "secondary-vga", "qemu-extended-regs", "off" },
205     { "virtio-scsi-pci", "any_layout", "off" },
206     { "usb-mouse", "usb_version", "1" },
207     { "usb-kbd", "usb_version", "1" },
208     { "virtio-pci", "virtio-pci-bus-master-bug-migration", "on" },
209 };
210 const size_t hw_compat_2_1_len = G_N_ELEMENTS(hw_compat_2_1);
211 
212 static char *machine_get_kernel(Object *obj, Error **errp)
213 {
214     MachineState *ms = MACHINE(obj);
215 
216     return g_strdup(ms->kernel_filename);
217 }
218 
219 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
220 {
221     MachineState *ms = MACHINE(obj);
222 
223     g_free(ms->kernel_filename);
224     ms->kernel_filename = g_strdup(value);
225 }
226 
227 static char *machine_get_initrd(Object *obj, Error **errp)
228 {
229     MachineState *ms = MACHINE(obj);
230 
231     return g_strdup(ms->initrd_filename);
232 }
233 
234 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
235 {
236     MachineState *ms = MACHINE(obj);
237 
238     g_free(ms->initrd_filename);
239     ms->initrd_filename = g_strdup(value);
240 }
241 
242 static char *machine_get_append(Object *obj, Error **errp)
243 {
244     MachineState *ms = MACHINE(obj);
245 
246     return g_strdup(ms->kernel_cmdline);
247 }
248 
249 static void machine_set_append(Object *obj, const char *value, Error **errp)
250 {
251     MachineState *ms = MACHINE(obj);
252 
253     g_free(ms->kernel_cmdline);
254     ms->kernel_cmdline = g_strdup(value);
255 }
256 
257 static char *machine_get_dtb(Object *obj, Error **errp)
258 {
259     MachineState *ms = MACHINE(obj);
260 
261     return g_strdup(ms->dtb);
262 }
263 
264 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
265 {
266     MachineState *ms = MACHINE(obj);
267 
268     g_free(ms->dtb);
269     ms->dtb = g_strdup(value);
270 }
271 
272 static char *machine_get_dumpdtb(Object *obj, Error **errp)
273 {
274     MachineState *ms = MACHINE(obj);
275 
276     return g_strdup(ms->dumpdtb);
277 }
278 
279 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
280 {
281     MachineState *ms = MACHINE(obj);
282 
283     g_free(ms->dumpdtb);
284     ms->dumpdtb = g_strdup(value);
285 }
286 
287 static void machine_get_phandle_start(Object *obj, Visitor *v,
288                                       const char *name, void *opaque,
289                                       Error **errp)
290 {
291     MachineState *ms = MACHINE(obj);
292     int64_t value = ms->phandle_start;
293 
294     visit_type_int(v, name, &value, errp);
295 }
296 
297 static void machine_set_phandle_start(Object *obj, Visitor *v,
298                                       const char *name, void *opaque,
299                                       Error **errp)
300 {
301     MachineState *ms = MACHINE(obj);
302     int64_t value;
303 
304     if (!visit_type_int(v, name, &value, errp)) {
305         return;
306     }
307 
308     ms->phandle_start = value;
309 }
310 
311 static char *machine_get_dt_compatible(Object *obj, Error **errp)
312 {
313     MachineState *ms = MACHINE(obj);
314 
315     return g_strdup(ms->dt_compatible);
316 }
317 
318 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
319 {
320     MachineState *ms = MACHINE(obj);
321 
322     g_free(ms->dt_compatible);
323     ms->dt_compatible = g_strdup(value);
324 }
325 
326 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
327 {
328     MachineState *ms = MACHINE(obj);
329 
330     return ms->dump_guest_core;
331 }
332 
333 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
334 {
335     MachineState *ms = MACHINE(obj);
336 
337     ms->dump_guest_core = value;
338 }
339 
340 static bool machine_get_mem_merge(Object *obj, Error **errp)
341 {
342     MachineState *ms = MACHINE(obj);
343 
344     return ms->mem_merge;
345 }
346 
347 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
348 {
349     MachineState *ms = MACHINE(obj);
350 
351     ms->mem_merge = value;
352 }
353 
354 static bool machine_get_usb(Object *obj, Error **errp)
355 {
356     MachineState *ms = MACHINE(obj);
357 
358     return ms->usb;
359 }
360 
361 static void machine_set_usb(Object *obj, bool value, Error **errp)
362 {
363     MachineState *ms = MACHINE(obj);
364 
365     ms->usb = value;
366     ms->usb_disabled = !value;
367 }
368 
369 static bool machine_get_graphics(Object *obj, Error **errp)
370 {
371     MachineState *ms = MACHINE(obj);
372 
373     return ms->enable_graphics;
374 }
375 
376 static void machine_set_graphics(Object *obj, bool value, Error **errp)
377 {
378     MachineState *ms = MACHINE(obj);
379 
380     ms->enable_graphics = value;
381 }
382 
383 static char *machine_get_firmware(Object *obj, Error **errp)
384 {
385     MachineState *ms = MACHINE(obj);
386 
387     return g_strdup(ms->firmware);
388 }
389 
390 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
391 {
392     MachineState *ms = MACHINE(obj);
393 
394     g_free(ms->firmware);
395     ms->firmware = g_strdup(value);
396 }
397 
398 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
399 {
400     MachineState *ms = MACHINE(obj);
401 
402     ms->suppress_vmdesc = value;
403 }
404 
405 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
406 {
407     MachineState *ms = MACHINE(obj);
408 
409     return ms->suppress_vmdesc;
410 }
411 
412 static void machine_set_enforce_config_section(Object *obj, bool value,
413                                              Error **errp)
414 {
415     MachineState *ms = MACHINE(obj);
416 
417     warn_report("enforce-config-section is deprecated, please use "
418                 "-global migration.send-configuration=on|off instead");
419 
420     ms->enforce_config_section = value;
421 }
422 
423 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
424 {
425     MachineState *ms = MACHINE(obj);
426 
427     return ms->enforce_config_section;
428 }
429 
430 static char *machine_get_memory_encryption(Object *obj, Error **errp)
431 {
432     MachineState *ms = MACHINE(obj);
433 
434     return g_strdup(ms->memory_encryption);
435 }
436 
437 static void machine_set_memory_encryption(Object *obj, const char *value,
438                                         Error **errp)
439 {
440     MachineState *ms = MACHINE(obj);
441 
442     g_free(ms->memory_encryption);
443     ms->memory_encryption = g_strdup(value);
444 
445     /*
446      * With memory encryption, the host can't see the real contents of RAM,
447      * so there's no point in it trying to merge areas.
448      */
449     if (value) {
450         machine_set_mem_merge(obj, false, errp);
451     }
452 }
453 
454 static bool machine_get_nvdimm(Object *obj, Error **errp)
455 {
456     MachineState *ms = MACHINE(obj);
457 
458     return ms->nvdimms_state->is_enabled;
459 }
460 
461 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
462 {
463     MachineState *ms = MACHINE(obj);
464 
465     ms->nvdimms_state->is_enabled = value;
466 }
467 
468 static bool machine_get_hmat(Object *obj, Error **errp)
469 {
470     MachineState *ms = MACHINE(obj);
471 
472     return ms->numa_state->hmat_enabled;
473 }
474 
475 static void machine_set_hmat(Object *obj, bool value, Error **errp)
476 {
477     MachineState *ms = MACHINE(obj);
478 
479     ms->numa_state->hmat_enabled = value;
480 }
481 
482 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
483 {
484     MachineState *ms = MACHINE(obj);
485 
486     return g_strdup(ms->nvdimms_state->persistence_string);
487 }
488 
489 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
490                                            Error **errp)
491 {
492     MachineState *ms = MACHINE(obj);
493     NVDIMMState *nvdimms_state = ms->nvdimms_state;
494 
495     if (strcmp(value, "cpu") == 0) {
496         nvdimms_state->persistence = 3;
497     } else if (strcmp(value, "mem-ctrl") == 0) {
498         nvdimms_state->persistence = 2;
499     } else {
500         error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
501                    value);
502         return;
503     }
504 
505     g_free(nvdimms_state->persistence_string);
506     nvdimms_state->persistence_string = g_strdup(value);
507 }
508 
509 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
510 {
511     strList *item = g_new0(strList, 1);
512 
513     item->value = g_strdup(type);
514     item->next = mc->allowed_dynamic_sysbus_devices;
515     mc->allowed_dynamic_sysbus_devices = item;
516 }
517 
518 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
519 {
520     MachineState *machine = opaque;
521     MachineClass *mc = MACHINE_GET_CLASS(machine);
522     bool allowed = false;
523     strList *wl;
524 
525     for (wl = mc->allowed_dynamic_sysbus_devices;
526          !allowed && wl;
527          wl = wl->next) {
528         allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
529     }
530 
531     if (!allowed) {
532         error_report("Option '-device %s' cannot be handled by this machine",
533                      object_class_get_name(object_get_class(OBJECT(sbdev))));
534         exit(1);
535     }
536 }
537 
538 static char *machine_get_memdev(Object *obj, Error **errp)
539 {
540     MachineState *ms = MACHINE(obj);
541 
542     return g_strdup(ms->ram_memdev_id);
543 }
544 
545 static void machine_set_memdev(Object *obj, const char *value, Error **errp)
546 {
547     MachineState *ms = MACHINE(obj);
548 
549     g_free(ms->ram_memdev_id);
550     ms->ram_memdev_id = g_strdup(value);
551 }
552 
553 
554 static void machine_init_notify(Notifier *notifier, void *data)
555 {
556     MachineState *machine = MACHINE(qdev_get_machine());
557 
558     /*
559      * Loop through all dynamically created sysbus devices and check if they are
560      * all allowed.  If a device is not allowed, error out.
561      */
562     foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
563 }
564 
565 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
566 {
567     int i;
568     HotpluggableCPUList *head = NULL;
569     MachineClass *mc = MACHINE_GET_CLASS(machine);
570 
571     /* force board to initialize possible_cpus if it hasn't been done yet */
572     mc->possible_cpu_arch_ids(machine);
573 
574     for (i = 0; i < machine->possible_cpus->len; i++) {
575         Object *cpu;
576         HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
577         HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
578 
579         cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
580         cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
581         cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
582                                    sizeof(*cpu_item->props));
583 
584         cpu = machine->possible_cpus->cpus[i].cpu;
585         if (cpu) {
586             cpu_item->has_qom_path = true;
587             cpu_item->qom_path = object_get_canonical_path(cpu);
588         }
589         list_item->value = cpu_item;
590         list_item->next = head;
591         head = list_item;
592     }
593     return head;
594 }
595 
596 /**
597  * machine_set_cpu_numa_node:
598  * @machine: machine object to modify
599  * @props: specifies which cpu objects to assign to
600  *         numa node specified by @props.node_id
601  * @errp: if an error occurs, a pointer to an area to store the error
602  *
603  * Associate NUMA node specified by @props.node_id with cpu slots that
604  * match socket/core/thread-ids specified by @props. It's recommended to use
605  * query-hotpluggable-cpus.props values to specify affected cpu slots,
606  * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
607  *
608  * However for CLI convenience it's possible to pass in subset of properties,
609  * which would affect all cpu slots that match it.
610  * Ex for pc machine:
611  *    -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
612  *    -numa cpu,node-id=0,socket_id=0 \
613  *    -numa cpu,node-id=1,socket_id=1
614  * will assign all child cores of socket 0 to node 0 and
615  * of socket 1 to node 1.
616  *
617  * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
618  * return error.
619  * Empty subset is disallowed and function will return with error in this case.
620  */
621 void machine_set_cpu_numa_node(MachineState *machine,
622                                const CpuInstanceProperties *props, Error **errp)
623 {
624     MachineClass *mc = MACHINE_GET_CLASS(machine);
625     NodeInfo *numa_info = machine->numa_state->nodes;
626     bool match = false;
627     int i;
628 
629     if (!mc->possible_cpu_arch_ids) {
630         error_setg(errp, "mapping of CPUs to NUMA node is not supported");
631         return;
632     }
633 
634     /* disabling node mapping is not supported, forbid it */
635     assert(props->has_node_id);
636 
637     /* force board to initialize possible_cpus if it hasn't been done yet */
638     mc->possible_cpu_arch_ids(machine);
639 
640     for (i = 0; i < machine->possible_cpus->len; i++) {
641         CPUArchId *slot = &machine->possible_cpus->cpus[i];
642 
643         /* reject unsupported by board properties */
644         if (props->has_thread_id && !slot->props.has_thread_id) {
645             error_setg(errp, "thread-id is not supported");
646             return;
647         }
648 
649         if (props->has_core_id && !slot->props.has_core_id) {
650             error_setg(errp, "core-id is not supported");
651             return;
652         }
653 
654         if (props->has_socket_id && !slot->props.has_socket_id) {
655             error_setg(errp, "socket-id is not supported");
656             return;
657         }
658 
659         if (props->has_die_id && !slot->props.has_die_id) {
660             error_setg(errp, "die-id is not supported");
661             return;
662         }
663 
664         /* skip slots with explicit mismatch */
665         if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
666                 continue;
667         }
668 
669         if (props->has_core_id && props->core_id != slot->props.core_id) {
670                 continue;
671         }
672 
673         if (props->has_die_id && props->die_id != slot->props.die_id) {
674                 continue;
675         }
676 
677         if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
678                 continue;
679         }
680 
681         /* reject assignment if slot is already assigned, for compatibility
682          * of legacy cpu_index mapping with SPAPR core based mapping do not
683          * error out if cpu thread and matched core have the same node-id */
684         if (slot->props.has_node_id &&
685             slot->props.node_id != props->node_id) {
686             error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
687                        slot->props.node_id);
688             return;
689         }
690 
691         /* assign slot to node as it's matched '-numa cpu' key */
692         match = true;
693         slot->props.node_id = props->node_id;
694         slot->props.has_node_id = props->has_node_id;
695 
696         if (machine->numa_state->hmat_enabled) {
697             if ((numa_info[props->node_id].initiator < MAX_NODES) &&
698                 (props->node_id != numa_info[props->node_id].initiator)) {
699                 error_setg(errp, "The initiator of CPU NUMA node %" PRId64
700                         " should be itself", props->node_id);
701                 return;
702             }
703             numa_info[props->node_id].has_cpu = true;
704             numa_info[props->node_id].initiator = props->node_id;
705         }
706     }
707 
708     if (!match) {
709         error_setg(errp, "no match found");
710     }
711 }
712 
713 static void smp_parse(MachineState *ms, QemuOpts *opts)
714 {
715     if (opts) {
716         unsigned cpus    = qemu_opt_get_number(opts, "cpus", 0);
717         unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
718         unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
719         unsigned threads = qemu_opt_get_number(opts, "threads", 0);
720 
721         /* compute missing values, prefer sockets over cores over threads */
722         if (cpus == 0 || sockets == 0) {
723             cores = cores > 0 ? cores : 1;
724             threads = threads > 0 ? threads : 1;
725             if (cpus == 0) {
726                 sockets = sockets > 0 ? sockets : 1;
727                 cpus = cores * threads * sockets;
728             } else {
729                 ms->smp.max_cpus =
730                         qemu_opt_get_number(opts, "maxcpus", cpus);
731                 sockets = ms->smp.max_cpus / (cores * threads);
732             }
733         } else if (cores == 0) {
734             threads = threads > 0 ? threads : 1;
735             cores = cpus / (sockets * threads);
736             cores = cores > 0 ? cores : 1;
737         } else if (threads == 0) {
738             threads = cpus / (cores * sockets);
739             threads = threads > 0 ? threads : 1;
740         } else if (sockets * cores * threads < cpus) {
741             error_report("cpu topology: "
742                          "sockets (%u) * cores (%u) * threads (%u) < "
743                          "smp_cpus (%u)",
744                          sockets, cores, threads, cpus);
745             exit(1);
746         }
747 
748         ms->smp.max_cpus =
749                 qemu_opt_get_number(opts, "maxcpus", cpus);
750 
751         if (ms->smp.max_cpus < cpus) {
752             error_report("maxcpus must be equal to or greater than smp");
753             exit(1);
754         }
755 
756         if (sockets * cores * threads > ms->smp.max_cpus) {
757             error_report("cpu topology: "
758                          "sockets (%u) * cores (%u) * threads (%u) > "
759                          "maxcpus (%u)",
760                          sockets, cores, threads,
761                          ms->smp.max_cpus);
762             exit(1);
763         }
764 
765         if (sockets * cores * threads != ms->smp.max_cpus) {
766             warn_report("Invalid CPU topology deprecated: "
767                         "sockets (%u) * cores (%u) * threads (%u) "
768                         "!= maxcpus (%u)",
769                         sockets, cores, threads,
770                         ms->smp.max_cpus);
771         }
772 
773         ms->smp.cpus = cpus;
774         ms->smp.cores = cores;
775         ms->smp.threads = threads;
776         ms->smp.sockets = sockets;
777     }
778 
779     if (ms->smp.cpus > 1) {
780         Error *blocker = NULL;
781         error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
782         replay_add_blocker(blocker);
783     }
784 }
785 
786 static void machine_class_init(ObjectClass *oc, void *data)
787 {
788     MachineClass *mc = MACHINE_CLASS(oc);
789 
790     /* Default 128 MB as guest ram size */
791     mc->default_ram_size = 128 * MiB;
792     mc->rom_file_has_mr = true;
793     mc->smp_parse = smp_parse;
794 
795     /* numa node memory size aligned on 8MB by default.
796      * On Linux, each node's border has to be 8MB aligned
797      */
798     mc->numa_mem_align_shift = 23;
799     mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
800 
801     object_class_property_add_str(oc, "kernel",
802         machine_get_kernel, machine_set_kernel);
803     object_class_property_set_description(oc, "kernel",
804         "Linux kernel image file");
805 
806     object_class_property_add_str(oc, "initrd",
807         machine_get_initrd, machine_set_initrd);
808     object_class_property_set_description(oc, "initrd",
809         "Linux initial ramdisk file");
810 
811     object_class_property_add_str(oc, "append",
812         machine_get_append, machine_set_append);
813     object_class_property_set_description(oc, "append",
814         "Linux kernel command line");
815 
816     object_class_property_add_str(oc, "dtb",
817         machine_get_dtb, machine_set_dtb);
818     object_class_property_set_description(oc, "dtb",
819         "Linux kernel device tree file");
820 
821     object_class_property_add_str(oc, "dumpdtb",
822         machine_get_dumpdtb, machine_set_dumpdtb);
823     object_class_property_set_description(oc, "dumpdtb",
824         "Dump current dtb to a file and quit");
825 
826     object_class_property_add(oc, "phandle-start", "int",
827         machine_get_phandle_start, machine_set_phandle_start,
828         NULL, NULL);
829     object_class_property_set_description(oc, "phandle-start",
830         "The first phandle ID we may generate dynamically");
831 
832     object_class_property_add_str(oc, "dt-compatible",
833         machine_get_dt_compatible, machine_set_dt_compatible);
834     object_class_property_set_description(oc, "dt-compatible",
835         "Overrides the \"compatible\" property of the dt root node");
836 
837     object_class_property_add_bool(oc, "dump-guest-core",
838         machine_get_dump_guest_core, machine_set_dump_guest_core);
839     object_class_property_set_description(oc, "dump-guest-core",
840         "Include guest memory in a core dump");
841 
842     object_class_property_add_bool(oc, "mem-merge",
843         machine_get_mem_merge, machine_set_mem_merge);
844     object_class_property_set_description(oc, "mem-merge",
845         "Enable/disable memory merge support");
846 
847     object_class_property_add_bool(oc, "usb",
848         machine_get_usb, machine_set_usb);
849     object_class_property_set_description(oc, "usb",
850         "Set on/off to enable/disable usb");
851 
852     object_class_property_add_bool(oc, "graphics",
853         machine_get_graphics, machine_set_graphics);
854     object_class_property_set_description(oc, "graphics",
855         "Set on/off to enable/disable graphics emulation");
856 
857     object_class_property_add_str(oc, "firmware",
858         machine_get_firmware, machine_set_firmware);
859     object_class_property_set_description(oc, "firmware",
860         "Firmware image");
861 
862     object_class_property_add_bool(oc, "suppress-vmdesc",
863         machine_get_suppress_vmdesc, machine_set_suppress_vmdesc);
864     object_class_property_set_description(oc, "suppress-vmdesc",
865         "Set on to disable self-describing migration");
866 
867     object_class_property_add_bool(oc, "enforce-config-section",
868         machine_get_enforce_config_section, machine_set_enforce_config_section);
869     object_class_property_set_description(oc, "enforce-config-section",
870         "Set on to enforce configuration section migration");
871 
872     object_class_property_add_str(oc, "memory-encryption",
873         machine_get_memory_encryption, machine_set_memory_encryption);
874     object_class_property_set_description(oc, "memory-encryption",
875         "Set memory encryption object to use");
876 }
877 
878 static void machine_class_base_init(ObjectClass *oc, void *data)
879 {
880     if (!object_class_is_abstract(oc)) {
881         MachineClass *mc = MACHINE_CLASS(oc);
882         const char *cname = object_class_get_name(oc);
883         assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
884         mc->name = g_strndup(cname,
885                             strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
886         mc->compat_props = g_ptr_array_new();
887     }
888 }
889 
890 static void machine_initfn(Object *obj)
891 {
892     MachineState *ms = MACHINE(obj);
893     MachineClass *mc = MACHINE_GET_CLASS(obj);
894 
895     ms->dump_guest_core = true;
896     ms->mem_merge = true;
897     ms->enable_graphics = true;
898 
899     if (mc->nvdimm_supported) {
900         Object *obj = OBJECT(ms);
901 
902         ms->nvdimms_state = g_new0(NVDIMMState, 1);
903         object_property_add_bool(obj, "nvdimm",
904                                  machine_get_nvdimm, machine_set_nvdimm);
905         object_property_set_description(obj, "nvdimm",
906                                         "Set on/off to enable/disable "
907                                         "NVDIMM instantiation");
908 
909         object_property_add_str(obj, "nvdimm-persistence",
910                                 machine_get_nvdimm_persistence,
911                                 machine_set_nvdimm_persistence);
912         object_property_set_description(obj, "nvdimm-persistence",
913                                         "Set NVDIMM persistence"
914                                         "Valid values are cpu, mem-ctrl");
915     }
916 
917     if (mc->cpu_index_to_instance_props && mc->get_default_cpu_node_id) {
918         ms->numa_state = g_new0(NumaState, 1);
919         object_property_add_bool(obj, "hmat",
920                                  machine_get_hmat, machine_set_hmat);
921         object_property_set_description(obj, "hmat",
922                                         "Set on/off to enable/disable "
923                                         "ACPI Heterogeneous Memory Attribute "
924                                         "Table (HMAT)");
925     }
926 
927     object_property_add_str(obj, "memory-backend",
928                             machine_get_memdev, machine_set_memdev);
929     object_property_set_description(obj, "memory-backend",
930                                     "Set RAM backend"
931                                     "Valid value is ID of hostmem based backend");
932 
933     /* Register notifier when init is done for sysbus sanity checks */
934     ms->sysbus_notifier.notify = machine_init_notify;
935     qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
936 }
937 
938 static void machine_finalize(Object *obj)
939 {
940     MachineState *ms = MACHINE(obj);
941 
942     g_free(ms->kernel_filename);
943     g_free(ms->initrd_filename);
944     g_free(ms->kernel_cmdline);
945     g_free(ms->dtb);
946     g_free(ms->dumpdtb);
947     g_free(ms->dt_compatible);
948     g_free(ms->firmware);
949     g_free(ms->device_memory);
950     g_free(ms->nvdimms_state);
951     g_free(ms->numa_state);
952 }
953 
954 bool machine_usb(MachineState *machine)
955 {
956     return machine->usb;
957 }
958 
959 int machine_phandle_start(MachineState *machine)
960 {
961     return machine->phandle_start;
962 }
963 
964 bool machine_dump_guest_core(MachineState *machine)
965 {
966     return machine->dump_guest_core;
967 }
968 
969 bool machine_mem_merge(MachineState *machine)
970 {
971     return machine->mem_merge;
972 }
973 
974 static char *cpu_slot_to_string(const CPUArchId *cpu)
975 {
976     GString *s = g_string_new(NULL);
977     if (cpu->props.has_socket_id) {
978         g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
979     }
980     if (cpu->props.has_die_id) {
981         g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
982     }
983     if (cpu->props.has_core_id) {
984         if (s->len) {
985             g_string_append_printf(s, ", ");
986         }
987         g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
988     }
989     if (cpu->props.has_thread_id) {
990         if (s->len) {
991             g_string_append_printf(s, ", ");
992         }
993         g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
994     }
995     return g_string_free(s, false);
996 }
997 
998 static void numa_validate_initiator(NumaState *numa_state)
999 {
1000     int i;
1001     NodeInfo *numa_info = numa_state->nodes;
1002 
1003     for (i = 0; i < numa_state->num_nodes; i++) {
1004         if (numa_info[i].initiator == MAX_NODES) {
1005             error_report("The initiator of NUMA node %d is missing, use "
1006                          "'-numa node,initiator' option to declare it", i);
1007             exit(1);
1008         }
1009 
1010         if (!numa_info[numa_info[i].initiator].present) {
1011             error_report("NUMA node %" PRIu16 " is missing, use "
1012                          "'-numa node' option to declare it first",
1013                          numa_info[i].initiator);
1014             exit(1);
1015         }
1016 
1017         if (!numa_info[numa_info[i].initiator].has_cpu) {
1018             error_report("The initiator of NUMA node %d is invalid", i);
1019             exit(1);
1020         }
1021     }
1022 }
1023 
1024 static void machine_numa_finish_cpu_init(MachineState *machine)
1025 {
1026     int i;
1027     bool default_mapping;
1028     GString *s = g_string_new(NULL);
1029     MachineClass *mc = MACHINE_GET_CLASS(machine);
1030     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
1031 
1032     assert(machine->numa_state->num_nodes);
1033     for (i = 0; i < possible_cpus->len; i++) {
1034         if (possible_cpus->cpus[i].props.has_node_id) {
1035             break;
1036         }
1037     }
1038     default_mapping = (i == possible_cpus->len);
1039 
1040     for (i = 0; i < possible_cpus->len; i++) {
1041         const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
1042 
1043         if (!cpu_slot->props.has_node_id) {
1044             /* fetch default mapping from board and enable it */
1045             CpuInstanceProperties props = cpu_slot->props;
1046 
1047             props.node_id = mc->get_default_cpu_node_id(machine, i);
1048             if (!default_mapping) {
1049                 /* record slots with not set mapping,
1050                  * TODO: make it hard error in future */
1051                 char *cpu_str = cpu_slot_to_string(cpu_slot);
1052                 g_string_append_printf(s, "%sCPU %d [%s]",
1053                                        s->len ? ", " : "", i, cpu_str);
1054                 g_free(cpu_str);
1055 
1056                 /* non mapped cpus used to fallback to node 0 */
1057                 props.node_id = 0;
1058             }
1059 
1060             props.has_node_id = true;
1061             machine_set_cpu_numa_node(machine, &props, &error_fatal);
1062         }
1063     }
1064 
1065     if (machine->numa_state->hmat_enabled) {
1066         numa_validate_initiator(machine->numa_state);
1067     }
1068 
1069     if (s->len && !qtest_enabled()) {
1070         warn_report("CPU(s) not present in any NUMA nodes: %s",
1071                     s->str);
1072         warn_report("All CPU(s) up to maxcpus should be described "
1073                     "in NUMA config, ability to start up with partial NUMA "
1074                     "mappings is obsoleted and will be removed in future");
1075     }
1076     g_string_free(s, true);
1077 }
1078 
1079 MemoryRegion *machine_consume_memdev(MachineState *machine,
1080                                      HostMemoryBackend *backend)
1081 {
1082     MemoryRegion *ret = host_memory_backend_get_memory(backend);
1083 
1084     if (memory_region_is_mapped(ret)) {
1085         error_report("memory backend %s can't be used multiple times.",
1086                      object_get_canonical_path_component(OBJECT(backend)));
1087         exit(EXIT_FAILURE);
1088     }
1089     host_memory_backend_set_mapped(backend, true);
1090     vmstate_register_ram_global(ret);
1091     return ret;
1092 }
1093 
1094 void machine_run_board_init(MachineState *machine)
1095 {
1096     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1097 
1098     if (machine->ram_memdev_id) {
1099         Object *o;
1100         o = object_resolve_path_type(machine->ram_memdev_id,
1101                                      TYPE_MEMORY_BACKEND, NULL);
1102         machine->ram = machine_consume_memdev(machine, MEMORY_BACKEND(o));
1103     }
1104 
1105     if (machine->numa_state) {
1106         numa_complete_configuration(machine);
1107         if (machine->numa_state->num_nodes) {
1108             machine_numa_finish_cpu_init(machine);
1109         }
1110     }
1111 
1112     /* If the machine supports the valid_cpu_types check and the user
1113      * specified a CPU with -cpu check here that the user CPU is supported.
1114      */
1115     if (machine_class->valid_cpu_types && machine->cpu_type) {
1116         ObjectClass *class = object_class_by_name(machine->cpu_type);
1117         int i;
1118 
1119         for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1120             if (object_class_dynamic_cast(class,
1121                                           machine_class->valid_cpu_types[i])) {
1122                 /* The user specificed CPU is in the valid field, we are
1123                  * good to go.
1124                  */
1125                 break;
1126             }
1127         }
1128 
1129         if (!machine_class->valid_cpu_types[i]) {
1130             /* The user specified CPU is not valid */
1131             error_report("Invalid CPU type: %s", machine->cpu_type);
1132             error_printf("The valid types are: %s",
1133                          machine_class->valid_cpu_types[0]);
1134             for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1135                 error_printf(", %s", machine_class->valid_cpu_types[i]);
1136             }
1137             error_printf("\n");
1138 
1139             exit(1);
1140         }
1141     }
1142 
1143     machine_class->init(machine);
1144 }
1145 
1146 static const TypeInfo machine_info = {
1147     .name = TYPE_MACHINE,
1148     .parent = TYPE_OBJECT,
1149     .abstract = true,
1150     .class_size = sizeof(MachineClass),
1151     .class_init    = machine_class_init,
1152     .class_base_init = machine_class_base_init,
1153     .instance_size = sizeof(MachineState),
1154     .instance_init = machine_initfn,
1155     .instance_finalize = machine_finalize,
1156 };
1157 
1158 static void machine_register_types(void)
1159 {
1160     type_register_static(&machine_info);
1161 }
1162 
1163 type_init(machine_register_types)
1164