140fe15e0SLoGin // 进程的用户空间内存管理 240fe15e0SLoGin 340fe15e0SLoGin use core::{ 440fe15e0SLoGin cmp, 540fe15e0SLoGin hash::Hasher, 640fe15e0SLoGin intrinsics::unlikely, 740fe15e0SLoGin ops::Add, 840fe15e0SLoGin sync::atomic::{compiler_fence, Ordering}, 940fe15e0SLoGin }; 1040fe15e0SLoGin 1140fe15e0SLoGin use alloc::{ 1240fe15e0SLoGin collections::BTreeMap, 1340fe15e0SLoGin sync::{Arc, Weak}, 1440fe15e0SLoGin vec::Vec, 1540fe15e0SLoGin }; 1640fe15e0SLoGin use hashbrown::HashSet; 17*56cc4dbeSJomo use ida::IdAllocator; 1891e9d4abSLoGin use system_error::SystemError; 1940fe15e0SLoGin 2040fe15e0SLoGin use crate::{ 211496ba7bSLoGin arch::{mm::PageMapper, CurrentIrqArch, MMArch}, 2240fe15e0SLoGin exception::InterruptArch, 2340fe15e0SLoGin libs::{ 2440fe15e0SLoGin align::page_align_up, 2540fe15e0SLoGin rwlock::{RwLock, RwLockWriteGuard}, 2640fe15e0SLoGin spinlock::{SpinLock, SpinLockGuard}, 2740fe15e0SLoGin }, 28*56cc4dbeSJomo mm::page::page_manager_lock_irasave, 291496ba7bSLoGin process::ProcessManager, 304cfa009bSJomo syscall::user_access::{UserBufferReader, UserBufferWriter}, 3140fe15e0SLoGin }; 3240fe15e0SLoGin 3340fe15e0SLoGin use super::{ 3440fe15e0SLoGin allocator::page_frame::{ 3540fe15e0SLoGin deallocate_page_frames, PageFrameCount, PhysPageFrame, VirtPageFrame, VirtPageFrameIter, 3640fe15e0SLoGin }, 3740fe15e0SLoGin page::{Flusher, InactiveFlusher, PageFlags, PageFlushAll}, 384cfa009bSJomo syscall::{MapFlags, MremapFlags, ProtFlags}, 394cfa009bSJomo MemoryManagementArch, PageTableKind, VirtAddr, VirtRegion, VmFlags, 4040fe15e0SLoGin }; 4140fe15e0SLoGin 4240fe15e0SLoGin /// MMAP_MIN_ADDR的默认值 4340fe15e0SLoGin /// 以下内容来自linux-5.19: 4440fe15e0SLoGin /// This is the portion of low virtual memory which should be protected 4540fe15e0SLoGin // from userspace allocation. Keeping a user from writing to low pages 4640fe15e0SLoGin // can help reduce the impact of kernel NULL pointer bugs. 4740fe15e0SLoGin // For most ia64, ppc64 and x86 users with lots of address space 4840fe15e0SLoGin // a value of 65536 is reasonable and should cause no problems. 4940fe15e0SLoGin // On arm and other archs it should not be higher than 32768. 5040fe15e0SLoGin // Programs which use vm86 functionality or have some need to map 5140fe15e0SLoGin // this low address space will need CAP_SYS_RAWIO or disable this 5240fe15e0SLoGin // protection by setting the value to 0. 5340fe15e0SLoGin pub const DEFAULT_MMAP_MIN_ADDR: usize = 65536; 5440fe15e0SLoGin 55*56cc4dbeSJomo /// LockedVMA的id分配器 56*56cc4dbeSJomo static LOCKEDVMA_ID_ALLOCATOR: IdAllocator = IdAllocator::new(0, usize::MAX); 57*56cc4dbeSJomo 5840fe15e0SLoGin #[derive(Debug)] 5940fe15e0SLoGin pub struct AddressSpace { 6040fe15e0SLoGin inner: RwLock<InnerAddressSpace>, 6140fe15e0SLoGin } 6240fe15e0SLoGin 6340fe15e0SLoGin impl AddressSpace { 6440fe15e0SLoGin pub fn new(create_stack: bool) -> Result<Arc<Self>, SystemError> { 6540fe15e0SLoGin let inner = InnerAddressSpace::new(create_stack)?; 6640fe15e0SLoGin let result = Self { 6740fe15e0SLoGin inner: RwLock::new(inner), 6840fe15e0SLoGin }; 6940fe15e0SLoGin return Ok(Arc::new(result)); 7040fe15e0SLoGin } 7140fe15e0SLoGin 7240fe15e0SLoGin /// 从pcb中获取当前进程的地址空间结构体的Arc指针 7340fe15e0SLoGin pub fn current() -> Result<Arc<AddressSpace>, SystemError> { 741496ba7bSLoGin let vm = ProcessManager::current_pcb() 751496ba7bSLoGin .basic() 761496ba7bSLoGin .user_vm() 7740fe15e0SLoGin .expect("Current process has no address space"); 781496ba7bSLoGin 791496ba7bSLoGin return Ok(vm); 8040fe15e0SLoGin } 8140fe15e0SLoGin 8240fe15e0SLoGin /// 判断某个地址空间是否为当前进程的地址空间 8340fe15e0SLoGin pub fn is_current(self: &Arc<Self>) -> bool { 8440fe15e0SLoGin let current = Self::current(); 8540fe15e0SLoGin if let Ok(current) = current { 8640fe15e0SLoGin return Arc::ptr_eq(¤t, self); 8740fe15e0SLoGin } 8840fe15e0SLoGin return false; 8940fe15e0SLoGin } 9040fe15e0SLoGin } 9140fe15e0SLoGin 9240fe15e0SLoGin impl core::ops::Deref for AddressSpace { 9340fe15e0SLoGin type Target = RwLock<InnerAddressSpace>; 9440fe15e0SLoGin 9540fe15e0SLoGin fn deref(&self) -> &Self::Target { 9640fe15e0SLoGin &self.inner 9740fe15e0SLoGin } 9840fe15e0SLoGin } 9940fe15e0SLoGin 10040fe15e0SLoGin impl core::ops::DerefMut for AddressSpace { 10140fe15e0SLoGin fn deref_mut(&mut self) -> &mut Self::Target { 10240fe15e0SLoGin &mut self.inner 10340fe15e0SLoGin } 10440fe15e0SLoGin } 10540fe15e0SLoGin 10640fe15e0SLoGin /// @brief 用户地址空间结构体(每个进程都有一个) 10740fe15e0SLoGin #[derive(Debug)] 10840fe15e0SLoGin pub struct InnerAddressSpace { 10940fe15e0SLoGin pub user_mapper: UserMapper, 11040fe15e0SLoGin pub mappings: UserMappings, 11140fe15e0SLoGin pub mmap_min: VirtAddr, 11240fe15e0SLoGin /// 用户栈信息结构体 11340fe15e0SLoGin pub user_stack: Option<UserStack>, 11440fe15e0SLoGin 11540fe15e0SLoGin pub elf_brk_start: VirtAddr, 11640fe15e0SLoGin pub elf_brk: VirtAddr, 11740fe15e0SLoGin 11840fe15e0SLoGin /// 当前进程的堆空间的起始地址 11940fe15e0SLoGin pub brk_start: VirtAddr, 12040fe15e0SLoGin /// 当前进程的堆空间的结束地址(不包含) 12140fe15e0SLoGin pub brk: VirtAddr, 12240fe15e0SLoGin 12340fe15e0SLoGin pub start_code: VirtAddr, 12440fe15e0SLoGin pub end_code: VirtAddr, 12540fe15e0SLoGin pub start_data: VirtAddr, 12640fe15e0SLoGin pub end_data: VirtAddr, 12740fe15e0SLoGin } 12840fe15e0SLoGin 12940fe15e0SLoGin impl InnerAddressSpace { 13040fe15e0SLoGin pub fn new(create_stack: bool) -> Result<Self, SystemError> { 13140fe15e0SLoGin let mut result = Self { 13240fe15e0SLoGin user_mapper: MMArch::setup_new_usermapper()?, 13340fe15e0SLoGin mappings: UserMappings::new(), 13440fe15e0SLoGin mmap_min: VirtAddr(DEFAULT_MMAP_MIN_ADDR), 13540fe15e0SLoGin elf_brk_start: VirtAddr::new(0), 13640fe15e0SLoGin elf_brk: VirtAddr::new(0), 13740fe15e0SLoGin brk_start: MMArch::USER_BRK_START, 13840fe15e0SLoGin brk: MMArch::USER_BRK_START, 13940fe15e0SLoGin user_stack: None, 14040fe15e0SLoGin start_code: VirtAddr(0), 14140fe15e0SLoGin end_code: VirtAddr(0), 14240fe15e0SLoGin start_data: VirtAddr(0), 14340fe15e0SLoGin end_data: VirtAddr(0), 14440fe15e0SLoGin }; 14540fe15e0SLoGin if create_stack { 14640fe15e0SLoGin // kdebug!("to create user stack."); 14740fe15e0SLoGin result.new_user_stack(UserStack::DEFAULT_USER_STACK_SIZE)?; 14840fe15e0SLoGin } 14940fe15e0SLoGin 15040fe15e0SLoGin return Ok(result); 15140fe15e0SLoGin } 15240fe15e0SLoGin 15340fe15e0SLoGin /// 尝试克隆当前进程的地址空间,包括这些映射都会被克隆 15440fe15e0SLoGin /// 15540fe15e0SLoGin /// # Returns 15640fe15e0SLoGin /// 15740fe15e0SLoGin /// 返回克隆后的,新的地址空间的Arc指针 1584fda81ceSLoGin #[inline(never)] 15940fe15e0SLoGin pub fn try_clone(&mut self) -> Result<Arc<AddressSpace>, SystemError> { 16040fe15e0SLoGin let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() }; 16140fe15e0SLoGin let new_addr_space = AddressSpace::new(false)?; 16240fe15e0SLoGin let mut new_guard = new_addr_space.write(); 16340fe15e0SLoGin 16440fe15e0SLoGin // 拷贝用户栈的结构体信息,但是不拷贝用户栈的内容(因为后面VMA的拷贝会拷贝用户栈的内容) 16540fe15e0SLoGin unsafe { 16640fe15e0SLoGin new_guard.user_stack = Some(self.user_stack.as_ref().unwrap().clone_info_only()); 16740fe15e0SLoGin } 16840fe15e0SLoGin let _current_stack_size = self.user_stack.as_ref().unwrap().stack_size(); 16940fe15e0SLoGin 17040fe15e0SLoGin let current_mapper = &mut self.user_mapper.utable; 17140fe15e0SLoGin 172ea8ad4d4SLoGin // 拷贝空洞 173ea8ad4d4SLoGin new_guard.mappings.vm_holes = self.mappings.vm_holes.clone(); 174ea8ad4d4SLoGin 17540fe15e0SLoGin for vma in self.mappings.vmas.iter() { 17640fe15e0SLoGin // TODO: 增加对VMA是否为文件映射的判断,如果是的话,就跳过 17740fe15e0SLoGin 17840fe15e0SLoGin let vma_guard: SpinLockGuard<'_, VMA> = vma.lock(); 17940fe15e0SLoGin let old_flags = vma_guard.flags(); 18040fe15e0SLoGin let tmp_flags: PageFlags<MMArch> = PageFlags::new().set_write(true); 18140fe15e0SLoGin 18240fe15e0SLoGin // 分配内存页并创建新的VMA 18340fe15e0SLoGin let new_vma = VMA::zeroed( 18440fe15e0SLoGin VirtPageFrame::new(vma_guard.region.start()), 18540fe15e0SLoGin PageFrameCount::new(vma_guard.region.size() / MMArch::PAGE_SIZE), 186b5b571e0SLoGin *vma_guard.vm_flags(), 18740fe15e0SLoGin tmp_flags, 18840fe15e0SLoGin &mut new_guard.user_mapper.utable, 18940fe15e0SLoGin (), 19040fe15e0SLoGin )?; 19140fe15e0SLoGin new_guard.mappings.vmas.insert(new_vma.clone()); 19240fe15e0SLoGin // kdebug!("new vma: {:x?}", new_vma); 19340fe15e0SLoGin let mut new_vma_guard = new_vma.lock(); 19440fe15e0SLoGin for page in new_vma_guard.pages().map(|p| p.virt_address()) { 19540fe15e0SLoGin // kdebug!("page: {:x?}", page); 19640fe15e0SLoGin let current_frame = unsafe { 19740fe15e0SLoGin MMArch::phys_2_virt( 19840fe15e0SLoGin current_mapper 19940fe15e0SLoGin .translate(page) 20040fe15e0SLoGin .expect("VMA page not mapped") 20140fe15e0SLoGin .0, 20240fe15e0SLoGin ) 20340fe15e0SLoGin } 20440fe15e0SLoGin .expect("Phys2Virt: vaddr overflow.") 20540fe15e0SLoGin .data() as *mut u8; 20640fe15e0SLoGin 20740fe15e0SLoGin let new_frame = unsafe { 20840fe15e0SLoGin MMArch::phys_2_virt( 20940fe15e0SLoGin new_guard 21040fe15e0SLoGin .user_mapper 21140fe15e0SLoGin .utable 21240fe15e0SLoGin .translate(page) 21340fe15e0SLoGin .expect("VMA page not mapped") 21440fe15e0SLoGin .0, 21540fe15e0SLoGin ) 21640fe15e0SLoGin } 21740fe15e0SLoGin .expect("Phys2Virt: vaddr overflow.") 21840fe15e0SLoGin .data() as *mut u8; 21940fe15e0SLoGin 22040fe15e0SLoGin unsafe { 22140fe15e0SLoGin // 拷贝数据 22240fe15e0SLoGin new_frame.copy_from_nonoverlapping(current_frame, MMArch::PAGE_SIZE); 22340fe15e0SLoGin } 22440fe15e0SLoGin } 22540fe15e0SLoGin drop(vma_guard); 22640fe15e0SLoGin 22740fe15e0SLoGin new_vma_guard.remap(old_flags, &mut new_guard.user_mapper.utable, ())?; 22840fe15e0SLoGin drop(new_vma_guard); 22940fe15e0SLoGin } 23040fe15e0SLoGin drop(new_guard); 23140fe15e0SLoGin drop(irq_guard); 23240fe15e0SLoGin return Ok(new_addr_space); 23340fe15e0SLoGin } 23440fe15e0SLoGin 23540fe15e0SLoGin /// 判断当前的地址空间是否是当前进程的地址空间 23640fe15e0SLoGin #[inline] 23740fe15e0SLoGin pub fn is_current(&self) -> bool { 23840fe15e0SLoGin return self.user_mapper.utable.is_current(); 23940fe15e0SLoGin } 24040fe15e0SLoGin 24140fe15e0SLoGin /// 进行匿名页映射 24240fe15e0SLoGin /// 24340fe15e0SLoGin /// ## 参数 24440fe15e0SLoGin /// 24540fe15e0SLoGin /// - `start_vaddr`:映射的起始地址 24640fe15e0SLoGin /// - `len`:映射的长度 24740fe15e0SLoGin /// - `prot_flags`:保护标志 24840fe15e0SLoGin /// - `map_flags`:映射标志 24940fe15e0SLoGin /// - `round_to_min`:是否将`start_vaddr`对齐到`mmap_min`,如果为`true`,则当`start_vaddr`不为0时,会对齐到`mmap_min`,否则仅向下对齐到页边界 2501496ba7bSLoGin /// 2511496ba7bSLoGin /// ## 返回 2521496ba7bSLoGin /// 2531496ba7bSLoGin /// 返回映射的起始虚拟页帧 25440fe15e0SLoGin pub fn map_anonymous( 25540fe15e0SLoGin &mut self, 25640fe15e0SLoGin start_vaddr: VirtAddr, 25740fe15e0SLoGin len: usize, 25840fe15e0SLoGin prot_flags: ProtFlags, 25940fe15e0SLoGin map_flags: MapFlags, 26040fe15e0SLoGin round_to_min: bool, 26140fe15e0SLoGin ) -> Result<VirtPageFrame, SystemError> { 26240fe15e0SLoGin // 用于对齐hint的函数 26340fe15e0SLoGin let round_hint_to_min = |hint: VirtAddr| { 26440fe15e0SLoGin // 先把hint向下对齐到页边界 26540fe15e0SLoGin let addr = hint.data() & (!MMArch::PAGE_OFFSET_MASK); 26640fe15e0SLoGin // kdebug!("map_anonymous: hint = {:?}, addr = {addr:#x}", hint); 26740fe15e0SLoGin // 如果hint不是0,且hint小于DEFAULT_MMAP_MIN_ADDR,则对齐到DEFAULT_MMAP_MIN_ADDR 26840fe15e0SLoGin if (addr != 0) && round_to_min && (addr < DEFAULT_MMAP_MIN_ADDR) { 26940fe15e0SLoGin Some(VirtAddr::new(page_align_up(DEFAULT_MMAP_MIN_ADDR))) 27040fe15e0SLoGin } else if addr == 0 { 27140fe15e0SLoGin None 27240fe15e0SLoGin } else { 27340fe15e0SLoGin Some(VirtAddr::new(addr)) 27440fe15e0SLoGin } 27540fe15e0SLoGin }; 27640fe15e0SLoGin // kdebug!("map_anonymous: start_vaddr = {:?}", start_vaddr); 27740fe15e0SLoGin // kdebug!("map_anonymous: len(no align) = {}", len); 27840fe15e0SLoGin 27940fe15e0SLoGin let len = page_align_up(len); 28040fe15e0SLoGin 2814cfa009bSJomo let vm_flags = VmFlags::from(prot_flags) 2824cfa009bSJomo | VmFlags::from(map_flags) 2834cfa009bSJomo | VmFlags::VM_MAYREAD 2844cfa009bSJomo | VmFlags::VM_MAYWRITE 2854cfa009bSJomo | VmFlags::VM_MAYEXEC; 2864cfa009bSJomo 28740fe15e0SLoGin // kdebug!("map_anonymous: len = {}", len); 28840fe15e0SLoGin 28940fe15e0SLoGin let start_page: VirtPageFrame = self.mmap( 29040fe15e0SLoGin round_hint_to_min(start_vaddr), 29140fe15e0SLoGin PageFrameCount::from_bytes(len).unwrap(), 29240fe15e0SLoGin prot_flags, 29340fe15e0SLoGin map_flags, 29440fe15e0SLoGin move |page, count, flags, mapper, flusher| { 295b5b571e0SLoGin VMA::zeroed(page, count, vm_flags, flags, mapper, flusher) 29640fe15e0SLoGin }, 29740fe15e0SLoGin )?; 29840fe15e0SLoGin 29940fe15e0SLoGin return Ok(start_page); 30040fe15e0SLoGin } 30140fe15e0SLoGin 30240fe15e0SLoGin /// 向进程的地址空间映射页面 30340fe15e0SLoGin /// 30440fe15e0SLoGin /// # 参数 30540fe15e0SLoGin /// 30640fe15e0SLoGin /// - `addr`:映射的起始地址,如果为`None`,则由内核自动分配 30740fe15e0SLoGin /// - `page_count`:映射的页面数量 30840fe15e0SLoGin /// - `prot_flags`:保护标志 30940fe15e0SLoGin /// - `map_flags`:映射标志 31040fe15e0SLoGin /// - `map_func`:映射函数,用于创建VMA 31140fe15e0SLoGin /// 31240fe15e0SLoGin /// # Returns 31340fe15e0SLoGin /// 31440fe15e0SLoGin /// 返回映射的起始虚拟页帧 31540fe15e0SLoGin /// 31640fe15e0SLoGin /// # Errors 31740fe15e0SLoGin /// 31840fe15e0SLoGin /// - `EINVAL`:参数错误 31940fe15e0SLoGin pub fn mmap< 32040fe15e0SLoGin F: FnOnce( 32140fe15e0SLoGin VirtPageFrame, 32240fe15e0SLoGin PageFrameCount, 32340fe15e0SLoGin PageFlags<MMArch>, 32440fe15e0SLoGin &mut PageMapper, 32540fe15e0SLoGin &mut dyn Flusher<MMArch>, 32640fe15e0SLoGin ) -> Result<Arc<LockedVMA>, SystemError>, 32740fe15e0SLoGin >( 32840fe15e0SLoGin &mut self, 32940fe15e0SLoGin addr: Option<VirtAddr>, 33040fe15e0SLoGin page_count: PageFrameCount, 33140fe15e0SLoGin prot_flags: ProtFlags, 33240fe15e0SLoGin map_flags: MapFlags, 33340fe15e0SLoGin map_func: F, 33440fe15e0SLoGin ) -> Result<VirtPageFrame, SystemError> { 33540fe15e0SLoGin if page_count == PageFrameCount::new(0) { 33640fe15e0SLoGin return Err(SystemError::EINVAL); 33740fe15e0SLoGin } 33840fe15e0SLoGin // kdebug!("mmap: addr: {addr:?}, page_count: {page_count:?}, prot_flags: {prot_flags:?}, map_flags: {map_flags:?}"); 33940fe15e0SLoGin 34040fe15e0SLoGin // 找到未使用的区域 34140fe15e0SLoGin let region = match addr { 34240fe15e0SLoGin Some(vaddr) => { 34340fe15e0SLoGin self.mappings 34440fe15e0SLoGin .find_free_at(self.mmap_min, vaddr, page_count.bytes(), map_flags)? 34540fe15e0SLoGin } 34640fe15e0SLoGin None => self 34740fe15e0SLoGin .mappings 34840fe15e0SLoGin .find_free(self.mmap_min, page_count.bytes()) 34940fe15e0SLoGin .ok_or(SystemError::ENOMEM)?, 35040fe15e0SLoGin }; 35140fe15e0SLoGin 35240fe15e0SLoGin let page = VirtPageFrame::new(region.start()); 35340fe15e0SLoGin 35440fe15e0SLoGin // kdebug!("mmap: page: {:?}, region={region:?}", page.virt_address()); 35540fe15e0SLoGin 35640fe15e0SLoGin compiler_fence(Ordering::SeqCst); 35740fe15e0SLoGin let (mut active, mut inactive); 35840fe15e0SLoGin let flusher = if self.is_current() { 35940fe15e0SLoGin active = PageFlushAll::new(); 36040fe15e0SLoGin &mut active as &mut dyn Flusher<MMArch> 36140fe15e0SLoGin } else { 36240fe15e0SLoGin inactive = InactiveFlusher::new(); 36340fe15e0SLoGin &mut inactive as &mut dyn Flusher<MMArch> 36440fe15e0SLoGin }; 36540fe15e0SLoGin compiler_fence(Ordering::SeqCst); 36640fe15e0SLoGin // 映射页面,并将VMA插入到地址空间的VMA列表中 36740fe15e0SLoGin self.mappings.insert_vma(map_func( 36840fe15e0SLoGin page, 36940fe15e0SLoGin page_count, 37040fe15e0SLoGin PageFlags::from_prot_flags(prot_flags, true), 37140fe15e0SLoGin &mut self.user_mapper.utable, 37240fe15e0SLoGin flusher, 37340fe15e0SLoGin )?); 37440fe15e0SLoGin 37540fe15e0SLoGin return Ok(page); 37640fe15e0SLoGin } 37740fe15e0SLoGin 3784cfa009bSJomo /// 重映射内存区域 3794cfa009bSJomo /// 3804cfa009bSJomo /// # 参数 3814cfa009bSJomo /// 3824cfa009bSJomo /// - `old_vaddr`:原映射的起始地址 3834cfa009bSJomo /// - `old_len`:原映射的长度 3844cfa009bSJomo /// - `new_len`:重新映射的长度 3854cfa009bSJomo /// - `mremap_flags`:重映射标志 3864cfa009bSJomo /// - `new_vaddr`:重新映射的起始地址 3874cfa009bSJomo /// - `vm_flags`:旧内存区域标志 3884cfa009bSJomo /// 3894cfa009bSJomo /// # Returns 3904cfa009bSJomo /// 3914cfa009bSJomo /// 返回重映射的起始虚拟页帧地址 3924cfa009bSJomo /// 3934cfa009bSJomo /// # Errors 3944cfa009bSJomo /// 3954cfa009bSJomo /// - `EINVAL`:参数错误 3964cfa009bSJomo pub fn mremap( 3974cfa009bSJomo &mut self, 3984cfa009bSJomo old_vaddr: VirtAddr, 3994cfa009bSJomo old_len: usize, 4004cfa009bSJomo new_len: usize, 4014cfa009bSJomo mremap_flags: MremapFlags, 4024cfa009bSJomo new_vaddr: VirtAddr, 4034cfa009bSJomo vm_flags: VmFlags, 4044cfa009bSJomo ) -> Result<VirtAddr, SystemError> { 4054cfa009bSJomo // 检查新内存地址是否对齐 4064cfa009bSJomo if !new_vaddr.check_aligned(MMArch::PAGE_SIZE) { 4074cfa009bSJomo return Err(SystemError::EINVAL); 4084cfa009bSJomo } 4094cfa009bSJomo 4104cfa009bSJomo // 检查新、旧内存区域是否冲突 4114cfa009bSJomo let old_region = VirtRegion::new(old_vaddr, old_len); 4124cfa009bSJomo let new_region = VirtRegion::new(new_vaddr, new_len); 4134cfa009bSJomo if old_region.collide(&new_region) { 4144cfa009bSJomo return Err(SystemError::EINVAL); 4154cfa009bSJomo } 4164cfa009bSJomo 4173055390cSJomo // 初始化映射标志 4183055390cSJomo let mut map_flags: MapFlags = vm_flags.into(); 4193055390cSJomo // 初始化内存区域保护标志 4203055390cSJomo let prot_flags: ProtFlags = vm_flags.into(); 4213055390cSJomo 4224cfa009bSJomo // 取消新内存区域的原映射 4234cfa009bSJomo if mremap_flags.contains(MremapFlags::MREMAP_FIXED) { 4243055390cSJomo map_flags |= MapFlags::MAP_FIXED; 4254cfa009bSJomo let start_page = VirtPageFrame::new(new_vaddr); 4264cfa009bSJomo let page_count = PageFrameCount::from_bytes(new_len).unwrap(); 4274cfa009bSJomo self.munmap(start_page, page_count)?; 4284cfa009bSJomo } 4294cfa009bSJomo 4304cfa009bSJomo // 获取映射后的新内存页面 4314cfa009bSJomo let new_page = self.map_anonymous(new_vaddr, new_len, prot_flags, map_flags, true)?; 4324cfa009bSJomo let new_page_vaddr = new_page.virt_address(); 4334cfa009bSJomo 4344cfa009bSJomo // 拷贝旧内存区域内容到新内存区域 4354cfa009bSJomo let old_buffer_reader = 4364cfa009bSJomo UserBufferReader::new(old_vaddr.data() as *const u8, old_len, true)?; 4374cfa009bSJomo let old_buf: &[u8] = old_buffer_reader.read_from_user(0)?; 4384cfa009bSJomo let mut new_buffer_writer = 4394cfa009bSJomo UserBufferWriter::new(new_page_vaddr.data() as *mut u8, new_len, true)?; 4404cfa009bSJomo let new_buf: &mut [u8] = new_buffer_writer.buffer(0)?; 4414cfa009bSJomo let len = old_buf.len().min(new_buf.len()); 442b5b571e0SLoGin new_buf[..len].copy_from_slice(&old_buf[..len]); 4434cfa009bSJomo 4444cfa009bSJomo return Ok(new_page_vaddr); 4454cfa009bSJomo } 4464cfa009bSJomo 44740fe15e0SLoGin /// 取消进程的地址空间中的映射 44840fe15e0SLoGin /// 44940fe15e0SLoGin /// # 参数 45040fe15e0SLoGin /// 45140fe15e0SLoGin /// - `start_page`:起始页帧 45240fe15e0SLoGin /// - `page_count`:取消映射的页帧数量 45340fe15e0SLoGin /// 45440fe15e0SLoGin /// # Errors 45540fe15e0SLoGin /// 45640fe15e0SLoGin /// - `EINVAL`:参数错误 45740fe15e0SLoGin /// - `ENOMEM`:内存不足 45840fe15e0SLoGin pub fn munmap( 45940fe15e0SLoGin &mut self, 46040fe15e0SLoGin start_page: VirtPageFrame, 46140fe15e0SLoGin page_count: PageFrameCount, 46240fe15e0SLoGin ) -> Result<(), SystemError> { 46340fe15e0SLoGin let to_unmap = VirtRegion::new(start_page.virt_address(), page_count.bytes()); 46440fe15e0SLoGin let mut flusher: PageFlushAll<MMArch> = PageFlushAll::new(); 46540fe15e0SLoGin 46640fe15e0SLoGin let regions: Vec<Arc<LockedVMA>> = self.mappings.conflicts(to_unmap).collect::<Vec<_>>(); 46740fe15e0SLoGin 46840fe15e0SLoGin for r in regions { 46940fe15e0SLoGin let r = r.lock().region; 47040fe15e0SLoGin let r = self.mappings.remove_vma(&r).unwrap(); 47140fe15e0SLoGin let intersection = r.lock().region().intersect(&to_unmap).unwrap(); 472*56cc4dbeSJomo let split_result = r.extract(intersection, &self.user_mapper.utable).unwrap(); 47340fe15e0SLoGin 47440fe15e0SLoGin // TODO: 当引入后备页映射后,这里需要增加通知文件的逻辑 47540fe15e0SLoGin 476b5b571e0SLoGin if let Some(before) = split_result.prev { 47740fe15e0SLoGin // 如果前面有VMA,则需要将前面的VMA重新插入到地址空间的VMA列表中 47840fe15e0SLoGin self.mappings.insert_vma(before); 47940fe15e0SLoGin } 48040fe15e0SLoGin 481b5b571e0SLoGin if let Some(after) = split_result.after { 48240fe15e0SLoGin // 如果后面有VMA,则需要将后面的VMA重新插入到地址空间的VMA列表中 48340fe15e0SLoGin self.mappings.insert_vma(after); 48440fe15e0SLoGin } 48540fe15e0SLoGin 48640fe15e0SLoGin r.unmap(&mut self.user_mapper.utable, &mut flusher); 48740fe15e0SLoGin } 48840fe15e0SLoGin 48940fe15e0SLoGin // TODO: 当引入后备页映射后,这里需要增加通知文件的逻辑 49040fe15e0SLoGin 49140fe15e0SLoGin return Ok(()); 49240fe15e0SLoGin } 49340fe15e0SLoGin 49440fe15e0SLoGin pub fn mprotect( 49540fe15e0SLoGin &mut self, 49640fe15e0SLoGin start_page: VirtPageFrame, 49740fe15e0SLoGin page_count: PageFrameCount, 49840fe15e0SLoGin prot_flags: ProtFlags, 49940fe15e0SLoGin ) -> Result<(), SystemError> { 50040fe15e0SLoGin // kdebug!( 50140fe15e0SLoGin // "mprotect: start_page: {:?}, page_count: {:?}, prot_flags:{prot_flags:?}", 50240fe15e0SLoGin // start_page, 50340fe15e0SLoGin // page_count 50440fe15e0SLoGin // ); 50540fe15e0SLoGin let (mut active, mut inactive); 50640fe15e0SLoGin let mut flusher = if self.is_current() { 50740fe15e0SLoGin active = PageFlushAll::new(); 50840fe15e0SLoGin &mut active as &mut dyn Flusher<MMArch> 50940fe15e0SLoGin } else { 51040fe15e0SLoGin inactive = InactiveFlusher::new(); 51140fe15e0SLoGin &mut inactive as &mut dyn Flusher<MMArch> 51240fe15e0SLoGin }; 51340fe15e0SLoGin 51440fe15e0SLoGin let mapper = &mut self.user_mapper.utable; 51540fe15e0SLoGin let region = VirtRegion::new(start_page.virt_address(), page_count.bytes()); 51640fe15e0SLoGin // kdebug!("mprotect: region: {:?}", region); 51740fe15e0SLoGin 51840fe15e0SLoGin let regions = self.mappings.conflicts(region).collect::<Vec<_>>(); 51940fe15e0SLoGin // kdebug!("mprotect: regions: {:?}", regions); 52040fe15e0SLoGin 52140fe15e0SLoGin for r in regions { 52240fe15e0SLoGin // kdebug!("mprotect: r: {:?}", r); 523b5b571e0SLoGin let r = *r.lock().region(); 52440fe15e0SLoGin let r = self.mappings.remove_vma(&r).unwrap(); 52540fe15e0SLoGin 52640fe15e0SLoGin let intersection = r.lock().region().intersect(®ion).unwrap(); 527*56cc4dbeSJomo let split_result = r 528*56cc4dbeSJomo .extract(intersection, mapper) 529*56cc4dbeSJomo .expect("Failed to extract VMA"); 53040fe15e0SLoGin 531b5b571e0SLoGin if let Some(before) = split_result.prev { 53240fe15e0SLoGin self.mappings.insert_vma(before); 53340fe15e0SLoGin } 534b5b571e0SLoGin if let Some(after) = split_result.after { 53540fe15e0SLoGin self.mappings.insert_vma(after); 53640fe15e0SLoGin } 53740fe15e0SLoGin 53840fe15e0SLoGin let mut r_guard = r.lock(); 53940fe15e0SLoGin // 如果VMA的保护标志不允许指定的修改,则返回错误 54040fe15e0SLoGin if !r_guard.can_have_flags(prot_flags) { 54140fe15e0SLoGin drop(r_guard); 54240fe15e0SLoGin self.mappings.insert_vma(r.clone()); 54340fe15e0SLoGin return Err(SystemError::EACCES); 54440fe15e0SLoGin } 54540fe15e0SLoGin 54640fe15e0SLoGin let new_flags: PageFlags<MMArch> = r_guard 54740fe15e0SLoGin .flags() 54840fe15e0SLoGin .set_execute(prot_flags.contains(ProtFlags::PROT_EXEC)) 54940fe15e0SLoGin .set_write(prot_flags.contains(ProtFlags::PROT_WRITE)); 55040fe15e0SLoGin 55140fe15e0SLoGin r_guard.remap(new_flags, mapper, &mut flusher)?; 55240fe15e0SLoGin drop(r_guard); 55340fe15e0SLoGin self.mappings.insert_vma(r); 55440fe15e0SLoGin } 55540fe15e0SLoGin 55640fe15e0SLoGin return Ok(()); 55740fe15e0SLoGin } 55840fe15e0SLoGin 55940fe15e0SLoGin /// 创建新的用户栈 56040fe15e0SLoGin /// 56140fe15e0SLoGin /// ## 参数 56240fe15e0SLoGin /// 56340fe15e0SLoGin /// - `size`:栈的大小 56440fe15e0SLoGin pub fn new_user_stack(&mut self, size: usize) -> Result<(), SystemError> { 56540fe15e0SLoGin assert!(self.user_stack.is_none(), "User stack already exists"); 56640fe15e0SLoGin let stack = UserStack::new(self, None, size)?; 56740fe15e0SLoGin self.user_stack = Some(stack); 56840fe15e0SLoGin return Ok(()); 56940fe15e0SLoGin } 57040fe15e0SLoGin 57140fe15e0SLoGin #[inline(always)] 57240fe15e0SLoGin pub fn user_stack_mut(&mut self) -> Option<&mut UserStack> { 57340fe15e0SLoGin return self.user_stack.as_mut(); 57440fe15e0SLoGin } 57540fe15e0SLoGin 57640fe15e0SLoGin /// 取消用户空间内的所有映射 57740fe15e0SLoGin pub unsafe fn unmap_all(&mut self) { 57840fe15e0SLoGin let mut flusher: PageFlushAll<MMArch> = PageFlushAll::new(); 57940fe15e0SLoGin for vma in self.mappings.iter_vmas() { 58040fe15e0SLoGin vma.unmap(&mut self.user_mapper.utable, &mut flusher); 58140fe15e0SLoGin } 58240fe15e0SLoGin } 58340fe15e0SLoGin 58440fe15e0SLoGin /// 设置进程的堆的内存空间 58540fe15e0SLoGin /// 58640fe15e0SLoGin /// ## 参数 58740fe15e0SLoGin /// 58840fe15e0SLoGin /// - `new_brk`:新的堆的结束地址。需要满足页对齐要求,并且是用户空间地址,且大于等于当前的堆的起始地址 58940fe15e0SLoGin /// 59040fe15e0SLoGin /// ## 返回值 59140fe15e0SLoGin /// 59240fe15e0SLoGin /// 返回旧的堆的结束地址 59340fe15e0SLoGin pub unsafe fn set_brk(&mut self, new_brk: VirtAddr) -> Result<VirtAddr, SystemError> { 59440fe15e0SLoGin assert!(new_brk.check_aligned(MMArch::PAGE_SIZE)); 59540fe15e0SLoGin 59640fe15e0SLoGin if !new_brk.check_user() || new_brk < self.brk_start { 59740fe15e0SLoGin return Err(SystemError::EFAULT); 59840fe15e0SLoGin } 59940fe15e0SLoGin 60040fe15e0SLoGin let old_brk = self.brk; 6011496ba7bSLoGin 60240fe15e0SLoGin if new_brk > self.brk { 60340fe15e0SLoGin let len = new_brk - self.brk; 60440fe15e0SLoGin let prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC; 60540fe15e0SLoGin let map_flags = MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS | MapFlags::MAP_FIXED; 6061496ba7bSLoGin self.map_anonymous(old_brk, len, prot_flags, map_flags, true)?; 6071496ba7bSLoGin 60840fe15e0SLoGin self.brk = new_brk; 60940fe15e0SLoGin return Ok(old_brk); 61040fe15e0SLoGin } else { 61140fe15e0SLoGin let unmap_len = self.brk - new_brk; 61240fe15e0SLoGin let unmap_start = new_brk; 61340fe15e0SLoGin if unmap_len == 0 { 61440fe15e0SLoGin return Ok(old_brk); 61540fe15e0SLoGin } 61640fe15e0SLoGin self.munmap( 61740fe15e0SLoGin VirtPageFrame::new(unmap_start), 61840fe15e0SLoGin PageFrameCount::from_bytes(unmap_len).unwrap(), 61940fe15e0SLoGin )?; 62040fe15e0SLoGin self.brk = new_brk; 62140fe15e0SLoGin return Ok(old_brk); 62240fe15e0SLoGin } 62340fe15e0SLoGin } 62440fe15e0SLoGin 62540fe15e0SLoGin pub unsafe fn sbrk(&mut self, incr: isize) -> Result<VirtAddr, SystemError> { 62640fe15e0SLoGin if incr == 0 { 62740fe15e0SLoGin return Ok(self.brk); 62840fe15e0SLoGin } 62940fe15e0SLoGin 63040fe15e0SLoGin let new_brk = if incr > 0 { 63140fe15e0SLoGin self.brk + incr as usize 63240fe15e0SLoGin } else { 633b5b571e0SLoGin self.brk - incr.unsigned_abs() 63440fe15e0SLoGin }; 63540fe15e0SLoGin 63640fe15e0SLoGin let new_brk = VirtAddr::new(page_align_up(new_brk.data())); 63740fe15e0SLoGin 63840fe15e0SLoGin return self.set_brk(new_brk); 63940fe15e0SLoGin } 64040fe15e0SLoGin } 64140fe15e0SLoGin 64240fe15e0SLoGin impl Drop for InnerAddressSpace { 64340fe15e0SLoGin fn drop(&mut self) { 64440fe15e0SLoGin unsafe { 64540fe15e0SLoGin self.unmap_all(); 64640fe15e0SLoGin } 64740fe15e0SLoGin } 64840fe15e0SLoGin } 64940fe15e0SLoGin 65040fe15e0SLoGin #[derive(Debug, Hash)] 65140fe15e0SLoGin pub struct UserMapper { 65240fe15e0SLoGin pub utable: PageMapper, 65340fe15e0SLoGin } 65440fe15e0SLoGin 65540fe15e0SLoGin impl UserMapper { 65640fe15e0SLoGin pub fn new(utable: PageMapper) -> Self { 65740fe15e0SLoGin return Self { utable }; 65840fe15e0SLoGin } 65940fe15e0SLoGin } 66040fe15e0SLoGin 66140fe15e0SLoGin impl Drop for UserMapper { 66240fe15e0SLoGin fn drop(&mut self) { 66340fe15e0SLoGin if self.utable.is_current() { 66440fe15e0SLoGin // 如果当前要被销毁的用户空间的页表是当前进程的页表,那么就切换回初始内核页表 66540fe15e0SLoGin unsafe { MMArch::set_table(PageTableKind::User, MMArch::initial_page_table()) } 66640fe15e0SLoGin } 66740fe15e0SLoGin // 释放用户空间顶层页表占用的页帧 66840fe15e0SLoGin // 请注意,在释放这个页帧之前,用户页表应该已经被完全释放,否则会产生内存泄露 66940fe15e0SLoGin unsafe { 67040fe15e0SLoGin deallocate_page_frames( 67140fe15e0SLoGin PhysPageFrame::new(self.utable.table().phys()), 67240fe15e0SLoGin PageFrameCount::new(1), 673*56cc4dbeSJomo &mut page_manager_lock_irasave(), 67440fe15e0SLoGin ) 67540fe15e0SLoGin }; 67640fe15e0SLoGin } 67740fe15e0SLoGin } 67840fe15e0SLoGin 67940fe15e0SLoGin /// 用户空间映射信息 68040fe15e0SLoGin #[derive(Debug)] 68140fe15e0SLoGin pub struct UserMappings { 68240fe15e0SLoGin /// 当前用户空间的虚拟内存区域 68340fe15e0SLoGin vmas: HashSet<Arc<LockedVMA>>, 68440fe15e0SLoGin /// 当前用户空间的VMA空洞 68540fe15e0SLoGin vm_holes: BTreeMap<VirtAddr, usize>, 68640fe15e0SLoGin } 68740fe15e0SLoGin 68840fe15e0SLoGin impl UserMappings { 68940fe15e0SLoGin pub fn new() -> Self { 69040fe15e0SLoGin return Self { 69140fe15e0SLoGin vmas: HashSet::new(), 69240fe15e0SLoGin vm_holes: core::iter::once((VirtAddr::new(0), MMArch::USER_END_VADDR.data())) 69340fe15e0SLoGin .collect::<BTreeMap<_, _>>(), 69440fe15e0SLoGin }; 69540fe15e0SLoGin } 69640fe15e0SLoGin 69740fe15e0SLoGin /// 判断当前进程的VMA内,是否有包含指定的虚拟地址的VMA。 69840fe15e0SLoGin /// 69940fe15e0SLoGin /// 如果有,返回包含指定虚拟地址的VMA的Arc指针,否则返回None。 70040fe15e0SLoGin #[allow(dead_code)] 70140fe15e0SLoGin pub fn contains(&self, vaddr: VirtAddr) -> Option<Arc<LockedVMA>> { 70240fe15e0SLoGin for v in self.vmas.iter() { 70340fe15e0SLoGin let guard = v.lock(); 70440fe15e0SLoGin if guard.region.contains(vaddr) { 70540fe15e0SLoGin return Some(v.clone()); 70640fe15e0SLoGin } 70740fe15e0SLoGin } 70840fe15e0SLoGin return None; 70940fe15e0SLoGin } 71040fe15e0SLoGin 71140fe15e0SLoGin /// 获取当前进程的地址空间中,与给定虚拟地址范围有重叠的VMA的迭代器。 71240fe15e0SLoGin pub fn conflicts(&self, request: VirtRegion) -> impl Iterator<Item = Arc<LockedVMA>> + '_ { 71340fe15e0SLoGin let r = self 71440fe15e0SLoGin .vmas 71540fe15e0SLoGin .iter() 716b5b571e0SLoGin .filter(move |v| v.lock().region.intersect(&request).is_some()) 71740fe15e0SLoGin .cloned(); 71840fe15e0SLoGin return r; 71940fe15e0SLoGin } 72040fe15e0SLoGin 72140fe15e0SLoGin /// 在当前进程的地址空间中,寻找第一个符合条件的空闲的虚拟内存范围。 72240fe15e0SLoGin /// 72340fe15e0SLoGin /// @param min_vaddr 最小的起始地址 72440fe15e0SLoGin /// @param size 请求的大小 72540fe15e0SLoGin /// 72640fe15e0SLoGin /// @return 如果找到了,返回虚拟内存范围,否则返回None 72740fe15e0SLoGin pub fn find_free(&self, min_vaddr: VirtAddr, size: usize) -> Option<VirtRegion> { 72840fe15e0SLoGin let _vaddr = min_vaddr; 72940fe15e0SLoGin let mut iter = self 73040fe15e0SLoGin .vm_holes 73140fe15e0SLoGin .iter() 73240fe15e0SLoGin .skip_while(|(hole_vaddr, hole_size)| hole_vaddr.add(**hole_size) <= min_vaddr); 73340fe15e0SLoGin 73440fe15e0SLoGin let (hole_vaddr, size) = iter.find(|(hole_vaddr, hole_size)| { 73540fe15e0SLoGin // 计算当前空洞的可用大小 73640fe15e0SLoGin let available_size: usize = 73740fe15e0SLoGin if hole_vaddr <= &&min_vaddr && min_vaddr <= hole_vaddr.add(**hole_size) { 73840fe15e0SLoGin **hole_size - (min_vaddr - **hole_vaddr) 73940fe15e0SLoGin } else { 74040fe15e0SLoGin **hole_size 74140fe15e0SLoGin }; 74240fe15e0SLoGin 74340fe15e0SLoGin size <= available_size 74440fe15e0SLoGin })?; 74540fe15e0SLoGin 74640fe15e0SLoGin // 创建一个新的虚拟内存范围。 74740fe15e0SLoGin let region = VirtRegion::new(cmp::max(*hole_vaddr, min_vaddr), *size); 7484cfa009bSJomo 74940fe15e0SLoGin return Some(region); 75040fe15e0SLoGin } 75140fe15e0SLoGin 75240fe15e0SLoGin pub fn find_free_at( 75340fe15e0SLoGin &self, 75440fe15e0SLoGin min_vaddr: VirtAddr, 75540fe15e0SLoGin vaddr: VirtAddr, 75640fe15e0SLoGin size: usize, 75740fe15e0SLoGin flags: MapFlags, 75840fe15e0SLoGin ) -> Result<VirtRegion, SystemError> { 75940fe15e0SLoGin // 如果没有指定地址,那么就在当前进程的地址空间中寻找一个空闲的虚拟内存范围。 76040fe15e0SLoGin if vaddr == VirtAddr::new(0) { 76140fe15e0SLoGin return self.find_free(min_vaddr, size).ok_or(SystemError::ENOMEM); 76240fe15e0SLoGin } 76340fe15e0SLoGin 76440fe15e0SLoGin // 如果指定了地址,那么就检查指定的地址是否可用。 76540fe15e0SLoGin 76640fe15e0SLoGin let requested = VirtRegion::new(vaddr, size); 76740fe15e0SLoGin 76840fe15e0SLoGin if requested.end() >= MMArch::USER_END_VADDR || !vaddr.check_aligned(MMArch::PAGE_SIZE) { 76940fe15e0SLoGin return Err(SystemError::EINVAL); 77040fe15e0SLoGin } 77140fe15e0SLoGin 77240fe15e0SLoGin if let Some(_x) = self.conflicts(requested).next() { 77340fe15e0SLoGin if flags.contains(MapFlags::MAP_FIXED_NOREPLACE) { 77440fe15e0SLoGin // 如果指定了 MAP_FIXED_NOREPLACE 标志,由于所指定的地址无法成功建立映射,则放弃映射,不对地址做修正 77540fe15e0SLoGin return Err(SystemError::EEXIST); 77640fe15e0SLoGin } 77740fe15e0SLoGin 77840fe15e0SLoGin if flags.contains(MapFlags::MAP_FIXED) { 77940fe15e0SLoGin // todo: 支持MAP_FIXED标志对已有的VMA进行覆盖 78040fe15e0SLoGin return Err(SystemError::EOPNOTSUPP_OR_ENOTSUP); 78140fe15e0SLoGin } 78240fe15e0SLoGin 78340fe15e0SLoGin // 如果没有指定MAP_FIXED标志,那么就对地址做修正 78440fe15e0SLoGin let requested = self.find_free(min_vaddr, size).ok_or(SystemError::ENOMEM)?; 78540fe15e0SLoGin return Ok(requested); 78640fe15e0SLoGin } 78740fe15e0SLoGin 78840fe15e0SLoGin return Ok(requested); 78940fe15e0SLoGin } 79040fe15e0SLoGin 79140fe15e0SLoGin /// 在当前进程的地址空间中,保留一个指定大小的区域,使得该区域不在空洞中。 79240fe15e0SLoGin /// 该函数会修改vm_holes中的空洞信息。 79340fe15e0SLoGin /// 79440fe15e0SLoGin /// @param region 要保留的区域 79540fe15e0SLoGin /// 79640fe15e0SLoGin /// 请注意,在调用本函数之前,必须先确定region所在范围内没有VMA。 79740fe15e0SLoGin fn reserve_hole(&mut self, region: &VirtRegion) { 79840fe15e0SLoGin let prev_hole: Option<(&VirtAddr, &mut usize)> = 799971462beSGnoCiYeH self.vm_holes.range_mut(..=region.start()).next_back(); 80040fe15e0SLoGin 80140fe15e0SLoGin if let Some((prev_hole_vaddr, prev_hole_size)) = prev_hole { 80240fe15e0SLoGin let prev_hole_end = prev_hole_vaddr.add(*prev_hole_size); 80340fe15e0SLoGin 80440fe15e0SLoGin if prev_hole_end > region.start() { 80540fe15e0SLoGin // 如果前一个空洞的结束地址大于当前空洞的起始地址,那么就需要调整前一个空洞的大小。 80640fe15e0SLoGin *prev_hole_size = region.start().data() - prev_hole_vaddr.data(); 80740fe15e0SLoGin } 80840fe15e0SLoGin 80940fe15e0SLoGin if prev_hole_end > region.end() { 81040fe15e0SLoGin // 如果前一个空洞的结束地址大于当前空洞的结束地址,那么就需要增加一个新的空洞。 81140fe15e0SLoGin self.vm_holes 81240fe15e0SLoGin .insert(region.end(), prev_hole_end - region.end()); 81340fe15e0SLoGin } 81440fe15e0SLoGin } 81540fe15e0SLoGin } 81640fe15e0SLoGin 81740fe15e0SLoGin /// 在当前进程的地址空间中,释放一个指定大小的区域,使得该区域成为一个空洞。 81840fe15e0SLoGin /// 该函数会修改vm_holes中的空洞信息。 81940fe15e0SLoGin fn unreserve_hole(&mut self, region: &VirtRegion) { 82040fe15e0SLoGin // 如果将要插入的空洞与后一个空洞相邻,那么就需要合并。 82140fe15e0SLoGin let next_hole_size: Option<usize> = self.vm_holes.remove(®ion.end()); 82240fe15e0SLoGin 82340fe15e0SLoGin if let Some((_prev_hole_vaddr, prev_hole_size)) = self 82440fe15e0SLoGin .vm_holes 82540fe15e0SLoGin .range_mut(..region.start()) 82640fe15e0SLoGin .next_back() 82740fe15e0SLoGin .filter(|(offset, size)| offset.data() + **size == region.start().data()) 82840fe15e0SLoGin { 82940fe15e0SLoGin *prev_hole_size += region.size() + next_hole_size.unwrap_or(0); 83040fe15e0SLoGin } else { 83140fe15e0SLoGin self.vm_holes 83240fe15e0SLoGin .insert(region.start(), region.size() + next_hole_size.unwrap_or(0)); 83340fe15e0SLoGin } 83440fe15e0SLoGin } 83540fe15e0SLoGin 83640fe15e0SLoGin /// 在当前进程的映射关系中,插入一个新的VMA。 83740fe15e0SLoGin pub fn insert_vma(&mut self, vma: Arc<LockedVMA>) { 838b5b571e0SLoGin let region = vma.lock().region; 83940fe15e0SLoGin // 要求插入的地址范围必须是空闲的,也就是说,当前进程的地址空间中,不能有任何与之重叠的VMA。 84040fe15e0SLoGin assert!(self.conflicts(region).next().is_none()); 84140fe15e0SLoGin self.reserve_hole(®ion); 84240fe15e0SLoGin 84340fe15e0SLoGin self.vmas.insert(vma); 84440fe15e0SLoGin } 84540fe15e0SLoGin 84640fe15e0SLoGin /// @brief 删除一个VMA,并把对应的地址空间加入空洞中。 84740fe15e0SLoGin /// 84840fe15e0SLoGin /// 这里不会取消VMA对应的地址的映射 84940fe15e0SLoGin /// 85040fe15e0SLoGin /// @param region 要删除的VMA所在的地址范围 85140fe15e0SLoGin /// 85240fe15e0SLoGin /// @return 如果成功删除了VMA,则返回被删除的VMA,否则返回None 85340fe15e0SLoGin /// 如果没有可以删除的VMA,则不会执行删除操作,并报告失败。 85440fe15e0SLoGin pub fn remove_vma(&mut self, region: &VirtRegion) -> Option<Arc<LockedVMA>> { 85540fe15e0SLoGin // 请注意,由于这里会对每个VMA加锁,因此性能很低 85640fe15e0SLoGin let vma: Arc<LockedVMA> = self 85740fe15e0SLoGin .vmas 85840fe15e0SLoGin .drain_filter(|vma| vma.lock().region == *region) 85940fe15e0SLoGin .next()?; 86040fe15e0SLoGin self.unreserve_hole(region); 86140fe15e0SLoGin 86240fe15e0SLoGin return Some(vma); 86340fe15e0SLoGin } 86440fe15e0SLoGin 86540fe15e0SLoGin /// @brief Get the iterator of all VMAs in this process. 86640fe15e0SLoGin pub fn iter_vmas(&self) -> hashbrown::hash_set::Iter<Arc<LockedVMA>> { 86740fe15e0SLoGin return self.vmas.iter(); 86840fe15e0SLoGin } 86940fe15e0SLoGin } 87040fe15e0SLoGin 87140fe15e0SLoGin impl Default for UserMappings { 87240fe15e0SLoGin fn default() -> Self { 87340fe15e0SLoGin return Self::new(); 87440fe15e0SLoGin } 87540fe15e0SLoGin } 87640fe15e0SLoGin 87740fe15e0SLoGin /// 加了锁的VMA 87840fe15e0SLoGin /// 87940fe15e0SLoGin /// 备注:进行性能测试,看看SpinLock和RwLock哪个更快。 88040fe15e0SLoGin #[derive(Debug)] 881*56cc4dbeSJomo pub struct LockedVMA { 882*56cc4dbeSJomo /// 用于计算哈希值,避免总是获取vma锁来计算哈希值 883*56cc4dbeSJomo id: usize, 884*56cc4dbeSJomo vma: SpinLock<VMA>, 885*56cc4dbeSJomo } 88640fe15e0SLoGin 88740fe15e0SLoGin impl core::hash::Hash for LockedVMA { 88840fe15e0SLoGin fn hash<H: Hasher>(&self, state: &mut H) { 889*56cc4dbeSJomo self.id.hash(state); 89040fe15e0SLoGin } 89140fe15e0SLoGin } 89240fe15e0SLoGin 89340fe15e0SLoGin impl PartialEq for LockedVMA { 89440fe15e0SLoGin fn eq(&self, other: &Self) -> bool { 895*56cc4dbeSJomo self.id.eq(&other.id) 89640fe15e0SLoGin } 89740fe15e0SLoGin } 89840fe15e0SLoGin 89940fe15e0SLoGin impl Eq for LockedVMA {} 90040fe15e0SLoGin 90140fe15e0SLoGin #[allow(dead_code)] 90240fe15e0SLoGin impl LockedVMA { 90340fe15e0SLoGin pub fn new(vma: VMA) -> Arc<Self> { 904*56cc4dbeSJomo let r = Arc::new(Self { 905*56cc4dbeSJomo id: LOCKEDVMA_ID_ALLOCATOR.alloc().unwrap(), 906*56cc4dbeSJomo vma: SpinLock::new(vma), 907*56cc4dbeSJomo }); 908*56cc4dbeSJomo r.vma.lock().self_ref = Arc::downgrade(&r); 90940fe15e0SLoGin return r; 91040fe15e0SLoGin } 91140fe15e0SLoGin 91240fe15e0SLoGin pub fn lock(&self) -> SpinLockGuard<VMA> { 913*56cc4dbeSJomo return self.vma.lock(); 91440fe15e0SLoGin } 91540fe15e0SLoGin 91640fe15e0SLoGin /// 调整当前VMA的页面的标志位 91740fe15e0SLoGin /// 91840fe15e0SLoGin /// TODO:增加调整虚拟页映射的物理地址的功能 91940fe15e0SLoGin /// 92040fe15e0SLoGin /// @param flags 新的标志位 92140fe15e0SLoGin /// @param mapper 页表映射器 92240fe15e0SLoGin /// @param flusher 页表项刷新器 92340fe15e0SLoGin /// 92440fe15e0SLoGin pub fn remap( 92540fe15e0SLoGin &self, 92640fe15e0SLoGin flags: PageFlags<MMArch>, 92740fe15e0SLoGin mapper: &mut PageMapper, 92840fe15e0SLoGin mut flusher: impl Flusher<MMArch>, 92940fe15e0SLoGin ) -> Result<(), SystemError> { 93040fe15e0SLoGin let mut guard = self.lock(); 93140fe15e0SLoGin assert!(guard.mapped); 93240fe15e0SLoGin for page in guard.region.pages() { 93340fe15e0SLoGin // 暂时要求所有的页帧都已经映射到页表 93440fe15e0SLoGin // TODO: 引入Lazy Mapping, 通过缺页中断来映射页帧,这里就不必要求所有的页帧都已经映射到页表了 93540fe15e0SLoGin let r = unsafe { 93640fe15e0SLoGin mapper 93740fe15e0SLoGin .remap(page.virt_address(), flags) 93840fe15e0SLoGin .expect("Failed to remap, beacuse of some page is not mapped") 93940fe15e0SLoGin }; 94040fe15e0SLoGin flusher.consume(r); 94140fe15e0SLoGin } 94240fe15e0SLoGin guard.flags = flags; 94340fe15e0SLoGin return Ok(()); 94440fe15e0SLoGin } 94540fe15e0SLoGin 94640fe15e0SLoGin pub fn unmap(&self, mapper: &mut PageMapper, mut flusher: impl Flusher<MMArch>) { 9471a62e776SLoGin // todo: 如果当前vma与文件相关,完善文件相关的逻辑 9481a62e776SLoGin 94940fe15e0SLoGin let mut guard = self.lock(); 95040fe15e0SLoGin assert!(guard.mapped); 951*56cc4dbeSJomo 952*56cc4dbeSJomo // 获取物理页的anon_vma的守卫 953*56cc4dbeSJomo let mut anon_vma_guard: SpinLockGuard<'_, crate::mm::page::PageManager> = 954*56cc4dbeSJomo page_manager_lock_irasave(); 95540fe15e0SLoGin for page in guard.region.pages() { 95640fe15e0SLoGin let (paddr, _, flush) = unsafe { mapper.unmap_phys(page.virt_address(), true) } 95740fe15e0SLoGin .expect("Failed to unmap, beacuse of some page is not mapped"); 95840fe15e0SLoGin 959*56cc4dbeSJomo // 从anon_vma中删除当前VMA 960*56cc4dbeSJomo let page = anon_vma_guard.get_mut(&paddr); 961*56cc4dbeSJomo page.remove_vma(self); 96240fe15e0SLoGin 963*56cc4dbeSJomo // 如果物理页的anon_vma链表长度为0并且不是共享页,则释放物理页. 964*56cc4dbeSJomo if page.can_deallocate() { 965*56cc4dbeSJomo unsafe { 966*56cc4dbeSJomo deallocate_page_frames( 967*56cc4dbeSJomo PhysPageFrame::new(paddr), 968*56cc4dbeSJomo PageFrameCount::new(1), 969*56cc4dbeSJomo &mut anon_vma_guard, 970*56cc4dbeSJomo ) 971*56cc4dbeSJomo }; 972*56cc4dbeSJomo } 97340fe15e0SLoGin 97440fe15e0SLoGin flusher.consume(flush); 97540fe15e0SLoGin } 97640fe15e0SLoGin guard.mapped = false; 97740fe15e0SLoGin } 97840fe15e0SLoGin 97940fe15e0SLoGin pub fn mapped(&self) -> bool { 980*56cc4dbeSJomo return self.vma.lock().mapped; 98140fe15e0SLoGin } 98240fe15e0SLoGin 98340fe15e0SLoGin /// 将当前VMA进行切分,切分成3个VMA,分别是: 98440fe15e0SLoGin /// 98540fe15e0SLoGin /// 1. 前面的VMA,如果没有则为None 98640fe15e0SLoGin /// 2. 中间的VMA,也就是传入的Region 98740fe15e0SLoGin /// 3. 后面的VMA,如果没有则为None 988*56cc4dbeSJomo pub fn extract(&self, region: VirtRegion, utable: &PageMapper) -> Option<VMASplitResult> { 98940fe15e0SLoGin assert!(region.start().check_aligned(MMArch::PAGE_SIZE)); 99040fe15e0SLoGin assert!(region.end().check_aligned(MMArch::PAGE_SIZE)); 99140fe15e0SLoGin 99240fe15e0SLoGin let mut guard = self.lock(); 99340fe15e0SLoGin { 99440fe15e0SLoGin // 如果传入的region不在当前VMA的范围内,则直接返回None 99540fe15e0SLoGin if unlikely(region.start() < guard.region.start() || region.end() > guard.region.end()) 99640fe15e0SLoGin { 99740fe15e0SLoGin return None; 99840fe15e0SLoGin } 99940fe15e0SLoGin 100040fe15e0SLoGin let intersect: Option<VirtRegion> = guard.region.intersect(®ion); 100140fe15e0SLoGin // 如果当前VMA不包含region,则直接返回None 100240fe15e0SLoGin if unlikely(intersect.is_none()) { 100340fe15e0SLoGin return None; 100440fe15e0SLoGin } 100540fe15e0SLoGin let intersect: VirtRegion = intersect.unwrap(); 100640fe15e0SLoGin if unlikely(intersect == guard.region) { 100740fe15e0SLoGin // 如果当前VMA完全包含region,则直接返回当前VMA 1008b5b571e0SLoGin return Some(VMASplitResult::new( 1009b5b571e0SLoGin None, 1010b5b571e0SLoGin guard.self_ref.upgrade().unwrap(), 1011b5b571e0SLoGin None, 1012b5b571e0SLoGin )); 101340fe15e0SLoGin } 101440fe15e0SLoGin } 101540fe15e0SLoGin 101640fe15e0SLoGin let before: Option<Arc<LockedVMA>> = guard.region.before(®ion).map(|virt_region| { 101740fe15e0SLoGin let mut vma: VMA = unsafe { guard.clone() }; 101840fe15e0SLoGin vma.region = virt_region; 101940fe15e0SLoGin 102040fe15e0SLoGin let vma: Arc<LockedVMA> = LockedVMA::new(vma); 102140fe15e0SLoGin vma 102240fe15e0SLoGin }); 102340fe15e0SLoGin 102440fe15e0SLoGin let after: Option<Arc<LockedVMA>> = guard.region.after(®ion).map(|virt_region| { 102540fe15e0SLoGin let mut vma: VMA = unsafe { guard.clone() }; 102640fe15e0SLoGin vma.region = virt_region; 102740fe15e0SLoGin 102840fe15e0SLoGin let vma: Arc<LockedVMA> = LockedVMA::new(vma); 102940fe15e0SLoGin vma 103040fe15e0SLoGin }); 103140fe15e0SLoGin 1032*56cc4dbeSJomo // 重新设置before、after这两个VMA里面的物理页的anon_vma 1033*56cc4dbeSJomo let mut anon_vma_guard = page_manager_lock_irasave(); 1034*56cc4dbeSJomo if let Some(before) = before.clone() { 1035*56cc4dbeSJomo let virt_iter = before.lock().region.iter_pages(); 1036*56cc4dbeSJomo for frame in virt_iter { 1037*56cc4dbeSJomo let paddr = utable.translate(frame.virt_address()).unwrap().0; 1038*56cc4dbeSJomo let page = anon_vma_guard.get_mut(&paddr); 1039*56cc4dbeSJomo page.insert_vma(before.clone()); 1040*56cc4dbeSJomo page.remove_vma(self); 1041*56cc4dbeSJomo } 1042*56cc4dbeSJomo } 104340fe15e0SLoGin 1044*56cc4dbeSJomo if let Some(after) = after.clone() { 1045*56cc4dbeSJomo let virt_iter = after.lock().region.iter_pages(); 1046*56cc4dbeSJomo for frame in virt_iter { 1047*56cc4dbeSJomo let paddr = utable.translate(frame.virt_address()).unwrap().0; 1048*56cc4dbeSJomo let page = anon_vma_guard.get_mut(&paddr); 1049*56cc4dbeSJomo page.insert_vma(after.clone()); 1050*56cc4dbeSJomo page.remove_vma(self); 1051*56cc4dbeSJomo } 1052*56cc4dbeSJomo } 1053*56cc4dbeSJomo 1054*56cc4dbeSJomo guard.region = region; 105540fe15e0SLoGin 1056b5b571e0SLoGin return Some(VMASplitResult::new( 1057b5b571e0SLoGin before, 1058b5b571e0SLoGin guard.self_ref.upgrade().unwrap(), 1059b5b571e0SLoGin after, 1060b5b571e0SLoGin )); 1061b5b571e0SLoGin } 1062b5b571e0SLoGin } 1063b5b571e0SLoGin 1064*56cc4dbeSJomo impl Drop for LockedVMA { 1065*56cc4dbeSJomo fn drop(&mut self) { 1066*56cc4dbeSJomo LOCKEDVMA_ID_ALLOCATOR.free(self.id); 1067*56cc4dbeSJomo } 1068*56cc4dbeSJomo } 1069*56cc4dbeSJomo 1070b5b571e0SLoGin /// VMA切分结果 1071b5b571e0SLoGin pub struct VMASplitResult { 1072b5b571e0SLoGin pub prev: Option<Arc<LockedVMA>>, 1073b5b571e0SLoGin pub middle: Arc<LockedVMA>, 1074b5b571e0SLoGin pub after: Option<Arc<LockedVMA>>, 1075b5b571e0SLoGin } 1076b5b571e0SLoGin 1077b5b571e0SLoGin impl VMASplitResult { 1078b5b571e0SLoGin pub fn new( 1079b5b571e0SLoGin prev: Option<Arc<LockedVMA>>, 1080b5b571e0SLoGin middle: Arc<LockedVMA>, 1081b5b571e0SLoGin post: Option<Arc<LockedVMA>>, 1082b5b571e0SLoGin ) -> Self { 1083b5b571e0SLoGin Self { 1084b5b571e0SLoGin prev, 1085b5b571e0SLoGin middle, 1086b5b571e0SLoGin after: post, 1087b5b571e0SLoGin } 108840fe15e0SLoGin } 108940fe15e0SLoGin } 109040fe15e0SLoGin 109140fe15e0SLoGin /// @brief 虚拟内存区域 109240fe15e0SLoGin #[derive(Debug)] 109340fe15e0SLoGin pub struct VMA { 109440fe15e0SLoGin /// 虚拟内存区域对应的虚拟地址范围 109540fe15e0SLoGin region: VirtRegion, 10964cfa009bSJomo /// 虚拟内存区域标志 10974cfa009bSJomo vm_flags: VmFlags, 109840fe15e0SLoGin /// VMA内的页帧的标志 109940fe15e0SLoGin flags: PageFlags<MMArch>, 110040fe15e0SLoGin /// VMA内的页帧是否已经映射到页表 110140fe15e0SLoGin mapped: bool, 110240fe15e0SLoGin /// VMA所属的用户地址空间 110340fe15e0SLoGin user_address_space: Option<Weak<AddressSpace>>, 110440fe15e0SLoGin self_ref: Weak<LockedVMA>, 1105971462beSGnoCiYeH 1106971462beSGnoCiYeH provider: Provider, 110740fe15e0SLoGin } 110840fe15e0SLoGin 110940fe15e0SLoGin impl core::hash::Hash for VMA { 111040fe15e0SLoGin fn hash<H: Hasher>(&self, state: &mut H) { 111140fe15e0SLoGin self.region.hash(state); 111240fe15e0SLoGin self.flags.hash(state); 111340fe15e0SLoGin self.mapped.hash(state); 111440fe15e0SLoGin } 111540fe15e0SLoGin } 111640fe15e0SLoGin 1117971462beSGnoCiYeH /// 描述不同类型的内存提供者或资源 1118971462beSGnoCiYeH #[derive(Debug)] 1119971462beSGnoCiYeH pub enum Provider { 1120971462beSGnoCiYeH Allocated, // TODO:其他 1121971462beSGnoCiYeH } 1122971462beSGnoCiYeH 112340fe15e0SLoGin #[allow(dead_code)] 112440fe15e0SLoGin impl VMA { 11254cfa009bSJomo pub fn new( 11264cfa009bSJomo region: VirtRegion, 11274cfa009bSJomo vm_flags: VmFlags, 11284cfa009bSJomo flags: PageFlags<MMArch>, 11294cfa009bSJomo mapped: bool, 11304cfa009bSJomo ) -> Self { 11314cfa009bSJomo VMA { 11324cfa009bSJomo region, 11334cfa009bSJomo vm_flags, 11344cfa009bSJomo flags, 11354cfa009bSJomo mapped, 11364cfa009bSJomo user_address_space: None, 11374cfa009bSJomo self_ref: Weak::default(), 11384cfa009bSJomo provider: Provider::Allocated, 11394cfa009bSJomo } 11404cfa009bSJomo } 11414cfa009bSJomo 114240fe15e0SLoGin pub fn region(&self) -> &VirtRegion { 114340fe15e0SLoGin return &self.region; 114440fe15e0SLoGin } 114540fe15e0SLoGin 11464cfa009bSJomo pub fn vm_flags(&self) -> &VmFlags { 11474cfa009bSJomo return &self.vm_flags; 11484cfa009bSJomo } 11494cfa009bSJomo 11504cfa009bSJomo pub fn set_vm_flags(&mut self, vm_flags: VmFlags) { 11514cfa009bSJomo self.vm_flags = vm_flags; 11524cfa009bSJomo } 11534cfa009bSJomo 11544cfa009bSJomo pub fn set_region_size(&mut self, new_region_size: usize) { 11554cfa009bSJomo self.region.set_size(new_region_size); 11564cfa009bSJomo } 11574cfa009bSJomo 115840fe15e0SLoGin /// # 拷贝当前VMA的内容 115940fe15e0SLoGin /// 116040fe15e0SLoGin /// ### 安全性 116140fe15e0SLoGin /// 116240fe15e0SLoGin /// 由于这样操作可能由于错误的拷贝,导致内存泄露、内存重复释放等问题,所以需要小心使用。 116340fe15e0SLoGin pub unsafe fn clone(&self) -> Self { 116440fe15e0SLoGin return Self { 116540fe15e0SLoGin region: self.region, 11664cfa009bSJomo vm_flags: self.vm_flags, 116740fe15e0SLoGin flags: self.flags, 116840fe15e0SLoGin mapped: self.mapped, 116940fe15e0SLoGin user_address_space: self.user_address_space.clone(), 117040fe15e0SLoGin self_ref: self.self_ref.clone(), 1171971462beSGnoCiYeH provider: Provider::Allocated, 117240fe15e0SLoGin }; 117340fe15e0SLoGin } 117440fe15e0SLoGin 117540fe15e0SLoGin #[inline(always)] 117640fe15e0SLoGin pub fn flags(&self) -> PageFlags<MMArch> { 117740fe15e0SLoGin return self.flags; 117840fe15e0SLoGin } 117940fe15e0SLoGin 118040fe15e0SLoGin pub fn pages(&self) -> VirtPageFrameIter { 118140fe15e0SLoGin return VirtPageFrameIter::new( 118240fe15e0SLoGin VirtPageFrame::new(self.region.start()), 118340fe15e0SLoGin VirtPageFrame::new(self.region.end()), 118440fe15e0SLoGin ); 118540fe15e0SLoGin } 118640fe15e0SLoGin 118740fe15e0SLoGin pub fn remap( 118840fe15e0SLoGin &mut self, 118940fe15e0SLoGin flags: PageFlags<MMArch>, 119040fe15e0SLoGin mapper: &mut PageMapper, 119140fe15e0SLoGin mut flusher: impl Flusher<MMArch>, 119240fe15e0SLoGin ) -> Result<(), SystemError> { 119340fe15e0SLoGin assert!(self.mapped); 119440fe15e0SLoGin for page in self.region.pages() { 119540fe15e0SLoGin // kdebug!("remap page {:?}", page.virt_address()); 119640fe15e0SLoGin // 暂时要求所有的页帧都已经映射到页表 119740fe15e0SLoGin // TODO: 引入Lazy Mapping, 通过缺页中断来映射页帧,这里就不必要求所有的页帧都已经映射到页表了 119840fe15e0SLoGin let r = unsafe { 119940fe15e0SLoGin mapper 120040fe15e0SLoGin .remap(page.virt_address(), flags) 120140fe15e0SLoGin .expect("Failed to remap, beacuse of some page is not mapped") 120240fe15e0SLoGin }; 120340fe15e0SLoGin // kdebug!("consume page {:?}", page.virt_address()); 120440fe15e0SLoGin flusher.consume(r); 120540fe15e0SLoGin // kdebug!("remap page {:?} done", page.virt_address()); 120640fe15e0SLoGin } 120740fe15e0SLoGin self.flags = flags; 120840fe15e0SLoGin return Ok(()); 120940fe15e0SLoGin } 121040fe15e0SLoGin 121140fe15e0SLoGin /// 检查当前VMA是否可以拥有指定的标志位 121240fe15e0SLoGin /// 121340fe15e0SLoGin /// ## 参数 121440fe15e0SLoGin /// 121540fe15e0SLoGin /// - `prot_flags` 要检查的标志位 121640fe15e0SLoGin pub fn can_have_flags(&self, prot_flags: ProtFlags) -> bool { 1217971462beSGnoCiYeH let is_downgrade = (self.flags.has_write() || !prot_flags.contains(ProtFlags::PROT_WRITE)) 121840fe15e0SLoGin && (self.flags.has_execute() || !prot_flags.contains(ProtFlags::PROT_EXEC)); 1219971462beSGnoCiYeH 1220971462beSGnoCiYeH match self.provider { 1221971462beSGnoCiYeH Provider::Allocated { .. } => true, 1222971462beSGnoCiYeH 1223971462beSGnoCiYeH #[allow(unreachable_patterns)] 1224971462beSGnoCiYeH _ => is_downgrade, 1225971462beSGnoCiYeH } 122640fe15e0SLoGin } 122740fe15e0SLoGin 122840fe15e0SLoGin /// 把物理地址映射到虚拟地址 122940fe15e0SLoGin /// 123040fe15e0SLoGin /// @param phys 要映射的物理地址 123140fe15e0SLoGin /// @param destination 要映射到的虚拟地址 123240fe15e0SLoGin /// @param count 要映射的页帧数量 123340fe15e0SLoGin /// @param flags 页面标志位 123440fe15e0SLoGin /// @param mapper 页表映射器 123540fe15e0SLoGin /// @param flusher 页表项刷新器 123640fe15e0SLoGin /// 123740fe15e0SLoGin /// @return 返回映射后的虚拟内存区域 123840fe15e0SLoGin pub fn physmap( 123940fe15e0SLoGin phys: PhysPageFrame, 124040fe15e0SLoGin destination: VirtPageFrame, 124140fe15e0SLoGin count: PageFrameCount, 12424cfa009bSJomo vm_flags: VmFlags, 124340fe15e0SLoGin flags: PageFlags<MMArch>, 124440fe15e0SLoGin mapper: &mut PageMapper, 124540fe15e0SLoGin mut flusher: impl Flusher<MMArch>, 124640fe15e0SLoGin ) -> Result<Arc<LockedVMA>, SystemError> { 124740fe15e0SLoGin let mut cur_phy = phys; 124840fe15e0SLoGin let mut cur_dest = destination; 124940fe15e0SLoGin 125040fe15e0SLoGin for _ in 0..count.data() { 125140fe15e0SLoGin // 将物理页帧映射到虚拟页帧 1252*56cc4dbeSJomo let r = 1253*56cc4dbeSJomo unsafe { mapper.map_phys(cur_dest.virt_address(), cur_phy.phys_address(), flags) } 125440fe15e0SLoGin .expect("Failed to map phys, may be OOM error"); 125540fe15e0SLoGin 125640fe15e0SLoGin // todo: 增加OOM处理 125740fe15e0SLoGin 125840fe15e0SLoGin // 刷新TLB 125940fe15e0SLoGin flusher.consume(r); 126040fe15e0SLoGin 126140fe15e0SLoGin cur_phy = cur_phy.next(); 126240fe15e0SLoGin cur_dest = cur_dest.next(); 126340fe15e0SLoGin } 126440fe15e0SLoGin 126540fe15e0SLoGin let r: Arc<LockedVMA> = LockedVMA::new(VMA { 126640fe15e0SLoGin region: VirtRegion::new(destination.virt_address(), count.data() * MMArch::PAGE_SIZE), 12674cfa009bSJomo vm_flags, 126840fe15e0SLoGin flags, 126940fe15e0SLoGin mapped: true, 127040fe15e0SLoGin user_address_space: None, 127140fe15e0SLoGin self_ref: Weak::default(), 1272971462beSGnoCiYeH provider: Provider::Allocated, 127340fe15e0SLoGin }); 1274*56cc4dbeSJomo 1275*56cc4dbeSJomo // 将VMA加入到anon_vma中 1276*56cc4dbeSJomo let mut anon_vma_guard = page_manager_lock_irasave(); 1277*56cc4dbeSJomo cur_phy = phys; 1278*56cc4dbeSJomo for _ in 0..count.data() { 1279*56cc4dbeSJomo let paddr = cur_phy.phys_address(); 1280*56cc4dbeSJomo let page = anon_vma_guard.get_mut(&paddr); 1281*56cc4dbeSJomo page.insert_vma(r.clone()); 1282*56cc4dbeSJomo cur_phy = cur_phy.next(); 1283*56cc4dbeSJomo } 1284*56cc4dbeSJomo 128540fe15e0SLoGin return Ok(r); 128640fe15e0SLoGin } 128740fe15e0SLoGin 128840fe15e0SLoGin /// 从页分配器中分配一些物理页,并把它们映射到指定的虚拟地址,然后创建VMA 128940fe15e0SLoGin /// 129040fe15e0SLoGin /// @param destination 要映射到的虚拟地址 129140fe15e0SLoGin /// @param count 要映射的页帧数量 129240fe15e0SLoGin /// @param flags 页面标志位 129340fe15e0SLoGin /// @param mapper 页表映射器 129440fe15e0SLoGin /// @param flusher 页表项刷新器 129540fe15e0SLoGin /// 129640fe15e0SLoGin /// @return 返回映射后的虚拟内存区域 129740fe15e0SLoGin pub fn zeroed( 129840fe15e0SLoGin destination: VirtPageFrame, 129940fe15e0SLoGin page_count: PageFrameCount, 13004cfa009bSJomo vm_flags: VmFlags, 130140fe15e0SLoGin flags: PageFlags<MMArch>, 130240fe15e0SLoGin mapper: &mut PageMapper, 130340fe15e0SLoGin mut flusher: impl Flusher<MMArch>, 130440fe15e0SLoGin ) -> Result<Arc<LockedVMA>, SystemError> { 130540fe15e0SLoGin let mut cur_dest: VirtPageFrame = destination; 130640fe15e0SLoGin // kdebug!( 130740fe15e0SLoGin // "VMA::zeroed: page_count = {:?}, destination={destination:?}", 130840fe15e0SLoGin // page_count 130940fe15e0SLoGin // ); 131040fe15e0SLoGin for _ in 0..page_count.data() { 131140fe15e0SLoGin // kdebug!( 131240fe15e0SLoGin // "VMA::zeroed: cur_dest={cur_dest:?}, vaddr = {:?}", 131340fe15e0SLoGin // cur_dest.virt_address() 131440fe15e0SLoGin // ); 131540fe15e0SLoGin let r = unsafe { mapper.map(cur_dest.virt_address(), flags) } 131640fe15e0SLoGin .expect("Failed to map zero, may be OOM error"); 131740fe15e0SLoGin // todo: 增加OOM处理 131840fe15e0SLoGin 131940fe15e0SLoGin // 稍后再刷新TLB,这里取消刷新 132040fe15e0SLoGin flusher.consume(r); 132140fe15e0SLoGin cur_dest = cur_dest.next(); 132240fe15e0SLoGin } 132340fe15e0SLoGin let r = LockedVMA::new(VMA { 132440fe15e0SLoGin region: VirtRegion::new( 132540fe15e0SLoGin destination.virt_address(), 132640fe15e0SLoGin page_count.data() * MMArch::PAGE_SIZE, 132740fe15e0SLoGin ), 13284cfa009bSJomo vm_flags, 132940fe15e0SLoGin flags, 133040fe15e0SLoGin mapped: true, 133140fe15e0SLoGin user_address_space: None, 133240fe15e0SLoGin self_ref: Weak::default(), 1333971462beSGnoCiYeH provider: Provider::Allocated, 133440fe15e0SLoGin }); 133540fe15e0SLoGin drop(flusher); 133640fe15e0SLoGin // kdebug!("VMA::zeroed: flusher dropped"); 133740fe15e0SLoGin 1338*56cc4dbeSJomo // 清空这些内存并将VMA加入到anon_vma中 1339*56cc4dbeSJomo let mut anon_vma_guard = page_manager_lock_irasave(); 13409550910aSChiichen let virt_iter: VirtPageFrameIter = 13419550910aSChiichen VirtPageFrameIter::new(destination, destination.add(page_count)); 134240fe15e0SLoGin for frame in virt_iter { 134340fe15e0SLoGin let paddr = mapper.translate(frame.virt_address()).unwrap().0; 134440fe15e0SLoGin 1345*56cc4dbeSJomo // 将VMA加入到anon_vma 1346*56cc4dbeSJomo let page = anon_vma_guard.get_mut(&paddr); 1347*56cc4dbeSJomo page.insert_vma(r.clone()); 1348*56cc4dbeSJomo 1349*56cc4dbeSJomo // 清空内存 135040fe15e0SLoGin unsafe { 135140fe15e0SLoGin let vaddr = MMArch::phys_2_virt(paddr).unwrap(); 135240fe15e0SLoGin MMArch::write_bytes(vaddr, 0, MMArch::PAGE_SIZE); 135340fe15e0SLoGin } 135440fe15e0SLoGin } 135540fe15e0SLoGin // kdebug!("VMA::zeroed: done"); 135640fe15e0SLoGin return Ok(r); 135740fe15e0SLoGin } 135840fe15e0SLoGin } 135940fe15e0SLoGin 136040fe15e0SLoGin impl Drop for VMA { 136140fe15e0SLoGin fn drop(&mut self) { 136240fe15e0SLoGin // 当VMA被释放时,需要确保它已经被从页表中解除映射 136340fe15e0SLoGin assert!(!self.mapped, "VMA is still mapped"); 136440fe15e0SLoGin } 136540fe15e0SLoGin } 136640fe15e0SLoGin 136740fe15e0SLoGin impl PartialEq for VMA { 136840fe15e0SLoGin fn eq(&self, other: &Self) -> bool { 136940fe15e0SLoGin return self.region == other.region; 137040fe15e0SLoGin } 137140fe15e0SLoGin } 137240fe15e0SLoGin 137340fe15e0SLoGin impl Eq for VMA {} 137440fe15e0SLoGin 137540fe15e0SLoGin impl PartialOrd for VMA { 137640fe15e0SLoGin fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> { 1377b5b571e0SLoGin Some(self.cmp(other)) 137840fe15e0SLoGin } 137940fe15e0SLoGin } 138040fe15e0SLoGin 138140fe15e0SLoGin impl Ord for VMA { 138240fe15e0SLoGin fn cmp(&self, other: &Self) -> cmp::Ordering { 138340fe15e0SLoGin return self.region.cmp(&other.region); 138440fe15e0SLoGin } 138540fe15e0SLoGin } 138640fe15e0SLoGin 138740fe15e0SLoGin #[derive(Debug)] 138840fe15e0SLoGin pub struct UserStack { 138940fe15e0SLoGin // 栈底地址 139040fe15e0SLoGin stack_bottom: VirtAddr, 139140fe15e0SLoGin // 当前已映射的大小 139240fe15e0SLoGin mapped_size: usize, 139340fe15e0SLoGin /// 栈顶地址(这个值需要仔细确定!因为它可能不会实时与用户栈的真实栈顶保持一致!要小心!) 139440fe15e0SLoGin current_sp: VirtAddr, 139540fe15e0SLoGin } 139640fe15e0SLoGin 139740fe15e0SLoGin impl UserStack { 139840fe15e0SLoGin /// 默认的用户栈底地址 139940fe15e0SLoGin pub const DEFAULT_USER_STACK_BOTTOM: VirtAddr = MMArch::USER_STACK_START; 140040fe15e0SLoGin /// 默认的用户栈大小为8MB 140140fe15e0SLoGin pub const DEFAULT_USER_STACK_SIZE: usize = 8 * 1024 * 1024; 140240fe15e0SLoGin /// 用户栈的保护页数量 140340fe15e0SLoGin pub const GUARD_PAGES_NUM: usize = 4; 140440fe15e0SLoGin 140540fe15e0SLoGin /// 创建一个用户栈 140640fe15e0SLoGin pub fn new( 140740fe15e0SLoGin vm: &mut InnerAddressSpace, 140840fe15e0SLoGin stack_bottom: Option<VirtAddr>, 140940fe15e0SLoGin stack_size: usize, 141040fe15e0SLoGin ) -> Result<Self, SystemError> { 141140fe15e0SLoGin let stack_bottom = stack_bottom.unwrap_or(Self::DEFAULT_USER_STACK_BOTTOM); 141240fe15e0SLoGin assert!(stack_bottom.check_aligned(MMArch::PAGE_SIZE)); 141340fe15e0SLoGin 141440fe15e0SLoGin // 分配用户栈的保护页 141540fe15e0SLoGin let guard_size = Self::GUARD_PAGES_NUM * MMArch::PAGE_SIZE; 141640fe15e0SLoGin let actual_stack_bottom = stack_bottom - guard_size; 141740fe15e0SLoGin 141840fe15e0SLoGin let mut prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE; 141940fe15e0SLoGin let map_flags = 142040fe15e0SLoGin MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS | MapFlags::MAP_FIXED_NOREPLACE; 142140fe15e0SLoGin // kdebug!( 142240fe15e0SLoGin // "map anonymous stack: {:?} {}", 142340fe15e0SLoGin // actual_stack_bottom, 142440fe15e0SLoGin // guard_size 142540fe15e0SLoGin // ); 142640fe15e0SLoGin vm.map_anonymous( 142740fe15e0SLoGin actual_stack_bottom, 142840fe15e0SLoGin guard_size, 142940fe15e0SLoGin prot_flags, 143040fe15e0SLoGin map_flags, 143140fe15e0SLoGin false, 143240fe15e0SLoGin )?; 143340fe15e0SLoGin // test_buddy(); 143440fe15e0SLoGin // 设置保护页只读 143540fe15e0SLoGin prot_flags.remove(ProtFlags::PROT_WRITE); 143640fe15e0SLoGin // kdebug!( 143740fe15e0SLoGin // "to mprotect stack guard pages: {:?} {}", 143840fe15e0SLoGin // actual_stack_bottom, 143940fe15e0SLoGin // guard_size 144040fe15e0SLoGin // ); 144140fe15e0SLoGin vm.mprotect( 144240fe15e0SLoGin VirtPageFrame::new(actual_stack_bottom), 144340fe15e0SLoGin PageFrameCount::new(Self::GUARD_PAGES_NUM), 144440fe15e0SLoGin prot_flags, 144540fe15e0SLoGin )?; 144640fe15e0SLoGin 144740fe15e0SLoGin // kdebug!( 144840fe15e0SLoGin // "mprotect stack guard pages done: {:?} {}", 144940fe15e0SLoGin // actual_stack_bottom, 145040fe15e0SLoGin // guard_size 145140fe15e0SLoGin // ); 145240fe15e0SLoGin 145340fe15e0SLoGin let mut user_stack = UserStack { 145440fe15e0SLoGin stack_bottom: actual_stack_bottom, 145540fe15e0SLoGin mapped_size: guard_size, 145640fe15e0SLoGin current_sp: actual_stack_bottom - guard_size, 145740fe15e0SLoGin }; 145840fe15e0SLoGin 145940fe15e0SLoGin // kdebug!("extend user stack: {:?} {}", stack_bottom, stack_size); 146040fe15e0SLoGin // 分配用户栈 146140fe15e0SLoGin user_stack.initial_extend(vm, stack_size)?; 146240fe15e0SLoGin // kdebug!("user stack created: {:?} {}", stack_bottom, stack_size); 146340fe15e0SLoGin return Ok(user_stack); 146440fe15e0SLoGin } 146540fe15e0SLoGin 146640fe15e0SLoGin fn initial_extend( 146740fe15e0SLoGin &mut self, 146840fe15e0SLoGin vm: &mut InnerAddressSpace, 146940fe15e0SLoGin mut bytes: usize, 147040fe15e0SLoGin ) -> Result<(), SystemError> { 147140fe15e0SLoGin let prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC; 147240fe15e0SLoGin let map_flags = MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS; 147340fe15e0SLoGin 147440fe15e0SLoGin bytes = page_align_up(bytes); 147540fe15e0SLoGin self.mapped_size += bytes; 147640fe15e0SLoGin 147740fe15e0SLoGin vm.map_anonymous( 147840fe15e0SLoGin self.stack_bottom - self.mapped_size, 147940fe15e0SLoGin bytes, 148040fe15e0SLoGin prot_flags, 148140fe15e0SLoGin map_flags, 148240fe15e0SLoGin false, 148340fe15e0SLoGin )?; 148440fe15e0SLoGin 148540fe15e0SLoGin return Ok(()); 148640fe15e0SLoGin } 148740fe15e0SLoGin 148840fe15e0SLoGin /// 扩展用户栈 148940fe15e0SLoGin /// 149040fe15e0SLoGin /// ## 参数 149140fe15e0SLoGin /// 149240fe15e0SLoGin /// - `vm` 用户地址空间结构体 149340fe15e0SLoGin /// - `bytes` 要扩展的字节数 149440fe15e0SLoGin /// 149540fe15e0SLoGin /// ## 返回值 149640fe15e0SLoGin /// 149740fe15e0SLoGin /// - **Ok(())** 扩展成功 149840fe15e0SLoGin /// - **Err(SystemError)** 扩展失败 149940fe15e0SLoGin #[allow(dead_code)] 150040fe15e0SLoGin pub fn extend( 150140fe15e0SLoGin &mut self, 150240fe15e0SLoGin vm: &mut RwLockWriteGuard<InnerAddressSpace>, 150340fe15e0SLoGin mut bytes: usize, 150440fe15e0SLoGin ) -> Result<(), SystemError> { 150540fe15e0SLoGin let prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC; 150640fe15e0SLoGin let map_flags = MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS; 150740fe15e0SLoGin 150840fe15e0SLoGin bytes = page_align_up(bytes); 150940fe15e0SLoGin self.mapped_size += bytes; 151040fe15e0SLoGin 151140fe15e0SLoGin vm.map_anonymous( 151240fe15e0SLoGin self.stack_bottom - self.mapped_size, 151340fe15e0SLoGin bytes, 151440fe15e0SLoGin prot_flags, 151540fe15e0SLoGin map_flags, 151640fe15e0SLoGin false, 151740fe15e0SLoGin )?; 151840fe15e0SLoGin 151940fe15e0SLoGin return Ok(()); 152040fe15e0SLoGin } 152140fe15e0SLoGin 152240fe15e0SLoGin /// 获取栈顶地址 152340fe15e0SLoGin /// 152440fe15e0SLoGin /// 请注意,如果用户栈的栈顶地址发生变化,这个值可能不会实时更新! 152540fe15e0SLoGin pub fn sp(&self) -> VirtAddr { 152640fe15e0SLoGin return self.current_sp; 152740fe15e0SLoGin } 152840fe15e0SLoGin 152940fe15e0SLoGin pub unsafe fn set_sp(&mut self, sp: VirtAddr) { 153040fe15e0SLoGin self.current_sp = sp; 153140fe15e0SLoGin } 153240fe15e0SLoGin 153340fe15e0SLoGin /// 仅仅克隆用户栈的信息,不会克隆用户栈的内容/映射 153440fe15e0SLoGin pub unsafe fn clone_info_only(&self) -> Self { 153540fe15e0SLoGin return Self { 153640fe15e0SLoGin stack_bottom: self.stack_bottom, 153740fe15e0SLoGin mapped_size: self.mapped_size, 153840fe15e0SLoGin current_sp: self.current_sp, 153940fe15e0SLoGin }; 154040fe15e0SLoGin } 154140fe15e0SLoGin 154240fe15e0SLoGin /// 获取当前用户栈的大小(不包括保护页) 154340fe15e0SLoGin pub fn stack_size(&self) -> usize { 154440fe15e0SLoGin return self.mapped_size - Self::GUARD_PAGES_NUM * MMArch::PAGE_SIZE; 154540fe15e0SLoGin } 154640fe15e0SLoGin } 1547