All of lore.kernel.org
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>,
	Anthony Liguori <aliguori@us.ibm.com>,
	agraf@suse.de, blauwirbel@gmail.com,
	Paolo Bonzini <pbonzini@redhat.com>,
	KONRAD Frederic <fred.konrad@greensocs.com>
Subject: [Qemu-devel] [PATCH v2 RFC] virtio-pci: fix LE host/BE guest capacity for blk
Date: Tue, 28 May 2013 13:18:54 +0300	[thread overview]
Message-ID: <20130528101854.GA29978@redhat.com> (raw)

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.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---

Changes from v1:
fixed some obvious bugs.

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!


diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index 9668b2b..e7971cc 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -98,6 +98,12 @@
 /* HACK for virtio to determine if it's running a big endian guest */
 bool virtio_is_big_endian(void);
 
+#ifdef HOST_WORDS_BIGENDIAN
+#define VIRTIO_HOST_IS_BIG_ENDIAN true
+#else
+#define VIRTIO_HOST_IS_BIG_ENDIAN false
+#endif
+
 static void virtio_pci_bus_new(VirtioBusState *bus, VirtIOPCIProxy *dev);
 
 /* virtio device */
@@ -411,6 +417,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() != VIRTIO_HOST_IS_BIG_ENDIAN &&
+            proxy->vdev->device_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);

             reply	other threads:[~2013-05-28 10:18 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-05-28 10:18 Michael S. Tsirkin [this message]
2013-06-14  0:55 ` [Qemu-devel] [PATCH v2 RFC] virtio-pci: fix LE host/BE guest capacity for blk Alexander Graf

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20130528101854.GA29978@redhat.com \
    --to=mst@redhat.com \
    --cc=agraf@suse.de \
    --cc=aliguori@us.ibm.com \
    --cc=blauwirbel@gmail.com \
    --cc=fred.konrad@greensocs.com \
    --cc=pbonzini@redhat.com \
    --cc=peter.maydell@linaro.org \
    --cc=qemu-devel@nongnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.