From a8bf43b1130734d8e2266e45a610e04cc7abc751 Mon Sep 17 00:00:00 2001 From: zhangxinyu <840317537@qq.com> Date: Thu, 8 Jun 2023 10:58:47 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9F=BA=E4=BA=8E=E5=BA=94=E7=94=A8=E5=90=8D?= =?UTF-8?q?=E7=9A=84=E5=BA=94=E7=94=A8=E5=8A=A0=E8=BD=BD=E5=99=A8=E5=AE=8C?= =?UTF-8?q?=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ch5/os/build.rs | 12 +++++++++++ ch5/os/src/loader.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++ ch5/os/src/main.rs | 11 ++++++---- 3 files changed, 70 insertions(+), 4 deletions(-) diff --git a/ch5/os/build.rs b/ch5/os/build.rs index d09ceea..c103d93 100644 --- a/ch5/os/build.rs +++ b/ch5/os/build.rs @@ -52,6 +52,18 @@ _num_app: } writeln!(f, r#" .quad app_{}_end"#, apps.len() - 1)?; + // app的名字列表 + writeln!( + f, + r#" + .global _app_names +_app_names:"# + )?; + for app in apps.iter() { + writeln!(f, r#" .string "{}""#, app)?; + } + + for (idx, app) in apps.iter().enumerate() { println!("app_{}: {}", idx, app); writeln!( diff --git a/ch5/os/src/loader.rs b/ch5/os/src/loader.rs index 75611bc..5a4893d 100644 --- a/ch5/os/src/loader.rs +++ b/ch5/os/src/loader.rs @@ -1,6 +1,9 @@ +use alloc::vec::Vec; use core::arch::asm; +use lazy_static::lazy_static; use crate::config::*; +use crate::*; use crate::trap::TrapContext; static KERNEL_STACK: [[u8; KERNEL_STACK_SIZE]; MAX_APP_NUM] = [[0; KERNEL_STACK_SIZE]; MAX_APP_NUM]; // 这一章, 每个用户应用对应一个内核栈 [每个应用自己的内核栈; 总应用数] @@ -8,6 +11,7 @@ static USER_STACK: [[u8; USER_STACK_SIZE]; MAX_APP_NUM] = [[0; USER_STACK_SIZE]; extern "C" { fn _num_app(); + fn _app_names(); } // 得到用户app的数量 @@ -31,4 +35,51 @@ pub fn get_app_data(app_id: usize) -> &'static [u8] { app_start[app_id + 1] - app_start[app_id], ) } +} + +lazy_static!{ + static ref APP_NAMES:Vec<&'static str> = { + // app总数 + let num_app = get_num_app(); + + // 名字开始的位置 + let mut start = _app_names as usize as *const u8; + + // 找到每个用户名 + let mut app_names = Vec::new(); + unsafe { + for _ in 0..num_app { + let mut end = start; + while end.read_volatile() != b'\0'{ + end = end.add(1) + } + + // 从start 到 end长度的切片 + let slice = core::slice::from_raw_parts(start, end as usize - start as usize); + + let str = core::str::from_utf8(slice).unwrap(); + app_names.push(str); + start = end.add(1) + }; + } + app_names + }; +} + + +/// 根据app的名字 找到app的下标, 然后找到app的二进制elf数据 +pub fn get_app_data_by_name(name: &str) -> Option<&'static [u8]> { + let num_app = get_num_app(); + (0..num_app) + .find(|&i| APP_NAMES[i] == name) + .map(get_app_data) +} + +/// 输出所有的app的名字 +pub fn list_apps() { + println!("/**** APPS ****"); + for app in APP_NAMES.iter() { + println!("{}", app); + } + println!("**************/"); } \ No newline at end of file diff --git a/ch5/os/src/main.rs b/ch5/os/src/main.rs index 6f65348..6040ca4 100644 --- a/ch5/os/src/main.rs +++ b/ch5/os/src/main.rs @@ -50,10 +50,13 @@ pub fn rust_main(){ // 初始化动态内存分配器, 使我们能在内核中使用动态大小数据类型 mm::init(); - trap::init(); - trap::enable_timer_interrupt(); // 允许定时器中断 - timer::set_next_trigger(); // 在进入用户态之前, 设置一个时钟中断, 防止第一个用户任务死循环 - task::run_first_task(); + use loader; + loader::list_apps(); + + // trap::init(); + // trap::enable_timer_interrupt(); // 允许定时器中断 + // timer::set_next_trigger(); // 在进入用户态之前, 设置一个时钟中断, 防止第一个用户任务死循环 + // task::run_first_task(); panic!("Disable run here") }