|
|
@ -1,5 +1,5 @@
|
|
|
|
pub use self::area_frame_allocator::AreaFrameAllocator;
|
|
|
|
pub use self::area_frame_allocator::AreaFrameAllocator;
|
|
|
|
pub use self::paging::remap_the_kernel;
|
|
|
|
pub use self::paging::*;
|
|
|
|
pub use self::stack_allocator::Stack;
|
|
|
|
pub use self::stack_allocator::Stack;
|
|
|
|
pub use self::address::*;
|
|
|
|
pub use self::address::*;
|
|
|
|
|
|
|
|
|
|
|
@ -46,8 +46,7 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
|
|
|
|
boot_info_start, boot_info_end,
|
|
|
|
boot_info_start, boot_info_end,
|
|
|
|
memory_map_tag.memory_areas());
|
|
|
|
memory_map_tag.memory_areas());
|
|
|
|
|
|
|
|
|
|
|
|
let mut active_table = paging::remap_the_kernel(&mut frame_allocator,
|
|
|
|
let mut active_table = remap_the_kernel(&mut frame_allocator, boot_info);
|
|
|
|
boot_info);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("{:?}", active_table);
|
|
|
|
println!("{:?}", active_table);
|
|
|
|
|
|
|
|
|
|
|
@ -76,6 +75,76 @@ pub fn init(boot_info: &BootInformation) -> MemoryController {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn remap_the_kernel<A>(allocator: &mut A, boot_info: &BootInformation)
|
|
|
|
|
|
|
|
-> ActivePageTable
|
|
|
|
|
|
|
|
where A: FrameAllocator
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let mut temporary_page = TemporaryPage::new(Page::containing_address(0xcafebabe), allocator);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut active_table = unsafe { ActivePageTable::new() };
|
|
|
|
|
|
|
|
let mut new_table = {
|
|
|
|
|
|
|
|
let frame = allocator.allocate_frame().expect("no more frames");
|
|
|
|
|
|
|
|
InactivePageTable::new(frame, &mut active_table, &mut temporary_page)
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
active_table.with(&mut new_table, &mut temporary_page, |mapper| {
|
|
|
|
|
|
|
|
let elf_sections_tag = boot_info.elf_sections_tag()
|
|
|
|
|
|
|
|
.expect("Memory map tag required");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for section in elf_sections_tag.sections() {
|
|
|
|
|
|
|
|
if !section.is_allocated() {
|
|
|
|
|
|
|
|
// section is not loaded to memory
|
|
|
|
|
|
|
|
continue;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
assert!(section.start_address() % PAGE_SIZE == 0,
|
|
|
|
|
|
|
|
"sections need to be page aligned");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
println!("mapping section at addr: {:#x}, size: {:#x}",
|
|
|
|
|
|
|
|
section.addr, section.size);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let flags = EntryFlags::from_elf_section_flags(section);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
fn to_physical_frame(addr: usize) -> Frame {
|
|
|
|
|
|
|
|
Frame::containing_address(
|
|
|
|
|
|
|
|
if addr < KERNEL_OFFSET { addr }
|
|
|
|
|
|
|
|
else { addr - KERNEL_OFFSET })
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let start_frame = to_physical_frame(section.start_address());
|
|
|
|
|
|
|
|
let end_frame = to_physical_frame(section.end_address() - 1);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
for frame in Frame::range_inclusive(start_frame, end_frame) {
|
|
|
|
|
|
|
|
let page = Page::containing_address(frame.start_address().to_kernel_virtual());
|
|
|
|
|
|
|
|
mapper.map_to(page, frame, flags, allocator);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// identity map the VGA text buffer
|
|
|
|
|
|
|
|
let vga_buffer_frame = Frame::containing_address(0xb8000);
|
|
|
|
|
|
|
|
mapper.identity_map(vga_buffer_frame, WRITABLE, allocator);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// identity map the multiboot info structure
|
|
|
|
|
|
|
|
let multiboot_start = Frame::containing_address(boot_info.start_address());
|
|
|
|
|
|
|
|
let multiboot_end = Frame::containing_address(boot_info.end_address() - 1);
|
|
|
|
|
|
|
|
for frame in Frame::range_inclusive(multiboot_start, multiboot_end) {
|
|
|
|
|
|
|
|
mapper.identity_map(frame, PRESENT, allocator);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
debug!("{:?}", mapper);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let old_table = active_table.switch(new_table);
|
|
|
|
|
|
|
|
println!("NEW TABLE!!!");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// turn the old p4 page into a guard page
|
|
|
|
|
|
|
|
let old_p4_page = Page::containing_address(
|
|
|
|
|
|
|
|
old_table.p4_frame.start_address().to_identity_virtual()
|
|
|
|
|
|
|
|
);
|
|
|
|
|
|
|
|
active_table.unmap(old_p4_page, allocator);
|
|
|
|
|
|
|
|
println!("guard page at {:#x}", old_p4_page.start_address());
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
active_table
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
pub struct Frame {
|
|
|
|
pub struct Frame {
|
|
|
|
number: usize,
|
|
|
|
number: usize,
|
|
|
|