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