diff --git a/Cargo.lock b/Cargo.lock index 0be5ad125d..f0063e655a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -470,6 +470,7 @@ checksum = "d140bfddcc22e1f83bbc5d6930ec5713c62489cdd94b86c92852edfc1db96f2b" dependencies = [ "align-address 0.3.0", "smallvec", + "x86_64 0.15.1", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 1ace093de3..c4eac5e24d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -128,6 +128,7 @@ features = [ ] [target.'cfg(target_arch = "x86_64")'.dependencies] +free-list = { version = "0.3", features = ["x86_64"] } multiboot = "0.8" uart_16550 = "0.3" x86 = { version = "0.52", default-features = false } diff --git a/src/arch/x86_64/mm/paging.rs b/src/arch/x86_64/mm/paging.rs index 62020f210a..8e80da762b 100644 --- a/src/arch/x86_64/mm/paging.rs +++ b/src/arch/x86_64/mm/paging.rs @@ -158,6 +158,7 @@ pub fn map( #[cfg(feature = "smp")] let mut ipi_tlb_flush = false; + let mut frame_allocator = physicalmem::PHYSICAL_FREE_LIST.lock(); for (page, frame) in pages.zip(frames) { unsafe { // TODO: Require explicit unmaps @@ -170,11 +171,12 @@ pub fn map( debug!("Had to unmap page {page:?} before mapping."); } recursive_page_table() - .map_to(page, frame, flags, &mut physicalmem::FrameAlloc) + .map_to(page, frame, flags, &mut *frame_allocator) .unwrap() .flush(); } } + drop(frame_allocator); #[cfg(feature = "smp")] if ipi_tlb_flush { @@ -218,12 +220,13 @@ where frame.start_address() ); + let mut frame_allocator = physicalmem::PHYSICAL_FREE_LIST.lock(); unsafe { recursive_page_table() .identity_map( frame, PageTableEntryFlags::PRESENT | PageTableEntryFlags::NO_EXECUTE, - &mut physicalmem::FrameAlloc, + &mut *frame_allocator, ) .unwrap() .flush(); diff --git a/src/arch/x86_64/mm/physicalmem.rs b/src/arch/x86_64/mm/physicalmem.rs index ea7fface8e..b410a5fc7d 100644 --- a/src/arch/x86_64/mm/physicalmem.rs +++ b/src/arch/x86_64/mm/physicalmem.rs @@ -1,6 +1,5 @@ use core::sync::atomic::{AtomicUsize, Ordering}; -use ::x86_64::structures::paging::{FrameAllocator, PhysFrame}; use free_list::{AllocError, FreeList, PageLayout, PageRange}; use hermit_sync::InterruptTicketMutex; use multiboot::information::{MemoryType, Multiboot}; @@ -10,7 +9,7 @@ use crate::arch::x86_64::mm::paging::{BasePageSize, PageSize}; use crate::arch::x86_64::mm::{MultibootMemory, PhysAddr, VirtAddr}; use crate::mm; -static PHYSICAL_FREE_LIST: InterruptTicketMutex> = +pub static PHYSICAL_FREE_LIST: InterruptTicketMutex> = InterruptTicketMutex::new(FreeList::new()); static TOTAL_MEMORY: AtomicUsize = AtomicUsize::new(0); @@ -173,16 +172,6 @@ pub fn allocate(size: usize) -> Result { )) } -pub struct FrameAlloc; - -unsafe impl FrameAllocator for FrameAlloc { - fn allocate_frame(&mut self) -> Option> { - let layout = PageLayout::from_size_align(S::SIZE as usize, S::SIZE as usize).unwrap(); - let addr = PHYSICAL_FREE_LIST.lock().allocate(layout).ok()?.start() as u64; - Some(PhysFrame::from_start_address(x86_64::PhysAddr::new(addr)).unwrap()) - } -} - pub fn allocate_aligned(size: usize, align: usize) -> Result { assert!(size > 0); assert!(align > 0);