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