From 0a81014007ba99a894b0295bd475c186392ad373 Mon Sep 17 00:00:00 2001 From: lcy1996 <992195697@qq.com> Date: Tue, 16 Oct 2018 21:51:17 +0800 Subject: [PATCH] Add page handler for swap in/out in riscv32's pagefault --- crate/memory/src/swap/mock_swapper.rs | 3 ++- crate/memory/src/swap/mod.rs | 9 +++++---- kernel/src/arch/riscv32/interrupt.rs | 2 +- kernel/src/memory.rs | 21 ++++++++++++++++++++- 4 files changed, 28 insertions(+), 7 deletions(-) diff --git a/crate/memory/src/swap/mock_swapper.rs b/crate/memory/src/swap/mock_swapper.rs index ce4c922..ef6a57c 100644 --- a/crate/memory/src/swap/mock_swapper.rs +++ b/crate/memory/src/swap/mock_swapper.rs @@ -4,7 +4,8 @@ //! Used to test page table operation use super::Swapper; -use alloc::btree_map::BTreeMap; +//use alloc::btree_map::BTreeMap; +use alloc::collections::BTreeMap; use core::mem::uninitialized; const PAGE_SIZE: usize = 4096; diff --git a/crate/memory/src/swap/mod.rs b/crate/memory/src/swap/mod.rs index 4e358c5..aaffbdd 100644 --- a/crate/memory/src/swap/mod.rs +++ b/crate/memory/src/swap/mod.rs @@ -13,10 +13,11 @@ use core::ops::{Deref, DerefMut}; //pub use self::fifo::FifoSwapManager; pub use self::enhanced_clock::EnhancedClockSwapManager; -mod fifo; +pub mod fifo; mod enhanced_clock; -#[cfg(test)] -mod mock_swapper; +pub mod mock_swapper; +//#[cfg(test)] +//mod mock_swapper; /// Manage all swappable pages, decide which to swap out pub trait SwapManager { @@ -76,7 +77,7 @@ pub trait Swapper { } /// Wrapper for page table, supporting swap functions -struct SwapExt { +pub struct SwapExt { page_table: T, swap_manager: M, swapper: S, diff --git a/kernel/src/arch/riscv32/interrupt.rs b/kernel/src/arch/riscv32/interrupt.rs index 7a7a12b..7c781e1 100644 --- a/kernel/src/arch/riscv32/interrupt.rs +++ b/kernel/src/arch/riscv32/interrupt.rs @@ -73,7 +73,7 @@ pub extern fn rust_trap(tf: &mut TrapFrame) { Trap::Exception(E::IllegalInstruction) => illegal_inst(tf), Trap::Exception(E::UserEnvCall) => syscall(tf), Trap::Exception(E::LoadPageFault) => page_fault(tf), - Trap::Exception(E::StoreFault) => page_fault(tf), + Trap::Exception(E::StorePageFault) => page_fault(tf), Trap::Exception(E::InstructionPageFault) => page_fault(tf), _ => ::trap::error(tf), } diff --git a/kernel/src/memory.rs b/kernel/src/memory.rs index 6e38762..d735ce7 100644 --- a/kernel/src/memory.rs +++ b/kernel/src/memory.rs @@ -6,6 +6,7 @@ use super::HEAP_ALLOCATOR; use ucore_memory::{*, paging::PageTable}; use ucore_memory::cow::CowExt; pub use ucore_memory::memory_set::{MemoryArea, MemoryAttr, MemorySet as MemorySet_, Stack}; +use ucore_memory::swap::*; pub type MemorySet = MemorySet_; @@ -52,6 +53,16 @@ pub fn active_table() -> MutexGuard<'static, CowExt> { ACTIVE_TABLE.lock() } +// Page table for swpa in and out +lazy_static!{ + static ref ACTIVE_TABLE_SWAP: Mutex> = + Mutex::new(unsafe{SwapExt::new(ActivePageTable::new(), fifo::FifoSwapManager::default(), mock_swapper::MockSwapper::default())}); +} + +pub fn active_table_swap() -> MutexGuard<'static, SwapExt>{ + ACTIVE_TABLE_SWAP.lock() +} + /* * @param: * addr: the virtual address of the page fault @@ -63,7 +74,15 @@ pub fn active_table() -> MutexGuard<'static, CowExt> { pub fn page_fault_handler(addr: usize) -> bool { // Handle copy on write unsafe { ACTIVE_TABLE.force_unlock(); } - active_table().page_fault_handler(addr, || alloc_frame().unwrap()) + if active_table().page_fault_handler(addr, || alloc_frame().unwrap()){ + return true; + } + // handle the swap in/out + unsafe { ACTIVE_TABLE_SWAP.force_unlock(); } + if active_table_swap().page_fault_handler(addr, || alloc_frame()){ + return true; + } + false } pub fn init_heap() {