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