xref: /openbmc/qemu/hw/core/machine.c (revision 77a8257e)
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 "hw/boards.h"
14 #include "qapi/visitor.h"
15 #include "hw/sysbus.h"
16 #include "sysemu/sysemu.h"
17 #include "qemu/error-report.h"
18 
19 static char *machine_get_accel(Object *obj, Error **errp)
20 {
21     MachineState *ms = MACHINE(obj);
22 
23     return g_strdup(ms->accel);
24 }
25 
26 static void machine_set_accel(Object *obj, const char *value, Error **errp)
27 {
28     MachineState *ms = MACHINE(obj);
29 
30     g_free(ms->accel);
31     ms->accel = g_strdup(value);
32 }
33 
34 static void machine_set_kernel_irqchip(Object *obj, bool value, Error **errp)
35 {
36     MachineState *ms = MACHINE(obj);
37 
38     ms->kernel_irqchip_allowed = value;
39     ms->kernel_irqchip_required = value;
40 }
41 
42 static void machine_get_kvm_shadow_mem(Object *obj, Visitor *v,
43                                        void *opaque, const char *name,
44                                        Error **errp)
45 {
46     MachineState *ms = MACHINE(obj);
47     int64_t value = ms->kvm_shadow_mem;
48 
49     visit_type_int(v, &value, name, errp);
50 }
51 
52 static void machine_set_kvm_shadow_mem(Object *obj, Visitor *v,
53                                        void *opaque, const char *name,
54                                        Error **errp)
55 {
56     MachineState *ms = MACHINE(obj);
57     Error *error = NULL;
58     int64_t value;
59 
60     visit_type_int(v, &value, name, &error);
61     if (error) {
62         error_propagate(errp, error);
63         return;
64     }
65 
66     ms->kvm_shadow_mem = value;
67 }
68 
69 static char *machine_get_kernel(Object *obj, Error **errp)
70 {
71     MachineState *ms = MACHINE(obj);
72 
73     return g_strdup(ms->kernel_filename);
74 }
75 
76 static void machine_set_kernel(Object *obj, const char *value, Error **errp)
77 {
78     MachineState *ms = MACHINE(obj);
79 
80     g_free(ms->kernel_filename);
81     ms->kernel_filename = g_strdup(value);
82 }
83 
84 static char *machine_get_initrd(Object *obj, Error **errp)
85 {
86     MachineState *ms = MACHINE(obj);
87 
88     return g_strdup(ms->initrd_filename);
89 }
90 
91 static void machine_set_initrd(Object *obj, const char *value, Error **errp)
92 {
93     MachineState *ms = MACHINE(obj);
94 
95     g_free(ms->initrd_filename);
96     ms->initrd_filename = g_strdup(value);
97 }
98 
99 static char *machine_get_append(Object *obj, Error **errp)
100 {
101     MachineState *ms = MACHINE(obj);
102 
103     return g_strdup(ms->kernel_cmdline);
104 }
105 
106 static void machine_set_append(Object *obj, const char *value, Error **errp)
107 {
108     MachineState *ms = MACHINE(obj);
109 
110     g_free(ms->kernel_cmdline);
111     ms->kernel_cmdline = g_strdup(value);
112 }
113 
114 static char *machine_get_dtb(Object *obj, Error **errp)
115 {
116     MachineState *ms = MACHINE(obj);
117 
118     return g_strdup(ms->dtb);
119 }
120 
121 static void machine_set_dtb(Object *obj, const char *value, Error **errp)
122 {
123     MachineState *ms = MACHINE(obj);
124 
125     g_free(ms->dtb);
126     ms->dtb = g_strdup(value);
127 }
128 
129 static char *machine_get_dumpdtb(Object *obj, Error **errp)
130 {
131     MachineState *ms = MACHINE(obj);
132 
133     return g_strdup(ms->dumpdtb);
134 }
135 
136 static void machine_set_dumpdtb(Object *obj, const char *value, Error **errp)
137 {
138     MachineState *ms = MACHINE(obj);
139 
140     g_free(ms->dumpdtb);
141     ms->dumpdtb = g_strdup(value);
142 }
143 
144 static void machine_get_phandle_start(Object *obj, Visitor *v,
145                                        void *opaque, const char *name,
146                                        Error **errp)
147 {
148     MachineState *ms = MACHINE(obj);
149     int64_t value = ms->phandle_start;
150 
151     visit_type_int(v, &value, name, errp);
152 }
153 
154 static void machine_set_phandle_start(Object *obj, Visitor *v,
155                                        void *opaque, const char *name,
156                                        Error **errp)
157 {
158     MachineState *ms = MACHINE(obj);
159     Error *error = NULL;
160     int64_t value;
161 
162     visit_type_int(v, &value, name, &error);
163     if (error) {
164         error_propagate(errp, error);
165         return;
166     }
167 
168     ms->phandle_start = value;
169 }
170 
171 static char *machine_get_dt_compatible(Object *obj, Error **errp)
172 {
173     MachineState *ms = MACHINE(obj);
174 
175     return g_strdup(ms->dt_compatible);
176 }
177 
178 static void machine_set_dt_compatible(Object *obj, const char *value, Error **errp)
179 {
180     MachineState *ms = MACHINE(obj);
181 
182     g_free(ms->dt_compatible);
183     ms->dt_compatible = g_strdup(value);
184 }
185 
186 static bool machine_get_dump_guest_core(Object *obj, Error **errp)
187 {
188     MachineState *ms = MACHINE(obj);
189 
190     return ms->dump_guest_core;
191 }
192 
193 static void machine_set_dump_guest_core(Object *obj, bool value, Error **errp)
194 {
195     MachineState *ms = MACHINE(obj);
196 
197     ms->dump_guest_core = value;
198 }
199 
200 static bool machine_get_mem_merge(Object *obj, Error **errp)
201 {
202     MachineState *ms = MACHINE(obj);
203 
204     return ms->mem_merge;
205 }
206 
207 static void machine_set_mem_merge(Object *obj, bool value, Error **errp)
208 {
209     MachineState *ms = MACHINE(obj);
210 
211     ms->mem_merge = value;
212 }
213 
214 static bool machine_get_usb(Object *obj, Error **errp)
215 {
216     MachineState *ms = MACHINE(obj);
217 
218     return ms->usb;
219 }
220 
221 static void machine_set_usb(Object *obj, bool value, Error **errp)
222 {
223     MachineState *ms = MACHINE(obj);
224 
225     ms->usb = value;
226 }
227 
228 static char *machine_get_firmware(Object *obj, Error **errp)
229 {
230     MachineState *ms = MACHINE(obj);
231 
232     return g_strdup(ms->firmware);
233 }
234 
235 static void machine_set_firmware(Object *obj, const char *value, Error **errp)
236 {
237     MachineState *ms = MACHINE(obj);
238 
239     g_free(ms->firmware);
240     ms->firmware = g_strdup(value);
241 }
242 
243 static bool machine_get_iommu(Object *obj, Error **errp)
244 {
245     MachineState *ms = MACHINE(obj);
246 
247     return ms->iommu;
248 }
249 
250 static void machine_set_iommu(Object *obj, bool value, Error **errp)
251 {
252     MachineState *ms = MACHINE(obj);
253 
254     ms->iommu = value;
255 }
256 
257 static void machine_set_suppress_vmdesc(Object *obj, bool value, Error **errp)
258 {
259     MachineState *ms = MACHINE(obj);
260 
261     ms->suppress_vmdesc = value;
262 }
263 
264 static bool machine_get_suppress_vmdesc(Object *obj, Error **errp)
265 {
266     MachineState *ms = MACHINE(obj);
267 
268     return ms->suppress_vmdesc;
269 }
270 
271 static int error_on_sysbus_device(SysBusDevice *sbdev, void *opaque)
272 {
273     error_report("Option '-device %s' cannot be handled by this machine",
274                  object_class_get_name(object_get_class(OBJECT(sbdev))));
275     exit(1);
276 }
277 
278 static void machine_init_notify(Notifier *notifier, void *data)
279 {
280     Object *machine = qdev_get_machine();
281     ObjectClass *oc = object_get_class(machine);
282     MachineClass *mc = MACHINE_CLASS(oc);
283 
284     if (mc->has_dynamic_sysbus) {
285         /* Our machine can handle dynamic sysbus devices, we're all good */
286         return;
287     }
288 
289     /*
290      * Loop through all dynamically created devices and check whether there
291      * are sysbus devices among them. If there are, error out.
292      */
293     foreach_dynamic_sysbus_device(error_on_sysbus_device, NULL);
294 }
295 
296 static void machine_initfn(Object *obj)
297 {
298     MachineState *ms = MACHINE(obj);
299 
300     ms->kernel_irqchip_allowed = true;
301     ms->kvm_shadow_mem = -1;
302     ms->dump_guest_core = true;
303     ms->mem_merge = true;
304 
305     object_property_add_str(obj, "accel",
306                             machine_get_accel, machine_set_accel, NULL);
307     object_property_set_description(obj, "accel",
308                                     "Accelerator list",
309                                     NULL);
310     object_property_add_bool(obj, "kernel-irqchip",
311                              NULL,
312                              machine_set_kernel_irqchip,
313                              NULL);
314     object_property_set_description(obj, "kernel-irqchip",
315                                     "Use KVM in-kernel irqchip",
316                                     NULL);
317     object_property_add(obj, "kvm-shadow-mem", "int",
318                         machine_get_kvm_shadow_mem,
319                         machine_set_kvm_shadow_mem,
320                         NULL, NULL, NULL);
321     object_property_set_description(obj, "kvm-shadow-mem",
322                                     "KVM shadow MMU size",
323                                     NULL);
324     object_property_add_str(obj, "kernel",
325                             machine_get_kernel, machine_set_kernel, NULL);
326     object_property_set_description(obj, "kernel",
327                                     "Linux kernel image file",
328                                     NULL);
329     object_property_add_str(obj, "initrd",
330                             machine_get_initrd, machine_set_initrd, NULL);
331     object_property_set_description(obj, "initrd",
332                                     "Linux initial ramdisk file",
333                                     NULL);
334     object_property_add_str(obj, "append",
335                             machine_get_append, machine_set_append, NULL);
336     object_property_set_description(obj, "append",
337                                     "Linux kernel command line",
338                                     NULL);
339     object_property_add_str(obj, "dtb",
340                             machine_get_dtb, machine_set_dtb, NULL);
341     object_property_set_description(obj, "dtb",
342                                     "Linux kernel device tree file",
343                                     NULL);
344     object_property_add_str(obj, "dumpdtb",
345                             machine_get_dumpdtb, machine_set_dumpdtb, NULL);
346     object_property_set_description(obj, "dumpdtb",
347                                     "Dump current dtb to a file and quit",
348                                     NULL);
349     object_property_add(obj, "phandle-start", "int",
350                         machine_get_phandle_start,
351                         machine_set_phandle_start,
352                         NULL, NULL, NULL);
353     object_property_set_description(obj, "phandle-start",
354                                     "The first phandle ID we may generate dynamically",
355                                     NULL);
356     object_property_add_str(obj, "dt-compatible",
357                             machine_get_dt_compatible,
358                             machine_set_dt_compatible,
359                             NULL);
360     object_property_set_description(obj, "dt-compatible",
361                                     "Overrides the \"compatible\" property of the dt root node",
362                                     NULL);
363     object_property_add_bool(obj, "dump-guest-core",
364                              machine_get_dump_guest_core,
365                              machine_set_dump_guest_core,
366                              NULL);
367     object_property_set_description(obj, "dump-guest-core",
368                                     "Include guest memory in  a core dump",
369                                     NULL);
370     object_property_add_bool(obj, "mem-merge",
371                              machine_get_mem_merge,
372                              machine_set_mem_merge, NULL);
373     object_property_set_description(obj, "mem-merge",
374                                     "Enable/disable memory merge support",
375                                     NULL);
376     object_property_add_bool(obj, "usb",
377                              machine_get_usb,
378                              machine_set_usb, NULL);
379     object_property_set_description(obj, "usb",
380                                     "Set on/off to enable/disable usb",
381                                     NULL);
382     object_property_add_str(obj, "firmware",
383                             machine_get_firmware,
384                             machine_set_firmware, NULL);
385     object_property_set_description(obj, "firmware",
386                                     "Firmware image",
387                                     NULL);
388     object_property_add_bool(obj, "iommu",
389                              machine_get_iommu,
390                              machine_set_iommu, NULL);
391     object_property_set_description(obj, "iommu",
392                                     "Set on/off to enable/disable Intel IOMMU (VT-d)",
393                                     NULL);
394     object_property_add_bool(obj, "suppress-vmdesc",
395                              machine_get_suppress_vmdesc,
396                              machine_set_suppress_vmdesc, NULL);
397     object_property_set_description(obj, "suppress-vmdesc",
398                                     "Set on to disable self-describing migration",
399                                     NULL);
400 
401     /* Register notifier when init is done for sysbus sanity checks */
402     ms->sysbus_notifier.notify = machine_init_notify;
403     qemu_add_machine_init_done_notifier(&ms->sysbus_notifier);
404 }
405 
406 static void machine_finalize(Object *obj)
407 {
408     MachineState *ms = MACHINE(obj);
409 
410     g_free(ms->accel);
411     g_free(ms->kernel_filename);
412     g_free(ms->initrd_filename);
413     g_free(ms->kernel_cmdline);
414     g_free(ms->dtb);
415     g_free(ms->dumpdtb);
416     g_free(ms->dt_compatible);
417     g_free(ms->firmware);
418 }
419 
420 bool machine_usb(MachineState *machine)
421 {
422     return machine->usb;
423 }
424 
425 bool machine_iommu(MachineState *machine)
426 {
427     return machine->iommu;
428 }
429 
430 bool machine_kernel_irqchip_allowed(MachineState *machine)
431 {
432     return machine->kernel_irqchip_allowed;
433 }
434 
435 bool machine_kernel_irqchip_required(MachineState *machine)
436 {
437     return machine->kernel_irqchip_required;
438 }
439 
440 int machine_kvm_shadow_mem(MachineState *machine)
441 {
442     return machine->kvm_shadow_mem;
443 }
444 
445 int machine_phandle_start(MachineState *machine)
446 {
447     return machine->phandle_start;
448 }
449 
450 bool machine_dump_guest_core(MachineState *machine)
451 {
452     return machine->dump_guest_core;
453 }
454 
455 bool machine_mem_merge(MachineState *machine)
456 {
457     return machine->mem_merge;
458 }
459 
460 static const TypeInfo machine_info = {
461     .name = TYPE_MACHINE,
462     .parent = TYPE_OBJECT,
463     .abstract = true,
464     .class_size = sizeof(MachineClass),
465     .instance_size = sizeof(MachineState),
466     .instance_init = machine_initfn,
467     .instance_finalize = machine_finalize,
468 };
469 
470 static void machine_register_types(void)
471 {
472     type_register_static(&machine_info);
473 }
474 
475 type_init(machine_register_types)
476