xref: /openbmc/qemu/hw/core/machine.c (revision 23b0898e4471f42e62aa1fea304f6a6e23d03310)
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 void machine_set_kernel_irqchip(Object *obj, Visitor *v,
182                                        const char *name, void *opaque,
183                                        Error **errp)
184 {
185     Error *err = NULL;
186     MachineState *ms = MACHINE(obj);
187     OnOffSplit mode;
188 
189     visit_type_OnOffSplit(v, name, &mode, &err);
190     if (err) {
191         error_propagate(errp, err);
192         return;
193     } else {
194         switch (mode) {
195         case ON_OFF_SPLIT_ON:
196             ms->kernel_irqchip_allowed = true;
197             ms->kernel_irqchip_required = true;
198             ms->kernel_irqchip_split = false;
199             break;
200         case ON_OFF_SPLIT_OFF:
201             ms->kernel_irqchip_allowed = false;
202             ms->kernel_irqchip_required = false;
203             ms->kernel_irqchip_split = false;
204             break;
205         case ON_OFF_SPLIT_SPLIT:
206             ms->kernel_irqchip_allowed = true;
207             ms->kernel_irqchip_required = true;
208             ms->kernel_irqchip_split = true;
209             break;
210         default:
211             /* The value was checked in visit_type_OnOffSplit() above. If
212              * we get here, then something is wrong in QEMU.
213              */
214             abort();
215         }
216     }
217 }
218 
219 static char *machine_get_kernel(Object *obj, Error **errp)
220 {
221     MachineState *ms = MACHINE(obj);
222 
223     return g_strdup(ms->kernel_filename);
224 }
225 
226 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
227 {
228     MachineState *ms = MACHINE(obj);
229 
230     g_free(ms->kernel_filename);
231     ms->kernel_filename = g_strdup(value);
232 }
233 
234 static char *machine_get_initrd(Object *obj, Error **errp)
235 {
236     MachineState *ms = MACHINE(obj);
237 
238     return g_strdup(ms->initrd_filename);
239 }
240 
241 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
242 {
243     MachineState *ms = MACHINE(obj);
244 
245     g_free(ms->initrd_filename);
246     ms->initrd_filename = g_strdup(value);
247 }
248 
249 static char *machine_get_append(Object *obj, Error **errp)
250 {
251     MachineState *ms = MACHINE(obj);
252 
253     return g_strdup(ms->kernel_cmdline);
254 }
255 
256 static void machine_set_append(Object *obj, const char *value, Error **errp)
257 {
258     MachineState *ms = MACHINE(obj);
259 
260     g_free(ms->kernel_cmdline);
261     ms->kernel_cmdline = g_strdup(value);
262 }
263 
264 static char *machine_get_dtb(Object *obj, Error **errp)
265 {
266     MachineState *ms = MACHINE(obj);
267 
268     return g_strdup(ms->dtb);
269 }
270 
271 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
272 {
273     MachineState *ms = MACHINE(obj);
274 
275     g_free(ms->dtb);
276     ms->dtb = g_strdup(value);
277 }
278 
279 static char *machine_get_dumpdtb(Object *obj, Error **errp)
280 {
281     MachineState *ms = MACHINE(obj);
282 
283     return g_strdup(ms->dumpdtb);
284 }
285 
286 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
287 {
288     MachineState *ms = MACHINE(obj);
289 
290     g_free(ms->dumpdtb);
291     ms->dumpdtb = g_strdup(value);
292 }
293 
294 static void machine_get_phandle_start(Object *obj, Visitor *v,
295                                       const char *name, void *opaque,
296                                       Error **errp)
297 {
298     MachineState *ms = MACHINE(obj);
299     int64_t value = ms->phandle_start;
300 
301     visit_type_int(v, name, &value, errp);
302 }
303 
304 static void machine_set_phandle_start(Object *obj, Visitor *v,
305                                       const char *name, void *opaque,
306                                       Error **errp)
307 {
308     MachineState *ms = MACHINE(obj);
309     Error *error = NULL;
310     int64_t value;
311 
312     visit_type_int(v, name, &value, &error);
313     if (error) {
314         error_propagate(errp, error);
315         return;
316     }
317 
318     ms->phandle_start = value;
319 }
320 
321 static char *machine_get_dt_compatible(Object *obj, Error **errp)
322 {
323     MachineState *ms = MACHINE(obj);
324 
325     return g_strdup(ms->dt_compatible);
326 }
327 
328 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
329 {
330     MachineState *ms = MACHINE(obj);
331 
332     g_free(ms->dt_compatible);
333     ms->dt_compatible = g_strdup(value);
334 }
335 
336 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
337 {
338     MachineState *ms = MACHINE(obj);
339 
340     return ms->dump_guest_core;
341 }
342 
343 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
344 {
345     MachineState *ms = MACHINE(obj);
346 
347     ms->dump_guest_core = value;
348 }
349 
350 static bool machine_get_mem_merge(Object *obj, Error **errp)
351 {
352     MachineState *ms = MACHINE(obj);
353 
354     return ms->mem_merge;
355 }
356 
357 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
358 {
359     MachineState *ms = MACHINE(obj);
360 
361     ms->mem_merge = value;
362 }
363 
364 static bool machine_get_usb(Object *obj, Error **errp)
365 {
366     MachineState *ms = MACHINE(obj);
367 
368     return ms->usb;
369 }
370 
371 static void machine_set_usb(Object *obj, bool value, Error **errp)
372 {
373     MachineState *ms = MACHINE(obj);
374 
375     ms->usb = value;
376     ms->usb_disabled = !value;
377 }
378 
379 static bool machine_get_graphics(Object *obj, Error **errp)
380 {
381     MachineState *ms = MACHINE(obj);
382 
383     return ms->enable_graphics;
384 }
385 
386 static void machine_set_graphics(Object *obj, bool value, Error **errp)
387 {
388     MachineState *ms = MACHINE(obj);
389 
390     ms->enable_graphics = value;
391 }
392 
393 static char *machine_get_firmware(Object *obj, Error **errp)
394 {
395     MachineState *ms = MACHINE(obj);
396 
397     return g_strdup(ms->firmware);
398 }
399 
400 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
401 {
402     MachineState *ms = MACHINE(obj);
403 
404     g_free(ms->firmware);
405     ms->firmware = g_strdup(value);
406 }
407 
408 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
409 {
410     MachineState *ms = MACHINE(obj);
411 
412     ms->suppress_vmdesc = value;
413 }
414 
415 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
416 {
417     MachineState *ms = MACHINE(obj);
418 
419     return ms->suppress_vmdesc;
420 }
421 
422 static void machine_set_enforce_config_section(Object *obj, bool value,
423                                              Error **errp)
424 {
425     MachineState *ms = MACHINE(obj);
426 
427     warn_report("enforce-config-section is deprecated, please use "
428                 "-global migration.send-configuration=on|off instead");
429 
430     ms->enforce_config_section = value;
431 }
432 
433 static bool machine_get_enforce_config_section(Object *obj, Error **errp)
434 {
435     MachineState *ms = MACHINE(obj);
436 
437     return ms->enforce_config_section;
438 }
439 
440 static char *machine_get_memory_encryption(Object *obj, Error **errp)
441 {
442     MachineState *ms = MACHINE(obj);
443 
444     return g_strdup(ms->memory_encryption);
445 }
446 
447 static void machine_set_memory_encryption(Object *obj, const char *value,
448                                         Error **errp)
449 {
450     MachineState *ms = MACHINE(obj);
451 
452     g_free(ms->memory_encryption);
453     ms->memory_encryption = g_strdup(value);
454 }
455 
456 static bool machine_get_nvdimm(Object *obj, Error **errp)
457 {
458     MachineState *ms = MACHINE(obj);
459 
460     return ms->nvdimms_state->is_enabled;
461 }
462 
463 static void machine_set_nvdimm(Object *obj, bool value, Error **errp)
464 {
465     MachineState *ms = MACHINE(obj);
466 
467     ms->nvdimms_state->is_enabled = value;
468 }
469 
470 static char *machine_get_nvdimm_persistence(Object *obj, Error **errp)
471 {
472     MachineState *ms = MACHINE(obj);
473 
474     return g_strdup(ms->nvdimms_state->persistence_string);
475 }
476 
477 static void machine_set_nvdimm_persistence(Object *obj, const char *value,
478                                            Error **errp)
479 {
480     MachineState *ms = MACHINE(obj);
481     NVDIMMState *nvdimms_state = ms->nvdimms_state;
482 
483     if (strcmp(value, "cpu") == 0) {
484         nvdimms_state->persistence = 3;
485     } else if (strcmp(value, "mem-ctrl") == 0) {
486         nvdimms_state->persistence = 2;
487     } else {
488         error_setg(errp, "-machine nvdimm-persistence=%s: unsupported option",
489                    value);
490         return;
491     }
492 
493     g_free(nvdimms_state->persistence_string);
494     nvdimms_state->persistence_string = g_strdup(value);
495 }
496 
497 void machine_class_allow_dynamic_sysbus_dev(MachineClass *mc, const char *type)
498 {
499     strList *item = g_new0(strList, 1);
500 
501     item->value = g_strdup(type);
502     item->next = mc->allowed_dynamic_sysbus_devices;
503     mc->allowed_dynamic_sysbus_devices = item;
504 }
505 
506 static void validate_sysbus_device(SysBusDevice *sbdev, void *opaque)
507 {
508     MachineState *machine = opaque;
509     MachineClass *mc = MACHINE_GET_CLASS(machine);
510     bool allowed = false;
511     strList *wl;
512 
513     for (wl = mc->allowed_dynamic_sysbus_devices;
514          !allowed && wl;
515          wl = wl->next) {
516         allowed |= !!object_dynamic_cast(OBJECT(sbdev), wl->value);
517     }
518 
519     if (!allowed) {
520         error_report("Option '-device %s' cannot be handled by this machine",
521                      object_class_get_name(object_get_class(OBJECT(sbdev))));
522         exit(1);
523     }
524 }
525 
526 static void machine_init_notify(Notifier *notifier, void *data)
527 {
528     MachineState *machine = MACHINE(qdev_get_machine());
529 
530     /*
531      * Loop through all dynamically created sysbus devices and check if they are
532      * all allowed.  If a device is not allowed, error out.
533      */
534     foreach_dynamic_sysbus_device(validate_sysbus_device, machine);
535 }
536 
537 HotpluggableCPUList *machine_query_hotpluggable_cpus(MachineState *machine)
538 {
539     int i;
540     HotpluggableCPUList *head = NULL;
541     MachineClass *mc = MACHINE_GET_CLASS(machine);
542 
543     /* force board to initialize possible_cpus if it hasn't been done yet */
544     mc->possible_cpu_arch_ids(machine);
545 
546     for (i = 0; i < machine->possible_cpus->len; i++) {
547         Object *cpu;
548         HotpluggableCPUList *list_item = g_new0(typeof(*list_item), 1);
549         HotpluggableCPU *cpu_item = g_new0(typeof(*cpu_item), 1);
550 
551         cpu_item->type = g_strdup(machine->possible_cpus->cpus[i].type);
552         cpu_item->vcpus_count = machine->possible_cpus->cpus[i].vcpus_count;
553         cpu_item->props = g_memdup(&machine->possible_cpus->cpus[i].props,
554                                    sizeof(*cpu_item->props));
555 
556         cpu = machine->possible_cpus->cpus[i].cpu;
557         if (cpu) {
558             cpu_item->has_qom_path = true;
559             cpu_item->qom_path = object_get_canonical_path(cpu);
560         }
561         list_item->value = cpu_item;
562         list_item->next = head;
563         head = list_item;
564     }
565     return head;
566 }
567 
568 /**
569  * machine_set_cpu_numa_node:
570  * @machine: machine object to modify
571  * @props: specifies which cpu objects to assign to
572  *         numa node specified by @props.node_id
573  * @errp: if an error occurs, a pointer to an area to store the error
574  *
575  * Associate NUMA node specified by @props.node_id with cpu slots that
576  * match socket/core/thread-ids specified by @props. It's recommended to use
577  * query-hotpluggable-cpus.props values to specify affected cpu slots,
578  * which would lead to exact 1:1 mapping of cpu slots to NUMA node.
579  *
580  * However for CLI convenience it's possible to pass in subset of properties,
581  * which would affect all cpu slots that match it.
582  * Ex for pc machine:
583  *    -smp 4,cores=2,sockets=2 -numa node,nodeid=0 -numa node,nodeid=1 \
584  *    -numa cpu,node-id=0,socket_id=0 \
585  *    -numa cpu,node-id=1,socket_id=1
586  * will assign all child cores of socket 0 to node 0 and
587  * of socket 1 to node 1.
588  *
589  * On attempt of reassigning (already assigned) cpu slot to another NUMA node,
590  * return error.
591  * Empty subset is disallowed and function will return with error in this case.
592  */
593 void machine_set_cpu_numa_node(MachineState *machine,
594                                const CpuInstanceProperties *props, Error **errp)
595 {
596     MachineClass *mc = MACHINE_GET_CLASS(machine);
597     bool match = false;
598     int i;
599 
600     if (!mc->possible_cpu_arch_ids) {
601         error_setg(errp, "mapping of CPUs to NUMA node is not supported");
602         return;
603     }
604 
605     /* disabling node mapping is not supported, forbid it */
606     assert(props->has_node_id);
607 
608     /* force board to initialize possible_cpus if it hasn't been done yet */
609     mc->possible_cpu_arch_ids(machine);
610 
611     for (i = 0; i < machine->possible_cpus->len; i++) {
612         CPUArchId *slot = &machine->possible_cpus->cpus[i];
613 
614         /* reject unsupported by board properties */
615         if (props->has_thread_id && !slot->props.has_thread_id) {
616             error_setg(errp, "thread-id is not supported");
617             return;
618         }
619 
620         if (props->has_core_id && !slot->props.has_core_id) {
621             error_setg(errp, "core-id is not supported");
622             return;
623         }
624 
625         if (props->has_socket_id && !slot->props.has_socket_id) {
626             error_setg(errp, "socket-id is not supported");
627             return;
628         }
629 
630         if (props->has_die_id && !slot->props.has_die_id) {
631             error_setg(errp, "die-id is not supported");
632             return;
633         }
634 
635         /* skip slots with explicit mismatch */
636         if (props->has_thread_id && props->thread_id != slot->props.thread_id) {
637                 continue;
638         }
639 
640         if (props->has_core_id && props->core_id != slot->props.core_id) {
641                 continue;
642         }
643 
644         if (props->has_die_id && props->die_id != slot->props.die_id) {
645                 continue;
646         }
647 
648         if (props->has_socket_id && props->socket_id != slot->props.socket_id) {
649                 continue;
650         }
651 
652         /* reject assignment if slot is already assigned, for compatibility
653          * of legacy cpu_index mapping with SPAPR core based mapping do not
654          * error out if cpu thread and matched core have the same node-id */
655         if (slot->props.has_node_id &&
656             slot->props.node_id != props->node_id) {
657             error_setg(errp, "CPU is already assigned to node-id: %" PRId64,
658                        slot->props.node_id);
659             return;
660         }
661 
662         /* assign slot to node as it's matched '-numa cpu' key */
663         match = true;
664         slot->props.node_id = props->node_id;
665         slot->props.has_node_id = props->has_node_id;
666     }
667 
668     if (!match) {
669         error_setg(errp, "no match found");
670     }
671 }
672 
673 static void smp_parse(MachineState *ms, QemuOpts *opts)
674 {
675     if (opts) {
676         unsigned cpus    = qemu_opt_get_number(opts, "cpus", 0);
677         unsigned sockets = qemu_opt_get_number(opts, "sockets", 0);
678         unsigned cores   = qemu_opt_get_number(opts, "cores", 0);
679         unsigned threads = qemu_opt_get_number(opts, "threads", 0);
680 
681         /* compute missing values, prefer sockets over cores over threads */
682         if (cpus == 0 || sockets == 0) {
683             cores = cores > 0 ? cores : 1;
684             threads = threads > 0 ? threads : 1;
685             if (cpus == 0) {
686                 sockets = sockets > 0 ? sockets : 1;
687                 cpus = cores * threads * sockets;
688             } else {
689                 ms->smp.max_cpus =
690                         qemu_opt_get_number(opts, "maxcpus", cpus);
691                 sockets = ms->smp.max_cpus / (cores * threads);
692             }
693         } else if (cores == 0) {
694             threads = threads > 0 ? threads : 1;
695             cores = cpus / (sockets * threads);
696             cores = cores > 0 ? cores : 1;
697         } else if (threads == 0) {
698             threads = cpus / (cores * sockets);
699             threads = threads > 0 ? threads : 1;
700         } else if (sockets * cores * threads < cpus) {
701             error_report("cpu topology: "
702                          "sockets (%u) * cores (%u) * threads (%u) < "
703                          "smp_cpus (%u)",
704                          sockets, cores, threads, cpus);
705             exit(1);
706         }
707 
708         ms->smp.max_cpus =
709                 qemu_opt_get_number(opts, "maxcpus", cpus);
710 
711         if (ms->smp.max_cpus < cpus) {
712             error_report("maxcpus must be equal to or greater than smp");
713             exit(1);
714         }
715 
716         if (sockets * cores * threads > ms->smp.max_cpus) {
717             error_report("cpu topology: "
718                          "sockets (%u) * cores (%u) * threads (%u) > "
719                          "maxcpus (%u)",
720                          sockets, cores, threads,
721                          ms->smp.max_cpus);
722             exit(1);
723         }
724 
725         if (sockets * cores * threads != ms->smp.max_cpus) {
726             warn_report("Invalid CPU topology deprecated: "
727                         "sockets (%u) * cores (%u) * threads (%u) "
728                         "!= maxcpus (%u)",
729                         sockets, cores, threads,
730                         ms->smp.max_cpus);
731         }
732 
733         ms->smp.cpus = cpus;
734         ms->smp.cores = cores;
735         ms->smp.threads = threads;
736     }
737 
738     if (ms->smp.cpus > 1) {
739         Error *blocker = NULL;
740         error_setg(&blocker, QERR_REPLAY_NOT_SUPPORTED, "smp");
741         replay_add_blocker(blocker);
742     }
743 }
744 
745 static void machine_class_init(ObjectClass *oc, void *data)
746 {
747     MachineClass *mc = MACHINE_CLASS(oc);
748 
749     /* Default 128 MB as guest ram size */
750     mc->default_ram_size = 128 * MiB;
751     mc->rom_file_has_mr = true;
752     mc->smp_parse = smp_parse;
753 
754     /* numa node memory size aligned on 8MB by default.
755      * On Linux, each node's border has to be 8MB aligned
756      */
757     mc->numa_mem_align_shift = 23;
758     mc->numa_auto_assign_ram = numa_default_auto_assign_ram;
759 
760     object_class_property_add(oc, "kernel-irqchip", "on|off|split",
761         NULL, machine_set_kernel_irqchip,
762         NULL, NULL, &error_abort);
763     object_class_property_set_description(oc, "kernel-irqchip",
764         "Configure KVM in-kernel irqchip", &error_abort);
765 
766     object_class_property_add_str(oc, "kernel",
767         machine_get_kernel, machine_set_kernel, &error_abort);
768     object_class_property_set_description(oc, "kernel",
769         "Linux kernel image file", &error_abort);
770 
771     object_class_property_add_str(oc, "initrd",
772         machine_get_initrd, machine_set_initrd, &error_abort);
773     object_class_property_set_description(oc, "initrd",
774         "Linux initial ramdisk file", &error_abort);
775 
776     object_class_property_add_str(oc, "append",
777         machine_get_append, machine_set_append, &error_abort);
778     object_class_property_set_description(oc, "append",
779         "Linux kernel command line", &error_abort);
780 
781     object_class_property_add_str(oc, "dtb",
782         machine_get_dtb, machine_set_dtb, &error_abort);
783     object_class_property_set_description(oc, "dtb",
784         "Linux kernel device tree file", &error_abort);
785 
786     object_class_property_add_str(oc, "dumpdtb",
787         machine_get_dumpdtb, machine_set_dumpdtb, &error_abort);
788     object_class_property_set_description(oc, "dumpdtb",
789         "Dump current dtb to a file and quit", &error_abort);
790 
791     object_class_property_add(oc, "phandle-start", "int",
792         machine_get_phandle_start, machine_set_phandle_start,
793         NULL, NULL, &error_abort);
794     object_class_property_set_description(oc, "phandle-start",
795             "The first phandle ID we may generate dynamically", &error_abort);
796 
797     object_class_property_add_str(oc, "dt-compatible",
798         machine_get_dt_compatible, machine_set_dt_compatible, &error_abort);
799     object_class_property_set_description(oc, "dt-compatible",
800         "Overrides the \"compatible\" property of the dt root node",
801         &error_abort);
802 
803     object_class_property_add_bool(oc, "dump-guest-core",
804         machine_get_dump_guest_core, machine_set_dump_guest_core, &error_abort);
805     object_class_property_set_description(oc, "dump-guest-core",
806         "Include guest memory in a core dump", &error_abort);
807 
808     object_class_property_add_bool(oc, "mem-merge",
809         machine_get_mem_merge, machine_set_mem_merge, &error_abort);
810     object_class_property_set_description(oc, "mem-merge",
811         "Enable/disable memory merge support", &error_abort);
812 
813     object_class_property_add_bool(oc, "usb",
814         machine_get_usb, machine_set_usb, &error_abort);
815     object_class_property_set_description(oc, "usb",
816         "Set on/off to enable/disable usb", &error_abort);
817 
818     object_class_property_add_bool(oc, "graphics",
819         machine_get_graphics, machine_set_graphics, &error_abort);
820     object_class_property_set_description(oc, "graphics",
821         "Set on/off to enable/disable graphics emulation", &error_abort);
822 
823     object_class_property_add_str(oc, "firmware",
824         machine_get_firmware, machine_set_firmware,
825         &error_abort);
826     object_class_property_set_description(oc, "firmware",
827         "Firmware image", &error_abort);
828 
829     object_class_property_add_bool(oc, "suppress-vmdesc",
830         machine_get_suppress_vmdesc, machine_set_suppress_vmdesc,
831         &error_abort);
832     object_class_property_set_description(oc, "suppress-vmdesc",
833         "Set on to disable self-describing migration", &error_abort);
834 
835     object_class_property_add_bool(oc, "enforce-config-section",
836         machine_get_enforce_config_section, machine_set_enforce_config_section,
837         &error_abort);
838     object_class_property_set_description(oc, "enforce-config-section",
839         "Set on to enforce configuration section migration", &error_abort);
840 
841     object_class_property_add_str(oc, "memory-encryption",
842         machine_get_memory_encryption, machine_set_memory_encryption,
843         &error_abort);
844     object_class_property_set_description(oc, "memory-encryption",
845         "Set memory encryption object to use", &error_abort);
846 }
847 
848 static void machine_class_base_init(ObjectClass *oc, void *data)
849 {
850     if (!object_class_is_abstract(oc)) {
851         MachineClass *mc = MACHINE_CLASS(oc);
852         const char *cname = object_class_get_name(oc);
853         assert(g_str_has_suffix(cname, TYPE_MACHINE_SUFFIX));
854         mc->name = g_strndup(cname,
855                             strlen(cname) - strlen(TYPE_MACHINE_SUFFIX));
856         mc->compat_props = g_ptr_array_new();
857     }
858 }
859 
860 static void machine_initfn(Object *obj)
861 {
862     MachineState *ms = MACHINE(obj);
863     MachineClass *mc = MACHINE_GET_CLASS(obj);
864 
865     ms->kernel_irqchip_allowed = true;
866     ms->kernel_irqchip_split = mc->default_kernel_irqchip_split;
867     ms->dump_guest_core = true;
868     ms->mem_merge = true;
869     ms->enable_graphics = true;
870 
871     if (mc->nvdimm_supported) {
872         Object *obj = OBJECT(ms);
873 
874         ms->nvdimms_state = g_new0(NVDIMMState, 1);
875         object_property_add_bool(obj, "nvdimm",
876                                  machine_get_nvdimm, machine_set_nvdimm,
877                                  &error_abort);
878         object_property_set_description(obj, "nvdimm",
879                                         "Set on/off to enable/disable "
880                                         "NVDIMM instantiation", NULL);
881 
882         object_property_add_str(obj, "nvdimm-persistence",
883                                 machine_get_nvdimm_persistence,
884                                 machine_set_nvdimm_persistence,
885                                 &error_abort);
886         object_property_set_description(obj, "nvdimm-persistence",
887                                         "Set NVDIMM persistence"
888                                         "Valid values are cpu, mem-ctrl",
889                                         NULL);
890     }
891 
892     if (mc->numa_mem_supported) {
893         ms->numa_state = g_new0(NumaState, 1);
894     }
895 
896     /* Register notifier when init is done for sysbus sanity checks */
897     ms->sysbus_notifier.notify = machine_init_notify;
898     qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
899 }
900 
901 static void machine_finalize(Object *obj)
902 {
903     MachineState *ms = MACHINE(obj);
904 
905     g_free(ms->kernel_filename);
906     g_free(ms->initrd_filename);
907     g_free(ms->kernel_cmdline);
908     g_free(ms->dtb);
909     g_free(ms->dumpdtb);
910     g_free(ms->dt_compatible);
911     g_free(ms->firmware);
912     g_free(ms->device_memory);
913     g_free(ms->nvdimms_state);
914     g_free(ms->numa_state);
915 }
916 
917 bool machine_usb(MachineState *machine)
918 {
919     return machine->usb;
920 }
921 
922 bool machine_kernel_irqchip_allowed(MachineState *machine)
923 {
924     return machine->kernel_irqchip_allowed;
925 }
926 
927 bool machine_kernel_irqchip_required(MachineState *machine)
928 {
929     return machine->kernel_irqchip_required;
930 }
931 
932 bool machine_kernel_irqchip_split(MachineState *machine)
933 {
934     return machine->kernel_irqchip_split;
935 }
936 
937 int machine_phandle_start(MachineState *machine)
938 {
939     return machine->phandle_start;
940 }
941 
942 bool machine_dump_guest_core(MachineState *machine)
943 {
944     return machine->dump_guest_core;
945 }
946 
947 bool machine_mem_merge(MachineState *machine)
948 {
949     return machine->mem_merge;
950 }
951 
952 static char *cpu_slot_to_string(const CPUArchId *cpu)
953 {
954     GString *s = g_string_new(NULL);
955     if (cpu->props.has_socket_id) {
956         g_string_append_printf(s, "socket-id: %"PRId64, cpu->props.socket_id);
957     }
958     if (cpu->props.has_die_id) {
959         g_string_append_printf(s, "die-id: %"PRId64, cpu->props.die_id);
960     }
961     if (cpu->props.has_core_id) {
962         if (s->len) {
963             g_string_append_printf(s, ", ");
964         }
965         g_string_append_printf(s, "core-id: %"PRId64, cpu->props.core_id);
966     }
967     if (cpu->props.has_thread_id) {
968         if (s->len) {
969             g_string_append_printf(s, ", ");
970         }
971         g_string_append_printf(s, "thread-id: %"PRId64, cpu->props.thread_id);
972     }
973     return g_string_free(s, false);
974 }
975 
976 static void machine_numa_finish_cpu_init(MachineState *machine)
977 {
978     int i;
979     bool default_mapping;
980     GString *s = g_string_new(NULL);
981     MachineClass *mc = MACHINE_GET_CLASS(machine);
982     const CPUArchIdList *possible_cpus = mc->possible_cpu_arch_ids(machine);
983 
984     assert(machine->numa_state->num_nodes);
985     for (i = 0; i < possible_cpus->len; i++) {
986         if (possible_cpus->cpus[i].props.has_node_id) {
987             break;
988         }
989     }
990     default_mapping = (i == possible_cpus->len);
991 
992     for (i = 0; i < possible_cpus->len; i++) {
993         const CPUArchId *cpu_slot = &possible_cpus->cpus[i];
994 
995         if (!cpu_slot->props.has_node_id) {
996             /* fetch default mapping from board and enable it */
997             CpuInstanceProperties props = cpu_slot->props;
998 
999             props.node_id = mc->get_default_cpu_node_id(machine, i);
1000             if (!default_mapping) {
1001                 /* record slots with not set mapping,
1002                  * TODO: make it hard error in future */
1003                 char *cpu_str = cpu_slot_to_string(cpu_slot);
1004                 g_string_append_printf(s, "%sCPU %d [%s]",
1005                                        s->len ? ", " : "", i, cpu_str);
1006                 g_free(cpu_str);
1007 
1008                 /* non mapped cpus used to fallback to node 0 */
1009                 props.node_id = 0;
1010             }
1011 
1012             props.has_node_id = true;
1013             machine_set_cpu_numa_node(machine, &props, &error_fatal);
1014         }
1015     }
1016     if (s->len && !qtest_enabled()) {
1017         warn_report("CPU(s) not present in any NUMA nodes: %s",
1018                     s->str);
1019         warn_report("All CPU(s) up to maxcpus should be described "
1020                     "in NUMA config, ability to start up with partial NUMA "
1021                     "mappings is obsoleted and will be removed in future");
1022     }
1023     g_string_free(s, true);
1024 }
1025 
1026 void machine_run_board_init(MachineState *machine)
1027 {
1028     MachineClass *machine_class = MACHINE_GET_CLASS(machine);
1029 
1030     if (machine_class->numa_mem_supported) {
1031         numa_complete_configuration(machine);
1032         if (machine->numa_state->num_nodes) {
1033             machine_numa_finish_cpu_init(machine);
1034         }
1035     }
1036 
1037     /* If the machine supports the valid_cpu_types check and the user
1038      * specified a CPU with -cpu check here that the user CPU is supported.
1039      */
1040     if (machine_class->valid_cpu_types && machine->cpu_type) {
1041         ObjectClass *class = object_class_by_name(machine->cpu_type);
1042         int i;
1043 
1044         for (i = 0; machine_class->valid_cpu_types[i]; i++) {
1045             if (object_class_dynamic_cast(class,
1046                                           machine_class->valid_cpu_types[i])) {
1047                 /* The user specificed CPU is in the valid field, we are
1048                  * good to go.
1049                  */
1050                 break;
1051             }
1052         }
1053 
1054         if (!machine_class->valid_cpu_types[i]) {
1055             /* The user specified CPU is not valid */
1056             error_report("Invalid CPU type: %s", machine->cpu_type);
1057             error_printf("The valid types are: %s",
1058                          machine_class->valid_cpu_types[0]);
1059             for (i = 1; machine_class->valid_cpu_types[i]; i++) {
1060                 error_printf(", %s", machine_class->valid_cpu_types[i]);
1061             }
1062             error_printf("\n");
1063 
1064             exit(1);
1065         }
1066     }
1067 
1068     machine_class->init(machine);
1069 }
1070 
1071 static const TypeInfo machine_info = {
1072     .name = TYPE_MACHINE,
1073     .parent = TYPE_OBJECT,
1074     .abstract = true,
1075     .class_size = sizeof(MachineClass),
1076     .class_init    = machine_class_init,
1077     .class_base_init = machine_class_base_init,
1078     .instance_size = sizeof(MachineState),
1079     .instance_init = machine_initfn,
1080     .instance_finalize = machine_finalize,
1081 };
1082 
1083 static void machine_register_types(void)
1084 {
1085     type_register_static(&machine_info);
1086 }
1087 
1088 type_init(machine_register_types)
1089