Skip to content

Commit 7649ac4

Browse files
committed
feedback: attempt to have a single source of truth for partition-wide objects
1 parent 3a6c5cc commit 7649ac4

File tree

2 files changed

+97
-46
lines changed

2 files changed

+97
-46
lines changed

openhcl/underhill_mem/src/init.rs

Lines changed: 55 additions & 34 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::GuestValidMemory;
9+
use crate::mapping::GuestPartitionMemoryBuilder;
1010
use anyhow::Context;
1111
use futures::future::try_join_all;
1212
use guestmem::GuestMemory;
@@ -210,18 +210,19 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
210210
// kernel-registered RAM.
211211

212212
tracing::debug!("Building valid encrypted memory view");
213-
let valid_encrypted_memory = Arc::new({
214-
let _span = tracing::info_span!("create encrypted memory bitmap").entered();
215-
GuestValidMemory::new(params.mem_layout, true)?
216-
});
213+
let encrypted_memory_builder = {
214+
let _span = tracing::info_span!("create encrypted memory view").entered();
215+
GuestPartitionMemoryBuilder::new(params.mem_layout, Some(true))?
216+
};
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-
GuestMemoryMapping::builder(0)
222-
.dma_base_address(None) // prohibit direct DMA attempts until TDISP is supported
223-
.use_partition_valid_memory(Some(valid_encrypted_memory.clone()))
224-
.build(&gpa_fd, params.mem_layout)
221+
encrypted_memory_builder
222+
.build_guest_memory_mapping(
223+
&gpa_fd,
224+
GuestMemoryMapping::builder(0).dma_base_address(None),
225+
)
225226
.context("failed to map vtl0 memory")?
226227
});
227228

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

292293
tracing::debug!("Building shared memory map");
293294

294-
let valid_shared_memory = Arc::new({
295-
let _span = tracing::info_span!("create shared memory bitmap").entered();
296-
GuestValidMemory::new(params.complete_memory_layout, false)?
297-
});
295+
let shared_memory_builder = {
296+
let _span = tracing::info_span!("create shared memory view").entered();
297+
GuestPartitionMemoryBuilder::new(params.complete_memory_layout, Some(false))?
298+
};
299+
300+
let valid_shared_memory = shared_memory_builder.partition_valid_memory();
298301

299302
// Update the shared mapping bitmap for pages used by the shared
300303
// visibility pool to be marked as shared, since by default pages are
301304
// marked as no-access in the bitmap.
302305
tracing::debug!("Updating shared mapping bitmaps");
303306
for range in params.shared_pool {
304-
valid_shared_memory.update_valid(range.range, true);
307+
valid_shared_memory
308+
.as_ref()
309+
.unwrap()
310+
.update_valid(range.range, true);
305311
}
306312

307313
let shared_mapping = Arc::new({
308314
let _span = tracing::info_span!("map_shared_memory").entered();
309-
GuestMemoryMapping::builder(shared_offset)
310-
.shared(true)
311-
.use_partition_valid_memory(Some(valid_shared_memory.clone()))
312-
.ignore_registration_failure(params.boot_init.is_none())
313-
.dma_base_address(Some(dma_base_address))
314-
.build(&gpa_fd, params.complete_memory_layout)
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+
)
315323
.context("failed to map shared memory")?
316324
});
317325

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

367375
let protector = Arc::new(HardwareIsolatedMemoryProtector::new(
368-
valid_encrypted_memory.clone(),
369-
valid_shared_memory.clone(),
376+
encrypted_memory_builder
377+
.partition_valid_memory()
378+
.unwrap()
379+
.clone(),
380+
valid_shared_memory.unwrap().clone(),
370381
vtl0_mapping.clone(),
371382
params.mem_layout.clone(),
372383
acceptor.as_ref().unwrap().clone(),
@@ -385,16 +396,22 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
385396
}),
386397
}
387398
} else {
399+
let partition_memory_builder = GuestPartitionMemoryBuilder::new(params.mem_layout, None)?;
400+
388401
tracing::debug!("Creating VTL0 guest memory");
389402
let vtl0_mapping = {
390403
let _span = tracing::info_span!("map_vtl0_memory").entered();
391404
let base_address = params.vtl0_alias_map_bit.unwrap_or(0);
405+
392406
Arc::new(
393-
GuestMemoryMapping::builder(base_address)
394-
.for_kernel_access(true)
395-
.dma_base_address(Some(base_address))
396-
.ignore_registration_failure(params.boot_init.is_none())
397-
.build(&gpa_fd, params.mem_layout)
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+
)
398415
.context("failed to map vtl0 memory")?,
399416
)
400417
};
@@ -424,13 +441,17 @@ pub async fn init(params: &Init<'_>) -> anyhow::Result<MemoryMappings> {
424441
tracing::debug!("Creating VTL 1 memory map");
425442

426443
let _span = tracing::info_span!("map_vtl1_memory").entered();
427-
let vtl1_mapping = GuestMemoryMapping::builder(0)
428-
.for_kernel_access(true)
429-
.dma_base_address(Some(0))
430-
.ignore_registration_failure(params.boot_init.is_none())
431-
.build(&gpa_fd, params.mem_layout)
432-
.context("failed to map vtl1 memory")?;
433-
Some(Arc::new(vtl1_mapping))
444+
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+
)
453+
.context("failed to map vtl1 memory")?,
454+
))
434455
}
435456
} else {
436457
None

openhcl/underhill_mem/src/mapping.rs

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

24+
pub struct GuestPartitionMemoryBuilder<'a> {
25+
memory_layout: &'a MemoryLayout,
26+
valid_memory: Option<Arc<GuestValidMemory>>,
27+
}
28+
29+
impl<'a> GuestPartitionMemoryBuilder<'a> {
30+
/// valid_bitmap_state is valid when allocating a tracking bitmap is needed
31+
/// for memory access, and specifies the initial state of the bitmap.
32+
///
33+
/// This is used to support tracking the shared/encrypted state of each
34+
/// page.
35+
pub fn new(
36+
memory_layout: &'a MemoryLayout,
37+
valid_bitmap_state: Option<bool>,
38+
) -> Result<Self, MappingError> {
39+
let valid_memory = valid_bitmap_state
40+
.map(|state| GuestValidMemory::new(memory_layout, state).map(Arc::new))
41+
.transpose()?;
42+
Ok(Self {
43+
memory_layout,
44+
valid_memory,
45+
})
46+
}
47+
48+
pub fn partition_valid_memory(&self) -> Option<Arc<GuestValidMemory>> {
49+
self.valid_memory.clone()
50+
}
51+
52+
pub fn build_guest_memory_mapping(
53+
&self,
54+
mshv_vtl_low: &MshvVtlLow,
55+
memory_mapping_builder: &mut GuestMemoryMappingBuilder,
56+
) -> Result<GuestMemoryMapping, MappingError> {
57+
memory_mapping_builder
58+
.use_partition_valid_memory(self.valid_memory.clone())
59+
.build(mshv_vtl_low, self.memory_layout)
60+
}
61+
}
62+
2463
/// Partition-wide (cross-vtl) tracking of valid memory that can be used in
2564
/// individual GuestMemoryMappings.
2665
#[derive(Debug)]
@@ -30,10 +69,7 @@ pub struct GuestValidMemory {
3069
}
3170

3271
impl GuestValidMemory {
33-
pub fn new(
34-
memory_layout: &MemoryLayout,
35-
valid_bitmap_state: bool,
36-
) -> Result<Self, MappingError> {
72+
fn new(memory_layout: &MemoryLayout, valid_bitmap_state: bool) -> Result<Self, MappingError> {
3773
let valid_bitmap = {
3874
let mut bitmap = {
3975
// Calculate the total size of the address space by looking at the ending region.
@@ -205,15 +241,9 @@ pub struct GuestMemoryMappingBuilder {
205241
}
206242

207243
impl GuestMemoryMappingBuilder {
208-
/// Set whether to allocate a tracking for memory access, and specify the
209-
/// initial state of the bitmap.
210-
///
211-
/// This is used to support tracking the shared/encrypted state of each
212-
/// page.
213-
///
214244
/// FUTURE: use bitmaps to track VTL permissions as well, to support guest
215245
/// VSM for hardware-isolated VMs.
216-
pub fn use_partition_valid_memory(
246+
fn use_partition_valid_memory(
217247
&mut self,
218248
valid_memory: Option<Arc<GuestValidMemory>>,
219249
) -> &mut Self {
@@ -284,7 +314,7 @@ impl GuestMemoryMappingBuilder {
284314
/// If `bitmap_state` is `Some`, a bitmap is created to track the
285315
/// accessibility state of each page in the lower VTL memory. The bitmap is
286316
/// initialized to the provided state.
287-
pub fn build(
317+
fn build(
288318
&self,
289319
mshv_vtl_low: &MshvVtlLow,
290320
memory_layout: &MemoryLayout,

0 commit comments

Comments
 (0)