Skip to contentSkip to conversation

hw/pci: catch a few error cases

PATCH In review Nicholas Piggin 4 participants 8 messages Jan 17, 2025 – Jan 22, 2025

lore
Share & tools
lore
#0 lore raw

cover hw/pci: catch a few error cases

Hi,

These would have helped to catch a few bugs I ran into / created recently, so they might be worth upstreaming.

Thanks,
Nick

Nicholas Piggin (2):
hw/pci/msix: Warn on PBA writes
hw/pci: Assert a bar is not registered multiple times

 hw/pci/msix.c | 9 +++++++++
 hw/pci/pci.c  | 1 +
 2 files changed, 10 insertions(+)
Signature · 1 line
2.45.2
#1 lore raw

PATCH 1/2 hw/pci/msix: Warn on PBA writes

Of the MSI-X PBA pending bits, the PCI Local Bus Specification says:

Software should never write, and should only read Pending Bits. If software writes to Pending Bits, the result is undefined.

Log a GUEST_ERROR message if the PBA is written to by software.

Cc Michael S. Tsirkin <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc Dmitry Fleytman <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Sriram Yagnaraman <[email protected]>
---
 hw/pci/msix.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/pci/msix.c +9 -0
index 57ec7084a47..66f27b9d712 100644
@@ -15,6 +15,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/log.h"
 #include "hw/pci/msi.h"
 #include "hw/pci/msix.h"
 #include "hw/pci/pci.h"
@@ -260,6 +261,14 @@ static uint64_t msix_pba_mmio_read(void *opaque, hwaddr addr,
 static void msix_pba_mmio_write(void *opaque, hwaddr addr,
                                 uint64_t val, unsigned size)
 {
+    PCIDevice *dev = opaque;
+
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "PCI [%s:%02x:%02x.%x] attempt to write to MSI-X "
+                  "PBA at 0x%" FMT_PCIBUS ", ignoring.\n",
+                  pci_root_bus_path(dev), pci_dev_bus_num(dev),
+                  PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
+                  addr);
 }
 
 static const MemoryRegionOps msix_pba_mmio_ops = {
Signature · 1 line
2.45.2
Reviewed-by on v1 1/2
#2 lore raw

Re: [PATCH 1/2] hw/pci/msix: Warn on PBA writes

Quoted 6 lines · Nicholas Piggin

Of the MSI-X PBA pending bits, the PCI Local Bus Specification says:

Software should never write, and should only read Pending Bits. If software writes to Pending Bits, the result is undefined.

Quoted 8 lines

Log a GUEST_ERROR message if the PBA is written to by software.

Cc Michael S. Tsirkin <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc Dmitry Fleytman <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Sriram Yagnaraman <[email protected]>
Reviewed-by on v1 1/2
#3 lore raw

Re: [PATCH 1/2] hw/pci/msix: Warn on PBA writes

In reply to Nicholas Piggin
Quoted 6 lines · Nicholas Piggin

Of the MSI-X PBA pending bits, the PCI Local Bus Specification says:

Software should never write, and should only read Pending Bits. If software writes to Pending Bits, the result is undefined.

Quoted 9 lines

Log a GUEST_ERROR message if the PBA is written to by software.

Cc Michael S. Tsirkin <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc Dmitry Fleytman <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Sriram Yagnaraman <[email protected]>
Quoted 37 lines
---
 hw/pci/msix.c | 9 +++++++++
 1 file changed, 9 insertions(+)
hw/pci/msix.c +9 -0
index 57ec7084a47..66f27b9d712 100644
@@ -15,6 +15,7 @@
  */
 
 #include "qemu/osdep.h"
+#include "qemu/log.h"
 #include "hw/pci/msi.h"
 #include "hw/pci/msix.h"
 #include "hw/pci/pci.h"
@@ -260,6 +261,14 @@ static uint64_t msix_pba_mmio_read(void *opaque,
 hwaddr addr,
 static void msix_pba_mmio_write(void *opaque, hwaddr addr,
                                 uint64_t val, unsigned size)
 {
+    PCIDevice *dev = opaque;
+
+    qemu_log_mask(LOG_GUEST_ERROR,
+                  "PCI [%s:%02x:%02x.%x] attempt to write to MSI-X "
+                  "PBA at 0x%" FMT_PCIBUS ", ignoring.\n",
+                  pci_root_bus_path(dev), pci_dev_bus_num(dev),
+                  PCI_SLOT(dev->devfn), PCI_FUNC(dev->devfn),
+                  addr);
 }
 

static const MemoryRegionOps msix_pba_mmio_ops = {
--
2.45.2

Reviewed-by on v1 1/2
#4 lore raw

Re: [PATCH 1/2] hw/pci/msix: Warn on PBA writes

In reply to Nicholas Piggin
Quoted 14 lines · Nicholas Piggin

Of the MSI-X PBA pending bits, the PCI Local Bus Specification says:

Software should never write, and should only read Pending Bits. If software writes to Pending Bits, the result is undefined.

Log a GUEST_ERROR message if the PBA is written to by software.

Cc Michael S. Tsirkin <[email protected]>
Cc Marcel Apfelbaum <[email protected]>
Cc Dmitry Fleytman <[email protected]>
Cc Akihiko Odaki <[email protected]>
Cc Sriram Yagnaraman <[email protected]>
Quoted 3 lines
---
  hw/pci/msix.c | 9 +++++++++
  1 file changed, 9 insertions(+)
#5 lore raw

PATCH 2/2 hw/pci: Assert a bar is not registered multiple times

In reply to Nicholas Piggin

Nothing should be doing this, but it doesn't get caught by pci_register_bar(). Add an assertion to prevent misuse.

---
 hw/pci/pci.c | 1 +
 1 file changed, 1 insertion(+)
hw/pci/pci.c +1 -0
index 2afa423925c..b067a55c5bc 100644
@@ -1391,6 +1391,7 @@ void pci_register_bar(PCIDevice *pci_dev, int region_num,
     assert(hdr_type != PCI_HEADER_TYPE_BRIDGE || region_num < 2);
 
     r = &pci_dev->io_regions[region_num];
+    assert(!r->size);
     r->addr = PCI_BAR_UNMAPPED;
     r->size = size;
     r->type = type;
Signature · 1 line
2.45.2
Reviewed-by on v1 2/2
#6 lore raw

Re: [PATCH 2/2] hw/pci: Assert a bar is not registered multiple times

Looks good to me. There is a risk here that the assertion will fail on existing code. (Unless you've rigorously audited all callers, which would be quite the task.) However, I agree that this would constitute a bug in the calling code, not an issue with this change. Since we've still got a few months left in the 10.0 release cycle, I say go for it - hopefully such bugs, if there are any, will be shaken out over the next few weeks.

Quoted 5 lines · Nicholas Piggin

Nothing should be doing this, but it doesn't get caught by pci_register_bar(). Add an assertion to prevent misuse.

Quoted 22 lines
---
 hw/pci/pci.c | 1 +
 1 file changed, 1 insertion(+)
hw/pci/pci.c +1 -0
index 2afa423925c..b067a55c5bc 100644
@@ -1391,6 +1391,7 @@ void pci_register_bar(PCIDevice *pci_dev, int
 region_num,
     assert(hdr_type != PCI_HEADER_TYPE_BRIDGE || region_num < 2);
 
     r = &pci_dev->io_regions[region_num];
+    assert(!r->size);
     r->addr = PCI_BAR_UNMAPPED;
     r->size = size;
     r->type = type;

--
2.45.2

#7 lore raw

Re: [PATCH 2/2] hw/pci: Assert a bar is not registered multiple times

Quoted 6 lines · Phil Dennis-Jordan

Looks good to me. There is a risk here that the assertion will fail on existing code. (Unless you've rigorously audited all callers, which would be quite the task.) However, I agree that this would constitute a bug in the calling code, not an issue with this change. Since we've still got a few months left in the 10.0 release cycle, I say go for it - hopefully such bugs, if there are any, will be shaken out over the next few weeks.

You're right I didn't do an exhaustive audit or test beyond CI and some browsing. I think it would be quite buggy already if this happens so we should just catch and fix it quickly, but happy to change to a warning first if anybody is concerned.

Thanks,
Nick

Quoted 33 lines · Nicholas Piggin
Quoted 6 lines

Nothing should be doing this, but it doesn't get caught by pci_register_bar(). Add an assertion to prevent misuse.

Quoted 22 lines
---
 hw/pci/pci.c | 1 +
 1 file changed, 1 insertion(+)
hw/pci/pci.c +1 -0
index 2afa423925c..b067a55c5bc 100644
@@ -1391,6 +1391,7 @@ void pci_register_bar(PCIDevice *pci_dev, int
 region_num,
     assert(hdr_type != PCI_HEADER_TYPE_BRIDGE || region_num < 2);
 
     r = &pci_dev->io_regions[region_num];
+    assert(!r->size);
     r->addr = PCI_BAR_UNMAPPED;
     r->size = size;
     r->type = type;

--
2.45.2