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