Xen Security Advisory 400 v2 (CVE-2022-26358,CVE-2022-26359,CVE-2022-26360,CVE-2022-26361) - IOMMU: RMRR (VT-d) and unity map (AMD-Vi) handling issues
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256
Xen Security Advisory CVE-2022-26358,CVE-2022-26359,CVE-2022-26360,CVE-2022-26361 / XSA-400
version 2
IOMMU: RMRR (VT-d) and unity map (AMD-Vi) handling issues
UPDATES IN VERSION 2
====================
Public release.
ISSUE DESCRIPTION
=================
Certain PCI devices in a system might be assigned Reserved Memory Regions (specified via Reserved Memory Region Reporting, "RMRR") for Intel VT-d or Unity Mapping ranges for AMD-Vi. These are typically used for platform tasks such as legacy USB emulation.
Since the precise purpose of these regions is unknown, once a device associated with such a region is active, the mappings of these regions need to remain continuouly accessible by the device. This requirement has been violated.
Subsequent DMA or interrupts from the device may have unpredictable behaviour, ranging from IOMMU faults to memory corruption.
IMPACT
======
The precise impact is system specific, but would likely be a Denial of Service (DoS) affecting the entire host. Privilege escalation and information leaks cannot be ruled out.
VULNERABLE SYSTEMS
==================
All Xen versions supporting PCI passthrough are affected.
Only x86 systems with IOMMU hardware are vulnerable. Arm systems as well as x86 systems without IOMMU hardware or without any IOMMUs in use are not vulnerable.
Only x86 guests which have physical devices passed through to them, and only when any such device has an associated RMRR or unity map, can leverage the vulnerability. (Whether a device is associated with an RMRR or unity map is not easy to discern.)
MITIGATION
==========
Not passing through physical devices to untrusted guests when the devices have assoicated RMRRs / unity maps will avoid the vulnerability.
CREDITS
=======
Aspects of this issue were discovered by Jan Beulich of SUSE and Roger Pau Monné of Citrix.
RESOLUTION
==========
Applying the appropriate set of attached patches resolves this issue.
Note that patches for released versions are generally prepared to apply to the stable branches, and may not apply cleanly to the most recent release tarball. Downstreams are encouraged to update to the tip of the stable branch before applying these patches.
xsa400/xsa400-??.patch xen-unstable xsa400/xsa400-4.16-*.patch Xen 4.16.x xsa400/xsa400-4.15-*.patch Xen 4.15.x xsa400/xsa400-4.14-*.patch Xen 4.14.x xsa400/xsa400-4.13-*.patch Xen 4.13.x xsa400/xsa400-4.12-*.patch Xen 4.12.x
$ sha256sum xsa400*/* 108544235a011e96e8717e9e744190fd8128e99cca8141f682667bd7b8533f45 xsa400/xsa400-01.patch 1648ef0213dd6beeaa782a18926416a00aa51ad89136aa1565debd5312bf58aa xsa400/xsa400-02.patch 39e02738ce3d3a65c02355dd45cb844ecca8be9715b7887dedcdaada02c4bda1 xsa400/xsa400-03.patch 2d5e7f4090418d817df002b7cd7a9a40246ff9900faf53fc2b2f685ac77b2a0e xsa400/xsa400-4.12-00.patch a4291033d1960f8095a11e765072f60cf9e5be07d4cd0cbfed1896f9c66a4576 xsa400/xsa400-4.12-01.patch 2f5b2ce22ae6cba8a5380ccf63ae2dcf6e7ce8f29a33c87c0630abc6c1d24793 xsa400/xsa400-4.12-02.patch 86ee30d46957f63e4516ea47f14470579d0906fd536b39e31645e2cd471c1b49 xsa400/xsa400-4.12-03.patch 546430f61df3eb92a55e32047cce8557d85a8e860f67d558c35fea44e7c56b18 xsa400/xsa400-4.12-04.patch 178463bf81b138668c2f036b11a0b9406a6f5bb98c0643741787ccdf276495d9 xsa400/xsa400-4.12-05.patch b19c599eb5eb17f8e0e17879174a4235fdc3f2172bc77bffdee60074ef2d7c90 xsa400/xsa400-4.12-06.patch 02eae481f2b06763dd913b46a65837791147b7916bbd0ed50e5d972784b310aa xsa400/xsa400-4.12-07.patch 298339cc343d9803158abd0c36030f36e7dc8360ccfed320f34ff01813c47434 xsa400/xsa400-4.12-08.patch 9253d7120db39a1e3c14fc6b8735577f3d215837996eed057511146c0ba199d6 xsa400/xsa400-4.12-09.patch 762bbe418c028449812b4441c4e369230477eeea4d863c05a2efd9c7e5e19bc3 xsa400/xsa400-4.12-10.patch 7172957a27cdf02327c28b5cfedf35b12e76cb17237cbb9c318442961f503fe6 xsa400/xsa400-4.12-11.patch c685d9ee2453728f509a78506930f756118ff443fbe6d5307873e90455b5e131 xsa400/xsa400-4.13-00.patch 0b0a80a249c9c58726f913df24163976cfaadabc685d3d274dd7b972fe4cbffc xsa400/xsa400-4.13-01.patch eabee7d9cabe00748e78fda0363818ca6085ac395804658a11f94394236d2734 xsa400/xsa400-4.13-02.patch 66cd2a0ee8a002668e04e2bb7192ac2f774012434765988bdea0a7e79621f58c xsa400/xsa400-4.13-03.patch 4457c142c1fdc67e0dc77a57bc57159ee63dc1946e432ae1225937c7ed3a3e82 xsa400/xsa400-4.13-04.patch 2feabcbcf160d8c1130b2c86efd98dd9fee0332489520f3a10aa8bb37d6a5a20 xsa400/xsa400-4.13-05.patch 903099123d37806b7fe684d75ab0fb6a28b4609d7370cce65a9ba5ab4db43ddd xsa400/xsa400-4.13-06.patch e8a39d36f629b7f79634d09d22ace97f1c23ee10ee9fef6afd68ec2204c55d81 xsa400/xsa400-4.13-07.patch d85c63917542d2d2b47dd6322b4c32364719be4707acf616290aacd536c57805 xsa400/xsa400-4.13-08.patch 5496ca04a2789603069ca8e8d3bf6d301c19c799ccff813bf17865b46f687757 xsa400/xsa400-4.13-09.patch 8b343f3616c0283caac334b4f0cc42b1523f2dc9de3463f2edadbf1a11c17a22 xsa400/xsa400-4.13-10.patch 577d20d2ed5da8a89f32eae717a140f9ec3900e31fc9e10ee23e37ae5dc19d4a xsa400/xsa400-4.13-11.patch bfa82c6beae9ec8ab88e042296442eed3ca162b42124c982785c7c0d95440480 xsa400/xsa400-4.14-01.patch f8f7da8d0cc3d149874956e9aefe666a2e33275dba66ed0d55f0b559d078f01f xsa400/xsa400-4.14-02.patch 49acd3d795aa091f8fa1c72b5064b5a71966e77c9785f5d0f8226d99daba3ead xsa400/xsa400-4.14-03.patch 0ce4435a8c7fd6f4186ea31d29932094b1a902d9d0c5dafa3fcb1c15c5eca88c xsa400/xsa400-4.14-04.patch aeae4cef4bf31ecd854ad820239fe793f36ed81c0ad28ce3935ab3f83c3ee58c xsa400/xsa400-4.14-05.patch f8d93e2ab3f891f70abd17ddba9aae8237605f1f680754ff9df646e5ddb9d419 xsa400/xsa400-4.14-06.patch afb9f731ba7a53930626e322ebb39db19f817b5c60710fc934c395fcdea4c7f4 xsa400/xsa400-4.14-07.patch c236bb7b5c692cf30e7b84e6668f87b248bcf80c6df1dd74c655b91b955eb271 xsa400/xsa400-4.14-08.patch 6c4d44983eae92212be6bda7663198260bdeb0506c5b622c38ddbcedf9360d69 xsa400/xsa400-4.14-09.patch 5393c6681c675c666396f6725c2ac2e48293465e97bc228dcfc410ee84d8ba7c xsa400/xsa400-4.14-10.patch 8d1dcdeb4d9420840a6753bb56f35bf199af63a6c477f2352e47222df5337c1a xsa400/xsa400-4.14-11.patch f52a70fd92c4819658630c9d478ae03a9801352b024240059877d0ee2002e31e xsa400/xsa400-4.15-01.patch 243a0b20b20c78b41f833004c82ce26b1249285671f150bdfe4a2314860df316 xsa400/xsa400-4.15-02.patch 175f2472f80dacffcee578543f4fc4521f5533de46199ce86b072bfd0ede9ae3 xsa400/xsa400-4.15-03.patch dee6fbe9e3c03d695bae1f81cebca80ca54ecf02a51db64ae5f3d313837eed8c xsa400/xsa400-4.15-04.patch 55a35033b8ba45b1c9c556e0c2866733e518137299f2f3f4d41046766898aeb5 xsa400/xsa400-4.15-05.patch 94ec5289a3a632fed8d220478847ef3b780d7db345f30ee9d6b186905de61048 xsa400/xsa400-4.15-06.patch 50cbb08b931cd1cc8ec3e2aa17b537db80dcd03a6de0994331fc3818f53cbfe4 xsa400/xsa400-4.15-07.patch e4c77fac42b8b0b50cdc9de30f406176c44de75d647e36b4bbd6360dd70c8aa6 xsa400/xsa400-4.15-08.patch 652cd8700b830b3520db325cfd90eab6c08bd423debb21c05caa7edfef9ee671 xsa400/xsa400-4.15-09.patch 50e58d663690475ce35a0a65eda88a2c2da319ddc02eb15b5a7d568a8f0a0366 xsa400/xsa400-4.15-10.patch 37a18da6ad1f529bfdc4156225f18c17c10a45302b7d6045ee38c934656589da xsa400/xsa400-4.15-11.patch ff069d123df2aba7a4f3185f21f7ca36b34fc026dcdd279224c86698f84c0975 xsa400/xsa400-4.15-12.patch 76497919563cdf2804f5071325c032acd04cf8df75c0dcb4b207a93b9ae80927 xsa400/xsa400-4.16-01.patch 150f2a7621ec17d6369a6ebbc2c08c502f3524acb89855a86a25d7b4fa3e3270 xsa400/xsa400-4.16-02.patch cd59bca0fcab4bda1d0ea839501543a59b53aef61e96a0d949675bc5550a6fcb xsa400/xsa400-4.16-03.patch 098edab3ed8915a3598badaa1d452f7c8ab2d8e72879dd9bf941b2093e6df9d1 xsa400/xsa400-4.16-04.patch a85640291e1bc1cdb757172eb6d2036834ad2eb7b84252cc64d29db3feeff331 xsa400/xsa400-4.16-05.patch 99d43caf1ce60f421d940c2774e2a59d65d1f0dcccd941f13066117a19222b22 xsa400/xsa400-4.16-06.patch fbe5ec58da594dffd3e63c18406280f27d976609350d7c7083acbf2f2e6538cd xsa400/xsa400-4.16-07.patch e6b0d400beda8ff0e9bdbfd033bc23935069b41dbd5dddf863dbd70de44d908f xsa400/xsa400-4.16-08.patch 965f96bdb33a872942de42597c7cc32012020f37b25a84015ecc55387d6b07dd xsa400/xsa400-4.16-09.patch 7996b1462374168ddc9cbc01c990d5cb40140d7a100096f284f6b74fb4ad7ec4 xsa400/xsa400-4.16-10.patch 23e325be799b299e9621e76eeff646b81518a4124474fb766d3bf6f0cc925083 xsa400/xsa400-4.16-11.patch 6fa43e1a8f53184724cd4a7b5c13f0fd699c998af968bafaefda570432b5a7d8 xsa400/xsa400-4.16-12.patch 82306d680ef445bee04969028184f65b5e106c89c308fbb876b858f77fea9506 xsa400/xsa400-04.patch ccf3ff62b427d3d2ea46d4da96beeef2cb64674bc0d247352233c4b84a21f205 xsa400/xsa400-05.patch 8b3f767ca659e8bbf4983927999bdb92d9fa42e3a88973e22facda0e23f29a84 xsa400/xsa400-06.patch bb570b89a2d4b535831d9211bf08cc0c62c060dc7808911afd8186082b884cf8 xsa400/xsa400-07.patch 8382fd5336b5b4b3388dee099da00b1e728dea77d96825c6088991d7e50f333b xsa400/xsa400-08.patch 6909363863932ca77c9b7384516965bc1697fab9b4814294a1675fb6ce8e166e xsa400/xsa400-09.patch c37fee4dd9ab2bf64ebb8b9c553f793bfcef9a7cf1972b0000fe6ce28b9e8e60 xsa400/xsa400-10.patch 70f11f64457c9703f09c3121d08d5ff4676af20bd42eb1262c433a5e0f79ea41 xsa400/xsa400-11.patch 724e34d262939162ecab713c070cc07b8f7baeca50ee8b62bb59460ec4f7fbdb xsa400/xsa400-12.patch $
DEPLOYMENT DURING EMBARGO
=========================
Deployment of the patches described above (or others which are substantially similar) is permitted during the embargo, even on public-facing systems with untrusted guest users and administrators.
HOWEVER, deployment of the mitigation is NOT permitted (except where all the affected systems and VMs are administered and used only by organisations which are members of the Xen Project Security Issues Predisclosure List). Specifically, deployment on public cloud systems is NOT permitted.
This is because removal of pass-through devices or their replacement by emulated devices is a guest visible configuration change, which may lead to re-discovery of the issue.
Deployment of this mitigation is permitted only AFTER the embargo ends.
AND: Distribution of updated software is prohibited (except to other members of the predisclosure list).
(Note: this during-embargo deployment notice is retained in post-embargo publicly released Xen Project advisories, even though it is then no longer applicable. This is to enable the community to have oversight of the Xen Project Security Team's decisionmaking.)
For more information about permissible uses of embargoed information, consult the Xen Project community's agreed Security Policy:
http://www.xenproject.org/security-policy.html
-----BEGIN PGP SIGNATURE-----
iQFABAEBCAAqFiEEI+MiLBRfRHX6gGCng/4UyVfoK9kFAmJML+0MHHBncEB4ZW4u b3JnAAoJEIP+FMlX6CvZQl8IALuh2RTTSiQBYeybKZPr3QEOfy+L3VFzNbRZeGBc jpN12lXjzkFvVuTXDu6Cgw1g6yhRrcRnhGWhx3T8RpGeYhHq1GeKSw7+c4NTsiDL 21P9F3mEban6tOUa82V2PTkYfAMMpbn5apOcsMvdlPoEgXdgLPh5HoVIvXQQO6Ni 3MkCN7foV3M07jAiB4ehqrsPLhzGSCCRJfiD9PA1/RnnzCBrspyyWepF7mwzh2cx 1kENmG8mSwA3Eg0fNUMDBi6W+drNmIx4gooYo3LBnCyMgzUrv7+bzkvjpcU8+dAq H5i2Morbx4j0F/TcQ8+23KjzNvJHbiqKVrIFCoa1z54dGC8=
=tz+l
-----END PGP SIGNATURE-----
From: Jan Beulich <[email protected]>
Subject: VT-d: fix (de)assign ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully updated.
Also adjust a related log message.
This is CVE-2022-26358 / part of XSA-400.
---
v2: Don't unmap RMRRs for Dom0.
xen/drivers/passthrough/vtd/iommu.c +36 -20
@@ -2414,6 +2414,10 @@ static int cf_check reassign_device_owne {int ret; + ret =domain_context_unmap (source, devfn, pdev); +if ( ret ) +return ret; +/* * If the device belongs to the hardware domain, and it has RMRR, don't * remove it from the hardware domain, because BIOS may use RMRR at
@@ -2442,10 +2446,6 @@ static int cf_check reassign_device_owne } } - ret =domain_context_unmap (source, devfn, pdev); -if ( ret ) -return ret; -if ( devfn == pdev->devfn && pdev->domain != dom_io ) {list_move (&pdev->domain_list, &dom_io->pdev_list);
@@ -2530,9 +2530,8 @@ static int cf_check intel_iommu_assign_d } } - ret =reassign_device_ownership (s, d, devfn, pdev); -if ( ret || d == dom_io ) -return ret; +if ( d == dom_io ) +return reassign_device_ownership (s, d, devfn, pdev);/* Setup rmrr identity mapping */ for_each_rmrr_device ( rmrr, bdf, i )
@@ -2545,20 +2544,37 @@ static int cf_check intel_iommu_assign_d rmrr->end_address, flag);if ( ret ) { -int rc; - - rc =reassign_device_ownership (d, s, devfn, pdev); printk(XENLOG_G_ERR VTDPREFIX -" cannot map reserved region (%" PRIx64",%" PRIx64"] for Dom%d (%d)\n" , - rmrr->base_address, rmrr->end_address, - d->domain_id, ret); -if ( rc ) - { - printk(XENLOG_ERR VTDPREFIX -" failed to reclaim %pp from %pd (%d)\n" , - &PCI_SBDF3(seg, bus, devfn), d, rc); -domain_crash (d); - } +"%pd: cannot map reserved region [%" PRIx64",%" PRIx64"]: %d\n" , + d, rmrr->base_address, rmrr->end_address, ret); +break ; + } + } + } + +if ( !ret ) + ret =reassign_device_ownership (s, d, devfn, pdev); + +/* See reassign_device_ownership() for the hwdom aspect. */ +if ( !ret ||is_hardware_domain (d) ) +return ret; + +for_each_rmrr_device ( rmrr, bdf, i ) + { +if ( rmrr->segment == seg && +PCI_BUS (bdf) == bus && +PCI_DEVFN2 (bdf) == devfn ) + { +int rc =iommu_identity_mapping (d, p2m_access_x, + rmrr->base_address, + rmrr->end_address,0 ); + +if ( rc && rc != -ENOENT ) + { + printk(XENLOG_ERR VTDPREFIX +"%pd: cannot unmap reserved region [%" PRIx64",%" PRIx64"]: %d\n" , + d, rmrr->base_address, rmrr->end_address, rc); +domain_crash (d);break ; } }
From: Jan Beulich <[email protected]>
Subject: VT-d: fix add/remove ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully cleared.
Also switch to %pd in related log messages.
---
v7: Re-base.
v2: New.
xen/drivers/passthrough/vtd/iommu.c +14 -13
@@ -1992,14 +1992,6 @@ static int cf_check intel_iommu_add_deviif ( !pdev->domain )return -EINVAL ; - ret =domain_context_mapping (pdev->domain, devfn, pdev); -if ( ret ) - { - dprintk(XENLOG_ERR VTDPREFIX, "d%d: context mapping failed\n", - pdev->domain->domain_id); -return ret; - } - for_each_rmrr_device ( rmrr, bdf, i ) { if ( rmrr->segment == pdev->seg &&
@@ -2016,12 +2008,17 @@ static int cf_check intel_iommu_add_devi rmrr->base_address, rmrr->end_address, 0);if ( ret ) - dprintk(XENLOG_ERR VTDPREFIX, "d%d: RMRR mapping failed\n", - pdev->domain->domain_id); + dprintk(XENLOG_ERR VTDPREFIX, "%pd: RMRR mapping failed\n", + pdev->domain); } } -return 0 ; + ret =domain_context_mapping (pdev->domain, devfn, pdev); +if ( ret ) + dprintk(XENLOG_ERR VTDPREFIX, "%pd: context mapping failed\n", + pdev->domain); + +return ret; } static int cf_check intel_iommu_enable_device(struct pci_dev *pdev)
@@ -2043,11 +2040,15 @@ static int cf_check intel_iommu_remove_d {struct acpi_rmrr_unit *rmrr;u16 bdf; -int i; +int ret, i;if ( !pdev->domain )return -EINVAL ; + ret =domain_context_unmap (pdev->domain, devfn, pdev); +if ( ret ) +return ret; + for_each_rmrr_device ( rmrr, bdf, i ) { if ( rmrr->segment != pdev->seg ||
@@ -2063,7 +2064,7 @@ static int cf_check intel_iommu_remove_d rmrr->end_address, 0); } -return domain_context_unmap (pdev->domain, devfn, pdev); +return 0 ; } static int __hwdom_init cf_check setup_hwdom_device(
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: tighten iommu_alloc_pgtable()'s parameter
This is to make more obvious that nothing outside of domain_iommu(d) actually changes or is otherwise needed by the function.
No functional change intended.
---
v3: Re-base.
v2: New.
xen/arch/x86/include/asm/iommu.h +2 -1
@@ -142,7 +142,8 @@ static inline void iommu_sync_cache(cons }int __must_check iommu_free_pgtables (struct domain *d); -struct page_info *__must_check iommu_alloc_pgtable (struct domain *d); +struct domain_iommu ; +struct page_info *__must_check iommu_alloc_pgtable (struct domain_iommu *hd);#endif /* !__ARCH_X86_IOMMU_H__ */ /*
xen/drivers/passthrough/amd/iommu_map.c +5 -5
@@ -184,7 +184,7 @@ static int iommu_pde_from_dfn(struct domunsigned long next_table_mfn;unsigned int level;struct page_info *table; -const struct domain_iommu *hd =dom_iommu (d); +struct domain_iommu *hd =dom_iommu (d); table = hd->arch.amd.root_table; level = hd->arch.amd.paging_mode;
@@ -219,7 +219,7 @@ static int iommu_pde_from_dfn(struct dom mfn = next_table_mfn;/* allocate lower level page table */ - table =iommu_alloc_pgtable (d); + table =iommu_alloc_pgtable (hd);if ( table ==NULL ) {AMD_IOMMU_ERROR ("cannot allocate I/O page table\n" );
@@ -249,7 +249,7 @@ static int iommu_pde_from_dfn(struct domif ( next_table_mfn ==0 ) { - table =iommu_alloc_pgtable (d); + table =iommu_alloc_pgtable (hd);if ( table ==NULL ) {AMD_IOMMU_ERROR ("cannot allocate I/O page table\n" );
@@ -555,7 +555,7 @@ int __init cf_check amd_iommu_quarantinespin_lock (&hd->arch.mapping_lock); - hd->arch.amd.root_table =iommu_alloc_pgtable (d); + hd->arch.amd.root_table =iommu_alloc_pgtable (hd);if ( !hd->arch.amd.root_table ) goto out;
@@ -570,7 +570,7 @@ int __init cf_check amd_iommu_quarantine* page table pages, and the resulting allocations are always * zeroed. */ - pg =iommu_alloc_pgtable (d); + pg =iommu_alloc_pgtable (hd);if ( !pg )break ;
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
@@ -242,7 +242,7 @@ int amd_iommu_alloc_root(struct domain *if (unlikely (!hd->arch.amd.root_table) ) { - hd->arch.amd.root_table =iommu_alloc_pgtable (d); + hd->arch.amd.root_table =iommu_alloc_pgtable (hd);if ( !hd->arch.amd.root_table )return -ENOMEM ; }
xen/drivers/passthrough/vtd/iommu.c +4 -4
@@ -318,7 +318,7 @@ static u64 addr_to_dma_page_maddr(struct {struct page_info *pg; -if ( !alloc || !(pg =iommu_alloc_pgtable (domain)) ) +if ( !alloc || !(pg =iommu_alloc_pgtable (hd)) ) goto out; hd->arch.vtd.pgd_maddr =page_to_maddr (pg);
@@ -338,7 +338,7 @@ static u64 addr_to_dma_page_maddr(structif ( !alloc )break ; - pg =iommu_alloc_pgtable (domain); + pg =iommu_alloc_pgtable (hd);if ( !pg )break ;
@@ -2763,7 +2763,7 @@ static int __init cf_check intel_iommu_q goto out; } - pg =iommu_alloc_pgtable (d); + pg =iommu_alloc_pgtable (hd); rc = -ENOMEM ;if ( !pg )
@@ -2782,7 +2782,7 @@ static int __init cf_check intel_iommu_q* page table pages, and the resulting allocations are always * zeroed. */ - pg =iommu_alloc_pgtable (d); + pg =iommu_alloc_pgtable (hd);if ( !pg ) goto out;
xen/drivers/passthrough/x86/iommu.c +1 -2
@@ -420,9 +420,8 @@ int iommu_free_pgtables(struct domain *dreturn 0 ; } -struct page_info *iommu_alloc_pgtable (struct domain *d) +struct page_info *iommu_alloc_pgtable (struct domain_iommu *hd) { -struct domain_iommu *hd =dom_iommu (d);unsigned int memflags =0 ;struct page_info *pg;void *p;
From: Jan Beulich <[email protected]>
Subject: VT-d: split domid map cleanup check into a function
This logic will want invoking from elsewhere.
No functional change intended.
xen/drivers/passthrough/vtd/iommu.c +64 -40
122 lines not on this page · raw message
From: Jan Beulich <[email protected]>
Subject: VT-d: fix (de)assign ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully updated.
Also adjust a related log message.
This is CVE-2022-26358 / part of XSA-400.
xen/drivers/passthrough/vtd/iommu.c +36 -20
81 lines not on this page · raw message
From: Jan Beulich <[email protected]>
Subject: VT-d: fix add/remove ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully cleared.
Also switch to %pd in related log messages.
xen/drivers/passthrough/vtd/iommu.c +14 -13
@@ -1985,14 +1985,6 @@ static int intel_iommu_add_device(u8 devif ( !pdev->domain )return -EINVAL ; - ret =domain_context_mapping (pdev->domain, devfn, pdev); -if ( ret ) - { - dprintk(XENLOG_ERR VTDPREFIX, "d%d: context mapping failed\n", - pdev->domain->domain_id); -return ret; - } - for_each_rmrr_device ( rmrr, bdf, i ) { if ( rmrr->segment == pdev->seg &&
@@ -2009,12 +2001,17 @@ static int intel_iommu_add_device(u8 dev rmrr->base_address, rmrr->end_address, 0);if ( ret ) - dprintk(XENLOG_ERR VTDPREFIX, "d%d: RMRR mapping failed\n", - pdev->domain->domain_id); + dprintk(XENLOG_ERR VTDPREFIX, "%pd: RMRR mapping failed\n", + pdev->domain); } } -return 0 ; + ret =domain_context_mapping (pdev->domain, devfn, pdev); +if ( ret ) + dprintk(XENLOG_ERR VTDPREFIX, "%pd: context mapping failed\n", + pdev->domain); + +return ret; }static int intel_iommu_enable_device (struct pci_dev *pdev)
@@ -2036,11 +2033,15 @@ static int intel_iommu_remove_device(u8 {struct acpi_rmrr_unit *rmrr;u16 bdf; -int i; +int ret, i;if ( !pdev->domain )return -EINVAL ; + ret =domain_context_unmap (pdev->domain, devfn, pdev); +if ( ret ) +return ret; + for_each_rmrr_device ( rmrr, bdf, i ) { if ( rmrr->segment != pdev->seg ||
@@ -2056,7 +2057,7 @@ static int intel_iommu_remove_device(u8 rmrr->end_address, 0); } -return domain_context_unmap (pdev->domain, devfn, pdev); +return 0 ; } static int __hwdom_init setup_hwdom_device(u8 devfn, struct pci_dev *pdev)
From: Jan Beulich <[email protected]>
Subject: VT-d: drop ownership checking from domain_context_mapping_one()
Despite putting in quite a bit of effort it was not possible to establish why exactly this code exists (beyond possibly sanity checking). Instead of a subsequent change further complicating this logic, simply get rid of it.
Take the opportunity and move the respective unmap_vtd_domain_page() out of the locked region.
xen/drivers/passthrough/vtd/iommu.c +2 -64
79 lines not on this page · raw message
From: Jan Beulich <[email protected]>
Subject: VT-d: re-assign devices directly
Devices with RMRRs, due to it being unspecified how/when the specified memory regions may get accessed, may not be left disconnected from their respective mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than unmapping the old context and then mapping the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26359 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any RMRRs. The main difference is when it comes to updating context entries, which need to be atomic when there are RMRRs. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
The seemingly complicated choice of non-negative return values for domain_context_mapping_one() is to limit code churn: This way callers passing NULL for pdev don't need fiddling with.
xen/drivers/passthrough/vtd/extern.h +4 -3
@@ -71,7 +71,8 @@ void free_pgtable_maddr(u64 maddr);void *map_vtd_domain_page (u64 maddr);void unmap_vtd_domain_page (void *va);int domain_context_mapping_one (struct domain *domain,struct iommu *iommu, -u8 bus,u8 devfn,const struct pci_dev *); +uint8_t bus,uint8_t devfn, +const struct pci_dev *pdev,unsigned int mode);int domain_context_unmap_one (struct domain *domain,struct iommu *iommu,u8 bus,u8 devfn);int intel_iommu_get_reserved_device_memory (iommu_grdm_t *func,void *ctxt);
@@ -91,8 +92,8 @@ int is_igd_vt_enabled_quirk(void);void platform_quirks_init (void );void vtd_ops_preamble_quirk (struct iommu * iommu);void vtd_ops_postamble_quirk (struct iommu * iommu); -int __must_check me_wifi_quirk (struct domain *domain, -u8 bus,u8 devfn,int map); +int __must_check me_wifi_quirk (struct domain *domain,uint8_t bus, +uint8_t devfn,unsigned int mode);void pci_vtd_quirk (const struct pci_dev *);void quirk_iommu_caps (struct iommu *iommu);
xen/drivers/passthrough/vtd/iommu.c +219 -60
404 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +6 -2
@@ -201,8 +201,12 @@ struct root_entry { do {(root).val |= ((value) & PAGE_MASK_4K);} while(0)struct context_entry { -u64 lo; -u64 hi; +union { +struct { +uint64_t lo, hi; + }; +__uint128_t full; + }; };#define ROOT_ENTRY_NR (PAGE_SIZE_4K/sizeof(struct root_entry))#define context_present (c) ((c).lo & 1)
xen/drivers/passthrough/vtd/quirks.c +8 -6
@@ -330,7 +330,8 @@ void __init platform_quirks_init(void)*/ static int __must_check map_me_phantom_function (struct domain *domain, -u32 dev,int map) +unsigned int dev, +unsigned int mode) {struct acpi_drhd_unit *drhd;struct pci_dev *pdev;
@@ -341,9 +342,9 @@ static int __must_check map_me_phantom_f drhd =acpi_find_matched_drhd_unit (pdev);/* map or unmap ME phantom function */ -if ( map ) +if ( !(mode &UNMAP_ME_PHANTOM_FUNC ) ) rc =domain_context_mapping_one (domain, drhd->iommu,0 , -PCI_DEVFN (dev,7 ),NULL ); +PCI_DEVFN (dev,7 ),NULL , mode);else rc =domain_context_unmap_one (domain, drhd->iommu,0 ,PCI_DEVFN (dev,7 ));
@@ -351,7 +352,8 @@ static int __must_check map_me_phantom_freturn rc; } -int me_wifi_quirk (struct domain *domain,u8 bus,u8 devfn,int map) +int me_wifi_quirk (struct domain *domain,uint8_t bus,uint8_t devfn, +unsigned int mode) {u32 id;int rc =0 ;
@@ -375,7 +377,7 @@ int me_wifi_quirk(struct domain *domain,case 0x423b8086 :case 0x423c8086 :case 0x423d8086 : - rc =map_me_phantom_function (domain,3 , map); + rc =map_me_phantom_function (domain,3 , mode);break ;default :break ;
@@ -401,7 +403,7 @@ int me_wifi_quirk(struct domain *domain,case 0x42388086 :/* Puma Peak */ case 0x422b8086 :case 0x422c8086 : - rc =map_me_phantom_function (domain,22 , map); + rc =map_me_phantom_function (domain,22 , mode);break ;default :break ;
xen/drivers/passthrough/vtd/vtd.h +8 -2
@@ -22,8 +22,14 @@#include <xen/iommu.h> -#define MAP_ME_PHANTOM_FUNC 1 -#define UNMAP_ME_PHANTOM_FUNC 0 +/* +* Values for domain_context_mapping_one()'s and me_wifi_quirk()'s "mode" +* parameters. +*/ +#define MAP_WITH_RMRR (1u << 0) +#define MAP_OWNER_DYING (1u << 1) +#define MAP_SINGLE_DEVICE (1u << 2) +#define UNMAP_ME_PHANTOM_FUNC (1u << 3)/* Allow for both IOAPIC and IOSAPIC. */ #define IO_xAPIC_route_entry IO_APIC_route_entry
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-assign devices directly
Devices with unity map ranges, due to it being unspecified how/when these memory ranges may get accessed, may not be left disconnected from their unity mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than tearing down the old root page table pointer and then establishing the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26360 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any unity map ranges. The main difference is when it comes to updating DTEs, which need to be atomic when there are unity mappings. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +6 -2
@@ -78,8 +78,12 @@ void amd_iommu_share_p2m(struct domain *int get_dma_requestor_id (u16 seg,u16 bdf);void amd_iommu_set_intremap_table (u32 *dte,u64 intremap_ptr,u8 int_valid); -void amd_iommu_set_root_page_table ( -u32 *dte,u64 root_ptr,u16 domain_id,u8 paging_mode,u8 valid); +#define SET_ROOT_VALID (1u << 0) +#define SET_ROOT_WITH_UNITY_MAP (1u << 1) +int __must_check amd_iommu_set_root_page_table ( +uint32_t *dte,uint64_t root_ptr,uint16_t domain_id,uint8_t paging_mode, +unsigned int flags); +paddr_t amd_iommu_get_root_page_table (const uint32_t *dte);void iommu_dte_set_iotlb (u32 *dte,u8 i);void iommu_dte_add_device_entry (u32 *dte,struct ivrs_mappings *ivrs_dev); void iommu_dte_set_guest_cr3(u32 *dte, u16 dom_id, u64 gcr3,
xen/drivers/passthrough/amd/iommu_map.c +110 -6
137 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +123 -50
249 lines not on this page · raw message
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part I)
Arrange for domain ID and page table root to be passed around, the latter in particular to domain_pgd_maddr() such that taking it from the per-domain fields can be overridden.
No functional change intended.
xen/drivers/passthrough/vtd/extern.h +5 -3
@@ -72,9 +72,10 @@ void *map_vtd_domain_page(u64 maddr);void unmap_vtd_domain_page (void *va);int domain_context_mapping_one (struct domain *domain,struct iommu *iommu,uint8_t bus,uint8_t devfn, -const struct pci_dev *pdev,unsigned int mode); +const struct pci_dev *pdev,domid_t domid, +paddr_t pgd_maddr,unsigned int mode);int domain_context_unmap_one (struct domain *domain,struct iommu *iommu, -u8 bus,u8 devfn); +uint8_t bus,uint8_t devfn,domid_t domid);int intel_iommu_get_reserved_device_memory (iommu_grdm_t *func,void *ctxt);unsigned int io_apic_read_remap_rte (unsigned int apic,unsigned int reg);
@@ -93,7 +94,8 @@ void platform_quirks_init(void);void vtd_ops_preamble_quirk (struct iommu * iommu);void vtd_ops_postamble_quirk (struct iommu * iommu);int __must_check me_wifi_quirk (struct domain *domain,uint8_t bus, -uint8_t devfn,unsigned int mode); +uint8_t devfn,domid_t domid,paddr_t pgd_maddr, +unsigned int mode);void pci_vtd_quirk (const struct pci_dev *);void quirk_iommu_caps (struct iommu *iommu);
xen/drivers/passthrough/vtd/iommu.c +41 -21
176 lines not on this page · raw message
xen/drivers/passthrough/vtd/quirks.c +8 -5
@@ -331,6 +331,8 @@ void __init platform_quirks_init(void)static int __must_check map_me_phantom_function (struct domain *domain,unsigned int dev, +domid_t domid, +paddr_t pgd_maddr,unsigned int mode) {struct acpi_drhd_unit *drhd;
@@ -344,16 +346,17 @@ static int __must_check map_me_phantom_f/* map or unmap ME phantom function */ if ( !(mode &UNMAP_ME_PHANTOM_FUNC ) ) rc =domain_context_mapping_one (domain, drhd->iommu,0 , -PCI_DEVFN (dev,7 ),NULL , mode); +PCI_DEVFN (dev,7 ),NULL , + domid, pgd_maddr, mode);else rc =domain_context_unmap_one (domain, drhd->iommu,0 , -PCI_DEVFN (dev,7 )); +PCI_DEVFN (dev,7 ), domid);return rc; }int me_wifi_quirk (struct domain *domain,uint8_t bus,uint8_t devfn, -unsigned int mode) +domid_t domid,paddr_t pgd_maddr,unsigned int mode) {u32 id;int rc =0 ;
@@ -377,7 +380,7 @@ int me_wifi_quirk(struct domain *domain,case 0x423b8086 :case 0x423c8086 :case 0x423d8086 : - rc =map_me_phantom_function (domain,3 , mode); + rc =map_me_phantom_function (domain,3 , domid, pgd_maddr, mode);break ;default :break ;
@@ -403,7 +406,7 @@ int me_wifi_quirk(struct domain *domain,case 0x42388086 :/* Puma Peak */ case 0x422b8086 :case 0x422c8086 : - rc =map_me_phantom_function (domain,22 , mode); + rc =map_me_phantom_function (domain,22 , domid, pgd_maddr, mode);break ;default :break ;
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part II)
Replace the passing of struct domain * by domid_t in preparation of per-device quarantine page tables also requiring per-device pseudo domain IDs, which aren't going to be associated with any struct domain instances.
No functional change intended (except for slightly adjusted log message text).
xen/drivers/passthrough/vtd/iommu.c +17 -16
@@ -52,8 +52,8 @@ static struct tasklet vtd_fault_tasklet;static int setup_hwdom_device (u8 devfn,struct pci_dev *);static void setup_hwdom_rmrr (struct domain *d); -static int domain_iommu_domid (struct domain *d, -struct iommu *iommu) +static int get_iommu_did (domid_t domid,const struct iommu *iommu, +bool warn) {unsigned long nr_dom, i;
@@ -61,23 +61,24 @@ static int domain_iommu_domid(struct dom i =find_first_bit (iommu->domid_bitmap, nr_dom);while ( i < nr_dom ) { -if ( iommu->domid_map[i] == d->domain_id ) +if ( iommu->domid_map[i] == domid )return i; i =find_next_bit (iommu->domid_bitmap, nr_dom, i+1 ); } - dprintk(XENLOG_ERR VTDPREFIX, -"Cannot get valid iommu domid: domid=%d iommu->index=%d\n" , - d->domain_id, iommu->index); +if ( warn ) + dprintk(XENLOG_ERR VTDPREFIX, +"No valid iommu %u domid for Dom%d\n" , + iommu->index, domid); +return -1 ; }#define DID_FIELD_WIDTH 16#define DID_HIGH_OFFSET 8static int context_set_domain_id (struct context_entry *context, -struct domain *d, -struct iommu *iommu) +domid_t domid,struct iommu *iommu) {unsigned long nr_dom, i;int found =0 ;
@@ -88,7 +89,7 @@ static int context_set_domain_id(struct i =find_first_bit (iommu->domid_bitmap, nr_dom);while ( i < nr_dom ) { -if ( iommu->domid_map[i] == d->domain_id ) +if ( iommu->domid_map[i] == domid ) { found =1 ;break ;
@@ -104,7 +105,7 @@ static int context_set_domain_id(struct dprintk(XENLOG_ERR VTDPREFIX, "IOMMU: no free domain ids\n");return -EFAULT ; } - iommu->domid_map[i] = d->domain_id; + iommu->domid_map[i] = domid; }set_bit (i, iommu->domid_bitmap);
@@ -131,9 +132,9 @@ static void __init free_intel_iommu(struxfree (intel); } -static void cleanup_domid_map (struct domain *domain,struct iommu *iommu) +static void cleanup_domid_map (domid_t domid,struct iommu *iommu) { -int iommu_domid =domain_iommu_domid (domain, iommu); +int iommu_domid =get_iommu_did (domid, iommu, false);if ( iommu_domid >=0 ) {
@@ -189,7 +190,7 @@ static void check_cleanup_domid_map(struif ( !found ) {clear_bit (iommu->index, &dom_iommu (d)->arch.iommu_bitmap); -cleanup_domid_map (d, iommu); +cleanup_domid_map (d->domain_id, iommu); } }
@@ -670,7 +671,7 @@ static int __must_check iommu_flush_iotlcontinue ; flush_dev_iotlb = !!find_ats_dev_drhd (iommu); - iommu_domid=domain_iommu_domid (d, iommu); + iommu_domid =get_iommu_did (d->domain_id, iommu, !d->is_dying);if ( iommu_domid ==-1 )continue ;
@@ -1495,7 +1496,7 @@ int domain_context_mapping_one(spin_unlock (&hd->arch.mapping_lock); } - rc =context_set_domain_id (&lctxt, domain, iommu); + rc =context_set_domain_id (&lctxt, domid, iommu);if ( rc ) { unlock:
@@ -1803,7 +1804,7 @@ int domain_context_unmap_one(context_clear_entry (*context);iommu_sync_cache (context,sizeof (struct context_entry )); - iommu_domid=domain_iommu_domid (domain, iommu); + iommu_domid =get_iommu_did (domid, iommu, !domain->is_dying);if ( iommu_domid ==-1 ) {spin_unlock (&iommu->lock);
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: maintain a per-device pseudo domain ID
In order to subsequently enable per-device quarantine page tables, we'll need domain-ID-like identifiers to be inserted in the respective device (AMD) or context (Intel) table entries alongside the per-device page table root addresses.
Make use of "real" domain IDs occupying only half of the value range coverable by domid_t.
Note that in VT-d's iommu_alloc() I didn't want to introduce new memory leaks in case of error, but existing ones don't get plugged - that'll be the subject of a later change.
The VT-d changes are slightly asymmetric, but this way we can avoid assigning pseudo domain IDs to devices which would never be mapped while still avoiding to add a new parameter to domain_context_unmap().
xen/include/asm-x86/iommu.h +4 -0
@@ -112,6 +112,10 @@ int pi_update_irte(const struct pi_desc ops->sync_cache (addr, size); \ }) +unsigned long *iommu_init_domid (void ); +domid_t iommu_alloc_domid (unsigned long *map); +void iommu_free_domid (domid_t domid,unsigned long *map); +#endif /* !__ARCH_X86_IOMMU_H__ */ /* * Local variables:
xen/include/asm-x86/pci.h +6 -0
@@ -15,6 +15,12 @@struct arch_pci_dev {vmask_t used_vectors; +/* +* These fields are (de)initialized under pcidevs-lock. Other uses of +* them don't race (de)initialization and hence don't strictly need any +* locking. +*/ +domid_t pseudo_domid; }; int pci_conf_write_intercept(unsigned int seg, unsigned int bdf,
xen/include/asm-x86/amd-iommu.h +1 -0
@@ -97,6 +97,7 @@ struct amd_iommu {struct ring_buffer cmd_buffer;struct ring_buffer event_log;struct ring_buffer ppr_log; +unsigned long *domid_map;int exclusion_enable;int exclusion_allow_all;
xen/drivers/passthrough/amd/iommu_detect.c +8 -0
@@ -150,6 +150,11 @@ int __init amd_iommu_detect_one_acpi(if ( rt ) goto out; + iommu->domid_map =iommu_init_domid (); + rt = -ENOMEM ; +if ( !iommu->domid_map ) + goto out; + rt =pci_ro_device (iommu->seg, bus,PCI_DEVFN (dev, func));if ( rt ) printk(XENLOG_ERR
@@ -161,7 +166,10 @@ int __init amd_iommu_detect_one_acpi( out:if ( rt ) + { +xfree (iommu->domid_map);xfree (iommu); + }return rt; }
xen/drivers/passthrough/amd/pci_amd_iommu.c +22 -1
@@ -541,6 +541,8 @@ static int amd_iommu_add_device(u8 devfn {struct amd_iommu *iommu;u16 bdf; +bool fresh_domid = false; +int ret;if ( !pdev->domain )return -EINVAL ;
@@ -565,7 +567,22 @@ static int amd_iommu_add_device(u8 devfnreturn -ENODEV ; } -return amd_iommu_setup_domain_device (pdev->domain, iommu, devfn, pdev); +if ( iommu_quarantine && pdev->arch.pseudo_domid ==DOMID_INVALID ) + { + pdev->arch.pseudo_domid =iommu_alloc_domid (iommu->domid_map); +if ( pdev->arch.pseudo_domid ==DOMID_INVALID ) +return -ENOSPC ; + fresh_domid = true; + } + + ret =amd_iommu_setup_domain_device (pdev->domain, iommu, devfn, pdev); +if ( ret && fresh_domid ) + { +iommu_free_domid (pdev->arch.pseudo_domid, iommu->domid_map); + pdev->arch.pseudo_domid =DOMID_INVALID ; + } + +return ret; }static int amd_iommu_remove_device (u8 devfn,struct pci_dev *pdev)
@@ -587,6 +604,10 @@ static int amd_iommu_remove_device(u8 de }amd_iommu_disable_domain_device (pdev->domain, iommu, devfn, pdev); + +iommu_free_domid (pdev->arch.pseudo_domid, iommu->domid_map); + pdev->arch.pseudo_domid =DOMID_INVALID ; +return 0 ; }
xen/drivers/passthrough/pci.c +8 -4
@@ -343,6 +343,7 @@ static struct pci_dev *alloc_pdev(struct *((u8 *) &pdev->bus) = bus; *((u8 *) &pdev->devfn) = devfn; pdev->domain =NULL ; + pdev->arch.pseudo_domid =DOMID_INVALID ;INIT_LIST_HEAD (&pdev->msi_list); if ( pci_find_cap_offset(pseg->nr, bus, PCI_SLOT(devfn), PCI_FUNC(devfn),
@@ -1311,10 +1312,13 @@ static int _dump_pci_devices(struct pci_ list_for_each_entry ( pdev, &pseg->alldevs_list, alldevs_list ) { -printk ("%04x:%02x:%02x.%u - dom %-3d - node %-3d - MSIs < " , - pseg->nr, pdev->bus, -PCI_SLOT (pdev->devfn),PCI_FUNC (pdev->devfn), - pdev->domain ? pdev->domain->domain_id :-1 , +printk ("%04x:%02x:%02x.%u - " , pseg->nr, pdev->bus, +PCI_SLOT (pdev->devfn),PCI_FUNC (pdev->devfn)); +if ( pdev->domain == dom_io ) +printk ("DomIO:%x" , pdev->arch.pseudo_domid); +else if ( pdev->domain ) +printk ("Dom%d" , pdev->domain->domain_id); +printk (" - node %-3d - MSIs < " , (pdev->node !=NUMA_NO_NODE ) ? pdev->node :-1 ); list_for_each_entry ( msi, &pdev->msi_list, list )printk ("%d " , msi->irq);
xen/drivers/passthrough/vtd/iommu.c +60 -17
183 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -0
@@ -541,6 +541,7 @@ struct iommu {struct msi_desc msi;struct intel_iommu *intel;struct list_head ats_devices; +unsigned long *pseudo_domid_map;/* "pseudo" domain id bitmap */ unsigned long *domid_bitmap;/* domain id bitmap */ u16 *domid_map;/* domain id mapping array */ };
xen/drivers/passthrough/x86/iommu.c +47 -0
53 lines not on this page · raw message
xen/include/public/xen.h +3 -0
@@ -584,6 +584,9 @@ DEFINE_XEN_GUEST_HANDLE(mmuext_op_t);/* Idle domain. */ #define DOMID_IDLE xen_mk_uint(0x7FFF) +/* Mask for valid domain id values */ +#define DOMID_MASK xen_mk_uint(0x7FFF) +#ifndef __ASSEMBLY__typedef uint16_t domid_t ;
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: drop TLB flushes from quarantine_init() hooks
The page tables just created aren't hooked up yet anywhere, so there's nothing that could be present in any TLB, and hence nothing to flush. Dropping this flush is, at least on the VT-d side, a prereq to per- device domain ID use when quarantining devices, as dom_io isn't going to be assigned a DID anymore: The warning in get_iommu_did() would trigger.
xen/drivers/passthrough/amd/iommu_map.c +0 -2
@@ -840,8 +840,6 @@ int __init amd_iommu_quarantine_init(str out:spin_unlock (&hd->arch.mapping_lock); -amd_iommu_flush_all_pages (d); -/* Pages leaked in failure case */ return level ? -ENOMEM :0 ; }
xen/drivers/passthrough/vtd/iommu.c +1 -4
@@ -2858,7 +2858,6 @@ static int __init intel_iommu_quarantinestruct dma_pte *parent;unsigned int agaw =width_to_agaw (DEFAULT_DOMAIN_ADDRESS_WIDTH );unsigned int level =agaw_to_level (agaw); -int rc;if ( hd->arch.pgd_maddr ) {
@@ -2905,10 +2904,8 @@ static int __init intel_iommu_quarantine out:spin_unlock (&hd->arch.mapping_lock); - rc =iommu_flush_iotlb_all (d); -/* Pages leaked in failure case */ -return level ? -ENOMEM : rc; +return level ? -ENOMEM :0 ; } const struct iommu_ops __initconstrel intel_iommu_ops = {
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: abstract maximum number of page table levels
We will want to use the constant elsewhere.
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +1 -1
@@ -185,7 +185,7 @@ static inline int amd_iommu_get_paging_mwhile ( max_frames >PTE_PER_TABLE_SIZE ) { max_frames =PTE_PER_TABLE_ALIGN (max_frames) >>PTE_PER_TABLE_SHIFT ; -if ( ++level >6 ) +if ( ++level >IOMMU_MAX_PT_LEVELS )return -ENOMEM ; }
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +2 -0
@@ -114,6 +114,8 @@#define IOMMU_DEV_TABLE_PAGE_TABLE_PTR_LOW_MASK 0xFFFFF000#define IOMMU_DEV_TABLE_PAGE_TABLE_PTR_LOW_SHIFT 12 +#define IOMMU_MAX_PT_LEVELS 6 +/* DeviceTable Entry[63:32] */ #define IOMMU_DEV_TABLE_GV_SHIFT 23#define IOMMU_DEV_TABLE_GV_MASK 0x800000
xen/drivers/passthrough/amd/iommu_map.c +1 -1
@@ -477,7 +477,7 @@ static int iommu_pde_from_dfn(struct dom table = hd->arch.root_table; level = hd->arch.paging_mode; -BUG_ON ( table ==NULL || level <1 || level >6 ); +BUG_ON ( table ==NULL || level <1 || level >IOMMU_MAX_PT_LEVELS );/* * A frame number past what the current page tables can represent can't
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: use per-device page tables for quarantining
Devices with RMRRs / unity mapped regions, due to it being unspecified how/when these memory regions may be accessed, may not be left disconnected from the mappings of these regions (as long as it's not certain that the device has been fully quiesced). Hence even the page tables used when quarantining such devices need to have mappings of those regions. This implies installing page tables in the first place even when not in scratch-page quarantining mode.
This is CVE-2022-26361 / part of XSA-400.
While for the purpose here it would be sufficient to have devices with RMRRs / unity mapped regions use per-device page tables, extend this to all devices (in scratch-page quarantining mode). This allows the leaf pages to be mapped r/w, thus covering also memory writes (rather than just reads) issued by non-quiescent devices.
Set up quarantine page tables as late as possible, yet early enough to not encounter failure during de-assign. This means setup generally happens in assign_device(), while (for now) the one in deassign_device() is there mainly to be on the safe side.
In VT-d's DID allocation function don't require the IOMMU lock to be held anymore: All involved code paths hold pcidevs_lock, so this way we avoid the need to acquire the IOMMU lock around the new call to context_set_domain_id().
xen/arch/x86/mm/p2m.c +1 -1
@@ -1424,7 +1424,7 @@ int set_identity_p2m_entry(struct domainstruct p2m_domain *p2m =p2m_get_hostp2m (d);int ret; -if ( !paging_mode_translate (p2m->domain) ) +if ( !paging_mode_translate (d) ) {if ( !has_iommu_pt (d) )return 0 ;
xen/include/asm-x86/pci.h +13 -0
@@ -1,6 +1,8 @@#ifndef __X86_PCI_H__#define __X86_PCI_H__ +#include <xen/mm.h> +#define CF8_BDF (cf8) ( ((cf8) & 0x00ffff00) >> 8)#define CF8_ADDR_LO (cf8) ( (cf8) & 0x000000fc)#define CF8_ADDR_HI (cf8) ( ((cf8) & 0x0f000000) >> 16)
@@ -20,7 +22,18 @@ struct arch_pci_dev {* them don't race (de)initialization and hence don't strictly need any * locking. */ +union { +/* Subset of struct arch_iommu's fields, to be used in dom_io. */ +struct { +uint64_t pgd_maddr; + } vtd; +struct { +struct page_info *root_table; + } amd; + };domid_t pseudo_domid; +mfn_t leaf_mfn; +struct page_list_head pgtables_list; }; int pci_conf_write_intercept(unsigned int seg, unsigned int bdf,
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +2 -1
@@ -51,7 +51,8 @@ void get_iommu_features(struct amd_iommuint amd_iommu_init (void );int amd_iommu_update_ivrs_mapping_acpi (void ); -int amd_iommu_quarantine_init (struct domain *d); +int amd_iommu_quarantine_init (struct pci_dev *pdev); +void amd_iommu_quarantine_teardown (struct pci_dev *pdev);/* mapping functions */ int __must_check amd_iommu_map_page(struct domain *d, dfn_t dfn,
xen/drivers/passthrough/amd/iommu_map.c +127 -41
191 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +28 -11
@@ -150,6 +150,8 @@ static int __must_check amd_iommu_setup_u8 bus = pdev->bus;struct domain_iommu *hd =dom_iommu (domain);const struct ivrs_mappings *ivrs_dev; +const struct page_info *root_pg; +domid_t domid;BUG_ON (!hd->arch.paging_mode || !iommu->dev_table.buffer);
@@ -172,14 +174,25 @@ static int __must_check amd_iommu_setup_ dte = iommu->dev_table.buffer + (req_id *IOMMU_DEV_TABLE_ENTRY_SIZE ); ivrs_dev = &get_ivrs_mappings (iommu->seg)[req_id]; +if ( domain != dom_io ) + { + root_pg = hd->arch.root_table; + domid = domain->domain_id; + } +else + { + root_pg = pdev->arch.amd.root_table; + domid = pdev->arch.pseudo_domid; + } +spin_lock_irqsave (&iommu->lock, flags);if ( !is_translation_valid ((u32 *)dte) ) {/* bind DTE to domain page-tables */ rc =amd_iommu_set_root_page_table ( - dte,page_to_maddr (hd->arch.root_table), - domain->domain_id, hd->arch.paging_mode, sr_flags); + dte,page_to_maddr (root_pg), domid, + hd->arch.paging_mode, sr_flags);if ( rc ) {ASSERT (rc <0 );
@@ -193,8 +206,7 @@ static int __must_check amd_iommu_setup_amd_iommu_flush_device (iommu, req_id); } -else if (amd_iommu_get_root_page_table (dte) != -page_to_maddr (hd->arch.root_table) ) +else if (amd_iommu_get_root_page_table (dte) !=page_to_maddr (root_pg) ) {/* * Strictly speaking if the device is the only one with this requestor
@@ -207,8 +219,8 @@ static int __must_check amd_iommu_setup_ rc = -EOPNOTSUPP ; else rc =amd_iommu_set_root_page_table ( - dte,page_to_maddr (hd->arch.root_table), - domain->domain_id, hd->arch.paging_mode, sr_flags); + dte,page_to_maddr (root_pg), domid, + hd->arch.paging_mode, sr_flags);if ( rc <0 ) {spin_unlock_irqrestore (&iommu->lock, flags);
@@ -227,6 +239,7 @@ static int __must_check amd_iommu_setup_* intended anyway. */ !pdev->domain->is_dying && + pdev->domain != dom_io && (any_pdev_behind_iommu(pdev->domain, pdev, iommu) || pdev->phantom_stride) ) printk(" %04x:%02x:%02x.%u: reassignment may cause %pd data corruption\n",
@@ -247,9 +260,8 @@ static int __must_check amd_iommu_setup_AMD_IOMMU_DEBUG ("Setup I/O page table: device id = %#x, type = %#x, " "root table = %#" PRIx64", " "domain = %d, paging mode = %d\n" , - req_id, pdev->type, -page_to_maddr (hd->arch.root_table), - domain->domain_id, hd->arch.paging_mode); + req_id, pdev->type,page_to_maddr (root_pg), + domid, hd->arch.paging_mode);ASSERT (pcidevs_locked ());
@@ -296,7 +308,7 @@ int __init amd_iov_detect(void)int amd_iommu_alloc_root (struct domain_iommu *hd) { -if (unlikely (!hd->arch.root_table) ) +if (unlikely (!hd->arch.root_table) && hd !=dom_iommu (dom_io) ) { hd->arch.root_table =alloc_amd_iommu_pgtable ();if ( !hd->arch.root_table )
@@ -376,7 +388,10 @@ void amd_iommu_disable_domain_device(strAMD_IOMMU_DEBUG ("Disable: device id = %#x, " "domain = %d, paging mode = %d\n" , - req_id, domain->domain_id, + req_id, +get_field_from_reg_u32 (((uint32_t *)dte)[2 ], +IOMMU_DEV_TABLE_DOMAIN_ID_MASK , +IOMMU_DEV_TABLE_DOMAIN_ID_SHIFT ),dom_iommu (domain)->arch.paging_mode); }spin_unlock_irqrestore (&iommu->lock, flags);
@@ -605,6 +620,8 @@ static int amd_iommu_remove_device(u8 deamd_iommu_disable_domain_device (pdev->domain, iommu, devfn, pdev); +amd_iommu_quarantine_teardown (pdev); +iommu_free_domid (pdev->arch.pseudo_domid, iommu->domid_map); pdev->arch.pseudo_domid =DOMID_INVALID ;
xen/drivers/passthrough/iommu.c +8 -8
@@ -497,19 +497,19 @@ int iommu_iotlb_flush_all(struct domainreturn rc; } -static int __init iommu_quarantine_init (void ) +int iommu_quarantine_dev_init (device_t *dev) {const struct domain_iommu *hd =dom_iommu (dom_io); -int rc; - - rc =iommu_domain_init (dom_io); -if ( rc ) -return rc; -if ( !hd->platform_ops->quarantine_init ) +if ( !iommu_quarantine || !hd->platform_ops->quarantine_init )return 0 ; -return hd->platform_ops->quarantine_init (dom_io); +return hd->platform_ops->quarantine_init (dev); +} + +static int __init iommu_quarantine_init (void ) +{ +return iommu_domain_init (dom_io); }int __init iommu_setup (void )
xen/drivers/passthrough/pci.c +17 -3
@@ -1512,6 +1512,13 @@ static int assign_device(struct domain *msixtbl_init (d); } +if ( pdev->domain != dom_io ) + { + rc =iommu_quarantine_dev_init (pci_to_dev (pdev)); +if ( rc ) + goto done; + } + pdev->fault.count =0 ;if ( (rc = hd->platform_ops->assign_device (d, devfn,pci_to_dev (pdev), flag)) )
@@ -1558,9 +1565,16 @@ int deassign_device(struct domain *d, u1return -ENODEV ;/* De-assignment from dom_io should de-quarantine the device */ - target = ((pdev->quarantine || iommu_quarantine) && - pdev->domain != dom_io) ? - dom_io : hardware_domain; +if ( (pdev->quarantine || iommu_quarantine) && pdev->domain != dom_io ) + { + ret =iommu_quarantine_dev_init (pci_to_dev (pdev)); +if ( ret ) +return ret; + + target = dom_io; + } +else + target = hardware_domain;while ( pdev->phantom_stride ) {
xen/drivers/passthrough/vtd/iommu.c +179 -68
375 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/include/xen/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: split domid map cleanup check into a function
This logic will want invoking from elsewhere.
No functional change intended.
xen/drivers/passthrough/vtd/iommu.c +47 -23
70 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix (de)assign ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully updated.
Also adjust a related log message.
This is CVE-2022-26358 / part of XSA-400.
xen/drivers/passthrough/vtd/iommu.c +36 -20
56 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix add/remove ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully cleared.
Also switch to %pd in related log messages.
xen/drivers/passthrough/vtd/iommu.c +14 -13
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: drop ownership checking from domain_context_mapping_one()
Despite putting in quite a bit of effort it was not possible to establish why exactly this code exists (beyond possibly sanity checking). Instead of a subsequent change further complicating this logic, simply get rid of it.
Take the opportunity and move the respective unmap_vtd_domain_page() out of the locked region.
xen/drivers/passthrough/vtd/iommu.c +2 -64
66 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: re-assign devices directly
Devices with RMRRs, due to it being unspecified how/when the specified memory regions may get accessed, may not be left disconnected from their respective mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than unmapping the old context and then mapping the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26359 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any RMRRs. The main difference is when it comes to updating context entries, which need to be atomic when there are RMRRs. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
The seemingly complicated choice of non-negative return values for domain_context_mapping_one() is to limit code churn: This way callers passing NULL for pdev don't need fiddling with.
xen/drivers/passthrough/vtd/extern.h +4 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +207 -61
268 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +6 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/quirks.c +8 -6
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/vtd.h +8 -2
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-assign devices directly
Devices with unity map ranges, due to it being unspecified how/when these memory ranges may get accessed, may not be left disconnected from their unity mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than tearing down the old root page table pointer and then establishing the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26360 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any unity map ranges. The main difference is when it comes to updating DTEs, which need to be atomic when there are unity mappings. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +7 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +64 -3
67 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +129 -51
180 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part I)
Arrange for domain ID and page table root to be passed around, the latter in particular to domain_pgd_maddr() such that taking it from the per-domain fields can be overridden.
No functional change intended.
xen/drivers/passthrough/vtd/extern.h +5 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +41 -21
62 lines not on this page · raw message
xen/drivers/passthrough/vtd/quirks.c +8 -5
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part II)
Replace the passing of struct domain * by domid_t in preparation of per-device quarantine page tables also requiring per-device pseudo domain IDs, which aren't going to be associated with any struct domain instances.
No functional change intended (except for slightly adjusted log message text).
xen/drivers/passthrough/vtd/iommu.c +16 -17
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: maintain a per-device pseudo domain ID
In order to subsequently enable per-device quarantine page tables, we'll need domain-ID-like identifiers to be inserted in the respective device (AMD) or context (Intel) table entries alongside the per-device page table root addresses.
Make use of "real" domain IDs occupying only half of the value range coverable by domid_t.
Note that in VT-d's iommu_alloc() I didn't want to introduce new memory leaks in case of error, but existing ones don't get plugged - that'll be the subject of a later change.
The VT-d changes are slightly asymmetric, but this way we can avoid assigning pseudo domain IDs to devices which would never be mapped while still avoiding to add a new parameter to domain_context_unmap().
xen/include/asm-x86/iommu.h +4 -0
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/pci.h +6 -0
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/amd-iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_detect.c +8 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +21 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +8 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +57 -12
69 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/x86/iommu.c +47 -0
47 lines not on this page · raw message
xen/include/public/xen.h +3 -0
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: drop TLB flushes from quarantine_init() hooks
The page tables just created aren't hooked up yet anywhere, so there's nothing that could be present in any TLB, and hence nothing to flush. Dropping this flush is, at least on the VT-d side, a prereq to per- device domain ID use when quarantining devices, as dom_io isn't going to be assigned a DID anymore: The warning in get_iommu_did() would trigger.
xen/drivers/passthrough/amd/iommu_map.c +0 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +1 -4
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: abstract maximum number of page table levels
We will want to use the constant elsewhere.
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +1 -1
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/hvm/svm/amd-iommu-defs.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +1 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: use per-device page tables for quarantining
Devices with RMRRs / unity mapped regions, due to it being unspecified how/when these memory regions may be accessed, may not be left disconnected from the mappings of these regions (as long as it's not certain that the device has been fully quiesced). Hence even the page tables used when quarantining such devices need to have mappings of those regions. This implies installing page tables in the first place even when not in scratch-page quarantining mode.
This is CVE-2022-26361 / part of XSA-400.
While for the purpose here it would be sufficient to have devices with RMRRs / unity mapped regions use per-device page tables, extend this to all devices (in scratch-page quarantining mode). This allows the leaf pages to be mapped r/w, thus covering also memory writes (rather than just reads) issued by non-quiescent devices.
Set up quarantine page tables as late as possible, yet early enough to not encounter failure during de-assign. This means setup generally happens in assign_device(), while (for now) the one in deassign_device() is there mainly to be on the safe side.
In VT-d's DID allocation function don't require the IOMMU lock to be held anymore: All involved code paths hold pcidevs_lock, so this way we avoid the need to acquire the IOMMU lock around the new call to context_set_domain_id().
xen/arch/x86/mm/p2m.c +1 -1
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/pci.h +13 -0
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/hvm/svm/amd-iommu-proto.h +2 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +114 -41
155 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +25 -10
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/iommu.c +9 -9
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +17 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +179 -68
247 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/include/xen/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix (de)assign ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully updated.
Also adjust a related log message.
This is CVE-2022-26358 / part of XSA-400.
xen/drivers/passthrough/vtd/iommu.c +36 -20
56 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix add/remove ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully cleared.
Also switch to %pd in related log messages.
xen/drivers/passthrough/vtd/iommu.c +14 -13
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: drop ownership checking from domain_context_mapping_one()
Despite putting in quite a bit of effort it was not possible to establish why exactly this code exists (beyond possibly sanity checking). Instead of a subsequent change further complicating this logic, simply get rid of it.
Take the opportunity and move the respective unmap_vtd_domain_page() out of the locked region.
xen/drivers/passthrough/vtd/iommu.c +2 -62
64 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: re-assign devices directly
Devices with RMRRs, due to it being unspecified how/when the specified memory regions may get accessed, may not be left disconnected from their respective mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than unmapping the old context and then mapping the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26359 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any RMRRs. The main difference is when it comes to updating context entries, which need to be atomic when there are RMRRs. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
The seemingly complicated choice of non-negative return values for domain_context_mapping_one() is to limit code churn: This way callers passing NULL for pdev don't need fiddling with.
xen/drivers/passthrough/vtd/extern.h +4 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +206 -61
267 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +6 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/quirks.c +8 -6
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/vtd.h +8 -2
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-assign devices directly
Devices with unity map ranges, due to it being unspecified how/when these memory ranges may get accessed, may not be left disconnected from their unity mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than tearing down the old root page table pointer and then establishing the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26360 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any unity map ranges. The main difference is when it comes to updating DTEs, which need to be atomic when there are unity mappings. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
xen/drivers/passthrough/amd/iommu.h +7 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +64 -3
67 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +129 -51
180 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part I)
Arrange for domain ID and page table root to be passed around, the latter in particular to domain_pgd_maddr() such that taking it from the per-domain fields can be overridden.
No functional change intended.
xen/drivers/passthrough/vtd/extern.h +5 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +41 -21
62 lines not on this page · raw message
xen/drivers/passthrough/vtd/quirks.c +8 -5
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part II)
Replace the passing of struct domain * by domid_t in preparation of per-device quarantine page tables also requiring per-device pseudo domain IDs, which aren't going to be associated with any struct domain instances.
No functional change intended (except for slightly adjusted log message text).
xen/drivers/passthrough/vtd/iommu.c +16 -17
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: maintain a per-device pseudo domain ID
In order to subsequently enable per-device quarantine page tables, we'll need domain-ID-like identifiers to be inserted in the respective device (AMD) or context (Intel) table entries alongside the per-device page table root addresses.
Make use of "real" domain IDs occupying only half of the value range coverable by domid_t.
Note that in VT-d's iommu_alloc() I didn't want to introduce new memory leaks in case of error, but existing ones don't get plugged - that'll be the subject of a later change.
The VT-d changes are slightly asymmetric, but this way we can avoid assigning pseudo domain IDs to devices which would never be mapped while still avoiding to add a new parameter to domain_context_unmap().
xen/include/asm-x86/iommu.h +4 -0
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/pci.h +6 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_detect.c +8 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +21 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +8 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +57 -12
69 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/x86/iommu.c +47 -0
47 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: drop TLB flushes from quarantine_init() hooks
The page tables just created aren't hooked up yet anywhere, so there's nothing that could be present in any TLB, and hence nothing to flush. Dropping this flush is, at least on the VT-d side, a prereq to per- device domain ID use when quarantining devices, as dom_io isn't going to be assigned a DID anymore: The warning in get_iommu_did() would trigger.
xen/drivers/passthrough/amd/iommu_map.c +0 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +1 -4
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: abstract maximum number of page table levels
We will want to use the constant elsewhere.
xen/drivers/passthrough/amd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu-defs.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +1 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: use per-device page tables for quarantining
Devices with RMRRs / unity mapped regions, due to it being unspecified how/when these memory regions may be accessed, may not be left disconnected from the mappings of these regions (as long as it's not certain that the device has been fully quiesced). Hence even the page tables used when quarantining such devices need to have mappings of those regions. This implies installing page tables in the first place even when not in scratch-page quarantining mode.
This is CVE-2022-26361 / part of XSA-400.
While for the purpose here it would be sufficient to have devices with RMRRs / unity mapped regions use per-device page tables, extend this to all devices (in scratch-page quarantining mode). This allows the leaf pages to be mapped r/w, thus covering also memory writes (rather than just reads) issued by non-quiescent devices.
Set up quarantine page tables as late as possible, yet early enough to not encounter failure during de-assign. This means setup generally happens in assign_device(), while (for now) the one in deassign_device() is there mainly to be on the safe side.
In VT-d's DID allocation function don't require the IOMMU lock to be held anymore: All involved code paths hold pcidevs_lock, so this way we avoid the need to acquire the IOMMU lock around the new call to context_set_domain_id().
xen/arch/x86/mm/p2m.c +1 -1
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/pci.h +13 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +114 -41
155 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +25 -10
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/iommu.c +9 -9
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +17 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +179 -68
247 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/include/xen/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix (de)assign ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully updated.
Also adjust a related log message.
This is CVE-2022-26358 / part of XSA-400.
xen/drivers/passthrough/vtd/iommu.c +36 -20
56 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix add/remove ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully cleared.
Also switch to %pd in related log messages.
xen/drivers/passthrough/vtd/iommu.c +14 -13
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: tighten iommu_alloc_pgtable()'s parameter
This is to make more obvious that nothing outside of domain_iommu(d) actually changes or is otherwise needed by the function.
No functional change intended.
xen/include/asm-x86/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +5 -5
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +4 -4
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/x86/iommu.c +1 -2
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: drop ownership checking from domain_context_mapping_one()
Despite putting in quite a bit of effort it was not possible to establish why exactly this code exists (beyond possibly sanity checking). Instead of a subsequent change further complicating this logic, simply get rid of it.
Take the opportunity and move the respective unmap_vtd_domain_page() out of the locked region.
xen/drivers/passthrough/vtd/iommu.c +2 -59
61 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: re-assign devices directly
Devices with RMRRs, due to it being unspecified how/when the specified memory regions may get accessed, may not be left disconnected from their respective mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than unmapping the old context and then mapping the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26359 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any RMRRs. The main difference is when it comes to updating context entries, which need to be atomic when there are RMRRs. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
The seemingly complicated choice of non-negative return values for domain_context_mapping_one() is to limit code churn: This way callers passing NULL for pdev don't need fiddling with.
xen/drivers/passthrough/vtd/extern.h +4 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +205 -60
265 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +6 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/quirks.c +8 -6
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/vtd.h +8 -2
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-assign devices directly
Devices with unity map ranges, due to it being unspecified how/when these memory ranges may get accessed, may not be left disconnected from their unity mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than tearing down the old root page table pointer and then establishing the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26360 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any unity map ranges. The main difference is when it comes to updating DTEs, which need to be atomic when there are unity mappings. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
xen/drivers/passthrough/amd/iommu.h +7 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +64 -3
67 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +128 -51
179 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part I)
Arrange for domain ID and page table root to be passed around, the latter in particular to domain_pgd_maddr() such that taking it from the per-domain fields can be overridden.
No functional change intended.
xen/drivers/passthrough/vtd/extern.h +5 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +41 -21
62 lines not on this page · raw message
xen/drivers/passthrough/vtd/quirks.c +8 -5
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part II)
Replace the passing of struct domain * by domid_t in preparation of per-device quarantine page tables also requiring per-device pseudo domain IDs, which aren't going to be associated with any struct domain instances.
No functional change intended (except for slightly adjusted log message text).
xen/drivers/passthrough/vtd/iommu.c +16 -17
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: maintain a per-device pseudo domain ID
In order to subsequently enable per-device quarantine page tables, we'll need domain-ID-like identifiers to be inserted in the respective device (AMD) or context (Intel) table entries alongside the per-device page table root addresses.
Make use of "real" domain IDs occupying only half of the value range coverable by domid_t.
Note that in VT-d's iommu_alloc() I didn't want to introduce new memory leaks in case of error, but existing ones don't get plugged - that'll be the subject of a later change.
The VT-d changes are slightly asymmetric, but this way we can avoid assigning pseudo domain IDs to devices which would never be mapped while still avoiding to add a new parameter to domain_context_unmap().
xen/include/asm-x86/iommu.h +4 -0
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/pci.h +6 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_detect.c +8 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +21 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +7 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +57 -12
69 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/x86/iommu.c +47 -0
47 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: drop TLB flushes from quarantine_init() hooks
The page tables just created aren't hooked up yet anywhere, so there's nothing that could be present in any TLB, and hence nothing to flush. Dropping this flush is, at least on the VT-d side, a prereq to per- device domain ID use when quarantining devices, as dom_io isn't going to be assigned a DID anymore: The warning in get_iommu_did() would trigger.
xen/drivers/passthrough/amd/iommu_map.c +0 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +0 -3
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: abstract maximum number of page table levels
We will want to use the constant elsewhere.
xen/drivers/passthrough/amd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu-defs.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +1 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: use per-device page tables for quarantining
Devices with RMRRs / unity mapped regions, due to it being unspecified how/when these memory regions may be accessed, may not be left disconnected from the mappings of these regions (as long as it's not certain that the device has been fully quiesced). Hence even the page tables used when quarantining such devices need to have mappings of those regions. This implies installing page tables in the first place even when not in scratch-page quarantining mode.
This is CVE-2022-26361 / part of XSA-400.
While for the purpose here it would be sufficient to have devices with RMRRs / unity mapped regions use per-device page tables, extend this to all devices (in scratch-page quarantining mode). This allows the leaf pages to be mapped r/w, thus covering also memory writes (rather than just reads) issued by non-quiescent devices.
Set up quarantine page tables as late as possible, yet early enough to not encounter failure during de-assign. This means setup generally happens in assign_device(), while (for now) the one in deassign_device() is there mainly to be on the safe side.
In VT-d's DID allocation function don't require the IOMMU lock to be held anymore: All involved code paths hold pcidevs_lock, so this way we avoid the need to acquire the IOMMU lock around the new call to context_set_domain_id().
xen/arch/x86/mm/p2m.c +1 -1
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/pci.h +13 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +112 -41
153 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +25 -10
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/iommu.c +9 -9
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +17 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +172 -73
245 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/include/xen/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix (de)assign ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully updated.
Also adjust a related log message.
This is CVE-2022-26358 / part of XSA-400.
xen/drivers/passthrough/vtd/iommu.c +36 -20
56 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: fix add/remove ordering when RMRRs are in use
In the event that the RMRR mappings are essential for device operation, they should be established before updating the device's context entry, while they should be torn down only after the device's context entry was successfully cleared.
Also switch to %pd in related log messages.
xen/drivers/passthrough/vtd/iommu.c +14 -13
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: tighten iommu_alloc_pgtable()'s parameter
This is to make more obvious that nothing outside of domain_iommu(d) actually changes or is otherwise needed by the function.
No functional change intended.
xen/include/asm-x86/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +5 -5
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +4 -4
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/x86/iommu.c +1 -2
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: drop ownership checking from domain_context_mapping_one()
Despite putting in quite a bit of effort it was not possible to establish why exactly this code exists (beyond possibly sanity checking). Instead of a subsequent change further complicating this logic, simply get rid of it.
Take the opportunity and move the respective unmap_vtd_domain_page() out of the locked region.
xen/drivers/passthrough/vtd/iommu.c +2 -59
61 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: re-assign devices directly
Devices with RMRRs, due to it being unspecified how/when the specified memory regions may get accessed, may not be left disconnected from their respective mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than unmapping the old context and then mapping the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26359 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any RMRRs. The main difference is when it comes to updating context entries, which need to be atomic when there are RMRRs. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
The seemingly complicated choice of non-negative return values for domain_context_mapping_one() is to limit code churn: This way callers passing NULL for pdev don't need fiddling with.
xen/drivers/passthrough/vtd/extern.h +4 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +215 -65
280 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +6 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/quirks.c +8 -6
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/vtd.h +8 -2
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-assign devices directly
Devices with unity map ranges, due to it being unspecified how/when these memory ranges may get accessed, may not be left disconnected from their unity mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than tearing down the old root page table pointer and then establishing the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26360 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any unity map ranges. The main difference is when it comes to updating DTEs, which need to be atomic when there are unity mappings. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
xen/drivers/passthrough/amd/iommu.h +7 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +64 -3
67 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +113 -29
142 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part I)
Arrange for domain ID and page table root to be passed around, the latter in particular to domain_pgd_maddr() such that taking it from the per-domain fields can be overridden.
No functional change intended.
xen/drivers/passthrough/vtd/extern.h +5 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +49 -27
76 lines not on this page · raw message
xen/drivers/passthrough/vtd/quirks.c +8 -5
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part II)
Replace the passing of struct domain * by domid_t in preparation of per-device quarantine page tables also requiring per-device pseudo domain IDs, which aren't going to be associated with any struct domain instances.
No functional change intended (except for slightly adjusted log message text).
xen/drivers/passthrough/vtd/iommu.c +17 -18
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: maintain a per-device pseudo domain ID
In order to subsequently enable per-device quarantine page tables, we'll need domain-ID-like identifiers to be inserted in the respective device (AMD) or context (Intel) table entries alongside the per-device page table root addresses.
Make use of "real" domain IDs occupying only half of the value range coverable by domid_t.
Note that in VT-d's iommu_alloc() I didn't want to introduce new memory leaks in case of error, but existing ones don't get plugged - that'll be the subject of a later change.
The VT-d changes are slightly asymmetric, but this way we can avoid assigning pseudo domain IDs to devices which would never be mapped while still avoiding to add a new parameter to domain_context_unmap().
xen/include/asm-x86/iommu.h +4 -0
Lines of this file are not shown. View the raw message.
xen/include/asm-x86/pci.h +7 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_detect.c +8 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +21 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +8 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +66 -16
82 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/x86/iommu.c +52 -0
52 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: drop TLB flushes from quarantine_init() hooks
The page tables just created aren't hooked up yet anywhere, so there's nothing that could be present in any TLB, and hence nothing to flush. Dropping this flush is, at least on the VT-d side, a prereq to per- device domain ID use when quarantining devices, as dom_io isn't going to be assigned a DID anymore: The warning in get_iommu_did() would trigger.
xen/drivers/passthrough/amd/iommu_map.c +0 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +0 -3
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: abstract maximum number of page table levels
We will want to use the constant elsewhere.
xen/drivers/passthrough/amd/iommu-defs.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: use per-device page tables for quarantining
Devices with RMRRs / unity mapped regions, due to it being unspecified how/when these memory regions may be accessed, may not be left disconnected from the mappings of these regions (as long as it's not certain that the device has been fully quiesced). Hence even the page tables used when quarantining such devices need to have mappings of those regions. This implies installing page tables in the first place even when not in scratch-page quarantining mode.
This is CVE-2022-26361 / part of XSA-400.
While for the purpose here it would be sufficient to have devices with RMRRs / unity mapped regions use per-device page tables, extend this to all devices (in scratch-page quarantining mode). This allows the leaf pages to be mapped r/w, thus covering also memory writes (rather than just reads) issued by non-quiescent devices.
Set up quarantine page tables as late as possible, yet early enough to not encounter failure during de-assign. This means setup generally happens in assign_device(), while (for now) the one in deassign_device() is there mainly to be on the safe side.
As to the removal of QUARANTINE_SKIP() from domain_context_unmap_one():
I think this was never really needed there, as the function explicitly deals with finding a non-present context entry. Leaving it there would require propagating pgd_maddr into the function (like was done by "VT-d:
prepare for per-device quarantine page tables" for domain_context_mapping_one()).
In VT-d's DID allocation function don't require the IOMMU lock to be held anymore: All involved code paths hold pcidevs_lock, so this way we avoid the need to acquire the IOMMU lock around the new call to context_set_domain_id().
xen/include/asm-x86/pci.h +13 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +115 -41
156 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +29 -14
43 lines not on this page · raw message
xen/drivers/passthrough/iommu.c +10 -9
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +17 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +174 -79
253 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/include/xen/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: drop ownership checking from domain_context_mapping_one()
Despite putting in quite a bit of effort it was not possible to establish why exactly this code exists (beyond possibly sanity checking). Instead of a subsequent change further complicating this logic, simply get rid of it.
Take the opportunity and move the respective unmap_vtd_domain_page() out of the locked region.
---
v4: New.
xen/drivers/passthrough/vtd/iommu.c +2 -59
61 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: re-assign devices directly
Devices with RMRRs, due to it being unspecified how/when the specified memory regions may get accessed, may not be left disconnected from their respective mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than unmapping the old context and then mapping the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26359 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any RMRRs. The main difference is when it comes to updating context entries, which need to be atomic when there are RMRRs. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
The seemingly complicated choice of non-negative return values for domain_context_mapping_one() is to limit code churn: This way callers passing NULL for pdev don't need fiddling with.
---
v7: Re-base.
v6: Also include devices with phantom functions in the not-single-device
check. Shorten BDF comparison in domain_context_mapping().
v5: Refuse re-assignment of PCI devices behind a bridge and with RMRR.
Shrink adjustment to context_set_domain_id(). Skip actual write in domain_context_mapping_one() for "same domain" case. Convert asserting of CX16 success to error return. Prefer any_pdev_behind_iommu() over list_{first,last}_entry_or_null(). Refine warning message text and use PCI_SBDF3() there. Use correct domain to decide whether to warn. Move #define-s to vtd.h.
v4: Use atomic context entry updates when possible. Re-base over new
earlier patch.
v3: Unmap on error when the device was previously unmapped.
v2: New.
xen/drivers/passthrough/vtd/extern.h +4 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +217 -67
284 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +6 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/quirks.c +8 -6
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/vtd.h +8 -2
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: re-assign devices directly
Devices with unity map ranges, due to it being unspecified how/when these memory ranges may get accessed, may not be left disconnected from their unity mappings (as long as it's not certain that the device has been fully quiesced). Hence rather than tearing down the old root page table pointer and then establishing the new one, re-assignment needs to be done in a single step.
This is CVE-2022-26360 / part of XSA-400.
Similarly quarantining scratch-page mode relies on page tables to be continuously wired up.
To avoid complicating things more than necessary, treat all devices mostly equally, i.e. regardless of their association with any unity map ranges. The main difference is when it comes to updating DTEs, which need to be atomic when there are unity mappings. Yet atomicity can only be achieved with CMPXCHG16B, availability of which we can't take for given.
---
v6: Also include devices with phantom functions in the not-single-device
check. Add comment ahead of amd_iommu_set_root_page_table().
v5: Refuse re-assignment of devices with requestor ID different from BDF
and with unity map. Convert asserting of CX16 success to error return. Introduce and use any_pdev_behind_iommu(). Refine warning message text and use PCI_SBDF3() there. Use correct domain to decide whether to warn.
v4: Refine when to attempt atomic DTE update. Add warning.
v3: Replace smp_wmb() by comment. Drop a redundant assignment.
v2: New.
xen/drivers/passthrough/amd/iommu.h +7 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +64 -3
67 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +113 -29
142 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part I)
Arrange for domain ID and page table root to be passed around, the latter in particular to domain_pgd_maddr() such that taking it from the per-domain fields can be overridden.
No functional change intended.
---
v7: Re-base.
v5: Also pass around domain ID.
v4: Drop stray insertion of a blank line in the middle of a comment.
Re-base over new earlier patch and other changes earlier in the series.
v3: Re-base over changes earlier in the series.
v2: New.
xen/drivers/passthrough/vtd/extern.h +5 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +49 -27
76 lines not on this page · raw message
xen/drivers/passthrough/vtd/quirks.c +8 -5
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: VT-d: prepare for per-device quarantine page tables (part II)
Replace the passing of struct domain * by domid_t in preparation of per-device quarantine page tables also requiring per-device pseudo domain IDs, which aren't going to be associated with any struct domain instances.
No functional change intended (except for slightly adjusted log message text).
---
v6: context_set_domain_id()'s ASSERT() change moved to a later patch.
v5: New.
xen/drivers/passthrough/vtd/iommu.c +19 -20
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: maintain a per-device pseudo domain ID
In order to subsequently enable per-device quarantine page tables, we'll need domain-ID-like identifiers to be inserted in the respective device (AMD) or context (Intel) table entries alongside the per-device page table root addresses.
Make use of "real" domain IDs occupying only half of the value range coverable by domid_t.
Note that in VT-d's iommu_alloc() I didn't want to introduce new memory leaks in case of error, but existing ones don't get plugged - that'll be the subject of a later change.
The VT-d changes are slightly asymmetric, but this way we can avoid assigning pseudo domain IDs to devices which would never be mapped while still avoiding to add a new parameter to domain_context_unmap().
---
v7: Re-base.
v6: Also adjust nr_dom range check in iommu_alloc().
v5: New.
xen/arch/x86/include/asm/iommu.h +4 -0
Lines of this file are not shown. View the raw message.
xen/arch/x86/include/asm/pci.h +7 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_detect.c +8 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +21 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +8 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +74 -16
90 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/x86/iommu.c +60 -0
60 lines not on this page · raw message
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: drop TLB flushes from quarantine_init() hooks
The page tables just created aren't hooked up yet anywhere, so there's nothing that could be present in any TLB, and hence nothing to flush. Dropping this flush is, at least on the VT-d side, a prereq to per- device domain ID use when quarantining devices, as dom_io isn't going to be assigned a DID anymore: The warning in get_iommu_did() would trigger.
---
v5: This (previously follow-on) patch is now part of this series.
xen/drivers/passthrough/amd/iommu_map.c +0 -2
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +0 -3
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: AMD/IOMMU: abstract maximum number of page table levels
We will want to use the constant elsewhere.
---
v7: New.
xen/drivers/passthrough/amd/iommu-defs.h +1 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/pci_amd_iommu.c +1 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
From: Jan Beulich <[email protected]>
Subject: IOMMU/x86: use per-device page tables for quarantining
Devices with RMRRs / unity mapped regions, due to it being unspecified how/when these memory regions may be accessed, may not be left disconnected from the mappings of these regions (as long as it's not certain that the device has been fully quiesced). Hence even the page tables used when quarantining such devices need to have mappings of those regions. This implies installing page tables in the first place even when not in scratch-page quarantining mode.
This is CVE-2022-26361 / part of XSA-400.
While for the purpose here it would be sufficient to have devices with RMRRs / unity mapped regions use per-device page tables, extend this to all devices (in scratch-page quarantining mode). This allows the leaf pages to be mapped r/w, thus covering also memory writes (rather than just reads) issued by non-quiescent devices.
Set up quarantine page tables as late as possible, yet early enough to not encounter failure during de-assign. This means setup generally happens in assign_device(), while (for now) the one in deassign_device() is there mainly to be on the safe side.
As to the removal of QUARANTINE_SKIP() from domain_context_unmap_one():
I think this was never really needed there, as the function explicitly deals with finding a non-present context entry. Leaving it there would require propagating pgd_maddr into the function (like was done by "VT-d:
prepare for per-device quarantine page tables" for domain_context_mapping_one()).
In VT-d's DID allocation function don't require the IOMMU lock to be held anymore: All involved code paths hold pcidevs_lock, so this way we avoid the need to acquire the IOMMU lock around the new call to context_set_domain_id().
---
v7: Add comment ahead of context_set_domain_id(). Use
IOMMU_MAX_PT_LEVELS. Re-base.
v6: context_set_domain_id()'s ASSERT() change moved here (from earlier
patch). Shorten BDF comparison in intel_iommu_quarantine_init(). Re- base over changes earlier in the series.
v5: Also use per-device (pseudo) domain ID. Suppress corruption warning
for DomIO. Re-base over changes to earlier patches.
v4: Re-base over new earlier patch.
v3: No spinning in quarantine_init() hooks when transiently populating
dom_io's page table root. quarantine_teardown() and arrangements for them redone. Re-base over changes earlier in the series. Extend a comment. Introduce DEVICE_PGTABLE(). Re-base.
v2: New.
xen/arch/x86/include/asm/pci.h +13 -0
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/amd/iommu_map.c +115 -41
156 lines not on this page · raw message
xen/drivers/passthrough/amd/pci_amd_iommu.c +29 -14
43 lines not on this page · raw message
xen/drivers/passthrough/iommu.c +10 -9
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/pci.c +17 -3
Lines of this file are not shown. View the raw message.
xen/drivers/passthrough/vtd/iommu.c +175 -80
255 lines not on this page · raw message
xen/drivers/passthrough/vtd/iommu.h +1 -1
Lines of this file are not shown. View the raw message.
xen/include/xen/iommu.h +2 -1
Lines of this file are not shown. View the raw message.
The rest of this diff is not shown. View the raw message.
Part of the diff is not shown here. View the raw message.