Skip to contentSkip to conversation

virtio-pci: fix LE host/BE guest capacity for blk

RFC PATCH Superseded Michael S. Tsirkin 2 participants 3 messages May 28, 2013

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

RFC PATCH 1/1 virtio-pci: fix LE host/BE guest capacity for blk

When a BE guest reads capacity from an LE host virtio-blk device or vice versa, it will get the dwords of the qword field swapped.
As virtio-blk is the only one with such a quirk,
and as non-pci transports don't do byte-swaps at all,
solve this with a bit of device-specific hackery in virtio-pci.

---

I don't seem to be able to boot any big-endian guests ATM, so this is only compile-tested - sending this out for early feedback/flames.
Testing reports also wellcome!

 hw/virtio/virtio-pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/virtio/virtio-pci.c +9 -0
index 9668b2b..0e9ae4c 100644
@@ -411,6 +411,15 @@ static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
         }
         break;
     case 4:
+        /* Most devices don't have 64 bit config fields.
+         * Block is an exception: first 8 bytes include
+         * a 64 bit capacity field.
+         */
+        if (virtio_is_big_endian() != defined(HOST_WORDS_BIGENDIAN) &&
+            proxy->vdev->dev_id == VIRTIO_ID_BLOCK && addr < 8) {
+            /* Swap first two words */
+            addr ^= 0x4;
+        }
         val = virtio_config_readl(proxy->vdev, addr);
         if (virtio_is_big_endian()) {
             val = bswap32(val);
Signature · 1 line
MST
#1 lore raw

Il 28/05/2013 12:11, Michael S. Tsirkin ha scritto:

Quoted 23 lines

When a BE guest reads capacity from an LE host virtio-blk device or vice versa, it will get the dwords of the qword field swapped.
As virtio-blk is the only one with such a quirk,
and as non-pci transports don't do byte-swaps at all,
solve this with a bit of device-specific hackery in virtio-pci.

---

I don't seem to be able to boot any big-endian guests ATM, so this is only compile-tested - sending this out for early feedback/flames.
Testing reports also wellcome!

 hw/virtio/virtio-pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/virtio/virtio-pci.c +0 -0
index 9668b2b..0e9ae4c 100644

hw/virtio/virtio-pci.c static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, · line 418

Quoted 8 lines
hw/virtio/virtio-pci.c +5 -0
@@ -411,6 +411,15 @@ static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
         }
         break;
     case 4:
+        /* Most devices don't have 64 bit config fields.
+         * Block is an exception: first 8 bytes include
+         * a 64 bit capacity field.
+         */
+        if (virtio_is_big_endian() != defined(HOST_WORDS_BIGENDIAN) &&

I thought think this is not valid C?

Paolo

Quoted 8 lines
hw/virtio/virtio-pci.c +4 -0
+            proxy->vdev->dev_id == VIRTIO_ID_BLOCK && addr < 8) {
+            /* Swap first two words */
+            addr ^= 0x4;
+        }
         val = virtio_config_readl(proxy->vdev, addr);
         if (virtio_is_big_endian()) {
             val = bswap32(val);
#2 lore raw
Quoted 35 lines · Paolo Bonzini

Il 28/05/2013 12:11, Michael S. Tsirkin ha scritto:

Quoted 23 lines

When a BE guest reads capacity from an LE host virtio-blk device or vice versa, it will get the dwords of the qword field swapped.
As virtio-blk is the only one with such a quirk,
and as non-pci transports don't do byte-swaps at all,
solve this with a bit of device-specific hackery in virtio-pci.

---

I don't seem to be able to boot any big-endian guests ATM, so this is only compile-tested - sending this out for early feedback/flames.
Testing reports also wellcome!

 hw/virtio/virtio-pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/virtio/virtio-pci.c +0 -0
index 9668b2b..0e9ae4c 100644

hw/virtio/virtio-pci.c static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr, · line 418

Quoted 8 lines
hw/virtio/virtio-pci.c +5 -0
@@ -411,6 +411,15 @@ static uint64_t virtio_pci_config_read(void *opaque, hwaddr addr,
         }
         break;
     case 4:
+        /* Most devices don't have 64 bit config fields.
+         * Block is an exception: first 8 bytes include
+         * a 64 bit capacity field.
+         */
+        if (virtio_is_big_endian() != defined(HOST_WORDS_BIGENDIAN) &&

I thought think this is not valid C?

Quoted 1 line

Paolo

Yes, v2 I just sent fixes this.
Thanks for paying attention.

Quoted 8 lines
Quoted 8 lines
hw/virtio/virtio-pci.c +4 -0
+            proxy->vdev->dev_id == VIRTIO_ID_BLOCK && addr < 8) {
+            /* Swap first two words */
+            addr ^= 0x4;
+        }
         val = virtio_config_readl(proxy->vdev, addr);
         if (virtio_is_big_endian()) {
             val = bswap32(val);