qemu-devel.nongnu.org archive mirror
 help / color / mirror / Atom feed
From: "Michael S. Tsirkin" <mst@redhat.com>
To: qemu-devel@nongnu.org
Cc: Peter Maydell <peter.maydell@linaro.org>
Subject: [Qemu-devel] [PULL 04/10] vhost: set vring endianness for legacy virtio
Date: Wed, 17 Jun 2015 21:38:12 +0200	[thread overview]
Message-ID: <1434569795-10524-5-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1434569795-10524-1-git-send-email-mst@redhat.com>

From: Greg Kurz <gkurz@linux.vnet.ibm.com>

Legacy virtio is native endian: if the guest and host endianness differ,
we have to tell vhost so it can swap bytes where appropriate. This is
done through a vhost ring ioctl.

Signed-off-by: Greg Kurz <gkurz@linux.vnet.ibm.com>
Reviewed-by: Michael S. Tsirkin <mst@redhat.com>
Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/virtio/vhost.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 49 insertions(+), 1 deletion(-)

diff --git a/hw/virtio/vhost.c b/hw/virtio/vhost.c
index 7908255..7ea45b3 100644
--- a/hw/virtio/vhost.c
+++ b/hw/virtio/vhost.c
@@ -17,9 +17,11 @@
 #include "hw/hw.h"
 #include "qemu/atomic.h"
 #include "qemu/range.h"
+#include "qemu/error-report.h"
 #include <linux/vhost.h>
 #include "exec/address-spaces.h"
 #include "hw/virtio/virtio-bus.h"
+#include "hw/virtio/virtio-access.h"
 #include "migration/migration.h"
 
 static struct vhost_log *vhost_log;
@@ -686,6 +688,27 @@ static void vhost_log_stop(MemoryListener *listener,
     /* FIXME: implement */
 }
 
+static int vhost_virtqueue_set_vring_endian_legacy(struct vhost_dev *dev,
+                                                   bool is_big_endian,
+                                                   int vhost_vq_index)
+{
+    struct vhost_vring_state s = {
+        .index = vhost_vq_index,
+        .num = is_big_endian
+    };
+
+    if (!dev->vhost_ops->vhost_call(dev, VHOST_SET_VRING_ENDIAN, &s)) {
+        return 0;
+    }
+
+    if (errno == ENOTTY) {
+        error_report("vhost does not support cross-endian");
+        return -ENOSYS;
+    }
+
+    return -errno;
+}
+
 static int vhost_virtqueue_start(struct vhost_dev *dev,
                                 struct VirtIODevice *vdev,
                                 struct vhost_virtqueue *vq,
@@ -716,6 +739,16 @@ static int vhost_virtqueue_start(struct vhost_dev *dev,
         return -errno;
     }
 
+    if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) &&
+        virtio_legacy_is_cross_endian(vdev)) {
+        r = vhost_virtqueue_set_vring_endian_legacy(dev,
+                                                    virtio_is_big_endian(vdev),
+                                                    vhost_vq_index);
+        if (r) {
+            return -errno;
+        }
+    }
+
     s = l = virtio_queue_get_desc_size(vdev, idx);
     a = virtio_queue_get_desc_addr(vdev, idx);
     vq->desc = cpu_physical_memory_map(a, &l, 0);
@@ -786,8 +819,9 @@ static void vhost_virtqueue_stop(struct vhost_dev *dev,
                                     struct vhost_virtqueue *vq,
                                     unsigned idx)
 {
+    int vhost_vq_index = idx - dev->vq_index;
     struct vhost_vring_state state = {
-        .index = idx - dev->vq_index
+        .index = vhost_vq_index,
     };
     int r;
     assert(idx >= dev->vq_index && idx < dev->vq_index + dev->nvqs);
@@ -798,6 +832,20 @@ static void vhost_virtqueue_stop(struct vhost_dev *dev,
     }
     virtio_queue_set_last_avail_idx(vdev, idx, state.num);
     virtio_queue_invalidate_signalled_used(vdev, idx);
+
+    /* In the cross-endian case, we need to reset the vring endianness to
+     * native as legacy devices expect so by default.
+     */
+    if (!virtio_has_feature(vdev, VIRTIO_F_VERSION_1) &&
+        virtio_legacy_is_cross_endian(vdev)) {
+        r = vhost_virtqueue_set_vring_endian_legacy(dev,
+                                                    !virtio_is_big_endian(vdev),
+                                                    vhost_vq_index);
+        if (r < 0) {
+            error_report("failed to reset vring endianness");
+        }
+    }
+
     assert (r >= 0);
     cpu_physical_memory_unmap(vq->ring, virtio_queue_get_ring_size(vdev, idx),
                               0, virtio_queue_get_ring_size(vdev, idx));
-- 
MST

  parent reply	other threads:[~2015-06-17 19:38 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-17 19:38 [Qemu-devel] [PULL 00/10] virtio, pci fixes, enhancements Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 01/10] vhost-user: part of virtio Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 02/10] linux-headers: sync vhost.h Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 03/10] virtio: introduce virtio_legacy_is_cross_endian() Michael S. Tsirkin
2015-06-17 19:38 ` Michael S. Tsirkin [this message]
2015-06-17 19:38 ` [Qemu-devel] [PULL 05/10] tap: add VNET_LE/VNET_BE operations Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 06/10] vhost-net: tell tap backend about the vnet endianness Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 07/10] vhost_net: re-enable when cross endian Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 08/10] hw/core: rebase sysbus_get_fw_dev_path() to g_strdup_printf() Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 09/10] pci: Don't register a specialized 'config_write' if default behavior is intended Michael S. Tsirkin
2015-06-17 19:38 ` [Qemu-devel] [PULL 10/10] vhost: enable vhost without without MSI-X Michael S. Tsirkin
2015-06-18 10:36 ` [Qemu-devel] [PULL 00/10] virtio, pci fixes, enhancements Peter Maydell
2015-06-18 11:14   ` Michael S. Tsirkin
2015-06-18 16:29     ` Peter Maydell
2015-06-19  7:30       ` Michael S. Tsirkin
2015-06-19  8:32         ` Peter Maydell
2015-06-19 10:19           ` Michael S. Tsirkin
2015-06-19 11:29             ` Peter Maydell

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=1434569795-10524-5-git-send-email-mst@redhat.com \
    --to=mst@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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).