[PATCH v10 11/17] vpci/header: program p2m with guest BAR view

2023-10-12 Thread Volodymyr Babchuk
From: Oleksandr Andrushchenko 

Take into account guest's BAR view and program its p2m accordingly:
gfn is guest's view of the BAR and mfn is the physical BAR value.
This way hardware domain sees physical BAR values and guest sees
emulated ones.

Hardware domain continues getting the BARs identity mapped, while for
domUs the BARs are mapped at the requested guest address without
modifying the BAR address in the device PCI config space.

Signed-off-by: Oleksandr Andrushchenko 
Signed-off-by: Volodymyr Babchuk 
---
In v10:
- Moved GFN variable definition outside the loop in map_range()
- Updated printk error message in map_range()
- Now BAR address is always stored in bar->guest_addr, even for
  HW dom, this removes bunch of ugly is_hwdom() checks in modify_bars()
- vmsix_table_base() now uses .guest_addr instead of .addr
In v9:
- Extended the commit message
- Use bar->guest_addr in modify_bars
- Extended printk error message in map_range
- Moved map_data initialization so .bar can be initialized during declaration
Since v5:
- remove debug print in map_range callback
- remove "identity" from the debug print
Since v4:
- moved start_{gfn|mfn} calculation into map_range
- pass vpci_bar in the map_data instead of start_{gfn|mfn}
- s/guest_addr/guest_reg
Since v3:
- updated comment (Roger)
- removed gfn_add(map->start_gfn, rc); which is wrong
- use v->domain instead of v->vpci.pdev->domain
- removed odd e.g. in comment
- s/d%d/%pd in altered code
- use gdprintk for map/unmap logs
Since v2:
- improve readability for data.start_gfn and restructure ?: construct
Since v1:
 - s/MSI/MSI-X in comments
---
 xen/drivers/vpci/header.c | 53 ---
 xen/include/xen/vpci.h|  3 ++-
 2 files changed, 41 insertions(+), 15 deletions(-)

diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c
index 5c056923ad..efce0bc2ae 100644
--- a/xen/drivers/vpci/header.c
+++ b/xen/drivers/vpci/header.c
@@ -33,6 +33,7 @@
 
 struct map_data {
 struct domain *d;
+const struct vpci_bar *bar;
 bool map;
 };
 
@@ -40,11 +41,21 @@ static int cf_check map_range(
 unsigned long s, unsigned long e, void *data, unsigned long *c)
 {
 const struct map_data *map = data;
+/* Start address of the BAR as seen by the guest. */
+unsigned long start_gfn = PFN_DOWN(map->bar->guest_addr);
+/* Physical start address of the BAR. */
+mfn_t start_mfn = _mfn(PFN_DOWN(map->bar->addr));
 int rc;
 
 for ( ; ; )
 {
 unsigned long size = e - s + 1;
+/*
+ * Ranges to be mapped don't always start at the BAR start address, as
+ * there can be holes or partially consumed ranges. Account for the
+ * offset of the current address from the BAR start.
+ */
+mfn_t map_mfn = mfn_add(start_mfn, s - start_gfn);
 
 if ( !iomem_access_permitted(map->d, s, e) )
 {
@@ -72,8 +83,8 @@ static int cf_check map_range(
  * - {un}map_mmio_regions doesn't support preemption.
  */
 
-rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(s))
-  : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(s));
+rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, map_mfn)
+  : unmap_mmio_regions(map->d, _gfn(s), size, map_mfn);
 if ( rc == 0 )
 {
 *c += size;
@@ -82,8 +93,9 @@ static int cf_check map_range(
 if ( rc < 0 )
 {
 printk(XENLOG_G_WARNING
-   "Failed to identity %smap [%lx, %lx] for d%d: %d\n",
-   map->map ? "" : "un", s, e, map->d->domain_id, rc);
+   "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n",
+   map->map ? "" : "un", s, e, mfn_x(map_mfn),
+   mfn_x(map_mfn) + size, map->d, rc);
 break;
 }
 ASSERT(rc < size);
@@ -162,10 +174,6 @@ static void modify_decoding(const struct pci_dev *pdev, 
uint16_t cmd,
 bool vpci_process_pending(struct vcpu *v)
 {
 struct pci_dev *pdev = v->vpci.pdev;
-struct map_data data = {
-.d = v->domain,
-.map = v->vpci.cmd & PCI_COMMAND_MEMORY,
-};
 struct vpci_header *header = NULL;
 unsigned int i;
 
@@ -184,6 +192,11 @@ bool vpci_process_pending(struct vcpu *v)
 for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
 {
 struct vpci_bar *bar = &header->bars[i];
+struct map_data data = {
+.d = v->domain,
+.map = v->vpci.cmd & PCI_COMMAND_MEMORY,
+.bar = bar,
+};
 int rc;
 
 if ( rangeset_is_empty(bar->mem) )
@@ -234,7 +247,6 @@ bool vpci_process_pending(struct vcpu *v)
 static int __init apply_map(struct domain *d, const struct pci_dev *pdev,
 uint16_t cmd)
 {
-struct map_data data = { .d = d, .map = true };
 struct vpci_header *header = &pdev->vpci->header;
 int rc = 0;
 unsigned int i;
@@ -244,6 +256,

Re: [PATCH v10 11/17] vpci/header: program p2m with guest BAR view

2023-11-21 Thread Roger Pau Monné
On Thu, Oct 12, 2023 at 10:09:17PM +, Volodymyr Babchuk wrote:
> From: Oleksandr Andrushchenko 
> 
> Take into account guest's BAR view and program its p2m accordingly:
> gfn is guest's view of the BAR and mfn is the physical BAR value.
> This way hardware domain sees physical BAR values and guest sees
> emulated ones.
> 
> Hardware domain continues getting the BARs identity mapped, while for
> domUs the BARs are mapped at the requested guest address without
> modifying the BAR address in the device PCI config space.
> 
> Signed-off-by: Oleksandr Andrushchenko 
> Signed-off-by: Volodymyr Babchuk 
> ---
> In v10:
> - Moved GFN variable definition outside the loop in map_range()
> - Updated printk error message in map_range()
> - Now BAR address is always stored in bar->guest_addr, even for
>   HW dom, this removes bunch of ugly is_hwdom() checks in modify_bars()
> - vmsix_table_base() now uses .guest_addr instead of .addr
> In v9:
> - Extended the commit message
> - Use bar->guest_addr in modify_bars
> - Extended printk error message in map_range
> - Moved map_data initialization so .bar can be initialized during declaration
> Since v5:
> - remove debug print in map_range callback
> - remove "identity" from the debug print
> Since v4:
> - moved start_{gfn|mfn} calculation into map_range
> - pass vpci_bar in the map_data instead of start_{gfn|mfn}
> - s/guest_addr/guest_reg
> Since v3:
> - updated comment (Roger)
> - removed gfn_add(map->start_gfn, rc); which is wrong
> - use v->domain instead of v->vpci.pdev->domain
> - removed odd e.g. in comment
> - s/d%d/%pd in altered code
> - use gdprintk for map/unmap logs
> Since v2:
> - improve readability for data.start_gfn and restructure ?: construct
> Since v1:
>  - s/MSI/MSI-X in comments
> ---
>  xen/drivers/vpci/header.c | 53 ---
>  xen/include/xen/vpci.h|  3 ++-
>  2 files changed, 41 insertions(+), 15 deletions(-)
> 
> diff --git a/xen/drivers/vpci/header.c b/xen/drivers/vpci/header.c
> index 5c056923ad..efce0bc2ae 100644
> --- a/xen/drivers/vpci/header.c
> +++ b/xen/drivers/vpci/header.c
> @@ -33,6 +33,7 @@
>  
>  struct map_data {
>  struct domain *d;
> +const struct vpci_bar *bar;
>  bool map;
>  };
>  
> @@ -40,11 +41,21 @@ static int cf_check map_range(
>  unsigned long s, unsigned long e, void *data, unsigned long *c)
>  {
>  const struct map_data *map = data;
> +/* Start address of the BAR as seen by the guest. */
> +unsigned long start_gfn = PFN_DOWN(map->bar->guest_addr);
> +/* Physical start address of the BAR. */
> +mfn_t start_mfn = _mfn(PFN_DOWN(map->bar->addr));
>  int rc;
>  
>  for ( ; ; )
>  {
>  unsigned long size = e - s + 1;
> +/*
> + * Ranges to be mapped don't always start at the BAR start address, 
> as
> + * there can be holes or partially consumed ranges. Account for the
> + * offset of the current address from the BAR start.
> + */
> +mfn_t map_mfn = mfn_add(start_mfn, s - start_gfn);
>  
>  if ( !iomem_access_permitted(map->d, s, e) )

This check must be switched to use host physical addresses (mfns), not
the guest ones, same for the xsm_iomem_mapping() check just below.

>  {
> @@ -72,8 +83,8 @@ static int cf_check map_range(
>   * - {un}map_mmio_regions doesn't support preemption.
>   */
>  
> -rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, _mfn(s))
> -  : unmap_mmio_regions(map->d, _gfn(s), size, _mfn(s));
> +rc = map->map ? map_mmio_regions(map->d, _gfn(s), size, map_mfn)
> +  : unmap_mmio_regions(map->d, _gfn(s), size, map_mfn);
>  if ( rc == 0 )
>  {
>  *c += size;
> @@ -82,8 +93,9 @@ static int cf_check map_range(
>  if ( rc < 0 )
>  {
>  printk(XENLOG_G_WARNING
> -   "Failed to identity %smap [%lx, %lx] for d%d: %d\n",
> -   map->map ? "" : "un", s, e, map->d->domain_id, rc);
> +   "Failed to %smap [%lx %lx] -> [%lx %lx] for %pd: %d\n",
> +   map->map ? "" : "un", s, e, mfn_x(map_mfn),
> +   mfn_x(map_mfn) + size, map->d, rc);
>  break;
>  }
>  ASSERT(rc < size);
> @@ -162,10 +174,6 @@ static void modify_decoding(const struct pci_dev *pdev, 
> uint16_t cmd,
>  bool vpci_process_pending(struct vcpu *v)
>  {
>  struct pci_dev *pdev = v->vpci.pdev;
> -struct map_data data = {
> -.d = v->domain,
> -.map = v->vpci.cmd & PCI_COMMAND_MEMORY,
> -};
>  struct vpci_header *header = NULL;
>  unsigned int i;
>  
> @@ -184,6 +192,11 @@ bool vpci_process_pending(struct vcpu *v)
>  for ( i = 0; i < ARRAY_SIZE(header->bars); i++ )
>  {
>  struct vpci_bar *bar = &header->bars[i];
> +struct map_data data = {
> +.d = v->domain,
> +.map = v->vpci.cmd & PCI_CO