From 3b1dfc70430b249a864875d2b1aa9b6e10a56c46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Kr=C3=B6ning?= Date: Wed, 8 May 2024 16:52:24 +0200 Subject: [PATCH] refactor(riscv64): migrate to free-list crate MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Martin Kröning --- src/arch/riscv64/mm/physicalmem.rs | 38 +++++++++++++++++---------- src/arch/riscv64/mm/virtualmem.rs | 42 ++++++++++++++++++------------ src/logging.rs | 2 ++ src/mm/mod.rs | 6 ++++- 4 files changed, 57 insertions(+), 31 deletions(-) diff --git a/src/arch/riscv64/mm/physicalmem.rs b/src/arch/riscv64/mm/physicalmem.rs index 67e7b61cbe..89be2606cb 100644 --- a/src/arch/riscv64/mm/physicalmem.rs +++ b/src/arch/riscv64/mm/physicalmem.rs @@ -1,16 +1,16 @@ -use core::alloc::AllocError; use core::convert::TryInto; use core::sync::atomic::{AtomicUsize, Ordering}; +use free_list::{AllocError, FreeList, PageLayout, PageRange}; use hermit_sync::InterruptSpinMutex; use crate::arch::riscv64::kernel::{get_limit, get_ram_address}; use crate::arch::riscv64::mm::paging::{BasePageSize, PageSize}; use crate::arch::riscv64::mm::PhysAddr; use crate::mm; -use crate::mm::freelist::{FreeList, FreeListEntry}; -static PHYSICAL_FREE_LIST: InterruptSpinMutex = InterruptSpinMutex::new(FreeList::new()); +static PHYSICAL_FREE_LIST: InterruptSpinMutex> = + InterruptSpinMutex::new(FreeList::new()); static TOTAL_MEMORY: AtomicUsize = AtomicUsize::new(0); fn detect_from_limits() -> Result<(), ()> { @@ -19,11 +19,14 @@ fn detect_from_limits() -> Result<(), ()> { return Err(()); } - let entry = FreeListEntry::new( + let range = PageRange::new( mm::kernel_end_address().as_usize(), get_ram_address().as_usize() + limit, - ); - PHYSICAL_FREE_LIST.lock().push(entry); + ) + .unwrap(); + unsafe { + PHYSICAL_FREE_LIST.lock().deallocate(range).unwrap(); + } TOTAL_MEMORY.store(limit, Ordering::SeqCst); Ok(()) @@ -47,10 +50,13 @@ pub fn allocate(size: usize) -> Result { BasePageSize::SIZE as usize ); + let layout = PageLayout::from_size(size).unwrap(); + Ok(PhysAddr( PHYSICAL_FREE_LIST .lock() - .allocate(size, None)? + .allocate(layout)? + .start() .try_into() .unwrap(), )) @@ -72,10 +78,13 @@ pub fn allocate_aligned(size: usize, align: usize) -> Result = InterruptSpinMutex::new(FreeList::new()); +static KERNEL_FREE_LIST: InterruptSpinMutex> = + InterruptSpinMutex::new(FreeList::new()); /// End of the virtual memory address space reserved for kernel memory (256 GiB). /// This also marks the start of the virtual memory address space reserved for the task heap. const KERNEL_VIRTUAL_MEMORY_END: VirtAddr = VirtAddr(0x4000000000); pub fn init() { - let entry = FreeListEntry { - start: (get_ram_address() + PhysAddr(physicalmem::total_memory_size() as u64)) + let range = PageRange::new( + (get_ram_address() + PhysAddr(physicalmem::total_memory_size() as u64)) .as_usize() .align_up(HugePageSize::SIZE as usize), - end: KERNEL_VIRTUAL_MEMORY_END.as_usize(), - }; - KERNEL_FREE_LIST.lock().push(entry); + KERNEL_VIRTUAL_MEMORY_END.as_usize(), + ) + .unwrap(); + unsafe { + KERNEL_FREE_LIST.lock().deallocate(range).unwrap(); + } } pub fn allocate(size: usize) -> Result { @@ -36,10 +39,13 @@ pub fn allocate(size: usize) -> Result { BasePageSize::SIZE as usize ); + let layout = PageLayout::from_size(size).unwrap(); + Ok(VirtAddr( KERNEL_FREE_LIST .lock() - .allocate(size, None)? + .allocate(layout)? + .start() .try_into() .unwrap(), )) @@ -61,10 +67,13 @@ pub fn allocate_aligned(size: usize, align: usize) -> Result (::log::info!("{:25}{}", concat!($str, ":"), format_args!($($arg)+))); } +#[cfg(any(not(target_arch = "riscv64"), feature = "pci", feature = "tcp"))] macro_rules! infofooter { () => {{ ::log::info!("{:=^70}", '='); diff --git a/src/mm/mod.rs b/src/mm/mod.rs index 574c6f0f4c..34a5f0c33d 100644 --- a/src/mm/mod.rs +++ b/src/mm/mod.rs @@ -1,6 +1,10 @@ pub mod allocator; pub mod device_alloc; -#[cfg(not(any(target_arch = "x86_64", target_arch = "aarch64")))] +#[cfg(not(any( + target_arch = "x86_64", + target_arch = "aarch64", + target_arch = "riscv64" +)))] pub mod freelist; use core::mem;