sys_yield tests worked on qemu.

This commit is contained in:
Yifan Wu 2020-11-29 04:01:38 +08:00
parent 91043b08cd
commit 4590f233b5
10 changed files with 134 additions and 53 deletions

View File

@ -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 {
unsafe {
let trap_cx_ptr = (self.get_sp() - core::mem::size_of::<TrapContext>()) as *mut TrapContext;
unsafe { *trap_cx_ptr = trap_cx; }
*trap_cx_ptr = trap_cx;
let task_cx_ptr = (trap_cx_ptr as usize - core::mem::size_of::<TaskContext>()) as *mut TaskContext;
unsafe { *task_cx_ptr = task_cx; }
unsafe { task_cx_ptr.as_mut().unwrap() }
*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(),

View File

@ -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
}

View File

@ -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<TaskManagerInner>,
}
struct TaskManagerInner {
tasks: [TaskControlBlock; MAX_APP_NUM],
current_task: RefCell<usize>,
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,
inner: RefCell::new(TaskManagerInner {
tasks,
current_task: RefCell::new(0),
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;
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<usize> {
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(&self.tasks[current].task_cx_ptr, &self.tasks[next].task_cx_ptr);
__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();
}

View File

@ -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);
}

18
os/src/task/task.rs Normal file
View File

@ -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,
}

View File

@ -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();
}

View File

@ -19,3 +19,5 @@ build: binary
clean:
@cargo clean
.PHONY: elf binary build clean

View File

@ -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!");

View File

@ -6,11 +6,14 @@ 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!");

View File

@ -6,11 +6,14 @@ 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!");