time unmodified

This commit is contained in:
Tateisi 2025-08-03 15:42:14 +08:00
parent cc1a9a4751
commit 58fa67414e
7 changed files with 89 additions and 12 deletions

View file

@ -30,7 +30,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]),
SYSCALL_SBRK => sys_sbrk(args[0] as i32),
SYSCALL_MMAP => sys_mmap(args[0], args[1], args[2]),
SYSCALL_MUNMAP => sys_munmap(args[0], args[1]),

View file

@ -3,7 +3,7 @@
use crate::config::PAGE_SIZE;
use crate::mm::{MapPermission, VPNRange, VirtAddr};
use crate::task::{change_program_brk, create_new_map_area, exit_current_and_run_next, get_current_task_page_table_entry, suspend_current_and_run_next, unmap_virtual_page};
use crate::timer::get_time_ms;
use crate::timer::get_time_us;
/// task exits and submit an exit code
pub fn sys_exit(exit_code: i32) -> ! {
@ -18,9 +18,24 @@ pub fn sys_yield() -> isize {
0
}
#[repr(C)]
#[derive(Debug)]
pub struct TimeVal {
pub sec: usize,
pub usec: usize,
}
/// get current time
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
-1
}
/// change data segment size

View file

@ -6,16 +6,23 @@ 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()
}
/// get current time in microseconds
#[allow(unused)]
pub fn get_time_ms() -> usize {
time::read() / (CLOCK_FREQ / MSEC_PER_SEC)
}
/// get current time in us
pub fn get_time_us() -> usize {
time::read() / (CLOCK_FREQ / MICRO_PER_SEC)
}
/// set the next timer interrupt
pub fn set_next_trigger() {
set_timer(get_time() + CLOCK_FREQ / TICKS_PER_SEC);