// Copyright (C) 2021 Alibaba Cloud. All rights reserved.
// SPDX-License-Identifier: Apache-2.0

use crate::{AddressSpaceRegion, AddressSpaceRegionType};

/// Address space layout configuration.
///
/// The layout configuration must guarantee that `mem_start` <= `mem_end` <= `phys_end`.
/// Non-memory region should be arranged into the range [mem_end, phys_end).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct AddressSpaceLayout {
    /// end of guest physical address
    pub phys_end: u64,
    /// start of guest memory address
    pub mem_start: u64,
    /// end of guest memory address
    pub mem_end: u64,
}

impl AddressSpaceLayout {
    /// Create a new instance of `AddressSpaceLayout`.
    pub fn new(phys_end: u64, mem_start: u64, mem_end: u64) -> Self {
        AddressSpaceLayout {
            phys_end,
            mem_start,
            mem_end,
        }
    }

    /// Check whether an region is valid with the constraints of the layout.
    pub fn is_region_valid(&self, region: &AddressSpaceRegion) -> bool {
        let region_end = match region.base.0.checked_add(region.size) {
            None => return false,
            Some(v) => v,
        };

        match region.ty {
            AddressSpaceRegionType::DefaultMemory => {
                if region.base.0 < self.mem_start || region_end > self.mem_end {
                    return false;
                }
            }
            AddressSpaceRegionType::DeviceMemory | AddressSpaceRegionType::DAXMemory => {
                if region.base.0 < self.mem_end || region_end > self.phys_end {
                    return false;
                }
            }
        }

        true
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use vm_memory::GuestAddress;

    #[test]
    fn test_is_region_valid() {
        let layout = AddressSpaceLayout::new(0x1_0000_0000, 0x1000_0000, 0x2000_0000);

        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DefaultMemory,
            GuestAddress(0x0),
            0x1_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DefaultMemory,
            GuestAddress(0x2000_0000),
            0x1_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DefaultMemory,
            GuestAddress(0x1_0000),
            0x2000_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DefaultMemory,
            GuestAddress(u64::MAX),
            0x1_0000_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DefaultMemory,
            GuestAddress(0x1000_0000),
            0x1_0000,
        );
        assert!(layout.is_region_valid(&region));

        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DeviceMemory,
            GuestAddress(0x1000_0000),
            0x1_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DeviceMemory,
            GuestAddress(0x1_0000_0000),
            0x1_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DeviceMemory,
            GuestAddress(0x1_0000),
            0x1_0000_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DeviceMemory,
            GuestAddress(u64::MAX),
            0x1_0000_0000,
        );
        assert!(!layout.is_region_valid(&region));
        let region = AddressSpaceRegion::new(
            AddressSpaceRegionType::DeviceMemory,
            GuestAddress(0x8000_0000),
            0x1_0000,
        );
        assert!(layout.is_region_valid(&region));
    }
}
