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