Skip to content

Commit

Permalink
Implements sys_clock_gettime with clock_id = 4 (#822)
Browse files Browse the repository at this point in the history
  • Loading branch information
SuchAFuriousDeath authored Apr 11, 2024
1 parent 81fa97a commit 85f0f1a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 14 deletions.
4 changes: 2 additions & 2 deletions src/kernel/src/dev/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ pub struct RngInput {
}

pub struct RngManager {
dipsw: Arc<CharacterDevice>,
rng: Arc<CharacterDevice>,
}

impl RngManager {
Expand All @@ -68,7 +68,7 @@ impl RngManager {
MakeDevFlags::MAKEDEV_ETERNAL,
)?;

Ok(Arc::new(Self { dipsw: rng }))
Ok(Arc::new(Self { rng }))
}
}

Expand Down
10 changes: 2 additions & 8 deletions src/kernel/src/dev/ttyconsole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,8 @@ impl Tty {
self.generic_ioctl(cmd, td)
}

/// See `tty_ioctl` on the PS4 for a reference.
/// See `tty_generic_ioctl` on the PS4 for a reference.
fn generic_ioctl(&self, cmd: IoCmd, _td: Option<&VThread>) -> Result<(), TtyIoctlError> {
// TODO: implement ttydevsw_ioctl

match cmd {
IoCmd::TIOCSCTTY => todo!(),
_ => todo!(),
Expand Down Expand Up @@ -133,8 +131,4 @@ pub enum TtyManagerInitError {

/// Represents an error when [`Tty::ioctl`] fails to initialize.
#[derive(Debug, Error, Errno)]
pub enum TtyIoctlError {
#[error("process is not leader")]
#[errno(EPERM)]
ProcessNotLeader,
}
pub enum TtyIoctlError {}
1 change: 1 addition & 0 deletions src/kernel/src/fs/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ commands! {
/// Seek hole.
FIOSEEKHOLE(&mut i64) = 0xC0086662,

/// Currently unknown gc command
GC12(&mut Unknown16) = 0xc010810b,
/// Currently unknown gc command
GC16(&mut Unknown12) = 0xc00c8110,
Expand Down
3 changes: 2 additions & 1 deletion src/kernel/src/net/socket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use thiserror::Error;
pub struct Socket {
cred: Arc<Ucred>, // so_cred
name: Option<Box<str>>,
backend: Protocol,
backend: Protocol, // so_proto + so_type
}

impl Socket {
Expand All @@ -31,6 +31,7 @@ impl Socket {
td: &VThread,
name: Option<&str>,
) -> Result<Arc<Self>, SocketCreateError> {
// TODO: implement prison_check_af
let backend = match domain {
2 => {
let protocol = match (ty, proto) {
Expand Down
82 changes: 79 additions & 3 deletions src/kernel/src/time/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::errno::Errno;
use crate::info;
use crate::process::VThread;
use crate::syscalls::{SysErr, SysIn, SysOut, Syscalls};
use std::num::NonZeroI32;
Expand Down Expand Up @@ -35,15 +36,66 @@ impl TimeManager {
}

pub fn sys_clock_gettime(self: &Arc<Self>, _: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
let clock: i32 = i.args[0].try_into().unwrap();
let clock: Clock = {
let clock: i32 = i.args[0].try_into().unwrap();

todo!("clock_gettime with clock = {clock}")
clock.try_into()?
};

let timespec: *mut TimeSpec = i.args[1].into();

info!("Getting clock time with clock_id = {clock:?}");

unsafe {
*timespec = match clock {
Clock::Monotonic => Self::nanouptime()?,
}
}

Ok(SysOut::ZERO)
}

#[cfg(unix)]
pub fn nanouptime() -> Result<TimeSpec, NanoUpTimeError> {
use libc::clock_gettime;
use std::mem::MaybeUninit;

let mut ts = MaybeUninit::uninit();

let res = unsafe { clock_gettime(libc::CLOCK_MONOTONIC, ts.as_mut_ptr()) };

if res < 0 {
return Err(std::io::Error::last_os_error().into());
}

Ok(unsafe { ts.assume_init() }.into())
}

#[cfg(windows)]
pub fn nanouptime() -> Result<TimeSpec, NanoUpTimeError> {
todo!()
}
}

#[derive(Debug)]
enum Clock {
Monotonic = 4,
}

impl TryFrom<i32> for Clock {
type Error = SysErr;

fn try_from(value: i32) -> Result<Self, Self::Error> {
match value {
4 => Ok(Self::Monotonic),
_ => todo!(),
}
}
}

/// An implementation of the `timespec` structure.
#[derive(Debug, Clone, Copy)]
#[repr(C)]
#[derive(Debug, Clone, Copy)]
pub struct TimeSpec {
sec: i64,
nsec: i64,
Expand All @@ -55,6 +107,16 @@ impl TimeSpec {
}
}

#[cfg(unix)]
impl From<libc::timespec> for TimeSpec {
fn from(ts: libc::timespec) -> Self {
Self {
sec: ts.tv_sec,
nsec: ts.tv_nsec,
}
}
}

impl From<TimeVal> for TimeSpec {
fn from(tv: TimeVal) -> Self {
Self {
Expand Down Expand Up @@ -125,6 +187,20 @@ struct TimeZone {
dsttime: i32, // tz_dsttime
}

#[derive(Debug, Error)]
pub enum NanoUpTimeError {
#[error("Failed to get time")]
IoError(#[from] std::io::Error),
}

impl Errno for NanoUpTimeError {
fn errno(&self) -> NonZeroI32 {
match self {
Self::IoError(_) => todo!(),
}
}
}

#[derive(Debug, Error)]
pub enum MicroTimeError {
#[error("Failed to get time")]
Expand Down

0 comments on commit 85f0f1a

Please sign in to comment.