Skip to contentSkip to conversation

IOMMU subsystem redesign and PV-IOMMU interface

RFC PATCH Superseded Teddy Astie 6 participants 20 messages Jan 21, 2025 – Feb 4, 2025

v5 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

v5 cover IOMMU subsystem redesign and PV-IOMMU interface

This work has been presented at Xen Summit 2024 during the
IOMMU paravirtualization and Xen IOMMU subsystem rework
design session.

Operating systems may want to have access to a IOMMU in order to do DMA protection or implement certain features (e.g VFIO on Linux).

VFIO support is mandatory for framework such as SPDK, which can be useful to implement an alternative storage backend for virtual machines [1].

In this patch series, we introduce in Xen the ability to manage several contexts per domain and provide a new hypercall interface to allow guests to manage IOMMU contexts.

The VT-d driver is updated to support these new features.

[1] Using SPDK with the Xen hypervisor - FOSDEM 2023
---
Cc: Marek Marczykowski-Górecki <[email protected]>

PCI Passthrough now work on my side, but things are still feels quite brittle.

Changed in v2 :
* fixed Xen crash when dumping IOMMU contexts (using X debug key) with DomUs without IOMMU
* s/dettach/detach/
* removed some unused includes
* fix dangling devices in contexts with detach

Changed in v3 :
* lock entirely map/unmap in hypercall
* prevent IOMMU operations on dying contexts (fix race condition)
* iommu_check_context+iommu_get_context -> iommu_get_context and check for NULL

Changed in v4 :
* Part of initialization logic is moved to domain or toolstack (IOMMU_init)
+ domain/toolstack now decides on "context count" and "pagetable pool size"
+ for now, all domains are able to initialize PV-IOMMU
* introduce "dom0-iommu=no-dma" to make default context block all DMA (disables HAP and sync-pt), enforcing usage of PV-IOMMU for DMA
Can be used to expose properly "Pre-boot DMA protection"
* redesigned locking logic for contexts
+ contexts are accessed using iommu_get_context and released with iommu_put_context

Changed in v5 :
* various PCI Passthrough related fixes
+ rewrote parts of PCI Passthrough logic
+ various other related bug fixes
* simplified VT-d DID (for hardware) management by only having one map instead of two (pseudo_domid map was previously used for old quarantine code then recycled for PV-IOMMU in addition to another map also tracing Domain<->VT-d DID, now there is only one map tracking both making things simpler)
* reworked parts of Xen quarantine logic (needed for PCI Passthrough)
* added cf_check annotations
* some changes to PV-IOMMU headers (Alejandro)

TODO:
* add stub implementations for bissecting needs and non-ported IOMMU implementations
* fix some issues with no-dma+PV and grants
* complete "no-dma" mode (expose to toolstack, add documentation, ...)
* properly define nested mode and PASID support

* make new quarantine code more unity region aware (isolate devices with different reserved regions regions using separate 'contexts')
* find a way to make PV-IOMMU work in DomUs (they don't see machine bdf)
* there are corner cases with PV-IOMMU and to-domain Xen PCI Passthrough (e.g pci-assignable-remove will reassign to context 0, while the driver expects the device to to be in context X)

Teddy Astie (5):
docs/designs: Add a design document for IOMMU subsystem redesign docs/designs: Add a design document for PV-IOMMU xen/public: Introduce PV-IOMMU hypercall interface
IOMMU: Introduce redesigned IOMMU subsystem
VT-d: Port IOMMU driver to new subsystem

 docs/designs/iommu-contexts.md       |  403 ++++++
 docs/designs/pv-iommu.md             |  116 ++
 xen/arch/x86/domain.c                |    2 +-
 xen/arch/x86/include/asm/arena.h     |   54 +
 xen/arch/x86/include/asm/iommu.h     |   58 +-
 xen/arch/x86/include/asm/pci.h       |   17 -
 xen/arch/x86/mm/p2m-ept.c            |    2 +-
 xen/arch/x86/pv/dom0_build.c         |    6 +-
 xen/arch/x86/tboot.c                 |    4 +-
 xen/common/Makefile                  |    1 +
 xen/common/memory.c                  |    4 +-
 xen/common/pv-iommu.c                |  539 ++++++++
 xen/drivers/passthrough/Makefile     |    3 +
 xen/drivers/passthrough/context.c    |  740 +++++++++++
 xen/drivers/passthrough/iommu.c      |  431 +++----
 xen/drivers/passthrough/pci.c        |  379 ++----
 xen/drivers/passthrough/quarantine.c |   49 +
 xen/drivers/passthrough/vtd/Makefile |    2 +-
 xen/drivers/passthrough/vtd/extern.h |   16 +-
 xen/drivers/passthrough/vtd/iommu.c  | 1692 ++++++++------------------
 xen/drivers/passthrough/vtd/iommu.h  |    4 +-
 xen/drivers/passthrough/vtd/qinval.c |    2 +-
 xen/drivers/passthrough/vtd/quirks.c |   20 +-
 xen/drivers/passthrough/x86/Makefile |    1 +
 xen/drivers/passthrough/x86/arena.c  |  157 +++
 xen/drivers/passthrough/x86/iommu.c  |  299 +++--
 xen/include/hypercall-defs.c         |    6 +
 xen/include/public/pv-iommu.h        |  343 ++++++
 xen/include/public/xen.h             |    1 +
 xen/include/xen/iommu.h              |  119 +-
 xen/include/xen/pci.h                |    3 +
 31 files changed, 3606 insertions(+), 1867 deletions(-)
 create mode 100644 docs/designs/iommu-contexts.md
 create mode 100644 docs/designs/pv-iommu.md
 create mode 100644 xen/arch/x86/include/asm/arena.h
 create mode 100644 xen/common/pv-iommu.c
 create mode 100644 xen/drivers/passthrough/context.c
 create mode 100644 xen/drivers/passthrough/quarantine.c
 create mode 100644 xen/drivers/passthrough/x86/arena.c
 create mode 100644 xen/include/public/pv-iommu.h

--
2.45.3

| Vates

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#2 lore raw

RFC PATCH v5 2/5 docs/designs: Add a design document for PV-IOMMU

Some operating systems want to use IOMMU to implement various features (e.g VFIO) or DMA protection.
This patch introduce a proposal for IOMMU paravirtualization for Dom0.

Signed-off-by Teddy Astie <[email protected]>
---
Changed in V2:
* nit s/dettach/detach/

Changed in v4:
* updated for iommu_context locking changes

---
 docs/designs/pv-iommu.md | 116 +++++++++++++++++++++++++++++++++++++++
 1 file changed, 116 insertions(+)
 create mode 100644 docs/designs/pv-iommu.md
docs/designs/pv-iommu.md new file +116 -0
new file mode 100644
index 0000000000..7df9fa0b94
@@ -0,0 +1,116 @@
+# IOMMU paravirtualization for Dom0
+
+Status: Experimental
+
+# Background
+
+By default, Xen only uses the IOMMU for itself, either to make device adress
+space coherent with guest adress space (x86 HVM/PVH) or to prevent devices
+from doing DMA outside it's expected memory regions including the hypervisor
+(x86 PV).
+
+A limitation is that guests (especially privildged ones) may want to use
+IOMMU hardware in order to implement features such as DMA protection and
+VFIO [1] as IOMMU functionality is not available outside of the hypervisor
+currently.
+
+[1] VFIO - "Virtual Function I/O" - https://www.kernel.org/doc/html/latest/driver-api/vfio.html
+
+# Design
+
+The operating system may want to have access to various IOMMU features such as
+context management and DMA remapping. We can create a new hypercall that allows
+the guest to have access to a new paravirtualized IOMMU interface.
+
+This feature is only meant to be available for the Dom0, as DomU have some
+emulated devices that can't be managed on Xen side and are not hardware, we
+can't rely on the hardware IOMMU to enforce DMA remapping.
+
+This interface is exposed under the `iommu_op` hypercall.
+
+In addition, Xen domains are modified in order to allow existence of several
+IOMMU context including a default one that implement default behavior (e.g
+hardware assisted paging) and can't be modified by guest. DomU cannot have
+contexts, and therefore act as if they only have the default domain.
+
+Each IOMMU context within a Xen domain is identified using a domain-specific
+context number that is used in the Xen IOMMU subsystem and the hypercall
+interface.
+
+The number of IOMMU context a domain is specified by either the toolstack or
+the domain itself.
+
+# IOMMU operations
+
+## Initialize PV-IOMMU
+
+Initialize PV-IOMMU for the domain.
+It can only be called once.
+
+## Alloc context
+
+Create a new IOMMU context for the guest and return the context number to the
+guest.
+Fail if the IOMMU context limit of the guest is reached.
+
+A flag can be specified to create a identity mapping.
+
+## Free context
+
+Destroy a IOMMU context created previously.
+It is not possible to free the default context.
+
+Reattach context devices to default context if specified by the guest.
+
+Fail if there is a device in the context and reattach-to-default flag is not
+specified.
+
+## Reattach device
+
+Reattach a device to another IOMMU context (including the default one).
+The target IOMMU context number must be valid and the context allocated.
+
+The guest needs to specify a PCI SBDF of a device he has access to.
+
+## Map/unmap page
+
+Map/unmap a page on a context.
+The guest needs to specify a gfn and target dfn to map.
+
+Refuse to create the mapping if one already exist for the same dfn.
+
+## Lookup page
+
+Get the gfn mapped by a specific dfn.
+
+## Remote command
+
+Make a PV-IOMMU operation on behalf of another domain.
+Especially useful for implementing IOMMU emulation (e.g using QEMU)
+or initializing PV-IOMMU with enforced limits.
+
+# Implementation considerations
+
+## Hypercall batching
+
+In order to prevent unneeded hypercalls and IOMMU flushing, it is advisable to
+be able to batch some critical IOMMU operations (e.g map/unmap multiple pages).
+
+## Hardware without IOMMU support
+
+Operating system needs to be aware on PV-IOMMU capability, and whether it is
+able to make contexts. However, some operating system may critically fail in
+case they are able to make a new IOMMU context. Which is supposed to happen
+if no IOMMU hardware is available.
+
+The hypercall interface needs a interface to advertise the ability to create
+and manage IOMMU contexts including the amount of context the guest is able
+to use. Using these informations, the Dom0 may decide whether to use or not
+the PV-IOMMU interface.
+
+## Page pool for contexts
+
+In order to prevent unexpected starving on the hypervisor memory with a
+buggy Dom0. We can preallocate the pages the contexts will use and make
+map/unmap use these pages instead of allocating them dynamically.
+
Signature · 9 lines
2.45.3



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#3 lore raw

RFC PATCH v5 3/5 xen/public: Introduce PV-IOMMU hypercall interface

In reply to Teddy Astie

Introduce a new pv interface to manage the underlying IOMMU and manage contexts and devices. This interface allows creation of new contexts from Dom0 and addition of IOMMU mappings using guest PoV.

This interface doesn't allow creation of mapping to other domains.

Signed-off-by Teddy Astie <[email protected]>
---
Changed in V2:
* cleanup some unneeded includes
* fix dangling devices in context on detach

Changed in V3:
* add unlocked _iommu_lookup_page
* iommu_check_context+iommu_get_context -> iommu_get_context and check for NULL
* prevent IOMMU operations on dying contexts

Changed in V4:
* changed context lock logic : iommu_get_context -> iommu_get_context+iommu_put_context
* added no-dma mode (see cover letter)
* use new initialization logic

---
 xen/common/Makefile           |   1 +
 xen/common/pv-iommu.c         | 539 ++++++++++++++++++++++++++++++++++
 xen/include/hypercall-defs.c  |   6 +
 xen/include/public/pv-iommu.h | 343 ++++++++++++++++++++++
 xen/include/public/xen.h      |   1 +
 5 files changed, 890 insertions(+)
 create mode 100644 xen/common/pv-iommu.c
 create mode 100644 xen/include/public/pv-iommu.h
xen/common/Makefile +1 -0
index f12a474d40..52ada89888 100644
@@ -58,6 +58,7 @@ obj-y += wait.o
 obj-bin-y += warning.init.o
 obj-$(CONFIG_XENOPROF) += xenoprof.o
 obj-y += xmalloc_tlsf.o
+obj-y += pv-iommu.o
 
 obj-bin-$(CONFIG_X86) += $(foreach n,decompress bunzip2 unxz unlzma lzo unlzo unlz4 unzstd earlycpio,$(n).init.o)
 
xen/common/pv-iommu.c new file +539 -0

539 lines not on this page · raw message

xen/include/hypercall-defs.c +6 -0
index 47c093acc8..59d7c02f55 100644
@@ -209,6 +209,9 @@ hypfs_op(unsigned int cmd, const char *arg1, unsigned long arg2, void *arg3, uns
 #ifdef CONFIG_X86
 xenpmu_op(unsigned int op, xen_pmu_params_t *arg)
 #endif
+#ifdef CONFIG_HAS_PASSTHROUGH
+iommu_op(unsigned int subop, void *arg)
+#endif
 
 #ifdef CONFIG_PV
 caller: pv64
@@ -295,5 +298,8 @@ mca                                do       do       -        -        -
 #ifndef CONFIG_PV_SHIM_EXCLUSIVE
 paging_domctl_cont                 do       do       do       do       -
 #endif
+#ifdef CONFIG_HAS_PASSTHROUGH
+iommu_op                           do       do       do       do       -
+#endif
 
 #endif /* !CPPCHECK */
xen/include/public/pv-iommu.h new file +343 -0

344 lines not on this page · raw message

xen/include/public/xen.h +1 -0
index b47d48d0e2..28ab815ebc 100644
@@ -118,6 +118,7 @@ DEFINE_XEN_GUEST_HANDLE(xen_ulong_t);
 #define __HYPERVISOR_xenpmu_op            40
 #define __HYPERVISOR_dm_op                41
 #define __HYPERVISOR_hypfs_op             42
+#define __HYPERVISOR_iommu_op             43
 
 /* Architecture-specific hypercall definitions. */
 #define __HYPERVISOR_arch_0               48
Signature · 9 lines
2.45.3



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#1 lore raw

RFC PATCH v5 1/5 docs/designs: Add a design document for IOMMU subsystem redesign

In reply to Teddy Astie

Current IOMMU subsystem has some limitations that make PV-IOMMU practically impossible. One of them is the assumtion that each domain is bound to a single "IOMMU domain", which also causes complications with quarantine implementation.

Moreover, current IOMMU subsystem is not entirely well-defined, for instance, the behavior of map_page between ARM SMMUv3 and x86 VT-d/AMD-Vi greatly differs. On ARM, it can modifies the domain page table while on x86, it may be forbidden (e.g using HAP with PVH), or only modifying the devices PoV (e.g using PV).

The goal of this redesign is to define more explicitely the behavior and interface of the IOMMU subsystem while allowing PV-IOMMU to be effectively implemented.

Signed-off-by Teddy Astie <[email protected]>
---
Changes in v4:
* added init and remote_op commands

---
 docs/designs/iommu-contexts.md | 403 +++++++++++++++++++++++++++++++++
 1 file changed, 403 insertions(+)
 create mode 100644 docs/designs/iommu-contexts.md
docs/designs/iommu-contexts.md new file +403 -0
new file mode 100644
index 0000000000..9d6fb95549
@@ -0,0 +1,403 @@
+# IOMMU context management in Xen
+
+Status: Experimental
+Revision: 0
+
+# Background
+
+The design for *IOMMU paravirtualization for Dom0* [1] explains that some guests may
+want to access to IOMMU features. In order to implement this in Xen, several adjustments
+needs to be made to the IOMMU subsystem.
+
+This "hardware IOMMU domain" is currently implemented on a per-domain basis such as each
+domain actually has a specific *hardware IOMMU domain*, this design aims to allow a
+single Xen domain to manage several "IOMMU context", and allow some domains (e.g Dom0
+[1]) to modify their IOMMU contexts.
+
+In addition to this, quarantine feature can be refactored into using IOMMU contexts
+to reduce the complexity of platform-specific implementations and ensuring more
+consistency across platforms.
+
+# IOMMU context
+
+We define a "IOMMU context" as being a *hardware IOMMU domain*, but named as a context
+to avoid confusion with Xen domains.
+It represents some hardware-specific data structure that contains mappings from a device
+frame-number to a machine frame-number (e.g using a pagetable) that can be applied to
+a device using IOMMU hardware.
+
+This structure is bound to a Xen domain, but a Xen domain may have several IOMMU context.
+These contexts may be modifiable using the interface as defined in [1] aside some
+specific cases (e.g modifying default context).
+
+This is implemented in Xen as a new structure that will hold context-specific
+data.
+
+```c
+struct iommu_context {
+    u16 id; /* Context id (0 means default context) */
+    struct list_head devices;
+
+    struct arch_iommu_context arch;
+
+    bool opaque; /* context can't be modified nor accessed (e.g HAP) */
+};
+```
+
+A context is identified by a number that is domain-specific and may be used by IOMMU
+users such as PV-IOMMU by the guest.
+
+struct arch_iommu_context is splited from struct arch_iommu
+
+```c
+struct arch_iommu_context
+{
+    spinlock_t pgtables_lock;
+    struct page_list_head pgtables;
+
+    union {
+        /* Intel VT-d */
+        struct {
+            uint64_t pgd_maddr; /* io page directory machine address */
+            domid_t *didmap; /* per-iommu DID */
+            unsigned long *iommu_bitmap; /* bitmap of iommu(s) that the context uses */
+        } vtd;
+        /* AMD IOMMU */
+        struct {
+            struct page_info *root_table;
+        } amd;
+    };
+};
+
+struct arch_iommu
+{
+    spinlock_t mapping_lock; /* io page table lock */
+    struct {
+        struct page_list_head list;
+        spinlock_t lock;
+    } pgtables;
+
+    struct list_head identity_maps;
+
+    union {
+        /* Intel VT-d */
+        struct {
+            /* no more context-specific values */
+            unsigned int agaw; /* adjusted guest address width, 0 is level 2 30-bit */
+        } vtd;
+        /* AMD IOMMU */
+        struct {
+            unsigned int paging_mode;
+            struct guest_iommu *g_iommu;
+        } amd;
+    };
+};
+```
+
+IOMMU context information is now carried by iommu_context rather than being integrated to
+struct arch_iommu.
+
+# Xen domain IOMMU structure
+
+`struct domain_iommu` is modified to allow multiples context within a single Xen domain
+to exist :
+
+```c
+struct iommu_context_list {
+    uint16_t count; /* Context count excluding default context */
+
+    /* if count > 0 */
+
+    uint64_t *bitmap; /* bitmap of context allocation */
+    struct iommu_context *map; /* Map of contexts */
+};
+
+struct domain_iommu {
+    /* ... */
+
+    struct iommu_context default_ctx;
+    struct iommu_context_list other_contexts;
+
+    /* ... */
+}
+```
+
+default_ctx is a special context with id=0 that holds the page table mapping the entire
+domain, which basically preserve the previous behavior. All devices are expected to be
+bound to this context during initialization.
+
+Along with this default context that always exist, we use a pool of contexts that has a
+fixed size at domain initialization, where contexts can be allocated (if possible), and
+have a id matching their position in the map (considering that id != 0).
+These contexts may be used by IOMMU contexts users such as PV-IOMMU or quarantine domain
+(DomIO).
+
+# Platform independent context management interface
+
+A new platform independant interface is introduced in Xen hypervisor to allow
+IOMMU contexts users to create and manage contexts within domains.
+
+```c
+/* Direct context access functions (not supposed to be used directly) */
+struct iommu_context *iommu_get_context(struct domain *d, u16 ctx_no);
+void iommu_put_context(struct iommu_context *ctx);
+
+/* Flag for default context initialization */
+#define IOMMU_CONTEXT_INIT_default (1 << 0)
+
+/* Flag for quarantine contexts (scratch page, DMA Abort mode, ...) */
+#define IOMMU_CONTEXT_INIT_quarantine (1 << 1)
+
+int iommu_context_init(struct domain *d, struct iommu_context *ctx, u16 ctx_no, u32 flags);
+
+/* Flag to specify that devices will need to be reattached to default domain */
+#define IOMMU_TEARDOWN_REATTACH_DEFAULT (1 << 0)
+
+/*
+ * Flag to specify that the context needs to be destroyed preemptively
+ * (multiple calls to iommu_context_teardown will be required)
+ */
+#define IOMMU_TEARDOWN_PREEMPT (1 << 1)
+
+int iommu_context_teardown(struct domain *d, struct iommu_context *ctx, u32 flags);
+
+/* Allocate a new context, uses CONTEXT_INIT flags */
+int iommu_context_alloc(struct domain *d, u16 *ctx_no, u32 flags);
+
+/* Free a context, uses CONTEXT_TEARDOWN flags */
+int iommu_context_free(struct domain *d, u16 ctx_no, u32 flags);
+
+/* Move a device from one context to another, including between different domains. */
+int iommu_reattach_context(struct domain *prev_dom, struct domain *next_dom,
+                           device_t *dev, u16 ctx_no);
+
+/* Add a device to a context for first initialization */
+int iommu_attach_context(struct domain *d, device_t *dev, u16 ctx_no);
+
+/* Remove a device from a context, effectively removing it from the IOMMU. */
+int iommu_detach_context(struct domain *d, device_t *dev);
+```
+
+This interface will use a new interface with drivers to implement these features.
+
+Some existing functions will have a new parameter to specify on what context to do the operation.
+- iommu_map (iommu_legacy_map untouched)
+- iommu_unmap (iommu_legacy_unmap untouched)
+- iommu_lookup_page
+- iommu_iotlb_flush
+
+These functions will modify the iommu_context structure to accomodate with the
+operations applied, these functions will be used to replace some operations previously
+made in the IOMMU driver.
+
+# IOMMU platform_ops interface changes
+
+The IOMMU driver needs to expose a way to create and manage IOMMU contexts, the approach
+taken here is to modify the interface to allow specifying a IOMMU context on operations,
+and at the same time, simplifying the interface by relying more on iommu
+platform-independent code.
+
+Added functions in iommu_ops
+
+```c
+/* Initialize a context (creating page tables, allocating hardware, structures, ...) */
+int (*context_init)(struct domain *d, struct iommu_context *ctx,
+                    u32 flags);
+/* Destroy a context, assumes no device is bound to the context. */
+int (*context_teardown)(struct domain *d, struct iommu_context *ctx,
+                        u32 flags);
+/* Put a device in a context (assumes the device is not attached to another context) */
+int (*attach)(struct domain *d, device_t *dev,
+              struct iommu_context *ctx);
+/* Remove a device from a context, and from the IOMMU. */
+int (*detach)(struct domain *d, device_t *dev,
+              struct iommu_context *prev_ctx);
+/* Move the device from a context to another, including if the new context is in
+   another domain. d corresponds to the target domain. */
+int (*reattach)(struct domain *d, device_t *dev,
+                struct iommu_context *prev_ctx,
+                struct iommu_context *ctx);
+
+#ifdef CONFIG_HAS_PCI
+/* Specific interface for phantom function devices. */
+int (*add_devfn)(struct domain *d, struct pci_dev *pdev, u16 devfn,
+                 struct iommu_context *ctx);
+int (*remove_devfn)(struct domain *d, struct pci_dev *pdev, u16 devfn,
+                    struct iommu_context *ctx);
+#endif
+
+/* Changes in existing to use a specified iommu_context. */
+int __must_check (*map_page)(struct domain *d, dfn_t dfn, mfn_t mfn,
+                             unsigned int flags,
+                             unsigned int *flush_flags,
+                             struct iommu_context *ctx);
+int __must_check (*unmap_page)(struct domain *d, dfn_t dfn,
+                               unsigned int order,
+                               unsigned int *flush_flags,
+                               struct iommu_context *ctx);
+int __must_check (*lookup_page)(struct domain *d, dfn_t dfn, mfn_t *mfn,
+                                unsigned int *flags,
+                                struct iommu_context *ctx);
+
+int __must_check (*iotlb_flush)(struct domain *d,
+                                struct iommu_context *ctx, dfn_t dfn,
+                                unsigned long page_count,
+                                unsigned int flush_flags);
+
+void (*clear_root_pgtable)(struct domain *d, struct iommu_context *ctx);
+```
+
+These functions are redundant with existing functions, therefore, the following functions
+are replaced with new equivalents :
+- quarantine_init : platform-independent code and IOMMU_CONTEXT_INIT_quarantine flag
+- add_device : attach and add_devfn (phantom)
+- assign_device : attach and add_devfn (phantom)
+- remove_device : detach and remove_devfn (phantom)
+- reassign_device : reattach
+
+Some functionnal differences with previous functions, the following should be handled
+by platform-independent/arch-specific code instead of IOMMU driver :
+- identity mappings (unity mappings and rmrr)
+- device list in context and domain
+- domain of a device
+- quarantine
+
+The idea behind this is to implement IOMMU context features while simplifying IOMMU
+drivers implementations and ensuring more consistency between IOMMU drivers.
+
+## Phantom function handling
+
+PCI devices may use additionnal devfn to do DMA operations, in order to support such
+devices, an interface is added to map specific device functions without implying that
+the device is mapped to a new context (that may cause duplicates in Xen data structures).
+
+Functions add_devfn and remove_devfn allows to map a iommu context on specific devfn
+for a pci device, without altering platform-independent data structures.
+
+It is important for the reattach operation to care about these devices, in order
+to prevent devices from being partially reattached to the new context (see XSA-449 [2])
+by using a all-or-nothing approach for reattaching such devices.
+
+# Quarantine refactoring using IOMMU contexts
+
+The quarantine mecanism can be entirely reimplemented using IOMMU context, making
+it simpler, more consistent between platforms,
+
+Quarantine is currently only supported with x86 platforms and works by creating a
+single *hardware IOMMU domain* per quarantined device. All the quarantine logic is
+the implemented in a platform-specific fashion while actually implementing the same
+concepts :
+
+The *hardware IOMMU context* data structures for quarantine are currently stored in
+the device structure itself (using arch_pci_dev) and IOMMU driver needs to care about
+whether we are dealing with quarantine operations or regular operations (often dealt
+using macros such as QUARANTINE_SKIP or DEVICE_PGTABLE).
+
+The page table that will apply on the quarantined device is created reserved device
+regions, and adding mappings to a scratch page if enabled (quarantine=scratch-page).
+
+A new approach we can use is allowing the quarantine domain (DomIO) to manage IOMMU
+contexts, and implement all the quarantine logic using IOMMU contexts.
+
+That way, the quarantine implementation can be platform-independent, thus have a more
+consistent implementation between platforms. It will also allows quarantine to work
+with other IOMMU implementations without having to implement platform-specific behavior.
+Moreover, quarantine operations can be implemented using regular context operations
+instead of relying on driver-specific code.
+
+Quarantine implementation can be summarised as
+
+```c
+int iommu_quarantine_dev_init(device_t *dev)
+{
+    int ret;
+    u16 ctx_no;
+
+    if ( !iommu_quarantine )
+        return -EINVAL;
+
+    ret = iommu_context_alloc(dom_io, &ctx_no, IOMMU_CONTEXT_INIT_quarantine);
+
+    if ( ret )
+        return ret;
+
+    /** TODO: Setup scratch page, mappings... */
+
+    ret = iommu_reattach_context(dev->domain, dom_io, dev, ctx_no);
+
+    if ( ret )
+    {
+        ASSERT(!iommu_context_free(dom_io, ctx_no, 0));
+        return ret;
+    }
+
+    return ret;
+}
+```
+
+# Platform-specific considerations
+
+## Reference counters on target pages
+
+When mapping a guest page onto a IOMMU context, we need to make sure that
+this page is not reused for something else while being actually referenced
+by a IOMMU context. One way of doing it is incrementing the reference counter
+of each target page we map (excluding reserved regions), and decrementing it
+when the mapping isn't used anymore.
+
+One consideration to have is when destroying the context while having existing
+mappings in it. We can walk through the entire page table and decrement the
+reference counter of all mappings. All of that assumes that there is no reserved
+region mapped (which should be the case as a requirement of teardown, or as a
+consequence of REATTACH_DEFAULT flag).
+
+Another consideration is that the "cleanup mappings" operation may take a lot
+of time depending on the complexity of the page table. Making the teardown operation preemptable can allow the hypercall to be preempted if needed also preventing a malicious
+guest from stalling a CPU in a teardown operation with a specially crafted IOMMU
+context (e.g with several 1G superpages).
+
+## Limit the amount of pages IOMMU contexts can use
+
+In order to prevent a (eventually malicious) guest from causing too much allocations
+in Xen, we can enforce limits on the memory the IOMMU subsystem can use for IOMMU context.
+A possible implementation can be to preallocate a reasonably large chunk of memory
+and split it into pages for use by the IOMMU subsystem only for non-default IOMMU
+contexts (e.g PV-IOMMU interface), if this limitation is overcome, some operations
+may fail from the guest side. These limitations shouldn't impact "usual" operations
+of the IOMMU subsystem (e.g default context initialization).
+
+## x86 Architecture
+
+TODO
+
+### Intel VT-d
+
+VT-d uses DID to tag the *IOMMU domain* applied to a device and assumes that all entries
+with the same DID uses the same page table (i.e same IOMMU context).
+Under certain circonstances (e.g DRHD with DID limit below 16-bits), the *DID* is
+transparently converted into a DRHD-specific DID using a map managed internally.
+
+The current implementation of the code reuses the Xen domain_id as DID.
+However, by using multiples IOMMU contexts per domain, we can't use the domain_id for
+contexts (otherwise, different page tables will be mapped with the same DID).
+The following strategy is used :
+- on the default context, reuse the domain_id (the default context is unique per domain)
+- on non-default context, use a id allocated in the pseudo_domid map, (actually used by
+quarantine) which is a DID outside of Xen domain_id range
+
+### AMD-Vi
+
+TODO
+
+## Device-tree platforms
+
+### SMMU and SMMUv3
+
+TODO
+
+* * *
+
+[1] See pv-iommu.md
+
+[2] pci: phantom functions assigned to incorrect contexts
+https://xenbits.xen.org/xsa/advisory-449.html
\ No newline at end of file
Signature · 9 lines
2.45.3



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#13 lore raw

RFC PATCH v5 4/5 IOMMU: Introduce redesigned IOMMU subsystem

In reply to Teddy Astie

Based on docs/designs/iommu-contexts.md, implement the redesigned IOMMU subsystem.

Signed-off-by Teddy Astie <[email protected]>

---
Changed in V2:
* cleanup some unneeded includes
* s/dettach/detach/
* don't dump IOMMU context of non-iommu domains (fix crash with DomUs)

Changed in v4:
* add "no-dma" support
* use new locking logic

Changed in v5:
* rewrote parts of PCI passthrough logic (pci.c)
* reworked quarantine logic (mostly fixes PCI Passthrough)
* make iotlb_flush_all context-specific
* various bug fixes related to iommu initialization with DomUs (e.g flags being wrongly defined)

---
 xen/arch/x86/domain.c                |   2 +-
 xen/arch/x86/mm/p2m-ept.c            |   2 +-
 xen/arch/x86/pv/dom0_build.c         |   6 +-
 xen/arch/x86/tboot.c                 |   4 +-
 xen/common/memory.c                  |   4 +-
 xen/drivers/passthrough/Makefile     |   3 +
 xen/drivers/passthrough/context.c    | 740 +++++++++++++++++++++++++++
 xen/drivers/passthrough/iommu.c      | 431 ++++++----------
 xen/drivers/passthrough/pci.c        | 379 +++++---------
 xen/drivers/passthrough/quarantine.c |  49 ++
 xen/include/xen/iommu.h              | 119 ++++-
 xen/include/xen/pci.h                |   3 +
 12 files changed, 1189 insertions(+), 553 deletions(-)
 create mode 100644 xen/drivers/passthrough/context.c
 create mode 100644 xen/drivers/passthrough/quarantine.c
xen/arch/x86/domain.c +1 -1
index ccadfe0c9e..1dd2453d71 100644
@@ -2405,7 +2405,7 @@ int domain_relinquish_resources(struct domain *d)
 
     PROGRESS(iommu_pagetables):
 
-        ret = iommu_free_pgtables(d);
+        ret = iommu_free_pgtables(d, iommu_default_context(d));
         if ( ret )
             return ret;
 
xen/arch/x86/mm/p2m-ept.c +1 -1
index 469e27ee93..80026a9cb9 100644
@@ -975,7 +975,7 @@ out:
             rc = iommu_iotlb_flush(d, _dfn(gfn), 1ul << order,
                                    (iommu_flags ? IOMMU_FLUSHF_added : 0) |
                                    (vtd_pte_present ? IOMMU_FLUSHF_modified
-                                                    : 0));
+                                                    : 0), 0);
         else if ( need_iommu_pt_sync(d) )
             rc = iommu_flags ?
                 iommu_legacy_map(d, _dfn(gfn), mfn, 1ul << order, iommu_flags) :
xen/arch/x86/pv/dom0_build.c +3 -3
index 07e9594493..b77f1c5ca3 100644
@@ -76,7 +76,7 @@ static __init void mark_pv_pt_pages_rdonly(struct domain *d,
          * iommu_memory_setup() ended up mapping them.
          */
         if ( need_iommu_pt_sync(d) &&
-             iommu_unmap(d, _dfn(mfn_x(page_to_mfn(page))), 1, 0, flush_flags) )
+             iommu_unmap(d, _dfn(mfn_x(page_to_mfn(page))), 1, 0, flush_flags, 0) )
             BUG();
 
         /* Read-only mapping + PGC_allocated + page-table page. */
@@ -127,7 +127,7 @@ static void __init iommu_memory_setup(struct domain *d, const char *what,
 
     while ( (rc = iommu_map(d, _dfn(mfn_x(mfn)), mfn, nr,
                             IOMMUF_readable | IOMMUF_writable | IOMMUF_preempt,
-                            flush_flags)) > 0 )
+                            flush_flags, 0)) > 0 )
     {
         mfn = mfn_add(mfn, rc);
         nr -= rc;
@@ -943,7 +943,7 @@ static int __init dom0_construct(struct domain *d,
     }
 
     /* Use while() to avoid compiler warning. */
-    while ( iommu_iotlb_flush_all(d, flush_flags) )
+    while ( iommu_iotlb_flush_all(d, 0, flush_flags) )
         break;
 
     if ( initrd_len != 0 )
xen/arch/x86/tboot.c +2 -2
index ba0700d2d5..ca55306830 100644
@@ -216,9 +216,9 @@ static void tboot_gen_domain_integrity(const uint8_t key[TB_KEY_SIZE],
 
         if ( is_iommu_enabled(d) && is_vtd )
         {
-            const struct domain_iommu *dio = dom_iommu(d);
+            struct domain_iommu *dio = dom_iommu(d);
 
-            update_iommu_mac(&ctx, dio->arch.vtd.pgd_maddr,
+            update_iommu_mac(&ctx, iommu_default_context(d)->arch.vtd.pgd_maddr,
                              agaw_to_level(dio->arch.vtd.agaw));
         }
     }
xen/common/memory.c +2 -2
index de2cc7ad92..0eb0f9da7b 100644
@@ -925,7 +925,7 @@ int xenmem_add_to_physmap(struct domain *d, struct xen_add_to_physmap *xatp,
         this_cpu(iommu_dont_flush_iotlb) = 0;
 
         ret = iommu_iotlb_flush(d, _dfn(xatp->idx - done), done,
-                                IOMMU_FLUSHF_modified);
+                                IOMMU_FLUSHF_modified, 0);
         if ( unlikely(ret) && rc >= 0 )
             rc = ret;
 
@@ -939,7 +939,7 @@ int xenmem_add_to_physmap(struct domain *d, struct xen_add_to_physmap *xatp,
             put_page(pages[i]);
 
         ret = iommu_iotlb_flush(d, _dfn(xatp->gpfn - done), done,
-                                IOMMU_FLUSHF_added | IOMMU_FLUSHF_modified);
+                                IOMMU_FLUSHF_added | IOMMU_FLUSHF_modified, 0);
         if ( unlikely(ret) && rc >= 0 )
             rc = ret;
     }
xen/drivers/passthrough/Makefile +3 -0
index a1621540b7..69327080ab 100644
@@ -4,6 +4,9 @@ obj-$(CONFIG_X86) += x86/
 obj-$(CONFIG_ARM) += arm/
 
 obj-y += iommu.o
+obj-y += context.o
+obj-y += quarantine.o
+
 obj-$(CONFIG_HAS_PCI) += pci.o
 obj-$(CONFIG_HAS_DEVICE_TREE) += device_tree.o
 obj-$(CONFIG_HAS_PCI) += ats.o
xen/drivers/passthrough/context.c new file +740 -0

740 lines not on this page · raw message

xen/drivers/passthrough/iommu.c +151 -280

518 lines not on this page · raw message

xen/drivers/passthrough/pci.c +133 -246

483 lines not on this page · raw message

xen/drivers/passthrough/quarantine.c new file +49 -0

49 lines not on this page · raw message

xen/include/xen/iommu.h +101 -18

234 lines not on this page · raw message

xen/include/xen/pci.h +3 -0
index 82e1221c9c..bb3931b01d 100644
@@ -97,6 +97,7 @@ struct pci_dev_info {
 struct pci_dev {
     struct list_head alldevs_list;
     struct list_head domain_list;
+    struct list_head context_list;
 
     struct list_head msi_list;
 
@@ -104,6 +105,8 @@ struct pci_dev {
 
     struct domain *domain;
 
+    uint16_t context; /* IOMMU context number of domain */
+
     const union {
         struct {
             uint8_t devfn;
Signature · 9 lines
2.45.3



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech
#14 lore raw

RFC PATCH v5 5/5 VT-d: Port IOMMU driver to new subsystem

In reply to Teddy Astie

Port the driver with guidances specified in iommu-contexts.md.

Add a arena-based allocator for allocating a fixed chunk of memory and split it into 4k pages for use by the IOMMU contexts. This chunk size is configurable with X86_ARENA_ORDER and dom0-iommu=arena-order=N.

Signed-off-by Teddy Astie <[email protected]>
---
Changed in V2:
* formatting

Changed in V3:
* prevent IOMMU operations on dying contexts

Changed in V4:
* redesigned hypercall interface [1]
* added remote_cmd and init logic

[1] https://lore.kernel.org/all/[email protected]/

Changed in V5:
* simplified domid management
* also dump dom_io iommu contexts (with 'X' key handler)

---
 xen/arch/x86/include/asm/arena.h     |   54 +
 xen/arch/x86/include/asm/iommu.h     |   58 +-
 xen/arch/x86/include/asm/pci.h       |   17 -
 xen/drivers/passthrough/vtd/Makefile |    2 +-
 xen/drivers/passthrough/vtd/extern.h |   16 +-
 xen/drivers/passthrough/vtd/iommu.c  | 1692 ++++++++------------------
 xen/drivers/passthrough/vtd/iommu.h  |    4 +-
 xen/drivers/passthrough/vtd/qinval.c |    2 +-
 xen/drivers/passthrough/vtd/quirks.c |   20 +-
 xen/drivers/passthrough/x86/Makefile |    1 +
 xen/drivers/passthrough/x86/arena.c  |  157 +++
 xen/drivers/passthrough/x86/iommu.c  |  299 +++--
 12 files changed, 1008 insertions(+), 1314 deletions(-)
 create mode 100644 xen/arch/x86/include/asm/arena.h
 create mode 100644 xen/drivers/passthrough/x86/arena.c
xen/arch/x86/include/asm/arena.h new file +54 -0
new file mode 100644
index 0000000000..7555b100e0
@@ -0,0 +1,54 @@
+/* SPDX-License-Identifier: GPL-2.0 */
+/**
+ * Simple arena-based page allocator.
+ */
+
+#ifndef __XEN_IOMMU_ARENA_H__
+#define __XEN_IOMMU_ARENA_H__
+
+#include "xen/domain.h"
+#include "xen/atomic.h"
+#include "xen/mm-frame.h"
+#include "xen/types.h"
+
+/**
+ * struct page_arena: Page arena structure
+ */
+struct iommu_arena {
+    /* mfn of the first page of the memory region */
+    mfn_t region_start;
+    /* bitmap of allocations */
+    unsigned long *map;
+
+    /* Order of the arena */
+    unsigned int order;
+
+    /* Used page count */
+    atomic_t used_pages;
+};
+
+/**
+ * Initialize a arena using domheap allocator.
+ * @param [out] arena Arena to allocate
+ * @param [in] domain domain that has ownership of arena pages
+ * @param [in] order order of the arena (power of two of the size)
+ * @param [in] memflags Flags for domheap_alloc_pages()
+ * @return -ENOMEM on arena allocation error, 0 otherwise
+ */
+int iommu_arena_initialize(struct iommu_arena *arena, struct domain *domain,
+                           unsigned int order, unsigned int memflags);
+
+/**
+ * Teardown a arena.
+ * @param [out] arena arena to allocate
+ * @param [in] check check for existing allocations
+ * @return -EBUSY if check is specified
+ */
+int iommu_arena_teardown(struct iommu_arena *arena, bool check);
+
+struct page_info *iommu_arena_allocate_page(struct iommu_arena *arena);
+bool iommu_arena_free_page(struct iommu_arena *arena, struct page_info *page);
+
+#define iommu_arena_size(arena) (1LLU << (arena)->order)
+
+#endif
xen/arch/x86/include/asm/iommu.h +44 -14

109 lines not on this page · raw message

xen/arch/x86/include/asm/pci.h +0 -17
index fd5480d67d..214c1a0948 100644
@@ -15,23 +15,6 @@
 
 struct arch_pci_dev {
     vmask_t used_vectors;
-    /*
-     * These fields are (de)initialized under pcidevs-lock. Other uses of
-     * them don't race (de)initialization and hence don't strictly need any
-     * locking.
-     */
-    union {
-        /* Subset of struct arch_iommu's fields, to be used in dom_io. */
-        struct {
-            uint64_t pgd_maddr;
-        } vtd;
-        struct {
-            struct page_info *root_table;
-        } amd;
-    };
-    domid_t pseudo_domid;
-    mfn_t leaf_mfn;
-    struct page_list_head pgtables_list;
 };
 
 int pci_conf_write_intercept(unsigned int seg, unsigned int bdf,
xen/drivers/passthrough/vtd/Makefile +1 -1
index fde7555fac..81e1f46179 100644
@@ -5,4 +5,4 @@ obj-y += dmar.o
 obj-y += utils.o
 obj-y += qinval.o
 obj-y += intremap.o
-obj-y += quirks.o
+obj-y += quirks.o
\ No newline at end of file
xen/drivers/passthrough/vtd/extern.h +6 -10
index 667590ee52..c564982489 100644
@@ -45,8 +45,6 @@ void disable_intremap(struct vtd_iommu *iommu);
 int iommu_alloc(struct acpi_drhd_unit *drhd);
 void iommu_free(struct acpi_drhd_unit *drhd);
 
-domid_t did_to_domain_id(const struct vtd_iommu *iommu, unsigned int did);
-
 int iommu_flush_iec_global(struct vtd_iommu *iommu);
 int iommu_flush_iec_index(struct vtd_iommu *iommu, u8 im, u16 iidx);
 void clear_fault_bits(struct vtd_iommu *iommu);
@@ -80,12 +78,10 @@ uint64_t alloc_pgtable_maddr(unsigned long npages, nodeid_t node);
 void free_pgtable_maddr(u64 maddr);
 void *map_vtd_domain_page(u64 maddr);
 void unmap_vtd_domain_page(const void *va);
-int domain_context_mapping_one(struct domain *domain, struct vtd_iommu *iommu,
-                               uint8_t bus, uint8_t devfn,
-                               const struct pci_dev *pdev, domid_t domid,
-                               paddr_t pgd_maddr, unsigned int mode);
-int domain_context_unmap_one(struct domain *domain, struct vtd_iommu *iommu,
-                             uint8_t bus, uint8_t devfn);
+int apply_context_single(struct domain *domain, struct iommu_context *ctx,
+                         struct vtd_iommu *iommu, uint8_t bus, uint8_t devfn);
+int unapply_context_single(struct domain *domain, struct vtd_iommu *iommu,
+                           uint8_t bus, uint8_t devfn);
 int cf_check intel_iommu_get_reserved_device_memory(
     iommu_grdm_t *func, void *ctxt);
 
@@ -106,8 +102,8 @@ void platform_quirks_init(void);
 void vtd_ops_preamble_quirk(struct vtd_iommu *iommu);
 void vtd_ops_postamble_quirk(struct vtd_iommu *iommu);
 int __must_check me_wifi_quirk(struct domain *domain, uint8_t bus,
-                               uint8_t devfn, domid_t domid, paddr_t pgd_maddr,
-                               unsigned int mode);
+                               uint8_t devfn, domid_t domid,
+                               unsigned int mode, struct iommu_context *ctx);
 void pci_vtd_quirk(const struct pci_dev *);
 void quirk_iommu_caps(struct vtd_iommu *iommu);
 
xen/drivers/passthrough/vtd/iommu.c +530 -1162

2,179 lines not on this page · raw message

xen/drivers/passthrough/vtd/iommu.h +1 -3
index 78aa8a96f5..48b5900d49 100644
@@ -104,7 +104,7 @@
 #define DMA_TLB_GLOBAL_FLUSH (((u64)1) << 60)
 #define DMA_TLB_DSI_FLUSH (((u64)2) << 60)
 #define DMA_TLB_PSI_FLUSH (((u64)3) << 60)
-#define DMA_TLB_IIRG(x) (((x) >> 60) & 7) 
+#define DMA_TLB_IIRG(x) (((x) >> 60) & 7)
 #define DMA_TLB_IAIG(val) (((val) >> 57) & 7)
 #define DMA_TLB_DID(x) (((uint64_t)((x) & 0xffff)) << 32)
 
@@ -506,9 +506,7 @@ struct vtd_iommu {
     } flush;
 
     struct list_head ats_devices;
-    unsigned long *pseudo_domid_map; /* "pseudo" domain id bitmap */
     unsigned long *domid_bitmap;  /* domain id bitmap */
-    domid_t *domid_map;           /* domain id mapping array */
     uint32_t version;
 };
 
xen/drivers/passthrough/vtd/qinval.c +1 -1
index 036f3e8505..3f25b6a2e0 100644
@@ -229,7 +229,7 @@ static int __must_check dev_invalidate_sync(struct vtd_iommu *iommu,
     rc = queue_invalidate_wait(iommu, 0, 1, 1, 1);
     if ( rc == -ETIMEDOUT && !pdev->broken )
     {
-        struct domain *d = rcu_lock_domain_by_id(did_to_domain_id(iommu, did));
+        struct domain *d = rcu_lock_domain(pdev->domain);
 
         /*
          * In case the domain has been freed or the IOMMU domid bitmap is
xen/drivers/passthrough/vtd/quirks.c +9 -11
index 950dcd56ef..568a1a06d5 100644
@@ -408,9 +408,8 @@ void __init platform_quirks_init(void)
 
 static int __must_check map_me_phantom_function(struct domain *domain,
                                                 unsigned int dev,
-                                                domid_t domid,
-                                                paddr_t pgd_maddr,
-                                                unsigned int mode)
+                                                unsigned int mode,
+                                                struct iommu_context *ctx)
 {
     struct acpi_drhd_unit *drhd;
     struct pci_dev *pdev;
@@ -422,18 +421,17 @@ static int __must_check map_me_phantom_function(struct domain *domain,
 
     /* map or unmap ME phantom function */
     if ( !(mode & UNMAP_ME_PHANTOM_FUNC) )
-        rc = domain_context_mapping_one(domain, drhd->iommu, 0,
-                                        PCI_DEVFN(dev, 7), NULL,
-                                        domid, pgd_maddr, mode);
+        rc = apply_context_single(domain, ctx, drhd->iommu, 0,
+                                  PCI_DEVFN(dev, 7));
     else
-        rc = domain_context_unmap_one(domain, drhd->iommu, 0,
-                                      PCI_DEVFN(dev, 7));
+        rc = unapply_context_single(domain, drhd->iommu, 0, PCI_DEVFN(dev, 7));
 
     return rc;
 }
 
 int me_wifi_quirk(struct domain *domain, uint8_t bus, uint8_t devfn,
-                  domid_t domid, paddr_t pgd_maddr, unsigned int mode)
+                  domid_t domid, unsigned int mode,
+                  struct iommu_context *ctx)
 {
     u32 id;
     int rc = 0;
@@ -457,7 +455,7 @@ int me_wifi_quirk(struct domain *domain, uint8_t bus, uint8_t devfn,
             case 0x423b8086:
             case 0x423c8086:
             case 0x423d8086:
-                rc = map_me_phantom_function(domain, 3, domid, pgd_maddr, mode);
+                rc = map_me_phantom_function(domain, 3, mode, ctx);
                 break;
             default:
                 break;
@@ -483,7 +481,7 @@ int me_wifi_quirk(struct domain *domain, uint8_t bus, uint8_t devfn,
             case 0x42388086:        /* Puma Peak */
             case 0x422b8086:
             case 0x422c8086:
-                rc = map_me_phantom_function(domain, 22, domid, pgd_maddr, mode);
+                rc = map_me_phantom_function(domain, 22, mode, ctx);
                 break;
             default:
                 break;
xen/drivers/passthrough/x86/Makefile +1 -0
index 75b2885336..1614f3d284 100644
@@ -1,2 +1,3 @@
 obj-y += iommu.o
+obj-y += arena.o
 obj-$(CONFIG_HVM) += hvm.o
xen/drivers/passthrough/x86/arena.c new file +157 -0

158 lines not on this page · raw message

xen/drivers/passthrough/x86/iommu.c +204 -95

359 lines not on this page · raw message

The rest of this diff is not shown. View the raw message.

Signature · 9 lines
2.45.3



Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

Part of the diff is not shown here. View the raw message.

#15 lore raw
In reply to Teddy Astie
Quoted 62 lines · Teddy Astie

This work has been presented at Xen Summit 2024 during the
IOMMU paravirtualization and Xen IOMMU subsystem rework
design session.

Operating systems may want to have access to a IOMMU in order to do DMA protection or implement certain features (e.g VFIO on Linux).

VFIO support is mandatory for framework such as SPDK, which can be useful to implement an alternative storage backend for virtual machines [1].

In this patch series, we introduce in Xen the ability to manage several contexts per domain and provide a new hypercall interface to allow guests to manage IOMMU contexts.

The VT-d driver is updated to support these new features.

[1] Using SPDK with the Xen hypervisor - FOSDEM 2023
---
Cc: Marek Marczykowski-Górecki <[email protected]>

PCI Passthrough now work on my side, but things are still feels quite brittle.

Changed in v2 :
* fixed Xen crash when dumping IOMMU contexts (using X debug key) with DomUs without IOMMU
* s/dettach/detach/
* removed some unused includes
* fix dangling devices in contexts with detach

Changed in v3 :
* lock entirely map/unmap in hypercall
* prevent IOMMU operations on dying contexts (fix race condition)
* iommu_check_context+iommu_get_context -> iommu_get_context and check for NULL

Changed in v4 :
* Part of initialization logic is moved to domain or toolstack (IOMMU_init)
+ domain/toolstack now decides on "context count" and "pagetable pool size"
+ for now, all domains are able to initialize PV-IOMMU
* introduce "dom0-iommu=no-dma" to make default context block all DMA (disables HAP and sync-pt), enforcing usage of PV-IOMMU for DMA
Can be used to expose properly "Pre-boot DMA protection"
* redesigned locking logic for contexts
+ contexts are accessed using iommu_get_context and released with iommu_put_context

Changed in v5 :
* various PCI Passthrough related fixes
+ rewrote parts of PCI Passthrough logic
+ various other related bug fixes
* simplified VT-d DID (for hardware) management by only having one map instead of two (pseudo_domid map was previously used for old quarantine code then recycled for PV-IOMMU in addition to another map also tracing Domain<->VT-d DID, now there is only one map tracking both making things simpler)
* reworked parts of Xen quarantine logic (needed for PCI Passthrough)
* added cf_check annotations
* some changes to PV-IOMMU headers (Alejandro)

TODO:
* add stub implementations for bissecting needs and non-ported IOMMU implementations
* fix some issues with no-dma+PV and grants
* complete "no-dma" mode (expose to toolstack, add documentation, ...)
* properly define nested mode and PASID support

Quoted 6 lines

* make new quarantine code more unity region aware (isolate devices with different reserved regions regions using separate 'contexts')
* find a way to make PV-IOMMU work in DomUs (they don't see machine bdf)
* there are corner cases with PV-IOMMU and to-domain Xen PCI Passthrough (e.g pci-assignable-remove will reassign to context 0, while the driver expects the device to to be in context X)

Thanks for the updated patches. I have run them through gitlab-ci, and
here are some observations:
- I needed to disable CONFIG_AMD_IOMMU (it fails to build, as expected at this point)
- I needed to disable pvshim (it fails to build)
- fails to build with clang: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373789/viewer#L3525
- gcc-ibt build fails: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373785#L1314
- fails to build for ARM (both 32 and 64) and PPC64
- QEMU smoke test panic with PV dom0, looks like it runs on AMD, so it
may be related to the disabled CONFIG_AMD_IOMMU, but I wouldn't expect
it to panic on _PV_ dom0 boot...
- PVH dom0 fails to boot (on real hw) with a lot of VT-d faults: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373875
- PCI passthrough (with PV dom0) results in a lot of VT-d faults: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373881

Note this uses only this series, but plain Linux (appears to be 6.1.19). IIUC if one doesn't try to configure PV-IOMMU specifically (non-default contexts) it should still work.

BTW Linux says it detected "Xen version 4.19." - shouldn't it report 4.20 already at this point in release cycle?

All results:
https://gitlab.com/xen-project/people/marmarek/xen/-/pipelines/1637849303

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#16 lore raw
Quoted 2 lines · Marek Marczykowski-Górecki

BTW Linux says it detected "Xen version 4.19." - shouldn't it report 4.20 already at this point in release cycle?

Not only at this point, but throughout the release cycle. Yet I fear I haven't seen such, so I wouldn't be able to look into it.

Jan

#17 lore raw
Quoted 6 lines · Jan Beulich
Quoted 2 lines · Marek Marczykowski-Górecki

BTW Linux says it detected "Xen version 4.19." - shouldn't it report 4.20 already at this point in release cycle?

Not only at this point, but throughout the release cycle. Yet I fear I haven't seen such, so I wouldn't be able to look into it.

Ah, my bad, it seems Teddy's branch is based on 4.19, not staging.

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#18 lore raw
In reply to Marek Marczykowski-Górecki
Quoted 87 lines · Marek Marczykowski-Górecki
Quoted 62 lines · Teddy Astie

This work has been presented at Xen Summit 2024 during the
IOMMU paravirtualization and Xen IOMMU subsystem rework
design session.

Operating systems may want to have access to a IOMMU in order to do DMA protection or implement certain features (e.g VFIO on Linux).

VFIO support is mandatory for framework such as SPDK, which can be useful to implement an alternative storage backend for virtual machines [1].

In this patch series, we introduce in Xen the ability to manage several contexts per domain and provide a new hypercall interface to allow guests to manage IOMMU contexts.

The VT-d driver is updated to support these new features.

[1] Using SPDK with the Xen hypervisor - FOSDEM 2023
---
Cc: Marek Marczykowski-Górecki <[email protected]>

PCI Passthrough now work on my side, but things are still feels quite brittle.

Changed in v2 :
* fixed Xen crash when dumping IOMMU contexts (using X debug key) with DomUs without IOMMU
* s/dettach/detach/
* removed some unused includes
* fix dangling devices in contexts with detach

Changed in v3 :
* lock entirely map/unmap in hypercall
* prevent IOMMU operations on dying contexts (fix race condition)
* iommu_check_context+iommu_get_context -> iommu_get_context and check for NULL

Changed in v4 :
* Part of initialization logic is moved to domain or toolstack (IOMMU_init)
+ domain/toolstack now decides on "context count" and "pagetable pool size"
+ for now, all domains are able to initialize PV-IOMMU
* introduce "dom0-iommu=no-dma" to make default context block all DMA (disables HAP and sync-pt), enforcing usage of PV-IOMMU for DMA
Can be used to expose properly "Pre-boot DMA protection"
* redesigned locking logic for contexts
+ contexts are accessed using iommu_get_context and released with iommu_put_context

Changed in v5 :
* various PCI Passthrough related fixes
+ rewrote parts of PCI Passthrough logic
+ various other related bug fixes
* simplified VT-d DID (for hardware) management by only having one map instead of two (pseudo_domid map was previously used for old quarantine code then recycled for PV-IOMMU in addition to another map also tracing Domain<->VT-d DID, now there is only one map tracking both making things simpler)
* reworked parts of Xen quarantine logic (needed for PCI Passthrough)
* added cf_check annotations
* some changes to PV-IOMMU headers (Alejandro)

TODO:
* add stub implementations for bissecting needs and non-ported IOMMU implementations
* fix some issues with no-dma+PV and grants
* complete "no-dma" mode (expose to toolstack, add documentation, ...)
* properly define nested mode and PASID support

Quoted 6 lines

* make new quarantine code more unity region aware (isolate devices with different reserved regions regions using separate 'contexts')
* find a way to make PV-IOMMU work in DomUs (they don't see machine bdf)
* there are corner cases with PV-IOMMU and to-domain Xen PCI Passthrough (e.g pci-assignable-remove will reassign to context 0, while the driver expects the device to to be in context X)

Thanks for the updated patches. I have run them through gitlab-ci, and
here are some observations:
- I needed to disable CONFIG_AMD_IOMMU (it fails to build, as expected at this point)
- I needed to disable pvshim (it fails to build)
- fails to build with clang: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373789/viewer#L3525
- gcc-ibt build fails: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373785#L1314
- fails to build for ARM (both 32 and 64) and PPC64
- QEMU smoke test panic with PV dom0, looks like it runs on AMD, so it
may be related to the disabled CONFIG_AMD_IOMMU, but I wouldn't expect
it to panic on _PV_ dom0 boot...
- PVH dom0 fails to boot (on real hw) with a lot of VT-d faults: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373875
- PCI passthrough (with PV dom0) results in a lot of VT-d faults: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373881

Note this uses only this series, but plain Linux (appears to be 6.1.19). IIUC if one doesn't try to configure PV-IOMMU specifically (non-default contexts) it should still work.

Quoted 5 lines

BTW Linux says it detected "Xen version 4.19." - shouldn't it report 4.20 already at this point in release cycle?

All results:
https://gitlab.com/xen-project/people/marmarek/xen/-/pipelines/1637849303

FWIW the test run rebased on staging looks similar:
https://gitlab.com/xen-project/people/marmarek/xen/-/pipelines/1638019332

Signature · 3 lines
Best Regards,
Marek Marczykowski-Górecki
Invisible Things Lab
#19 lore raw
In reply to Marek Marczykowski-Górecki

Hello Marek,
Thanks for your testing.

Le 23/01/2025 à 13:45, Marek Marczykowski-Górecki a écrit :

Quoted 1 line

Thanks for the updated patches. I have run them through gitlab-

ci, and

Quoted 5 lines

here are some observations:
- I needed to disable CONFIG_AMD_IOMMU (it fails to build, as expected at this point)
- I needed to disable pvshim (it fails to build)

- fails to build with clang: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373789/viewer#L3525> - gcc-ibt build fails:

https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373785#L1314

Looks like another cf_check related issue that I missed.

Quoted 1 line

- fails to build for ARM (both 32 and 64) and PPC64

This is expected like for the AMD_IOMMU part.

Quoted 3 lines

- QEMU smoke test panic with PV dom0, looks like it runs on AMD, so it may be related to the disabled CONFIG_AMD_IOMMU, but I wouldn't expect it to panic on _PV_ dom0 boot...

Looks like I broke something when there is no IOMMU detected (removed some check that should be there).

This patch should fix it (tested with QEMU without IOMMU).

---

(quoted diff) +0 -0

b/xen/drivers/passthrough/context.c
index 6e68f840f3..98c84b439b 100644

xen/drivers/passthrough/context.c +0 -0
@@ -347,6 +347,10 @@ int iommu_iotlb_flush_all(struct domain *d, u16

ctx_no, unsigned int flush_flags

      struct iommu_context *ctx;
      int rc;

+ if ( !is_iommu_enabled(d) || !hd->platform_ops->iotlb_flush ||
+ !flush_flags )
+ return 0;
+

      if ( !(ctx = iommu_get_context(d, ctx_no)) )
          return -ENOENT;

---

Quoted 1 line

- PVH dom0 fails to boot (on real hw) with a lot of VT-d faults: https://gitlab.com/xen-project/people/marmarek/xen/-/jobs/8931373875

I guess 00:02.0 is the iGPU. The addresses point to reserved memory in E820 (is it the framebuffer ?) which should be reconfigured by the guest.

Is the guest dying (maybe due to the PVH Dom0 issue) before being able to setup anything, causing the GPU to not be properly set ? I tested a plain Alpine 3.18 PVH Dom0 and the kernel crashes very early (6.1.123-0-lts though).

Or there is something else going wrong like with PCI Passthrough.

Quoted 4 lines

- PCI passthrough (with PV dom0) results in a lot of VT-d faults:
Note this uses only this series, but plain Linux (appears to be 6.1.19). IIUC if one doesn't try to configure PV-IOMMU specifically (non-default contexts) it should still work.

Yes, and PV-IOMMU drivers will likely not fix the issues you are facing.

Quoted 4 lines

BTW Linux says it detected "Xen version 4.19." - shouldn't it report 4.20 already at this point in release cycle?

It's probably because I mostly tested on Xen 4.19 (for practical reasons to make toolstack happy), but I will update it to staging.

Quoted 3 lines

All results:
https://gitlab.com/xen-project/people/marmarek/xen/-/pipelines/1637849303

Thanks

Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#4 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

In reply to Teddy Astie

Hi Teddy,

Thanks for working on this. I'm curious about your plans for this:

Quoted 16 lines · Teddy Astie
xen/include/public/pv-iommu.h new file +16 -0
+/**
+ * IOMMU_alloc_nested
+ * Create a nested IOMMU context (needs IOMMUCAP_nested).
+ *
+ * This context uses a platform-specific page table from domain address space
+ * specified in pgtable_gfn and use it for nested translations.
+ *
+ * Explicit flushes needs to be submited with IOMMU_flush_nested on
+ * modification of the nested pagetable to ensure coherency between IOTLB and
+ * nested page table.
+ *
+ * This context can be destroyed using IOMMU_free_context.
+ * This context cannot be modified using map_pages, unmap_pages.
+ */
+struct pv_iommu_alloc_nested {
+    /* OUT: allocated IOMMU context number */

xen/include/public/pv-iommu.h line 291

Quoted 10 lines
xen/include/public/pv-iommu.h new file +10 -0
+    uint16_t ctx_no;
+
+    /* IN: guest frame number of the nested page table */
+    uint64_aligned_t pgtable_gfn;
+
+    /* IN: nested mode flags */
+    uint64_aligned_t nested_flags;
+};
+typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
+DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

Is this command intended to be used for GVA -> GPA translation? Would you need some way to associate with another iommu context for GPA -> HPA translation?

Maybe more broadly, what are your goals for enabling PV-IOMMU? The examples on your blog post cover a domain restrict device access to specific portions of the the GPA space. Are you also interested in GVA -> GPA? Does VFIO required GVA -> GPA?

And, sorry to bike shed, but ctx_no reads like "Context No" to me. I think ctxid/ctx_id might be clearer. Others probably have their own opinions :)

Thanks,
Jason

#5 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

Quoted 30 lines · Jason Andryuk
Quoted 16 lines · Teddy Astie
xen/include/public/pv-iommu.h new file +16 -0
+/**
+ * IOMMU_alloc_nested
+ * Create a nested IOMMU context (needs IOMMUCAP_nested).
+ *
+ * This context uses a platform-specific page table from domain address space
+ * specified in pgtable_gfn and use it for nested translations.
+ *
+ * Explicit flushes needs to be submited with IOMMU_flush_nested on
+ * modification of the nested pagetable to ensure coherency between IOTLB and
+ * nested page table.
+ *
+ * This context can be destroyed using IOMMU_free_context.
+ * This context cannot be modified using map_pages, unmap_pages.
+ */
+struct pv_iommu_alloc_nested {
+    /* OUT: allocated IOMMU context number */

xen/include/public/pv-iommu.h line 291

Quoted 10 lines
xen/include/public/pv-iommu.h new file +10 -0
+    uint16_t ctx_no;
+
+    /* IN: guest frame number of the nested page table */
+    uint64_aligned_t pgtable_gfn;
+
+    /* IN: nested mode flags */
+    uint64_aligned_t nested_flags;
+};
+typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
+DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

Is this command intended to be used for GVA -> GPA translation?  Would you need some way to associate with another iommu context for GPA -> HPA translation?

Quoted 3 lines

Maybe more broadly, what are your goals for enabling PV-IOMMU?  The examples on your blog post cover a domain restrict device access to specific portions of the the GPA space.  Are you also interested in GVA -> GPA?  Does VFIO required GVA -> GPA?

And, sorry to bike shed, but ctx_no reads like "Context No" to me.  I think ctxid/ctx_id might be clearer.  Others probably have their own opinions :)

Or, if it absolutely is to derive from "number", ctx_nr. That said, I agree "id" is a better term to use here.

Jan

#6 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

In reply to Jason Andryuk

Hello Jason,

Le 30/01/2025 à 21:17, Jason Andryuk a écrit :

Quoted 29 lines

Hi Teddy,

Thanks for working on this.  I'm curious about your plans for this:

Quoted 24 lines · Teddy Astie

+/**
+ * IOMMU_alloc_nested
+ * Create a nested IOMMU context (needs IOMMUCAP_nested).
+ *
+ * This context uses a platform-specific page table from domain address space
+ * specified in pgtable_gfn and use it for nested translations.
+ *
+ * Explicit flushes needs to be submited with IOMMU_flush_nested on
+ * modification of the nested pagetable to ensure coherency between IOTLB and
+ * nested page table.
+ *
+ * This context can be destroyed using IOMMU_free_context.
+ * This context cannot be modified using map_pages, unmap_pages.
+ */

xen/include/public/pv-iommu.h new file +8 -0
+struct pv_iommu_alloc_nested {
+    /* OUT: allocated IOMMU context number */
+    uint16_t ctx_no;
+
+    /* IN: guest frame number of the nested page table */
+    uint64_aligned_t pgtable_gfn;
+
+    /* IN: nested mode flags */
Quoted 9 lines

xen/include/public/pv-iommu.h line 291

Quoted 4 lines

+    uint64_aligned_t nested_flags;

xen/include/public/pv-iommu.h new file +3 -0
+};
+typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
+DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

Is this command intended to be used for GVA -> GPA translation?  Would you need some way to associate with another iommu context for GPA -> HPA translation?

It's intended to be used for accelerating IOMMU emulation for the guest. So in this case the other GPA->HPA translation is domain's p2m page-table (or something similar) such as the translations made from this nested context is meaningful from guest point of view.

The idea to use it is to use the "remote_op" sub-command to let the device model (e.g QEMU) alter the IOMMU behavior for the affected domain (e.g by reattaching devices, making new IOMMU contexts, ...).

I think it can also be used for virtio-iommu pagetable.

Quoted 5 lines

Maybe more broadly, what are your goals for enabling PV-IOMMU?  The examples on your blog post cover a domain restrict device access to specific portions of the the GPA space.  Are you also interested in GVA -> GPA?  Does VFIO required GVA -> GPA?

The current way of enabling and using PV-IOMMU is with the dedicated Linux IOMMU driver [1] that implements Linux's IOMMU subsystem with this proposed interface.
This in practice in the PV case replaces the xen-swiotlb with dma-iommu and do all DMA through the paravirtualized IOMMU (e.g creating DMA domains, moving devices to it).

Regarding GVA->GPA, this is what this interface provides, and restricting device access to memory is one way of using it. This is a requirement for VFIO (as it is also one for Linux IOMMU), and I managed to make SPDK and DPDK work in Dom0 using VFIO and these patches [2].

[1] Originally
https://lists.xen.org/archives/html/xen-devel/2024-06/msg01145.html but this patch got quite old and probably doesn't work anymore with this new Xen patch series.
I have a updated patch in my xen-pviommu branch
https://gitlab.com/xen-project/people/tsnake41/linux/-/commit/125d67a09fa9f66a32f9175641cfccca22dbbdb6

[2] You also need to set "vfio_iommu_type1.allow_unsafe_interrupts=1" to make VFIO work for now.

Quoted 4 lines

And, sorry to bike shed, but ctx_no reads like "Context No" to me.  I think ctxid/ctx_id might be clearer.  Others probably have their own opinions :)

ctxid/ctx_id would make more sense (we already have names like domid).

Quoted 2 lines

Thanks,
Jason

Thanks
Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#7 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

Quoted 66 lines · Teddy Astie

Hello Jason,

Le 30/01/2025 à 21:17, Jason Andryuk a écrit :

Quoted 29 lines

Hi Teddy,

Thanks for working on this.  I'm curious about your plans for this:

Quoted 24 lines · Teddy Astie

+/**
+ * IOMMU_alloc_nested
+ * Create a nested IOMMU context (needs IOMMUCAP_nested).
+ *
+ * This context uses a platform-specific page table from domain address space
+ * specified in pgtable_gfn and use it for nested translations.
+ *
+ * Explicit flushes needs to be submited with IOMMU_flush_nested on
+ * modification of the nested pagetable to ensure coherency between IOTLB and
+ * nested page table.
+ *
+ * This context can be destroyed using IOMMU_free_context.
+ * This context cannot be modified using map_pages, unmap_pages.
+ */

xen/include/public/pv-iommu.h new file +8 -0
+struct pv_iommu_alloc_nested {
+    /* OUT: allocated IOMMU context number */
+    uint16_t ctx_no;
+
+    /* IN: guest frame number of the nested page table */
+    uint64_aligned_t pgtable_gfn;
+
+    /* IN: nested mode flags */
Quoted 9 lines

xen/include/public/pv-iommu.h line 291

Quoted 4 lines

+    uint64_aligned_t nested_flags;

xen/include/public/pv-iommu.h new file +3 -0
+};
+typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
+DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

Is this command intended to be used for GVA -> GPA translation?  Would you need some way to associate with another iommu context for GPA -> HPA translation?

It's intended to be used for accelerating IOMMU emulation for the guest. So in this case the other GPA->HPA translation is domain's p2m page-table (or something similar) such as the translations made from this nested context is meaningful from guest point of view.

The idea to use it is to use the "remote_op" sub-command to let the device model (e.g QEMU) alter the IOMMU behavior for the affected domain (e.g by reattaching devices, making new IOMMU contexts, ...).

I think it can also be used for virtio-iommu pagetable.

Quoted 5 lines

Maybe more broadly, what are your goals for enabling PV-IOMMU?  The examples on your blog post cover a domain restrict device access to specific portions of the the GPA space.  Are you also interested in GVA -> GPA?  Does VFIO required GVA -> GPA?

The current way of enabling and using PV-IOMMU is with the dedicated Linux IOMMU driver [1] that implements Linux's IOMMU subsystem with this proposed interface.
This in practice in the PV case replaces the xen-swiotlb with dma-iommu and do all DMA through the paravirtualized IOMMU (e.g creating DMA domains, moving devices to it).

Quoted 4 lines

Regarding GVA->GPA, this is what this interface provides, and restricting device access to memory is one way of using it. This is a requirement for VFIO (as it is also one for Linux IOMMU), and I managed to make SPDK and DPDK work in Dom0 using VFIO and these patches [2].

Thanks for the explanation, Teddy. It certainly seems that this work is moving in the right direction. However, I have a couple of questions on this point, as I admit that I have not fully understood it.

Modern IOMMUs support two stages of translation. Using ARM terminology, these are referred to as stage2 and stage1. The stage2 translation is used by Xen for the domain's GPA->PA (p2m) mapping. The pagetable used for stage2 could potentially be shared with the pagetable used by Xen for the p2m. Stage1 is typically controlled by the guest for its own address translations, mapping guest virtual addresses (GVA) to guest physical addresses (GPA).

I can see that this patch series introduces an interface that allows Dom0 to modify its own stage2 mappings.

My question is: how do we allow the domain to also set up stage1 mappings? I would assume that the interface would take the form of a hypercall that allows a domain to pass a stage1 pagetable pointer, which Xen would then use to configure the IOMMU stage1. However, I do not see such a hypercall in your series. I was under the impression that GVA-to-GPA translation was left as future work, so I am confused by your statement that this patch series already provides it.

Quoted 9 lines

[1] Originally
https://lists.xen.org/archives/html/xen-devel/2024-06/msg01145.html but this patch got quite old and probably doesn't work anymore with this new Xen patch series.
I have a updated patch in my xen-pviommu branch
https://gitlab.com/xen-project/people/tsnake41/linux/-/commit/125d67a09fa9f66a32f9175641cfccca22dbbdb6

[2] You also need to set "vfio_iommu_type1.allow_unsafe_interrupts=1" to make VFIO work for now.

#8 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

Hello Stefano,

Le 03/02/2025 à 18:47, Stefano Stabellini a écrit :

Quoted 87 lines · Teddy Astie
Quoted 66 lines

Hello Jason,

Le 30/01/2025 à 21:17, Jason Andryuk a écrit :

Quoted 29 lines

Hi Teddy,

Thanks for working on this.  I'm curious about your plans for this:

Quoted 24 lines · Teddy Astie

+/**
+ * IOMMU_alloc_nested
+ * Create a nested IOMMU context (needs IOMMUCAP_nested).
+ *
+ * This context uses a platform-specific page table from domain address space
+ * specified in pgtable_gfn and use it for nested translations.
+ *
+ * Explicit flushes needs to be submited with IOMMU_flush_nested on
+ * modification of the nested pagetable to ensure coherency between IOTLB and
+ * nested page table.
+ *
+ * This context can be destroyed using IOMMU_free_context.
+ * This context cannot be modified using map_pages, unmap_pages.
+ */

xen/include/public/pv-iommu.h new file +8 -0
+struct pv_iommu_alloc_nested {
+    /* OUT: allocated IOMMU context number */
+    uint16_t ctx_no;
+
+    /* IN: guest frame number of the nested page table */
+    uint64_aligned_t pgtable_gfn;
+
+    /* IN: nested mode flags */
Quoted 9 lines

xen/include/public/pv-iommu.h line 291

Quoted 4 lines

+    uint64_aligned_t nested_flags;

xen/include/public/pv-iommu.h new file +3 -0
+};
+typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
+DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

Is this command intended to be used for GVA -> GPA translation?  Would you need some way to associate with another iommu context for GPA -> HPA translation?

It's intended to be used for accelerating IOMMU emulation for the guest. So in this case the other GPA->HPA translation is domain's p2m page-table (or something similar) such as the translations made from this nested context is meaningful from guest point of view.

The idea to use it is to use the "remote_op" sub-command to let the device model (e.g QEMU) alter the IOMMU behavior for the affected domain (e.g by reattaching devices, making new IOMMU contexts, ...).

I think it can also be used for virtio-iommu pagetable.

Quoted 5 lines

Maybe more broadly, what are your goals for enabling PV-IOMMU?  The examples on your blog post cover a domain restrict device access to specific portions of the the GPA space.  Are you also interested in GVA -> GPA?  Does VFIO required GVA -> GPA?

The current way of enabling and using PV-IOMMU is with the dedicated Linux IOMMU driver [1] that implements Linux's IOMMU subsystem with this proposed interface.
This in practice in the PV case replaces the xen-swiotlb with dma-iommu and do all DMA through the paravirtualized IOMMU (e.g creating DMA domains, moving devices to it).

Quoted 4 lines

Regarding GVA->GPA, this is what this interface provides, and restricting device access to memory is one way of using it. This is a requirement for VFIO (as it is also one for Linux IOMMU), and I managed to make SPDK and DPDK work in Dom0 using VFIO and these patches [2].

Thanks for the explanation, Teddy. It certainly seems that this work is moving in the right direction. However, I have a couple of questions on this point, as I admit that I have not fully understood it.

Modern IOMMUs support two stages of translation. Using ARM terminology, these are referred to as stage2 and stage1. The stage2 translation is used by Xen for the domain's GPA->PA (p2m) mapping. The pagetable used for stage2 could potentially be shared with the pagetable used by Xen for the p2m. Stage1 is typically controlled by the guest for its own address translations, mapping guest virtual addresses (GVA) to guest physical addresses (GPA).

I can see that this patch series introduces an interface that allows Dom0 to modify its own stage2 mappings.

Quoted 8 lines

My question is: how do we allow the domain to also set up stage1 mappings? I would assume that the interface would take the form of a hypercall that allows a domain to pass a stage1 pagetable pointer, which Xen would then use to configure the IOMMU stage1. However, I do not see such a hypercall in your series. I was under the impression that GVA-to-GPA translation was left as future work, so I am confused by your statement that this patch series already provides it.

There are 2 interfaces for the guest (and device model) to configure its GVA-to-GPA mappings. There are map/unmap subcommands and (depending on hardware support) nested iommu contexts where the guest/device model provides the GPA of the stage1 pagetable (this is meant to be a way of accelerating IOMMU emulation through QEMU).

Here is the hypercall subcommands for map/unmap where you can map and unmap pages to the "paravirtualized IOMMU context" (making the mapped region visible to devices of the context attached through reattach_device).

/**
* IOMMU_map_pages
* Map pages on a IOMMU context.
*
* pgsize must be supported by pgsize_mask.
* Fails with -EINVAL if mapping on top of another mapping.
* Report actually mapped page count in mapped field (regardless of
failure).
*/

struct pv_iommu_map_pages {
     /* IN: IOMMU context number */
     uint16_t ctx_no;

     /* IN: Guest frame number */
     uint64_aligned_t gfn;

     /* IN: Device frame number */
     uint64_aligned_t dfn;

     /* IN: Map flags */
     uint32_t map_flags;

     /* IN: Size of pages to map */
     uint32_t pgsize;

     /* IN: Number of pages to map */
     uint32_t nr_pages;

     /* OUT: Number of pages actually mapped */
     uint32_t mapped;
};

typedef struct pv_iommu_map_pages pv_iommu_map_pages_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_map_pages_t);

/**
* IOMMU_unmap_pages
* Unmap pages on a IOMMU context.
*
* pgsize must be supported by pgsize_mask.
* Report actually unmapped page count in mapped field (regardless of
failure).
* Fails with -ENOENT when attempting to unmap a page without any mapping */

struct pv_iommu_unmap_pages {
     /* IN: IOMMU context number */
     uint16_t ctx_no;

     /* IN: Device frame number */
     uint64_aligned_t dfn;

     /* IN: Size of pages to unmap */
     uint32_t pgsize;

     /* IN: Number of pages to unmap */
     uint32_t nr_pages;

     /* OUT: Number of pages actually unmapped */
     uint32_t unmapped;
};

typedef struct pv_iommu_unmap_pages pv_iommu_unmap_pages_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_unmap_pages_t);

If the hardware supports it, there is a alternative (still being drafted) interface to allow the guest to directly provide native pagetables.

This is exposed through the "_nested" subcommands, there is no implementation of this feature in this patch series yet.

/**
* IOMMU_alloc_nested
* Create a nested IOMMU context (needs IOMMUCAP_nested).
*
* This context uses a platform-specific page table from domain address
space
* specified in pgtable_gfn and use it for nested translations. *
* Explicit flushes needs to be submited with IOMMU_flush_nested on
* modification of the nested pagetable to ensure coherency between
IOTLB and
* nested page table.
*
* This context can be destroyed using IOMMU_free_context.
* This context cannot be modified using map_pages, unmap_pages. */

struct pv_iommu_alloc_nested {
     /* OUT: allocated IOMMU context number */
     uint16_t ctx_no;

     /* IN: guest frame number of the nested page table */
     uint64_aligned_t pgtable_gfn;

     /* IN: nested mode flags */
     uint64_aligned_t nested_flags;
};

typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

/**
* IOMMU_flush_nested (needs IOMMUCAP_nested)
* Flush the IOTLB for nested translation.
*/

struct pv_iommu_flush_nested {
     /* TODO */
};

typedef struct pv_iommu_flush_nested pv_iommu_flush_nested_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_flush_nested_t);

Quoted 10 lines
Quoted 10 lines

[1] Originally
https://lists.xen.org/archives/html/xen-devel/2024-06/msg01145.html but this patch got quite old and probably doesn't work anymore with this new Xen patch series.
I have a updated patch in my xen-pviommu branch
https://gitlab.com/xen-project/people/tsnake41/linux/-/commit/125d67a09fa9f66a32f9175641cfccca22dbbdb6

Quoted 2 lines
Quoted 2 lines

[2] You also need to set "vfio_iommu_type1.allow_unsafe_interrupts=1" to make VFIO work for now.

Thanks
Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#9 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

Quoted 6 lines · Teddy Astie

If the hardware supports it, there is a alternative (still being drafted) interface to allow the guest to directly provide native pagetables.

This is exposed through the "_nested" subcommands, there is no implementation of this feature in this patch series yet.

Out of interest, if the HW support two stage translations, then why do we need a PV interface? Wouldn’t it be better to exposed an emulated iommu? This would reduce the amount of enlightenment required in the guest OS. In the long run, this would provide some better performance because some IOMMU HW can now accelerate TLB flush commands (among other things). For instance, see the NVIDIA vIOMMU.

Cheers,

Quoted 66 lines

/**
* IOMMU_alloc_nested
* Create a nested IOMMU context (needs IOMMUCAP_nested).
*
* This context uses a platform-specific page table from domain address
space
* specified in pgtable_gfn and use it for nested translations. *
* Explicit flushes needs to be submited with IOMMU_flush_nested on
* modification of the nested pagetable to ensure coherency between
IOTLB and
* nested page table.
*
* This context can be destroyed using IOMMU_free_context.
* This context cannot be modified using map_pages, unmap_pages. */

struct pv_iommu_alloc_nested {
     /* OUT: allocated IOMMU context number */
     uint16_t ctx_no;

     /* IN: guest frame number of the nested page table */
     uint64_aligned_t pgtable_gfn;

     /* IN: nested mode flags */
     uint64_aligned_t nested_flags;
};

typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

/**
* IOMMU_flush_nested (needs IOMMUCAP_nested)
* Flush the IOTLB for nested translation.
*/

struct pv_iommu_flush_nested {
     /* TODO */
};

typedef struct pv_iommu_flush_nested pv_iommu_flush_nested_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_flush_nested_t);

Quoted 9 lines
Quoted 9 lines

[1] Originally
https://lists.xen.org/archives/html/xen-devel/2024-06/msg01145.html but this patch got quite old and probably doesn't work anymore with this new Xen patch series.
I have a updated patch in my xen-pviommu branch

https://gitlab.com/xen-project/people/tsnake41/linux/-/commit/125d67a09fa9f66a32f9175641cfccca22dbbdb6

Quoted 3 lines
Quoted 3 lines

[2] You also need to set "vfio_iommu_type1.allow_unsafe_interrupts=1" to make VFIO work for now.

Thanks
Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#10 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

Quoted 11 lines · Julien Grall

On Tue, 4 Feb 2025 at 11:46, Teddy Astie <[email protected]> wrote:
If the hardware supports it, there is a alternative (still being drafted) interface to allow the guest to directly provide native pagetables.

This is exposed through the "_nested" subcommands, there is no implementation of this feature in this patch series yet.

Out of interest, if the HW support two stage translations, then why do we need a PV interface? Wouldn’t it be better to exposed an emulated iommu? This would reduce the amount of enlightenment required in the guest OS. In the long run, this would provide some better performance because some IOMMU HW can now accelerate TLB flush commands (among other things). For instance, see the NVIDIA vIOMMU.

Hi Julien, I am not Teddy, but I have been considering the same question, and here are the conclusions I reached.

A virtual IOMMU of the same type as the underlying IOMMU provides better operating system compatibility, particularly for running non-Linux OSes such as Windows.

On the other hand, a PV IOMMU should be faster to develop because there is no need to emulate a hardware interface. Additionally, a single PV IOMMU implementation could support multiple underlying hardware IOMMUs. Specifically, it could be used for both Intel and AMD platforms.

Quoted 67 lines
      /**
        * IOMMU_alloc_nested
        * Create a nested IOMMU context (needs IOMMUCAP_nested).
        *
        * This context uses a platform-specific page table from domain address
      space
        * specified in pgtable_gfn and use it for nested translations.
        *
        * Explicit flushes needs to be submited with IOMMU_flush_nested on
        * modification of the nested pagetable to ensure coherency between
      IOTLB and
        * nested page table.
        *
        * This context can be destroyed using IOMMU_free_context.
        * This context cannot be modified using map_pages, unmap_pages.
        */
      struct pv_iommu_alloc_nested {
           /* OUT: allocated IOMMU context number */
           uint16_t ctx_no;

           /* IN: guest frame number of the nested page table */
           uint64_aligned_t pgtable_gfn;

           /* IN: nested mode flags */
           uint64_aligned_t nested_flags;
      };
      typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
      DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

      /**
        * IOMMU_flush_nested (needs IOMMUCAP_nested)
        * Flush the IOTLB for nested translation.
        */
      struct pv_iommu_flush_nested {
           /* TODO */
      };
      typedef struct pv_iommu_flush_nested pv_iommu_flush_nested_t;
      DEFINE_XEN_GUEST_HANDLE(pv_iommu_flush_nested_t);


      >
      >
      >
      >> [1] Originally
      >> https://lists.xen.org/archives/html/xen-devel/2024-06/msg01145.html but
      >> this patch got quite old and probably doesn't work anymore with this new
      >> Xen patch series.
      >> I have a updated patch in my xen-pviommu branch
      >> https://gitlab.com/xen-project/people/tsnake41/linux/-/commit/125d67a09fa9f66a32f9175641cfccca22dbbdb6
      >>
      >> [2] You also need to set "vfio_iommu_type1.allow_unsafe_interrupts=1" to
      >> make VFIO work for now.

      Thanks
      Teddy



      Teddy Astie | Vates XCP-ng Developer

      XCP-ng & Xen Orchestra - Vates solutions

      web: https://vates.tech
#11 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

Hi Stefano,

Quoted 20 lines · Stefano Stabellini
Quoted 3 lines · Julien Grall

On Tue, 4 Feb 2025 at 11:46, Teddy Astie <[email protected]> wrote:
If the hardware supports it, there is a alternative (still being drafted) interface to allow the guest to directly provide native

pagetables.

Quoted 6 lines

This is exposed through the "_nested" subcommands, there is no implementation of this feature in this patch series yet.

Out of interest, if the HW support two stage translations, then why do

we need a PV interface? Wouldn’t it be better to exposed an emulated

Quoted 1 line

iommu? This would reduce the amount of enlightenment required in the

guest OS. In the long run, this would provide some better performance

Quoted 1 line

because some IOMMU HW can now accelerate TLB flush commands (among

other things). For instance, see the NVIDIA vIOMMU.

Hi Julien, I am not Teddy, but I have been considering the same question, and here are the conclusions I reached.

Quoted 6 lines

A virtual IOMMU of the same type as the underlying IOMMU provides better operating system compatibility, particularly for running non-Linux OSes such as Windows.

On the other hand, a PV IOMMU should be faster to develop because there is no need to emulate a hardware interface.

Additionally, a single PV

Quoted 2 lines

IOMMU implementation could support multiple underlying hardware IOMMUs. Specifically, it could be used for both Intel and AMD platforms.

I agree with both points. However what matters is the performance. In the case of nested, my concern with the PV approach is you are never going to reach the same performance as native. I am not talking about setting up the page tables, but more the TLB flushes. Have we measure the overhead and whether that’s acceptable?

With an emulated solution and HW support, virtual TLB flush could come for free because they will be handled directly by the HW. AFAICT, with a PV approach you are never going to be able to take advantage of this.

Cheers,

Quoted 77 lines
Quoted 9 lines

/**
* IOMMU_alloc_nested
* Create a nested IOMMU context (needs IOMMUCAP_nested). *
* This context uses a platform-specific page table from domain

address

Quoted 4 lines

space
* specified in pgtable_gfn and use it for nested translations. *
* Explicit flushes needs to be submited with IOMMU_flush_nested

on

Quoted 1 line

* modification of the nested pagetable to ensure coherency

between

Quoted 27 lines
      IOTLB and
        * nested page table.
        *
        * This context can be destroyed using IOMMU_free_context.
        * This context cannot be modified using map_pages, unmap_pages.
        */
      struct pv_iommu_alloc_nested {
           /* OUT: allocated IOMMU context number */
           uint16_t ctx_no;

           /* IN: guest frame number of the nested page table */
           uint64_aligned_t pgtable_gfn;

           /* IN: nested mode flags */
           uint64_aligned_t nested_flags;
      };
      typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
      DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

      /**
        * IOMMU_flush_nested (needs IOMMUCAP_nested)
        * Flush the IOTLB for nested translation.
        */
      struct pv_iommu_flush_nested {
           /* TODO */
      };
      typedef struct pv_iommu_flush_nested pv_iommu_flush_nested_t;
Quoted 8 lines
      DEFINE_XEN_GUEST_HANDLE(pv_iommu_flush_nested_t);


      >
      >
      >
      >> [1] Originally
      >>

https://lists.xen.org/archives/html/xen-devel/2024-06/msg01145.html but

Quoted 1 line

>> this patch got quite old and probably doesn't work anymore with

this new

Quoted 3 lines

>> Xen patch series.
>> I have a updated patch in my xen-pviommu branch >>

https://gitlab.com/xen-project/people/tsnake41/linux/-/commit/125d67a09fa9f66a32f9175641cfccca22dbbdb6

Quoted 2 lines

>>
>> [2] You also need to set

"vfio_iommu_type1.allow_unsafe_interrupts=1" to

Quoted 15 lines

>> make VFIO work for now.

Thanks
Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech

#12 lore raw

Re: [XEN RFC PATCH v5 3/5] xen/public: Introduce PV-IOMMU hypercall interface

In reply to Teddy Astie
Quoted 168 lines · Teddy Astie

Hello Stefano,

Le 03/02/2025 à 18:47, Stefano Stabellini a écrit :

Quoted 87 lines · Teddy Astie
Quoted 66 lines

Hello Jason,

Le 30/01/2025 à 21:17, Jason Andryuk a écrit :

Quoted 29 lines

Hi Teddy,

Thanks for working on this.  I'm curious about your plans for this:

Quoted 24 lines · Teddy Astie

+/**
+ * IOMMU_alloc_nested
+ * Create a nested IOMMU context (needs IOMMUCAP_nested).
+ *
+ * This context uses a platform-specific page table from domain address space
+ * specified in pgtable_gfn and use it for nested translations.
+ *
+ * Explicit flushes needs to be submited with IOMMU_flush_nested on
+ * modification of the nested pagetable to ensure coherency between IOTLB and
+ * nested page table.
+ *
+ * This context can be destroyed using IOMMU_free_context.
+ * This context cannot be modified using map_pages, unmap_pages.
+ */

xen/include/public/pv-iommu.h new file +8 -0
+struct pv_iommu_alloc_nested {
+    /* OUT: allocated IOMMU context number */
+    uint16_t ctx_no;
+
+    /* IN: guest frame number of the nested page table */
+    uint64_aligned_t pgtable_gfn;
+
+    /* IN: nested mode flags */
Quoted 9 lines

xen/include/public/pv-iommu.h line 291

Quoted 4 lines

+    uint64_aligned_t nested_flags;

xen/include/public/pv-iommu.h new file +3 -0
+};
+typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t;
+DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

Is this command intended to be used for GVA -> GPA translation?  Would you need some way to associate with another iommu context for GPA -> HPA translation?

It's intended to be used for accelerating IOMMU emulation for the guest. So in this case the other GPA->HPA translation is domain's p2m page-table (or something similar) such as the translations made from this nested context is meaningful from guest point of view.

The idea to use it is to use the "remote_op" sub-command to let the device model (e.g QEMU) alter the IOMMU behavior for the affected domain (e.g by reattaching devices, making new IOMMU contexts, ...).

I think it can also be used for virtio-iommu pagetable.

Quoted 5 lines

Maybe more broadly, what are your goals for enabling PV-IOMMU?  The examples on your blog post cover a domain restrict device access to specific portions of the the GPA space.  Are you also interested in GVA -> GPA?  Does VFIO required GVA -> GPA?

The current way of enabling and using PV-IOMMU is with the dedicated Linux IOMMU driver [1] that implements Linux's IOMMU subsystem with this proposed interface.
This in practice in the PV case replaces the xen-swiotlb with dma-iommu and do all DMA through the paravirtualized IOMMU (e.g creating DMA domains, moving devices to it).

Quoted 4 lines

Regarding GVA->GPA, this is what this interface provides, and restricting device access to memory is one way of using it. This is a requirement for VFIO (as it is also one for Linux IOMMU), and I managed to make SPDK and DPDK work in Dom0 using VFIO and these patches [2].

Thanks for the explanation, Teddy. It certainly seems that this work is moving in the right direction. However, I have a couple of questions on this point, as I admit that I have not fully understood it.

Modern IOMMUs support two stages of translation. Using ARM terminology, these are referred to as stage2 and stage1. The stage2 translation is used by Xen for the domain's GPA->PA (p2m) mapping. The pagetable used for stage2 could potentially be shared with the pagetable used by Xen for the p2m. Stage1 is typically controlled by the guest for its own address translations, mapping guest virtual addresses (GVA) to guest physical addresses (GPA).

I can see that this patch series introduces an interface that allows Dom0 to modify its own stage2 mappings.

Quoted 8 lines

My question is: how do we allow the domain to also set up stage1 mappings? I would assume that the interface would take the form of a hypercall that allows a domain to pass a stage1 pagetable pointer, which Xen would then use to configure the IOMMU stage1. However, I do not see such a hypercall in your series. I was under the impression that GVA-to-GPA translation was left as future work, so I am confused by your statement that this patch series already provides it.

There are 2 interfaces for the guest (and device model) to configure its GVA-to-GPA mappings. There are map/unmap subcommands and (depending on hardware support) nested iommu contexts where the guest/device model provides the GPA of the stage1 pagetable (this is meant to be a way of accelerating IOMMU emulation through QEMU).

Here is the hypercall subcommands for map/unmap where you can map and unmap pages to the "paravirtualized IOMMU context" (making the mapped region visible to devices of the context attached through reattach_device).

/**
* IOMMU_map_pages
* Map pages on a IOMMU context.
*
* pgsize must be supported by pgsize_mask.
* Fails with -EINVAL if mapping on top of another mapping.
* Report actually mapped page count in mapped field (regardless of
failure).
*/

struct pv_iommu_map_pages {
     /* IN: IOMMU context number */
     uint16_t ctx_no;

     /* IN: Guest frame number */
     uint64_aligned_t gfn;

     /* IN: Device frame number */
     uint64_aligned_t dfn;

     /* IN: Map flags */
     uint32_t map_flags;

     /* IN: Size of pages to map */
     uint32_t pgsize;

     /* IN: Number of pages to map */
     uint32_t nr_pages;

     /* OUT: Number of pages actually mapped */
     uint32_t mapped;
};

typedef struct pv_iommu_map_pages pv_iommu_map_pages_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_map_pages_t);

/**
* IOMMU_unmap_pages
* Unmap pages on a IOMMU context.
*
* pgsize must be supported by pgsize_mask.
* Report actually unmapped page count in mapped field (regardless of
failure).
* Fails with -ENOENT when attempting to unmap a page without any mapping */

struct pv_iommu_unmap_pages {
     /* IN: IOMMU context number */
     uint16_t ctx_no;

     /* IN: Device frame number */
     uint64_aligned_t dfn;

     /* IN: Size of pages to unmap */
     uint32_t pgsize;

     /* IN: Number of pages to unmap */
     uint32_t nr_pages;

     /* OUT: Number of pages actually unmapped */
     uint32_t unmapped;
};
Quoted 5 lines

typedef struct pv_iommu_unmap_pages pv_iommu_unmap_pages_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_unmap_pages_t);

If the hardware supports it, there is a alternative (still being drafted) interface to allow the guest to directly provide native pagetables.

OK, this is the important one. I am glad you already thought about it. If possible, I would suggest to have a rough PoC in place just to prove that the interface would work for the intended usecase.

For interfaces, sometimes it is hard to spot if there are any issues during review, so having a barebone PoC to validate the concept would be ideal.

Thanks for your work on this.

Quoted 67 lines

This is exposed through the "_nested" subcommands, there is no implementation of this feature in this patch series yet.

/**
* IOMMU_alloc_nested
* Create a nested IOMMU context (needs IOMMUCAP_nested).
*
* This context uses a platform-specific page table from domain address
space
* specified in pgtable_gfn and use it for nested translations. *
* Explicit flushes needs to be submited with IOMMU_flush_nested on
* modification of the nested pagetable to ensure coherency between
IOTLB and
* nested page table.
*
* This context can be destroyed using IOMMU_free_context.
* This context cannot be modified using map_pages, unmap_pages. */

struct pv_iommu_alloc_nested {
     /* OUT: allocated IOMMU context number */
     uint16_t ctx_no;

     /* IN: guest frame number of the nested page table */
     uint64_aligned_t pgtable_gfn;

     /* IN: nested mode flags */
     uint64_aligned_t nested_flags;
};

typedef struct pv_iommu_alloc_nested pv_iommu_alloc_nested_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_alloc_nested_t);

/**
* IOMMU_flush_nested (needs IOMMUCAP_nested)
* Flush the IOTLB for nested translation.
*/

struct pv_iommu_flush_nested {
     /* TODO */
};

typedef struct pv_iommu_flush_nested pv_iommu_flush_nested_t; DEFINE_XEN_GUEST_HANDLE(pv_iommu_flush_nested_t);

Quoted 10 lines
Quoted 10 lines

[1] Originally
https://lists.xen.org/archives/html/xen-devel/2024-06/msg01145.html but this patch got quite old and probably doesn't work anymore with this new Xen patch series.
I have a updated patch in my xen-pviommu branch
https://gitlab.com/xen-project/people/tsnake41/linux/-/commit/125d67a09fa9f66a32f9175641cfccca22dbbdb6

Quoted 2 lines
Quoted 2 lines

[2] You also need to set "vfio_iommu_type1.allow_unsafe_interrupts=1" to make VFIO work for now.

Thanks
Teddy

Teddy Astie | Vates XCP-ng Developer

XCP-ng & Xen Orchestra - Vates solutions

web: https://vates.tech