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