aboutsummaryrefslogtreecommitdiff
path: root/src/mm/mod.rs
diff options
context:
space:
mode:
authorTianhao Wang <shrik3@mailbox.org>2024-05-29 19:53:52 +0200
committerTianhao Wang <shrik3@mailbox.org>2024-06-11 15:17:11 +0200
commitcd658673a35df8b0da3551e819e26d35c18b89f2 (patch)
tree74c209b519290eb43545e800b88ddf9dfa796a7a /src/mm/mod.rs
parent9cf85e88211512b0410f9bb9f2f19ea4ce9a8190 (diff)
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 <shrik3@mailbox.org>
Diffstat (limited to 'src/mm/mod.rs')
-rw-r--r--src/mm/mod.rs51
1 files changed, 50 insertions, 1 deletions
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<pma::PageStackAllocator> =
+ 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,
+ );
+ }
+}