二叠纪"锯齿螈"原始操作系统完成

ch3-coop
zhangxinyu 2 years ago
parent 3d8c59d4ac
commit abd6d04a0c

@ -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; // 现在栈顶和栈底都在一个内存位置

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

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

@ -13,7 +13,7 @@ fn main() -> i32 {
print!("A");
}
println!(" [{}/{}]", i + 1, HEIGHT);
sys_yield();
// sys_yield();
}
println!("Test write_a OK!");
0

@ -13,7 +13,7 @@ fn main() -> i32 {
print!("B");
}
println!(" [{}/{}]", i + 1, HEIGHT);
sys_yield();
// sys_yield();
}
println!("Test write_b OK!");
0

@ -13,7 +13,7 @@ fn main() -> i32 {
print!("C");
}
println!(" [{}/{}]", i + 1, HEIGHT);
sys_yield();
// sys_yield();
}
println!("Test write_c OK!");
0

Loading…
Cancel
Save