pvpanic.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. /*
  2. * QEMU simulated pvpanic device.
  3. *
  4. * Copyright Fujitsu, Corp. 2013
  5. *
  6. * Authors:
  7. * Wen Congyang <wency@cn.fujitsu.com>
  8. * Hu Tao <hutao@cn.fujitsu.com>
  9. *
  10. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  11. * See the COPYING file in the top-level directory.
  12. *
  13. */
  14. #include "qemu/osdep.h"
  15. #include "qemu/log.h"
  16. #include "qemu/module.h"
  17. #include "sysemu/runstate.h"
  18. #include "hw/nvram/fw_cfg.h"
  19. #include "hw/qdev-properties.h"
  20. #include "hw/misc/pvpanic.h"
  21. #include "qom/object.h"
  22. /* The bit of supported pv event, TODO: include uapi header and remove this */
  23. #define PVPANIC_F_PANICKED 0
  24. #define PVPANIC_F_CRASHLOADED 1
  25. /* The pv event value */
  26. #define PVPANIC_PANICKED (1 << PVPANIC_F_PANICKED)
  27. #define PVPANIC_CRASHLOADED (1 << PVPANIC_F_CRASHLOADED)
  28. typedef struct PVPanicState PVPanicState;
  29. DECLARE_INSTANCE_CHECKER(PVPanicState, ISA_PVPANIC_DEVICE,
  30. TYPE_PVPANIC)
  31. static void handle_event(int event)
  32. {
  33. static bool logged;
  34. if (event & ~(PVPANIC_PANICKED | PVPANIC_CRASHLOADED) && !logged) {
  35. qemu_log_mask(LOG_GUEST_ERROR, "pvpanic: unknown event %#x.\n", event);
  36. logged = true;
  37. }
  38. if (event & PVPANIC_PANICKED) {
  39. qemu_system_guest_panicked(NULL);
  40. return;
  41. }
  42. if (event & PVPANIC_CRASHLOADED) {
  43. qemu_system_guest_crashloaded(NULL);
  44. return;
  45. }
  46. }
  47. #include "hw/isa/isa.h"
  48. struct PVPanicState {
  49. ISADevice parent_obj;
  50. MemoryRegion io;
  51. uint16_t ioport;
  52. };
  53. /* return supported events on read */
  54. static uint64_t pvpanic_ioport_read(void *opaque, hwaddr addr, unsigned size)
  55. {
  56. return PVPANIC_PANICKED;
  57. }
  58. static void pvpanic_ioport_write(void *opaque, hwaddr addr, uint64_t val,
  59. unsigned size)
  60. {
  61. handle_event(val);
  62. }
  63. static const MemoryRegionOps pvpanic_ops = {
  64. .read = pvpanic_ioport_read,
  65. .write = pvpanic_ioport_write,
  66. .impl = {
  67. .min_access_size = 1,
  68. .max_access_size = 1,
  69. },
  70. };
  71. static void pvpanic_isa_initfn(Object *obj)
  72. {
  73. PVPanicState *s = ISA_PVPANIC_DEVICE(obj);
  74. memory_region_init_io(&s->io, OBJECT(s), &pvpanic_ops, s, "pvpanic", 1);
  75. }
  76. static void pvpanic_isa_realizefn(DeviceState *dev, Error **errp)
  77. {
  78. ISADevice *d = ISA_DEVICE(dev);
  79. PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
  80. FWCfgState *fw_cfg = fw_cfg_find();
  81. uint16_t *pvpanic_port;
  82. if (!fw_cfg) {
  83. return;
  84. }
  85. pvpanic_port = g_malloc(sizeof(*pvpanic_port));
  86. *pvpanic_port = cpu_to_le16(s->ioport);
  87. fw_cfg_add_file(fw_cfg, "etc/pvpanic-port", pvpanic_port,
  88. sizeof(*pvpanic_port));
  89. isa_register_ioport(d, &s->io, s->ioport);
  90. }
  91. static Property pvpanic_isa_properties[] = {
  92. DEFINE_PROP_UINT16(PVPANIC_IOPORT_PROP, PVPanicState, ioport, 0x505),
  93. DEFINE_PROP_END_OF_LIST(),
  94. };
  95. static void pvpanic_isa_class_init(ObjectClass *klass, void *data)
  96. {
  97. DeviceClass *dc = DEVICE_CLASS(klass);
  98. dc->realize = pvpanic_isa_realizefn;
  99. device_class_set_props(dc, pvpanic_isa_properties);
  100. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  101. }
  102. static TypeInfo pvpanic_isa_info = {
  103. .name = TYPE_PVPANIC,
  104. .parent = TYPE_ISA_DEVICE,
  105. .instance_size = sizeof(PVPanicState),
  106. .instance_init = pvpanic_isa_initfn,
  107. .class_init = pvpanic_isa_class_init,
  108. };
  109. static void pvpanic_register_types(void)
  110. {
  111. type_register_static(&pvpanic_isa_info);
  112. }
  113. type_init(pvpanic_register_types)