@@ -13,11 +13,12 @@ use std::sync::Arc;
1313
1414use kvm_bindings:: { KVM_MEM_LOG_DIRTY_PAGES , kvm_userspace_memory_region} ;
1515use kvm_ioctls:: VmFd ;
16+ use serde:: { Deserialize , Serialize } ;
1617use vmm_sys_util:: eventfd:: EventFd ;
1718
1819pub use crate :: arch:: { ArchVm as Vm , ArchVmError , VmState } ;
1920use crate :: logger:: info;
20- use crate :: persist:: CreateSnapshotError ;
21+ use crate :: persist:: { CreateSnapshotError , VmInfo } ;
2122use crate :: utils:: u64_to_usize;
2223use crate :: vmm_config:: snapshot:: SnapshotType ;
2324use crate :: vstate:: memory:: {
@@ -36,6 +37,20 @@ pub struct VmCommon {
3637 pub guest_memory : GuestMemoryMmap ,
3738}
3839
40+ /// Describes the region of guest memory that can be used for creating the memfile.
41+ #[ derive( Clone , Copy , Debug , Default , PartialEq , Eq , Deserialize , Serialize ) ]
42+ pub struct GuestMemoryRegionMapping {
43+ /// Base host virtual address where the guest memory contents for this region
44+ /// should be copied/populated.
45+ pub base_host_virt_addr : u64 ,
46+ /// Region size.
47+ pub size : usize ,
48+ /// Offset in the backend file/buffer where the region contents are.
49+ pub offset : u64 ,
50+ /// The configured page size for this memory region.
51+ pub page_size : usize ,
52+ }
53+
3954/// Errors associated with the wrappers over KVM ioctls.
4055/// Needs `rustfmt::skip` to make multiline comments work
4156#[ rustfmt:: skip]
@@ -185,6 +200,22 @@ impl Vm {
185200 & self . common . guest_memory
186201 }
187202
203+ /// Gets the mappings for the guest memory.
204+ pub fn guest_memory_mappings ( & self , vm_info : & VmInfo ) -> Vec < GuestMemoryRegionMapping > {
205+ let mut offset = 0 ;
206+ let mut mappings = Vec :: new ( ) ;
207+ for mem_region in self . guest_memory ( ) . iter ( ) {
208+ mappings. push ( GuestMemoryRegionMapping {
209+ base_host_virt_addr : mem_region. as_ptr ( ) as u64 ,
210+ size : mem_region. size ( ) ,
211+ offset,
212+ page_size : vm_info. huge_pages . page_size ( ) ,
213+ } ) ;
214+ offset += mem_region. size ( ) as u64 ;
215+ }
216+ mappings
217+ }
218+
188219 /// Resets the KVM dirty bitmap for each of the guest's memory regions.
189220 pub fn reset_dirty_bitmap ( & self ) {
190221 self . guest_memory ( )
0 commit comments