diff --git a/ch3-coop/os/src/batch.rs b/ch3-coop/os/src/batch.rs index 559a257..a42c9e9 100644 --- a/ch3-coop/os/src/batch.rs +++ b/ch3-coop/os/src/batch.rs @@ -2,6 +2,7 @@ use core::arch::asm; use lazy_static::*; use riscv::register::mcause::Trap; use crate::println; +use crate::sbi::shutdown; use crate::sync::UPSafeCell; use crate::trap::TrapContext; @@ -64,31 +65,6 @@ impl AppManager{ ); } } - - // 把app_idx位置的app 加载到指定的内存APP_BASE_ADDRESS位置 - unsafe fn load_app(&self, app_idx: usize){ - if app_idx >= self.num_app{ - panic!("All applications completed!") - } - // 清空app执行区域的内存 - core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, APP_SIZE_LIMIT).fill(0); - - // 得到指定app_idx位置的数据(下一个位置的开始 - 需要运行app的开始 = 需要运行app的内存大小 - let app_src = core::slice::from_raw_parts(self.app_start_lis[app_idx] as *const u8, - self.app_start_lis[app_idx + 1] - self.app_start_lis[app_idx]); - - // 把app的代码 copy到指定的运行的区域 - let app_len = app_src.len(); - if app_len > APP_SIZE_LIMIT { - panic!("app memory overrun!") - } - core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, app_len) - .copy_from_slice(app_src); - - // 刷新cache - asm!("fence.i"); - } - } @@ -97,18 +73,17 @@ pub fn init() { } pub fn run_next_app() -> ! { - // 运行一个新的app - // 把需要执行的指定app, 加载到执行位置APP_BASE_ADDRESS - // app_manager 需要drop 或者在一个作用域中, 因为这个函数不会返回, 后面直接进入用户态了 - { - let mut app_manager = APP_MANAGER.exclusive_access(); - unsafe{ - app_manager.load_app(app_manager.current_app); - - println!("------------- run_next_app load app {}", app_manager.current_app); - } - app_manager.current_app += 1; + // 得到下一个需要运行的app的入口地址 + let mut app_manager = APP_MANAGER.exclusive_access(); + let current_app = app_manager.current_app; + if current_app >= app_manager.num_app { + shutdown(); } + let app_entry_ptr = app_manager.app_start_lis[current_app]; + println!("------------- run_next_app load app {}, {:x}", current_app, app_entry_ptr); + app_manager.current_app += 1; + drop(app_manager); + extern "C" { fn __restore(trap_context_ptr: usize); @@ -118,7 +93,7 @@ pub fn run_next_app() -> ! { // 得到用户栈的栈顶 let user_stack_top = USER_STACK.as_ptr() as usize + USER_STACK_SIZE; // 得到用户trap的上下文以及寄存器状态 - let user_trap_context = TrapContext::app_init_context(APP_BASE_ADDRESS, user_stack_top); + let user_trap_context = TrapContext::app_init_context(app_entry_ptr, user_stack_top); // 把用户trap copy到内核栈 let kernel_stack_top = KERNEL_STACK.as_ptr() as usize + KERNEL_STACK_SIZE; // 现在栈顶和栈底都在一个内存位置 diff --git a/ch3-coop/os/src/loader.rs b/ch3-coop/os/src/loader.rs index b5fa6d5..2fa5b83 100644 --- a/ch3-coop/os/src/loader.rs +++ b/ch3-coop/os/src/loader.rs @@ -9,7 +9,7 @@ extern "C" { // 得到用户app的数量 fn get_num_app() -> usize{ unsafe{ - _num_app as usize as *const usize.read_volatile() + (_num_app as usize as *const usize).read_volatile() } } @@ -27,7 +27,7 @@ pub fn load_app() { }; // 清除缓存 - unsafe { asm!("fence.i" :::: "volatile"); } + unsafe { asm!("fence.i"); } // 加载app for app_id in 0..num_app { diff --git a/ch3-coop/os/src/main.rs b/ch3-coop/os/src/main.rs index ffc78a9..bb76f3f 100644 --- a/ch3-coop/os/src/main.rs +++ b/ch3-coop/os/src/main.rs @@ -8,9 +8,9 @@ use lang_items::console; pub mod lang_items; pub mod sbi; -// pub mod batch; +pub mod batch; pub mod sync; -// pub mod trap; +pub mod trap; pub mod syscall; pub mod loader; pub mod config; @@ -39,9 +39,12 @@ pub fn rust_main(){ println!("boot_stack_top_bound: {:#x}, boot_stack_lower_bound: {:#x}", boot_stack_top_bound as usize, boot_stack_lower_bound as usize); loader::load_app(); - // trap::init(); - // batch::init(); - // batch::run_next_app(); + trap::init(); + batch::init(); + batch::run_next_app(); + loop { + + } } /// ## 初始化bss段 diff --git a/ch3-coop/user/src/bin/00write_a.rs b/ch3-coop/user/src/bin/00write_a.rs index 3303899..67088ed 100644 --- a/ch3-coop/user/src/bin/00write_a.rs +++ b/ch3-coop/user/src/bin/00write_a.rs @@ -13,7 +13,7 @@ fn main() -> i32 { print!("A"); } println!(" [{}/{}]", i + 1, HEIGHT); - sys_yield(); + // sys_yield(); } println!("Test write_a OK!"); 0 diff --git a/ch3-coop/user/src/bin/01write_b.rs b/ch3-coop/user/src/bin/01write_b.rs index 64bd665..1ae798f 100644 --- a/ch3-coop/user/src/bin/01write_b.rs +++ b/ch3-coop/user/src/bin/01write_b.rs @@ -13,7 +13,7 @@ fn main() -> i32 { print!("B"); } println!(" [{}/{}]", i + 1, HEIGHT); - sys_yield(); + // sys_yield(); } println!("Test write_b OK!"); 0 diff --git a/ch3-coop/user/src/bin/02write_c.rs b/ch3-coop/user/src/bin/02write_c.rs index 25464bf..f9c8f71 100644 --- a/ch3-coop/user/src/bin/02write_c.rs +++ b/ch3-coop/user/src/bin/02write_c.rs @@ -13,7 +13,7 @@ fn main() -> i32 { print!("C"); } println!(" [{}/{}]", i + 1, HEIGHT); - sys_yield(); + // sys_yield(); } println!("Test write_c OK!"); 0