diff --git a/os/src/link_app.S b/os/src/link_app.S index f80d18c..530389b 100644 --- a/os/src/link_app.S +++ b/os/src/link_app.S @@ -3,9 +3,13 @@ .section .data .global _num_app _num_app: - .quad 1 + .quad 5 .quad app_0_start - .quad app_0_end + .quad app_1_start + .quad app_2_start + .quad app_3_start + .quad app_4_start + .quad app_4_end .section .data .global app_0_start @@ -14,3 +18,35 @@ _num_app: app_0_start: .incbin "../user/target/riscv64gc-unknown-none-elf/release/hello" app_0_end: + + .section .data + .global app_1_start + .global app_1_end + .align 3 +app_1_start: + .incbin "../user/target/riscv64gc-unknown-none-elf/release/power_3" +app_1_end: + + .section .data + .global app_2_start + .global app_2_end + .align 3 +app_2_start: + .incbin "../user/target/riscv64gc-unknown-none-elf/release/power_5" +app_2_end: + + .section .data + .global app_3_start + .global app_3_end + .align 3 +app_3_start: + .incbin "../user/target/riscv64gc-unknown-none-elf/release/power_7" +app_3_end: + + .section .data + .global app_4_start + .global app_4_end + .align 3 +app_4_start: + .incbin "../user/target/riscv64gc-unknown-none-elf/release/sleep" +app_4_end: diff --git a/user/src/bin/power_3.rs b/user/src/bin/power_3.rs new file mode 100644 index 0000000..bcf9670 --- /dev/null +++ b/user/src/bin/power_3.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user; + +const LEN: usize = 100; + +static mut S: [u64; LEN] = [0u64; LEN]; + +#[no_mangle] +unsafe fn main() -> i32 { + let p = 3u64; + let m = 998244353u64; + let iter: usize = 300000; + let mut cur = 0usize; + S[cur] = 1; + for i in 1..=iter { + let next = if cur + 1 == LEN { 0 } else { cur + 1 }; + S[next] = S[cur] * p % m; + cur = next; + if i % 10000 == 0 { + println!("power_3 [{}/{}]", i, iter); + } + } + println!("{}^{} = {}(mod {})", p, iter, S[cur], m); + println!("Test power_3 OK!"); + 0 +} \ No newline at end of file diff --git a/user/src/bin/power_5.rs b/user/src/bin/power_5.rs new file mode 100644 index 0000000..aba4029 --- /dev/null +++ b/user/src/bin/power_5.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user; + +const LEN: usize = 100; + +static mut S: [u64; LEN] = [0u64; LEN]; + +#[no_mangle] +unsafe fn main() -> i32 { + let p = 5u64; + let m = 998244353u64; + let iter: usize = 210000; + let mut cur = 0usize; + S[cur] = 1; + for i in 1..=iter { + let next = if cur + 1 == LEN { 0 } else { cur + 1 }; + S[next] = S[cur] * p % m; + cur = next; + if i % 10000 == 0 { + println!("power_5 [{}/{}]", i, iter); + } + } + println!("{}^{} = {}(mod {})", p, iter, S[cur], m); + println!("Test power_5 OK!"); + 0 +} diff --git a/user/src/bin/power_7.rs b/user/src/bin/power_7.rs new file mode 100644 index 0000000..3274daf --- /dev/null +++ b/user/src/bin/power_7.rs @@ -0,0 +1,29 @@ +#![no_std] +#![no_main] + +#[macro_use] +extern crate user; + +const LEN: usize = 100; + +static mut S: [u64; LEN] = [0u64; LEN]; + +#[no_mangle] +unsafe fn main() -> i32 { + let p = 7u64; + let m = 998244353u64; + let iter: usize = 240000; + let mut cur = 0usize; + S[cur] = 1; + for i in 1..=iter { + let next = if cur + 1 == LEN { 0 } else { cur + 1 }; + S[next] = S[cur] * p % m; + cur = next; + if i % 10000 == 0 { + println!("power_7 [{}/{}]", i, iter); + } + } + println!("{}^{} = {}(mod {})", p, iter, S[cur], m); + println!("Test power_7 OK!"); + 0 +} diff --git a/user/src/bin/sleep.rs b/user/src/bin/sleep.rs new file mode 100644 index 0000000..9fccd24 --- /dev/null +++ b/user/src/bin/sleep.rs @@ -0,0 +1,17 @@ +#![no_std] +#![no_main] +#[macro_use] +extern crate user; + +use user::{get_time, yield_}; + +#[no_mangle] +fn main() -> i32 { + let current_timer = get_time(); + let wait_for = current_timer + 3000; + while get_time() < wait_for { + yield_(); + } + println!("Test sleep OK!"); + 0 +} \ No newline at end of file diff --git a/user/src/lib.rs b/user/src/lib.rs index 29c7c38..9102c0b 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -2,7 +2,7 @@ #![feature(asm)] #![feature(linkage)] -use scall_os::{sys_exit, sys_write}; +use scall_os::*; #[macro_use] pub mod console; @@ -23,4 +23,6 @@ fn main() -> i32 { } pub fn write(fd: usize, buf: &[u8]) -> isize { sys_write(fd, buf) } -pub fn exit(exit_code: i32) -> isize { sys_exit(exit_code) } \ No newline at end of file +pub fn exit(exit_code: i32) -> isize { sys_exit(exit_code) } +pub fn yield_() -> isize { sys_yield() } +pub fn get_time() -> isize { sys_get_time() } \ No newline at end of file diff --git a/user/src/scall_os.rs b/user/src/scall_os.rs index 02615f5..ee05a6d 100644 --- a/user/src/scall_os.rs +++ b/user/src/scall_os.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; fn syscall(which: usize, args: [usize; 3]) -> isize { let mut ret; @@ -23,3 +25,11 @@ pub fn sys_write(fd: usize, buffer: &[u8]) -> isize { pub fn sys_exit(exit_code: i32) -> isize { syscall(SYSCALL_EXIT, [exit_code 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]) +}