diff --git a/os/src/syscall/mod.rs b/os/src/syscall/mod.rs index da411680..8ca22fe3 100644 --- a/os/src/syscall/mod.rs +++ b/os/src/syscall/mod.rs @@ -14,7 +14,7 @@ pub fn syscall(syscall_id: usize, args: [usize; 3]) -> isize { SYSCALL_WRITE => sys_write(args[0], args[1] as *const u8, args[2]), SYSCALL_EXIT => sys_exit(args[0] as i32), SYSCALL_YIELD => sys_yield(), - SYSCALL_GET_TIME => sys_get_time(), + SYSCALL_GET_TIME => sys_get_time(args[0] as *mut TimeVal, args[1]), _ => panic!("Unsupported syscall_id: {}", syscall_id), } } diff --git a/os/src/syscall/process.rs b/os/src/syscall/process.rs index 23eab1ff..d0168494 100644 --- a/os/src/syscall/process.rs +++ b/os/src/syscall/process.rs @@ -2,7 +2,14 @@ use crate::task::{ suspend_current_and_run_next, exit_current_and_run_next, }; -use crate::timer::get_time_ms; +use crate::timer::get_time_us; + +#[repr(C)] +#[derive(Debug)] +pub struct TimeVal { + pub sec: usize, + pub usec: usize, +} pub fn sys_exit(exit_code: i32) -> ! { println!("[kernel] Application exited with code {}", exit_code); @@ -15,6 +22,17 @@ pub fn sys_yield() -> isize { 0 } -pub fn sys_get_time() -> isize { - get_time_ms() as isize +// pub fn sys_get_time() -> isize { +// get_time_ms() as isize +// } + +pub fn sys_get_time(ts: *mut TimeVal, _tz: usize) -> isize { + let us = get_time_us(); + unsafe { + *ts = TimeVal { + sec: us / 1_000_000, + usec: us % 1_000_000, + }; + } + 0 } \ No newline at end of file diff --git a/os/src/timer.rs b/os/src/timer.rs index ad226e64..8b908bcf 100644 --- a/os/src/timer.rs +++ b/os/src/timer.rs @@ -1,16 +1,16 @@ -use riscv::register::time; -use crate::sbi::set_timer; use crate::config::CLOCK_FREQ; +use crate::sbi::set_timer; +use riscv::register::time; const TICKS_PER_SEC: usize = 100; -const MSEC_PER_SEC: usize = 1000; +const MICRO_PER_SEC: usize = 1_000_000; pub fn get_time() -> usize { time::read() } -pub fn get_time_ms() -> usize { - time::read() / (CLOCK_FREQ / MSEC_PER_SEC) +pub fn get_time_us() -> usize { + time::read() / (CLOCK_FREQ / MICRO_PER_SEC) } pub fn set_next_trigger() {