From 0d0c7255b6465e01fed3f55f290fce5bc32aec9d Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Thu, 19 Nov 2020 04:33:06 +0800 Subject: [PATCH] Auto link multiple applications in kernel. --- os/build.rs | 19 ++++++++++++++++++- os/src/main.rs | 17 ++++++++++++----- os/rust-toolchain => rust-toolchain | 0 user/Makefile | 10 +++++++--- user/src/bin/power.rs | 27 +++++++++++++++++++++++++++ 5 files changed, 64 insertions(+), 9 deletions(-) rename os/rust-toolchain => rust-toolchain (100%) create mode 100644 user/src/bin/power.rs diff --git a/os/build.rs b/os/build.rs index 925ec8af..a358a266 100644 --- a/os/build.rs +++ b/os/build.rs @@ -20,13 +20,30 @@ fn insert_app_data() -> Result<()> { }) .collect(); + writeln!(f, r#" + .align 4 + .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_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}" + .incbin "{2}{1}.bin" app_{0}_end: "#, idx, app_with_extension, TARGET_PATH)?; } diff --git a/os/src/main.rs b/os/src/main.rs index 6ec3f173..d571e81f 100644 --- a/os/src/main.rs +++ b/os/src/main.rs @@ -26,12 +26,19 @@ fn clear_bss() { #[no_mangle] pub fn rust_main() -> ! { - extern "C" { - fn app_0_start(); - fn app_0_end(); - }; clear_bss(); println!("Hello, world!"); - println!("app_0 [{:#x}, {:#x})", app_0_start as usize, app_0_end as usize); + 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() }; + println!("num_app = {}", num_app); + let app_start: &[usize] = unsafe { + core::slice::from_raw_parts(num_app_ptr.add(1), num_app + 1) + }; + for i in 0..num_app { + println!("app_{} [{:#x}, {:#x})", i, app_start[i], app_start[i + 1]); + } panic!("Shutdown machine!"); } \ No newline at end of file diff --git a/os/rust-toolchain b/rust-toolchain similarity index 100% rename from os/rust-toolchain rename to rust-toolchain diff --git a/user/Makefile b/user/Makefile index 546a2859..ab508d9a 100644 --- a/user/Makefile +++ b/user/Makefile @@ -2,16 +2,20 @@ 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)) +APPS := $(wildcard $(APP_DIR)/*.rs) +ELFS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%, $(APPS)) +BINS := $(patsubst $(APP_DIR)/%.rs, $(TARGET_DIR)/%.bin, $(APPS)) OBJDUMP := rust-objdump --arch-name=riscv64 OBJCOPY := rust-objcopy --binary-architecture=riscv64 elf: @cargo build --release + @echo $(APPS) + @echo $(ELFS) + @echo $(BINS) binary: elf - $(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/*, $(TARGET_DIR)/*.bin, $(elf))) + $(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));) build: binary diff --git a/user/src/bin/power.rs b/user/src/bin/power.rs new file mode 100644 index 00000000..cc9cb32d --- /dev/null +++ b/user/src/bin/power.rs @@ -0,0 +1,27 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +const SIZE: usize = 10; +const P: u32 = 3; +const STEP: usize = 10000000; +const MOD: u32 = 10007; + +#[no_mangle] +fn main() -> i32 { + let mut pow = [0u32; SIZE]; + let mut index: usize = 0; + pow[index] = 1; + for i in 1..=STEP { + let last = pow[index]; + index = (index + 1) % SIZE; + pow[index] = last * P % MOD; + if i % 10000 == 0 { + println!("{}^{}={}", P, i, pow[index]); + } + } + println!("Test power OK!"); + 0 +} \ No newline at end of file