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