From 4590f233b54a31d334329484c0f9070dcff32251 Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Sun, 29 Nov 2020 04:01:38 +0800 Subject: [PATCH] sys_yield tests worked on qemu. --- os/src/loader.rs | 21 +++------ os/src/syscall/process.rs | 14 ++++-- os/src/task/mod.rs | 93 +++++++++++++++++++++++++++++++-------- os/src/task/switch.rs | 4 +- os/src/task/task.rs | 18 ++++++++ os/src/trap/mod.rs | 2 +- user/Makefile | 4 +- user/src/bin/00write_a.rs | 9 ++-- user/src/bin/01write_b.rs | 11 +++-- user/src/bin/02write_c.rs | 11 +++-- 10 files changed, 134 insertions(+), 53 deletions(-) create mode 100644 os/src/task/task.rs diff --git a/os/src/loader.rs b/os/src/loader.rs index 33c8dd5d..893a0677 100644 --- a/os/src/loader.rs +++ b/os/src/loader.rs @@ -1,5 +1,3 @@ -use core::cell::RefCell; -use lazy_static::*; use crate::trap::TrapContext; use crate::task::TaskContext; use crate::config::*; @@ -29,11 +27,13 @@ impl KernelStack { self.data.as_ptr() as usize + KERNEL_STACK_SIZE } pub fn push_context(&self, trap_cx: TrapContext, task_cx: TaskContext) -> &'static mut TaskContext { - let trap_cx_ptr = (self.get_sp() - core::mem::size_of::()) as *mut TrapContext; - unsafe { *trap_cx_ptr = trap_cx; } - let task_cx_ptr = (trap_cx_ptr as usize - core::mem::size_of::()) as *mut TaskContext; - unsafe { *task_cx_ptr = task_cx; } - unsafe { task_cx_ptr.as_mut().unwrap() } + unsafe { + let trap_cx_ptr = (self.get_sp() - core::mem::size_of::()) as *mut TrapContext; + *trap_cx_ptr = trap_cx; + let task_cx_ptr = (trap_cx_ptr as usize - core::mem::size_of::()) as *mut TaskContext; + *task_cx_ptr = task_cx; + task_cx_ptr.as_mut().unwrap() + } } } @@ -63,8 +63,6 @@ pub fn load_apps() { unsafe { llvm_asm!("fence.i" :::: "volatile"); } // load apps for i in 0..num_app { - println!("i = {}", i); - println!("[{:#x}, {:#x})", app_start[i], app_start[i + 1]); let base_i = get_base_i(i); // clear region (base_i..base_i + APP_SIZE_LIMIT).for_each(|addr| unsafe { @@ -82,11 +80,6 @@ pub fn load_apps() { } pub fn init_app_cx(app_id: usize) -> &'static TaskContext { - println!("app_id = {}, kernel_sp = {:#x}, user_sp = {:#x}", - app_id, - KERNEL_STACK[app_id].get_sp(), - USER_STACK[app_id].get_sp() - ); KERNEL_STACK[app_id].push_context( TrapContext::app_init_context(get_base_i(app_id), USER_STACK[app_id].get_sp()), TaskContext::goto_restore(), diff --git a/os/src/syscall/process.rs b/os/src/syscall/process.rs index 77a3a7c9..9e83ffe8 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -1,12 +1,18 @@ -use crate::task::switch_to_next_task; +use crate::task::{ + mark_current_suspended, + mark_current_exited, + run_next_task +}; pub fn sys_exit(xstate: i32) -> ! { println!("[kernel] Application exited with code {}", xstate); - //run_next_app() - panic!("[kernel] first exit!"); + mark_current_exited(); + run_next_task(); + panic!("Unreachable in sys_exit!"); } pub fn sys_yield() -> isize { - switch_to_next_task(); + mark_current_suspended(); + run_next_task(); 0 } \ No newline at end of file diff --git a/os/src/task/mod.rs b/os/src/task/mod.rs index 32efb4e8..b05485f2 100644 --- a/os/src/task/mod.rs +++ b/os/src/task/mod.rs @@ -1,22 +1,24 @@ mod context; mod switch; +mod task; use crate::config::MAX_APP_NUM; use crate::loader::{get_num_app, init_app_cx}; use core::cell::RefCell; use lazy_static::*; use switch::__switch; +use task::{TaskControlBlock, TaskStatus}; pub use context::TaskContext; -struct TaskControlBlock { - task_cx_ptr: usize, -} - pub struct TaskManager { num_app: usize, + inner: RefCell, +} + +struct TaskManagerInner { tasks: [TaskControlBlock; MAX_APP_NUM], - current_task: RefCell, + current_task: usize, } unsafe impl Sync for TaskManager {} @@ -24,30 +26,75 @@ unsafe impl Sync for TaskManager {} lazy_static! { pub static ref TASK_MANAGER: TaskManager = { let num_app = get_num_app(); - let mut tasks = [TaskControlBlock { task_cx_ptr: 0 }; MAX_APP_NUM]; + let mut tasks = [ + TaskControlBlock { task_cx_ptr: 0, task_status: TaskStatus::UnInit }; + MAX_APP_NUM + ]; for i in 0..num_app { - tasks[i] = TaskControlBlock { task_cx_ptr: init_app_cx(i) as *const _ as usize, }; + tasks[i].task_cx_ptr = init_app_cx(i) as * const _ as usize; + tasks[i].task_status = TaskStatus::Ready; } TaskManager { num_app, - tasks, - current_task: RefCell::new(0), + inner: RefCell::new(TaskManagerInner { + tasks, + current_task: 0, + }), } }; } impl TaskManager { - pub fn run_first_task(&self) { + fn run_first_task(&self) { + self.inner.borrow_mut().tasks[0].task_status = TaskStatus::Running; + let next_task_cx = self.inner.borrow().tasks[0].get_task_cx_ptr2(); unsafe { - __switch(&0usize, &self.tasks[0].task_cx_ptr); + __switch( + &0usize as *const _, + next_task_cx, + ); } } - pub fn switch_to_next_task(&self) { - let current = *self.current_task.borrow(); - let next = if current == self.num_app - 1 { 0 } else { current + 1 }; - *self.current_task.borrow_mut() = next; - unsafe { - __switch(&self.tasks[current].task_cx_ptr, &self.tasks[next].task_cx_ptr); + + fn mark_current_suspended(&self) { + let mut inner = self.inner.borrow_mut(); + let current = inner.current_task; + inner.tasks[current].task_status = TaskStatus::Ready; + } + + fn mark_current_exited(&self) { + let mut inner = self.inner.borrow_mut(); + let current = inner.current_task; + inner.tasks[current].task_status = TaskStatus::Exited; + } + + fn find_next_task(&self) -> Option { + let inner = self.inner.borrow(); + let current = inner.current_task; + (current + 1..current + self.num_app + 1) + .map(|id| id % self.num_app) + .find(|id| { + inner.tasks[*id].task_status == TaskStatus::Ready + }) + } + + fn run_next_task(&self) { + if let Some(next) = self.find_next_task() { + let mut inner = self.inner.borrow_mut(); + let current = inner.current_task; + inner.tasks[next].task_status = TaskStatus::Running; + inner.current_task = next; + let current_task_cx = inner.tasks[current].get_task_cx_ptr2(); + let next_task_cx = inner.tasks[next].get_task_cx_ptr2(); + core::mem::drop(inner); + unsafe { + __switch( + current_task_cx, + next_task_cx, + ); + } + } else { + panic!("[kernel] All applications completed!"); } } } @@ -56,6 +103,14 @@ pub fn run_first_task() { TASK_MANAGER.run_first_task(); } -pub fn switch_to_next_task() { - TASK_MANAGER.switch_to_next_task(); +pub fn run_next_task() { + TASK_MANAGER.run_next_task(); +} + +pub fn mark_current_suspended() { + TASK_MANAGER.mark_current_suspended(); +} + +pub fn mark_current_exited() { + TASK_MANAGER.mark_current_exited(); } \ No newline at end of file diff --git a/os/src/task/switch.rs b/os/src/task/switch.rs index 0b0f6b76..c5fe5f47 100644 --- a/os/src/task/switch.rs +++ b/os/src/task/switch.rs @@ -1,7 +1,5 @@ -use super::TaskContext; - global_asm!(include_str!("switch.S")); extern "C" { - pub fn __switch(current_task_cx: &usize, next_task_cx: &usize); + pub fn __switch(current_task_cx: *const usize, next_task_cx: *const usize); } diff --git a/os/src/task/task.rs b/os/src/task/task.rs new file mode 100644 index 00000000..54dc3600 --- /dev/null +++ b/os/src/task/task.rs @@ -0,0 +1,18 @@ +pub struct TaskControlBlock { + pub task_cx_ptr: usize, + pub task_status: TaskStatus, +} + +impl TaskControlBlock { + pub fn get_task_cx_ptr2(&self) -> *const usize { + &self.task_cx_ptr as *const usize + } +} + +#[derive(Copy, Clone, PartialEq)] +pub enum TaskStatus { + UnInit, + Ready, + Running, + Exited, +} \ No newline at end of file diff --git a/os/src/trap/mod.rs b/os/src/trap/mod.rs index 6948cbf1..a6288997 100644 --- a/os/src/trap/mod.rs +++ b/os/src/trap/mod.rs @@ -32,7 +32,7 @@ pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext { } Trap::Exception(Exception::StoreFault) | Trap::Exception(Exception::StorePageFault) => { - println!("[kernel] PageFault in application, core dumped."); + println!("[kernel] PageFault in application, bad addr = {:#x}, bad instruction = {:#x}, core dumped.", stval, cx.sepc); panic!("[kernel] Cannot continue!"); //run_next_app(); } diff --git a/user/Makefile b/user/Makefile index 1e548149..eabb4b27 100644 --- a/user/Makefile +++ b/user/Makefile @@ -18,4 +18,6 @@ binary: elf build: binary clean: - @cargo clean \ No newline at end of file + @cargo clean + +.PHONY: elf binary build clean \ No newline at end of file diff --git a/user/src/bin/00write_a.rs b/user/src/bin/00write_a.rs index a7ae889b..f7ada612 100644 --- a/user/src/bin/00write_a.rs +++ b/user/src/bin/00write_a.rs @@ -6,11 +6,14 @@ extern crate user_lib; use user_lib::sys_yield; +const WIDTH: usize = 10; +const HEIGHT: usize = 5; + #[no_mangle] fn main() -> i32 { - for _ in 0..3 { - for _ in 0..10 { print!("A"); } - println!(""); + for i in 0..HEIGHT { + for _ in 0..WIDTH { print!("A"); } + println!(" [{}/{}]", i + 1, HEIGHT); sys_yield(); } println!("Test write_a OK!"); diff --git a/user/src/bin/01write_b.rs b/user/src/bin/01write_b.rs index 713bfa58..e16b79da 100644 --- a/user/src/bin/01write_b.rs +++ b/user/src/bin/01write_b.rs @@ -6,13 +6,16 @@ extern crate user_lib; use user_lib::sys_yield; +const WIDTH: usize = 10; +const HEIGHT: usize = 2; + #[no_mangle] fn main() -> i32 { - for _ in 0..3 { - for _ in 0..10 { print!("B"); } - println!(""); + for i in 0..HEIGHT { + for _ in 0..WIDTH { print!("B"); } + println!(" [{}/{}]", i + 1, HEIGHT); sys_yield(); } println!("Test write_b OK!"); 0 -} \ No newline at end of file +} diff --git a/user/src/bin/02write_c.rs b/user/src/bin/02write_c.rs index 96edfef4..771f6447 100644 --- a/user/src/bin/02write_c.rs +++ b/user/src/bin/02write_c.rs @@ -6,13 +6,16 @@ extern crate user_lib; use user_lib::sys_yield; +const WIDTH: usize = 10; +const HEIGHT: usize = 3; + #[no_mangle] fn main() -> i32 { - for _ in 0..3 { - for _ in 0..10 { print!("C"); } - println!(""); + for i in 0..HEIGHT { + for _ in 0..WIDTH { print!("C"); } + println!(" [{}/{}]", i + 1, HEIGHT); sys_yield(); } println!("Test write_c OK!"); 0 -} \ No newline at end of file +}