140 lines
4.4 KiB
Rust
140 lines
4.4 KiB
Rust
//! Trap handling functionality
|
|
//!
|
|
//! For rCore, we have a single trap entry point, namely `__alltraps`. At
|
|
//! initialization in [`init()`], we set the `stvec` CSR to point to it.
|
|
//!
|
|
//! All traps go through `__alltraps`, which is defined in `trap.S`. The
|
|
//! assembly language code does just enough work restore the kernel space
|
|
//! context, ensuring that Rust code safely runs, and transfers control to
|
|
//! [`trap_handler()`].
|
|
//!
|
|
//! It then calls different functionality based on what exactly the exception
|
|
//! was. For example, timer interrupts trigger task preemption, and syscalls go
|
|
//! to [`syscall()`].
|
|
mod context;
|
|
|
|
use crate::config::{TRAMPOLINE, TRAP_CONTEXT};
|
|
use crate::syscall::syscall;
|
|
use crate::task::{
|
|
current_trap_cx, current_user_token, exit_current_and_run_next, suspend_current_and_run_next,
|
|
};
|
|
use crate::timer::set_next_trigger;
|
|
use core::arch::{asm, global_asm};
|
|
use riscv::register::{
|
|
mtvec::TrapMode,
|
|
scause::{self, Exception, Interrupt, Trap},
|
|
sie, stval, stvec,
|
|
};
|
|
|
|
global_asm!(include_str!("trap.S"));
|
|
/// initialize CSR `stvec` as the entry of `__alltraps`
|
|
pub fn init() {
|
|
set_kernel_trap_entry();
|
|
}
|
|
|
|
fn set_kernel_trap_entry() {
|
|
unsafe {
|
|
stvec::write(trap_from_kernel as usize, TrapMode::Direct);
|
|
}
|
|
}
|
|
|
|
fn set_user_trap_entry() {
|
|
unsafe {
|
|
stvec::write(TRAMPOLINE as usize, TrapMode::Direct);
|
|
}
|
|
}
|
|
/// enable timer interrupt in sie CSR
|
|
pub fn enable_timer_interrupt() {
|
|
unsafe {
|
|
sie::set_stimer();
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
/// handle an interrupt, exception, or system call from user space
|
|
pub fn trap_handler() -> ! {
|
|
set_kernel_trap_entry();
|
|
let scause = scause::read();
|
|
let stval = stval::read();
|
|
match scause.cause() {
|
|
Trap::Exception(Exception::UserEnvCall) => {
|
|
// jump to next instruction anyway
|
|
let mut cx = current_trap_cx();
|
|
cx.sepc += 4;
|
|
// get system call return value
|
|
let result = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]);
|
|
// cx is changed during sys_exec, so we have to call it again
|
|
cx = current_trap_cx();
|
|
cx.x[10] = result as usize;
|
|
}
|
|
Trap::Exception(Exception::StoreFault)
|
|
| Trap::Exception(Exception::StorePageFault)
|
|
| Trap::Exception(Exception::InstructionFault)
|
|
| Trap::Exception(Exception::InstructionPageFault)
|
|
| Trap::Exception(Exception::LoadFault)
|
|
| Trap::Exception(Exception::LoadPageFault) => {
|
|
println!(
|
|
"[kernel] {:?} in application, bad addr = {:#x}, bad instruction = {:#x}, kernel killed it.",
|
|
scause.cause(),
|
|
stval,
|
|
current_trap_cx().sepc,
|
|
);
|
|
// page fault exit code
|
|
exit_current_and_run_next(-2);
|
|
}
|
|
Trap::Exception(Exception::IllegalInstruction) => {
|
|
println!("[kernel] IllegalInstruction in application, kernel killed it.");
|
|
// illegal instruction exit code
|
|
exit_current_and_run_next(-3);
|
|
}
|
|
Trap::Interrupt(Interrupt::SupervisorTimer) => {
|
|
set_next_trigger();
|
|
suspend_current_and_run_next();
|
|
}
|
|
_ => {
|
|
panic!(
|
|
"Unsupported trap {:?}, stval = {:#x}!",
|
|
scause.cause(),
|
|
stval
|
|
);
|
|
}
|
|
}
|
|
//println!("before trap_return");
|
|
trap_return();
|
|
}
|
|
|
|
#[no_mangle]
|
|
/// set the new addr of __restore asm function in TRAMPOLINE page,
|
|
/// set the reg a0 = trap_cx_ptr, reg a1 = phy addr of usr page table,
|
|
/// finally, jump to new addr of __restore asm function
|
|
pub fn trap_return() -> ! {
|
|
set_user_trap_entry();
|
|
let trap_cx_ptr = TRAP_CONTEXT;
|
|
let user_satp = current_user_token();
|
|
extern "C" {
|
|
fn __alltraps();
|
|
fn __restore();
|
|
}
|
|
let restore_va = __restore as usize - __alltraps as usize + TRAMPOLINE;
|
|
unsafe {
|
|
asm!(
|
|
"fence.i",
|
|
"jr {restore_va}",
|
|
restore_va = in(reg) restore_va,
|
|
in("a0") trap_cx_ptr,
|
|
in("a1") user_satp,
|
|
options(noreturn)
|
|
);
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
/// Unimplement: traps/interrupts/exceptions from kernel mode
|
|
/// Todo: Chapter 9: I/O device
|
|
pub fn trap_from_kernel() -> ! {
|
|
use riscv::register::sepc;
|
|
println!("stval = {:#x}, sepc = {:#x}", stval::read(), sepc::read());
|
|
panic!("a trap {:?} from kernel!", scause::read().cause());
|
|
}
|
|
|
|
pub use context::TrapContext;
|