Skip to contentSkip to conversation

virtio-pci: implement cfg capability

PATCH In review Michael S. Tsirkin 4 participants 19 messages Jul 2, 2015 – Jul 6, 2015

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 virtio-pci: implement cfg capability

spec says we must, so let's do it!

---

untested at this point.
Paolo, could you pls take a look and confirm this
memory API usage is ok?

 hw/virtio/virtio-pci.h |  3 ++
 hw/virtio/virtio-pci.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++-
 2 files changed, 78 insertions(+), 1 deletion(-)
hw/virtio/virtio-pci.h +3 -0
index 05d9d24..b6c442f 100644
@@ -112,9 +112,12 @@ struct VirtIOPCIProxy {
     VirtIOPCIRegion device;
     VirtIOPCIRegion notify;
     MemoryRegion modern_bar;
+    MemoryRegion modern_cfg;
+    AddressSpace modern_as;
     uint32_t legacy_io_bar;
     uint32_t msix_bar;
     uint32_t modern_mem_bar;
+    int config_cap;
     uint32_t flags;
     uint32_t class_code;
     uint32_t nvectors;
hw/virtio/virtio-pci.c +75 -1

136 lines not on this page · raw message

Signature · 1 line
MST
#1 lore raw

hw/virtio/virtio-pci.c static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, · line 476

Quoted 9 lines · Michael S. Tsirkin
hw/virtio/virtio-pci.c +9 -0
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);
+        }

This parses pci_cfg_data in target endianness I think. You just want to move the little-endian value from the config cap to the little-endian value in the modern_as, so you need to use ldl_le_p and address_space_stl_le.

Quoted 14 lines
hw/virtio/virtio-pci.c +14 -0
+    }
+}
+
+static uint32_t virtio_read_config(PCIDevice *pci_dev,
+                                   uint32_t address, int len)
+{
+    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
+    struct virtio_pci_cfg_cap *cfg;
+
+    if (proxy->config_cap &&
+        ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
+                                                                  pci_cfg_data),
+                       sizeof cfg->pci_cfg_data)) {
+        uint32_t off;

hw/virtio/virtio-pci.c static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, · line 500

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+        uint32_t len;
+
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);

Same here, use address_space_ldl_le to read into an int, and stl_le_p to write into cfg->pci_cfg_data.

The best way to check it, of course, is to write a unit test! :) But you could also use a Linux BE guest on LE host.

Everything else looks good.

Paolo

#2 lore raw
Quoted 13 lines · Paolo Bonzini

hw/virtio/virtio-pci.c static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, · line 476

Quoted 9 lines · Michael S. Tsirkin
hw/virtio/virtio-pci.c +9 -0
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);
+        }
Quoted 4 lines

This parses pci_cfg_data in target endianness I think. You just want to move the little-endian value from the config cap to the little-endian value in the modern_as, so you need to use ldl_le_p and address_space_stl_le.

but isn't that two byteswaps? why isn't this same as memcpy?

Quoted 34 lines
Quoted 14 lines
hw/virtio/virtio-pci.c +14 -0
+    }
+}
+
+static uint32_t virtio_read_config(PCIDevice *pci_dev,
+                                   uint32_t address, int len)
+{
+    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
+    struct virtio_pci_cfg_cap *cfg;
+
+    if (proxy->config_cap &&
+        ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
+                                                                  pci_cfg_data),
+                       sizeof cfg->pci_cfg_data)) {
+        uint32_t off;

hw/virtio/virtio-pci.c static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, · line 500

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+        uint32_t len;
+
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);

Same here, use address_space_ldl_le to read into an int, and stl_le_p to write into cfg->pci_cfg_data.

The best way to check it, of course, is to write a unit test! :) But you could also use a Linux BE guest on LE host.

Everything else looks good.

Paolo

#3 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Paolo Bonzini
Quoted 9 lines · Michael S. Tsirkin
hw/virtio/virtio-pci.c +9 -0
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);
+        }
Quoted 6 lines
Quoted 4 lines

This parses pci_cfg_data in target endianness I think. You just want to move the little-endian value from the config cap to the little-endian value in the modern_as, so you need to use ldl_le_p and address_space_stl_le.

but isn't that two byteswaps? why isn't this same as memcpy?

It is a memcpy if you write to RAM, but the MMIO ops take an unsigned integer, so you can still have one byteswap hiding; you have to be careful when you have this kind of "forwarder", and the simplest way to do it is to use an ldl/stl pair with the same endianness.

Paolo

Quoted 34 lines
Quoted 34 lines
Quoted 14 lines
hw/virtio/virtio-pci.c +14 -0
+    }
+}
+
+static uint32_t virtio_read_config(PCIDevice *pci_dev,
+                                   uint32_t address, int len)
+{
+    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
+    struct virtio_pci_cfg_cap *cfg;
+
+    if (proxy->config_cap &&
+        ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
+                                                                  pci_cfg_data),
+                       sizeof cfg->pci_cfg_data)) {
+        uint32_t off;

hw/virtio/virtio-pci.c static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, · line 500

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+        uint32_t len;
+
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);

Same here, use address_space_ldl_le to read into an int, and stl_le_p to write into cfg->pci_cfg_data.

The best way to check it, of course, is to write a unit test! :) But you could also use a Linux BE guest on LE host.

Everything else looks good.

Paolo

#4 lore raw
In reply to Paolo Bonzini
Quoted 22 lines · Paolo Bonzini
Quoted 19 lines · Michael S. Tsirkin
Quoted 17 lines · Paolo Bonzini

hw/virtio/virtio-pci.c static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, · line 476

Quoted 9 lines · Michael S. Tsirkin
hw/virtio/virtio-pci.c +9 -0
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);
+        }

This parses pci_cfg_data in target endianness I think. You just want to move the little-endian value from the config cap to the little-endian value in the modern_as, so you need to use ldl_le_p and address_space_stl_le.

Quoted 8 lines
Quoted 1 line

but isn't that two byteswaps? why isn't this same as memcpy?

It is a memcpy if you write to RAM, but the MMIO ops take an unsigned integer, so you can still have one byteswap hiding; you have to be careful when you have this kind of "forwarder", and the simplest way to do it is to use an ldl/stl pair with the same endianness.

Paolo

The fact that address_space_write/_read actually does a byteswap if host!=target endian should probably be documented.

Or maybe it should be changed: it seems likely that non-target-specific devices that use it do this incorrectly ATM. In particular, dma_memory_rw_relaxed calls address_space_rw and since DMA originates with devices I think there's very little chance that these actually want a different behaviour depending on the target endian-ness.

Most likely, these only work correctly because DMA outside RAM is highly unusual.

Quoted 34 lines
Quoted 34 lines
Quoted 34 lines
Quoted 14 lines
hw/virtio/virtio-pci.c +14 -0
+    }
+}
+
+static uint32_t virtio_read_config(PCIDevice *pci_dev,
+                                   uint32_t address, int len)
+{
+    VirtIOPCIProxy *proxy = DO_UPCAST(VirtIOPCIProxy, pci_dev, pci_dev);
+    struct virtio_pci_cfg_cap *cfg;
+
+    if (proxy->config_cap &&
+        ranges_overlap(address, len, proxy->config_cap + offsetof(struct virtio_pci_cfg_cap,
+                                                                  pci_cfg_data),
+                       sizeof cfg->pci_cfg_data)) {
+        uint32_t off;

hw/virtio/virtio-pci.c static void virtio_write_config(PCIDevice *pci_dev, uint32_t address, · line 500

Quoted 10 lines
hw/virtio/virtio-pci.c +10 -0
+        uint32_t len;
+
+        cfg = (void *)(proxy->pci_dev.config + proxy->config_cap);
+        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);

Same here, use address_space_ldl_le to read into an int, and stl_le_p to write into cfg->pci_cfg_data.

The best way to check it, of course, is to write a unit test! :) But you could also use a Linux BE guest on LE host.

Everything else looks good.

Paolo

#5 lore raw
Quoted 2 lines · Michael S. Tsirkin

The fact that address_space_write/_read actually does a byteswap if host!=target endian should probably be documented.

FWIW, it's not if host != target endian. It's if memory region endianness != target endianness. See memory_region_wrong_endianness.

Quoted 8 lines

Or maybe it should be changed: it seems likely that non-target-specific devices that use it do this incorrectly ATM. In particular, dma_memory_rw_relaxed calls address_space_rw and since DMA originates with devices I think there's very little chance that these actually want a different behaviour depending on the target endian-ness.

Most likely, these only work correctly because DMA outside RAM is highly unusual.

They work correctly because of that, and because most devices *and* targets are little endian so you have no swap. On ppc64, which has TARGET_WORDS_BIGENDIAN, it probably wouldn't work correctly.

Paolo

#6 lore raw
Quoted 8 lines · Paolo Bonzini
Quoted 2 lines · Michael S. Tsirkin

The fact that address_space_write/_read actually does a byteswap if host!=target endian should probably be documented.

FWIW, it's not if host != target endian. It's if memory region endianness != target endianness. See memory_region_wrong_endianness.

Right. What I meant is that if device != host then we want a byteswap anyway. E.g. if device says it's BE and system is LE, then it does want to get byte 0 as MSB in the uint32 it gets.

IOW we want byteswap if host != MR.
Instead we get a byteswap if target != MR.

This is the same unless host != target.

Quoted 6 lines
Quoted 6 lines

Or maybe it should be changed: it seems likely that non-target-specific devices that use it do this incorrectly ATM. In particular, dma_memory_rw_relaxed calls address_space_rw and since DMA originates with devices I think there's very little chance that these actually want a different behaviour depending on the target endian-ness.

Quoted 8 lines
Quoted 2 lines

Most likely, these only work correctly because DMA outside RAM is highly unusual.

They work correctly because of that, and because most devices *and* targets are little endian so you have no swap. On ppc64, which has TARGET_WORDS_BIGENDIAN, it probably wouldn't work correctly.

Paolo

Also, by luck, some values work the same whatever the endian-ness. E.g. dma_memory_set fills the buffer with a given pattern, so nothing changes if you byte-swap it.

Here's an example that's wrong: dp8393x. Typically it's accessing memory for DMA, so there's no byteswap. Works fine.

But should device attempt to access another device memory, it would break because MIPS target is BE.

Cc Hervé for confirmation.

I conclude that virtio is not so special in needing a variant of address_space_rw that assumes host endian format for the data.

Signature · 1 line
MST
#7 lore raw
Quoted 7 lines · Michael S. Tsirkin

Also, by luck, some values work the same whatever the endian-ness. E.g. dma_memory_set fills the buffer with a given pattern, so nothing changes if you byte-swap it.

Here's an example that's wrong: dp8393x. Typically it's accessing memory for DMA, so there's no byteswap. Works fine.

Quoted 7 lines

But should device attempt to access another device memory, it would break because MIPS target is BE.

Cc Hervé for confirmation.

I conclude that virtio is not so special in needing a variant of address_space_rw that assumes host endian format for the data.

Why host endian and not device (in this case little) endian?

Paolo

#8 lore raw
Quoted 13 lines · Paolo Bonzini
Quoted 10 lines · Michael S. Tsirkin

Also, by luck, some values work the same whatever the endian-ness. E.g. dma_memory_set fills the buffer with a given pattern, so nothing changes if you byte-swap it.

Here's an example that's wrong: dp8393x. Typically it's accessing memory for DMA, so there's no byteswap. Works fine.

But should device attempt to access another device memory, it would break because MIPS target is BE.

Quoted 8 lines
Quoted 4 lines

Cc Hervé for confirmation.

I conclude that virtio is not so special in needing a variant of address_space_rw that assumes host endian format for the data.

Why host endian and not device (in this case little) endian?

Paolo

It's the endian of the originator of the transaction. And emulated device code is all compiled in host endian-ness.

Signature · 1 line
MST
#9 lore raw
Quoted 5 lines · Michael S. Tsirkin
Quoted 1 line · Paolo Bonzini

Why host endian and not device (in this case little) endian?

It's the endian of the originator of the transaction. And emulated device code is all compiled in host endian-ness.

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

thanks
-- PMM

#10 lore raw
Quoted 14 lines · Peter Maydell
Quoted 5 lines · Michael S. Tsirkin
Quoted 1 line · Paolo Bonzini

Why host endian and not device (in this case little) endian?

It's the endian of the originator of the transaction. And emulated device code is all compiled in host endian-ness.

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Quoted 2 lines

thanks
-- PMM

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Signature · 1 line
MST
#11 lore raw
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Yes, and address_space_rw() already deals with conversion to that device's specified endianness.

-- PMM

#12 lore raw
Quoted 9 lines · Peter Maydell
Quoted 7 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Quoted 8 lines
Quoted 3 lines

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Yes, and address_space_rw() already deals with conversion to that device's specified endianness.

-- PMM

Yes, but incorrectly if target endian != host endian. For example, LE target and LE device on BE host.

IO callbacks always get a native endian format so they expect to get byte 0 of the buffer in MSB on this host.

Signature · 1 line
MST
#13 lore raw
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Quoted 5 lines
Quoted 2 lines

Yes, and address_space_rw() already deals with conversion to that device's specified endianness.

Yes, but incorrectly if target endian != host endian. For example, LE target and LE device on BE host.

Having walked through the code, got confused, talked to bonzini on IRC about it and got unconfused again, I believe we do get this correct.

* address_space_rw() takes a pointer to a pile of bytes
* if the destination is RAM, we just memcpy them (because guest RAM is also a pile of bytes)
* if the destination is a device, then we read a value out of the pile of bytes at whatever width the target device can handle. The functions we use for this are ldl_q/ldl_p/etc, which do "load target endianness" (ie "interpret this set of 4 bytes as if it were an integer in the target-endianness") because the API of memory_region_dispatch_write() is that it takes a uint64_t data whose contents are the value to write in target endianness order. (This is regrettably undocumented.)
* memory_region_dispatch_write() then calls adjust_endianness(), converting a target-endian value to the endianness the device says it requires
* we then call the device's read/write functions, whose API is that they get a value in the endianness they asked for.

Quoted 2 lines

IO callbacks always get a native endian format so they expect to get byte 0 of the buffer in MSB on this host.

IO callbacks get the format they asked for (which might be BE, LE or target endianness). They will get byte 0 of the buffer in the MSB if they said they were BE devices (or if they said they were target-endian on a BE target).

thanks
-- PMM

#14 lore raw
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Yes, and address_space_rw() already deals with conversion to that device's specified endianness.

Yes, but incorrectly if target endian != host endian. For example, LE target and LE device on BE host.

Having walked through the code, got confused, talked to bonzini on IRC about it and got unconfused again,

Ah, *that discussion*. So it was yet another XY question, :) but for the better because it also helped me abstract Michael's question.

Peter's analysis below summarizes the implementation very well.

I believe

Quoted 14 lines

we do get this correct.

* address_space_rw() takes a pointer to a pile of bytes
* if the destination is RAM, we just memcpy them (because guest RAM is also a pile of bytes)
* if the destination is a device, then we read a value out of the pile of bytes at whatever width the target device can handle. The functions we use for this are ldl_q/ldl_p/etc, which do "load target endianness" (ie "interpret this set of 4 bytes as if it were an integer in the target-endianness") because the API of memory_region_dispatch_write() is that it takes a uint64_t data whose contents are the value to write in target endianness order. (This is regrettably undocumented.)

^^ And this is the part where "the endianness of the CPU->device bus/link" enters the picture. But it doesn't matter if the source is instead another device. What matters is that address_space_rw() manages conversion from a pile of bytes, and the device doing DMA provides that---a pile of bytes.

In the patch at the beginning of this thread, problems arose because what you passed to address_space_write wasn't just a "pile of bytes" coming from a network packet or a disk sector. Instead, it was the outcome of a previous conversion from "pile of bytes" to "bytes representing an integer in little-endian format". This conversion could have possibly included a byteswap.

Once you have established that the bytes represent an integer the right way to access them is to use ld*_p/st*_p and address_space_ld*/address_space_st*. This ensures that you do an even number of further byteswaps; for *_le_p and address_space_*_le, there will be 0 further byteswaps on little-endian hosts and 2 on big-endian hosts.

Paolo

Quoted 13 lines

* memory_region_dispatch_write() then calls adjust_endianness(), converting a target-endian value to the endianness the device says it requires
* we then call the device's read/write functions, whose API is that they get a value in the endianness they asked for.

Quoted 2 lines

IO callbacks always get a native endian format so they expect to get byte 0 of the buffer in MSB on this host.

IO callbacks get the format they asked for (which might be BE, LE or target endianness). They will get byte 0 of the buffer in the MSB if they said they were BE devices (or if they said they were target-endian on a BE target).

#16 lore raw
In reply to Peter Maydell
Quoted 34 lines · Peter Maydell
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Quoted 5 lines
Quoted 2 lines

Yes, and address_space_rw() already deals with conversion to that device's specified endianness.

Yes, but incorrectly if target endian != host endian. For example, LE target and LE device on BE host.

Having walked through the code, got confused, talked to bonzini on IRC about it and got unconfused again, I believe we do get this correct.

* address_space_rw() takes a pointer to a pile of bytes
* if the destination is RAM, we just memcpy them (because guest RAM is also a pile of bytes)
* if the destination is a device, then we read a value out of the pile of bytes at whatever width the target device can handle. The functions we use for this are ldl_q/ldl_p/etc, which do "load target endianness" (ie "interpret this set of 4 bytes as if it were an integer in the target-endianness") because the API of

Quoted 8 lines

memory_region_dispatch_write() is that it takes a uint64_t data whose contents are the value to write in target endianness order. (This is regrettably undocumented.)
* memory_region_dispatch_write() then calls adjust_endianness(), converting a target-endian value to the endianness the device says it requires
* we then call the device's read/write functions, whose API is that they get a value in the endianness they asked for.

IMO what's missing here is that device read/write callbacks actually want host endian-ness. See below for examples.

Quoted 10 lines
Quoted 2 lines

IO callbacks always get a native endian format so they expect to get byte 0 of the buffer in MSB on this host.

IO callbacks get the format they asked for (which might be BE, LE or target endianness). They will get byte 0 of the buffer in the MSB if they said they were BE devices (or if they said they were target-endian on a BE target).

thanks
-- PMM

I believe this last point is wrong.

For example typical PCI devices use little endian. This does not mean they want an LE value in their callback.

This means guest will give them LE values and they want data converted *from that* to the natural host endian, for convenience.

Check e.g. e1000_mmio_write as a typical example.

Imagine that we have another device read data (e.g. from RAM using address_space_rw) and then write it out to another device usng address_space_rw.

Run it with x86 target on BE host.
What do you get is byte 0 goes to bits 0-7 in the value.

Run it with x86 target on LE host.
What do you get is byte 0 goes to bits 0-7 in the value.

The effect on the device will be very different, and clearly this is a bug since host endian mustn't affect how devices work.

Signature · 1 line
MST
#15 lore raw
In reply to Paolo Bonzini
Quoted 54 lines · Paolo Bonzini
Quoted 15 lines · Peter Maydell
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Quoted 8 lines
Quoted 5 lines
Quoted 2 lines

Yes, and address_space_rw() already deals with conversion to that device's specified endianness.

Yes, but incorrectly if target endian != host endian. For example, LE target and LE device on BE host.

Having walked through the code, got confused, talked to bonzini on IRC about it and got unconfused again,

Ah, *that discussion*. So it was yet another XY question, :) but for the better because it also helped me abstract Michael's question.

Peter's analysis below summarizes the implementation very well.

I believe

Quoted 14 lines

we do get this correct.

* address_space_rw() takes a pointer to a pile of bytes
* if the destination is RAM, we just memcpy them (because guest RAM is also a pile of bytes)
* if the destination is a device, then we read a value out of the pile of bytes at whatever width the target device can handle. The functions we use for this are ldl_q/ldl_p/etc, which do "load target endianness" (ie "interpret this set of 4 bytes as if it were an integer in the target-endianness") because the API of memory_region_dispatch_write() is that it takes a uint64_t data whose contents are the value to write in target endianness order. (This is regrettably undocumented.)

^^ And this is the part where "the endianness of the CPU->device bus/link" enters the picture. But it doesn't matter if the source is instead another device. What matters is that address_space_rw() manages conversion from a pile of bytes, and the device doing DMA provides that---a pile of bytes.

Quoted 6 lines

In the patch at the beginning of this thread, problems arose because what you passed to address_space_write wasn't just a "pile of bytes" coming from a network packet or a disk sector. Instead, it was the outcome of a previous conversion from "pile of bytes" to "bytes representing an integer in little-endian format". This conversion could have possibly included a byteswap.

Well, not really. It's a pile of bytes from guest POV. And same thing happens if you read a pile of bytes from RAM using address_space_read.

Quoted 8 lines

Once you have established that the bytes represent an integer the right way to access them is to use ld*_p/st*_p and address_space_ld*/address_space_st*. This ensures that you do an even number of further byteswaps; for *_le_p and address_space_*_le, there will be 0 further byteswaps on little-endian hosts and 2 on big-endian hosts.

Paolo

I believe this summarizes the implementation correctly. I also argue that many devices use address_space_rw incorrectly assuming it converts from host endian, simply because most devices are written in host endian.

Quoted 13 lines
Quoted 13 lines

* memory_region_dispatch_write() then calls adjust_endianness(), converting a target-endian value to the endianness the device says it requires
* we then call the device's read/write functions, whose API is that they get a value in the endianness they asked for.

Quoted 2 lines

IO callbacks always get a native endian format so they expect to get byte 0 of the buffer in MSB on this host.

IO callbacks get the format they asked for (which might be BE, LE or target endianness). They will get byte 0 of the buffer in the MSB if they said they were BE devices (or if they said they were target-endian on a BE target).

#17 lore raw
In reply to Michael S. Tsirkin
Quoted 44 lines · Michael S. Tsirkin
Quoted 34 lines · Peter Maydell
Quoted 14 lines · Michael S. Tsirkin
Quoted 13 lines · Peter Maydell
Quoted 11 lines · Michael S. Tsirkin
Quoted 6 lines · Peter Maydell

But address_space_rw() is just the "memcpy bytes to the target's memory" operation -- if you have a pile of bytes then there are no endianness concerns. If you don't have a pile of bytes then you need to know the structure of the data you're DMAing around, and you should probably have a loop doing things with the specify-the-width functions.

Absolutely. But what if DMA happens to target another device and not memory? Device needs some endian-ness so it needs to be converted to that.

Quoted 5 lines
Quoted 2 lines

Yes, and address_space_rw() already deals with conversion to that device's specified endianness.

Yes, but incorrectly if target endian != host endian. For example, LE target and LE device on BE host.

Having walked through the code, got confused, talked to bonzini on IRC about it and got unconfused again, I believe we do get this correct.

* address_space_rw() takes a pointer to a pile of bytes
* if the destination is RAM, we just memcpy them (because guest RAM is also a pile of bytes)
* if the destination is a device, then we read a value out of the pile of bytes at whatever width the target device can handle. The functions we use for this are ldl_q/ldl_p/etc, which do "load target endianness" (ie "interpret this set of 4 bytes as if it were an integer in the target-endianness") because the API of

Quoted 8 lines

memory_region_dispatch_write() is that it takes a uint64_t data whose contents are the value to write in target endianness order. (This is regrettably undocumented.)
* memory_region_dispatch_write() then calls adjust_endianness(), converting a target-endian value to the endianness the device says it requires
* we then call the device's read/write functions, whose API is that they get a value in the endianness they asked for.

Quoted 2 lines

IMO what's missing here is that device read/write callbacks actually want host endian-ness. See below for examples.

Absolutely not. The endianness of the device read/write callbacks is included in the MemoryRegionOps.

Quoted 11 lines
Quoted 10 lines
Quoted 2 lines

IO callbacks always get a native endian format so they expect to get byte 0 of the buffer in MSB on this host.

IO callbacks get the format they asked for (which might be BE, LE or target endianness). They will get byte 0 of the buffer in the MSB if they said they were BE devices (or if they said they were target-endian on a BE target).

thanks
-- PMM

Quoted 8 lines

I believe this last point is wrong.

For example typical PCI devices use little endian. This does not mean they want an LE value in their callback.

This means guest will give them LE values and they want data converted *from that* to the natural host endian, for convenience.

An X-bit unsigned integer, such as the one passed to e1000_mmio_write, doesn't have an endianness--neither big nor little. It's just a number between 0 and 2^X-1. Endianness becomes a property of uintX_t only when you read it as bytes through memcpy, which e1000_mmio_write doesn't do (mac_reg is an array of uint32_t).

Quoted 6 lines

Imagine that we have another device read data (e.g. from RAM using address_space_rw) and then write it out to another device usng address_space_rw.

Run it with x86 target on BE host.
What do you get is byte 0 goes to bits 0-7 in the value.

You mean byte 7.

Quoted 6 lines

Run it with x86 target on LE host.
What do you get is byte 0 goes to bits 0-7 in the value.

The effect on the device will be very different, and clearly this is a bug since host endian mustn't affect how devices work.

I fail to see how host endianness enters the picture. As Peter said, there isn't a single access based on host endianness in the MMIO dispatch path.

If you think there is a bug, please write a testcase. Then we can discuss based on something concrete, or just fix it. See the recent problems with access clamping, where based on concrete testcases we were able to fix all the bugs and actually show that the patch was desirable despite the number of bugs it unveiled.

Paolo