aboutsummaryrefslogtreecommitdiff
path: root/src/mm
diff options
context:
space:
mode:
authorTianhao Wang <wth@riseup.net>2024-02-25 19:16:35 +0100
committerTianhao Wang <shrik3@mailbox.org>2024-06-11 15:13:39 +0200
commitd0b37d9f6e423576d84367090af41fcf03630121 (patch)
tree2fa5e790ce8cfed24c19898ac0f8120e91d89135 /src/mm
parentb31c08a010d5113edb549dc6dda2f726f282277e (diff)
paging: add basic bitmap frame allocator
Diffstat (limited to 'src/mm')
-rw-r--r--src/mm/pma.rs70
1 files changed, 53 insertions, 17 deletions
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;
+ }
+ }
+}