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 v2 15/16] virtio fix cfg endian-ness for BE targets
Date: Wed, 8 Jul 2015 12:42:14 +0300	[thread overview]
Message-ID: <1436348444-907-16-git-send-email-mst@redhat.com> (raw)
In-Reply-To: <1436348444-907-1-git-send-email-mst@redhat.com>

address_space_rw assumes data is in target format
and byte-swaps it if target is BE and device is LE.
Use fixed-endian LE APIs instead.

Signed-off-by: Michael S. Tsirkin <mst@redhat.com>
---
 hw/virtio/virtio-pci.c | 91 +++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 83 insertions(+), 8 deletions(-)

diff --git a/hw/virtio/virtio-pci.c b/hw/virtio/virtio-pci.c
index 7890b00..6ca0258 100644
--- a/hw/virtio/virtio-pci.c
+++ b/hw/virtio/virtio-pci.c
@@ -443,6 +443,83 @@ static const MemoryRegionOps virtio_pci_config_ops = {
     .endianness = DEVICE_LITTLE_ENDIAN,
 };
 
+/* Below are generic functions to do memcpy from/to an address space,
+ * without byteswaps, with input validation.
+ *
+ * As regular address_space_* APIs all do some kind of byteswap at least for
+ * some host/target combinations, we are forced to explicitly convert to a
+ * known-endianness integer value.
+ * It doesn't really matter which endian format to go through, so the code
+ * below selects the endian that causes the least amount of work on the given
+ * host.
+ *
+ * Note: host pointer must be aligned.
+ */
+static
+void virtio_address_space_write(AddressSpace *as, hwaddr addr,
+                                const uint8_t *buf, int len)
+{
+    uint32_t val;
+
+    /* address_space_* APIs assume an aligned address.
+     * As address is under guest control, handle illegal values.
+     */
+    addr &= ~(len - 1);
+
+    /* Make sure caller aligned buf properly */
+    assert(!(((uintptr_t)buf) & (len - 1)));
+
+    switch (len) {
+    case 1:
+        val = pci_get_byte(buf);
+        address_space_stb(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
+        break;
+    case 2:
+        val = pci_get_word(buf);
+        address_space_stw_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
+        break;
+    case 4:
+        val = pci_get_long(buf);
+        address_space_stl_le(as, addr, val, MEMTXATTRS_UNSPECIFIED, NULL);
+        break;
+    default:
+        /* As length is under guest control, handle illegal values. */
+        break;
+    }
+}
+
+static void
+virtio_address_space_read(AddressSpace *as, hwaddr addr, uint8_t *buf, int len)
+{
+    uint32_t val;
+
+    /* address_space_* APIs assume an aligned address.
+     * As address is under guest control, handle illegal values.
+     */
+    addr &= ~(len - 1);
+
+    /* Make sure caller aligned buf properly */
+    assert(!(((uintptr_t)buf) & (len - 1)));
+
+    switch (len) {
+    case 1:
+        val = address_space_ldub(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
+        pci_set_byte(buf, val);
+        break;
+    case 2:
+        val = address_space_lduw_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
+        pci_set_word(buf, val);
+        break;
+    case 4:
+        val = address_space_ldl_le(as, addr, MEMTXATTRS_UNSPECIFIED, NULL);
+        pci_set_long(buf, val);
+        break;
+    default:
+        /* As length is under guest control, handle illegal values. */
+        break;
+    }
+}
+
 static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
                                 uint32_t val, int len)
 {
@@ -469,10 +546,9 @@ static void virtio_write_config(PCIDevice *pci_dev, uint32_t address,
         off = le32_to_cpu(cfg->cap.offset);
         len = le32_to_cpu(cfg->cap.length);
 
-        if ((len == 1 || len == 2 || len == 4)) {
-            address_space_write(&proxy->modern_as, off,
-                                MEMTXATTRS_UNSPECIFIED,
-                                cfg->pci_cfg_data, len);
+        if (len <= sizeof cfg->pci_cfg_data) {
+            virtio_address_space_write(&proxy->modern_as, off,
+                                       cfg->pci_cfg_data, len);
         }
     }
 }
@@ -494,10 +570,9 @@ static uint32_t virtio_read_config(PCIDevice *pci_dev,
         off = le32_to_cpu(cfg->cap.offset);
         len = le32_to_cpu(cfg->cap.length);
 
-        if ((len == 1 || len == 2 || len == 4)) {
-            address_space_read(&proxy->modern_as, off,
-                                MEMTXATTRS_UNSPECIFIED,
-                                cfg->pci_cfg_data, len);
+        if (len <= sizeof cfg->pci_cfg_data) {
+            virtio_address_space_read(&proxy->modern_as, off,
+                                      cfg->pci_cfg_data, len);
         }
     }
 
-- 
MST

  parent reply	other threads:[~2015-07-08  9:42 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-07-08  9:41 [Qemu-devel] [PULL v2 00/16] pc,virtio,pci: fixes and updates Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 01/16] dataplane: fix cross-endian issues Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 02/16] Revert "dataplane: allow virtio-1 devices" Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 03/16] acpi: split out ICH ACPI support Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 04/16] ich9: add TCO interface emulation Michael S. Tsirkin
2015-07-13  9:49   ` Amit Shah
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 05/16] tests: add testcase for TCO watchdog emulation Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 06/16] ich9: implement strap SPKR pin logic Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 07/16] hw/i386/pc: factor out pc_cmos_init_floppy() Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 08/16] hw/i386/pc: reflect any FDC @ ioport 0x3f0 in the CMOS Michael S. Tsirkin
2015-07-08  9:41 ` [Qemu-devel] [PULL v2 09/16] hw/i386/pc: don't carry FDC from pc_basic_device_init() to pc_cmos_init() Michael S. Tsirkin
2015-07-08  9:42 ` [Qemu-devel] [PULL v2 10/16] virtio_net: reuse constants from linux Michael S. Tsirkin
2015-07-08  9:42 ` [Qemu-devel] [PULL v2 11/16] pci_regs.h: import " Michael S. Tsirkin
2015-07-08  9:42 ` [Qemu-devel] [PULL v2 12/16] pcie: Set the "link active" in the link status register Michael S. Tsirkin
2015-07-08  9:42 ` [Qemu-devel] [PULL v2 13/16] virtio: define virtio_pci_cfg_cap in header Michael S. Tsirkin
2015-07-08  9:42 ` [Qemu-devel] [PULL v2 14/16] virtio-pci: implement cfg capability Michael S. Tsirkin
2015-07-08  9:42 ` Michael S. Tsirkin [this message]
2015-07-08  9:42 ` [Qemu-devel] [PULL v2 16/16] tco-test: fix up config accesses and re-enable Michael S. Tsirkin
2015-07-08 13:47 ` [Qemu-devel] [PULL v2 00/16] pc,virtio,pci: fixes and updates 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=1436348444-907-16-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).