[kvm-unit-tests PATCH v2 3/4] pci: Ensure BAR validity for public functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Cc: Thomas Huth <thuth@xxxxxxxxxx>
Cc: Andrew Jones <drjones@xxxxxxxxxx>
Signed-off-by: Alexander Gordeev <agordeev@xxxxxxxxxx>
---
 lib/pci.c | 9 +++++++++
 1 file changed, 9 insertions(+)

diff --git a/lib/pci.c b/lib/pci.c
index 7a3e505ab314..144bec755c8d 100644
--- a/lib/pci.c
+++ b/lib/pci.c
@@ -35,6 +35,8 @@ uint32_t pci_bar_mask(uint32_t bar)
 
 uint32_t pci_bar_get(pcidevaddr_t dev, int bar_num)
 {
+	assert(bar_num >= 0 && bar_num < 6);
+
 	return pci_config_readl(dev, PCI_BASE_ADDRESS_0 + bar_num * 4);
 }
 
@@ -45,6 +47,9 @@ phys_addr_t pci_bar_get_addr(pcidevaddr_t dev, int bar_num)
 	uint64_t addr = bar & mask;
 	phys_addr_t phys_addr;
 
+	if (!pci_bar_exists(dev, bar_num))
+		return INVALID_PHYS_ADDR;
+
 	if (pci_bar_is64(dev, bar_num))
 		addr |= (uint64_t)pci_bar_get(dev, bar_num + 1) << 32;
 
@@ -58,6 +63,8 @@ void pci_bar_set_addr(pcidevaddr_t dev, int bar_num, phys_addr_t addr)
 {
 	int off = PCI_BASE_ADDRESS_0 + bar_num * 4;
 
+	assert(pci_bar_exists(dev, bar_num));
+
 	pci_config_writel(dev, off, (uint32_t)addr);
 
 	if (pci_bar_is64(dev, bar_num))
@@ -91,6 +98,8 @@ phys_addr_t pci_bar_size(pcidevaddr_t dev, int bar_num)
 {
 	uint32_t bar, size;
 
+	assert(pci_bar_is_valid(dev, bar_num));
+
 	size = pci_bar_size_helper(dev, bar_num);
 	if (!size)
 		return 0;
-- 
1.8.3.1




[Index of Archives]     [KVM ARM]     [KVM ia64]     [KVM ppc]     [Virtualization Tools]     [Spice Development]     [Libvirt]     [Libvirt Users]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite Questions]     [Linux Kernel]     [Linux SCSI]     [XFree86]
  Powered by Linux