-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: use a different frame allocator
- Loading branch information
Showing
17 changed files
with
243 additions
and
240 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
pub mod allocator; | ||
pub mod empty; | ||
pub mod normal; | ||
pub mod page; | ||
pub mod pt; | ||
pub mod section; | ||
pub mod slab; | ||
pub mod user; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
use alloc::vec::Vec; | ||
use spin::Spin; | ||
|
||
use crate::{mem::allocator::PageAllocator, mm::address::PhyPageNum}; | ||
|
||
pub struct FrameAllocator { | ||
inner: Spin<FrameAllocatorInner>, | ||
} | ||
|
||
/// The allocator for page table. | ||
pub struct FrameAllocatorInner { | ||
start: PhyPageNum, | ||
end: PhyPageNum, | ||
recycled: Vec<PhyPageNum>, | ||
} | ||
|
||
impl const Default for FrameAllocatorInner { | ||
fn default() -> Self { | ||
Self { | ||
start: PhyPageNum(0), | ||
end: PhyPageNum(0), | ||
recycled: Default::default(), | ||
} | ||
} | ||
} | ||
|
||
impl const Default for FrameAllocator { | ||
fn default() -> Self { | ||
Self { | ||
inner: Spin::new(FrameAllocatorInner::default()), | ||
} | ||
} | ||
} | ||
|
||
impl PageAllocator for FrameAllocator { | ||
/// The init could be done only once. | ||
unsafe fn init(&self, start: PhyPageNum, end: PhyPageNum) { | ||
let mut guard = self.inner.lock(); | ||
guard.start = start; | ||
guard.end = end; | ||
guard.recycled.clear(); | ||
} | ||
|
||
fn alloc_page(&self) -> PhyPageNum { | ||
let mut guard = self.inner.lock(); | ||
let candidate = guard.recycled.pop(); | ||
if let Some(ppn) = candidate { | ||
ppn | ||
} else if guard.start < guard.end { | ||
let ppn = guard.start; | ||
guard.start += 1; | ||
ppn | ||
} else { | ||
PhyPageNum::null() | ||
} | ||
} | ||
|
||
fn dealloc_page(&self, ppn: PhyPageNum) { | ||
self.inner.lock().recycled.push(ppn); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use crate::{config::MEMORY_END, mem::allocator::PageAllocator}; | ||
|
||
use self::allocator::FrameAllocator; | ||
|
||
pub mod allocator; | ||
pub mod page; | ||
|
||
pub static FRAME_ALLOCATOR: FrameAllocator = FrameAllocator::default(); | ||
|
||
pub fn init_frame_allocator() { | ||
extern "C" { | ||
fn ekernel(); | ||
} | ||
unsafe { FRAME_ALLOCATOR.init((ekernel as usize).into(), MEMORY_END.into()) }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
use core::ops::{Deref, DerefMut}; | ||
|
||
use spin::SpinGuard; | ||
|
||
use crate::{ | ||
config::PAGE_SIZE, | ||
mem::{allocator::PageAllocator, page::Page}, | ||
mm::address::PhyPageNum, | ||
}; | ||
|
||
use super::FRAME_ALLOCATOR; | ||
|
||
/// One possible representation of metadata of page, when the page is used by the page table. | ||
/// | ||
/// Here, **pt** stands for page table. | ||
#[derive(Debug)] | ||
pub struct NormalPage { | ||
pub ppn: PhyPageNum, | ||
pub refcnt: usize, | ||
} | ||
|
||
pub struct NormalPageHandle { | ||
pub ppn: PhyPageNum, | ||
} | ||
|
||
/// Automatically fetch the lock, returning the guard. | ||
pub struct NormalPageGuard<'a> { | ||
pub ppn: PhyPageNum, | ||
guard: SpinGuard<'a, Page>, | ||
} | ||
|
||
impl NormalPage { | ||
/// Create a page with referencing count is equal to 1. | ||
pub fn alloc(ppn: PhyPageNum) { | ||
*Page::from_ppn(ppn).lock() = Page::Normal(Self { ppn, refcnt: 1 }); | ||
} | ||
} | ||
|
||
impl NormalPageHandle { | ||
pub fn new() -> Self { | ||
let ppn = FRAME_ALLOCATOR.alloc_page(); | ||
NormalPage::alloc(ppn); | ||
|
||
// init | ||
let handle = Self { ppn }; | ||
handle.init(); | ||
handle | ||
} | ||
|
||
pub fn init(&self) { | ||
let ptr = usize::from(self.ppn) as *mut u8; | ||
unsafe { | ||
core::slice::from_raw_parts_mut(ptr, PAGE_SIZE).fill(0); | ||
} | ||
} | ||
} | ||
|
||
impl Drop for NormalPageHandle { | ||
fn drop(&mut self) { | ||
let cnt = { | ||
let mut page = NormalPageGuard::new(self.ppn); | ||
page.refcnt -= 1; | ||
page.refcnt | ||
}; | ||
if cnt == 0 { | ||
FRAME_ALLOCATOR.dealloc_page(self.ppn); | ||
} | ||
} | ||
} | ||
|
||
impl<'a> NormalPageGuard<'a> { | ||
pub fn new(ppn: PhyPageNum) -> Self { | ||
let guard = Page::from_ppn(ppn).lock(); | ||
Self { ppn, guard } | ||
} | ||
} | ||
|
||
impl<'a> Deref for NormalPageGuard<'a> { | ||
type Target = NormalPage; | ||
|
||
fn deref(&self) -> &Self::Target { | ||
self.guard.as_normal() | ||
} | ||
} | ||
|
||
impl<'a> DerefMut for NormalPageGuard<'a> { | ||
fn deref_mut(&mut self) -> &mut Self::Target { | ||
self.guard.as_normal_mut() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.