aboutsummaryrefslogtreecommitdiff
path: root/src/mm/mod.rs
blob: 3ed6b194080de0d8b52f30af0020abea9a8aeb17 (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
52
53
use crate::defs::*;
use crate::io::*;
use crate::machine::multiboot;
use core::ops::Range;
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
		let mut r = mblock.get_range();
		if mblock.get_range().contains(&pmap_kernel_end()) {
			r.start = pmap_kernel_end();
		}
		inserted += GLOBAL_PMA.lock().insert_range(&r);
	}

	println!(
		"[init] pma: kernel loaded at phy: {:#X} - {:#X}",
		pmap_kernel_start(),
		pmap_kernel_end()
	);
	println!(
		"[init] pma: {:#X} KiB free memory, {:#X} frames inserted",
		inserted * 0x4,
		inserted,
	);
}