Skip to content

Commit 84f9323

Browse files
committed
feat(vmm): add offset/gpa conversion functions
This is because vCPUs reason in GPAs while the secret-free UFFD protocol is guest_memfd-offset-based. TODO: sob patrick Signed-off-by: Nikita Kalyazin <[email protected]>
1 parent 34e6bb0 commit 84f9323

File tree

1 file changed

+32
-0
lines changed

1 file changed

+32
-0
lines changed

src/vmm/src/vstate/memory.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -323,6 +323,12 @@ where
323323

324324
/// Store the dirty bitmap in internal store
325325
fn store_dirty_bitmap(&self, dirty_bitmap: &DirtyBitmap, page_size: usize);
326+
327+
/// Convert guest physical address to file offset
328+
fn gpa_to_offset(&self, gpa: GuestAddress) -> Option<u64>;
329+
330+
/// Convert file offset to guest physical address
331+
fn offset_to_gpa(&self, offset: u64) -> Option<GuestAddress>;
326332
}
327333

328334
/// State of a guest memory region saved to file/buffer.
@@ -473,6 +479,32 @@ impl GuestMemoryExtension for GuestMemoryMmap {
473479
}
474480
});
475481
}
482+
483+
/// Convert guest physical address to file offset
484+
fn gpa_to_offset(&self, gpa: GuestAddress) -> Option<u64> {
485+
self.find_region(gpa)
486+
.map(|r| gpa.0 - r.start_addr().0 + r.file_offset().unwrap().start())
487+
}
488+
489+
/// Convert file offset to guest physical address
490+
fn offset_to_gpa(&self, offset: u64) -> Option<GuestAddress> {
491+
self.iter().find_map(|region| {
492+
if let Some(reg_offset) = region.file_offset() {
493+
let region_start = reg_offset.start();
494+
let region_size = region.size();
495+
496+
if offset >= region_start && offset < region_start + region_size as u64 {
497+
Some(GuestAddress(
498+
region.start_addr().0 + (offset - region_start),
499+
))
500+
} else {
501+
None
502+
}
503+
} else {
504+
None
505+
}
506+
})
507+
}
476508
}
477509

478510
/// Creates a memfd of the given size and huge pages configuration

0 commit comments

Comments
 (0)