From 6e0d5ef43aca754f1872d52d02e4b5ee7a9103ee Mon Sep 17 00:00:00 2001 From: Ashwin Naren Date: Sun, 15 Feb 2026 22:05:10 -0800 Subject: [PATCH] implement clock_settime --- etc/syscalls_linux_aarch64.md | 2 +- src/arch/arm64/exceptions/syscall.rs | 2 ++ src/clock/mod.rs | 1 + src/clock/settime.rs | 27 +++++++++++++++++++++++++++ 4 files changed, 31 insertions(+), 1 deletion(-) create mode 100644 src/clock/settime.rs diff --git a/etc/syscalls_linux_aarch64.md b/etc/syscalls_linux_aarch64.md index bfd3028..65db90f 100644 --- a/etc/syscalls_linux_aarch64.md +++ b/etc/syscalls_linux_aarch64.md @@ -112,7 +112,7 @@ | 0x6d (109) | timer_getoverrun | (timer_t timer_id) | __arm64_sys_timer_getoverrun | false | | 0x6e (110) | timer_settime | (timer_t timer_id, int flags, const struct __kernel_itimerspec *new_setting, struct __kernel_itimerspec *old_setting) | __arm64_sys_timer_settime | false | | 0x6f (111) | timer_delete | (timer_t timer_id) | __arm64_sys_timer_delete | false | -| 0x70 (112) | clock_settime | (const clockid_t which_clock, const struct __kernel_timespec *tp) | __arm64_sys_clock_settime | false | +| 0x70 (112) | clock_settime | (const clockid_t which_clock, const struct __kernel_timespec *tp) | __arm64_sys_clock_settime | true | | 0x71 (113) | clock_gettime | (const clockid_t which_clock, struct __kernel_timespec *tp) | __arm64_sys_clock_gettime | true | | 0x72 (114) | clock_getres | (const clockid_t which_clock, struct __kernel_timespec *tp) | __arm64_sys_clock_getres | false | | 0x73 (115) | clock_nanosleep | (const clockid_t which_clock, int flags, const struct __kernel_timespec *rqtp, struct __kernel_timespec *rmtp) | __arm64_sys_clock_nanosleep | partially | diff --git a/src/arch/arm64/exceptions/syscall.rs b/src/arch/arm64/exceptions/syscall.rs index ec6e1db..c050f97 100644 --- a/src/arch/arm64/exceptions/syscall.rs +++ b/src/arch/arm64/exceptions/syscall.rs @@ -2,6 +2,7 @@ use crate::{ arch::{Arch, ArchImpl}, clock::{ gettime::sys_clock_gettime, + settime::sys_clock_settime, timeofday::{sys_gettimeofday, sys_settimeofday}, }, fs::{ @@ -419,6 +420,7 @@ pub async fn handle_syscall() { } 0x63 => sys_set_robust_list(TUA::from_value(arg1 as _), arg2 as _).await, 0x65 => sys_nanosleep(TUA::from_value(arg1 as _), TUA::from_value(arg2 as _)).await, + 0x70 => sys_clock_settime(arg1 as _, TUA::from_value(arg2 as _)).await, 0x71 => sys_clock_gettime(arg1 as _, TUA::from_value(arg2 as _)).await, 0x73 => { sys_clock_nanosleep( diff --git a/src/clock/mod.rs b/src/clock/mod.rs index 82ee818..d73959c 100644 --- a/src/clock/mod.rs +++ b/src/clock/mod.rs @@ -1,5 +1,6 @@ pub mod gettime; pub mod realtime; +pub mod settime; pub mod timeofday; pub mod timespec; diff --git a/src/clock/settime.rs b/src/clock/settime.rs new file mode 100644 index 0000000..922f341 --- /dev/null +++ b/src/clock/settime.rs @@ -0,0 +1,27 @@ +use crate::clock::ClockId; +use crate::clock::realtime::set_date; +use crate::clock::timespec::TimeSpec; +use crate::memory::uaccess::copy_from_user; +use libkernel::error::KernelError; +use libkernel::memory::address::TUA; + +pub async fn sys_clock_settime( + clockid: i32, + time_spec: TUA, +) -> libkernel::error::Result { + let time_spec = copy_from_user(time_spec).await?; + if time_spec.tv_sec < 0 || time_spec.tv_nsec >= 1_000_000_000 { + return Err(KernelError::InvalidValue); + } + match ClockId::try_from(clockid).map_err(|_| KernelError::InvalidValue)? { + ClockId::Monotonic | ClockId::MonotonicCoarse | ClockId::MonotonicRaw => { + // Monotonic clock cannot be set + Err(KernelError::InvalidValue) + } + ClockId::Realtime => { + set_date(time_spec.into()); + Ok(0) + } + _ => Err(KernelError::NotSupported), + } +}