xen/pci: detect when BARs are not suitably positioned
PATCH v3 1/1 xen/pci: detect when BARs are not suitably positioned
One of the boxes where I was attempting to boot Xen in PVH dom0 mode has quirky firmware, as it will handover with a PCI device with memory decoding enabled and a BAR of size 4K at address 0. Such BAR overlaps with a RAM range on the e820.
This interacts badly with the dom0 PVH build, as BARs will be setup on the p2m before RAM, so if there's a BAR positioned over a RAM region it will trigger a domain crash when the dom0 builder attempts to populate that region with a regular RAM page.
It's in general a very bad idea to have a BAR overlapping with any memory region defined in the memory map, so add some sanity checks for devices that are added with memory decoding enabled in order to assure that BARs are not placed on top of memory regions defined in the memory map. If overlaps are detected just disable the memory decoding bit for the device and expect the hardware domain to properly position the BAR.
Note apply_quirks must be called before check_pdev so that ignore_bars is set when calling the later. PCI_HEADER_{NORMAL,BRIDGE}_NR_BARS needs to be moved into pci_regs.h so it's defined even in the absence of vPCI.
---
Changes since v2:
- Unify warning message and store in a static const var.
- Rename checker function to is_memory_hole.
- Pass an inclusive MFN range to the checker function.
- Remove Arm implementation of is_memory_hole due to lack of feedback.
Changes since v1:
- Add comment regarding pci_size_mem_bar failure.
- Make e820entry const.
- Move is_iomem_range after is_iomem_page.
- Reword error message.
- Make is_iomem_range paddr_t
- Expand commit message.
- Move PCI_HEADER_{NORMAL,BRIDGE}_NR_BARS.
- Only attempt to read ROM BAR if rom_pos != 0.
--- xen/arch/x86/mm.c | 17 +++++++++ xen/drivers/passthrough/pci.c | 71 ++++++++++++++++++++++++++++++++++- xen/include/xen/mm.h | 2 + xen/include/xen/pci_regs.h | 2 + xen/include/xen/vpci.h | 2 - 5 files changed, 91 insertions(+), 3 deletions(-)
xen/arch/x86/mm.c +17 -0
@@ -783,6 +783,23 @@ bool is_iomem_page(mfn_t mfn)return (page_get_owner (page) == dom_io); } +bool is_memory_hole (unsigned long start,unsigned long end) +{ +unsigned int i; + +for ( i =0 ; i < e820.nr_map; i++ ) + { +const struct e820entry *entry = &e820.map[i]; + +/* Do not allow overlaps with any memory range. */ +if ( start <PFN_DOWN (entry->addr + entry->size) && +PFN_DOWN (entry->addr) <= end ) +return false; + } + +return true; +} +static int update_xen_mappings (unsigned long mfn,unsigned int cacheattr) {int err =0 ;
xen/drivers/passthrough/pci.c +70 -1
101 lines not on this page · raw message
xen/include/xen/mm.h +2 -0
@@ -554,6 +554,8 @@ int __must_check steal_page(struct domain *d, struct page_info *page,int page_is_ram_type (unsigned long mfn,unsigned long mem_type);/* Returns the page type(s). */ unsigned int page_get_ram_type (mfn_t mfn); +/* Check if a range falls into a hole in the memory map. */ +bool is_memory_hole (paddr_t start,uint64_t size);/* Prepare/destroy a ring for a dom0 helper. Helper with talk * with Xen on behalf of this domain. */
xen/include/xen/pci_regs.h +2 -0
@@ -88,6 +88,8 @@* 0xffffffff to the register, and reading it back. Only * 1 bits are decoded. */ +#define PCI_HEADER_NORMAL_NR_BARS 6 +#define PCI_HEADER_BRIDGE_NR_BARS 2#define PCI_BASE_ADDRESS_0 0x10/* 32 bits */ #define PCI_BASE_ADDRESS_1 0x14/* 32 bits [htype 0,1 only] */ #define PCI_BASE_ADDRESS_2 0x18/* 32 bits [htype 0 only] */
xen/include/xen/vpci.h +0 -2
@@ -80,8 +80,6 @@ struct vpci { bool prefetchable : 1;/* Store whether the BAR is mapped into guest p2m. */ bool enabled : 1; -#define PCI_HEADER_NORMAL_NR_BARS 6 -#define PCI_HEADER_BRIDGE_NR_BARS 2 } bars[PCI_HEADER_NORMAL_NR_BARS +1 ];/* At most 6 BARS + 1 expansion ROM BAR. */
Signature · 1 line
2.34.1
Quoted 12 lines · Roger Pau Monne
xen/arch/x86/mm.c +6
-0@@ -783,6 +783,23 @@ bool is_iomem_page(mfn_t mfn)return (page_get_owner (page) == dom_io); } +bool is_memory_hole (unsigned long start,unsigned long end) +{ +unsigned int i; + +for ( i =0 ; i < e820.nr_map; i++ ) + {
xen/arch/x86/mm.c bool is_iomem_page(mfn_t mfn) · line 801
Quoted 10 lines
xen/arch/x86/mm.c +10
-0+const struct e820entry *entry = &e820.map[i]; + +/* Do not allow overlaps with any memory range. */ +if ( start <PFN_DOWN (entry->addr + entry->size) && +PFN_DOWN (entry->addr) <= end ) +return false; + } + +return true; +}
Doesn't the left side of the && need to use PFN_UP()?
I also think it would help if a brief comment ahead of the function said that the range is inclusive. Otherwise the use of < and >= gives the impression of something being wrong. Then again it may be better to switch to <= anyway, as I think you want to avoid possible zero-size regions (at which point subtracting 1 for using <= is going to be valid).
Finally I wonder whether the function parameters wouldn't better be named e.g. spfn and epfn, but maybe their units can be inferred from their types being unsigned long (which, however, would build on the assumption that we use appropriate types everywhere).
xen/include/xen/mm.h int __must_check steal_page(struct domain *d, struct page_info *page, · line 558
Quoted 8 lines
xen/include/xen/mm.h +2
-0@@ -554,6 +554,8 @@ int __must_check steal_page(struct domain *d, struct page_info *page,int page_is_ram_type (unsigned long mfn,unsigned long mem_type);/* Returns the page type(s). */ unsigned int page_get_ram_type (mfn_t mfn); +/* Check if a range falls into a hole in the memory map. */ +bool is_memory_hole (paddr_t start,uint64_t size);
While resolving to the same type, these now also want to be "unsigned long".
Jan
Hi,
Quoted 33 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne
xen/arch/x86/mm.c +6
-0@@ -783,6 +783,23 @@ bool is_iomem_page(mfn_t mfn)return (page_get_owner (page) == dom_io); } +bool is_memory_hole (unsigned long start,unsigned long end) +{ +unsigned int i; + +for ( i =0 ; i < e820.nr_map; i++ ) + {xen/arch/x86/mm.c bool is_iomem_page(mfn_t mfn) · line 801
Quoted 10 lines
xen/arch/x86/mm.c +10
-0+const struct e820entry *entry = &e820.map[i]; + +/* Do not allow overlaps with any memory range. */ +if ( start <PFN_DOWN (entry->addr + entry->size) && +PFN_DOWN (entry->addr) <= end ) +return false; + } + +return true; +}Doesn't the left side of the && need to use
PFN_UP()?I also think it would help if a brief comment ahead of the function said that the range is inclusive. Otherwise the use of < and >= gives the impression of something being wrong. Then again it may be better to switch to <= anyway, as I think you want to avoid possible zero-size regions (at which point subtracting 1 for using <= is going to be valid).
Quoted 5 lines
Finally I wonder whether the function parameters wouldn't better be named e.g. spfn and epfn, but maybe their units can be inferred from their types being unsigned long (which, however, would build on the assumption that we use appropriate types everywhere).
I think this a case where mfn_t would be useful to use.
Quoted 13 lines
xen/include/xen/mm.h int __must_check steal_page(struct domain *d, struct page_info *page, · line 558
Quoted 9 lines
xen/include/xen/mm.h +2
-0@@ -554,6 +554,8 @@ int __must_check steal_page(struct domain *d, struct page_info *page,int page_is_ram_type (unsigned long mfn,unsigned long mem_type);/* Returns the page type(s). */ unsigned int page_get_ram_type (mfn_t mfn); +/* Check if a range falls into a hole in the memory map. */ +bool is_memory_hole (paddr_t start,uint64_t size);While resolving to the same type, these now also want to be "unsigned long".
Quoted 2 lines
Jan
Cheers,
Signature · 1 line
Julien Grall
Quoted 34 lines · Julien Grall
Quoted 33 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne
xen/arch/x86/mm.c +6
-0@@ -783,6 +783,23 @@ bool is_iomem_page(mfn_t mfn)return (page_get_owner (page) == dom_io); } +bool is_memory_hole (unsigned long start,unsigned long end) +{ +unsigned int i; + +for ( i =0 ; i < e820.nr_map; i++ ) + {xen/arch/x86/mm.c bool is_iomem_page(mfn_t mfn) · line 801
Quoted 10 lines
xen/arch/x86/mm.c +10
-0+const struct e820entry *entry = &e820.map[i]; + +/* Do not allow overlaps with any memory range. */ +if ( start <PFN_DOWN (entry->addr + entry->size) && +PFN_DOWN (entry->addr) <= end ) +return false; + } + +return true; +}Doesn't the left side of the && need to use
PFN_UP()?I also think it would help if a brief comment ahead of the function said that the range is inclusive. Otherwise the use of < and >= gives the impression of something being wrong. Then again it may be better to switch to <= anyway, as I think you want to avoid possible zero-size regions (at which point subtracting 1 for using <= is going to be valid).
Quoted 7 lines
Quoted 5 lines
Finally I wonder whether the function parameters wouldn't better be named e.g. spfn and epfn, but maybe their units can be inferred from their types being unsigned long (which, however, would build on the assumption that we use appropriate types everywhere).
I think this a case where
mfn_twould be useful to use.
Actually I did consider to suggest it when asking to convert to frame numbers, and specifically didn't because its use will clutter the code here quite a bit. Which isn't to mean I'd object if the adjustment was made ...
Jan
Hi Jan,
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Julien Grall
Quoted 38 lines · Jan Beulich
Quoted 12 lines · Roger Pau Monne
xen/arch/x86/mm.c +6
-0@@ -783,6 +783,23 @@ bool is_iomem_page(mfn_t mfn)return (page_get_owner (page) == dom_io); } +bool is_memory_hole (unsigned long start,unsigned long end) +{ +unsigned int i; + +for ( i =0 ; i < e820.nr_map; i++ ) + {xen/arch/x86/mm.c bool is_iomem_page(mfn_t mfn) · line 801
Quoted 10 lines
xen/arch/x86/mm.c +10
-0+const struct e820entry *entry = &e820.map[i]; + +/* Do not allow overlaps with any memory range. */ +if ( start <PFN_DOWN (entry->addr + entry->size) && +PFN_DOWN (entry->addr) <= end ) +return false; + } + +return true; +}Doesn't the left side of the && need to use
PFN_UP()?I also think it would help if a brief comment ahead of the function said that the range is inclusive. Otherwise the use of < and >= gives the impression of something being wrong. Then again it may be better to switch to <= anyway, as I think you want to avoid possible zero-size regions (at which point subtracting 1 for using <= is going to be valid).
Finally I wonder whether the function parameters wouldn't better be named e.g. spfn and epfn, but maybe their units can be inferred from their types being unsigned long (which, however, would build on the assumption that we use appropriate types everywhere).
Quoted 6 lines
Quoted 1 line
I think this a case where
mfn_twould be useful to use.Actually I did consider to suggest it when asking to convert to frame numbers, and specifically didn't because its use will clutter the code here quite a bit. Which isn't to mean I'd object if the adjustment was made ...
I thought about code cluterring, but there are two use of the variables. So it would not look too bad.
But I care more about the external interface to be typesafe. So an alternative would be:
unsigned long smfn_ = mfn_x();
unsigned long emfn_ = mfn_x();
Cheers,
Signature · 1 line
Julien Grall
Quoted 17 lines · Jan Beulich
Quoted 16 lines · Roger Pau Monne
xen/arch/x86/mm.c +10
-0@@ -783,6 +783,23 @@ bool is_iomem_page(mfn_t mfn)return (page_get_owner (page) == dom_io); } +bool is_memory_hole (unsigned long start,unsigned long end) +{ +unsigned int i; + +for ( i =0 ; i < e820.nr_map; i++ ) + { +const struct e820entry *entry = &e820.map[i]; + +/* Do not allow overlaps with any memory range. */ + if ( start < PFN_DOWN(entry->addr + entry->size) &&
Quoted 8 lines
xen/arch/x86/mm.c bool is_iomem_page(mfn_t mfn) · line 801
Quoted 6 lines
xen/arch/x86/mm.c +6
-0+ PFN_DOWN(entry->addr) <= end ) +return false; + } + +return true; +}Doesn't the left side of the && need to use
PFN_UP()?
Hm, I had is using PFN_UP before and switched to PFN_DOWN for some weird reasoning.
Quoted 7 lines
I also think it would help if a brief comment ahead of the function said that the range is inclusive. Otherwise the use of < and >= gives the impression of something being wrong. Then again it may be better to switch to <= anyway, as I think you want to avoid possible zero-size regions (at which point subtracting 1 for using <= is going to be valid).
Right, so that would end up being:
start <= PFN_DOWN(entry->addr + entry->size - 1) &&
Rejecting entries with size == 0 beforehand.
Quoted 5 lines
Finally I wonder whether the function parameters wouldn't better be named e.g. spfn and epfn, but maybe their units can be inferred from their types being unsigned long (which, however, would build on the assumption that we use appropriate types everywhere).
I guess I should switch to using mfn_t for the types and convert them locally to unsigned long for the comparisons.
Quoted 11 lines
xen/include/xen/mm.h int __must_check steal_page(struct domain *d, struct page_info *page, · line 558
Quoted 8 lines
xen/include/xen/mm.h +2
-0@@ -554,6 +554,8 @@ int __must_check steal_page(struct domain *d, struct page_info *page,int page_is_ram_type (unsigned long mfn,unsigned long mem_type);/* Returns the page type(s). */ unsigned int page_get_ram_type (mfn_t mfn); +/* Check if a range falls into a hole in the memory map. */ +bool is_memory_hole (paddr_t start,uint64_t size);While resolving to the same type, these now also want to be "unsigned long".
Doh, yes, sorry. Will convert them to mfn_t if we agree on that.
Thanks, Roger.
Quoted 14 lines · Roger Pau Monné
Quoted 12 lines · Jan Beulich
xen/include/xen/mm.h int __must_check steal_page(struct domain *d, struct page_info *page, · line 558
Quoted 8 lines · Roger Pau Monne
xen/include/xen/mm.h +2
-0@@ -554,6 +554,8 @@ int __must_check steal_page(struct domain *d, struct page_info *page,int page_is_ram_type (unsigned long mfn,unsigned long mem_type);/* Returns the page type(s). */ unsigned int page_get_ram_type (mfn_t mfn); +/* Check if a range falls into a hole in the memory map. */ +bool is_memory_hole (paddr_t start,uint64_t size);While resolving to the same type, these now also want to be "unsigned long".
Quoted 1 line
Doh, yes, sorry. Will convert them to
mfn_tif we agree on that.
As said in reply to Julien - I don't mind the change, but in this particular case I also don't view it as strictly necessary / useful.
Jan