blog_os
blog_os copied to clipboard
Unmapping memory pages
I've written a routine to unmap memory pages (working on getting ACPI in) that looks like this:
pub fn free_range(start: u64, end: u64) {
let mut mapper = MAPPER.lock();
match mapper.as_mut() {
Some(m) => {
let page_range: PageRangeInclusive<Size4KiB> = {
let start = VirtAddr::new(start as u64);
let end = VirtAddr::new(end);
let start_page = Page::containing_address(start);
let end_page = Page::containing_address(end);
Page::range_inclusive(start_page, end_page)
};
for page in page_range {
match m.unmap(page) {
Ok((_, r)) => r.flush(),
Err(e) => printkln!(
"Kernel: warning: Cannot unmap physical memory address range {:X}h-{:X}h: {:#?}",
start, end, e
),
}
}
}
_ => panic!("Memory allocator or frame allocator are not set"),
}
}
My questions are:
- Is this code right?
- The unmap() routine returns a
Result<(PhysFrame<S>, MapperFlush<S>), UnmapError>
. Should I deallocate the frame? And if so, how do I force its deallocation?
The unmap
method is the counterpart to the map
method. So it depends where the frame that was passed to map
came from. If it was allocated from the frame allocator, it should be safe to deallocate after unmapping. We don't have any code for deallocating frames on the blog though, so you need to create your own data structure for holding freed frames (a simple vec should work, just be careful to not create allocation cycles).