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