|
|
@ -2,6 +2,7 @@ use core::arch::asm;
|
|
|
|
use lazy_static::*;
|
|
|
|
use lazy_static::*;
|
|
|
|
use riscv::register::mcause::Trap;
|
|
|
|
use riscv::register::mcause::Trap;
|
|
|
|
use crate::println;
|
|
|
|
use crate::println;
|
|
|
|
|
|
|
|
use crate::sbi::shutdown;
|
|
|
|
use crate::sync::UPSafeCell;
|
|
|
|
use crate::sync::UPSafeCell;
|
|
|
|
use crate::trap::TrapContext;
|
|
|
|
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() -> ! {
|
|
|
|
pub fn run_next_app() -> ! {
|
|
|
|
// 运行一个新的app
|
|
|
|
// 得到下一个需要运行的app的入口地址
|
|
|
|
// 把需要执行的指定app, 加载到执行位置APP_BASE_ADDRESS
|
|
|
|
|
|
|
|
// app_manager 需要drop 或者在一个作用域中, 因为这个函数不会返回, 后面直接进入用户态了
|
|
|
|
|
|
|
|
{
|
|
|
|
|
|
|
|
let mut app_manager = APP_MANAGER.exclusive_access();
|
|
|
|
let mut app_manager = APP_MANAGER.exclusive_access();
|
|
|
|
unsafe{
|
|
|
|
let current_app = app_manager.current_app;
|
|
|
|
app_manager.load_app(app_manager.current_app);
|
|
|
|
if current_app >= app_manager.num_app {
|
|
|
|
|
|
|
|
shutdown();
|
|
|
|
println!("------------- run_next_app load app {}", app_manager.current_app);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
app_manager.current_app += 1;
|
|
|
|
}
|
|
|
|
drop(app_manager);
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
extern "C" {
|
|
|
|
extern "C" {
|
|
|
|
fn __restore(trap_context_ptr: usize);
|
|
|
|
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;
|
|
|
|
let user_stack_top = USER_STACK.as_ptr() as usize + USER_STACK_SIZE;
|
|
|
|
// 得到用户trap的上下文以及寄存器状态
|
|
|
|
// 得到用户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到内核栈
|
|
|
|
// 把用户trap copy到内核栈
|
|
|
|
let kernel_stack_top = KERNEL_STACK.as_ptr() as usize + KERNEL_STACK_SIZE; // 现在栈顶和栈底都在一个内存位置
|
|
|
|
let kernel_stack_top = KERNEL_STACK.as_ptr() as usize + KERNEL_STACK_SIZE; // 现在栈顶和栈底都在一个内存位置
|
|
|
|