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