From e552f3cfcaaecb4f3be99e886ff9accf42f1105e Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Sat, 28 Nov 2020 16:22:29 +0800 Subject: [PATCH] Add sys_yield tests and compile them to different location. --- os/src/syscall/mod.rs | 2 ++ user/Makefile | 8 ++++---- user/build.py | 25 +++++++++++++++++++++++++ user/src/bin/00hello_world.rs | 11 ----------- user/src/bin/00write_a.rs | 18 ++++++++++++++++++ user/src/bin/01store_fault.rs | 14 -------------- user/src/bin/01write_b.rs | 18 ++++++++++++++++++ user/src/bin/02power.rs | 27 --------------------------- user/src/bin/02write_c.rs | 18 ++++++++++++++++++ user/src/lib.rs | 4 +++- user/src/linker.ld | 1 + user/src/syscall.rs | 10 ++++++++++ 12 files changed, 99 insertions(+), 57 deletions(-) create mode 100644 user/build.py delete mode 100644 user/src/bin/00hello_world.rs create mode 100644 user/src/bin/00write_a.rs delete mode 100644 user/src/bin/01store_fault.rs create mode 100644 user/src/bin/01write_b.rs delete mode 100644 user/src/bin/02power.rs create mode 100644 user/src/bin/02write_c.rs diff --git a/os/src/syscall/mod.rs b/os/src/syscall/mod.rs index 51dea1e7..0a72b193 100644 --- a/os/src/syscall/mod.rs +++ b/os/src/syscall/mod.rs @@ -1,5 +1,7 @@ const SYSCALL_WRITE: usize = 64; const SYSCALL_EXIT: usize = 93; +const SYSCALL_YIELD: usize = 124; +const SYSCALL_GET_TIME: usize = 169; mod fs; mod process; diff --git a/user/Makefile b/user/Makefile index ab508d9a..5cce8cbf 100644 --- a/user/Makefile +++ b/user/Makefile @@ -10,12 +10,12 @@ OBJDUMP := rust-objdump --arch-name=riscv64 OBJCOPY := rust-objcopy --binary-architecture=riscv64 elf: - @cargo build --release - @echo $(APPS) - @echo $(ELFS) - @echo $(BINS) + @python3 build.py binary: elf $(foreach elf, $(ELFS), $(OBJCOPY) $(elf) --strip-all -O binary $(patsubst $(TARGET_DIR)/%, $(TARGET_DIR)/%.bin, $(elf));) build: binary + +clean: + @cargo clean \ No newline at end of file diff --git a/user/build.py b/user/build.py new file mode 100644 index 00000000..1d1d8953 --- /dev/null +++ b/user/build.py @@ -0,0 +1,25 @@ +import os + +base_address = 0x80040000 +step = 0x20000 +linker = 'src/linker.ld' + +app_id = 0 +apps = os.listdir('src/bin') +apps.sort() +for app in apps: + app = app[:app.find('.')] + lines = [] + lines_before = [] + with open(linker, 'r') as f: + for line in f.readlines(): + lines_before.append(line) + line = line.replace(hex(base_address), hex(base_address+step*app_id)) + lines.append(line) + with open(linker, 'w+') as f: + f.writelines(lines) + os.system('cargo build --bin %s --release' % app) + print('[build.py] application %s start with address %s' %(app, hex(base_address+step*app_id))) + with open(linker, 'w+') as f: + f.writelines(lines_before) + app_id = app_id + 1 diff --git a/user/src/bin/00hello_world.rs b/user/src/bin/00hello_world.rs deleted file mode 100644 index 684396a2..00000000 --- a/user/src/bin/00hello_world.rs +++ /dev/null @@ -1,11 +0,0 @@ -#![no_std] -#![no_main] - -#[macro_use] -extern crate user_lib; - -#[no_mangle] -fn main() -> i32 { - println!("Hello, world!"); - 0 -} \ No newline at end of file diff --git a/user/src/bin/00write_a.rs b/user/src/bin/00write_a.rs new file mode 100644 index 00000000..a7ae889b --- /dev/null +++ b/user/src/bin/00write_a.rs @@ -0,0 +1,18 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use user_lib::sys_yield; + +#[no_mangle] +fn main() -> i32 { + for _ in 0..3 { + for _ in 0..10 { print!("A"); } + println!(""); + sys_yield(); + } + println!("Test write_a OK!"); + 0 +} \ No newline at end of file diff --git a/user/src/bin/01store_fault.rs b/user/src/bin/01store_fault.rs deleted file mode 100644 index 790196b2..00000000 --- a/user/src/bin/01store_fault.rs +++ /dev/null @@ -1,14 +0,0 @@ -#![no_std] -#![no_main] -#![feature(llvm_asm)] - -#[macro_use] -extern crate user_lib; - -#[no_mangle] -fn main() -> i32 { - println!("Into Test store_fault, we will insert an invalid store operation..."); - println!("Kernel should kill this application!"); - unsafe { (0x0 as *mut u8).write_volatile(0); } - 0 -} \ No newline at end of file diff --git a/user/src/bin/01write_b.rs b/user/src/bin/01write_b.rs new file mode 100644 index 00000000..713bfa58 --- /dev/null +++ b/user/src/bin/01write_b.rs @@ -0,0 +1,18 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use user_lib::sys_yield; + +#[no_mangle] +fn main() -> i32 { + for _ in 0..3 { + for _ in 0..10 { print!("B"); } + println!(""); + sys_yield(); + } + println!("Test write_b OK!"); + 0 +} \ No newline at end of file diff --git a/user/src/bin/02power.rs b/user/src/bin/02power.rs deleted file mode 100644 index 86154c71..00000000 --- a/user/src/bin/02power.rs +++ /dev/null @@ -1,27 +0,0 @@ -#![no_std] -#![no_main] - -#[macro_use] -extern crate user_lib; - -const SIZE: usize = 10; -const P: u32 = 3; -const STEP: usize = 100000; -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 diff --git a/user/src/bin/02write_c.rs b/user/src/bin/02write_c.rs new file mode 100644 index 00000000..96edfef4 --- /dev/null +++ b/user/src/bin/02write_c.rs @@ -0,0 +1,18 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user_lib; + +use user_lib::sys_yield; + +#[no_mangle] +fn main() -> i32 { + for _ in 0..3 { + for _ in 0..10 { print!("C"); } + println!(""); + sys_yield(); + } + println!("Test write_c OK!"); + 0 +} \ No newline at end of file diff --git a/user/src/lib.rs b/user/src/lib.rs index ed7ba84c..fdcb6d56 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -30,4 +30,6 @@ fn clear_bss() { (start_bss as usize..end_bss as usize).for_each(|addr| { unsafe { (addr as *mut u8).write_volatile(0); } }); -} \ No newline at end of file +} + +pub use syscall::*; \ No newline at end of file diff --git a/user/src/linker.ld b/user/src/linker.ld index cbf7486e..ed9f3017 100644 --- a/user/src/linker.ld +++ b/user/src/linker.ld @@ -1,3 +1,4 @@ + OUTPUT_ARCH(riscv) ENTRY(_start) diff --git a/user/src/syscall.rs b/user/src/syscall.rs index edb26d77..8e4ff50b 100644 --- a/user/src/syscall.rs +++ b/user/src/syscall.rs @@ -2,6 +2,8 @@ pub const STDOUT: usize = 1; const SYSCALL_WRITE: usize = 64; const SYSCALL_EXIT: usize = 93; +const SYSCALL_YIELD: usize = 124; +const SYSCALL_GET_TIME: usize = 169; fn syscall(id: usize, args: [usize; 3]) -> isize { let mut ret: isize; @@ -23,3 +25,11 @@ pub fn sys_write(fd: usize, buffer: &[u8]) -> isize { pub fn sys_exit(xstate: i32) -> isize { syscall(SYSCALL_EXIT, [xstate as usize, 0, 0]) } + +pub fn sys_yield() -> isize { + syscall(SYSCALL_YIELD, [0, 0, 0]) +} + +pub fn sys_get_time() -> isize { + syscall(SYSCALL_GET_TIME, [0, 0, 0]) +} \ No newline at end of file