Skip to contentSkip to conversation

msix: allow byte and word reading from mmio

PATCH In review Bernhard Kohl 3 participants 6 messages Aug 19, 2010 – Nov 15, 2010

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 msix: allow byte and word reading from mmio

It's legal that the guest reads a single byte or word from mmio. I have an OS which reads single bytes and it works fine on real hardware. Maybe this happens due to casting.

---
 hw/msix.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
hw/msix.c +16 -4
index d99403a..7dac7f7 100644
@@ -100,10 +100,22 @@ static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
     return pci_get_long(page + offset);
 }
 
-static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr)
+static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
 {
-    fprintf(stderr, "MSI-X: only dword read is allowed!\n");
-    return 0;
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x1;
+    void *page = dev->msix_table_page;
+
+    return pci_get_word(page + offset);
+}
+
+static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
+{
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr & (MSIX_PAGE_SIZE - 1);
+    void *page = dev->msix_table_page;
+
+    return pci_get_byte(page + offset);
 }
 
 static uint8_t msix_pending_mask(int vector)
@@ -198,7 +210,7 @@ static CPUWriteMemoryFunc * const msix_mmio_write[] = {
 };
 
 static CPUReadMemoryFunc * const msix_mmio_read[] = {
-    msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl
+    msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
 };
 
 /* Should be called from device's map method. */
Signature · 1 line
1.7.2.1
#1 lore raw
Quoted 6 lines · Bernhard Kohl

It's legal that the guest reads a single byte or word from mmio. I have an OS which reads single bytes and it works fine on real hardware. Maybe this happens due to casting.

Hi Michael,

I'm looking for this to come through your tree unless you disagree.

Regards,

Anthony Liguori

Quoted 44 lines
---
  hw/msix.c |   20 ++++++++++++++++----
  1 files changed, 16 insertions(+), 4 deletions(-)
hw/msix.c +16 -4
index d99403a..7dac7f7 100644
@@ -100,10 +100,22 @@ static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
      return pci_get_long(page + offset);
  }
 
-static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr)
+static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
  {
-    fprintf(stderr, "MSI-X: only dword read is allowed!\n");
-    return 0;
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&  (MSIX_PAGE_SIZE - 1)&  ~0x1;
+    void *page = dev->msix_table_page;
+
+    return pci_get_word(page + offset);
+}
+
+static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
+{
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&  (MSIX_PAGE_SIZE - 1);
+    void *page = dev->msix_table_page;
+
+    return pci_get_byte(page + offset);
  }
 
  static uint8_t msix_pending_mask(int vector)
@@ -198,7 +210,7 @@ static CPUWriteMemoryFunc * const msix_mmio_write[] = {
  };
 
  static CPUReadMemoryFunc * const msix_mmio_read[] = {
-    msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl
+    msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
  };
 
  /* Should be called from device's map method. */
#2 lore raw

Am 22.08.2010 23:34, schrieb ext Anthony Liguori:

Quoted 9 lines · Bernhard Kohl
Quoted 8 lines

It's legal that the guest reads a single byte or word from mmio. I have an OS which reads single bytes and it works fine on real hardware. Maybe this happens due to casting.

Quoted 8 lines

Hi Michael,

I'm looking for this to come through your tree unless you disagree.

Regards,

Anthony Liguori

Hi,

could you please look at this again?

Thanks
Bernhard

Quoted 48 lines
Quoted 46 lines
   
---
   hw/msix.c |   20 ++++++++++++++++----
   1 files changed, 16 insertions(+), 4 deletions(-)
hw/msix.c +16 -4
index d99403a..7dac7f7 100644
@@ -100,10 +100,22 @@ static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
       return pci_get_long(page + offset);
   }
 
-static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr)
+static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
   {
-    fprintf(stderr, "MSI-X: only dword read is allowed!\n");
-    return 0;
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&   (MSIX_PAGE_SIZE - 1)&   ~0x1;
+    void *page = dev->msix_table_page;
+
+    return pci_get_word(page + offset);
+}
+
+static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
+{
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&   (MSIX_PAGE_SIZE - 1);
+    void *page = dev->msix_table_page;
+
+    return pci_get_byte(page + offset);
   }
 
   static uint8_t msix_pending_mask(int vector)
@@ -198,7 +210,7 @@ static CPUWriteMemoryFunc * const msix_mmio_write[] = {
   };
 
   static CPUReadMemoryFunc * const msix_mmio_read[] = {
-    msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl
+    msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
   };
 
   /* Should be called from device's map method. */
#3 lore raw
In reply to Bernhard Kohl
Quoted 1 line · Bernhard Kohl

It's legal that the guest reads a single byte or word from mmio.

Interesting. The spec seems to say this:

For all accesses to MSI-X Table and MSI-X PBA fields, software must use aligned full DWORD or aligned full QWORD transactions; otherwise, the result is undefined.

Quoted 2 lines

I have an OS which reads single bytes and it works fine on real hardware. Maybe this happens due to casting.

What do you mean by casting?

Quoted 2 lines

I guess we can merge this, but we need a comment I think since this seems to contradict the spec, and people were sending patches relying on this.

Does something like the following describe the situation correctly?

/* Note: PCI spec requires that all MSI-X table accesses
are either DWORD or QWORD, size aligned. Some guests seem to violate this rule for read accesses, performing single byte reads. Since it's easy to support this, let's do so.
Also support 16 bit size aligned reads, just in case.
*/

Does you guest also do 16 bit reads? Are accesses at least aligned?

Quoted 45 lines
---
 hw/msix.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
hw/msix.c +16 -4
index d99403a..7dac7f7 100644
@@ -100,10 +100,22 @@ static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
     return pci_get_long(page + offset);
 }
 
-static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr)
+static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
 {
-    fprintf(stderr, "MSI-X: only dword read is allowed!\n");
-    return 0;
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr & (MSIX_PAGE_SIZE - 1) & ~0x1;
+    void *page = dev->msix_table_page;
+
+    return pci_get_word(page + offset);
+}
+
+static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
+{
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr & (MSIX_PAGE_SIZE - 1);
+    void *page = dev->msix_table_page;
+
+    return pci_get_byte(page + offset);
 }
 
 static uint8_t msix_pending_mask(int vector)
@@ -198,7 +210,7 @@ static CPUWriteMemoryFunc * const msix_mmio_write[] = {
 };
 
 static CPUReadMemoryFunc * const msix_mmio_read[] = {
-    msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl
+    msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
 };
 
 /* Should be called from device's map method. */
Signature · 1 line
1.7.2.1
#4 lore raw

Am 15.11.2010 11:42, schrieb ext Michael S. Tsirkin:

Quoted 10 lines · Bernhard Kohl
Quoted 3 lines

It's legal that the guest reads a single byte or word from mmio.

Interesting. The spec seems to say this:

For all accesses to MSI-X Table and MSI-X PBA fields, software must use aligned full DWORD or aligned full QWORD transactions; otherwise, the result is undefined.

I will remove the first statement from the commit message and add something like the comment you proposed below.

Quoted 6 lines
Quoted 4 lines

I have an OS which reads single bytes and it works fine on real hardware. Maybe this happens due to casting.

What do you mean by casting?

I did not yet locate the line of code where this happens in the guest. As all other accesses are dword, I assume that there is some masking, shifting or casting to a byte variable in the source code, and the compiler generates a byte access from this.

Quoted 7 lines
Quoted 3 lines

I guess we can merge this, but we need a comment I think since this seems to contradict the spec, and people were sending patches relying on this.

Quoted 9 lines

Does something like the following describe the situation correctly?

/* Note: PCI spec requires that all MSI-X table accesses
are either DWORD or QWORD, size aligned. Some guests seem to violate this rule for read accesses, performing single byte reads. Since it's easy to support this, let's do so.
Also support 16 bit size aligned reads, just in case.
*/

Yes, that's is exactly the situation with my guest. I will add this comment.

Quoted 2 lines

Does you guest also do 16 bit reads? Are accesses at least aligned?

I will check this with my guest and the readw function disabled in the patch. This will take some time.

Quoted 48 lines
Quoted 47 lines
   
---
  hw/msix.c |   20 ++++++++++++++++----
  1 files changed, 16 insertions(+), 4 deletions(-)
hw/msix.c +16 -4
index d99403a..7dac7f7 100644
@@ -100,10 +100,22 @@ static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
      return pci_get_long(page + offset);
  }
 
-static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr)
+static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
  {
-    fprintf(stderr, "MSI-X: only dword read is allowed!\n");
-    return 0;
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&  (MSIX_PAGE_SIZE - 1)&  ~0x1;
+    void *page = dev->msix_table_page;
+
+    return pci_get_word(page + offset);
+}
+
+static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
+{
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&  (MSIX_PAGE_SIZE - 1);
+    void *page = dev->msix_table_page;
+
+    return pci_get_byte(page + offset);
  }
 
  static uint8_t msix_pending_mask(int vector)
@@ -198,7 +210,7 @@ static CPUWriteMemoryFunc * const msix_mmio_write[] = {
  };
 
  static CPUReadMemoryFunc * const msix_mmio_read[] = {
-    msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl
+    msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
  };
 
  /* Should be called from device's map method. */
Signature · 1 line
1.7.2.1
#5 lore raw
Quoted 12 lines · Bernhard Kohl

Am 15.11.2010 11:42, schrieb ext Michael S. Tsirkin:

Quoted 7 lines · Bernhard Kohl
Quoted 1 line

It's legal that the guest reads a single byte or word from mmio.

Interesting. The spec seems to say this:

For all accesses to MSI-X Table and MSI-X PBA fields, software must use aligned full DWORD or aligned full QWORD transactions; otherwise, the result is undefined.

I will remove the first statement from the commit message and add something like the comment you proposed below.

Quoted 8 lines
Quoted 3 lines
Quoted 2 lines

I have an OS which reads single bytes and it works fine on real hardware. Maybe this happens due to casting.

What do you mean by casting?

I did not yet locate the line of code where this happens in the guest. As all other accesses are dword, I assume that there is some masking, shifting or casting to a byte variable in the source code, and the compiler generates a byte access from this.

And I presume it's too late to fix the guest in question?

Quoted 67 lines
Quoted 5 lines
Quoted 1 line

I guess we can merge this, but we need a comment I think since this seems to contradict the spec, and people were sending patches relying on this.

Quoted 8 lines

Does something like the following describe the situation correctly?

/* Note: PCI spec requires that all MSI-X table accesses
are either DWORD or QWORD, size aligned. Some guests seem to violate this rule for read accesses, performing single byte reads. Since it's easy to support this, let's do so.
Also support 16 bit size aligned reads, just in case.
*/

Yes, that's is exactly the situation with my guest. I will add this comment.

Quoted 1 line

Does you guest also do 16 bit reads? Are accesses at least aligned?

I will check this with my guest and the readw function disabled in the patch. This will take some time.

Quoted 45 lines
Quoted 45 lines
---
 hw/msix.c |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
hw/msix.c +16 -4
index d99403a..7dac7f7 100644
@@ -100,10 +100,22 @@ static uint32_t msix_mmio_readl(void *opaque, target_phys_addr_t addr)
     return pci_get_long(page + offset);
 }
 
-static uint32_t msix_mmio_read_unallowed(void *opaque, target_phys_addr_t addr)
+static uint32_t msix_mmio_readw(void *opaque, target_phys_addr_t addr)
 {
-    fprintf(stderr, "MSI-X: only dword read is allowed!\n");
-    return 0;
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&  (MSIX_PAGE_SIZE - 1)&  ~0x1;
+    void *page = dev->msix_table_page;
+
+    return pci_get_word(page + offset);
+}
+
+static uint32_t msix_mmio_readb(void *opaque, target_phys_addr_t addr)
+{
+    PCIDevice *dev = opaque;
+    unsigned int offset = addr&  (MSIX_PAGE_SIZE - 1);
+    void *page = dev->msix_table_page;
+
+    return pci_get_byte(page + offset);
 }
 
 static uint8_t msix_pending_mask(int vector)
@@ -198,7 +210,7 @@ static CPUWriteMemoryFunc * const msix_mmio_write[] = {
 };
 
 static CPUReadMemoryFunc * const msix_mmio_read[] = {
-    msix_mmio_read_unallowed, msix_mmio_read_unallowed, msix_mmio_readl
+    msix_mmio_readb, msix_mmio_readw, msix_mmio_readl
 };
 
 /* Should be called from device's map method. */
Signature · 1 line
1.7.2.1