feat(paging): add frame allocator and mapping test
Signed-off-by: Julien CLEMENT <julien.clement@epita.fr>
This commit is contained in:
1 parent
62be6d047e
commit
273b004851
10 files changed
+257
-25
No files matched your search
@@ -0,0 +1,94 @@
|
||||
pub use super::PAGE_SIZE;
|
||||
use multiboot2::{MemoryArea, MemoryAreaIter};
|
||||
pub use x86_64::structures::paging::{
|
||||
frame::PhysFrame as Frame, FrameAllocator, FrameDeallocator, Size4KiB,
|
||||
};
|
||||
use x86_64::PhysAddr;
|
||||
|
||||
pub struct AreaFrameAllocator {
|
||||
next_free_frame: Frame,
|
||||
current_area: Option<&'static MemoryArea>,
|
||||
areas: MemoryAreaIter,
|
||||
kernel_start: Frame,
|
||||
kernel_end: Frame,
|
||||
multiboot_start: Frame,
|
||||
multiboot_end: Frame,
|
||||
}
|
||||
|
||||
impl AreaFrameAllocator {
|
||||
pub fn new(kernel_start: u64, kernel_end: u64, multiboot_start: u64, multiboot_end: u64, memory_areas: MemoryAreaIter) -> AreaFrameAllocator {
|
||||
let mut allocator = AreaFrameAllocator {
|
||||
next_free_frame: Frame::containing_address(PhysAddr::new(0)),
|
||||
current_area: None,
|
||||
areas: memory_areas,
|
||||
kernel_start: Frame::containing_address(PhysAddr::new(kernel_start)),
|
||||
kernel_end: Frame::containing_address(PhysAddr::new(kernel_end)),
|
||||
multiboot_start: Frame::containing_address(PhysAddr::new(multiboot_start)),
|
||||
multiboot_end: Frame::containing_address(PhysAddr::new(multiboot_end)),
|
||||
};
|
||||
allocator.choose_next_area();
|
||||
allocator
|
||||
}
|
||||
|
||||
fn choose_next_area(&mut self) {
|
||||
self.current_area = self
|
||||
.areas
|
||||
.clone()
|
||||
.filter(|area| {
|
||||
let address = area.base_addr + area.length - 1;
|
||||
Frame::containing_address(PhysAddr::new(address)) >= self.next_free_frame
|
||||
})
|
||||
.min_by_key(|area| area.base_addr);
|
||||
|
||||
if let Some(area) = self.current_area {
|
||||
let start_frame = Frame::containing_address(PhysAddr::new(area.base_addr));
|
||||
if self.next_free_frame < start_frame {
|
||||
self.next_free_frame = start_frame;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
unsafe impl FrameAllocator<Size4KiB> for AreaFrameAllocator {
|
||||
fn allocate_frame(&mut self) -> Option<Frame> {
|
||||
if let Some(area) = self.current_area {
|
||||
// "Clone" the frame to return it if it's free. Frame doesn't
|
||||
// implement Clone, but we can construct an identical frame.
|
||||
let frame = Frame::containing_address(self.next_free_frame.start_address());
|
||||
|
||||
// the last frame of the current area
|
||||
let current_area_last_frame = {
|
||||
let address = area.base_addr + area.length - 1;
|
||||
Frame::containing_address(PhysAddr::new(address))
|
||||
};
|
||||
|
||||
if frame > current_area_last_frame {
|
||||
// all frames of current area are used, switch to next area
|
||||
self.choose_next_area();
|
||||
} else if frame >= self.kernel_start && frame <= self.kernel_end {
|
||||
// `frame` is used by the kernel
|
||||
self.next_free_frame =
|
||||
Frame::containing_address(self.kernel_end.start_address() + PAGE_SIZE);
|
||||
} else if frame >= self.multiboot_start && frame <= self.multiboot_end {
|
||||
// `frame` is used by the multiboot information structure
|
||||
self.next_free_frame =
|
||||
Frame::containing_address(self.multiboot_end.start_address() + PAGE_SIZE);
|
||||
} else {
|
||||
// frame is unused, increment `next_free_frame` and return it
|
||||
self.next_free_frame =
|
||||
Frame::containing_address(self.next_free_frame.start_address() + PAGE_SIZE);
|
||||
return Some(frame);
|
||||
}
|
||||
// `frame` was not valid, try it again with the updated `next_free_frame`
|
||||
self.allocate_frame()
|
||||
} else {
|
||||
None // no free frames left
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl FrameDeallocator<Size4KiB> for AreaFrameAllocator {
|
||||
unsafe fn deallocate_frame(&mut self, _frame: Frame) {
|
||||
unimplemented!()
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,7 @@
|
||||
pub use self::frame_allocator::AreaFrameAllocator;
|
||||
pub use paging::kernel_remap;
|
||||
|
||||
pub mod frame_allocator;
|
||||
pub mod paging;
|
||||
|
||||
pub const PAGE_SIZE: usize = 4096;
|
||||
@@ -0,0 +1,33 @@
|
||||
use multiboot2::BootInformation;
|
||||
use x86_64::structures::paging::{FrameAllocator, Size4KiB, PageTable, RecursivePageTable, Page, PageTableFlags as Flags, Mapper};
|
||||
use crate::println;
|
||||
use x86_64::VirtAddr;
|
||||
|
||||
pub const P4: *mut PageTable = 0o177777_777_777_777_777_0000 as *mut _;
|
||||
|
||||
pub fn kernel_remap<A>(_allocator: &mut A, _boot_info: &BootInformation)
|
||||
where A: FrameAllocator<Size4KiB>
|
||||
{
|
||||
}
|
||||
|
||||
pub fn test_paging<A>(allocator: &mut A)
|
||||
where A: FrameAllocator<Size4KiB>
|
||||
{
|
||||
let mut page_table = unsafe { RecursivePageTable::new(&mut *P4).expect("Could not create Page Table") };
|
||||
|
||||
let addr = 42 * 512 * 512 * 4096; // 42th P3 entry
|
||||
let page = Page::containing_address(VirtAddr::new(addr));
|
||||
let frame = allocator.allocate_frame().expect("no more frames");
|
||||
println!("None = , map to {:?}", frame);
|
||||
unsafe { page_table.map_to(page, frame, Flags::PRESENT, allocator).expect("Could not map").flush() };
|
||||
println!("next free frame: {:?}", allocator.allocate_frame());
|
||||
|
||||
let page_ptr: *mut u8 = page.start_address().as_mut_ptr();
|
||||
let frame_ptr: *mut u8 = frame.start_address().as_u64() as *mut u8;
|
||||
|
||||
unsafe {
|
||||
println!("Page: {:#?}, Frame: {:#?}", *page_ptr, *frame_ptr);
|
||||
*frame_ptr = 42;
|
||||
println!("Page: {:#?}, Frame: {:#?}", *page_ptr, *frame_ptr);
|
||||
}
|
||||
}
|
||||