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