xref: /DragonOS/kernel/src/arch/x86_64/kvm/vmx/ept.rs (revision 40314b30ab2a7e1fd06a05a00f693e644e446035)
1*40314b30SXiaoye Zheng use crate::arch::mm::PageMapper;
2*40314b30SXiaoye Zheng use crate::arch::MMArch;
3*40314b30SXiaoye Zheng use crate::mm::page::PageFlags;
4*40314b30SXiaoye Zheng use crate::mm::{PageTableKind, PhysAddr, VirtAddr};
5*40314b30SXiaoye Zheng use crate::smp::core::smp_get_processor_id;
6*40314b30SXiaoye Zheng use crate::{arch::mm::LockedFrameAllocator, syscall::SystemError};
7*40314b30SXiaoye Zheng use core::sync::atomic::{compiler_fence, AtomicUsize, Ordering};
8*40314b30SXiaoye Zheng use x86::msr;
9*40314b30SXiaoye Zheng 
10*40314b30SXiaoye Zheng /// Check if MTRR is supported
11*40314b30SXiaoye Zheng pub fn check_ept_features() -> Result<(), SystemError> {
12*40314b30SXiaoye Zheng     const MTRR_ENABLE_BIT: u64 = 1 << 11;
13*40314b30SXiaoye Zheng     let ia32_mtrr_def_type = unsafe { msr::rdmsr(msr::IA32_MTRR_DEF_TYPE) };
14*40314b30SXiaoye Zheng     if (ia32_mtrr_def_type & MTRR_ENABLE_BIT) == 0 {
15*40314b30SXiaoye Zheng         return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP);
16*40314b30SXiaoye Zheng     }
17*40314b30SXiaoye Zheng     Ok(())
18*40314b30SXiaoye Zheng }
19*40314b30SXiaoye Zheng 
20*40314b30SXiaoye Zheng // pub fn ept_build_mtrr_map() -> Result<(), SystemError> {
21*40314b30SXiaoye Zheng // let ia32_mtrr_cap = unsafe { msr::rdmsr(msr::IA32_MTRRCAP) };
22*40314b30SXiaoye Zheng // Ok(())
23*40314b30SXiaoye Zheng // }
24*40314b30SXiaoye Zheng 
25*40314b30SXiaoye Zheng /// 标志当前没有处理器持有内核映射器的锁
26*40314b30SXiaoye Zheng /// 之所以需要这个标志,是因为AtomicUsize::new(0)会把0当作一个处理器的id
27*40314b30SXiaoye Zheng const EPT_MAPPER_NO_PROCESSOR: usize = !0;
28*40314b30SXiaoye Zheng /// 当前持有内核映射器锁的处理器
29*40314b30SXiaoye Zheng static EPT_MAPPER_LOCK_OWNER: AtomicUsize = AtomicUsize::new(EPT_MAPPER_NO_PROCESSOR);
30*40314b30SXiaoye Zheng /// 内核映射器的锁计数器
31*40314b30SXiaoye Zheng static EPT_MAPPER_LOCK_COUNT: AtomicUsize = AtomicUsize::new(0);
32*40314b30SXiaoye Zheng 
33*40314b30SXiaoye Zheng pub struct EptMapper {
34*40314b30SXiaoye Zheng     /// EPT页表映射器
35*40314b30SXiaoye Zheng     mapper: PageMapper,
36*40314b30SXiaoye Zheng     /// 标记当前映射器是否为只读
37*40314b30SXiaoye Zheng     readonly: bool,
38*40314b30SXiaoye Zheng     // EPT页表根地址
39*40314b30SXiaoye Zheng     // root_hpa: PhysAddr,
40*40314b30SXiaoye Zheng }
41*40314b30SXiaoye Zheng 
42*40314b30SXiaoye Zheng impl EptMapper {
43*40314b30SXiaoye Zheng     fn lock_cpu(cpuid: usize, mapper: PageMapper) -> Self {
44*40314b30SXiaoye Zheng         loop {
45*40314b30SXiaoye Zheng             match EPT_MAPPER_LOCK_OWNER.compare_exchange_weak(
46*40314b30SXiaoye Zheng                 EPT_MAPPER_NO_PROCESSOR,
47*40314b30SXiaoye Zheng                 cpuid,
48*40314b30SXiaoye Zheng                 Ordering::Acquire,
49*40314b30SXiaoye Zheng                 Ordering::Relaxed,
50*40314b30SXiaoye Zheng             ) {
51*40314b30SXiaoye Zheng                 Ok(_) => break,
52*40314b30SXiaoye Zheng                 // 当前处理器已经持有了锁
53*40314b30SXiaoye Zheng                 Err(id) if id == cpuid => break,
54*40314b30SXiaoye Zheng                 // either CAS failed, or some other hardware thread holds the lock
55*40314b30SXiaoye Zheng                 Err(_) => core::hint::spin_loop(),
56*40314b30SXiaoye Zheng             }
57*40314b30SXiaoye Zheng         }
58*40314b30SXiaoye Zheng 
59*40314b30SXiaoye Zheng         let prev_count = EPT_MAPPER_LOCK_COUNT.fetch_add(1, Ordering::Relaxed);
60*40314b30SXiaoye Zheng         compiler_fence(Ordering::Acquire);
61*40314b30SXiaoye Zheng 
62*40314b30SXiaoye Zheng         // 本地核心已经持有过锁,因此标记当前加锁获得的映射器为只读
63*40314b30SXiaoye Zheng         let readonly = prev_count > 0;
64*40314b30SXiaoye Zheng 
65*40314b30SXiaoye Zheng         return Self { mapper, readonly };
66*40314b30SXiaoye Zheng     }
67*40314b30SXiaoye Zheng 
68*40314b30SXiaoye Zheng     /// @brief 锁定内核映射器, 并返回一个内核映射器对象
69*40314b30SXiaoye Zheng     #[inline(always)]
70*40314b30SXiaoye Zheng     pub fn lock() -> Self {
71*40314b30SXiaoye Zheng         let cpuid = smp_get_processor_id() as usize;
72*40314b30SXiaoye Zheng         let mapper = unsafe { PageMapper::current(PageTableKind::EPT, LockedFrameAllocator) };
73*40314b30SXiaoye Zheng         return Self::lock_cpu(cpuid, mapper);
74*40314b30SXiaoye Zheng     }
75*40314b30SXiaoye Zheng 
76*40314b30SXiaoye Zheng     /// 映射guest physical addr(gpa)到指定的host physical addr(hpa)。
77*40314b30SXiaoye Zheng     ///
78*40314b30SXiaoye Zheng     /// ## 参数
79*40314b30SXiaoye Zheng     ///
80*40314b30SXiaoye Zheng     /// - `gpa`: 要映射的guest physical addr
81*40314b30SXiaoye Zheng     /// - `hpa`: 要映射的host physical addr
82*40314b30SXiaoye Zheng     /// - `flags`: 页面标志
83*40314b30SXiaoye Zheng     ///
84*40314b30SXiaoye Zheng     /// ## 返回
85*40314b30SXiaoye Zheng     ///
86*40314b30SXiaoye Zheng     /// - 成功:返回Ok(())
87*40314b30SXiaoye Zheng     /// - 失败: 如果当前映射器为只读,则返回EAGAIN_OR_EWOULDBLOCK
88*40314b30SXiaoye Zheng     pub unsafe fn walk(
89*40314b30SXiaoye Zheng         &mut self,
90*40314b30SXiaoye Zheng         gpa: u64,
91*40314b30SXiaoye Zheng         hpa: u64,
92*40314b30SXiaoye Zheng         flags: PageFlags<MMArch>,
93*40314b30SXiaoye Zheng     ) -> Result<(), SystemError> {
94*40314b30SXiaoye Zheng         if self.readonly {
95*40314b30SXiaoye Zheng             return Err(SystemError::EAGAIN_OR_EWOULDBLOCK);
96*40314b30SXiaoye Zheng         }
97*40314b30SXiaoye Zheng         self.mapper
98*40314b30SXiaoye Zheng             .map_phys(
99*40314b30SXiaoye Zheng                 VirtAddr::new(gpa as usize),
100*40314b30SXiaoye Zheng                 PhysAddr::new(hpa as usize),
101*40314b30SXiaoye Zheng                 flags,
102*40314b30SXiaoye Zheng             )
103*40314b30SXiaoye Zheng             .unwrap()
104*40314b30SXiaoye Zheng             .flush();
105*40314b30SXiaoye Zheng         return Ok(());
106*40314b30SXiaoye Zheng     }
107*40314b30SXiaoye Zheng 
108*40314b30SXiaoye Zheng     // fn get_ept_index(addr: u64, level: usize) -> u64 {
109*40314b30SXiaoye Zheng     //     let pt64_level_shift = PAGE_SHIFT + (level - 1) * PT64_LEVEL_BITS;
110*40314b30SXiaoye Zheng     //     (addr >> pt64_level_shift) & ((1 << PT64_LEVEL_BITS) - 1)
111*40314b30SXiaoye Zheng     // }
112*40314b30SXiaoye Zheng }
113