140fe15e0SLoGin use core::{ 240fe15e0SLoGin fmt::{self, Debug, Error, Formatter}, 340fe15e0SLoGin marker::PhantomData, 440fe15e0SLoGin mem, 540fe15e0SLoGin ops::Add, 640fe15e0SLoGin sync::atomic::{compiler_fence, Ordering}, 740fe15e0SLoGin }; 840fe15e0SLoGin 9*56cc4dbeSJomo use alloc::sync::Arc; 10*56cc4dbeSJomo use hashbrown::{HashMap, HashSet}; 11*56cc4dbeSJomo 1240fe15e0SLoGin use crate::{ 1340fe15e0SLoGin arch::{interrupt::ipi::send_ipi, MMArch}, 1440fe15e0SLoGin exception::ipi::{IpiKind, IpiTarget}, 1540fe15e0SLoGin kerror, kwarn, 16*56cc4dbeSJomo libs::spinlock::{SpinLock, SpinLockGuard}, 1740fe15e0SLoGin }; 1840fe15e0SLoGin 1940fe15e0SLoGin use super::{ 20*56cc4dbeSJomo allocator::page_frame::FrameAllocator, syscall::ProtFlags, ucontext::LockedVMA, 21*56cc4dbeSJomo MemoryManagementArch, PageTableKind, PhysAddr, VirtAddr, 2240fe15e0SLoGin }; 2340fe15e0SLoGin 24*56cc4dbeSJomo /// 全局物理页信息管理器 25*56cc4dbeSJomo pub static mut PAGE_MANAGER: Option<SpinLock<PageManager>> = None; 26*56cc4dbeSJomo 27*56cc4dbeSJomo /// 初始化PAGE_MANAGER 28*56cc4dbeSJomo pub fn page_manager_init() { 29*56cc4dbeSJomo kinfo!("page_manager_init"); 30*56cc4dbeSJomo let page_manager = SpinLock::new(PageManager::new()); 31*56cc4dbeSJomo 32*56cc4dbeSJomo compiler_fence(Ordering::SeqCst); 33*56cc4dbeSJomo unsafe { PAGE_MANAGER = Some(page_manager) }; 34*56cc4dbeSJomo compiler_fence(Ordering::SeqCst); 35*56cc4dbeSJomo 36*56cc4dbeSJomo kinfo!("page_manager_init done"); 37*56cc4dbeSJomo } 38*56cc4dbeSJomo 39*56cc4dbeSJomo pub fn page_manager_lock_irasave() -> SpinLockGuard<'static, PageManager> { 40*56cc4dbeSJomo unsafe { PAGE_MANAGER.as_ref().unwrap().lock_irqsave() } 41*56cc4dbeSJomo } 42*56cc4dbeSJomo 43*56cc4dbeSJomo // 物理页管理器 44*56cc4dbeSJomo pub struct PageManager { 45*56cc4dbeSJomo phys2page: HashMap<PhysAddr, Page>, 46*56cc4dbeSJomo } 47*56cc4dbeSJomo 48*56cc4dbeSJomo impl PageManager { 49*56cc4dbeSJomo pub fn new() -> Self { 50*56cc4dbeSJomo Self { 51*56cc4dbeSJomo phys2page: HashMap::new(), 52*56cc4dbeSJomo } 53*56cc4dbeSJomo } 54*56cc4dbeSJomo 55*56cc4dbeSJomo pub fn get_mut(&mut self, paddr: &PhysAddr) -> &mut Page { 56*56cc4dbeSJomo self.phys2page.get_mut(paddr).unwrap() 57*56cc4dbeSJomo } 58*56cc4dbeSJomo 59*56cc4dbeSJomo pub fn insert(&mut self, paddr: PhysAddr, page: Page) { 60*56cc4dbeSJomo self.phys2page.insert(paddr, page); 61*56cc4dbeSJomo } 62*56cc4dbeSJomo 63*56cc4dbeSJomo pub fn remove_page(&mut self, paddr: &PhysAddr) { 64*56cc4dbeSJomo self.phys2page.remove(paddr); 65*56cc4dbeSJomo } 66*56cc4dbeSJomo } 67*56cc4dbeSJomo 68*56cc4dbeSJomo /// 物理页面信息 69*56cc4dbeSJomo pub struct Page { 70*56cc4dbeSJomo /// 映射计数 71*56cc4dbeSJomo map_count: usize, 72*56cc4dbeSJomo /// 是否为共享页 73*56cc4dbeSJomo shared: bool, 74*56cc4dbeSJomo /// 映射到当前page的VMA 75*56cc4dbeSJomo anon_vma: HashSet<Arc<LockedVMA>>, 76*56cc4dbeSJomo } 77*56cc4dbeSJomo 78*56cc4dbeSJomo impl Page { 79*56cc4dbeSJomo pub fn new(shared: bool) -> Self { 80*56cc4dbeSJomo Self { 81*56cc4dbeSJomo map_count: 0, 82*56cc4dbeSJomo shared, 83*56cc4dbeSJomo anon_vma: HashSet::new(), 84*56cc4dbeSJomo } 85*56cc4dbeSJomo } 86*56cc4dbeSJomo 87*56cc4dbeSJomo /// 将vma加入anon_vma 88*56cc4dbeSJomo pub fn insert_vma(&mut self, vma: Arc<LockedVMA>) { 89*56cc4dbeSJomo self.anon_vma.insert(vma); 90*56cc4dbeSJomo self.map_count += 1; 91*56cc4dbeSJomo } 92*56cc4dbeSJomo 93*56cc4dbeSJomo /// 将vma从anon_vma中删去 94*56cc4dbeSJomo pub fn remove_vma(&mut self, vma: &LockedVMA) { 95*56cc4dbeSJomo self.anon_vma.remove(vma); 96*56cc4dbeSJomo self.map_count -= 1; 97*56cc4dbeSJomo } 98*56cc4dbeSJomo 99*56cc4dbeSJomo /// 判断当前物理页是否能被回 100*56cc4dbeSJomo pub fn can_deallocate(&self) -> bool { 101*56cc4dbeSJomo self.map_count == 0 && !self.shared 102*56cc4dbeSJomo } 103*56cc4dbeSJomo } 104*56cc4dbeSJomo 10540fe15e0SLoGin #[derive(Debug)] 10640fe15e0SLoGin pub struct PageTable<Arch> { 10740fe15e0SLoGin /// 当前页表表示的虚拟地址空间的起始地址 10840fe15e0SLoGin base: VirtAddr, 10940fe15e0SLoGin /// 当前页表所在的物理地址 11040fe15e0SLoGin phys: PhysAddr, 11140fe15e0SLoGin /// 当前页表的层级(请注意,最顶级页表的level为[Arch::PAGE_LEVELS - 1]) 11240fe15e0SLoGin level: usize, 11340fe15e0SLoGin phantom: PhantomData<Arch>, 11440fe15e0SLoGin } 11540fe15e0SLoGin 11640fe15e0SLoGin #[allow(dead_code)] 11740fe15e0SLoGin impl<Arch: MemoryManagementArch> PageTable<Arch> { 11840fe15e0SLoGin pub unsafe fn new(base: VirtAddr, phys: PhysAddr, level: usize) -> Self { 11940fe15e0SLoGin Self { 12040fe15e0SLoGin base, 12140fe15e0SLoGin phys, 12240fe15e0SLoGin level, 12340fe15e0SLoGin phantom: PhantomData, 12440fe15e0SLoGin } 12540fe15e0SLoGin } 12640fe15e0SLoGin 12740fe15e0SLoGin /// 获取顶级页表 12840fe15e0SLoGin /// 12940fe15e0SLoGin /// ## 参数 13040fe15e0SLoGin /// 13140fe15e0SLoGin /// - table_kind 页表类型 13240fe15e0SLoGin /// 13340fe15e0SLoGin /// ## 返回值 13440fe15e0SLoGin /// 13540fe15e0SLoGin /// 返回顶级页表 13640fe15e0SLoGin pub unsafe fn top_level_table(table_kind: PageTableKind) -> Self { 13740fe15e0SLoGin return Self::new( 13840fe15e0SLoGin VirtAddr::new(0), 13940fe15e0SLoGin Arch::table(table_kind), 14040fe15e0SLoGin Arch::PAGE_LEVELS - 1, 14140fe15e0SLoGin ); 14240fe15e0SLoGin } 14340fe15e0SLoGin 14440fe15e0SLoGin /// 获取当前页表的物理地址 14540fe15e0SLoGin #[inline(always)] 14640fe15e0SLoGin pub fn phys(&self) -> PhysAddr { 14740fe15e0SLoGin self.phys 14840fe15e0SLoGin } 14940fe15e0SLoGin 15040fe15e0SLoGin /// 当前页表表示的虚拟地址空间的起始地址 15140fe15e0SLoGin #[inline(always)] 15240fe15e0SLoGin pub fn base(&self) -> VirtAddr { 15340fe15e0SLoGin self.base 15440fe15e0SLoGin } 15540fe15e0SLoGin 15640fe15e0SLoGin /// 获取当前页表的层级 15740fe15e0SLoGin #[inline(always)] 15840fe15e0SLoGin pub fn level(&self) -> usize { 15940fe15e0SLoGin self.level 16040fe15e0SLoGin } 16140fe15e0SLoGin 16240fe15e0SLoGin /// 获取当前页表自身所在的虚拟地址 16340fe15e0SLoGin #[inline(always)] 16440fe15e0SLoGin pub unsafe fn virt(&self) -> VirtAddr { 16540fe15e0SLoGin return Arch::phys_2_virt(self.phys).unwrap(); 16640fe15e0SLoGin } 16740fe15e0SLoGin 16840fe15e0SLoGin /// 获取第i个页表项所表示的虚拟内存空间的起始地址 16940fe15e0SLoGin pub fn entry_base(&self, i: usize) -> Option<VirtAddr> { 17040fe15e0SLoGin if i < Arch::PAGE_ENTRY_NUM { 17140fe15e0SLoGin let shift = self.level * Arch::PAGE_ENTRY_SHIFT + Arch::PAGE_SHIFT; 17240fe15e0SLoGin return Some(self.base.add(i << shift)); 17340fe15e0SLoGin } else { 17440fe15e0SLoGin return None; 17540fe15e0SLoGin } 17640fe15e0SLoGin } 17740fe15e0SLoGin 17840fe15e0SLoGin /// 获取当前页表的第i个页表项所在的虚拟地址(注意与entry_base进行区分) 17940fe15e0SLoGin pub unsafe fn entry_virt(&self, i: usize) -> Option<VirtAddr> { 18040fe15e0SLoGin if i < Arch::PAGE_ENTRY_NUM { 18140fe15e0SLoGin return Some(self.virt().add(i * Arch::PAGE_ENTRY_SIZE)); 18240fe15e0SLoGin } else { 18340fe15e0SLoGin return None; 18440fe15e0SLoGin } 18540fe15e0SLoGin } 18640fe15e0SLoGin 18740fe15e0SLoGin /// 获取当前页表的第i个页表项 18840fe15e0SLoGin pub unsafe fn entry(&self, i: usize) -> Option<PageEntry<Arch>> { 18940fe15e0SLoGin let entry_virt = self.entry_virt(i)?; 1907a29d4fcSLoGin return Some(PageEntry::from_usize(Arch::read::<usize>(entry_virt))); 19140fe15e0SLoGin } 19240fe15e0SLoGin 19340fe15e0SLoGin /// 设置当前页表的第i个页表项 19440fe15e0SLoGin pub unsafe fn set_entry(&self, i: usize, entry: PageEntry<Arch>) -> Option<()> { 19540fe15e0SLoGin let entry_virt = self.entry_virt(i)?; 19640fe15e0SLoGin Arch::write::<usize>(entry_virt, entry.data()); 19740fe15e0SLoGin return Some(()); 19840fe15e0SLoGin } 19940fe15e0SLoGin 20040fe15e0SLoGin /// 判断当前页表的第i个页表项是否已经填写了值 20140fe15e0SLoGin /// 20240fe15e0SLoGin /// ## 参数 20340fe15e0SLoGin /// - Some(true) 如果已经填写了值 20440fe15e0SLoGin /// - Some(false) 如果未填写值 20540fe15e0SLoGin /// - None 如果i超出了页表项的范围 20640fe15e0SLoGin pub fn entry_mapped(&self, i: usize) -> Option<bool> { 20740fe15e0SLoGin let etv = unsafe { self.entry_virt(i) }?; 20840fe15e0SLoGin if unsafe { Arch::read::<usize>(etv) } != 0 { 20940fe15e0SLoGin return Some(true); 21040fe15e0SLoGin } else { 21140fe15e0SLoGin return Some(false); 21240fe15e0SLoGin } 21340fe15e0SLoGin } 21440fe15e0SLoGin 21540fe15e0SLoGin /// 根据虚拟地址,获取对应的页表项在页表中的下标 21640fe15e0SLoGin /// 21740fe15e0SLoGin /// ## 参数 21840fe15e0SLoGin /// 21940fe15e0SLoGin /// - addr: 虚拟地址 22040fe15e0SLoGin /// 22140fe15e0SLoGin /// ## 返回值 22240fe15e0SLoGin /// 22340fe15e0SLoGin /// 页表项在页表中的下标。如果addr不在当前页表所表示的虚拟地址空间中,则返回None 22440fe15e0SLoGin pub unsafe fn index_of(&self, addr: VirtAddr) -> Option<usize> { 22540fe15e0SLoGin let addr = VirtAddr::new(addr.data() & Arch::PAGE_ADDRESS_MASK); 22640fe15e0SLoGin let shift = self.level * Arch::PAGE_ENTRY_SHIFT + Arch::PAGE_SHIFT; 22740fe15e0SLoGin 22840fe15e0SLoGin let mask = (MMArch::PAGE_ENTRY_NUM << shift) - 1; 22940fe15e0SLoGin if addr < self.base || addr >= self.base.add(mask) { 23040fe15e0SLoGin return None; 23140fe15e0SLoGin } else { 23240fe15e0SLoGin return Some((addr.data() >> shift) & MMArch::PAGE_ENTRY_MASK); 23340fe15e0SLoGin } 23440fe15e0SLoGin } 23540fe15e0SLoGin 23640fe15e0SLoGin /// 获取第i个页表项指向的下一级页表 23740fe15e0SLoGin pub unsafe fn next_level_table(&self, index: usize) -> Option<Self> { 23840fe15e0SLoGin if self.level == 0 { 23940fe15e0SLoGin return None; 24040fe15e0SLoGin } 24140fe15e0SLoGin 24240fe15e0SLoGin // 返回下一级页表 24340fe15e0SLoGin return Some(PageTable::new( 24440fe15e0SLoGin self.entry_base(index)?, 24540fe15e0SLoGin self.entry(index)?.address().ok()?, 24640fe15e0SLoGin self.level - 1, 24740fe15e0SLoGin )); 24840fe15e0SLoGin } 24940fe15e0SLoGin } 25040fe15e0SLoGin 25140fe15e0SLoGin /// 页表项 25240fe15e0SLoGin #[derive(Copy, Clone)] 25340fe15e0SLoGin pub struct PageEntry<Arch> { 25440fe15e0SLoGin data: usize, 25540fe15e0SLoGin phantom: PhantomData<Arch>, 25640fe15e0SLoGin } 25740fe15e0SLoGin 25840fe15e0SLoGin impl<Arch> Debug for PageEntry<Arch> { 25940fe15e0SLoGin fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error> { 26040fe15e0SLoGin f.write_fmt(format_args!("PageEntry({:#x})", self.data)) 26140fe15e0SLoGin } 26240fe15e0SLoGin } 26340fe15e0SLoGin 26440fe15e0SLoGin impl<Arch: MemoryManagementArch> PageEntry<Arch> { 26540fe15e0SLoGin #[inline(always)] 2667a29d4fcSLoGin pub fn new(paddr: PhysAddr, flags: PageFlags<Arch>) -> Self { 2677a29d4fcSLoGin Self { 2687a29d4fcSLoGin data: MMArch::make_entry(paddr, flags.data()), 2697a29d4fcSLoGin phantom: PhantomData, 2707a29d4fcSLoGin } 2717a29d4fcSLoGin } 2727a29d4fcSLoGin #[inline(always)] 2737a29d4fcSLoGin pub fn from_usize(data: usize) -> Self { 27440fe15e0SLoGin Self { 27540fe15e0SLoGin data, 27640fe15e0SLoGin phantom: PhantomData, 27740fe15e0SLoGin } 27840fe15e0SLoGin } 27940fe15e0SLoGin 28040fe15e0SLoGin #[inline(always)] 28140fe15e0SLoGin pub fn data(&self) -> usize { 28240fe15e0SLoGin self.data 28340fe15e0SLoGin } 28440fe15e0SLoGin 28540fe15e0SLoGin /// 获取当前页表项指向的物理地址 28640fe15e0SLoGin /// 28740fe15e0SLoGin /// ## 返回值 28840fe15e0SLoGin /// 28940fe15e0SLoGin /// - Ok(PhysAddr) 如果当前页面存在于物理内存中, 返回物理地址 29040fe15e0SLoGin /// - Err(PhysAddr) 如果当前页表项不存在, 返回物理地址 29140fe15e0SLoGin #[inline(always)] 29240fe15e0SLoGin pub fn address(&self) -> Result<PhysAddr, PhysAddr> { 2937a29d4fcSLoGin let paddr: PhysAddr = { 2947a29d4fcSLoGin #[cfg(target_arch = "x86_64")] 2957a29d4fcSLoGin { 2967a29d4fcSLoGin PhysAddr::new(self.data & Arch::PAGE_ADDRESS_MASK) 2977a29d4fcSLoGin } 2987a29d4fcSLoGin 2997a29d4fcSLoGin #[cfg(target_arch = "riscv64")] 3007a29d4fcSLoGin { 301453452ccSLoGin let ppn = ((self.data & (!((1 << 10) - 1))) >> 10) & ((1 << 54) - 1); 3027a29d4fcSLoGin super::allocator::page_frame::PhysPageFrame::from_ppn(ppn).phys_address() 3037a29d4fcSLoGin } 3047a29d4fcSLoGin }; 30540fe15e0SLoGin 30640fe15e0SLoGin if self.present() { 30740fe15e0SLoGin Ok(paddr) 30840fe15e0SLoGin } else { 30940fe15e0SLoGin Err(paddr) 31040fe15e0SLoGin } 31140fe15e0SLoGin } 31240fe15e0SLoGin 31340fe15e0SLoGin #[inline(always)] 31440fe15e0SLoGin pub fn flags(&self) -> PageFlags<Arch> { 31540fe15e0SLoGin unsafe { PageFlags::from_data(self.data & Arch::ENTRY_FLAGS_MASK) } 31640fe15e0SLoGin } 31740fe15e0SLoGin 31840fe15e0SLoGin #[inline(always)] 31940fe15e0SLoGin pub fn set_flags(&mut self, flags: PageFlags<Arch>) { 32040fe15e0SLoGin self.data = (self.data & !Arch::ENTRY_FLAGS_MASK) | flags.data(); 32140fe15e0SLoGin } 32240fe15e0SLoGin 32340fe15e0SLoGin #[inline(always)] 32440fe15e0SLoGin pub fn present(&self) -> bool { 32540fe15e0SLoGin return self.data & Arch::ENTRY_FLAG_PRESENT != 0; 32640fe15e0SLoGin } 32740fe15e0SLoGin } 32840fe15e0SLoGin 32940fe15e0SLoGin /// 页表项的标志位 33040fe15e0SLoGin #[derive(Copy, Clone, Hash)] 33140fe15e0SLoGin pub struct PageFlags<Arch> { 33240fe15e0SLoGin data: usize, 33340fe15e0SLoGin phantom: PhantomData<Arch>, 33440fe15e0SLoGin } 33540fe15e0SLoGin 33640fe15e0SLoGin #[allow(dead_code)] 33740fe15e0SLoGin impl<Arch: MemoryManagementArch> PageFlags<Arch> { 33840fe15e0SLoGin #[inline(always)] 33940fe15e0SLoGin pub fn new() -> Self { 34040fe15e0SLoGin let mut r = unsafe { 34140fe15e0SLoGin Self::from_data( 34240fe15e0SLoGin Arch::ENTRY_FLAG_DEFAULT_PAGE 34340fe15e0SLoGin | Arch::ENTRY_FLAG_READONLY 34440fe15e0SLoGin | Arch::ENTRY_FLAG_NO_EXEC, 34540fe15e0SLoGin ) 34640fe15e0SLoGin }; 34740fe15e0SLoGin 34840fe15e0SLoGin #[cfg(target_arch = "x86_64")] 34940fe15e0SLoGin { 35040fe15e0SLoGin if crate::arch::mm::X86_64MMArch::is_xd_reserved() { 35140fe15e0SLoGin r = r.set_execute(true); 35240fe15e0SLoGin } 35340fe15e0SLoGin } 35440fe15e0SLoGin 35540fe15e0SLoGin return r; 35640fe15e0SLoGin } 35740fe15e0SLoGin 35840fe15e0SLoGin /// 根据ProtFlags生成PageFlags 35940fe15e0SLoGin /// 36040fe15e0SLoGin /// ## 参数 36140fe15e0SLoGin /// 36240fe15e0SLoGin /// - prot_flags: 页的保护标志 36340fe15e0SLoGin /// - user: 用户空间是否可访问 36440fe15e0SLoGin pub fn from_prot_flags(prot_flags: ProtFlags, user: bool) -> PageFlags<Arch> { 36540fe15e0SLoGin let flags: PageFlags<Arch> = PageFlags::new() 36640fe15e0SLoGin .set_user(user) 36740fe15e0SLoGin .set_execute(prot_flags.contains(ProtFlags::PROT_EXEC)) 36840fe15e0SLoGin .set_write(prot_flags.contains(ProtFlags::PROT_WRITE)); 36940fe15e0SLoGin 37040fe15e0SLoGin return flags; 37140fe15e0SLoGin } 37240fe15e0SLoGin 37340fe15e0SLoGin #[inline(always)] 37440fe15e0SLoGin pub fn data(&self) -> usize { 37540fe15e0SLoGin self.data 37640fe15e0SLoGin } 37740fe15e0SLoGin 37840fe15e0SLoGin #[inline(always)] 37940fe15e0SLoGin pub const unsafe fn from_data(data: usize) -> Self { 38040fe15e0SLoGin return Self { 381b5b571e0SLoGin data, 38240fe15e0SLoGin phantom: PhantomData, 38340fe15e0SLoGin }; 38440fe15e0SLoGin } 38540fe15e0SLoGin 38640fe15e0SLoGin /// 为新页表的页表项设置默认值 38740fe15e0SLoGin /// 38840fe15e0SLoGin /// 默认值为: 38940fe15e0SLoGin /// - present 39040fe15e0SLoGin /// - read only 39140fe15e0SLoGin /// - kernel space 39240fe15e0SLoGin /// - no exec 39340fe15e0SLoGin #[inline(always)] 39440fe15e0SLoGin pub fn new_page_table(user: bool) -> Self { 39540fe15e0SLoGin return unsafe { 3967a29d4fcSLoGin let r = { 3977a29d4fcSLoGin #[cfg(target_arch = "x86_64")] 3987a29d4fcSLoGin { 3997a29d4fcSLoGin Self::from_data(Arch::ENTRY_FLAG_DEFAULT_TABLE | Arch::ENTRY_FLAG_READWRITE) 4007a29d4fcSLoGin } 4017a29d4fcSLoGin 4027a29d4fcSLoGin #[cfg(target_arch = "riscv64")] 4037a29d4fcSLoGin { 4047a29d4fcSLoGin // riscv64指向下一级页表的页表项,不应设置R/W/X权限位 4057a29d4fcSLoGin Self::from_data(Arch::ENTRY_FLAG_DEFAULT_TABLE) 4067a29d4fcSLoGin } 4077a29d4fcSLoGin }; 40840fe15e0SLoGin if user { 40940fe15e0SLoGin r.set_user(true) 41040fe15e0SLoGin } else { 41140fe15e0SLoGin r 41240fe15e0SLoGin } 41340fe15e0SLoGin }; 41440fe15e0SLoGin } 41540fe15e0SLoGin 41640fe15e0SLoGin /// 取得当前页表项的所有权,更新当前页表项的标志位,并返回更新后的页表项。 41740fe15e0SLoGin /// 41840fe15e0SLoGin /// ## 参数 41940fe15e0SLoGin /// - flag 要更新的标志位的值 42040fe15e0SLoGin /// - value 如果为true,那么将flag对应的位设置为1,否则设置为0 42140fe15e0SLoGin /// 42240fe15e0SLoGin /// ## 返回值 42340fe15e0SLoGin /// 42440fe15e0SLoGin /// 更新后的页表项 42540fe15e0SLoGin #[inline(always)] 42640fe15e0SLoGin #[must_use] 42740fe15e0SLoGin pub fn update_flags(mut self, flag: usize, value: bool) -> Self { 42840fe15e0SLoGin if value { 42940fe15e0SLoGin self.data |= flag; 43040fe15e0SLoGin } else { 43140fe15e0SLoGin self.data &= !flag; 43240fe15e0SLoGin } 43340fe15e0SLoGin return self; 43440fe15e0SLoGin } 43540fe15e0SLoGin 43640fe15e0SLoGin /// 判断当前页表项是否存在指定的flag(只有全部flag都存在才返回true) 43740fe15e0SLoGin #[inline(always)] 43840fe15e0SLoGin pub fn has_flag(&self, flag: usize) -> bool { 43940fe15e0SLoGin return self.data & flag == flag; 44040fe15e0SLoGin } 44140fe15e0SLoGin 44240fe15e0SLoGin #[inline(always)] 44340fe15e0SLoGin pub fn present(&self) -> bool { 44440fe15e0SLoGin return self.has_flag(Arch::ENTRY_FLAG_PRESENT); 44540fe15e0SLoGin } 44640fe15e0SLoGin 44740fe15e0SLoGin /// 设置当前页表项的权限 44840fe15e0SLoGin /// 44940fe15e0SLoGin /// @param value 如果为true,那么将当前页表项的权限设置为用户态可访问 45040fe15e0SLoGin #[must_use] 45140fe15e0SLoGin #[inline(always)] 45240fe15e0SLoGin pub fn set_user(self, value: bool) -> Self { 45340fe15e0SLoGin return self.update_flags(Arch::ENTRY_FLAG_USER, value); 45440fe15e0SLoGin } 45540fe15e0SLoGin 45640fe15e0SLoGin /// 用户态是否可以访问当前页表项 45740fe15e0SLoGin #[inline(always)] 45840fe15e0SLoGin pub fn has_user(&self) -> bool { 45940fe15e0SLoGin return self.has_flag(Arch::ENTRY_FLAG_USER); 46040fe15e0SLoGin } 46140fe15e0SLoGin 46240fe15e0SLoGin /// 设置当前页表项的可写性, 如果为true,那么将当前页表项的权限设置为可写, 否则设置为只读 46340fe15e0SLoGin /// 46440fe15e0SLoGin /// ## 返回值 46540fe15e0SLoGin /// 46640fe15e0SLoGin /// 更新后的页表项. 46740fe15e0SLoGin /// 46840fe15e0SLoGin /// **请注意,**本函数会取得当前页表项的所有权,因此返回的页表项不是原来的页表项 46940fe15e0SLoGin #[must_use] 47040fe15e0SLoGin #[inline(always)] 47140fe15e0SLoGin pub fn set_write(self, value: bool) -> Self { 4727a29d4fcSLoGin #[cfg(target_arch = "x86_64")] 4737a29d4fcSLoGin { 47440fe15e0SLoGin // 有的架构同时具有可写和不可写的标志位,因此需要同时更新 47540fe15e0SLoGin return self 47640fe15e0SLoGin .update_flags(Arch::ENTRY_FLAG_READONLY, !value) 47740fe15e0SLoGin .update_flags(Arch::ENTRY_FLAG_READWRITE, value); 47840fe15e0SLoGin } 47940fe15e0SLoGin 4807a29d4fcSLoGin #[cfg(target_arch = "riscv64")] 4817a29d4fcSLoGin { 4827a29d4fcSLoGin if value { 4837a29d4fcSLoGin return self.update_flags(Arch::ENTRY_FLAG_READWRITE, true); 4847a29d4fcSLoGin } else { 4857a29d4fcSLoGin return self.update_flags(Arch::ENTRY_FLAG_READONLY, true); 4867a29d4fcSLoGin } 4877a29d4fcSLoGin } 4887a29d4fcSLoGin } 4897a29d4fcSLoGin 49040fe15e0SLoGin /// 当前页表项是否可写 49140fe15e0SLoGin #[inline(always)] 49240fe15e0SLoGin pub fn has_write(&self) -> bool { 49340fe15e0SLoGin // 有的架构同时具有可写和不可写的标志位,因此需要同时判断 49440fe15e0SLoGin return self.data & (Arch::ENTRY_FLAG_READWRITE | Arch::ENTRY_FLAG_READONLY) 49540fe15e0SLoGin == Arch::ENTRY_FLAG_READWRITE; 49640fe15e0SLoGin } 49740fe15e0SLoGin 49840fe15e0SLoGin /// 设置当前页表项的可执行性, 如果为true,那么将当前页表项的权限设置为可执行, 否则设置为不可执行 49940fe15e0SLoGin #[must_use] 50040fe15e0SLoGin #[inline(always)] 50140fe15e0SLoGin pub fn set_execute(self, mut value: bool) -> Self { 50240fe15e0SLoGin #[cfg(target_arch = "x86_64")] 50340fe15e0SLoGin { 50440fe15e0SLoGin // 如果xd位被保留,那么将可执行性设置为true 50540fe15e0SLoGin if crate::arch::mm::X86_64MMArch::is_xd_reserved() { 50640fe15e0SLoGin value = true; 50740fe15e0SLoGin } 50840fe15e0SLoGin } 50940fe15e0SLoGin 51040fe15e0SLoGin // 有的架构同时具有可执行和不可执行的标志位,因此需要同时更新 51140fe15e0SLoGin return self 51240fe15e0SLoGin .update_flags(Arch::ENTRY_FLAG_NO_EXEC, !value) 51340fe15e0SLoGin .update_flags(Arch::ENTRY_FLAG_EXEC, value); 51440fe15e0SLoGin } 51540fe15e0SLoGin 51640fe15e0SLoGin /// 当前页表项是否可执行 51740fe15e0SLoGin #[inline(always)] 51840fe15e0SLoGin pub fn has_execute(&self) -> bool { 51940fe15e0SLoGin // 有的架构同时具有可执行和不可执行的标志位,因此需要同时判断 52040fe15e0SLoGin return self.data & (Arch::ENTRY_FLAG_EXEC | Arch::ENTRY_FLAG_NO_EXEC) 52140fe15e0SLoGin == Arch::ENTRY_FLAG_EXEC; 52240fe15e0SLoGin } 52340fe15e0SLoGin 52440fe15e0SLoGin /// 设置当前页表项的缓存策略 52540fe15e0SLoGin /// 52640fe15e0SLoGin /// ## 参数 52740fe15e0SLoGin /// 52840fe15e0SLoGin /// - value: 如果为true,那么将当前页表项的缓存策略设置为不缓存。 52940fe15e0SLoGin #[inline(always)] 53040fe15e0SLoGin pub fn set_page_cache_disable(self, value: bool) -> Self { 53140fe15e0SLoGin return self.update_flags(Arch::ENTRY_FLAG_CACHE_DISABLE, value); 53240fe15e0SLoGin } 53340fe15e0SLoGin 53440fe15e0SLoGin /// 获取当前页表项的缓存策略 53540fe15e0SLoGin /// 53640fe15e0SLoGin /// ## 返回值 53740fe15e0SLoGin /// 53840fe15e0SLoGin /// 如果当前页表项的缓存策略为不缓存,那么返回true,否则返回false。 53940fe15e0SLoGin #[inline(always)] 54040fe15e0SLoGin pub fn has_page_cache_disable(&self) -> bool { 54140fe15e0SLoGin return self.has_flag(Arch::ENTRY_FLAG_CACHE_DISABLE); 54240fe15e0SLoGin } 54340fe15e0SLoGin 54440fe15e0SLoGin /// 设置当前页表项的写穿策略 54540fe15e0SLoGin /// 54640fe15e0SLoGin /// ## 参数 54740fe15e0SLoGin /// 54840fe15e0SLoGin /// - value: 如果为true,那么将当前页表项的写穿策略设置为写穿。 54940fe15e0SLoGin #[inline(always)] 55040fe15e0SLoGin pub fn set_page_write_through(self, value: bool) -> Self { 55140fe15e0SLoGin return self.update_flags(Arch::ENTRY_FLAG_WRITE_THROUGH, value); 55240fe15e0SLoGin } 55340fe15e0SLoGin 55440fe15e0SLoGin /// 获取当前页表项的写穿策略 55540fe15e0SLoGin /// 55640fe15e0SLoGin /// ## 返回值 55740fe15e0SLoGin /// 55840fe15e0SLoGin /// 如果当前页表项的写穿策略为写穿,那么返回true,否则返回false。 55940fe15e0SLoGin #[inline(always)] 56040fe15e0SLoGin pub fn has_page_write_through(&self) -> bool { 56140fe15e0SLoGin return self.has_flag(Arch::ENTRY_FLAG_WRITE_THROUGH); 56240fe15e0SLoGin } 56340fe15e0SLoGin 56440fe15e0SLoGin /// MMIO内存的页表项标志 56540fe15e0SLoGin #[inline(always)] 56640fe15e0SLoGin pub fn mmio_flags() -> Self { 56740fe15e0SLoGin return Self::new() 56840fe15e0SLoGin .set_user(false) 56940fe15e0SLoGin .set_write(true) 57040fe15e0SLoGin .set_execute(true) 57140fe15e0SLoGin .set_page_cache_disable(true) 57240fe15e0SLoGin .set_page_write_through(true); 57340fe15e0SLoGin } 57440fe15e0SLoGin } 57540fe15e0SLoGin 57640fe15e0SLoGin impl<Arch: MemoryManagementArch> fmt::Debug for PageFlags<Arch> { 57740fe15e0SLoGin fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 57840fe15e0SLoGin f.debug_struct("PageFlags") 57940fe15e0SLoGin .field("bits", &format_args!("{:#0x}", self.data)) 58040fe15e0SLoGin .field("present", &self.present()) 58140fe15e0SLoGin .field("has_write", &self.has_write()) 58240fe15e0SLoGin .field("has_execute", &self.has_execute()) 58340fe15e0SLoGin .field("has_user", &self.has_user()) 58440fe15e0SLoGin .finish() 58540fe15e0SLoGin } 58640fe15e0SLoGin } 58740fe15e0SLoGin 58840fe15e0SLoGin /// 页表映射器 58940fe15e0SLoGin #[derive(Hash)] 59040fe15e0SLoGin pub struct PageMapper<Arch, F> { 59140fe15e0SLoGin /// 页表类型 59240fe15e0SLoGin table_kind: PageTableKind, 59340fe15e0SLoGin /// 根页表物理地址 59440fe15e0SLoGin table_paddr: PhysAddr, 59540fe15e0SLoGin /// 页分配器 59640fe15e0SLoGin frame_allocator: F, 59740fe15e0SLoGin phantom: PhantomData<fn() -> Arch>, 59840fe15e0SLoGin } 59940fe15e0SLoGin 60040fe15e0SLoGin impl<Arch: MemoryManagementArch, F: FrameAllocator> PageMapper<Arch, F> { 60140fe15e0SLoGin /// 创建新的页面映射器 60240fe15e0SLoGin /// 60340fe15e0SLoGin /// ## 参数 60440fe15e0SLoGin /// - table_kind 页表类型 60540fe15e0SLoGin /// - table_paddr 根页表物理地址 60640fe15e0SLoGin /// - allocator 页分配器 60740fe15e0SLoGin /// 60840fe15e0SLoGin /// ## 返回值 60940fe15e0SLoGin /// 61040fe15e0SLoGin /// 页面映射器 61140fe15e0SLoGin pub unsafe fn new(table_kind: PageTableKind, table_paddr: PhysAddr, allocator: F) -> Self { 61240fe15e0SLoGin return Self { 61340fe15e0SLoGin table_kind, 61440fe15e0SLoGin table_paddr, 61540fe15e0SLoGin frame_allocator: allocator, 61640fe15e0SLoGin phantom: PhantomData, 61740fe15e0SLoGin }; 61840fe15e0SLoGin } 61940fe15e0SLoGin 62040fe15e0SLoGin /// 创建页表,并为这个页表创建页面映射器 62140fe15e0SLoGin pub unsafe fn create(table_kind: PageTableKind, mut allocator: F) -> Option<Self> { 62240fe15e0SLoGin let table_paddr = allocator.allocate_one()?; 62340fe15e0SLoGin // 清空页表 62440fe15e0SLoGin let table_vaddr = Arch::phys_2_virt(table_paddr)?; 62540fe15e0SLoGin Arch::write_bytes(table_vaddr, 0, Arch::PAGE_SIZE); 62640fe15e0SLoGin return Some(Self::new(table_kind, table_paddr, allocator)); 62740fe15e0SLoGin } 62840fe15e0SLoGin 62940fe15e0SLoGin /// 获取当前页表的页面映射器 63040fe15e0SLoGin #[inline(always)] 63140fe15e0SLoGin pub unsafe fn current(table_kind: PageTableKind, allocator: F) -> Self { 63240fe15e0SLoGin let table_paddr = Arch::table(table_kind); 63340fe15e0SLoGin return Self::new(table_kind, table_paddr, allocator); 63440fe15e0SLoGin } 63540fe15e0SLoGin 63640fe15e0SLoGin /// 判断当前页表分配器所属的页表是否是当前页表 63740fe15e0SLoGin #[inline(always)] 63840fe15e0SLoGin pub fn is_current(&self) -> bool { 63940fe15e0SLoGin return unsafe { self.table().phys() == Arch::table(self.table_kind) }; 64040fe15e0SLoGin } 64140fe15e0SLoGin 64240fe15e0SLoGin /// 将当前页表分配器所属的页表设置为当前页表 64340fe15e0SLoGin #[inline(always)] 64440fe15e0SLoGin pub unsafe fn make_current(&self) { 64540fe15e0SLoGin Arch::set_table(self.table_kind, self.table_paddr); 64640fe15e0SLoGin } 64740fe15e0SLoGin 64840fe15e0SLoGin /// 获取当前页表分配器所属的根页表的结构体 64940fe15e0SLoGin #[inline(always)] 65040fe15e0SLoGin pub fn table(&self) -> PageTable<Arch> { 65140fe15e0SLoGin // 由于只能通过new方法创建PageMapper,因此这里假定table_paddr是有效的 65240fe15e0SLoGin return unsafe { 65340fe15e0SLoGin PageTable::new(VirtAddr::new(0), self.table_paddr, Arch::PAGE_LEVELS - 1) 65440fe15e0SLoGin }; 65540fe15e0SLoGin } 65640fe15e0SLoGin 65740fe15e0SLoGin /// 获取当前PageMapper所对应的页分配器实例的引用 65840fe15e0SLoGin #[inline(always)] 65940fe15e0SLoGin #[allow(dead_code)] 66040fe15e0SLoGin pub fn allocator_ref(&self) -> &F { 66140fe15e0SLoGin return &self.frame_allocator; 66240fe15e0SLoGin } 66340fe15e0SLoGin 66440fe15e0SLoGin /// 获取当前PageMapper所对应的页分配器实例的可变引用 66540fe15e0SLoGin #[inline(always)] 66640fe15e0SLoGin pub fn allocator_mut(&mut self) -> &mut F { 66740fe15e0SLoGin return &mut self.frame_allocator; 66840fe15e0SLoGin } 66940fe15e0SLoGin 67040fe15e0SLoGin /// 从当前PageMapper的页分配器中分配一个物理页,并将其映射到指定的虚拟地址 67140fe15e0SLoGin pub unsafe fn map( 67240fe15e0SLoGin &mut self, 67340fe15e0SLoGin virt: VirtAddr, 67440fe15e0SLoGin flags: PageFlags<Arch>, 67540fe15e0SLoGin ) -> Option<PageFlush<Arch>> { 67640fe15e0SLoGin compiler_fence(Ordering::SeqCst); 67740fe15e0SLoGin let phys: PhysAddr = self.frame_allocator.allocate_one()?; 67840fe15e0SLoGin compiler_fence(Ordering::SeqCst); 679*56cc4dbeSJomo 680*56cc4dbeSJomo page_manager_lock_irasave().insert(phys, Page::new(false)); 68140fe15e0SLoGin return self.map_phys(virt, phys, flags); 68240fe15e0SLoGin } 68340fe15e0SLoGin 68440fe15e0SLoGin /// 映射一个物理页到指定的虚拟地址 68540fe15e0SLoGin pub unsafe fn map_phys( 68640fe15e0SLoGin &mut self, 68740fe15e0SLoGin virt: VirtAddr, 68840fe15e0SLoGin phys: PhysAddr, 68940fe15e0SLoGin flags: PageFlags<Arch>, 69040fe15e0SLoGin ) -> Option<PageFlush<Arch>> { 69140fe15e0SLoGin // 验证虚拟地址和物理地址是否对齐 69240fe15e0SLoGin if !(virt.check_aligned(Arch::PAGE_SIZE) && phys.check_aligned(Arch::PAGE_SIZE)) { 69340fe15e0SLoGin kerror!( 69440fe15e0SLoGin "Try to map unaligned page: virt={:?}, phys={:?}", 69540fe15e0SLoGin virt, 69640fe15e0SLoGin phys 69740fe15e0SLoGin ); 69840fe15e0SLoGin return None; 69940fe15e0SLoGin } 7007a29d4fcSLoGin 70140fe15e0SLoGin let virt = VirtAddr::new(virt.data() & (!Arch::PAGE_NEGATIVE_MASK)); 70240fe15e0SLoGin 70340fe15e0SLoGin // TODO: 验证flags是否合法 70440fe15e0SLoGin 70540fe15e0SLoGin // 创建页表项 7067a29d4fcSLoGin let entry = PageEntry::new(phys, flags); 70740fe15e0SLoGin let mut table = self.table(); 70840fe15e0SLoGin loop { 70940fe15e0SLoGin let i = table.index_of(virt)?; 71040fe15e0SLoGin assert!(i < Arch::PAGE_ENTRY_NUM); 71140fe15e0SLoGin if table.level() == 0 { 71240fe15e0SLoGin // todo: 检查是否已经映射 71340fe15e0SLoGin // 现在不检查的原因是,刚刚启动系统时,内核会映射一些页。 714b5b571e0SLoGin if table.entry_mapped(i)? { 71540fe15e0SLoGin kwarn!("Page {:?} already mapped", virt); 71640fe15e0SLoGin } 7177a29d4fcSLoGin 71840fe15e0SLoGin compiler_fence(Ordering::SeqCst); 7197a29d4fcSLoGin 72040fe15e0SLoGin table.set_entry(i, entry); 72140fe15e0SLoGin compiler_fence(Ordering::SeqCst); 72240fe15e0SLoGin return Some(PageFlush::new(virt)); 72340fe15e0SLoGin } else { 72440fe15e0SLoGin let next_table = table.next_level_table(i); 72540fe15e0SLoGin if let Some(next_table) = next_table { 72640fe15e0SLoGin table = next_table; 72740fe15e0SLoGin // kdebug!("Mapping {:?} to next level table...", virt); 72840fe15e0SLoGin } else { 72940fe15e0SLoGin // 分配下一级页表 73040fe15e0SLoGin let frame = self.frame_allocator.allocate_one()?; 7317a29d4fcSLoGin 73240fe15e0SLoGin // 清空这个页帧 73340fe15e0SLoGin MMArch::write_bytes(MMArch::phys_2_virt(frame).unwrap(), 0, MMArch::PAGE_SIZE); 73440fe15e0SLoGin 73540fe15e0SLoGin // 设置页表项的flags 7367a29d4fcSLoGin let flags: PageFlags<Arch> = 73740fe15e0SLoGin PageFlags::new_page_table(virt.kind() == PageTableKind::User); 73840fe15e0SLoGin 73940fe15e0SLoGin // kdebug!("Flags: {:?}", flags); 74040fe15e0SLoGin 74140fe15e0SLoGin // 把新分配的页表映射到当前页表 7427a29d4fcSLoGin table.set_entry(i, PageEntry::new(frame, flags)); 74340fe15e0SLoGin 74440fe15e0SLoGin // 获取新分配的页表 74540fe15e0SLoGin table = table.next_level_table(i)?; 74640fe15e0SLoGin } 74740fe15e0SLoGin } 74840fe15e0SLoGin } 74940fe15e0SLoGin } 75040fe15e0SLoGin 75140fe15e0SLoGin /// 将物理地址映射到具有线性偏移量的虚拟地址 75240fe15e0SLoGin #[allow(dead_code)] 75340fe15e0SLoGin pub unsafe fn map_linearly( 75440fe15e0SLoGin &mut self, 75540fe15e0SLoGin phys: PhysAddr, 75640fe15e0SLoGin flags: PageFlags<Arch>, 75740fe15e0SLoGin ) -> Option<(VirtAddr, PageFlush<Arch>)> { 75840fe15e0SLoGin let virt: VirtAddr = Arch::phys_2_virt(phys)?; 75940fe15e0SLoGin return self.map_phys(virt, phys, flags).map(|flush| (virt, flush)); 76040fe15e0SLoGin } 76140fe15e0SLoGin 76240fe15e0SLoGin /// 修改虚拟地址的页表项的flags,并返回页表项刷新器 76340fe15e0SLoGin /// 76440fe15e0SLoGin /// 请注意,需要在修改完flags后,调用刷新器的flush方法,才能使修改生效 76540fe15e0SLoGin /// 76640fe15e0SLoGin /// ## 参数 76740fe15e0SLoGin /// - virt 虚拟地址 76840fe15e0SLoGin /// - flags 新的页表项的flags 76940fe15e0SLoGin /// 77040fe15e0SLoGin /// ## 返回值 77140fe15e0SLoGin /// 77240fe15e0SLoGin /// 如果修改成功,返回刷新器,否则返回None 77340fe15e0SLoGin pub unsafe fn remap( 77440fe15e0SLoGin &mut self, 77540fe15e0SLoGin virt: VirtAddr, 77640fe15e0SLoGin flags: PageFlags<Arch>, 77740fe15e0SLoGin ) -> Option<PageFlush<Arch>> { 77840fe15e0SLoGin return self 77940fe15e0SLoGin .visit(virt, |p1, i| { 78040fe15e0SLoGin let mut entry = p1.entry(i)?; 78140fe15e0SLoGin entry.set_flags(flags); 78240fe15e0SLoGin p1.set_entry(i, entry); 78340fe15e0SLoGin Some(PageFlush::new(virt)) 78440fe15e0SLoGin }) 78540fe15e0SLoGin .flatten(); 78640fe15e0SLoGin } 78740fe15e0SLoGin 78840fe15e0SLoGin /// 根据虚拟地址,查找页表,获取对应的物理地址和页表项的flags 78940fe15e0SLoGin /// 79040fe15e0SLoGin /// ## 参数 79140fe15e0SLoGin /// 79240fe15e0SLoGin /// - virt 虚拟地址 79340fe15e0SLoGin /// 79440fe15e0SLoGin /// ## 返回值 79540fe15e0SLoGin /// 79640fe15e0SLoGin /// 如果查找成功,返回物理地址和页表项的flags,否则返回None 79740fe15e0SLoGin pub fn translate(&self, virt: VirtAddr) -> Option<(PhysAddr, PageFlags<Arch>)> { 79840fe15e0SLoGin let entry: PageEntry<Arch> = self.visit(virt, |p1, i| unsafe { p1.entry(i) })??; 79940fe15e0SLoGin let paddr = entry.address().ok()?; 80040fe15e0SLoGin let flags = entry.flags(); 80140fe15e0SLoGin return Some((paddr, flags)); 80240fe15e0SLoGin } 80340fe15e0SLoGin 80440fe15e0SLoGin /// 取消虚拟地址的映射,释放页面,并返回页表项刷新器 80540fe15e0SLoGin /// 80640fe15e0SLoGin /// 请注意,需要在取消映射后,调用刷新器的flush方法,才能使修改生效 80740fe15e0SLoGin /// 80840fe15e0SLoGin /// ## 参数 80940fe15e0SLoGin /// 81040fe15e0SLoGin /// - virt 虚拟地址 81140fe15e0SLoGin /// - unmap_parents 是否在父页表内,取消空闲子页表的映射 81240fe15e0SLoGin /// 81340fe15e0SLoGin /// ## 返回值 81440fe15e0SLoGin /// 如果取消成功,返回刷新器,否则返回None 8152dd9f0c7SLoGin #[allow(dead_code)] 81640fe15e0SLoGin pub unsafe fn unmap(&mut self, virt: VirtAddr, unmap_parents: bool) -> Option<PageFlush<Arch>> { 81740fe15e0SLoGin let (paddr, _, flusher) = self.unmap_phys(virt, unmap_parents)?; 81840fe15e0SLoGin self.frame_allocator.free_one(paddr); 81940fe15e0SLoGin return Some(flusher); 82040fe15e0SLoGin } 82140fe15e0SLoGin 82240fe15e0SLoGin /// 取消虚拟地址的映射,并返回物理地址和页表项的flags 82340fe15e0SLoGin /// 82440fe15e0SLoGin /// ## 参数 82540fe15e0SLoGin /// 82640fe15e0SLoGin /// - vaddr 虚拟地址 82740fe15e0SLoGin /// - unmap_parents 是否在父页表内,取消空闲子页表的映射 82840fe15e0SLoGin /// 82940fe15e0SLoGin /// ## 返回值 83040fe15e0SLoGin /// 83140fe15e0SLoGin /// 如果取消成功,返回物理地址和页表项的flags,否则返回None 83240fe15e0SLoGin pub unsafe fn unmap_phys( 83340fe15e0SLoGin &mut self, 83440fe15e0SLoGin virt: VirtAddr, 83540fe15e0SLoGin unmap_parents: bool, 83640fe15e0SLoGin ) -> Option<(PhysAddr, PageFlags<Arch>, PageFlush<Arch>)> { 83740fe15e0SLoGin if !virt.check_aligned(Arch::PAGE_SIZE) { 83840fe15e0SLoGin kerror!("Try to unmap unaligned page: virt={:?}", virt); 83940fe15e0SLoGin return None; 84040fe15e0SLoGin } 84140fe15e0SLoGin 842b5b571e0SLoGin let table = self.table(); 843b5b571e0SLoGin return unmap_phys_inner(virt, &table, unmap_parents, self.allocator_mut()) 84440fe15e0SLoGin .map(|(paddr, flags)| (paddr, flags, PageFlush::<Arch>::new(virt))); 84540fe15e0SLoGin } 84640fe15e0SLoGin 84740fe15e0SLoGin /// 在页表中,访问虚拟地址对应的页表项,并调用传入的函数F 84840fe15e0SLoGin fn visit<T>( 84940fe15e0SLoGin &self, 85040fe15e0SLoGin virt: VirtAddr, 85140fe15e0SLoGin f: impl FnOnce(&mut PageTable<Arch>, usize) -> T, 85240fe15e0SLoGin ) -> Option<T> { 85340fe15e0SLoGin let mut table = self.table(); 85440fe15e0SLoGin unsafe { 85540fe15e0SLoGin loop { 85640fe15e0SLoGin let i = table.index_of(virt)?; 85740fe15e0SLoGin if table.level() == 0 { 85840fe15e0SLoGin return Some(f(&mut table, i)); 85940fe15e0SLoGin } else { 86040fe15e0SLoGin table = table.next_level_table(i)?; 86140fe15e0SLoGin } 86240fe15e0SLoGin } 86340fe15e0SLoGin } 86440fe15e0SLoGin } 86540fe15e0SLoGin } 86640fe15e0SLoGin 86740fe15e0SLoGin /// 取消页面映射,返回被取消映射的页表项的:【物理地址】和【flags】 86840fe15e0SLoGin /// 86940fe15e0SLoGin /// ## 参数 87040fe15e0SLoGin /// 87140fe15e0SLoGin /// - vaddr 虚拟地址 87240fe15e0SLoGin /// - table 页表 87340fe15e0SLoGin /// - unmap_parents 是否在父页表内,取消空闲子页表的映射 87440fe15e0SLoGin /// - allocator 页面分配器(如果页表从这个分配器分配,那么在取消映射时,也需要归还到这个分配器内) 87540fe15e0SLoGin /// 87640fe15e0SLoGin /// ## 返回值 87740fe15e0SLoGin /// 87840fe15e0SLoGin /// 如果取消成功,返回被取消映射的页表项的:【物理地址】和【flags】,否则返回None 87940fe15e0SLoGin unsafe fn unmap_phys_inner<Arch: MemoryManagementArch>( 88040fe15e0SLoGin vaddr: VirtAddr, 881840045afSLoGin table: &PageTable<Arch>, 88240fe15e0SLoGin unmap_parents: bool, 88340fe15e0SLoGin allocator: &mut impl FrameAllocator, 88440fe15e0SLoGin ) -> Option<(PhysAddr, PageFlags<Arch>)> { 88540fe15e0SLoGin // 获取页表项的索引 88640fe15e0SLoGin let i = table.index_of(vaddr)?; 88740fe15e0SLoGin 88840fe15e0SLoGin // 如果当前是最后一级页表,直接取消页面映射 88940fe15e0SLoGin if table.level() == 0 { 89040fe15e0SLoGin let entry = table.entry(i)?; 8917a29d4fcSLoGin table.set_entry(i, PageEntry::from_usize(0)); 89240fe15e0SLoGin return Some((entry.address().ok()?, entry.flags())); 89340fe15e0SLoGin } 89440fe15e0SLoGin 895b5b571e0SLoGin let subtable = table.next_level_table(i)?; 89640fe15e0SLoGin // 递归地取消映射 897b5b571e0SLoGin let result = unmap_phys_inner(vaddr, &subtable, unmap_parents, allocator)?; 89840fe15e0SLoGin 89940fe15e0SLoGin // TODO: This is a bad idea for architectures where the kernel mappings are done in the process tables, 90040fe15e0SLoGin // as these mappings may become out of sync 90140fe15e0SLoGin if unmap_parents { 90240fe15e0SLoGin // 如果子页表已经没有映射的页面了,就取消子页表的映射 90340fe15e0SLoGin 90440fe15e0SLoGin // 检查子页表中是否还有映射的页面 90540fe15e0SLoGin let x = (0..Arch::PAGE_ENTRY_NUM) 90640fe15e0SLoGin .map(|k| subtable.entry(k).expect("invalid page entry")) 90740fe15e0SLoGin .any(|e| e.present()); 90840fe15e0SLoGin if !x { 90940fe15e0SLoGin // 如果没有,就取消子页表的映射 9107a29d4fcSLoGin table.set_entry(i, PageEntry::from_usize(0)); 91140fe15e0SLoGin // 释放子页表 91240fe15e0SLoGin allocator.free_one(subtable.phys()); 91340fe15e0SLoGin } 91440fe15e0SLoGin } 91540fe15e0SLoGin 91640fe15e0SLoGin return Some(result); 91740fe15e0SLoGin } 91840fe15e0SLoGin 91940fe15e0SLoGin impl<Arch, F: Debug> Debug for PageMapper<Arch, F> { 92040fe15e0SLoGin fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { 92140fe15e0SLoGin f.debug_struct("PageMapper") 92240fe15e0SLoGin .field("table_paddr", &self.table_paddr) 92340fe15e0SLoGin .field("frame_allocator", &self.frame_allocator) 92440fe15e0SLoGin .finish() 92540fe15e0SLoGin } 92640fe15e0SLoGin } 92740fe15e0SLoGin 92840fe15e0SLoGin /// 页表刷新器的trait 9297ae679ddSLoGin pub trait Flusher<Arch: MemoryManagementArch> { 93040fe15e0SLoGin /// 取消对指定的page flusher的刷新 93140fe15e0SLoGin fn consume(&mut self, flush: PageFlush<Arch>); 93240fe15e0SLoGin } 93340fe15e0SLoGin 93440fe15e0SLoGin /// 用于刷新某个虚拟地址的刷新器。这个刷新器一经产生,就必须调用flush()方法, 93540fe15e0SLoGin /// 否则会造成对页表的更改被忽略,这是不安全的 93640fe15e0SLoGin #[must_use = "The flusher must call the 'flush()', or the changes to page table will be unsafely ignored."] 9377ae679ddSLoGin pub struct PageFlush<Arch: MemoryManagementArch> { 93840fe15e0SLoGin virt: VirtAddr, 93940fe15e0SLoGin phantom: PhantomData<Arch>, 94040fe15e0SLoGin } 94140fe15e0SLoGin 94240fe15e0SLoGin impl<Arch: MemoryManagementArch> PageFlush<Arch> { 94340fe15e0SLoGin pub fn new(virt: VirtAddr) -> Self { 94440fe15e0SLoGin return Self { 94540fe15e0SLoGin virt, 94640fe15e0SLoGin phantom: PhantomData, 94740fe15e0SLoGin }; 94840fe15e0SLoGin } 94940fe15e0SLoGin 95040fe15e0SLoGin pub fn flush(self) { 95140fe15e0SLoGin unsafe { Arch::invalidate_page(self.virt) }; 95240fe15e0SLoGin } 95340fe15e0SLoGin 95440fe15e0SLoGin /// 忽略掉这个刷新器 95540fe15e0SLoGin pub unsafe fn ignore(self) { 95640fe15e0SLoGin mem::forget(self); 95740fe15e0SLoGin } 95840fe15e0SLoGin } 95940fe15e0SLoGin 9607ae679ddSLoGin impl<Arch: MemoryManagementArch> Drop for PageFlush<Arch> { 9617ae679ddSLoGin fn drop(&mut self) { 9627ae679ddSLoGin unsafe { 9637ae679ddSLoGin MMArch::invalidate_page(self.virt); 9647ae679ddSLoGin } 9657ae679ddSLoGin } 9667ae679ddSLoGin } 9677ae679ddSLoGin 96840fe15e0SLoGin /// 用于刷新整个页表的刷新器。这个刷新器一经产生,就必须调用flush()方法, 96940fe15e0SLoGin /// 否则会造成对页表的更改被忽略,这是不安全的 97040fe15e0SLoGin #[must_use = "The flusher must call the 'flush()', or the changes to page table will be unsafely ignored."] 97140fe15e0SLoGin pub struct PageFlushAll<Arch: MemoryManagementArch> { 97240fe15e0SLoGin phantom: PhantomData<fn() -> Arch>, 97340fe15e0SLoGin } 97440fe15e0SLoGin 97540fe15e0SLoGin #[allow(dead_code)] 97640fe15e0SLoGin impl<Arch: MemoryManagementArch> PageFlushAll<Arch> { 97740fe15e0SLoGin pub fn new() -> Self { 97840fe15e0SLoGin return Self { 97940fe15e0SLoGin phantom: PhantomData, 98040fe15e0SLoGin }; 98140fe15e0SLoGin } 98240fe15e0SLoGin 98340fe15e0SLoGin pub fn flush(self) { 98440fe15e0SLoGin unsafe { Arch::invalidate_all() }; 98540fe15e0SLoGin } 98640fe15e0SLoGin 98740fe15e0SLoGin /// 忽略掉这个刷新器 98840fe15e0SLoGin pub unsafe fn ignore(self) { 98940fe15e0SLoGin mem::forget(self); 99040fe15e0SLoGin } 99140fe15e0SLoGin } 99240fe15e0SLoGin 99340fe15e0SLoGin impl<Arch: MemoryManagementArch> Flusher<Arch> for PageFlushAll<Arch> { 99440fe15e0SLoGin /// 为page flush all 实现consume,消除对单个页面的刷新。(刷新整个页表了就不需要刷新单个页面了) 99540fe15e0SLoGin fn consume(&mut self, flush: PageFlush<Arch>) { 99640fe15e0SLoGin unsafe { flush.ignore() }; 99740fe15e0SLoGin } 99840fe15e0SLoGin } 99940fe15e0SLoGin 100040fe15e0SLoGin impl<Arch: MemoryManagementArch, T: Flusher<Arch> + ?Sized> Flusher<Arch> for &mut T { 100140fe15e0SLoGin /// 允许一个flusher consume掉另一个flusher 100240fe15e0SLoGin fn consume(&mut self, flush: PageFlush<Arch>) { 100340fe15e0SLoGin <T as Flusher<Arch>>::consume(self, flush); 100440fe15e0SLoGin } 100540fe15e0SLoGin } 100640fe15e0SLoGin 100740fe15e0SLoGin impl<Arch: MemoryManagementArch> Flusher<Arch> for () { 100840fe15e0SLoGin fn consume(&mut self, _flush: PageFlush<Arch>) {} 100940fe15e0SLoGin } 101040fe15e0SLoGin 101140fe15e0SLoGin impl<Arch: MemoryManagementArch> Drop for PageFlushAll<Arch> { 101240fe15e0SLoGin fn drop(&mut self) { 101340fe15e0SLoGin unsafe { 101440fe15e0SLoGin Arch::invalidate_all(); 101540fe15e0SLoGin } 101640fe15e0SLoGin } 101740fe15e0SLoGin } 101840fe15e0SLoGin 101940fe15e0SLoGin /// 未在当前CPU上激活的页表的刷新器 102040fe15e0SLoGin /// 102140fe15e0SLoGin /// 如果页表没有在当前cpu上激活,那么需要发送ipi到其他核心,尝试在其他核心上刷新页表 102240fe15e0SLoGin /// 102340fe15e0SLoGin /// TODO: 这个方式很暴力,也许把它改成在指定的核心上刷新页表会更好。(可以测试一下开销) 102440fe15e0SLoGin #[derive(Debug)] 102540fe15e0SLoGin pub struct InactiveFlusher; 102640fe15e0SLoGin 102740fe15e0SLoGin impl InactiveFlusher { 102840fe15e0SLoGin pub fn new() -> Self { 102940fe15e0SLoGin return Self {}; 103040fe15e0SLoGin } 103140fe15e0SLoGin } 103240fe15e0SLoGin 103340fe15e0SLoGin impl Flusher<MMArch> for InactiveFlusher { 103440fe15e0SLoGin fn consume(&mut self, flush: PageFlush<MMArch>) { 103540fe15e0SLoGin unsafe { 103640fe15e0SLoGin flush.ignore(); 103740fe15e0SLoGin } 103840fe15e0SLoGin } 103940fe15e0SLoGin } 104040fe15e0SLoGin 104140fe15e0SLoGin impl Drop for InactiveFlusher { 104240fe15e0SLoGin fn drop(&mut self) { 104340fe15e0SLoGin // 发送刷新页表的IPI 104440fe15e0SLoGin send_ipi(IpiKind::FlushTLB, IpiTarget::Other); 104540fe15e0SLoGin } 104640fe15e0SLoGin } 104740fe15e0SLoGin 104840fe15e0SLoGin /// # 把一个地址向下对齐到页大小 104940fe15e0SLoGin pub fn round_down_to_page_size(addr: usize) -> usize { 105040fe15e0SLoGin addr & !(MMArch::PAGE_SIZE - 1) 105140fe15e0SLoGin } 105240fe15e0SLoGin 105340fe15e0SLoGin /// # 把一个地址向上对齐到页大小 105440fe15e0SLoGin pub fn round_up_to_page_size(addr: usize) -> usize { 105540fe15e0SLoGin round_down_to_page_size(addr + MMArch::PAGE_SIZE - 1) 105640fe15e0SLoGin } 1057