master
c 508 lines 15.4 KB
Raw
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 */
26 static uint16_t qvirtio_readw(QVirtioDevice *d, QTestState *qts, uint64_t addr)
27 {
28 uint16_t val;
29
30 if (d->features & (1ull << VIRTIO_F_VERSION_1)) {
31 qtest_memread(qts, addr, &val, sizeof(val));
32 val = le16_to_cpu(val);
33 } else {
34 val = qtest_readw(qts, addr);
35 }
36
37 return val;
38 }
39
40 static uint32_t qvirtio_readl(QVirtioDevice *d, QTestState *qts, uint64_t addr)
41 {
42 uint32_t val;
43
44 if (d->features & (1ull << VIRTIO_F_VERSION_1)) {
45 qtest_memread(qts, addr, &val, sizeof(val));
46 val = le32_to_cpu(val);
47 } else {
48 val = qtest_readl(qts, addr);
49 }
50
51 return val;
52 }
53
54 static void qvirtio_writew(QVirtioDevice *d, QTestState *qts,
55 uint64_t addr, uint16_t val)
56 {
57 if (d->features & (1ull << VIRTIO_F_VERSION_1)) {
58 val = cpu_to_le16(val);
59 qtest_memwrite(qts, addr, &val, sizeof(val));
60 } else {
61 qtest_writew(qts, addr, val);
62 }
63 }
64
65 static void qvirtio_writel(QVirtioDevice *d, QTestState *qts,
66 uint64_t addr, uint32_t val)
67 {
68 if (d->features & (1ull << VIRTIO_F_VERSION_1)) {
69 val = cpu_to_le32(val);
70 qtest_memwrite(qts, addr, &val, sizeof(val));
71 } else {
72 qtest_writel(qts, addr, val);
73 }
74 }
75
76 static void qvirtio_writeq(QVirtioDevice *d, QTestState *qts,
77 uint64_t addr, uint64_t val)
78 {
79 if (d->features & (1ull << VIRTIO_F_VERSION_1)) {
80 val = cpu_to_le64(val);
81 qtest_memwrite(qts, addr, &val, sizeof(val));
82 } else {
83 qtest_writeq(qts, addr, val);
84 }
85 }
86
87 uint8_t qvirtio_config_readb(QVirtioDevice *d, uint64_t addr)
88 {
89 g_assert_true(d->features_negotiated);
90 return d->bus->config_readb(d, addr);
91 }
92
93 uint16_t qvirtio_config_readw(QVirtioDevice *d, uint64_t addr)
94 {
95 g_assert_true(d->features_negotiated);
96 return d->bus->config_readw(d, addr);
97 }
98
99 uint32_t qvirtio_config_readl(QVirtioDevice *d, uint64_t addr)
100 {
101 g_assert_true(d->features_negotiated);
102 return d->bus->config_readl(d, addr);
103 }
104
105 uint64_t qvirtio_config_readq(QVirtioDevice *d, uint64_t addr)
106 {
107 g_assert_true(d->features_negotiated);
108 return d->bus->config_readq(d, addr);
109 }
110
111 uint64_t qvirtio_get_features(QVirtioDevice *d)
112 {
113 return d->bus->get_features(d);
114 }
115
116 void qvirtio_set_features(QVirtioDevice *d, uint64_t features)
117 {
118 g_assert(!(features & QVIRTIO_F_BAD_FEATURE));
119
120 d->features = features;
121 d->bus->set_features(d, features);
122
123 /*
124 * This could be a separate function for drivers that want to access
125 * configuration space before setting FEATURES_OK, but no existing users
126 * need that and it's less code for callers if this is done implicitly.
127 */
128 if (features & (1ull << VIRTIO_F_VERSION_1)) {
129 uint8_t status = d->bus->get_status(d) |
130 VIRTIO_CONFIG_S_FEATURES_OK;
131
132 d->bus->set_status(d, status);
133 g_assert_cmphex(d->bus->get_status(d), ==, status);
134 }
135
136 d->features_negotiated = true;
137 }
138
139 QVirtQueue *qvirtqueue_setup(QVirtioDevice *d,
140 QGuestAllocator *alloc, uint16_t index)
141 {
142 g_assert_true(d->features_negotiated);
143 return d->bus->virtqueue_setup(d, alloc, index);
144 }
145
146 void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
147 QGuestAllocator *alloc)
148 {
149 return bus->virtqueue_cleanup(vq, alloc);
150 }
151
152 void qvirtio_reset(QVirtioDevice *d)
153 {
154 d->bus->set_status(d, 0);
155 g_assert_cmphex(d->bus->get_status(d), ==, 0);
156 d->features_negotiated = false;
157 }
158
159 void qvirtio_set_acknowledge(QVirtioDevice *d)
160 {
161 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_ACKNOWLEDGE);
162 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_ACKNOWLEDGE);
163 }
164
165 void qvirtio_set_driver(QVirtioDevice *d)
166 {
167 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER);
168 g_assert_cmphex(d->bus->get_status(d), ==,
169 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE);
170 }
171
172 void qvirtio_set_driver_ok(QVirtioDevice *d)
173 {
174 d->bus->set_status(d, d->bus->get_status(d) | VIRTIO_CONFIG_S_DRIVER_OK);
175 g_assert_cmphex(d->bus->get_status(d), ==, VIRTIO_CONFIG_S_DRIVER_OK |
176 VIRTIO_CONFIG_S_DRIVER | VIRTIO_CONFIG_S_ACKNOWLEDGE |
177 (d->features & (1ull << VIRTIO_F_VERSION_1) ?
178 VIRTIO_CONFIG_S_FEATURES_OK : 0));
179 }
180
181 void qvirtio_wait_queue_isr(QTestState *qts, QVirtioDevice *d,
182 QVirtQueue *vq, gint64 timeout_us)
183 {
184 gint64 start_time = g_get_monotonic_time();
185
186 for (;;) {
187 if (d->bus->get_queue_isr_status(d, vq)) {
188 return;
189 }
190 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
191 }
192 }
193
194 /* Wait for the status byte at given guest memory address to be set
195 *
196 * The virtqueue interrupt must not be raised, making this useful for testing
197 * event_index functionality.
198 */
199 uint8_t qvirtio_wait_status_byte_no_isr(QTestState *qts, QVirtioDevice *d,
200 QVirtQueue *vq,
201 uint64_t addr,
202 gint64 timeout_us)
203 {
204 gint64 start_time = g_get_monotonic_time();
205 uint8_t val;
206
207 while ((val = qtest_readb(qts, addr)) == 0xff) {
208 g_assert(!d->bus->get_queue_isr_status(d, vq));
209 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
210 }
211 return val;
212 }
213
214 /*
215 * qvirtio_wait_used_elem:
216 * @desc_idx: The next expected vq->desc[] index in the used ring
217 * @len: A pointer that is filled with the length written into the buffer, may
218 * be NULL
219 * @timeout_us: How many microseconds to wait before failing
220 *
221 * This function waits for the next completed request on the used ring.
222 */
223 void qvirtio_wait_used_elem(QTestState *qts, QVirtioDevice *d,
224 QVirtQueue *vq,
225 uint32_t desc_idx,
226 uint32_t *len,
227 gint64 timeout_us)
228 {
229 gint64 start_time = g_get_monotonic_time();
230
231 for (;;) {
232 uint32_t got_desc_idx;
233
234
235 if (d->bus->get_queue_isr_status(d, vq) &&
236 qvirtqueue_get_buf(qts, vq, &got_desc_idx, len)) {
237 g_assert_cmpint(got_desc_idx, ==, desc_idx);
238 return;
239 }
240 g_assert(g_get_monotonic_time() - start_time <= timeout_us);
241 }
242 }
243
244 void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
245 {
246 d->bus->wait_config_isr_status(d, timeout_us);
247 }
248
249 void qvring_init(QTestState *qts, const QGuestAllocator *alloc, QVirtQueue *vq,
250 uint64_t addr)
251 {
252 int i;
253
254 vq->desc = addr;
255 vq->avail = vq->desc + vq->size * sizeof(struct vring_desc);
256 vq->used = (uint64_t)((vq->avail + sizeof(uint16_t) * (3 + vq->size)
257 + vq->align - 1) & ~(vq->align - 1));
258
259 for (i = 0; i < vq->size - 1; i++) {
260 /* vq->desc[i].addr */
261 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * i), 0);
262 /* vq->desc[i].next */
263 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * i) + 14, i + 1);
264 }
265
266 /* vq->avail->flags */
267 qvirtio_writew(vq->vdev, qts, vq->avail, 0);
268
269 qvirtqueue_set_avail_idx(qts, vq->vdev, vq, 0);
270
271 /* vq->avail->used_event */
272 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0);
273
274 /* vq->used->flags */
275 qvirtio_writew(vq->vdev, qts, vq->used, 0);
276 /* vq->used->idx */
277 qvirtio_writew(vq->vdev, qts, vq->used + 2, 0);
278 /* vq->used->avail_event */
279 qvirtio_writew(vq->vdev, qts, vq->used + 4 +
280 sizeof(struct vring_used_elem) * vq->size, 0);
281 }
282
283 QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
284 QGuestAllocator *alloc,
285 uint16_t elem)
286 {
287 int i;
288 QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
289
290 indirect->index = 0;
291 indirect->elem = elem;
292 indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
293
294 for (i = 0; i < elem; ++i) {
295 /* indirect->desc[i].addr */
296 qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0);
297
298 /*
299 * If it's not the last element of the ring, set
300 * the chain (VRING_DESC_F_NEXT) flag and
301 * desc->next. Clear the last element - there's
302 * no guarantee that guest_alloc() will do it.
303 */
304 if (i != elem - 1) {
305 /* indirect->desc[i].flags */
306 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12,
307 VRING_DESC_F_NEXT);
308
309 /* indirect->desc[i].next */
310 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1);
311 } else {
312 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12, 0);
313 qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, 0);
314 }
315 }
316
317 return indirect;
318 }
319
320 void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts,
321 QVRingIndirectDesc *indirect,
322 uint64_t data, uint32_t len, bool write)
323 {
324 uint16_t flags;
325
326 g_assert_cmpint(indirect->index, <, indirect->elem);
327
328 flags = qvirtio_readw(d, qts, indirect->desc +
329 (16 * indirect->index) + 12);
330
331 if (write) {
332 flags |= VRING_DESC_F_WRITE;
333 }
334
335 /* indirect->desc[indirect->index].addr */
336 qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data);
337 /* indirect->desc[indirect->index].len */
338 qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len);
339 /* indirect->desc[indirect->index].flags */
340 qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12,
341 flags);
342
343 indirect->index++;
344 }
345
346 uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
347 uint32_t len, bool write, bool next)
348 {
349 uint16_t flags = 0;
350 vq->num_free--;
351
352 if (write) {
353 flags |= VRING_DESC_F_WRITE;
354 }
355
356 if (next) {
357 flags |= VRING_DESC_F_NEXT;
358 }
359
360 /* vq->desc[vq->free_head].addr */
361 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data);
362 /* vq->desc[vq->free_head].len */
363 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len);
364 /* vq->desc[vq->free_head].flags */
365 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags);
366
367 return vq->free_head++; /* Return and increase, in this order */
368 }
369
370 uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
371 QVRingIndirectDesc *indirect)
372 {
373 g_assert(vq->indirect);
374 g_assert_cmpint(vq->size, >=, indirect->elem);
375 g_assert_cmpint(indirect->index, ==, indirect->elem);
376
377 vq->num_free--;
378
379 /* vq->desc[vq->free_head].addr */
380 qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head),
381 indirect->desc);
382 /* vq->desc[vq->free_head].len */
383 qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8,
384 sizeof(struct vring_desc) * indirect->elem);
385 /* vq->desc[vq->free_head].flags */
386 qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12,
387 VRING_DESC_F_INDIRECT);
388
389 return vq->free_head++; /* Return and increase, in this order */
390 }
391
392 void qvirtqueue_set_avail_idx(QTestState *qts, QVirtioDevice *d,
393 QVirtQueue *vq, uint16_t idx)
394 {
395 /* vq->avail->idx */
396 qvirtio_writew(d, qts, vq->avail + 2, idx);
397 }
398
399 void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
400 uint32_t free_head)
401 {
402 /* vq->avail->idx */
403 uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
404 /* vq->used->flags */
405 uint16_t flags;
406 /* vq->used->avail_event */
407 uint16_t avail_event;
408
409 /* vq->avail->ring[idx % vq->size] */
410 qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
411
412 qvirtqueue_set_avail_idx(qts, d, vq, idx + 1);
413
414 /* Must read after idx is updated */
415 flags = qvirtio_readw(d, qts, vq->used);
416 avail_event = qvirtio_readw(d, qts, vq->used + 4 +
417 sizeof(struct vring_used_elem) * vq->size);
418
419 /* < 1 because we add elements to avail queue one by one */
420 if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
421 (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
422 d->bus->virtqueue_kick(d, vq);
423 }
424 }
425
426 /*
427 * qvirtqueue_get_buf:
428 * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
429 * @len: A pointer that is filled with the length written into the buffer, may
430 * be NULL
431 *
432 * This function gets the next used element if there is one ready.
433 *
434 * Returns: true if an element was ready, false otherwise
435 */
436 bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
437 uint32_t *len)
438 {
439 uint16_t idx;
440 uint64_t elem_addr, addr;
441
442 idx = qvirtio_readw(vq->vdev, qts,
443 vq->used + offsetof(struct vring_used, idx));
444 if (idx == vq->last_used_idx) {
445 return false;
446 }
447
448 elem_addr = vq->used +
449 offsetof(struct vring_used, ring) +
450 (vq->last_used_idx % vq->size) *
451 sizeof(struct vring_used_elem);
452
453 if (desc_idx) {
454 addr = elem_addr + offsetof(struct vring_used_elem, id);
455 *desc_idx = qvirtio_readl(vq->vdev, qts, addr);
456 }
457
458 if (len) {
459 addr = elem_addr + offsetof(struct vring_used_elem, len);
460 *len = qvirtio_readw(vq->vdev, qts, addr);
461 }
462
463 vq->last_used_idx++;
464 return true;
465 }
466
467 /*
468 * qvirtqueue_reset_pool:
469 * @vq: The virtqueue to reset
470 *
471 * Reset the descriptor pool state without reinitializing the device.
472 * This is useful for tests that issue a high number of requests and
473 * are limited by the simplified virtio test driver's descriptor tracking,
474 * which decrements num_free but never increments it back.
475 *
476 * This is only safe for synchronous test code where requests are
477 * sent and completed before the next request is issued. Do not use
478 * with asynchronous code where multiple requests may be in-flight.
479 *
480 * Note: This only resets the available descriptor pool (free_head,
481 * num_free). The used ring position (last_used_idx) is NOT reset
482 * and should continue to track consumed responses across iterations.
483 */
484 void qvirtqueue_reset_pool(QVirtQueue *vq)
485 {
486 vq->free_head = 0;
487 vq->num_free = vq->size;
488 }
489
490 void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
491 {
492 g_assert(vq->event);
493
494 /* vq->avail->used_event */
495 qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx);
496 }
497
498 void qvirtio_start_device(QVirtioDevice *vdev)
499 {
500 qvirtio_reset(vdev);
501 qvirtio_set_acknowledge(vdev);
502 qvirtio_set_driver(vdev);
503 }
504
505 bool qvirtio_is_big_endian(QVirtioDevice *d)
506 {
507 return d->big_endian;
508 }