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