mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-05-05 18:01:31 +08:00
116 lines
3.2 KiB
Rust
116 lines
3.2 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 riscv::register::{
|
|
mtvec::TrapMode,
|
|
scause::{self, Exception, Interrupt, Trap},
|
|
sie, stval, stvec,
|
|
};
|
|
|
|
core::arch::global_asm!(include_str!("trap.S"));
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
pub fn enable_timer_interrupt() {
|
|
unsafe {
|
|
sie::set_stimer();
|
|
}
|
|
}
|
|
|
|
#[no_mangle]
|
|
pub fn trap_handler() -> ! {
|
|
set_kernel_trap_entry();
|
|
let cx = current_trap_cx();
|
|
let scause = scause::read();
|
|
let stval = stval::read();
|
|
match scause.cause() {
|
|
Trap::Exception(Exception::UserEnvCall) => {
|
|
cx.sepc += 4;
|
|
cx.x[10] = syscall(cx.x[17], [cx.x[10], cx.x[11], cx.x[12]]) as usize;
|
|
}
|
|
Trap::Exception(Exception::StoreFault)
|
|
| Trap::Exception(Exception::StorePageFault)
|
|
| Trap::Exception(Exception::LoadPageFault) => {
|
|
error!("[kernel] PageFault in application, bad addr = {:#x}, bad instruction = {:#x}, core dumped.", stval, cx.sepc);
|
|
exit_current_and_run_next();
|
|
}
|
|
Trap::Exception(Exception::IllegalInstruction) => {
|
|
error!("[kernel] IllegalInstruction in application, core dumped.");
|
|
exit_current_and_run_next();
|
|
}
|
|
Trap::Interrupt(Interrupt::SupervisorTimer) => {
|
|
set_next_trigger();
|
|
suspend_current_and_run_next();
|
|
}
|
|
_ => {
|
|
panic!(
|
|
"Unsupported trap {:?}, stval = {:#x}!",
|
|
scause.cause(),
|
|
stval
|
|
);
|
|
}
|
|
}
|
|
trap_return();
|
|
}
|
|
|
|
#[no_mangle]
|
|
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 {
|
|
core::arch::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]
|
|
pub fn trap_from_kernel() -> ! {
|
|
panic!("a trap from kernel!");
|
|
}
|
|
|
|
pub use context::TrapContext;
|