From 6f2d059ba5b9c74e9c88e2f88082b203cc99e37a Mon Sep 17 00:00:00 2001 From: dzy Date: Wed, 26 Dec 2018 15:42:08 +0800 Subject: [PATCH] implement map_kernel, now enters the kernel well --- crate/riscv | 2 +- kernel/src/arch/riscv32/consts.rs | 5 ++++- kernel/src/arch/riscv32/memory.rs | 15 +++------------ kernel/src/arch/riscv32/mod.rs | 6 ++++++ kernel/src/arch/riscv32/paging.rs | 23 +++++++++++------------ 5 files changed, 25 insertions(+), 26 deletions(-) diff --git a/crate/riscv b/crate/riscv index f7bea54..c00e91c 160000 --- a/crate/riscv +++ b/crate/riscv @@ -1 +1 @@ -Subproject commit f7bea54d7f254b63b5a6130285ab421c23d2f3bd +Subproject commit c00e91c621fdba648dc15d81a7bcf392f21c423f diff --git a/kernel/src/arch/riscv32/consts.rs b/kernel/src/arch/riscv32/consts.rs index 99396c3..d6f6cd7 100644 --- a/kernel/src/arch/riscv32/consts.rs +++ b/kernel/src/arch/riscv32/consts.rs @@ -17,7 +17,10 @@ pub const KERN_VA_BASE: usize = 0; pub const KERN_VA_BASE: usize = 0xFFFF_FFFF_0000_0000; #[cfg(target_arch = "riscv32")] -pub const KERNEL_P2_INDEX: usize = 0x8000_0000 >> 22; +pub const KERNEL_P2_INDEX: usize = 0x8000_0000 >> 12 >> 10; +#[cfg(target_arch = "riscv64")] +pub const KERNEL_P4_INDEX: usize = 0x0000_FFFF_8000_0000 >> 12 >> 9 >> 9 >> 9; + #[cfg(feature = "board_k210")] pub const KERNEL_HEAP_SIZE: usize = 0x0010_0000; #[cfg(not(feature = "board_k210"))] diff --git a/kernel/src/arch/riscv32/memory.rs b/kernel/src/arch/riscv32/memory.rs index a56e249..17871ff 100644 --- a/kernel/src/arch/riscv32/memory.rs +++ b/kernel/src/arch/riscv32/memory.rs @@ -25,10 +25,12 @@ pub fn init() { unsafe { sstatus::set_sum(); } // Allow user memory access // initialize heap and Frame allocator init_frame_allocator(); + info!("init_frame_allocator end"); init_heap(); + info!("init_heap end"); // remap the kernel use 4K page remap_the_kernel(); - loop { } + info!("remap_the_kernel end"); } pub fn init_other() { @@ -86,32 +88,21 @@ fn remap_the_kernel() { unsafe { ms.activate(); } unsafe { SATP = ms.token(); } mem::forget(ms); - info!("kernel remap end"); } #[cfg(all(target_arch = "riscv64", not(feature = "no_mmu")))] fn remap_the_kernel() { - error!("remap the kernel begin, satp: {:x}", satp::read().bits()); let mut ms = MemorySet::new_bare(); - info!("ms new bare"); #[cfg(feature = "no_bbl")] ms.push(MemoryArea::new_identity(0x0000_0000_1000_0000, 0x0000_0000_1000_0008, MemoryAttr::default(), "serial")); ms.push(MemoryArea::new_identity(stext as usize, etext as usize, MemoryAttr::default().execute().readonly(), "text")); - info!("ms new ident text"); ms.push(MemoryArea::new_identity(sdata as usize, edata as usize, MemoryAttr::default(), "data")); - info!("ms new ident data"); ms.push(MemoryArea::new_identity(srodata as usize, erodata as usize, MemoryAttr::default().readonly(), "rodata")); - info!("ms new ident rodata"); ms.push(MemoryArea::new_identity(bootstack as usize, bootstacktop as usize, MemoryAttr::default(), "stack")); - info!("ms new ident rodatistack"); ms.push(MemoryArea::new_identity(sbss as usize, ebss as usize, MemoryAttr::default(), "bss")); - info!("ms push finish"); unsafe { ms.activate(); } - info!("ms activate finish"); unsafe { SATP = ms.token(); } - info!("satp token ok"); mem::forget(ms); - error!("kernel remap end"); } // First core stores its SATP here. diff --git a/kernel/src/arch/riscv32/mod.rs b/kernel/src/arch/riscv32/mod.rs index 7dc2d33..1223ccd 100644 --- a/kernel/src/arch/riscv32/mod.rs +++ b/kernel/src/arch/riscv32/mod.rs @@ -6,6 +6,7 @@ pub mod memory; pub mod compiler_rt; pub mod consts; pub mod cpu; +use log::*; #[no_mangle] pub extern fn rust_main(hartid: usize, dtb: usize, hart_mask: usize, functions: usize) -> ! { @@ -27,11 +28,16 @@ pub extern fn rust_main(hartid: usize, dtb: usize, hart_mask: usize, functions: crate::logging::init(); interrupt::init(); + info!("interrupt::init end"); memory::init(); + info!("memory::init end"); timer::init(); + info!("timer::init end"); crate::process::init(); + info!("crate::process::init end"); unsafe { cpu::start_others(hart_mask); } + info!("going to crate::kmain"); crate::kmain(); } diff --git a/kernel/src/arch/riscv32/paging.rs b/kernel/src/arch/riscv32/paging.rs index fa3bab2..46a55a3 100644 --- a/kernel/src/arch/riscv32/paging.rs +++ b/kernel/src/arch/riscv32/paging.rs @@ -12,6 +12,8 @@ use ucore_memory::paging::*; use log::*; #[cfg(target_arch = "riscv32")] use crate::consts::KERNEL_P2_INDEX; +#[cfg(target_arch = "riscv64")] +use crate::consts::KERNEL_P4_INDEX; pub struct ActivePageTable(RecursivePageTable<'static>, PageEntry); @@ -368,14 +370,6 @@ impl InactivePageTable for InactivePageTable0 { unsafe fn activate(&self) { let old_frame = satp::read().frame(); let new_frame = self.root_frame.clone(); - active_table().with_temporary_map(&new_frame, |_, table: &mut RvPageTable| { - info!("new_frame's pa: {:x}", new_frame.start_address().as_usize()); - info!("entry 0o0: {:?}", table[0x0]); - info!("entry 0o774: {:?}", table[0x1fc]); - info!("entry 0o775: {:?}", table[0x1fd]); - info!("entry 0o776: {:?}", table[0x1fe]); - info!("entry 0o777: {:?}", table[0x1ff]); - }); debug!("switch table {:x?} -> {:x?}", old_frame, new_frame); if old_frame != new_frame { satp::set(SATP_MODE, 0, new_frame); @@ -421,7 +415,6 @@ impl InactivePageTable for InactivePageTable0 { #[cfg(target_arch = "riscv64")] fn token(&self) -> usize { use bit_field::BitField; - info!("{}", self.root_frame.number()); let mut satp = self.root_frame.number(); satp.set_bits(44..60, 0); // AS is 0 satp.set_bits(60..64, satp::Mode::Sv48 as usize); // Mode is Sv48 @@ -452,7 +445,7 @@ impl InactivePageTable0 { let table = unsafe { &mut *ROOT_PAGE_TABLE }; let e0 = table[0x40]; let e1 = table[KERNEL_P2_INDEX]; - // for larger heap memroy + // for larger heap memory let e2 = table[KERNEL_P2_INDEX + 1]; let e3 = table[KERNEL_P2_INDEX + 2]; assert!(!e1.is_unused()); @@ -467,10 +460,16 @@ impl InactivePageTable0 { table[KERNEL_P2_INDEX + 2].set(e3.frame(), EF::VALID | EF::GLOBAL); }); } + #[cfg(target_arch = "riscv64")] fn map_kernel(&mut self) { - unimplemented!(); - // TODO + let table = unsafe { &mut *ROOT_PAGE_TABLE }; + let e1 = table[KERNEL_P4_INDEX]; + assert!(!e1.is_unused()); + + self.edit(|_| { + table[KERNEL_P4_INDEX] = e1; + }); } }