vPCI capabilities filtering
v2 cover vPCI capabilities filtering
This small series enables vPCI to filter which PCI capabilities we expose to a domU. This series adds vPCI register handlers within xen/drivers/vpci/header.c:init_bars(), along with some supporting functions.
Note there are minor rebase conflicts with the in-progress vPCI series [1]. These conflicts fall into the category of functions and code being added adjacent to one another, so are easily resolved. I did not identify any dependency on the vPCI locking work, and the two series deal with different aspects of emulating the PCI header.
Future work may involve adding handlers for more registers in the vPCI header, such as STATUS, VID/DID, etc. Future work may also involve exposing additional capabilities to the guest for broader device/driver support.
v1->v2:
* squash helper functions into the patch where they are used to avoid transient dead code situation
* add new RFC patch, possibly throwaway, to get an idea of what it would look like to get rid of the (void *)(uintptr_t) cast by introducing a new memory allocation
[1] https://lists.xenproject.org/archives/html/xen-devel/2023-07/msg01281.html
Stewart Hildebrand (2):
xen/vpci: header: filter PCI capabilities
[RFC] xen/vpci: header: avoid cast for value passed to vpci_read_val
xen/drivers/pci/pci.c | 18 +++++----- xen/drivers/vpci/header.c | 73 +++++++++++++++++++++++++++++++++++++++ xen/drivers/vpci/vpci.c | 18 ++++++++++ xen/include/xen/pci.h | 3 +- xen/include/xen/vpci.h | 8 +++++ 5 files changed, 110 insertions(+), 10 deletions(-)
base-commit: d0eabe3eaf0db5b78843095a2918d50961e99e96
Signature · 1 line
2.41.0
PATCH v2 1/2 xen/vpci: header: filter PCI capabilities
Currently, Xen vPCI only supports virtualizing the MSI and MSI-X capabilities. Hide all other PCI capabilities (including extended capabilities) from domUs for now, even though there may be certain devices/drivers that depend on being able to discover certain capabilities.
We parse the physical PCI capabilities linked list and add vPCI register handlers for the next elements, inserting our own next value, thus presenting a modified linked list to the domU.
Introduce helper functions vpci_hw_read8 and vpci_read_val. The vpci_read_val helper function returns a fixed value, which may be used for RAZ registers, or registers whose value doesn't change.
Adapt pci_find_next_cap() to suit our needs, which has no existing callers.
---
If there are no capabilities to be exposed to the guest, a future status register handler likely would want to mask the PCI_STATUS_CAP_LIST bit. See [1] for a suggestion of how this might be tracked in struct vpci_header.
RFC: I'm not a fan of the (void *)(uintptr_t) cast for passing a value to
vpci_read_val, but without the cast, the only alternative I could think of would be to introduce a new memory allocation. See the next patch for an example of what this might look like.
v1->v2:
* change type of ttl to int
* use switch statement instead of if/else
* adapt existing pci_find_next_cap helper instead of rolling our own
* pass ttl as in/out
* "pass through" the lower 2 bits of the next pointer
* squash helper functions into this patch to avoid transient dead code situation
* extended capabilities RAZ/WI
[1] https://lists.xenproject.org/archives/html/xen-devel/2023-08/msg01173.html
--- xen/drivers/pci/pci.c | 18 +++++------ xen/drivers/vpci/header.c | 66 +++++++++++++++++++++++++++++++++++++++ xen/drivers/vpci/vpci.c | 12 +++++++ xen/include/xen/pci.h | 3 +- xen/include/xen/vpci.h | 5 +++ 5 files changed, 94 insertions(+), 10 deletions(-)
xen/drivers/pci/pci.c +9 -9
@@ -39,27 +39,27 @@ int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap)return 0 ; } -int pci_find_next_cap (u16 seg,u8 bus,unsigned int devfn,u8 pos,int cap) +int pci_find_next_cap (pci_sbdf_t sbdf,uint8_t pos,bool (*is_match)(uint8_t ), +int *ttl) { -u8 id; -int ttl =48 ; +uint8_t id; -while ( ttl-- ) +while ( (*ttl)-- >0 ) { - pos =pci_conf_read8 (PCI_SBDF (seg, bus, devfn), pos); + pos =pci_conf_read8 (sbdf, pos);if ( pos <0x40 )break ; - pos &= ~3 ; - id =pci_conf_read8 (PCI_SBDF (seg, bus, devfn), pos +PCI_CAP_LIST_ID ); + id =pci_conf_read8 (sbdf, (pos & ~3 ) +PCI_CAP_LIST_ID );if ( id ==0xff )break ; -if ( id == cap ) +if (is_match (id) )return pos; - pos +=PCI_CAP_LIST_NEXT ; + pos = (pos & ~3 ) +PCI_CAP_LIST_NEXT ; } +return 0 ; }
xen/drivers/vpci/header.c +66 -0
78 lines not on this page · raw message
xen/drivers/vpci/vpci.c +12 -0
@@ -133,6 +133,18 @@ static void cf_check vpci_ignored_write( { } +uint32_t cf_check vpci_read_val( +const struct pci_dev *pdev,unsigned int reg,void *data) +{ +return (uintptr_t )data; +} + +uint32_t cf_check vpci_hw_read8( +const struct pci_dev *pdev,unsigned int reg,void *data) +{ +return pci_conf_read8 (pdev->sbdf, reg); +} + uint32_t cf_check vpci_hw_read16(const struct pci_dev *pdev,unsigned int reg,void *data) {
xen/include/xen/pci.h +2 -1
@@ -194,7 +194,8 @@ int pci_mmcfg_read(unsigned int seg, unsigned int bus,int pci_mmcfg_write (unsigned int seg,unsigned int bus,unsigned int devfn,int reg,int len,u32 value);int pci_find_cap_offset (u16 seg,u8 bus,u8 dev,u8 func,u8 cap); -int pci_find_next_cap (u16 seg,u8 bus,unsigned int devfn,u8 pos,int cap); +int pci_find_next_cap (pci_sbdf_t sbdf,uint8_t pos,bool (*is_match)(uint8_t ), +int *ttl);int pci_find_ext_capability (int seg,int bus,int devfn,int cap);int pci_find_next_ext_capability (int seg,int bus,int devfn,int pos,int cap); const char *parse_pci(const char *, unsigned int *seg, unsigned int *bus,
xen/include/xen/vpci.h +5 -0
@@ -45,7 +45,12 @@ uint32_t vpci_read(pci_sbdf_t sbdf, unsigned int reg, unsigned int size);void vpci_write (pci_sbdf_t sbdf,unsigned int reg,unsigned int size,uint32_t data); +uint32_t cf_check vpci_read_val( +const struct pci_dev *pdev,unsigned int reg,void *data); +/* Passthrough handlers. */ +uint32_t cf_check vpci_hw_read8( +const struct pci_dev *pdev,unsigned int reg,void *data); uint32_t cf_check vpci_hw_read16(const struct pci_dev *pdev,unsigned int reg,void *data); uint32_t cf_check vpci_hw_read32(
Signature · 1 line
2.41.0
Re: [PATCH v2 1/2] xen/vpci: header: filter PCI capabilities
Quoted 3 lines · Stewart Hildebrand
If there are no capabilities to be exposed to the guest, a future status register handler likely would want to mask the
PCI_STATUS_CAP_LISTbit. See [1] for a suggestion of how this might be tracked in structvpci_header.
Can we actually get away without doing this right away? I'm not sure consumers are required to range check what they read from PCI_CAPABILITY_LIST.
xen/drivers/pci/pci.c int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap) · line 43
Quoted 9 lines
xen/drivers/pci/pci.c +2
-1@@ -39,27 +39,27 @@ int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap)return 0 ; } -int pci_find_next_cap (u16 seg,u8 bus,unsigned int devfn,u8 pos,int cap) +int pci_find_next_cap (pci_sbdf_t sbdf,uint8_t pos,bool (*is_match)(uint8_t ), +int *ttl)
Why plain int? When values can't go negative, respective variables generally want to be of unsigned types.
xen/drivers/pci/pci.c int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap) · line 47
Quoted 7 lines
xen/drivers/pci/pci.c +2
-3{ -u8 id; -int ttl =48 ; +uint8_t id; -while ( ttl-- ) +while ( (*ttl)-- >0 )
I don't see why you add "> 0" here.
Quoted 12 lines
xen/drivers/pci/pci.c +2
-3{ - pos =pci_conf_read8 (PCI_SBDF (seg, bus, devfn), pos); + pos =pci_conf_read8 (sbdf, pos);if ( pos <0x40 )break ; - pos &= ~3 ; - id =pci_conf_read8 (PCI_SBDF (seg, bus, devfn), pos +PCI_CAP_LIST_ID ); + id =pci_conf_read8 (sbdf, (pos & ~3 ) +PCI_CAP_LIST_ID );if ( id ==0xff )break ;
xen/drivers/pci/pci.c int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap) · line 64
Quoted 10 lines
xen/drivers/pci/pci.c +3
-2-if ( id == cap ) +if (is_match (id) )return pos; - pos +=PCI_CAP_LIST_NEXT ; + pos = (pos & ~3 ) +PCI_CAP_LIST_NEXT ; } +return 0 ; }
As said in context of v1, this function should remain usable for its original purpose. That, to me, includes the caller not needing to care about ttl. I could see you convert the original function the way you do, but under a new name, and then implement the original one simply in terms of this more general purpose function.
Also, while I appreciate the sbdf conversion, that wants to be a separate patch, which would then want to take care of the sibling functions as well.
Quoted 22 lines
xen/drivers/vpci/header.c +12
-0@@ -513,6 +513,18 @@ static void cf_check rom_write( rom->addr = val &PCI_ROM_ADDRESS_MASK ; } +static bool cf_check vpci_cap_supported(uint8_t id) +{ +switch ( id ) + { +case PCI_CAP_ID_MSI : +case PCI_CAP_ID_MSIX : +return true; +default : +return false; + } +} + static int cf_check init_bars(struct pci_dev *pdev) {uint16_t cmd;
xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 562
Quoted 7 lines
xen/drivers/vpci/header.c +4
-0@@ -544,6 +556,60 @@ static int cf_check init_bars(struct pci_dev *pdev)if ( rc )return rc; +if ( !is_hardware_domain (pdev->domain) ) + { +if ( (pci_conf_read16 (pdev->sbdf,PCI_STATUS ) &PCI_STATUS_CAP_LIST ) + ==0 )
This fits on a single line when written this more commonly used way:
if ( !(pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST) )
Otherwise it needs wrapping differently - binary operators at a wrapping point belong on the earlier line in our style.
xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 566
Quoted 4 lines
xen/drivers/vpci/header.c +4
-0+ { +/* RAZ/WI */ + rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL , +PCI_CAPABILITY_LIST ,1 ,NULL );
This last NULL is likely misleading to readers: It does not obviously represent the value 0 cast to void *. (Same then for the extended cap handler at the end.)
Quoted 9 lines
xen/drivers/vpci/header.c +9
-0+if ( rc ) +return rc; + } +else + { +/* Only expose capabilities to the guest that vPCI can handle. */ +uint8_t next; +int ttl =48 ; +
xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 585
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+ next =pci_find_next_cap (pdev->sbdf,PCI_CAPABILITY_LIST , + vpci_cap_supported, &ttl); + + rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL , +PCI_CAPABILITY_LIST ,1 , + (void *)(uintptr_t )next); +if ( rc ) +return rc; + +while ( next && (ttl >0 ) )
Don't you need to mask off the low two bits first (rather than [only] ...
Quoted 8 lines
xen/drivers/vpci/header.c +8
-0+ { +uint8_t pos = next; + + next =pci_find_next_cap (pdev->sbdf, pos +PCI_CAP_LIST_NEXT , + vpci_cap_supported, &ttl); + + rc =vpci_add_register (pdev->vpci, vpci_hw_read8,NULL , + pos +PCI_CAP_LIST_ID ,1 ,NULL );
xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 603
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+if ( rc ) +return rc; + + rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL , + pos +PCI_CAP_LIST_NEXT ,1 , + (void *)(uintptr_t )next); +if ( rc ) +return rc; + + next &= ~3 ;
... here)?
Jan
Re: [PATCH v2 1/2] xen/vpci: header: filter PCI capabilities
Quoted 7 lines · Jan Beulich
Quoted 3 lines · Stewart Hildebrand
If there are no capabilities to be exposed to the guest, a future status register handler likely would want to mask the
PCI_STATUS_CAP_LISTbit. See [1] for a suggestion of how this might be tracked in structvpci_header.Can we actually get away without doing this right away? I'm not sure consumers are required to range check what they read from
PCI_CAPABILITY_LIST.
I think you're right, this should be done right away. I'll add a status register handler with PCI_STATUS_CAP_LIST bit masking in the next version of the series. This will include a modification to vpci_write_helper() be able to handle rw1c registers properly.
Quoted 6 lines
Quoted 6 lines
xen/drivers/pci/pci.c +0
-0@@ -39,27 +39,27 @@ int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap)return 0 ; }
Quoted 6 lines
xen/drivers/pci/pci.c int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap) · line 43
Quoted 3 lines
xen/drivers/pci/pci.c +2
-1-int pci_find_next_cap (u16 seg,u8 bus,unsigned int devfn,u8 pos,int cap) +int pci_find_next_cap (pci_sbdf_t sbdf,uint8_t pos,bool (*is_match)(uint8_t ), +int *ttl)Why plain int? When values can't go negative, respective variables generally want to be of unsigned types.
I'll change to unsigned int.
Quoted 9 lines
xen/drivers/pci/pci.c int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap) · line 47
Quoted 7 lines
xen/drivers/pci/pci.c +2
-3{ -u8 id; -int ttl =48 ; +uint8_t id; -while ( ttl-- ) +while ( (*ttl)-- >0 )I don't see why you add "> 0" here.
I'll remove it.
Quoted 23 lines
Quoted 12 lines
xen/drivers/pci/pci.c +2
-3{ - pos =pci_conf_read8 (PCI_SBDF (seg, bus, devfn), pos); + pos =pci_conf_read8 (sbdf, pos);if ( pos <0x40 )break ; - pos &= ~3 ; - id =pci_conf_read8 (PCI_SBDF (seg, bus, devfn), pos +PCI_CAP_LIST_ID ); + id =pci_conf_read8 (sbdf, (pos & ~3 ) +PCI_CAP_LIST_ID );if ( id ==0xff )break ;xen/drivers/pci/pci.c int pci_find_cap_offset(u16 seg, u8 bus, u8 dev, u8 func, u8 cap) · line 64
Quoted 10 lines
xen/drivers/pci/pci.c +3
-2-if ( id == cap ) +if (is_match (id) )return pos; - pos +=PCI_CAP_LIST_NEXT ; + pos = (pos & ~3 ) +PCI_CAP_LIST_NEXT ; } +return 0 ; }
Quoted 5 lines
As said in context of v1, this function should remain usable for its original purpose. That, to me, includes the caller not needing to care about ttl. I could see you convert the original function the way you do, but under a new name, and then implement the original one simply in terms of this more general purpose function.
Will do.
Quoted 2 lines
Also, while I appreciate the sbdf conversion, that wants to be a separate patch, which would then want to take care of the sibling functions as well.
OK, will do. To be clear, this means converting the following 4 functions to use pci_sbdf_t, along with all the call sites:pci_find_cap_offsetpci_find_next_cappci_find_ext_capabilitypci_find_next_ext_capability
Quoted 25 lines
Quoted 25 lines
xen/drivers/vpci/header.c +12
-0@@ -513,6 +513,18 @@ static void cf_check rom_write( rom->addr = val &PCI_ROM_ADDRESS_MASK ; } +static bool cf_check vpci_cap_supported(uint8_t id) +{ +switch ( id ) + { +case PCI_CAP_ID_MSI : +case PCI_CAP_ID_MSIX : +return true; +default : +return false; + } +} + static int cf_check init_bars(struct pci_dev *pdev) {uint16_t cmd;@@ -544,6 +556,60 @@ static int cf_check init_bars(struct pci_dev *pdev)if ( rc )return rc;
Quoted 8 lines
xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 562
Quoted 4 lines
xen/drivers/vpci/header.c +4
-0+if ( !is_hardware_domain (pdev->domain) ) + { +if ( (pci_conf_read16 (pdev->sbdf,PCI_STATUS ) &PCI_STATUS_CAP_LIST ) + ==0 )This fits on a single line when written this more commonly used way:
if ( !(pci_conf_read16(pdev->sbdf, PCI_STATUS) & PCI_STATUS_CAP_LIST) )
I'll do this.
Quoted 11 lines
Otherwise it needs wrapping differently - binary operators at a wrapping point belong on the earlier line in our style.
xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 566
Quoted 4 lines
xen/drivers/vpci/header.c +4
-0+ { +/* RAZ/WI */ + rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL , +PCI_CAPABILITY_LIST ,1 ,NULL );This last NULL is likely misleading to readers: It does not obviously represent the value 0 cast to void *. (Same then for the extended cap handler at the end.)
I'll change to (void *)0
Quoted 13 lines
Quoted 13 lines
xen/drivers/vpci/header.c +13
-0+if ( rc ) +return rc; + } +else + { +/* Only expose capabilities to the guest that vPCI can handle. */ +uint8_t next; +int ttl =48 ; + + next =pci_find_next_cap (pdev->sbdf,PCI_CAPABILITY_LIST , + vpci_cap_supported, &ttl); + + rc = vpci_add_register(pdev->vpci, vpci_read_val, NULL,
Quoted 8 lines
xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 585
Quoted 6 lines
xen/drivers/vpci/header.c +6
-0+PCI_CAPABILITY_LIST ,1 , + (void *)(uintptr_t)next); +if ( rc ) +return rc; + +while ( next && (ttl >0 ) )Don't you need to mask off the low two bits first (rather than [only] ...
Yes, I'll fix this.
Quoted 23 lines
Quoted 9 lines
xen/drivers/vpci/header.c +8
-0+ { +uint8_t pos = next; + + next =pci_find_next_cap (pdev->sbdf, pos +PCI_CAP_LIST_NEXT , + vpci_cap_supported, &ttl); + + rc =vpci_add_register (pdev->vpci, vpci_hw_read8,NULL , + pos +PCI_CAP_LIST_ID ,1 ,NULL );xen/drivers/vpci/header.c static int cf_check init_bars(struct pci_dev *pdev) · line 603
Quoted 10 lines
xen/drivers/vpci/header.c +10
-0+if ( rc ) +return rc; + + rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL , + pos +PCI_CAP_LIST_NEXT ,1 , + (void *)(uintptr_t )next); +if ( rc ) +return rc; + + next &= ~3 ;... here)?
Jan
RFC PATCH v2 2/2 xen/vpci: header: avoid cast for value passed to vpci_read_val
In reply to Stewart HildebrandThe only purpose of this is to give an idea of what it might look like to introduce a new memory allocation in order to get rid of the casts for the value passed to vpci_read_val. If this is deemed preferable vs the casts, I will squash it for the next version of the series.
---v1->v2:
* new patch
--- xen/drivers/vpci/header.c | 13 ++++++++++--- xen/drivers/vpci/vpci.c | 8 +++++++- xen/include/xen/vpci.h | 3 +++ 3 files changed, 20 insertions(+), 4 deletions(-)
xen/drivers/vpci/header.c +10 -3
@@ -571,14 +571,19 @@ static int cf_check init_bars(struct pci_dev *pdev) {/* Only expose capabilities to the guest that vPCI can handle. */ uint8_t next; -int ttl =48 ; +int ttl =48 , i =0 ; + + header->caps_list =xzalloc_array (uint8_t, ttl); +if ( !header->caps_list ) +return -ENOMEM ; next =pci_find_next_cap (pdev->sbdf,PCI_CAPABILITY_LIST , vpci_cap_supported, &ttl); + header->caps_list[i] = next; rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL ,PCI_CAPABILITY_LIST ,1 , - (void *)(uintptr_t )next); + &header->caps_list[i]);if ( rc )return rc;
@@ -594,9 +599,11 @@ static int cf_check init_bars(struct pci_dev *pdev)if ( rc )return rc; + i++; + header->caps_list[i] = next; rc =vpci_add_register (pdev->vpci, vpci_read_val,NULL , pos +PCI_CAP_LIST_NEXT ,1 , - (void *)(uintptr_t )next); + &header->caps_list[i]);if ( rc )return rc;
xen/drivers/vpci/vpci.c +7 -1
@@ -61,6 +61,7 @@ void vpci_remove_device(struct pci_dev *pdev)if ( pdev->vpci->msix->table[i] )iounmap (pdev->vpci->msix->table[i]); } +xfree (pdev->vpci->header.caps_list);xfree (pdev->vpci->msix);xfree (pdev->vpci->msi);xfree (pdev->vpci);
@@ -136,7 +137,12 @@ static void cf_check vpci_ignored_write( uint32_t cf_check vpci_read_val(const struct pci_dev *pdev,unsigned int reg,void *data) { -return (uintptr_t )data; +uint8_t *val = data; + +if ( val ) +return *val; + +return 0 ; } uint32_t cf_check vpci_hw_read8(
xen/include/xen/vpci.h +3 -0
@@ -88,6 +88,9 @@ struct vpci { } bars[PCI_HEADER_NORMAL_NR_BARS +1 ];/* At most 6 BARS + 1 expansion ROM BAR. */ +/* Guest view of capabilities next pointers. */ +uint8_t *caps_list; +/* * Store whether the ROM enable bit is set (doesn't imply ROM BAR * is mapped into guest p2m) if there's a ROM BAR on the device.
Signature · 1 line
2.41.0
Re: [RFC PATCH v2 2/2] xen/vpci: header: avoid cast for value passed to vpci_read_val
Quoted 4 lines · Stewart Hildebrand
The only purpose of this is to give an idea of what it might look like to introduce a new memory allocation in order to get rid of the casts for the value passed to
vpci_read_val. If this is deemed preferable vs the casts, I will squash it for the next version of the series.
I would say this isn't worth it, but it'll be Roger to ultimately judge.
Jan