Skip to content

Commit

Permalink
more apps
Browse files Browse the repository at this point in the history
  • Loading branch information
buhe committed Oct 18, 2021
1 parent bacad92 commit 9f77471
Show file tree
Hide file tree
Showing 7 changed files with 156 additions and 4 deletions.
40 changes: 38 additions & 2 deletions os/src/link_app.S
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
29 changes: 29 additions & 0 deletions user/src/bin/power_3.rs
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions user/src/bin/power_5.rs
Original file line number Diff line number Diff line change
@@ -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
}
29 changes: 29 additions & 0 deletions user/src/bin/power_7.rs
Original file line number Diff line number Diff line change
@@ -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
}
17 changes: 17 additions & 0 deletions user/src/bin/sleep.rs
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 4 additions & 2 deletions user/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
#![feature(asm)]
#![feature(linkage)]

use scall_os::{sys_exit, sys_write};
use scall_os::*;

#[macro_use]
pub mod console;
Expand All @@ -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) }
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() }
10 changes: 10 additions & 0 deletions user/src/scall_os.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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])
}

0 comments on commit 9f77471

Please sign in to comment.