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