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