2 * Virtual Machine coreinfo device
4 * Copyright (C) 2017 Red Hat, Inc.
6 * Authors: Marc-André Lureau <marcandre.lureau@redhat.com>
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.
13 #include "qemu/osdep.h"
14 #include "qapi/error.h"
15 #include "qemu/module.h"
16 #include "hw/nvram/fw_cfg.h"
17 #include "hw/misc/vmcoreinfo.h"
19 static void fw_cfg_vmci_write(void *dev
, off_t offset
, size_t len
)
21 VMCoreInfoState
*s
= VMCOREINFO(dev
);
23 s
->has_vmcoreinfo
= offset
== 0 && len
== sizeof(s
->vmcoreinfo
)
24 && s
->vmcoreinfo
.guest_format
!= FW_CFG_VMCOREINFO_FORMAT_NONE
;
27 static void vmcoreinfo_reset(void *dev
)
29 VMCoreInfoState
*s
= VMCOREINFO(dev
);
31 s
->has_vmcoreinfo
= false;
32 memset(&s
->vmcoreinfo
, 0, sizeof(s
->vmcoreinfo
));
33 s
->vmcoreinfo
.host_format
= cpu_to_le16(FW_CFG_VMCOREINFO_FORMAT_ELF
);
36 static void vmcoreinfo_realize(DeviceState
*dev
, Error
**errp
)
38 VMCoreInfoState
*s
= VMCOREINFO(dev
);
39 FWCfgState
*fw_cfg
= fw_cfg_find();
40 /* for gdb script dump-guest-memory.py */
41 static VMCoreInfoState
* volatile vmcoreinfo_state G_GNUC_UNUSED
;
43 /* Given that this function is executing, there is at least one VMCOREINFO
44 * device. Check if there are several.
46 if (!vmcoreinfo_find()) {
47 error_setg(errp
, "at most one %s device is permitted",
52 if (!fw_cfg
|| !fw_cfg
->dma_enabled
) {
53 error_setg(errp
, "%s device requires fw_cfg with DMA",
58 fw_cfg_add_file_callback(fw_cfg
, FW_CFG_VMCOREINFO_FILENAME
,
59 NULL
, fw_cfg_vmci_write
, s
,
60 &s
->vmcoreinfo
, sizeof(s
->vmcoreinfo
), false);
62 qemu_register_reset(vmcoreinfo_reset
, dev
);
66 static const VMStateDescription vmstate_vmcoreinfo
= {
69 .minimum_version_id
= 1,
70 .fields
= (VMStateField
[]) {
71 VMSTATE_BOOL(has_vmcoreinfo
, VMCoreInfoState
),
72 VMSTATE_UINT16(vmcoreinfo
.host_format
, VMCoreInfoState
),
73 VMSTATE_UINT16(vmcoreinfo
.guest_format
, VMCoreInfoState
),
74 VMSTATE_UINT32(vmcoreinfo
.size
, VMCoreInfoState
),
75 VMSTATE_UINT64(vmcoreinfo
.paddr
, VMCoreInfoState
),
80 static void vmcoreinfo_device_class_init(ObjectClass
*klass
, void *data
)
82 DeviceClass
*dc
= DEVICE_CLASS(klass
);
84 dc
->vmsd
= &vmstate_vmcoreinfo
;
85 dc
->realize
= vmcoreinfo_realize
;
86 dc
->hotpluggable
= false;
87 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
90 static const TypeInfo vmcoreinfo_device_info
= {
91 .name
= VMCOREINFO_DEVICE
,
92 .parent
= TYPE_DEVICE
,
93 .instance_size
= sizeof(VMCoreInfoState
),
94 .class_init
= vmcoreinfo_device_class_init
,
97 static void vmcoreinfo_register_types(void)
99 type_register_static(&vmcoreinfo_device_info
);
102 type_init(vmcoreinfo_register_types
)