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