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