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