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