1*91e9d4abSLoGin use crate::arch::mm::LockedFrameAllocator; 240314b30SXiaoye Zheng use crate::arch::mm::PageMapper; 340314b30SXiaoye Zheng use crate::arch::MMArch; 440314b30SXiaoye Zheng use crate::mm::page::PageFlags; 540314b30SXiaoye Zheng use crate::mm::{PageTableKind, PhysAddr, VirtAddr}; 640314b30SXiaoye Zheng use crate::smp::core::smp_get_processor_id; 740314b30SXiaoye Zheng use core::sync::atomic::{compiler_fence, AtomicUsize, Ordering}; 8*91e9d4abSLoGin use system_error::SystemError; 940314b30SXiaoye Zheng use x86::msr; 1040314b30SXiaoye Zheng 1140314b30SXiaoye Zheng /// Check if MTRR is supported 1240314b30SXiaoye Zheng pub fn check_ept_features() -> Result<(), SystemError> { 1340314b30SXiaoye Zheng const MTRR_ENABLE_BIT: u64 = 1 << 11; 1440314b30SXiaoye Zheng let ia32_mtrr_def_type = unsafe { msr::rdmsr(msr::IA32_MTRR_DEF_TYPE) }; 1540314b30SXiaoye Zheng if (ia32_mtrr_def_type & MTRR_ENABLE_BIT) == 0 { 1640314b30SXiaoye Zheng return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 1740314b30SXiaoye Zheng } 1840314b30SXiaoye Zheng Ok(()) 1940314b30SXiaoye Zheng } 2040314b30SXiaoye Zheng 2140314b30SXiaoye Zheng // pub fn ept_build_mtrr_map() -> Result<(), SystemError> { 2240314b30SXiaoye Zheng // let ia32_mtrr_cap = unsafe { msr::rdmsr(msr::IA32_MTRRCAP) }; 2340314b30SXiaoye Zheng // Ok(()) 2440314b30SXiaoye Zheng // } 2540314b30SXiaoye Zheng 2640314b30SXiaoye Zheng /// 标志当前没有处理器持有内核映射器的锁 2740314b30SXiaoye Zheng /// 之所以需要这个标志,是因为AtomicUsize::new(0)会把0当作一个处理器的id 2840314b30SXiaoye Zheng const EPT_MAPPER_NO_PROCESSOR: usize = !0; 2940314b30SXiaoye Zheng /// 当前持有内核映射器锁的处理器 3040314b30SXiaoye Zheng static EPT_MAPPER_LOCK_OWNER: AtomicUsize = AtomicUsize::new(EPT_MAPPER_NO_PROCESSOR); 3140314b30SXiaoye Zheng /// 内核映射器的锁计数器 3240314b30SXiaoye Zheng static EPT_MAPPER_LOCK_COUNT: AtomicUsize = AtomicUsize::new(0); 3340314b30SXiaoye Zheng 3440314b30SXiaoye Zheng pub struct EptMapper { 3540314b30SXiaoye Zheng /// EPT页表映射器 3640314b30SXiaoye Zheng mapper: PageMapper, 3740314b30SXiaoye Zheng /// 标记当前映射器是否为只读 3840314b30SXiaoye Zheng readonly: bool, 3940314b30SXiaoye Zheng // EPT页表根地址 4040314b30SXiaoye Zheng // root_hpa: PhysAddr, 4140314b30SXiaoye Zheng } 4240314b30SXiaoye Zheng 4340314b30SXiaoye Zheng impl EptMapper { 4440314b30SXiaoye Zheng fn lock_cpu(cpuid: usize, mapper: PageMapper) -> Self { 4540314b30SXiaoye Zheng loop { 4640314b30SXiaoye Zheng match EPT_MAPPER_LOCK_OWNER.compare_exchange_weak( 4740314b30SXiaoye Zheng EPT_MAPPER_NO_PROCESSOR, 4840314b30SXiaoye Zheng cpuid, 4940314b30SXiaoye Zheng Ordering::Acquire, 5040314b30SXiaoye Zheng Ordering::Relaxed, 5140314b30SXiaoye Zheng ) { 5240314b30SXiaoye Zheng Ok(_) => break, 5340314b30SXiaoye Zheng // 当前处理器已经持有了锁 5440314b30SXiaoye Zheng Err(id) if id == cpuid => break, 5540314b30SXiaoye Zheng // either CAS failed, or some other hardware thread holds the lock 5640314b30SXiaoye Zheng Err(_) => core::hint::spin_loop(), 5740314b30SXiaoye Zheng } 5840314b30SXiaoye Zheng } 5940314b30SXiaoye Zheng 6040314b30SXiaoye Zheng let prev_count = EPT_MAPPER_LOCK_COUNT.fetch_add(1, Ordering::Relaxed); 6140314b30SXiaoye Zheng compiler_fence(Ordering::Acquire); 6240314b30SXiaoye Zheng 6340314b30SXiaoye Zheng // 本地核心已经持有过锁,因此标记当前加锁获得的映射器为只读 6440314b30SXiaoye Zheng let readonly = prev_count > 0; 6540314b30SXiaoye Zheng 6640314b30SXiaoye Zheng return Self { mapper, readonly }; 6740314b30SXiaoye Zheng } 6840314b30SXiaoye Zheng 6940314b30SXiaoye Zheng /// @brief 锁定内核映射器, 并返回一个内核映射器对象 7040314b30SXiaoye Zheng #[inline(always)] 7140314b30SXiaoye Zheng pub fn lock() -> Self { 7240314b30SXiaoye Zheng let cpuid = smp_get_processor_id() as usize; 7340314b30SXiaoye Zheng let mapper = unsafe { PageMapper::current(PageTableKind::EPT, LockedFrameAllocator) }; 7440314b30SXiaoye Zheng return Self::lock_cpu(cpuid, mapper); 7540314b30SXiaoye Zheng } 7640314b30SXiaoye Zheng 7740314b30SXiaoye Zheng /// 映射guest physical addr(gpa)到指定的host physical addr(hpa)。 7840314b30SXiaoye Zheng /// 7940314b30SXiaoye Zheng /// ## 参数 8040314b30SXiaoye Zheng /// 8140314b30SXiaoye Zheng /// - `gpa`: 要映射的guest physical addr 8240314b30SXiaoye Zheng /// - `hpa`: 要映射的host physical addr 8340314b30SXiaoye Zheng /// - `flags`: 页面标志 8440314b30SXiaoye Zheng /// 8540314b30SXiaoye Zheng /// ## 返回 8640314b30SXiaoye Zheng /// 8740314b30SXiaoye Zheng /// - 成功:返回Ok(()) 8840314b30SXiaoye Zheng /// - 失败: 如果当前映射器为只读,则返回EAGAIN_OR_EWOULDBLOCK 8940314b30SXiaoye Zheng pub unsafe fn walk( 9040314b30SXiaoye Zheng &mut self, 9140314b30SXiaoye Zheng gpa: u64, 9240314b30SXiaoye Zheng hpa: u64, 9340314b30SXiaoye Zheng flags: PageFlags<MMArch>, 9440314b30SXiaoye Zheng ) -> Result<(), SystemError> { 9540314b30SXiaoye Zheng if self.readonly { 9640314b30SXiaoye Zheng return Err(SystemError::EAGAIN_OR_EWOULDBLOCK); 9740314b30SXiaoye Zheng } 9840314b30SXiaoye Zheng self.mapper 9940314b30SXiaoye Zheng .map_phys( 10040314b30SXiaoye Zheng VirtAddr::new(gpa as usize), 10140314b30SXiaoye Zheng PhysAddr::new(hpa as usize), 10240314b30SXiaoye Zheng flags, 10340314b30SXiaoye Zheng ) 10440314b30SXiaoye Zheng .unwrap() 10540314b30SXiaoye Zheng .flush(); 10640314b30SXiaoye Zheng return Ok(()); 10740314b30SXiaoye Zheng } 10840314b30SXiaoye Zheng 10940314b30SXiaoye Zheng // fn get_ept_index(addr: u64, level: usize) -> u64 { 11040314b30SXiaoye Zheng // let pt64_level_shift = PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS; 11140314b30SXiaoye Zheng // (addr >> pt64_level_shift) & ((1 << PT64_LEVEL_BITS) - 1) 11240314b30SXiaoye Zheng // } 11340314b30SXiaoye Zheng } 114