qemu/tests/qtest/libqos/virtio.c
<<
>>
Prefs
   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 */
  26static 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
  40static 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
  54static 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
  65static 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
  76static 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
  87uint8_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
  93uint16_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
  99uint32_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
 105uint64_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
 111uint64_t qvirtio_get_features(QVirtioDevice *d)
 112{
 113    return d->bus->get_features(d);
 114}
 115
 116void 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
 139QVirtQueue *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
 146void qvirtqueue_cleanup(const QVirtioBus *bus, QVirtQueue *vq,
 147                        QGuestAllocator *alloc)
 148{
 149    return bus->virtqueue_cleanup(vq, alloc);
 150}
 151
 152void 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
 159void 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
 165void 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
 172void 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
 181void 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 */
 199uint8_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 */
 223void 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
 244void qvirtio_wait_config_isr(QVirtioDevice *d, gint64 timeout_us)
 245{
 246    d->bus->wait_config_isr_status(d, timeout_us);
 247}
 248
 249void 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    /* vq->avail->idx */
 269    qvirtio_writew(vq->vdev, qts, vq->avail + 2, 0);
 270    /* vq->avail->used_event */
 271    qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), 0);
 272
 273    /* vq->used->flags */
 274    qvirtio_writew(vq->vdev, qts, vq->used, 0);
 275    /* vq->used->idx */
 276    qvirtio_writew(vq->vdev, qts, vq->used + 2, 0);
 277    /* vq->used->avail_event */
 278    qvirtio_writew(vq->vdev, qts, vq->used + 4 +
 279                   sizeof(struct vring_used_elem) * vq->size, 0);
 280}
 281
 282QVRingIndirectDesc *qvring_indirect_desc_setup(QTestState *qs, QVirtioDevice *d,
 283                                               QGuestAllocator *alloc,
 284                                               uint16_t elem)
 285{
 286    int i;
 287    QVRingIndirectDesc *indirect = g_malloc(sizeof(*indirect));
 288
 289    indirect->index = 0;
 290    indirect->elem = elem;
 291    indirect->desc = guest_alloc(alloc, sizeof(struct vring_desc) * elem);
 292
 293    for (i = 0; i < elem; ++i) {
 294        /* indirect->desc[i].addr */
 295        qvirtio_writeq(d, qs, indirect->desc + (16 * i), 0);
 296
 297        /*
 298         * If it's not the last element of the ring, set
 299         * the chain (VRING_DESC_F_NEXT) flag and
 300         * desc->next. Clear the last element - there's
 301         * no guarantee that guest_alloc() will do it.
 302         */
 303        if (i != elem - 1) {
 304            /* indirect->desc[i].flags */
 305            qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12,
 306                           VRING_DESC_F_NEXT);
 307
 308            /* indirect->desc[i].next */
 309            qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, i + 1);
 310        } else {
 311            qvirtio_writew(d, qs, indirect->desc + (16 * i) + 12, 0);
 312            qvirtio_writew(d, qs, indirect->desc + (16 * i) + 14, 0);
 313        }
 314    }
 315
 316    return indirect;
 317}
 318
 319void qvring_indirect_desc_add(QVirtioDevice *d, QTestState *qts,
 320                              QVRingIndirectDesc *indirect,
 321                              uint64_t data, uint32_t len, bool write)
 322{
 323    uint16_t flags;
 324
 325    g_assert_cmpint(indirect->index, <, indirect->elem);
 326
 327    flags = qvirtio_readw(d, qts, indirect->desc +
 328                                  (16 * indirect->index) + 12);
 329
 330    if (write) {
 331        flags |= VRING_DESC_F_WRITE;
 332    }
 333
 334    /* indirect->desc[indirect->index].addr */
 335    qvirtio_writeq(d, qts, indirect->desc + (16 * indirect->index), data);
 336    /* indirect->desc[indirect->index].len */
 337    qvirtio_writel(d, qts, indirect->desc + (16 * indirect->index) + 8, len);
 338    /* indirect->desc[indirect->index].flags */
 339    qvirtio_writew(d, qts, indirect->desc + (16 * indirect->index) + 12,
 340                   flags);
 341
 342    indirect->index++;
 343}
 344
 345uint32_t qvirtqueue_add(QTestState *qts, QVirtQueue *vq, uint64_t data,
 346                        uint32_t len, bool write, bool next)
 347{
 348    uint16_t flags = 0;
 349    vq->num_free--;
 350
 351    if (write) {
 352        flags |= VRING_DESC_F_WRITE;
 353    }
 354
 355    if (next) {
 356        flags |= VRING_DESC_F_NEXT;
 357    }
 358
 359    /* vq->desc[vq->free_head].addr */
 360    qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head), data);
 361    /* vq->desc[vq->free_head].len */
 362    qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8, len);
 363    /* vq->desc[vq->free_head].flags */
 364    qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12, flags);
 365
 366    return vq->free_head++; /* Return and increase, in this order */
 367}
 368
 369uint32_t qvirtqueue_add_indirect(QTestState *qts, QVirtQueue *vq,
 370                                 QVRingIndirectDesc *indirect)
 371{
 372    g_assert(vq->indirect);
 373    g_assert_cmpint(vq->size, >=, indirect->elem);
 374    g_assert_cmpint(indirect->index, ==, indirect->elem);
 375
 376    vq->num_free--;
 377
 378    /* vq->desc[vq->free_head].addr */
 379    qvirtio_writeq(vq->vdev, qts, vq->desc + (16 * vq->free_head),
 380                   indirect->desc);
 381    /* vq->desc[vq->free_head].len */
 382    qvirtio_writel(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 8,
 383                   sizeof(struct vring_desc) * indirect->elem);
 384    /* vq->desc[vq->free_head].flags */
 385    qvirtio_writew(vq->vdev, qts, vq->desc + (16 * vq->free_head) + 12,
 386                   VRING_DESC_F_INDIRECT);
 387
 388    return vq->free_head++; /* Return and increase, in this order */
 389}
 390
 391void qvirtqueue_kick(QTestState *qts, QVirtioDevice *d, QVirtQueue *vq,
 392                     uint32_t free_head)
 393{
 394    /* vq->avail->idx */
 395    uint16_t idx = qvirtio_readw(d, qts, vq->avail + 2);
 396    /* vq->used->flags */
 397    uint16_t flags;
 398    /* vq->used->avail_event */
 399    uint16_t avail_event;
 400
 401    /* vq->avail->ring[idx % vq->size] */
 402    qvirtio_writew(d, qts, vq->avail + 4 + (2 * (idx % vq->size)), free_head);
 403    /* vq->avail->idx */
 404    qvirtio_writew(d, qts, vq->avail + 2, idx + 1);
 405
 406    /* Must read after idx is updated */
 407    flags = qvirtio_readw(d, qts, vq->used);
 408    avail_event = qvirtio_readw(d, qts, vq->used + 4 +
 409                                sizeof(struct vring_used_elem) * vq->size);
 410
 411    /* < 1 because we add elements to avail queue one by one */
 412    if ((flags & VRING_USED_F_NO_NOTIFY) == 0 &&
 413                            (!vq->event || (uint16_t)(idx-avail_event) < 1)) {
 414        d->bus->virtqueue_kick(d, vq);
 415    }
 416}
 417
 418/*
 419 * qvirtqueue_get_buf:
 420 * @desc_idx: A pointer that is filled with the vq->desc[] index, may be NULL
 421 * @len: A pointer that is filled with the length written into the buffer, may
 422 *       be NULL
 423 *
 424 * This function gets the next used element if there is one ready.
 425 *
 426 * Returns: true if an element was ready, false otherwise
 427 */
 428bool qvirtqueue_get_buf(QTestState *qts, QVirtQueue *vq, uint32_t *desc_idx,
 429                        uint32_t *len)
 430{
 431    uint16_t idx;
 432    uint64_t elem_addr, addr;
 433
 434    idx = qvirtio_readw(vq->vdev, qts,
 435                        vq->used + offsetof(struct vring_used, idx));
 436    if (idx == vq->last_used_idx) {
 437        return false;
 438    }
 439
 440    elem_addr = vq->used +
 441        offsetof(struct vring_used, ring) +
 442        (vq->last_used_idx % vq->size) *
 443        sizeof(struct vring_used_elem);
 444
 445    if (desc_idx) {
 446        addr = elem_addr + offsetof(struct vring_used_elem, id);
 447        *desc_idx = qvirtio_readl(vq->vdev, qts, addr);
 448    }
 449
 450    if (len) {
 451        addr = elem_addr + offsetof(struct vring_used_elem, len);
 452        *len = qvirtio_readw(vq->vdev, qts, addr);
 453    }
 454
 455    vq->last_used_idx++;
 456    return true;
 457}
 458
 459void qvirtqueue_set_used_event(QTestState *qts, QVirtQueue *vq, uint16_t idx)
 460{
 461    g_assert(vq->event);
 462
 463    /* vq->avail->used_event */
 464    qvirtio_writew(vq->vdev, qts, vq->avail + 4 + (2 * vq->size), idx);
 465}
 466
 467void qvirtio_start_device(QVirtioDevice *vdev)
 468{
 469    qvirtio_reset(vdev);
 470    qvirtio_set_acknowledge(vdev);
 471    qvirtio_set_driver(vdev);
 472}
 473
 474bool qvirtio_is_big_endian(QVirtioDevice *d)
 475{
 476    return d->big_endian;
 477}
 478