mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
Add a BootInfoFrameAllocator
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
||||||
use x86_64::structures::paging::{
|
use x86_64::structures::paging::{
|
||||||
FrameAllocator, Mapper, Page, PageTable, PhysFrame, RecursivePageTable, Size4KiB,
|
FrameAllocator, Mapper, Page, PageTable, PhysFrame, RecursivePageTable, Size4KiB,
|
||||||
};
|
};
|
||||||
@@ -20,6 +21,24 @@ pub unsafe fn init(level_4_table_addr: usize) -> RecursivePageTable<'static> {
|
|||||||
init_inner(level_4_table_addr)
|
init_inner(level_4_table_addr)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Create a FrameAllocator from the passed memory map
|
||||||
|
pub fn init_frame_allocator(
|
||||||
|
memory_map: &'static MemoryMap,
|
||||||
|
) -> BootInfoFrameAllocator<impl Iterator<Item = PhysFrame>> {
|
||||||
|
// get usable regions from memory map
|
||||||
|
let regions = memory_map
|
||||||
|
.iter()
|
||||||
|
.filter(|r| r.region_type == MemoryRegionType::Usable);
|
||||||
|
// map each region to its address range
|
||||||
|
let addr_ranges = regions.map(|r| r.range.start_addr()..r.range.end_addr());
|
||||||
|
// transform to an iterator of frame start addresses
|
||||||
|
let frame_addresses = addr_ranges.flat_map(|r| r.into_iter().step_by(4096));
|
||||||
|
// create `PhysFrame` types from the start addresses
|
||||||
|
let frames = frame_addresses.map(|addr| PhysFrame::containing_address(PhysAddr::new(addr)));
|
||||||
|
|
||||||
|
BootInfoFrameAllocator { frames }
|
||||||
|
}
|
||||||
|
|
||||||
/// Returns the physical address for the given virtual address, or `None` if
|
/// Returns the physical address for the given virtual address, or `None` if
|
||||||
/// the virtual address is not mapped.
|
/// the virtual address is not mapped.
|
||||||
pub fn translate_addr(addr: u64, recursive_page_table: &RecursivePageTable) -> Option<PhysAddr> {
|
pub fn translate_addr(addr: u64, recursive_page_table: &RecursivePageTable) -> Option<PhysAddr> {
|
||||||
@@ -53,3 +72,19 @@ impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
|||||||
None
|
None
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct BootInfoFrameAllocator<I>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = PhysFrame>,
|
||||||
|
{
|
||||||
|
frames: I,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<I> FrameAllocator<Size4KiB> for BootInfoFrameAllocator<I>
|
||||||
|
where
|
||||||
|
I: Iterator<Item = PhysFrame>,
|
||||||
|
{
|
||||||
|
fn allocate_frame(&mut self) -> Option<PhysFrame> {
|
||||||
|
self.frames.next()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user