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