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