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