|
|
|
@@ -58,24 +58,18 @@ pub struct FoundryOSFrameAllocator {
|
|
|
|
|
next: usize,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn init_frame_allocator(memory_map: &'static MemoryMapResponse) {
|
|
|
|
|
unsafe {
|
|
|
|
|
FRAME_ALLOCATOR.call_once(|| Mutex::new(FoundryOSFrameAllocator::init(memory_map)));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl FoundryOSFrameAllocator {
|
|
|
|
|
/// Creates a new `FoundryOSFrameAllocator` from a memory map.
|
|
|
|
|
///
|
|
|
|
|
/// This function takes a reference to a `MemoryMapResponse` and initializes a
|
|
|
|
|
/// `FoundryOSFrameAllocator` with it. The `next` field is set to 0, indicating that
|
|
|
|
|
/// the first frame to be allocated is the first frame in the memory map.
|
|
|
|
|
pub const unsafe fn init(memory_map: &'static MemoryMapResponse) -> Self {
|
|
|
|
|
Self {
|
|
|
|
|
pub unsafe fn init(memory_map: &'static MemoryMapResponse) { unsafe {
|
|
|
|
|
FRAME_ALLOCATOR.call_once(|| Mutex::new(Self {
|
|
|
|
|
memory_map,
|
|
|
|
|
next: 0,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}));
|
|
|
|
|
}}
|
|
|
|
|
|
|
|
|
|
pub fn count_usable_frames(&self) -> u32 {
|
|
|
|
|
self.usable_frames().count() as u32
|
|
|
|
@@ -89,11 +83,8 @@ impl FoundryOSFrameAllocator {
|
|
|
|
|
fn usable_frames(&self) -> impl Iterator<Item = PhysFrame> + use<> {
|
|
|
|
|
let regions = self.memory_map.entries().iter();
|
|
|
|
|
let usable_regions = regions.filter(|region| region.entry_type == EntryType::USABLE);
|
|
|
|
|
|
|
|
|
|
let addr_ranges = usable_regions.map(|region| region.base..region.base + region.length);
|
|
|
|
|
|
|
|
|
|
let frame_addresses = addr_ranges.flat_map(|r| r.step_by(4096));
|
|
|
|
|
|
|
|
|
|
frame_addresses.map(|addr| PhysFrame::from_start_address(PhysAddr::new(addr)).unwrap())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@@ -114,36 +105,4 @@ unsafe impl FrameAllocator<Size4KiB> for FoundryOSFrameAllocator {
|
|
|
|
|
self.next += 1;
|
|
|
|
|
frame
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// pub unsafe fn translate_addr(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
|
|
|
|
|
// translate_addr_inner(addr, physical_memory_offset)
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// fn translate_addr_inner(addr: VirtAddr, physical_memory_offset: VirtAddr) -> Option<PhysAddr> {
|
|
|
|
|
// let (l4_table_frame, _) = Cr3::read();
|
|
|
|
|
|
|
|
|
|
// let table_indexes = [
|
|
|
|
|
// addr.p4_index(),
|
|
|
|
|
// addr.p3_index(),
|
|
|
|
|
// addr.p2_index(),
|
|
|
|
|
// addr.p1_index(),
|
|
|
|
|
// ];
|
|
|
|
|
// let mut frame = l4_table_frame;
|
|
|
|
|
|
|
|
|
|
// for &i in &table_indexes {
|
|
|
|
|
// let virt = physical_memory_offset + frame.start_address().as_u64();
|
|
|
|
|
// let table_ptr: *const PageTable = virt.as_ptr();
|
|
|
|
|
// let table = unsafe { &*table_ptr };
|
|
|
|
|
|
|
|
|
|
// let entry = &table[i];
|
|
|
|
|
|
|
|
|
|
// frame = match entry.frame() {
|
|
|
|
|
// Ok(frame) => frame,
|
|
|
|
|
// Err(FrameError::FrameNotPresent) => return None,
|
|
|
|
|
// Err(FrameError::HugeFrame) => panic!("huge frames are not supported!"),
|
|
|
|
|
// };
|
|
|
|
|
// }
|
|
|
|
|
|
|
|
|
|
// Some(frame.start_address() + u64::from(addr.page_offset()))
|
|
|
|
|
// }
|
|
|
|
|
}
|