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}, 1740fe15e0SLoGin page::round_up_to_page_size, 1840fe15e0SLoGin ucontext::{AddressSpace, UserMapper}, 1940fe15e0SLoGin }; 20004e86ffSlogin 2182d2e446Slogin pub mod allocator; 2240fe15e0SLoGin pub mod c_adapter; 2340fe15e0SLoGin pub mod kernel_mapper; 24*45626c85SLoGin pub mod memblock; 25c2481452Shoumkh pub mod mmio_buddy; 2640fe15e0SLoGin pub mod no_init; 2740fe15e0SLoGin pub mod page; 28c3dad001SLoGin pub mod percpu; 29ab5c8ca4Slogin pub mod syscall; 3040fe15e0SLoGin pub mod ucontext; 3140fe15e0SLoGin 3240fe15e0SLoGin /// 内核INIT进程的用户地址空间结构体(仅在process_init中初始化) 3340fe15e0SLoGin static mut __INITIAL_PROCESS_ADDRESS_SPACE: Option<Arc<AddressSpace>> = None; 3440fe15e0SLoGin 3540fe15e0SLoGin /// 获取内核INIT进程的用户地址空间结构体 3640fe15e0SLoGin #[allow(non_snake_case)] 3740fe15e0SLoGin #[inline(always)] 3840fe15e0SLoGin pub fn INITIAL_PROCESS_ADDRESS_SPACE() -> Arc<AddressSpace> { 3940fe15e0SLoGin unsafe { 4040fe15e0SLoGin return __INITIAL_PROCESS_ADDRESS_SPACE 4140fe15e0SLoGin .as_ref() 4240fe15e0SLoGin .expect("INITIAL_PROCESS_ADDRESS_SPACE is null") 4340fe15e0SLoGin .clone(); 4440fe15e0SLoGin } 4540fe15e0SLoGin } 4640fe15e0SLoGin 4740fe15e0SLoGin /// 设置内核INIT进程的用户地址空间结构体全局变量 4840fe15e0SLoGin #[allow(non_snake_case)] 4940fe15e0SLoGin pub unsafe fn set_INITIAL_PROCESS_ADDRESS_SPACE(address_space: Arc<AddressSpace>) { 5040fe15e0SLoGin static INITIALIZED: AtomicBool = AtomicBool::new(false); 5140fe15e0SLoGin if INITIALIZED 5240fe15e0SLoGin .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire) 5340fe15e0SLoGin .is_err() 5440fe15e0SLoGin { 5540fe15e0SLoGin panic!("INITIAL_PROCESS_ADDRESS_SPACE is already initialized"); 5640fe15e0SLoGin } 5740fe15e0SLoGin __INITIAL_PROCESS_ADDRESS_SPACE = Some(address_space); 5840fe15e0SLoGin } 59004e86ffSlogin 60004e86ffSlogin /// @brief 将内核空间的虚拟地址转换为物理地址 61004e86ffSlogin #[inline(always)] 62004e86ffSlogin pub fn virt_2_phys(addr: usize) -> usize { 63004e86ffSlogin addr - PAGE_OFFSET as usize 64004e86ffSlogin } 65004e86ffSlogin 66004e86ffSlogin /// @brief 将物理地址转换为内核空间的虚拟地址 67004e86ffSlogin #[inline(always)] 68004e86ffSlogin pub fn phys_2_virt(addr: usize) -> usize { 69004e86ffSlogin addr + PAGE_OFFSET as usize 70004e86ffSlogin } 71bacd691cSlogin 7240fe15e0SLoGin #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] 7340fe15e0SLoGin pub enum PageTableKind { 7440fe15e0SLoGin /// 用户可访问的页表 7540fe15e0SLoGin User, 7640fe15e0SLoGin /// 内核页表 7740fe15e0SLoGin Kernel, 7840314b30SXiaoye Zheng /// 内存虚拟化中使用的EPT 7940314b30SXiaoye Zheng EPT, 8040fe15e0SLoGin } 8140fe15e0SLoGin 8240fe15e0SLoGin /// 物理内存地址 8340fe15e0SLoGin #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)] 8440fe15e0SLoGin #[repr(transparent)] 8540fe15e0SLoGin pub struct PhysAddr(usize); 8640fe15e0SLoGin 8740fe15e0SLoGin impl PhysAddr { 8840fe15e0SLoGin #[inline(always)] 8940fe15e0SLoGin pub const fn new(address: usize) -> Self { 9040fe15e0SLoGin Self(address) 9140fe15e0SLoGin } 9240fe15e0SLoGin 9340fe15e0SLoGin /// @brief 获取物理地址的值 9440fe15e0SLoGin #[inline(always)] 9540fe15e0SLoGin pub fn data(&self) -> usize { 9640fe15e0SLoGin self.0 9740fe15e0SLoGin } 9840fe15e0SLoGin 9940fe15e0SLoGin /// @brief 将物理地址加上一个偏移量 10040fe15e0SLoGin #[inline(always)] 10140fe15e0SLoGin pub fn add(self, offset: usize) -> Self { 10240fe15e0SLoGin Self(self.0 + offset) 10340fe15e0SLoGin } 10440fe15e0SLoGin 10540fe15e0SLoGin /// @brief 判断物理地址是否按照指定要求对齐 10640fe15e0SLoGin #[inline(always)] 10740fe15e0SLoGin pub fn check_aligned(&self, align: usize) -> bool { 10840fe15e0SLoGin return self.0 & (align - 1) == 0; 10940fe15e0SLoGin } 11040fe15e0SLoGin 11140fe15e0SLoGin #[inline(always)] 11240fe15e0SLoGin pub fn is_null(&self) -> bool { 11340fe15e0SLoGin return self.0 == 0; 11440fe15e0SLoGin } 11540fe15e0SLoGin } 11640fe15e0SLoGin 11740fe15e0SLoGin impl Debug for PhysAddr { 11840fe15e0SLoGin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 11940fe15e0SLoGin write!(f, "PhysAddr({:#x})", self.0) 12040fe15e0SLoGin } 12140fe15e0SLoGin } 12240fe15e0SLoGin 12340fe15e0SLoGin impl core::ops::Add<usize> for PhysAddr { 12440fe15e0SLoGin type Output = Self; 12540fe15e0SLoGin 12640fe15e0SLoGin #[inline(always)] 12740fe15e0SLoGin fn add(self, rhs: usize) -> Self::Output { 12840fe15e0SLoGin return Self(self.0 + rhs); 12940fe15e0SLoGin } 13040fe15e0SLoGin } 13140fe15e0SLoGin 13240fe15e0SLoGin impl core::ops::AddAssign<usize> for PhysAddr { 13340fe15e0SLoGin #[inline(always)] 13440fe15e0SLoGin fn add_assign(&mut self, rhs: usize) { 13540fe15e0SLoGin self.0 += rhs; 13640fe15e0SLoGin } 13740fe15e0SLoGin } 13840fe15e0SLoGin 13940fe15e0SLoGin impl core::ops::Add<PhysAddr> for PhysAddr { 14040fe15e0SLoGin type Output = Self; 14140fe15e0SLoGin 14240fe15e0SLoGin #[inline(always)] 14340fe15e0SLoGin fn add(self, rhs: PhysAddr) -> Self::Output { 14440fe15e0SLoGin return Self(self.0 + rhs.0); 14540fe15e0SLoGin } 14640fe15e0SLoGin } 14740fe15e0SLoGin 14840fe15e0SLoGin impl core::ops::AddAssign<PhysAddr> for PhysAddr { 14940fe15e0SLoGin #[inline(always)] 15040fe15e0SLoGin fn add_assign(&mut self, rhs: PhysAddr) { 15140fe15e0SLoGin self.0 += rhs.0; 15240fe15e0SLoGin } 15340fe15e0SLoGin } 15440fe15e0SLoGin 1552dd9f0c7SLoGin impl core::ops::BitOrAssign<usize> for PhysAddr { 1562dd9f0c7SLoGin #[inline(always)] 1572dd9f0c7SLoGin fn bitor_assign(&mut self, rhs: usize) { 1582dd9f0c7SLoGin self.0 |= rhs; 1592dd9f0c7SLoGin } 1602dd9f0c7SLoGin } 1612dd9f0c7SLoGin 1622dd9f0c7SLoGin impl core::ops::BitOrAssign<PhysAddr> for PhysAddr { 1632dd9f0c7SLoGin #[inline(always)] 1642dd9f0c7SLoGin fn bitor_assign(&mut self, rhs: PhysAddr) { 1652dd9f0c7SLoGin self.0 |= rhs.0; 1662dd9f0c7SLoGin } 1672dd9f0c7SLoGin } 1682dd9f0c7SLoGin 16940fe15e0SLoGin impl core::ops::Sub<usize> for PhysAddr { 17040fe15e0SLoGin type Output = Self; 17140fe15e0SLoGin 17240fe15e0SLoGin #[inline(always)] 17340fe15e0SLoGin fn sub(self, rhs: usize) -> Self::Output { 17440fe15e0SLoGin return Self(self.0 - rhs); 17540fe15e0SLoGin } 17640fe15e0SLoGin } 17740fe15e0SLoGin 17840fe15e0SLoGin impl core::ops::SubAssign<usize> for PhysAddr { 17940fe15e0SLoGin #[inline(always)] 18040fe15e0SLoGin fn sub_assign(&mut self, rhs: usize) { 18140fe15e0SLoGin self.0 -= rhs; 18240fe15e0SLoGin } 18340fe15e0SLoGin } 18440fe15e0SLoGin 18540fe15e0SLoGin impl core::ops::Sub<PhysAddr> for PhysAddr { 18640fe15e0SLoGin type Output = usize; 18740fe15e0SLoGin 18840fe15e0SLoGin #[inline(always)] 18940fe15e0SLoGin fn sub(self, rhs: PhysAddr) -> Self::Output { 19040fe15e0SLoGin return self.0 - rhs.0; 19140fe15e0SLoGin } 19240fe15e0SLoGin } 19340fe15e0SLoGin 19440fe15e0SLoGin impl core::ops::SubAssign<PhysAddr> for PhysAddr { 19540fe15e0SLoGin #[inline(always)] 19640fe15e0SLoGin fn sub_assign(&mut self, rhs: PhysAddr) { 19740fe15e0SLoGin self.0 -= rhs.0; 19840fe15e0SLoGin } 19940fe15e0SLoGin } 20040fe15e0SLoGin 20140fe15e0SLoGin /// 虚拟内存地址 20240fe15e0SLoGin #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)] 20340fe15e0SLoGin #[repr(transparent)] 20440fe15e0SLoGin pub struct VirtAddr(usize); 20540fe15e0SLoGin 20640fe15e0SLoGin impl VirtAddr { 20740fe15e0SLoGin #[inline(always)] 20840fe15e0SLoGin pub const fn new(address: usize) -> Self { 20940fe15e0SLoGin return Self(address); 21040fe15e0SLoGin } 21140fe15e0SLoGin 21240fe15e0SLoGin /// @brief 获取虚拟地址的值 21340fe15e0SLoGin #[inline(always)] 21440fe15e0SLoGin pub fn data(&self) -> usize { 21540fe15e0SLoGin return self.0; 21640fe15e0SLoGin } 21740fe15e0SLoGin 21840fe15e0SLoGin /// @brief 判断虚拟地址的类型 21940fe15e0SLoGin #[inline(always)] 22040fe15e0SLoGin pub fn kind(&self) -> PageTableKind { 22140fe15e0SLoGin if self.check_user() { 22240fe15e0SLoGin return PageTableKind::User; 22340fe15e0SLoGin } else { 22440fe15e0SLoGin return PageTableKind::Kernel; 22540fe15e0SLoGin } 22640fe15e0SLoGin } 22740fe15e0SLoGin 22840fe15e0SLoGin /// @brief 判断虚拟地址是否按照指定要求对齐 22940fe15e0SLoGin #[inline(always)] 23040fe15e0SLoGin pub fn check_aligned(&self, align: usize) -> bool { 23140fe15e0SLoGin return self.0 & (align - 1) == 0; 23240fe15e0SLoGin } 23340fe15e0SLoGin 23440fe15e0SLoGin /// @brief 判断虚拟地址是否在用户空间 23540fe15e0SLoGin #[inline(always)] 23640fe15e0SLoGin pub fn check_user(&self) -> bool { 23740fe15e0SLoGin if self < &MMArch::USER_END_VADDR { 23840fe15e0SLoGin return true; 23940fe15e0SLoGin } else { 24040fe15e0SLoGin return false; 24140fe15e0SLoGin } 24240fe15e0SLoGin } 24340fe15e0SLoGin 24440fe15e0SLoGin #[inline(always)] 24540fe15e0SLoGin pub fn as_ptr<T>(self) -> *mut T { 24640fe15e0SLoGin return self.0 as *mut T; 24740fe15e0SLoGin } 24840fe15e0SLoGin 24940fe15e0SLoGin #[inline(always)] 25040fe15e0SLoGin pub fn is_null(&self) -> bool { 25140fe15e0SLoGin return self.0 == 0; 25240fe15e0SLoGin } 25340fe15e0SLoGin } 25440fe15e0SLoGin 25540fe15e0SLoGin impl Add<VirtAddr> for VirtAddr { 25640fe15e0SLoGin type Output = Self; 25740fe15e0SLoGin 25840fe15e0SLoGin #[inline(always)] 25940fe15e0SLoGin fn add(self, rhs: VirtAddr) -> Self::Output { 26040fe15e0SLoGin return Self(self.0 + rhs.0); 26140fe15e0SLoGin } 26240fe15e0SLoGin } 26340fe15e0SLoGin 26440fe15e0SLoGin impl Add<usize> for VirtAddr { 26540fe15e0SLoGin type Output = Self; 26640fe15e0SLoGin 26740fe15e0SLoGin #[inline(always)] 26840fe15e0SLoGin fn add(self, rhs: usize) -> Self::Output { 26940fe15e0SLoGin return Self(self.0 + rhs); 27040fe15e0SLoGin } 27140fe15e0SLoGin } 27240fe15e0SLoGin 27340fe15e0SLoGin impl Sub<VirtAddr> for VirtAddr { 27440fe15e0SLoGin type Output = usize; 27540fe15e0SLoGin 27640fe15e0SLoGin #[inline(always)] 27740fe15e0SLoGin fn sub(self, rhs: VirtAddr) -> Self::Output { 27840fe15e0SLoGin return self.0 - rhs.0; 27940fe15e0SLoGin } 28040fe15e0SLoGin } 28140fe15e0SLoGin 28240fe15e0SLoGin impl Sub<usize> for VirtAddr { 28340fe15e0SLoGin type Output = Self; 28440fe15e0SLoGin 28540fe15e0SLoGin #[inline(always)] 28640fe15e0SLoGin fn sub(self, rhs: usize) -> Self::Output { 28740fe15e0SLoGin return Self(self.0 - rhs); 28840fe15e0SLoGin } 28940fe15e0SLoGin } 29040fe15e0SLoGin 29140fe15e0SLoGin impl AddAssign<usize> for VirtAddr { 29240fe15e0SLoGin #[inline(always)] 29340fe15e0SLoGin fn add_assign(&mut self, rhs: usize) { 29440fe15e0SLoGin self.0 += rhs; 29540fe15e0SLoGin } 29640fe15e0SLoGin } 29740fe15e0SLoGin 29840fe15e0SLoGin impl AddAssign<VirtAddr> for VirtAddr { 29940fe15e0SLoGin #[inline(always)] 30040fe15e0SLoGin fn add_assign(&mut self, rhs: VirtAddr) { 30140fe15e0SLoGin self.0 += rhs.0; 30240fe15e0SLoGin } 30340fe15e0SLoGin } 30440fe15e0SLoGin 30540fe15e0SLoGin impl SubAssign<usize> for VirtAddr { 30640fe15e0SLoGin #[inline(always)] 30740fe15e0SLoGin fn sub_assign(&mut self, rhs: usize) { 30840fe15e0SLoGin self.0 -= rhs; 30940fe15e0SLoGin } 31040fe15e0SLoGin } 31140fe15e0SLoGin 31240fe15e0SLoGin impl SubAssign<VirtAddr> for VirtAddr { 31340fe15e0SLoGin #[inline(always)] 31440fe15e0SLoGin fn sub_assign(&mut self, rhs: VirtAddr) { 31540fe15e0SLoGin self.0 -= rhs.0; 31640fe15e0SLoGin } 31740fe15e0SLoGin } 31840fe15e0SLoGin 31940fe15e0SLoGin impl Debug for VirtAddr { 32040fe15e0SLoGin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 32140fe15e0SLoGin write!(f, "VirtAddr({:#x})", self.0) 32240fe15e0SLoGin } 32340fe15e0SLoGin } 32440fe15e0SLoGin 32540fe15e0SLoGin /// @brief 物理内存区域 32640fe15e0SLoGin #[derive(Clone, Copy, Debug)] 32740fe15e0SLoGin pub struct PhysMemoryArea { 32840fe15e0SLoGin /// 物理基地址 32940fe15e0SLoGin pub base: PhysAddr, 33040fe15e0SLoGin /// 该区域的物理内存大小 33140fe15e0SLoGin pub size: usize, 33240fe15e0SLoGin } 33340fe15e0SLoGin 33499dbf38dSLoGin impl PhysMemoryArea { 335*45626c85SLoGin pub const DEFAULT: Self = Self { 336*45626c85SLoGin base: PhysAddr::new(0), 337*45626c85SLoGin size: 0, 338*45626c85SLoGin }; 339*45626c85SLoGin 34099dbf38dSLoGin pub fn new(base: PhysAddr, size: usize) -> Self { 34199dbf38dSLoGin Self { base, size } 34299dbf38dSLoGin } 34399dbf38dSLoGin 34499dbf38dSLoGin /// 返回向上页面对齐的区域起始物理地址 34599dbf38dSLoGin pub fn area_base_aligned(&self) -> PhysAddr { 34699dbf38dSLoGin return PhysAddr::new( 34799dbf38dSLoGin (self.base.data() + (MMArch::PAGE_SIZE - 1)) & !(MMArch::PAGE_SIZE - 1), 34899dbf38dSLoGin ); 34999dbf38dSLoGin } 35099dbf38dSLoGin 35199dbf38dSLoGin /// 返回向下页面对齐的区域截止物理地址 35299dbf38dSLoGin pub fn area_end_aligned(&self) -> PhysAddr { 35399dbf38dSLoGin return PhysAddr::new((self.base.data() + self.size) & !(MMArch::PAGE_SIZE - 1)); 35499dbf38dSLoGin } 35599dbf38dSLoGin } 35699dbf38dSLoGin 35799dbf38dSLoGin impl Default for PhysMemoryArea { 35899dbf38dSLoGin fn default() -> Self { 359*45626c85SLoGin return Self::DEFAULT; 36099dbf38dSLoGin } 36199dbf38dSLoGin } 36299dbf38dSLoGin 36340fe15e0SLoGin pub trait MemoryManagementArch: Clone + Copy + Debug { 36440fe15e0SLoGin /// 页面大小的shift(假如页面4K,那么这个值就是12,因为2^12=4096) 36540fe15e0SLoGin const PAGE_SHIFT: usize; 36640fe15e0SLoGin /// 每个页表的页表项数目。(以2^n次幂来表示)假如有512个页表项,那么这个值就是9 36740fe15e0SLoGin const PAGE_ENTRY_SHIFT: usize; 36840fe15e0SLoGin /// 页表层级数量 36940fe15e0SLoGin const PAGE_LEVELS: usize; 37040fe15e0SLoGin 37140fe15e0SLoGin /// 页表项的有效位的index(假如页表项的第0-51位有效,那么这个值就是52) 37240fe15e0SLoGin const ENTRY_ADDRESS_SHIFT: usize; 37340fe15e0SLoGin /// 页面的页表项的默认值 37440fe15e0SLoGin const ENTRY_FLAG_DEFAULT_PAGE: usize; 37540fe15e0SLoGin /// 页表的页表项的默认值 37640fe15e0SLoGin const ENTRY_FLAG_DEFAULT_TABLE: usize; 37740fe15e0SLoGin /// 页表项的present位被置位之后的值 37840fe15e0SLoGin const ENTRY_FLAG_PRESENT: usize; 37940fe15e0SLoGin /// 页表项为read only时的值 38040fe15e0SLoGin const ENTRY_FLAG_READONLY: usize; 38140fe15e0SLoGin /// 页表项为可读写状态的值 38240fe15e0SLoGin const ENTRY_FLAG_READWRITE: usize; 38340fe15e0SLoGin /// 页面项标记页面为user page的值 38440fe15e0SLoGin const ENTRY_FLAG_USER: usize; 38540fe15e0SLoGin /// 页面项标记页面为write through的值 38640fe15e0SLoGin const ENTRY_FLAG_WRITE_THROUGH: usize; 38740fe15e0SLoGin /// 页面项标记页面为cache disable的值 38840fe15e0SLoGin const ENTRY_FLAG_CACHE_DISABLE: usize; 38940fe15e0SLoGin /// 标记当前页面不可执行的标志位(Execute disable)(也就是说,不能从这段内存里面获取处理器指令) 39040fe15e0SLoGin const ENTRY_FLAG_NO_EXEC: usize; 39140fe15e0SLoGin /// 标记当前页面可执行的标志位(Execute enable) 39240fe15e0SLoGin const ENTRY_FLAG_EXEC: usize; 39340fe15e0SLoGin 39440fe15e0SLoGin /// 虚拟地址与物理地址的偏移量 39540fe15e0SLoGin const PHYS_OFFSET: usize; 39640fe15e0SLoGin 39740fe15e0SLoGin /// 每个页面的大小 39840fe15e0SLoGin const PAGE_SIZE: usize = 1 << Self::PAGE_SHIFT; 39940fe15e0SLoGin /// 通过这个mask,获取地址的页内偏移量 40040fe15e0SLoGin const PAGE_OFFSET_MASK: usize = Self::PAGE_SIZE - 1; 40140314b30SXiaoye Zheng /// 通过这个mask,获取页的首地址 40240314b30SXiaoye Zheng const PAGE_MASK: usize = !(Self::PAGE_OFFSET_MASK); 40340fe15e0SLoGin /// 页表项的地址、数据部分的shift。 40440fe15e0SLoGin /// 打个比方,如果这个值为52,那么意味着页表项的[0, 52)位,用于表示地址以及其他的标志位 40540fe15e0SLoGin const PAGE_ADDRESS_SHIFT: usize = Self::PAGE_LEVELS * Self::PAGE_ENTRY_SHIFT + Self::PAGE_SHIFT; 40640fe15e0SLoGin /// 最大的虚拟地址(对于不同的架构,由于上述PAGE_ADDRESS_SHIFT可能包括了reserved bits, 事实上能表示的虚拟地址应该比这个值要小) 40740fe15e0SLoGin const PAGE_ADDRESS_SIZE: usize = 1 << Self::PAGE_ADDRESS_SHIFT; 40840fe15e0SLoGin /// 页表项的值与这个常量进行与运算,得到的结果是所填写的物理地址 40940fe15e0SLoGin const PAGE_ADDRESS_MASK: usize = Self::PAGE_ADDRESS_SIZE - Self::PAGE_SIZE; 41040fe15e0SLoGin /// 每个页表项的大小 41140fe15e0SLoGin const PAGE_ENTRY_SIZE: usize = 1 << (Self::PAGE_SHIFT - Self::PAGE_ENTRY_SHIFT); 41240fe15e0SLoGin /// 每个页表的页表项数目 41340fe15e0SLoGin const PAGE_ENTRY_NUM: usize = 1 << Self::PAGE_ENTRY_SHIFT; 41440fe15e0SLoGin /// 该字段用于根据虚拟地址,获取该虚拟地址在对应的页表中是第几个页表项 41540fe15e0SLoGin const PAGE_ENTRY_MASK: usize = Self::PAGE_ENTRY_NUM - 1; 41640fe15e0SLoGin 41740fe15e0SLoGin const PAGE_NEGATIVE_MASK: usize = !((Self::PAGE_ADDRESS_SIZE) - 1); 41840fe15e0SLoGin 41940fe15e0SLoGin const ENTRY_ADDRESS_SIZE: usize = 1 << Self::ENTRY_ADDRESS_SHIFT; 42040fe15e0SLoGin /// 该mask用于获取页表项中地址字段 42140fe15e0SLoGin const ENTRY_ADDRESS_MASK: usize = Self::ENTRY_ADDRESS_SIZE - Self::PAGE_SIZE; 42240fe15e0SLoGin /// 这个mask用于获取页表项中的flags 42340fe15e0SLoGin const ENTRY_FLAGS_MASK: usize = !Self::ENTRY_ADDRESS_MASK; 42440fe15e0SLoGin 42540fe15e0SLoGin /// 用户空间的最高地址 42640fe15e0SLoGin const USER_END_VADDR: VirtAddr; 42740fe15e0SLoGin /// 用户堆的起始地址 42840fe15e0SLoGin const USER_BRK_START: VirtAddr; 42940fe15e0SLoGin /// 用户栈起始地址(向下生长,不包含该值) 43040fe15e0SLoGin const USER_STACK_START: VirtAddr; 43140fe15e0SLoGin 43240fe15e0SLoGin /// @brief 用于初始化内存管理模块与架构相关的信息。 433*45626c85SLoGin /// 该函数应调用其他模块的接口,把可用内存区域添加到memblock,提供给BumpAllocator使用 434*45626c85SLoGin unsafe fn init(); 43540fe15e0SLoGin 43640fe15e0SLoGin /// @brief 读取指定虚拟地址的值,并假设它是类型T的指针 43740fe15e0SLoGin #[inline(always)] 43840fe15e0SLoGin unsafe fn read<T>(address: VirtAddr) -> T { 43940fe15e0SLoGin return ptr::read(address.data() as *const T); 44040fe15e0SLoGin } 44140fe15e0SLoGin 44240fe15e0SLoGin /// @brief 将value写入到指定的虚拟地址 44340fe15e0SLoGin #[inline(always)] 44440fe15e0SLoGin unsafe fn write<T>(address: VirtAddr, value: T) { 44540fe15e0SLoGin ptr::write(address.data() as *mut T, value); 44640fe15e0SLoGin } 44740fe15e0SLoGin 44840fe15e0SLoGin #[inline(always)] 44940fe15e0SLoGin unsafe fn write_bytes(address: VirtAddr, value: u8, count: usize) { 45040fe15e0SLoGin ptr::write_bytes(address.data() as *mut u8, value, count); 45140fe15e0SLoGin } 45240fe15e0SLoGin 45340fe15e0SLoGin /// @brief 刷新TLB中,关于指定虚拟地址的条目 45440fe15e0SLoGin unsafe fn invalidate_page(address: VirtAddr); 45540fe15e0SLoGin 45640fe15e0SLoGin /// @brief 刷新TLB中,所有的条目 45740fe15e0SLoGin unsafe fn invalidate_all(); 45840fe15e0SLoGin 45940fe15e0SLoGin /// @brief 获取顶级页表的物理地址 46040fe15e0SLoGin unsafe fn table(table_kind: PageTableKind) -> PhysAddr; 46140fe15e0SLoGin 46240fe15e0SLoGin /// @brief 设置顶级页表的物理地址到处理器中 46340fe15e0SLoGin unsafe fn set_table(table_kind: PageTableKind, table: PhysAddr); 46440fe15e0SLoGin 46540fe15e0SLoGin /// @brief 将物理地址转换为虚拟地址. 46640fe15e0SLoGin /// 46740fe15e0SLoGin /// @param phys 物理地址 46840fe15e0SLoGin /// 46940fe15e0SLoGin /// @return 转换后的虚拟地址。如果转换失败,返回None 47040fe15e0SLoGin #[inline(always)] 47140fe15e0SLoGin unsafe fn phys_2_virt(phys: PhysAddr) -> Option<VirtAddr> { 47240fe15e0SLoGin if let Some(vaddr) = phys.data().checked_add(Self::PHYS_OFFSET) { 47340fe15e0SLoGin return Some(VirtAddr::new(vaddr)); 47440fe15e0SLoGin } else { 47540fe15e0SLoGin return None; 47640fe15e0SLoGin } 47740fe15e0SLoGin } 47840fe15e0SLoGin 47940fe15e0SLoGin /// 将虚拟地址转换为物理地址 48040fe15e0SLoGin /// 48140fe15e0SLoGin /// ## 参数 48240fe15e0SLoGin /// 48340fe15e0SLoGin /// - `virt` 虚拟地址 48440fe15e0SLoGin /// 48540fe15e0SLoGin /// ## 返回值 48640fe15e0SLoGin /// 48740fe15e0SLoGin /// 转换后的物理地址。如果转换失败,返回None 48840fe15e0SLoGin #[inline(always)] 48940fe15e0SLoGin unsafe fn virt_2_phys(virt: VirtAddr) -> Option<PhysAddr> { 49040fe15e0SLoGin if let Some(paddr) = virt.data().checked_sub(Self::PHYS_OFFSET) { 49140fe15e0SLoGin return Some(PhysAddr::new(paddr)); 49240fe15e0SLoGin } else { 49340fe15e0SLoGin return None; 49440fe15e0SLoGin } 49540fe15e0SLoGin } 49640fe15e0SLoGin 49740fe15e0SLoGin /// @brief 判断指定的虚拟地址是否正确(符合规范) 49840fe15e0SLoGin fn virt_is_valid(virt: VirtAddr) -> bool; 49940fe15e0SLoGin 50040fe15e0SLoGin /// 获取内存管理初始化时,创建的第一个内核页表的地址 50140fe15e0SLoGin fn initial_page_table() -> PhysAddr; 50240fe15e0SLoGin 50340fe15e0SLoGin /// 初始化新的usermapper,为用户进程创建页表 50440fe15e0SLoGin fn setup_new_usermapper() -> Result<UserMapper, SystemError>; 50540fe15e0SLoGin } 50640fe15e0SLoGin 50740fe15e0SLoGin /// @brief 虚拟地址范围 50840fe15e0SLoGin /// 该结构体用于表示一个虚拟地址范围,包括起始地址与大小 50940fe15e0SLoGin /// 51040fe15e0SLoGin /// 请注意与VMA进行区分,该结构体被VMA所包含 51140fe15e0SLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 51240fe15e0SLoGin pub struct VirtRegion { 51340fe15e0SLoGin start: VirtAddr, 51440fe15e0SLoGin size: usize, 51540fe15e0SLoGin } 51640fe15e0SLoGin 51740fe15e0SLoGin #[allow(dead_code)] 51840fe15e0SLoGin impl VirtRegion { 51940fe15e0SLoGin /// # 创建一个新的虚拟地址范围 52040fe15e0SLoGin pub fn new(start: VirtAddr, size: usize) -> Self { 52140fe15e0SLoGin VirtRegion { start, size } 52240fe15e0SLoGin } 52340fe15e0SLoGin 52440fe15e0SLoGin /// 获取虚拟地址范围的起始地址 52540fe15e0SLoGin #[inline(always)] 52640fe15e0SLoGin pub fn start(&self) -> VirtAddr { 52740fe15e0SLoGin self.start 52840fe15e0SLoGin } 52940fe15e0SLoGin 53040fe15e0SLoGin /// 获取虚拟地址范围的截止地址(不包括返回的地址) 53140fe15e0SLoGin #[inline(always)] 53240fe15e0SLoGin pub fn end(&self) -> VirtAddr { 53340fe15e0SLoGin return self.start().add(self.size); 53440fe15e0SLoGin } 53540fe15e0SLoGin 53640fe15e0SLoGin /// # Create a new VirtRegion from a range [start, end) 53740fe15e0SLoGin /// 53840fe15e0SLoGin /// If end <= start, return None 53940fe15e0SLoGin pub fn between(start: VirtAddr, end: VirtAddr) -> Option<Self> { 54040fe15e0SLoGin if unlikely(end.data() <= start.data()) { 54140fe15e0SLoGin return None; 54240fe15e0SLoGin } 54340fe15e0SLoGin let size = end.data() - start.data(); 54440fe15e0SLoGin return Some(VirtRegion::new(start, size)); 54540fe15e0SLoGin } 54640fe15e0SLoGin 54740fe15e0SLoGin /// # 取两个虚拟地址范围的交集 54840fe15e0SLoGin /// 54940fe15e0SLoGin /// 如果两个虚拟地址范围没有交集,返回None 55040fe15e0SLoGin pub fn intersect(&self, other: &VirtRegion) -> Option<VirtRegion> { 55140fe15e0SLoGin let start = self.start.max(other.start); 55240fe15e0SLoGin let end = self.end().min(other.end()); 55340fe15e0SLoGin return VirtRegion::between(start, end); 55440fe15e0SLoGin } 55540fe15e0SLoGin 55640fe15e0SLoGin /// 设置虚拟地址范围的起始地址 55740fe15e0SLoGin #[inline(always)] 55840fe15e0SLoGin pub fn set_start(&mut self, start: VirtAddr) { 55940fe15e0SLoGin self.start = start; 56040fe15e0SLoGin } 56140fe15e0SLoGin 56240fe15e0SLoGin #[inline(always)] 56340fe15e0SLoGin pub fn size(&self) -> usize { 56440fe15e0SLoGin self.size 56540fe15e0SLoGin } 56640fe15e0SLoGin 56740fe15e0SLoGin /// 设置虚拟地址范围的大小 56840fe15e0SLoGin #[inline(always)] 56940fe15e0SLoGin pub fn set_size(&mut self, size: usize) { 57040fe15e0SLoGin self.size = size; 57140fe15e0SLoGin } 57240fe15e0SLoGin 57340fe15e0SLoGin /// 判断虚拟地址范围是否为空 57440fe15e0SLoGin #[inline(always)] 57540fe15e0SLoGin pub fn is_empty(&self) -> bool { 57640fe15e0SLoGin self.size == 0 57740fe15e0SLoGin } 57840fe15e0SLoGin 57940fe15e0SLoGin /// 将虚拟地址区域的大小向上对齐到页大小 58040fe15e0SLoGin #[inline(always)] 58140fe15e0SLoGin pub fn round_up_size_to_page(self) -> Self { 58240fe15e0SLoGin return VirtRegion::new(self.start, round_up_to_page_size(self.size)); 58340fe15e0SLoGin } 58440fe15e0SLoGin 58540fe15e0SLoGin /// 判断两个虚拟地址范围是否由于具有交集而导致冲突 58640fe15e0SLoGin #[inline(always)] 58740fe15e0SLoGin pub fn collide(&self, other: &VirtRegion) -> bool { 58840fe15e0SLoGin return self.intersect(other).is_some(); 58940fe15e0SLoGin } 59040fe15e0SLoGin 59140fe15e0SLoGin pub fn iter_pages(&self) -> VirtPageFrameIter { 59240fe15e0SLoGin return VirtPageFrame::iter_range( 59340fe15e0SLoGin VirtPageFrame::new(self.start), 59440fe15e0SLoGin VirtPageFrame::new(self.end()), 59540fe15e0SLoGin ); 59640fe15e0SLoGin } 59740fe15e0SLoGin 59840fe15e0SLoGin /// 获取[self.start(), region.start())的虚拟地址范围 59940fe15e0SLoGin /// 60040fe15e0SLoGin /// 如果self.start() >= region.start(),返回None 60140fe15e0SLoGin pub fn before(self, region: &VirtRegion) -> Option<Self> { 60240fe15e0SLoGin return Self::between(self.start(), region.start()); 60340fe15e0SLoGin } 60440fe15e0SLoGin 60540fe15e0SLoGin /// 获取[region.end(),self.end())的虚拟地址范围 60640fe15e0SLoGin /// 60740fe15e0SLoGin /// 如果 self.end() >= region.end() ,返回None 60840fe15e0SLoGin pub fn after(self, region: &VirtRegion) -> Option<Self> { 60940fe15e0SLoGin // if self.end() > region.end() none 61040fe15e0SLoGin return Self::between(region.end(), self.end()); 61140fe15e0SLoGin } 61240fe15e0SLoGin 61340fe15e0SLoGin /// 把当前虚拟地址范围内的某个虚拟地址,转换为另一个虚拟地址范围内的虚拟地址 61440fe15e0SLoGin /// 61540fe15e0SLoGin /// 如果vaddr不在当前虚拟地址范围内,返回None 61640fe15e0SLoGin /// 61740fe15e0SLoGin /// 如果vaddr在当前虚拟地址范围内,返回vaddr在new_base中的虚拟地址 61840fe15e0SLoGin pub fn rebase(self, vaddr: VirtAddr, new_base: &VirtRegion) -> Option<VirtAddr> { 61940fe15e0SLoGin if !self.contains(vaddr) { 62040fe15e0SLoGin return None; 62140fe15e0SLoGin } 62240fe15e0SLoGin let offset = vaddr.data() - self.start().data(); 62340fe15e0SLoGin let new_start = new_base.start().data() + offset; 62440fe15e0SLoGin return Some(VirtAddr::new(new_start)); 62540fe15e0SLoGin } 62640fe15e0SLoGin 62740fe15e0SLoGin /// 判断虚拟地址范围是否包含指定的虚拟地址 62840fe15e0SLoGin pub fn contains(&self, addr: VirtAddr) -> bool { 62940fe15e0SLoGin return self.start() <= addr && addr < self.end(); 63040fe15e0SLoGin } 63140fe15e0SLoGin 63240fe15e0SLoGin /// 创建当前虚拟地址范围的页面迭代器 63340fe15e0SLoGin pub fn pages(&self) -> VirtPageFrameIter { 63440fe15e0SLoGin return VirtPageFrame::iter_range( 63540fe15e0SLoGin VirtPageFrame::new(self.start()), 63640fe15e0SLoGin VirtPageFrame::new(self.end()), 63740fe15e0SLoGin ); 63840fe15e0SLoGin } 63940fe15e0SLoGin } 64040fe15e0SLoGin 64140fe15e0SLoGin impl PartialOrd for VirtRegion { 64240fe15e0SLoGin fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { 64340fe15e0SLoGin return self.start.partial_cmp(&other.start); 64440fe15e0SLoGin } 64540fe15e0SLoGin } 64640fe15e0SLoGin 64740fe15e0SLoGin impl Ord for VirtRegion { 64840fe15e0SLoGin fn cmp(&self, other: &Self) -> cmp::Ordering { 64940fe15e0SLoGin return self.start.cmp(&other.start); 65040fe15e0SLoGin } 65140fe15e0SLoGin } 65240fe15e0SLoGin 65340fe15e0SLoGin /// ## 判断虚拟地址是否超出了用户空间 65440fe15e0SLoGin /// 65540fe15e0SLoGin /// 如果虚拟地址超出了用户空间,返回Err(SystemError::EFAULT). 65640fe15e0SLoGin /// 如果end < start,返回Err(SystemError::EOVERFLOW) 65740fe15e0SLoGin /// 65840fe15e0SLoGin /// 否则返回Ok(()) 65940fe15e0SLoGin pub fn verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError> { 66040fe15e0SLoGin let end = addr.add(size); 66140fe15e0SLoGin if unlikely(end.data() < addr.data()) { 66240fe15e0SLoGin return Err(SystemError::EOVERFLOW); 66340fe15e0SLoGin } 66440fe15e0SLoGin 66540fe15e0SLoGin if !addr.check_user() || !end.check_user() { 66640fe15e0SLoGin return Err(SystemError::EFAULT); 66740fe15e0SLoGin } 66840fe15e0SLoGin 66940fe15e0SLoGin return Ok(()); 67040fe15e0SLoGin } 671