memory bitmap init
This commit is contained in:
@ -11,7 +11,7 @@ pub const PKG_NAME: &str = env!("CARGO_PKG_NAME");
|
||||
pub const KERNEL_STACK_SIZE: usize = 1024 * 1024;
|
||||
|
||||
pub const PAGE_SIZE: usize = 4096;
|
||||
pub const MAX_MEMORY: usize = 256 * 1024 * 1024;
|
||||
pub const MAX_MEMORY: usize = 16 * 1024 * 1024 * 1024;
|
||||
pub const FRAME_COUNT: usize = MAX_MEMORY / PAGE_SIZE;
|
||||
pub const BITS_PER_ENTRY: usize = 8 * core::mem::size_of::<usize>();
|
||||
pub const ENTRY_COUNT: usize = FRAME_COUNT / BITS_PER_ENTRY;
|
||||
|
@ -13,7 +13,10 @@ use core::ptr::addr_of;
|
||||
|
||||
use x86_64::VirtAddr;
|
||||
|
||||
use crate::constant::{BANNER, KERNEL_STACK_SIZE, PKG_VERSION};
|
||||
use crate::{
|
||||
constant::{BANNER, KERNEL_STACK_SIZE, PKG_VERSION},
|
||||
memory::BitmapMemoryTable,
|
||||
};
|
||||
|
||||
#[repr(C, align(16))]
|
||||
struct AlignedStack {
|
||||
@ -77,5 +80,15 @@ pub extern "sysv64" fn main(usable_memory: &nel_os_common::memory::UsableMemory)
|
||||
}
|
||||
info!("Usable memory: {}MiB", count / 1024 / 1024);
|
||||
|
||||
let bitmap_table = BitmapMemoryTable::init(usable_memory);
|
||||
info!(
|
||||
"Memory bitmap initialized: {} -> {}",
|
||||
bitmap_table.start, bitmap_table.end
|
||||
);
|
||||
info!("Usable memory map:");
|
||||
for i in bitmap_table.start..bitmap_table.end {
|
||||
info!(" {:064b}", bitmap_table.used_map[i]);
|
||||
}
|
||||
|
||||
hlt_loop();
|
||||
}
|
||||
|
@ -1,6 +1,9 @@
|
||||
use nel_os_common::memory::{self, UsableMemory};
|
||||
|
||||
use crate::constant::{BITS_PER_ENTRY, ENTRY_COUNT, PAGE_SIZE};
|
||||
use crate::{
|
||||
constant::{BITS_PER_ENTRY, ENTRY_COUNT, PAGE_SIZE},
|
||||
info,
|
||||
};
|
||||
|
||||
pub struct BitmapMemoryTable {
|
||||
pub used_map: [usize; ENTRY_COUNT],
|
||||
@ -17,19 +20,50 @@ impl BitmapMemoryTable {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn init(usable_memory: UsableMemory) -> Self {
|
||||
let table = Self::new();
|
||||
for range in usable_memory.ranges() {}
|
||||
pub fn init(usable_memory: &UsableMemory) -> Self {
|
||||
let mut table = Self::new();
|
||||
for range in usable_memory.ranges() {
|
||||
table.set_range(range);
|
||||
}
|
||||
|
||||
for i in 0..ENTRY_COUNT {
|
||||
let index = ENTRY_COUNT - i - 1;
|
||||
if table.used_map[index] != 0 {
|
||||
let offset = 63 - table.used_map[index].leading_zeros();
|
||||
table.end = (index + 1) * BITS_PER_ENTRY + offset as usize;
|
||||
}
|
||||
}
|
||||
|
||||
table
|
||||
}
|
||||
|
||||
pub fn set_range(&mut self, range: memory::Range) {
|
||||
let start = range.start;
|
||||
let end = range.end;
|
||||
pub fn set_range(&mut self, range: &memory::Range) {
|
||||
let start = Self::addr_to_pfn(range.start as usize);
|
||||
let size = (range.end - range.start) / PAGE_SIZE as u64;
|
||||
|
||||
for i in 0..size {
|
||||
self.set_frame(start + i as usize, true);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_frame(frame: usize, state: bool) {}
|
||||
pub fn set_frame(&mut self, frame: usize, state: bool) {
|
||||
let index = Self::frame_to_index(frame);
|
||||
let offset = Self::frame_to_offset(frame);
|
||||
|
||||
if state {
|
||||
self.used_map[index] |= 1usize << offset;
|
||||
self.start = self.start.min(frame);
|
||||
} else {
|
||||
self.used_map[index] &= !(1usize << offset);
|
||||
if self.start == frame {
|
||||
self.start += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn addr_to_pfn(addr: usize) -> usize {
|
||||
addr / PAGE_SIZE
|
||||
}
|
||||
|
||||
pub fn frame_to_index(frame: usize) -> usize {
|
||||
frame / BITS_PER_ENTRY
|
||||
|
Reference in New Issue
Block a user