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