aboutsummaryrefslogtreecommitdiff
path: root/src/mm/mod.rs
blob: 27737e854ccfdc7237610a427528b3a1366a9a8f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
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,
		);
	}
}