FrameAllocator is an unsafe trait now

Make `BootInfoFrameAllocator` unsafe because the caller must guarantee that the given memory map is valid.
This commit is contained in:
Philipp Oppermann
2019-05-03 18:41:01 +02:00
parent 78e4b22a2f
commit 7796d4c14a
2 changed files with 8 additions and 4 deletions

View File

@@ -18,7 +18,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! {
blog_os::init(); blog_os::init();
let mut mapper = unsafe { memory::init(boot_info.physical_memory_offset) }; let mut mapper = unsafe { memory::init(boot_info.physical_memory_offset) };
let mut frame_allocator = BootInfoFrameAllocator::init(&boot_info.memory_map); let mut frame_allocator = unsafe { BootInfoFrameAllocator::init(&boot_info.memory_map) };
// map a previously unmapped page // map a previously unmapped page
let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000)); let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000));

View File

@@ -59,7 +59,7 @@ pub fn create_example_mapping(
/// A FrameAllocator that always returns `None`. /// A FrameAllocator that always returns `None`.
pub struct EmptyFrameAllocator; pub struct EmptyFrameAllocator;
impl FrameAllocator<Size4KiB> for EmptyFrameAllocator { unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
fn allocate_frame(&mut self) -> Option<PhysFrame> { fn allocate_frame(&mut self) -> Option<PhysFrame> {
None None
} }
@@ -73,7 +73,11 @@ pub struct BootInfoFrameAllocator {
impl BootInfoFrameAllocator { impl BootInfoFrameAllocator {
/// Create a FrameAllocator from the passed memory map. /// Create a FrameAllocator from the passed memory map.
pub fn init(memory_map: &'static MemoryMap) -> Self { ///
/// This function is unsafe because the caller must guarantee that the passed
/// memory map is valid. The main requirement is that all frames that are marked
/// as `USABLE` in it are really unused.
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
BootInfoFrameAllocator { BootInfoFrameAllocator {
memory_map, memory_map,
next: 0, next: 0,
@@ -94,7 +98,7 @@ impl BootInfoFrameAllocator {
} }
} }
impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator { unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
fn allocate_frame(&mut self) -> Option<PhysFrame> { fn allocate_frame(&mut self) -> Option<PhysFrame> {
let frame = self.usable_frames().nth(self.next); let frame = self.usable_frames().nth(self.next);
self.next += 1; self.next += 1;