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