os/trap/
mod.rs

1//! Trap handling functionality
2//!
3//! For rCore, we have a single trap entry point, namely `__alltraps`. At
4//! initialization in [`init()`], we set the `stvec` CSR to point to it.
5//!
6//! All traps go through `__alltraps`, which is defined in `trap.S`. The
7//! assembly language code does just enough work restore the kernel space
8//! context, ensuring that Rust code safely runs, and transfers control to
9//! [`trap_handler()`].
10//!
11//! It then calls different functionality based on what exactly the exception
12//! was. For example, timer interrupts trigger task preemption, and syscalls go
13//! to [`syscall()`].
14
15mod context;
16
17use crate::syscall::syscall;
18use crate::task::{exit_current_and_run_next, suspend_current_and_run_next};
19use crate::timer::set_next_trigger;
20use core::arch::global_asm;
21use riscv::register::{
22    mtvec::TrapMode,
23    scause::{self, Exception, Interrupt, Trap},
24    sie, stval, stvec,
25};
26
27global_asm!(include_str!("trap.S"));
28
29/// initialize CSR `stvec` as the entry of `__alltraps`
30pub fn init() {
31    unsafe extern "C" {
32        safe fn __alltraps();
33    }
34    unsafe {
35        stvec::write(__alltraps as usize, TrapMode::Direct);
36    }
37}
38
39/// timer interrupt enabled
40pub fn enable_timer_interrupt() {
41    unsafe {
42        sie::set_stimer();
43    }
44}
45
46#[unsafe(no_mangle)]
47/// handle an interrupt, exception, or system call from user space
48pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext {
49    let scause = scause::read(); // get trap cause
50    let stval = stval::read(); // get extra value
51    match scause.cause() {
52        Trap::Exception(Exception::UserEnvCall) => {
53            cx.sepc += 4;
54            cx.x[10] = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]) as usize;
55        }
56        Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => {
57            println!(
58                "[kernel] PageFault in application, bad addr = {:#x}, bad instruction = {:#x}, kernel killed it.",
59                stval, cx.sepc
60            );
61            exit_current_and_run_next();
62        }
63        Trap::Exception(Exception::IllegalInstruction) => {
64            println!("[kernel] IllegalInstruction in application, kernel killed it.");
65            exit_current_and_run_next();
66        }
67        Trap::Interrupt(Interrupt::SupervisorTimer) => {
68            set_next_trigger();
69            suspend_current_and_run_next();
70        }
71        _ => {
72            panic!(
73                "Unsupported trap {:?}, stval = {:#x}!",
74                scause.cause(),
75                stval
76            );
77        }
78    }
79    cx
80}
81
82pub use context::TrapContext;