1 /*
2 * libqos virtio driver
3 *
4 * Copyright (c) 2014 Marc Marí
5 *
6 * This work is licensed under the terms of the GNU GPL, version 2 or later.
7 * See the COPYING file in the top-level directory.
8 */
9
10 #include "qemu/osdep.h"
11 #include "qemu/bswap.h"
12 #include "../libqtest.h"
13 #include "virtio.h"
14 #include "standard-headers/linux/virtio_config.h"
15 #include "standard-headers/linux/virtio_ring.h"
16
17 /*
18 * qtest_readX/writeX() functions transfer host endian from/to guest endian.
19 * This works great for Legacy VIRTIO devices where we need guest endian
20 * accesses. For VIRTIO 1.0 the vring is little-endian so the automatic guest
21 * endianness conversion is not wanted.
22 *
23 * The following qvirtio_readX/writeX() functions handle Legacy and VIRTIO 1.0
24 * accesses seamlessly.
25 */
qvirtio_readw(QVirtioDevice * d,QTestState * qts,uint64_t addr)26 static uint16_t qvirtio_readw(QVirtioDevice *d, QTestState *qts, uint64_t addr)
27 {
28 uint16_t val = qtest_readw(qts, addr);
29
30 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
31 val = bswap16(val);
32 }
33 return val;
34 }
35
qvirtio_readl(QVirtioDevice * d,QTestState * qts,uint64_t addr)36 static uint32_t qvirtio_readl(QVirtioDevice *d, QTestState *qts, uint64_t addr)
37 {
38 uint32_t val = qtest_readl(qts, addr);
39
40 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
41 val = bswap32(val);
42 }
43 return val;
44 }
45
qvirtio_writew(QVirtioDevice * d,QTestState * qts,uint64_t addr,uint16_t val)46 static void qvirtio_writew(QVirtioDevice *d, QTestState *qts,
47 uint64_t addr, uint16_t val)
48 {
49 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
50 val = bswap16(val);
51 }
52 qtest_writew(qts, addr, val);
53 }
54
qvirtio_writel(QVirtioDevice * d,QTestState * qts,uint64_t addr,uint32_t val)55 static void qvirtio_writel(QVirtioDevice *d, QTestState *qts,
56 uint64_t addr, uint32_t val)
57 {
58 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
59 val = bswap32(val);
60 }
61 qtest_writel(qts, addr, val);
62 }
63
qvirtio_writeq(QVirtioDevice * d,QTestState * qts,uint64_t addr,uint64_t val)64 static void qvirtio_writeq(QVirtioDevice *d, QTestState *qts,
65 uint64_t addr, uint64_t val)
66 {
67 if (d->features & (1ull << VIRTIO_F_VERSION_1) && qtest_big_endian(qts)) {
68 val = bswap64(val);
69 }
70 qtest_writeq(qts, addr, val);
71 }
72
qvirtio_config_readb(QVirtioDevice * d,uint64_t addr)73 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
74 {
75 g_assert_true(d->features_negotiated);
76 return d->bus->config_readb(d, addr);
77 }
78
qvirtio_config_readw(QVirtioDevice * d,uint64_t addr)79 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr)
80 {
81 g_assert_true(d->features_negotiated);
82 return d->bus->config_readw(d, addr);
83 }
84
qvirtio_config_readl(QVirtioDevice * d,uint64_t addr)85 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr)
86 {
87 g_assert_true(d->features_negotiated);
88 return d->bus->config_readl(d, addr);
89 }
90
qvirtio_config_readq(QVirtioDevice * d,uint64_t addr)91 uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr)
92 {
93 g_assert_true(d->features_negotiated);
94 return d->bus->config_readq(d, addr);
95 }
96
qvirtio_get_features(QVirtioDevice * d)97 uint64_t qvirtio_get_features(QVirtioDevice *d)
98 {
99 return d->bus->get_features(d);
100 }
101
qvirtio_set_features(QVirtioDevice * d,uint64_t features)102 void qvirtio_set_features(QVirtioDevice *d, uint64_t features)
103 {
104 g_assert(!(features & QVIRTIO_F_BAD_FEATURE));
105
106 d->features = features;
107 d->bus->set_features(d, features);
108
109 /*
110 * This could be a separate function for drivers that want to access
111 * configuration space before setting FEATURES_OK, but no existing users
112 * need that and it's less code for callers if this is done implicitly.
113 */
114 if (features & (1ull << VIRTIO_F_VERSION_1)) {
115 uint8_t status = d->bus->get_status(d) |
116 VIRTIO_CONFIG_S_FEATURES_OK;
117
118 d->bus->set_status(d, status);
119 g_assert_cmphex(d->bus->get_status(d), ==, status);
120 }
121
122 d->features_negotiated = true;
123 }
124
qvirtqueue_setup(QVirtioDevice * d,QGuestAllocator * alloc,uint16_t index)125 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
126 QGuestAllocator *alloc, uint16_t index)
127 {
128 g_assert_true(d->features_negotiated);
129 return d->bus->virtqueue_setup(d, alloc, index);
130 }
131
qvirtqueue_cleanup(const QVirtioBus * bus,QVirtQueue * vq,QGuestAllocator * alloc)132 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
133 QGuestAllocator *alloc)
134 {
135 return bus->virtqueue_cleanup(vq, alloc);
136 }
137
qvirtio_reset(QVirtioDevice * d)138 void qvirtio_reset(QVirtioDevice *d)
139 {
140 d->bus->set_status(d, 0);
141 g_assert_cmphex(d->bus->get_status(d), ==, 0);
142 d->features_negotiated = false;
143 }
144
qvirtio_set_acknowledge(QVirtioDevice * d)145 void qvirtio_set_acknowledge(QVirtioDevice *d)
146 {
147 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
148 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
149 }
150
qvirtio_set_driver(QVirtioDevice * d)151 void qvirtio_set_driver(QVirtioDevice *d)
152 {
153 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
154 g_assert_cmphex(d->bus->get_status(d), ==,
155 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
156 }
157
qvirtio_set_driver_ok(QVirtioDevice * d)158 void qvirtio_set_driver_ok(QVirtioDevice *d)
159 {
160 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
161 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
162 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE |
163 (d->features & (1ull << VIRTIO_F_VERSION_1) ?
164 VIRTIO_CONFIG_S_FEATURES_OK : 0));
165 }
166
qvirtio_wait_queue_isr(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,gint64 timeout_us)167 void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d,
168 QVirtQueue *vq, gint64 timeout_us)
169 {
170 gint64 start_time = g_get_monotonic_time();
171
172 for (;;) {
173 qtest_clock_step(qts, 100);
174 if (d->bus->get_queue_isr_status(d, vq)) {
175 return;
176 }
177 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
178 }
179 }
180
181 /* Wait for the status byte at given guest memory address to be set
182 *
183 * The virtqueue interrupt must not be raised, making this useful for testing
184 * event_index functionality.
185 */
qvirtio_wait_status_byte_no_isr(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,uint64_t addr,gint64 timeout_us)186 uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
187 QVirtQueue *vq,
188 uint64_t addr,
189 gint64 timeout_us)
190 {
191 gint64 start_time = g_get_monotonic_time();
192 uint8_t val;
193
194 while ((val = qtest_readb(qts, addr)) == 0xff) {
195 qtest_clock_step(qts, 100);
196 g_assert(!d->bus->get_queue_isr_status(d, vq));
197 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
198 }
199 return val;
200 }
201
202 /*
203 * qvirtio_wait_used_elem:
204 * @desc_idx: The next expected vq->desc[] index in the used ring
205 * @len: A pointer that is filled with the length written into the buffer, may
206 * be NULL
207 * @timeout_us: How many microseconds to wait before failing
208 *
209 * This function waits for the next completed request on the used ring.
210 */
qvirtio_wait_used_elem(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,uint32_t desc_idx,uint32_t * len,gint64 timeout_us)211 void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
212 QVirtQueue *vq,
213 uint32_t desc_idx,
214 uint32_t *len,
215 gint64 timeout_us)
216 {
217 gint64 start_time = g_get_monotonic_time();
218
219 for (;;) {
220 uint32_t got_desc_idx;
221
222 qtest_clock_step(qts, 100);
223
224 if (d->bus->get_queue_isr_status(d, vq) &&
225 qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) {
226 g_assert_cmpint(got_desc_idx, ==, desc_idx);
227 return;
228 }
229
230 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
231 }
232 }
233
qvirtio_wait_config_isr(QVirtioDevice * d,gint64 timeout_us)234 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
235 {
236 d->bus->wait_config_isr_status(d, timeout_us);
237 }
238
qvring_init(QTestState * qts,const QGuestAllocator * alloc,QVirtQueue * vq,uint64_t addr)239 void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq,
240 uint64_t addr)
241 {
242 int i;
243
244 vq->desc = addr;
245 vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
246 vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
247 + vq->align - 1) & ~(vq->align - 1));
248
249 for (i = 0; i < vq->size - 1; i++) {
250 /* vq->desc[i].addr */
251 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * i), 0);
252 /* vq->desc[i].next */
253 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * i) + 14, i + 1);
254 }
255
256 /* vq->avail->flags */
257 qvirtio_writew(vq->vdev, qts, vq->avail, 0);
258 /* vq->avail->idx */
259 qvirtio_writew(vq->vdev, qts, vq->avail + 2, 0);
260 /* vq->avail->used_event */
261 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0);
262
263 /* vq->used->flags */
264 qvirtio_writew(vq->vdev, qts, vq->used, 0);
265 /* vq->used->idx */
266 qvirtio_writew(vq->vdev, qts, vq->used + 2, 0);
267 /* vq->used->avail_event */
268 qvirtio_writew(vq->vdev, qts, vq->used + 4 +
269 sizeof(struct vring_used_elem) * vq->size, 0);
270 }
271
qvring_indirect_desc_setup(QTestState * qs,QVirtioDevice * d,QGuestAllocator * alloc,uint16_t elem)272 QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
273 QGuestAllocator *alloc,
274 uint16_t elem)
275 {
276 int i;
277 QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
278
279 indirect->index = 0;
280 indirect->elem = elem;
281 indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
282
283 for (i = 0; i < elem; ++i) {
284 /* indirect->desc[i].addr */
285 qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0);
286
287 /*
288 * If it's not the last element of the ring, set
289 * the chain (VRING_DESC_F_NEXT) flag and
290 * desc->next. Clear the last element - there's
291 * no guarantee that guest_alloc() will do it.
292 */
293 if (i != elem - 1) {
294 /* indirect->desc[i].flags */
295 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12,
296 VRING_DESC_F_NEXT);
297
298 /* indirect->desc[i].next */
299 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1);
300 } else {
301 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12, 0);
302 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, 0);
303 }
304 }
305
306 return indirect;
307 }
308
qvring_indirect_desc_add(QVirtioDevice * d,QTestState * qts,QVRingIndirectDesc * indirect,uint64_t data,uint32_t len,bool write)309 void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts,
310 QVRingIndirectDesc *indirect,
311 uint64_t data, uint32_t len, bool write)
312 {
313 uint16_t flags;
314
315 g_assert_cmpint(indirect->index, <, indirect->elem);
316
317 flags = qvirtio_readw(d, qts, indirect->desc +
318 (16 * indirect->index) + 12);
319
320 if (write) {
321 flags |= VRING_DESC_F_WRITE;
322 }
323
324 /* indirect->desc[indirect->index].addr */
325 qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data);
326 /* indirect->desc[indirect->index].len */
327 qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len);
328 /* indirect->desc[indirect->index].flags */
329 qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12,
330 flags);
331
332 indirect->index++;
333 }
334
qvirtqueue_add(QTestState * qts,QVirtQueue * vq,uint64_t data,uint32_t len,bool write,bool next)335 uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
336 uint32_t len, bool write, bool next)
337 {
338 uint16_t flags = 0;
339 vq->num_free--;
340
341 if (write) {
342 flags |= VRING_DESC_F_WRITE;
343 }
344
345 if (next) {
346 flags |= VRING_DESC_F_NEXT;
347 }
348
349 /* vq->desc[vq->free_head].addr */
350 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data);
351 /* vq->desc[vq->free_head].len */
352 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len);
353 /* vq->desc[vq->free_head].flags */
354 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags);
355
356 return vq->free_head++; /* Return and increase, in this order */
357 }
358
qvirtqueue_add_indirect(QTestState * qts,QVirtQueue * vq,QVRingIndirectDesc * indirect)359 uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
360 QVRingIndirectDesc *indirect)
361 {
362 g_assert(vq->indirect);
363 g_assert_cmpint(vq->size, >=, indirect->elem);
364 g_assert_cmpint(indirect->index, ==, indirect->elem);
365
366 vq->num_free--;
367
368 /* vq->desc[vq->free_head].addr */
369 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head),
370 indirect->desc);
371 /* vq->desc[vq->free_head].len */
372 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8,
373 sizeof(struct vring_desc) * indirect->elem);
374 /* vq->desc[vq->free_head].flags */
375 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12,
376 VRING_DESC_F_INDIRECT);
377
378 return vq->free_head++; /* Return and increase, in this order */
379 }
380
qvirtqueue_kick(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,uint32_t free_head)381 void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
382 uint32_t free_head)
383 {
384 /* vq->avail->idx */
385 uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
386 /* vq->used->flags */
387 uint16_t flags;
388 /* vq->used->avail_event */
389 uint16_t avail_event;
390
391 /* vq->avail->ring[idx % vq->size] */
392 qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
393 /* vq->avail->idx */
394 qvirtio_writew(d, qts, vq->avail + 2, idx + 1);
395
396 /* Must read after idx is updated */
397 flags = qvirtio_readw(d, qts, vq->used);
398 avail_event = qvirtio_readw(d, qts, vq->used + 4 +
399 sizeof(struct vring_used_elem) * vq->size);
400
401 /* < 1 because we add elements to avail queue one by one */
402 if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
403 (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
404 d->bus->virtqueue_kick(d, vq);
405 }
406 }
407
408 /*
409 * qvirtqueue_get_buf:
410 * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
411 * @len: A pointer that is filled with the length written into the buffer, may
412 * be NULL
413 *
414 * This function gets the next used element if there is one ready.
415 *
416 * Returns: true if an element was ready, false otherwise
417 */
qvirtqueue_get_buf(QTestState * qts,QVirtQueue * vq,uint32_t * desc_idx,uint32_t * len)418 bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
419 uint32_t *len)
420 {
421 uint16_t idx;
422 uint64_t elem_addr, addr;
423
424 idx = qvirtio_readw(vq->vdev, qts,
425 vq->used + offsetof(struct vring_used, idx));
426 if (idx == vq->last_used_idx) {
427 return false;
428 }
429
430 elem_addr = vq->used +
431 offsetof(struct vring_used, ring) +
432 (vq->last_used_idx % vq->size) *
433 sizeof(struct vring_used_elem);
434
435 if (desc_idx) {
436 addr = elem_addr + offsetof(struct vring_used_elem, id);
437 *desc_idx = qvirtio_readl(vq->vdev, qts, addr);
438 }
439
440 if (len) {
441 addr = elem_addr + offsetof(struct vring_used_elem, len);
442 *len = qvirtio_readw(vq->vdev, qts, addr);
443 }
444
445 vq->last_used_idx++;
446 return true;
447 }
448
qvirtqueue_set_used_event(QTestState * qts,QVirtQueue * vq,uint16_t idx)449 void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
450 {
451 g_assert(vq->event);
452
453 /* vq->avail->used_event */
454 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx);
455 }
456
qvirtio_start_device(QVirtioDevice * vdev)457 void qvirtio_start_device(QVirtioDevice *vdev)
458 {
459 qvirtio_reset(vdev);
460 qvirtio_set_acknowledge(vdev);
461 qvirtio_set_driver(vdev);
462 }
463
qvirtio_is_big_endian(QVirtioDevice * d)464 bool qvirtio_is_big_endian(QVirtioDevice *d)
465 {
466 return d->big_endian;
467 }
468