From mboxrd@z Thu Jan 1 00:00:00 1970 From: Alexander Gordeev Subject: [kvm-unit-tests PATCH v3] pci: Add BAR sanity checks Date: Mon, 27 Mar 2017 16:32:14 +0200 Message-ID: <1490625134-17376-1-git-send-email-agordeev@redhat.com> Cc: Alexander Gordeev , Thomas Huth , Andrew Jones , Peter Xu To: kvm@vger.kernel.org Return-path: Received: from mx1.redhat.com ([209.132.183.28]:35788 "EHLO mx1.redhat.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1752976AbdC0Ocz (ORCPT ); Mon, 27 Mar 2017 10:32:55 -0400 Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 97DCE437F77 for ; Mon, 27 Mar 2017 14:32:35 +0000 (UTC) Sender: kvm-owner@vger.kernel.org List-ID: Cc: Thomas Huth Cc: Andrew Jones Cc: Peter Xu Reviewed-by: Thomas Huth Reviewed-by: Andrew Jones Signed-off-by: Alexander Gordeev --- 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