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     d->features = features;
105     d->bus->set_features(d, features);
106 
107     /*
108      * This could be a separate function for drivers that want to access
109      * configuration space before setting FEATURES_OK, but no existing users
110      * need that and it's less code for callers if this is done implicitly.
111     */
112     if (features & (1ull << VIRTIO_F_VERSION_1)) {
113         uint8_t status = d->bus->get_status(d) |
114                          VIRTIO_CONFIG_S_FEATURES_OK;
115 
116         d->bus->set_status(d, status);
117         g_assert_cmphex(d->bus->get_status(d), ==, status);
118     }
119 
120     d->features_negotiated = true;
121 }
122 
qvirtqueue_setup(QVirtioDevice * d,QGuestAllocator * alloc,uint16_t index)123 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
124                              QGuestAllocator *alloc, uint16_t index)
125 {
126     g_assert_true(d->features_negotiated);
127     return d->bus->virtqueue_setup(d, alloc, index);
128 }
129 
qvirtqueue_cleanup(const QVirtioBus * bus,QVirtQueue * vq,QGuestAllocator * alloc)130 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
131                         QGuestAllocator *alloc)
132 {
133     return bus->virtqueue_cleanup(vq, alloc);
134 }
135 
qvirtio_reset(QVirtioDevice * d)136 void qvirtio_reset(QVirtioDevice *d)
137 {
138     d->bus->set_status(d, 0);
139     g_assert_cmphex(d->bus->get_status(d), ==, 0);
140     d->features_negotiated = false;
141 }
142 
qvirtio_set_acknowledge(QVirtioDevice * d)143 void qvirtio_set_acknowledge(QVirtioDevice *d)
144 {
145     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
146     g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
147 }
148 
qvirtio_set_driver(QVirtioDevice * d)149 void qvirtio_set_driver(QVirtioDevice *d)
150 {
151     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
152     g_assert_cmphex(d->bus->get_status(d), ==,
153                     VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
154 }
155 
qvirtio_set_driver_ok(QVirtioDevice * d)156 void qvirtio_set_driver_ok(QVirtioDevice *d)
157 {
158     d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
159     g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
160                     VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE |
161                     (d->features & (1ull << VIRTIO_F_VERSION_1) ?
162                      VIRTIO_CONFIG_S_FEATURES_OK : 0));
163 }
164 
qvirtio_wait_queue_isr(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,gint64 timeout_us)165 void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d,
166                             QVirtQueue *vq, gint64 timeout_us)
167 {
168     gint64 start_time = g_get_monotonic_time();
169 
170     for (;;) {
171         qtest_clock_step(qts, 100);
172         if (d->bus->get_queue_isr_status(d, vq)) {
173             return;
174         }
175         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
176     }
177 }
178 
179 /* Wait for the status byte at given guest memory address to be set
180  *
181  * The virtqueue interrupt must not be raised, making this useful for testing
182  * event_index functionality.
183  */
qvirtio_wait_status_byte_no_isr(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,uint64_t addr,gint64 timeout_us)184 uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
185                                         QVirtQueue *vq,
186                                         uint64_t addr,
187                                         gint64 timeout_us)
188 {
189     gint64 start_time = g_get_monotonic_time();
190     uint8_t val;
191 
192     while ((val = qtest_readb(qts, addr)) == 0xff) {
193         qtest_clock_step(qts, 100);
194         g_assert(!d->bus->get_queue_isr_status(d, vq));
195         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
196     }
197     return val;
198 }
199 
200 /*
201  * qvirtio_wait_used_elem:
202  * @desc_idx: The next expected vq->desc[] index in the used ring
203  * @len: A pointer that is filled with the length written into the buffer, may
204  *       be NULL
205  * @timeout_us: How many microseconds to wait before failing
206  *
207  * This function waits for the next completed request on the used ring.
208  */
qvirtio_wait_used_elem(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,uint32_t desc_idx,uint32_t * len,gint64 timeout_us)209 void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
210                             QVirtQueue *vq,
211                             uint32_t desc_idx,
212                             uint32_t *len,
213                             gint64 timeout_us)
214 {
215     gint64 start_time = g_get_monotonic_time();
216 
217     for (;;) {
218         uint32_t got_desc_idx;
219 
220         qtest_clock_step(qts, 100);
221 
222         if (d->bus->get_queue_isr_status(d, vq) &&
223             qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) {
224             g_assert_cmpint(got_desc_idx, ==, desc_idx);
225             return;
226         }
227 
228         g_assert(g_get_monotonic_time() - start_time <= timeout_us);
229     }
230 }
231 
qvirtio_wait_config_isr(QVirtioDevice * d,gint64 timeout_us)232 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
233 {
234     d->bus->wait_config_isr_status(d, timeout_us);
235 }
236 
qvring_init(QTestState * qts,const QGuestAllocator * alloc,QVirtQueue * vq,uint64_t addr)237 void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq,
238                  uint64_t addr)
239 {
240     int i;
241 
242     vq->desc = addr;
243     vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
244     vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
245         + vq->align - 1) & ~(vq->align - 1));
246 
247     for (i = 0; i < vq->size - 1; i++) {
248         /* vq->desc[i].addr */
249         qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * i), 0);
250         /* vq->desc[i].next */
251         qvirtio_writew(vq->vdev, qts, vq->desc + (16 * i) + 14, i + 1);
252     }
253 
254     /* vq->avail->flags */
255     qvirtio_writew(vq->vdev, qts, vq->avail, 0);
256     /* vq->avail->idx */
257     qvirtio_writew(vq->vdev, qts, vq->avail + 2, 0);
258     /* vq->avail->used_event */
259     qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0);
260 
261     /* vq->used->flags */
262     qvirtio_writew(vq->vdev, qts, vq->used, 0);
263     /* vq->used->avail_event */
264     qvirtio_writew(vq->vdev, qts, vq->used + 2 +
265                    sizeof(struct vring_used_elem) * vq->size, 0);
266 }
267 
qvring_indirect_desc_setup(QTestState * qs,QVirtioDevice * d,QGuestAllocator * alloc,uint16_t elem)268 QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
269                                                QGuestAllocator *alloc,
270                                                uint16_t elem)
271 {
272     int i;
273     QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
274 
275     indirect->index = 0;
276     indirect->elem = elem;
277     indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
278 
279     for (i = 0; i < elem - 1; ++i) {
280         /* indirect->desc[i].addr */
281         qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0);
282         /* indirect->desc[i].flags */
283         qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12,
284                        VRING_DESC_F_NEXT);
285         /* indirect->desc[i].next */
286         qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1);
287     }
288 
289     return indirect;
290 }
291 
qvring_indirect_desc_add(QVirtioDevice * d,QTestState * qts,QVRingIndirectDesc * indirect,uint64_t data,uint32_t len,bool write)292 void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts,
293                               QVRingIndirectDesc *indirect,
294                               uint64_t data, uint32_t len, bool write)
295 {
296     uint16_t flags;
297 
298     g_assert_cmpint(indirect->index, <, indirect->elem);
299 
300     flags = qvirtio_readw(d, qts, indirect->desc +
301                                   (16 * indirect->index) + 12);
302 
303     if (write) {
304         flags |= VRING_DESC_F_WRITE;
305     }
306 
307     /* indirect->desc[indirect->index].addr */
308     qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data);
309     /* indirect->desc[indirect->index].len */
310     qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len);
311     /* indirect->desc[indirect->index].flags */
312     qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12,
313                    flags);
314 
315     indirect->index++;
316 }
317 
qvirtqueue_add(QTestState * qts,QVirtQueue * vq,uint64_t data,uint32_t len,bool write,bool next)318 uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
319                         uint32_t len, bool write, bool next)
320 {
321     uint16_t flags = 0;
322     vq->num_free--;
323 
324     if (write) {
325         flags |= VRING_DESC_F_WRITE;
326     }
327 
328     if (next) {
329         flags |= VRING_DESC_F_NEXT;
330     }
331 
332     /* vq->desc[vq->free_head].addr */
333     qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data);
334     /* vq->desc[vq->free_head].len */
335     qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len);
336     /* vq->desc[vq->free_head].flags */
337     qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags);
338 
339     return vq->free_head++; /* Return and increase, in this order */
340 }
341 
qvirtqueue_add_indirect(QTestState * qts,QVirtQueue * vq,QVRingIndirectDesc * indirect)342 uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
343                                  QVRingIndirectDesc *indirect)
344 {
345     g_assert(vq->indirect);
346     g_assert_cmpint(vq->size, >=, indirect->elem);
347     g_assert_cmpint(indirect->index, ==, indirect->elem);
348 
349     vq->num_free--;
350 
351     /* vq->desc[vq->free_head].addr */
352     qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head),
353                    indirect->desc);
354     /* vq->desc[vq->free_head].len */
355     qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8,
356                    sizeof(struct vring_desc) * indirect->elem);
357     /* vq->desc[vq->free_head].flags */
358     qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12,
359                    VRING_DESC_F_INDIRECT);
360 
361     return vq->free_head++; /* Return and increase, in this order */
362 }
363 
qvirtqueue_kick(QTestState * qts,QVirtioDevice * d,QVirtQueue * vq,uint32_t free_head)364 void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
365                      uint32_t free_head)
366 {
367     /* vq->avail->idx */
368     uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
369     /* vq->used->flags */
370     uint16_t flags;
371     /* vq->used->avail_event */
372     uint16_t avail_event;
373 
374     /* vq->avail->ring[idx % vq->size] */
375     qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
376     /* vq->avail->idx */
377     qvirtio_writew(d, qts, vq->avail + 2, idx + 1);
378 
379     /* Must read after idx is updated */
380     flags = qvirtio_readw(d, qts, vq->avail);
381     avail_event = qvirtio_readw(d, qts, vq->used + 4 +
382                                 sizeof(struct vring_used_elem) * vq->size);
383 
384     /* < 1 because we add elements to avail queue one by one */
385     if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
386                             (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
387         d->bus->virtqueue_kick(d, vq);
388     }
389 }
390 
391 /*
392  * qvirtqueue_get_buf:
393  * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
394  * @len: A pointer that is filled with the length written into the buffer, may
395  *       be NULL
396  *
397  * This function gets the next used element if there is one ready.
398  *
399  * Returns: true if an element was ready, false otherwise
400  */
qvirtqueue_get_buf(QTestState * qts,QVirtQueue * vq,uint32_t * desc_idx,uint32_t * len)401 bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
402                         uint32_t *len)
403 {
404     uint16_t idx;
405     uint64_t elem_addr, addr;
406 
407     idx = qvirtio_readw(vq->vdev, qts,
408                         vq->used + offsetof(struct vring_used, idx));
409     if (idx == vq->last_used_idx) {
410         return false;
411     }
412 
413     elem_addr = vq->used +
414         offsetof(struct vring_used, ring) +
415         (vq->last_used_idx % vq->size) *
416         sizeof(struct vring_used_elem);
417 
418     if (desc_idx) {
419         addr = elem_addr + offsetof(struct vring_used_elem, id);
420         *desc_idx = qvirtio_readl(vq->vdev, qts, addr);
421     }
422 
423     if (len) {
424         addr = elem_addr + offsetof(struct vring_used_elem, len);
425         *len = qvirtio_readw(vq->vdev, qts, addr);
426     }
427 
428     vq->last_used_idx++;
429     return true;
430 }
431 
qvirtqueue_set_used_event(QTestState * qts,QVirtQueue * vq,uint16_t idx)432 void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
433 {
434     g_assert(vq->event);
435 
436     /* vq->avail->used_event */
437     qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx);
438 }
439 
qvirtio_start_device(QVirtioDevice * vdev)440 void qvirtio_start_device(QVirtioDevice *vdev)
441 {
442     qvirtio_reset(vdev);
443     qvirtio_set_acknowledge(vdev);
444     qvirtio_set_driver(vdev);
445 }
446 
qvirtio_is_big_endian(QVirtioDevice * d)447 bool qvirtio_is_big_endian(QVirtioDevice *d)
448 {
449     return d->big_endian;
450 }
451