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