mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-07-04 19:46:04 +08:00
add os[1-8]-ref for os refereces, add guide, add README
This commit is contained in:
28
os3-ref/src/trap/context.rs
Normal file
28
os3-ref/src/trap/context.rs
Normal file
@@ -0,0 +1,28 @@
|
||||
//! Implementation of [`TrapContext`]
|
||||
|
||||
use riscv::register::sstatus::{self, Sstatus, SPP};
|
||||
|
||||
#[repr(C)]
|
||||
/// trap context structure containing sstatus, sepc and registers
|
||||
pub struct TrapContext {
|
||||
pub x: [usize; 32],
|
||||
pub sstatus: Sstatus,
|
||||
pub 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
|
||||
}
|
||||
}
|
||||
78
os3-ref/src/trap/mod.rs
Normal file
78
os3-ref/src/trap/mod.rs
Normal file
@@ -0,0 +1,78 @@
|
||||
//! 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::syscall::syscall;
|
||||
use crate::task::{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"));
|
||||
|
||||
/// initialize CSR `stvec` as the entry of `__alltraps`
|
||||
pub fn init() {
|
||||
extern "C" {
|
||||
fn __alltraps();
|
||||
}
|
||||
unsafe {
|
||||
stvec::write(__alltraps as usize, TrapMode::Direct);
|
||||
}
|
||||
}
|
||||
|
||||
/// timer interrupt enabled
|
||||
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(cx: &mut TrapContext) -> &mut TrapContext {
|
||||
let scause = scause::read(); // get trap cause
|
||||
let stval = stval::read(); // get extra value
|
||||
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) => {
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
cx
|
||||
}
|
||||
|
||||
pub use context::TrapContext;
|
||||
61
os3-ref/src/trap/trap.S
Normal file
61
os3-ref/src/trap/trap.S
Normal file
@@ -0,0 +1,61 @@
|
||||
.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:
|
||||
# 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
|
||||
Reference in New Issue
Block a user