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