xref: /openbmc/qemu/hw/misc/vmcoreinfo.c (revision 6a0acfff)
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 "hw/misc/vmcoreinfo.h"
19 
20 static void fw_cfg_vmci_write(void *dev, off_t offset, size_t len)
21 {
22     VMCoreInfoState *s = VMCOREINFO(dev);
23 
24     s->has_vmcoreinfo = offset == 0 && len == sizeof(s->vmcoreinfo)
25         && s->vmcoreinfo.guest_format != FW_CFG_VMCOREINFO_FORMAT_NONE;
26 }
27 
28 static void vmcoreinfo_reset(void *dev)
29 {
30     VMCoreInfoState *s = VMCOREINFO(dev);
31 
32     s->has_vmcoreinfo = false;
33     memset(&s->vmcoreinfo, 0, sizeof(s->vmcoreinfo));
34     s->vmcoreinfo.host_format = cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF);
35 }
36 
37 static void vmcoreinfo_realize(DeviceState *dev, Error **errp)
38 {
39     VMCoreInfoState *s = VMCOREINFO(dev);
40     FWCfgState *fw_cfg = fw_cfg_find();
41     /* for gdb script dump-guest-memory.py */
42     static VMCoreInfoState * volatile vmcoreinfo_state G_GNUC_UNUSED;
43 
44     /* Given that this function is executing, there is at least one VMCOREINFO
45      * device. Check if there are several.
46      */
47     if (!vmcoreinfo_find()) {
48         error_setg(errp, "at most one %s device is permitted",
49                    VMCOREINFO_DEVICE);
50         return;
51     }
52 
53     if (!fw_cfg || !fw_cfg->dma_enabled) {
54         error_setg(errp, "%s device requires fw_cfg with DMA",
55                    VMCOREINFO_DEVICE);
56         return;
57     }
58 
59     fw_cfg_add_file_callback(fw_cfg, FW_CFG_VMCOREINFO_FILENAME,
60                              NULL, fw_cfg_vmci_write, s,
61                              &s->vmcoreinfo, sizeof(s->vmcoreinfo), false);
62 
63     qemu_register_reset(vmcoreinfo_reset, dev);
64     vmcoreinfo_state = s;
65 }
66 
67 static const VMStateDescription vmstate_vmcoreinfo = {
68     .name = "vmcoreinfo",
69     .version_id = 1,
70     .minimum_version_id = 1,
71     .fields = (VMStateField[]) {
72         VMSTATE_BOOL(has_vmcoreinfo, VMCoreInfoState),
73         VMSTATE_UINT16(vmcoreinfo.host_format, VMCoreInfoState),
74         VMSTATE_UINT16(vmcoreinfo.guest_format, VMCoreInfoState),
75         VMSTATE_UINT32(vmcoreinfo.size, VMCoreInfoState),
76         VMSTATE_UINT64(vmcoreinfo.paddr, VMCoreInfoState),
77         VMSTATE_END_OF_LIST()
78     },
79 };
80 
81 static void vmcoreinfo_device_class_init(ObjectClass *klass, void *data)
82 {
83     DeviceClass *dc = DEVICE_CLASS(klass);
84 
85     dc->vmsd = &vmstate_vmcoreinfo;
86     dc->realize = vmcoreinfo_realize;
87     dc->hotpluggable = false;
88     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
89 }
90 
91 static const TypeInfo vmcoreinfo_device_info = {
92     .name          = VMCOREINFO_DEVICE,
93     .parent        = TYPE_DEVICE,
94     .instance_size = sizeof(VMCoreInfoState),
95     .class_init    = vmcoreinfo_device_class_init,
96 };
97 
98 static void vmcoreinfo_register_types(void)
99 {
100     type_register_static(&vmcoreinfo_device_info);
101 }
102 
103 type_init(vmcoreinfo_register_types)
104