Skip to content

Commit

Permalink
Merge branch 'obhq:main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
SuchAFuriousDeath committed Apr 1, 2024
2 parents 8eb422e + 39175cc commit af66472
Show file tree
Hide file tree
Showing 16 changed files with 246 additions and 66 deletions.
4 changes: 2 additions & 2 deletions src/kernel/src/dev/deci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl DeviceDriver for Driver {
dev: &Arc<CharacterDevice>,
data: &mut UioMut,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
todo!()
}

Expand All @@ -44,7 +44,7 @@ impl DeviceDriver for Driver {
dev: &Arc<CharacterDevice>,
data: &mut Uio,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
todo!()
}
}
Expand Down
55 changes: 52 additions & 3 deletions src/kernel/src/dev/dipsw.rs
Original file line number Diff line number Diff line change
@@ -1,25 +1,74 @@
use thiserror::Error;

use crate::{
errno::Errno,
fs::{CharacterDevice, DeviceDriver, IoCmd},
fs::{
make_dev, CharacterDevice, DeviceDriver, DriverFlags, IoCmd, MakeDevError, MakeDevFlags,
Mode,
},
process::VThread,
ucred::{Gid, Uid},
};
use std::sync::Arc;

#[derive(Debug)]
struct Dipsw {}

impl Dipsw {
fn new() -> Self {
Self {}
}
}

impl DeviceDriver for Dipsw {
#[allow(unused_variables)]
fn ioctl(
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
td: &VThread,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
let td = td.unwrap();

if !td.cred().is_system() {
todo!()
} else {
todo!()
match cmd {
// TODO: properly implement this
IoCmd::DIPSWCHECK2(val) => *val = false as i32,
_ => todo!(),
}
}

Ok(())
}
}

pub struct DipswManager {
dipsw: Arc<CharacterDevice>,
}

impl DipswManager {
pub fn new() -> Result<Arc<Self>, DipswInitError> {
let dipsw = make_dev(
Dipsw::new(),
DriverFlags::from_bits_retain(0x80000004),
0,
"dipsw",
Uid::ROOT,
Gid::ROOT,
Mode::new(0o644).unwrap(),
None,
MakeDevFlags::MAKEDEV_ETERNAL,
)?;

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

/// Represents an error when [`TtyManager`] fails to initialize.
#[derive(Debug, Error)]
pub enum DipswInitError {
#[error("cannot create dipsw device")]
CreateConsoleFailed(#[from] MakeDevError),
}
4 changes: 3 additions & 1 deletion src/kernel/src/dev/dmem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,10 @@ impl DeviceDriver for Dmem {
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
td: &VThread,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
let td = td.unwrap();

let cred = td.cred();

if cred.is_unk1() || cred.is_unk2() {
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/dev/gc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ impl DeviceDriver for Gc {
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
td: &VThread,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
todo!()
}
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/src/dev/hid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl DeviceDriver for Hid {
dev: &Arc<CharacterDevice>,
data: &mut UioMut,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
todo!()
}

Expand All @@ -24,7 +24,7 @@ impl DeviceDriver for Hid {
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
td: &VThread,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
todo!()
}
Expand Down
6 changes: 3 additions & 3 deletions src/kernel/src/dev/random.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl DeviceDriver for Random {
dev: &Arc<CharacterDevice>,
data: &mut UioMut,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
todo!()
}

Expand All @@ -25,15 +25,15 @@ impl DeviceDriver for Random {
dev: &Arc<CharacterDevice>,
data: &mut Uio,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
todo!()
}

fn ioctl(
&self,
_: &Arc<CharacterDevice>,
cmd: IoCmd,
_: &VThread,
_: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
match cmd {
IoCmd::FIOASYNC(_) | IoCmd::FIONBIO(_) => Ok(()),
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/dev/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ impl DeviceDriver for Rng {
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
_: &VThread,
_: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
match cmd {
IoCmd::RNG1 => todo!(),
Expand Down
2 changes: 1 addition & 1 deletion src/kernel/src/dev/sbl_srv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ impl DeviceDriver for SblSrv {
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
td: &VThread,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
todo!()
}
Expand Down
64 changes: 56 additions & 8 deletions src/kernel/src/dev/ttyconsole.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
use crate::errno::EPERM;
use crate::fs::{
make_dev, CharacterDevice, DeviceDriver, DriverFlags, IoCmd, MakeDevError, MakeDevFlags, Mode,
OpenFlags, Uio, UioMut,
};
use crate::ucred::{Gid, Uid};
use crate::{errno::Errno, process::VThread};
use macros::Errno;
use std::sync::Arc;
use thiserror::Error;

#[derive(Debug)]
pub struct TtyConsole {}
pub struct TtyConsole {
tty: Tty,
}

impl TtyConsole {
pub fn new() -> Self {
Self {}
Self { tty: Tty::new() }
}
}

impl DeviceDriver for TtyConsole {
#[allow(unused_variables)] // TODO: remove when implementing
fn open(
&self,
dev: &Arc<CharacterDevice>,
Expand All @@ -33,7 +38,7 @@ impl DeviceDriver for TtyConsole {
dev: &Arc<CharacterDevice>,
data: &mut UioMut,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
todo!()
}

Expand All @@ -43,18 +48,53 @@ impl DeviceDriver for TtyConsole {
dev: &Arc<CharacterDevice>,
data: &mut Uio,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
todo!()
}

#[allow(unused_variables)] // TODO: remove when implementing
/// See `ttydev_ioctl` on the PS4 for a reference.
fn ioctl(
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
td: &VThread,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
todo!()
// TODO: implement tty_wait_background

match cmd {
IoCmd::TIOCSCTTY => self.tty.ioctl(cmd, td)?,
_ => todo!(),
}

Ok(())
}
}

#[derive(Debug)]
struct Tty {}

impl Tty {
fn new() -> Self {
Self {}
}

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

self.generic_ioctl(cmd, td)
}

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

match cmd {
// TODO: implement this properly
IoCmd::TIOCSCTTY => return Ok(()),
_ => todo!(),
}
}
}

Expand All @@ -66,7 +106,7 @@ pub struct TtyManager {
}

impl TtyManager {
pub fn new() -> Result<Arc<Self>, TtyInitError> {
pub fn new() -> Result<Arc<Self>, TtyManagerInitError> {
// Create /dev/console.

let console = make_dev(
Expand All @@ -87,7 +127,15 @@ impl TtyManager {

/// Represents an error when [`TtyManager`] fails to initialize.
#[derive(Debug, Error)]
pub enum TtyInitError {
pub enum TtyManagerInitError {
#[error("cannot create console device")]
CreateConsoleFailed(#[from] MakeDevError),
}

/// Represents an error when [`Tty::ioctl`] fails to initialize.
#[derive(Debug, Error, Errno)]
pub enum TtyIoctlError {
#[error("process is not leader")]
#[errno(EPERM)]
ProcessNotLeader,
}
21 changes: 17 additions & 4 deletions src/kernel/src/fs/dev/cdev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,13 @@ impl FileBackend for CharacterDevice {
cmd: IoCmd,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
todo!()
match cmd {
IoCmd::FIODTYPE(_) => todo!(),
IoCmd::FIODGNAME(_) => todo!(),
_ => self.driver.ioctl(self, cmd, td)?,
}

Ok(())
}

#[allow(unused_variables)] // TODO: remove when implementing
Expand Down Expand Up @@ -179,6 +185,13 @@ bitflags! {
}
}

#[repr(C)]
#[derive(Debug)]
pub struct FioDeviceGetNameArg {
len: i32,
buf: *mut u8,
}

/// An implementation of the `cdevsw` structure.
pub trait DeviceDriver: Debug + Sync + Send + 'static {
#[allow(unused_variables)]
Expand All @@ -198,7 +211,7 @@ pub trait DeviceDriver: Debug + Sync + Send + 'static {
dev: &Arc<CharacterDevice>,
data: &mut UioMut,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
Err(Box::new(DefaultDeviceError::ReadNotSupported))
}

Expand All @@ -208,7 +221,7 @@ pub trait DeviceDriver: Debug + Sync + Send + 'static {
dev: &Arc<CharacterDevice>,
data: &mut Uio,
td: Option<&VThread>,
) -> Result<usize, Box<dyn Errno>> {
) -> Result<(), Box<dyn Errno>> {
Err(Box::new(DefaultDeviceError::WriteNotSupported))
}

Expand All @@ -217,7 +230,7 @@ pub trait DeviceDriver: Debug + Sync + Send + 'static {
&self,
dev: &Arc<CharacterDevice>,
cmd: IoCmd,
td: &VThread,
td: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
Err(Box::new(DefaultDeviceError::IoctlNotSupported))
}
Expand Down
12 changes: 9 additions & 3 deletions src/kernel/src/fs/dev/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,13 @@ impl crate::fs::VnodeBackend for VnodeBackend {
_ => 0,
};

todo!()
Ok(VnodeAttrs {
uid: *uid,
gid: *gid,
mode: *mode,
size,
fsid: u32::MAX,
})
}

fn ioctl(
Expand Down Expand Up @@ -162,9 +168,9 @@ impl crate::fs::VnodeBackend for VnodeBackend {
}
}

fn revoke(&self, vn: &Arc<Vnode>, flags: RevokeFlags) -> Result<(), Box<dyn Errno>> {
fn revoke(&self, vn: &Arc<Vnode>, _flags: RevokeFlags) -> Result<(), Box<dyn Errno>> {
// TODO: Implement this.
todo!()
Ok(())
}

fn read(
Expand Down
Loading

0 comments on commit af66472

Please sign in to comment.