xref: /openbmc/qemu/hw/misc/vmcoreinfo.c (revision 135b03cb)
1 /*
2  * Virtual Machine coreinfo device
3  *
4  * Copyright (C) 2017 Red Hat, Inc.
5  *
6  * Authors: Marc-André Lureau <marcandre.lureau@redhat.com>
7  *
8  * This work is licensed under the terms of the GNU GPL, version 2 or later.
9  * See the COPYING file in the top-level directory.
10  *
11  */
12 
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/module.h"
16 #include "sysemu/reset.h"
17 #include "hw/nvram/fw_cfg.h"
18 #include "migration/vmstate.h"
19 #include "hw/misc/vmcoreinfo.h"
20 
21 static void fw_cfg_vmci_write(void *dev, off_t offset, size_t len)
22 {
23     VMCoreInfoState *s = VMCOREINFO(dev);
24 
25     s->has_vmcoreinfo = offset == 0 && len == sizeof(s->vmcoreinfo)
26         && s->vmcoreinfo.guest_format != FW_CFG_VMCOREINFO_FORMAT_NONE;
27 }
28 
29 static void vmcoreinfo_reset(void *dev)
30 {
31     VMCoreInfoState *s = VMCOREINFO(dev);
32 
33     s->has_vmcoreinfo = false;
34     memset(&s->vmcoreinfo, 0, sizeof(s->vmcoreinfo));
35     s->vmcoreinfo.host_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF);
36 }
37 
38 static void vmcoreinfo_realize(DeviceState *dev, Error **errp)
39 {
40     VMCoreInfoState *s = VMCOREINFO(dev);
41     FWCfgState *fw_cfg = fw_cfg_find();
42     /* for gdb script dump-guest-memory.py */
43     static VMCoreInfoState * volatile vmcoreinfo_state G_GNUC_UNUSED;
44 
45     /* Given that this function is executing, there is at least one VMCOREINFO
46      * device. Check if there are several.
47      */
48     if (!vmcoreinfo_find()) {
49         error_setg(errp, "at most one %s device is permitted",
50                    VMCOREINFO_DEVICE);
51         return;
52     }
53 
54     if (!fw_cfg || !fw_cfg->dma_enabled) {
55         error_setg(errp, "%s device requires fw_cfg with DMA",
56                    VMCOREINFO_DEVICE);
57         return;
58     }
59 
60     fw_cfg_add_file_callback(fw_cfg, FW_CFG_VMCOREINFO_FILENAME,
61                              NULL, fw_cfg_vmci_write, s,
62                              &s->vmcoreinfo, sizeof(s->vmcoreinfo), false);
63 
64     qemu_register_reset(vmcoreinfo_reset, dev);
65     vmcoreinfo_state = s;
66 }
67 
68 static const VMStateDescription vmstate_vmcoreinfo = {
69     .name = "vmcoreinfo",
70     .version_id = 1,
71     .minimum_version_id = 1,
72     .fields = (VMStateField[]) {
73         VMSTATE_BOOL(has_vmcoreinfo, VMCoreInfoState),
74         VMSTATE_UINT16(vmcoreinfo.host_format, VMCoreInfoState),
75         VMSTATE_UINT16(vmcoreinfo.guest_format, VMCoreInfoState),
76         VMSTATE_UINT32(vmcoreinfo.size, VMCoreInfoState),
77         VMSTATE_UINT64(vmcoreinfo.paddr, VMCoreInfoState),
78         VMSTATE_END_OF_LIST()
79     },
80 };
81 
82 static void vmcoreinfo_device_class_init(ObjectClass *klass, void *data)
83 {
84     DeviceClass *dc = DEVICE_CLASS(klass);
85 
86     dc->vmsd = &vmstate_vmcoreinfo;
87     dc->realize = vmcoreinfo_realize;
88     dc->hotpluggable = false;
89     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
90 }
91 
92 static const TypeInfo vmcoreinfo_device_info = {
93     .name          = VMCOREINFO_DEVICE,
94     .parent        = TYPE_DEVICE,
95     .instance_size = sizeof(VMCoreInfoState),
96     .class_init    = vmcoreinfo_device_class_init,
97 };
98 
99 static void vmcoreinfo_register_types(void)
100 {
101     type_register_static(&vmcoreinfo_device_info);
102 }
103 
104 type_init(vmcoreinfo_register_types)
105