From 7796d4c14adc7037d9076a2f9940ee19ca1300fd Mon Sep 17 00:00:00 2001 From: Philipp Oppermann Date: Fri, 3 May 2019 18:41:01 +0200 Subject: [PATCH] FrameAllocator is an `unsafe trait` now Make `BootInfoFrameAllocator` unsafe because the caller must guarantee that the given memory map is valid. --- src/main.rs | 2 +- src/memory.rs | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index c31a92b5..10b38955 100644 --- a/src/main.rs +++ b/src/main.rs @@ -18,7 +18,7 @@ fn kernel_main(boot_info: &'static BootInfo) -> ! { blog_os::init(); 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 let page = Page::containing_address(VirtAddr::new(0xdeadbeaf000)); diff --git a/src/memory.rs b/src/memory.rs index 2e110fce..626f4fdb 100644 --- a/src/memory.rs +++ b/src/memory.rs @@ -59,7 +59,7 @@ pub fn create_example_mapping( /// A FrameAllocator that always returns `None`. pub struct EmptyFrameAllocator; -impl FrameAllocator for EmptyFrameAllocator { +unsafe impl FrameAllocator for EmptyFrameAllocator { fn allocate_frame(&mut self) -> Option { None } @@ -73,7 +73,11 @@ pub struct BootInfoFrameAllocator { impl BootInfoFrameAllocator { /// 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 { memory_map, next: 0, @@ -94,7 +98,7 @@ impl BootInfoFrameAllocator { } } -impl FrameAllocator for BootInfoFrameAllocator { +unsafe impl FrameAllocator for BootInfoFrameAllocator { fn allocate_frame(&mut self) -> Option { let frame = self.usable_frames().nth(self.next); self.next += 1;