-
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
21 changed files
with
246 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Empty file.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
use std::fs::{read_dir, File}; | ||
use std::io::{Result, Write}; | ||
|
||
fn main() { | ||
println!("cargo:rerun-if-changed=../user/src/"); | ||
println!("cargo:rerun-if-changed={}", TARGET_PATH); | ||
insert_app_data().unwrap(); | ||
} | ||
|
||
static TARGET_PATH: &str = "../user/target/riscv64gc-unknown-none-elf/release/"; | ||
|
||
fn insert_app_data() -> Result<()> { | ||
let mut f = File::create("src/link_app.S").unwrap(); | ||
let mut apps: Vec<_> = read_dir("../user/src/bin") | ||
.unwrap() | ||
.into_iter() | ||
.map(|dir_entry| { | ||
let mut name_with_ext = dir_entry.unwrap().file_name().into_string().unwrap(); | ||
name_with_ext.drain(name_with_ext.find('.').unwrap()..name_with_ext.len()); | ||
name_with_ext | ||
}) | ||
.collect(); | ||
apps.sort(); | ||
|
||
writeln!( | ||
f, | ||
r#" | ||
.align 3 | ||
.section .data | ||
.global _num_app | ||
_num_app: | ||
.quad {}"#, | ||
apps.len() | ||
)?; | ||
|
||
for i in 0..apps.len() { | ||
writeln!(f, r#" .quad app_{}_start"#, i)?; | ||
} | ||
writeln!(f, r#" .quad app_{}_end"#, apps.len() - 1)?; | ||
|
||
for (idx, app) in apps.iter().enumerate() { | ||
println!("app_{}: {}", idx, app); | ||
writeln!( | ||
f, | ||
r#" | ||
.section .data | ||
.global app_{0}_start | ||
.global app_{0}_end | ||
app_{0}_start: | ||
.incbin "{2}{1}.bin" | ||
app_{0}_end:"#, | ||
idx, app, TARGET_PATH | ||
)?; | ||
} | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
use core::panic::PanicInfo; | ||
|
||
use crate::scall_sbi::shutdown; | ||
|
||
#[panic_handler] | ||
fn panic(_: &PanicInfo) -> ! { | ||
loop {} | ||
shutdown(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
|
||
.align 3 | ||
.section .data | ||
.global _num_app | ||
_num_app: | ||
.quad 1 | ||
.quad app_0_start | ||
.quad app_0_end | ||
|
||
.section .data | ||
.global app_0_start | ||
.global app_0_end | ||
app_0_start: | ||
.incbin "../user/target/riscv64gc-unknown-none-elf/release/hello.bin" | ||
app_0_end: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
pub fn sys_exit(exit_code: i32) -> ! { | ||
println!("[kernel] Application exited with code {}", exit_code); | ||
panic!("It should shutdown!"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
use crate::trap::TrapContext; | ||
use core::{cell::RefCell, usize}; | ||
use lazy_static::*; | ||
|
||
const USER_STACK_SIZE: usize = 4096 * 2; | ||
const KERNEL_STACK_SIZE: usize = 4096 * 2; | ||
const APP_BASE_ADDRESS: usize = 0x80400000; | ||
const APP_SIZE_LIMIT: usize = 0x20000; | ||
|
||
#[repr(align(4096))] | ||
struct KernelStack { | ||
data: [u8; KERNEL_STACK_SIZE], | ||
} | ||
|
||
#[repr(align(4096))] | ||
struct UserStack { | ||
data: [u8; USER_STACK_SIZE], | ||
} | ||
|
||
static KERNEL_STACK: KernelStack = KernelStack { | ||
data: [0; KERNEL_STACK_SIZE], | ||
}; | ||
static USER_STACK: UserStack = UserStack { | ||
data: [0; USER_STACK_SIZE], | ||
}; | ||
|
||
impl KernelStack { | ||
fn get_sp(&self) -> usize { | ||
self.data.as_ptr() as usize + KERNEL_STACK_SIZE | ||
} | ||
pub fn push_context(&self, cx: TrapContext) -> &'static mut TrapContext { | ||
let cx_ptr = (self.get_sp() - core::mem::size_of::<TrapContext>()) as *mut TrapContext; | ||
unsafe { | ||
*cx_ptr = cx; | ||
} | ||
unsafe { cx_ptr.as_mut().unwrap() } | ||
} | ||
} | ||
|
||
impl UserStack { | ||
fn get_sp(&self) -> usize { | ||
self.data.as_ptr() as usize + USER_STACK_SIZE | ||
} | ||
} | ||
|
||
struct AppManager { | ||
inner: RefCell<AppManagerInner>, | ||
} | ||
struct AppManagerInner { | ||
app_start: [usize; 2], | ||
} | ||
unsafe impl Sync for AppManager {} | ||
|
||
impl AppManagerInner { | ||
pub fn print_app_info(&self) { | ||
println!( | ||
"[kernel] app_{} [{:#x}, {:#x})", | ||
0, self.app_start[0], self.app_start[1] | ||
); | ||
} | ||
|
||
unsafe fn load_app(&self) { | ||
// clear app area | ||
(APP_BASE_ADDRESS..APP_BASE_ADDRESS + APP_SIZE_LIMIT).for_each(|addr| { | ||
(addr as *mut u8).write_volatile(0); | ||
}); | ||
let app_src = core::slice::from_raw_parts( | ||
self.app_start[0] as *const u8, | ||
self.app_start[1] - self.app_start[0], | ||
); | ||
let app_dst = core::slice::from_raw_parts_mut(APP_BASE_ADDRESS as *mut u8, app_src.len()); | ||
app_dst.copy_from_slice(app_src); | ||
} | ||
} | ||
|
||
lazy_static! { | ||
static ref APP_MANAGER: AppManager = AppManager { | ||
inner: RefCell::new({ | ||
extern "C" { | ||
fn _num_app(); | ||
} | ||
let num_app_ptr = _num_app as usize as *const usize; | ||
let num_app = unsafe { num_app_ptr.read_volatile() }; | ||
let mut app_start: [usize; 2] = [0; 2]; | ||
let app_start_raw: &[usize] = | ||
unsafe { core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) }; | ||
app_start[..=num_app].copy_from_slice(app_start_raw); | ||
AppManagerInner { app_start } | ||
}), | ||
}; | ||
} | ||
|
||
pub fn init() { | ||
print_app_info(); | ||
} | ||
|
||
pub fn print_app_info() { | ||
APP_MANAGER.inner.borrow().print_app_info(); | ||
} | ||
|
||
pub fn run() -> ! { | ||
unsafe { | ||
APP_MANAGER.inner.borrow().load_app(); | ||
} | ||
extern "C" { | ||
fn __restore(cx_addr: usize); | ||
} | ||
unsafe { | ||
__restore(KERNEL_STACK.push_context(TrapContext::app_init_context( | ||
APP_BASE_ADDRESS, | ||
USER_STACK.get_sp(), | ||
)) as *const _ as usize); | ||
} | ||
panic!("Unreachable in task::run!"); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
[build] | ||
target = "riscv64gc-unknown-none-elf" | ||
|
||
|
||
[target.riscv64gc-unknown-none-elf] | ||
rustflags = [ | ||
"-Clink-arg=-Tsrc/linker.ld", "-Cforce-frame-pointers=yes" | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,6 @@ extern crate user; | |
|
||
#[no_mangle] | ||
fn main() -> i32 { | ||
println!("Hello OS"); | ||
println!("Hello OS from app"); | ||
0 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
OUTPUT_ARCH(riscv) | ||
ENTRY(_start) | ||
BASE_ADDRESS = 0x0; | ||
BASE_ADDRESS = 0x80400000; | ||
|
||
SECTIONS | ||
{ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters