|
|
@ -1,7 +1,8 @@ |
|
|
|
use bootloader::bootinfo::{MemoryMap, MemoryRegionType};
|
|
|
|
use x86_64::{
|
|
|
|
structures::paging::{
|
|
|
|
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB, UnusedPhysFrame,
|
|
|
|
FrameAllocator, Mapper, OffsetPageTable, Page, PageTable, PhysFrame, Size4KiB,
|
|
|
|
UnusedPhysFrame,
|
|
|
|
},
|
|
|
|
PhysAddr, VirtAddr,
|
|
|
|
};
|
|
|
@ -35,6 +36,32 @@ unsafe fn active_level_4_table(physical_memory_offset: VirtAddr) -> &'static mut |
|
|
|
&mut *page_table_ptr // unsafe
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates an example mapping for the given page to frame `0xb8000`.
|
|
|
|
pub fn create_example_mapping(
|
|
|
|
page: Page,
|
|
|
|
mapper: &mut OffsetPageTable,
|
|
|
|
frame_allocator: &mut impl FrameAllocator<Size4KiB>,
|
|
|
|
) {
|
|
|
|
use x86_64::structures::paging::PageTableFlags as Flags;
|
|
|
|
|
|
|
|
let frame = PhysFrame::containing_address(PhysAddr::new(0xb8000));
|
|
|
|
// FIXME: ONLY FOR TEMPORARY TESTING
|
|
|
|
let unused_frame = unsafe { UnusedPhysFrame::new(frame) };
|
|
|
|
let flags = Flags::PRESENT | Flags::WRITABLE;
|
|
|
|
|
|
|
|
let map_to_result = mapper.map_to(page, unused_frame, flags, frame_allocator);
|
|
|
|
map_to_result.expect("map_to failed").flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A FrameAllocator that always returns `None`.
|
|
|
|
pub struct EmptyFrameAllocator;
|
|
|
|
|
|
|
|
unsafe impl FrameAllocator<Size4KiB> for EmptyFrameAllocator {
|
|
|
|
fn allocate_frame(&mut self) -> Option<UnusedPhysFrame> {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// A FrameAllocator that returns usable frames from the bootloader's memory map.
|
|
|
|
pub struct BootInfoFrameAllocator {
|
|
|
|
memory_map: &'static MemoryMap,
|
|
|
@ -53,18 +80,14 @@ impl BootInfoFrameAllocator { |
|
|
|
next: 0,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl BootInfoFrameAllocator {
|
|
|
|
/// Returns an iterator over the usable frames specified in the memory map.
|
|
|
|
fn usable_frames(&self) -> impl Iterator<Item = UnusedPhysFrame> {
|
|
|
|
// get usable regions from memory map
|
|
|
|
let regions = self.memory_map.iter();
|
|
|
|
let usable_regions = regions
|
|
|
|
.filter(|r| r.region_type == MemoryRegionType::Usable);
|
|
|
|
let usable_regions = regions.filter(|r| r.region_type == MemoryRegionType::Usable);
|
|
|
|
// map each region to its address range
|
|
|
|
let addr_ranges = usable_regions
|
|
|
|
.map(|r| r.range.start_addr()..r.range.end_addr());
|
|
|
|
let addr_ranges = usable_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.step_by(4096));
|
|
|
|
// create `PhysFrame` types from the start addresses
|
|
|
@ -81,4 +104,3 @@ unsafe impl FrameAllocator<Size4KiB> for BootInfoFrameAllocator { |
|
|
|
frame
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|