Skip to content

Commit ef1ad01

Browse files
committed
openhcl_tdisp: accept VBS device BARs
1 parent 4544d10 commit ef1ad01

16 files changed

Lines changed: 323 additions & 40 deletions

File tree

‎Guide/src/reference/architecture/openhcl/product_policy.md‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,10 @@ checks no TDI report, so nothing attests the device's identity or
9090
configuration. On VBS, OpenHCL authorizes the device's DMA when the guest first
9191
enables its MMIO and doesn't revoke it while the device is assigned.
9292

93+
`allow_shared_bars` keeps the BARs of those devices shared with the host, which
94+
can then still access them, for hosts that can't make device MMIO private. It
95+
defaults to `false`; only the SIVM policy sets it.
96+
9397
The catalog defines each device once, with any of `vendor_id`, `device_id`,
9498
`revision_id`, `base_class`, `sub_class`, and `prog_if`. A field that is left
9599
out matches any value, but an allowed device must set at least one:
@@ -126,5 +130,5 @@ in `vm/loader/manifests/policies/devices.toml`:
126130
|---|---|---|
127131
| `openhcl-x64-cvm-{dev,release}.toml` | `azure` | NVMe, MANA, and the Azure Integrated HSM; `default_on` |
128132
| `openhcl-x64-{dev,release}.toml`, `openhcl-x64-direct-{dev,release}.toml`, `openhcl-aarch64-{dev,release}.toml` | `azure` | The same devices; `default_off` |
129-
| `openhcl-x64-cvm-sivm-{dev,release}.toml` | `sivm` | A GPU; `must_be_on`; `allow_unattested_devices` |
133+
| `openhcl-x64-cvm-sivm-{dev,release}.toml` | `sivm` | A GPU; `must_be_on`; `allow_unattested_devices`; `allow_shared_bars` |
130134
| `openhcl-x64-test-sivm.toml` | `sivm` | None |

‎openhcl/hcl/src/ioctl.rs‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -804,13 +804,14 @@ impl MshvHvcall {
804804
}
805805
}
806806

807-
/// Accepts VTL 0 pages with no host visibility.
807+
/// Accepts VTL 0 pages with the given host visibility.
808808
///
809809
/// [`HypercallCode::HvCallAcceptGpaPages`] must be allowed.
810810
pub fn accept_gpa_pages(
811811
&self,
812812
range: MemoryRange,
813813
memory_type: hvdef::hypercall::AcceptMemoryType,
814+
host_visibility: HostVisibilityType,
814815
) -> Result<(), AcceptPagesError> {
815816
const MAX_INPUT_ELEMENTS: usize = (HV_PAGE_SIZE as usize
816817
- size_of::<hvdef::hypercall::AcceptGpaPages>())
@@ -827,7 +828,7 @@ impl MshvHvcall {
827828
partition_id: HV_PARTITION_ID_SELF,
828829
page_attributes: hvdef::hypercall::AcceptPagesAttributes::new()
829830
.with_memory_type(memory_type.0)
830-
.with_host_visibility(HostVisibilityType::PRIVATE)
831+
.with_host_visibility(host_visibility)
831832
.with_vtl_set(0), // vtl protections cannot be applied for VTL 0 memory
832833
vtl_permission_set: hvdef::hypercall::VtlPermissionSet {
833834
vtl_permission_from_1: [0; hvdef::hypercall::HV_VTL_PERMISSION_SET_SIZE],

‎openhcl/openhcl_tdisp/src/client.rs‎

Lines changed: 80 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -81,13 +81,16 @@ impl TdispClient {
8181
/// * `bar_masks` - Which BARs the device implements.
8282
/// * `allow_unattested_devices` - Allow the device without TDISP attestation,
8383
/// skipping the steps that need it.
84+
/// * `allow_shared_bars` - Keep the BARs of a device without TDISP
85+
/// attestation shared with the host.
8486
pub fn new(
8587
transport: Box<dyn TdispCommandTransport>,
8688
resource_validator: Arc<dyn TdispResourceValidationInterface>,
8789
isolation_type: IsolationType,
8890
target_vtl: Vtl,
8991
bar_masks: [bool; 6],
9092
allow_unattested_devices: bool,
93+
allow_shared_bars: bool,
9194
) -> Self {
9295
Self(futures::lock::Mutex::new(TdispClientState::new(
9396
transport,
@@ -96,6 +99,7 @@ impl TdispClient {
9699
target_vtl,
97100
bar_masks,
98101
allow_unattested_devices,
102+
allow_shared_bars,
99103
)))
100104
}
101105

@@ -330,6 +334,9 @@ struct TdispClientState {
330334
/// Allow the device without TDISP attestation, skipping the steps that
331335
/// need it.
332336
allow_unattested_devices: bool,
337+
/// Keep the BARs of a device without TDISP attestation shared with the
338+
/// host.
339+
allow_shared_bars: bool,
333340
}
334341

335342
impl TdispClientState {
@@ -340,6 +347,7 @@ impl TdispClientState {
340347
target_vtl: Vtl,
341348
bar_masks: [bool; 6],
342349
allow_unattested_devices: bool,
350+
allow_shared_bars: bool,
343351
) -> Self {
344352
Self {
345353
transport,
@@ -356,6 +364,7 @@ impl TdispClientState {
356364
bar_masks,
357365
resource_validator,
358366
allow_unattested_devices,
367+
allow_shared_bars,
359368
}
360369
}
361370

@@ -769,7 +778,10 @@ impl TdispClientState {
769778
/// panic.
770779
async fn unbind(&mut self, reason: TdispGuestUnbindReason) {
771780
if self.allow_unattested_devices {
772-
// Nothing was bound on the host, and DMA stays unblocked.
781+
// Nothing was bound on the host, and DMA stays unblocked. The host
782+
// unmaps the BARs, so they're unblocked again on the next
783+
// activation.
784+
self.mutable_state.validated_mmio_bars.clear();
773785
return;
774786
}
775787

@@ -1179,6 +1191,18 @@ impl TdispClientState {
11791191
return TdispResourceIsolation::Shared;
11801192
}
11811193

1194+
// Without a TDI report, every other BAR the device implements is
1195+
// private, unless the policy keeps them shared.
1196+
if self.allow_unattested_devices {
1197+
return if !self.bar_masks[usize::from(bar_id)] {
1198+
TdispResourceIsolation::Invalid
1199+
} else if self.allow_shared_bars {
1200+
TdispResourceIsolation::Shared
1201+
} else {
1202+
TdispResourceIsolation::Private
1203+
};
1204+
}
1205+
11821206
// No cached report yet (attestation hasn't run) → we don't know
11831207
// if this BAR is claimed at all, so INVALID rather than SHARED.
11841208
let Some(report) = self.mutable_state.tdi_report.as_ref() else {
@@ -1272,22 +1296,6 @@ impl TdispClientState {
12721296
return Ok(());
12731297
}
12741298

1275-
if self.allow_unattested_devices {
1276-
// There's no TDI report to classify the BAR by, so only unblock DMA.
1277-
if !self.mutable_state.dma_unblocked {
1278-
let device_id = self
1279-
.mutable_state
1280-
.guest_device_id
1281-
.id()
1282-
.context("tdisp_on_mmio_reconfigured: no TDI device id is known")?;
1283-
self.resource_validator
1284-
.tdisp_unblock_dma(self.target_vtl, device_id)
1285-
.context("tdisp_on_mmio_reconfigured: failed to unblock DMA")?;
1286-
self.mutable_state.dma_unblocked = true;
1287-
}
1288-
return Ok(());
1289-
}
1290-
12911299
if self.mutable_state.validated_mmio_bars.contains_key(&bar_id) {
12921300
tracing::debug!(
12931301
bar_id,
@@ -1326,6 +1334,9 @@ impl TdispClientState {
13261334
interface report; treating it as SHARED and skipping the MMIO unblock"
13271335
);
13281336
}
1337+
self.resource_validator
1338+
.tdisp_accept_shared_mmio(self.target_vtl, base_address, length, bar_id)
1339+
.context("tdisp_on_mmio_reconfigured: failed to accept shared MMIO")?;
13291340
// Record the range so we don't repeatedly fall through here
13301341
// on subsequent reconfigurations. Marked `Shared`, which
13311342
// tells the unbind path there is no block call to undo.
@@ -1337,6 +1348,18 @@ impl TdispClientState {
13371348
isolation: TdispResourceIsolation::Shared,
13381349
},
13391350
);
1351+
// With every BAR shared, no private BAR unblocks DMA below.
1352+
if self.allow_shared_bars && !self.mutable_state.dma_unblocked {
1353+
let device_id = self
1354+
.mutable_state
1355+
.guest_device_id
1356+
.id()
1357+
.context("tdisp_on_mmio_reconfigured: no TDI device id is known")?;
1358+
self.resource_validator
1359+
.tdisp_unblock_dma(self.target_vtl, device_id)
1360+
.context("tdisp_on_mmio_reconfigured: failed to unblock DMA")?;
1361+
self.mutable_state.dma_unblocked = true;
1362+
}
13401363
return Ok(());
13411364
}
13421365
TdispResourceIsolation::Invalid => {
@@ -1366,9 +1389,11 @@ impl TdispClientState {
13661389

13671390
// Tell the host before the platform unblocks. Depending on the platform, the host
13681391
// may need to perform bookkeeping operations before the guest can unblock the range.
1369-
self.host_unblock_mmio_range(bar_id, base_address, length)
1370-
.await
1371-
.context("tdisp_on_mmio_reconfigured: failed to unblock MMIO on the host")?;
1392+
if !self.allow_unattested_devices {
1393+
self.host_unblock_mmio_range(bar_id, base_address, length)
1394+
.await
1395+
.context("tdisp_on_mmio_reconfigured: failed to unblock MMIO on the host")?;
1396+
}
13721397

13731398
self.resource_validator
13741399
.tdisp_unblock_mmio(self.target_vtl, device_id, base_address, 0, length, bar_id)
@@ -1454,6 +1479,7 @@ mod tests {
14541479
Vtl::Vtl0,
14551480
present_bars,
14561481
false,
1482+
false,
14571483
)
14581484
}
14591485

@@ -1620,7 +1646,8 @@ mod tests {
16201646
}
16211647

16221648
/// With the host checks bypassed, attestation sends the host no TDISP
1623-
/// command, and DMA is unblocked and stays unblocked across an unbind.
1649+
/// command, and DMA is unblocked and stays unblocked across an unbind. The
1650+
/// BAR is unblocked again after the unbind, as the host remaps it.
16241651
#[test]
16251652
fn bypassed_host_checks_unblock_dma_without_the_host() {
16261653
let validator = Arc::new(crate::noop::TdispNoopResourceValidator::new());
@@ -1631,6 +1658,7 @@ mod tests {
16311658
Vtl::Vtl0,
16321659
[true, true, false, false, false, false],
16331660
true,
1661+
false,
16341662
);
16351663
futures::executor::block_on(async {
16361664
let info = state.query_capabilities().await.unwrap();
@@ -1643,6 +1671,37 @@ mod tests {
16431671
assert!(validator.dma_unblocked());
16441672
state.unbind(TdispGuestUnbindReason::Graceful).await;
16451673
assert!(validator.dma_unblocked());
1674+
state
1675+
.on_mmio_reconfigured(0, 0x1000_0000, 0x1000)
1676+
.await
1677+
.unwrap();
1678+
assert_eq!(validator.unblocked_mmio_ranges().len(), 2);
1679+
});
1680+
}
1681+
1682+
/// With shared BARs allowed, nothing is unblocked as private, and DMA is
1683+
/// unblocked after the first BAR instead.
1684+
#[test]
1685+
fn shared_bars_unblock_dma_without_private_mmio() {
1686+
let validator = Arc::new(crate::noop::TdispNoopResourceValidator::new());
1687+
let mut state = TdispClientState::new(
1688+
Box::new(PanickingTransport),
1689+
validator.clone(),
1690+
IsolationType::Vbs,
1691+
Vtl::Vtl0,
1692+
[true, true, false, false, false, false],
1693+
true,
1694+
true,
1695+
);
1696+
futures::executor::block_on(async {
1697+
let info = state.query_capabilities().await.unwrap();
1698+
state.attest(info).await.unwrap();
1699+
state
1700+
.on_mmio_reconfigured(0, 0x1000_0000, 0x1000)
1701+
.await
1702+
.unwrap();
1703+
assert!(validator.dma_unblocked());
1704+
assert!(validator.unblocked_mmio_ranges().is_empty());
16461705
});
16471706
}
16481707
}

‎openhcl/openhcl_tdisp/src/lib.rs‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ pub use vbs::TdispVbsResourceValidator;
5858
use anyhow::Context;
5959
use guid::Guid;
6060
use hvdef::Vtl;
61+
use memory_range::MemoryRange;
6162
use std::future::Future;
6263
use std::pin::Pin;
6364
use std::sync::Arc;
@@ -168,6 +169,24 @@ pub trait TdispResourceValidationInterface: Send + Sync {
168169
range_id: u16,
169170
) -> Pin<Box<dyn Future<Output = anyhow::Result<()>> + Send + Sync + 'a>>;
170171

172+
/// Make an MMIO range that stays shared with the host reachable from
173+
/// `target_vtl`. Only needed on platforms where the lower VTL can't reach
174+
/// device MMIO until it's accepted, so by default this does nothing.
175+
///
176+
/// * `target_vtl` - The VTL to make the range reachable from.
177+
/// * `base_gpa` - The base guest physical address of the MMIO range.
178+
/// * `length_in_bytes` - The length in bytes of the MMIO range.
179+
/// * `range_id` - The PCI BAR index of the range.
180+
fn tdisp_accept_shared_mmio(
181+
&self,
182+
_target_vtl: Vtl,
183+
_base_gpa: u64,
184+
_length_in_bytes: u64,
185+
_range_id: u16,
186+
) -> anyhow::Result<()> {
187+
Ok(())
188+
}
189+
171190
/// Unblock DMA access for the device's IOMMU domain by asking the platform
172191
/// specific TSM to perform an unblock operation.
173192
///
@@ -220,11 +239,14 @@ pub trait TdispResourceValidationInterface: Send + Sync {
220239
/// validator derives the hypervisor's logical device ID from it. That's
221240
/// temporary, to enable the VBS TDISP flows, which will eventually use the
222241
/// same guest device ID as the other platforms.
242+
/// * `vtl0_mmio` - Where the guest may place device BARs. The VBS validator
243+
/// only accepts BARs inside these ranges.
223244
/// * `is_test_environment` - `true` if running in a test environment.
224245
pub fn new_resource_validator(
225246
isolation: IsolationType,
226247
vtom: Option<u64>,
227248
instance_id: Guid,
249+
vtl0_mmio: &[MemoryRange],
228250
is_test_environment: bool,
229251
) -> anyhow::Result<Arc<dyn TdispResourceValidationInterface>> {
230252
tracing::info!(
@@ -256,7 +278,10 @@ pub fn new_resource_validator(
256278
}
257279

258280
#[cfg(target_os = "linux")]
259-
IsolationType::Vbs => Ok(Arc::new(TdispVbsResourceValidator::new(instance_id)?)),
281+
IsolationType::Vbs => Ok(Arc::new(TdispVbsResourceValidator::new(
282+
instance_id,
283+
vtl0_mmio.to_vec(),
284+
)?)),
260285

261286
// Isolation types with no validator of their own, or running in an
262287
// environment outside of a OpenHCL Linux build

0 commit comments

Comments
 (0)