|
|
|
@ -1,3 +1,4 @@
|
|
|
|
|
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
|
|
|
|
use x86_64::{
|
|
|
|
|
structures::paging::{
|
|
|
|
|
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
|
|
|
@ -79,3 +80,33 @@ unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub struct BootInfoFrameAllocator {
|
|
|
|
|
memory_map: &'static MemoryMap,
|
|
|
|
|
next: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl BootInfoFrameAllocator {
|
|
|
|
|
pub unsafe fn init(memory_map: &'static MemoryMap) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
memory_map,
|
|
|
|
|
next: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> {
|
|
|
|
|
let regions = self.memory_map.iter();
|
|
|
|
|
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
|
|
|
|
let addr_range = usable_regions.map(|r| r.range.start_addr()..r.range.end_addr());
|
|
|
|
|
let frame_address = addr_range.flat_map(|r| r.step_by(4096));
|
|
|
|
|
frame_address.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator {
|
|
|
|
|
fn allocate_frame(&mut self) -> Option<PhysFrame<Size4KiB>> {
|
|
|
|
|
let frame = self.usable_frames().nth(self.next);
|
|
|
|
|
self.next += 1;
|
|
|
|
|
frame
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|