Skip to contentSkip to conversation

libqos: Virtio MMIO driver

PATCH Superseded Marc Marí 4 participants 17 messages Nov 1, 2014 – Nov 24, 2014

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore

Marc Marí

author
#0 lore raw

v2 cover libqos: Virtio MMIO driver

Add virtio-mmio support to libqos and test case for virtio-blk.

This series depends on patch "libqos: Convert malloc-pc allocator to a generic allocator"

Marc Marí (5):
libqos: Change use of pointers to uint64_t in virtio
tests: Prepare virtio-blk-test for multi-arch implementation
libqos: Remove PCI assumptions in constants of virtio driver
libqos: Add malloc generic
libqos: Add virtio MMIO support

 tests/Makefile                |    4 +-
 tests/libqos/malloc-generic.c |   50 +++++++++
 tests/libqos/malloc-generic.h |   21 ++++
 tests/libqos/virtio-mmio.c    |  190 ++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-mmio.h    |   46 +++++++++
 tests/libqos/virtio-pci.c     |   50 ++++-----
 tests/libqos/virtio-pci.h     |   24 ++---
 tests/libqos/virtio.c         |    8 +-
 tests/libqos/virtio.h         |   16 +--
 tests/virtio-blk-test.c       |  229 ++++++++++++++++++++++++++++++++++-------
 10 files changed, 552 insertions(+), 86 deletions(-)
 create mode 100644 tests/libqos/malloc-generic.c
 create mode 100644 tests/libqos/malloc-generic.h
 create mode 100644 tests/libqos/virtio-mmio.c
 create mode 100644 tests/libqos/virtio-mmio.h
Signature · 1 line
1.7.10.4
author
#1 lore raw

PATCH v2 1/5 libqos: Change use of pointers to uint64_t in virtio

Convert use of pointers in functions of virtio to uint64_t in order to make it platform-independent.

Add casting from pointers (in PCI functions) to uint64_t and vice versa through uintptr_t.

---
 tests/libqos/virtio-pci.c |   20 +++++++++++---------
 tests/libqos/virtio.c     |    8 ++++----
 tests/libqos/virtio.h     |   16 ++++++++--------
 tests/virtio-blk-test.c   |   21 ++++++++++++++-------
 4 files changed, 37 insertions(+), 28 deletions(-)
tests/libqos/virtio-pci.c +11 -9
index 788ebaf..92bcac1 100644
@@ -60,25 +60,25 @@ static void qvirtio_pci_assign_device(QVirtioDevice *d, void *data)
     *vpcidev = (QVirtioPCIDevice *)d;
 }
 
-static uint8_t qvirtio_pci_config_readb(QVirtioDevice *d, void *addr)
+static uint8_t qvirtio_pci_config_readb(QVirtioDevice *d, uint64_t addr)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    return qpci_io_readb(dev->pdev, addr);
+    return qpci_io_readb(dev->pdev, (void *)(uintptr_t)addr);
 }
 
-static uint16_t qvirtio_pci_config_readw(QVirtioDevice *d, void *addr)
+static uint16_t qvirtio_pci_config_readw(QVirtioDevice *d, uint64_t addr)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    return qpci_io_readw(dev->pdev, addr);
+    return qpci_io_readw(dev->pdev, (void *)(uintptr_t)addr);
 }
 
-static uint32_t qvirtio_pci_config_readl(QVirtioDevice *d, void *addr)
+static uint32_t qvirtio_pci_config_readl(QVirtioDevice *d, uint64_t addr)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    return qpci_io_readl(dev->pdev, addr);
+    return qpci_io_readl(dev->pdev, (void *)(uintptr_t)addr);
 }
 
-static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, void *addr)
+static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, uint64_t addr)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
     int i;
@@ -86,11 +86,13 @@ static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, void *addr)
 
     if (qtest_big_endian()) {
         for (i = 0; i < 8; ++i) {
-            u64 |= (uint64_t)qpci_io_readb(dev->pdev, addr + i) << (7 - i) * 8;
+            u64 |= (uint64_t)qpci_io_readb(dev->pdev,
+                                (void *)(uintptr_t)addr + i) << (7 - i) * 8;
         }
     } else {
         for (i = 0; i < 8; ++i) {
-            u64 |= (uint64_t)qpci_io_readb(dev->pdev, addr + i) << i * 8;
+            u64 |= (uint64_t)qpci_io_readb(dev->pdev,
+                                (void *)(uintptr_t)addr + i) << i * 8;
         }
     }
 
tests/libqos/virtio.c +4 -4
index a061289..3205b88 100644
@@ -12,25 +12,25 @@
 #include "libqos/virtio.h"
 
 uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr)
+                                                                uint64_t addr)
 {
     return bus->config_readb(d, addr);
 }
 
 uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr)
+                                                                uint64_t addr)
 {
     return bus->config_readw(d, addr);
 }
 
 uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr)
+                                                                uint64_t addr)
 {
     return bus->config_readl(d, addr);
 }
 
 uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr)
+                                                                uint64_t addr)
 {
     return bus->config_readq(d, addr);
 }
tests/libqos/virtio.h +8 -8
index 29fbacb..2449fee 100644
@@ -93,10 +93,10 @@ typedef struct QVRingIndirectDesc {
 } QVRingIndirectDesc;
 
 typedef struct QVirtioBus {
-    uint8_t (*config_readb)(QVirtioDevice *d, void *addr);
-    uint16_t (*config_readw)(QVirtioDevice *d, void *addr);
-    uint32_t (*config_readl)(QVirtioDevice *d, void *addr);
-    uint64_t (*config_readq)(QVirtioDevice *d, void *addr);
+    uint8_t (*config_readb)(QVirtioDevice *d, uint64_t addr);
+    uint16_t (*config_readw)(QVirtioDevice *d, uint64_t addr);
+    uint32_t (*config_readl)(QVirtioDevice *d, uint64_t addr);
+    uint64_t (*config_readq)(QVirtioDevice *d, uint64_t addr);
 
     /* Get features of the device */
     uint32_t (*get_features)(QVirtioDevice *d);
@@ -144,13 +144,13 @@ static inline uint32_t qvring_size(uint32_t num, uint32_t align)
 }
 
 uint8_t qvirtio_config_readb(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr);
+                                                                uint64_t addr);
 uint16_t qvirtio_config_readw(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr);
+                                                                uint64_t addr);
 uint32_t qvirtio_config_readl(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr);
+                                                                uint64_t addr);
 uint64_t qvirtio_config_readq(const QVirtioBus *bus, QVirtioDevice *d,
-                                                                void *addr);
+                                                                uint64_t addr);
 uint32_t qvirtio_get_features(const QVirtioBus *bus, QVirtioDevice *d);
 void qvirtio_set_features(const QVirtioBus *bus, QVirtioDevice *d,
                                                             uint32_t features);
tests/virtio-blk-test.c +14 -7
index ead3911..33e8094 100644
@@ -157,7 +157,8 @@ static void pci_basic(void)
     /* MSI-X is not enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
 
-    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
+                                                    (uint64_t)(uintptr_t)addr);
     g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
 
     features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
@@ -303,7 +304,8 @@ static void pci_indirect(void)
     /* MSI-X is not enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
 
-    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
+                                                    (uint64_t)(uintptr_t)addr);
     g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
 
     features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
@@ -394,7 +396,8 @@ static void pci_config(void)
     /* MSI-X is not enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
 
-    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
+                                                    (uint64_t)(uintptr_t)addr);
     g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
 
     qvirtio_set_driver_ok(&qvirtio_pci, &dev->vdev);
@@ -403,7 +406,8 @@ static void pci_config(void)
                                                     " 'size': %d } }", n_size);
     qvirtio_wait_config_isr(&qvirtio_pci, &dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
 
-    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
+                                                    (uint64_t)(uintptr_t)addr);
     g_assert_cmpint(capacity, ==, n_size / 512);
 
     qvirtio_pci_device_disable(dev);
@@ -438,7 +442,8 @@ static void pci_msix(void)
     /* MSI-X is enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_MSIX;
 
-    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
+                                                    (uint64_t)(uintptr_t)addr);
     g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
 
     features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
@@ -458,7 +463,8 @@ static void pci_msix(void)
 
     qvirtio_wait_config_isr(&qvirtio_pci, &dev->vdev, QVIRTIO_BLK_TIMEOUT_US);
 
-    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
+                                                    (uint64_t)(uintptr_t)addr);
     g_assert_cmpint(capacity, ==, n_size / 512);
 
     /* Write request */
@@ -547,7 +553,8 @@ static void pci_idx(void)
     /* MSI-X is enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_MSIX;
 
-    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev, addr);
+    capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
+                                                    (uint64_t)(uintptr_t)addr);
     g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
 
     features = qvirtio_get_features(&qvirtio_pci, &dev->vdev);
Signature · 1 line
1.7.10.4
#2 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] libqos: Change use of pointers to uint64_t in virtio

Quoted 6 lines · Marc Marí

Convert use of pointers in functions of virtio to uint64_t in order to make it platform-independent.

Add casting from pointers (in PCI functions) to uint64_t and vice versa through uintptr_t.

Quoted 7 lines
---
 tests/libqos/virtio-pci.c |   20 +++++++++++---------
 tests/libqos/virtio.c     |    8 ++++----
 tests/libqos/virtio.h     |   16 ++++++++--------
 tests/virtio-blk-test.c   |   21 ++++++++++++++-------
 4 files changed, 37 insertions(+), 28 deletions(-)

This makes sense. To fully abolish pointers the PCI code also needs to be converted. Do plan plan to do that?

Stefan

author
#3 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] libqos: Change use of pointers to uint64_t in virtio

Quoted 15 lines · El Mon, 17 Nov 2014 15:16:21 +0000 Stefan Hajnoczi <[email protected]>…
Quoted 6 lines · Marc Marí

Convert use of pointers in functions of virtio to uint64_t in order to make it platform-independent.

Add casting from pointers (in PCI functions) to uint64_t and vice versa through uintptr_t.

Quoted 7 lines
---
 tests/libqos/virtio-pci.c |   20 +++++++++++---------
 tests/libqos/virtio.c     |    8 ++++----
 tests/libqos/virtio.h     |   16 ++++++++--------
 tests/virtio-blk-test.c   |   21 ++++++++++++++-------
 4 files changed, 37 insertions(+), 28 deletions(-)
Quoted 4 lines

This makes sense. To fully abolish pointers the PCI code also needs to be converted. Do plan plan to do that?

Stefan

Not planned, but if asked, I may do it.

Marc

#4 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] libqos: Change use of pointers to uint64_t in virtio

Am 17.11.2014 um 16:19 schrieb Marc Marí:

Quoted 17 lines
Quoted 15 lines · El Mon, 17 Nov 2014 15:16:21 +0000 Stefan Hajnoczi <[email protected]>…
Quoted 13 lines · Marc Marí

Convert use of pointers in functions of virtio to uint64_t in order to make it platform-independent.

Add casting from pointers (in PCI functions) to uint64_t and vice versa through uintptr_t.

---
 tests/libqos/virtio-pci.c |   20 +++++++++++---------
 tests/libqos/virtio.c     |    8 ++++----
 tests/libqos/virtio.h     |   16 ++++++++--------
 tests/virtio-blk-test.c   |   21 ++++++++++++++-------
 4 files changed, 37 insertions(+), 28 deletions(-)
Quoted 6 lines
Quoted 4 lines

This makes sense. To fully abolish pointers the PCI code also needs to be converted. Do plan plan to do that?

Stefan

Not planned, but if asked, I may do it.

Pretty please. :) I was wondering the same thing when reading the patch.

Regards,
Andreas

Signature · 2 lines
SUSE LINUX GmbH, Maxfeldstr. 5, 90409 Nürnberg, Germany
GF: Jeff Hawn, Jennifer Guild, Felix Imendörffer; HRB 21284 AG Nürnberg
#5 lore raw

Re: [Qemu-devel] [PATCH v2 1/5] libqos: Change use of pointers to uint64_t in virtio

Quoted 18 lines · Andreas Färber

Am 17.11.2014 um 16:19 schrieb Marc Marí:

Quoted 17 lines
Quoted 15 lines · El Mon, 17 Nov 2014 15:16:21 +0000 Stefan Hajnoczi <[email protected]>…
Quoted 13 lines · Marc Marí

Convert use of pointers in functions of virtio to uint64_t in order to make it platform-independent.

Add casting from pointers (in PCI functions) to uint64_t and vice versa through uintptr_t.

---
 tests/libqos/virtio-pci.c |   20 +++++++++++---------
 tests/libqos/virtio.c     |    8 ++++----
 tests/libqos/virtio.h     |   16 ++++++++--------
 tests/virtio-blk-test.c   |   21 ++++++++++++++-------
 4 files changed, 37 insertions(+), 28 deletions(-)
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines

This makes sense. To fully abolish pointers the PCI code also needs to be converted. Do plan plan to do that?

Stefan

Not planned, but if asked, I may do it.

Pretty please. :) I was wondering the same thing when reading the patch.

That said, you do not need to convert PCI in this patch series.

If you do the PCI conversion in a separate patch series we can keep merging code incrementally. It's more satisfying for you that way and less intimidating for code reviews to review short series.

Stefan

author
#6 lore raw

PATCH v2 2/5 tests: Prepare virtio-blk-test for multi-arch implementation

In reply to Marc Marí

Modularize functions in virtio-blk-test and add PCI suffix for PCI specific components.

---
 tests/virtio-blk-test.c |   57 +++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 19 deletions(-)
tests/virtio-blk-test.c +38 -19
index 33e8094..6f07d5a 100644
@@ -55,11 +55,11 @@ typedef struct QVirtioBlkReq {
     uint8_t status;
 } QVirtioBlkReq;
 
-static QPCIBus *test_start(void)
+static char *drive_create(void)
 {
-    char *cmdline;
-    char tmp_path[] = "/tmp/qtest.XXXXXX";
     int fd, ret;
+    char *tmp_path = malloc(18);
+    strncpy(tmp_path, "/tmp/qtest.XXXXXX", 18);
 
     /* Create a temporary raw image */
     fd = mkstemp(tmp_path);
@@ -68,6 +68,16 @@ static QPCIBus *test_start(void)
     g_assert_cmpint(ret, ==, 0);
     close(fd);
 
+    return tmp_path;
+}
+
+static QPCIBus *pci_test_start(void)
+{
+    char *cmdline;
+    char *tmp_path;
+
+    tmp_path = drive_create();
+
     cmdline = g_strdup_printf("-drive if=none,id=drive0,file=%s "
                               "-drive if=none,id=drive1,file=/dev/null "
                               "-device virtio-blk-pci,id=drv0,drive=drive0,"
@@ -85,7 +95,7 @@ static void test_end(void)
     qtest_end();
 }
 
-static QVirtioPCIDevice *virtio_blk_init(QPCIBus *bus, int slot)
+static QVirtioPCIDevice *virtio_blk_pci_init(QPCIBus *bus, int slot)
 {
     QVirtioPCIDevice *dev;
 
@@ -150,9 +160,9 @@ static void pci_basic(void)
     uint8_t status;
     char *data;
 
-    bus = test_start();
+    bus = pci_test_start();
 
-    dev = virtio_blk_init(bus, PCI_SLOT);
+    dev = virtio_blk_pci_init(bus, PCI_SLOT);
 
     /* MSI-X is not enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
@@ -276,8 +286,10 @@ static void pci_basic(void)
 
     /* End test */
     guest_free(alloc, vqpci->vq.desc);
+    pc_alloc_uninit(alloc);
     qvirtio_pci_device_disable(dev);
     g_free(dev);
+    qpci_free_pc(bus);
     test_end();
 }
 
@@ -297,9 +309,9 @@ static void pci_indirect(void)
     uint8_t status;
     char *data;
 
-    bus = test_start();
+    bus = pci_test_start();
 
-    dev = virtio_blk_init(bus, PCI_SLOT);
+    dev = virtio_blk_pci_init(bus, PCI_SLOT);
 
     /* MSI-X is not enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
@@ -376,8 +388,10 @@ static void pci_indirect(void)
 
     /* End test */
     guest_free(alloc, vqpci->vq.desc);
+    pc_alloc_uninit(alloc);
     qvirtio_pci_device_disable(dev);
     g_free(dev);
+    qpci_free_pc(bus);
     test_end();
 }
 
@@ -389,9 +403,9 @@ static void pci_config(void)
     void *addr;
     uint64_t capacity;
 
-    bus = test_start();
+    bus = pci_test_start();
 
-    dev = virtio_blk_init(bus, PCI_SLOT);
+    dev = virtio_blk_pci_init(bus, PCI_SLOT);
 
     /* MSI-X is not enabled */
     addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
@@ -412,6 +426,7 @@ static void pci_config(void)
 
     qvirtio_pci_device_disable(dev);
     g_free(dev);
+    qpci_free_pc(bus);
     test_end();
 }
 
@@ -431,10 +446,10 @@ static void pci_msix(void)
     uint8_t status;
     char *data;
 
-    bus = test_start();
+    bus = pci_test_start();
     alloc = pc_alloc_init();
 
-    dev = virtio_blk_init(bus, PCI_SLOT);
+    dev = virtio_blk_pci_init(bus, PCI_SLOT);
     qpci_msix_enable(dev->pdev);
 
     qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);
@@ -520,10 +535,12 @@ static void pci_msix(void)
     guest_free(alloc, req_addr);
 
     /* End test */
-    guest_free(alloc, (uint64_t)vqpci->vq.desc);
+    guest_free(alloc, vqpci->vq.desc);
+    pc_alloc_uninit(alloc);
     qpci_msix_disable(dev->pdev);
     qvirtio_pci_device_disable(dev);
     g_free(dev);
+    qpci_free_pc(bus);
     test_end();
 }
 
@@ -542,10 +559,10 @@ static void pci_idx(void)
     uint8_t status;
     char *data;
 
-    bus = test_start();
+    bus = pci_test_start();
     alloc = pc_alloc_init();
 
-    dev = virtio_blk_init(bus, PCI_SLOT);
+    dev = virtio_blk_pci_init(bus, PCI_SLOT);
     qpci_msix_enable(dev->pdev);
 
     qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);
@@ -643,24 +660,26 @@ static void pci_idx(void)
 
     /* End test */
     guest_free(alloc, vqpci->vq.desc);
+    pc_alloc_uninit(alloc);
     qpci_msix_disable(dev->pdev);
     qvirtio_pci_device_disable(dev);
     g_free(dev);
+    qpci_free_pc(bus);
     test_end();
 }
 
-static void hotplug(void)
+static void pci_hotplug(void)
 {
     QPCIBus *bus;
     QVirtioPCIDevice *dev;
 
-    bus = test_start();
+    bus = pci_test_start();
 
     /* plug secondary disk */
     qpci_plug_device_test("virtio-blk-pci", "drv1", PCI_SLOT_HP,
                           "'drive': 'drive1'");
 
-    dev = virtio_blk_init(bus, PCI_SLOT_HP);
+    dev = virtio_blk_pci_init(bus, PCI_SLOT_HP);
     g_assert(dev);
     qvirtio_pci_device_disable(dev);
     g_free(dev);
@@ -681,7 +700,7 @@ int main(int argc, char **argv)
     g_test_add_func("/virtio/blk/pci/config", pci_config);
     g_test_add_func("/virtio/blk/pci/msix", pci_msix);
     g_test_add_func("/virtio/blk/pci/idx", pci_idx);
-    g_test_add_func("/virtio/blk/pci/hotplug", hotplug);
+    g_test_add_func("/virtio/blk/pci/hotplug", pci_hotplug);
 
     ret = g_test_run();
 
Signature · 1 line
1.7.10.4
#7 lore raw

Re: [Qemu-devel] [PATCH v2 2/5] tests: Prepare virtio-blk-test for multi-arch implementation

Quoted 14 lines · Marc Marí

Modularize functions in virtio-blk-test and add PCI suffix for PCI specific components.

---
 tests/virtio-blk-test.c |   57 +++++++++++++++++++++++++++++++----------------
 1 file changed, 38 insertions(+), 19 deletions(-)
tests/virtio-blk-test.c +0 -0
index 33e8094..6f07d5a 100644
@@ -55,11 +55,11 @@ typedef struct QVirtioBlkReq {
     uint8_t status;

tests/virtio-blk-test.c typedef struct QVirtioBlkReq { · line 62

Quoted 10 lines
tests/virtio-blk-test.c +3 -3
 } QVirtioBlkReq;
 
-static QPCIBus *test_start(void)
+static char *drive_create(void)
 {
-    char *cmdline;
-    char tmp_path[] = "/tmp/qtest.XXXXXX";
     int fd, ret;
+    char *tmp_path = malloc(18);
+    strncpy(tmp_path, "/tmp/qtest.XXXXXX", 18);

Please use g_strdup():
https://developer.gnome.org/glib/stable/glib-String-Utility-Functions.html#g-strdup

QEMU does not use malloc(3)/free(3) directly. Always use glib memory allocation (in this case g_strdup() allocates memory for you).

Quoted 6 lines
tests/virtio-blk-test.c +0 -0
     /* Create a temporary raw image */
     fd = mkstemp(tmp_path);
@@ -68,6 +68,16 @@ static QPCIBus *test_start(void)
     g_assert_cmpint(ret, ==, 0);
     close(fd);

tests/virtio-blk-test.c static QPCIBus *test_start(void) · line 79

Quoted 10 lines
tests/virtio-blk-test.c +9 -0
 
+    return tmp_path;
+}
+
+static QPCIBus *pci_test_start(void)
+{
+    char *cmdline;
+    char *tmp_path;
+
+    tmp_path = drive_create();

It seems tmp_path is leaked. You must free dynamically allocated memory!

author
#8 lore raw

PATCH v2 3/5 libqos: Remove PCI assumptions in constants of virtio driver

In reply to Marc Marí

Convert PCI-specific constants names of libqos virtio driver.

---
 tests/libqos/virtio-pci.c |   30 +++++++++++++++---------------
 tests/libqos/virtio-pci.h |   24 ++++++++++++------------
 tests/virtio-blk-test.c   |   10 +++++-----
 3 files changed, 32 insertions(+), 32 deletions(-)
tests/libqos/virtio-pci.c +15 -15
index 92bcac1..046a316 100644
@@ -102,31 +102,31 @@ static uint64_t qvirtio_pci_config_readq(QVirtioDevice *d, uint64_t addr)
 static uint32_t qvirtio_pci_get_features(QVirtioDevice *d)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_DEVICE_FEATURES);
+    return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_PCI_DEVICE_FEATURES);
 }
 
 static void qvirtio_pci_set_features(QVirtioDevice *d, uint32_t features)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_GUEST_FEATURES, features);
+    qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_PCI_GUEST_FEATURES, features);
 }
 
 static uint32_t qvirtio_pci_get_guest_features(QVirtioDevice *d)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_GUEST_FEATURES);
+    return qpci_io_readl(dev->pdev, dev->addr + QVIRTIO_PCI_GUEST_FEATURES);
 }
 
 static uint8_t qvirtio_pci_get_status(QVirtioDevice *d)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS);
+    return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_PCI_DEVICE_STATUS);
 }
 
 static void qvirtio_pci_set_status(QVirtioDevice *d, uint8_t status)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_DEVICE_STATUS, status);
+    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_PCI_DEVICE_STATUS, status);
 }
 
 static bool qvirtio_pci_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
@@ -146,7 +146,7 @@ static bool qvirtio_pci_get_queue_isr_status(QVirtioDevice *d, QVirtQueue *vq)
             return data == vqpci->msix_data;
         }
     } else {
-        return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_ISR_STATUS) & 1;
+        return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_PCI_ISR_STATUS) & 1;
     }
 }
 
@@ -166,26 +166,26 @@ static bool qvirtio_pci_get_config_isr_status(QVirtioDevice *d)
             return data == dev->config_msix_data;
         }
     } else {
-        return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_ISR_STATUS) & 2;
+        return qpci_io_readb(dev->pdev, dev->addr + QVIRTIO_PCI_ISR_STATUS) & 2;
     }
 }
 
 static void qvirtio_pci_queue_select(QVirtioDevice *d, uint16_t index)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_QUEUE_SELECT, index);
+    qpci_io_writeb(dev->pdev, dev->addr + QVIRTIO_PCI_QUEUE_SELECT, index);
 }
 
 static uint16_t qvirtio_pci_get_queue_size(QVirtioDevice *d)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    return qpci_io_readw(dev->pdev, dev->addr + QVIRTIO_QUEUE_SIZE);
+    return qpci_io_readw(dev->pdev, dev->addr + QVIRTIO_PCI_QUEUE_SIZE);
 }
 
 static void qvirtio_pci_set_queue_address(QVirtioDevice *d, uint32_t pfn)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_QUEUE_ADDRESS, pfn);
+    qpci_io_writel(dev->pdev, dev->addr + QVIRTIO_PCI_QUEUE_ADDRESS, pfn);
 }
 
 static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
@@ -227,7 +227,7 @@ static QVirtQueue *qvirtio_pci_virtqueue_setup(QVirtioDevice *d,
 static void qvirtio_pci_virtqueue_kick(QVirtioDevice *d, QVirtQueue *vq)
 {
     QVirtioPCIDevice *dev = (QVirtioPCIDevice *)d;
-    qpci_io_writew(dev->pdev, dev->addr + QVIRTIO_QUEUE_NOTIFY, vq->index);
+    qpci_io_writew(dev->pdev, dev->addr + QVIRTIO_PCI_QUEUE_NOTIFY, vq->index);
 }
 
 const QVirtioBus qvirtio_pci = {
@@ -307,8 +307,8 @@ void qvirtqueue_pci_msix_setup(QVirtioPCIDevice *d, QVirtQueuePCI *vqpci,
                                         control & ~PCI_MSIX_ENTRY_CTRL_MASKBIT);
 
     qvirtio_pci_queue_select(&d->vdev, vqpci->vq.index);
-    qpci_io_writew(d->pdev, d->addr + QVIRTIO_MSIX_QUEUE_VECTOR, entry);
-    vector = qpci_io_readw(d->pdev, d->addr + QVIRTIO_MSIX_QUEUE_VECTOR);
+    qpci_io_writew(d->pdev, d->addr + QVIRTIO_PCI_MSIX_QUEUE_VECTOR, entry);
+    vector = qpci_io_readw(d->pdev, d->addr + QVIRTIO_PCI_MSIX_QUEUE_VECTOR);
     g_assert_cmphex(vector, !=, QVIRTIO_MSI_NO_VECTOR);
 }
 
@@ -339,7 +339,7 @@ void qvirtio_pci_set_msix_configuration_vector(QVirtioPCIDevice *d,
     qpci_io_writel(d->pdev, addr + PCI_MSIX_ENTRY_VECTOR_CTRL,
                                         control & ~PCI_MSIX_ENTRY_CTRL_MASKBIT);
 
-    qpci_io_writew(d->pdev, d->addr + QVIRTIO_MSIX_CONF_VECTOR, entry);
-    vector = qpci_io_readw(d->pdev, d->addr + QVIRTIO_MSIX_CONF_VECTOR);
+    qpci_io_writew(d->pdev, d->addr + QVIRTIO_PCI_MSIX_CONF_VECTOR, entry);
+    vector = qpci_io_readw(d->pdev, d->addr + QVIRTIO_PCI_MSIX_CONF_VECTOR);
     g_assert_cmphex(vector, !=, QVIRTIO_MSI_NO_VECTOR);
 }
tests/libqos/virtio-pci.h +12 -12
index 883f7ff..8f0e52a 100644
@@ -13,18 +13,18 @@
 #include "libqos/virtio.h"
 #include "libqos/pci.h"
 
-#define QVIRTIO_DEVICE_FEATURES         0x00
-#define QVIRTIO_GUEST_FEATURES          0x04
-#define QVIRTIO_QUEUE_ADDRESS           0x08
-#define QVIRTIO_QUEUE_SIZE              0x0C
-#define QVIRTIO_QUEUE_SELECT            0x0E
-#define QVIRTIO_QUEUE_NOTIFY            0x10
-#define QVIRTIO_DEVICE_STATUS           0x12
-#define QVIRTIO_ISR_STATUS              0x13
-#define QVIRTIO_MSIX_CONF_VECTOR        0x14
-#define QVIRTIO_MSIX_QUEUE_VECTOR       0x16
-#define QVIRTIO_DEVICE_SPECIFIC_MSIX    0x18
-#define QVIRTIO_DEVICE_SPECIFIC_NO_MSIX 0x14
+#define QVIRTIO_PCI_DEVICE_FEATURES         0x00
+#define QVIRTIO_PCI_GUEST_FEATURES          0x04
+#define QVIRTIO_PCI_QUEUE_ADDRESS           0x08
+#define QVIRTIO_PCI_QUEUE_SIZE              0x0C
+#define QVIRTIO_PCI_QUEUE_SELECT            0x0E
+#define QVIRTIO_PCI_QUEUE_NOTIFY            0x10
+#define QVIRTIO_PCI_DEVICE_STATUS           0x12
+#define QVIRTIO_PCI_ISR_STATUS              0x13
+#define QVIRTIO_PCI_MSIX_CONF_VECTOR        0x14
+#define QVIRTIO_PCI_MSIX_QUEUE_VECTOR       0x16
+#define QVIRTIO_PCI_DEVICE_SPECIFIC_MSIX    0x18
+#define QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX 0x14
 
 #define QVIRTIO_PCI_ALIGN   4096
 
tests/virtio-blk-test.c +5 -5
index 6f07d5a..449e8a4 100644
@@ -165,7 +165,7 @@ static void pci_basic(void)
     dev = virtio_blk_pci_init(bus, PCI_SLOT);
 
     /* MSI-X is not enabled */
-    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
+    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
 
     capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                     (uint64_t)(uintptr_t)addr);
@@ -314,7 +314,7 @@ static void pci_indirect(void)
     dev = virtio_blk_pci_init(bus, PCI_SLOT);
 
     /* MSI-X is not enabled */
-    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
+    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
 
     capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                     (uint64_t)(uintptr_t)addr);
@@ -408,7 +408,7 @@ static void pci_config(void)
     dev = virtio_blk_pci_init(bus, PCI_SLOT);
 
     /* MSI-X is not enabled */
-    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_NO_MSIX;
+    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_NO_MSIX;
 
     capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                     (uint64_t)(uintptr_t)addr);
@@ -455,7 +455,7 @@ static void pci_msix(void)
     qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);
 
     /* MSI-X is enabled */
-    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_MSIX;
+    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_MSIX;
 
     capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                     (uint64_t)(uintptr_t)addr);
@@ -568,7 +568,7 @@ static void pci_idx(void)
     qvirtio_pci_set_msix_configuration_vector(dev, alloc, 0);
 
     /* MSI-X is enabled */
-    addr = dev->addr + QVIRTIO_DEVICE_SPECIFIC_MSIX;
+    addr = dev->addr + QVIRTIO_PCI_DEVICE_SPECIFIC_MSIX;
 
     capacity = qvirtio_config_readq(&qvirtio_pci, &dev->vdev,
                                                     (uint64_t)(uintptr_t)addr);
Signature · 1 line
1.7.10.4
author
#9 lore raw

PATCH v2 4/5 libqos: Add malloc generic

In reply to Marc Marí

This malloc is a basic interface implementation that works for any platform. It should be replaced in the future for a real malloc implementation for each of the platforms.

---
 tests/libqos/malloc-generic.c |   50 +++++++++++++++++++++++++++++++++++++++++
 tests/libqos/malloc-generic.h |   21 +++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 tests/libqos/malloc-generic.c
 create mode 100644 tests/libqos/malloc-generic.h
tests/libqos/malloc-generic.c new file +50 -0
new file mode 100644
index 0000000..0049424
@@ -0,0 +1,50 @@
+/*
+ * Basic libqos generic malloc support
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "libqos/malloc-generic.h"
+#include "libqos/malloc.h"
+#include <glib.h>
+
+/*
+ * Mostly for valgrind happiness, but it does offer
+ * a chokepoint for debugging guest memory leaks, too.
+ */
+void generic_alloc_uninit(QGuestAllocator *allocator)
+{
+    alloc_uninit(allocator);
+}
+
+QGuestAllocator *generic_alloc_init_flags(uint64_t base_addr, uint64_t size,
+                                        uint32_t page_size, QAllocOpts flags)
+{
+    QGuestAllocator *s = g_malloc0(sizeof(*s));
+    MemBlock *node;
+
+    s->opts = flags;
+    s->page_size = page_size;
+
+    /* Start at 1MB */
+    s->start = base_addr + (1 << 20);
+
+    s->end = s->start + size;
+
+    QTAILQ_INIT(&s->used);
+    QTAILQ_INIT(&s->free);
+
+    node = mlist_new(s->start, s->end - s->start);
+    QTAILQ_INSERT_HEAD(&s->free, node, MLIST_ENTNAME);
+
+    return s;
+}
+
+inline QGuestAllocator *generic_alloc_init(uint64_t base_addr, uint64_t size,
+                                                            uint32_t page_size)
+{
+    return generic_alloc_init_flags(base_addr, size, page_size, ALLOC_NO_FLAGS);
+}
tests/libqos/malloc-generic.h new file +21 -0
new file mode 100644
index 0000000..90104ec
@@ -0,0 +1,21 @@
+/*
+ * Basic libqos generic malloc support
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#ifndef LIBQOS_MALLOC_GENERIC_H
+#define LIBQOS_MALLOC_GENERIC_H
+
+#include "libqos/malloc.h"
+
+QGuestAllocator *generic_alloc_init(uint64_t base_addr, uint64_t size,
+                                                            uint32_t page_size);
+QGuestAllocator *generic_alloc_init_flags(uint64_t base_addr, uint64_t size,
+                                        uint32_t page_size, QAllocOpts flags);
+void generic_alloc_uninit(QGuestAllocator *allocator);
+
+#endif
Signature · 1 line
1.7.10.4
#10 lore raw

Re: [Qemu-devel] [PATCH v2 4/5] libqos: Add malloc generic

Quoted 20 lines · Marc Marí

This malloc is a basic interface implementation that works for any platform. It should be replaced in the future for a real malloc implementation for each of the platforms.

---
 tests/libqos/malloc-generic.c |   50 +++++++++++++++++++++++++++++++++++++++++
 tests/libqos/malloc-generic.h |   21 +++++++++++++++++
 2 files changed, 71 insertions(+)
 create mode 100644 tests/libqos/malloc-generic.c
 create mode 100644 tests/libqos/malloc-generic.h
tests/libqos/malloc-generic.c new file +2 -0
new file mode 100644
index 0000000..0049424
@@ -0,0 +1,50 @@
+/*
+ * Basic libqos generic malloc support

tests/libqos/malloc-generic.c line 12

Quoted 10 lines
tests/libqos/malloc-generic.c new file +10 -0
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.
+ * See the COPYING file in the top-level directory.
+ */
+
+#include "libqos/malloc-generic.h"
+#include "libqos/malloc.h"
+#include <glib.h>

Please always include system headers (<>) before user headers (""). This ensures that QEMU's headers do not pollute the namespace or affect system headers in any way.

author
#11 lore raw

PATCH v2 5/5 libqos: Add virtio MMIO support

In reply to Marc Marí

Add virtio MMIO support.
Add virtio-blk-test MMIO test case.

---
 tests/Makefile             |    4 +-
 tests/libqos/virtio-mmio.c |  190 ++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-mmio.h |   46 +++++++++++
 tests/virtio-blk-test.c    |  143 +++++++++++++++++++++++++++++++--
 4 files changed, 375 insertions(+), 8 deletions(-)
 create mode 100644 tests/libqos/virtio-mmio.c
 create mode 100644 tests/libqos/virtio-mmio.h
tests/Makefile +3 -1
index 4ae0ca4..f193b03 100644
@@ -183,6 +183,8 @@ gcov-files-sparc-y += hw/timer/m48t59.c
 gcov-files-sparc64-y += hw/timer/m48t59.c
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
+check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
+gcov-files-arm-y += arm-softmmu/hw/block/virtio-blk.c
 check-qtest-ppc-y += tests/boot-order-test$(EXESUF)
 check-qtest-ppc64-y += tests/boot-order-test$(EXESUF)
 check-qtest-ppc64-y += tests/spapr-phb-test$(EXESUF)
@@ -300,8 +302,8 @@ libqos-obj-y += tests/libqos/i2c.o
 libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
-libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o
 libqos-usb-obj-y = $(libqos-pc-obj-y) tests/libqos/usb.o
+libqos-virtio-obj-y = $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o tests/libqos/virtio-mmio.o tests/libqos/malloc-generic.o
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
tests/libqos/virtio-mmio.c new file +190 -0

190 lines not on this page · raw message

tests/libqos/virtio-mmio.h new file +46 -0

46 lines not on this page · raw message

tests/virtio-blk-test.c +136 -7

182 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#12 lore raw

Re: [Qemu-devel] [PATCH v2 5/5] libqos: Add virtio MMIO support

Quoted 48 lines · Marc Marí

Add virtio MMIO support.
Add virtio-blk-test MMIO test case.

---
 tests/Makefile             |    4 +-
 tests/libqos/virtio-mmio.c |  190 ++++++++++++++++++++++++++++++++++++++++++++
 tests/libqos/virtio-mmio.h |   46 +++++++++++
 tests/virtio-blk-test.c    |  143 +++++++++++++++++++++++++++++++--
 4 files changed, 375 insertions(+), 8 deletions(-)
 create mode 100644 tests/libqos/virtio-mmio.c
 create mode 100644 tests/libqos/virtio-mmio.h
tests/Makefile +3 -1
index 4ae0ca4..f193b03 100644
@@ -183,6 +183,8 @@ gcov-files-sparc-y += hw/timer/m48t59.c
 gcov-files-sparc64-y += hw/timer/m48t59.c
 check-qtest-arm-y = tests/tmp105-test$(EXESUF)
 gcov-files-arm-y += hw/misc/tmp105.c
+check-qtest-arm-y += tests/virtio-blk-test$(EXESUF)
+gcov-files-arm-y += arm-softmmu/hw/block/virtio-blk.c
 check-qtest-ppc-y += tests/boot-order-test$(EXESUF)
 check-qtest-ppc64-y += tests/boot-order-test$(EXESUF)
 check-qtest-ppc64-y += tests/spapr-phb-test$(EXESUF)
@@ -300,8 +302,8 @@ libqos-obj-y += tests/libqos/i2c.o
 libqos-pc-obj-y = $(libqos-obj-y) tests/libqos/pci-pc.o
 libqos-pc-obj-y += tests/libqos/malloc-pc.o
 libqos-omap-obj-y = $(libqos-obj-y) tests/libqos/i2c-omap.o
-libqos-virtio-obj-y = $(libqos-obj-y) $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o
 libqos-usb-obj-y = $(libqos-pc-obj-y) tests/libqos/usb.o
+libqos-virtio-obj-y = $(libqos-pc-obj-y) tests/libqos/virtio.o tests/libqos/virtio-pci.o tests/libqos/virtio-mmio.o tests/libqos/malloc-generic.o
 
 tests/rtc-test$(EXESUF): tests/rtc-test.o
 tests/m48t59-test$(EXESUF): tests/m48t59-test.o
tests/libqos/virtio-mmio.c new file +6 -0
new file mode 100644
index 0000000..6896f7b
@@ -0,0 +1,190 @@
+/*
+ * libqos virtio MMIO driver
+ *
+ * Copyright (c) 2014 Marc Marí
+ *
+ * This work is licensed under the terms of the GNU GPL, version 2 or later.

tests/libqos/virtio-mmio.c line 16

Quoted 10 lines
tests/libqos/virtio-mmio.c new file +10 -0
+ * See the COPYING file in the top-level directory.
+ */
+
+#include <glib.h>
+#include "libqtest.h"
+#include "libqos/virtio.h"
+#include "libqos/virtio-mmio.h"
+#include "libqos/malloc.h"
+#include "libqos/malloc-generic.h"
+#include <stdio.h>

Please include system headers before user headers.

tests/libqos/virtio-mmio.c line 181

Quoted 8 lines
tests/libqos/virtio-mmio.c new file +8 -0
+QVirtioMMIODevice *qvirtio_mmio_init_device(uint64_t addr, uint32_t page_size)
+{
+    QVirtioMMIODevice *dev;
+    uint32_t magic;
+    dev = g_malloc0(sizeof(*dev));
+
+    magic = readl(addr + QVIRTIO_MMIO_MAGIC_VALUE);
+    g_assert(magic == ('v' | 'i' << 8 | 'r' << 16 | 't' << 24));

The magic value is little-endian according to the virtio 1.0 specification and the Linux kernel's virtio_mmio implementation.

QEMU's virtio-mmio is native endian (i.e. host endian) and I think this is a bug.

qtest's readl() is host-endian so it matches what QEMU does. I suppose you can leave this for now but it may need to be changed in the future.

Quoted 84 lines
tests/libqos/virtio-mmio.c new file +84 -0
+static void mmio_basic(void)
+{
+    QVirtioMMIODevice *dev;
+    QVirtQueue *vq;
+    QGuestAllocator *alloc;
+    QVirtioBlkReq req;
+    int n_size = TEST_IMAGE_SIZE / 2;
+    uint64_t req_addr;
+    uint64_t capacity;
+    uint32_t features;
+    uint32_t free_head;
+    uint8_t status;
+    char *data;
+
+    arm_test_start();
+
+    dev = qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR, MMIO_PAGE_SIZE);
+    g_assert(dev != NULL);
+    g_assert_cmphex(dev->vdev.device_type, ==, QVIRTIO_BLK_DEVICE_ID);
+
+    qvirtio_reset(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_acknowledge(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_driver(&qvirtio_mmio, &dev->vdev);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+                                                QVIRTIO_MMIO_DEVICE_SPECIFIC);
+    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
+
+    features = qvirtio_get_features(&qvirtio_mmio, &dev->vdev);
+    features = features & ~(QVIRTIO_F_RING_INDIRECT_DESC |
+                                QVIRTIO_F_RING_EVENT_IDX | QVIRTIO_BLK_F_SCSI);
+    qvirtio_set_features(&qvirtio_mmio, &dev->vdev, features);
+
+    alloc = generic_alloc_init(MMIO_RAM_ADDR, MMIO_RAM_SIZE, MMIO_PAGE_SIZE);
+    vq = qvirtqueue_setup(&qvirtio_mmio, &dev->vdev, alloc, 0);
+
+    qvirtio_set_driver_ok(&qvirtio_mmio, &dev->vdev);
+
+    qmp("{ 'execute': 'block_resize', 'arguments': { 'device': 'drive0', "
+                                                    " 'size': %d } }", n_size);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+                                                QVIRTIO_MMIO_DEVICE_SPECIFIC);
+    g_assert_cmpint(capacity, ==, n_size / 512);
+
+    /* Write request */
+    req.type = QVIRTIO_BLK_T_OUT;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+    strcpy(req.data, "TEST");
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 528, false, true);
+    qvirtqueue_add(vq, req_addr + 528, 1, true, false);
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
+
+    guest_free(alloc, req_addr);
+
+    /* Read request */
+    req.type = QVIRTIO_BLK_T_IN;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
+    qvirtqueue_add(vq, req_addr + 16, 513, true, false);
+
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);

tests/libqos/virtio-mmio.c line 118

Quoted 10 lines
tests/libqos/virtio-mmio.c new file +10 -0
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
+
+    data = g_malloc0(512);
+    memread(req_addr + 16, data, 512);
+    g_assert_cmpstr(data, ==, "TEST");
+    g_free(data);

There is a lot of code duplication here. Can the test logic but shared between PCI and MMIO?

author
#13 lore raw

Re: [Qemu-devel] [PATCH v2 5/5] libqos: Add virtio MMIO support

Quoted 97 lines · El Mon, 17 Nov 2014 15:48:09 +0000 Stefan Hajnoczi <[email protected]>…
Quoted 95 lines · Marc Marí
tests/virtio-blk-test.c +17 -0
+static void mmio_basic(void)
+{
+    QVirtioMMIODevice *dev;
+    QVirtQueue *vq;
+    QGuestAllocator *alloc;
+    QVirtioBlkReq req;
+    int n_size = TEST_IMAGE_SIZE / 2;
+    uint64_t req_addr;
+    uint64_t capacity;
+    uint32_t features;
+    uint32_t free_head;
+    uint8_t status;
+    char *data;
+
+    arm_test_start();
+
+    dev = qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR,

MMIO_PAGE_SIZE);

(quoted diff) +2 -0
+    g_assert(dev != NULL);
+    g_assert_cmphex(dev->vdev.device_type, ==,

QVIRTIO_BLK_DEVICE_ID); +

tests/virtio-blk-test.c +6 -0
+    qvirtio_reset(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_acknowledge(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_driver(&qvirtio_mmio, &dev->vdev);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+

QVIRTIO_MMIO_DEVICE_SPECIFIC);

tests/virtio-blk-test.c +5 -0
+    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
+
+    features = qvirtio_get_features(&qvirtio_mmio, &dev->vdev);
+    features = features & ~(QVIRTIO_F_RING_INDIRECT_DESC |
+                                QVIRTIO_F_RING_EVENT_IDX |

QVIRTIO_BLK_F_SCSI);

tests/virtio-blk-test.c +3 -0
+    qvirtio_set_features(&qvirtio_mmio, &dev->vdev, features);
+
+    alloc = generic_alloc_init(MMIO_RAM_ADDR, MMIO_RAM_SIZE,

MMIO_PAGE_SIZE);

tests/virtio-blk-test.c +5 -0
+    vq = qvirtqueue_setup(&qvirtio_mmio, &dev->vdev, alloc, 0);
+
+    qvirtio_set_driver_ok(&qvirtio_mmio, &dev->vdev);
+
+    qmp("{ 'execute': 'block_resize', 'arguments': { 'device':

'drive0', "
+ " 'size':
%d } }", n_size); +

tests/virtio-blk-test.c +5 -0
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+

QVIRTIO_MMIO_DEVICE_SPECIFIC);

tests/virtio-blk-test.c +43 -0
+    g_assert_cmpint(capacity, ==, n_size / 512);
+
+    /* Write request */
+    req.type = QVIRTIO_BLK_T_OUT;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+    strcpy(req.data, "TEST");
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 528, false, true);
+    qvirtqueue_add(vq, req_addr + 528, 1, true, false);
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
+
+    guest_free(alloc, req_addr);
+
+    /* Read request */
+    req.type = QVIRTIO_BLK_T_IN;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
+    qvirtqueue_add(vq, req_addr + 16, 513, true, false);
+
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
Quoted 8 lines

tests/virtio-blk-test.c static void pci_hotplug(void) · line 805

Quoted 5 lines
tests/virtio-blk-test.c +5 -0
+
+    data = g_malloc0(512);
+    memread(req_addr + 16, data, 512);
+    g_assert_cmpstr(data, ==, "TEST");
+    g_free(data);

There is a lot of code duplication here. Can the test logic but shared between PCI and MMIO?

The code duplication that can be easily extracted and shared is performing a simple write - read operation on the block device (which is used in various test cases). Other places (for example, checking the image size) use arch specific offsets. This could be abstracted, but I think is a bit too complicated for a test case.

By now, I will extract just write - read operation. If you think that adding abstraction for arch specific offsets is worth the effort, then I'll add them.

Marc

#14 lore raw

Re: [Qemu-devel] [PATCH v2 5/5] libqos: Add virtio MMIO support

Quoted 108 lines · Marc Marí
Quoted 97 lines · El Mon, 17 Nov 2014 15:48:09 +0000 Stefan Hajnoczi <[email protected]>…
Quoted 95 lines · Marc Marí
tests/virtio-blk-test.c +17 -0
+static void mmio_basic(void)
+{
+    QVirtioMMIODevice *dev;
+    QVirtQueue *vq;
+    QGuestAllocator *alloc;
+    QVirtioBlkReq req;
+    int n_size = TEST_IMAGE_SIZE / 2;
+    uint64_t req_addr;
+    uint64_t capacity;
+    uint32_t features;
+    uint32_t free_head;
+    uint8_t status;
+    char *data;
+
+    arm_test_start();
+
+    dev = qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR,

MMIO_PAGE_SIZE);

(quoted diff) +2 -0
+    g_assert(dev != NULL);
+    g_assert_cmphex(dev->vdev.device_type, ==,

QVIRTIO_BLK_DEVICE_ID); +

tests/virtio-blk-test.c +6 -0
+    qvirtio_reset(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_acknowledge(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_driver(&qvirtio_mmio, &dev->vdev);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+

QVIRTIO_MMIO_DEVICE_SPECIFIC);

tests/virtio-blk-test.c +5 -0
+    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
+
+    features = qvirtio_get_features(&qvirtio_mmio, &dev->vdev);
+    features = features & ~(QVIRTIO_F_RING_INDIRECT_DESC |
+                                QVIRTIO_F_RING_EVENT_IDX |

QVIRTIO_BLK_F_SCSI);

tests/virtio-blk-test.c +3 -0
+    qvirtio_set_features(&qvirtio_mmio, &dev->vdev, features);
+
+    alloc = generic_alloc_init(MMIO_RAM_ADDR, MMIO_RAM_SIZE,

MMIO_PAGE_SIZE);

tests/virtio-blk-test.c +5 -0
+    vq = qvirtqueue_setup(&qvirtio_mmio, &dev->vdev, alloc, 0);
+
+    qvirtio_set_driver_ok(&qvirtio_mmio, &dev->vdev);
+
+    qmp("{ 'execute': 'block_resize', 'arguments': { 'device':

'drive0', "
+ " 'size':
%d } }", n_size); +

tests/virtio-blk-test.c +5 -0
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+

QVIRTIO_MMIO_DEVICE_SPECIFIC);

tests/virtio-blk-test.c +43 -0
+    g_assert_cmpint(capacity, ==, n_size / 512);
+
+    /* Write request */
+    req.type = QVIRTIO_BLK_T_OUT;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+    strcpy(req.data, "TEST");
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 528, false, true);
+    qvirtqueue_add(vq, req_addr + 528, 1, true, false);
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
+
+    guest_free(alloc, req_addr);
+
+    /* Read request */
+    req.type = QVIRTIO_BLK_T_IN;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
+    qvirtqueue_add(vq, req_addr + 16, 513, true, false);
+
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
Quoted 8 lines

tests/virtio-blk-test.c static void pci_hotplug(void) · line 805

Quoted 5 lines
tests/virtio-blk-test.c +5 -0
+
+    data = g_malloc0(512);
+    memread(req_addr + 16, data, 512);
+    g_assert_cmpstr(data, ==, "TEST");
+    g_free(data);

There is a lot of code duplication here. Can the test logic but shared between PCI and MMIO?

Quoted 5 lines

The code duplication that can be easily extracted and shared is performing a simple write - read operation on the block device (which is used in various test cases). Other places (for example, checking the image size) use arch specific offsets. This could be abstracted, but I think is a bit too complicated for a test case.

Virtio config space is not transport-specific.

The Linux virtio_blk driver does:
virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);

Why does this patch use:
capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev, QVIRTIO_MMIO_DEVICE_SPECIFIC); ?

I don't have the virtio spec open right now, but I guess qvirtio_config_readq() should always start at virtio configuration byte 0 and not rely on transport-specific offsets.

Stefan

author
#15 lore raw

Re: [Qemu-devel] [PATCH v2 5/5] libqos: Add virtio MMIO support

Quoted 124 lines · El Mon, 24 Nov 2014 11:59:19 +0000 Stefan Hajnoczi <[email protected]>…
Quoted 108 lines · Marc Marí
Quoted 97 lines · El Mon, 17 Nov 2014 15:48:09 +0000 Stefan Hajnoczi <[email protected]>…
Quoted 95 lines · Marc Marí
tests/virtio-blk-test.c +17 -0
+static void mmio_basic(void)
+{
+    QVirtioMMIODevice *dev;
+    QVirtQueue *vq;
+    QGuestAllocator *alloc;
+    QVirtioBlkReq req;
+    int n_size = TEST_IMAGE_SIZE / 2;
+    uint64_t req_addr;
+    uint64_t capacity;
+    uint32_t features;
+    uint32_t free_head;
+    uint8_t status;
+    char *data;
+
+    arm_test_start();
+
+    dev = qvirtio_mmio_init_device(MMIO_DEV_BASE_ADDR,

MMIO_PAGE_SIZE);

(quoted diff) +2 -0
+    g_assert(dev != NULL);
+    g_assert_cmphex(dev->vdev.device_type, ==,

QVIRTIO_BLK_DEVICE_ID); +

tests/virtio-blk-test.c +6 -0
+    qvirtio_reset(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_acknowledge(&qvirtio_mmio, &dev->vdev);
+    qvirtio_set_driver(&qvirtio_mmio, &dev->vdev);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+

QVIRTIO_MMIO_DEVICE_SPECIFIC);

tests/virtio-blk-test.c +5 -0
+    g_assert_cmpint(capacity, ==, TEST_IMAGE_SIZE / 512);
+
+    features = qvirtio_get_features(&qvirtio_mmio, &dev->vdev);
+    features = features & ~(QVIRTIO_F_RING_INDIRECT_DESC |
+                                QVIRTIO_F_RING_EVENT_IDX |

QVIRTIO_BLK_F_SCSI);

tests/virtio-blk-test.c +3 -0
+    qvirtio_set_features(&qvirtio_mmio, &dev->vdev, features);
+
+    alloc = generic_alloc_init(MMIO_RAM_ADDR, MMIO_RAM_SIZE,

MMIO_PAGE_SIZE);

tests/virtio-blk-test.c +5 -0
+    vq = qvirtqueue_setup(&qvirtio_mmio, &dev->vdev, alloc, 0);
+
+    qvirtio_set_driver_ok(&qvirtio_mmio, &dev->vdev);
+
+    qmp("{ 'execute': 'block_resize', 'arguments': { 'device':

'drive0', "
+ " 'size':
%d } }", n_size); +

tests/virtio-blk-test.c +5 -0
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+
+    capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev,
+

QVIRTIO_MMIO_DEVICE_SPECIFIC);

tests/virtio-blk-test.c +43 -0
+    g_assert_cmpint(capacity, ==, n_size / 512);
+
+    /* Write request */
+    req.type = QVIRTIO_BLK_T_OUT;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+    strcpy(req.data, "TEST");
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 528, false, true);
+    qvirtqueue_add(vq, req_addr + 528, 1, true, false);
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
+
+    guest_free(alloc, req_addr);
+
+    /* Read request */
+    req.type = QVIRTIO_BLK_T_IN;
+    req.ioprio = 1;
+    req.sector = 0;
+    req.data = g_malloc0(512);
+
+    req_addr = virtio_blk_request(alloc, &req, 512);
+
+    g_free(req.data);
+
+    free_head = qvirtqueue_add(vq, req_addr, 16, false, true);
+    qvirtqueue_add(vq, req_addr + 16, 513, true, false);
+
+    qvirtqueue_kick(&qvirtio_mmio, &dev->vdev, vq, free_head);
+
+    qvirtio_wait_queue_isr(&qvirtio_mmio, &dev->vdev, vq,
+                           QVIRTIO_BLK_TIMEOUT_US);
+    status = readb(req_addr + 528);
+    g_assert_cmpint(status, ==, 0);
Quoted 8 lines

tests/virtio-blk-test.c static void pci_hotplug(void) · line 805

Quoted 5 lines
tests/virtio-blk-test.c +5 -0
+
+    data = g_malloc0(512);
+    memread(req_addr + 16, data, 512);
+    g_assert_cmpstr(data, ==, "TEST");
+    g_free(data);

There is a lot of code duplication here. Can the test logic but shared between PCI and MMIO?

Quoted 5 lines

The code duplication that can be easily extracted and shared is performing a simple write - read operation on the block device (which is used in various test cases). Other places (for example, checking the image size) use arch specific offsets. This could be abstracted, but I think is a bit too complicated for a test case.

Virtio config space is not transport-specific.

The Linux virtio_blk driver does:
virtio_cread(vdev, struct virtio_blk_config, capacity, &capacity);

Why does this patch use:
capacity = qvirtio_config_readq(&qvirtio_mmio, &dev->vdev, QVIRTIO_MMIO_DEVICE_SPECIFIC); ?

Quoted 5 lines

I don't have the virtio spec open right now, but I guess qvirtio_config_readq() should always start at virtio configuration byte 0 and not rely on transport-specific offsets.

Stefan

As usual, I arrived to wrong conclusions because I didn't look at it carefully enough. Sorry.

What is variable is the position of the device-specific configuration (in PCI depends of MSIX enabled or disabled and it can be 0x14 or 0x18, and in MMIO is 0x100). But the capacity is always in the position 0 of the device-specific configuration. With the base position, as well as an allocator, a device and a bus, the basic tests can be performed (and expanded later).

Thanks
Marc