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