Xen PV IOMMU interface draft B
Hi All,
Here is a design for allowing guests to control the IOMMU. This allows for the guest GFN mapping to be programmed into the IOMMU and avoid using the SWIOTLB bounce buffer technique in the Linux kernel (except for legacy 32 bit DMA IO devices).
Draft B has been expanded to include Bus Address mapping/lookup for Mediated pass-through emulators.
The pandoc markdown format of the document is provided below to allow for easier inline comments:
% Xen PV IOMMU interface
% Malcolm Crossley <<[email protected]>>
Paul Durrant <<[email protected]>>
% Draft B
Introduction
============
Revision History
----------------
--------------------------------------------------------------------
Version Date Changes ------- ----------- ---------------------------------------------- Draft A 10 Apr 2014 Initial draft.
Draft B 12 Jun 2015 Second draft.
--------------------------------------------------------------------
Background
==========
Linux kernel SWIOTLB
--------------------
Xen PV guests use a Pseudophysical Frame Number(PFN) address space which is decoupled from the host Machine Frame Number(MFN) address space.
PV guest hardware drivers are only aware of the PFN address space only and assume that if PFN addresses are contiguous then the hardware addresses would be contiguous as well. The decoupling between PFN and MFN address spaces means PFN and MFN addresses may not be contiguous across page boundaries and thus a buffer allocated in GFN address space which spans a page boundary may not be contiguous in MFN address space.
PV hardware drivers cannot tolerate this behaviour and so a special "bounce buffer" region is used to hide this issue from the drivers.
A bounce buffer region is a special part of the PFN address space which has been made to be contiguous in both PFN and MFN address spaces. When a driver requests a buffer which spans a page boundary be made available for hardware to read the core operating system code copies the buffer into a temporarily reserved part of the bounce buffer region and then returns the MFN address of the reserved part of the bounce buffer region back to the driver itself. The driver then instructs the hardware to read the copy of the buffer in the bounce buffer. Similarly if the driver requests a buffer is made available for hardware to write to the first a region of the bounce buffer is reserved and then after the hardware completes writing then the reserved region of bounce buffer is copied to the originally allocated buffer.
The overheard of memory copies to/from the bounce buffer region is high and damages performance. Furthermore, there is a risk the fixed size bounce buffer region will become exhausted and it will not be possible to return an hardware address back to the driver. The Linux kernel drivers do not tolerate this failure and so the kernel is forced to crash, as an uncorrectable error has occurred.
Input/Output Memory Management Units (IOMMU) allow for an inbound address mapping to be created from the I/O Bus address space (typically PCI) to the machine frame number address space. IOMMU's typically use a page table mechanism to manage the mappings and therefore can create mappings of page size granularity or larger.
The I/O Bus address space will be referred to as the Bus Frame Number (BFN) address space for the rest of this document.
Mediated Pass-through Emulators
-------------------------------
Mediated Pass-through emulators allow guest domains to interact with hardware devices via emulator mediation. The emulator runs in a domain separate to the guest domain and it is used to enforce security of guest access to the hardware devices and isolation of different guests accessing the same hardware device.
The emulator requires a mechanism to map guest address's to a bus address that the hardware devices can access.
Clarification of GFN and BFN fields for different guest types
-------------------------------------------------------------
Guest Frame Numbers (GFN) definition varies depending on the guest type.
Diagram below details the memory accesses originating from CPU, per guest type:
HVM guest PV guest
(VA) (VA)
| |
MMU MMU
| |
(GFN) |
| | (GFN)
HAP a.k.a EPT/NPT |
| |
(MFN) (MFN)
| |
RAM RAM
For PV guests GFN is equal to MFN for a single page but not for a contiguous range of pages.
Bus Frame Numbers (BFN) refer to the address presented on the physical bus before being translated by the IOMMU.
Diagram below details memory accesses originating from physical device.
Physical Device
|
(BFN)
|
IOMMU-PT
|
(MFN)
|
RAMPurpose
=======
1. Allow Xen guests to create/modify/destroy IOMMU mappings for hardware devices that the PV guests has access to. This enables the PV guest to program a bus address space mapping which matches it's GFN mapping. Once a 1-1 mapping of PFN to bus address space is created then a bounce buffer region is not required for the IO devices connected to the IOMMU.
2. Allow for Xen guests to lookup/create/modify/destroy IOMMU mappings for guest memory of domains the calling Xen guest has sufficient privilege over. This enables domains to provide mediated hardware acceleration to other guest domains.
Xen Architecture
================
The Xen architecture consists of a new hypercall interface and changes to the grant map interface.
The existing IOMMU mappings setup at domain creation time will be preserved so that PV domains unaware of this feature will continue to function with no changes required.
Memory ballooning will be supported by taking an additional reference on the MFN backing the GFN for each successful IOMMU mapping created.
An M2B tracking structure will be used to ensure all reference's to a MFN can be located easily.
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_t do_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)
First argument, guest handle pointer to array of struct pv_iommu_op Second argument, unsigned integer count of struct pv_iommu_op elements in array.
Definition of struct pv_iommu_op:
struct pv_iommu_op {
uint16_t subop_id;
uint16_t flags;
int32_t status;
union {
struct {
uint64_t bfn;
uint64_t gfn;
} map_page;
struct {
uint64_t bfn;
} unmap_page;
struct {
uint64_t bfn;
uint64_t gfn;
uint16_t domid;
ioservid_t ioserver;
} map_foreign_page;
struct {
uint64_t bfn;
uint64_t gfn;
uint16_t domid;
ioservid_t ioserver;
} lookup_foreign_page;
struct {
uint64_t bfn;
ioservid_t ioserver;
} unmap_foreign_page;
} u;
};Definition of PV IOMMU subops:
#define IOMMUOP_query_caps 1
#define IOMMUOP_map_page 2
#define IOMMUOP_unmap_page 3
#define IOMMUOP_map_foreign_page 4
#define IOMMUOP_lookup_foreign_page 5
#define IOMMUOP_unmap_foreign_page 6Design considerations for hypercall op
-------------------------------------------
IOMMU map/unmap operations can be slow and can involve flushing the IOMMU TLB to ensure the IO device uses the updated mappings.
The op has been designed to take an array of operations and a count as parameters. This allows for easily implemented hypercall continuations to be used and allows for batches of IOMMU operations to be submitted before flushing the IOMMU TLB.
The subop_id to be used for a particular element is encoded into the element itself. This allows for map and unmap operations to be performed in one hypercall and for the IOMMU TLB flushing optimisations to be still applied.
The hypercall will ensure that the required IOMMU TLB flushes are applied before returning to guest via either hypercall completion or a hypercall continuation.
IOMMUOP_query_caps
------------------
This subop queries the runtime capabilities of the PV-IOMMU interface for the specific called domain. This subop uses struct pv_iommu_op directly.
------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `flags` [out] This field details the IOMMUOP capabilities.
status [out] Status of this op, op specific values listed below
------------------------------------------------------------------------------
Defined bits for flags field:
------------------------------------------------------------------------------
Name Bit Definition
---- ------ ----------------------------------IOMMU_QUERY_map_cap 0 IOMMUOP_map_page or IOMMUOP_map_foreign
can be used for this domain
IOMMU_QUERY_map_all_gfns 1 IOMMUOP_map_page subop can map any MFN
not used by Xen
Reserved for future use 2-9 n/a
IOMMU_page_order 10-15 Returns maximum possible page order for
all other IOMMUOP subops
------------------------------------------------------------------------------
Defined values for query_caps subop status field:
Value Reason ------ ---------------------------------------------------------- 0 subop successfully returned
IOMMUOP_map_page
----------------------
This subop uses struct map_page part of the struct pv_iommu_op.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFN's except for Xen owned MFN's else the hardware domain will only be allowed to map GFN's which it owns.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFN's without taking a reference to the MFN backing the GFN by setting the IOMMU_MAP_OP_no_ref_cnt flag.
Every successful pv_iommu_op will result in an additional page reference being taken on the MFN backing the GFN except for the condition detailed above.
If the map_op flags indicate a writeable mapping is required then a writeable page type reference will be taken otherwise a standard page reference will be taken.
All the following conditions are required to be true for PV IOMMU map subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. If hardware_domain and the following Xen IOMMU options are NOT enabled: dom0-passthrough
This subop usage of the "struct pv_iommu_op" and ``struct map_page` fields are detailed below:
------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `bfn` [in] Bus address frame number(BFN) to be mapped to specified gfn
below
gfn [in] Guest address frame number for DOMID_SELF
flags [in] Flags for signalling type of IOMMU mapping to be created,
Flags can be combined.
status [out] Mapping status of this op, op specific values listed below
------------------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMU_OP_readable 0 Create readable IOMMU mapping IOMMU_OP_writeable 1 Create writeable IOMMU mapping IOMMU_MAP_OP_no_ref_cnt 2 IOMMU mapping does not take a reference to
MFN backing BFN mapping
Reserved for future use 3-9 n/aIOMMU_page_order 10-15 Page order to be used for both gfn and bfn
Defined values for map_page subop status field:
Value Reason ------ ---------------------------------------------------------------------- 0 subop successfully returned -EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM Domain is not a domain and GFN does not belong to domain -EPERM Domain is a hardware domain, IOMMU dom-strict mode is enabled and
GFN does not belong to domain-EACCES BFN address conflicts with RMRR regions for device's attached to
DOMID_SELF-ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMUOP_unmap_page
------------------
This subop uses struct unmap_page part of the struct pv_iommu_op.
The subop usage of the "struct pv_iommu_op" and ``struct unmap_page` fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
flags [in] Flags for signalling page order of unmap operation
status [out] Mapping status of this unmap operation, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------
Reserved for future use 0-9 n/aIOMMU_page_order 10-15 Page order to be used for bfn
Defined values for unmap_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned -EIO IOMMU unit returned error when attempting to unmap BFN. -ENOSPC Page order is too large for either BFN address or IOMMU unit
------------------------------------------------------------------------
IOMMUOP_map_foreign_page
----------------
This subop uses struct map_foreign_page part of the struct pv_iommu_op.
It is not valid to use domid representing the calling domain.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
If there IOMMU support then the specified BFN is returned for the GFN + domid combination
The M2B mechanism is a MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
All the following conditions are required to be true for PV IOMMU map_foreign subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is a hardware_domain and the following Xen IOMMU options are NOT enabled: dom0-passthrough
This subop usage of the "struct pv_iommu_op" and ``struct map_foreign_page` fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain ID for which the gfn field applies
ioserver [in] IOREQ server id associated with mapping
bfn [in] Bus address frame number for gfn address
gfn [in] Guest address frame number
flags [in] Details the status of the BFN mapping
status [out] status of this subop, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safely
swapped to scratch page
Reserved for future use 3-9 Reserved flag bits should be 0IOMMU_page_order 10-15 Returns maximum possible page order for
all other IOMMUOP subops
Defined values for map_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned
-EIO IOMMU unit returned error when attempting to map BFN to GFN.
-EPERM Calling domain does not have sufficient privilege over domid
-EPERM GFN could not be mapped because the GFN belongs to Xen.
-EPERM domid maps to DOMID_SELF
-EACCES BFN address conflicts with RMRR regions for device's attached to
DOMID_SELF
-ENODEV Provided ioserver id is not valid
-ENXIO Provided domid id is not valid
-ENXIO Provided GFN address is not valid
-ENOSPC Page order is too large for either BFN, GFN or IOMMU unitIOMMU_lookup_foreign_page
----------------
This subop uses struct lookup_foreign_page part of the struct pv_iommu_op.
If the BFN is specified as an input and parameter and there is no IOMMU support for the calling domain then an error will be returned.
It is the calling domain responsibility to ensure there are no conflicts
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
This subop usage of the "struct pv_iommu_op" and ``struct lookup_foreign_page` fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain ID for which the gfn field applies
ioserver [in] IOREQ server id associated with mapping
bfn [out] Bus address frame number for gfn address
gfn [in] Guest address frame number
flags [out] Details the status of the BFN mapping
status [out] status of this subop, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 Returned BFN IOMMU mapping is readable IOMMUOP_writeable 1 Returned BFN IOMMU mapping is writeable Reserved for future use 2-9 Reserved flag bits should be 0 IOMMU_page_order 10-15 Returns maximum possible page order for
all other IOMMUOP subops
Defined values for lookup_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned -EPERM Calling domain does not have sufficient privilege over domid -ENOENT There is no available BFN for provided GFN + domid combination -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid
IOMMUOP_unmap_foreign_page
----------------
This subop uses struct unmap_foreign_page part of the struct pv_iommu_op.
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
If there is IOMMU support then the specified BFN is returned for the GFN + domid combination
Each successful subop will add to the M2B if there was not an existing identical M2B entry. The
Every new M2B entry will take a reference to the MFN backing the GFN.
This subop usage of the "struct pv_iommu_op" and ``struct unmap_foreign_page` fields are detailed below:
-----------------------------------------------------------------------
Field Purpose
----- --------------------------------------------------------ioserver [in] IOREQ server id associated with mapping
bfn [in] Bus address frame number for gfn address
flags [out] Flags for signalling page order of unmap operation
status [out] status of this subop, 0 indicates success
-----------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition ---- ----- ---------------------------------- Reserved for future use 0-9 n/a IOMMU_page_order 10-15 Page order to be used for bfn
Defined values for unmap_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned-ENOENT There is no mapped BFN + ioserver id combination to unmap
IOMMUOP_*_foreign_page interactions with guest domain ballooning
================================================================
Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered IO request of type IOREQ_TYPE_INVALIDATE to the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered IO request ring is full then a standard (synchronous) IO request of type IOREQ_TYPE_INVALIDATE will be issued to the affected IOREQ server the with just invalidated BFN address in the data field.
The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a IOMMUOP_unmap_foreign_page subop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.
For hosts with IOMMU support:
If the BFN was mapped without the IOMMUOP_swap_mfn flag set in the IOMMUOP_map_foreign_page then the affected affected emulator(s) must specifically issue a IOMMUOP_unmap_foreign_page subop for the now invalid BFN address so that the references to the underlying MFN are removed.
If the BFN was mapped with the IOMMUOP_swap_mfn flag set in the IOMMUOP_map_foreign_page subop for all emulators with mappings of that GFN then the BFN mapping will be swapped to point at a scratch MFN page and all BFN references to the invalid MFN will be removed by Xen after the BFN mapping has been updated to point at the scratch MFN page.
The rationale for swapping the BFN mapping to point at scratch pages is to enable guest domains to balloon quickly without requiring hypercall(s) from emulators.
Not all BFN mappings can be swapped without potentially causing problems for the hardware itself (command rings etc.) so the IOMMUOP_swap_mfn flag is used to allow per BFN control of Xen ballooning behaviour.
PV IOMMU interactions with self ballooning
==========================================
The guest should clear any IOMMU mappings it has of it's own pages before releasing a page back to Xen. It will need to add IOMMU mappings after repopulating a page with the populate_physmap hypercall.
This requires that IOMMU mappings get a writeable page type reference count and that guests clear any IOMMU mappings before pinning page table pages.
Security Implications of allowing domain IOMMU control
===============================================================
Xen currently allows IO devices attached to hardware domain to have direct access to the all of the MFN address space (except Xen hypervisor memory regions), provided the Xen IOMMU option dom0-strict is not enabled.
The PV IOMMU feature provides the same level of access to MFN address space and the feature is not enabled when the Xen IOMMU option dom0-strict is enabled. Therefore security is not degraded by the PV IOMMU feature.
Domains with physical device(s) assigned which are not hardware domains are only allowed to map their own GFNs or GFNs for domain(s) they have privilege over.
PV IOMMU interactions with grant map/unmap operations
=====================================================
Grant map operations return a Physical device accessible address (BFN) if the GNTMAP_device_map flag is set. This operation currently returns the MFN for PV guests which may conflict with the BFN address space the guest uses if PV IOMMU map support is available to the guest.
This design proposes to allow the calling domain to control the BFN address that a grant map operation uses.
This can be achieved by specifying that the dev_bus_addr in the gnttab_map_grant_ref structure is used an input parameter instead of the output parameter it is currently.
Only PAGE_SIZE aligned addresses are allowed for dev_bus_addr input parameter.
The revised structure is shown below for convenience.
struct gnttab_map_grant_ref {
/* IN parameters. */
uint64_t host_addr;
uint32_t flags; /* GNTMAP_* */
grant_ref_t ref;
domid_t dom;
/* OUT parameters. */
int16_t status; /* => enum grant_status */
grant_handle_t handle;
/* IN/OUT parameters */
uint64_t dev_bus_addr;
};The grant map operation would then behave similarly to the IOMMUOP_map_page subop for the creation of the IOMMU mapping.
The grant unmap operation would then behave similarly to the IOMMUOP_unmap_page subop for the removal of the IOMMU mapping.
A new grantmap flag would be used to indicate the domain is requesting the dev_bus_addr field is used an input parameter.
#define _GNTMAP_request_bfn_map (6)
#define GNTMAP_request_bfn_map (1<<_GNTMAP_request_bfn_map)Linux kernel architecture
=========================
The Linux kernel will use the PV-IOMMU hypercalls to map it's PFN address space into the IOMMU. It will map the PFN's to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
The native SWIOTLB will be used to handle device's which cannot DMA to all of the kernel's PFN address space.
An interface shall be provided for emulator usage of IOMMUOP_*_foreign_page subops which will allow the Linux kernel to centrally manage that domains BFN resource and ensure there are no unexpected conflicts.
Emulator usage of PV IOMMU interface
====================================
Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
A IOMMUOP_query_caps subop will return the IOMMU_QUERY_map_cap flag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via the IOMMUOP_map_foreign_page subop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via the IOMMUOP_lookup_foreign_page.
Operating systems which use the IOMMUOP_map_page subop are expected to provide a common interface for emulators
Emulators should unmap unused GFN mappings as often as possible using IOMMUOP_unmap_foreign_page subops so that guest domains can balloon pages quickly and efficiently.
Emulators should conform to the ballooning behaviour described section "IOMMUOP_*_foreign_page interactions with guest domain ballooning" so that guest domains are able to effectively balloon out and in memory.
Emulators must unmap any active BFN mappings when they shutdown.
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.06.15 at 18:43, <[email protected]> wrote:
IOMMUOP_query_caps
------------------This subop queries the runtime capabilities of the PV-IOMMU interface for the
specific called domain. This subop usesstruct pv_iommu_opdirectly.
"calling domain" perhaps?
Quoted 11 lines
----------------------------------------------------------------------------
--
Field Purpose
-----
---------------------------------------------------------------flags[out] This field details the IOMMUOP capabilities.
status[out] Status of this op, op specific values listed below
----------------------------------------------------------------------------
--
Quoted 8 lines
Defined bits for flags field:
----------------------------------------------------------------------------
--
Name Bit Definition
---- ------ ----------------------------------IOMMU_QUERY_map_cap0IOMMUOP_map_pageorIOMMUOP_map_foreign
can be used for this domain
"this" (see also above) perhaps being the calling domain? In which case I wonder how the "for" and IOMMUOP_map_foreign are meant to fit together: I assume the flag to indicate that mapping into the (calling) domain is possible. Which then makes me wonder - what use if the new hypercall when this flag isn't set?
Quoted 2 lines
IOMMU_QUERY_map_all_gfns1IOMMUOP_map_pagesubop can map any MFN
not used by Xen
"gfns" or "MFN"?
Review
Quoted 9 lines
Defined values for
map_pagesubop status field:Value Reason
------
----------------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-3-EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM Domain is not a domain and GFN does not belong to domain
"is not a hardware domain"? Also, I think we're pretty determined for there to ever only be one, so perhaps it should be "the hardware domain" here and elsewhere.
Quoted 11 lines
IOMMUOP_unmap_page
------------------
This subop usesstruct unmap_pagepart of thestruct pv_iommu_op.The subop usage of the "struct
pv_iommu_op" and ``struct unmap_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
Has it been determined that unmapping based on GFN is never going to be needed, and that unmapping by BFN is the more practical solution? The map counterpart doesn't seem to exclude establishing multiple mappings for the same BFN, and hence the inverse here would become kind of fuzzy in that case.
Quoted 5 lines
IOMMUOP_map_foreign_page
----------------
This subop usesstruct map_foreign_pagepart of thestruct pv_iommu_op.It is not valid to use domid representing the calling domain.
And what's the point of that? Was it considered to have only one map/unmap pair, capable of mapping both local and foreign pages? If so, what speaks against that?
Quoted 1 line
The M2B mechanism is a MFN to (BFN,domid,ioserver) tuple.
I didn't see anything explaining the significance of this (namely the ioserver part, I think I can see the need for the domid), can you explain the backgound here please?
Quoted 1 line
Every new M2B entry will take a reference to the MFN backing the GFN.
What happens when that GFN->MFN mapping changes?
Quoted 8 lines
All the following conditions are required to be true for PV IOMMU
map_foreign
subop to succeed:1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is ahardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthrough
Is there a way for the hardware domain to know it is running in pass-through mode? Also, "the domain" is ambiguous here; I'm sure you mean the invoking domain, not the one owning the page.
Quoted 11 lines
This subop usage of the "struct
pv_iommu_op" and ``struct map_foreign_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain ID for which the gfn field applies
ioserver[in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn address
In the description above you speak of returning data in this field. Is [in] really correct?
Quoted 8 lines
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safelyswapped to scratch page
Scratch page? This needs some explanation.
Quoted 3 lines
Reserved for future use 3-9 Reserved flag bits should be 0
IOMMU_page_order10-15 Returns maximum possible page order for
all other IOMMUOP subops
"Returns"? "other"?
Quoted 8 lines
IOMMU_lookup_foreign_page
----------------
This subop usesstruct lookup_foreign_pagepart of thestruct pv_iommu_op.If the BFN is specified as an input and parameter and there is no IOMMU support
for the calling domain then an error will be returned.
"input and parameter"? The field is marked [out] below, and I also can't see a flag allowing this to be optional (the more that flags is also [out]).
Quoted 1 line
It is the calling domain responsibility to ensure there are no conflicts
No races you mean?
Quoted 5 lines
Each successful subop will add to the M2B if there was not an existing identical
M2B entry.Every new M2B entry will take a reference to the MFN backing the GFN.
This is a lookup - why would it add something somewhere? Perhaps this is just a copy-and-paste mistake? Or is the use of "lookup" here misleading (as the last section of the document seems to suggest)?
Quoted 9 lines
Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------IOMMUOP_readable0 Returned BFN IOMMU mapping is
readableIOMMUOP_writeable1 Returned BFN IOMMU mapping is
writeable
Reserved for future use 2-9 Reserved flag bits should be 0
Reserved flags will be returned as 0.
Review
Quoted 7 lines
Defined values for
lookup_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-2-EPERM Calling domain does not have sufficient privilege over domid -ENOENT There is no available BFN for provided GFN + domid combination
Throughout here there seems to be a mixture of references to (GFN, domid) pairs anmd (GFN,domid,ioserver) triplets. Along with there being some explanation missing, these need to be made consistent to avoid confusion.
Quoted 8 lines
IOMMUOP_unmap_foreign_page
----------------
This subop usesstruct unmap_foreign_pagepart of thestruct pv_iommu_op.If there is no IOMMU support then the MFN is returned in the BFN field (that is
the only valid bus address for the GFN + domid combination).
Copy-and-paste mistake again (doesn't seem to apply to unmap)?
Quoted 9 lines
If there is IOMMU support then the specified BFN is returned for the GFN + domid
combinationEach successful subop will add to the M2B if there was not an existing identical
M2B entry. TheEvery new M2B entry will take a reference to the MFN backing the GFN.
And again?
Quoted 4 lines
This subop usage of the "struct
pv_iommu_op" and ``struct unmap_foreign_page` fields
are detailed below:
Quoted 8 lines
-----------------------------------------------------------------------
Field Purpose
----- --------------------------------------------------------ioserver[in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn address
flags[out] Flags for signalling page order of unmap operation
[out]?
Quoted 4 lines
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned-ENOENTThere is no mapped BFN + ioserver id combination to unmap
Yet another pair, the unique mapping properties of which don't follow from anything said earlier.
Quoted 10 lines
PV IOMMU interactions with self ballooning
==========================================The guest should clear any IOMMU mappings it has of it's own pages before releasing a page back to Xen. It will need to add IOMMU mappings after repopulating a page with the
populate_physmaphypercall.This requires that IOMMU mappings get a writeable page type reference count and
that guests clear any IOMMU mappings before pinning page table pages.
I suppose this is only for aware PV guests. If so, perhaps this should be made explicit.
Quoted 38 lines
PV IOMMU interactions with grant map/unmap operations
=====================================================Grant map operations return a Physical device accessible address (BFN) if the
GNTMAP_device_mapflag is set. This operation currently returns the MFN for PV
guests which may conflict with the BFN address space the guest uses if PV IOMMU
map support is available to the guest.This design proposes to allow the calling domain to control the BFN address that
a grant map operation uses.This can be achieved by specifying that the
dev_bus_addrin thegnttab_map_grant_refstructure is used an input parameter instead of the output parameter it is currently.Only
PAGE_SIZEaligned addresses are allowed fordev_bus_addrinput parameter.The revised structure is shown below for convenience.
struct gnttab_map_grant_ref { /* IN parameters. */ uint64_t host_addr; uint32_t flags; /* GNTMAP_* */ grant_ref_t ref; domid_t dom; /* OUT parameters. */ int16_t status; /* => enum grant_status */ grant_handle_t handle; /* IN/OUT parameters */ uint64_t dev_bus_addr; };
Quoted 6 lines
The grant map operation would then behave similarly to the
IOMMUOP_map_pagesubop for the creation of the IOMMU mapping.The grant unmap operation would then behave similarly to the
IOMMUOP_unmap_page
subop for the removal of the IOMMU mapping.
We're talking about mappings of foreign pages here - aren't these the wrong IOMMUOPs then? And if so, where would the ioserver id come from?
Jan
Quoted 30 lines · Jan Beulich
Quoted 7 lines
Quoted 1 line
Quoted 1 line · On 12.06.15 at 18:43, <[email protected]> wrote:
IOMMUOP_query_caps
------------------This subop queries the runtime capabilities of the PV-IOMMU interface for the
specific called domain. This subop usesstruct pv_iommu_opdirectly."calling domain" perhaps?
Quoted 11 lines
----------------------------------------------------------------------------
--
Field Purpose
-----
---------------------------------------------------------------flags[out] This field details the IOMMUOP capabilities.
status[out] Status of this op, op specific values listed below
----------------------------------------------------------------------------
--Quoted 8 lines
Defined bits for flags field:
----------------------------------------------------------------------------
--
Name Bit Definition
---- ------ ----------------------------------IOMMU_QUERY_map_cap0IOMMUOP_map_pageorIOMMUOP_map_foreign
can be used for this domain
Quoted 5 lines
"this" (see also above) perhaps being the calling domain? In which case I wonder how the "for" and
IOMMUOP_map_foreignare meant to fit together: I assume the flag to indicate that mapping into the (calling) domain is possible. Which then makes me wonder - what use if the new hypercall when this flag isn't set?
"This" is the calling domain. The IOMMU_lookup_foreign should continue to work if this flag is not set.
Quoted 5 lines
Quoted 3 lines
IOMMU_QUERY_map_all_gfns1IOMMUOP_map_pagesubop can map any MFN
not used by Xen"gfns" or "MFN"?
gfns . This is meant to apply to the hardware domain only, it's to allow the same access control as dom0-relaxed mode allows currently.
Quoted 14 lines
Review
Quoted 10 lines
Defined values for
map_pagesubop status field:Value Reason
------
----------------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-3-EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM Domain is not a domain and GFN does not belong to domain"is not a hardware domain"? Also, I think we're pretty determined for there to ever only be one, so perhaps it should be "the hardware domain" here and elsewhere.
That is a typo. It should say "is not the hardware domain"
I will correct the other occurrences of "a hardware domain" to "the hardware domain".
Quoted 13 lines
Quoted 5 lines
IOMMUOP_unmap_page
------------------
This subop usesstruct unmap_pagepart of thestruct pv_iommu_op.Quoted 7 lines
The subop usage of the "struct
pv_iommu_op" and ``struct unmap_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
Quoted 5 lines
Has it been determined that unmapping based on GFN is never going to be needed, and that unmapping by BFN is the more practical solution? The map counterpart doesn't seem to exclude establishing multiple mappings for the same BFN, and hence the inverse here would become kind of fuzzy in that case.
There will be only one BFN to MFN mapping per domain, the map hypercall will fail any attempt to have map a BFN to more than one GFN. This is why the unmap is based on the BFN. It is allowed to have multiple BFN mappings of the same GFN however.
Quoted 10 lines
Quoted 6 lines
IOMMUOP_map_foreign_page
----------------
This subop usesstruct map_foreign_pagepart of thestruct pv_iommu_op.It is not valid to use domid representing the calling domain.
And what's the point of that? Was it considered to have only one map/unmap pair, capable of mapping both local and foreign pages? If so, what speaks against that?
It was considered to have one map/unmap pair. The foreign map operation is the more complex of the two types of mappings and so I thought it would make for a cleaner API to have a separate subops for each type of mapping. The handling of M2B in particular is what may make the internal implementation complex.
Quoted 6 lines
Quoted 2 lines
The M2B mechanism is a MFN to (BFN,domid,ioserver) tuple.
I didn't see anything explaining the significance of this (namely the ioserver part, I think I can see the need for the domid), can you explain the backgound here please?
The ioserver part of the tuple is mainly for supporting a notification mechanism when a guest balloons out a GFN. The
Quoted 4 lines
Quoted 2 lines
Every new M2B entry will take a reference to the MFN backing the GFN.
What happens when that
GFN->MFNmapping changes?
The IOREQ server will be notified so that is can ensure any mediated device is not using the now invalid BFN mapping. Once all BFN mappings are removed by affected IOREQ servers (decrementing reference count each time) then the MFN will be released back to Xen.
Quoted 5 lines
Quoted 5 lines
All the following conditions are required to be true for PV IOMMU
map_foreign
subop to succeed:
Quoted 8 lines
Quoted 4 lines
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is ahardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthroughIs there a way for the hardware domain to know it is running in pass-through mode? Also, "the domain" is ambiguous here; I'm sure you mean the invoking domain, not the one owning the page.
The IOMMU_QUERY_map_cap flag will not be set. I will change "the domain" to "the calling domain".
Quoted 9 lines
Quoted 9 lines
This subop usage of the "struct
pv_iommu_op" and ``struct map_foreign_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain ID for which the gfn field applies
Quoted 7 lines
Quoted 3 lines
ioserver[in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn addressIn the description above you speak of returning data in this field. Is [in] really correct?
The description is wrong, the map_foreign_page will allow the bfn to be specified by the calling domain.
Quoted 10 lines
Quoted 8 lines
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safelyswapped to scratch page
Scratch page? This needs some explanation.
Hopefully this is explained further down. It is expected that certain BFN mappings are mapping data for the mediated device and not control information. By allowing a scratch page to be used Xen can immediately swap the BFN mappings and remove the references to the affected MFN and free the MFN back to Xen. This avoids the delay for the ioreq servers to unmap the BFNs explicitly and hopefully speeds up ballooning.
Quoted 6 lines
Quoted 4 lines
Reserved for future use 3-9 Reserved flag bits should be 0
IOMMU_page_order10-15 Returns maximum possible page order for
all other IOMMUOP subops"Returns"? "other"?
Copy paste error, this should be the input page order not the returned page order.
Quoted 6 lines
Quoted 6 lines
IOMMU_lookup_foreign_page
----------------
This subop usesstruct lookup_foreign_pagepart of thestruct pv_iommu_op.
Quoted 7 lines
Quoted 3 lines
If the BFN is specified as an input and parameter and there is no IOMMU support
for the calling domain then an error will be returned."input and parameter"? The field is marked [out] below, and I also can't see a flag allowing this to be optional (the more that flags is also [out]).
Another mistake by me. This is old text from a draft where map and lookup were the same subop.
The BFN is always an output parameter for the lookup_foreign_page subop.
Quoted 4 lines
Quoted 2 lines
It is the calling domain responsibility to ensure there are no conflicts
No races you mean?
Race's are also the calling domain responsibility but I'm referring to BFN address space conflicts here.
Quoted 10 lines
Quoted 6 lines
Each successful subop will add to the M2B if there was not an existing identical
M2B entry.Every new M2B entry will take a reference to the MFN backing the GFN.
This is a lookup - why would it add something somewhere? Perhaps this is just a copy-and-paste mistake? Or is the use of "lookup" here misleading (as the last section of the document seems to suggest)?
I can see how the term "lookup" is misleading. This subop really does a lookup and take reference. Can you suggest an alternative name? I'm wary of allowing
Quoted 11 lines
Quoted 10 lines
Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------IOMMUOP_readable0 Returned BFN IOMMU mapping is
readableIOMMUOP_writeable1 Returned BFN IOMMU mapping is
writeable
Reserved for future use 2-9 Reserved flag bits should be 0
Quoted 1 line
Reserved flags will be returned as 0.
Agreed, I will make the change.
Quoted 13 lines
Review
Quoted 8 lines
Defined values for
lookup_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-2-EPERM Calling domain does not have sufficient privilege over domid -ENOENT There is no available BFN for provided GFN + domid combinationThroughout here there seems to be a mixture of references to (GFN, domid) pairs anmd (GFN,domid,ioserver) triplets. Along with there being some explanation missing, these need to be made consistent to avoid confusion.
I'll adjust them to all be (GFN,domid,ioserver) triples.
A particular BFN will map to a (GFN, domid) set so a (GFN,domid,ioserver) triple can be determined via a (BFN, ioserver).
Quoted 11 lines
Quoted 9 lines
IOMMUOP_unmap_foreign_page
----------------
This subop usesstruct unmap_foreign_pagepart of thestruct pv_iommu_op.If there is no IOMMU support then the MFN is returned in the BFN field (that is
the only valid bus address for the GFN + domid combination).Copy-and-paste mistake again (doesn't seem to apply to unmap)?
Yes, it's a copy paste mistake.
Quoted 5 lines
Quoted 5 lines
If there is IOMMU support then the specified BFN is returned for the GFN + domid
combination
Quoted 7 lines
Quoted 5 lines
Each successful subop will add to the M2B if there was not an existing identical
M2B entry. TheEvery new M2B entry will take a reference to the MFN backing the GFN.
And again?
Yes, it's a copy paste mistake.
Quoted 10 lines
Quoted 10 lines
This subop usage of the "struct
pv_iommu_op" and ``struct unmap_foreign_page` fields
are detailed below:-----------------------------------------------------------------------
Field Purpose
----- --------------------------------------------------------ioserver[in] IOREQ server id associated with mapping
Quoted 5 lines
Quoted 3 lines
bfn[in] Bus address frame number for gfn address
flags[out] Flags for signalling page order of unmap operation[out]?
Yes, it's a copy paste mistake. It should be [in]
Quoted 8 lines
Quoted 5 lines
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned-ENOENTThere is no mapped BFN + ioserver id combination to unmapYet another pair, the unique mapping properties of which don't follow from anything said earlier.
A particular BFN will map to a (GFN, domid) set so a (GFN,domid,ioserver) triple will be determined via a (BFN, ioserver) pair.
The ioserver may have lost track of the (GFN, domid) pair used for a particular BFN mapping but may have just been notified of it's BFN mapping becoming invalid. So it's more efficient to unmap based on BFN and ioserver ID.
Quoted 8 lines
Quoted 8 lines
PV IOMMU interactions with self ballooning
==========================================The guest should clear any IOMMU mappings it has of it's own pages before releasing a page back to Xen. It will need to add IOMMU mappings after repopulating a page with the
populate_physmaphypercall.
Quoted 6 lines
Quoted 3 lines
This requires that IOMMU mappings get a writeable page type reference count and
that guests clear any IOMMU mappings before pinning page table pages.I suppose this is only for aware PV guests. If so, perhaps this should be made explicit.
This is only for PV guests. I will make the correction.
Quoted 42 lines
Quoted 42 lines
PV IOMMU interactions with grant map/unmap operations
=====================================================Grant map operations return a Physical device accessible address (BFN) if the
GNTMAP_device_mapflag is set. This operation currently returns the MFN for PV
guests which may conflict with the BFN address space the guest uses if PV IOMMU
map support is available to the guest.This design proposes to allow the calling domain to control the BFN address that
a grant map operation uses.This can be achieved by specifying that the
dev_bus_addrin thegnttab_map_grant_refstructure is used an input parameter instead of the output parameter it is currently.Only
PAGE_SIZEaligned addresses are allowed fordev_bus_addrinput parameter.The revised structure is shown below for convenience.
struct gnttab_map_grant_ref { /* IN parameters. */ uint64_t host_addr; uint32_t flags; /* GNTMAP_* */ grant_ref_t ref; domid_t dom; /* OUT parameters. */ int16_t status; /* => enum grant_status */ grant_handle_t handle; /* IN/OUT parameters */ uint64_t dev_bus_addr; };The grant map operation would then behave similarly to the
IOMMUOP_map_pagesubop for the creation of the IOMMU mapping.
Quoted 8 lines
Quoted 3 lines
The grant unmap operation would then behave similarly to the
IOMMUOP_unmap_page
subop for the removal of the IOMMU mapping.We're talking about mappings of foreign pages here - aren't these the wrong IOMMUOPs then? And if so, where would the ioserver id come from?
I don't expected grant mapped pages to be ballooned out or to be directly used by ioservers so I believe the grant mapped pages match more closely to the standard map_page than the foreign_map_page.
Generally I think I need to rework the document to introduce the some concepts before the actual interface itself.
Thank you for the comments and sorry for the document having quite a few typos and copy paste errors. It's been through quite a few internal updates.
I think I should add a section to explain that there are two type's of BFN address space users.
The first type (map_page) is more simple and just allows the calling domain to control the BFN mappings for GFNs it owns and for any grant mapped pages of other domains. There is no need to deal with other domains ballooning their pages will this type of mapping.
The second type (map_foreign_page) is more complex and allows the calling domain to control the BFN mappings for GFNs of other domains that the calling domain has privilege over. This type of mapping is designed for mediated passthrough ioservers. It has to deal with other domains ballooning their pages.
The interface is designed so that you cannot "remap" an existing BFN mapping. This allows the different types of BFN mappings to be tracked separately by Xen. There is no need for the M2B for the first type of mapping.
Thanks for the comments. I can see how busy it is on the list and I wasn't expecting a response this quickly :)
Malcolm
Quoted 2 lines
Jan
Quoted 10 lines
Quoted 1 line
Quoted 1 line · On 16.06.15 at 16:47, <[email protected]> wrote:
Quoted 5 lines · Jan Beulich
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 12.06.15 at 18:43, <[email protected]> wrote:
IOMMU_QUERY_map_all_gfns1IOMMUOP_map_pagesubop can map any MFN
not used by Xen"gfns" or "MFN"?
gfns . This is meant to apply to the hardware domain only, it's to allow the same access control as dom0-relaxed mode allows currently.
But why "gfns" in the name and "any MFN" in the description?
Quoted 18 lines
Quoted 12 lines
Quoted 11 lines
IOMMUOP_unmap_page
------------------
This subop usesstruct unmap_pagepart of thestruct pv_iommu_op.The subop usage of the "struct
pv_iommu_op" and ``struct unmap_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELFQuoted 5 lines
Has it been determined that unmapping based on GFN is never going to be needed, and that unmapping by BFN is the more practical solution? The map counterpart doesn't seem to exclude establishing multiple mappings for the same BFN, and hence the inverse here would become kind of fuzzy in that case.
Quoted 5 lines
There will be only one BFN to MFN mapping per domain, the map hypercall will fail any attempt to have map a BFN to more than one GFN. This is why the unmap
is based on the BFN. It is allowed to have multiple BFN mappings of the same GFN however.
Okay, I got confused again by the term BFN - I keep mixing up the parts of the bus between device and IOMMU vs between IOMMU and RAM. Alternatives I could think of (DFN for Device Frame Number) wouldn't be any better, so I guess we need to live with the ambiguity.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
Each successful subop will add to the M2B if there was not an existing identical
M2B entry.
Quoted 8 lines
Quoted 5 lines
Quoted 1 line
Every new M2B entry will take a reference to the MFN backing the GFN.
This is a lookup - why would it add something somewhere? Perhaps this is just a copy-and-paste mistake? Or is the use of "lookup" here misleading (as the last section of the document seems to suggest)?
I can see how the term "lookup" is misleading. This subop really does a lookup and take reference. Can you suggest an alternative name?
get_foreign_page_map? In any event, in particular with possibly ambiguous name the description should be particularly clear and obvious to help the reader (which also applies to the code to be written).
Quoted 1 line
I'm wary of allowing
???
Quoted 7 lines
Quoted 7 lines
Quoted 7 lines
PV IOMMU interactions with self ballooning
==========================================The guest should clear any IOMMU mappings it has of it's own pages before releasing a page back to Xen. It will need to add IOMMU mappings after repopulating a page with the
populate_physmaphypercall.
Quoted 8 lines
Quoted 6 lines
Quoted 3 lines
This requires that IOMMU mappings get a writeable page type reference count and
that guests clear any IOMMU mappings before pinning page table pages.I suppose this is only for aware PV guests. If so, perhaps this should be made explicit.
This is only for PV guests. I will make the correction.
The emphasis was on "aware", not on "PV" (which is already stated).
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines
The grant map operation would then behave similarly to the
IOMMUOP_map_pagesubop for the creation of the IOMMU mapping.The grant unmap operation would then behave similarly to the
IOMMUOP_unmap_page
subop for the removal of the IOMMU mapping.We're talking about mappings of foreign pages here - aren't these the wrong IOMMUOPs then? And if so, where would the ioserver id come from?
Quoted 5 lines
I don't expected grant mapped pages to be ballooned out or to be directly used by
ioservers so I believe the grant mapped pages match more closely to the standardmap_pagethan theforeign_map_page.
Right, that became clear with you saying that the ioserver id is meant to be used for balloon out notifications only. But that should be made explicit.
Quoted 3 lines
Generally I think I need to rework the document to introduce the some concepts
before the actual interface itself.
Yes, that would be very helpful. The interface spec should probably the (almost) last thing.
Jan
Hi Malcolm,
Thank you very much for accommodate our XenGT requirement in your
design. Following are some XenGT related questions. :)
Quoted 402 lines · Malcolm Crossley
Hi All,
Here is a design for allowing guests to control the IOMMU. This allows for the guest GFN mapping to be programmed into the IOMMU and avoid using the SWIOTLB bounce buffer technique in the Linux kernel (except for legacy 32 bit DMA IO devices).
Draft B has been expanded to include Bus Address mapping/lookup for Mediated pass-through emulators.
The pandoc markdown format of the document is provided below to allow for easier inline comments:
% Xen PV IOMMU interface
% Malcolm Crossley <<[email protected]>>
Paul Durrant <<[email protected]>>
% Draft BIntroduction
============Revision History
------------------------------------------------------------------------------------
Version Date Changes ------- ----------- ---------------------------------------------- Draft A 10 Apr 2014 Initial draft.Draft B 12 Jun 2015 Second draft.
--------------------------------------------------------------------Background
==========Linux kernel SWIOTLB
--------------------Xen PV guests use a Pseudophysical Frame Number(PFN) address space which is decoupled from the host Machine Frame Number(MFN) address space.
PV guest hardware drivers are only aware of the PFN address space only and assume that if PFN addresses are contiguous then the hardware addresses would be contiguous as well. The decoupling between PFN and MFN address spaces means PFN and MFN addresses may not be contiguous across page boundaries and thus a buffer allocated in GFN address space which spans a page boundary may not be contiguous in MFN address space.
PV hardware drivers cannot tolerate this behaviour and so a special "bounce buffer" region is used to hide this issue from the drivers.
A bounce buffer region is a special part of the PFN address space which has been made to be contiguous in both PFN and MFN address spaces. When a driver requests a buffer which spans a page boundary be made available for hardware to read the core operating system code copies the buffer into a temporarily reserved part of the bounce buffer region and then returns the MFN address of the reserved part of the bounce buffer region back to the driver itself. The driver then instructs the hardware to read the copy of the buffer in the bounce buffer. Similarly if the driver requests a buffer is made available for hardware to write to the first a region of the bounce buffer is reserved and then after the hardware completes writing then the reserved region of bounce buffer is copied to the originally allocated buffer.
The overheard of memory copies to/from the bounce buffer region is high and damages performance. Furthermore, there is a risk the fixed size bounce buffer region will become exhausted and it will not be possible to return an hardware address back to the driver. The Linux kernel drivers do not tolerate this failure and so the kernel is forced to crash, as an uncorrectable error has occurred.
Input/Output Memory Management Units (IOMMU) allow for an inbound address mapping to be created from the I/O Bus address space (typically PCI) to the machine frame number address space. IOMMU's typically use a page table mechanism to manage the mappings and therefore can create mappings of page size granularity or larger.
The I/O Bus address space will be referred to as the Bus Frame Number (BFN) address space for the rest of this document.
Mediated Pass-through Emulators
-------------------------------Mediated Pass-through emulators allow guest domains to interact with hardware devices via emulator mediation. The emulator runs in a domain separate to the guest domain and it is used to enforce security of guest access to the hardware devices and isolation of different guests accessing the same hardware device.
The emulator requires a mechanism to map guest address's to a bus address that the hardware devices can access.
Clarification of GFN and BFN fields for different guest types
-------------------------------------------------------------
Guest Frame Numbers (GFN) definition varies depending on the guest type.Diagram below details the memory accesses originating from CPU, per guest type:
HVM guest PV guest
(VA) (VA)
| |
MMU MMU
| |
(GFN) |
| | (GFN)
HAP a.k.a EPT/NPT |
| |
(MFN) (MFN)
| |
RAM RAMFor PV guests GFN is equal to MFN for a single page but not for a contiguous range of pages.
Bus Frame Numbers (BFN) refer to the address presented on the physical bus before being translated by the IOMMU.
Diagram below details memory accesses originating from physical device.
Physical Device | (BFN) | IOMMU-PT | (MFN) | RAMPurpose
=======1. Allow Xen guests to create/modify/destroy IOMMU mappings for hardware devices that the PV guests has access to. This enables the PV guest to program a bus address space mapping which matches it's GFN mapping. Once a 1-1 mapping of PFN to bus address space is created then a bounce buffer region is not required for the IO devices connected to the IOMMU.
2. Allow for Xen guests to lookup/create/modify/destroy IOMMU mappings for guest memory of domains the calling Xen guest has sufficient privilege over. This enables domains to provide mediated hardware acceleration to other guest domains.
Xen Architecture
================The Xen architecture consists of a new hypercall interface and changes to the grant map interface.
The existing IOMMU mappings setup at domain creation time will be preserved so that PV domains unaware of this feature will continue to function with no changes required.
Memory ballooning will be supported by taking an additional reference on the MFN backing the GFN for each successful IOMMU mapping created.
An M2B tracking structure will be used to ensure all reference's to a MFN can be located easily.
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_tdo_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)First argument, guest handle pointer to array of
struct pv_iommu_opSecond argument, unsigned integer count ofstruct pv_iommu_opelements in array.Definition of struct
pv_iommu_op:struct pv_iommu_op { uint16_t subop_id; uint16_t flags; int32_t status; union { struct { uint64_t bfn; uint64_t gfn; } map_page; struct { uint64_t bfn; } unmap_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver; } map_foreign_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver; } lookup_foreign_page; struct { uint64_t bfn; ioservid_t ioserver; } unmap_foreign_page; } u; };Definition of PV IOMMU subops:
#define IOMMUOP_query_caps 1 #define IOMMUOP_map_page 2 #define IOMMUOP_unmap_page 3 #define IOMMUOP_map_foreign_page 4 #define IOMMUOP_lookup_foreign_page 5 #define IOMMUOP_unmap_foreign_page 6Design considerations for hypercall op
-------------------------------------------
IOMMU map/unmap operations can be slow and can involve flushing the IOMMU TLB to ensure the IO device uses the updated mappings.The op has been designed to take an array of operations and a count as parameters. This allows for easily implemented hypercall continuations to be used and allows for batches of IOMMU operations to be submitted before flushing the IOMMU TLB.
The
subop_idto be used for a particular element is encoded into the element itself. This allows for map and unmap operations to be performed in one hypercall and for the IOMMU TLB flushing optimisations to be still applied.The hypercall will ensure that the required IOMMU TLB flushes are applied before returning to guest via either hypercall completion or a hypercall continuation.
IOMMUOP_query_caps
------------------This subop queries the runtime capabilities of the PV-IOMMU interface for the specific called domain. This subop uses
struct pv_iommu_opdirectly.------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `flags` [out] This field details the IOMMUOP capabilities.
status[out] Status of this op, op specific values listed below
------------------------------------------------------------------------------Defined bits for flags field:
------------------------------------------------------------------------------
Name Bit Definition
---- ------ ----------------------------------IOMMU_QUERY_map_cap0IOMMUOP_map_pageorIOMMUOP_map_foreign
can be used for this domain
IOMMU_QUERY_map_all_gfns1IOMMUOP_map_pagesubop can map any MFN
not used by XenReserved for future use 2-9 n/a
IOMMU_page_order10-15 Returns maximum possible page order for
all other IOMMUOP subops
------------------------------------------------------------------------------Defined values for
query_capssubop status field:Value Reason ------ ---------------------------------------------------------- 0 subop successfully returned
IOMMUOP_map_page
----------------------
This subop usesstruct map_pagepart of thestruct pv_iommu_op.If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFN's except for Xen owned MFN's else the hardware domain will only be allowed to map GFN's which it owns.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFN's without taking a reference to the MFN backing the GFN by setting the
IOMMU_MAP_OP_no_ref_cntflag.Every successful
pv_iommu_opwill result in an additional page reference being taken on the MFN backing the GFN except for the condition detailed above.If the
map_opflags indicate a writeable mapping is required then a writeable page type reference will be taken otherwise a standard page reference will be taken.All the following conditions are required to be true for PV IOMMU map subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. Ifhardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthroughThis subop usage of the "struct
pv_iommu_op" and ``struct map_page` fields are detailed below:------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `bfn` [in] Bus address frame number(BFN) to be mapped to specified gfnbelow
gfn[in] Guest address frame number forDOMID_SELF
flags[in] Flags for signalling type of IOMMU mapping to be created,
Flags can be combined.
status[out] Mapping status of this op, op specific values listed below
------------------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMU_OP_readable 0 Create readable IOMMU mapping IOMMU_OP_writeable 1 Create writeable IOMMU mapping IOMMU_MAP_OP_no_ref_cnt 2 IOMMU mapping does not take a reference toMFN backing BFN mapping
Reserved for future use 3-9 n/aIOMMU_page_order10-15 Page order to be used for both gfn and bfnDefined values for
map_pagesubop status field:Value Reason ------ ---------------------------------------------------------------------- 0 subop successfully returned(quoted diff) +0
-6-EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM Domain is not a domain and GFN does not belong to domain -EPERM Domain is a hardware domain, IOMMU dom-strict mode is enabled and GFN does not belong to domain -EACCES BFN address conflicts with RMRR regions for device's attached to DOMID_SELF -ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMUOP_unmap_page
------------------
This subop usesstruct unmap_pagepart of thestruct pv_iommu_op.The subop usage of the "struct
pv_iommu_op" and ``struct unmap_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
flags[in] Flags for signalling page order of unmap operation
status[out] Mapping status of this unmap operation, 0 indicates success
--------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------
Reserved for future use 0-9 n/aIOMMU_page_order10-15 Page order to be used for bfnDefined values for
unmap_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-3-EIO IOMMU unit returned error when attempting to unmap BFN. -ENOSPC Page order is too large for either BFN address or IOMMU unit ------------------------------------------------------------------------
IOMMUOP_map_foreign_page
----------------
This subop usesstruct map_foreign_pagepart of thestruct pv_iommu_op.It is not valid to use domid representing the calling domain.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
If there IOMMU support then the specified BFN is returned for the GFN + domid combination
The M2B mechanism is a MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
Quoted 7 lines
All the following conditions are required to be true for PV IOMMU
map_foreignsubop to succeed:1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is ahardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthrough
What if the IOMMU is enabled, and runs in the default mode, which 1:1 maps all memories except owned by Xen?
Quoted 56 lines
This subop usage of the "struct
pv_iommu_op" and ``struct map_foreign_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain ID for which the gfn field applies
ioserver[in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn address
gfn[in] Guest address frame number
flags[in] Details the status of the BFN mapping
status[out] status of this subop, 0 indicates success
--------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safelyswapped to scratch page
Reserved for future use 3-9 Reserved flag bits should be 0IOMMU_page_order10-15 Returns maximum possible page order for
all other IOMMUOP subopsDefined values for
map_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-9-EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM Calling domain does not have sufficient privilege over domid -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM domid maps to DOMID_SELF -EACCES BFN address conflicts with RMRR regions for device's attached to DOMID_SELF -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid -ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMU_lookup_foreign_page
----------------
This subop usesstruct lookup_foreign_pagepart of thestruct pv_iommu_op.If the BFN is specified as an input and parameter and there is no IOMMU support for the calling domain then an error will be returned.
Quoted 7 lines
It is the calling domain responsibility to ensure there are no conflicts
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
Similarly, what if the IOMMU is enabled, and runs in the default mode, which 1:1 maps all memories except owned by Xen? Will a MFN be returned? Or should we take the query/map ops instead of the lookup op for this situation?
Quoted 110 lines
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
This subop usage of the "struct
pv_iommu_op" and ``struct lookup_foreign_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain ID for which the gfn field applies
ioserver[in] IOREQ server id associated with mapping
bfn[out] Bus address frame number for gfn address
gfn[in] Guest address frame number
flags[out] Details the status of the BFN mapping
status[out] status of this subop, 0 indicates success
--------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 Returned BFN IOMMU mapping is readable IOMMUOP_writeable 1 Returned BFN IOMMU mapping is writeable Reserved for future use 2-9 Reserved flag bits should be 0 IOMMU_page_order 10-15 Returns maximum possible page order forall other IOMMUOP subops
Defined values for
lookup_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-5-EPERM Calling domain does not have sufficient privilege over domid -ENOENT There is no available BFN for provided GFN + domid combination -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid
IOMMUOP_unmap_foreign_page
----------------
This subop usesstruct unmap_foreign_pagepart of thestruct pv_iommu_op.If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
If there is IOMMU support then the specified BFN is returned for the GFN + domid combination
Each successful subop will add to the M2B if there was not an existing identical M2B entry. The
Every new M2B entry will take a reference to the MFN backing the GFN.
This subop usage of the "struct
pv_iommu_op" and ``struct unmap_foreign_page` fields are detailed below:-----------------------------------------------------------------------
Field Purpose
----- --------------------------------------------------------ioserver[in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn address
flags[out] Flags for signalling page order of unmap operation
status[out] status of this subop, 0 indicates success
-----------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition ---- ----- ---------------------------------- Reserved for future use 0-9 n/a IOMMU_page_order 10-15 Page order to be used for bfnDefined values for
unmap_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned-ENOENTThere is no mapped BFN + ioserver id combination to unmapIOMMUOP_*_foreign_page interactions with guest domain ballooning
================================================================Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered IO request of type
IOREQ_TYPE_INVALIDATEto the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered IO request ring is full then a standard (synchronous) IO request of typeIOREQ_TYPE_INVALIDATEwill be issued to the affected IOREQ server the with just invalidated BFN address in the data field.The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
Quoted 4 lines
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a
IOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.
I do not quite understand this. With no IOMMU support, these BFNs are supplied by hypervisor. So why not let hypervisor do this unmap and notify the calling domain?
Quoted 118 lines
For hosts with IOMMU support:
If the BFN was mapped without theIOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagethen the affected affected emulator(s) must specifically issue aIOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed.If the BFN was mapped with the
IOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagesubop for all emulators with mappings of that GFN then the BFN mapping will be swapped to point at a scratch MFN page and all BFN references to the invalid MFN will be removed by Xen after the BFN mapping has been updated to point at the scratch MFN page.The rationale for swapping the BFN mapping to point at scratch pages is to enable guest domains to balloon quickly without requiring hypercall(s) from emulators.
Not all BFN mappings can be swapped without potentially causing problems for the hardware itself (command rings etc.) so the
IOMMUOP_swap_mfnflag is used to allow per BFN control of Xen ballooning behaviour.PV IOMMU interactions with self ballooning
==========================================The guest should clear any IOMMU mappings it has of it's own pages before releasing a page back to Xen. It will need to add IOMMU mappings after repopulating a page with the
populate_physmaphypercall.This requires that IOMMU mappings get a writeable page type reference count and that guests clear any IOMMU mappings before pinning page table pages.
Security Implications of allowing domain IOMMU control
===============================================================Xen currently allows IO devices attached to hardware domain to have direct access to the all of the MFN address space (except Xen hypervisor memory regions), provided the Xen IOMMU option dom0-strict is not enabled.
The PV IOMMU feature provides the same level of access to MFN address space and the feature is not enabled when the Xen IOMMU option dom0-strict is enabled. Therefore security is not degraded by the PV IOMMU feature.
Domains with physical device(s) assigned which are not hardware domains are only allowed to map their own GFNs or GFNs for domain(s) they have privilege over.
PV IOMMU interactions with grant map/unmap operations
=====================================================Grant map operations return a Physical device accessible address (BFN) if the
GNTMAP_device_mapflag is set. This operation currently returns the MFN for PV guests which may conflict with the BFN address space the guest uses if PV IOMMU map support is available to the guest.This design proposes to allow the calling domain to control the BFN address that a grant map operation uses.
This can be achieved by specifying that the
dev_bus_addrin thegnttab_map_grant_refstructure is used an input parameter instead of the output parameter it is currently.Only
PAGE_SIZEaligned addresses are allowed fordev_bus_addrinput parameter.The revised structure is shown below for convenience.
struct gnttab_map_grant_ref { /* IN parameters. */ uint64_t host_addr; uint32_t flags; /* GNTMAP_* */ grant_ref_t ref; domid_t dom; /* OUT parameters. */ int16_t status; /* => enum grant_status */ grant_handle_t handle; /* IN/OUT parameters */ uint64_t dev_bus_addr; };The grant map operation would then behave similarly to the
IOMMUOP_map_pagesubop for the creation of the IOMMU mapping.The grant unmap operation would then behave similarly to the
IOMMUOP_unmap_pagesubop for the removal of the IOMMU mapping.A new grantmap flag would be used to indicate the domain is requesting the
dev_bus_addrfield is used an input parameter.#define _GNTMAP_request_bfn_map (6) #define GNTMAP_request_bfn_map (1<<_GNTMAP_request_bfn_map)Linux kernel architecture
=========================The Linux kernel will use the PV-IOMMU hypercalls to map it's PFN address space into the IOMMU. It will map the PFN's to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
The native SWIOTLB will be used to handle device's which cannot DMA to all of the kernel's PFN address space.
An interface shall be provided for emulator usage of IOMMUOP_*_foreign_page subops which will allow the Linux kernel to centrally manage that domains BFN resource and ensure there are no unexpected conflicts.
Emulator usage of PV IOMMU interface
====================================Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
Quoted 8 lines
A
IOMMUOP_query_capssubop will return theIOMMU_QUERY_map_capflag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via theIOMMUOP_map_foreign_pagesubop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via theIOMMUOP_lookup_foreign_page.Operating systems which use the
IOMMUOP_map_pagesubop are expected to provide a common interface for emulators
According to our previous internal discussions, my understanding about the usage is this:
1> PV IOMMU has an interface in dom0's kernel to do the query/map/lookup all at once, which also includes the BFN allocation algorithm. 2> When XenGT emulator tries to construct a shadow PTE, we can just call your interface, which returns a BFN whatever.
However, the above description seems the XenGT device model need to do the query/lookup/map by itself?
Besides, could you please give a more detailed information about this 'common interface'? :)
Thanks
Yu
Quoted 16 lines
Emulators should unmap unused GFN mappings as often as possible using
IOMMUOP_unmap_foreign_pagesubops so that guest domains can balloon pages quickly and efficiently.Emulators should conform to the ballooning behaviour described section "IOMMUOP_*_foreign_page interactions with guest domain ballooning" so that guest domains are able to effectively balloon out and in memory.
Emulators must unmap any active BFN mappings when they shutdown.
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Quoted 3 lines
Quoted 1 line
Quoted 1 line · On 17.06.15 at 14:48, <[email protected]> wrote:
Thank you very much for accommodate our XenGT requirement in your
design. Following are some XenGT related questions. :)
Please trim your replies.
Jan
Quoted 7 lines · Zhang
Hi Malcolm,
Thank you very much for accommodate our XenGT requirement in your
design. Following are some XenGT related questions. :)Quoted 1 line · Malcolm Crossley
Hi All,
<snip>
Quoted 26 lines
Quoted 26 lines
IOMMUOP_map_foreign_page
----------------
This subop usesstruct map_foreign_pagepart of thestruct pv_iommu_op.It is not valid to use domid representing the calling domain.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
If there IOMMU support then the specified BFN is returned for the GFN + domid combination
The M2B mechanism is a MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
All the following conditions are required to be true for PV IOMMU
map_foreignsubop to succeed:
Quoted 6 lines
Quoted 4 lines
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is ahardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthroughWhat if the IOMMU is enabled, and runs in the default mode, which 1:1 maps all memories except owned by Xen?
Good question. A PV IOMMU aware guest will know the 1:1 map exists and can use the IOMMUOP_unmap_page to remove any mappings which will conflict with it's planned BFN mappings.
For a PV IOMMU unaware guest I think the IOMMUOP_lookup_foreign_page should be used instead. This will allow the IOSERVER to register interest in the Domid + GFN it's using and allow ballooning to be used.
FYI, The 1:1 map on PV guests will be setup without taking a reference to the MFN otherwise unaware PV guests will be unable to create page tables.
Quoted 61 lines
Quoted 61 lines
This subop usage of the "struct
pv_iommu_op" and ``struct map_foreign_page` fields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain ID for which the gfn field applies
ioserver[in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn address
gfn[in] Guest address frame number
flags[in] Details the status of the BFN mapping
status[out] status of this subop, 0 indicates success
--------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safelyswapped to scratch page
Reserved for future use 3-9 Reserved flag bits should be 0IOMMU_page_order10-15 Returns maximum possible page order for
all other IOMMUOP subopsDefined values for
map_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-9-EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM Calling domain does not have sufficient privilege over domid -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM domid maps to DOMID_SELF -EACCES BFN address conflicts with RMRR regions for device's attached to DOMID_SELF -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid -ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMU_lookup_foreign_page
----------------
This subop usesstruct lookup_foreign_pagepart of thestruct pv_iommu_op.If the BFN is specified as an input and parameter and there is no IOMMU support for the calling domain then an error will be returned.
It is the calling domain responsibility to ensure there are no conflicts
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid
Quoted 6 lines
Quoted 2 lines
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
Similarly, what if the IOMMU is enabled, and runs in the default mode, which 1:1 maps all memories except owned by Xen? Will a MFN be returned? Or should we take the query/map ops instead of the lookup op for this situation?
The lookup will return the BFN which is 1:1 mapped to the MFN.
Only the hardware domain will have precreated BFN mappings of other domains memory.
So the logic could look like this:
If dom0 then lookup use P2M to get MFN then use M2B to lookup BFN if this fails then check if BFN is mapped to MFN 1:1, if so
return BFN else return -ENOENT.
Quoted 6 lines
Quoted 6 lines
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
<snip>
Quoted 19 lines
Quoted 19 lines
IOMMUOP_*_foreign_page interactions with guest domain ballooning
================================================================Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered IO request of type
IOREQ_TYPE_INVALIDATEto the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered IO request ring is full then a standard (synchronous) IO request of typeIOREQ_TYPE_INVALIDATEwill be issued to the affected IOREQ server the with just invalidated BFN address in the data field.The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
Quoted 7 lines
Quoted 4 lines
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a
IOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.I do not quite understand this. With no IOMMU support, these BFNs are supplied by hypervisor. So why not let hypervisor do this unmap and notify the calling domain?
We need the emulators to do the unmap so that they can ensure that hardware is not actively using the BFN (same as MFN in this case) otherwise Xen may allocate that MFN to another guest and that guest will have it's memory corrupted.
Another way to think about it is that a malicious guest could set up a long running DMA to it's RAM and then deliberately balloons out that RAM whilst the DMA is running. The only way to secure that scenario is not let the balloon out RAM to be used until the emulator confirms it's safe to do so.
The IOMMUOP_swap_mfn optimisation has been added to allow Xen to drop reference's safely. Unfortunately it requires the IOMMU to be enabled.
Quoted 1 line
Quoted 1 line
<snip>
Quoted 22 lines
Quoted 6 lines
Emulator usage of PV IOMMU interface
====================================Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
Quoted 8 lines
A
IOMMUOP_query_capssubop will return theIOMMU_QUERY_map_capflag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via theIOMMUOP_map_foreign_pagesubop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via theIOMMUOP_lookup_foreign_page.Operating systems which use the
IOMMUOP_map_pagesubop are expected to provide a common interface for emulatorsAccording to our previous internal discussions, my understanding about the usage is this:
1> PV IOMMU has an interface in dom0's kernel to do the query/map/lookup all at once, which also includes the BFN allocation algorithm. 2> When XenGT emulator tries to construct a shadow PTE, we can just call your interface, which returns a BFN whatever.
Quoted 2 lines
However, the above description seems the XenGT device model need to do the query/lookup/map by itself?
The above description is to cover emulator which may run in their own domain (stub domain).
Quoted 2 lines
Besides, could you please give a more detailed information about this 'common interface'? :)
I will try to include more details in the next draft.
My current thinking is to reuse the "struct pv_iommu_op" array of ops and just implement a common function for requesting a BFN mapping. The common function will fill in the subOp_field for the caller.
Thanks for your feedback and please trim your replies as Jan suggested. It makes it much easier to find and reply to your inline comments.
Quoted 9 lines
Thanks
YuQuoted 6 lines
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel
Xen PV IOMMU interface draft C
In reply to Malcolm CrossleyHi All,
Here is a design for allowing guests to control the IOMMU. This allows for the guest GFN mapping to be programmed into the IOMMU and avoid using the SWIOTLB bounce buffer technique in the Linux kernel (except for legacy 32 bit DMA IO devices).
Draft C has been reordered to explain expected behaviours before the APIs themselves. There's an additional section to explain the rationale for separate subops from local GFN mappings and foreign GFN mappings.
There's also further detail on the Linux API for foreign BFN mappings.
The plan is to start writing code against this version so please provide feedback on any major design problems/concerns.
The pandoc markdown format of the document is provided below to allow for easier inline comments:
% Xen PV IOMMU interface
% Malcolm Crossley <<[email protected]>>
Paul Durrant <<[email protected]>>
% Draft C
Introduction
============
Revision History
----------------
--------------------------------------------------------------------
Version Date Changes ------- ----------- ---------------------------------------------- Draft A 10 Apr 2014 Initial draft.
Draft B 12 Jun 2015 Second draft.
Draft C 26 Jun 2015 Third draft.
--------------------------------------------------------------------
Background
==========
Linux kernel SWIOTLB
--------------------
Xen PV guests use a Pseudophysical Frame Number(PFN) address space which is decoupled from the host Machine Frame Number(MFN) address space.
PV guest hardware drivers are aware of the PFN address space only and assume that if PFN addresses are contiguous then the hardware addresses would be contiguous as well. The decoupling between PFN and MFN address spaces means PFN and MFN addresses may not be contiguous across page boundaries and thus a buffer allocated in GFN address space which spans a page boundary may not be contiguous in MFN address space.
PV hardware drivers cannot tolerate this behaviour and so a special "bounce buffer" region is used to hide this issue from the drivers.
A bounce buffer region is a special part of the PFN address space which has been made to be contiguous in both PFN and MFN address spaces. When a driver requests a buffer which spans a page boundary be made available for hardware to read the core operating system code copies the buffer into a temporarily reserved part of the bounce buffer region and then returns the MFN address of the reserved part of the bounce buffer region back to the driver itself. The driver then instructs the hardware to read the copy of the buffer in the bounce buffer. Similarly if the driver requests a buffer is made available for hardware to write to the first a region of the bounce buffer is reserved and then after the hardware completes writing then the reserved region of bounce buffer is copied to the originally allocated buffer.
The overheard of memory copies to/from the bounce buffer region is high and damages performance. Furthermore, there is a risk the fixed size bounce buffer region will become exhausted and it will not be possible to return an hardware address back to the driver. The Linux kernel drivers do not tolerate this failure and so the kernel is forced to crash, as an unrecoverable error has occurred.
Input/Output Memory Management Units (IOMMU) allow for an inbound address mapping to be created from the I/O Bus address space (typically PCI) to the machine frame number address space. IOMMUs typically use a page table mechanism to manage the mappings and therefore can create mappings of page size granularity or larger.
The I/O Bus address space will be referred to as the Bus Frame Number (BFN) address space for the rest of this document.
Mediated Pass-through Emulators
-------------------------------
Mediated Pass-through emulators allow guest domains to interact with hardware devices via emulator mediation. The emulator runs in a domain separate to the guest domain and it is used to enforce security of guest access to the hardware devices and isolation of different guests accessing the same hardware device.
The emulator requires a mechanism to map guest addresses to a bus address that the hardware devices can access.
Clarification of GFN and BFN fields for different guest types
-------------------------------------------------------------
Guest Frame Numbers (GFN) definition varies depending on the guest type.
Diagram below details the memory accesses originating from CPU, per guest type:
HVM guest PV guest
(VA) (VA)
| |
MMU MMU
| |
(GFN) |
| | (GFN)
HAP a.k.a EPT/NPT |
| |
(MFN) (MFN)
| |
RAM RAM
For PV guests GFN is equal to MFN for a single page but not for a contiguous range of pages.
Bus Frame Numbers (BFN) refer to the address presented on the physical bus before being translated by the IOMMU.
Diagram below details memory accesses originating from physical device.
Physical Device
|
(BFN)
|
IOMMU-PT
|
(MFN)
|
RAMPurpose
=======
1. Allow Xen guests to create/modify/destroy IOMMU mappings for hardware devices that the PV guests has access to. This enables the PV guest to program a bus address space mapping which matches its GFN mapping. Once a 1-1 mapping of PFN to bus address space is created then a bounce buffer region is not required for the I/O devices connected to the IOMMU.
2. Allow for Xen guests to lookup/create/modify/destroy IOMMU mappings for guest memory of domains the calling Xen guest has sufficient privilege over. This enables domains to provide mediated hardware acceleration to other guest domains.
General principles for PV IOMMU interface
=========================================
There are two different usage models for the BFN address space of a calling guest based upon the two purposes specified in the section above.
A calling guest may use their BFN address space for only one of the purposes detailed above and so the PV IOMMU interface has a subop per usage model. Furthermore, the IOMMU mapping of foreign domains memory is more complex than IOMMU mapping local domain memory and seperating the subops allows for the complexity to be split in the implementation.
The PV IOMMU design allows the calling domain to control it's BFN memory map. Thus the design also assigns the responsiblity of ensuring a BFN address mapped for local domain memory mappings are not reused for foreign domain memory mappings without an explict unmap of BFN address first. This simplifies the usage of the API and the extra overhead for the calling domains should be minimal as they should be already tracking the BFN address space usage already.
Emulator usage of PV IOMMU interface
====================================
Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
A IOMMUOP_query_caps subop will return the IOMMU_QUERY_map_cap flag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via the IOMMUOP_map_foreign_page subop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via the IOMMUOP_lookup_foreign_page.
Operating systems which use the IOMMUOP_map_page subop are expected to provide a common interface for emulators to use. Otherwise emulators will not be aware of existing BFN mappings created by operating system and will get failed subops due to conflicts in the BFN address space for the domain.
Emulators should unmap unused GFN mappings as often as possible using IOMMUOP_unmap_foreign_page subops so that guest domains can balloon pages quickly and efficiently.
Emulators should conform to the ballooning behaviour described section "IOMMUOP_*_foreign_page interactions with guest domain ballooning" so that guest domains are able to effectively balloon out and in memory.
Emulators must unmap any active BFN mappings when they shutdown.
IOMMUOP_*_foreign_page interactions with guest domain ballooning
================================================================
Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered I/O request of type IOREQ_TYPE_INVALIDATE to the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered I/O request ring is full then a standard (synchronous) I/O request of type IOREQ_TYPE_INVALIDATE will be issued to the affected IOREQ server the with just invalidated BFN address in the data field.
The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a IOMMUOP_unmap_foreign_page subop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.
For hosts with IOMMU support:
If the BFN was mapped without the IOMMUOP_swap_mfn flag set in the IOMMUOP_map_foreign_page then the affected affected emulator(s) must specifically issue a IOMMUOP_unmap_foreign_page subop for the now invalid BFN address so that the references to the underlying MFN are removed.
If the BFN was mapped with the IOMMUOP_swap_mfn flag set in the IOMMUOP_map_foreign_page subop for all emulators with mappings of that GFN then the BFN mapping will be swapped to point at a scratch MFN page and all BFN references to the invalid MFN will be removed by Xen after the BFN mapping has been updated to point at the scratch MFN page.
The rationale for swapping the BFN mapping to point at scratch pages is to enable guest domains to balloon quickly without requiring hypercall(s) from emulators.
Not all BFN mappings can be swapped without potentially causing problems for the hardware itself (command rings etc.) so the IOMMUOP_swap_mfn flag is used to allow per BFN control of Xen ballooning behaviour.
PV IOMMU interactions with self ballooning
==========================================
A guest should clear any IOMMU mappings it has of its own pages before releasing a page back to Xen. The guest also will need to add IOMMU mappings after repopulating a page with the populate_physmap hypercall.
PV guests must clear any IOMMU mappings before pinning page table pages because the IOMMU mappings will take a writable reference count and this will prevent page table pinning.
Security Implications of allowing domain IOMMU control
======================================================
Xen currently allows I/O devices attached to hardware domain to have direct access to the all of the MFN address space (except Xen hypervisor memory regions), provided the Xen IOMMU option dom0-strict is not enabled.
The PV IOMMU feature provides the same level of access to MFN address space and the feature is not enabled when the Xen IOMMU option dom0-strict is enabled. Therefore security is not degraded by the PV IOMMU feature.
Domains with physical device(s) assigned which are not hardware domains are only allowed to map their own GFNs or GFNs for domain(s) they have privilege over.
PV IOMMU interactions with grant map/unmap operations
=====================================================
Grant map operations return a Physical device accessible address (BFN) if the GNTMAP_device_map flag is set. This operation currently returns the MFN for PV guests which may conflict with the BFN address space the guest uses if PV IOMMU map support is available to the guest.
This design proposes to allow the calling domain to control the BFN address that a grant map operation uses.
This can be achieved by specifying that the dev_bus_addr in the gnttab_map_grant_ref structure is used an input parameter instead of the output parameter it is currently.
Only PAGE_SIZE aligned addresses are allowed for dev_bus_addr input parameter.
The revised structure is shown below for convenience.
struct gnttab_map_grant_ref {
/* IN parameters. */
uint64_t host_addr;
uint32_t flags; /* GNTMAP_* */
grant_ref_t ref;
domid_t dom;
/* OUT parameters. */
int16_t status; /* => enum grant_status */
grant_handle_t handle;
/* IN/OUT parameters */
uint64_t dev_bus_addr;
};The grant map operation would then behave similarly to the IOMMUOP_map_page subop for the creation of the IOMMU mapping.
The grant unmap operation would then behave similarly to the IOMMUOP_unmap_page subop for the removal of the IOMMU mapping.
A new grantmap flag would be used to indicate the domain is requesting the dev_bus_addr field is used an input parameter.
#define _GNTMAP_request_bfn_map (6)
#define GNTMAP_request_bfn_map (1<<_GNTMAP_request_bfn_map)Xen PV-IOMMU Architecture
=========================
The Xen architecture consists of a new hypercall interface and changes to the grant map interface.
The existing IOMMU mappings setup at domain creation time will be preserved so that PV domains unaware of this feature will continue to function with no changes required.
Memory ballooning will be supported by taking an additional reference on the MFN backing the GFN for each successful IOMMU mapping created.
An M2B tracking structure will be used to ensure all references to an MFN can be located efficiently.
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_t do_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)
First argument, guest handle pointer to array of struct pv_iommu_op
Second argument, unsigned integer count of struct pv_iommu_op elements in array.
Definition of struct pv_iommu_op:
struct pv_iommu_op {
uint16_t subop_id;
uint16_t flags;
int32_t status;
union {
struct {
uint64_t bfn;
uint64_t gfn;
} map_page;
struct {
uint64_t bfn;
} unmap_page;
struct {
uint64_t bfn;
uint64_t gfn;
uint16_t domid;
ioservid_t ioserver;
} map_foreign_page;
struct {
uint64_t bfn;
uint64_t gfn;
uint16_t domid;
ioservid_t ioserver;
} lookup_foreign_page;
struct {
uint64_t bfn;
ioservid_t ioserver;
} unmap_foreign_page;
} u;
};Definition of PV IOMMU subops:
#define IOMMUOP_query_caps 1
#define IOMMUOP_map_page 2
#define IOMMUOP_unmap_page 3
#define IOMMUOP_map_foreign_page 4
#define IOMMUOP_lookup_foreign_page 5
#define IOMMUOP_unmap_foreign_page 6Design considerations for hypercall op
-------------------------------------------
IOMMU map/unmap operations can be slow and can involve flushing the IOMMU TLB to ensure the I/O device uses the updated mappings.
The op has been designed to take an array of operations and a count as parameters. This allows for easily implemented hypercall continuations to be used and allows for batches of IOMMU operations to be submitted before flushing the IOMMU TLB.
The subop_id to be used for a particular element is encoded into the element itself. This allows for map and unmap operations to be performed in one hypercall and for the IOMMU TLB flushing optimisations to be still applied.
The hypercall will ensure that the required IOMMU TLB flushes are applied before returning to guest via either hypercall completion or a hypercall continuation.
IOMMUOP_query_caps
------------------
This subop queries the runtime capabilities of the PV-IOMMU interface for the specific calling domain. This subop uses struct pv_iommu_op directly.
------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `flags` [out] This field details the IOMMUOP capabilities.
status [out] Status of this op, op specific values listed below
------------------------------------------------------------------------------
Defined bits for flags field:
------------------------------------------------------------------------------
Name Bit Definition
---- ------ ----------------------------------IOMMU_QUERY_map_cap 0 IOMMUOP_map_page or IOMMUOP_map_foreign
can be used for the calling domain
IOMMU_QUERY_map_all_mfns 1 IOMMUOP_map_page subop can map any MFN
not used by Xen
Reserved for future use 2-9 n/a
IOMMU_page_order 10-15 Returns maximum possible page order for
all other IOMMUOP subops
------------------------------------------------------------------------------
Defined values for query_caps subop status field:
Value Reason ------ ---------------------------------------------------------- 0 subop successfully returned
IOMMUOP_map_page
----------------------
This subop uses struct map_page part of the struct pv_iommu_op.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs except for Xen owned MFNs else the hardware domain will only be allowed to map GFNs which it owns.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs without taking a reference to the MFN backing the GFN by setting the IOMMU_MAP_OP_no_ref_cnt flag.
Every successful pv_iommu_op will result in an additional page reference being taken on the MFN backing the GFN except for the condition detailed above.
If the map_op flags indicate a writeable mapping is required then a writeable page type reference will be taken otherwise a standard page reference will be taken.
All the following conditions are required to be true for PV IOMMU map subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. If hardware_domain and the following Xen IOMMU options are NOT enabled: dom0-passthrough
This subop usage of the struct pv_iommu_op and struct map_page fields are detailed below:
------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `bfn` [in] Bus address frame number(BFN) to be mapped to specified gfn
below
gfn [in] Guest address frame number for DOMID_SELF
flags [in] Flags for signalling type of IOMMU mapping to be created,
Flags can be combined.
status [out] Mapping status of this op, op specific values listed below
------------------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMU_OP_readable 0 Create readable IOMMU mapping IOMMU_OP_writeable 1 Create writeable IOMMU mapping IOMMU_MAP_OP_no_ref_cnt 2 IOMMU mapping does not take a reference to
MFN backing BFN mapping
Reserved for future use 3-9 n/aIOMMU_page_order 10-15 Page order to be used for both gfn and bfn
Defined values for map_page subop status field:
Value Reason ------ ---------------------------------------------------------------------- 0 subop successfully returned -EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM Domain is not the hardware domain and GFN does not belong to domain -EPERM Domain is the hardware domain, IOMMU dom-strict mode is enabled and
GFN does not belong to domain-EACCES BFN address conflicts with RMRR regions for devices attached to
DOMID_SELF-ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMUOP_unmap_page
------------------
This subop uses struct unmap_page part of the struct pv_iommu_op.
The subop usage of the struct pv_iommu_op and struct unmap_page fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
flags [in] Flags for signalling page order of unmap operation
status [out] Mapping status of this unmap operation, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------
Reserved for future use 0-9 n/aIOMMU_page_order 10-15 Page order to be used for bfn
Defined values for unmap_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned -EIO IOMMU unit returned error when attempting to unmap BFN. -ENOSPC Page order is too large for either BFN address or IOMMU unit
------------------------------------------------------------------------
IOMMUOP_map_foreign_page
------------------------
This subop uses struct map_foreign_page part of the struct pv_iommu_op.
It is not valid to use a domid representing the calling domain.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
The M2B mechanism is an MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
All the following conditions are required to be true for PV IOMMU map_foreign subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is the hardware_domain and the following Xen IOMMU options are NOT enabled: dom0-passthrough
This subop usage of the struct pv_iommu_op and struct map_foreign_page fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain id for which the gfn field applies
ioserver [in] IOREQ server id associated with mapping
bfn [in] Bus address frame number for gfn address
gfn [in] Guest address frame number
flags [in] Details the status of the BFN mapping
status [out] status of this subop, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safely
swapped to scratch page
Reserved for future use 3-9 Reserved flag bits should be 0IOMMU_page_order 10-15 Page order to be used for both gfn and bfn
Defined values for map_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned
-EIO IOMMU unit returned error when attempting to map BFN to GFN.
-EPERM Calling domain does not have sufficient privilege over domid
-EPERM GFN could not be mapped because the GFN belongs to Xen.
-EPERM domid maps to DOMID_SELF
-EACCES BFN address conflicts with RMRR regions for devices attached to
DOMID_SELF
-ENODEV Provided ioserver id is not valid
-ENXIO Provided domid id is not valid
-ENXIO Provided GFN address is not valid
-ENOSPC Page order is too large for either BFN, GFN or IOMMU unitIOMMU_lookup_foreign_page
-------------------------
This subop uses struct lookup_foreign_page part of the struct pv_iommu_op.
This subop lookups up a BFN mapping for a ioserver + gfn + target domid combination.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
If a 1:1 mapping exists of BFN to MFN then a M2B entry is added and a reference is taken to the underlying MFN. If an existing mapping is present then the BFN is returned and no additional reference's will be taken to the underlying MFN.
A 1:1 mapping will exist if there is no IOMMU support or if the PV hardware domain was booted in dom0-relaxed mode or in dom0-passthrough mode.
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
This subop usage of the struct pv_iommu_op and struct lookup_foreign_page fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain id for which the gfn field applies
ioserver [in] IOREQ server id associated with mapping
bfn [out] Bus address frame number for gfn address
gfn [in] Guest address frame number
flags [out] Details the status of the BFN mapping
status [out] status of this subop, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 Returned BFN IOMMU mapping is readable IOMMUOP_writeable 1 Returned BFN IOMMU mapping is writeable Reserved for future use 2-9 Reserved flag bits should be 0 IOMMU_page_order 10-15 Returns maximum possible page order for
all other IOMMUOP subops
Defined values for lookup_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned -EPERM Calling domain does not have sufficient privilege over domid -ENOENT There is no available BFN for provided GFN + domid combination -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid
IOMMUOP_unmap_foreign_page
--------------------------
This subop uses struct unmap_foreign_page part of the struct pv_iommu_op.
It only allows BFNs acquired via IOMMUOP_map_foreign_page or IOMMUOP_lookup_page to be unmapped. If an attempt is made to unmap a BFN mapped via IOMMUOP_map_page then the subop will fail.
The subop will perform a B2M lookup (IO page table walk) for the calling domain and then index the M2B using the returned MFN. This is safe because a particular BFN mapping can only map to one MFN for a particular calling domain.
This subop usage of the struct pv_iommu_op and struct unmap_foreign_page fields are detailed below:
-----------------------------------------------------------------------
Field Purpose ----- -------------------------------------------------------- `ioserver` [in] IOREQ server id associated with mapping
bfn [in] Bus address frame number for gfn address
flags [in] Flags for signalling page order of unmap operation
status [out] status of this subop, 0 indicates success
-----------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition ---- ----- ---------------------------------- Reserved for future use 0-9 n/a IOMMU_page_order 10-15 Page order to be used for bfn unmapping
Defined values for unmap_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned-ENOENT An M2B entry was not found for the specified input parameters.
Linux kernel architecture
=========================
The Linux kernel will use the PV-IOMMU hypercalls to map its PFN address space into the IOMMU. It will map the PFNs to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
The native SWIOTLB will be used to handle devices which cannot DMA to all of the kernel's PFN address space.
An interface shall be provided for emulator usage of IOMMUOP_*_foreign_page subops which will allow the Linux kernel to centrally manage that domain's BFN resource and ensure there are no unexpected conflicts.
Kernel Map Foreign GFN to BFN interface
---------------------------------------
An array of 'count' of 'struct pv_iommu_ops' will be passed to the mapping function.
int map_foreign_gfn_to_bfn(int count, struct pv_iommu_op *ops)
The calling function will use the struct map_foreign_page inside the struct pv_iommu_op and will fill in the domid, gfn and ioserver_id fields.
The kernel function will reuse the passed in struct pv_iommu_op for the hypercall and will set the subop_id field based on the IOMMU_QUERY_map_cap capability.
If the IOMMU_QUERY_map_cap is set then the kernel will allocate a suitable BFN address, set the BFN field in the op to this address and set the subop_id to IOMMUOP_map_page. It will do this on all 'ops' and then issue the hypercall.
If the IOMMU_QUERY_map_cap is NOT set then the kernel will set the subops_id to IOMMUOP_lookup_page on all ops and then issue the hypercall.
The calling function should check the status field in each op and if the status field is 0 then it can use the returned BFN address in each op.
Kernel Unmap Foreign GFN to BFN interface
-----------------------------------------
An array of 'count' of 'struct pv_iommu_ops' will be passed the mapping function.
int unmap_foreign_gfn_to_bfn(int count, struct pv_iommu_op *ops)
The calling function will use the struct unmap_foreign_page inside the struct pv_iommu_op and will fill in the bfn field.
The kernel function will set the subop_id field to IOMMUOP_unmap_foreign_page in each op and then issue the hypercall.
The calling function should check the status field in each op and if the status field is 0 then the BFN has been successfully unmapped.
Re: Xen PV IOMMU interface draft C
+ARM devs.
Quoted 1 line · Malcolm Crossley
Hi All,
I had a chat with Malcolm about this with respect to ARM.
The upshot is that this does not help us to remove the dom0 1:1 workaround or associated swiotlb uses on systems without an SMMU, nor does it allow us to sensibly do passthrough on systems which lack an SMMU.
What it will be good for is in the future when doing "mediated passthrough", that is the xengt like thing where the device is partly assigned to the guest and partly emulated in a privileged domain.
I had a look through the previous draft earlier in the week and didn't notice anything which would preclude use on ARM in the future.
Ian.
Quoted 792 lines
Here is a design for allowing guests to control the IOMMU. This allows for the guest GFN mapping to be programmed into the IOMMU and avoid using the SWIOTLB bounce buffer technique in the Linux kernel (except for legacy 32 bit DMA IO devices).
Draft C has been reordered to explain expected behaviours before the APIs themselves. There's an additional section to explain the rationale for separate subops from local GFN mappings and foreign GFN mappings.
There's also further detail on the Linux API for foreign BFN mappings.
The plan is to start writing code against this version so please provide feedback on any major design problems/concerns.
The pandoc markdown format of the document is provided below to allow for easier inline comments:
% Xen PV IOMMU interface
% Malcolm Crossley <<[email protected]>>
Paul Durrant <<[email protected]>>
% Draft CIntroduction
============Revision History
------------------------------------------------------------------------------------
Version Date Changes ------- ----------- ---------------------------------------------- Draft A 10 Apr 2014 Initial draft.Draft B 12 Jun 2015 Second draft.
Draft C 26 Jun 2015 Third draft.
--------------------------------------------------------------------Background
==========Linux kernel SWIOTLB
--------------------Xen PV guests use a Pseudophysical Frame Number(PFN) address space which is decoupled from the host Machine Frame Number(MFN) address space.
PV guest hardware drivers are aware of the PFN address space only and assume that if PFN addresses are contiguous then the hardware addresses would be contiguous as well. The decoupling between PFN and MFN address spaces means PFN and MFN addresses may not be contiguous across page boundaries and thus a buffer allocated in GFN address space which spans a page boundary may not be contiguous in MFN address space.
PV hardware drivers cannot tolerate this behaviour and so a special "bounce buffer" region is used to hide this issue from the drivers.
A bounce buffer region is a special part of the PFN address space which has been made to be contiguous in both PFN and MFN address spaces. When a driver requests a buffer which spans a page boundary be made available for hardware to read the core operating system code copies the buffer into a temporarily reserved part of the bounce buffer region and then returns the MFN address of the reserved part of the bounce buffer region back to the driver itself. The driver then instructs the hardware to read the copy of the buffer in the bounce buffer. Similarly if the driver requests a buffer is made available for hardware to write to the first a region of the bounce buffer is reserved and then after the hardware completes writing then the reserved region of bounce buffer is copied to the originally allocated buffer.
The overheard of memory copies to/from the bounce buffer region is high and damages performance. Furthermore, there is a risk the fixed size bounce buffer region will become exhausted and it will not be possible to return an hardware address back to the driver. The Linux kernel drivers do not tolerate this failure and so the kernel is forced to crash, as an unrecoverable error has occurred.
Input/Output Memory Management Units (IOMMU) allow for an inbound address mapping to be created from the I/O Bus address space (typically PCI) to the machine frame number address space. IOMMUs typically use a page table mechanism to manage the mappings and therefore can create mappings of page size granularity or larger.
The I/O Bus address space will be referred to as the Bus Frame Number (BFN) address space for the rest of this document.
Mediated Pass-through Emulators
-------------------------------Mediated Pass-through emulators allow guest domains to interact with hardware devices via emulator mediation. The emulator runs in a domain separate to the guest domain and it is used to enforce security of guest access to the hardware devices and isolation of different guests accessing the same hardware device.
The emulator requires a mechanism to map guest addresses to a bus address that the hardware devices can access.
Clarification of GFN and BFN fields for different guest types
-------------------------------------------------------------
Guest Frame Numbers (GFN) definition varies depending on the guest type.Diagram below details the memory accesses originating from CPU, per guest type:
HVM guest PV guest
(VA) (VA)
| |
MMU MMU
| |
(GFN) |
| | (GFN)
HAP a.k.a EPT/NPT |
| |
(MFN) (MFN)
| |
RAM RAMFor PV guests GFN is equal to MFN for a single page but not for a contiguous range of pages.
Bus Frame Numbers (BFN) refer to the address presented on the physical bus before being translated by the IOMMU.
Diagram below details memory accesses originating from physical device.
Physical Device | (BFN) | IOMMU-PT | (MFN) | RAMPurpose
=======1. Allow Xen guests to create/modify/destroy IOMMU mappings for hardware devices that the PV guests has access to. This enables the PV guest to program a bus address space mapping which matches its GFN mapping. Once a 1-1 mapping of PFN to bus address space is created then a bounce buffer region is not required for the I/O devices connected to the IOMMU.
2. Allow for Xen guests to lookup/create/modify/destroy IOMMU mappings for guest memory of domains the calling Xen guest has sufficient privilege over. This enables domains to provide mediated hardware acceleration to other guest domains.
General principles for PV IOMMU interface
=========================================There are two different usage models for the BFN address space of a calling guest based upon the two purposes specified in the section above.
A calling guest may use their BFN address space for only one of the purposes detailed above and so the PV IOMMU interface has a subop per usage model. Furthermore, the IOMMU mapping of foreign domains memory is more complex than IOMMU mapping local domain memory and seperating the subops allows for the complexity to be split in the implementation.
The PV IOMMU design allows the calling domain to control it's BFN memory map. Thus the design also assigns the responsiblity of ensuring a BFN address mapped for local domain memory mappings are not reused for foreign domain memory mappings without an explict unmap of BFN address first. This simplifies the usage of the API and the extra overhead for the calling domains should be minimal as they should be already tracking the BFN address space usage already.
Emulator usage of PV IOMMU interface
====================================Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
A
IOMMUOP_query_capssubop will return theIOMMU_QUERY_map_capflag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via theIOMMUOP_map_foreign_pagesubop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via theIOMMUOP_lookup_foreign_page.Operating systems which use the
IOMMUOP_map_pagesubop are expected to provide a common interface for emulators to use. Otherwise emulators will not be aware of existing BFN mappings created by operating system and will get failed subops due to conflicts in the BFN address space for the domain.Emulators should unmap unused GFN mappings as often as possible using
IOMMUOP_unmap_foreign_pagesubops so that guest domains can balloon pages quickly and efficiently.Emulators should conform to the ballooning behaviour described section "IOMMUOP_*_foreign_page interactions with guest domain ballooning" so that guest domains are able to effectively balloon out and in memory.
Emulators must unmap any active BFN mappings when they shutdown.
IOMMUOP_*_foreign_page interactions with guest domain ballooning
================================================================Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered I/O request of type
IOREQ_TYPE_INVALIDATEto the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered I/O request ring is full then a standard (synchronous) I/O request of typeIOREQ_TYPE_INVALIDATEwill be issued to the affected IOREQ server the with just invalidated BFN address in the data field.The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a
IOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.For hosts with IOMMU support:
If the BFN was mapped without theIOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagethen the affected affected emulator(s) must specifically issue aIOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed.If the BFN was mapped with the
IOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagesubop for all emulators with mappings of that GFN then the BFN mapping will be swapped to point at a scratch MFN page and all BFN references to the invalid MFN will be removed by Xen after the BFN mapping has been updated to point at the scratch MFN page.The rationale for swapping the BFN mapping to point at scratch pages is to enable guest domains to balloon quickly without requiring hypercall(s) from emulators.
Not all BFN mappings can be swapped without potentially causing problems for the hardware itself (command rings etc.) so the
IOMMUOP_swap_mfnflag is used to allow per BFN control of Xen ballooning behaviour.PV IOMMU interactions with self ballooning
==========================================A guest should clear any IOMMU mappings it has of its own pages before releasing a page back to Xen. The guest also will need to add IOMMU mappings after repopulating a page with the
populate_physmaphypercall.PV guests must clear any IOMMU mappings before pinning page table pages because the IOMMU mappings will take a writable reference count and this will prevent page table pinning.
Security Implications of allowing domain IOMMU control
======================================================Xen currently allows I/O devices attached to hardware domain to have direct access to the all of the MFN address space (except Xen hypervisor memory regions), provided the Xen IOMMU option dom0-strict is not enabled.
The PV IOMMU feature provides the same level of access to MFN address space and the feature is not enabled when the Xen IOMMU option dom0-strict is enabled. Therefore security is not degraded by the PV IOMMU feature.
Domains with physical device(s) assigned which are not hardware domains are only allowed to map their own GFNs or GFNs for domain(s) they have privilege over.
PV IOMMU interactions with grant map/unmap operations
=====================================================Grant map operations return a Physical device accessible address (BFN) if the
GNTMAP_device_mapflag is set. This operation currently returns the MFN for PV guests which may conflict with the BFN address space the guest uses if PV IOMMU map support is available to the guest.This design proposes to allow the calling domain to control the BFN address that a grant map operation uses.
This can be achieved by specifying that the
dev_bus_addrin thegnttab_map_grant_refstructure is used an input parameter instead of the output parameter it is currently.Only
PAGE_SIZEaligned addresses are allowed fordev_bus_addrinput parameter.The revised structure is shown below for convenience.
struct gnttab_map_grant_ref { /* IN parameters. */ uint64_t host_addr; uint32_t flags; /* GNTMAP_* */ grant_ref_t ref; domid_t dom; /* OUT parameters. */ int16_t status; /* => enum grant_status */ grant_handle_t handle; /* IN/OUT parameters */ uint64_t dev_bus_addr; };The grant map operation would then behave similarly to the
IOMMUOP_map_pagesubop for the creation of the IOMMU mapping.The grant unmap operation would then behave similarly to the
IOMMUOP_unmap_pagesubop for the removal of the IOMMU mapping.A new grantmap flag would be used to indicate the domain is requesting the
dev_bus_addrfield is used an input parameter.#define _GNTMAP_request_bfn_map (6) #define GNTMAP_request_bfn_map (1<<_GNTMAP_request_bfn_map)Xen PV-IOMMU Architecture
=========================The Xen architecture consists of a new hypercall interface and changes to the grant map interface.
The existing IOMMU mappings setup at domain creation time will be preserved so that PV domains unaware of this feature will continue to function with no changes required.
Memory ballooning will be supported by taking an additional reference on the MFN backing the GFN for each successful IOMMU mapping created.
An M2B tracking structure will be used to ensure all references to an MFN can be located efficiently.
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_tdo_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)First argument, guest handle pointer to array of
struct pv_iommu_opSecond argument, unsigned integer count of
struct pv_iommu_opelements in array.Definition of
struct pv_iommu_op:struct pv_iommu_op { uint16_t subop_id; uint16_t flags; int32_t status; union { struct { uint64_t bfn; uint64_t gfn; } map_page; struct { uint64_t bfn; } unmap_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver; } map_foreign_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver; } lookup_foreign_page; struct { uint64_t bfn; ioservid_t ioserver; } unmap_foreign_page; } u; };Definition of PV IOMMU subops:
#define IOMMUOP_query_caps 1 #define IOMMUOP_map_page 2 #define IOMMUOP_unmap_page 3 #define IOMMUOP_map_foreign_page 4 #define IOMMUOP_lookup_foreign_page 5 #define IOMMUOP_unmap_foreign_page 6Design considerations for hypercall op
-------------------------------------------
IOMMU map/unmap operations can be slow and can involve flushing the IOMMU TLB to ensure the I/O device uses the updated mappings.The op has been designed to take an array of operations and a count as parameters. This allows for easily implemented hypercall continuations to be used and allows for batches of IOMMU operations to be submitted before flushing the IOMMU TLB.
The
subop_idto be used for a particular element is encoded into the element itself. This allows for map and unmap operations to be performed in one hypercall and for the IOMMU TLB flushing optimisations to be still applied.The hypercall will ensure that the required IOMMU TLB flushes are applied before returning to guest via either hypercall completion or a hypercall continuation.
IOMMUOP_query_caps
------------------This subop queries the runtime capabilities of the PV-IOMMU interface for the specific calling domain. This subop uses
struct pv_iommu_opdirectly.------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `flags` [out] This field details the IOMMUOP capabilities.
status[out] Status of this op, op specific values listed below
------------------------------------------------------------------------------Defined bits for flags field:
------------------------------------------------------------------------------
Name Bit Definition
---- ------ ----------------------------------IOMMU_QUERY_map_cap0IOMMUOP_map_pageorIOMMUOP_map_foreign
can be used for the calling domain
IOMMU_QUERY_map_all_mfns1IOMMUOP_map_pagesubop can map any MFN
not used by XenReserved for future use 2-9 n/a
IOMMU_page_order10-15 Returns maximum possible page order for
all other IOMMUOP subops
------------------------------------------------------------------------------Defined values for
query_capssubop status field:Value Reason ------ ---------------------------------------------------------- 0 subop successfully returned
IOMMUOP_map_page
----------------------
This subop usesstruct map_pagepart of thestruct pv_iommu_op.If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs except for Xen owned MFNs else the hardware domain will only be allowed to map GFNs which it owns.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs without taking a reference to the MFN backing the GFN by setting the
IOMMU_MAP_OP_no_ref_cntflag.Every successful
pv_iommu_opwill result in an additional page reference being taken on the MFN backing the GFN except for the condition detailed above.If the
map_opflags indicate a writeable mapping is required then a writeable page type reference will be taken otherwise a standard page reference will be taken.All the following conditions are required to be true for PV IOMMU map subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. Ifhardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthroughThis subop usage of the
struct pv_iommu_opandstruct map_pagefields are detailed below:------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `bfn` [in] Bus address frame number(BFN) to be mapped to specified gfnbelow
gfn[in] Guest address frame number forDOMID_SELF
flags[in] Flags for signalling type of IOMMU mapping to be created,
Flags can be combined.
status[out] Mapping status of this op, op specific values listed below
------------------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMU_OP_readable 0 Create readable IOMMU mapping IOMMU_OP_writeable 1 Create writeable IOMMU mapping IOMMU_MAP_OP_no_ref_cnt 2 IOMMU mapping does not take a reference toMFN backing BFN mapping
Reserved for future use 3-9 n/aIOMMU_page_order10-15 Page order to be used for both gfn and bfnDefined values for
map_pagesubop status field:Value Reason ------ ---------------------------------------------------------------------- 0 subop successfully returned(quoted diff) +0
-6-EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM Domain is not the hardware domain and GFN does not belong to domain -EPERM Domain is the hardware domain, IOMMU dom-strict mode is enabled and GFN does not belong to domain -EACCES BFN address conflicts with RMRR regions for devices attached to DOMID_SELF -ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMUOP_unmap_page
------------------
This subop usesstruct unmap_pagepart of thestruct pv_iommu_op.The subop usage of the
struct pv_iommu_opandstruct unmap_pagefields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
flags[in] Flags for signalling page order of unmap operation
status[out] Mapping status of this unmap operation, 0 indicates success
--------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------
Reserved for future use 0-9 n/aIOMMU_page_order10-15 Page order to be used for bfnDefined values for
unmap_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-3-EIO IOMMU unit returned error when attempting to unmap BFN. -ENOSPC Page order is too large for either BFN address or IOMMU unit ------------------------------------------------------------------------
IOMMUOP_map_foreign_page
------------------------
This subop usesstruct map_foreign_pagepart of thestruct pv_iommu_op.It is not valid to use a domid representing the calling domain.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
The M2B mechanism is an MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
All the following conditions are required to be true for PV IOMMU
map_foreignsubop to succeed:1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is thehardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthroughThis subop usage of the
struct pv_iommu_opandstruct map_foreign_pagefields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain id for which the gfn field applies
ioserver[in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn address
gfn[in] Guest address frame number
flags[in] Details the status of the BFN mapping
status[out] status of this subop, 0 indicates success
--------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safelyswapped to scratch page
Reserved for future use 3-9 Reserved flag bits should be 0IOMMU_page_order10-15 Page order to be used for both gfn and bfnDefined values for
map_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-9-EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM Calling domain does not have sufficient privilege over domid -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM domid maps to DOMID_SELF -EACCES BFN address conflicts with RMRR regions for devices attached to DOMID_SELF -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid -ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMU_lookup_foreign_page
-------------------------
This subop usesstruct lookup_foreign_pagepart of thestruct pv_iommu_op.This subop lookups up a BFN mapping for a ioserver + gfn + target domid combination.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
If a 1:1 mapping exists of BFN to MFN then a M2B entry is added and a reference is taken to the underlying MFN. If an existing mapping is present then the BFN is returned and no additional reference's will be taken to the underlying MFN.
A 1:1 mapping will exist if there is no IOMMU support or if the PV hardware domain was booted in dom0-relaxed mode or in dom0-passthrough mode.
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
This subop usage of the
struct pv_iommu_opandstruct lookup_foreign_pagefields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain id for which the gfn field applies
ioserver[in] IOREQ server id associated with mapping
bfn[out] Bus address frame number for gfn address
gfn[in] Guest address frame number
flags[out] Details the status of the BFN mapping
status[out] status of this subop, 0 indicates success
--------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 Returned BFN IOMMU mapping is readable IOMMUOP_writeable 1 Returned BFN IOMMU mapping is writeable Reserved for future use 2-9 Reserved flag bits should be 0 IOMMU_page_order 10-15 Returns maximum possible page order forall other IOMMUOP subops
Defined values for
lookup_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned(quoted diff) +0
-5-EPERM Calling domain does not have sufficient privilege over domid -ENOENT There is no available BFN for provided GFN + domid combination -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid
IOMMUOP_unmap_foreign_page
--------------------------
This subop usesstruct unmap_foreign_pagepart of thestruct pv_iommu_op.It only allows BFNs acquired via
IOMMUOP_map_foreign_pageorIOMMUOP_lookup_pageto be unmapped. If an attempt is made to unmap a BFN mapped viaIOMMUOP_map_pagethen the subop will fail.The subop will perform a B2M lookup (IO page table walk) for the calling domain and then index the M2B using the returned MFN. This is safe because a particular BFN mapping can only map to one MFN for a particular calling domain.
This subop usage of the
struct pv_iommu_opandstruct unmap_foreign_pagefields are detailed below:-----------------------------------------------------------------------
Field Purpose ----- -------------------------------------------------------- `ioserver` [in] IOREQ server id associated with mapping
bfn[in] Bus address frame number for gfn address
flags[in] Flags for signalling page order of unmap operation
status[out] status of this subop, 0 indicates success
-----------------------------------------------------------------------Defined bits for flags field:
Name Bit Definition ---- ----- ---------------------------------- Reserved for future use 0-9 n/a IOMMU_page_order 10-15 Page order to be used for bfn unmappingDefined values for
unmap_foreign_pagesubop status field:Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned-ENOENTAn M2B entry was not found for the specified input parameters.Linux kernel architecture
=========================The Linux kernel will use the PV-IOMMU hypercalls to map its PFN address space into the IOMMU. It will map the PFNs to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
The native SWIOTLB will be used to handle devices which cannot DMA to all of the kernel's PFN address space.
An interface shall be provided for emulator usage of IOMMUOP_*_foreign_page subops which will allow the Linux kernel to centrally manage that domain's BFN resource and ensure there are no unexpected conflicts.
Kernel Map Foreign GFN to BFN interface
---------------------------------------An array of 'count' of 'struct
pv_iommu_ops' will be passed to the mapping function.int
map_foreign_gfn_to_bfn(int count, struct pv_iommu_op *ops)The calling function will use the
struct map_foreign_pageinside thestruct pv_iommu_opand will fill in the domid, gfn andioserver_idfields.The kernel function will reuse the passed in struct
pv_iommu_opfor the hypercall and will set thesubop_idfield based on theIOMMU_QUERY_map_capcapability.If the
IOMMU_QUERY_map_capis set then the kernel will allocate a suitable BFN address, set the BFN field in the op to this address and set thesubop_idtoIOMMUOP_map_page. It will do this on all 'ops' and then issue the hypercall.If the
IOMMU_QUERY_map_capis NOT set then the kernel will set thesubops_idtoIOMMUOP_lookup_pageon allopsand then issue the hypercall.The calling function should check the status field in each op and if the status field is 0 then it can use the returned BFN address in each op.
Kernel Unmap Foreign GFN to BFN interface
-----------------------------------------An array of 'count' of 'struct
pv_iommu_ops' will be passed the mapping function.int
unmap_foreign_gfn_to_bfn(int count, struct pv_iommu_op *ops)The calling function will use the
struct unmap_foreign_pageinside thestruct pv_iommu_opand will fill in the bfn field.The kernel function will set the
subop_idfield toIOMMUOP_unmap_foreign_pagein each op and then issue the hypercall.The calling function should check the status field in each op and if the status field is 0 then the BFN has been successfully unmapped.
Re: Xen PV IOMMU interface draft C
Quoted 11 lines · Ian Campbell
+ARM devs.
Quoted 1 line · Malcolm Crossley
Hi All,
I had a chat with Malcolm about this with respect to ARM.
The upshot is that this does not help us to remove the dom0 1:1 workaround or associated swiotlb uses on systems without an SMMU, nor does it allow us to sensibly do passthrough on systems which lack an SMMU.
What would?
Re: Xen PV IOMMU interface draft C
Quoted 6 lines · Konrad Rzeszutek Wilk
Quoted 5 lines · Ian Campbell
+ARM devs.
Quoted 1 line · Malcolm Crossley
Hi All,
Quoted 8 lines
Quoted 6 lines
I had a chat with Malcolm about this with respect to ARM.
The upshot is that this does not help us to remove the dom0 1:1 workaround or associated swiotlb uses on systems without an SMMU, nor does it allow us to sensibly do passthrough on systems which lack an SMMU.
What would?
This "Xen PV IOMMU interface".
Re: Xen PV IOMMU interface draft C
Quoted 9 lines · Ian Campbell
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 7 lines · Ian Campbell
+ARM devs.
Quoted 1 line · Malcolm Crossley
Hi All,
I had a chat with Malcolm about this with respect to ARM.
Quoted 8 lines
Quoted 6 lines
Quoted 4 lines
The upshot is that this does not help us to remove the dom0 1:1 workaround or associated swiotlb uses on systems without an SMMU, nor does it allow us to sensibly do passthrough on systems which lack an SMMU.
What would?
This "Xen PV IOMMU interface".
I guess Konrad is asking if this PV IOMMU interface doesn't solve these problems, what (other possible interface) would? I guess the answer is a SMMU?
David
Re: Xen PV IOMMU interface draft C
In reply to Ian CampbellQuoted 9 lines · Ian Campbell
Quoted 8 lines · Konrad Rzeszutek Wilk
Quoted 7 lines · Ian Campbell
+ARM devs.
Quoted 1 line · Malcolm Crossley
Hi All,
I had a chat with Malcolm about this with respect to ARM.
Quoted 10 lines
Quoted 6 lines
Quoted 4 lines
The upshot is that this does not help us to remove the dom0 1:1 workaround or associated swiotlb uses on systems without an SMMU, nor does it allow us to sensibly do passthrough on systems which lack an SMMU.
What would?
This "Xen PV IOMMU interface".
You only get "mediated device passthrough" on system without IOMMU's.
Re: Xen PV IOMMU interface draft C
In reply to David VrabelQuoted 15 lines · David Vrabel
Quoted 14 lines · Ian Campbell
Quoted 13 lines · Konrad Rzeszutek Wilk
Quoted 11 lines · Ian Campbell
+ARM devs.
Quoted 1 line · Malcolm Crossley
Hi All,
I had a chat with Malcolm about this with respect to ARM.
The upshot is that this does not help us to remove the dom0 1:1 workaround or associated swiotlb uses on systems without an SMMU, nor does it allow us to sensibly do passthrough on systems which lack an SMMU.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
What would?
This "Xen PV IOMMU interface".
I guess Konrad is asking if this PV IOMMU interface doesn't solve these problems, what (other possible interface) would?
Ah, I thought it was a response to the this in "... that this doest not...".
Quoted 1 line
I guess the answer is a SMMU?
That's the only solution I've thought of, yes.
Ian.
Re: Xen PV IOMMU interface draft C
In reply to Malcolm CrossleyQuoted 9 lines
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_tdo_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)First argument, guest handle pointer to array of
struct pv_iommu_opSecond argument, unsigned integer count of
struct pv_iommu_opelements in array.
Is there an upper limit on it? If there is an we hit, what is the return value? Should it be E2BIG and the toolstack (or kernel) should adjust size accordinly?
.. snip..
Quoted 10 lines
Linux kernel architecture
=========================The Linux kernel will use the PV-IOMMU hypercalls to map its PFN address space into the IOMMU. It will map the PFNs to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
The native SWIOTLB will be used to handle devices which cannot DMA to all of the kernel's PFN address space.
native? Or the Xen SWIOTLB which consult the P2M space? In which case it is the Xen-SWIOTLB.
Xen PV IOMMU interface draft D
In reply to Malcolm Crossley% Xen PV IOMMU interface
% Malcolm Crossley <<[email protected]>>
Paul Durrant <<[email protected]>>
% Draft D
Introduction
============
Revision History
----------------
--------------------------------------------------------------------
Version Date Changes ------- ----------- ---------------------------------------------- Draft A 10 Apr 2014 Initial draft.
Draft B 12 Jun 2015 Second draft.
Draft C 26 Jun 2015 Third draft.
Draft D 09 Feb 2016 Fourth draft.
--------------------------------------------------------------------
Background
==========
Linux kernel SWIOTLB
--------------------
Xen PV guests use a Pseudophysical Frame Number(PFN) address space which is decoupled from the host Machine Frame Number(MFN) address space.
PV guest hardware drivers are aware of the PFN address space only and assume that if PFN addresses are contiguous then the hardware addresses would be contiguous as well. The decoupling between PFN and MFN address spaces means PFN and MFN addresses may not be contiguous across page boundaries and thus a buffer allocated in GFN address space which spans a page boundary may not be contiguous in MFN address space.
PV hardware drivers cannot tolerate this behaviour and so a special "bounce buffer" region is used to hide this issue from the drivers.
A bounce buffer region is a special part of the PFN address space which has been made to be contiguous in both PFN and MFN address spaces. When a driver requests a buffer which spans a page boundary be made available for hardware to read the core operating system code copies the buffer into a temporarily reserved part of the bounce buffer region and then returns the MFN address of the reserved part of the bounce buffer region back to the driver itself. The driver then instructs the hardware to read the copy of the buffer in the bounce buffer. Similarly if the driver requests a buffer is made available for hardware to write to the first a region of the bounce buffer is reserved and then after the hardware completes writing then the reserved region of bounce buffer is copied to the originally allocated buffer.
The overheard of memory copies to/from the bounce buffer region is high and damages performance. Furthermore, there is a risk the fixed size bounce buffer region will become exhausted and it will not be possible to return an hardware address back to the driver. The Linux kernel drivers do not tolerate this failure and so the kernel is forced to crash, as an unrecoverable error has occurred.
Input/Output Memory Management Units (IOMMU) allow for an inbound address mapping to be created from the I/O Bus address space (typically PCI) to the machine frame number address space. IOMMUs typically use a page table mechanism to manage the mappings and therefore can create mappings of page size granularity or larger.
The I/O Bus address space will be referred to as the Bus Frame Number (BFN) address space for the rest of this document.
Mediated Pass-through Emulators
-------------------------------
Mediated Pass-through emulators allow guest domains to interact with hardware devices via emulator mediation. The emulator runs in a domain separate to the guest domain and it is used to enforce security of guest access to the hardware devices and isolation of different guests accessing the same hardware device.
The emulator requires a mechanism to map guest addresses to a bus address that the hardware devices can access.
Clarification of GFN and BFN fields for different guest types
-------------------------------------------------------------
Guest Frame Numbers (GFN) definition varies depending on the guest type.
Diagram below details the memory accesses originating from CPU, per guest type:
HVM guest PV guest
(VA) (VA)
| |
MMU MMU
| |
(GFN) |
| | (GFN)
HAP a.k.a EPT/NPT |
| |
(MFN) (MFN)
| |
RAM RAM
For PV guests GFN is equal to MFN for a single page but not for a contiguous range of pages.
Bus Frame Numbers (BFN) refer to the address presented on the physical bus before being translated by the IOMMU.
Diagram below details memory accesses originating from physical device.
Physical Device
|
(BFN)
|
IOMMU-PT
|
(MFN)
|
RAMPurpose
=======
1. Allow Xen guests to create/modify/destroy IOMMU mappings for hardware devices that the PV guests has access to. This enables the PV guest to program a bus address space mapping which matches its GFN mapping. Once a 1-1 mapping of PFN to bus address space is created then a bounce buffer region is not required for the I/O devices connected to the IOMMU.
2. Allow for Xen guests to lookup/create/modify/destroy IOMMU mappings for guest memory of domains the calling Xen guest has sufficient privilege over. This enables domains to provide mediated hardware acceleration to other guest domains.
General principles for PV IOMMU interface
=========================================
There are two different usage models for the BFN address space of a calling guest based upon the two purposes specified in the section above.
A calling guest may use their BFN address space for only one of the purposes detailed above and so the PV IOMMU interface has a subop per usage model. Furthermore, the IOMMU mapping of foreign domains memory is more complex than IOMMU mapping local domain memory and seperating the subops allows for the complexity to be split in the implementation.
The PV IOMMU design allows the calling domain to control it's BFN memory map. Thus the design also assigns the responsiblity of ensuring a BFN address mapped for local domain memory mappings are not reused for foreign domain memory mappings without an explict unmap of BFN address first. This simplifies the usage of the API and the extra overhead for the calling domains should be minimal as they should be already tracking the BFN address space usage already.
Emulator usage of PV IOMMU interface
====================================
Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
A IOMMUOP_query_caps subop will return the IOMMU_QUERY_map_cap flag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via the IOMMUOP_map_foreign_page subop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via the IOMMUOP_lookup_foreign_page.
Operating systems which use the IOMMUOP_map_page subop are expected to provide a common interface for emulators to use. Otherwise emulators will not be aware of existing BFN mappings created by operating system and will get failed subops due to conflicts in the BFN address space for the domain.
Emulators should unmap unused GFN mappings as often as possible using IOMMUOP_unmap_foreign_page subops so that guest domains can balloon pages quickly and efficiently.
Emulators should conform to the ballooning behaviour described section "IOMMUOP_*_foreign_page interactions with guest domain ballooning" so that guest domains are able to effectively balloon out and in memory.
Emulators must unmap any active BFN mappings when they shutdown.
IOMMUOP_*_foreign_page interactions with guest domain ballooning
================================================================
Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered I/O request of type IOREQ_TYPE_INVALIDATE to the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered I/O request ring is full then a standard (synchronous) I/O request of type IOREQ_TYPE_INVALIDATE will be issued to the affected IOREQ server the with just invalidated BFN address in the data field.
The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a IOMMUOP_unmap_foreign_page subop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.
For hosts with IOMMU support:
If the BFN was mapped without the IOMMUOP_swap_mfn flag set in the IOMMUOP_map_foreign_page then the affected affected emulator(s) must specifically issue a IOMMUOP_unmap_foreign_page subop for the now invalid BFN address so that the references to the underlying MFN are removed.
If the BFN was mapped with the IOMMUOP_swap_mfn flag set in the IOMMUOP_map_foreign_page subop for all emulators with mappings of that GFN then the BFN mapping will be swapped to point at a scratch MFN page and all BFN references to the invalid MFN will be removed by Xen after the BFN mapping has been updated to point at the scratch MFN page.
The rationale for swapping the BFN mapping to point at scratch pages is to enable guest domains to balloon quickly without requiring hypercall(s) from emulators.
Not all BFN mappings can be swapped without potentially causing problems for the hardware itself (command rings etc.) so the IOMMUOP_swap_mfn flag is used to allow per BFN control of Xen ballooning behaviour.
PV IOMMU interactions with self ballooning
==========================================
A guest should clear any IOMMU mappings it has of its own pages before releasing a page back to Xen. The guest also will need to add IOMMU mappings after repopulating a page with the populate_physmap hypercall.
PV guests must clear any IOMMU mappings before pinning page table pages because the IOMMU mappings will take a writable reference count and this will prevent page table pinning.
Security Implications of allowing domain IOMMU control
======================================================
Xen currently allows I/O devices attached to hardware domain to have direct access to the all of the MFN address space (except Xen hypervisor memory regions), provided the Xen IOMMU option dom0-strict is not enabled.
The PV IOMMU feature provides the same level of access to MFN address space and the feature is not enabled when the Xen IOMMU option dom0-strict is enabled. Therefore security is not degraded by the PV IOMMU feature.
Domains with physical device(s) assigned which are not hardware domains are only allowed to map their own GFNs or GFNs for domain(s) they have privilege over.
PV IOMMU interactions with grant map/unmap operations
=====================================================
Grant map operations return a Physical device accessible address (BFN) if the GNTMAP_device_map flag is set. This operation currently returns the MFN for PV guests which may conflict with the BFN address space the guest uses if PV IOMMU map support is available to the guest.
This design proposes to allow the calling domain to control the BFN address that a grant map operation uses.
This can be achieved by specifying that the dev_bus_addr in the gnttab_map_grant_ref structure is used an input parameter instead of the output parameter it is currently.
Only PAGE_SIZE aligned addresses are allowed for dev_bus_addr input parameter.
The revised structure is shown below for convenience.
struct gnttab_map_grant_ref {
/* IN parameters. */
uint64_t host_addr;
uint32_t flags; /* GNTMAP_* */
grant_ref_t ref;
domid_t dom;
/* OUT parameters. */
int16_t status; /* => enum grant_status */
grant_handle_t handle;
/* IN/OUT parameters */
uint64_t dev_bus_addr;
};The grant map operation would then behave similarly to the IOMMUOP_map_page subop for the creation of the IOMMU mapping.
The grant unmap operation would then behave similarly to the IOMMUOP_unmap_page subop for the removal of the IOMMU mapping.
A new grantmap flag would be used to indicate the domain is requesting the dev_bus_addr field is used an input parameter.
#define _GNTMAP_request_bfn_map (6)
#define GNTMAP_request_bfn_map (1<<_GNTMAP_request_bfn_map)Xen PV-IOMMU Architecture
=========================
The Xen architecture consists of a new hypercall interface and changes to the grant map interface.
The existing IOMMU mappings setup at domain creation time will be preserved so that PV domains unaware of this feature will continue to function with no changes required.
Memory ballooning will be supported by taking an additional reference on the MFN backing the GFN for each successful IOMMU mapping created.
An M2B tracking structure will be used to ensure all references to an MFN can be located efficiently.
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_t do_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)
First argument, guest handle pointer to array of struct pv_iommu_op
Second argument, unsigned integer count of struct pv_iommu_op elements in array.
Definition of struct pv_iommu_op:
struct pv_iommu_op {
uint16_t subop_id;
uint16_t flags;
int32_t status;
union {
struct {
uint64_t bfn;
uint64_t gfn;
} map_page;
struct {
uint64_t bfn;
} unmap_page;
struct {
uint64_t bfn;
uint64_t gfn;
uint16_t domid;
ioservid_t ioserver;
} map_foreign_page;
struct {
uint64_t bfn;
uint64_t gfn;
uint16_t domid;
ioservid_t ioserver;
} lookup_foreign_page;
struct {
uint64_t bfn;
ioservid_t ioserver;
} unmap_foreign_page;
} u;
};Definition of PV IOMMU subops:
#define IOMMUOP_query_caps 1
#define IOMMUOP_map_page 2
#define IOMMUOP_unmap_page 3
#define IOMMUOP_map_foreign_page 4
#define IOMMUOP_lookup_foreign_page 5
#define IOMMUOP_unmap_foreign_page 6Design considerations for hypercall op
-------------------------------------------
IOMMU map/unmap operations can be slow and can involve flushing the IOMMU TLB to ensure the I/O device uses the updated mappings.
The op has been designed to take an array of operations and a count as parameters. This allows for easily implemented hypercall continuations to be used and allows for batches of IOMMU operations to be submitted before flushing the IOMMU TLB.
The subop_id to be used for a particular element is encoded into the element itself. This allows for map and unmap operations to be performed in one hypercall and for the IOMMU TLB flushing optimisations to be still applied.
The hypercall will ensure that the required IOMMU TLB flushes are applied before returning to guest via either hypercall completion or a hypercall continuation.
IOMMUOP_query_caps
------------------
This subop queries the runtime capabilities of the PV-IOMMU interface for the specific calling domain. This subop uses struct pv_iommu_op directly.
------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `flags` [out] This field details the IOMMUOP capabilities.
status [out] Status of this op, op specific values listed below
------------------------------------------------------------------------------
Defined bits for flags field:
------------------------------------------------------------------------------
Name Bit Definition
---- ------ ----------------------------------IOMMU_QUERY_map_cap 0 IOMMUOP_map_page or IOMMUOP_map_foreign
can be used for the calling domain
IOMMU_QUERY_map_all_mfns 1 IOMMUOP_map_page subop can map any MFN
not used by Xen
Reserved for future use 2-9 n/a
IOMMU_page_order 10-15 Returns maximum possible page order for
all other IOMMUOP subops
------------------------------------------------------------------------------
Defined values for query_caps subop status field:
Value Reason ------ ---------------------------------------------------------- 0 subop successfully returned
IOMMUOP_map_page
----------------------
This subop uses struct map_page part of the struct pv_iommu_op.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs except for Xen owned MFNs else the hardware domain will only be allowed to map GFNs which it owns.
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs without taking a reference to the MFN backing the GFN by setting the IOMMU_MAP_OP_no_ref_cnt flag.
Every successful pv_iommu_op will result in an additional page reference being taken on the MFN backing the GFN except for the condition detailed above.
If the map_op flags indicate a writeable mapping is required then a writeable page type reference will be taken otherwise a standard page reference will be taken.
All the following conditions are required to be true for PV IOMMU map subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. If hardware_domain and the following Xen IOMMU options are NOT enabled: dom0-passthrough
This subop usage of the struct pv_iommu_op and struct map_page fields are detailed below:
------------------------------------------------------------------------------
Field Purpose ----- --------------------------------------------------------------- `bfn` [in] Bus address frame number(BFN) to be mapped to specified gfn
below
gfn [in] Guest address frame number for DOMID_SELF
flags [in] Flags for signalling type of IOMMU mapping to be created,
Flags can be combined.
status [out] Mapping status of this op, op specific values listed below
------------------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMU_OP_readable 0 Create readable IOMMU mapping IOMMU_OP_writeable 1 Create writeable IOMMU mapping IOMMU_MAP_OP_no_ref_cnt 2 IOMMU mapping does not take a reference to
MFN backing BFN mappingIOMMU_MAP_OP_add_m2b 3 Wildcard M2B mapping added for
lookup_foreign_page to use
Reserved for future use 4-9 n/aIOMMU_page_order 10-15 Page order to be used for both gfn and bfn
Defined values for map_page subop status field:
Value Reason ------ ---------------------------------------------------------------------- 0 subop successfully returned -EIO IOMMU unit returned error when attempting to map BFN to GFN. -EPERM GFN could not be mapped because the GFN belongs to Xen. -EPERM Domain is not the hardware domain and GFN does not belong to domain -EPERM Domain is the hardware domain, IOMMU dom-strict mode is enabled and
GFN does not belong to domain-EACCES BFN address conflicts with RMRR regions for devices attached to
DOMID_SELF-ENOSPC Page order is too large for either BFN, GFN or IOMMU unit
IOMMUOP_unmap_page
------------------
This subop uses struct unmap_page part of the struct pv_iommu_op.
The subop usage of the struct pv_iommu_op and struct unmap_page fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
flags [in] Flags for signalling page order of unmap operation
status [out] Mapping status of this unmap operation, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------IOMMU_UNMAP_OP_remove_m2b 0 Wildcard M2B mapping removed for
lookup_foreign_page use
Reserved for future use 1-9 n/aIOMMU_page_order 10-15 Page order to be used for bfn
Defined values for unmap_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned -EIO IOMMU unit returned error when attempting to unmap BFN. -ENOSPC Page order is too large for either BFN address or IOMMU unit
------------------------------------------------------------------------
IOMMUOP_map_foreign_page
------------------------
This subop uses struct map_foreign_page part of the struct pv_iommu_op.
It is not valid to use a domid representing the calling domain.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
The M2B mechanism is an MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
All the following conditions are required to be true for PV IOMMU map_foreign subop to succeed:
1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is the hardware_domain and the following Xen IOMMU options are NOT enabled: dom0-passthrough
This subop usage of the struct pv_iommu_op and struct map_foreign_page fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain id for which the gfn field applies
ioserver [in] IOREQ server id associated with mapping
bfn [in] Bus address frame number for gfn address
gfn [in] Guest address frame number
flags [in] Details the status of the BFN mapping
status [out] status of this subop, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 BFN IOMMU mapping is readable IOMMUOP_writeable 1 BFN IOMMU mapping is writeable IOMMUOP_swap_mfn 2 BFN IOMMU mapping can be safely
swapped to scratch page
Reserved for future use 3-9 Reserved flag bits should be 0IOMMU_page_order 10-15 Page order to be used for both gfn and bfn
Defined values for map_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned
-EIO IOMMU unit returned error when attempting to map BFN to GFN.
-EPERM Calling domain does not have sufficient privilege over domid
-EPERM GFN could not be mapped because the GFN belongs to Xen.
-EPERM domid maps to DOMID_SELF
-EACCES BFN address conflicts with RMRR regions for devices attached to
DOMID_SELF
-ENODEV Provided ioserver id is not valid
-ENXIO Provided domid id is not valid
-ENXIO Provided GFN address is not valid
-ENOSPC Page order is too large for either BFN, GFN or IOMMU unitIOMMU_lookup_foreign_page
-------------------------
This subop uses struct lookup_foreign_page part of the struct pv_iommu_op.
This subop lookups up a BFN mapping for a ioserver + gfn + target domid combination.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
If a 1:1 mapping exists of BFN to MFN then a M2B entry is added and a reference is taken to the underlying MFN. If an existing mapping is present then the BFN is returned and no additional reference's will be taken to the underlying MFN.
A 1:1 mapping will exist if there is no IOMMU support or if the PV hardware domain was booted in dom0-relaxed mode or in dom0-passthrough mode.
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
This subop usage of the struct pv_iommu_op and struct lookup_foreign_page fields are detailed below:
--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `domid` [in] The domain id for which the gfn field applies
ioserver [in] IOREQ server id associated with mapping
bfn [out] Bus address frame number for gfn address
gfn [in] Guest address frame number
flags [out] Details the status of the BFN mapping
status [out] status of this subop, 0 indicates success
--------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition
---- ----- ---------------------------------- IOMMUOP_readable 0 Returned BFN IOMMU mapping is readable IOMMUOP_writeable 1 Returned BFN IOMMU mapping is writeable Reserved for future use 2-9 Reserved flag bits should be 0 IOMMU_page_order 10-15 Returns maximum possible page order for
all other IOMMUOP subops
Defined values for lookup_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned -EPERM Calling domain does not have sufficient privilege over domid -ENOENT There is no available BFN for provided GFN + domid combination -ENODEV Provided ioserver id is not valid -ENXIO Provided domid id is not valid -ENXIO Provided GFN address is not valid
IOMMUOP_unmap_foreign_page
--------------------------
This subop uses struct unmap_foreign_page part of the struct pv_iommu_op.
It only allows BFNs acquired via IOMMUOP_map_foreign_page or IOMMUOP_lookup_page to be unmapped. If an attempt is made to unmap a BFN mapped via IOMMUOP_map_page then the subop will fail.
The subop will perform a B2M lookup (IO page table walk) for the calling domain and then index the M2B using the returned MFN. This is safe because a particular BFN mapping can only map to one MFN for a particular calling domain.
This subop usage of the struct pv_iommu_op and struct unmap_foreign_page fields are detailed below:
-----------------------------------------------------------------------
Field Purpose ----- -------------------------------------------------------- `ioserver` [in] IOREQ server id associated with mapping
bfn [in] Bus address frame number for gfn address
flags [in] Flags for signalling page order of unmap operation
status [out] status of this subop, 0 indicates success
-----------------------------------------------------------------------
Defined bits for flags field:
Name Bit Definition ---- ----- ---------------------------------- Reserved for future use 0-9 n/a IOMMU_page_order 10-15 Page order to be used for bfn unmapping
Defined values for unmap_foreign_page subop status field:
Error code Reason
---------- ------------------------------------------------------------
0 subop successfully returned-ENOENT An M2B entry was not found for the specified input parameters.
Linux kernel architecture
=========================
The Linux kernel will use the PV-IOMMU hypercalls to map its PFN address space into the IOMMU. It will map the PFNs to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
The native SWIOTLB will be used to handle devices which cannot DMA to all of the kernel's PFN address space.
An interface shall be provided for emulator usage of IOMMUOP_*_foreign_page subops which will allow the Linux kernel to centrally manage that domain's BFN resource and ensure there are no unexpected conflicts.
Kernel Map Foreign GFN to BFN interface
---------------------------------------
An array of 'count' of 'struct pv_iommu_ops' will be passed to the mapping function.
int map_foreign_gfn_to_bfn(int count, struct pv_iommu_op *ops)
The calling function will use the struct map_foreign_page inside the struct pv_iommu_op and will fill in the domid, gfn and ioserver_id fields.
The kernel function will reuse the passed in struct pv_iommu_op for the hypercall and will set the subop_id field based on the IOMMU_QUERY_map_cap capability.
If the IOMMU_QUERY_map_cap is set then the kernel will allocate a suitable BFN address, set the BFN field in the op to this address and set the subop_id to IOMMUOP_map_page. It will do this on all 'ops' and then issue the hypercall.
If the IOMMU_QUERY_map_cap is NOT set then the kernel will set the subops_id to IOMMUOP_lookup_page on all ops and then issue the hypercall.
The calling function should check the status field in each op and if the status field is 0 then it can use the returned BFN address in each op.
Kernel Unmap Foreign GFN to BFN interface
-----------------------------------------
An array of 'count' of 'struct pv_iommu_ops' will be passed the mapping function.
int unmap_foreign_gfn_to_bfn(int count, struct pv_iommu_op *ops)
The calling function will use the struct unmap_foreign_page inside the struct pv_iommu_op and will fill in the bfn field.
The kernel function will set the subop_id field to IOMMUOP_unmap_foreign_page in each op and then issue the hypercall.
The calling function should check the status field in each op and if the status field is 0 then the BFN has been successfully unmapped.
Re: Xen PV IOMMU interface draft D
Quoted 2 lines
From: Malcolm Crossley [mailto:[email protected]]
Sent: Wednesday, February 10, 2016 6:09 PM
As Konrad commented, it's better to add this doc as 1st patch in your series then it's easier to review it with other patches together. Also it's always good to include such design doc in the repo.
Other comments embedded.
[...]
Quoted 4 lines
Clarification of GFN and BFN fields for different guest types
-------------------------------------------------------------
[...]
Quoted 5 lines
Bus Frame Numbers (BFN) refer to the address presented on the physical bus before being translated by the IOMMU.
Diagram below details memory accesses originating from physical device.
Quoted 9 lines
Physical Device
|
(BFN)
|
IOMMU-PT
|
(MFN)
|
RAMCurious what IOMMU-'PT' means here?
[...]
Quoted 12 lines
General principles for PV IOMMU interface
=========================================There are two different usage models for the BFN address space of a calling guest based upon the two purposes specified in the section above.
A calling guest may use their BFN address space for only one of the purposes detailed above and so the PV IOMMU interface has a subop per usage model. Furthermore, the IOMMU mapping of foreign domains memory is more complex than IOMMU mapping local domain memory and seperating the subops allows for the complexity to be split in the implementation.
Quoted 6 lines
The PV IOMMU design allows the calling domain to control it's BFN memory map. Thus the design also assigns the responsiblity of ensuring a BFN address mapped for local domain memory mappings are not reused for foreign domain memory mappings without an explict unmap of BFN address first. This simplifies the usage of the API and the extra overhead for the calling domains should be minimal as they should be already tracking the BFN address space usage already.
It might be clearer if you can add a separate section for BFN itself, i.e. how it is managed/allocated in different scenarios. I know most info is already provided in this text, but not centralized so far. :-)
Quoted 4 lines
Emulator usage of PV IOMMU interface
====================================
I'd suggest moving this and later sections to behind basic API introduction. Otherwise insufficient background on so many API references at this point.
Quoted 9 lines
Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
A
IOMMUOP_query_capssubop will return theIOMMU_QUERY_map_capflag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via theIOMMUOP_map_foreign_pagesubop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via theIOMMUOP_lookup_foreign_page.
IOMMU_QUERY_map_cap is a bit confusing here. Above paragraph is about whether emulator is allowed to allocate/specify BFN itself. However this capability name is more read as whether the calling domain can map foreign pages which is actually true regardless of how BFN is allocated.
Quoted 5 lines
Operating systems which use the
IOMMUOP_map_pagesubop are expected to provide a common interface for emulators to use. Otherwise emulators will not be aware of existing BFN mappings created by operating system and will get failed subops due to conflicts in the BFN address space for the domain.
Do you mean that emulator needs to detect whether OS is using IOMMUOP_map_page? If yes, then emulator calls a common interface provided by OS. If not, then emulator just directly invoke raw IOMMUOP itself. I'm not certain whether there is common mechanism to detect this so far. Could you elaborate your thought here?
Quoted 4 lines
Emulators should unmap unused GFN mappings as often as possible using
IOMMUOP_unmap_foreign_pagesubops so that guest domains can balloon pages quickly and efficiently.
Following earlier analysis then this only applies when OS doesn't use IOMMUOP. Otherwise emulator needs call a 'OS common interface' right?
Quoted 21 lines
Emulators should conform to the ballooning behaviour described section "IOMMUOP_*_foreign_page interactions with guest domain ballooning" so that guest domains are able to effectively balloon out and in memory.
Emulators must unmap any active BFN mappings when they shutdown.
IOMMUOP_*_foreign_page interactions with guest domain ballooning
=====================================================
===========Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered I/O request of type
IOREQ_TYPE_INVALIDATEto the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered I/O request ring is full then a standard (synchronous) I/O request of typeIOREQ_TYPE_INVALIDATEwill be issued to the affected IOREQ server the with just invalidated BFN address in the data field.
Quoted 4 lines
The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
Is it a real problem? Today for PCI passthru, what will happen if guest programs assigned device with a bad GPA which is not mapped in IOMMU? I think IOMMU fault should be fine, and we can just leverage existing IOMMU fault handling after the fault is triggered.
Quoted 12 lines
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a
IOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.For hosts with IOMMU support:
If the BFN was mapped without theIOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagethen the affected affected emulator(s) must specifically issue aIOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed.
Quoted 5 lines
If the BFN was mapped with the
IOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagesubop for all emulators with mappings of that GFN then the BFN mapping will be swapped to point at a scratch MFN page and all BFN references to the invalid MFN will be removed by Xen after the BFN mapping has been updated to point at the scratch MFN page.
I don't understand why for 'swap' case you don't need emulator to do explicit unmap. You can think 'noswap' (page-A to invalid) as a special example of 'swap' (page-A to scratch page), since they both move away from page-A reference. If there is a reason that emulator needs to do some cleanup internally before dropping the reference, does 'swap_mfn' breaks that situation then?
Quoted 8 lines
The rationale for swapping the BFN mapping to point at scratch pages is to enable guest domains to balloon quickly without requiring hypercall(s) from emulators.
Not all BFN mappings can be swapped without potentially causing problems for the hardware itself (command rings etc.) so the
IOMMUOP_swap_mfnflag is used to allow per BFN control of Xen ballooning behaviour.
Who will judge whether a BFN mapping can be swapped then?
[...]
Quoted 40 lines
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_tdo_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)First argument, guest handle pointer to array of
struct pv_iommu_opSecond argument, unsigned integer count of
struct pv_iommu_opelements in array.Definition of
struct pv_iommu_op:struct pv_iommu_op { uint16_t subop_id; uint16_t flags; int32_t status; union { struct { uint64_t bfn; uint64_t gfn; } map_page; struct { uint64_t bfn; } unmap_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver; } map_foreign_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver;
Quoted 8 lines
} lookup_foreign_page;
struct {
uint64_t bfn;
ioservid_t ioserver;
} unmap_foreign_page;
} u;
};Do we really need such ioserver ID here? Could it be simple as looping all ioreq servers with INVALIDATE notifications?
[...]
Quoted 8 lines
IOMMUOP_map_page
----------------------
This subop usesstruct map_pagepart of thestruct pv_iommu_op.If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs except for Xen owned MFNs else the hardware domain will only be allowed to map GFNs which it owns.
"map all GFNs" -> "map all MFNs" since you use "except for Xen owned MFNs" later. Since you have a capability called IOMMU_QUERY_map_all_mfns, should you add such condition in above description?
Quoted 4 lines
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs without taking a reference to the MFN backing the GFN by setting the
IOMMU_MAP_OP_no_ref_cntflag.
could you elaborate when no_ref_cnt is required?
[...]
Quoted 18 lines
IOMMUOP_unmap_page
------------------
This subop usesstruct unmap_pagepart of thestruct pv_iommu_op.The subop usage of the
struct pv_iommu_opandstruct unmap_pagefields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
flags[in] Flags for signalling page order of unmap operation
status[out] Mapping status of this unmap operation, 0 indicates success
--------------------------------------------------------------------
Quoted 6 lines
Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------IOMMU_UNMAP_OP_remove_m2b0 Wildcard M2B mapping removed for
lookup_foreign_pageuse
Is it explicitly required? Should it be implicit as long as a valid M2B entry existing?
[...]
Quoted 5 lines
IOMMUOP_map_foreign_page
------------------------
This subop usesstruct map_foreign_pagepart of thestruct pv_iommu_op.It is not valid to use a domid representing the calling domain.
Then what's being used here to represent the calling domain?
Quoted 3 lines
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
How is this privilege check being done? Is there existing mechanism, or something new to add?
Quoted 8 lines
The M2B mechanism is an MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
Quoted 7 lines
All the following conditions are required to be true for PV IOMMU
map_foreignsubop to succeed:1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is thehardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthrough
4. the domain has sufficient privilege over the specified domid;
[...]
Quoted 5 lines
IOMMU_lookup_foreign_page
-------------------------
This subop usesstruct lookup_foreign_pagepart of thestruct pv_iommu_op.
Quoted 8 lines
This subop lookups up a BFN mapping for a ioserver + gfn + target domid combination.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
If a 1:1 mapping exists of BFN to MFN then a M2B entry is added and a reference is taken to the underlying MFN. If an existing mapping is present
Then when will this very reference be dropped?
Quoted 5 lines
then the BFN is returned and no additional reference's will be taken to the underlying MFN.
A 1:1 mapping will exist if there is no IOMMU support or if the PV hardware domain was booted in dom0-relaxed mode or in dom0-passthrough mode.
what about hardware domain using IOMMUOPS in the meantime? In that case, from your earlier description it's hardware domain to manage BFN addr space, while here 1:1 mapping is some hard assumption in hypervisor, so two things together may conflict. There needs to be a mechanism that once Xen sees any explicit BFN passed from hardware domain, then such 1:1 mapping scheme should be disabled.
Quoted 4 lines
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
[...]
Quoted 9 lines
Linux kernel architecture
=========================The Linux kernel will use the PV-IOMMU hypercalls to map its PFN address space into the IOMMU. It will map the PFNs to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
Quoted 6 lines
The native SWIOTLB will be used to handle devices which cannot DMA to all of the kernel's PFN address space.
An interface shall be provided for emulator usage of IOMMUOP_*_foreign_page subops which will allow the Linux kernel to centrally manage that domain's BFN resource and ensure there are no unexpected conflicts.
One open here. When IOMMU is enabled, there is supposed to be a IOVA space created in Linux kernel. How does this BFN space play with that one?
Thanks
Kevin
Re: Xen PV IOMMU interface draft D
In reply to Malcolm CrossleyQuoted 17 lines
Quoted 1 line
Quoted 1 line · On 10.02.16 at 11:09, <[email protected]> wrote:
% Xen PV IOMMU interface
% Malcolm Crossley <<[email protected]>>
Paul Durrant <<[email protected]>>
% Draft DIntroduction
============Revision History
------------------------------------------------------------------------------------
Version Date Changes ------- ----------- ---------------------------------------------- Draft A 10 Apr 2014 Initial draft.
Quoted 5 lines
Draft B 12 Jun 2015 Second draft.
Draft C 26 Jun 2015 Third draft.
Draft D 09 Feb 2016 Fourth draft.
Unless this is a complete re-write, I'd really like to avoid needing to read through all of it again. Do you perhaps have a PDF version somewhere with change marks?
Jan
Re: Xen PV IOMMU interface draft D
Quoted 19 lines · Jan Beulich
Quoted 19 lines
Quoted 1 line
Quoted 1 line · On 10.02.16 at 11:09, <[email protected]> wrote:
% Xen PV IOMMU interface
% Malcolm Crossley <<[email protected]>>
Paul Durrant <<[email protected]>>
% Draft DIntroduction
============Revision History
------------------------------------------------------------------------------------
Version Date Changes ------- ----------- ---------------------------------------------- Draft A 10 Apr 2014 Initial draft.Draft B 12 Jun 2015 Second draft.
Quoted 8 lines
Quoted 3 lines
Draft C 26 Jun 2015 Third draft.
Draft D 09 Feb 2016 Fourth draft.
Unless this is a complete re-write, I'd really like to avoid needing to read through all of it again. Do you perhaps have a PDF version somewhere with change marks?
It was really a minor update to ensure the design matched the RFC implementation. The diff for the draft D is below. For the next revision I will details what has changed.
designs/pv-iommu-control/design.txt +9 -3
@@ -1,7 +1,7 @@ % Xen PV IOMMU interface % Malcolm Crossley <<[email protected]>> Paul Durrant <<[email protected]>> -% Draft C +% Draft D Introduction ============
@@ -17,6 +17,8 @@ Draft A 10 Apr 2014 Initial draft. Draft B 12 Jun 2015 Second draft. Draft C 26 Jun 2015 Third draft. + +Draft D 09 Feb 2016 Fourth draft. -------------------------------------------------------------------- Background
@@ -481,7 +483,9 @@ IOMMU_OP_readable 0 Cr IOMMU_OP_writeable 1 Create writeable IOMMU mapping IOMMU_MAP_OP_no_ref_cnt 2 IOMMU mapping does not take a reference to MFN backing BFN mapping -Reserved for future use 3-9 n/a +IOMMU_MAP_OP_add_m2b 3 Wildcard M2B mapping added for + lookup_foreign_page to use +Reserved for future use 4-9 n/a IOMMU_page_order 10-15 Page order to be used for both gfn and bfn Defined values for map_page subop status field:
@@ -519,7 +523,9 @@ Defined bits for flags field: Name Bit Definition ---- ----- ---------------------------------- -Reserved for future use 0-9 n/a +IOMMU_UNMAP_OP_remove_m2b 0 Wildcard M2B mapping removed for + lookup_foreign_page use +Reserved for future use 1-9 n/a IOMMU_page_order 10-15 Page order to be used for bfn
Sorry about the hassle. I posted the RFC because I was would like to get some comments on the M2B implementation and the iommu_lookup_page code.
Malcolm
Quoted 2 lines
Jan
Re: Xen PV IOMMU interface draft D
In reply to Malcolm CrossleyHi, Malcolm,
Not sure whether I missed your reply or not, but failed to find it in my archive. Could you help re-post if you already did so? Sorry that my comments might be a bit late which didn't catch previous draft discussions, but some of below questions are really important to help us understand how this new interface works with XenGT...
Thanks
Kevin
Quoted 361 lines
From: Tian, Kevin
Sent: Thursday, February 18, 2016 4:21 PMQuoted 2 lines
From: Malcolm Crossley [mailto:[email protected]]
Sent: Wednesday, February 10, 2016 6:09 PMAs Konrad commented, it's better to add this doc as 1st patch in your series then it's easier to review it with other patches together. Also it's always good to include such design doc in the repo.
Other comments embedded.
[...]
Quoted 4 lines
Clarification of GFN and BFN fields for different guest types
-------------------------------------------------------------[...]
Quoted 5 lines
Bus Frame Numbers (BFN) refer to the address presented on the physical bus before being translated by the IOMMU.
Diagram below details memory accesses originating from physical device.
Quoted 9 lines
Physical Device | (BFN) | IOMMU-PT | (MFN) | RAMCurious what IOMMU-'PT' means here?
[...]
Quoted 12 lines
General principles for PV IOMMU interface
=========================================There are two different usage models for the BFN address space of a calling guest based upon the two purposes specified in the section above.
A calling guest may use their BFN address space for only one of the purposes detailed above and so the PV IOMMU interface has a subop per usage model. Furthermore, the IOMMU mapping of foreign domains memory is more complex than IOMMU mapping local domain memory and seperating the subops allows for the complexity to be split in the implementation.
Quoted 6 lines
The PV IOMMU design allows the calling domain to control it's BFN memory map. Thus the design also assigns the responsiblity of ensuring a BFN address mapped for local domain memory mappings are not reused for foreign domain memory mappings without an explict unmap of BFN address first. This simplifies the usage of the API and the extra overhead for the calling domains should be minimal as they should be already tracking the BFN address space usage already.
It might be clearer if you can add a separate section for BFN itself, i.e. how it is managed/allocated in different scenarios. I know most info is already provided in this text, but not centralized so far. :-)
Quoted 4 lines
Emulator usage of PV IOMMU interface
====================================I'd suggest moving this and later sections to behind basic API introduction. Otherwise insufficient background on so many API references at this point.
Quoted 9 lines
Emulators which require bus address mapping of guest RAM must first determine if it's possible for the domain to control the bus addresses themselves.
A
IOMMUOP_query_capssubop will return theIOMMU_QUERY_map_capflag. If this flag is set then the emulator may specify the BFN address it wishes guest RAM to be mapped to via theIOMMUOP_map_foreign_pagesubop. If the flag is not set then the emulator must use BFN addresses supplied by the Xen via theIOMMUOP_lookup_foreign_page.
IOMMU_QUERY_map_capis a bit confusing here. Above paragraph is about whether emulator is allowed to allocate/specify BFN itself. However this capability name is more read as whether the calling domain can map foreign pages which is actually true regardless of how BFN is allocated.Quoted 2 lines
Operating systems which use the
IOMMUOP_map_pagesubop are expected to providea
Quoted 3 lines
common interface for emulators to use. Otherwise emulators will not be aware of existing BFN mappings created by operating system and will get failed subops due to conflicts in the BFN address space for the domain.
Do you mean that emulator needs to detect whether OS is using
IOMMUOP_map_page? If yes, then emulator calls a common interface provided by OS. If not, then emulator just directly invoke raw IOMMUOP itself. I'm not certain whether there is common mechanism to detect this so far. Could you elaborate your thought here?Quoted 4 lines
Emulators should unmap unused GFN mappings as often as possible using
IOMMUOP_unmap_foreign_pagesubops so that guest domains can balloon pages quickly and efficiently.Following earlier analysis then this only applies when OS doesn't use IOMMUOP. Otherwise emulator needs call a 'OS common interface' right?
Quoted 9 lines
Emulators should conform to the ballooning behaviour described section "IOMMUOP_*_foreign_page interactions with guest domain ballooning" so that guest domains are able to effectively balloon out and in memory.
Emulators must unmap any active BFN mappings when they shutdown.
IOMMUOP_*_foreign_page interactions with guest domain ballooning
=====================================================
Quoted 12 lines
===========
Guest domains can balloon out a set of GFN mappings at any time and render the BFN to GFN mapping invalid.
When a BFN to GFN mapping becomes invalid, Xen will issue a buffered I/O request of type
IOREQ_TYPE_INVALIDATEto the affected IOREQ servers with the now invalid BFN address in the data field. If the buffered I/O request ring is full then a standard (synchronous) I/O request of typeIOREQ_TYPE_INVALIDATEwill be issued to the affected IOREQ server the with just invalidated BFN address in the data field.Quoted 4 lines
The BFN mappings cannot be simply unmapped at the point of the balloon hypercall otherwise a malicious guest could specifically balloon out an in use GFN address in use by an emulator and trigger IOMMU faults for the domains with BFN mappings.
Is it a real problem? Today for PCI passthru, what will happen if guest programs assigned device with a bad GPA which is not mapped in IOMMU? I think IOMMU fault should be fine, and we can just leverage existing IOMMU fault handling after the fault is triggered.
Quoted 12 lines
For hosts with no IOMMU support: The affected emulator(s) must specifically issue a
IOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed and the MFN can be freed back to the Xen memory allocator.For hosts with IOMMU support:
If the BFN was mapped without theIOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagethen the affected affected emulator(s) must specifically issue aIOMMUOP_unmap_foreign_pagesubop for the now invalid BFN address so that the references to the underlying MFN are removed.Quoted 5 lines
If the BFN was mapped with the
IOMMUOP_swap_mfnflag set in theIOMMUOP_map_foreign_pagesubop for all emulators with mappings of that GFN then the BFN mapping will be swapped to point at a scratch MFN page and all BFN references to the invalid MFN will be removed by Xen after the BFN mapping has been updated to point at the scratch MFN page.I don't understand why for 'swap' case you don't need emulator to do explicit unmap. You can think 'noswap' (page-A to invalid) as a special example of 'swap' (page-A to scratch page), since they both move away from page-A reference. If there is a reason that emulator needs to do some cleanup internally before dropping the reference, does '
swap_mfn' breaks that situation then?Quoted 8 lines
The rationale for swapping the BFN mapping to point at scratch pages is to enable guest domains to balloon quickly without requiring hypercall(s) from emulators.
Not all BFN mappings can be swapped without potentially causing problems for the hardware itself (command rings etc.) so the
IOMMUOP_swap_mfnflag is used to allow per BFN control of Xen ballooning behaviour.Who will judge whether a BFN mapping can be swapped then?
[...]
Quoted 40 lines
Xen PV IOMMU hypercall interface
--------------------------------
A two argument hypercall interface (do_iommu_op).
ret_tdo_iommu_op(XEN_GUEST_HANDLE_PARAM(void) arg, unsigned int count)First argument, guest handle pointer to array of
struct pv_iommu_opSecond argument, unsigned integer count of
struct pv_iommu_opelements in array.Definition of
struct pv_iommu_op:struct pv_iommu_op { uint16_t subop_id; uint16_t flags; int32_t status; union { struct { uint64_t bfn; uint64_t gfn; } map_page; struct { uint64_t bfn; } unmap_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver; } map_foreign_page; struct { uint64_t bfn; uint64_t gfn; uint16_t domid; ioservid_t ioserver;Quoted 8 lines
} lookup_foreign_page; struct { uint64_t bfn; ioservid_t ioserver; } unmap_foreign_page; } u; };Do we really need such ioserver ID here? Could it be simple as looping all ioreq servers with INVALIDATE notifications?
[...]
Quoted 8 lines
IOMMUOP_map_page
----------------------
This subop usesstruct map_pagepart of thestruct pv_iommu_op.If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs except for Xen owned MFNs else the hardware domain will only be allowed to map GFNs which it owns.
"map all GFNs" -> "map all MFNs" since you use "except for Xen owned MFNs" later. Since you have a capability called
IOMMU_QUERY_map_all_mfns, should you add such condition in above description?Quoted 4 lines
If IOMMU dom0-strict mode is NOT enabled then the hardware domain will be allowed to map all GFNs without taking a reference to the MFN backing the GFN by setting the
IOMMU_MAP_OP_no_ref_cntflag.could you elaborate when
no_ref_cntis required?[...]
Quoted 18 lines
IOMMUOP_unmap_page
------------------
This subop usesstruct unmap_pagepart of thestruct pv_iommu_op.The subop usage of the
struct pv_iommu_opandstruct unmap_pagefields are detailed below:--------------------------------------------------------------------
Field Purpose ----- ----------------------------------------------------- `bfn` [in] Bus address frame number to be unmapped in DOMID_SELF
flags[in] Flags for signalling page order of unmap operation
status[out] Mapping status of this unmap operation, 0 indicates success
--------------------------------------------------------------------Quoted 6 lines
Defined bits for flags field:
Name Bit Definition
---- ----- ----------------------------------IOMMU_UNMAP_OP_remove_m2b0 Wildcard M2B mapping removed for
lookup_foreign_pageuseIs it explicitly required? Should it be implicit as long as a valid M2B entry existing?
[...]
Quoted 5 lines
IOMMUOP_map_foreign_page
------------------------
This subop usesstruct map_foreign_pagepart of thestruct pv_iommu_op.It is not valid to use a domid representing the calling domain.
Then what's being used here to represent the calling domain?
Quoted 3 lines
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
How is this privilege check being done? Is there existing mechanism, or something new to add?
Quoted 8 lines
The M2B mechanism is an MFN to (BFN,domid,ioserver) tuple.
Each successful subop will add to the M2B if there was not an existing identical M2B entry.
Every new M2B entry will take a reference to the MFN backing the GFN.
Quoted 7 lines
All the following conditions are required to be true for PV IOMMU
map_foreignsubop to succeed:1. IOMMU detected and supported by Xen
2. The domain has IOMMU controlled hardware allocated to it
3. The domain is thehardware_domainand the following Xen IOMMU options are NOT enabled: dom0-passthrough4. the domain has sufficient privilege over the specified domid;
[...]
Quoted 5 lines
IOMMU_lookup_foreign_page
-------------------------
This subop usesstruct lookup_foreign_pagepart of thestruct pv_iommu_op.Quoted 8 lines
This subop lookups up a BFN mapping for a ioserver + gfn + target domid combination.
The hypercall will only succeed if calling domain has sufficient privilege over the specified domid.
If a 1:1 mapping exists of BFN to MFN then a M2B entry is added and a reference is taken to the underlying MFN. If an existing mapping is present
Then when will this very reference be dropped?
Quoted 5 lines
then the BFN is returned and no additional reference's will be taken to the underlying MFN.
A 1:1 mapping will exist if there is no IOMMU support or if the PV hardware domain was booted in dom0-relaxed mode or in dom0-passthrough mode.
what about hardware domain using IOMMUOPS in the meantime? In that case, from your earlier description it's hardware domain to manage BFN addr space, while here 1:1 mapping is some hard assumption in hypervisor, so two things together may conflict. There needs to be a mechanism that once Xen sees any explicit BFN passed from hardware domain, then such 1:1 mapping scheme should be disabled.
Quoted 4 lines
If there is no IOMMU support then the MFN is returned in the BFN field (that is the only valid bus address for the GFN + domid combination).
[...]
Quoted 9 lines
Linux kernel architecture
=========================The Linux kernel will use the PV-IOMMU hypercalls to map its PFN address space into the IOMMU. It will map the PFNs to the IOMMU address space using a 1:1 mapping, it does this by programming a BFN to GFN mapping which matches the PFN to GFN mapping.
Quoted 6 lines
The native SWIOTLB will be used to handle devices which cannot DMA to all of the kernel's PFN address space.
An interface shall be provided for emulator usage of IOMMUOP_*_foreign_page subops which will allow the Linux kernel to centrally manage that domain's BFN resource and ensure there are no unexpected conflicts.
Quoted 6 lines
One open here. When IOMMU is enabled, there is supposed to be a IOVA space created in Linux kernel. How does this BFN space play with that one?
Thanks
Kevin
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel