From 89bcd5f6601d8f9a069589bbbad1e167ef8f049f Mon Sep 17 00:00:00 2001 From: WangRunji Date: Fri, 13 Jul 2018 01:45:48 +0800 Subject: [PATCH] Fix kernel thread --- crate/riscv | 2 +- src/arch/riscv32/context.rs | 7 ++++++- src/arch/riscv32/interrupt.rs | 7 +++++-- src/arch/riscv32/memory.rs | 9 ++++----- src/arch/riscv32/paging.rs | 8 ++++---- 5 files changed, 20 insertions(+), 13 deletions(-) diff --git a/crate/riscv b/crate/riscv index b979c91..a8bbb3e 160000 --- a/crate/riscv +++ b/crate/riscv @@ -1 +1 @@ -Subproject commit b979c91db3bcfd9c9e9edabf45ef04e41dc4f659 +Subproject commit a8bbb3eb031fc377098e1493b14f22ba8448b2ad diff --git a/src/arch/riscv32/context.rs b/src/arch/riscv32/context.rs index 8ac7e58..d889238 100644 --- a/src/arch/riscv32/context.rs +++ b/src/arch/riscv32/context.rs @@ -12,11 +12,16 @@ pub struct TrapFrame { /// 用于在内核栈中构造新线程的中断帧 impl TrapFrame { - fn new_kernel_thread(entry: extern fn(usize) -> !, arg: usize, rsp: usize) -> Self { + fn new_kernel_thread(entry: extern fn(usize) -> !, arg: usize, sp: usize) -> Self { use core::mem::zeroed; let mut tf: Self = unsafe { zeroed() }; tf.x[10] = arg; // a0 + tf.x[2] = sp; tf.sepc = entry as usize; + tf.sstatus = sstatus::read(); + tf.sstatus.set_spie(true); + tf.sstatus.set_sie(false); + tf.sstatus.set_spp(sstatus::SPP::Supervisor); tf } fn new_user_thread(entry_addr: usize, rsp: usize) -> Self { diff --git a/src/arch/riscv32/interrupt.rs b/src/arch/riscv32/interrupt.rs index 248b71a..ef67896 100644 --- a/src/arch/riscv32/interrupt.rs +++ b/src/arch/riscv32/interrupt.rs @@ -19,7 +19,9 @@ pub unsafe fn enable() { #[inline(always)] pub unsafe fn disable_and_store() -> usize { - sstatus::read().sie() as usize + let e = sstatus::read().sie() as usize; + sstatus::clear_sie(); + e } #[inline(always)] @@ -35,9 +37,10 @@ pub extern fn rust_trap(tf: &mut TrapFrame) { trace!("Interrupt: {:?}", tf.scause.cause()); match tf.scause.cause() { Trap::Interrupt(SupervisorTimer) => timer(), - _ => panic!("Unhandled interrupt: {:?}\n{:#x?}", tf.scause.cause(), tf), + _ => panic!("Unhandled interrupt: {:?}\n{:#010x?}", tf.scause.cause(), tf), } ::trap::before_return(); + trace!("Interrupt end"); } fn timer() { diff --git a/src/arch/riscv32/memory.rs b/src/arch/riscv32/memory.rs index a3aaacc..d8e7a7d 100644 --- a/src/arch/riscv32/memory.rs +++ b/src/arch/riscv32/memory.rs @@ -11,10 +11,8 @@ pub fn init() { let frame = Frame::of_addr(PhysAddr::new(&PAGE_TABLE_ROOT as *const _ as u32)); super::paging::setup_page_table(frame); init_frame_allocator(); - let ms = remap_the_kernel(); + remap_the_kernel(); init_heap(); - use core::mem::forget; - forget(ms); } fn init_frame_allocator() { @@ -34,7 +32,7 @@ fn init_frame_allocator() { } } -fn remap_the_kernel() -> MemorySet { +fn remap_the_kernel() { use consts::{KERNEL_HEAP_OFFSET, KERNEL_HEAP_SIZE}; let kstack = Stack { top: bootstacktop as usize, @@ -48,8 +46,9 @@ fn remap_the_kernel() -> MemorySet { ms.push(MemoryArea::new_identity(sbss as usize, ebss as usize, MemoryAttr::default(), "bss")); ms.push(MemoryArea::new(KERNEL_HEAP_OFFSET, KERNEL_HEAP_OFFSET + KERNEL_HEAP_SIZE, MemoryAttr::default(), "kernel_heap")); unsafe { ms.activate(); } + use core::mem::forget; + forget(ms); info!("kernel remap end"); - ms } // Symbols provided by linker script diff --git a/src/arch/riscv32/paging.rs b/src/arch/riscv32/paging.rs index c1b23a5..8821e81 100644 --- a/src/arch/riscv32/paging.rs +++ b/src/arch/riscv32/paging.rs @@ -218,11 +218,11 @@ impl InactivePageTable for InactivePageTable0 { impl InactivePageTable0 { fn map_kernel(&mut self) { let table = unsafe { &mut *ROOT_PAGE_TABLE }; - let e1 = table[KERNEL_PML4].clone(); - let e2 = table[KERNEL_PML4 + 1].clone(); + let e1 = table[KERNEL_PML4]; + assert!(!e1.is_unused()); + self.edit(|_| { - table[KERNEL_PML4] = e1; - table[KERNEL_PML4 + 1] = e2; + table[KERNEL_PML4].set(e1.frame(), EF::VALID | EF::GLOBAL); }); } }