xref: /openbmc/qemu/hw/hyperv/hyperv_testdev.c (revision c76c86fb)
1 /*
2  * QEMU KVM Hyper-V test device to support Hyper-V kvm-unit-tests
3  *
4  * Copyright (C) 2015 Andrey Smetanin <asmetanin@virtuozzo.com>
5  *
6  * Authors:
7  *  Andrey Smetanin <asmetanin@virtuozzo.com>
8  *
9  * This work is licensed under the terms of the GNU GPL, version 2 or later.
10  * See the COPYING file in the top-level directory.
11  *
12  */
13 
14 #include "qemu/osdep.h"
15 #include "qemu/main-loop.h"
16 #include "qemu/module.h"
17 #include "qemu/queue.h"
18 #include "hw/isa/isa.h"
19 #include "hw/hyperv/hyperv.h"
20 #include "qom/object.h"
21 
22 typedef struct TestSintRoute {
23     QLIST_ENTRY(TestSintRoute) le;
24     uint8_t vp_index;
25     uint8_t sint;
26     HvSintRoute *sint_route;
27 } TestSintRoute;
28 
29 typedef struct TestMsgConn {
30     QLIST_ENTRY(TestMsgConn) le;
31     uint8_t conn_id;
32     HvSintRoute *sint_route;
33     struct hyperv_message msg;
34 } TestMsgConn;
35 
36 typedef struct TestEvtConn {
37     QLIST_ENTRY(TestEvtConn) le;
38     uint8_t conn_id;
39     HvSintRoute *sint_route;
40     EventNotifier notifier;
41 } TestEvtConn;
42 
43 struct HypervTestDev {
44     ISADevice parent_obj;
45     MemoryRegion sint_control;
46     QLIST_HEAD(, TestSintRoute) sint_routes;
47     QLIST_HEAD(, TestMsgConn) msg_conns;
48     QLIST_HEAD(, TestEvtConn) evt_conns;
49 };
50 
51 #define TYPE_HYPERV_TEST_DEV "hyperv-testdev"
52 OBJECT_DECLARE_SIMPLE_TYPE(HypervTestDev, HYPERV_TEST_DEV)
53 
54 enum {
55     HV_TEST_DEV_SINT_ROUTE_CREATE = 1,
56     HV_TEST_DEV_SINT_ROUTE_DESTROY,
57     HV_TEST_DEV_SINT_ROUTE_SET_SINT,
58     HV_TEST_DEV_MSG_CONN_CREATE,
59     HV_TEST_DEV_MSG_CONN_DESTROY,
60     HV_TEST_DEV_EVT_CONN_CREATE,
61     HV_TEST_DEV_EVT_CONN_DESTROY,
62 };
63 
64 static void sint_route_create(HypervTestDev *dev,
65                               uint8_t vp_index, uint8_t sint)
66 {
67     TestSintRoute *sint_route;
68 
69     sint_route = g_new0(TestSintRoute, 1);
70     assert(sint_route);
71 
72     sint_route->vp_index = vp_index;
73     sint_route->sint = sint;
74 
75     sint_route->sint_route = hyperv_sint_route_new(vp_index, sint, NULL, NULL);
76     assert(sint_route->sint_route);
77 
78     QLIST_INSERT_HEAD(&dev->sint_routes, sint_route, le);
79 }
80 
81 static TestSintRoute *sint_route_find(HypervTestDev *dev,
82                                       uint8_t vp_index, uint8_t sint)
83 {
84     TestSintRoute *sint_route;
85 
86     QLIST_FOREACH(sint_route, &dev->sint_routes, le) {
87         if (sint_route->vp_index == vp_index && sint_route->sint == sint) {
88             return sint_route;
89         }
90     }
91     g_assert_not_reached();
92 }
93 
94 static void sint_route_destroy(HypervTestDev *dev,
95                                uint8_t vp_index, uint8_t sint)
96 {
97     TestSintRoute *sint_route;
98 
99     sint_route = sint_route_find(dev, vp_index, sint);
100     QLIST_REMOVE(sint_route, le);
101     hyperv_sint_route_unref(sint_route->sint_route);
102     g_free(sint_route);
103 }
104 
105 static void sint_route_set_sint(HypervTestDev *dev,
106                                 uint8_t vp_index, uint8_t sint)
107 {
108     TestSintRoute *sint_route;
109 
110     sint_route = sint_route_find(dev, vp_index, sint);
111 
112     hyperv_sint_route_set_sint(sint_route->sint_route);
113 }
114 
115 static void msg_retry(void *opaque)
116 {
117     TestMsgConn *conn = opaque;
118     assert(!hyperv_post_msg(conn->sint_route, &conn->msg));
119 }
120 
121 static void msg_cb(void *data, int status)
122 {
123     TestMsgConn *conn = data;
124 
125     if (!status) {
126         return;
127     }
128 
129     assert(status == -EAGAIN);
130 
131     aio_bh_schedule_oneshot(qemu_get_aio_context(), msg_retry, conn);
132 }
133 
134 static uint16_t msg_handler(const struct hyperv_post_message_input *msg,
135                             void *data)
136 {
137     int ret;
138     TestMsgConn *conn = data;
139 
140     /* post the same message we've got */
141     conn->msg.header.message_type = msg->message_type;
142     assert(msg->payload_size < sizeof(conn->msg.payload));
143     conn->msg.header.payload_size = msg->payload_size;
144     memcpy(&conn->msg.payload, msg->payload, msg->payload_size);
145 
146     ret = hyperv_post_msg(conn->sint_route, &conn->msg);
147 
148     switch (ret) {
149     case 0:
150         return HV_STATUS_SUCCESS;
151     case -EAGAIN:
152         return HV_STATUS_INSUFFICIENT_BUFFERS;
153     default:
154         return HV_STATUS_INVALID_HYPERCALL_INPUT;
155     }
156 }
157 
158 static void msg_conn_create(HypervTestDev *dev, uint8_t vp_index,
159                             uint8_t sint, uint8_t conn_id)
160 {
161     TestMsgConn *conn;
162 
163     conn = g_new0(TestMsgConn, 1);
164     assert(conn);
165 
166     conn->conn_id = conn_id;
167 
168     conn->sint_route = hyperv_sint_route_new(vp_index, sint, msg_cb, conn);
169     assert(conn->sint_route);
170 
171     assert(!hyperv_set_msg_handler(conn->conn_id, msg_handler, conn));
172 
173     QLIST_INSERT_HEAD(&dev->msg_conns, conn, le);
174 }
175 
176 static void msg_conn_destroy(HypervTestDev *dev, uint8_t conn_id)
177 {
178     TestMsgConn *conn;
179 
180     QLIST_FOREACH(conn, &dev->msg_conns, le) {
181         if (conn->conn_id == conn_id) {
182             QLIST_REMOVE(conn, le);
183             hyperv_set_msg_handler(conn->conn_id, NULL, NULL);
184             hyperv_sint_route_unref(conn->sint_route);
185             g_free(conn);
186             return;
187         }
188     }
189     g_assert_not_reached();
190 }
191 
192 static void evt_conn_handler(EventNotifier *notifier)
193 {
194     TestEvtConn *conn = container_of(notifier, TestEvtConn, notifier);
195 
196     event_notifier_test_and_clear(notifier);
197 
198     /* signal the same event flag we've got */
199     assert(!hyperv_set_event_flag(conn->sint_route, conn->conn_id));
200 }
201 
202 static void evt_conn_create(HypervTestDev *dev, uint8_t vp_index,
203                             uint8_t sint, uint8_t conn_id)
204 {
205     TestEvtConn *conn;
206 
207     conn = g_new0(TestEvtConn, 1);
208     assert(conn);
209 
210     conn->conn_id = conn_id;
211 
212     conn->sint_route = hyperv_sint_route_new(vp_index, sint, NULL, NULL);
213     assert(conn->sint_route);
214 
215     assert(!event_notifier_init(&conn->notifier, false));
216 
217     event_notifier_set_handler(&conn->notifier, evt_conn_handler);
218 
219     assert(!hyperv_set_event_flag_handler(conn_id, &conn->notifier));
220 
221     QLIST_INSERT_HEAD(&dev->evt_conns, conn, le);
222 }
223 
224 static void evt_conn_destroy(HypervTestDev *dev, uint8_t conn_id)
225 {
226     TestEvtConn *conn;
227 
228     QLIST_FOREACH(conn, &dev->evt_conns, le) {
229         if (conn->conn_id == conn_id) {
230             QLIST_REMOVE(conn, le);
231             hyperv_set_event_flag_handler(conn->conn_id, NULL);
232             event_notifier_set_handler(&conn->notifier, NULL);
233             event_notifier_cleanup(&conn->notifier);
234             hyperv_sint_route_unref(conn->sint_route);
235             g_free(conn);
236             return;
237         }
238     }
239     g_assert_not_reached();
240 }
241 
242 static uint64_t hv_test_dev_read(void *opaque, hwaddr addr, unsigned size)
243 {
244     return 0;
245 }
246 
247 static void hv_test_dev_write(void *opaque, hwaddr addr, uint64_t data,
248                                 uint32_t len)
249 {
250     HypervTestDev *dev = HYPERV_TEST_DEV(opaque);
251     uint8_t sint = data & 0xFF;
252     uint8_t vp_index = (data >> 8ULL) & 0xFF;
253     uint8_t ctl = (data >> 16ULL) & 0xFF;
254     uint8_t conn_id = (data >> 24ULL) & 0xFF;
255 
256     switch (ctl) {
257     case HV_TEST_DEV_SINT_ROUTE_CREATE:
258         sint_route_create(dev, vp_index, sint);
259         break;
260     case HV_TEST_DEV_SINT_ROUTE_DESTROY:
261         sint_route_destroy(dev, vp_index, sint);
262         break;
263     case HV_TEST_DEV_SINT_ROUTE_SET_SINT:
264         sint_route_set_sint(dev, vp_index, sint);
265         break;
266     case HV_TEST_DEV_MSG_CONN_CREATE:
267         msg_conn_create(dev, vp_index, sint, conn_id);
268         break;
269     case HV_TEST_DEV_MSG_CONN_DESTROY:
270         msg_conn_destroy(dev, conn_id);
271         break;
272     case HV_TEST_DEV_EVT_CONN_CREATE:
273         evt_conn_create(dev, vp_index, sint, conn_id);
274         break;
275     case HV_TEST_DEV_EVT_CONN_DESTROY:
276         evt_conn_destroy(dev, conn_id);
277         break;
278     default:
279         break;
280     }
281 }
282 
283 static const MemoryRegionOps synic_test_sint_ops = {
284     .read = hv_test_dev_read,
285     .write = hv_test_dev_write,
286     .valid.min_access_size = 4,
287     .valid.max_access_size = 4,
288     .endianness = DEVICE_LITTLE_ENDIAN,
289 };
290 
291 static void hv_test_dev_realizefn(DeviceState *d, Error **errp)
292 {
293     ISADevice *isa = ISA_DEVICE(d);
294     HypervTestDev *dev = HYPERV_TEST_DEV(d);
295     MemoryRegion *io = isa_address_space_io(isa);
296 
297     QLIST_INIT(&dev->sint_routes);
298     QLIST_INIT(&dev->msg_conns);
299     QLIST_INIT(&dev->evt_conns);
300     memory_region_init_io(&dev->sint_control, OBJECT(dev),
301                           &synic_test_sint_ops, dev,
302                           "hyperv-testdev-ctl", 4);
303     memory_region_add_subregion(io, 0x3000, &dev->sint_control);
304 }
305 
306 static void hv_test_dev_class_init(ObjectClass *klass, void *data)
307 {
308     DeviceClass *dc = DEVICE_CLASS(klass);
309 
310     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
311     dc->realize = hv_test_dev_realizefn;
312 }
313 
314 static const TypeInfo hv_test_dev_info = {
315     .name           = TYPE_HYPERV_TEST_DEV,
316     .parent         = TYPE_ISA_DEVICE,
317     .instance_size  = sizeof(HypervTestDev),
318     .class_init     = hv_test_dev_class_init,
319 };
320 
321 static void hv_test_dev_register_types(void)
322 {
323     type_register_static(&hv_test_dev_info);
324 }
325 type_init(hv_test_dev_register_types);
326