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