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()`].
14mod context;
15
16use crate::config::{TRAMPOLINE, TRAP_CONTEXT};
17use crate::syscall::syscall;
18use crate::task::{
19    current_trap_cx, current_user_token, exit_current_and_run_next, suspend_current_and_run_next,
20};
21use crate::timer::set_next_trigger;
22use core::arch::{asm, global_asm};
23use riscv::register::{
24    mtvec::TrapMode,
25    scause::{self, Exception, Interrupt, Trap},
26    sie, stval, stvec,
27};
28
29global_asm!(include_str!("trap.S"));
30/// initialize CSR `stvec` as the entry of `__alltraps`
31pub fn init() {
32    set_kernel_trap_entry();
33}
34
35fn set_kernel_trap_entry() {
36    unsafe {
37        stvec::write(trap_from_kernel as usize, TrapMode::Direct);
38    }
39}
40
41fn set_user_trap_entry() {
42    unsafe {
43        stvec::write(TRAMPOLINE as usize, TrapMode::Direct);
44    }
45}
46/// enable timer interrupt in sie CSR
47pub fn enable_timer_interrupt() {
48    unsafe {
49        sie::set_stimer();
50    }
51}
52
53#[unsafe(no_mangle)]
54/// handle an interrupt, exception, or system call from user space
55pub fn trap_handler() -> ! {
56    set_kernel_trap_entry();
57    let scause = scause::read();
58    let stval = stval::read();
59    match scause.cause() {
60        Trap::Exception(Exception::UserEnvCall) => {
61            // jump to next instruction anyway
62            let mut cx = current_trap_cx();
63            cx.sepc += 4;
64            // get system call return value
65            let result = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]);
66            // cx is changed during sys_exec, so we have to call it again
67            cx = current_trap_cx();
68            cx.x[10] = result as usize;
69        }
70        Trap::Exception(Exception::StoreFault)
71        | Trap::Exception(Exception::StorePageFault)
72        | Trap::Exception(Exception::InstructionFault)
73        | Trap::Exception(Exception::InstructionPageFault)
74        | Trap::Exception(Exception::LoadFault)
75        | Trap::Exception(Exception::LoadPageFault) => {
76            println!(
77                "[kernel] {:?} in application, bad addr = {:#x}, bad instruction = {:#x}, kernel killed it.",
78                scause.cause(),
79                stval,
80                current_trap_cx().sepc,
81            );
82            // page fault exit code
83            exit_current_and_run_next(-2);
84        }
85        Trap::Exception(Exception::IllegalInstruction) => {
86            println!("[kernel] IllegalInstruction in application, kernel killed it.");
87            // illegal instruction exit code
88            exit_current_and_run_next(-3);
89        }
90        Trap::Interrupt(Interrupt::SupervisorTimer) => {
91            set_next_trigger();
92            suspend_current_and_run_next();
93        }
94        _ => {
95            panic!(
96                "Unsupported trap {:?}, stval = {:#x}!",
97                scause.cause(),
98                stval
99            );
100        }
101    }
102    trap_return();
103}
104
105#[unsafe(no_mangle)]
106/// set the new addr of __restore asm function in TRAMPOLINE page,
107/// set the reg a0 = trap_cx_ptr, reg a1 = phy addr of usr page table,
108/// finally, jump to new addr of __restore asm function
109pub fn trap_return() -> ! {
110    set_user_trap_entry();
111    let trap_cx_ptr = TRAP_CONTEXT;
112    let user_satp = current_user_token();
113    unsafe extern "C" {
114        unsafe fn __alltraps();
115        unsafe fn __restore();
116    }
117    let restore_va = __restore as usize - __alltraps as usize + TRAMPOLINE;
118    unsafe {
119        asm!(
120            "fence.i",
121            "jr {restore_va}",
122            restore_va = in(reg) restore_va,
123            in("a0") trap_cx_ptr,
124            in("a1") user_satp,
125            options(noreturn)
126        );
127    }
128}
129
130#[unsafe(no_mangle)]
131/// Unimplement: traps/interrupts/exceptions from kernel mode
132/// Todo: Chapter 9: I/O device
133pub fn trap_from_kernel() -> ! {
134    panic!("a trap {:?} from kernel!", scause::read().cause());
135}
136
137pub use context::TrapContext;