mirror of
https://github.com/LearningOS/rust-based-os-comp2022.git
synced 2026-07-04 03:26:04 +08:00
add os[1-8]-ref for os refereces, add guide, add README
This commit is contained in:
47
os6-ref/src/trap/context.rs
Normal file
47
os6-ref/src/trap/context.rs
Normal file
@@ -0,0 +1,47 @@
|
||||
//! Implementation of [`TrapContext`]
|
||||
|
||||
use riscv::register::sstatus::{self, Sstatus, SPP};
|
||||
|
||||
#[repr(C)]
|
||||
/// trap context structure containing sstatus, sepc and registers
|
||||
pub struct TrapContext {
|
||||
/// General-Purpose Register x0-31
|
||||
pub x: [usize; 32],
|
||||
/// sstatus
|
||||
pub sstatus: Sstatus,
|
||||
/// sepc
|
||||
pub sepc: usize,
|
||||
/// Token of kernel address space
|
||||
pub kernel_satp: usize,
|
||||
/// Kernel stack pointer of the current application
|
||||
pub kernel_sp: usize,
|
||||
/// Virtual address of trap handler entry point in kernel
|
||||
pub trap_handler: usize,
|
||||
}
|
||||
|
||||
impl TrapContext {
|
||||
pub fn set_sp(&mut self, sp: usize) {
|
||||
self.x[2] = sp;
|
||||
}
|
||||
pub fn app_init_context(
|
||||
entry: usize,
|
||||
sp: usize,
|
||||
kernel_satp: usize,
|
||||
kernel_sp: usize,
|
||||
trap_handler: usize,
|
||||
) -> Self {
|
||||
let mut sstatus = sstatus::read();
|
||||
// set CPU privilege to User after trapping back
|
||||
sstatus.set_spp(SPP::User);
|
||||
let mut cx = Self {
|
||||
x: [0; 32],
|
||||
sstatus,
|
||||
sepc: entry,
|
||||
kernel_satp,
|
||||
kernel_sp,
|
||||
trap_handler,
|
||||
};
|
||||
cx.set_sp(sp);
|
||||
cx
|
||||
}
|
||||
}
|
||||
131
os6-ref/src/trap/mod.rs
Normal file
131
os6-ref/src/trap/mod.rs
Normal file
@@ -0,0 +1,131 @@
|
||||
//! 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 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.x[13]]);
|
||||
// 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}, core dumped.",
|
||||
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, core dumped.");
|
||||
// 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
|
||||
);
|
||||
}
|
||||
}
|
||||
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!", scause::read().cause());
|
||||
}
|
||||
|
||||
pub use context::TrapContext;
|
||||
69
os6-ref/src/trap/trap.S
Normal file
69
os6-ref/src/trap/trap.S
Normal file
@@ -0,0 +1,69 @@
|
||||
.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.trampoline
|
||||
.globl __alltraps
|
||||
.globl __restore
|
||||
.align 2
|
||||
__alltraps:
|
||||
csrrw sp, sscratch, sp
|
||||
# now sp->*TrapContext in user space, sscratch->user stack
|
||||
# save other 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 have been saved in TrapContext
|
||||
csrr t0, sstatus
|
||||
csrr t1, sepc
|
||||
sd t0, 32*8(sp)
|
||||
sd t1, 33*8(sp)
|
||||
# read user stack from sscratch and save it in TrapContext
|
||||
csrr t2, sscratch
|
||||
sd t2, 2*8(sp)
|
||||
# load kernel_satp into t0
|
||||
ld t0, 34*8(sp)
|
||||
# load trap_handler into t1
|
||||
ld t1, 36*8(sp)
|
||||
# move to kernel_sp
|
||||
ld sp, 35*8(sp)
|
||||
# switch to kernel space
|
||||
csrw satp, t0
|
||||
sfence.vma
|
||||
# jump to trap_handler
|
||||
jr t1
|
||||
|
||||
__restore:
|
||||
# a0: *TrapContext in user space(Constant); a1: user space token
|
||||
# switch to user space
|
||||
csrw satp, a1
|
||||
sfence.vma
|
||||
csrw sscratch, a0
|
||||
mv sp, a0
|
||||
# now sp points to TrapContext in user space, start restoring based on it
|
||||
# restore sstatus/sepc
|
||||
ld t0, 32*8(sp)
|
||||
ld t1, 33*8(sp)
|
||||
csrw sstatus, t0
|
||||
csrw sepc, t1
|
||||
# restore general purpose registers except x0/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
|
||||
# back to user stack
|
||||
ld sp, 2*8(sp)
|
||||
sret
|
||||
Reference in New Issue
Block a user