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