diff options
| author | Tianhao Wang <wth@riseup.net> | 2024-02-25 19:16:35 +0100 |
|---|---|---|
| committer | Tianhao Wang <shrik3@mailbox.org> | 2024-06-11 15:13:39 +0200 |
| commit | d0b37d9f6e423576d84367090af41fcf03630121 (patch) | |
| tree | 2fa5e790ce8cfed24c19898ac0f8120e91d89135 | |
| parent | b31c08a010d5113edb549dc6dda2f726f282277e (diff) | |
paging: add basic bitmap frame allocator
| -rw-r--r-- | Cargo.toml | 4 | ||||
| -rw-r--r-- | src/ds/bitmap.rs | 0 | ||||
| -rw-r--r-- | src/ds/mod.rs | 1 | ||||
| -rw-r--r-- | src/lib.rs | 6 | ||||
| -rw-r--r-- | src/mm/pma.rs | 70 |
5 files changed, 59 insertions, 22 deletions
@@ -9,10 +9,6 @@ edition = "2021" spin = "0.9.8" bitflags = "2.4.2" -[dependencies.bit-vec] -version = "0.6.3" -default-features = false - [dependencies.num_enum] version = "0.7.2" default-features = false diff --git a/src/ds/bitmap.rs b/src/ds/bitmap.rs deleted file mode 100644 index e69de29..0000000 --- a/src/ds/bitmap.rs +++ /dev/null diff --git a/src/ds/mod.rs b/src/ds/mod.rs index 7cfb300..e8ae652 100644 --- a/src/ds/mod.rs +++ b/src/ds/mod.rs @@ -1,2 +1 @@ -pub mod bitmap; pub mod queue; @@ -35,6 +35,12 @@ pub extern "C" fn _entry() -> ! { // interrupt::interrupt_enable(); // // busy loop query keyboard + let mut framemap = mm::pma::FMap::new(); + framemap.init(); + println!("Bitmap starting from : {:p}", framemap.bm.as_ptr()); + println!("Skip first {} bytes", framemap.skip_byte); + + loop { // let code = io::KBCTL_GLOBAL.lock().simple_read(); io::KBCTL_GLOBAL.lock().fetch_key(); diff --git a/src/mm/pma.rs b/src/mm/pma.rs index aa5e193..af789e8 100644 --- a/src/mm/pma.rs +++ b/src/mm/pma.rs @@ -1,4 +1,4 @@ -use crate::defs::Mem; +use crate::defs::{self, Mem}; use core::ffi::c_void; use core::{ptr, slice}; // this is POC code, it will be ugly @@ -6,26 +6,62 @@ use core::{ptr, slice}; extern "C" { pub fn ___KERNEL_END__(); } -/// Bitmap for physical frames. to get around the chicken-egg problem, we provide a provisional -/// bitmap of fixed length in the startup code. -pub struct PFMap { - bm: &'static mut [u8], - skip: usize, // pfn to skip (because they are already used by the initial kernel image) - end: usize, // pfn limit + +type BitU8 = u8; +/// Bitmap for physical frames +pub struct FMap { + pub bm: &'static mut [BitU8], + // skip over the kernel image and the bitmap itself. + pub skip_byte: usize, } -// TODO PMA initialization : needs to singleton +pub enum PMAError{ + DoubleFree +} -impl PFMap {} +impl FMap { + pub fn new() -> Self { + let map_start = ___KERNEL_END__ as usize; + let fmap = Self { + bm: unsafe { slice::from_raw_parts_mut(map_start as *mut u8, Mem::PHY_BM_SIZE) }, + // looks ugly, perhaps FIXME + // We'll waste several frames for the sake of easy alignment + skip_byte: 1 + ((map_start >> Mem::PAGE_SHIFT) / 8), + }; + fmap + } -pub struct Frame { - pfn: usize, -} + /// return : index to the bitmap u8 , bit mask to retrive the bit. + fn locate_bit(addr: usize) -> Option<(usize, u8)> { + if addr >= Mem::PHY_TOP { + return None + } + let pn = addr >> Mem::PAGE_SHIFT; + let idx = pn / 8; + let mask:u8 = 1 << (pn % 8); + Some((idx, mask)) + } -impl Frame { - pub fn addr(&self) -> usize { - self.pfn << Mem::PAGE_SHIFT + pub fn alloc_frame(&mut self) -> usize { + for i in self.skip_byte..self.bm.len() { + if self.bm[i] == 0xff { + continue + } + todo!() + } + 0 + } + + pub fn dealloc_frame(&mut self) -> Result<(),PMAError>{ + Ok(()) } -} -// pub struct PageAlloctor; + pub fn init(&mut self) { + for i in 0..self.skip_byte { + self.bm[i] = 0xff; + } + for i in self.skip_byte..self.bm.len() { + self.bm[i] = 0; + } + } +} |
