Files
moss-kernel/src/clock/gettime.rs
Matthew Leach aaac9ffd2c sched: eliminate CUR_TASK_PTR
Replace `CUR_TASK_PTR` with `ProcessCtx`. This allows differentiation
between functions that access process context (take in `ProcessCtx` as a
parameter) and those that don't.

When creating a new class of scheduleable tasks (softirqs, kthreads),
this ensure that those functions cannot call context-sensitive
functions.
2026-03-19 20:58:15 +00:00

49 lines
1.8 KiB
Rust

use core::sync::atomic::Ordering;
use core::time::Duration;
use libkernel::{
error::{KernelError, Result},
memory::address::TUA,
};
use super::{ClockId, realtime::date, timespec::TimeSpec};
use crate::drivers::timer::{Instant, now};
use crate::sched::syscall_ctx::ProcessCtx;
use crate::{drivers::timer::uptime, memory::uaccess::copy_to_user};
pub async fn sys_clock_gettime(
ctx: &ProcessCtx,
clockid: i32,
time_spec: TUA<TimeSpec>,
) -> Result<usize> {
let time = match ClockId::try_from(clockid).map_err(|_| KernelError::InvalidValue)? {
ClockId::Realtime => date(),
ClockId::Monotonic => uptime(),
ClockId::ProcessCpuTimeId => {
let task = ctx.shared();
let total_time = task.process.stime.load(Ordering::Relaxed) as u64
+ task.process.utime.load(Ordering::Relaxed) as u64;
let last_update = Instant::from_user_normalized(
task.process.last_account.load(Ordering::Relaxed) as u64,
);
let now = now().unwrap();
let delta = now - last_update;
Duration::from(Instant::from_user_normalized(total_time)) + delta
}
ClockId::ThreadCpuTimeId => {
let task = ctx.shared();
let total_time = task.stime.load(Ordering::Relaxed) as u64
+ task.utime.load(Ordering::Relaxed) as u64;
let last_update =
Instant::from_user_normalized(task.last_account.load(Ordering::Relaxed) as u64);
let now = now().unwrap();
let delta = now - last_update;
Duration::from(Instant::from_user_normalized(total_time)) + delta
}
_ => return Err(KernelError::InvalidValue),
};
copy_to_user(time_spec, time.into()).await?;
Ok(0)
}