update comments

This commit is contained in:
Yu Chen 2022-03-25 16:45:39 +08:00
parent 5b9dfeb6ad
commit 676fdd86b7
4 changed files with 32 additions and 12 deletions

View file

@ -36,15 +36,14 @@ mod lang_items;
mod loader; mod loader;
mod sbi; mod sbi;
mod sync; mod sync;
mod syscall; pub mod syscall;
mod task; pub mod task;
mod timer; mod timer;
mod trap; pub mod trap;
global_asm!(include_str!("entry.asm")); global_asm!(include_str!("entry.asm"));
global_asm!(include_str!("link_app.S")); global_asm!(include_str!("link_app.S"));
/// clear BSS segment /// clear BSS segment
fn clear_bss() { fn clear_bss() {
extern "C" { extern "C" {

View file

@ -1,14 +1,19 @@
//! Implementation of [`TaskContext`] //! Implementation of [`TaskContext`]
/// Task Context
#[derive(Copy, Clone)] #[derive(Copy, Clone)]
#[repr(C)] #[repr(C)]
pub struct TaskContext { pub struct TaskContext {
/// return address ( e.g. __restore ) of __switch ASM function
ra: usize, ra: usize,
/// kernel stack pointer of app
sp: usize, sp: usize,
/// callee saved registers: s 0..11
s: [usize; 12], s: [usize; 12],
} }
impl TaskContext { impl TaskContext {
/// init task context
pub fn zero_init() -> Self { pub fn zero_init() -> Self {
Self { Self {
ra: 0, ra: 0,
@ -16,6 +21,8 @@ impl TaskContext {
s: [0; 12], s: [0; 12],
} }
} }
/// set task context {__restore ASM funciton, kernel stack, s_0..12 }
pub fn goto_restore(kstack_ptr: usize) -> Self { pub fn goto_restore(kstack_ptr: usize) -> Self {
extern "C" { extern "C" {
fn __restore(); fn __restore();

View file

@ -6,11 +6,12 @@
//! A single global instance of [`TaskManager`] called `TASK_MANAGER` controls //! A single global instance of [`TaskManager`] called `TASK_MANAGER` controls
//! all the tasks in the operating system. //! all the tasks in the operating system.
//! //!
//! Be careful when you see [`__switch`]. Control flow around this function //! Be careful when you see `__switch` ASM function in `switch.S`. Control flow around this function
//! might not be what you expect. //! might not be what you expect.
mod context; mod context;
mod switch; mod switch;
#[allow(clippy::module_inception)] #[allow(clippy::module_inception)]
mod task; mod task;
@ -39,7 +40,8 @@ pub struct TaskManager {
inner: UPSafeCell<TaskManagerInner>, inner: UPSafeCell<TaskManagerInner>,
} }
struct TaskManagerInner { /// Inner of Task Manager
pub struct TaskManagerInner {
/// task list /// task list
tasks: [TaskControlBlock; MAX_APP_NUM], tasks: [TaskControlBlock; MAX_APP_NUM],
/// id of current `Running` task /// id of current `Running` task
@ -47,6 +49,7 @@ struct TaskManagerInner {
} }
lazy_static! { lazy_static! {
/// Global variable: TASK_MANAGER
pub static ref TASK_MANAGER: TaskManager = { pub static ref TASK_MANAGER: TaskManager = {
let num_app = get_num_app(); let num_app = get_num_app();
let mut tasks = [TaskControlBlock { let mut tasks = [TaskControlBlock {
@ -135,27 +138,33 @@ impl TaskManager {
} }
} }
/// run first task
pub fn run_first_task() { pub fn run_first_task() {
TASK_MANAGER.run_first_task(); TASK_MANAGER.run_first_task();
} }
/// rust next task
fn run_next_task() { fn run_next_task() {
TASK_MANAGER.run_next_task(); TASK_MANAGER.run_next_task();
} }
/// suspend current task
fn mark_current_suspended() { fn mark_current_suspended() {
TASK_MANAGER.mark_current_suspended(); TASK_MANAGER.mark_current_suspended();
} }
/// exit current task
fn mark_current_exited() { fn mark_current_exited() {
TASK_MANAGER.mark_current_exited(); TASK_MANAGER.mark_current_exited();
} }
/// suspend current task, then run next task
pub fn suspend_current_and_run_next() { pub fn suspend_current_and_run_next() {
mark_current_suspended(); mark_current_suspended();
run_next_task(); run_next_task();
} }
/// exit current task, then run next task
pub fn exit_current_and_run_next() { pub fn exit_current_and_run_next() {
mark_current_exited(); mark_current_exited();
run_next_task(); run_next_task();

View file

@ -1,25 +1,30 @@
use riscv::register::sstatus::{self, Sstatus, SPP}; use riscv::register::sstatus::{self, Sstatus, SPP};
/// Trap Context
#[repr(C)] #[repr(C)]
pub struct TrapContext { pub struct TrapContext {
/// general regs[0..31]
pub x: [usize; 32], pub x: [usize; 32],
/// CSR sstatus
pub sstatus: Sstatus, pub sstatus: Sstatus,
/// CSR sepc
pub sepc: usize, pub sepc: usize,
} }
impl TrapContext { impl TrapContext {
/// set stack pointer to x_2 reg (sp)
pub fn set_sp(&mut self, sp: usize) { pub fn set_sp(&mut self, sp: usize) {
self.x[2] = sp; self.x[2] = sp;
} }
/// init app context
pub fn app_init_context(entry: usize, sp: usize) -> Self { pub fn app_init_context(entry: usize, sp: usize) -> Self {
let mut sstatus = sstatus::read(); let mut sstatus = sstatus::read(); // CSR sstatus
sstatus.set_spp(SPP::User); sstatus.set_spp(SPP::User); //previous privilege mode: user mode
let mut cx = Self { let mut cx = Self {
x: [0; 32], x: [0; 32],
sstatus, sstatus,
sepc: entry, sepc: entry, // entry point of app
}; };
cx.set_sp(sp); cx.set_sp(sp); // app's user stack pointer
cx cx // return initial Trap Context of app
} }
} }