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