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