Skip to content

Commit 6780b35

Browse files
committed
noted
1 parent bf157c4 commit 6780b35

File tree

6 files changed

+18
-7
lines changed

6 files changed

+18
-7
lines changed

os/src/main.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ fn kernel_log_info() {
9999
pub fn rust_main() -> ! {
100100
clear_bss();
101101
kernel_log_info();
102-
mm::init();
102+
mm::init();//初始化内存,放在较前的位置
103103
println!("[kernel] back to world!");
104104
mm::remap_test();
105105
trap::init();

os/src/mm/address.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,13 +114,14 @@ impl VirtAddr {
114114
self.page_offset() == 0
115115
}
116116
}
117-
/// 虚拟内存和虚拟页号转换
117+
//向下取整
118118
impl From<VirtAddr> for VirtPageNum {
119119
fn from(v: VirtAddr) -> Self {
120120
assert_eq!(v.page_offset(), 0);
121121
v.floor()
122122
}
123123
}
124+
//得到的是页起始地址
124125
impl From<VirtPageNum> for VirtAddr {
125126
fn from(v: VirtPageNum) -> Self {
126127
Self(v.0 << PAGE_SIZE_BITS)

os/src/mm/frame_allocator.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ pub fn init_frame_allocator() {
108108
fn ekernel();//end of kernel, 内核结束地址
109109
}
110110
FRAME_ALLOCATOR.exclusive_access().init(
111-
PhysAddr::from(ekernel as usize).ceil(),
111+
PhysAddr::from(ekernel as usize).ceil(),//向上取整,返回下一页页号
112112
PhysAddr::from(MEMORY_END).floor(),
113113
);//从内核结束开始,内存末尾结束
114114
}
@@ -120,6 +120,7 @@ pub fn frame_alloc() -> Option<FrameTracker> {
120120
.alloc()
121121
.map(FrameTracker::new)
122122
//最后这行等价于.map(|x| FrameTracker::new(x))
123+
//返回一个map里面new的Option<FrameTracker>
123124
}
124125

125126
/// Deallocate a physical page frame with a given ppn

os/src/mm/heap_allocator.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use buddy_system_allocator::LockedHeap;
44

55
#[global_allocator]
66
/// heap allocator instance
7+
/// !关键:提供给rust语言Vec等使用的动态内存分配器,可避免与frame allocator冲突
78
static HEAP_ALLOCATOR: LockedHeap = LockedHeap::empty();
89

910
#[alloc_error_handler]
@@ -12,13 +13,14 @@ pub fn handle_alloc_error(layout: core::alloc::Layout) -> ! {
1213
panic!("Heap allocation error, layout = {:?}", layout);
1314
}
1415
/// heap space ([u8; KERNEL_HEAP_SIZE])
16+
/// 堆空间,内核中的动态内存分配时用到
1517
static mut HEAP_SPACE: [u8; KERNEL_HEAP_SIZE] = [0; KERNEL_HEAP_SIZE];
1618
/// initiate heap allocator
1719
pub fn init_heap() {
1820
unsafe {
1921
HEAP_ALLOCATOR
2022
.lock()
21-
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);
23+
.init(HEAP_SPACE.as_ptr() as usize, KERNEL_HEAP_SIZE);//0-0x2000_0000
2224
}
2325
}
2426

os/src/mm/memory_set.rs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -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

os/src/mm/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ pub use page_table::{PTEFlags, PageTable};
2222

2323
/// initiate heap allocator, frame allocator and kernel space
2424
pub fn init() {
25-
heap_allocator::init_heap();
25+
heap_allocator::init_heap();//先初始化堆分配器,用于进行动态内存分配
2626
frame_allocator::init_frame_allocator();
2727
KERNEL_SPACE.exclusive_access().activate();
2828
}

0 commit comments

Comments
 (0)