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