pvpanic.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  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 "qapi/qmp/qobject.h"
  15. #include "qapi/qmp/qjson.h"
  16. #include "monitor/monitor.h"
  17. #include "sysemu/sysemu.h"
  18. #include "qemu/log.h"
  19. #include "hw/nvram/fw_cfg.h"
  20. #include "hw/i386/pc.h"
  21. /* The bit of supported pv event */
  22. #define PVPANIC_F_PANICKED 0
  23. /* The pv event value */
  24. #define PVPANIC_PANICKED (1 << PVPANIC_F_PANICKED)
  25. #define TYPE_ISA_PVPANIC_DEVICE "pvpanic"
  26. #define ISA_PVPANIC_DEVICE(obj) \
  27. OBJECT_CHECK(PVPanicState, (obj), TYPE_ISA_PVPANIC_DEVICE)
  28. static void panicked_mon_event(const char *action)
  29. {
  30. QObject *data;
  31. data = qobject_from_jsonf("{ 'action': %s }", action);
  32. monitor_protocol_event(QEVENT_GUEST_PANICKED, data);
  33. qobject_decref(data);
  34. }
  35. static void handle_event(int event)
  36. {
  37. static bool logged;
  38. if (event & ~PVPANIC_PANICKED && !logged) {
  39. qemu_log_mask(LOG_GUEST_ERROR, "pvpanic: unknown event %#x.\n", event);
  40. logged = true;
  41. }
  42. if (event & PVPANIC_PANICKED) {
  43. panicked_mon_event("pause");
  44. vm_stop(RUN_STATE_GUEST_PANICKED);
  45. return;
  46. }
  47. }
  48. #include "hw/isa/isa.h"
  49. typedef struct PVPanicState {
  50. ISADevice parent_obj;
  51. MemoryRegion io;
  52. uint16_t ioport;
  53. } PVPanicState;
  54. /* return supported events on read */
  55. static uint64_t pvpanic_ioport_read(void *opaque, hwaddr addr, unsigned size)
  56. {
  57. return PVPANIC_PANICKED;
  58. }
  59. static void pvpanic_ioport_write(void *opaque, hwaddr addr, uint64_t val,
  60. unsigned size)
  61. {
  62. handle_event(val);
  63. }
  64. static const MemoryRegionOps pvpanic_ops = {
  65. .read = pvpanic_ioport_read,
  66. .write = pvpanic_ioport_write,
  67. .impl = {
  68. .min_access_size = 1,
  69. .max_access_size = 1,
  70. },
  71. };
  72. static void pvpanic_isa_initfn(Object *obj)
  73. {
  74. PVPanicState *s = ISA_PVPANIC_DEVICE(obj);
  75. memory_region_init_io(&s->io, OBJECT(s), &pvpanic_ops, s, "pvpanic", 1);
  76. }
  77. static void pvpanic_isa_realizefn(DeviceState *dev, Error **errp)
  78. {
  79. ISADevice *d = ISA_DEVICE(dev);
  80. PVPanicState *s = ISA_PVPANIC_DEVICE(dev);
  81. FWCfgState *fw_cfg = fw_cfg_find();
  82. uint16_t *pvpanic_port;
  83. if (!fw_cfg) {
  84. return;
  85. }
  86. pvpanic_port = g_malloc(sizeof(*pvpanic_port));
  87. *pvpanic_port = cpu_to_le16(s->ioport);
  88. fw_cfg_add_file(fw_cfg, "etc/pvpanic-port", pvpanic_port,
  89. sizeof(*pvpanic_port));
  90. isa_register_ioport(d, &s->io, s->ioport);
  91. }
  92. #define PVPANIC_IOPORT_PROP "ioport"
  93. uint16_t pvpanic_port(void)
  94. {
  95. Object *o = object_resolve_path_type("", TYPE_ISA_PVPANIC_DEVICE, NULL);
  96. if (!o) {
  97. return 0;
  98. }
  99. return object_property_get_int(o, PVPANIC_IOPORT_PROP, NULL);
  100. }
  101. static Property pvpanic_isa_properties[] = {
  102. DEFINE_PROP_UINT16(PVPANIC_IOPORT_PROP, PVPanicState, ioport, 0x505),
  103. DEFINE_PROP_END_OF_LIST(),
  104. };
  105. static void pvpanic_isa_class_init(ObjectClass *klass, void *data)
  106. {
  107. DeviceClass *dc = DEVICE_CLASS(klass);
  108. dc->realize = pvpanic_isa_realizefn;
  109. dc->props = pvpanic_isa_properties;
  110. set_bit(DEVICE_CATEGORY_MISC, dc->categories);
  111. }
  112. static TypeInfo pvpanic_isa_info = {
  113. .name = TYPE_ISA_PVPANIC_DEVICE,
  114. .parent = TYPE_ISA_DEVICE,
  115. .instance_size = sizeof(PVPanicState),
  116. .instance_init = pvpanic_isa_initfn,
  117. .class_init = pvpanic_isa_class_init,
  118. };
  119. static void pvpanic_register_types(void)
  120. {
  121. type_register_static(&pvpanic_isa_info);
  122. }
  123. type_init(pvpanic_register_types)