1 use alloc::sync::Arc; 2 use page::EntryFlags; 3 use system_error::SystemError; 4 5 use crate::arch::MMArch; 6 7 use core::{ 8 cmp, 9 fmt::Debug, 10 intrinsics::unlikely, 11 ops::{Add, AddAssign, Sub, SubAssign}, 12 ptr, 13 sync::atomic::{AtomicBool, Ordering}, 14 }; 15 16 use self::{ 17 allocator::page_frame::{VirtPageFrame, VirtPageFrameIter}, 18 memblock::MemoryAreaAttr, 19 page::round_up_to_page_size, 20 ucontext::{AddressSpace, LockedVMA, UserMapper}, 21 }; 22 23 pub mod allocator; 24 pub mod c_adapter; 25 pub mod early_ioremap; 26 pub mod fault; 27 pub mod init; 28 pub mod kernel_mapper; 29 pub mod madvise; 30 pub mod memblock; 31 pub mod mmio_buddy; 32 pub mod no_init; 33 pub mod page; 34 pub mod percpu; 35 pub mod syscall; 36 pub mod ucontext; 37 38 /// 内核INIT进程的用户地址空间结构体(仅在process_init中初始化) 39 static mut __IDLE_PROCESS_ADDRESS_SPACE: Option<Arc<AddressSpace>> = None; 40 41 bitflags! { 42 /// Virtual memory flags 43 #[allow(clippy::bad_bit_mask)] 44 pub struct VmFlags:usize{ 45 const VM_NONE = 0x00000000; 46 47 const VM_READ = 0x00000001; 48 const VM_WRITE = 0x00000002; 49 const VM_EXEC = 0x00000004; 50 const VM_SHARED = 0x00000008; 51 52 const VM_MAYREAD = 0x00000010; 53 const VM_MAYWRITE = 0x00000020; 54 const VM_MAYEXEC = 0x00000040; 55 const VM_MAYSHARE = 0x00000080; 56 57 const VM_GROWSDOWN = 0x00000100; 58 const VM_UFFD_MISSING = 0x00000200; 59 const VM_PFNMAP = 0x00000400; 60 const VM_UFFD_WP = 0x00001000; 61 62 const VM_LOCKED = 0x00002000; 63 const VM_IO = 0x00004000; 64 65 const VM_SEQ_READ = 0x00008000; 66 const VM_RAND_READ = 0x00010000; 67 68 const VM_DONTCOPY = 0x00020000; 69 const VM_DONTEXPAND = 0x00040000; 70 const VM_LOCKONFAULT = 0x00080000; 71 const VM_ACCOUNT = 0x00100000; 72 const VM_NORESERVE = 0x00200000; 73 const VM_HUGETLB = 0x00400000; 74 const VM_SYNC = 0x00800000; 75 const VM_ARCH_1 = 0x01000000; 76 const VM_WIPEONFORK = 0x02000000; 77 const VM_DONTDUMP = 0x04000000; 78 } 79 80 /// 描述页面错误处理过程中发生的不同情况或结果 81 pub struct VmFaultReason:u32 { 82 const VM_FAULT_OOM = 0x000001; 83 const VM_FAULT_SIGBUS = 0x000002; 84 const VM_FAULT_MAJOR = 0x000004; 85 const VM_FAULT_WRITE = 0x000008; 86 const VM_FAULT_HWPOISON = 0x000010; 87 const VM_FAULT_HWPOISON_LARGE = 0x000020; 88 const VM_FAULT_SIGSEGV = 0x000040; 89 const VM_FAULT_NOPAGE = 0x000100; 90 const VM_FAULT_LOCKED = 0x000200; 91 const VM_FAULT_RETRY = 0x000400; 92 const VM_FAULT_FALLBACK = 0x000800; 93 const VM_FAULT_DONE_COW = 0x001000; 94 const VM_FAULT_NEEDDSYNC = 0x002000; 95 const VM_FAULT_COMPLETED = 0x004000; 96 const VM_FAULT_HINDEX_MASK = 0x0f0000; 97 const VM_FAULT_ERROR = 0x000001 | 0x000002 | 0x000040 | 0x000010 | 0x000020 | 0x000800; 98 } 99 100 pub struct MsFlags:usize { 101 const MS_ASYNC = 1; 102 const MS_INVALIDATE = 2; 103 const MS_SYNC = 4; 104 } 105 } 106 107 impl core::ops::Index<VmFlags> for [usize] { 108 type Output = usize; 109 110 fn index(&self, index: VmFlags) -> &Self::Output { 111 &self[index.bits] 112 } 113 } 114 115 impl core::ops::IndexMut<VmFlags> for [usize] { 116 fn index_mut(&mut self, index: VmFlags) -> &mut Self::Output { 117 &mut self[index.bits] 118 } 119 } 120 121 /// 获取内核IDLE进程的用户地址空间结构体 122 #[allow(non_snake_case)] 123 #[inline(always)] 124 pub fn IDLE_PROCESS_ADDRESS_SPACE() -> Arc<AddressSpace> { 125 unsafe { 126 return __IDLE_PROCESS_ADDRESS_SPACE 127 .as_ref() 128 .expect("IDLE_PROCESS_ADDRESS_SPACE is null") 129 .clone(); 130 } 131 } 132 133 /// 设置内核IDLE进程的用户地址空间结构体全局变量 134 #[allow(non_snake_case)] 135 pub unsafe fn set_IDLE_PROCESS_ADDRESS_SPACE(address_space: Arc<AddressSpace>) { 136 static INITIALIZED: AtomicBool = AtomicBool::new(false); 137 if INITIALIZED 138 .compare_exchange(false, true, Ordering::SeqCst, Ordering::Acquire) 139 .is_err() 140 { 141 panic!("IDLE_PROCESS_ADDRESS_SPACE is already initialized"); 142 } 143 __IDLE_PROCESS_ADDRESS_SPACE = Some(address_space); 144 } 145 146 #[derive(Clone, Copy, Debug, Eq, Ord, PartialEq, PartialOrd, Hash)] 147 pub enum PageTableKind { 148 /// 用户可访问的页表 149 User, 150 /// 内核页表 151 Kernel, 152 /// x86内存虚拟化中使用的EPT 153 #[cfg(target_arch = "x86_64")] 154 EPT, 155 } 156 157 /// 物理内存地址 158 #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)] 159 #[repr(transparent)] 160 pub struct PhysAddr(usize); 161 162 impl PhysAddr { 163 /// 最大物理地址 164 pub const MAX: Self = PhysAddr(usize::MAX); 165 166 #[inline(always)] 167 pub const fn new(address: usize) -> Self { 168 Self(address) 169 } 170 171 /// @brief 获取物理地址的值 172 #[inline(always)] 173 pub const fn data(&self) -> usize { 174 self.0 175 } 176 177 /// @brief 将物理地址加上一个偏移量 178 #[inline(always)] 179 pub fn add(self, offset: usize) -> Self { 180 Self(self.0 + offset) 181 } 182 183 /// @brief 判断物理地址是否按照指定要求对齐 184 #[inline(always)] 185 pub fn check_aligned(&self, align: usize) -> bool { 186 return self.0 & (align - 1) == 0; 187 } 188 189 #[inline(always)] 190 pub fn is_null(&self) -> bool { 191 return self.0 == 0; 192 } 193 } 194 195 impl Debug for PhysAddr { 196 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 197 write!(f, "PhysAddr({:#x})", self.0) 198 } 199 } 200 201 impl core::ops::Add<usize> for PhysAddr { 202 type Output = Self; 203 204 #[inline(always)] 205 fn add(self, rhs: usize) -> Self::Output { 206 return Self(self.0 + rhs); 207 } 208 } 209 210 impl core::ops::AddAssign<usize> for PhysAddr { 211 #[inline(always)] 212 fn add_assign(&mut self, rhs: usize) { 213 self.0 += rhs; 214 } 215 } 216 217 impl core::ops::Add<PhysAddr> for PhysAddr { 218 type Output = Self; 219 220 #[inline(always)] 221 fn add(self, rhs: PhysAddr) -> Self::Output { 222 return Self(self.0 + rhs.0); 223 } 224 } 225 226 impl core::ops::AddAssign<PhysAddr> for PhysAddr { 227 #[inline(always)] 228 fn add_assign(&mut self, rhs: PhysAddr) { 229 self.0 += rhs.0; 230 } 231 } 232 233 impl core::ops::BitOrAssign<usize> for PhysAddr { 234 #[inline(always)] 235 fn bitor_assign(&mut self, rhs: usize) { 236 self.0 |= rhs; 237 } 238 } 239 240 impl core::ops::BitOrAssign<PhysAddr> for PhysAddr { 241 #[inline(always)] 242 fn bitor_assign(&mut self, rhs: PhysAddr) { 243 self.0 |= rhs.0; 244 } 245 } 246 247 impl core::ops::Sub<usize> for PhysAddr { 248 type Output = Self; 249 250 #[inline(always)] 251 fn sub(self, rhs: usize) -> Self::Output { 252 return Self(self.0 - rhs); 253 } 254 } 255 256 impl core::ops::SubAssign<usize> for PhysAddr { 257 #[inline(always)] 258 fn sub_assign(&mut self, rhs: usize) { 259 self.0 -= rhs; 260 } 261 } 262 263 impl core::ops::Sub<PhysAddr> for PhysAddr { 264 type Output = usize; 265 266 #[inline(always)] 267 fn sub(self, rhs: PhysAddr) -> Self::Output { 268 return self.0 - rhs.0; 269 } 270 } 271 272 impl core::ops::SubAssign<PhysAddr> for PhysAddr { 273 #[inline(always)] 274 fn sub_assign(&mut self, rhs: PhysAddr) { 275 self.0 -= rhs.0; 276 } 277 } 278 279 /// 虚拟内存地址 280 #[derive(Clone, Copy, Eq, Ord, PartialEq, PartialOrd, Hash)] 281 #[repr(transparent)] 282 pub struct VirtAddr(usize); 283 284 impl VirtAddr { 285 #[inline(always)] 286 pub const fn new(address: usize) -> Self { 287 return Self(address); 288 } 289 290 /// @brief 获取虚拟地址的值 291 #[inline(always)] 292 pub const fn data(&self) -> usize { 293 return self.0; 294 } 295 296 /// @brief 判断虚拟地址的类型 297 #[inline(always)] 298 pub fn kind(&self) -> PageTableKind { 299 if self.check_user() { 300 return PageTableKind::User; 301 } else { 302 return PageTableKind::Kernel; 303 } 304 } 305 306 /// @brief 判断虚拟地址是否按照指定要求对齐 307 #[inline(always)] 308 pub fn check_aligned(&self, align: usize) -> bool { 309 return self.0 & (align - 1) == 0; 310 } 311 312 /// @brief 判断虚拟地址是否在用户空间 313 #[inline(always)] 314 pub fn check_user(&self) -> bool { 315 return self < &MMArch::USER_END_VADDR; 316 } 317 318 #[inline(always)] 319 pub fn as_ptr<T>(self) -> *mut T { 320 return self.0 as *mut T; 321 } 322 323 #[inline(always)] 324 pub fn is_null(&self) -> bool { 325 return self.0 == 0; 326 } 327 } 328 329 impl Add<VirtAddr> for VirtAddr { 330 type Output = Self; 331 332 #[inline(always)] 333 fn add(self, rhs: VirtAddr) -> Self::Output { 334 return Self(self.0 + rhs.0); 335 } 336 } 337 338 impl Add<usize> for VirtAddr { 339 type Output = Self; 340 341 #[inline(always)] 342 fn add(self, rhs: usize) -> Self::Output { 343 return Self(self.0 + rhs); 344 } 345 } 346 347 impl Sub<VirtAddr> for VirtAddr { 348 type Output = usize; 349 350 #[inline(always)] 351 fn sub(self, rhs: VirtAddr) -> Self::Output { 352 return self.0 - rhs.0; 353 } 354 } 355 356 impl Sub<usize> for VirtAddr { 357 type Output = Self; 358 359 #[inline(always)] 360 fn sub(self, rhs: usize) -> Self::Output { 361 return Self(self.0 - rhs); 362 } 363 } 364 365 impl AddAssign<usize> for VirtAddr { 366 #[inline(always)] 367 fn add_assign(&mut self, rhs: usize) { 368 self.0 += rhs; 369 } 370 } 371 372 impl AddAssign<VirtAddr> for VirtAddr { 373 #[inline(always)] 374 fn add_assign(&mut self, rhs: VirtAddr) { 375 self.0 += rhs.0; 376 } 377 } 378 379 impl SubAssign<usize> for VirtAddr { 380 #[inline(always)] 381 fn sub_assign(&mut self, rhs: usize) { 382 self.0 -= rhs; 383 } 384 } 385 386 impl SubAssign<VirtAddr> for VirtAddr { 387 #[inline(always)] 388 fn sub_assign(&mut self, rhs: VirtAddr) { 389 self.0 -= rhs.0; 390 } 391 } 392 393 impl Debug for VirtAddr { 394 fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { 395 write!(f, "VirtAddr({:#x})", self.0) 396 } 397 } 398 399 /// @brief 物理内存区域 400 #[derive(Clone, Copy, Debug)] 401 pub struct PhysMemoryArea { 402 /// 物理基地址 403 pub base: PhysAddr, 404 /// 该区域的物理内存大小 405 pub size: usize, 406 407 pub flags: MemoryAreaAttr, 408 } 409 410 impl PhysMemoryArea { 411 pub const DEFAULT: Self = Self { 412 base: PhysAddr::new(0), 413 size: 0, 414 flags: MemoryAreaAttr::empty(), 415 }; 416 417 pub fn new(base: PhysAddr, size: usize, flags: MemoryAreaAttr) -> Self { 418 Self { base, size, flags } 419 } 420 421 /// 返回向上页面对齐的区域起始物理地址 422 pub fn area_base_aligned(&self) -> PhysAddr { 423 return PhysAddr::new( 424 (self.base.data() + (MMArch::PAGE_SIZE - 1)) & !(MMArch::PAGE_SIZE - 1), 425 ); 426 } 427 428 /// 返回向下页面对齐的区域截止物理地址 429 pub fn area_end_aligned(&self) -> PhysAddr { 430 return PhysAddr::new((self.base.data() + self.size) & !(MMArch::PAGE_SIZE - 1)); 431 } 432 } 433 434 impl Default for PhysMemoryArea { 435 fn default() -> Self { 436 return Self::DEFAULT; 437 } 438 } 439 440 #[allow(dead_code)] 441 pub trait MemoryManagementArch: Clone + Copy + Debug { 442 /// 是否支持缺页中断 443 const PAGE_FAULT_ENABLED: bool; 444 /// 页面大小的shift(假如页面4K,那么这个值就是12,因为2^12=4096) 445 const PAGE_SHIFT: usize; 446 /// 每个页表的页表项数目。(以2^n次幂来表示)假如有512个页表项,那么这个值就是9 447 const PAGE_ENTRY_SHIFT: usize; 448 /// 页表层级数量 449 const PAGE_LEVELS: usize; 450 451 /// 页表项的有效位的index(假如页表项的第0-51位有效,那么这个值就是52) 452 const ENTRY_ADDRESS_SHIFT: usize; 453 /// 页面的页表项的默认值 454 const ENTRY_FLAG_DEFAULT_PAGE: usize; 455 /// 页表的页表项的默认值 456 const ENTRY_FLAG_DEFAULT_TABLE: usize; 457 /// 页表项的present位被置位之后的值 458 const ENTRY_FLAG_PRESENT: usize; 459 /// 页表项为read only时的值 460 const ENTRY_FLAG_READONLY: usize; 461 /// 页表项的write bit 462 const ENTRY_FLAG_WRITEABLE: usize; 463 /// 页表项为可读写状态的值 464 const ENTRY_FLAG_READWRITE: usize; 465 /// 页面项标记页面为user page的值 466 const ENTRY_FLAG_USER: usize; 467 /// 页面项标记页面为write through的值 468 const ENTRY_FLAG_WRITE_THROUGH: usize; 469 /// 页面项标记页面为cache disable的值 470 const ENTRY_FLAG_CACHE_DISABLE: usize; 471 /// 标记当前页面不可执行的标志位(Execute disable)(也就是说,不能从这段内存里面获取处理器指令) 472 const ENTRY_FLAG_NO_EXEC: usize; 473 /// 标记当前页面可执行的标志位(Execute enable) 474 const ENTRY_FLAG_EXEC: usize; 475 /// 当该位为1时,标明这是一个脏页 476 const ENTRY_FLAG_DIRTY: usize; 477 /// 当该位为1时,代表这个页面被处理器访问过 478 const ENTRY_FLAG_ACCESSED: usize; 479 /// 标记该页表项指向的页是否为大页 480 const ENTRY_FLAG_HUGE_PAGE: usize; 481 /// 当该位为1时,代表该页表项是全局的 482 const ENTRY_FLAG_GLOBAL: usize; 483 484 /// 虚拟地址与物理地址的偏移量 485 const PHYS_OFFSET: usize; 486 487 /// 内核在链接时被链接到的偏移量 488 const KERNEL_LINK_OFFSET: usize; 489 490 const KERNEL_VIRT_START: usize = Self::PHYS_OFFSET + Self::KERNEL_LINK_OFFSET; 491 492 /// 每个页面的大小 493 const PAGE_SIZE: usize = 1 << Self::PAGE_SHIFT; 494 /// 通过这个mask,获取地址的页内偏移量 495 const PAGE_OFFSET_MASK: usize = Self::PAGE_SIZE - 1; 496 /// 通过这个mask,获取页的首地址 497 const PAGE_MASK: usize = !(Self::PAGE_OFFSET_MASK); 498 /// 页表项的地址、数据部分的shift。 499 /// 打个比方,如果这个值为52,那么意味着页表项的[0, 52)位,用于表示地址以及其他的标志位 500 const PAGE_ADDRESS_SHIFT: usize = Self::PAGE_LEVELS * Self::PAGE_ENTRY_SHIFT + Self::PAGE_SHIFT; 501 /// 最大的虚拟地址(对于不同的架构,由于上述PAGE_ADDRESS_SHIFT可能包括了reserved bits, 事实上能表示的虚拟地址应该比这个值要小) 502 const PAGE_ADDRESS_SIZE: usize = 1 << Self::PAGE_ADDRESS_SHIFT; 503 /// 页表项的值与这个常量进行与运算,得到的结果是所填写的物理地址 504 const PAGE_ADDRESS_MASK: usize = Self::PAGE_ADDRESS_SIZE - Self::PAGE_SIZE; 505 /// 每个页表项的大小 506 const PAGE_ENTRY_SIZE: usize = 1 << (Self::PAGE_SHIFT - Self::PAGE_ENTRY_SHIFT); 507 /// 每个页表的页表项数目 508 const PAGE_ENTRY_NUM: usize = 1 << Self::PAGE_ENTRY_SHIFT; 509 /// 该字段用于根据虚拟地址,获取该虚拟地址在对应的页表中是第几个页表项 510 const PAGE_ENTRY_MASK: usize = Self::PAGE_ENTRY_NUM - 1; 511 /// 内核页表在顶级页表的第一个页表项的索引 512 const PAGE_KERNEL_INDEX: usize = (Self::PHYS_OFFSET & Self::PAGE_ADDRESS_MASK) 513 >> (Self::PAGE_ADDRESS_SHIFT - Self::PAGE_ENTRY_SHIFT); 514 515 const PAGE_NEGATIVE_MASK: usize = !((Self::PAGE_ADDRESS_SIZE) - 1); 516 517 const ENTRY_ADDRESS_SIZE: usize = 1 << Self::ENTRY_ADDRESS_SHIFT; 518 /// 该mask用于获取页表项中地址字段 519 const ENTRY_ADDRESS_MASK: usize = Self::ENTRY_ADDRESS_SIZE - Self::PAGE_SIZE; 520 /// 这个mask用于获取页表项中的flags 521 const ENTRY_FLAGS_MASK: usize = !Self::ENTRY_ADDRESS_MASK; 522 523 /// 用户空间的最高地址 524 const USER_END_VADDR: VirtAddr; 525 /// 用户堆的起始地址 526 const USER_BRK_START: VirtAddr; 527 /// 用户栈起始地址(向下生长,不包含该值) 528 const USER_STACK_START: VirtAddr; 529 530 /// 内核的固定映射区的起始地址 531 const FIXMAP_START_VADDR: VirtAddr; 532 /// 内核的固定映射区的大小 533 const FIXMAP_SIZE: usize; 534 /// 内核的固定映射区的结束地址 535 const FIXMAP_END_VADDR: VirtAddr = 536 VirtAddr::new(Self::FIXMAP_START_VADDR.data() + Self::FIXMAP_SIZE); 537 538 /// MMIO虚拟空间的基地址 539 const MMIO_BASE: VirtAddr; 540 /// MMIO虚拟空间的大小 541 const MMIO_SIZE: usize; 542 /// MMIO虚拟空间的顶端地址(不包含) 543 const MMIO_TOP: VirtAddr = VirtAddr::new(Self::MMIO_BASE.data() + Self::MMIO_SIZE); 544 545 /// @brief 用于初始化内存管理模块与架构相关的信息。 546 /// 该函数应调用其他模块的接口,把可用内存区域添加到memblock,提供给BumpAllocator使用 547 unsafe fn init(); 548 549 /// 内存管理初始化完成后,调用该函数 550 unsafe fn arch_post_init() {} 551 552 /// @brief 读取指定虚拟地址的值,并假设它是类型T的指针 553 #[inline(always)] 554 unsafe fn read<T>(address: VirtAddr) -> T { 555 return ptr::read(address.data() as *const T); 556 } 557 558 /// @brief 将value写入到指定的虚拟地址 559 #[inline(always)] 560 unsafe fn write<T>(address: VirtAddr, value: T) { 561 ptr::write(address.data() as *mut T, value); 562 } 563 564 #[inline(always)] 565 unsafe fn write_bytes(address: VirtAddr, value: u8, count: usize) { 566 ptr::write_bytes(address.data() as *mut u8, value, count); 567 } 568 569 /// @brief 刷新TLB中,关于指定虚拟地址的条目 570 unsafe fn invalidate_page(address: VirtAddr); 571 572 /// @brief 刷新TLB中,所有的条目 573 unsafe fn invalidate_all(); 574 575 /// @brief 获取顶级页表的物理地址 576 unsafe fn table(table_kind: PageTableKind) -> PhysAddr; 577 578 /// @brief 设置顶级页表的物理地址到处理器中 579 unsafe fn set_table(table_kind: PageTableKind, table: PhysAddr); 580 581 /// @brief 将物理地址转换为虚拟地址. 582 /// 583 /// @param phys 物理地址 584 /// 585 /// @return 转换后的虚拟地址。如果转换失败,返回None 586 #[inline(always)] 587 unsafe fn phys_2_virt(phys: PhysAddr) -> Option<VirtAddr> { 588 if let Some(vaddr) = phys.data().checked_add(Self::PHYS_OFFSET) { 589 return Some(VirtAddr::new(vaddr)); 590 } else { 591 return None; 592 } 593 } 594 595 /// 将虚拟地址转换为物理地址 596 /// 597 /// ## 参数 598 /// 599 /// - `virt` 虚拟地址 600 /// 601 /// ## 返回值 602 /// 603 /// 转换后的物理地址。如果转换失败,返回None 604 #[inline(always)] 605 unsafe fn virt_2_phys(virt: VirtAddr) -> Option<PhysAddr> { 606 if let Some(paddr) = virt.data().checked_sub(Self::PHYS_OFFSET) { 607 return Some(PhysAddr::new(paddr)); 608 } else { 609 return None; 610 } 611 } 612 613 /// @brief 判断指定的虚拟地址是否正确(符合规范) 614 fn virt_is_valid(virt: VirtAddr) -> bool; 615 616 /// 获取内存管理初始化时,创建的第一个内核页表的地址 617 fn initial_page_table() -> PhysAddr; 618 619 /// 初始化新的usermapper,为用户进程创建页表 620 fn setup_new_usermapper() -> Result<UserMapper, SystemError>; 621 622 /// 创建页表项 623 /// 624 /// 这是一个低阶api,用于根据物理地址以及指定好的EntryFlags,创建页表项 625 /// 626 /// ## 参数 627 /// 628 /// - `paddr` 物理地址 629 /// - `page_flags` 页表项的flags 630 /// 631 /// ## 返回值 632 /// 633 /// 页表项的值 634 fn make_entry(paddr: PhysAddr, page_flags: usize) -> usize; 635 636 /// 判断一个VMA是否允许访问 637 /// 638 /// ## 参数 639 /// 640 /// - `vma`: 进行判断的VMA 641 /// - `write`: 是否需要写入权限(true 表示需要写权限) 642 /// - `execute`: 是否需要执行权限(true 表示需要执行权限) 643 /// - `foreign`: 是否是外部的(即非当前进程的)VMA 644 /// 645 /// ## 返回值 646 /// - `true`: VMA允许访问 647 /// - `false`: 错误的说明 648 fn vma_access_permitted( 649 _vma: Arc<LockedVMA>, 650 _write: bool, 651 _execute: bool, 652 _foreign: bool, 653 ) -> bool { 654 true 655 } 656 657 const PAGE_NONE: usize; 658 const PAGE_SHARED: usize; 659 const PAGE_SHARED_EXEC: usize; 660 const PAGE_COPY_NOEXEC: usize; 661 const PAGE_COPY_EXEC: usize; 662 const PAGE_COPY: usize; 663 const PAGE_READONLY: usize; 664 const PAGE_READONLY_EXEC: usize; 665 666 const PAGE_READ: usize; 667 const PAGE_READ_EXEC: usize; 668 const PAGE_WRITE: usize; 669 const PAGE_WRITE_EXEC: usize; 670 const PAGE_EXEC: usize; 671 672 const PROTECTION_MAP: [EntryFlags<Self>; 16]; 673 674 /// 页面保护标志转换函数 675 /// ## 参数 676 /// 677 /// - `vm_flags`: VmFlags标志 678 /// 679 /// ## 返回值 680 /// - EntryFlags: 页面的保护位 681 fn vm_get_page_prot(vm_flags: VmFlags) -> EntryFlags<Self> { 682 let map = Self::PROTECTION_MAP; 683 let mut ret = map[vm_flags 684 .intersection( 685 VmFlags::VM_READ | VmFlags::VM_WRITE | VmFlags::VM_EXEC | VmFlags::VM_SHARED, 686 ) 687 .bits()]; 688 689 #[cfg(target_arch = "x86_64")] 690 { 691 // 如果xd位被保留,那么将可执行性设置为true 692 if crate::arch::mm::X86_64MMArch::is_xd_reserved() { 693 ret = ret.set_execute(true); 694 } 695 } 696 ret 697 } 698 } 699 700 /// @brief 虚拟地址范围 701 /// 该结构体用于表示一个虚拟地址范围,包括起始地址与大小 702 /// 703 /// 请注意与VMA进行区分,该结构体被VMA所包含 704 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 705 pub struct VirtRegion { 706 start: VirtAddr, 707 size: usize, 708 } 709 710 #[allow(dead_code)] 711 impl VirtRegion { 712 /// # 创建一个新的虚拟地址范围 713 pub fn new(start: VirtAddr, size: usize) -> Self { 714 VirtRegion { start, size } 715 } 716 717 /// 获取虚拟地址范围的起始地址 718 #[inline(always)] 719 pub fn start(&self) -> VirtAddr { 720 self.start 721 } 722 723 /// 获取虚拟地址范围的截止地址(不包括返回的地址) 724 #[inline(always)] 725 pub fn end(&self) -> VirtAddr { 726 return self.start().add(self.size); 727 } 728 729 /// # Create a new VirtRegion from a range [start, end) 730 /// 731 /// If end <= start, return None 732 pub fn between(start: VirtAddr, end: VirtAddr) -> Option<Self> { 733 if unlikely(end.data() <= start.data()) { 734 return None; 735 } 736 let size = end.data() - start.data(); 737 return Some(VirtRegion::new(start, size)); 738 } 739 740 /// # 取两个虚拟地址范围的交集 741 /// 742 /// 如果两个虚拟地址范围没有交集,返回None 743 pub fn intersect(&self, other: &VirtRegion) -> Option<VirtRegion> { 744 let start = self.start.max(other.start); 745 let end = self.end().min(other.end()); 746 return VirtRegion::between(start, end); 747 } 748 749 /// 设置虚拟地址范围的起始地址 750 #[inline(always)] 751 pub fn set_start(&mut self, start: VirtAddr) { 752 self.start = start; 753 } 754 755 #[inline(always)] 756 pub fn size(&self) -> usize { 757 self.size 758 } 759 760 /// 设置虚拟地址范围的大小 761 #[inline(always)] 762 pub fn set_size(&mut self, size: usize) { 763 self.size = size; 764 } 765 766 /// 判断虚拟地址范围是否为空 767 #[inline(always)] 768 pub fn is_empty(&self) -> bool { 769 self.size == 0 770 } 771 772 /// 将虚拟地址区域的大小向上对齐到页大小 773 #[inline(always)] 774 pub fn round_up_size_to_page(self) -> Self { 775 return VirtRegion::new(self.start, round_up_to_page_size(self.size)); 776 } 777 778 /// 判断两个虚拟地址范围是否由于具有交集而导致冲突 779 #[inline(always)] 780 pub fn collide(&self, other: &VirtRegion) -> bool { 781 return self.intersect(other).is_some(); 782 } 783 784 pub fn iter_pages(&self) -> VirtPageFrameIter { 785 return VirtPageFrame::iter_range( 786 VirtPageFrame::new(self.start), 787 VirtPageFrame::new(self.end()), 788 ); 789 } 790 791 /// 获取[self.start(), region.start())的虚拟地址范围 792 /// 793 /// 如果self.start() >= region.start(),返回None 794 pub fn before(self, region: &VirtRegion) -> Option<Self> { 795 return Self::between(self.start(), region.start()); 796 } 797 798 /// 获取[region.end(),self.end())的虚拟地址范围 799 /// 800 /// 如果 self.end() >= region.end() ,返回None 801 pub fn after(self, region: &VirtRegion) -> Option<Self> { 802 // if self.end() > region.end() none 803 return Self::between(region.end(), self.end()); 804 } 805 806 /// 把当前虚拟地址范围内的某个虚拟地址,转换为另一个虚拟地址范围内的虚拟地址 807 /// 808 /// 如果vaddr不在当前虚拟地址范围内,返回None 809 /// 810 /// 如果vaddr在当前虚拟地址范围内,返回vaddr在new_base中的虚拟地址 811 pub fn rebase(self, vaddr: VirtAddr, new_base: &VirtRegion) -> Option<VirtAddr> { 812 if !self.contains(vaddr) { 813 return None; 814 } 815 let offset = vaddr.data() - self.start().data(); 816 let new_start = new_base.start().data() + offset; 817 return Some(VirtAddr::new(new_start)); 818 } 819 820 /// 判断虚拟地址范围是否包含指定的虚拟地址 821 pub fn contains(&self, addr: VirtAddr) -> bool { 822 return self.start() <= addr && addr < self.end(); 823 } 824 825 /// 创建当前虚拟地址范围的页面迭代器 826 pub fn pages(&self) -> VirtPageFrameIter { 827 return VirtPageFrame::iter_range( 828 VirtPageFrame::new(self.start()), 829 VirtPageFrame::new(self.end()), 830 ); 831 } 832 } 833 834 impl PartialOrd for VirtRegion { 835 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { 836 Some(self.cmp(other)) 837 } 838 } 839 840 impl Ord for VirtRegion { 841 fn cmp(&self, other: &Self) -> cmp::Ordering { 842 return self.start.cmp(&other.start); 843 } 844 } 845 846 /// ## 判断虚拟地址是否超出了用户空间 847 /// 848 /// 如果虚拟地址超出了用户空间,返回Err(SystemError::EFAULT). 849 /// 如果end < start,返回Err(SystemError::EOVERFLOW) 850 /// 851 /// 否则返回Ok(()) 852 pub fn verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError> { 853 let end = addr.add(size); 854 if unlikely(end.data() < addr.data()) { 855 return Err(SystemError::EOVERFLOW); 856 } 857 858 if !addr.check_user() || !end.check_user() { 859 return Err(SystemError::EFAULT); 860 } 861 862 return Ok(()); 863 } 864