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