From 5e44304f740c4d745ff01a029592bb01eda29b8e Mon Sep 17 00:00:00 2001 From: Yuhao Zhou Date: Sat, 6 Apr 2019 20:26:10 +0800 Subject: [PATCH] Fix root page table ptr/buffer. --- kernel/src/arch/mipsel/boot/context.S | 4 +-- kernel/src/arch/mipsel/boot/trap.S | 8 +++--- kernel/src/arch/mipsel/context.rs | 4 --- kernel/src/arch/mipsel/interrupt.rs | 7 ++---- kernel/src/arch/mipsel/memory.rs | 9 +++++++ kernel/src/arch/mipsel/paging.rs | 36 ++++++++++++++++++++------- 6 files changed, 44 insertions(+), 24 deletions(-) diff --git a/kernel/src/arch/mipsel/boot/context.S b/kernel/src/arch/mipsel/boot/context.S index 86c9a85..6a3975a 100644 --- a/kernel/src/arch/mipsel/boot/context.S +++ b/kernel/src/arch/mipsel/boot/context.S @@ -5,7 +5,7 @@ .section .text.context .globl switch_context -.extern root_page_table_ptr +.extern _root_page_table_ptr switch_context: // save from's registers @@ -25,7 +25,7 @@ switch_context: sw ra, 12*4(sp) sw sp, 13*4(sp) - la s0, root_page_table_ptr + la s0, _root_page_table_ptr lw AT, 0(s0) sw AT, 4(sp) diff --git a/kernel/src/arch/mipsel/boot/trap.S b/kernel/src/arch/mipsel/boot/trap.S index 513b9cd..04cf983 100644 --- a/kernel/src/arch/mipsel/boot/trap.S +++ b/kernel/src/arch/mipsel/boot/trap.S @@ -158,9 +158,9 @@ kernel_stack: kernel_stack_top: .align 12 #PGSHIFT - .global root_page_table_buffer -root_page_table_buffer: + .global _root_page_table_buffer +_root_page_table_buffer: .space 1024 * 4 # 4KB - .global root_page_table_ptr -root_page_table_ptr: + .global _root_page_table_ptr +_root_page_table_ptr: .space 4 # 4bytes diff --git a/kernel/src/arch/mipsel/context.rs b/kernel/src/arch/mipsel/context.rs index 372a186..4d20869 100644 --- a/kernel/src/arch/mipsel/context.rs +++ b/kernel/src/arch/mipsel/context.rs @@ -1,9 +1,5 @@ use mips::registers::cp0; -extern { - static root_page_table_ptr : *mut usize; -} - /// Saved registers on a trap. #[derive(Clone)] #[repr(C)] diff --git a/kernel/src/arch/mipsel/interrupt.rs b/kernel/src/arch/mipsel/interrupt.rs index fc59fae..bf6256b 100644 --- a/kernel/src/arch/mipsel/interrupt.rs +++ b/kernel/src/arch/mipsel/interrupt.rs @@ -5,6 +5,7 @@ use crate::drivers::DRIVERS; use mips::paging::{PageTable as MIPSPageTable, PageTableEntry, PageTableFlags as EF, TwoLevelPageTable}; use mips::addr::*; pub use self::context::*; +use crate::arch::paging::get_root_page_table_ptr; use log::*; #[path = "context.rs"] @@ -131,10 +132,6 @@ fn syscall(tf: &mut TrapFrame) { tf.v0 = ret as usize; } -extern { - static root_page_table_ptr : *mut usize; -} - fn page_fault(tf: &mut TrapFrame) { // TODO: set access/dirty bit let addr = tf.vaddr; @@ -146,7 +143,7 @@ fn page_fault(tf: &mut TrapFrame) { let virt_addr = VirtAddr::new(addr); let root_table = unsafe { - &mut *(root_page_table_ptr as *mut MIPSPageTable) + &mut *(get_root_page_table_ptr() as *mut MIPSPageTable) }; let tlb_entry = root_table.lookup(addr); tlb::write_tlb_random(tlb_entry); diff --git a/kernel/src/arch/mipsel/memory.rs b/kernel/src/arch/mipsel/memory.rs index a1f09f9..67ff901 100644 --- a/kernel/src/arch/mipsel/memory.rs +++ b/kernel/src/arch/mipsel/memory.rs @@ -3,12 +3,21 @@ use rcore_memory::PAGE_SIZE; use log::*; use crate::memory::{FRAME_ALLOCATOR, init_heap, MemoryAttr, MemorySet, Linear}; use crate::consts::{MEMORY_OFFSET, MEMORY_END, KERNEL_OFFSET}; +use crate::arch::paging::*; /// Initialize the memory management module pub fn init() { // initialize heap and Frame allocator init_frame_allocator(); init_heap(); + + set_root_page_table_ptr(0xFFFF_FFFF); + extern "C" { + fn _root_page_table_buffer(); + fn _root_page_table_ptr(); + } + + println!("_root_page_table_ptr {:x}", _root_page_table_ptr as usize); } pub fn init_other() { diff --git a/kernel/src/arch/mipsel/paging.rs b/kernel/src/arch/mipsel/paging.rs index 772cea3..47c2899 100644 --- a/kernel/src/arch/mipsel/paging.rs +++ b/kernel/src/arch/mipsel/paging.rs @@ -6,8 +6,6 @@ use mips::paging::{Mapper, PageTable as MIPSPageTable, PageTableEntry, PageTable use mips::paging::{FrameAllocator, FrameDeallocator}; use rcore_memory::paging::*; use log::*; -#[cfg(target_arch = "riscv32")] -use crate::consts::KERNEL_P2_INDEX; pub struct ActivePageTable(TwoLevelPageTable<'static>, PageEntry); @@ -46,18 +44,38 @@ impl PageTable for ActivePageTable { } } +extern "C" { + fn _root_page_table_buffer(); + fn _root_page_table_ptr(); +} + +pub fn set_root_page_table_ptr(ptr : usize) { + unsafe { + *(_root_page_table_ptr as *mut usize) = ptr; + } +} + +pub fn get_root_page_table_ptr() -> usize { + unsafe { + *(_root_page_table_ptr as *mut usize) + } +} + +pub fn root_page_table_buffer() -> &'static mut MIPSPageTable { + unsafe { + &mut *(_root_page_table_ptr as *mut MIPSPageTable) + } +} + + impl PageTableExt for ActivePageTable {} /// The virtual address of root page table -extern { - static root_page_table_buffer : *mut MIPSPageTable; - static root_page_table_ptr : *mut usize; -} impl ActivePageTable { pub unsafe fn new() -> Self { ActivePageTable( - TwoLevelPageTable::new(&mut *(root_page_table_buffer as *mut MIPSPageTable)), + TwoLevelPageTable::new(root_page_table_buffer()), ::core::mem::uninitialized() ) } @@ -123,11 +141,11 @@ impl InactivePageTable for InactivePageTable0 { } unsafe fn set_token(token: usize) { - *root_page_table_ptr = token; + set_root_page_table_ptr(token); } fn active_token() -> usize { - unsafe { *root_page_table_ptr } + get_root_page_table_ptr() } fn flush_tlb() {