xref: /DragonOS/kernel/src/mm/ucontext.rs (revision c709f79fdac296d503ad95ac9ef8d4f28307a487)
1 // 进程的用户空间内存管理
2 
3 use core::{
4     cmp,
5     hash::Hasher,
6     intrinsics::unlikely,
7     ops::Add,
8     sync::atomic::{compiler_fence, Ordering},
9 };
10 
11 use alloc::{
12     collections::BTreeMap,
13     sync::{Arc, Weak},
14     vec::Vec,
15 };
16 use hashbrown::HashSet;
17 use ida::IdAllocator;
18 use system_error::SystemError;
19 
20 use crate::{
21     arch::{mm::PageMapper, CurrentIrqArch, MMArch},
22     exception::InterruptArch,
23     filesystem::vfs::file::File,
24     libs::{
25         align::page_align_up,
26         rwlock::RwLock,
27         spinlock::{SpinLock, SpinLockGuard},
28     },
29     mm::page::page_manager_lock_irqsave,
30     process::ProcessManager,
31     syscall::user_access::{UserBufferReader, UserBufferWriter},
32 };
33 
34 use super::{
35     allocator::page_frame::{
36         deallocate_page_frames, PageFrameCount, PhysPageFrame, VirtPageFrame, VirtPageFrameIter,
37     },
38     page::{EntryFlags, Flusher, InactiveFlusher, Page, PageFlushAll},
39     syscall::{MadvFlags, MapFlags, MremapFlags, ProtFlags},
40     MemoryManagementArch, PageTableKind, VirtAddr, VirtRegion, VmFlags,
41 };
42 
43 /// MMAP_MIN_ADDR的默认值
44 /// 以下内容来自linux-5.19:
45 ///  This is the portion of low virtual memory which should be protected
46 //   from userspace allocation.  Keeping a user from writing to low pages
47 //   can help reduce the impact of kernel NULL pointer bugs.
48 //   For most ia64, ppc64 and x86 users with lots of address space
49 //   a value of 65536 is reasonable and should cause no problems.
50 //   On arm and other archs it should not be higher than 32768.
51 //   Programs which use vm86 functionality or have some need to map
52 //   this low address space will need CAP_SYS_RAWIO or disable this
53 //   protection by setting the value to 0.
54 pub const DEFAULT_MMAP_MIN_ADDR: usize = 65536;
55 
56 /// LockedVMA的id分配器
57 static LOCKEDVMA_ID_ALLOCATOR: SpinLock<IdAllocator> =
58     SpinLock::new(IdAllocator::new(0, usize::MAX).unwrap());
59 
60 #[derive(Debug)]
61 pub struct AddressSpace {
62     inner: RwLock<InnerAddressSpace>,
63 }
64 
65 impl AddressSpace {
66     pub fn new(create_stack: bool) -> Result<Arc<Self>, SystemError> {
67         let inner = InnerAddressSpace::new(create_stack)?;
68         let result = Self {
69             inner: RwLock::new(inner),
70         };
71         return Ok(Arc::new(result));
72     }
73 
74     /// 从pcb中获取当前进程的地址空间结构体的Arc指针
75     pub fn current() -> Result<Arc<AddressSpace>, SystemError> {
76         let vm = ProcessManager::current_pcb()
77             .basic()
78             .user_vm()
79             .expect("Current process has no address space");
80 
81         return Ok(vm);
82     }
83 
84     /// 判断某个地址空间是否为当前进程的地址空间
85     pub fn is_current(self: &Arc<Self>) -> bool {
86         let current = Self::current();
87         if let Ok(current) = current {
88             return Arc::ptr_eq(&current, self);
89         }
90         return false;
91     }
92 }
93 
94 impl core::ops::Deref for AddressSpace {
95     type Target = RwLock<InnerAddressSpace>;
96 
97     fn deref(&self) -> &Self::Target {
98         &self.inner
99     }
100 }
101 
102 impl core::ops::DerefMut for AddressSpace {
103     fn deref_mut(&mut self) -> &mut Self::Target {
104         &mut self.inner
105     }
106 }
107 
108 /// @brief 用户地址空间结构体(每个进程都有一个)
109 #[derive(Debug)]
110 pub struct InnerAddressSpace {
111     pub user_mapper: UserMapper,
112     pub mappings: UserMappings,
113     pub mmap_min: VirtAddr,
114     /// 用户栈信息结构体
115     pub user_stack: Option<UserStack>,
116 
117     pub elf_brk_start: VirtAddr,
118     pub elf_brk: VirtAddr,
119 
120     /// 当前进程的堆空间的起始地址
121     pub brk_start: VirtAddr,
122     /// 当前进程的堆空间的结束地址(不包含)
123     pub brk: VirtAddr,
124 
125     pub start_code: VirtAddr,
126     pub end_code: VirtAddr,
127     pub start_data: VirtAddr,
128     pub end_data: VirtAddr,
129 }
130 
131 impl InnerAddressSpace {
132     pub fn new(create_stack: bool) -> Result<Self, SystemError> {
133         let mut result = Self {
134             user_mapper: MMArch::setup_new_usermapper()?,
135             mappings: UserMappings::new(),
136             mmap_min: VirtAddr(DEFAULT_MMAP_MIN_ADDR),
137             elf_brk_start: VirtAddr::new(0),
138             elf_brk: VirtAddr::new(0),
139             brk_start: MMArch::USER_BRK_START,
140             brk: MMArch::USER_BRK_START,
141             user_stack: None,
142             start_code: VirtAddr(0),
143             end_code: VirtAddr(0),
144             start_data: VirtAddr(0),
145             end_data: VirtAddr(0),
146         };
147         if create_stack {
148             // debug!("to create user stack.");
149             result.new_user_stack(UserStack::DEFAULT_USER_STACK_SIZE)?;
150         }
151 
152         return Ok(result);
153     }
154 
155     /// 尝试克隆当前进程的地址空间,包括这些映射都会被克隆
156     ///
157     /// # Returns
158     ///
159     /// 返回克隆后的,新的地址空间的Arc指针
160     #[inline(never)]
161     pub fn try_clone(&mut self) -> Result<Arc<AddressSpace>, SystemError> {
162         let irq_guard = unsafe { CurrentIrqArch::save_and_disable_irq() };
163         let new_addr_space = AddressSpace::new(false)?;
164         let mut new_guard = new_addr_space.write();
165         unsafe {
166             new_guard
167                 .user_mapper
168                 .clone_from(&mut self.user_mapper, MMArch::PAGE_FAULT_ENABLED)
169         };
170 
171         // 拷贝用户栈的结构体信息,但是不拷贝用户栈的内容(因为后面VMA的拷贝会拷贝用户栈的内容)
172         unsafe {
173             new_guard.user_stack = Some(self.user_stack.as_ref().unwrap().clone_info_only());
174         }
175         let _current_stack_size = self.user_stack.as_ref().unwrap().stack_size();
176 
177         // 拷贝空洞
178         new_guard.mappings.vm_holes = self.mappings.vm_holes.clone();
179 
180         for vma in self.mappings.vmas.iter() {
181             // TODO: 增加对VMA是否为文件映射的判断,如果是的话,就跳过
182 
183             let vma_guard: SpinLockGuard<'_, VMA> = vma.lock_irqsave();
184 
185             // 仅拷贝VMA信息并添加反向映射,因为UserMapper克隆时已经分配了新的物理页
186             let new_vma = LockedVMA::new(vma_guard.clone_info_only());
187             new_guard.mappings.vmas.insert(new_vma.clone());
188             // debug!("new vma: {:x?}", new_vma);
189             let new_vma_guard = new_vma.lock_irqsave();
190             let new_mapper = &new_guard.user_mapper.utable;
191             let mut page_manager_guard = page_manager_lock_irqsave();
192             for page in new_vma_guard.pages().map(|p| p.virt_address()) {
193                 if let Some((paddr, _)) = new_mapper.translate(page) {
194                     let page = page_manager_guard.get_unwrap(&paddr);
195                     page.write_irqsave().insert_vma(new_vma.clone());
196                 }
197             }
198 
199             drop(page_manager_guard);
200             drop(vma_guard);
201             drop(new_vma_guard);
202         }
203         drop(new_guard);
204         drop(irq_guard);
205         return Ok(new_addr_space);
206     }
207 
208     /// 拓展用户栈
209     /// ## 参数
210     ///
211     /// - `bytes`: 拓展大小
212     #[allow(dead_code)]
213     pub fn extend_stack(&mut self, mut bytes: usize) -> Result<(), SystemError> {
214         // debug!("extend user stack");
215         let prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC;
216         let map_flags = MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS | MapFlags::MAP_GROWSDOWN;
217         let stack = self.user_stack.as_mut().unwrap();
218 
219         bytes = page_align_up(bytes);
220         stack.mapped_size += bytes;
221         let len = stack.stack_bottom - stack.mapped_size;
222         self.map_anonymous(len, bytes, prot_flags, map_flags, false, false)?;
223         return Ok(());
224     }
225 
226     /// 判断当前的地址空间是否是当前进程的地址空间
227     #[inline]
228     pub fn is_current(&self) -> bool {
229         return self.user_mapper.utable.is_current();
230     }
231 
232     /// 进行匿名页映射
233     ///
234     /// ## 参数
235     ///
236     /// - `start_vaddr`:映射的起始地址
237     /// - `len`:映射的长度
238     /// - `prot_flags`:保护标志
239     /// - `map_flags`:映射标志
240     /// - `round_to_min`:是否将`start_vaddr`对齐到`mmap_min`,如果为`true`,则当`start_vaddr`不为0时,会对齐到`mmap_min`,否则仅向下对齐到页边界
241     /// - `allocate_at_once`:是否立即分配物理空间
242     ///
243     /// ## 返回
244     ///
245     /// 返回映射的起始虚拟页帧
246     pub fn map_anonymous(
247         &mut self,
248         start_vaddr: VirtAddr,
249         len: usize,
250         prot_flags: ProtFlags,
251         map_flags: MapFlags,
252         round_to_min: bool,
253         allocate_at_once: bool,
254     ) -> Result<VirtPageFrame, SystemError> {
255         let allocate_at_once = if MMArch::PAGE_FAULT_ENABLED {
256             allocate_at_once
257         } else {
258             true
259         };
260         // 用于对齐hint的函数
261         let round_hint_to_min = |hint: VirtAddr| {
262             // 先把hint向下对齐到页边界
263             let addr = hint.data() & (!MMArch::PAGE_OFFSET_MASK);
264             // debug!("map_anonymous: hint = {:?}, addr = {addr:#x}", hint);
265             // 如果hint不是0,且hint小于DEFAULT_MMAP_MIN_ADDR,则对齐到DEFAULT_MMAP_MIN_ADDR
266             if (addr != 0) && round_to_min && (addr < DEFAULT_MMAP_MIN_ADDR) {
267                 Some(VirtAddr::new(page_align_up(DEFAULT_MMAP_MIN_ADDR)))
268             } else if addr == 0 {
269                 None
270             } else {
271                 Some(VirtAddr::new(addr))
272             }
273         };
274         // debug!("map_anonymous: start_vaddr = {:?}", start_vaddr);
275         // debug!("map_anonymous: len(no align) = {}", len);
276 
277         let len = page_align_up(len);
278 
279         // debug!("map_anonymous: len = {}", len);
280 
281         let start_page: VirtPageFrame = self.mmap(
282             round_hint_to_min(start_vaddr),
283             PageFrameCount::from_bytes(len).unwrap(),
284             prot_flags,
285             map_flags,
286             move |page, count, vm_flags, flags, mapper, flusher| {
287                 if allocate_at_once {
288                     VMA::zeroed(page, count, vm_flags, flags, mapper, flusher, None, None)
289                 } else {
290                     Ok(LockedVMA::new(VMA::new(
291                         VirtRegion::new(page.virt_address(), count.data() * MMArch::PAGE_SIZE),
292                         vm_flags,
293                         flags,
294                         None,
295                         None,
296                         false,
297                     )))
298                 }
299             },
300         )?;
301 
302         return Ok(start_page);
303     }
304 
305     /// 进行文件页映射
306     ///
307     /// ## 参数
308     ///
309     /// - `start_vaddr`:映射的起始地址
310     /// - `len`:映射的长度
311     /// - `prot_flags`:保护标志
312     /// - `map_flags`:映射标志
313     /// - `fd`:文件描述符
314     /// - `offset`:映射偏移量
315     /// - `round_to_min`:是否将`start_vaddr`对齐到`mmap_min`,如果为`true`,则当`start_vaddr`不为0时,会对齐到`mmap_min`,否则仅向下对齐到页边界
316     /// - `allocate_at_once`:是否立即分配物理空间
317     ///
318     /// ## 返回
319     ///
320     /// 返回映射的起始虚拟页帧
321     #[allow(clippy::too_many_arguments)]
322     pub fn file_mapping(
323         &mut self,
324         start_vaddr: VirtAddr,
325         len: usize,
326         prot_flags: ProtFlags,
327         map_flags: MapFlags,
328         fd: i32,
329         offset: usize,
330         round_to_min: bool,
331         allocate_at_once: bool,
332     ) -> Result<VirtPageFrame, SystemError> {
333         let allocate_at_once = if MMArch::PAGE_FAULT_ENABLED {
334             allocate_at_once
335         } else {
336             true
337         };
338         // 用于对齐hint的函数
339         let round_hint_to_min = |hint: VirtAddr| {
340             // 先把hint向下对齐到页边界
341             let addr = hint.data() & (!MMArch::PAGE_OFFSET_MASK);
342             // debug!("map_anonymous: hint = {:?}, addr = {addr:#x}", hint);
343             // 如果hint不是0,且hint小于DEFAULT_MMAP_MIN_ADDR,则对齐到DEFAULT_MMAP_MIN_ADDR
344             if (addr != 0) && round_to_min && (addr < DEFAULT_MMAP_MIN_ADDR) {
345                 Some(VirtAddr::new(page_align_up(DEFAULT_MMAP_MIN_ADDR)))
346             } else if addr == 0 {
347                 None
348             } else {
349                 Some(VirtAddr::new(addr))
350             }
351         };
352         // debug!("map_anonymous: start_vaddr = {:?}", start_vaddr);
353         // debug!("map_anonymous: len(no align) = {}", len);
354 
355         let len = page_align_up(len);
356 
357         // debug!("map_anonymous: len = {}", len);
358 
359         let binding = ProcessManager::current_pcb().fd_table();
360         let fd_table_guard = binding.read();
361 
362         let file = fd_table_guard.get_file_by_fd(fd);
363         if file.is_none() {
364             return Err(SystemError::EBADF);
365         }
366         // drop guard 以避免无法调度的问题
367         drop(fd_table_guard);
368 
369         // offset需要4K对齐
370         if !offset & (MMArch::PAGE_SIZE - 1) == 0 {
371             return Err(SystemError::EINVAL);
372         }
373         let pgoff = offset >> MMArch::PAGE_SHIFT;
374 
375         let start_page: VirtPageFrame = self.mmap(
376             round_hint_to_min(start_vaddr),
377             PageFrameCount::from_bytes(len).unwrap(),
378             prot_flags,
379             map_flags,
380             move |page, count, vm_flags, flags, mapper, flusher| {
381                 if allocate_at_once {
382                     VMA::zeroed(
383                         page,
384                         count,
385                         vm_flags,
386                         flags,
387                         mapper,
388                         flusher,
389                         file,
390                         Some(pgoff),
391                     )
392                 } else {
393                     Ok(LockedVMA::new(VMA::new(
394                         VirtRegion::new(page.virt_address(), count.data() * MMArch::PAGE_SIZE),
395                         vm_flags,
396                         flags,
397                         file,
398                         Some(pgoff),
399                         false,
400                     )))
401                 }
402             },
403         )?;
404         return Ok(start_page);
405     }
406 
407     /// 向进程的地址空间映射页面
408     ///
409     /// # 参数
410     ///
411     /// - `addr`:映射的起始地址,如果为`None`,则由内核自动分配
412     /// - `page_count`:映射的页面数量
413     /// - `prot_flags`:保护标志
414     /// - `map_flags`:映射标志
415     /// - `map_func`:映射函数,用于创建VMA
416     ///
417     /// # Returns
418     ///
419     /// 返回映射的起始虚拟页帧
420     ///
421     /// # Errors
422     ///
423     /// - `EINVAL`:参数错误
424     pub fn mmap<
425         F: FnOnce(
426             VirtPageFrame,
427             PageFrameCount,
428             VmFlags,
429             EntryFlags<MMArch>,
430             &mut PageMapper,
431             &mut dyn Flusher<MMArch>,
432         ) -> Result<Arc<LockedVMA>, SystemError>,
433     >(
434         &mut self,
435         addr: Option<VirtAddr>,
436         page_count: PageFrameCount,
437         prot_flags: ProtFlags,
438         map_flags: MapFlags,
439         map_func: F,
440     ) -> Result<VirtPageFrame, SystemError> {
441         if page_count == PageFrameCount::new(0) {
442             return Err(SystemError::EINVAL);
443         }
444         // debug!("mmap: addr: {addr:?}, page_count: {page_count:?}, prot_flags: {prot_flags:?}, map_flags: {map_flags:?}");
445 
446         // 找到未使用的区域
447         let region = match addr {
448             Some(vaddr) => {
449                 self.mappings
450                     .find_free_at(self.mmap_min, vaddr, page_count.bytes(), map_flags)?
451             }
452             None => self
453                 .mappings
454                 .find_free(self.mmap_min, page_count.bytes())
455                 .ok_or(SystemError::ENOMEM)?,
456         };
457 
458         let page = VirtPageFrame::new(region.start());
459 
460         let vm_flags = VmFlags::from(prot_flags)
461             | VmFlags::from(map_flags)
462             | VmFlags::VM_MAYREAD
463             | VmFlags::VM_MAYWRITE
464             | VmFlags::VM_MAYEXEC;
465 
466         // debug!("mmap: page: {:?}, region={region:?}", page.virt_address());
467 
468         compiler_fence(Ordering::SeqCst);
469         let (mut active, mut inactive);
470         let flusher = if self.is_current() {
471             active = PageFlushAll::new();
472             &mut active as &mut dyn Flusher<MMArch>
473         } else {
474             inactive = InactiveFlusher::new();
475             &mut inactive as &mut dyn Flusher<MMArch>
476         };
477         compiler_fence(Ordering::SeqCst);
478         // 映射页面,并将VMA插入到地址空间的VMA列表中
479         self.mappings.insert_vma(map_func(
480             page,
481             page_count,
482             vm_flags,
483             EntryFlags::from_prot_flags(prot_flags, true),
484             &mut self.user_mapper.utable,
485             flusher,
486         )?);
487 
488         return Ok(page);
489     }
490 
491     /// 重映射内存区域
492     ///
493     /// # 参数
494     ///
495     /// - `old_vaddr`:原映射的起始地址
496     /// - `old_len`:原映射的长度
497     /// - `new_len`:重新映射的长度
498     /// - `mremap_flags`:重映射标志
499     /// - `new_vaddr`:重新映射的起始地址
500     /// - `vm_flags`:旧内存区域标志
501     ///
502     /// # Returns
503     ///
504     /// 返回重映射的起始虚拟页帧地址
505     ///
506     /// # Errors
507     ///
508     /// - `EINVAL`:参数错误
509     pub fn mremap(
510         &mut self,
511         old_vaddr: VirtAddr,
512         old_len: usize,
513         new_len: usize,
514         mremap_flags: MremapFlags,
515         new_vaddr: VirtAddr,
516         vm_flags: VmFlags,
517     ) -> Result<VirtAddr, SystemError> {
518         // 检查新内存地址是否对齐
519         if !new_vaddr.check_aligned(MMArch::PAGE_SIZE) {
520             return Err(SystemError::EINVAL);
521         }
522 
523         // 检查新、旧内存区域是否冲突
524         let old_region = VirtRegion::new(old_vaddr, old_len);
525         let new_region = VirtRegion::new(new_vaddr, new_len);
526         if old_region.collide(&new_region) {
527             return Err(SystemError::EINVAL);
528         }
529 
530         // 初始化映射标志
531         let mut map_flags: MapFlags = vm_flags.into();
532         // 初始化内存区域保护标志
533         let prot_flags: ProtFlags = vm_flags.into();
534 
535         // 取消新内存区域的原映射
536         if mremap_flags.contains(MremapFlags::MREMAP_FIXED) {
537             map_flags |= MapFlags::MAP_FIXED;
538             let start_page = VirtPageFrame::new(new_vaddr);
539             let page_count = PageFrameCount::from_bytes(new_len).unwrap();
540             self.munmap(start_page, page_count)?;
541         }
542 
543         // 获取映射后的新内存页面
544         let new_page = self.map_anonymous(new_vaddr, new_len, prot_flags, map_flags, true, true)?;
545         let new_page_vaddr = new_page.virt_address();
546 
547         // 拷贝旧内存区域内容到新内存区域
548         let old_buffer_reader =
549             UserBufferReader::new(old_vaddr.data() as *const u8, old_len, true)?;
550         let old_buf: &[u8] = old_buffer_reader.read_from_user(0)?;
551         let mut new_buffer_writer =
552             UserBufferWriter::new(new_page_vaddr.data() as *mut u8, new_len, true)?;
553         let new_buf: &mut [u8] = new_buffer_writer.buffer(0)?;
554         let len = old_buf.len().min(new_buf.len());
555         new_buf[..len].copy_from_slice(&old_buf[..len]);
556 
557         return Ok(new_page_vaddr);
558     }
559 
560     /// 取消进程的地址空间中的映射
561     ///
562     /// # 参数
563     ///
564     /// - `start_page`:起始页帧
565     /// - `page_count`:取消映射的页帧数量
566     ///
567     /// # Errors
568     ///
569     /// - `EINVAL`:参数错误
570     /// - `ENOMEM`:内存不足
571     pub fn munmap(
572         &mut self,
573         start_page: VirtPageFrame,
574         page_count: PageFrameCount,
575     ) -> Result<(), SystemError> {
576         let to_unmap = VirtRegion::new(start_page.virt_address(), page_count.bytes());
577         let mut flusher: PageFlushAll<MMArch> = PageFlushAll::new();
578 
579         let regions: Vec<Arc<LockedVMA>> = self.mappings.conflicts(to_unmap).collect::<Vec<_>>();
580 
581         for r in regions {
582             let r = r.lock_irqsave().region;
583             let r = self.mappings.remove_vma(&r).unwrap();
584             let intersection = r.lock_irqsave().region().intersect(&to_unmap).unwrap();
585             let split_result = r.extract(intersection, &self.user_mapper.utable).unwrap();
586 
587             // TODO: 当引入后备页映射后,这里需要增加通知文件的逻辑
588 
589             if let Some(before) = split_result.prev {
590                 // 如果前面有VMA,则需要将前面的VMA重新插入到地址空间的VMA列表中
591                 self.mappings.insert_vma(before);
592             }
593 
594             if let Some(after) = split_result.after {
595                 // 如果后面有VMA,则需要将后面的VMA重新插入到地址空间的VMA列表中
596                 self.mappings.insert_vma(after);
597             }
598 
599             r.unmap(&mut self.user_mapper.utable, &mut flusher);
600         }
601 
602         // TODO: 当引入后备页映射后,这里需要增加通知文件的逻辑
603 
604         return Ok(());
605     }
606 
607     pub fn mprotect(
608         &mut self,
609         start_page: VirtPageFrame,
610         page_count: PageFrameCount,
611         prot_flags: ProtFlags,
612     ) -> Result<(), SystemError> {
613         // debug!(
614         //     "mprotect: start_page: {:?}, page_count: {:?}, prot_flags:{prot_flags:?}",
615         //     start_page,
616         //     page_count
617         // );
618         let (mut active, mut inactive);
619         let flusher = if self.is_current() {
620             active = PageFlushAll::new();
621             &mut active as &mut dyn Flusher<MMArch>
622         } else {
623             inactive = InactiveFlusher::new();
624             &mut inactive as &mut dyn Flusher<MMArch>
625         };
626 
627         let mapper = &mut self.user_mapper.utable;
628         let region = VirtRegion::new(start_page.virt_address(), page_count.bytes());
629         // debug!("mprotect: region: {:?}", region);
630 
631         let regions = self.mappings.conflicts(region).collect::<Vec<_>>();
632         // debug!("mprotect: regions: {:?}", regions);
633 
634         for r in regions {
635             // debug!("mprotect: r: {:?}", r);
636             let r = *r.lock_irqsave().region();
637             let r = self.mappings.remove_vma(&r).unwrap();
638 
639             let intersection = r.lock_irqsave().region().intersect(&region).unwrap();
640             let split_result = r
641                 .extract(intersection, mapper)
642                 .expect("Failed to extract VMA");
643 
644             if let Some(before) = split_result.prev {
645                 self.mappings.insert_vma(before);
646             }
647             if let Some(after) = split_result.after {
648                 self.mappings.insert_vma(after);
649             }
650 
651             let mut r_guard = r.lock_irqsave();
652             // 如果VMA的保护标志不允许指定的修改,则返回错误
653             if !r_guard.can_have_flags(prot_flags) {
654                 drop(r_guard);
655                 self.mappings.insert_vma(r.clone());
656                 return Err(SystemError::EACCES);
657             }
658             r_guard.set_vm_flags(VmFlags::from(prot_flags));
659 
660             let new_flags: EntryFlags<MMArch> = r_guard
661                 .flags()
662                 .set_execute(prot_flags.contains(ProtFlags::PROT_EXEC))
663                 .set_write(prot_flags.contains(ProtFlags::PROT_WRITE));
664 
665             r_guard.remap(new_flags, mapper, &mut *flusher)?;
666             drop(r_guard);
667             self.mappings.insert_vma(r);
668         }
669 
670         return Ok(());
671     }
672 
673     pub fn madvise(
674         &mut self,
675         start_page: VirtPageFrame,
676         page_count: PageFrameCount,
677         behavior: MadvFlags,
678     ) -> Result<(), SystemError> {
679         let (mut active, mut inactive);
680         let flusher = if self.is_current() {
681             active = PageFlushAll::new();
682             &mut active as &mut dyn Flusher<MMArch>
683         } else {
684             inactive = InactiveFlusher::new();
685             &mut inactive as &mut dyn Flusher<MMArch>
686         };
687 
688         let mapper = &mut self.user_mapper.utable;
689 
690         let region = VirtRegion::new(start_page.virt_address(), page_count.bytes());
691         let regions = self.mappings.conflicts(region).collect::<Vec<_>>();
692 
693         for r in regions {
694             let r = *r.lock_irqsave().region();
695             let r = self.mappings.remove_vma(&r).unwrap();
696 
697             let intersection = r.lock_irqsave().region().intersect(&region).unwrap();
698             let split_result = r
699                 .extract(intersection, mapper)
700                 .expect("Failed to extract VMA");
701 
702             if let Some(before) = split_result.prev {
703                 self.mappings.insert_vma(before);
704             }
705             if let Some(after) = split_result.after {
706                 self.mappings.insert_vma(after);
707             }
708             r.do_madvise(behavior, mapper, &mut *flusher)?;
709             self.mappings.insert_vma(r);
710         }
711         Ok(())
712     }
713 
714     /// 创建新的用户栈
715     ///
716     /// ## 参数
717     ///
718     /// - `size`:栈的大小
719     pub fn new_user_stack(&mut self, size: usize) -> Result<(), SystemError> {
720         assert!(self.user_stack.is_none(), "User stack already exists");
721         let stack = UserStack::new(self, None, size)?;
722         self.user_stack = Some(stack);
723         return Ok(());
724     }
725 
726     #[inline(always)]
727     pub fn user_stack_mut(&mut self) -> Option<&mut UserStack> {
728         return self.user_stack.as_mut();
729     }
730 
731     /// 取消用户空间内的所有映射
732     pub unsafe fn unmap_all(&mut self) {
733         let mut flusher: PageFlushAll<MMArch> = PageFlushAll::new();
734         for vma in self.mappings.iter_vmas() {
735             if vma.mapped() {
736                 vma.unmap(&mut self.user_mapper.utable, &mut flusher);
737             }
738         }
739     }
740 
741     /// 设置进程的堆的内存空间
742     ///
743     /// ## 参数
744     ///
745     /// - `new_brk`:新的堆的结束地址。需要满足页对齐要求,并且是用户空间地址,且大于等于当前的堆的起始地址
746     ///
747     /// ## 返回值
748     ///
749     /// 返回旧的堆的结束地址
750     pub unsafe fn set_brk(&mut self, new_brk: VirtAddr) -> Result<VirtAddr, SystemError> {
751         assert!(new_brk.check_aligned(MMArch::PAGE_SIZE));
752 
753         if !new_brk.check_user() || new_brk < self.brk_start {
754             return Err(SystemError::EFAULT);
755         }
756 
757         let old_brk = self.brk;
758 
759         if new_brk > self.brk {
760             let len = new_brk - self.brk;
761             let prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC;
762             let map_flags = MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS | MapFlags::MAP_FIXED;
763             self.map_anonymous(old_brk, len, prot_flags, map_flags, true, false)?;
764 
765             self.brk = new_brk;
766             return Ok(old_brk);
767         } else {
768             let unmap_len = self.brk - new_brk;
769             let unmap_start = new_brk;
770             if unmap_len == 0 {
771                 return Ok(old_brk);
772             }
773             self.munmap(
774                 VirtPageFrame::new(unmap_start),
775                 PageFrameCount::from_bytes(unmap_len).unwrap(),
776             )?;
777             self.brk = new_brk;
778             return Ok(old_brk);
779         }
780     }
781 
782     pub unsafe fn sbrk(&mut self, incr: isize) -> Result<VirtAddr, SystemError> {
783         if incr == 0 {
784             return Ok(self.brk);
785         }
786 
787         let new_brk = if incr > 0 {
788             self.brk + incr as usize
789         } else {
790             self.brk - incr.unsigned_abs()
791         };
792 
793         let new_brk = VirtAddr::new(page_align_up(new_brk.data()));
794 
795         return self.set_brk(new_brk);
796     }
797 }
798 
799 impl Drop for InnerAddressSpace {
800     fn drop(&mut self) {
801         unsafe {
802             self.unmap_all();
803         }
804     }
805 }
806 
807 #[derive(Debug, Hash)]
808 pub struct UserMapper {
809     pub utable: PageMapper,
810 }
811 
812 impl UserMapper {
813     pub fn new(utable: PageMapper) -> Self {
814         return Self { utable };
815     }
816 
817     /// 拷贝用户空间映射
818     /// ## 参数
819     ///
820     /// - `umapper`: 要拷贝的用户空间
821     /// - `copy_on_write`: 是否写时复制
822     pub unsafe fn clone_from(&mut self, umapper: &mut Self, copy_on_write: bool) {
823         self.utable
824             .clone_user_mapping(&mut umapper.utable, copy_on_write);
825     }
826 }
827 
828 impl Drop for UserMapper {
829     fn drop(&mut self) {
830         if self.utable.is_current() {
831             // 如果当前要被销毁的用户空间的页表是当前进程的页表,那么就切换回初始内核页表
832             unsafe { MMArch::set_table(PageTableKind::User, MMArch::initial_page_table()) }
833         }
834         // 释放用户空间顶层页表占用的页帧
835         // 请注意,在释放这个页帧之前,用户页表应该已经被完全释放,否则会产生内存泄露
836         unsafe {
837             deallocate_page_frames(
838                 PhysPageFrame::new(self.utable.table().phys()),
839                 PageFrameCount::new(1),
840                 &mut page_manager_lock_irqsave(),
841             )
842         };
843     }
844 }
845 
846 /// 用户空间映射信息
847 #[derive(Debug)]
848 pub struct UserMappings {
849     /// 当前用户空间的虚拟内存区域
850     vmas: HashSet<Arc<LockedVMA>>,
851     /// 当前用户空间的VMA空洞
852     vm_holes: BTreeMap<VirtAddr, usize>,
853 }
854 
855 impl UserMappings {
856     pub fn new() -> Self {
857         return Self {
858             vmas: HashSet::new(),
859             vm_holes: core::iter::once((VirtAddr::new(0), MMArch::USER_END_VADDR.data()))
860                 .collect::<BTreeMap<_, _>>(),
861         };
862     }
863 
864     /// 判断当前进程的VMA内,是否有包含指定的虚拟地址的VMA。
865     ///
866     /// 如果有,返回包含指定虚拟地址的VMA的Arc指针,否则返回None。
867     #[allow(dead_code)]
868     pub fn contains(&self, vaddr: VirtAddr) -> Option<Arc<LockedVMA>> {
869         for v in self.vmas.iter() {
870             let guard = v.lock_irqsave();
871             if guard.region.contains(vaddr) {
872                 return Some(v.clone());
873             }
874         }
875         return None;
876     }
877 
878     /// 向下寻找距离虚拟地址最近的VMA
879     /// ## 参数
880     ///
881     /// - `vaddr`: 虚拟地址
882     ///
883     /// ## 返回值
884     /// - Some(Arc<LockedVMA>): 虚拟地址所在的或最近的下一个VMA
885     /// - None: 未找到VMA
886     #[allow(dead_code)]
887     pub fn find_nearest(&self, vaddr: VirtAddr) -> Option<Arc<LockedVMA>> {
888         let mut nearest: Option<Arc<LockedVMA>> = None;
889         for v in self.vmas.iter() {
890             let guard = v.lock_irqsave();
891             if guard.region.contains(vaddr) {
892                 return Some(v.clone());
893             }
894             if guard.region.start >= vaddr
895                 && if let Some(ref nearest) = nearest {
896                     guard.region.start < nearest.lock_irqsave().region.start
897                 } else {
898                     true
899                 }
900             {
901                 nearest = Some(v.clone());
902             }
903         }
904         return nearest;
905     }
906 
907     /// 获取当前进程的地址空间中,与给定虚拟地址范围有重叠的VMA的迭代器。
908     pub fn conflicts(&self, request: VirtRegion) -> impl Iterator<Item = Arc<LockedVMA>> + '_ {
909         let r = self
910             .vmas
911             .iter()
912             .filter(move |v| v.lock_irqsave().region.intersect(&request).is_some())
913             .cloned();
914         return r;
915     }
916 
917     /// 在当前进程的地址空间中,寻找第一个符合条件的空闲的虚拟内存范围。
918     ///
919     /// @param min_vaddr 最小的起始地址
920     /// @param size 请求的大小
921     ///
922     /// @return 如果找到了,返回虚拟内存范围,否则返回None
923     pub fn find_free(&self, min_vaddr: VirtAddr, size: usize) -> Option<VirtRegion> {
924         let _vaddr = min_vaddr;
925         let mut iter = self
926             .vm_holes
927             .iter()
928             .skip_while(|(hole_vaddr, hole_size)| hole_vaddr.add(**hole_size) <= min_vaddr);
929 
930         let (hole_vaddr, size) = iter.find(|(hole_vaddr, hole_size)| {
931             // 计算当前空洞的可用大小
932             let available_size: usize =
933                 if hole_vaddr <= &&min_vaddr && min_vaddr <= hole_vaddr.add(**hole_size) {
934                     **hole_size - (min_vaddr - **hole_vaddr)
935                 } else {
936                     **hole_size
937                 };
938 
939             size <= available_size
940         })?;
941 
942         // 创建一个新的虚拟内存范围。
943         let region = VirtRegion::new(cmp::max(*hole_vaddr, min_vaddr), *size);
944 
945         return Some(region);
946     }
947 
948     pub fn find_free_at(
949         &self,
950         min_vaddr: VirtAddr,
951         vaddr: VirtAddr,
952         size: usize,
953         flags: MapFlags,
954     ) -> Result<VirtRegion, SystemError> {
955         // 如果没有指定地址,那么就在当前进程的地址空间中寻找一个空闲的虚拟内存范围。
956         if vaddr == VirtAddr::new(0) {
957             return self.find_free(min_vaddr, size).ok_or(SystemError::ENOMEM);
958         }
959 
960         // 如果指定了地址,那么就检查指定的地址是否可用。
961 
962         let requested = VirtRegion::new(vaddr, size);
963 
964         if requested.end() >= MMArch::USER_END_VADDR || !vaddr.check_aligned(MMArch::PAGE_SIZE) {
965             return Err(SystemError::EINVAL);
966         }
967 
968         if let Some(_x) = self.conflicts(requested).next() {
969             if flags.contains(MapFlags::MAP_FIXED_NOREPLACE) {
970                 // 如果指定了 MAP_FIXED_NOREPLACE 标志,由于所指定的地址无法成功建立映射,则放弃映射,不对地址做修正
971                 return Err(SystemError::EEXIST);
972             }
973 
974             if flags.contains(MapFlags::MAP_FIXED) {
975                 // todo: 支持MAP_FIXED标志对已有的VMA进行覆盖
976                 return Err(SystemError::ENOSYS);
977             }
978 
979             // 如果没有指定MAP_FIXED标志,那么就对地址做修正
980             let requested = self.find_free(min_vaddr, size).ok_or(SystemError::ENOMEM)?;
981             return Ok(requested);
982         }
983 
984         return Ok(requested);
985     }
986 
987     /// 在当前进程的地址空间中,保留一个指定大小的区域,使得该区域不在空洞中。
988     /// 该函数会修改vm_holes中的空洞信息。
989     ///
990     /// @param region 要保留的区域
991     ///
992     /// 请注意,在调用本函数之前,必须先确定region所在范围内没有VMA。
993     fn reserve_hole(&mut self, region: &VirtRegion) {
994         let prev_hole: Option<(&VirtAddr, &mut usize)> =
995             self.vm_holes.range_mut(..=region.start()).next_back();
996 
997         if let Some((prev_hole_vaddr, prev_hole_size)) = prev_hole {
998             let prev_hole_end = prev_hole_vaddr.add(*prev_hole_size);
999 
1000             if prev_hole_end > region.start() {
1001                 // 如果前一个空洞的结束地址大于当前空洞的起始地址,那么就需要调整前一个空洞的大小。
1002                 *prev_hole_size = region.start().data() - prev_hole_vaddr.data();
1003             }
1004 
1005             if prev_hole_end > region.end() {
1006                 // 如果前一个空洞的结束地址大于当前空洞的结束地址,那么就需要增加一个新的空洞。
1007                 self.vm_holes
1008                     .insert(region.end(), prev_hole_end - region.end());
1009             }
1010         }
1011     }
1012 
1013     /// 在当前进程的地址空间中,释放一个指定大小的区域,使得该区域成为一个空洞。
1014     /// 该函数会修改vm_holes中的空洞信息。
1015     fn unreserve_hole(&mut self, region: &VirtRegion) {
1016         // 如果将要插入的空洞与后一个空洞相邻,那么就需要合并。
1017         let next_hole_size: Option<usize> = self.vm_holes.remove(&region.end());
1018 
1019         if let Some((_prev_hole_vaddr, prev_hole_size)) = self
1020             .vm_holes
1021             .range_mut(..region.start())
1022             .next_back()
1023             .filter(|(offset, size)| offset.data() + **size == region.start().data())
1024         {
1025             *prev_hole_size += region.size() + next_hole_size.unwrap_or(0);
1026         } else {
1027             self.vm_holes
1028                 .insert(region.start(), region.size() + next_hole_size.unwrap_or(0));
1029         }
1030     }
1031 
1032     /// 在当前进程的映射关系中,插入一个新的VMA。
1033     pub fn insert_vma(&mut self, vma: Arc<LockedVMA>) {
1034         let region = vma.lock_irqsave().region;
1035         // 要求插入的地址范围必须是空闲的,也就是说,当前进程的地址空间中,不能有任何与之重叠的VMA。
1036         assert!(self.conflicts(region).next().is_none());
1037         self.reserve_hole(&region);
1038 
1039         self.vmas.insert(vma);
1040     }
1041 
1042     /// @brief 删除一个VMA,并把对应的地址空间加入空洞中。
1043     ///
1044     /// 这里不会取消VMA对应的地址的映射
1045     ///
1046     /// @param region 要删除的VMA所在的地址范围
1047     ///
1048     /// @return 如果成功删除了VMA,则返回被删除的VMA,否则返回None
1049     /// 如果没有可以删除的VMA,则不会执行删除操作,并报告失败。
1050     pub fn remove_vma(&mut self, region: &VirtRegion) -> Option<Arc<LockedVMA>> {
1051         // 请注意,由于这里会对每个VMA加锁,因此性能很低
1052         let vma: Arc<LockedVMA> = self
1053             .vmas
1054             .drain_filter(|vma| vma.lock_irqsave().region == *region)
1055             .next()?;
1056         self.unreserve_hole(region);
1057 
1058         return Some(vma);
1059     }
1060 
1061     /// @brief Get the iterator of all VMAs in this process.
1062     pub fn iter_vmas(&self) -> hashbrown::hash_set::Iter<Arc<LockedVMA>> {
1063         return self.vmas.iter();
1064     }
1065 }
1066 
1067 impl Default for UserMappings {
1068     fn default() -> Self {
1069         return Self::new();
1070     }
1071 }
1072 
1073 /// 加了锁的VMA
1074 ///
1075 /// 备注:进行性能测试,看看SpinLock和RwLock哪个更快。
1076 #[derive(Debug)]
1077 pub struct LockedVMA {
1078     /// 用于计算哈希值,避免总是获取vma锁来计算哈希值
1079     id: usize,
1080     vma: SpinLock<VMA>,
1081 }
1082 
1083 impl core::hash::Hash for LockedVMA {
1084     fn hash<H: Hasher>(&self, state: &mut H) {
1085         self.id.hash(state);
1086     }
1087 }
1088 
1089 impl PartialEq for LockedVMA {
1090     fn eq(&self, other: &Self) -> bool {
1091         self.id.eq(&other.id)
1092     }
1093 }
1094 
1095 impl Eq for LockedVMA {}
1096 
1097 #[allow(dead_code)]
1098 impl LockedVMA {
1099     pub fn new(vma: VMA) -> Arc<Self> {
1100         let r = Arc::new(Self {
1101             id: LOCKEDVMA_ID_ALLOCATOR.lock().alloc().unwrap(),
1102             vma: SpinLock::new(vma),
1103         });
1104         r.vma.lock_irqsave().self_ref = Arc::downgrade(&r);
1105         return r;
1106     }
1107 
1108     pub fn id(&self) -> usize {
1109         self.id
1110     }
1111 
1112     pub fn lock(&self) -> SpinLockGuard<VMA> {
1113         return self.vma.lock();
1114     }
1115 
1116     pub fn lock_irqsave(&self) -> SpinLockGuard<VMA> {
1117         return self.vma.lock_irqsave();
1118     }
1119 
1120     /// 调整当前VMA的页面的标志位
1121     ///
1122     /// TODO:增加调整虚拟页映射的物理地址的功能
1123     ///
1124     /// @param flags 新的标志位
1125     /// @param mapper 页表映射器
1126     /// @param flusher 页表项刷新器
1127     ///
1128     pub fn remap(
1129         &self,
1130         flags: EntryFlags<MMArch>,
1131         mapper: &mut PageMapper,
1132         mut flusher: impl Flusher<MMArch>,
1133     ) -> Result<(), SystemError> {
1134         let mut guard = self.lock_irqsave();
1135         for page in guard.region.pages() {
1136             // 暂时要求所有的页帧都已经映射到页表
1137             // TODO: 引入Lazy Mapping, 通过缺页中断来映射页帧,这里就不必要求所有的页帧都已经映射到页表了
1138             let r = unsafe {
1139                 mapper
1140                     .remap(page.virt_address(), flags)
1141                     .expect("Failed to remap, beacuse of some page is not mapped")
1142             };
1143             flusher.consume(r);
1144         }
1145         guard.flags = flags;
1146         return Ok(());
1147     }
1148 
1149     pub fn unmap(&self, mapper: &mut PageMapper, mut flusher: impl Flusher<MMArch>) {
1150         // todo: 如果当前vma与文件相关,完善文件相关的逻辑
1151 
1152         let mut guard = self.lock_irqsave();
1153 
1154         // 获取物理页的anon_vma的守卫
1155         let mut page_manager_guard: SpinLockGuard<'_, crate::mm::page::PageManager> =
1156             page_manager_lock_irqsave();
1157         for page in guard.region.pages() {
1158             if mapper.translate(page.virt_address()).is_none() {
1159                 continue;
1160             }
1161             let (paddr, _, flush) = unsafe { mapper.unmap_phys(page.virt_address(), true) }
1162                 .expect("Failed to unmap, beacuse of some page is not mapped");
1163 
1164             // 从anon_vma中删除当前VMA
1165             let page = page_manager_guard.get_unwrap(&paddr);
1166             page.write_irqsave().remove_vma(self);
1167 
1168             // 如果物理页的anon_vma链表长度为0并且不是共享页,则释放物理页.
1169             if page.read_irqsave().can_deallocate() {
1170                 unsafe {
1171                     drop(page);
1172                     deallocate_page_frames(
1173                         PhysPageFrame::new(paddr),
1174                         PageFrameCount::new(1),
1175                         &mut page_manager_guard,
1176                     )
1177                 };
1178             }
1179 
1180             flusher.consume(flush);
1181         }
1182         guard.mapped = false;
1183 
1184         // 当vma对应共享文件的写映射时,唤醒脏页回写线程
1185         if guard.vm_file().is_some()
1186             && guard
1187                 .vm_flags()
1188                 .contains(VmFlags::VM_SHARED | VmFlags::VM_WRITE)
1189         {
1190             crate::mm::page::PageReclaimer::wakeup_claim_thread();
1191         }
1192     }
1193 
1194     pub fn mapped(&self) -> bool {
1195         return self.vma.lock_irqsave().mapped;
1196     }
1197 
1198     /// 将当前VMA进行切分,切分成3个VMA,分别是:
1199     ///
1200     /// 1. 前面的VMA,如果没有则为None
1201     /// 2. 中间的VMA,也就是传入的Region
1202     /// 3. 后面的VMA,如果没有则为None
1203     pub fn extract(&self, region: VirtRegion, utable: &PageMapper) -> Option<VMASplitResult> {
1204         assert!(region.start().check_aligned(MMArch::PAGE_SIZE));
1205         assert!(region.end().check_aligned(MMArch::PAGE_SIZE));
1206 
1207         let mut guard = self.lock_irqsave();
1208         {
1209             // 如果传入的region不在当前VMA的范围内,则直接返回None
1210             if unlikely(region.start() < guard.region.start() || region.end() > guard.region.end())
1211             {
1212                 return None;
1213             }
1214 
1215             let intersect: Option<VirtRegion> = guard.region.intersect(&region);
1216             // 如果当前VMA不包含region,则直接返回None
1217             if unlikely(intersect.is_none()) {
1218                 return None;
1219             }
1220             let intersect: VirtRegion = intersect.unwrap();
1221             if unlikely(intersect == guard.region) {
1222                 // 如果当前VMA完全包含region,则直接返回当前VMA
1223                 return Some(VMASplitResult::new(
1224                     None,
1225                     guard.self_ref.upgrade().unwrap(),
1226                     None,
1227                 ));
1228             }
1229         }
1230 
1231         let before: Option<Arc<LockedVMA>> = guard.region.before(&region).map(|virt_region| {
1232             let mut vma: VMA = unsafe { guard.clone() };
1233             vma.region = virt_region;
1234             vma.mapped = false;
1235             let vma: Arc<LockedVMA> = LockedVMA::new(vma);
1236             vma
1237         });
1238 
1239         let after: Option<Arc<LockedVMA>> = guard.region.after(&region).map(|virt_region| {
1240             let mut vma: VMA = unsafe { guard.clone() };
1241             vma.region = virt_region;
1242             vma.mapped = false;
1243             let vma: Arc<LockedVMA> = LockedVMA::new(vma);
1244             vma
1245         });
1246 
1247         // 重新设置before、after这两个VMA里面的物理页的anon_vma
1248         let mut page_manager_guard = page_manager_lock_irqsave();
1249         if let Some(before) = before.clone() {
1250             let virt_iter = before.lock_irqsave().region.iter_pages();
1251             for frame in virt_iter {
1252                 if let Some((paddr, _)) = utable.translate(frame.virt_address()) {
1253                     let page = page_manager_guard.get_unwrap(&paddr);
1254                     let mut page_guard = page.write_irqsave();
1255                     page_guard.insert_vma(before.clone());
1256                     page_guard.remove_vma(self);
1257                     before.lock_irqsave().mapped = true;
1258                 }
1259             }
1260         }
1261 
1262         if let Some(after) = after.clone() {
1263             let virt_iter = after.lock_irqsave().region.iter_pages();
1264             for frame in virt_iter {
1265                 if let Some((paddr, _)) = utable.translate(frame.virt_address()) {
1266                     let page = page_manager_guard.get_unwrap(&paddr);
1267                     let mut page_guard = page.write_irqsave();
1268                     page_guard.insert_vma(after.clone());
1269                     page_guard.remove_vma(self);
1270                     after.lock_irqsave().mapped = true;
1271                 }
1272             }
1273         }
1274 
1275         guard.region = region;
1276 
1277         return Some(VMASplitResult::new(
1278             before,
1279             guard.self_ref.upgrade().unwrap(),
1280             after,
1281         ));
1282     }
1283 
1284     /// 判断VMA是否为外部(非当前进程空间)的VMA
1285     pub fn is_foreign(&self) -> bool {
1286         let guard = self.lock_irqsave();
1287         if let Some(space) = guard.user_address_space.clone() {
1288             if let Some(space) = space.upgrade() {
1289                 return AddressSpace::is_current(&space);
1290             } else {
1291                 return true;
1292             }
1293         } else {
1294             return true;
1295         }
1296     }
1297 
1298     /// 判断VMA是否可访问
1299     pub fn is_accessible(&self) -> bool {
1300         let guard = self.lock_irqsave();
1301         let vm_access_flags: VmFlags = VmFlags::VM_READ | VmFlags::VM_WRITE | VmFlags::VM_EXEC;
1302         guard.vm_flags().intersects(vm_access_flags)
1303     }
1304 
1305     /// 判断VMA是否为匿名映射
1306     pub fn is_anonymous(&self) -> bool {
1307         let guard = self.lock_irqsave();
1308         guard.vm_file.is_none()
1309     }
1310 
1311     /// 判断VMA是否为大页映射
1312     pub fn is_hugepage(&self) -> bool {
1313         //TODO: 实现巨页映射判断逻辑,目前不支持巨页映射
1314         false
1315     }
1316 }
1317 
1318 impl Drop for LockedVMA {
1319     fn drop(&mut self) {
1320         LOCKEDVMA_ID_ALLOCATOR.lock().free(self.id);
1321     }
1322 }
1323 
1324 /// VMA切分结果
1325 #[allow(dead_code)]
1326 pub struct VMASplitResult {
1327     pub prev: Option<Arc<LockedVMA>>,
1328     pub middle: Arc<LockedVMA>,
1329     pub after: Option<Arc<LockedVMA>>,
1330 }
1331 
1332 impl VMASplitResult {
1333     pub fn new(
1334         prev: Option<Arc<LockedVMA>>,
1335         middle: Arc<LockedVMA>,
1336         post: Option<Arc<LockedVMA>>,
1337     ) -> Self {
1338         Self {
1339             prev,
1340             middle,
1341             after: post,
1342         }
1343     }
1344 }
1345 
1346 /// @brief 虚拟内存区域
1347 #[derive(Debug)]
1348 pub struct VMA {
1349     /// 虚拟内存区域对应的虚拟地址范围
1350     region: VirtRegion,
1351     /// 虚拟内存区域标志
1352     vm_flags: VmFlags,
1353     /// VMA内的页帧的标志
1354     flags: EntryFlags<MMArch>,
1355     /// VMA内的页帧是否已经映射到页表
1356     mapped: bool,
1357     /// VMA所属的用户地址空间
1358     user_address_space: Option<Weak<AddressSpace>>,
1359     self_ref: Weak<LockedVMA>,
1360 
1361     vm_file: Option<Arc<File>>,
1362     /// VMA映射的文件部分相对于整个文件的偏移页数
1363     file_pgoff: Option<usize>,
1364 
1365     provider: Provider,
1366 }
1367 
1368 impl core::hash::Hash for VMA {
1369     fn hash<H: Hasher>(&self, state: &mut H) {
1370         self.region.hash(state);
1371         self.flags.hash(state);
1372         self.mapped.hash(state);
1373     }
1374 }
1375 
1376 /// 描述不同类型的内存提供者或资源
1377 #[derive(Debug)]
1378 pub enum Provider {
1379     Allocated, // TODO:其他
1380 }
1381 
1382 #[allow(dead_code)]
1383 impl VMA {
1384     pub fn new(
1385         region: VirtRegion,
1386         vm_flags: VmFlags,
1387         flags: EntryFlags<MMArch>,
1388         file: Option<Arc<File>>,
1389         pgoff: Option<usize>,
1390         mapped: bool,
1391     ) -> Self {
1392         VMA {
1393             region,
1394             vm_flags,
1395             flags,
1396             mapped,
1397             user_address_space: None,
1398             self_ref: Weak::default(),
1399             provider: Provider::Allocated,
1400             vm_file: file,
1401             file_pgoff: pgoff,
1402         }
1403     }
1404 
1405     pub fn region(&self) -> &VirtRegion {
1406         return &self.region;
1407     }
1408 
1409     pub fn vm_flags(&self) -> &VmFlags {
1410         return &self.vm_flags;
1411     }
1412 
1413     pub fn vm_file(&self) -> Option<Arc<File>> {
1414         return self.vm_file.clone();
1415     }
1416 
1417     pub fn address_space(&self) -> Option<Weak<AddressSpace>> {
1418         return self.user_address_space.clone();
1419     }
1420 
1421     pub fn set_vm_flags(&mut self, vm_flags: VmFlags) {
1422         self.vm_flags = vm_flags;
1423     }
1424 
1425     pub fn set_region_size(&mut self, new_region_size: usize) {
1426         self.region.set_size(new_region_size);
1427     }
1428 
1429     pub fn set_mapped(&mut self, mapped: bool) {
1430         self.mapped = mapped;
1431     }
1432 
1433     pub fn set_flags(&mut self) {
1434         self.flags = MMArch::vm_get_page_prot(self.vm_flags);
1435     }
1436 
1437     /// # 拷贝当前VMA的内容
1438     ///
1439     /// ### 安全性
1440     ///
1441     /// 由于这样操作可能由于错误的拷贝,导致内存泄露、内存重复释放等问题,所以需要小心使用。
1442     pub unsafe fn clone(&self) -> Self {
1443         return Self {
1444             region: self.region,
1445             vm_flags: self.vm_flags,
1446             flags: self.flags,
1447             mapped: self.mapped,
1448             user_address_space: self.user_address_space.clone(),
1449             self_ref: self.self_ref.clone(),
1450             provider: Provider::Allocated,
1451             file_pgoff: self.file_pgoff,
1452             vm_file: self.vm_file.clone(),
1453         };
1454     }
1455 
1456     pub fn clone_info_only(&self) -> Self {
1457         return Self {
1458             region: self.region,
1459             vm_flags: self.vm_flags,
1460             flags: self.flags,
1461             mapped: self.mapped,
1462             user_address_space: None,
1463             self_ref: Weak::default(),
1464             provider: Provider::Allocated,
1465             file_pgoff: self.file_pgoff,
1466             vm_file: self.vm_file.clone(),
1467         };
1468     }
1469 
1470     #[inline(always)]
1471     pub fn flags(&self) -> EntryFlags<MMArch> {
1472         return self.flags;
1473     }
1474 
1475     #[inline(always)]
1476     pub fn file_page_offset(&self) -> Option<usize> {
1477         return self.file_pgoff;
1478     }
1479 
1480     pub fn pages(&self) -> VirtPageFrameIter {
1481         return VirtPageFrameIter::new(
1482             VirtPageFrame::new(self.region.start()),
1483             VirtPageFrame::new(self.region.end()),
1484         );
1485     }
1486 
1487     pub fn remap(
1488         &mut self,
1489         flags: EntryFlags<MMArch>,
1490         mapper: &mut PageMapper,
1491         mut flusher: impl Flusher<MMArch>,
1492     ) -> Result<(), SystemError> {
1493         for page in self.region.pages() {
1494             // debug!("remap page {:?}", page.virt_address());
1495             if mapper.translate(page.virt_address()).is_some() {
1496                 let r = unsafe {
1497                     mapper
1498                         .remap(page.virt_address(), flags)
1499                         .expect("Failed to remap")
1500                 };
1501                 flusher.consume(r);
1502             }
1503             // debug!("consume page {:?}", page.virt_address());
1504             // debug!("remap page {:?} done", page.virt_address());
1505         }
1506         self.flags = flags;
1507         return Ok(());
1508     }
1509 
1510     /// 检查当前VMA是否可以拥有指定的标志位
1511     ///
1512     /// ## 参数
1513     ///
1514     /// - `prot_flags` 要检查的标志位
1515     pub fn can_have_flags(&self, prot_flags: ProtFlags) -> bool {
1516         let is_downgrade = (self.flags.has_write() || !prot_flags.contains(ProtFlags::PROT_WRITE))
1517             && (self.flags.has_execute() || !prot_flags.contains(ProtFlags::PROT_EXEC));
1518 
1519         match self.provider {
1520             Provider::Allocated { .. } => true,
1521 
1522             #[allow(unreachable_patterns)]
1523             _ => is_downgrade,
1524         }
1525     }
1526 
1527     /// 把物理地址映射到虚拟地址
1528     ///
1529     /// @param phys 要映射的物理地址
1530     /// @param destination 要映射到的虚拟地址
1531     /// @param count 要映射的页帧数量
1532     /// @param flags 页面标志位
1533     /// @param mapper 页表映射器
1534     /// @param flusher 页表项刷新器
1535     ///
1536     /// @return 返回映射后的虚拟内存区域
1537     pub fn physmap(
1538         phys: PhysPageFrame,
1539         destination: VirtPageFrame,
1540         count: PageFrameCount,
1541         vm_flags: VmFlags,
1542         flags: EntryFlags<MMArch>,
1543         mapper: &mut PageMapper,
1544         mut flusher: impl Flusher<MMArch>,
1545     ) -> Result<Arc<LockedVMA>, SystemError> {
1546         let mut cur_phy = phys;
1547         let mut cur_dest = destination;
1548 
1549         for _ in 0..count.data() {
1550             // 将物理页帧映射到虚拟页帧
1551             let r =
1552                 unsafe { mapper.map_phys(cur_dest.virt_address(), cur_phy.phys_address(), flags) }
1553                     .expect("Failed to map phys, may be OOM error");
1554 
1555             // todo: 增加OOM处理
1556 
1557             // 刷新TLB
1558             flusher.consume(r);
1559 
1560             cur_phy = cur_phy.next();
1561             cur_dest = cur_dest.next();
1562         }
1563 
1564         let r: Arc<LockedVMA> = LockedVMA::new(VMA::new(
1565             VirtRegion::new(destination.virt_address(), count.data() * MMArch::PAGE_SIZE),
1566             vm_flags,
1567             flags,
1568             None,
1569             None,
1570             true,
1571         ));
1572 
1573         // 将VMA加入到anon_vma中
1574         let mut page_manager_guard = page_manager_lock_irqsave();
1575         cur_phy = phys;
1576         for _ in 0..count.data() {
1577             let paddr = cur_phy.phys_address();
1578             let page = page_manager_guard.get_unwrap(&paddr);
1579             page.write_irqsave().insert_vma(r.clone());
1580             cur_phy = cur_phy.next();
1581         }
1582 
1583         return Ok(r);
1584     }
1585 
1586     /// 从页分配器中分配一些物理页,并把它们映射到指定的虚拟地址,然后创建VMA
1587     /// ## 参数
1588     ///
1589     /// - `destination`: 要映射到的虚拟地址
1590     /// - `page_count`: 要映射的页帧数量
1591     /// - `vm_flags`: VMA标志位
1592     /// - `flags`: 页面标志位
1593     /// - `mapper`: 页表映射器
1594     /// - `flusher`: 页表项刷新器
1595     /// - `file`: 映射文件
1596     /// - `pgoff`: 返回映射后的虚拟内存区域
1597     ///
1598     /// ## 返回值
1599     /// - 页面错误处理信息标志
1600     #[allow(clippy::too_many_arguments)]
1601     pub fn zeroed(
1602         destination: VirtPageFrame,
1603         page_count: PageFrameCount,
1604         vm_flags: VmFlags,
1605         flags: EntryFlags<MMArch>,
1606         mapper: &mut PageMapper,
1607         mut flusher: impl Flusher<MMArch>,
1608         file: Option<Arc<File>>,
1609         pgoff: Option<usize>,
1610     ) -> Result<Arc<LockedVMA>, SystemError> {
1611         let mut cur_dest: VirtPageFrame = destination;
1612         // debug!(
1613         //     "VMA::zeroed: page_count = {:?}, destination={destination:?}",
1614         //     page_count
1615         // );
1616         for _ in 0..page_count.data() {
1617             // debug!(
1618             //     "VMA::zeroed: cur_dest={cur_dest:?}, vaddr = {:?}",
1619             //     cur_dest.virt_address()
1620             // );
1621             let r = unsafe { mapper.map(cur_dest.virt_address(), flags) }
1622                 .expect("Failed to map zero, may be OOM error");
1623             // todo: 增加OOM处理
1624 
1625             // 稍后再刷新TLB,这里取消刷新
1626             flusher.consume(r);
1627             cur_dest = cur_dest.next();
1628         }
1629         let r = LockedVMA::new(VMA::new(
1630             VirtRegion::new(
1631                 destination.virt_address(),
1632                 page_count.data() * MMArch::PAGE_SIZE,
1633             ),
1634             vm_flags,
1635             flags,
1636             file,
1637             pgoff,
1638             true,
1639         ));
1640         drop(flusher);
1641         // debug!("VMA::zeroed: flusher dropped");
1642 
1643         // 清空这些内存并将VMA加入到anon_vma中
1644         let mut page_manager_guard = page_manager_lock_irqsave();
1645         let virt_iter: VirtPageFrameIter =
1646             VirtPageFrameIter::new(destination, destination.add(page_count));
1647         for frame in virt_iter {
1648             let paddr = mapper.translate(frame.virt_address()).unwrap().0;
1649 
1650             // 将VMA加入到anon_vma
1651             let page = page_manager_guard.get_unwrap(&paddr);
1652             page.write_irqsave().insert_vma(r.clone());
1653         }
1654         // debug!("VMA::zeroed: done");
1655         return Ok(r);
1656     }
1657 
1658     pub fn page_address(&self, page: &Arc<Page>) -> Result<VirtAddr, SystemError> {
1659         let page_guard = page.read_irqsave();
1660         let index = page_guard.index().unwrap();
1661         if index >= self.file_pgoff.unwrap() {
1662             let address =
1663                 self.region.start + ((index - self.file_pgoff.unwrap()) << MMArch::PAGE_SHIFT);
1664             if address <= self.region.end() {
1665                 return Ok(address);
1666             }
1667         }
1668         return Err(SystemError::EFAULT);
1669     }
1670 }
1671 
1672 impl Drop for VMA {
1673     fn drop(&mut self) {
1674         // 当VMA被释放时,需要确保它已经被从页表中解除映射
1675         assert!(!self.mapped, "VMA is still mapped");
1676     }
1677 }
1678 
1679 impl PartialEq for VMA {
1680     fn eq(&self, other: &Self) -> bool {
1681         return self.region == other.region;
1682     }
1683 }
1684 
1685 impl Eq for VMA {}
1686 
1687 impl PartialOrd for VMA {
1688     fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
1689         Some(self.cmp(other))
1690     }
1691 }
1692 
1693 impl Ord for VMA {
1694     fn cmp(&self, other: &Self) -> cmp::Ordering {
1695         return self.region.cmp(&other.region);
1696     }
1697 }
1698 
1699 #[derive(Debug)]
1700 pub struct UserStack {
1701     // 栈底地址
1702     stack_bottom: VirtAddr,
1703     // 当前已映射的大小
1704     mapped_size: usize,
1705     /// 栈顶地址(这个值需要仔细确定!因为它可能不会实时与用户栈的真实栈顶保持一致!要小心!)
1706     current_sp: VirtAddr,
1707 }
1708 
1709 impl UserStack {
1710     /// 默认的用户栈底地址
1711     pub const DEFAULT_USER_STACK_BOTTOM: VirtAddr = MMArch::USER_STACK_START;
1712     /// 默认的用户栈大小为8MB
1713     pub const DEFAULT_USER_STACK_SIZE: usize = 8 * 1024 * 1024;
1714     /// 用户栈的保护页数量
1715     pub const GUARD_PAGES_NUM: usize = 4;
1716 
1717     /// 创建一个用户栈
1718     pub fn new(
1719         vm: &mut InnerAddressSpace,
1720         stack_bottom: Option<VirtAddr>,
1721         stack_size: usize,
1722     ) -> Result<Self, SystemError> {
1723         let stack_bottom = stack_bottom.unwrap_or(Self::DEFAULT_USER_STACK_BOTTOM);
1724         assert!(stack_bottom.check_aligned(MMArch::PAGE_SIZE));
1725 
1726         // 分配用户栈的保护页
1727         let guard_size = Self::GUARD_PAGES_NUM * MMArch::PAGE_SIZE;
1728         let actual_stack_bottom = stack_bottom - guard_size;
1729 
1730         let mut prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE;
1731         let map_flags = MapFlags::MAP_PRIVATE
1732             | MapFlags::MAP_ANONYMOUS
1733             | MapFlags::MAP_FIXED_NOREPLACE
1734             | MapFlags::MAP_GROWSDOWN;
1735         // debug!(
1736         //     "map anonymous stack: {:?} {}",
1737         //     actual_stack_bottom,
1738         //     guard_size
1739         // );
1740         vm.map_anonymous(
1741             actual_stack_bottom,
1742             guard_size,
1743             prot_flags,
1744             map_flags,
1745             false,
1746             false,
1747         )?;
1748         // test_buddy();
1749         // 设置保护页只读
1750         prot_flags.remove(ProtFlags::PROT_WRITE);
1751         // debug!(
1752         //     "to mprotect stack guard pages: {:?} {}",
1753         //     actual_stack_bottom,
1754         //     guard_size
1755         // );
1756         vm.mprotect(
1757             VirtPageFrame::new(actual_stack_bottom),
1758             PageFrameCount::new(Self::GUARD_PAGES_NUM),
1759             prot_flags,
1760         )?;
1761 
1762         // debug!(
1763         //     "mprotect stack guard pages done: {:?} {}",
1764         //     actual_stack_bottom,
1765         //     guard_size
1766         // );
1767 
1768         let mut user_stack = UserStack {
1769             stack_bottom: actual_stack_bottom,
1770             mapped_size: guard_size,
1771             current_sp: actual_stack_bottom - guard_size,
1772         };
1773 
1774         // debug!("extend user stack: {:?} {}", stack_bottom, stack_size);
1775         // 分配用户栈
1776         user_stack.initial_extend(vm, stack_size)?;
1777         // debug!("user stack created: {:?} {}", stack_bottom, stack_size);
1778         return Ok(user_stack);
1779     }
1780 
1781     fn initial_extend(
1782         &mut self,
1783         vm: &mut InnerAddressSpace,
1784         mut bytes: usize,
1785     ) -> Result<(), SystemError> {
1786         let prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC;
1787         let map_flags = MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS | MapFlags::MAP_GROWSDOWN;
1788 
1789         bytes = page_align_up(bytes);
1790         self.mapped_size += bytes;
1791 
1792         vm.map_anonymous(
1793             self.stack_bottom - self.mapped_size,
1794             bytes,
1795             prot_flags,
1796             map_flags,
1797             false,
1798             false,
1799         )?;
1800 
1801         return Ok(());
1802     }
1803 
1804     /// 扩展用户栈
1805     ///
1806     /// ## 参数
1807     ///
1808     /// - `vm` 用户地址空间结构体
1809     /// - `bytes` 要扩展的字节数
1810     ///
1811     /// ## 返回值
1812     ///
1813     /// - **Ok(())** 扩展成功
1814     /// - **Err(SystemError)** 扩展失败
1815     #[allow(dead_code)]
1816     pub fn extend(
1817         &mut self,
1818         vm: &mut InnerAddressSpace,
1819         mut bytes: usize,
1820     ) -> Result<(), SystemError> {
1821         let prot_flags = ProtFlags::PROT_READ | ProtFlags::PROT_WRITE | ProtFlags::PROT_EXEC;
1822         let map_flags = MapFlags::MAP_PRIVATE | MapFlags::MAP_ANONYMOUS;
1823 
1824         bytes = page_align_up(bytes);
1825         self.mapped_size += bytes;
1826 
1827         vm.map_anonymous(
1828             self.stack_bottom - self.mapped_size,
1829             bytes,
1830             prot_flags,
1831             map_flags,
1832             false,
1833             false,
1834         )?;
1835 
1836         return Ok(());
1837     }
1838 
1839     /// 获取栈顶地址
1840     ///
1841     /// 请注意,如果用户栈的栈顶地址发生变化,这个值可能不会实时更新!
1842     pub fn sp(&self) -> VirtAddr {
1843         return self.current_sp;
1844     }
1845 
1846     pub unsafe fn set_sp(&mut self, sp: VirtAddr) {
1847         self.current_sp = sp;
1848     }
1849 
1850     /// 仅仅克隆用户栈的信息,不会克隆用户栈的内容/映射
1851     pub unsafe fn clone_info_only(&self) -> Self {
1852         return Self {
1853             stack_bottom: self.stack_bottom,
1854             mapped_size: self.mapped_size,
1855             current_sp: self.current_sp,
1856         };
1857     }
1858 
1859     /// 获取当前用户栈的大小(不包括保护页)
1860     pub fn stack_size(&self) -> usize {
1861         return self.mapped_size - Self::GUARD_PAGES_NUM * MMArch::PAGE_SIZE;
1862     }
1863 }
1864