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