Skip to content

Commit 15dd82b

Browse files
authored
Fix problems in lab 7 (#48)
1 parent 5482598 commit 15dd82b

File tree

5 files changed

+19
-13
lines changed

5 files changed

+19
-13
lines changed

docs/labs/0x07/tasks.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
合并后的代码并不能直接运行,你需要基于合并后的代码、按照文档进行修改补充,才能逐步实现本次实验的功能。
2020

21+
需要注意,本次实验对内存分配器的实现从 `pkg/lib/src/allocator.rs` 文件变为 `pkg/lib/src/allocator` 文件夹,其中包含你需要实现的部分。请在合并代码后**手动删除 `pkg/lib/src/allocator.rs` 文件**,以保证编译能够正常进行。
22+
2123
本次实验代码量较小,给出的代码集中于 `pkg/kernel/src/proc/vm` 目录下。
2224

2325
- `heap.rs`:添加了 `Heap` 结构体,用于管理堆内存。

src/0x01/pkg/elf/src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -88,17 +88,15 @@ pub fn load_elf(
8888
page_table: &mut impl Mapper<Size4KiB>,
8989
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
9090
) -> Result<(), MapToError<Size4KiB>> {
91-
let file_buf = elf.input.as_ptr();
92-
93-
info!("Loading ELF file... @ {:#x}", file_buf as u64);
91+
trace!("Loading ELF file...{:?}", elf.input.as_ptr());
9492

9593
for segment in elf.program_iter() {
9694
if segment.get_type().unwrap() != program::Type::Load {
9795
continue;
9896
}
9997

10098
load_segment(
101-
file_buf,
99+
elf,
102100
physical_offset,
103101
&segment,
104102
page_table,
@@ -113,7 +111,7 @@ pub fn load_elf(
113111
///
114112
/// load segment to new frame and set page table
115113
fn load_segment(
116-
file_buf: *const u8,
114+
elf: &ElfFile,
117115
physical_offset: u64,
118116
segment: &program::ProgramHeader,
119117
page_table: &mut impl Mapper<Size4KiB>,
@@ -137,7 +135,7 @@ fn load_segment(
137135
let end_page = Page::containing_address(virt_start_addr + file_size - 1u64);
138136
let pages = Page::range_inclusive(start_page, end_page);
139137

140-
let data = unsafe { file_buf.add(file_offset as usize) };
138+
let data = unsafe { elf.input.as_ptr().add(file_offset as usize) };
141139

142140
for (idx, page) in pages.enumerate() {
143141
let frame = frame_allocator

src/0x07/pkg/kernel/src/proc/vm/heap.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl Heap {
6464

6565
// FIXME: update the end address
6666

67-
Some(new_end)
67+
new_end
6868
}
6969

7070
pub(super) fn clean_up(

src/0x07/pkg/kernel/src/proc/vm/mod.rs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ impl ProcessVm {
7171
// self
7272
// }
7373

74+
pub fn init_kernel_vm(mut self) -> Self {
75+
// TODO: record kernel code usage
76+
self.stack = Stack::kstack();
77+
self
78+
}
79+
7480
pub fn brk(&self, addr: Option<VirtAddr>) -> Option<VirtAddr> {
7581
self.heap.brk(
7682
addr,

src/0x07/pkg/lib/src/macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,12 @@ fn panic(info: &core::panic::PanicInfo) -> ! {
2525
} else {
2626
"Unknown location".to_string()
2727
};
28-
let msg = if let Some(msg) = info.message() {
29-
alloc::format!("{}", msg)
30-
} else {
31-
"No more message...".to_string()
32-
};
33-
errln!("\n\n\rERROR: panicked at {}\n\n\r{}", location, msg);
28+
29+
errln!(
30+
"\n\n\rERROR: panicked at {}\n\n\r{}",
31+
location,
32+
info.message()
33+
);
3434

3535
crate::sys_exit(1);
3636
}

0 commit comments

Comments
 (0)