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