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