kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [kvm-unit-tests PATCH v3] pci: Add BAR sanity checks
@ 2017-03-27 14:32 Alexander Gordeev
  0 siblings, 0 replies; only message in thread
From: Alexander Gordeev @ 2017-03-27 14:32 UTC (permalink / raw)
  To: kvm; +Cc: Alexander Gordeev, Thomas Huth, Andrew Jones, Peter Xu

Cc: Thomas Huth <thuth@redhat.com>
Cc: Andrew Jones <drjones@redhat.com>
Cc: Peter Xu <peterx@redhat.com>
Reviewed-by: Thomas Huth <thuth@redhat.com>
Reviewed-by: Andrew Jones <drjones@redhat.com>
Signed-off-by: Alexander Gordeev <agordeev@redhat.com>
---
 lib/pci.c | 14 +++++++++++++-
 lib/pci.h |  3 +++
 2 files changed, 16 insertions(+), 1 deletion(-)

diff --git a/lib/pci.c b/lib/pci.c
index daf398100b7e..bbd576344049 100644
--- a/lib/pci.c
+++ b/lib/pci.c
@@ -112,6 +112,8 @@ uint32_t pci_bar_mask(uint32_t bar)
 
 uint32_t pci_bar_get(struct pci_dev *dev, int bar_num)
 {
+	ASSERT_BAR_NUM(bar_num);
+
 	return pci_config_readl(dev->bdf, PCI_BASE_ADDRESS_0 +
 				bar_num * 4);
 }
@@ -134,6 +136,8 @@ static phys_addr_t __pci_bar_get_addr(struct pci_dev *dev, int bar_num)
 
 phys_addr_t pci_bar_get_addr(struct pci_dev *dev, int bar_num)
 {
+	ASSERT_BAR_NUM(bar_num);
+
 	return dev->resource[bar_num];
 }
 
@@ -141,11 +145,19 @@ void pci_bar_set_addr(struct pci_dev *dev, int bar_num, phys_addr_t addr)
 {
 	int off = PCI_BASE_ADDRESS_0 + bar_num * 4;
 
+	assert(addr != INVALID_PHYS_ADDR);
+	assert(dev->resource[bar_num] != INVALID_PHYS_ADDR);
+
+	ASSERT_BAR_NUM(bar_num);
+	if (pci_bar_is64(dev, bar_num))
+		ASSERT_BAR_NUM(bar_num + 1);
+	else
+		assert((addr >> 32) == 0);
+
 	pci_config_writel(dev->bdf, off, (uint32_t)addr);
 	dev->resource[bar_num] = addr;
 
 	if (pci_bar_is64(dev, bar_num)) {
-		assert(bar_num + 1 < PCI_BAR_NUM);
 		pci_config_writel(dev->bdf, off + 4, (uint32_t)(addr >> 32));
 		dev->resource[bar_num + 1] = dev->resource[bar_num];
 	}
diff --git a/lib/pci.h b/lib/pci.h
index 03cc0a72d48d..689f03ca7647 100644
--- a/lib/pci.h
+++ b/lib/pci.h
@@ -18,6 +18,9 @@ enum {
 #define PCI_BAR_NUM                     6
 #define PCI_DEVFN_MAX                   256
 
+#define ASSERT_BAR_NUM(bar_num)	\
+	do { assert(bar_num >= 0 && bar_num < PCI_BAR_NUM); } while (0)
+
 #define PCI_BDF_GET_DEVFN(x)            ((x) & 0xff)
 #define PCI_BDF_GET_BUS(x)              (((x) >> 8) & 0xff)
 
-- 
1.8.3.1

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2017-03-27 14:32 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2017-03-27 14:32 [kvm-unit-tests PATCH v3] pci: Add BAR sanity checks Alexander Gordeev

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).