2
0

vmcoreinfo.c 3.2 KB

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