@@ -44,7 +44,7 @@ impl MemorySet {
4444 pub fn new_bare ( ) -> Self {
4545 Self {
4646 page_table : PageTable :: new ( ) ,
47- areas : Vec :: new ( ) ,
47+ areas : Vec :: new ( ) , //理论上数据会被放在堆区,.ekernel之后
4848 }
4949 }
5050 /// Get the page table token
@@ -145,6 +145,7 @@ impl MemorySet {
145145 }
146146 /// Include sections in elf and trampoline and TrapContext and user stack,
147147 /// also returns user_sp_base and entry point.
148+ /// 没有引用self,关联函数,创建一个新的MemorySet,起点和终点由程序自己决定
148149 pub fn from_elf ( elf_data : & [ u8 ] ) -> ( Self , usize , usize ) {
149150 let mut memory_set = Self :: new_bare ( ) ;
150151 // map trampoline
@@ -156,6 +157,7 @@ impl MemorySet {
156157 assert_eq ! ( magic, [ 0x7f , 0x45 , 0x4c , 0x46 ] , "invalid elf!" ) ;
157158 let ph_count = elf_header. pt2 . ph_count ( ) ;
158159 let mut max_end_vpn = VirtPageNum ( 0 ) ;
160+ //映射每个段
159161 for i in 0 ..ph_count {
160162 let ph = elf. program_header ( i) . unwrap ( ) ;
161163 if ph. get_type ( ) . unwrap ( ) == xmas_elf:: program:: Type :: Load {
@@ -181,9 +183,11 @@ impl MemorySet {
181183 }
182184 }
183185 // map user stack with U flags
186+ // 取的是页起始地址
184187 let max_end_va: VirtAddr = max_end_vpn. into ( ) ;
188+ // 用户栈底
185189 let mut user_stack_bottom: usize = max_end_va. into ( ) ;
186- // guard page
190+ // guard page 保证按页对齐且不出现交错(不过似乎可能留下最高4095B空隙)
187191 user_stack_bottom += PAGE_SIZE ;
188192 let user_stack_top = user_stack_bottom + USER_STACK_SIZE ;
189193 memory_set. push (
@@ -230,10 +234,12 @@ impl MemorySet {
230234 }
231235 }
232236 /// Translate a virtual page number to a page table entry
237+ /// 虚拟页号到页表项,实际上也就是物理页号+权限信息
233238 pub fn translate ( & self , vpn : VirtPageNum ) -> Option < PageTableEntry > {
234239 self . page_table . translate ( vpn)
235240 }
236241 /// shrink the area to new_end
242+ /// 缩小某个指定起点的映射段,返回是否成功
237243 #[ allow( unused) ]
238244 pub fn shrink_to ( & mut self , start : VirtAddr , new_end : VirtAddr ) -> bool {
239245 if let Some ( area) = self
@@ -249,6 +255,7 @@ impl MemorySet {
249255 }
250256
251257 /// append the area to new_end
258+ /// 扩大段,同上
252259 #[ allow( unused) ]
253260 pub fn append_to ( & mut self , start : VirtAddr , new_end : VirtAddr ) -> bool {
254261 if let Some ( area) = self
0 commit comments