amd_iommu: Honor DTE[IR] and DTE[IW] when DTE[Mode] is 0
cover amd_iommu: Honor DTE[IR] and DTE[IW] when DTE[Mode] is 0
This patch implements honoring of the IR and IW bits, which determine whether reads and writes are allowed, respectively, when passthrough mode (Mode = 0) is enabled for a device.
A testcase is also provided, which tests different settings of these bits. Moreover, I have tested that there are no problems with the existing page table walking and IOTLB code, by running a virtual machine Arch Linux instance with an AMD IOMMU and watching the trace_amdvi_translation_result tracepoint for the storage device, and compiling QEMU and the test with UBSAN and ASAN.
Daniel Paziyski (2):
amd_iommu: Honor DTE[IR] and DTE[IW] when DTE[Mode] is 0 tests/qtest: Add amd-iommu passthrough mode test
hw/i386/amd_iommu.c | 136 ++++++++++++++------- hw/i386/trace-events | 2 +- tests/qtest/amd-iommu-test.c | 230 +++++++++++++++++++++++++++++++++++ 3 files changed, 322 insertions(+), 46 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/2 amd_iommu: Honor DTE[IR] and DTE[IW] when DTE[Mode] is 0
In an AMD IOMMU's Device Table Entry (DTE), the Mode field specifies the number of levels that the page table hierarchy for translating Guest Physical Addresses (GPAs) to System Physical Addresses (SPAs) will have. When the DTE is valid and translation is active (V = 1, TV = 1), and Mode is equal to 0, then the IOMMU will act as a "filter": GPAs are the same as SPAs, but, whether reads or writes are allowed depends on the values of I/O read permission (IR) and I/O write permission (IW) in the DTE.
In the current code, the values of IR and IW are considered only when traversing the page tables for translating GPAs to SPAs when Mode is not 0 (and are ANDed with their counterparts in the page tables to determine the final permission), and when the DTE for a device is in passthrough mode (Mode is 0), IR and IW are not taken into consideration, and access is given unconditionally, which conflicts with the specification.
This patch implements honoring of the IR and IW DTE bits when Mode is 0. To do so, both the iommu and iommu_nodma memory regions in the AMDVIAddressSpace struct are preserved, and a new field is added, passthrough_perms, which along with addr_translation will determine whether the iommu or the iommu_nodma memory region will be used.
After a DTE invalidation, passthrough_perms and addr_translation will be set accordingly with the new DTE configuration, and when it's time to switch address spaces in amdvi_switch_address_space:
- Iff passthrough is desired and IR = 1 and IW = 1, use the iommu_nodma memory region,
- otherwise, use the iommu memory region.
The handler for translation (amdvi_translate), which is called when the iommu memory region is enabled, has a fastpath which handles passthrough mode and verifies permissions. Additionally, since the DTE is fetched in the page walker, and it is possible for it to have been updated concurrently, if passthrough mode is requested, it is handled properly. Moreover, care has been taken to not fill the IOTLB with entries handed out when passthrough is active.
Also, for the sake of easier debugging, the trace_amdvi_page_fault tracepoint now also takes the BDF of the device which has caused the page fault.
--- hw/i386/amd_iommu.c | 136 +++++++++++++++++++++++++++++-------------- hw/i386/trace-events | 2 +- 2 files changed, 92 insertions(+), 46 deletions(-)
hw/i386/amd_iommu.c +91 -45
@@ -54,6 +54,8 @@ struct AMDVIAddressSpace {IOVATree *iova_tree;/* DMA address translation active */ bool addr_translation; +/* Permissions for direct map when DMA address translation is not active */ +unsigned passthrough_perms; };/* AMDVI cache entry */
@@ -1162,34 +1164,16 @@ static void amdvi_switch_address_space(AMDVIAddressSpace *amdvi_as) {AMDVIState *s = amdvi_as->iommu_state; -if (s->dma_remap && amdvi_as->addr_translation) { -/* Enabling DMA region */ -memory_region_set_enabled (&amdvi_as->iommu_nodma, false); -memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), true); - }else { -/* Disabling DMA region, using passthrough */ -memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), false); -memory_region_set_enabled (&amdvi_as->iommu_nodma, true); +if (!(s->dma_remap && amdvi_as->addr_translation)) { +if (amdvi_as->passthrough_perms == (AMDVI_PERM_READ |AMDVI_PERM_WRITE )) { +memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), false); +memory_region_set_enabled (&amdvi_as->iommu_nodma, true); +return ; + } } -} - -/* -* For all existing address spaces managed by the IOMMU, enable/disable the -* corresponding memory regions to reset the address translation mode and -* use passthrough by default. -*/ -static void amdvi_reset_address_translation_all (AMDVIState *s) -{ -AMDVIAddressSpace *iommu_as; -GHashTableIter as_it; - -g_hash_table_iter_init (&as_it, s->address_spaces); -while (g_hash_table_iter_next (&as_it,NULL , (void **)&iommu_as)) { -/* Use passthrough as default mode after reset */ - iommu_as->addr_translation = false; -amdvi_switch_address_space (iommu_as); - } +memory_region_set_enabled (&amdvi_as->iommu_nodma, false); +memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), true); }static void enable_dma_mode (AMDVIAddressSpace *as,bool inval_current)
@@ -1222,23 +1206,43 @@ static void enable_dma_mode(AMDVIAddressSpace *as, bool inval_current)* - invalidate all existing mappings * - switch to no_dma memory region */ -static void enable_nodma_mode (AMDVIAddressSpace *as) +static void enable_nodma_mode (AMDVIAddressSpace *as,unsigned perms) {IOMMUNotifier *n; -if (!as->addr_translation) { +if (!as->addr_translation && as->passthrough_perms == perms) {/* passthrough is already active, nothing to do */ return ; } - as->addr_translation = false; IOMMU_NOTIFIER_FOREACH(n, &as->iommu) {/* Drop all mappings for the address space */ amdvi_address_space_unmap (as, n); } + + as->addr_translation = false; + as->passthrough_perms = perms;amdvi_switch_address_space (as); } +/* +* For all existing address spaces managed by the IOMMU, enable/disable the +* corresponding memory regions to reset the address translation mode and +* use passthrough by default. +*/ +static void amdvi_reset_address_translation_all (AMDVIState *s) +{ +AMDVIAddressSpace *iommu_as; +GHashTableIter as_it; + +g_hash_table_iter_init (&as_it, s->address_spaces); + +while (g_hash_table_iter_next (&as_it,NULL , (void **)&iommu_as)) { +/* Use passthrough as default mode after reset */ +enable_nodma_mode (iommu_as,AMDVI_PERM_READ |AMDVI_PERM_WRITE ); + } +} +/* * A guest driver must issue the INVALIDATE_DEVTAB_ENTRY command to the IOMMU * after changing a Device Table entry. We can use this fact to detect when a
@@ -1252,6 +1256,7 @@ static void amdvi_update_addr_translation_mode(AMDVIState *s, uint16_t devid)AMDVIAddressSpace *as;uint64_t dte[4 ] = {0 };int ret; +unsigned perms; as =amdvi_get_as_by_devid (s, devid);if (!as) {
@@ -1268,14 +1273,16 @@ static void amdvi_update_addr_translation_mode(AMDVIState *s, uint16_t devid)case 0 :/* DTE was successfully retrieved */ if (!dte_mode) { -enable_nodma_mode (as);/* DTE[V]=1 && DTE[Mode]=0 => passthrough */ +/* DTE[V]=1 && DTE[Mode]=0 => passthrough */ + perms =amdvi_get_perms (dte[0 ]); +enable_nodma_mode (as, perms); }else {enable_dma_mode (as, false);/* Enable DMA translation */ }break ;case -AMDVI_FR_DTE_V :/* DTE[V]=0, address is passed untranslated */ -enable_nodma_mode (as); +enable_nodma_mode (as,AMDVI_PERM_READ |AMDVI_PERM_WRITE );break ;case -AMDVI_FR_DTE_RTR_ERR :case -AMDVI_FR_DTE_TV :
@@ -1889,15 +1896,45 @@ static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val, } } +static void amdvi_fill_iotlb_entry_passthrough (IOMMUTLBEntry *entry, +AMDVIAddressSpace *as, +unsigned passthrough_perms, +hwaddr addr, +IOMMUAccessFlags flag) +{ +unsigned perms =0 ; + +if (flag &IOMMU_RO ) { + perms |=AMDVI_PERM_READ ; + } +if (flag &IOMMU_WO ) { + perms |=AMDVI_PERM_WRITE ; + } + +if ((passthrough_perms & perms) != perms) { +amdvi_page_fault (as->iommu_state, as->devfn, addr, perms); +trace_amdvi_page_fault (addr,pci_bus_num (as->bus),PCI_SLOT (as->devfn), +PCI_FUNC (as->devfn), perms); +return ; + } + + entry->iova = addr &AMDVI_PAGE_MASK_4K ; + entry->translated_addr = addr &AMDVI_PAGE_MASK_4K ; + entry->addr_mask = ~AMDVI_PAGE_MASK_4K ; + entry->perm = flag &IOMMU_RW ; +} +static void amdvi_page_walk (AMDVIAddressSpace *as,uint64_t *dte,IOMMUTLBEntry *ret,unsigned perms, -hwaddr addr) +hwaddr addr,bool *update_iotlb) {hwaddr page_mask, pagesize =0 ;uint8_t mode;uint64_t pte;int fetch_ret; + *update_iotlb = false; +/* make sure the DTE has TV = 1 */ if (!(dte[0 ] &AMDVI_DEV_TRANSLATION_VALID )) {/*
@@ -1916,7 +1953,9 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,return ; }if (mode ==0 ) { - goto no_remap; +amdvi_fill_iotlb_entry_passthrough (ret, as,amdvi_get_perms (dte[0 ]), + addr, perms ==AMDVI_PERM_WRITE ?IOMMU_WO :IOMMU_RO ); +return ; }/* Attempt to fetch the PTE to determine if a valid mapping exists */
@@ -1931,7 +1970,8 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte, perms != (perms & amdvi_get_perms(pte))) {amdvi_page_fault (as->iommu_state, as->devfn, addr, perms); -trace_amdvi_page_fault (addr); +trace_amdvi_page_fault (addr,pci_bus_num (as->bus),PCI_SLOT (as->devfn), +PCI_FUNC (as->devfn), perms);return ; }
@@ -1944,13 +1984,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte, ret->translated_addr = (pte &AMDVI_DEV_PT_ROOT_MASK ) & page_mask; ret->addr_mask = ~page_mask; ret->perm =amdvi_get_perms (pte); -return ; - -no_remap: - ret->iova = addr &AMDVI_PAGE_MASK_4K ; - ret->translated_addr = addr &AMDVI_PAGE_MASK_4K ; - ret->addr_mask = ~AMDVI_PAGE_MASK_4K ; - ret->perm =amdvi_get_perms (dte[0 ]); + *update_iotlb = true; } static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,
@@ -1961,6 +1995,7 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,AMDVIIOTLBEntry *iotlb_entry =amdvi_iotlb_lookup (s, addr, devid);uint64_t entry[4 ];int dte_ret; +bool update_iotlb;if (iotlb_entry) { trace_amdvi_iotlb_hit(PCI_BUS_NUM(devid), PCI_SLOT(devid),
@@ -1983,10 +2018,14 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr, }amdvi_page_walk (as, entry, ret, - is_write ?AMDVI_PERM_WRITE :AMDVI_PERM_READ , addr); + is_write ?AMDVI_PERM_WRITE :AMDVI_PERM_READ , addr, + &update_iotlb); + +if (update_iotlb) { +amdvi_update_iotlb (s, devid, addr, *ret, + entry[1 ] &AMDVI_DEV_DOMID_ID_MASK ); + } -amdvi_update_iotlb (s, devid, addr, *ret, - entry[1 ] &AMDVI_DEV_DOMID_ID_MASK );return ; out:
@@ -2031,7 +2070,13 @@ static IOMMUTLBEntry amdvi_translate(IOMMUMemoryRegion *iommu, hwaddr addr,return ret; } -amdvi_do_translate (as, addr, flag &IOMMU_WO , &ret); +if (!as->addr_translation) { +amdvi_fill_iotlb_entry_passthrough (&ret, as, as->passthrough_perms, + addr, flag); + }else { +amdvi_do_translate (as, addr, flag &IOMMU_WO , &ret); + } +trace_amdvi_translation_result (pci_bus_num (as->bus),PCI_SLOT (as->devfn),PCI_FUNC (as->devfn), addr, ret.translated_addr);return ret;
@@ -2423,6 +2468,7 @@ static AddressSpace *amdvi_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) amdvi_dev_as->notifier_flags =IOMMU_NOTIFIER_NONE ; amdvi_dev_as->iova_tree =iova_tree_new (); amdvi_dev_as->addr_translation = false; + amdvi_dev_as->passthrough_perms =AMDVI_PERM_READ |AMDVI_PERM_WRITE ; key->bus = bus; key->devfn = devfn;
hw/i386/trace-events +1 -1
@@ -103,7 +103,7 @@ amdvi_dte_get_fail(uint64_t addr, uint32_t offset) "error: failed to access Devi amdvi_invalid_dte(uint64_t addr) "PTE entry at 0x%"PRIx64" is invalid " amdvi_get_pte_hwerror(uint64_t addr) "hardware error eccessing PTE at addr 0x%"PRIx64 amdvi_mode_invalid(uint8_t level, uint64_t addr)"error: translation level 0x%"PRIx8" translating addr 0x%"PRIx64 -amdvi_page_fault(uint64_t addr) "error: page fault accessing guest physical address 0x%"PRIx64 +amdvi_page_fault(uint64_t addr, uint8_t bus, uint8_t slot, uint8_t func, unsigned perms) "error: page fault accessing guest physical address 0x%"PRIx64" by devid %02x:%02x.%x with perms 0x%x" amdvi_iotlb_hit(uint8_t bus, uint8_t slot, uint8_t func, uint64_t addr, uint64_t txaddr) "hit iotlb devid %02x:%02x.%x gpa 0x%"PRIx64" hpa 0x%"PRIx64 amdvi_translation_result(uint8_t bus, uint8_t slot, uint8_t func, uint64_t addr, uint64_t txaddr) "devid: %02x:%02x.%x gpa 0x%"PRIx64" hpa 0x%"PRIx64 amdvi_mem_ir_write_req(uint64_t addr, uint64_t val, uint32_t size) "addr 0x%"PRIx64" data 0x%"PRIx64" size 0x%"PRIx32
Signature · 1 line
2.55.0
Hi Daniel,
Quoted 28 lines · Daniel Paziyski
In an AMD IOMMU's Device Table Entry (DTE), the Mode field specifies the number of levels that the page table hierarchy for translating Guest Physical Addresses (GPAs) to System Physical Addresses (SPAs) will have. When the DTE is valid and translation is active (V = 1, TV = 1), and Mode is equal to 0, then the IOMMU will act as a "filter": GPAs are the same as SPAs, but, whether reads or writes are allowed depends on the values of I/O read permission (IR) and I/O write permission (IW) in the DTE.
In the current code, the values of IR and IW are considered only when traversing the page tables for translating GPAs to SPAs when Mode is not 0 (and are ANDed with their counterparts in the page tables to determine the final permission), and when the DTE for a device is in passthrough mode (Mode is 0), IR and IW are not taken into consideration, and access is given unconditionally, which conflicts with the specification.
This patch implements honoring of the IR and IW DTE bits when Mode is 0. To do so, both the iommu and
iommu_nodmamemory regions in the AMDVIAddressSpace struct are preserved, and a new field is added,passthrough_perms, which along withaddr_translationwill determine whether the iommu or theiommu_nodmamemory region will be used.After a DTE invalidation,
passthrough_permsandaddr_translationwill be set accordingly with the new DTE configuration, and when it's time to switch address spaces inamdvi_switch_address_space:- Iff passthrough is desired and IR = 1 and IW = 1, use the
iommu_nodmamemory region,
Quoted 9 lines
- otherwise, use the iommu memory region.
The handler for translation (
amdvi_translate), which is called when the iommu memory region is enabled, has a fastpath which handles passthrough mode and verifies permissions. Additionally, since the DTE is fetched in the page walker, and it is possible for it to have been updated concurrently, if passthrough mode is requested, it is handled properly. Moreover, care has been taken to not fill the IOTLB with entries handed out when passthrough is active.
Thank you for the patches and the detailed description in the commit message. While the problem you are describing is valid, I think the solution in this series only addresses it partially. If my understanding is correct, your proposed Mode=0 handling covers a scenario like the following:
- an earlier INVALIDATE_DEVTAB_ENTRY read DTE[Mode]>0 and set addr_translation = true
- guest changes DTE in RAM to Mode=0
- The corresponding DTE invalidation has not yet been received/processed by QEMU, so addr_translation still reflects the older DTE with Mode>0
- a DMA request misses the IOTLB and triggers a page walk
- amdvi_do_translate() reads the new Mode=0 DTE, which conflicts with the older DTE state cached in addr_translation = true
I think the reverse scenario can also happen where a previous addr_translation = false can select the passthrough code path while the guest memory already has a DTE[Mode]>0.
There are other issues that the patch doesn't address e.g. for a VFIO device, the replay() callback does not handle Mode=0. It calls amdvi_sync_shadow_page_table_range()->fetch_pte() which rejects Mode=0, so no MAP notifications would be generated in case of DTEs with RO or WO permissions.
The larger problem is not with your patch, but that the current code has different emulation paths making decisions based on different versions of the DTE. e.g. the current code makes it possible to have addr_translation (and passthrough_perms) derived from an "older" DTE state be inconsistent with a newly retrieved DTE data from guest memory. Your change is fixing a special case, but unfortunately I think we need a larger rework so that it can be applied without introducing additional issues.
Based on my reading of the AMDVi documentation, the OS must issue INVALIDATE_DEVTAB_ENTRY after modifying a DTE before it can rely on any the updated field(s) being used. A guest can change the Device Table in its RAM, but the synchronization point where the change starts affecting behavior in the HW (or emulated HW in this case) is after the next INVALIDATE_DEVTAB_ENTRY command.
The approach I am considering is to cache the relevant DTE state (i.e. only the address translation related state) for each address space, and update it when processing INVALIDATE_DEVTAB_ENTRY or a global invalidation (INVALIDATE_IOMMU_ALL). THis way we avoid reading the DTE from guest memory at various paths, and instead use the cached version, so the emulation code makes decisions based on a consistent state.
This new DTE cache and the existing IOTLB cache should remain separate i.e. INVALIDATE_DEVTAB_ENTRY would update the DTE state but shouldn't call iommu_inval_iotlb(), based on my reading of Section 2.4.2 INVALIDATE_DEVTAB_ENTRY from the spec.
I think what I describe above should be a prerequisite for your patch to then implement consistent handling of IR/IW permissions with Mode=0. It is a much larger change, so I am still working through it, but you are welcome to contribute now that I have hopefully explained the idea :).
Thank you,
Alejandro
Quoted 287 lines
Also, for the sake of easier debugging, the
trace_amdvi_page_faulttracepoint now also takes the BDF of the device which has caused the page fault.Signed-off-by Daniel Paziyski <[email protected]>--- hw/i386/amd_iommu.c | 136 +++++++++++++++++++++++++++++-------------- hw/i386/trace-events | 2 +- 2 files changed, 92 insertions(+), 46 deletions(-)hw/i386/amd_iommu.c +91
-45@@ -54,6 +54,8 @@ struct AMDVIAddressSpace {IOVATree *iova_tree;/* DMA address translation active */ bool addr_translation; +/* Permissions for direct map when DMA address translation is not active */ +unsigned passthrough_perms; };/* AMDVI cache entry */ @@ -1162,34 +1164,16 @@ static void amdvi_switch_address_space(AMDVIAddressSpace *amdvi_as) {AMDVIState *s = amdvi_as->iommu_state; -if (s->dma_remap && amdvi_as->addr_translation) { -/* Enabling DMA region */ -memory_region_set_enabled (&amdvi_as->iommu_nodma, false); -memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), true); - }else { -/* Disabling DMA region, using passthrough */ -memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), false); -memory_region_set_enabled (&amdvi_as->iommu_nodma, true); +if (!(s->dma_remap && amdvi_as->addr_translation)) { +if (amdvi_as->passthrough_perms == (AMDVI_PERM_READ |AMDVI_PERM_WRITE )) { +memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), false); +memory_region_set_enabled (&amdvi_as->iommu_nodma, true); +return ; + } } -} - -/* -* For all existing address spaces managed by the IOMMU, enable/disable the -* corresponding memory regions to reset the address translation mode and -* use passthrough by default. -*/ -static void amdvi_reset_address_translation_all (AMDVIState *s) -{ -AMDVIAddressSpace *iommu_as; -GHashTableIter as_it; - -g_hash_table_iter_init (&as_it, s->address_spaces); -while (g_hash_table_iter_next (&as_it,NULL , (void **)&iommu_as)) { -/* Use passthrough as default mode after reset */ - iommu_as->addr_translation = false; -amdvi_switch_address_space (iommu_as); - } +memory_region_set_enabled (&amdvi_as->iommu_nodma, false); +memory_region_set_enabled (MEMORY_REGION (&amdvi_as->iommu), true); }static void enable_dma_mode (AMDVIAddressSpace *as,bool inval_current)@@ -1222,23 +1206,43 @@ static void enable_dma_mode(AMDVIAddressSpace *as, bool inval_current)* - invalidate all existing mappings * - switch to no_dma memory region */ -static void enable_nodma_mode (AMDVIAddressSpace *as) +static void enable_nodma_mode (AMDVIAddressSpace *as,unsigned perms) {IOMMUNotifier *n; -if (!as->addr_translation) { +if (!as->addr_translation && as->passthrough_perms == perms) {/* passthrough is already active, nothing to do */ return ; } - as->addr_translation = false; IOMMU_NOTIFIER_FOREACH(n, &as->iommu) {/* Drop all mappings for the address space */ amdvi_address_space_unmap (as, n); } + + as->addr_translation = false; + as->passthrough_perms = perms;amdvi_switch_address_space (as); } +/* +* For all existing address spaces managed by the IOMMU, enable/disable the +* corresponding memory regions to reset the address translation mode and +* use passthrough by default. +*/ +static void amdvi_reset_address_translation_all (AMDVIState *s) +{ +AMDVIAddressSpace *iommu_as; +GHashTableIter as_it; + +g_hash_table_iter_init (&as_it, s->address_spaces); + +while (g_hash_table_iter_next (&as_it,NULL , (void **)&iommu_as)) { +/* Use passthrough as default mode after reset */ +enable_nodma_mode (iommu_as,AMDVI_PERM_READ |AMDVI_PERM_WRITE ); + } +} +/* * A guest driver must issue the INVALIDATE_DEVTAB_ENTRY command to the IOMMU * after changing a Device Table entry. We can use this fact to detect when a @@ -1252,6 +1256,7 @@ static void amdvi_update_addr_translation_mode(AMDVIState *s, uint16_t devid)AMDVIAddressSpace *as;uint64_t dte[4 ] = {0 };int ret; +unsigned perms; as =amdvi_get_as_by_devid (s, devid);if (!as) {@@ -1268,14 +1273,16 @@ static void amdvi_update_addr_translation_mode(AMDVIState *s, uint16_t devid)case 0 :/* DTE was successfully retrieved */ if (!dte_mode) { -enable_nodma_mode (as);/* DTE[V]=1 && DTE[Mode]=0 => passthrough */ +/* DTE[V]=1 && DTE[Mode]=0 => passthrough */ + perms =amdvi_get_perms (dte[0 ]); +enable_nodma_mode (as, perms); }else {enable_dma_mode (as, false);/* Enable DMA translation */ }break ;case -AMDVI_FR_DTE_V :/* DTE[V]=0, address is passed untranslated */ -enable_nodma_mode (as); +enable_nodma_mode (as,AMDVI_PERM_READ |AMDVI_PERM_WRITE );break ;case -AMDVI_FR_DTE_RTR_ERR :case -AMDVI_FR_DTE_TV :@@ -1889,15 +1896,45 @@ static void amdvi_mmio_write(void *opaque, hwaddr addr, uint64_t val, } } +static void amdvi_fill_iotlb_entry_passthrough (IOMMUTLBEntry *entry, +AMDVIAddressSpace *as, +unsigned passthrough_perms, +hwaddr addr, +IOMMUAccessFlags flag) +{ +unsigned perms =0 ; + +if (flag &IOMMU_RO ) { + perms |=AMDVI_PERM_READ ; + } +if (flag &IOMMU_WO ) { + perms |=AMDVI_PERM_WRITE ; + } + +if ((passthrough_perms & perms) != perms) { +amdvi_page_fault (as->iommu_state, as->devfn, addr, perms); +trace_amdvi_page_fault (addr,pci_bus_num (as->bus),PCI_SLOT (as->devfn), +PCI_FUNC (as->devfn), perms); +return ; + } + + entry->iova = addr &AMDVI_PAGE_MASK_4K ; + entry->translated_addr = addr &AMDVI_PAGE_MASK_4K ; + entry->addr_mask = ~AMDVI_PAGE_MASK_4K ; + entry->perm = flag &IOMMU_RW ; +} +static void amdvi_page_walk (AMDVIAddressSpace *as,uint64_t *dte,IOMMUTLBEntry *ret,unsigned perms, -hwaddr addr) +hwaddr addr,bool *update_iotlb) {hwaddr page_mask, pagesize =0 ;uint8_t mode;uint64_t pte;int fetch_ret; + *update_iotlb = false; +/* make sure the DTE has TV = 1 */ if (!(dte[0 ] &AMDVI_DEV_TRANSLATION_VALID )) {/* @@ -1916,7 +1953,9 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte,return ; }if (mode ==0 ) { - goto no_remap; +amdvi_fill_iotlb_entry_passthrough (ret, as,amdvi_get_perms (dte[0 ]), + addr, perms ==AMDVI_PERM_WRITE ?IOMMU_WO :IOMMU_RO ); +return ; }/* Attempt to fetch the PTE to determine if a valid mapping exists */ @@ -1931,7 +1970,8 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte, perms != (perms & amdvi_get_perms(pte))) {amdvi_page_fault (as->iommu_state, as->devfn, addr, perms); -trace_amdvi_page_fault (addr); +trace_amdvi_page_fault (addr,pci_bus_num (as->bus),PCI_SLOT (as->devfn), +PCI_FUNC (as->devfn), perms);return ; }@@ -1944,13 +1984,7 @@ static void amdvi_page_walk(AMDVIAddressSpace *as, uint64_t *dte, ret->translated_addr = (pte &AMDVI_DEV_PT_ROOT_MASK ) & page_mask; ret->addr_mask = ~page_mask; ret->perm =amdvi_get_perms (pte); -return ; - -no_remap: - ret->iova = addr &AMDVI_PAGE_MASK_4K ; - ret->translated_addr = addr &AMDVI_PAGE_MASK_4K ; - ret->addr_mask = ~AMDVI_PAGE_MASK_4K ; - ret->perm =amdvi_get_perms (dte[0 ]); + *update_iotlb = true; } static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,@@ -1961,6 +1995,7 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr,AMDVIIOTLBEntry *iotlb_entry =amdvi_iotlb_lookup (s, addr, devid);uint64_t entry[4 ];int dte_ret; +bool update_iotlb;if (iotlb_entry) { trace_amdvi_iotlb_hit(PCI_BUS_NUM(devid), PCI_SLOT(devid),@@ -1983,10 +2018,14 @@ static void amdvi_do_translate(AMDVIAddressSpace *as, hwaddr addr, }amdvi_page_walk (as, entry, ret, - is_write ?AMDVI_PERM_WRITE :AMDVI_PERM_READ , addr); + is_write ?AMDVI_PERM_WRITE :AMDVI_PERM_READ , addr, + &update_iotlb); + +if (update_iotlb) { +amdvi_update_iotlb (s, devid, addr, *ret, + entry[1 ] &AMDVI_DEV_DOMID_ID_MASK ); + } -amdvi_update_iotlb (s, devid, addr, *ret, - entry[1 ] &AMDVI_DEV_DOMID_ID_MASK );return ; out:@@ -2031,7 +2070,13 @@ static IOMMUTLBEntry amdvi_translate(IOMMUMemoryRegion *iommu, hwaddr addr,return ret; } -amdvi_do_translate (as, addr, flag &IOMMU_WO , &ret); +if (!as->addr_translation) { +amdvi_fill_iotlb_entry_passthrough (&ret, as, as->passthrough_perms, + addr, flag); + }else { +amdvi_do_translate (as, addr, flag &IOMMU_WO , &ret); + } +trace_amdvi_translation_result (pci_bus_num (as->bus),PCI_SLOT (as->devfn),PCI_FUNC (as->devfn), addr, ret.translated_addr);return ret;@@ -2423,6 +2468,7 @@ static AddressSpace *amdvi_host_dma_iommu(PCIBus *bus, void *opaque, int devfn) amdvi_dev_as->notifier_flags =IOMMU_NOTIFIER_NONE ; amdvi_dev_as->iova_tree =iova_tree_new (); amdvi_dev_as->addr_translation = false; + amdvi_dev_as->passthrough_perms =AMDVI_PERM_READ |AMDVI_PERM_WRITE ; key->bus = bus; key->devfn = devfn;hw/i386/trace-events +1
-1@@ -103,7 +103,7 @@ amdvi_dte_get_fail(uint64_t addr, uint32_t offset) "error: failed to access Devi amdvi_invalid_dte(uint64_t addr) "PTE entry at 0x%"PRIx64" is invalid " amdvi_get_pte_hwerror(uint64_t addr) "hardware error eccessing PTE at addr 0x%"PRIx64 amdvi_mode_invalid(uint8_t level, uint64_t addr)"error: translation level 0x%"PRIx8" translating addr 0x%"PRIx64 -amdvi_page_fault(uint64_t addr) "error: page fault accessing guest physical address 0x%"PRIx64 +amdvi_page_fault(uint64_t addr, uint8_t bus, uint8_t slot, uint8_t func, unsigned perms) "error: page fault accessing guest physical address 0x%"PRIx64" by devid %02x:%02x.%x with perms 0x%x" amdvi_iotlb_hit(uint8_t bus, uint8_t slot, uint8_t func, uint64_t addr, uint64_t txaddr) "hit iotlb devid %02x:%02x.%x gpa 0x%"PRIx64" hpa 0x%"PRIx64 amdvi_translation_result(uint8_t bus, uint8_t slot, uint8_t func, uint64_t addr, uint64_t txaddr) "devid: %02x:%02x.%x gpa 0x%"PRIx64" hpa 0x%"PRIx64 amdvi_mem_ir_write_req(uint64_t addr, uint64_t val, uint32_t size) "addr 0x%"PRIx64" data 0x%"PRIx64" size 0x%"PRIx32Signature · 1 line
2.55.0
Hello Alejandro,
Thank you for the detailed review!
Quoted 14 lines · Alejandro Jimenez
Thank you for the patches and the detailed description in the commit message. While the problem you are describing is valid, I think the solution in this series only addresses it partially. If my understanding is correct, your proposed Mode=0 handling covers a scenario like the following:
- an earlier
INVALIDATE_DEVTAB_ENTRYread DTE[Mode]>0 and setaddr_translation= true
- guest changes DTE in RAM to Mode=0
- The corresponding DTE invalidation has not yet been received/processed by QEMU, soaddr_translationstill reflects the older DTE with Mode>0
- a DMA request misses the IOTLB and triggers a page walk
-amdvi_do_translate()reads the new Mode=0 DTE, which conflicts with the older DTE state cached inaddr_translation= true
Quoted 3 lines
I think the reverse scenario can also happen where a previous
addr_translation= false can select the passthrough code path while the guest memory already has a DTE[Mode]>0.
You're right. The approach that you suggest:
Quoted 6 lines
The approach I am considering is to cache the relevant DTE state (i.e. only the address translation related state) for each address space, and update it when processing
INVALIDATE_DEVTAB_ENTRYor a global invalidation (INVALIDATE_IOMMU_ALL). THis way we avoid reading the DTE from guest memory at various paths, and instead use the cached version, so the emulation code makes decisions based on a consistent state.
would solve this problem for the part of the code that I've touched, and other parts too.
Quoted 6 lines
Based on my reading of the AMDVi documentation, the OS must issue
INVALIDATE_DEVTAB_ENTRYafter modifying a DTE before it can rely on any the updated field(s) being used. A guest can change the Device Table in its RAM, but the synchronization point where the change starts affecting behavior in the HW (or emulated HW in this case) is after the nextINVALIDATE_DEVTAB_ENTRYcommand.
By the way, as I understand per the AMDVi specification, it would not be required to invalidate DTEs if address translation or interrupt translation information is changed, as long as V=0 and IV=0 before the change. However, I feel that emulating this will be complex, since the Device Table writes will have to be intercepted. Moreover, Linux (and probably many other OSes) seem to always flush DTEs, so implementing this wouldn't be necessary.
Quoted 5 lines
There are other issues that the patch doesn't address e.g. for a VFIO device, the
replay()callback does not handle Mode=0. It calls amdvi_sync_shadow_page_table_range()->fetch_pte() which rejects Mode=0, so no MAP notifications would be generated in case of DTEs with RO or WO permissions.
Oh, okay. I was not aware of this.
Quoted 4 lines
I think what I describe above should be a prerequisite for your patch to then implement consistent handling of IR/IW permissions with Mode=0. It is a much larger change, so I am still working through it, but you are welcome to contribute now that I have hopefully explained the idea :).
Sure! I am willing to make a V2 of this series, fixing the replay callback and taking advantage of the cached DTE, once you have implemented it.
Cheers,
Daniel
PATCH 2/2 tests/qtest: Add amd-iommu passthrough mode test
In reply to Daniel PaziyskiTest whether the AMD IOMMU behaves correctly when a device is in passthrough
mode: that is, DTE[V] = 1, DTE[TV] = 1, and DTE[IR] and DTE[IW] are some combination of 0 and 1. The edu device is used as an initiator of DMA transactions.
We are looking for, in order:
- when IR = 1 and IW = 1, no IO page faults
- when IR = 1 and IW = 0, an IO page fault on device write
- when IR = 0 and IW = 1, an IO page fault on read
- when IR = 1 and IW = 1, no IO page fault (back to nodma region)
- when IR = 0 and IV = 0, an IO page fault on device read/write
- after reset, the device to behave as if IR = 1 and IW = 1 (nodma)
--- tests/qtest/amd-iommu-test.c | 230 +++++++++++++++++++++++++++++++++++ 1 file changed, 230 insertions(+)
tests/qtest/amd-iommu-test.c +230 -0
@@ -4,11 +4,23 @@#include "qemu/osdep.h" #include "libqtest.h" +#include "libqos/pci-pc.h" #include "hw/i386/amd_iommu.h" #define CMDBUF_ADDR 0x200000#define CMDBUF_LEN_FIELD 8#define CMDBUF_ENTRIES (1U << CMDBUF_LEN_FIELD) +#define CMDBUF_SIZE (CMDBUF_ENTRIES * AMDVI_COMMAND_SIZE) + +#define DEVTAB_ADDR (CMDBUF_ADDR + CMDBUF_SIZE) +#define DEVTAB_LEN_FIELD 0x1ff +#define DEVTAB_ENTRIES (((DEVTAB_LEN_FIELD + 1) << 12U) / AMDVI_DEVTAB_ENTRY_SIZE) +#define DEVTAB_SIZE ((DEVTAB_LEN_FIELD + 1) << 12U) + +#define EVLOG_ADDR (DEVTAB_ADDR + DEVTAB_SIZE) +#define EVLOG_LEN_FIELD 0x0f +#define EVLOG_ENTRIES ((4096U << (EVLOG_LEN_FIELD - 8)) / AMDVI_EVENT_LEN) +#define EVLOG_SIZE (4096U << (EVLOG_LEN_FIELD - 8))static inline uint64_t amdvi_reg_readq (QTestState *s,uint64_t offset) {
@@ -21,6 +33,34 @@ static inline void amdvi_reg_writeq(QTestState *s, uint64_t offset,qtest_writeq (s,AMDVI_BASE_ADDR + offset, val); } +static void amdvi_invalidate_devtab_entry (QTestState *s,uint16_t bdf) +{ +uint64_t tail =amdvi_reg_readq (s,AMDVI_MMIO_COMMAND_TAIL ); + +qtest_writel (s,CMDBUF_ADDR + tail, bdf); +qtest_writel (s,CMDBUF_ADDR + tail +4 ,0x20000000 ); +qtest_writel (s,CMDBUF_ADDR + tail +8 ,0 ); +qtest_writel (s,CMDBUF_ADDR + tail +12 ,0 ); + +g_assert_true (amdvi_reg_readq (s,AMDVI_MMIO_STATUS ) + &AMDVI_MMIO_STATUS_CMDBUF_RUN ); + + tail = (tail +AMDVI_COMMAND_SIZE ) %CMDBUF_SIZE ; +amdvi_reg_writeq (s,AMDVI_MMIO_COMMAND_TAIL , tail); + +for (;;) { +g_assert_true (amdvi_reg_readq (s,AMDVI_MMIO_STATUS ) & +AMDVI_MMIO_STATUS_CMDBUF_RUN ); +if ((amdvi_reg_readq (s,AMDVI_MMIO_COMMAND_HEAD ) + &AMDVI_MMIO_CMDBUF_HEAD_MASK ) == tail) { +break ; + } + } + +g_assert_true (amdvi_reg_readq (s,AMDVI_MMIO_STATUS ) + &AMDVI_MMIO_STATUS_CMDBUF_RUN ); +} +static void test_cmdbuf_head_wrap (void ) {QTestState *s;
@@ -68,9 +108,199 @@ static void test_cmdbuf_head_wrap(void)qtest_quit (s); } +static void test_dte_mode_0_edu_dma (QTestState *s, +QPCIDevice *edu,QPCIBar edu_bar, +hwaddr edu_dma_src,hwaddr edu_dma_dst, +bool edu_to_ram, +bool expect_fail) +{ +bool log; +uint64_t head, tail, addr; + +qpci_io_writeq (edu, edu_bar,0x80 , edu_dma_src); +qpci_io_writeq (edu, edu_bar,0x88 , edu_dma_dst); +qpci_io_writeq (edu, edu_bar,0x90 ,1 ); +qpci_io_writeq (edu, edu_bar,0x98 ,0x01 | (edu_to_ram ?0x02 :0x00 )); + +while (qpci_io_readq (edu, edu_bar,0x98 ) &0x01 ) { +/* edu uses a timer for scheduling the DMA operation */ +qtest_clock_step_next (s); + } + + log =amdvi_reg_readq (s,AMDVI_MMIO_STATUS ) &AMDVI_MMIO_STATUS_EVENT_INT ; +if (expect_fail) { +g_assert_true (log); +amdvi_reg_writeq (s,AMDVI_MMIO_STATUS ,AMDVI_MMIO_STATUS_EVENT_INT ); + }else { +g_assert_false (log); + } + +if (expect_fail) { + head =amdvi_reg_readq (s,AMDVI_MMIO_EVENT_HEAD ) & +AMDVI_MMIO_EVTLOG_HEAD_MASK ; + tail =amdvi_reg_readq (s,AMDVI_MMIO_EVENT_TAIL ) & +AMDVI_MMIO_EVTLOG_TAIL_MASK ; + +/* make sure only one event has occurred */ +g_assert_true (((head +AMDVI_EVENT_LEN ) %EVLOG_SIZE ) == + (tail %EVLOG_SIZE )); + + addr =qtest_readq (s,EVLOG_ADDR + head +8 ); + +if (edu_to_ram) { +g_assert_true (addr == edu_dma_dst); + }else { +g_assert_true (addr == edu_dma_src); + } + + head = (head +AMDVI_EVENT_LEN ) %EVLOG_SIZE ; +amdvi_reg_writeq (s,AMDVI_MMIO_EVENT_HEAD , head); + } +} + +static void test_dte_mode_0_iommu_setup (QTestState *s) +{ +/* command buffer */ +amdvi_reg_writeq (s,AMDVI_MMIO_COMMAND_BASE , +CMDBUF_ADDR | ((uint64_t )CMDBUF_LEN_FIELD <<56 )); +amdvi_reg_writeq (s,AMDVI_MMIO_COMMAND_HEAD ,0 ); +amdvi_reg_writeq (s,AMDVI_MMIO_COMMAND_TAIL ,0 ); + +/* event log */ +amdvi_reg_writeq (s,AMDVI_MMIO_EVENT_BASE , +EVLOG_ADDR | ((uint64_t )EVLOG_LEN_FIELD <<56 )); +amdvi_reg_writeq (s,AMDVI_MMIO_EVENT_HEAD ,0 ); +amdvi_reg_writeq (s,AMDVI_MMIO_EVENT_TAIL ,0 ); + +/* device table */ +amdvi_reg_writeq (s,AMDVI_MMIO_DEVICE_TABLE , +DEVTAB_ADDR |DEVTAB_LEN_FIELD ); + +/* enable */ +amdvi_reg_writeq (s,AMDVI_MMIO_CONTROL ,AMDVI_MMIO_CONTROL_AMDVIEN | +AMDVI_MMIO_CONTROL_CMDBUFLEN | +AMDVI_MMIO_CONTROL_EVENTLOGEN | +AMDVI_MMIO_CONTROL_EVENTINTEN ); +} + +static void test_dte_mode_0 (void ) +{ +QTestState *s; +QPCIBus *bus; +QPCIDevice *edu; +QPCIBar edu_bar; +const unsigned int edu_devfn =PCI_DEVFN (0x14 ,0 ), + edu_devtab_off = edu_devfn *AMDVI_DEVTAB_ENTRY_SIZE ; +const hwaddr edu_dma_test_buf =0x1000 , edu_dma_dev_buf =0x40000 ; + +if (!qtest_has_machine ("q35" )) { +g_test_skip ("q35 machine not available" ); + } + +if (!qtest_has_device ("edu" )) { +g_test_skip ("edu device not available" ); + } + + s =qtest_init ("-M q35 -device amd-iommu,dma-remap=on,dma-translation=on," +"device-iotlb=on -device edu,addr=14.0,dma_mask=0xffffffff" ); + bus =qpci_new_pc (s,NULL ); + +test_dte_mode_0_iommu_setup (s); + +/* get bar of EDU device */ + edu =qpci_device_find (bus, edu_devfn); +g_assert_nonnull (edu); + + edu_bar =qpci_iomap (edu,0 ,NULL ); +qpci_device_enable (edu); + +g_test_message ("testing DMA passthrough, IR = 1, IW = 1" ); +qtest_writeq (s,DEVTAB_ADDR + edu_devtab_off, +AMDVI_DEV_PERM_READ |AMDVI_DEV_PERM_WRITE | +AMDVI_DEV_TRANSLATION_VALID |AMDVI_DEV_VALID ); +amdvi_invalidate_devtab_entry (s, edu_devfn); + +g_test_message ("testing read" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_test_buf, edu_dma_dev_buf, + false, false); +g_test_message ("testing write" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_dev_buf, edu_dma_test_buf, + true, false); + +g_test_message ("testing DMA passthrough, IR = 1, IW = 0" ); +qtest_writeq (s,DEVTAB_ADDR + edu_devtab_off, +AMDVI_DEV_PERM_READ | +AMDVI_DEV_TRANSLATION_VALID |AMDVI_DEV_VALID ); +amdvi_invalidate_devtab_entry (s, edu_devfn); + +g_test_message ("testing read" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_test_buf, edu_dma_dev_buf, + false, false); +g_test_message ("testing write" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_dev_buf, edu_dma_test_buf, + true, true); + +g_test_message ("testing DMA passthrough, IR = 0, IW = 1" ); +qtest_writeq (s,DEVTAB_ADDR + edu_devtab_off, +AMDVI_DEV_PERM_WRITE | +AMDVI_DEV_TRANSLATION_VALID |AMDVI_DEV_VALID ); +amdvi_invalidate_devtab_entry (s, edu_devfn); + +g_test_message ("testing read" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_test_buf, edu_dma_dev_buf, + false, true); +g_test_message ("testing write" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_dev_buf, edu_dma_test_buf, + true, false); + +/* make sure the nodma memory region is activated again properly */ +g_test_message ("testing DMA passthrough, IR = 1, IW = 1 (reset to nodma)" ); +qtest_writeq (s,DEVTAB_ADDR + edu_devtab_off, +AMDVI_DEV_PERM_READ |AMDVI_DEV_PERM_WRITE | +AMDVI_DEV_TRANSLATION_VALID |AMDVI_DEV_VALID ); +amdvi_invalidate_devtab_entry (s, edu_devfn); + +g_test_message ("testing read" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_test_buf, edu_dma_dev_buf, + false, false); +g_test_message ("testing write" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_dev_buf, edu_dma_test_buf, + true, false); + +g_test_message ("testing DMA passthrough, IR = 0, IW = 0" ); +qtest_writeq (s,DEVTAB_ADDR + edu_devtab_off, +AMDVI_DEV_TRANSLATION_VALID |AMDVI_DEV_VALID ); +amdvi_invalidate_devtab_entry (s, edu_devfn); + +g_test_message ("testing read" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_test_buf, edu_dma_dev_buf, + false, true); +g_test_message ("testing write" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_dev_buf, edu_dma_test_buf, + true, true); + +/* make sure that after reset, the device switches to nodma */ +qtest_system_reset (s); +test_dte_mode_0_iommu_setup (s); + +g_test_message ("testing DMA after reset" ); + edu_bar =qpci_iomap (edu,0 ,NULL ); +qpci_device_enable (edu); + +g_test_message ("testing read" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_test_buf, edu_dma_dev_buf, + false, false); +g_test_message ("testing write" ); +test_dte_mode_0_edu_dma (s, edu, edu_bar, edu_dma_dev_buf, edu_dma_test_buf, + true, false); + +qtest_quit (s); +} +int main (int argc,char **argv) {g_test_init (&argc, &argv,NULL );qtest_add_func ("/q35/amd-iommu/cmdbuf-head-wrap" , test_cmdbuf_head_wrap); +qtest_add_func ("/q35/amd-iommu/dte-mode-0" , test_dte_mode_0);return g_test_run (); }
Signature · 1 line
2.55.0