xref: /DragonOS/kernel/src/mm/mod.rs (revision 471d65cf158c9bf741c21f5d0ab92efe7bf1c3d4)
140fe15e0SLoGin use alloc::sync::Arc;
291e9d4abSLoGin use system_error::SystemError;
340fe15e0SLoGin 
491e9d4abSLoGin use crate::{arch::MMArch, include::bindings::bindings::PAGE_OFFSET};
540fe15e0SLoGin 
640fe15e0SLoGin use core::{
740fe15e0SLoGin     cmp,
840fe15e0SLoGin     fmt::Debug,
940fe15e0SLoGin     intrinsics::unlikely,
1040fe15e0SLoGin     ops::{Add, AddAssign, Sub, SubAssign},
1140fe15e0SLoGin     ptr,
1240fe15e0SLoGin     sync::atomic::{AtomicBool, Ordering},
1340fe15e0SLoGin };
1440fe15e0SLoGin 
1540fe15e0SLoGin use self::{
1640fe15e0SLoGin     allocator::page_frame::{VirtPageFrame, VirtPageFrameIter},
1792849878SLoGin     memblock::MemoryAreaAttr,
1840fe15e0SLoGin     page::round_up_to_page_size,
19a17651b1SMemoryShore     ucontext::{AddressSpace, LockedVMA, UserMapper},
2040fe15e0SLoGin };
21004e86ffSlogin 
2282d2e446Slogin pub mod allocator;
2340fe15e0SLoGin pub mod c_adapter;
2474ffde66SLoGin pub mod early_ioremap;
25a17651b1SMemoryShore pub mod fault;
26453452ccSLoGin pub mod init;
2740fe15e0SLoGin pub mod kernel_mapper;
28a17651b1SMemoryShore pub mod madvise;
2945626c85SLoGin pub mod memblock;
30c2481452Shoumkh pub mod mmio_buddy;
3140fe15e0SLoGin pub mod no_init;
3240fe15e0SLoGin pub mod page;
33c3dad001SLoGin pub mod percpu;
34ab5c8ca4Slogin pub mod syscall;
3540fe15e0SLoGin pub mod ucontext;
3640fe15e0SLoGin 
3740fe15e0SLoGin /// 内核INIT进程的用户地址空间结构体(仅在process_init中初始化)
388cb2e9b3SLoGin static mut __IDLE_PROCESS_ADDRESS_SPACE: Option<Arc<AddressSpace>> = None;
3940fe15e0SLoGin 
404cfa009bSJomo bitflags! {
414cfa009bSJomo     /// Virtual memory flags
42b5b571e0SLoGin     #[allow(clippy::bad_bit_mask)]
43a17651b1SMemoryShore     pub struct VmFlags:u64{
444cfa009bSJomo         const VM_NONE = 0x00000000;
454cfa009bSJomo 
464cfa009bSJomo         const VM_READ = 0x00000001;
474cfa009bSJomo         const VM_WRITE = 0x00000002;
484cfa009bSJomo         const VM_EXEC = 0x00000004;
494cfa009bSJomo         const VM_SHARED = 0x00000008;
504cfa009bSJomo 
514cfa009bSJomo         const VM_MAYREAD = 0x00000010;
524cfa009bSJomo         const VM_MAYWRITE = 0x00000020;
534cfa009bSJomo         const VM_MAYEXEC = 0x00000040;
544cfa009bSJomo         const VM_MAYSHARE = 0x00000080;
554cfa009bSJomo 
564cfa009bSJomo         const VM_GROWSDOWN = 0x00000100;
574cfa009bSJomo         const VM_UFFD_MISSING = 0x00000200;
584cfa009bSJomo         const VM_PFNMAP = 0x00000400;
594cfa009bSJomo         const VM_UFFD_WP = 0x00001000;
604cfa009bSJomo 
614cfa009bSJomo         const VM_LOCKED = 0x00002000;
624cfa009bSJomo         const VM_IO = 0x00004000;
634cfa009bSJomo 
644cfa009bSJomo         const VM_SEQ_READ = 0x00008000;
654cfa009bSJomo         const VM_RAND_READ = 0x00010000;
664cfa009bSJomo 
674cfa009bSJomo         const VM_DONTCOPY = 0x00020000;
684cfa009bSJomo         const VM_DONTEXPAND = 0x00040000;
694cfa009bSJomo         const VM_LOCKONFAULT = 0x00080000;
704cfa009bSJomo         const VM_ACCOUNT = 0x00100000;
714cfa009bSJomo         const VM_NORESERVE = 0x00200000;
724cfa009bSJomo         const VM_HUGETLB = 0x00400000;
734cfa009bSJomo         const VM_SYNC = 0x00800000;
744cfa009bSJomo         const VM_ARCH_1 = 0x01000000;
754cfa009bSJomo         const VM_WIPEONFORK = 0x02000000;
764cfa009bSJomo         const VM_DONTDUMP = 0x04000000;
774cfa009bSJomo     }
78a17651b1SMemoryShore 
79a17651b1SMemoryShore     /// 描述页面错误处理过程中发生的不同情况或结果
80a17651b1SMemoryShore         pub struct VmFaultReason:u32 {
81a17651b1SMemoryShore         const VM_FAULT_OOM = 0x000001;
82a17651b1SMemoryShore         const VM_FAULT_SIGBUS = 0x000002;
83a17651b1SMemoryShore         const VM_FAULT_MAJOR = 0x000004;
84a17651b1SMemoryShore         const VM_FAULT_WRITE = 0x000008;
85a17651b1SMemoryShore         const VM_FAULT_HWPOISON = 0x000010;
86a17651b1SMemoryShore         const VM_FAULT_HWPOISON_LARGE = 0x000020;
87a17651b1SMemoryShore         const VM_FAULT_SIGSEGV = 0x000040;
88a17651b1SMemoryShore         const VM_FAULT_NOPAGE = 0x000100;
89a17651b1SMemoryShore         const VM_FAULT_LOCKED = 0x000200;
90a17651b1SMemoryShore         const VM_FAULT_RETRY = 0x000400;
91a17651b1SMemoryShore         const VM_FAULT_FALLBACK = 0x000800;
92a17651b1SMemoryShore         const VM_FAULT_DONE_COW = 0x001000;
93a17651b1SMemoryShore         const VM_FAULT_NEEDDSYNC = 0x002000;
94a17651b1SMemoryShore         const VM_FAULT_COMPLETED = 0x004000;
95a17651b1SMemoryShore         const VM_FAULT_HINDEX_MASK = 0x0f0000;
96a17651b1SMemoryShore     }
974cfa009bSJomo }
984cfa009bSJomo 
998cb2e9b3SLoGin /// 获取内核IDLE进程的用户地址空间结构体
10040fe15e0SLoGin #[allow(non_snake_case)]
10140fe15e0SLoGin #[inline(always)]
1028cb2e9b3SLoGin pub fn IDLE_PROCESS_ADDRESS_SPACE() -> Arc<AddressSpace> {
10340fe15e0SLoGin     unsafe {
1048cb2e9b3SLoGin         return __IDLE_PROCESS_ADDRESS_SPACE
10540fe15e0SLoGin             .as_ref()
1068cb2e9b3SLoGin             .expect("IDLE_PROCESS_ADDRESS_SPACE is null")
10740fe15e0SLoGin             .clone();
10840fe15e0SLoGin     }
10940fe15e0SLoGin }
11040fe15e0SLoGin 
1118cb2e9b3SLoGin /// 设置内核IDLE进程的用户地址空间结构体全局变量
11240fe15e0SLoGin #[allow(non_snake_case)]
1138cb2e9b3SLoGin pub unsafe fn set_IDLE_PROCESS_ADDRESS_SPACE(address_space: Arc<AddressSpace>) {
11440fe15e0SLoGin     static INITIALIZED: AtomicBool = AtomicBool::new(false);
11540fe15e0SLoGin     if INITIALIZED
11640fe15e0SLoGin         .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
11740fe15e0SLoGin         .is_err()
11840fe15e0SLoGin     {
1198cb2e9b3SLoGin         panic!("IDLE_PROCESS_ADDRESS_SPACE is already initialized");
12040fe15e0SLoGin     }
1218cb2e9b3SLoGin     __IDLE_PROCESS_ADDRESS_SPACE = Some(address_space);
12240fe15e0SLoGin }
123004e86ffSlogin 
124004e86ffSlogin /// @brief 将内核空间的虚拟地址转换为物理地址
125004e86ffSlogin #[inline(always)]
126004e86ffSlogin pub fn virt_2_phys(addr: usize) -> usize {
127004e86ffSlogin     addr - PAGE_OFFSET as usize
128004e86ffSlogin }
129004e86ffSlogin 
130004e86ffSlogin /// @brief 将物理地址转换为内核空间的虚拟地址
131004e86ffSlogin #[inline(always)]
132004e86ffSlogin pub fn phys_2_virt(addr: usize) -> usize {
133004e86ffSlogin     addr + PAGE_OFFSET as usize
134004e86ffSlogin }
135bacd691cSlogin 
13640fe15e0SLoGin #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
13740fe15e0SLoGin pub enum PageTableKind {
13840fe15e0SLoGin     /// 用户可访问的页表
13940fe15e0SLoGin     User,
14040fe15e0SLoGin     /// 内核页表
14140fe15e0SLoGin     Kernel,
1427a29d4fcSLoGin     /// x86内存虚拟化中使用的EPT
1437a29d4fcSLoGin     #[cfg(target_arch = "x86_64")]
14440314b30SXiaoye Zheng     EPT,
14540fe15e0SLoGin }
14640fe15e0SLoGin 
14740fe15e0SLoGin /// 物理内存地址
14840fe15e0SLoGin #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
14940fe15e0SLoGin #[repr(transparent)]
15040fe15e0SLoGin pub struct PhysAddr(usize);
15140fe15e0SLoGin 
15240fe15e0SLoGin impl PhysAddr {
15392849878SLoGin     /// 最大物理地址
15492849878SLoGin     pub const MAX: Self = PhysAddr(usize::MAX);
15592849878SLoGin 
15640fe15e0SLoGin     #[inline(always)]
15740fe15e0SLoGin     pub const fn new(address: usize) -> Self {
15840fe15e0SLoGin         Self(address)
15940fe15e0SLoGin     }
16040fe15e0SLoGin 
16140fe15e0SLoGin     /// @brief 获取物理地址的值
16240fe15e0SLoGin     #[inline(always)]
16374ffde66SLoGin     pub const fn data(&self) -> usize {
16440fe15e0SLoGin         self.0
16540fe15e0SLoGin     }
16640fe15e0SLoGin 
16740fe15e0SLoGin     /// @brief 将物理地址加上一个偏移量
16840fe15e0SLoGin     #[inline(always)]
16940fe15e0SLoGin     pub fn add(self, offset: usize) -> Self {
17040fe15e0SLoGin         Self(self.0 + offset)
17140fe15e0SLoGin     }
17240fe15e0SLoGin 
17340fe15e0SLoGin     /// @brief 判断物理地址是否按照指定要求对齐
17440fe15e0SLoGin     #[inline(always)]
17540fe15e0SLoGin     pub fn check_aligned(&self, align: usize) -> bool {
17640fe15e0SLoGin         return self.0 & (align - 1) == 0;
17740fe15e0SLoGin     }
17840fe15e0SLoGin 
17940fe15e0SLoGin     #[inline(always)]
18040fe15e0SLoGin     pub fn is_null(&self) -> bool {
18140fe15e0SLoGin         return self.0 == 0;
18240fe15e0SLoGin     }
18340fe15e0SLoGin }
18440fe15e0SLoGin 
18540fe15e0SLoGin impl Debug for PhysAddr {
18640fe15e0SLoGin     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
18740fe15e0SLoGin         write!(f, "PhysAddr({:#x})", self.0)
18840fe15e0SLoGin     }
18940fe15e0SLoGin }
19040fe15e0SLoGin 
19140fe15e0SLoGin impl core::ops::Add<usize> for PhysAddr {
19240fe15e0SLoGin     type Output = Self;
19340fe15e0SLoGin 
19440fe15e0SLoGin     #[inline(always)]
19540fe15e0SLoGin     fn add(self, rhs: usize) -> Self::Output {
19640fe15e0SLoGin         return Self(self.0 + rhs);
19740fe15e0SLoGin     }
19840fe15e0SLoGin }
19940fe15e0SLoGin 
20040fe15e0SLoGin impl core::ops::AddAssign<usize> for PhysAddr {
20140fe15e0SLoGin     #[inline(always)]
20240fe15e0SLoGin     fn add_assign(&mut self, rhs: usize) {
20340fe15e0SLoGin         self.0 += rhs;
20440fe15e0SLoGin     }
20540fe15e0SLoGin }
20640fe15e0SLoGin 
20740fe15e0SLoGin impl core::ops::Add<PhysAddr> for PhysAddr {
20840fe15e0SLoGin     type Output = Self;
20940fe15e0SLoGin 
21040fe15e0SLoGin     #[inline(always)]
21140fe15e0SLoGin     fn add(self, rhs: PhysAddr) -> Self::Output {
21240fe15e0SLoGin         return Self(self.0 + rhs.0);
21340fe15e0SLoGin     }
21440fe15e0SLoGin }
21540fe15e0SLoGin 
21640fe15e0SLoGin impl core::ops::AddAssign<PhysAddr> for PhysAddr {
21740fe15e0SLoGin     #[inline(always)]
21840fe15e0SLoGin     fn add_assign(&mut self, rhs: PhysAddr) {
21940fe15e0SLoGin         self.0 += rhs.0;
22040fe15e0SLoGin     }
22140fe15e0SLoGin }
22240fe15e0SLoGin 
2232dd9f0c7SLoGin impl core::ops::BitOrAssign<usize> for PhysAddr {
2242dd9f0c7SLoGin     #[inline(always)]
2252dd9f0c7SLoGin     fn bitor_assign(&mut self, rhs: usize) {
2262dd9f0c7SLoGin         self.0 |= rhs;
2272dd9f0c7SLoGin     }
2282dd9f0c7SLoGin }
2292dd9f0c7SLoGin 
2302dd9f0c7SLoGin impl core::ops::BitOrAssign<PhysAddr> for PhysAddr {
2312dd9f0c7SLoGin     #[inline(always)]
2322dd9f0c7SLoGin     fn bitor_assign(&mut self, rhs: PhysAddr) {
2332dd9f0c7SLoGin         self.0 |= rhs.0;
2342dd9f0c7SLoGin     }
2352dd9f0c7SLoGin }
2362dd9f0c7SLoGin 
23740fe15e0SLoGin impl core::ops::Sub<usize> for PhysAddr {
23840fe15e0SLoGin     type Output = Self;
23940fe15e0SLoGin 
24040fe15e0SLoGin     #[inline(always)]
24140fe15e0SLoGin     fn sub(self, rhs: usize) -> Self::Output {
24240fe15e0SLoGin         return Self(self.0 - rhs);
24340fe15e0SLoGin     }
24440fe15e0SLoGin }
24540fe15e0SLoGin 
24640fe15e0SLoGin impl core::ops::SubAssign<usize> for PhysAddr {
24740fe15e0SLoGin     #[inline(always)]
24840fe15e0SLoGin     fn sub_assign(&mut self, rhs: usize) {
24940fe15e0SLoGin         self.0 -= rhs;
25040fe15e0SLoGin     }
25140fe15e0SLoGin }
25240fe15e0SLoGin 
25340fe15e0SLoGin impl core::ops::Sub<PhysAddr> for PhysAddr {
25440fe15e0SLoGin     type Output = usize;
25540fe15e0SLoGin 
25640fe15e0SLoGin     #[inline(always)]
25740fe15e0SLoGin     fn sub(self, rhs: PhysAddr) -> Self::Output {
25840fe15e0SLoGin         return self.0 - rhs.0;
25940fe15e0SLoGin     }
26040fe15e0SLoGin }
26140fe15e0SLoGin 
26240fe15e0SLoGin impl core::ops::SubAssign<PhysAddr> for PhysAddr {
26340fe15e0SLoGin     #[inline(always)]
26440fe15e0SLoGin     fn sub_assign(&mut self, rhs: PhysAddr) {
26540fe15e0SLoGin         self.0 -= rhs.0;
26640fe15e0SLoGin     }
26740fe15e0SLoGin }
26840fe15e0SLoGin 
26940fe15e0SLoGin /// 虚拟内存地址
27040fe15e0SLoGin #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
27140fe15e0SLoGin #[repr(transparent)]
27240fe15e0SLoGin pub struct VirtAddr(usize);
27340fe15e0SLoGin 
27440fe15e0SLoGin impl VirtAddr {
27540fe15e0SLoGin     #[inline(always)]
27640fe15e0SLoGin     pub const fn new(address: usize) -> Self {
27740fe15e0SLoGin         return Self(address);
27840fe15e0SLoGin     }
27940fe15e0SLoGin 
28040fe15e0SLoGin     /// @brief 获取虚拟地址的值
28140fe15e0SLoGin     #[inline(always)]
28274ffde66SLoGin     pub const fn data(&self) -> usize {
28340fe15e0SLoGin         return self.0;
28440fe15e0SLoGin     }
28540fe15e0SLoGin 
28640fe15e0SLoGin     /// @brief 判断虚拟地址的类型
28740fe15e0SLoGin     #[inline(always)]
28840fe15e0SLoGin     pub fn kind(&self) -> PageTableKind {
28940fe15e0SLoGin         if self.check_user() {
29040fe15e0SLoGin             return PageTableKind::User;
29140fe15e0SLoGin         } else {
29240fe15e0SLoGin             return PageTableKind::Kernel;
29340fe15e0SLoGin         }
29440fe15e0SLoGin     }
29540fe15e0SLoGin 
29640fe15e0SLoGin     /// @brief 判断虚拟地址是否按照指定要求对齐
29740fe15e0SLoGin     #[inline(always)]
29840fe15e0SLoGin     pub fn check_aligned(&self, align: usize) -> bool {
29940fe15e0SLoGin         return self.0 & (align - 1) == 0;
30040fe15e0SLoGin     }
30140fe15e0SLoGin 
30240fe15e0SLoGin     /// @brief 判断虚拟地址是否在用户空间
30340fe15e0SLoGin     #[inline(always)]
30440fe15e0SLoGin     pub fn check_user(&self) -> bool {
305b5b571e0SLoGin         return self < &MMArch::USER_END_VADDR;
30640fe15e0SLoGin     }
30740fe15e0SLoGin 
30840fe15e0SLoGin     #[inline(always)]
30940fe15e0SLoGin     pub fn as_ptr<T>(self) -> *mut T {
31040fe15e0SLoGin         return self.0 as *mut T;
31140fe15e0SLoGin     }
31240fe15e0SLoGin 
31340fe15e0SLoGin     #[inline(always)]
31440fe15e0SLoGin     pub fn is_null(&self) -> bool {
31540fe15e0SLoGin         return self.0 == 0;
31640fe15e0SLoGin     }
31740fe15e0SLoGin }
31840fe15e0SLoGin 
31940fe15e0SLoGin impl Add<VirtAddr> for VirtAddr {
32040fe15e0SLoGin     type Output = Self;
32140fe15e0SLoGin 
32240fe15e0SLoGin     #[inline(always)]
32340fe15e0SLoGin     fn add(self, rhs: VirtAddr) -> Self::Output {
32440fe15e0SLoGin         return Self(self.0 + rhs.0);
32540fe15e0SLoGin     }
32640fe15e0SLoGin }
32740fe15e0SLoGin 
32840fe15e0SLoGin impl Add<usize> for VirtAddr {
32940fe15e0SLoGin     type Output = Self;
33040fe15e0SLoGin 
33140fe15e0SLoGin     #[inline(always)]
33240fe15e0SLoGin     fn add(self, rhs: usize) -> Self::Output {
33340fe15e0SLoGin         return Self(self.0 + rhs);
33440fe15e0SLoGin     }
33540fe15e0SLoGin }
33640fe15e0SLoGin 
33740fe15e0SLoGin impl Sub<VirtAddr> for VirtAddr {
33840fe15e0SLoGin     type Output = usize;
33940fe15e0SLoGin 
34040fe15e0SLoGin     #[inline(always)]
34140fe15e0SLoGin     fn sub(self, rhs: VirtAddr) -> Self::Output {
34240fe15e0SLoGin         return self.0 - rhs.0;
34340fe15e0SLoGin     }
34440fe15e0SLoGin }
34540fe15e0SLoGin 
34640fe15e0SLoGin impl Sub<usize> for VirtAddr {
34740fe15e0SLoGin     type Output = Self;
34840fe15e0SLoGin 
34940fe15e0SLoGin     #[inline(always)]
35040fe15e0SLoGin     fn sub(self, rhs: usize) -> Self::Output {
35140fe15e0SLoGin         return Self(self.0 - rhs);
35240fe15e0SLoGin     }
35340fe15e0SLoGin }
35440fe15e0SLoGin 
35540fe15e0SLoGin impl AddAssign<usize> for VirtAddr {
35640fe15e0SLoGin     #[inline(always)]
35740fe15e0SLoGin     fn add_assign(&mut self, rhs: usize) {
35840fe15e0SLoGin         self.0 += rhs;
35940fe15e0SLoGin     }
36040fe15e0SLoGin }
36140fe15e0SLoGin 
36240fe15e0SLoGin impl AddAssign<VirtAddr> for VirtAddr {
36340fe15e0SLoGin     #[inline(always)]
36440fe15e0SLoGin     fn add_assign(&mut self, rhs: VirtAddr) {
36540fe15e0SLoGin         self.0 += rhs.0;
36640fe15e0SLoGin     }
36740fe15e0SLoGin }
36840fe15e0SLoGin 
36940fe15e0SLoGin impl SubAssign<usize> for VirtAddr {
37040fe15e0SLoGin     #[inline(always)]
37140fe15e0SLoGin     fn sub_assign(&mut self, rhs: usize) {
37240fe15e0SLoGin         self.0 -= rhs;
37340fe15e0SLoGin     }
37440fe15e0SLoGin }
37540fe15e0SLoGin 
37640fe15e0SLoGin impl SubAssign<VirtAddr> for VirtAddr {
37740fe15e0SLoGin     #[inline(always)]
37840fe15e0SLoGin     fn sub_assign(&mut self, rhs: VirtAddr) {
37940fe15e0SLoGin         self.0 -= rhs.0;
38040fe15e0SLoGin     }
38140fe15e0SLoGin }
38240fe15e0SLoGin 
38340fe15e0SLoGin impl Debug for VirtAddr {
38440fe15e0SLoGin     fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
38540fe15e0SLoGin         write!(f, "VirtAddr({:#x})", self.0)
38640fe15e0SLoGin     }
38740fe15e0SLoGin }
38840fe15e0SLoGin 
38940fe15e0SLoGin /// @brief 物理内存区域
39040fe15e0SLoGin #[derive(Clone, Copy, Debug)]
39140fe15e0SLoGin pub struct PhysMemoryArea {
39240fe15e0SLoGin     /// 物理基地址
39340fe15e0SLoGin     pub base: PhysAddr,
39440fe15e0SLoGin     /// 该区域的物理内存大小
39540fe15e0SLoGin     pub size: usize,
39692849878SLoGin 
39792849878SLoGin     pub flags: MemoryAreaAttr,
39840fe15e0SLoGin }
39940fe15e0SLoGin 
40099dbf38dSLoGin impl PhysMemoryArea {
40145626c85SLoGin     pub const DEFAULT: Self = Self {
40245626c85SLoGin         base: PhysAddr::new(0),
40345626c85SLoGin         size: 0,
40492849878SLoGin         flags: MemoryAreaAttr::empty(),
40545626c85SLoGin     };
40645626c85SLoGin 
40792849878SLoGin     pub fn new(base: PhysAddr, size: usize, flags: MemoryAreaAttr) -> Self {
40892849878SLoGin         Self { base, size, flags }
40999dbf38dSLoGin     }
41099dbf38dSLoGin 
41199dbf38dSLoGin     /// 返回向上页面对齐的区域起始物理地址
41299dbf38dSLoGin     pub fn area_base_aligned(&self) -> PhysAddr {
41399dbf38dSLoGin         return PhysAddr::new(
41499dbf38dSLoGin             (self.base.data() + (MMArch::PAGE_SIZE - 1)) & !(MMArch::PAGE_SIZE - 1),
41599dbf38dSLoGin         );
41699dbf38dSLoGin     }
41799dbf38dSLoGin 
41899dbf38dSLoGin     /// 返回向下页面对齐的区域截止物理地址
41999dbf38dSLoGin     pub fn area_end_aligned(&self) -> PhysAddr {
42099dbf38dSLoGin         return PhysAddr::new((self.base.data() + self.size) & !(MMArch::PAGE_SIZE - 1));
42199dbf38dSLoGin     }
42299dbf38dSLoGin }
42399dbf38dSLoGin 
42499dbf38dSLoGin impl Default for PhysMemoryArea {
42599dbf38dSLoGin     fn default() -> Self {
42645626c85SLoGin         return Self::DEFAULT;
42799dbf38dSLoGin     }
42899dbf38dSLoGin }
42999dbf38dSLoGin 
43040fe15e0SLoGin pub trait MemoryManagementArch: Clone + Copy + Debug {
431a17651b1SMemoryShore     /// 是否支持缺页中断
432a17651b1SMemoryShore     const PAGE_FAULT_ENABLED: bool;
43340fe15e0SLoGin     /// 页面大小的shift(假如页面4K,那么这个值就是12,因为2^12=4096)
43440fe15e0SLoGin     const PAGE_SHIFT: usize;
43540fe15e0SLoGin     /// 每个页表的页表项数目。(以2^n次幂来表示)假如有512个页表项,那么这个值就是9
43640fe15e0SLoGin     const PAGE_ENTRY_SHIFT: usize;
43740fe15e0SLoGin     /// 页表层级数量
43840fe15e0SLoGin     const PAGE_LEVELS: usize;
43940fe15e0SLoGin 
44040fe15e0SLoGin     /// 页表项的有效位的index(假如页表项的第0-51位有效,那么这个值就是52)
44140fe15e0SLoGin     const ENTRY_ADDRESS_SHIFT: usize;
44240fe15e0SLoGin     /// 页面的页表项的默认值
44340fe15e0SLoGin     const ENTRY_FLAG_DEFAULT_PAGE: usize;
44440fe15e0SLoGin     /// 页表的页表项的默认值
44540fe15e0SLoGin     const ENTRY_FLAG_DEFAULT_TABLE: usize;
44640fe15e0SLoGin     /// 页表项的present位被置位之后的值
44740fe15e0SLoGin     const ENTRY_FLAG_PRESENT: usize;
44840fe15e0SLoGin     /// 页表项为read only时的值
44940fe15e0SLoGin     const ENTRY_FLAG_READONLY: usize;
450*471d65cfSLoGin     /// 页表项的write bit
451*471d65cfSLoGin     const ENTRY_FLAG_WRITEABLE: usize;
45240fe15e0SLoGin     /// 页表项为可读写状态的值
45340fe15e0SLoGin     const ENTRY_FLAG_READWRITE: usize;
45440fe15e0SLoGin     /// 页面项标记页面为user page的值
45540fe15e0SLoGin     const ENTRY_FLAG_USER: usize;
45640fe15e0SLoGin     /// 页面项标记页面为write through的值
45740fe15e0SLoGin     const ENTRY_FLAG_WRITE_THROUGH: usize;
45840fe15e0SLoGin     /// 页面项标记页面为cache disable的值
45940fe15e0SLoGin     const ENTRY_FLAG_CACHE_DISABLE: usize;
46040fe15e0SLoGin     /// 标记当前页面不可执行的标志位(Execute disable)(也就是说,不能从这段内存里面获取处理器指令)
46140fe15e0SLoGin     const ENTRY_FLAG_NO_EXEC: usize;
46240fe15e0SLoGin     /// 标记当前页面可执行的标志位(Execute enable)
46340fe15e0SLoGin     const ENTRY_FLAG_EXEC: usize;
46492849878SLoGin     /// 当该位为1时,标明这是一个脏页
46592849878SLoGin     const ENTRY_FLAG_DIRTY: usize;
46692849878SLoGin     /// 当该位为1时,代表这个页面被处理器访问过
46792849878SLoGin     const ENTRY_FLAG_ACCESSED: usize;
468a17651b1SMemoryShore     /// 标记该页表项指向的页是否为大页
469a17651b1SMemoryShore     const ENTRY_FLAG_HUGE_PAGE: usize;
470a17651b1SMemoryShore     /// 当该位为1时,代表该页表项是全局的
471a17651b1SMemoryShore     const ENTRY_FLAG_GLOBAL: usize;
47240fe15e0SLoGin 
47340fe15e0SLoGin     /// 虚拟地址与物理地址的偏移量
47440fe15e0SLoGin     const PHYS_OFFSET: usize;
47540fe15e0SLoGin 
476453452ccSLoGin     /// 内核在链接时被链接到的偏移量
477453452ccSLoGin     const KERNEL_LINK_OFFSET: usize;
478453452ccSLoGin 
479453452ccSLoGin     const KERNEL_VIRT_START: usize = Self::PHYS_OFFSET + Self::KERNEL_LINK_OFFSET;
480453452ccSLoGin 
48140fe15e0SLoGin     /// 每个页面的大小
48240fe15e0SLoGin     const PAGE_SIZE: usize = 1 << Self::PAGE_SHIFT;
48340fe15e0SLoGin     /// 通过这个mask,获取地址的页内偏移量
48440fe15e0SLoGin     const PAGE_OFFSET_MASK: usize = Self::PAGE_SIZE - 1;
48540314b30SXiaoye Zheng     /// 通过这个mask,获取页的首地址
48640314b30SXiaoye Zheng     const PAGE_MASK: usize = !(Self::PAGE_OFFSET_MASK);
48740fe15e0SLoGin     /// 页表项的地址、数据部分的shift。
48840fe15e0SLoGin     /// 打个比方,如果这个值为52,那么意味着页表项的[0, 52)位,用于表示地址以及其他的标志位
48940fe15e0SLoGin     const PAGE_ADDRESS_SHIFT: usize = Self::PAGE_LEVELS * Self::PAGE_ENTRY_SHIFT + Self::PAGE_SHIFT;
49040fe15e0SLoGin     /// 最大的虚拟地址(对于不同的架构,由于上述PAGE_ADDRESS_SHIFT可能包括了reserved bits, 事实上能表示的虚拟地址应该比这个值要小)
49140fe15e0SLoGin     const PAGE_ADDRESS_SIZE: usize = 1 << Self::PAGE_ADDRESS_SHIFT;
49240fe15e0SLoGin     /// 页表项的值与这个常量进行与运算,得到的结果是所填写的物理地址
49340fe15e0SLoGin     const PAGE_ADDRESS_MASK: usize = Self::PAGE_ADDRESS_SIZE - Self::PAGE_SIZE;
49440fe15e0SLoGin     /// 每个页表项的大小
49540fe15e0SLoGin     const PAGE_ENTRY_SIZE: usize = 1 << (Self::PAGE_SHIFT - Self::PAGE_ENTRY_SHIFT);
49640fe15e0SLoGin     /// 每个页表的页表项数目
49740fe15e0SLoGin     const PAGE_ENTRY_NUM: usize = 1 << Self::PAGE_ENTRY_SHIFT;
49840fe15e0SLoGin     /// 该字段用于根据虚拟地址,获取该虚拟地址在对应的页表中是第几个页表项
49940fe15e0SLoGin     const PAGE_ENTRY_MASK: usize = Self::PAGE_ENTRY_NUM - 1;
500a17651b1SMemoryShore     /// 内核页表在顶级页表的第一个页表项的索引
501a17651b1SMemoryShore     const PAGE_KERNEL_INDEX: usize = (Self::PHYS_OFFSET & Self::PAGE_ADDRESS_MASK)
502a17651b1SMemoryShore         >> (Self::PAGE_ADDRESS_SHIFT - Self::PAGE_ENTRY_SHIFT);
50340fe15e0SLoGin 
50440fe15e0SLoGin     const PAGE_NEGATIVE_MASK: usize = !((Self::PAGE_ADDRESS_SIZE) - 1);
50540fe15e0SLoGin 
50640fe15e0SLoGin     const ENTRY_ADDRESS_SIZE: usize = 1 << Self::ENTRY_ADDRESS_SHIFT;
50740fe15e0SLoGin     /// 该mask用于获取页表项中地址字段
50840fe15e0SLoGin     const ENTRY_ADDRESS_MASK: usize = Self::ENTRY_ADDRESS_SIZE - Self::PAGE_SIZE;
50940fe15e0SLoGin     /// 这个mask用于获取页表项中的flags
51040fe15e0SLoGin     const ENTRY_FLAGS_MASK: usize = !Self::ENTRY_ADDRESS_MASK;
51140fe15e0SLoGin 
51240fe15e0SLoGin     /// 用户空间的最高地址
51340fe15e0SLoGin     const USER_END_VADDR: VirtAddr;
51440fe15e0SLoGin     /// 用户堆的起始地址
51540fe15e0SLoGin     const USER_BRK_START: VirtAddr;
51640fe15e0SLoGin     /// 用户栈起始地址(向下生长,不包含该值)
51740fe15e0SLoGin     const USER_STACK_START: VirtAddr;
51840fe15e0SLoGin 
51974ffde66SLoGin     /// 内核的固定映射区的起始地址
52074ffde66SLoGin     const FIXMAP_START_VADDR: VirtAddr;
52174ffde66SLoGin     /// 内核的固定映射区的大小
52274ffde66SLoGin     const FIXMAP_SIZE: usize;
52374ffde66SLoGin     /// 内核的固定映射区的结束地址
52474ffde66SLoGin     const FIXMAP_END_VADDR: VirtAddr =
52574ffde66SLoGin         VirtAddr::new(Self::FIXMAP_START_VADDR.data() + Self::FIXMAP_SIZE);
52674ffde66SLoGin 
52723ef2b33SLoGin     /// MMIO虚拟空间的基地址
52823ef2b33SLoGin     const MMIO_BASE: VirtAddr;
52923ef2b33SLoGin     /// MMIO虚拟空间的大小
53023ef2b33SLoGin     const MMIO_SIZE: usize;
53123ef2b33SLoGin     /// MMIO虚拟空间的顶端地址(不包含)
53223ef2b33SLoGin     const MMIO_TOP: VirtAddr = VirtAddr::new(Self::MMIO_BASE.data() + Self::MMIO_SIZE);
53323ef2b33SLoGin 
53440fe15e0SLoGin     /// @brief 用于初始化内存管理模块与架构相关的信息。
53545626c85SLoGin     /// 该函数应调用其他模块的接口,把可用内存区域添加到memblock,提供给BumpAllocator使用
53645626c85SLoGin     unsafe fn init();
53740fe15e0SLoGin 
53823ef2b33SLoGin     /// 内存管理初始化完成后,调用该函数
53923ef2b33SLoGin     unsafe fn arch_post_init() {}
54023ef2b33SLoGin 
54140fe15e0SLoGin     /// @brief 读取指定虚拟地址的值,并假设它是类型T的指针
54240fe15e0SLoGin     #[inline(always)]
54340fe15e0SLoGin     unsafe fn read<T>(address: VirtAddr) -> T {
54440fe15e0SLoGin         return ptr::read(address.data() as *const T);
54540fe15e0SLoGin     }
54640fe15e0SLoGin 
54740fe15e0SLoGin     /// @brief 将value写入到指定的虚拟地址
54840fe15e0SLoGin     #[inline(always)]
54940fe15e0SLoGin     unsafe fn write<T>(address: VirtAddr, value: T) {
55040fe15e0SLoGin         ptr::write(address.data() as *mut T, value);
55140fe15e0SLoGin     }
55240fe15e0SLoGin 
55340fe15e0SLoGin     #[inline(always)]
55440fe15e0SLoGin     unsafe fn write_bytes(address: VirtAddr, value: u8, count: usize) {
55540fe15e0SLoGin         ptr::write_bytes(address.data() as *mut u8, value, count);
55640fe15e0SLoGin     }
55740fe15e0SLoGin 
55840fe15e0SLoGin     /// @brief 刷新TLB中,关于指定虚拟地址的条目
55940fe15e0SLoGin     unsafe fn invalidate_page(address: VirtAddr);
56040fe15e0SLoGin 
56140fe15e0SLoGin     /// @brief 刷新TLB中,所有的条目
56240fe15e0SLoGin     unsafe fn invalidate_all();
56340fe15e0SLoGin 
56440fe15e0SLoGin     /// @brief 获取顶级页表的物理地址
56540fe15e0SLoGin     unsafe fn table(table_kind: PageTableKind) -> PhysAddr;
56640fe15e0SLoGin 
56740fe15e0SLoGin     /// @brief 设置顶级页表的物理地址到处理器中
56840fe15e0SLoGin     unsafe fn set_table(table_kind: PageTableKind, table: PhysAddr);
56940fe15e0SLoGin 
57040fe15e0SLoGin     /// @brief 将物理地址转换为虚拟地址.
57140fe15e0SLoGin     ///
57240fe15e0SLoGin     /// @param phys 物理地址
57340fe15e0SLoGin     ///
57440fe15e0SLoGin     /// @return 转换后的虚拟地址。如果转换失败,返回None
57540fe15e0SLoGin     #[inline(always)]
57640fe15e0SLoGin     unsafe fn phys_2_virt(phys: PhysAddr) -> Option<VirtAddr> {
57740fe15e0SLoGin         if let Some(vaddr) = phys.data().checked_add(Self::PHYS_OFFSET) {
57840fe15e0SLoGin             return Some(VirtAddr::new(vaddr));
57940fe15e0SLoGin         } else {
58040fe15e0SLoGin             return None;
58140fe15e0SLoGin         }
58240fe15e0SLoGin     }
58340fe15e0SLoGin 
58440fe15e0SLoGin     /// 将虚拟地址转换为物理地址
58540fe15e0SLoGin     ///
58640fe15e0SLoGin     /// ## 参数
58740fe15e0SLoGin     ///
58840fe15e0SLoGin     /// - `virt` 虚拟地址
58940fe15e0SLoGin     ///
59040fe15e0SLoGin     /// ## 返回值
59140fe15e0SLoGin     ///
59240fe15e0SLoGin     /// 转换后的物理地址。如果转换失败,返回None
59340fe15e0SLoGin     #[inline(always)]
59440fe15e0SLoGin     unsafe fn virt_2_phys(virt: VirtAddr) -> Option<PhysAddr> {
59540fe15e0SLoGin         if let Some(paddr) = virt.data().checked_sub(Self::PHYS_OFFSET) {
59640fe15e0SLoGin             return Some(PhysAddr::new(paddr));
59740fe15e0SLoGin         } else {
59840fe15e0SLoGin             return None;
59940fe15e0SLoGin         }
60040fe15e0SLoGin     }
60140fe15e0SLoGin 
60240fe15e0SLoGin     /// @brief 判断指定的虚拟地址是否正确(符合规范)
60340fe15e0SLoGin     fn virt_is_valid(virt: VirtAddr) -> bool;
60440fe15e0SLoGin 
60540fe15e0SLoGin     /// 获取内存管理初始化时,创建的第一个内核页表的地址
60640fe15e0SLoGin     fn initial_page_table() -> PhysAddr;
60740fe15e0SLoGin 
60840fe15e0SLoGin     /// 初始化新的usermapper,为用户进程创建页表
60940fe15e0SLoGin     fn setup_new_usermapper() -> Result<UserMapper, SystemError>;
6107a29d4fcSLoGin 
6117a29d4fcSLoGin     /// 创建页表项
6127a29d4fcSLoGin     ///
6137a29d4fcSLoGin     /// 这是一个低阶api,用于根据物理地址以及指定好的pageflags,创建页表项
6147a29d4fcSLoGin     ///
6157a29d4fcSLoGin     /// ## 参数
6167a29d4fcSLoGin     ///
6177a29d4fcSLoGin     /// - `paddr` 物理地址
6187a29d4fcSLoGin     /// - `page_flags` 页表项的flags
6197a29d4fcSLoGin     ///
6207a29d4fcSLoGin     /// ## 返回值
6217a29d4fcSLoGin     ///
6227a29d4fcSLoGin     /// 页表项的值
6237a29d4fcSLoGin     fn make_entry(paddr: PhysAddr, page_flags: usize) -> usize;
624a17651b1SMemoryShore 
625a17651b1SMemoryShore     /// 判断一个VMA是否允许访问
626a17651b1SMemoryShore     ///
627a17651b1SMemoryShore     /// ## 参数
628a17651b1SMemoryShore     ///
629a17651b1SMemoryShore     /// - `vma`: 进行判断的VMA
630a17651b1SMemoryShore     /// - `write`: 是否需要写入权限(true 表示需要写权限)
631a17651b1SMemoryShore     /// - `execute`: 是否需要执行权限(true 表示需要执行权限)
632a17651b1SMemoryShore     /// - `foreign`: 是否是外部的(即非当前进程的)VMA
633a17651b1SMemoryShore     ///
634a17651b1SMemoryShore     /// ## 返回值
635a17651b1SMemoryShore     /// - `true`: VMA允许访问
636a17651b1SMemoryShore     /// - `false`: 错误的说明
637a17651b1SMemoryShore     fn vma_access_permitted(
638a17651b1SMemoryShore         _vma: Arc<LockedVMA>,
639a17651b1SMemoryShore         _write: bool,
640a17651b1SMemoryShore         _execute: bool,
641a17651b1SMemoryShore         _foreign: bool,
642a17651b1SMemoryShore     ) -> bool {
643a17651b1SMemoryShore         true
644a17651b1SMemoryShore     }
64540fe15e0SLoGin }
64640fe15e0SLoGin 
64740fe15e0SLoGin /// @brief 虚拟地址范围
64840fe15e0SLoGin /// 该结构体用于表示一个虚拟地址范围,包括起始地址与大小
64940fe15e0SLoGin ///
65040fe15e0SLoGin /// 请注意与VMA进行区分,该结构体被VMA所包含
65140fe15e0SLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
65240fe15e0SLoGin pub struct VirtRegion {
65340fe15e0SLoGin     start: VirtAddr,
65440fe15e0SLoGin     size: usize,
65540fe15e0SLoGin }
65640fe15e0SLoGin 
65740fe15e0SLoGin #[allow(dead_code)]
65840fe15e0SLoGin impl VirtRegion {
65940fe15e0SLoGin     /// # 创建一个新的虚拟地址范围
66040fe15e0SLoGin     pub fn new(start: VirtAddr, size: usize) -> Self {
66140fe15e0SLoGin         VirtRegion { start, size }
66240fe15e0SLoGin     }
66340fe15e0SLoGin 
66440fe15e0SLoGin     /// 获取虚拟地址范围的起始地址
66540fe15e0SLoGin     #[inline(always)]
66640fe15e0SLoGin     pub fn start(&self) -> VirtAddr {
66740fe15e0SLoGin         self.start
66840fe15e0SLoGin     }
66940fe15e0SLoGin 
67040fe15e0SLoGin     /// 获取虚拟地址范围的截止地址(不包括返回的地址)
67140fe15e0SLoGin     #[inline(always)]
67240fe15e0SLoGin     pub fn end(&self) -> VirtAddr {
67340fe15e0SLoGin         return self.start().add(self.size);
67440fe15e0SLoGin     }
67540fe15e0SLoGin 
67640fe15e0SLoGin     /// # Create a new VirtRegion from a range [start, end)
67740fe15e0SLoGin     ///
67840fe15e0SLoGin     /// If end <= start, return None
67940fe15e0SLoGin     pub fn between(start: VirtAddr, end: VirtAddr) -> Option<Self> {
68040fe15e0SLoGin         if unlikely(end.data() <= start.data()) {
68140fe15e0SLoGin             return None;
68240fe15e0SLoGin         }
68340fe15e0SLoGin         let size = end.data() - start.data();
68440fe15e0SLoGin         return Some(VirtRegion::new(start, size));
68540fe15e0SLoGin     }
68640fe15e0SLoGin 
68740fe15e0SLoGin     /// # 取两个虚拟地址范围的交集
68840fe15e0SLoGin     ///
68940fe15e0SLoGin     /// 如果两个虚拟地址范围没有交集,返回None
69040fe15e0SLoGin     pub fn intersect(&self, other: &VirtRegion) -> Option<VirtRegion> {
69140fe15e0SLoGin         let start = self.start.max(other.start);
69240fe15e0SLoGin         let end = self.end().min(other.end());
69340fe15e0SLoGin         return VirtRegion::between(start, end);
69440fe15e0SLoGin     }
69540fe15e0SLoGin 
69640fe15e0SLoGin     /// 设置虚拟地址范围的起始地址
69740fe15e0SLoGin     #[inline(always)]
69840fe15e0SLoGin     pub fn set_start(&mut self, start: VirtAddr) {
69940fe15e0SLoGin         self.start = start;
70040fe15e0SLoGin     }
70140fe15e0SLoGin 
70240fe15e0SLoGin     #[inline(always)]
70340fe15e0SLoGin     pub fn size(&self) -> usize {
70440fe15e0SLoGin         self.size
70540fe15e0SLoGin     }
70640fe15e0SLoGin 
70740fe15e0SLoGin     /// 设置虚拟地址范围的大小
70840fe15e0SLoGin     #[inline(always)]
70940fe15e0SLoGin     pub fn set_size(&mut self, size: usize) {
71040fe15e0SLoGin         self.size = size;
71140fe15e0SLoGin     }
71240fe15e0SLoGin 
71340fe15e0SLoGin     /// 判断虚拟地址范围是否为空
71440fe15e0SLoGin     #[inline(always)]
71540fe15e0SLoGin     pub fn is_empty(&self) -> bool {
71640fe15e0SLoGin         self.size == 0
71740fe15e0SLoGin     }
71840fe15e0SLoGin 
71940fe15e0SLoGin     /// 将虚拟地址区域的大小向上对齐到页大小
72040fe15e0SLoGin     #[inline(always)]
72140fe15e0SLoGin     pub fn round_up_size_to_page(self) -> Self {
72240fe15e0SLoGin         return VirtRegion::new(self.start, round_up_to_page_size(self.size));
72340fe15e0SLoGin     }
72440fe15e0SLoGin 
72540fe15e0SLoGin     /// 判断两个虚拟地址范围是否由于具有交集而导致冲突
72640fe15e0SLoGin     #[inline(always)]
72740fe15e0SLoGin     pub fn collide(&self, other: &VirtRegion) -> bool {
72840fe15e0SLoGin         return self.intersect(other).is_some();
72940fe15e0SLoGin     }
73040fe15e0SLoGin 
73140fe15e0SLoGin     pub fn iter_pages(&self) -> VirtPageFrameIter {
73240fe15e0SLoGin         return VirtPageFrame::iter_range(
73340fe15e0SLoGin             VirtPageFrame::new(self.start),
73440fe15e0SLoGin             VirtPageFrame::new(self.end()),
73540fe15e0SLoGin         );
73640fe15e0SLoGin     }
73740fe15e0SLoGin 
73840fe15e0SLoGin     /// 获取[self.start(), region.start())的虚拟地址范围
73940fe15e0SLoGin     ///
74040fe15e0SLoGin     /// 如果self.start() >= region.start(),返回None
74140fe15e0SLoGin     pub fn before(self, region: &VirtRegion) -> Option<Self> {
74240fe15e0SLoGin         return Self::between(self.start(), region.start());
74340fe15e0SLoGin     }
74440fe15e0SLoGin 
74540fe15e0SLoGin     /// 获取[region.end(),self.end())的虚拟地址范围
74640fe15e0SLoGin     ///
74740fe15e0SLoGin     /// 如果 self.end() >= region.end() ,返回None
74840fe15e0SLoGin     pub fn after(self, region: &VirtRegion) -> Option<Self> {
74940fe15e0SLoGin         // if self.end() > region.end() none
75040fe15e0SLoGin         return Self::between(region.end(), self.end());
75140fe15e0SLoGin     }
75240fe15e0SLoGin 
75340fe15e0SLoGin     /// 把当前虚拟地址范围内的某个虚拟地址,转换为另一个虚拟地址范围内的虚拟地址
75440fe15e0SLoGin     ///
75540fe15e0SLoGin     /// 如果vaddr不在当前虚拟地址范围内,返回None
75640fe15e0SLoGin     ///
75740fe15e0SLoGin     /// 如果vaddr在当前虚拟地址范围内,返回vaddr在new_base中的虚拟地址
75840fe15e0SLoGin     pub fn rebase(self, vaddr: VirtAddr, new_base: &VirtRegion) -> Option<VirtAddr> {
75940fe15e0SLoGin         if !self.contains(vaddr) {
76040fe15e0SLoGin             return None;
76140fe15e0SLoGin         }
76240fe15e0SLoGin         let offset = vaddr.data() - self.start().data();
76340fe15e0SLoGin         let new_start = new_base.start().data() + offset;
76440fe15e0SLoGin         return Some(VirtAddr::new(new_start));
76540fe15e0SLoGin     }
76640fe15e0SLoGin 
76740fe15e0SLoGin     /// 判断虚拟地址范围是否包含指定的虚拟地址
76840fe15e0SLoGin     pub fn contains(&self, addr: VirtAddr) -> bool {
76940fe15e0SLoGin         return self.start() <= addr && addr < self.end();
77040fe15e0SLoGin     }
77140fe15e0SLoGin 
77240fe15e0SLoGin     /// 创建当前虚拟地址范围的页面迭代器
77340fe15e0SLoGin     pub fn pages(&self) -> VirtPageFrameIter {
77440fe15e0SLoGin         return VirtPageFrame::iter_range(
77540fe15e0SLoGin             VirtPageFrame::new(self.start()),
77640fe15e0SLoGin             VirtPageFrame::new(self.end()),
77740fe15e0SLoGin         );
77840fe15e0SLoGin     }
77940fe15e0SLoGin }
78040fe15e0SLoGin 
78140fe15e0SLoGin impl PartialOrd for VirtRegion {
78240fe15e0SLoGin     fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
783b5b571e0SLoGin         Some(self.cmp(other))
78440fe15e0SLoGin     }
78540fe15e0SLoGin }
78640fe15e0SLoGin 
78740fe15e0SLoGin impl Ord for VirtRegion {
78840fe15e0SLoGin     fn cmp(&self, other: &Self) -> cmp::Ordering {
78940fe15e0SLoGin         return self.start.cmp(&other.start);
79040fe15e0SLoGin     }
79140fe15e0SLoGin }
79240fe15e0SLoGin 
79340fe15e0SLoGin /// ## 判断虚拟地址是否超出了用户空间
79440fe15e0SLoGin ///
79540fe15e0SLoGin /// 如果虚拟地址超出了用户空间,返回Err(SystemError::EFAULT).
79640fe15e0SLoGin /// 如果end < start,返回Err(SystemError::EOVERFLOW)
79740fe15e0SLoGin ///
79840fe15e0SLoGin /// 否则返回Ok(())
79940fe15e0SLoGin pub fn verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError> {
80040fe15e0SLoGin     let end = addr.add(size);
80140fe15e0SLoGin     if unlikely(end.data() < addr.data()) {
80240fe15e0SLoGin         return Err(SystemError::EOVERFLOW);
80340fe15e0SLoGin     }
80440fe15e0SLoGin 
80540fe15e0SLoGin     if !addr.check_user() || !end.check_user() {
80640fe15e0SLoGin         return Err(SystemError::EFAULT);
80740fe15e0SLoGin     }
80840fe15e0SLoGin 
80940fe15e0SLoGin     return Ok(());
81040fe15e0SLoGin }
811