Skip to content

Commit 0878add

Browse files
authored
feat(time): 新增 sys_time 系统调用 (#1601)
- 实现 time() 系统调用,返回自纪元以来的秒数 - 支持将时间值写入用户空间指针 tloc - 使用与 gettimeofday 相同的时间源 Signed-off-by: longjin <longjin@DragonOS.org>
1 parent 58676c8 commit 0878add

File tree

2 files changed

+46
-0
lines changed

2 files changed

+46
-0
lines changed

kernel/src/time/syscall/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ mod sys_getitimer;
1313
mod sys_gettimeofday;
1414
mod sys_nanosleep;
1515
mod sys_setitimer;
16+
mod sys_time;
1617
mod sys_timer_create;
1718
mod sys_timer_delete;
1819
mod sys_timer_getoverrun;
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
use system_error::SystemError;
2+
3+
use crate::arch::interrupt::TrapFrame;
4+
use crate::arch::syscall::nr::SYS_TIME;
5+
use crate::syscall::table::{FormattedSyscallParam, Syscall};
6+
use crate::syscall::user_access::UserBufferWriter;
7+
8+
/// sys_time: time(time_t *tloc) -> time_t
9+
///
10+
/// Linux: returns seconds since the Epoch; if tloc != NULL, also stores it.
11+
pub struct SysTimeHandle;
12+
13+
impl Syscall for SysTimeHandle {
14+
fn num_args(&self) -> usize {
15+
1
16+
}
17+
18+
fn handle(&self, args: &[usize], frame: &mut TrapFrame) -> Result<usize, SystemError> {
19+
let tloc = args[0] as *mut i64;
20+
21+
// Use the same time source as gettimeofday.
22+
let now_ns = crate::time::timekeeping::do_gettimeofday().to_ns();
23+
let now_sec = (now_ns / 1_000_000_000) as i64;
24+
25+
if !tloc.is_null() {
26+
let mut w = UserBufferWriter::new(
27+
tloc as *mut u8,
28+
core::mem::size_of::<i64>(),
29+
frame.is_from_user(),
30+
)?;
31+
w.buffer_protected(0)?.write_one::<i64>(0, &now_sec)?;
32+
}
33+
34+
Ok(now_sec as usize)
35+
}
36+
37+
fn entry_format(&self, args: &[usize]) -> alloc::vec::Vec<FormattedSyscallParam> {
38+
alloc::vec![FormattedSyscallParam::new(
39+
"tloc",
40+
format!("{:#x}", args[0]),
41+
)]
42+
}
43+
}
44+
45+
syscall_table_macros::declare_syscall!(SYS_TIME, SysTimeHandle);

0 commit comments

Comments
 (0)