Try batch but kernel panicked.
This commit is contained in:
parent
220397e5a5
commit
bae5383602
11 changed files with 385 additions and 17 deletions
23
os/src/trap/context.rs
Normal file
23
os/src/trap/context.rs
Normal file
|
@ -0,0 +1,23 @@
|
|||
use riscv::register::sstatus::{Sstatus, self, SPP};
|
||||
|
||||
#[repr(C)]
|
||||
pub struct TrapContext {
|
||||
x: [usize; 32],
|
||||
sstatus: Sstatus,
|
||||
sepc: usize,
|
||||
}
|
||||
|
||||
impl TrapContext {
|
||||
pub fn set_sp(&mut self, sp: usize) { self.x[2] = sp; }
|
||||
pub fn app_init_context(entry: usize, sp: usize) -> Self {
|
||||
let mut sstatus = sstatus::read();
|
||||
sstatus.set_spp(SPP::User);
|
||||
let mut cx = Self {
|
||||
x: [0; 32],
|
||||
sstatus,
|
||||
sepc: entry,
|
||||
};
|
||||
cx.set_sp(sp);
|
||||
cx
|
||||
}
|
||||
}
|
38
os/src/trap/mod.rs
Normal file
38
os/src/trap/mod.rs
Normal file
|
@ -0,0 +1,38 @@
|
|||
mod context;
|
||||
|
||||
use riscv::register::{
|
||||
mtvec::TrapMode,
|
||||
stvec,
|
||||
scause::{
|
||||
self,
|
||||
Trap,
|
||||
Exception,
|
||||
},
|
||||
stval,
|
||||
};
|
||||
|
||||
global_asm!(include_str!("trap.S"));
|
||||
|
||||
pub fn init() {
|
||||
extern "C" { fn __alltraps(); }
|
||||
unsafe {
|
||||
stvec::write(__alltraps as usize, TrapMode::Direct);
|
||||
}
|
||||
}
|
||||
|
||||
#[no_mangle]
|
||||
pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext {
|
||||
let scause = scause::read();
|
||||
let stval = stval::read();
|
||||
match scause.cause() {
|
||||
Trap::Exception(Exception::UserEnvCall) => {
|
||||
panic!("Triggered UserEnvCall!");
|
||||
}
|
||||
_ => {
|
||||
panic!("Unsupported trap!");
|
||||
}
|
||||
}
|
||||
cx
|
||||
}
|
||||
|
||||
pub use context::TrapContext;
|
64
os/src/trap/trap.S
Normal file
64
os/src/trap/trap.S
Normal file
|
@ -0,0 +1,64 @@
|
|||
.altmacro
|
||||
.macro SAVE_GP n
|
||||
sd x\n, \n*8(sp)
|
||||
.endm
|
||||
.macro LOAD_GP n
|
||||
ld x\n, \n*8(sp)
|
||||
.endm
|
||||
.section .text
|
||||
.globl __alltraps
|
||||
.globl __restore
|
||||
.align 2
|
||||
__alltraps:
|
||||
csrrw sp, sscratch, sp
|
||||
# now sp->kernel stack, sscratch->user stack
|
||||
# allocate a TrapContext on kernel stack
|
||||
addi sp, sp, -34*8
|
||||
# save general-purpose registers
|
||||
sd x1, 1*8(sp)
|
||||
# skip sp(x2), we will save it later
|
||||
sd x3, 3*8(sp)
|
||||
# skip tp(x4), application does not use it
|
||||
# save x5~x31
|
||||
.set n, 5
|
||||
.rept 27
|
||||
SAVE_GP %n
|
||||
.set n, n+1
|
||||
.endr
|
||||
# we can use t0/t1/t2 freely, because they were saved on kernel stack
|
||||
csrr t0, sstatus
|
||||
csrr t1, sepc
|
||||
sd t0, 32*8(sp)
|
||||
sd t1, 33*8(sp)
|
||||
# read user stack from sscratch and save it on the kernel stack
|
||||
csrr t2, sscratch
|
||||
sd t2, 2*8(sp)
|
||||
# set input argument of trap_handler(cx: &mut TrapContext)
|
||||
mv a0, sp
|
||||
call trap_handler
|
||||
|
||||
__restore:
|
||||
# case1: start running app by __restore
|
||||
# case2: back to U after handling trap
|
||||
mv sp, a0
|
||||
# now sp->kernel stack(after allocated), sscratch->user stack
|
||||
# restore sstatus/sepc
|
||||
ld t0, 32*8(sp)
|
||||
ld t1, 33*8(sp)
|
||||
ld t2, 2*8(sp)
|
||||
csrw sstatus, t0
|
||||
csrw sepc, t1
|
||||
csrw sscratch, t2
|
||||
# restore general-purpuse registers except sp/tp
|
||||
ld x1, 1*8(sp)
|
||||
ld x3, 3*8(sp)
|
||||
.set n, 5
|
||||
.rept 27
|
||||
LOAD_GP %n
|
||||
.set n, n+1
|
||||
.endr
|
||||
# release TrapContext on kernel stack
|
||||
addi sp, sp, 34*8
|
||||
# now sp->kernel stack, sscratch->user stack
|
||||
csrrw sp, sscratch, sp
|
||||
sret
|
Loading…
Add table
Add a link
Reference in a new issue