Skip to content

Commit b950f5b

Browse files
kalyazinroypat
authored andcommitted
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. Note that offset_to_gpa is not used yet, but will likely be needed to support async PF to pass the GPA to a new ioctl when notifying KVM of a fault resolution. Signed-off-by: Nikita Kalyazin <[email protected]>
1 parent 74d9986 commit b950f5b

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed

src/vmm/src/vstate/memory.rs

Lines changed: 33 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,33 @@ 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).map(|r| {
486+
gpa.0 - r.start_addr().0 + r.file_offset().expect("File offset is None").start()
487+
})
488+
}
489+
490+
/// Convert file offset to guest physical address
491+
fn offset_to_gpa(&self, offset: u64) -> Option<GuestAddress> {
492+
self.iter().find_map(|region| {
493+
if let Some(reg_offset) = region.file_offset() {
494+
let region_start = reg_offset.start();
495+
let region_size = region.size();
496+
497+
if offset >= region_start && offset < region_start + region_size as u64 {
498+
Some(GuestAddress(
499+
region.start_addr().0 + (offset - region_start),
500+
))
501+
} else {
502+
None
503+
}
504+
} else {
505+
None
506+
}
507+
})
508+
}
476509
}
477510

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

0 commit comments

Comments
 (0)