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 /// MMIO虚拟空间的基地址 496 const MMIO_BASE: VirtAddr; 497 /// MMIO虚拟空间的大小 498 const MMIO_SIZE: usize; 499 /// MMIO虚拟空间的顶端地址(不包含) 500 const MMIO_TOP: VirtAddr = VirtAddr::new(Self::MMIO_BASE.data() + Self::MMIO_SIZE); 501 502 /// @brief 用于初始化内存管理模块与架构相关的信息。 503 /// 该函数应调用其他模块的接口,把可用内存区域添加到memblock,提供给BumpAllocator使用 504 unsafe fn init(); 505 506 /// 内存管理初始化完成后,调用该函数 507 unsafe fn arch_post_init() {} 508 509 /// @brief 读取指定虚拟地址的值,并假设它是类型T的指针 510 #[inline(always)] 511 unsafe fn read<T>(address: VirtAddr) -> T { 512 return ptr::read(address.data() as *const T); 513 } 514 515 /// @brief 将value写入到指定的虚拟地址 516 #[inline(always)] 517 unsafe fn write<T>(address: VirtAddr, value: T) { 518 ptr::write(address.data() as *mut T, value); 519 } 520 521 #[inline(always)] 522 unsafe fn write_bytes(address: VirtAddr, value: u8, count: usize) { 523 ptr::write_bytes(address.data() as *mut u8, value, count); 524 } 525 526 /// @brief 刷新TLB中,关于指定虚拟地址的条目 527 unsafe fn invalidate_page(address: VirtAddr); 528 529 /// @brief 刷新TLB中,所有的条目 530 unsafe fn invalidate_all(); 531 532 /// @brief 获取顶级页表的物理地址 533 unsafe fn table(table_kind: PageTableKind) -> PhysAddr; 534 535 /// @brief 设置顶级页表的物理地址到处理器中 536 unsafe fn set_table(table_kind: PageTableKind, table: PhysAddr); 537 538 /// @brief 将物理地址转换为虚拟地址. 539 /// 540 /// @param phys 物理地址 541 /// 542 /// @return 转换后的虚拟地址。如果转换失败,返回None 543 #[inline(always)] 544 unsafe fn phys_2_virt(phys: PhysAddr) -> Option<VirtAddr> { 545 if let Some(vaddr) = phys.data().checked_add(Self::PHYS_OFFSET) { 546 return Some(VirtAddr::new(vaddr)); 547 } else { 548 return None; 549 } 550 } 551 552 /// 将虚拟地址转换为物理地址 553 /// 554 /// ## 参数 555 /// 556 /// - `virt` 虚拟地址 557 /// 558 /// ## 返回值 559 /// 560 /// 转换后的物理地址。如果转换失败,返回None 561 #[inline(always)] 562 unsafe fn virt_2_phys(virt: VirtAddr) -> Option<PhysAddr> { 563 if let Some(paddr) = virt.data().checked_sub(Self::PHYS_OFFSET) { 564 return Some(PhysAddr::new(paddr)); 565 } else { 566 return None; 567 } 568 } 569 570 /// @brief 判断指定的虚拟地址是否正确(符合规范) 571 fn virt_is_valid(virt: VirtAddr) -> bool; 572 573 /// 获取内存管理初始化时,创建的第一个内核页表的地址 574 fn initial_page_table() -> PhysAddr; 575 576 /// 初始化新的usermapper,为用户进程创建页表 577 fn setup_new_usermapper() -> Result<UserMapper, SystemError>; 578 579 /// 创建页表项 580 /// 581 /// 这是一个低阶api,用于根据物理地址以及指定好的pageflags,创建页表项 582 /// 583 /// ## 参数 584 /// 585 /// - `paddr` 物理地址 586 /// - `page_flags` 页表项的flags 587 /// 588 /// ## 返回值 589 /// 590 /// 页表项的值 591 fn make_entry(paddr: PhysAddr, page_flags: usize) -> usize; 592 } 593 594 /// @brief 虚拟地址范围 595 /// 该结构体用于表示一个虚拟地址范围,包括起始地址与大小 596 /// 597 /// 请注意与VMA进行区分,该结构体被VMA所包含 598 #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 599 pub struct VirtRegion { 600 start: VirtAddr, 601 size: usize, 602 } 603 604 #[allow(dead_code)] 605 impl VirtRegion { 606 /// # 创建一个新的虚拟地址范围 607 pub fn new(start: VirtAddr, size: usize) -> Self { 608 VirtRegion { start, size } 609 } 610 611 /// 获取虚拟地址范围的起始地址 612 #[inline(always)] 613 pub fn start(&self) -> VirtAddr { 614 self.start 615 } 616 617 /// 获取虚拟地址范围的截止地址(不包括返回的地址) 618 #[inline(always)] 619 pub fn end(&self) -> VirtAddr { 620 return self.start().add(self.size); 621 } 622 623 /// # Create a new VirtRegion from a range [start, end) 624 /// 625 /// If end <= start, return None 626 pub fn between(start: VirtAddr, end: VirtAddr) -> Option<Self> { 627 if unlikely(end.data() <= start.data()) { 628 return None; 629 } 630 let size = end.data() - start.data(); 631 return Some(VirtRegion::new(start, size)); 632 } 633 634 /// # 取两个虚拟地址范围的交集 635 /// 636 /// 如果两个虚拟地址范围没有交集,返回None 637 pub fn intersect(&self, other: &VirtRegion) -> Option<VirtRegion> { 638 let start = self.start.max(other.start); 639 let end = self.end().min(other.end()); 640 return VirtRegion::between(start, end); 641 } 642 643 /// 设置虚拟地址范围的起始地址 644 #[inline(always)] 645 pub fn set_start(&mut self, start: VirtAddr) { 646 self.start = start; 647 } 648 649 #[inline(always)] 650 pub fn size(&self) -> usize { 651 self.size 652 } 653 654 /// 设置虚拟地址范围的大小 655 #[inline(always)] 656 pub fn set_size(&mut self, size: usize) { 657 self.size = size; 658 } 659 660 /// 判断虚拟地址范围是否为空 661 #[inline(always)] 662 pub fn is_empty(&self) -> bool { 663 self.size == 0 664 } 665 666 /// 将虚拟地址区域的大小向上对齐到页大小 667 #[inline(always)] 668 pub fn round_up_size_to_page(self) -> Self { 669 return VirtRegion::new(self.start, round_up_to_page_size(self.size)); 670 } 671 672 /// 判断两个虚拟地址范围是否由于具有交集而导致冲突 673 #[inline(always)] 674 pub fn collide(&self, other: &VirtRegion) -> bool { 675 return self.intersect(other).is_some(); 676 } 677 678 pub fn iter_pages(&self) -> VirtPageFrameIter { 679 return VirtPageFrame::iter_range( 680 VirtPageFrame::new(self.start), 681 VirtPageFrame::new(self.end()), 682 ); 683 } 684 685 /// 获取[self.start(), region.start())的虚拟地址范围 686 /// 687 /// 如果self.start() >= region.start(),返回None 688 pub fn before(self, region: &VirtRegion) -> Option<Self> { 689 return Self::between(self.start(), region.start()); 690 } 691 692 /// 获取[region.end(),self.end())的虚拟地址范围 693 /// 694 /// 如果 self.end() >= region.end() ,返回None 695 pub fn after(self, region: &VirtRegion) -> Option<Self> { 696 // if self.end() > region.end() none 697 return Self::between(region.end(), self.end()); 698 } 699 700 /// 把当前虚拟地址范围内的某个虚拟地址,转换为另一个虚拟地址范围内的虚拟地址 701 /// 702 /// 如果vaddr不在当前虚拟地址范围内,返回None 703 /// 704 /// 如果vaddr在当前虚拟地址范围内,返回vaddr在new_base中的虚拟地址 705 pub fn rebase(self, vaddr: VirtAddr, new_base: &VirtRegion) -> Option<VirtAddr> { 706 if !self.contains(vaddr) { 707 return None; 708 } 709 let offset = vaddr.data() - self.start().data(); 710 let new_start = new_base.start().data() + offset; 711 return Some(VirtAddr::new(new_start)); 712 } 713 714 /// 判断虚拟地址范围是否包含指定的虚拟地址 715 pub fn contains(&self, addr: VirtAddr) -> bool { 716 return self.start() <= addr && addr < self.end(); 717 } 718 719 /// 创建当前虚拟地址范围的页面迭代器 720 pub fn pages(&self) -> VirtPageFrameIter { 721 return VirtPageFrame::iter_range( 722 VirtPageFrame::new(self.start()), 723 VirtPageFrame::new(self.end()), 724 ); 725 } 726 } 727 728 impl PartialOrd for VirtRegion { 729 fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { 730 Some(self.cmp(other)) 731 } 732 } 733 734 impl Ord for VirtRegion { 735 fn cmp(&self, other: &Self) -> cmp::Ordering { 736 return self.start.cmp(&other.start); 737 } 738 } 739 740 /// ## 判断虚拟地址是否超出了用户空间 741 /// 742 /// 如果虚拟地址超出了用户空间,返回Err(SystemError::EFAULT). 743 /// 如果end < start,返回Err(SystemError::EOVERFLOW) 744 /// 745 /// 否则返回Ok(()) 746 pub fn verify_area(addr: VirtAddr, size: usize) -> Result<(), SystemError> { 747 let end = addr.add(size); 748 if unlikely(end.data() < addr.data()) { 749 return Err(SystemError::EOVERFLOW); 750 } 751 752 if !addr.check_user() || !end.check_user() { 753 return Err(SystemError::EFAULT); 754 } 755 756 return Ok(()); 757 } 758