Skip to content

Commit 5d6ab1c

Browse files
x86: enable guest-managed Intel VT-d on Windows (#5143)
## Summary - set the DMAR DMA-control platform opt-in flag for Intel VT-d guests - mask only the Hyper-V hardware-feature bits that claim DMA and interrupt remapping are hypervisor-managed - apply that CPUID override only when an Intel VT-d unit is configured - add focused DMAR and CPUID tests ## Motivation When OpenVMM runs on WHP, the underlying Hyper-V CPUID hardware-features leaf can report that DMA and interrupt remapping are already managed by the hypervisor. A Windows guest then does not take control of the emulated VT-d unit. Separately, Windows requires the DMAR DMA-control platform opt-in flag before enabling DMA remapping. The CPUID override is masked, so all unrelated hardware-feature bits continue to come from the underlying hypervisor. ## Validation - `cargo xtask fmt --fix --only-diffed` - `cargo test -p vmm_core guest_managed_iommu_leaf_clears_only_iommu_bits` - `cargo test -p vmm_core test_dmar_basic` - `cargo clippy -p acpi_spec -p vmm_core -p openvmm_core --all-targets` - `cargo doc --no-deps -p acpi_spec -p vmm_core -p openvmm_core` - `cargo build --release -p openvmm` The resulting current-main binary was boot-smoked with a Windows guest on WHP using emulated NVMe and Intel VT-d. Windows reported the DMAR opt-in, selected native IOMMU policy, configured four-level IOMMU page tables, and started the NVMe controller and disk. --------- Co-authored-by: Jeff Bromberger <6789497+jeffbromberger@users.noreply.github.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Copilot-Session: bec97463-419f-4fb3-a9ae-e8d73e3257dc Copilot-Session: ae062255-030e-4c06-861c-055fa337171c
1 parent b50d0c9 commit 5d6ab1c

4 files changed

Lines changed: 52 additions & 3 deletions

File tree

‎openvmm/openvmm_core/src/worker/dispatch.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1525,6 +1525,14 @@ impl InitializedVm {
15251525
extended_ioapic_rte,
15261526
confidential_vmbus,
15271527
));
1528+
#[cfg(guest_arch = "x86_64")]
1529+
if cfg
1530+
.pcie_root_complexes
1531+
.iter()
1532+
.any(|rc| matches!(rc.iommu, Some(PcieIommuConfig::IntelVtd)))
1533+
{
1534+
cpuid.push(vmm_core::cpuid::hyperv_guest_managed_iommu_leaf());
1535+
}
15281536
}
15291537

15301538
let (partition, vps) = proto

‎vm/acpi_spec/src/dmar.rs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ pub const DMAR_REVISION: u8 = 1;
2727
/// DMAR flags: interrupt remapping supported (§8.1, Table 8-1).
2828
pub const DMAR_FLAGS_INTR_REMAP: u8 = 0x01;
2929

30+
/// DMAR flags: platform firmware has opted into DMA control (§8.1, Table 8-1).
31+
pub const DMAR_FLAGS_DMA_CTRL_PLATFORM_OPT_IN: u8 = 0x04;
32+
3033
/// DRHD structure type (§8.2).
3134
pub const DMAR_TYPE_DRHD: u16 = 0x0000;
3235

‎vmm_core/src/acpi_builder.rs‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1012,7 +1012,10 @@ impl<T: AcpiTopology> AcpiTablesBuilder<'_, T> {
10121012
(f)(&acpi::builder::Table::new_dyn(
10131013
dmar::DMAR_REVISION,
10141014
None,
1015-
&dmar::Dmar::new(haw, dmar::DMAR_FLAGS_INTR_REMAP),
1015+
&dmar::Dmar::new(
1016+
haw,
1017+
dmar::DMAR_FLAGS_INTR_REMAP | dmar::DMAR_FLAGS_DMA_CTRL_PLATFORM_OPT_IN,
1018+
),
10161019
&[dmar_extra.as_slice()],
10171020
))
10181021
}
@@ -2782,8 +2785,8 @@ mod test {
27822785
let body_offset = 36;
27832786
// HAW = 47 (48-1)
27842787
assert_eq!(dmar[body_offset], 47);
2785-
// Flags: INTR_REMAP = 0x01
2786-
assert_eq!(dmar[body_offset + 1], 0x01);
2788+
// Flags: INTR_REMAP | DMA_CTRL_PLATFORM_OPT_IN = 0x05
2789+
assert_eq!(dmar[body_offset + 1], 0x05);
27872790

27882791
// DRHD structure starts at offset 48 (36 header + 12 body)
27892792
let drhd_offset = 48;

‎vmm_core/src/cpuid/mod.rs‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
//! VM CPUID support.
55
6+
use hvdef::HV_CPUID_FUNCTION_MS_HV_HARDWARE_FEATURES;
67
use hvdef::VIRTUALIZATION_STACK_CPUID_INTERFACE;
78
use hvdef::VIRTUALIZATION_STACK_CPUID_PROPERTIES;
89
use hvdef::VIRTUALIZATION_STACK_CPUID_VENDOR;
@@ -12,6 +13,12 @@ use hvdef::VS1_PARTITION_PROPERTIES_EAX_IS_PORTABLE;
1213
use virt::CpuidLeaf;
1314
use x86defs::cpuid::CpuidFunction;
1415

16+
// Hypervisor-managed DMA and interrupt remapping claims in EAX.
17+
const HYPERV_HARDWARE_FEATURES_IOMMU_IN_USE_MASK: u32 = hvdef::HvHardwareFeatures::new()
18+
.with_dma_remapping_in_use(true)
19+
.with_interrupt_remapping_in_use(true)
20+
.into_bits() as u32;
21+
1522
/// Returns CPUID leaves for Hyper-V-style VMs.
1623
///
1724
/// `extended_ioapic_rte` indicates that MSIs and the IOAPIC can reference a
@@ -66,3 +73,31 @@ pub fn hyperv_cpuid_leaves(
6673
]
6774
.into_iter()
6875
}
76+
77+
/// Returns a CPUID override indicating that the guest, not the hypervisor,
78+
/// manages DMA and interrupt remapping.
79+
pub fn hyperv_guest_managed_iommu_leaf() -> CpuidLeaf {
80+
CpuidLeaf::new(HV_CPUID_FUNCTION_MS_HV_HARDWARE_FEATURES, [0; 4]).masked([
81+
HYPERV_HARDWARE_FEATURES_IOMMU_IN_USE_MASK,
82+
0,
83+
0,
84+
0,
85+
])
86+
}
87+
88+
#[cfg(test)]
89+
mod tests {
90+
use super::*;
91+
92+
#[test]
93+
fn guest_managed_iommu_leaf_clears_only_iommu_bits() {
94+
let leaf = hyperv_guest_managed_iommu_leaf();
95+
let mut result = [u32::MAX; 4];
96+
97+
leaf.apply(&mut result);
98+
99+
assert_eq!(leaf.function, HV_CPUID_FUNCTION_MS_HV_HARDWARE_FEATURES);
100+
assert_eq!(leaf.mask, [0x30, 0, 0, 0]);
101+
assert_eq!(result, [!0x30, u32::MAX, u32::MAX, u32::MAX]);
102+
}
103+
}

0 commit comments

Comments
 (0)