This is a manual rebase of LCY's codemaster
parent
96a76290b6
commit
102866bcf9
@ -0,0 +1,34 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct ByFrame<T: FrameAllocator> {
|
||||||
|
flags: MemoryAttr,
|
||||||
|
allocator: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: FrameAllocator> MemoryHandler for ByFrame<T> {
|
||||||
|
fn box_clone(&self) -> Box<MemoryHandler> {
|
||||||
|
Box::new(self.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map(&self, pt: &mut PageTable, addr: VirtAddr) {
|
||||||
|
let target = self.allocator.alloc().expect("failed to allocate frame");
|
||||||
|
self.flags.apply(pt.map(addr, target));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unmap(&self, pt: &mut PageTable, addr: VirtAddr) {
|
||||||
|
let target = pt.get_entry(addr).expect("fail to get entry").target();
|
||||||
|
self.allocator.dealloc(target);
|
||||||
|
pt.unmap(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn page_fault_handler(&self, _pt: &mut PageTable, _addr: VirtAddr) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: FrameAllocator> ByFrame<T> {
|
||||||
|
pub fn new(flags: MemoryAttr, allocator: T) -> Self {
|
||||||
|
ByFrame { flags, allocator }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,46 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct Delay<T: FrameAllocator> {
|
||||||
|
flags: MemoryAttr,
|
||||||
|
allocator: T,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: FrameAllocator> MemoryHandler for Delay<T> {
|
||||||
|
fn box_clone(&self) -> Box<MemoryHandler> {
|
||||||
|
Box::new(self.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map(&self, pt: &mut PageTable, addr: VirtAddr) {
|
||||||
|
let entry = pt.map(addr, 0);
|
||||||
|
entry.set_present(false);
|
||||||
|
entry.update();
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unmap(&self, pt: &mut PageTable, addr: VirtAddr) {
|
||||||
|
let entry = pt.get_entry(addr).expect("failed to get entry");
|
||||||
|
if entry.present() {
|
||||||
|
self.allocator.dealloc(entry.target());
|
||||||
|
}
|
||||||
|
pt.unmap(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn page_fault_handler(&self, pt: &mut PageTable, addr: VirtAddr) -> bool {
|
||||||
|
let entry = pt.get_entry(addr).expect("failed to get entry");
|
||||||
|
if entry.present() {
|
||||||
|
// not a delay case
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
let frame = self.allocator.alloc().expect("failed to alloc frame");
|
||||||
|
entry.set_target(frame);
|
||||||
|
entry.set_present(true);
|
||||||
|
entry.update();
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl<T: FrameAllocator> Delay<T> {
|
||||||
|
pub fn new(flags: MemoryAttr, allocator: T) -> Self {
|
||||||
|
Delay { flags, allocator }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,32 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[derive(Debug, Eq, PartialEq, Clone)]
|
||||||
|
pub struct Linear {
|
||||||
|
offset: isize,
|
||||||
|
flags: MemoryAttr,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl MemoryHandler for Linear {
|
||||||
|
fn box_clone(&self) -> Box<MemoryHandler> {
|
||||||
|
Box::new(self.clone())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn map(&self, pt: &mut PageTable, addr: VirtAddr) {
|
||||||
|
let target = (addr as isize + self.offset) as PhysAddr;
|
||||||
|
self.flags.apply(pt.map(addr, target));
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unmap(&self, pt: &mut PageTable, addr: VirtAddr) {
|
||||||
|
pt.unmap(addr);
|
||||||
|
}
|
||||||
|
|
||||||
|
fn page_fault_handler(&self, _pt: &mut PageTable, _addr: VirtAddr) -> bool {
|
||||||
|
false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Linear {
|
||||||
|
pub fn new(offset: isize, flags: MemoryAttr) -> Self {
|
||||||
|
Linear { offset, flags }
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,29 @@
|
|||||||
|
use super::*;
|
||||||
|
|
||||||
|
// here may be a interesting part for lab
|
||||||
|
pub trait MemoryHandler: Debug + 'static {
|
||||||
|
fn box_clone(&self) -> Box<MemoryHandler>;
|
||||||
|
fn map(&self, pt: &mut PageTable, addr: VirtAddr);
|
||||||
|
fn unmap(&self, pt: &mut PageTable, addr: VirtAddr);
|
||||||
|
fn page_fault_handler(&self, pt: &mut PageTable, addr: VirtAddr) -> bool;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Clone for Box<MemoryHandler> {
|
||||||
|
fn clone(&self) -> Box<MemoryHandler> {
|
||||||
|
self.box_clone()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub trait FrameAllocator: Debug + Clone + 'static {
|
||||||
|
fn alloc(&self) -> Option<PhysAddr>;
|
||||||
|
fn dealloc(&self, target: PhysAddr);
|
||||||
|
}
|
||||||
|
|
||||||
|
mod linear;
|
||||||
|
mod byframe;
|
||||||
|
mod delay;
|
||||||
|
//mod swap;
|
||||||
|
|
||||||
|
pub use self::linear::Linear;
|
||||||
|
pub use self::byframe::ByFrame;
|
||||||
|
pub use self::delay::Delay;
|
Loading…
Reference in new issue