From ff2c39e40b677c56e225d2224680c6c69f9ded8d Mon Sep 17 00:00:00 2001 From: Yifan Wu Date: Wed, 6 Jan 2021 00:19:10 +0800 Subject: [PATCH] Update from previous chapters. --- os/src/config.rs | 4 ++-- os/src/syscall/process.rs | 8 ++++---- os/src/timer.rs | 9 +++++++-- user/src/bin/03sleep.rs | 10 +++++----- user/src/console.rs | 6 ++++-- user/src/lib.rs | 9 +++++++-- user/src/syscall.rs | 6 ++---- 7 files changed, 31 insertions(+), 21 deletions(-) diff --git a/os/src/config.rs b/os/src/config.rs index aac95791..113a3509 100644 --- a/os/src/config.rs +++ b/os/src/config.rs @@ -15,7 +15,7 @@ pub fn kernel_stack_position(app_id: usize) -> (usize, usize) { } #[cfg(feature = "board_k210")] -pub const CPU_FREQ: usize = 10000000; +pub const CLOCK_FREQ: usize = 10000000; #[cfg(feature = "board_qemu")] -pub const CPU_FREQ: usize = 12500000; \ No newline at end of file +pub const CLOCK_FREQ: usize = 12500000; \ No newline at end of file diff --git a/os/src/syscall/process.rs b/os/src/syscall/process.rs index e0bdebae..23eab1ff 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -2,10 +2,10 @@ use crate::task::{ suspend_current_and_run_next, exit_current_and_run_next, }; -use crate::timer::get_time; +use crate::timer::get_time_ms; -pub fn sys_exit(xstate: i32) -> ! { - println!("[kernel] Application exited with code {}", xstate); +pub fn sys_exit(exit_code: i32) -> ! { + println!("[kernel] Application exited with code {}", exit_code); exit_current_and_run_next(); panic!("Unreachable in sys_exit!"); } @@ -16,5 +16,5 @@ pub fn sys_yield() -> isize { } pub fn sys_get_time() -> isize { - get_time() as isize + get_time_ms() as isize } \ No newline at end of file diff --git a/os/src/timer.rs b/os/src/timer.rs index 7522e708..92d50e3a 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -1,13 +1,18 @@ use riscv::register::time; use crate::sbi::set_timer; -use crate::config::CPU_FREQ; +use crate::config::CLOCK_FREQ; const TICKS_PER_SEC: usize = 100; +const MSEC_PER_SEC: usize = 1000; pub fn get_time() -> usize { time::read() } +pub fn get_time_ms() -> usize { + time::read() / (CLOCK_FREQ / MSEC_PER_SEC) +} + pub fn set_next_trigger() { - set_timer(get_time() + CPU_FREQ / TICKS_PER_SEC); + set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC); } \ No newline at end of file diff --git a/user/src/bin/03sleep.rs b/user/src/bin/03sleep.rs index f3d9f31b..83411233 100644 --- a/user/src/bin/03sleep.rs +++ b/user/src/bin/03sleep.rs @@ -4,14 +4,14 @@ #[macro_use] extern crate user_lib; -use user_lib::{sys_get_time, sys_yield}; +use user_lib::{get_time, yield_}; #[no_mangle] fn main() -> i32 { - let current_timer = sys_get_time(); - let wait_for = current_timer + 10000000; - while sys_get_time() < wait_for { - sys_yield(); + let current_timer = get_time(); + let wait_for = current_timer + 3000; + while get_time() < wait_for { + yield_(); } println!("Test sleep OK!"); 0 diff --git a/user/src/console.rs b/user/src/console.rs index a826b8fe..ac801174 100644 --- a/user/src/console.rs +++ b/user/src/console.rs @@ -1,11 +1,13 @@ use core::fmt::{self, Write}; -use crate::syscall::{STDOUT, sys_write}; +use super::write; struct Stdout; +const STDOUT: usize = 1; + impl Write for Stdout { fn write_str(&mut self, s: &str) -> fmt::Result { - sys_write(STDOUT, s.as_bytes()); + write(STDOUT, s.as_bytes()); Ok(()) } } diff --git a/user/src/lib.rs b/user/src/lib.rs index c072ef49..122aefcf 100644 --- a/user/src/lib.rs +++ b/user/src/lib.rs @@ -11,7 +11,7 @@ mod lang_items; #[no_mangle] #[link_section = ".text.entry"] pub extern "C" fn _start() -> ! { - syscall::sys_exit(main()); + exit(main()); panic!("unreachable after sys_exit!"); } @@ -22,4 +22,9 @@ fn main() -> i32 { } -pub use syscall::*; \ No newline at end of file +use syscall::*; + +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 yield_() -> isize { sys_yield() } +pub fn get_time() -> isize { sys_get_time() } \ No newline at end of file diff --git a/user/src/syscall.rs b/user/src/syscall.rs index 8e4ff50b..1b23a48a 100644 --- a/user/src/syscall.rs +++ b/user/src/syscall.rs @@ -1,5 +1,3 @@ -pub const STDOUT: usize = 1; - const SYSCALL_WRITE: usize = 64; const SYSCALL_EXIT: usize = 93; const SYSCALL_YIELD: usize = 124; @@ -22,8 +20,8 @@ pub fn sys_write(fd: usize, buffer: &[u8]) -> isize { syscall(SYSCALL_WRITE, [fd, buffer.as_ptr() as usize, buffer.len()]) } -pub fn sys_exit(xstate: i32) -> isize { - syscall(SYSCALL_EXIT, [xstate as usize, 0, 0]) +pub fn sys_exit(exit_code: i32) -> isize { + syscall(SYSCALL_EXIT, [exit_code as usize, 0, 0]) } pub fn sys_yield() -> isize {