diff mbox series

[2/3] tools/libxl: Grant permission for mapping opregions to the target domain

Message ID 18bebc4af48b83d71b3247082434b958be84b841.1592171394.git.gorbak25@gmail.com (mailing list archive)
State New, archived
Headers show
Series IGD Passthrough fixes for linux based stubdomains | expand

Commit Message

Grzegorz Uriasz June 14, 2020, 10:12 p.m. UTC
IGD VGA devices require a special opregion MMIO region which functions as
an extra BAR in the PCI configuration space. Right now qemu is assigning those
permissions - this is failing inside a linux based stubdomain as the
stubdomain is not privileged. This patch grants the permissions for
opregions in libxl instead of qemu. Granting permissions in qemu may be removed
when this patch get's merged - for now linux based stubdomains which use IGD's
need to patch qemu and include this patch in xen for IGD passthrough to work.

Signed-off-by: Grzegorz Uriasz <gorbak25@gmail.com>
---
 tools/libxl/libxl_pci.c | 46 ++++++++++++++++++++++++++++++++++++++++-
 1 file changed, 45 insertions(+), 1 deletion(-)

Comments

Roger Pau Monné June 15, 2020, 12:44 p.m. UTC | #1
On Sun, Jun 14, 2020 at 10:12:02PM +0000, Grzegorz Uriasz wrote:
> IGD VGA devices require a special opregion MMIO region which functions as
> an extra BAR in the PCI configuration space. Right now qemu is assigning those
> permissions - this is failing inside a linux based stubdomain as the
> stubdomain is not privileged. This patch grants the permissions for
> opregions in libxl instead of qemu. Granting permissions in qemu may be removed
> when this patch get's merged - for now linux based stubdomains which use IGD's
> need to patch qemu and include this patch in xen for IGD passthrough to work.
> 
> Signed-off-by: Grzegorz Uriasz <gorbak25@gmail.com>

Thanks for the patch!

> ---
>  tools/libxl/libxl_pci.c | 46 ++++++++++++++++++++++++++++++++++++++++-
>  1 file changed, 45 insertions(+), 1 deletion(-)
> 
> diff --git a/tools/libxl/libxl_pci.c b/tools/libxl/libxl_pci.c
> index 436190f790..48b1d8073b 100644
> --- a/tools/libxl/libxl_pci.c
> +++ b/tools/libxl/libxl_pci.c
> @@ -2497,7 +2497,51 @@ static int libxl__grant_legacy_vga_permissions(libxl__gc *gc, const uint32_t dom
>  }
>  
>  static int libxl__grant_igd_opregion_permission(libxl__gc *gc, const uint32_t domid) {
> -    return 0;
> +    char* sysfs_path;
> +    FILE* f;
> +    uint32_t igd_host_opregion;
> +    int ret = 0;

No need to init ret.

> +    uint32_t stubdom_domid = libxl_get_stubdom_id(CTX, domid);
> +
> +    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/config", 0, 0, 2, 0);
> +    f = fopen(sysfs_path, "r");
> +    if (!f) {
> +        LOGED(ERROR, domid, "Unable to access IGD config space");
> +        return ERROR_FAIL;
> +    }
> +
> +    ret = fseek(f, 0xfc, SEEK_SET);
> +    if (ret < 0) {
> +        LOGED(ERROR, domid, "Unable to lseek in PCI config space");
> +        goto out;
> +    }
> +
> +    ret = fread((void*)&igd_host_opregion, 4, 1, f);
> +    if (ret < 0) {
> +        LOGED(ERROR, domid, "Unable to read opregion register");
> +        goto out;
> +    }
> +
> +    ret = xc_domain_iomem_permission(CTX->xch, stubdom_domid,
> +                                     (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT), 0x3, 1);
> +    if (ret < 0) {
> +        LOGED(ERROR, domid,
> +              "failed to give stubdom%d access to %"PRIx32" opregions for igd passthrough", stubdom_domid, igd_host_opregion);
> +        goto out;
> +    }

I think you only need to do this if there's a stubdomain? If
stubdom_domid is 0 then you don't need to do this.

Also, I'm not sure hardcoding the size is correct, AFAICT the size
should be fetched from the OpRegion Header size field?

The specification I'm reading for IGD OpRegion for Skylake processors
mentions the size of the OpRegion is 8KiB (so 2 pages).

> +
> +    ret = xc_domain_iomem_permission(CTX->xch, domid,
> +                                     (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT), 0x3, 1);
> +    if (ret < 0) {
> +        LOGED(ERROR, domid,
> +              "failed to give dom%d access to %"PRIx32" opregions for igd passthrough", domid, igd_host_opregion);
> +        goto out;
> +    }
> +
> +    out:

You should remove the leading spaces on the label.

> +    if(f)

No need for the 'if (f)', since all code paths leading here will have
f != NULL.

> +        fclose(f);
> +    return ret;

I think you want to return ERROR_FAIL if ret, ie: return ret ?
ERROR_FAIL : 0;

Thanks, Roger.
diff mbox series

Patch

diff --git a/tools/libxl/libxl_pci.c b/tools/libxl/libxl_pci.c
index 436190f790..48b1d8073b 100644
--- a/tools/libxl/libxl_pci.c
+++ b/tools/libxl/libxl_pci.c
@@ -2497,7 +2497,51 @@  static int libxl__grant_legacy_vga_permissions(libxl__gc *gc, const uint32_t dom
 }
 
 static int libxl__grant_igd_opregion_permission(libxl__gc *gc, const uint32_t domid) {
-    return 0;
+    char* sysfs_path;
+    FILE* f;
+    uint32_t igd_host_opregion;
+    int ret = 0;
+    uint32_t stubdom_domid = libxl_get_stubdom_id(CTX, domid);
+
+    sysfs_path = GCSPRINTF(SYSFS_PCI_DEV"/"PCI_BDF"/config", 0, 0, 2, 0);
+    f = fopen(sysfs_path, "r");
+    if (!f) {
+        LOGED(ERROR, domid, "Unable to access IGD config space");
+        return ERROR_FAIL;
+    }
+
+    ret = fseek(f, 0xfc, SEEK_SET);
+    if (ret < 0) {
+        LOGED(ERROR, domid, "Unable to lseek in PCI config space");
+        goto out;
+    }
+
+    ret = fread((void*)&igd_host_opregion, 4, 1, f);
+    if (ret < 0) {
+        LOGED(ERROR, domid, "Unable to read opregion register");
+        goto out;
+    }
+
+    ret = xc_domain_iomem_permission(CTX->xch, stubdom_domid,
+                                     (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT), 0x3, 1);
+    if (ret < 0) {
+        LOGED(ERROR, domid,
+              "failed to give stubdom%d access to %"PRIx32" opregions for igd passthrough", stubdom_domid, igd_host_opregion);
+        goto out;
+    }
+
+    ret = xc_domain_iomem_permission(CTX->xch, domid,
+                                     (unsigned long)(igd_host_opregion >> XC_PAGE_SHIFT), 0x3, 1);
+    if (ret < 0) {
+        LOGED(ERROR, domid,
+              "failed to give dom%d access to %"PRIx32" opregions for igd passthrough", domid, igd_host_opregion);
+        goto out;
+    }
+
+    out:
+    if(f)
+        fclose(f);
+    return ret;
 }
 
 int libxl__grant_vga_iomem_permission(libxl__gc *gc, const uint32_t domid,