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