diff --git a/.gitignore b/.gitignore index 30805dd4..c4e93345 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,6 @@ .idea/* os/target/* os/.idea/* +os/src/link_app.S user/target/* -user/.idea/* +user/.idea/* \ No newline at end of file diff --git a/os/build.rs b/os/build.rs new file mode 100644 index 00000000..925ec8af --- /dev/null +++ b/os/build.rs @@ -0,0 +1,34 @@ +use std::io::{Result, Write}; +use std::fs::{File, read_dir}; + +fn main() { + println!("cargo:rerun-if-changed=../user"); + insert_app_data().unwrap(); +} + +static TARGET_PATH: &'static str = "../user/target/riscv64gc-unknown-none-elf/release/"; + +fn insert_app_data() -> Result<()> { + let mut f = File::create("src/link_app.S").unwrap(); + let 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(); + + for (idx, app_with_extension) in apps.iter().enumerate() { + writeln!(f, r#" + .section .data + .global app_{0}_start + .global app_{0}_end +app_{0}_start: + .incbin "{2}{1}" +app_{0}_end: + "#, idx, app_with_extension, TARGET_PATH)?; + } + Ok(()) +} \ No newline at end of file diff --git a/os/src/main.rs b/os/src/main.rs index ebdea882..6ec3f173 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -8,9 +8,11 @@ mod console; mod lang_items; mod sbi; - +mod syscall; +mod trap; global_asm!(include_str!("entry.asm")); +global_asm!(include_str!("link_app.S")); fn clear_bss() { extern "C" { @@ -25,23 +27,11 @@ fn clear_bss() { #[no_mangle] pub fn rust_main() -> ! { extern "C" { - fn stext(); - fn etext(); - fn srodata(); - fn erodata(); - fn sdata(); - fn edata(); - fn sbss(); - fn ebss(); - fn boot_stack(); - fn boot_stack_top(); + fn app_0_start(); + fn app_0_end(); }; clear_bss(); println!("Hello, world!"); - println!(".text [{:#x}, {:#x})", stext as usize, etext as usize); - println!(".rodata [{:#x}, {:#x})", srodata as usize, erodata as usize); - println!(".data [{:#x}, {:#x})", sdata as usize, edata as usize); - println!("boot_stack [{:#x}, {:#x})", boot_stack as usize, boot_stack_top as usize); - println!(".bss [{:#x}, {:#x})", sbss as usize, ebss as usize); + println!("app_0 [{:#x}, {:#x})", app_0_start as usize, app_0_end as usize); panic!("Shutdown machine!"); } \ No newline at end of file diff --git a/os/src/syscall/mod.rs b/os/src/syscall/mod.rs new file mode 100644 index 00000000..5a1a1246 --- /dev/null +++ b/os/src/syscall/mod.rs @@ -0,0 +1,2 @@ +const SYSCALL_WRITE: usize = 64; + diff --git a/os/src/trap.rs b/os/src/trap.rs new file mode 100644 index 00000000..e69de29b diff --git a/user/.cargo/config b/user/.cargo/config index d55502a6..e5ded8a1 100644 --- a/user/.cargo/config +++ b/user/.cargo/config @@ -4,4 +4,4 @@ target = "riscv64gc-unknown-none-elf" [target.riscv64gc-unknown-none-elf] rustflags = [ "-Clink-args=-Tsrc/linker.ld", -] \ No newline at end of file +] diff --git a/user/Makefile b/user/Makefile new file mode 100644 index 00000000..546a2859 --- /dev/null +++ b/user/Makefile @@ -0,0 +1,17 @@ +TARGET := riscv64gc-unknown-none-elf +MODE := release +APP_DIR := src/bin +TARGET_DIR := target/$(TARGET)/$(MODE) +ELFS := $(patsubst $(APP_DIR)/*.rs, $(TARGET_DIR)/*, $(wildcard $(APP_DIR)/*.rs)) +BINS := $(patsubst $(APP_DIR)/*.rs, $(TARGET_DIR)/*.bin, $(wildcard $(APP_DIR)/*.rs)) + +OBJDUMP := rust-objdump --arch-name=riscv64 +OBJCOPY := rust-objcopy --binary-architecture=riscv64 + +elf: + @cargo build --release + +binary: elf + $(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/*, $(TARGET_DIR)/*.bin, $(elf))) + +build: binary