Skip to content

Commit c898724

Browse files
committed
feedback: flip builder direction
1 parent 9dfd455 commit c898724

2 files changed

Lines changed: 56 additions & 63 deletions

File tree

openhcl/underhill_mem/src/init.rs

Lines changed: 27 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use crate::HardwareIsolatedMemoryProtector;
77
use crate::MemoryAcceptor;
88
use crate::mapping::GuestMemoryMapping;
9-
use crate::mapping::GuestPartitionMemoryBuilder;
9+
use crate::mapping::GuestPartitionMemoryView;
1010
use anyhow::Context;
1111
use futures::future::try_join_all;
1212
use guestmem::GuestMemory;
@@ -210,19 +210,17 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
210210
// kernel-registered RAM.
211211

212212
tracing::debug!("Building valid encrypted memory view");
213-
let encrypted_memory_builder = {
213+
let encrypted_memory_view = {
214214
let _span = tracing::info_span!("create encrypted memory view").entered();
215-
GuestPartitionMemoryBuilder::new(params.mem_layout, Some(true))?
215+
GuestPartitionMemoryView::new(params.mem_layout, true)?
216216
};
217217

218218
tracing::debug!("Building VTL0 memory map");
219219
let vtl0_mapping = Arc::new({
220220
let _span = tracing::info_span!("map_vtl0_memory").entered();
221-
encrypted_memory_builder
222-
.build_guest_memory_mapping(
223-
&gpa_fd,
224-
GuestMemoryMapping::builder(0).dma_base_address(None),
225-
)
221+
GuestMemoryMapping::builder(0)
222+
.dma_base_address(None)
223+
.build_with_bitmap(&gpa_fd, &encrypted_memory_view)
226224
.context("failed to map vtl0 memory")?
227225
});
228226

@@ -292,34 +290,28 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
292290

293291
tracing::debug!("Building shared memory map");
294292

295-
let shared_memory_builder = {
293+
let shared_memory_view = {
296294
let _span = tracing::info_span!("create shared memory view").entered();
297-
GuestPartitionMemoryBuilder::new(params.complete_memory_layout, Some(false))?
295+
GuestPartitionMemoryView::new(params.complete_memory_layout, false)?
298296
};
299297

300-
let valid_shared_memory = shared_memory_builder.partition_valid_memory();
298+
let valid_shared_memory = shared_memory_view.partition_valid_memory();
301299

302300
// Update the shared mapping bitmap for pages used by the shared
303301
// visibility pool to be marked as shared, since by default pages are
304302
// marked as no-access in the bitmap.
305303
tracing::debug!("Updating shared mapping bitmaps");
306304
for range in params.shared_pool {
307-
valid_shared_memory
308-
.as_ref()
309-
.unwrap()
310-
.update_valid(range.range, true);
305+
valid_shared_memory.as_ref().update_valid(range.range, true);
311306
}
312307

313308
let shared_mapping = Arc::new({
314309
let _span = tracing::info_span!("map_shared_memory").entered();
315-
shared_memory_builder
316-
.build_guest_memory_mapping(
317-
&gpa_fd,
318-
GuestMemoryMapping::builder(shared_offset)
319-
.shared(true)
320-
.ignore_registration_failure(params.boot_init.is_none())
321-
.dma_base_address(Some(dma_base_address)),
322-
)
310+
GuestMemoryMapping::builder(shared_offset)
311+
.shared(true)
312+
.ignore_registration_failure(params.boot_init.is_none())
313+
.dma_base_address(Some(dma_base_address))
314+
.build_with_bitmap(&gpa_fd, &shared_memory_view)
323315
.context("failed to map shared memory")?
324316
});
325317

@@ -373,11 +365,8 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
373365
let private_vtl0_memory = GuestMemory::new("trusted", vtl0_mapping.clone());
374366

375367
let protector = Arc::new(HardwareIsolatedMemoryProtector::new(
376-
encrypted_memory_builder
377-
.partition_valid_memory()
378-
.unwrap()
379-
.clone(),
380-
valid_shared_memory.unwrap().clone(),
368+
encrypted_memory_view.partition_valid_memory().clone(),
369+
valid_shared_memory.clone(),
381370
vtl0_mapping.clone(),
382371
params.mem_layout.clone(),
383372
acceptor.as_ref().unwrap().clone(),
@@ -396,22 +385,17 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
396385
}),
397386
}
398387
} else {
399-
let partition_memory_builder = GuestPartitionMemoryBuilder::new(params.mem_layout, None)?;
400-
401388
tracing::debug!("Creating VTL0 guest memory");
402389
let vtl0_mapping = {
403390
let _span = tracing::info_span!("map_vtl0_memory").entered();
404391
let base_address = params.vtl0_alias_map_bit.unwrap_or(0);
405392

406393
Arc::new(
407-
partition_memory_builder
408-
.build_guest_memory_mapping(
409-
&gpa_fd,
410-
GuestMemoryMapping::builder(base_address)
411-
.for_kernel_access(true)
412-
.dma_base_address(Some(base_address))
413-
.ignore_registration_failure(params.boot_init.is_none()),
414-
)
394+
GuestMemoryMapping::builder(base_address)
395+
.for_kernel_access(true)
396+
.dma_base_address(Some(base_address))
397+
.ignore_registration_failure(params.boot_init.is_none())
398+
.build_without_bitmap(&gpa_fd, params.mem_layout)
415399
.context("failed to map vtl0 memory")?,
416400
)
417401
};
@@ -442,14 +426,11 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
442426

443427
let _span = tracing::info_span!("map_vtl1_memory").entered();
444428
Some(Arc::new(
445-
partition_memory_builder
446-
.build_guest_memory_mapping(
447-
&gpa_fd,
448-
GuestMemoryMapping::builder(0)
449-
.for_kernel_access(true)
450-
.dma_base_address(Some(0))
451-
.ignore_registration_failure(params.boot_init.is_none()),
452-
)
429+
GuestMemoryMapping::builder(0)
430+
.for_kernel_access(true)
431+
.dma_base_address(Some(0))
432+
.ignore_registration_failure(params.boot_init.is_none())
433+
.build_without_bitmap(&gpa_fd, params.mem_layout)
453434
.context("failed to map vtl1 memory")?,
454435
))
455436
}

openhcl/underhill_mem/src/mapping.rs

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,45 +21,43 @@ use std::sync::Arc;
2121
use thiserror::Error;
2222
use vm_topology::memory::MemoryLayout;
2323

24-
pub struct GuestPartitionMemoryBuilder<'a> {
24+
pub struct GuestPartitionMemoryView<'a> {
2525
memory_layout: &'a MemoryLayout,
26-
valid_memory: Option<Arc<GuestValidMemory>>,
26+
valid_memory: Arc<GuestValidMemory>,
2727
}
2828

29-
impl<'a> GuestPartitionMemoryBuilder<'a> {
30-
/// If `valid_bitmap_state` is `Some`, a bitmap is created to track the
31-
/// accessibility state of each page in the lower VTL memory. The bitmap is
32-
/// initialized to the provided state.
29+
impl<'a> GuestPartitionMemoryView<'a> {
30+
/// A bitmap is created to track the accessibility state of each page in the
31+
/// lower VTL memory. The bitmap is initialized to valid_bitmap_state.
3332
///
3433
/// This is used to support tracking the shared/encrypted state of each
3534
/// page.
3635
pub fn new(
3736
memory_layout: &'a MemoryLayout,
38-
valid_bitmap_state: Option<bool>,
37+
valid_bitmap_state: bool,
3938
) -> Result<Self, MappingError> {
40-
let valid_memory = valid_bitmap_state
41-
.map(|state| GuestValidMemory::new(memory_layout, state).map(Arc::new))
42-
.transpose()?;
39+
let valid_memory =
40+
GuestValidMemory::new(memory_layout, valid_bitmap_state).map(Arc::new)?;
4341
Ok(Self {
4442
memory_layout,
4543
valid_memory,
4644
})
4745
}
4846

4947
/// Returns the built partition-wide valid memory.
50-
pub fn partition_valid_memory(&self) -> Option<Arc<GuestValidMemory>> {
48+
pub fn partition_valid_memory(&self) -> Arc<GuestValidMemory> {
5149
self.valid_memory.clone()
5250
}
5351

5452
/// Build a [`GuestMemoryMapping`], feeding in any related partition-wide
5553
/// state.
56-
pub fn build_guest_memory_mapping(
54+
fn build_guest_memory_mapping(
5755
&self,
5856
mshv_vtl_low: &MshvVtlLow,
5957
memory_mapping_builder: &mut GuestMemoryMappingBuilder,
6058
) -> Result<GuestMemoryMapping, MappingError> {
6159
memory_mapping_builder
62-
.use_partition_valid_memory(self.valid_memory.clone())
60+
.use_partition_valid_memory(Some(self.valid_memory.clone()))
6361
.build(mshv_vtl_low, self.memory_layout)
6462
}
6563
}
@@ -307,6 +305,24 @@ impl GuestMemoryMappingBuilder {
307305
self
308306
}
309307

308+
/// Mapping should leverage the bitmap used to track the accessibility state
309+
/// of each page in the lower VTL memory.
310+
pub fn build_with_bitmap(
311+
&mut self,
312+
mshv_vtl_low: &MshvVtlLow,
313+
partition_builder: &GuestPartitionMemoryView<'_>,
314+
) -> Result<GuestMemoryMapping, MappingError> {
315+
partition_builder.build_guest_memory_mapping(mshv_vtl_low, self)
316+
}
317+
318+
pub fn build_without_bitmap(
319+
&self,
320+
mshv_vtl_low: &MshvVtlLow,
321+
memory_layout: &MemoryLayout,
322+
) -> Result<GuestMemoryMapping, MappingError> {
323+
self.build(mshv_vtl_low, memory_layout)
324+
}
325+
310326
/// Map the lower VTL address space.
311327
///
312328
/// If `is_shared`, then map the kernel mapping as shared memory.
@@ -318,10 +334,6 @@ impl GuestMemoryMappingBuilder {
318334
/// When handing out IOVAs for device DMA, add `iova_offset`. This can be
319335
/// VTOM for SNP-isolated VMs, or it can be the VTL0 alias map start for
320336
/// non-isolated VMs.
321-
///
322-
/// If `bitmap_state` is `Some`, a bitmap is created to track the
323-
/// accessibility state of each page in the lower VTL memory. The bitmap is
324-
/// initialized to the provided state.
325337
fn build(
326338
&self,
327339
mshv_vtl_low: &MshvVtlLow,

0 commit comments

Comments
 (0)