mirror of
https://github.com/rcore-os/rCore-Tutorial-v3.git
synced 2024-11-22 09:26:26 +04:00
sys_yield tests worked on qemu.
This commit is contained in:
parent
91043b08cd
commit
4590f233b5
@ -1,5 +1,3 @@
|
|||||||
use core::cell::RefCell;
|
|
||||||
use lazy_static::*;
|
|
||||||
use crate::trap::TrapContext;
|
use crate::trap::TrapContext;
|
||||||
use crate::task::TaskContext;
|
use crate::task::TaskContext;
|
||||||
use crate::config::*;
|
use crate::config::*;
|
||||||
@ -29,11 +27,13 @@ impl KernelStack {
|
|||||||
self.data.as_ptr() as usize + KERNEL_STACK_SIZE
|
self.data.as_ptr() as usize + KERNEL_STACK_SIZE
|
||||||
}
|
}
|
||||||
pub fn push_context(&self, trap_cx: TrapContext, task_cx: TaskContext) -> &'static mut TaskContext {
|
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::<TrapContext>()) as *mut TrapContext;
|
unsafe {
|
||||||
unsafe { *trap_cx_ptr = trap_cx; }
|
let trap_cx_ptr = (self.get_sp() - core::mem::size_of::<TrapContext>()) as *mut TrapContext;
|
||||||
let task_cx_ptr = (trap_cx_ptr as usize - core::mem::size_of::<TaskContext>()) as *mut TaskContext;
|
*trap_cx_ptr = trap_cx;
|
||||||
unsafe { *task_cx_ptr = task_cx; }
|
let task_cx_ptr = (trap_cx_ptr as usize - core::mem::size_of::<TaskContext>()) as *mut TaskContext;
|
||||||
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"); }
|
unsafe { llvm_asm!("fence.i" :::: "volatile"); }
|
||||||
// load apps
|
// load apps
|
||||||
for i in 0..num_app {
|
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);
|
let base_i = get_base_i(i);
|
||||||
// clear region
|
// clear region
|
||||||
(base_i..base_i + APP_SIZE_LIMIT).for_each(|addr| unsafe {
|
(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 {
|
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(
|
KERNEL_STACK[app_id].push_context(
|
||||||
TrapContext::app_init_context(get_base_i(app_id), USER_STACK[app_id].get_sp()),
|
TrapContext::app_init_context(get_base_i(app_id), USER_STACK[app_id].get_sp()),
|
||||||
TaskContext::goto_restore(),
|
TaskContext::goto_restore(),
|
||||||
|
@ -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) -> ! {
|
pub fn sys_exit(xstate: i32) -> ! {
|
||||||
println!("[kernel] Application exited with code {}", xstate);
|
println!("[kernel] Application exited with code {}", xstate);
|
||||||
//run_next_app()
|
mark_current_exited();
|
||||||
panic!("[kernel] first exit!");
|
run_next_task();
|
||||||
|
panic!("Unreachable in sys_exit!");
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn sys_yield() -> isize {
|
pub fn sys_yield() -> isize {
|
||||||
switch_to_next_task();
|
mark_current_suspended();
|
||||||
|
run_next_task();
|
||||||
0
|
0
|
||||||
}
|
}
|
@ -1,22 +1,24 @@
|
|||||||
mod context;
|
mod context;
|
||||||
mod switch;
|
mod switch;
|
||||||
|
mod task;
|
||||||
|
|
||||||
use crate::config::MAX_APP_NUM;
|
use crate::config::MAX_APP_NUM;
|
||||||
use crate::loader::{get_num_app, init_app_cx};
|
use crate::loader::{get_num_app, init_app_cx};
|
||||||
use core::cell::RefCell;
|
use core::cell::RefCell;
|
||||||
use lazy_static::*;
|
use lazy_static::*;
|
||||||
use switch::__switch;
|
use switch::__switch;
|
||||||
|
use task::{TaskControlBlock, TaskStatus};
|
||||||
|
|
||||||
pub use context::TaskContext;
|
pub use context::TaskContext;
|
||||||
|
|
||||||
struct TaskControlBlock {
|
|
||||||
task_cx_ptr: usize,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct TaskManager {
|
pub struct TaskManager {
|
||||||
num_app: usize,
|
num_app: usize,
|
||||||
|
inner: RefCell<TaskManagerInner>,
|
||||||
|
}
|
||||||
|
|
||||||
|
struct TaskManagerInner {
|
||||||
tasks: [TaskControlBlock; MAX_APP_NUM],
|
tasks: [TaskControlBlock; MAX_APP_NUM],
|
||||||
current_task: RefCell<usize>,
|
current_task: usize,
|
||||||
}
|
}
|
||||||
|
|
||||||
unsafe impl Sync for TaskManager {}
|
unsafe impl Sync for TaskManager {}
|
||||||
@ -24,30 +26,75 @@ unsafe impl Sync for TaskManager {}
|
|||||||
lazy_static! {
|
lazy_static! {
|
||||||
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 { 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 {
|
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 {
|
TaskManager {
|
||||||
num_app,
|
num_app,
|
||||||
tasks,
|
inner: RefCell::new(TaskManagerInner {
|
||||||
current_task: RefCell::new(0),
|
tasks,
|
||||||
|
current_task: 0,
|
||||||
|
}),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TaskManager {
|
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 {
|
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();
|
fn mark_current_suspended(&self) {
|
||||||
let next = if current == self.num_app - 1 { 0 } else { current + 1 };
|
let mut inner = self.inner.borrow_mut();
|
||||||
*self.current_task.borrow_mut() = next;
|
let current = inner.current_task;
|
||||||
unsafe {
|
inner.tasks[current].task_status = TaskStatus::Ready;
|
||||||
__switch(&self.tasks[current].task_cx_ptr, &self.tasks[next].task_cx_ptr);
|
}
|
||||||
|
|
||||||
|
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(
|
||||||
|
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();
|
TASK_MANAGER.run_first_task();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn switch_to_next_task() {
|
pub fn run_next_task() {
|
||||||
TASK_MANAGER.switch_to_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();
|
||||||
}
|
}
|
@ -1,7 +1,5 @@
|
|||||||
use super::TaskContext;
|
|
||||||
|
|
||||||
global_asm!(include_str!("switch.S"));
|
global_asm!(include_str!("switch.S"));
|
||||||
|
|
||||||
extern "C" {
|
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
18
os/src/task/task.rs
Normal 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,
|
||||||
|
}
|
@ -32,7 +32,7 @@ pub fn trap_handler(cx: &mut TrapContext) -> &mut TrapContext {
|
|||||||
}
|
}
|
||||||
Trap::Exception(Exception::StoreFault) |
|
Trap::Exception(Exception::StoreFault) |
|
||||||
Trap::Exception(Exception::StorePageFault) => {
|
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!");
|
panic!("[kernel] Cannot continue!");
|
||||||
//run_next_app();
|
//run_next_app();
|
||||||
}
|
}
|
||||||
|
@ -18,4 +18,6 @@ binary: elf
|
|||||||
build: binary
|
build: binary
|
||||||
|
|
||||||
clean:
|
clean:
|
||||||
@cargo clean
|
@cargo clean
|
||||||
|
|
||||||
|
.PHONY: elf binary build clean
|
@ -6,11 +6,14 @@ extern crate user_lib;
|
|||||||
|
|
||||||
use user_lib::sys_yield;
|
use user_lib::sys_yield;
|
||||||
|
|
||||||
|
const WIDTH: usize = 10;
|
||||||
|
const HEIGHT: usize = 5;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
fn main() -> i32 {
|
fn main() -> i32 {
|
||||||
for _ in 0..3 {
|
for i in 0..HEIGHT {
|
||||||
for _ in 0..10 { print!("A"); }
|
for _ in 0..WIDTH { print!("A"); }
|
||||||
println!("");
|
println!(" [{}/{}]", i + 1, HEIGHT);
|
||||||
sys_yield();
|
sys_yield();
|
||||||
}
|
}
|
||||||
println!("Test write_a OK!");
|
println!("Test write_a OK!");
|
||||||
|
@ -6,13 +6,16 @@ extern crate user_lib;
|
|||||||
|
|
||||||
use user_lib::sys_yield;
|
use user_lib::sys_yield;
|
||||||
|
|
||||||
|
const WIDTH: usize = 10;
|
||||||
|
const HEIGHT: usize = 2;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
fn main() -> i32 {
|
fn main() -> i32 {
|
||||||
for _ in 0..3 {
|
for i in 0..HEIGHT {
|
||||||
for _ in 0..10 { print!("B"); }
|
for _ in 0..WIDTH { print!("B"); }
|
||||||
println!("");
|
println!(" [{}/{}]", i + 1, HEIGHT);
|
||||||
sys_yield();
|
sys_yield();
|
||||||
}
|
}
|
||||||
println!("Test write_b OK!");
|
println!("Test write_b OK!");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
@ -6,13 +6,16 @@ extern crate user_lib;
|
|||||||
|
|
||||||
use user_lib::sys_yield;
|
use user_lib::sys_yield;
|
||||||
|
|
||||||
|
const WIDTH: usize = 10;
|
||||||
|
const HEIGHT: usize = 3;
|
||||||
|
|
||||||
#[no_mangle]
|
#[no_mangle]
|
||||||
fn main() -> i32 {
|
fn main() -> i32 {
|
||||||
for _ in 0..3 {
|
for i in 0..HEIGHT {
|
||||||
for _ in 0..10 { print!("C"); }
|
for _ in 0..WIDTH { print!("C"); }
|
||||||
println!("");
|
println!(" [{}/{}]", i + 1, HEIGHT);
|
||||||
sys_yield();
|
sys_yield();
|
||||||
}
|
}
|
||||||
println!("Test write_c OK!");
|
println!("Test write_c OK!");
|
||||||
0
|
0
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user