kvm.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Alexander Gordeev <agordeev@redhat.com>
To: kvm@vger.kernel.org
Cc: Alexander Gordeev <agordeev@redhat.com>,
	Thomas Huth <thuth@redhat.com>, Andrew Jones <drjones@redhat.com>,
	Peter Xu <peterx@redhat.com>
Subject: [kvm-unit-tests PATCH v3] pci: Add BAR sanity checks
Date: Mon, 27 Mar 2017 16:32:14 +0200	[thread overview]
Message-ID: <1490625134-17376-1-git-send-email-agordeev@redhat.com> (raw)

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

                 reply	other threads:[~2017-03-27 14:32 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=1490625134-17376-1-git-send-email-agordeev@redhat.com \
    --to=agordeev@redhat.com \
    --cc=drjones@redhat.com \
    --cc=kvm@vger.kernel.org \
    --cc=peterx@redhat.com \
    --cc=thuth@redhat.com \
    /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).