fn add_region(&self, flat_range: &FlatRange) -> Result<()> {
        if flat_range.owner.region_type() == RegionType::RomDevice
            && !flat_range.owner.get_rom_device_romd().unwrap()
        {
            if let Err(ref e) = self.delete_region(flat_range) {
                warn!(
                    "Rom-device Region changes to IO mode, Failed to delete region: {}",
                    e.display_chain()
                );
            }
            return Ok(());
        }

        if flat_range.owner.region_type() != RegionType::Ram
            && flat_range.owner.region_type() != RegionType::RomDevice
            && flat_range.owner.region_type() != RegionType::RamDevice
        {
            return Ok(());
        }

        let (aligned_addr, aligned_size) =
            Self::align_mem_slot(flat_range.addr_range, host_page_size())
                .map(|r| (r.base, r.size))
                .chain_err(|| "Failed to align mem slot")?;
        let align_adjust = aligned_addr.raw_value() - flat_range.addr_range.base.raw_value();

        // `unwrap()` won't fail because Ram-type Region definitely has hva
        let aligned_hva = flat_range.owner.get_host_address().unwrap()
            + flat_range.offset_in_region
            + align_adjust;

        let slot_idx = self
            .get_free_slot(aligned_addr.raw_value(), aligned_size, aligned_hva)
            .chain_err(|| "Failed to get available KVM mem slot")?;

        let mut flags = 0_u32;
        if flat_range.owner.get_rom_device_romd().unwrap_or(false) {
            flags |= MEM_READ_ONLY;
        }
        let kvm_region = kvm_userspace_memory_region {
            slot: slot_idx | (self.as_id.load(Ordering::SeqCst) << 16),
            guest_phys_addr: aligned_addr.raw_value(),
            memory_size: aligned_size,
            userspace_addr: aligned_hva,
            flags,
        };
        unsafe {
            KVM_FDS
                .load()
                .vm_fd
                .as_ref()
                .unwrap()
                .set_user_memory_region(kvm_region)
                .or_else(|e| {
                    self.delete_slot(aligned_addr.raw_value(), aligned_size)
                        .chain_err(|| "Failed to delete Kvm mem slot")?;
                    Err(e).chain_err(|| {
                        format!(
                            "KVM register memory region failed: addr 0x{:X}, size 0x{:X}",
                            aligned_addr.raw_value(),
                            aligned_size
                        )
                    })
                })?;
        }
        Ok(())
    }
/// Callback function for adding Region, which only care about Ram-type Region yet.
///
/// # Arguments
///
/// * `flat_range` - Corresponding FlatRange of new-added region.
///
/// # Errors
///
/// Return Error if fail to delete kvm_mem_slot.
Callback function for adding Region, which only care about Ram-type Region yet.
Callback function for adding Region which only care about Ram - type Region yet .
fn add_region(&self, flat_range: &FlatRange) -> Result<()> {
        if flat_range.owner.region_type() == RegionType::RomDevice
            && !flat_range.owner.get_rom_device_romd().unwrap()
        {
            if let Err(ref e) = self.delete_region(flat_range) {
                warn!(
                    "Rom-device Region changes to IO mode, Failed to delete region: {}",
                    e.display_chain()
                );
            }
            return Ok(());
        }
        if flat_range.owner.region_type() != RegionType::Ram
            && flat_range.owner.region_type() != RegionType::RomDevice
            && flat_range.owner.region_type() != RegionType::RamDevice
        {
            return Ok(());
        }
        let (aligned_addr, aligned_size) =
            Self::align_mem_slot(flat_range.addr_range, host_page_size())
                .map(|r| (r.base, r.size))
                .chain_err(|| "Failed to align mem slot")?;
        let align_adjust = aligned_addr.raw_value() - flat_range.addr_range.base.raw_value();
        // `unwrap()` won't fail because Ram-type Region definitely has hva
        let aligned_hva = flat_range.owner.get_host_address().unwrap()
            + flat_range.offset_in_region
            + align_adjust;
        let slot_idx = self
            .get_free_slot(aligned_addr.raw_value(), aligned_size, aligned_hva)
            .chain_err(|| "Failed to get available KVM mem slot")?;
        let mut flags = 0_u32;
        if flat_range.owner.get_rom_device_romd().unwrap_or(false) {
            flags |= MEM_READ_ONLY;
        }
        let kvm_region = kvm_userspace_memory_region {
            slot: slot_idx | (self.as_id.load(Ordering::SeqCst) << 16),
            guest_phys_addr: aligned_addr.raw_value(),
            memory_size: aligned_size,
            userspace_addr: aligned_hva,
            flags,
        };
        unsafe {
            KVM_FDS
                .load()
                .vm_fd
                .as_ref()
                .unwrap()
                .set_user_memory_region(kvm_region)
                .or_else(|e| {
                    self.delete_slot(aligned_addr.raw_value(), aligned_size)
                        .chain_err(|| "Failed to delete Kvm mem slot")?;
                    Err(e).chain_err(|| {
                        format!(
                            "KVM register memory region failed: addr 0x{:X}, size 0x{:X}",
                            aligned_addr.raw_value(),
                            aligned_size
                        )
                    })
                })?;
        }
        Ok(())
    }
fn add_region ( & self , flat_range : & FlatRange ) -> Result < ( ) > { if flat_range . owner . region_type ( ) == RegionType :: RomDevice && ! flat_range . owner . get_rom_device_romd ( ) . unwrap ( ) { if let Err ( ref e ) = self . delete_region ( flat_range ) { warn ! ( " " e display_chain ( ) ) ; } return Ok ( ( ) ) ; } if flat_range . owner . region_type ( ) != RegionType :: Ram && flat_range . owner . region_type ( ) != RegionType :: RomDevice && flat_range . owner . region_type ( ) != RegionType :: RamDevice { return Ok ( ( ) ) ; } let ( aligned_addr , aligned_size ) = Self :: align_mem_slot ( flat_range . addr_range , host_page_size ( ) ) . map ( | r | ( r . base , r . size ) ) . chain_err ( | | " " ) ? ; let align_adjust = aligned_addr . raw_value ( ) - flat_range . addr_range . base . raw_value ( ) ; // `unwrap()` won't fail because Ram-type Region definitely has hva let aligned_hva = flat_range . owner . get_host_address ( ) . unwrap ( ) + flat_range . offset_in_region + align_adjust ; let slot_idx = self . get_free_slot ( aligned_addr . raw_value ( ) , aligned_size , aligned_hva ) . chain_err ( | | " " ) ? ; let mut flags = 0_u32 ; if flat_range . owner . get_rom_device_romd ( ) . unwrap_or ( false ) { flags |= MEM_READ_ONLY ; } let kvm_region = kvm_userspace_memory_region { slot : slot_idx | ( self . as_id . load ( Ordering :: SeqCst ) << 16 ) , guest_phys_addr : aligned_addr . raw_value ( ) , memory_size : aligned_size , userspace_addr : aligned_hva , flags , } ; unsafe { KVM_FDS . load ( ) . vm_fd . as_ref ( ) . unwrap ( ) . set_user_memory_region ( kvm_region ) . or_else ( | e | { self . delete_slot ( aligned_addr . raw_value ( ) , aligned_size ) . chain_err ( | | " " ) ? ; Err ( e ) . chain_err ( | | { format ! ( " " aligned_addr raw_value ( ) aligned_size ) } ) } ) ? ; } Ok ( ( ) ) }
Callback function for adding Region, which only care about Ram-type Region yet.
Callback function for adding Region which only care about Ram - type Region yet .
&self
flat_range
&FlatRange
&self
flat_range
&FlatRange
errors
Return Error if fail to delete kvm_mem_slot.
Return Error if fail to delete kvm_mem_slot .