@@ -36,8 +36,9 @@ use crate::vmm_config::instance_info::InstanceInfo;
3636use crate :: vmm_config:: machine_config:: { HugePageConfig , MachineConfigError , MachineConfigUpdate } ;
3737use crate :: vmm_config:: snapshot:: { CreateSnapshotParams , LoadSnapshotParams , MemBackendType } ;
3838use crate :: vstate:: kvm:: KvmState ;
39- use crate :: vstate:: memory;
40- use crate :: vstate:: memory:: { GuestMemoryState , GuestRegionMmap , MemoryError } ;
39+ use crate :: vstate:: memory:: {
40+ self , GuestMemoryState , GuestRegionMmap , GuestRegionType , MemoryError ,
41+ } ;
4142use crate :: vstate:: vcpu:: { VcpuSendEventError , VcpuState } ;
4243use crate :: vstate:: vm:: { VmError , VmState } ;
4344use crate :: { EventManager , Vmm , vstate} ;
@@ -268,19 +269,48 @@ pub fn validate_cpu_manufacturer_id(microvm_state: &MicrovmState) {
268269pub enum SnapShotStateSanityCheckError {
269270 /// No memory region defined.
270271 NoMemory ,
272+ /// No DRAM memory region defined.
273+ NoDramMemory ,
274+ /// DRAM memory has more than a single slot.
275+ DramMemoryTooManySlots ,
276+ /// DRAM memory is unplugged.
277+ DramMemoryUnplugged ,
271278}
272279
273280/// Performs sanity checks against the state file and returns specific errors.
274281pub fn snapshot_state_sanity_check (
275282 microvm_state : & MicrovmState ,
276283) -> Result < ( ) , SnapShotStateSanityCheckError > {
277- // Check if the snapshot contains at least 1 mem region.
284+ // Check that the snapshot contains at least 1 mem region, that at least one is Dram,
285+ // and that Dram region contains a single plugged slot.
278286 // Upper bound check will be done when creating guest memory by comparing against
279287 // KVM max supported value kvm_context.max_memslots().
280- if microvm_state. vm_state . memory . regions . is_empty ( ) {
288+ let regions = & microvm_state. vm_state . memory . regions ;
289+
290+ if regions. is_empty ( ) {
281291 return Err ( SnapShotStateSanityCheckError :: NoMemory ) ;
282292 }
283293
294+ if !regions
295+ . iter ( )
296+ . any ( |r| r. region_type == GuestRegionType :: Dram )
297+ {
298+ return Err ( SnapShotStateSanityCheckError :: NoDramMemory ) ;
299+ }
300+
301+ for dram_region in regions
302+ . iter ( )
303+ . filter ( |r| r. region_type == GuestRegionType :: Dram )
304+ {
305+ if dram_region. plugged . len ( ) != 1 {
306+ return Err ( SnapShotStateSanityCheckError :: DramMemoryTooManySlots ) ;
307+ }
308+
309+ if !dram_region. plugged [ 0 ] {
310+ return Err ( SnapShotStateSanityCheckError :: DramMemoryUnplugged ) ;
311+ }
312+ }
313+
284314 #[ cfg( target_arch = "x86_64" ) ]
285315 validate_cpu_vendor ( microvm_state) ;
286316 #[ cfg( target_arch = "aarch64" ) ]
0 commit comments