140fe15e0SLoGin use alloc::sync::Arc;
2*cf7f801eSMemoryShore use page::EntryFlags;
391e9d4abSLoGin use system_error::SystemError;
440fe15e0SLoGin
5816ee5aeSLoGin use crate::arch::MMArch;
640fe15e0SLoGin
740fe15e0SLoGin use core::{
840fe15e0SLoGin cmp,
940fe15e0SLoGin fmt::Debug,
1040fe15e0SLoGin intrinsics::unlikely,
1140fe15e0SLoGin ops::{Add, AddAssign, Sub, SubAssign},
1240fe15e0SLoGin ptr,
1340fe15e0SLoGin sync::atomic::{AtomicBool, Ordering},
1440fe15e0SLoGin };
1540fe15e0SLoGin
1640fe15e0SLoGin use self::{
1740fe15e0SLoGin allocator::page_frame::{VirtPageFrame, VirtPageFrameIter},
1892849878SLoGin memblock::MemoryAreaAttr,
1940fe15e0SLoGin page::round_up_to_page_size,
20a17651b1SMemoryShore ucontext::{AddressSpace, LockedVMA, UserMapper},
2140fe15e0SLoGin };
22004e86ffSlogin
2382d2e446Slogin pub mod allocator;
2440fe15e0SLoGin pub mod c_adapter;
2574ffde66SLoGin pub mod early_ioremap;
26a17651b1SMemoryShore pub mod fault;
27453452ccSLoGin pub mod init;
2840fe15e0SLoGin pub mod kernel_mapper;
29a17651b1SMemoryShore pub mod madvise;
3045626c85SLoGin pub mod memblock;
31c2481452Shoumkh pub mod mmio_buddy;
3240fe15e0SLoGin pub mod no_init;
3340fe15e0SLoGin pub mod page;
34c3dad001SLoGin pub mod percpu;
35ab5c8ca4Slogin pub mod syscall;
3640fe15e0SLoGin pub mod ucontext;
3740fe15e0SLoGin
3840fe15e0SLoGin /// 内核INIT进程的用户地址空间结构体(仅在process_init中初始化)
398cb2e9b3SLoGin static mut __IDLE_PROCESS_ADDRESS_SPACE: Option<Arc<AddressSpace>> = None;
4040fe15e0SLoGin
414cfa009bSJomo bitflags! {
424cfa009bSJomo /// Virtual memory flags
43b5b571e0SLoGin #[allow(clippy::bad_bit_mask)]
44*cf7f801eSMemoryShore pub struct VmFlags:usize{
454cfa009bSJomo const VM_NONE = 0x00000000;
464cfa009bSJomo
474cfa009bSJomo const VM_READ = 0x00000001;
484cfa009bSJomo const VM_WRITE = 0x00000002;
494cfa009bSJomo const VM_EXEC = 0x00000004;
504cfa009bSJomo const VM_SHARED = 0x00000008;
514cfa009bSJomo
524cfa009bSJomo const VM_MAYREAD = 0x00000010;
534cfa009bSJomo const VM_MAYWRITE = 0x00000020;
544cfa009bSJomo const VM_MAYEXEC = 0x00000040;
554cfa009bSJomo const VM_MAYSHARE = 0x00000080;
564cfa009bSJomo
574cfa009bSJomo const VM_GROWSDOWN = 0x00000100;
584cfa009bSJomo const VM_UFFD_MISSING = 0x00000200;
594cfa009bSJomo const VM_PFNMAP = 0x00000400;
604cfa009bSJomo const VM_UFFD_WP = 0x00001000;
614cfa009bSJomo
624cfa009bSJomo const VM_LOCKED = 0x00002000;
634cfa009bSJomo const VM_IO = 0x00004000;
644cfa009bSJomo
654cfa009bSJomo const VM_SEQ_READ = 0x00008000;
664cfa009bSJomo const VM_RAND_READ = 0x00010000;
674cfa009bSJomo
684cfa009bSJomo const VM_DONTCOPY = 0x00020000;
694cfa009bSJomo const VM_DONTEXPAND = 0x00040000;
704cfa009bSJomo const VM_LOCKONFAULT = 0x00080000;
714cfa009bSJomo const VM_ACCOUNT = 0x00100000;
724cfa009bSJomo const VM_NORESERVE = 0x00200000;
734cfa009bSJomo const VM_HUGETLB = 0x00400000;
744cfa009bSJomo const VM_SYNC = 0x00800000;
754cfa009bSJomo const VM_ARCH_1 = 0x01000000;
764cfa009bSJomo const VM_WIPEONFORK = 0x02000000;
774cfa009bSJomo const VM_DONTDUMP = 0x04000000;
784cfa009bSJomo }
79a17651b1SMemoryShore
80a17651b1SMemoryShore /// 描述页面错误处理过程中发生的不同情况或结果
81a17651b1SMemoryShore pub struct VmFaultReason:u32 {
82a17651b1SMemoryShore const VM_FAULT_OOM = 0x000001;
83a17651b1SMemoryShore const VM_FAULT_SIGBUS = 0x000002;
84a17651b1SMemoryShore const VM_FAULT_MAJOR = 0x000004;
85a17651b1SMemoryShore const VM_FAULT_WRITE = 0x000008;
86a17651b1SMemoryShore const VM_FAULT_HWPOISON = 0x000010;
87a17651b1SMemoryShore const VM_FAULT_HWPOISON_LARGE = 0x000020;
88a17651b1SMemoryShore const VM_FAULT_SIGSEGV = 0x000040;
89a17651b1SMemoryShore const VM_FAULT_NOPAGE = 0x000100;
90a17651b1SMemoryShore const VM_FAULT_LOCKED = 0x000200;
91a17651b1SMemoryShore const VM_FAULT_RETRY = 0x000400;
92a17651b1SMemoryShore const VM_FAULT_FALLBACK = 0x000800;
93a17651b1SMemoryShore const VM_FAULT_DONE_COW = 0x001000;
94a17651b1SMemoryShore const VM_FAULT_NEEDDSYNC = 0x002000;
95a17651b1SMemoryShore const VM_FAULT_COMPLETED = 0x004000;
96a17651b1SMemoryShore const VM_FAULT_HINDEX_MASK = 0x0f0000;
97*cf7f801eSMemoryShore const VM_FAULT_ERROR = 0x000001 | 0x000002 | 0x000040 | 0x000010 | 0x000020 | 0x000800;
98*cf7f801eSMemoryShore }
99*cf7f801eSMemoryShore
100*cf7f801eSMemoryShore pub struct MsFlags:usize {
101*cf7f801eSMemoryShore const MS_ASYNC = 1;
102*cf7f801eSMemoryShore const MS_INVALIDATE = 2;
103*cf7f801eSMemoryShore const MS_SYNC = 4;
104*cf7f801eSMemoryShore }
105*cf7f801eSMemoryShore }
106*cf7f801eSMemoryShore
107*cf7f801eSMemoryShore impl core::ops::Index<VmFlags> for [usize] {
108*cf7f801eSMemoryShore type Output = usize;
109*cf7f801eSMemoryShore
index(&self, index: VmFlags) -> &Self::Output110*cf7f801eSMemoryShore fn index(&self, index: VmFlags) -> &Self::Output {
111*cf7f801eSMemoryShore &self[index.bits]
112*cf7f801eSMemoryShore }
113*cf7f801eSMemoryShore }
114*cf7f801eSMemoryShore
115*cf7f801eSMemoryShore impl core::ops::IndexMut<VmFlags> for [usize] {
index_mut(&mut self, index: VmFlags) -> &mut Self::Output116*cf7f801eSMemoryShore fn index_mut(&mut self, index: VmFlags) -> &mut Self::Output {
117*cf7f801eSMemoryShore &mut self[index.bits]
118a17651b1SMemoryShore }
1194cfa009bSJomo }
1204cfa009bSJomo
1218cb2e9b3SLoGin /// 获取内核IDLE进程的用户地址空间结构体
12240fe15e0SLoGin #[allow(non_snake_case)]
12340fe15e0SLoGin #[inline(always)]
IDLE_PROCESS_ADDRESS_SPACE() -> Arc<AddressSpace>1248cb2e9b3SLoGin pub fn IDLE_PROCESS_ADDRESS_SPACE() -> Arc<AddressSpace> {
12540fe15e0SLoGin unsafe {
1268cb2e9b3SLoGin return __IDLE_PROCESS_ADDRESS_SPACE
12740fe15e0SLoGin .as_ref()
1288cb2e9b3SLoGin .expect("IDLE_PROCESS_ADDRESS_SPACE is null")
12940fe15e0SLoGin .clone();
13040fe15e0SLoGin }
13140fe15e0SLoGin }
13240fe15e0SLoGin
1338cb2e9b3SLoGin /// 设置内核IDLE进程的用户地址空间结构体全局变量
13440fe15e0SLoGin #[allow(non_snake_case)]
set_IDLE_PROCESS_ADDRESS_SPACE(address_space: Arc<AddressSpace>)1358cb2e9b3SLoGin pub unsafe fn set_IDLE_PROCESS_ADDRESS_SPACE(address_space: Arc<AddressSpace>) {
13640fe15e0SLoGin static INITIALIZED: AtomicBool = AtomicBool::new(false);
13740fe15e0SLoGin if INITIALIZED
13840fe15e0SLoGin .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire)
13940fe15e0SLoGin .is_err()
14040fe15e0SLoGin {
1418cb2e9b3SLoGin panic!("IDLE_PROCESS_ADDRESS_SPACE is already initialized");
14240fe15e0SLoGin }
1438cb2e9b3SLoGin __IDLE_PROCESS_ADDRESS_SPACE = Some(address_space);
14440fe15e0SLoGin }
145004e86ffSlogin
14640fe15e0SLoGin #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)]
14740fe15e0SLoGin pub enum PageTableKind {
14840fe15e0SLoGin /// 用户可访问的页表
14940fe15e0SLoGin User,
15040fe15e0SLoGin /// 内核页表
15140fe15e0SLoGin Kernel,
1527a29d4fcSLoGin /// x86内存虚拟化中使用的EPT
1537a29d4fcSLoGin #[cfg(target_arch = "x86_64")]
15440314b30SXiaoye Zheng EPT,
15540fe15e0SLoGin }
15640fe15e0SLoGin
15740fe15e0SLoGin /// 物理内存地址
15840fe15e0SLoGin #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
15940fe15e0SLoGin #[repr(transparent)]
16040fe15e0SLoGin pub struct PhysAddr(usize);
16140fe15e0SLoGin
16240fe15e0SLoGin impl PhysAddr {
16392849878SLoGin /// 最大物理地址
16492849878SLoGin pub const MAX: Self = PhysAddr(usize::MAX);
16592849878SLoGin
16640fe15e0SLoGin #[inline(always)]
new(address: usize) -> Self16740fe15e0SLoGin pub const fn new(address: usize) -> Self {
16840fe15e0SLoGin Self(address)
16940fe15e0SLoGin }
17040fe15e0SLoGin
17140fe15e0SLoGin /// @brief 获取物理地址的值
17240fe15e0SLoGin #[inline(always)]
data(&self) -> usize17374ffde66SLoGin pub const fn data(&self) -> usize {
17440fe15e0SLoGin self.0
17540fe15e0SLoGin }
17640fe15e0SLoGin
17740fe15e0SLoGin /// @brief 将物理地址加上一个偏移量
17840fe15e0SLoGin #[inline(always)]
add(self, offset: usize) -> Self17940fe15e0SLoGin pub fn add(self, offset: usize) -> Self {
18040fe15e0SLoGin Self(self.0 + offset)
18140fe15e0SLoGin }
18240fe15e0SLoGin
18340fe15e0SLoGin /// @brief 判断物理地址是否按照指定要求对齐
18440fe15e0SLoGin #[inline(always)]
check_aligned(&self, align: usize) -> bool18540fe15e0SLoGin pub fn check_aligned(&self, align: usize) -> bool {
18640fe15e0SLoGin return self.0 & (align - 1) == 0;
18740fe15e0SLoGin }
18840fe15e0SLoGin
18940fe15e0SLoGin #[inline(always)]
is_null(&self) -> bool19040fe15e0SLoGin pub fn is_null(&self) -> bool {
19140fe15e0SLoGin return self.0 == 0;
19240fe15e0SLoGin }
19340fe15e0SLoGin }
19440fe15e0SLoGin
19540fe15e0SLoGin impl Debug for PhysAddr {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result19640fe15e0SLoGin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
19740fe15e0SLoGin write!(f, "PhysAddr({:#x})", self.0)
19840fe15e0SLoGin }
19940fe15e0SLoGin }
20040fe15e0SLoGin
20140fe15e0SLoGin impl core::ops::Add<usize> for PhysAddr {
20240fe15e0SLoGin type Output = Self;
20340fe15e0SLoGin
20440fe15e0SLoGin #[inline(always)]
add(self, rhs: usize) -> Self::Output20540fe15e0SLoGin fn add(self, rhs: usize) -> Self::Output {
20640fe15e0SLoGin return Self(self.0 + rhs);
20740fe15e0SLoGin }
20840fe15e0SLoGin }
20940fe15e0SLoGin
21040fe15e0SLoGin impl core::ops::AddAssign<usize> for PhysAddr {
21140fe15e0SLoGin #[inline(always)]
add_assign(&mut self, rhs: usize)21240fe15e0SLoGin fn add_assign(&mut self, rhs: usize) {
21340fe15e0SLoGin self.0 += rhs;
21440fe15e0SLoGin }
21540fe15e0SLoGin }
21640fe15e0SLoGin
21740fe15e0SLoGin impl core::ops::Add<PhysAddr> for PhysAddr {
21840fe15e0SLoGin type Output = Self;
21940fe15e0SLoGin
22040fe15e0SLoGin #[inline(always)]
add(self, rhs: PhysAddr) -> Self::Output22140fe15e0SLoGin fn add(self, rhs: PhysAddr) -> Self::Output {
22240fe15e0SLoGin return Self(self.0 + rhs.0);
22340fe15e0SLoGin }
22440fe15e0SLoGin }
22540fe15e0SLoGin
22640fe15e0SLoGin impl core::ops::AddAssign<PhysAddr> for PhysAddr {
22740fe15e0SLoGin #[inline(always)]
add_assign(&mut self, rhs: PhysAddr)22840fe15e0SLoGin fn add_assign(&mut self, rhs: PhysAddr) {
22940fe15e0SLoGin self.0 += rhs.0;
23040fe15e0SLoGin }
23140fe15e0SLoGin }
23240fe15e0SLoGin
2332dd9f0c7SLoGin impl core::ops::BitOrAssign<usize> for PhysAddr {
2342dd9f0c7SLoGin #[inline(always)]
bitor_assign(&mut self, rhs: usize)2352dd9f0c7SLoGin fn bitor_assign(&mut self, rhs: usize) {
2362dd9f0c7SLoGin self.0 |= rhs;
2372dd9f0c7SLoGin }
2382dd9f0c7SLoGin }
2392dd9f0c7SLoGin
2402dd9f0c7SLoGin impl core::ops::BitOrAssign<PhysAddr> for PhysAddr {
2412dd9f0c7SLoGin #[inline(always)]
bitor_assign(&mut self, rhs: PhysAddr)2422dd9f0c7SLoGin fn bitor_assign(&mut self, rhs: PhysAddr) {
2432dd9f0c7SLoGin self.0 |= rhs.0;
2442dd9f0c7SLoGin }
2452dd9f0c7SLoGin }
2462dd9f0c7SLoGin
24740fe15e0SLoGin impl core::ops::Sub<usize> for PhysAddr {
24840fe15e0SLoGin type Output = Self;
24940fe15e0SLoGin
25040fe15e0SLoGin #[inline(always)]
sub(self, rhs: usize) -> Self::Output25140fe15e0SLoGin fn sub(self, rhs: usize) -> Self::Output {
25240fe15e0SLoGin return Self(self.0 - rhs);
25340fe15e0SLoGin }
25440fe15e0SLoGin }
25540fe15e0SLoGin
25640fe15e0SLoGin impl core::ops::SubAssign<usize> for PhysAddr {
25740fe15e0SLoGin #[inline(always)]
sub_assign(&mut self, rhs: usize)25840fe15e0SLoGin fn sub_assign(&mut self, rhs: usize) {
25940fe15e0SLoGin self.0 -= rhs;
26040fe15e0SLoGin }
26140fe15e0SLoGin }
26240fe15e0SLoGin
26340fe15e0SLoGin impl core::ops::Sub<PhysAddr> for PhysAddr {
26440fe15e0SLoGin type Output = usize;
26540fe15e0SLoGin
26640fe15e0SLoGin #[inline(always)]
sub(self, rhs: PhysAddr) -> Self::Output26740fe15e0SLoGin fn sub(self, rhs: PhysAddr) -> Self::Output {
26840fe15e0SLoGin return self.0 - rhs.0;
26940fe15e0SLoGin }
27040fe15e0SLoGin }
27140fe15e0SLoGin
27240fe15e0SLoGin impl core::ops::SubAssign<PhysAddr> for PhysAddr {
27340fe15e0SLoGin #[inline(always)]
sub_assign(&mut self, rhs: PhysAddr)27440fe15e0SLoGin fn sub_assign(&mut self, rhs: PhysAddr) {
27540fe15e0SLoGin self.0 -= rhs.0;
27640fe15e0SLoGin }
27740fe15e0SLoGin }
27840fe15e0SLoGin
27940fe15e0SLoGin /// 虚拟内存地址
28040fe15e0SLoGin #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)]
28140fe15e0SLoGin #[repr(transparent)]
28240fe15e0SLoGin pub struct VirtAddr(usize);
28340fe15e0SLoGin
28440fe15e0SLoGin impl VirtAddr {
28540fe15e0SLoGin #[inline(always)]
new(address: usize) -> Self28640fe15e0SLoGin pub const fn new(address: usize) -> Self {
28740fe15e0SLoGin return Self(address);
28840fe15e0SLoGin }
28940fe15e0SLoGin
29040fe15e0SLoGin /// @brief 获取虚拟地址的值
29140fe15e0SLoGin #[inline(always)]
data(&self) -> usize29274ffde66SLoGin pub const fn data(&self) -> usize {
29340fe15e0SLoGin return self.0;
29440fe15e0SLoGin }
29540fe15e0SLoGin
29640fe15e0SLoGin /// @brief 判断虚拟地址的类型
29740fe15e0SLoGin #[inline(always)]
kind(&self) -> PageTableKind29840fe15e0SLoGin pub fn kind(&self) -> PageTableKind {
29940fe15e0SLoGin if self.check_user() {
30040fe15e0SLoGin return PageTableKind::User;
30140fe15e0SLoGin } else {
30240fe15e0SLoGin return PageTableKind::Kernel;
30340fe15e0SLoGin }
30440fe15e0SLoGin }
30540fe15e0SLoGin
30640fe15e0SLoGin /// @brief 判断虚拟地址是否按照指定要求对齐
30740fe15e0SLoGin #[inline(always)]
check_aligned(&self, align: usize) -> bool30840fe15e0SLoGin pub fn check_aligned(&self, align: usize) -> bool {
30940fe15e0SLoGin return self.0 & (align - 1) == 0;
31040fe15e0SLoGin }
31140fe15e0SLoGin
31240fe15e0SLoGin /// @brief 判断虚拟地址是否在用户空间
31340fe15e0SLoGin #[inline(always)]
check_user(&self) -> bool31440fe15e0SLoGin pub fn check_user(&self) -> bool {
315b5b571e0SLoGin return self < &MMArch::USER_END_VADDR;
31640fe15e0SLoGin }
31740fe15e0SLoGin
31840fe15e0SLoGin #[inline(always)]
as_ptr<T>(self) -> *mut T31940fe15e0SLoGin pub fn as_ptr<T>(self) -> *mut T {
32040fe15e0SLoGin return self.0 as *mut T;
32140fe15e0SLoGin }
32240fe15e0SLoGin
32340fe15e0SLoGin #[inline(always)]
is_null(&self) -> bool32440fe15e0SLoGin pub fn is_null(&self) -> bool {
32540fe15e0SLoGin return self.0 == 0;
32640fe15e0SLoGin }
32740fe15e0SLoGin }
32840fe15e0SLoGin
32940fe15e0SLoGin impl Add<VirtAddr> for VirtAddr {
33040fe15e0SLoGin type Output = Self;
33140fe15e0SLoGin
33240fe15e0SLoGin #[inline(always)]
add(self, rhs: VirtAddr) -> Self::Output33340fe15e0SLoGin fn add(self, rhs: VirtAddr) -> Self::Output {
33440fe15e0SLoGin return Self(self.0 + rhs.0);
33540fe15e0SLoGin }
33640fe15e0SLoGin }
33740fe15e0SLoGin
33840fe15e0SLoGin impl Add<usize> for VirtAddr {
33940fe15e0SLoGin type Output = Self;
34040fe15e0SLoGin
34140fe15e0SLoGin #[inline(always)]
add(self, rhs: usize) -> Self::Output34240fe15e0SLoGin fn add(self, rhs: usize) -> Self::Output {
34340fe15e0SLoGin return Self(self.0 + rhs);
34440fe15e0SLoGin }
34540fe15e0SLoGin }
34640fe15e0SLoGin
34740fe15e0SLoGin impl Sub<VirtAddr> for VirtAddr {
34840fe15e0SLoGin type Output = usize;
34940fe15e0SLoGin
35040fe15e0SLoGin #[inline(always)]
sub(self, rhs: VirtAddr) -> Self::Output35140fe15e0SLoGin fn sub(self, rhs: VirtAddr) -> Self::Output {
35240fe15e0SLoGin return self.0 - rhs.0;
35340fe15e0SLoGin }
35440fe15e0SLoGin }
35540fe15e0SLoGin
35640fe15e0SLoGin impl Sub<usize> for VirtAddr {
35740fe15e0SLoGin type Output = Self;
35840fe15e0SLoGin
35940fe15e0SLoGin #[inline(always)]
sub(self, rhs: usize) -> Self::Output36040fe15e0SLoGin fn sub(self, rhs: usize) -> Self::Output {
36140fe15e0SLoGin return Self(self.0 - rhs);
36240fe15e0SLoGin }
36340fe15e0SLoGin }
36440fe15e0SLoGin
36540fe15e0SLoGin impl AddAssign<usize> for VirtAddr {
36640fe15e0SLoGin #[inline(always)]
add_assign(&mut self, rhs: usize)36740fe15e0SLoGin fn add_assign(&mut self, rhs: usize) {
36840fe15e0SLoGin self.0 += rhs;
36940fe15e0SLoGin }
37040fe15e0SLoGin }
37140fe15e0SLoGin
37240fe15e0SLoGin impl AddAssign<VirtAddr> for VirtAddr {
37340fe15e0SLoGin #[inline(always)]
add_assign(&mut self, rhs: VirtAddr)37440fe15e0SLoGin fn add_assign(&mut self, rhs: VirtAddr) {
37540fe15e0SLoGin self.0 += rhs.0;
37640fe15e0SLoGin }
37740fe15e0SLoGin }
37840fe15e0SLoGin
37940fe15e0SLoGin impl SubAssign<usize> for VirtAddr {
38040fe15e0SLoGin #[inline(always)]
sub_assign(&mut self, rhs: usize)38140fe15e0SLoGin fn sub_assign(&mut self, rhs: usize) {
38240fe15e0SLoGin self.0 -= rhs;
38340fe15e0SLoGin }
38440fe15e0SLoGin }
38540fe15e0SLoGin
38640fe15e0SLoGin impl SubAssign<VirtAddr> for VirtAddr {
38740fe15e0SLoGin #[inline(always)]
sub_assign(&mut self, rhs: VirtAddr)38840fe15e0SLoGin fn sub_assign(&mut self, rhs: VirtAddr) {
38940fe15e0SLoGin self.0 -= rhs.0;
39040fe15e0SLoGin }
39140fe15e0SLoGin }
39240fe15e0SLoGin
39340fe15e0SLoGin impl Debug for VirtAddr {
fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result39440fe15e0SLoGin fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
39540fe15e0SLoGin write!(f, "VirtAddr({:#x})", self.0)
39640fe15e0SLoGin }
39740fe15e0SLoGin }
39840fe15e0SLoGin
39940fe15e0SLoGin /// @brief 物理内存区域
40040fe15e0SLoGin #[derive(Clone, Copy, Debug)]
40140fe15e0SLoGin pub struct PhysMemoryArea {
40240fe15e0SLoGin /// 物理基地址
40340fe15e0SLoGin pub base: PhysAddr,
40440fe15e0SLoGin /// 该区域的物理内存大小
40540fe15e0SLoGin pub size: usize,
40692849878SLoGin
40792849878SLoGin pub flags: MemoryAreaAttr,
40840fe15e0SLoGin }
40940fe15e0SLoGin
41099dbf38dSLoGin impl PhysMemoryArea {
41145626c85SLoGin pub const DEFAULT: Self = Self {
41245626c85SLoGin base: PhysAddr::new(0),
41345626c85SLoGin size: 0,
41492849878SLoGin flags: MemoryAreaAttr::empty(),
41545626c85SLoGin };
41645626c85SLoGin
new(base: PhysAddr, size: usize, flags: MemoryAreaAttr) -> Self41792849878SLoGin pub fn new(base: PhysAddr, size: usize, flags: MemoryAreaAttr) -> Self {
41892849878SLoGin Self { base, size, flags }
41999dbf38dSLoGin }
42099dbf38dSLoGin
42199dbf38dSLoGin /// 返回向上页面对齐的区域起始物理地址
area_base_aligned(&self) -> PhysAddr42299dbf38dSLoGin pub fn area_base_aligned(&self) -> PhysAddr {
42399dbf38dSLoGin return PhysAddr::new(
42499dbf38dSLoGin (self.base.data() + (MMArch::PAGE_SIZE - 1)) & !(MMArch::PAGE_SIZE - 1),
42599dbf38dSLoGin );
42699dbf38dSLoGin }
42799dbf38dSLoGin
42899dbf38dSLoGin /// 返回向下页面对齐的区域截止物理地址
area_end_aligned(&self) -> PhysAddr42999dbf38dSLoGin pub fn area_end_aligned(&self) -> PhysAddr {
43099dbf38dSLoGin return PhysAddr::new((self.base.data() + self.size) & !(MMArch::PAGE_SIZE - 1));
43199dbf38dSLoGin }
43299dbf38dSLoGin }
43399dbf38dSLoGin
43499dbf38dSLoGin impl Default for PhysMemoryArea {
default() -> Self43599dbf38dSLoGin fn default() -> Self {
43645626c85SLoGin return Self::DEFAULT;
43799dbf38dSLoGin }
43899dbf38dSLoGin }
43999dbf38dSLoGin
440bd70d2d1SLoGin #[allow(dead_code)]
44140fe15e0SLoGin pub trait MemoryManagementArch: Clone + Copy + Debug {
442a17651b1SMemoryShore /// 是否支持缺页中断
443a17651b1SMemoryShore const PAGE_FAULT_ENABLED: bool;
44440fe15e0SLoGin /// 页面大小的shift(假如页面4K,那么这个值就是12,因为2^12=4096)
44540fe15e0SLoGin const PAGE_SHIFT: usize;
44640fe15e0SLoGin /// 每个页表的页表项数目。(以2^n次幂来表示)假如有512个页表项,那么这个值就是9
44740fe15e0SLoGin const PAGE_ENTRY_SHIFT: usize;
44840fe15e0SLoGin /// 页表层级数量
44940fe15e0SLoGin const PAGE_LEVELS: usize;
45040fe15e0SLoGin
45140fe15e0SLoGin /// 页表项的有效位的index(假如页表项的第0-51位有效,那么这个值就是52)
45240fe15e0SLoGin const ENTRY_ADDRESS_SHIFT: usize;
45340fe15e0SLoGin /// 页面的页表项的默认值
45440fe15e0SLoGin const ENTRY_FLAG_DEFAULT_PAGE: usize;
45540fe15e0SLoGin /// 页表的页表项的默认值
45640fe15e0SLoGin const ENTRY_FLAG_DEFAULT_TABLE: usize;
45740fe15e0SLoGin /// 页表项的present位被置位之后的值
45840fe15e0SLoGin const ENTRY_FLAG_PRESENT: usize;
45940fe15e0SLoGin /// 页表项为read only时的值
46040fe15e0SLoGin const ENTRY_FLAG_READONLY: usize;
461471d65cfSLoGin /// 页表项的write bit
462471d65cfSLoGin const ENTRY_FLAG_WRITEABLE: usize;
46340fe15e0SLoGin /// 页表项为可读写状态的值
46440fe15e0SLoGin const ENTRY_FLAG_READWRITE: usize;
46540fe15e0SLoGin /// 页面项标记页面为user page的值
46640fe15e0SLoGin const ENTRY_FLAG_USER: usize;
46740fe15e0SLoGin /// 页面项标记页面为write through的值
46840fe15e0SLoGin const ENTRY_FLAG_WRITE_THROUGH: usize;
46940fe15e0SLoGin /// 页面项标记页面为cache disable的值
47040fe15e0SLoGin const ENTRY_FLAG_CACHE_DISABLE: usize;
47140fe15e0SLoGin /// 标记当前页面不可执行的标志位(Execute disable)(也就是说,不能从这段内存里面获取处理器指令)
47240fe15e0SLoGin const ENTRY_FLAG_NO_EXEC: usize;
47340fe15e0SLoGin /// 标记当前页面可执行的标志位(Execute enable)
47440fe15e0SLoGin const ENTRY_FLAG_EXEC: usize;
47592849878SLoGin /// 当该位为1时,标明这是一个脏页
47692849878SLoGin const ENTRY_FLAG_DIRTY: usize;
47792849878SLoGin /// 当该位为1时,代表这个页面被处理器访问过
47892849878SLoGin const ENTRY_FLAG_ACCESSED: usize;
479a17651b1SMemoryShore /// 标记该页表项指向的页是否为大页
480a17651b1SMemoryShore const ENTRY_FLAG_HUGE_PAGE: usize;
481a17651b1SMemoryShore /// 当该位为1时,代表该页表项是全局的
482a17651b1SMemoryShore const ENTRY_FLAG_GLOBAL: usize;
48340fe15e0SLoGin
48440fe15e0SLoGin /// 虚拟地址与物理地址的偏移量
48540fe15e0SLoGin const PHYS_OFFSET: usize;
48640fe15e0SLoGin
487453452ccSLoGin /// 内核在链接时被链接到的偏移量
488453452ccSLoGin const KERNEL_LINK_OFFSET: usize;
489453452ccSLoGin
490453452ccSLoGin const KERNEL_VIRT_START: usize = Self::PHYS_OFFSET + Self::KERNEL_LINK_OFFSET;
491453452ccSLoGin
49240fe15e0SLoGin /// 每个页面的大小
49340fe15e0SLoGin const PAGE_SIZE: usize = 1 << Self::PAGE_SHIFT;
49440fe15e0SLoGin /// 通过这个mask,获取地址的页内偏移量
49540fe15e0SLoGin const PAGE_OFFSET_MASK: usize = Self::PAGE_SIZE - 1;
49640314b30SXiaoye Zheng /// 通过这个mask,获取页的首地址
49740314b30SXiaoye Zheng const PAGE_MASK: usize = !(Self::PAGE_OFFSET_MASK);
49840fe15e0SLoGin /// 页表项的地址、数据部分的shift。
49940fe15e0SLoGin /// 打个比方,如果这个值为52,那么意味着页表项的[0, 52)位,用于表示地址以及其他的标志位
50040fe15e0SLoGin const PAGE_ADDRESS_SHIFT: usize = Self::PAGE_LEVELS * Self::PAGE_ENTRY_SHIFT + Self::PAGE_SHIFT;
50140fe15e0SLoGin /// 最大的虚拟地址(对于不同的架构,由于上述PAGE_ADDRESS_SHIFT可能包括了reserved bits, 事实上能表示的虚拟地址应该比这个值要小)
50240fe15e0SLoGin const PAGE_ADDRESS_SIZE: usize = 1 << Self::PAGE_ADDRESS_SHIFT;
50340fe15e0SLoGin /// 页表项的值与这个常量进行与运算,得到的结果是所填写的物理地址
50440fe15e0SLoGin const PAGE_ADDRESS_MASK: usize = Self::PAGE_ADDRESS_SIZE - Self::PAGE_SIZE;
50540fe15e0SLoGin /// 每个页表项的大小
50640fe15e0SLoGin const PAGE_ENTRY_SIZE: usize = 1 << (Self::PAGE_SHIFT - Self::PAGE_ENTRY_SHIFT);
50740fe15e0SLoGin /// 每个页表的页表项数目
50840fe15e0SLoGin const PAGE_ENTRY_NUM: usize = 1 << Self::PAGE_ENTRY_SHIFT;
50940fe15e0SLoGin /// 该字段用于根据虚拟地址,获取该虚拟地址在对应的页表中是第几个页表项
51040fe15e0SLoGin const PAGE_ENTRY_MASK: usize = Self::PAGE_ENTRY_NUM - 1;
511a17651b1SMemoryShore /// 内核页表在顶级页表的第一个页表项的索引
512a17651b1SMemoryShore const PAGE_KERNEL_INDEX: usize = (Self::PHYS_OFFSET & Self::PAGE_ADDRESS_MASK)
513a17651b1SMemoryShore >> (Self::PAGE_ADDRESS_SHIFT - Self::PAGE_ENTRY_SHIFT);
51440fe15e0SLoGin
51540fe15e0SLoGin const PAGE_NEGATIVE_MASK: usize = !((Self::PAGE_ADDRESS_SIZE) - 1);
51640fe15e0SLoGin
51740fe15e0SLoGin const ENTRY_ADDRESS_SIZE: usize = 1 << Self::ENTRY_ADDRESS_SHIFT;
51840fe15e0SLoGin /// 该mask用于获取页表项中地址字段
51940fe15e0SLoGin const ENTRY_ADDRESS_MASK: usize = Self::ENTRY_ADDRESS_SIZE - Self::PAGE_SIZE;
52040fe15e0SLoGin /// 这个mask用于获取页表项中的flags
52140fe15e0SLoGin const ENTRY_FLAGS_MASK: usize = !Self::ENTRY_ADDRESS_MASK;
52240fe15e0SLoGin
52340fe15e0SLoGin /// 用户空间的最高地址
52440fe15e0SLoGin const USER_END_VADDR: VirtAddr;
52540fe15e0SLoGin /// 用户堆的起始地址
52640fe15e0SLoGin const USER_BRK_START: VirtAddr;
52740fe15e0SLoGin /// 用户栈起始地址(向下生长,不包含该值)
52840fe15e0SLoGin const USER_STACK_START: VirtAddr;
52940fe15e0SLoGin
53074ffde66SLoGin /// 内核的固定映射区的起始地址
53174ffde66SLoGin const FIXMAP_START_VADDR: VirtAddr;
53274ffde66SLoGin /// 内核的固定映射区的大小
53374ffde66SLoGin const FIXMAP_SIZE: usize;
53474ffde66SLoGin /// 内核的固定映射区的结束地址
53574ffde66SLoGin const FIXMAP_END_VADDR: VirtAddr =
53674ffde66SLoGin VirtAddr::new(Self::FIXMAP_START_VADDR.data() + Self::FIXMAP_SIZE);
53774ffde66SLoGin
53823ef2b33SLoGin /// MMIO虚拟空间的基地址
53923ef2b33SLoGin const MMIO_BASE: VirtAddr;
54023ef2b33SLoGin /// MMIO虚拟空间的大小
54123ef2b33SLoGin const MMIO_SIZE: usize;
54223ef2b33SLoGin /// MMIO虚拟空间的顶端地址(不包含)
54323ef2b33SLoGin const MMIO_TOP: VirtAddr = VirtAddr::new(Self::MMIO_BASE.data() + Self::MMIO_SIZE);
54423ef2b33SLoGin
54540fe15e0SLoGin /// @brief 用于初始化内存管理模块与架构相关的信息。
54645626c85SLoGin /// 该函数应调用其他模块的接口,把可用内存区域添加到memblock,提供给BumpAllocator使用
init()54745626c85SLoGin unsafe fn init();
54840fe15e0SLoGin
54923ef2b33SLoGin /// 内存管理初始化完成后,调用该函数
arch_post_init()55023ef2b33SLoGin unsafe fn arch_post_init() {}
55123ef2b33SLoGin
55240fe15e0SLoGin /// @brief 读取指定虚拟地址的值,并假设它是类型T的指针
55340fe15e0SLoGin #[inline(always)]
read<T>(address: VirtAddr) -> T55440fe15e0SLoGin unsafe fn read<T>(address: VirtAddr) -> T {
55540fe15e0SLoGin return ptr::read(address.data() as *const T);
55640fe15e0SLoGin }
55740fe15e0SLoGin
55840fe15e0SLoGin /// @brief 将value写入到指定的虚拟地址
55940fe15e0SLoGin #[inline(always)]
write<T>(address: VirtAddr, value: T)56040fe15e0SLoGin unsafe fn write<T>(address: VirtAddr, value: T) {
56140fe15e0SLoGin ptr::write(address.data() as *mut T, value);
56240fe15e0SLoGin }
56340fe15e0SLoGin
56440fe15e0SLoGin #[inline(always)]
write_bytes(address: VirtAddr, value: u8, count: usize)56540fe15e0SLoGin unsafe fn write_bytes(address: VirtAddr, value: u8, count: usize) {
56640fe15e0SLoGin ptr::write_bytes(address.data() as *mut u8, value, count);
56740fe15e0SLoGin }
56840fe15e0SLoGin
56940fe15e0SLoGin /// @brief 刷新TLB中,关于指定虚拟地址的条目
invalidate_page(address: VirtAddr)57040fe15e0SLoGin unsafe fn invalidate_page(address: VirtAddr);
57140fe15e0SLoGin
57240fe15e0SLoGin /// @brief 刷新TLB中,所有的条目
invalidate_all()57340fe15e0SLoGin unsafe fn invalidate_all();
57440fe15e0SLoGin
57540fe15e0SLoGin /// @brief 获取顶级页表的物理地址
table(table_kind: PageTableKind) -> PhysAddr57640fe15e0SLoGin unsafe fn table(table_kind: PageTableKind) -> PhysAddr;
57740fe15e0SLoGin
57840fe15e0SLoGin /// @brief 设置顶级页表的物理地址到处理器中
set_table(table_kind: PageTableKind, table: PhysAddr)57940fe15e0SLoGin unsafe fn set_table(table_kind: PageTableKind, table: PhysAddr);
58040fe15e0SLoGin
58140fe15e0SLoGin /// @brief 将物理地址转换为虚拟地址.
58240fe15e0SLoGin ///
58340fe15e0SLoGin /// @param phys 物理地址
58440fe15e0SLoGin ///
58540fe15e0SLoGin /// @return 转换后的虚拟地址。如果转换失败,返回None
58640fe15e0SLoGin #[inline(always)]
phys_2_virt(phys: PhysAddr) -> Option<VirtAddr>58740fe15e0SLoGin unsafe fn phys_2_virt(phys: PhysAddr) -> Option<VirtAddr> {
58840fe15e0SLoGin if let Some(vaddr) = phys.data().checked_add(Self::PHYS_OFFSET) {
58940fe15e0SLoGin return Some(VirtAddr::new(vaddr));
59040fe15e0SLoGin } else {
59140fe15e0SLoGin return None;
59240fe15e0SLoGin }
59340fe15e0SLoGin }
59440fe15e0SLoGin
59540fe15e0SLoGin /// 将虚拟地址转换为物理地址
59640fe15e0SLoGin ///
59740fe15e0SLoGin /// ## 参数
59840fe15e0SLoGin ///
59940fe15e0SLoGin /// - `virt` 虚拟地址
60040fe15e0SLoGin ///
60140fe15e0SLoGin /// ## 返回值
60240fe15e0SLoGin ///
60340fe15e0SLoGin /// 转换后的物理地址。如果转换失败,返回None
60440fe15e0SLoGin #[inline(always)]
virt_2_phys(virt: VirtAddr) -> Option<PhysAddr>60540fe15e0SLoGin unsafe fn virt_2_phys(virt: VirtAddr) -> Option<PhysAddr> {
60640fe15e0SLoGin if let Some(paddr) = virt.data().checked_sub(Self::PHYS_OFFSET) {
60740fe15e0SLoGin return Some(PhysAddr::new(paddr));
60840fe15e0SLoGin } else {
60940fe15e0SLoGin return None;
61040fe15e0SLoGin }
61140fe15e0SLoGin }
61240fe15e0SLoGin
61340fe15e0SLoGin /// @brief 判断指定的虚拟地址是否正确(符合规范)
virt_is_valid(virt: VirtAddr) -> bool61440fe15e0SLoGin fn virt_is_valid(virt: VirtAddr) -> bool;
61540fe15e0SLoGin
61640fe15e0SLoGin /// 获取内存管理初始化时,创建的第一个内核页表的地址
initial_page_table() -> PhysAddr61740fe15e0SLoGin fn initial_page_table() -> PhysAddr;
61840fe15e0SLoGin
61940fe15e0SLoGin /// 初始化新的usermapper,为用户进程创建页表
setup_new_usermapper() -> Result<UserMapper, SystemError>62040fe15e0SLoGin fn setup_new_usermapper() -> Result<UserMapper, SystemError>;
6217a29d4fcSLoGin
6227a29d4fcSLoGin /// 创建页表项
6237a29d4fcSLoGin ///
624*cf7f801eSMemoryShore /// 这是一个低阶api,用于根据物理地址以及指定好的EntryFlags,创建页表项
6257a29d4fcSLoGin ///
6267a29d4fcSLoGin /// ## 参数
6277a29d4fcSLoGin ///
6287a29d4fcSLoGin /// - `paddr` 物理地址
6297a29d4fcSLoGin /// - `page_flags` 页表项的flags
6307a29d4fcSLoGin ///
6317a29d4fcSLoGin /// ## 返回值
6327a29d4fcSLoGin ///
6337a29d4fcSLoGin /// 页表项的值
make_entry(paddr: PhysAddr, page_flags: usize) -> usize6347a29d4fcSLoGin fn make_entry(paddr: PhysAddr, page_flags: usize) -> usize;
635a17651b1SMemoryShore
636a17651b1SMemoryShore /// 判断一个VMA是否允许访问
637a17651b1SMemoryShore ///
638a17651b1SMemoryShore /// ## 参数
639a17651b1SMemoryShore ///
640a17651b1SMemoryShore /// - `vma`: 进行判断的VMA
641a17651b1SMemoryShore /// - `write`: 是否需要写入权限(true 表示需要写权限)
642a17651b1SMemoryShore /// - `execute`: 是否需要执行权限(true 表示需要执行权限)
643a17651b1SMemoryShore /// - `foreign`: 是否是外部的(即非当前进程的)VMA
644a17651b1SMemoryShore ///
645a17651b1SMemoryShore /// ## 返回值
646a17651b1SMemoryShore /// - `true`: VMA允许访问
647a17651b1SMemoryShore /// - `false`: 错误的说明
vma_access_permitted( _vma: Arc<LockedVMA>, _write: bool, _execute: bool, _foreign: bool, ) -> bool648a17651b1SMemoryShore fn vma_access_permitted(
649a17651b1SMemoryShore _vma: Arc<LockedVMA>,
650a17651b1SMemoryShore _write: bool,
651a17651b1SMemoryShore _execute: bool,
652a17651b1SMemoryShore _foreign: bool,
653a17651b1SMemoryShore ) -> bool {
654a17651b1SMemoryShore true
655a17651b1SMemoryShore }
656*cf7f801eSMemoryShore
657*cf7f801eSMemoryShore const PAGE_NONE: usize;
658*cf7f801eSMemoryShore const PAGE_SHARED: usize;
659*cf7f801eSMemoryShore const PAGE_SHARED_EXEC: usize;
660*cf7f801eSMemoryShore const PAGE_COPY_NOEXEC: usize;
661*cf7f801eSMemoryShore const PAGE_COPY_EXEC: usize;
662*cf7f801eSMemoryShore const PAGE_COPY: usize;
663*cf7f801eSMemoryShore const PAGE_READONLY: usize;
664*cf7f801eSMemoryShore const PAGE_READONLY_EXEC: usize;
665*cf7f801eSMemoryShore
666*cf7f801eSMemoryShore const PAGE_READ: usize;
667*cf7f801eSMemoryShore const PAGE_READ_EXEC: usize;
668*cf7f801eSMemoryShore const PAGE_WRITE: usize;
669*cf7f801eSMemoryShore const PAGE_WRITE_EXEC: usize;
670*cf7f801eSMemoryShore const PAGE_EXEC: usize;
671*cf7f801eSMemoryShore
672*cf7f801eSMemoryShore const PROTECTION_MAP: [EntryFlags<Self>; 16];
673*cf7f801eSMemoryShore
674*cf7f801eSMemoryShore /// 页面保护标志转换函数
675*cf7f801eSMemoryShore /// ## 参数
676*cf7f801eSMemoryShore ///
677*cf7f801eSMemoryShore /// - `vm_flags`: VmFlags标志
678*cf7f801eSMemoryShore ///
679*cf7f801eSMemoryShore /// ## 返回值
680*cf7f801eSMemoryShore /// - EntryFlags: 页面的保护位
vm_get_page_prot(vm_flags: VmFlags) -> EntryFlags<Self>681*cf7f801eSMemoryShore fn vm_get_page_prot(vm_flags: VmFlags) -> EntryFlags<Self> {
682*cf7f801eSMemoryShore let map = Self::PROTECTION_MAP;
683*cf7f801eSMemoryShore let mut ret = map[vm_flags
684*cf7f801eSMemoryShore .intersection(
685*cf7f801eSMemoryShore VmFlags::VM_READ | VmFlags::VM_WRITE | VmFlags::VM_EXEC | VmFlags::VM_SHARED,
686*cf7f801eSMemoryShore )
687*cf7f801eSMemoryShore .bits()];
688*cf7f801eSMemoryShore
689*cf7f801eSMemoryShore #[cfg(target_arch = "x86_64")]
690*cf7f801eSMemoryShore {
691*cf7f801eSMemoryShore // 如果xd位被保留,那么将可执行性设置为true
692*cf7f801eSMemoryShore if crate::arch::mm::X86_64MMArch::is_xd_reserved() {
693*cf7f801eSMemoryShore ret = ret.set_execute(true);
694*cf7f801eSMemoryShore }
695*cf7f801eSMemoryShore }
696*cf7f801eSMemoryShore ret
697*cf7f801eSMemoryShore }
69840fe15e0SLoGin }
69940fe15e0SLoGin
70040fe15e0SLoGin /// @brief 虚拟地址范围
70140fe15e0SLoGin /// 该结构体用于表示一个虚拟地址范围,包括起始地址与大小
70240fe15e0SLoGin ///
70340fe15e0SLoGin /// 请注意与VMA进行区分,该结构体被VMA所包含
70440fe15e0SLoGin #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
70540fe15e0SLoGin pub struct VirtRegion {
70640fe15e0SLoGin start: VirtAddr,
70740fe15e0SLoGin size: usize,
70840fe15e0SLoGin }
70940fe15e0SLoGin
71040fe15e0SLoGin #[allow(dead_code)]
71140fe15e0SLoGin impl VirtRegion {
71240fe15e0SLoGin /// # 创建一个新的虚拟地址范围
new(start: VirtAddr, size: usize) -> Self71340fe15e0SLoGin pub fn new(start: VirtAddr, size: usize) -> Self {
71440fe15e0SLoGin VirtRegion { start, size }
71540fe15e0SLoGin }
71640fe15e0SLoGin
71740fe15e0SLoGin /// 获取虚拟地址范围的起始地址
71840fe15e0SLoGin #[inline(always)]
start(&self) -> VirtAddr71940fe15e0SLoGin pub fn start(&self) -> VirtAddr {
72040fe15e0SLoGin self.start
72140fe15e0SLoGin }
72240fe15e0SLoGin
72340fe15e0SLoGin /// 获取虚拟地址范围的截止地址(不包括返回的地址)
72440fe15e0SLoGin #[inline(always)]
end(&self) -> VirtAddr72540fe15e0SLoGin pub fn end(&self) -> VirtAddr {
72640fe15e0SLoGin return self.start().add(self.size);
72740fe15e0SLoGin }
72840fe15e0SLoGin
72940fe15e0SLoGin /// # Create a new VirtRegion from a range [start, end)
73040fe15e0SLoGin ///
73140fe15e0SLoGin /// If end <= start, return None
between(start: VirtAddr, end: VirtAddr) -> Option<Self>73240fe15e0SLoGin pub fn between(start: VirtAddr, end: VirtAddr) -> Option<Self> {
73340fe15e0SLoGin if unlikely(end.data() <= start.data()) {
73440fe15e0SLoGin return None;
73540fe15e0SLoGin }
73640fe15e0SLoGin let size = end.data() - start.data();
73740fe15e0SLoGin return Some(VirtRegion::new(start, size));
73840fe15e0SLoGin }
73940fe15e0SLoGin
74040fe15e0SLoGin /// # 取两个虚拟地址范围的交集
74140fe15e0SLoGin ///
74240fe15e0SLoGin /// 如果两个虚拟地址范围没有交集,返回None
intersect(&self, other: &VirtRegion) -> Option<VirtRegion>74340fe15e0SLoGin pub fn intersect(&self, other: &VirtRegion) -> Option<VirtRegion> {
74440fe15e0SLoGin let start = self.start.max(other.start);
74540fe15e0SLoGin let end = self.end().min(other.end());
74640fe15e0SLoGin return VirtRegion::between(start, end);
74740fe15e0SLoGin }
74840fe15e0SLoGin
74940fe15e0SLoGin /// 设置虚拟地址范围的起始地址
75040fe15e0SLoGin #[inline(always)]
set_start(&mut self, start: VirtAddr)75140fe15e0SLoGin pub fn set_start(&mut self, start: VirtAddr) {
75240fe15e0SLoGin self.start = start;
75340fe15e0SLoGin }
75440fe15e0SLoGin
75540fe15e0SLoGin #[inline(always)]
size(&self) -> usize75640fe15e0SLoGin pub fn size(&self) -> usize {
75740fe15e0SLoGin self.size
75840fe15e0SLoGin }
75940fe15e0SLoGin
76040fe15e0SLoGin /// 设置虚拟地址范围的大小
76140fe15e0SLoGin #[inline(always)]
set_size(&mut self, size: usize)76240fe15e0SLoGin pub fn set_size(&mut self, size: usize) {
76340fe15e0SLoGin self.size = size;
76440fe15e0SLoGin }
76540fe15e0SLoGin
76640fe15e0SLoGin /// 判断虚拟地址范围是否为空
76740fe15e0SLoGin #[inline(always)]
is_empty(&self) -> bool76840fe15e0SLoGin pub fn is_empty(&self) -> bool {
76940fe15e0SLoGin self.size == 0
77040fe15e0SLoGin }
77140fe15e0SLoGin
77240fe15e0SLoGin /// 将虚拟地址区域的大小向上对齐到页大小
77340fe15e0SLoGin #[inline(always)]
round_up_size_to_page(self) -> Self77440fe15e0SLoGin pub fn round_up_size_to_page(self) -> Self {
77540fe15e0SLoGin return VirtRegion::new(self.start, round_up_to_page_size(self.size));
77640fe15e0SLoGin }
77740fe15e0SLoGin
77840fe15e0SLoGin /// 判断两个虚拟地址范围是否由于具有交集而导致冲突
77940fe15e0SLoGin #[inline(always)]
collide(&self, other: &VirtRegion) -> bool78040fe15e0SLoGin pub fn collide(&self, other: &VirtRegion) -> bool {
78140fe15e0SLoGin return self.intersect(other).is_some();
78240fe15e0SLoGin }
78340fe15e0SLoGin
iter_pages(&self) -> VirtPageFrameIter78440fe15e0SLoGin pub fn iter_pages(&self) -> VirtPageFrameIter {
78540fe15e0SLoGin return VirtPageFrame::iter_range(
78640fe15e0SLoGin VirtPageFrame::new(self.start),
78740fe15e0SLoGin VirtPageFrame::new(self.end()),
78840fe15e0SLoGin );
78940fe15e0SLoGin }
79040fe15e0SLoGin
79140fe15e0SLoGin /// 获取[self.start(), region.start())的虚拟地址范围
79240fe15e0SLoGin ///
79340fe15e0SLoGin /// 如果self.start() >= region.start(),返回None
before(self, region: &VirtRegion) -> Option<Self>79440fe15e0SLoGin pub fn before(self, region: &VirtRegion) -> Option<Self> {
79540fe15e0SLoGin return Self::between(self.start(), region.start());
79640fe15e0SLoGin }
79740fe15e0SLoGin
79840fe15e0SLoGin /// 获取[region.end(),self.end())的虚拟地址范围
79940fe15e0SLoGin ///
80040fe15e0SLoGin /// 如果 self.end() >= region.end() ,返回None
after(self, region: &VirtRegion) -> Option<Self>80140fe15e0SLoGin pub fn after(self, region: &VirtRegion) -> Option<Self> {
80240fe15e0SLoGin // if self.end() > region.end() none
80340fe15e0SLoGin return Self::between(region.end(), self.end());
80440fe15e0SLoGin }
80540fe15e0SLoGin
80640fe15e0SLoGin /// 把当前虚拟地址范围内的某个虚拟地址,转换为另一个虚拟地址范围内的虚拟地址
80740fe15e0SLoGin ///
80840fe15e0SLoGin /// 如果vaddr不在当前虚拟地址范围内,返回None
80940fe15e0SLoGin ///
81040fe15e0SLoGin /// 如果vaddr在当前虚拟地址范围内,返回vaddr在new_base中的虚拟地址
rebase(self, vaddr: VirtAddr, new_base: &VirtRegion) -> Option<VirtAddr>81140fe15e0SLoGin pub fn rebase(self, vaddr: VirtAddr, new_base: &VirtRegion) -> Option<VirtAddr> {
81240fe15e0SLoGin if !self.contains(vaddr) {
81340fe15e0SLoGin return None;
81440fe15e0SLoGin }
81540fe15e0SLoGin let offset = vaddr.data() - self.start().data();
81640fe15e0SLoGin let new_start = new_base.start().data() + offset;
81740fe15e0SLoGin return Some(VirtAddr::new(new_start));
81840fe15e0SLoGin }
81940fe15e0SLoGin
82040fe15e0SLoGin /// 判断虚拟地址范围是否包含指定的虚拟地址
contains(&self, addr: VirtAddr) -> bool82140fe15e0SLoGin pub fn contains(&self, addr: VirtAddr) -> bool {
82240fe15e0SLoGin return self.start() <= addr && addr < self.end();
82340fe15e0SLoGin }
82440fe15e0SLoGin
82540fe15e0SLoGin /// 创建当前虚拟地址范围的页面迭代器
pages(&self) -> VirtPageFrameIter82640fe15e0SLoGin pub fn pages(&self) -> VirtPageFrameIter {
82740fe15e0SLoGin return VirtPageFrame::iter_range(
82840fe15e0SLoGin VirtPageFrame::new(self.start()),
82940fe15e0SLoGin VirtPageFrame::new(self.end()),
83040fe15e0SLoGin );
83140fe15e0SLoGin }
83240fe15e0SLoGin }
83340fe15e0SLoGin
83440fe15e0SLoGin impl PartialOrd for VirtRegion {
partial_cmp(&self, other: &Self) -> Option<cmp::Ordering>83540fe15e0SLoGin fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
836b5b571e0SLoGin Some(self.cmp(other))
83740fe15e0SLoGin }
83840fe15e0SLoGin }
83940fe15e0SLoGin
84040fe15e0SLoGin impl Ord for VirtRegion {
cmp(&self, other: &Self) -> cmp::Ordering84140fe15e0SLoGin fn cmp(&self, other: &Self) -> cmp::Ordering {
84240fe15e0SLoGin return self.start.cmp(&other.start);
84340fe15e0SLoGin }
84440fe15e0SLoGin }
84540fe15e0SLoGin
84640fe15e0SLoGin /// ## 判断虚拟地址是否超出了用户空间
84740fe15e0SLoGin ///
84840fe15e0SLoGin /// 如果虚拟地址超出了用户空间,返回Err(SystemError::EFAULT).
84940fe15e0SLoGin /// 如果end < start,返回Err(SystemError::EOVERFLOW)
85040fe15e0SLoGin ///
85140fe15e0SLoGin /// 否则返回Ok(())
verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError>85240fe15e0SLoGin pub fn verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError> {
85340fe15e0SLoGin let end = addr.add(size);
85440fe15e0SLoGin if unlikely(end.data() < addr.data()) {
85540fe15e0SLoGin return Err(SystemError::EOVERFLOW);
85640fe15e0SLoGin }
85740fe15e0SLoGin
85840fe15e0SLoGin if !addr.check_user() || !end.check_user() {
85940fe15e0SLoGin return Err(SystemError::EFAULT);
86040fe15e0SLoGin }
86140fe15e0SLoGin
86240fe15e0SLoGin return Ok(());
86340fe15e0SLoGin }
864