From cd658673a35df8b0da3551e819e26d35c18b89f2 Mon Sep 17 00:00:00 2001 From: Tianhao Wang Date: Wed, 29 May 2024 19:53:52 +0200 Subject: mm: add stack based PMA use 8MiB reserved array to manage up to 4GiB of physical memory (4K Pages only) Signed-off-by: Tianhao Wang --- src/mm/mod.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) (limited to 'src/mm/mod.rs') diff --git a/src/mm/mod.rs b/src/mm/mod.rs index b030dfb..27737e8 100644 --- a/src/mm/mod.rs +++ b/src/mm/mod.rs @@ -1,2 +1,51 @@ -pub mod heap_allocator; +use crate::defs::*; +use crate::io::*; +use crate::machine::multiboot; pub mod pma; + +use lazy_static::lazy_static; +use spin::Mutex; +lazy_static! { + pub static ref GLOBAL_PMA: Mutex = + Mutex::new(pma::PageStackAllocator::new()); +} + +pub fn init() { + let mbi = multiboot::get_mb_info().unwrap(); + let mmapinfo = unsafe { mbi.get_mmap() }.unwrap(); + let buf_start = mmapinfo.mmap_addr; + let buf_len = mmapinfo.mmap_length; + let buf_end = buf_start + buf_len; + let mut curr = buf_start as u64; + let mut inserted = 0; + loop { + if curr >= buf_end as u64 { + break; + } + let mblock = unsafe { &*(curr as *const multiboot::MultibootMmap) }; + curr += mblock.size as u64; + curr += 4; + if mblock.mtype != multiboot::MultibootMmap::MTYPE_RAM { + continue; + } + if mblock.get_end() <= pmap_kernel_start() { + continue; + } + // TODO early break if the array is already full + if mblock.get_range().contains(pmap_kernel_end()) { + let r = Range { + addr: pmap_kernel_end(), + len: mblock.get_end() - pmap_kernel_end(), + }; + inserted += GLOBAL_PMA.lock().insert_range(r); + } else { + inserted += GLOBAL_PMA.lock().insert_range(mblock.get_range()); + } + println!( + "pma init: {:#X}KiB free memory, {:#X} pages inserted from block {:#X?}", + inserted * 0x4, + inserted, + mblock, + ); + } +} -- cgit v1.2.3-70-g09d2