Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implements sys_netcontrol, refactors Filedesc::alloc #811

Merged
merged 36 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 31 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
9bc3dc3
Fixes mkdir on Windows
SuchAFuriousDeath Mar 27, 2024
8eb422e
Revert "Fixes mkdir on Windows"
SuchAFuriousDeath Mar 27, 2024
af66472
Merge branch 'obhq:main' into main
SuchAFuriousDeath Apr 1, 2024
9ed2cfe
Merge branch 'main' of https://github.com/SuchAFuriousDeath/obliteration
SuchAFuriousDeath Apr 1, 2024
9c29467
Merge branch 'obhq:main' into main
SuchAFuriousDeath Apr 2, 2024
a6f1563
Merge branch 'obhq:main' into main
SuchAFuriousDeath Apr 5, 2024
8070a70
Merge branch 'obhq:main' into main
SuchAFuriousDeath Apr 6, 2024
3765000
Merge branch 'obhq:main' into main
SuchAFuriousDeath Apr 6, 2024
b13348c
Merge branch 'obhq:main' into main
SuchAFuriousDeath Apr 6, 2024
9eb257d
Merge branch 'obhq:main' into main
SuchAFuriousDeath Apr 6, 2024
d405d70
Implements sys_netcontrol
SuchAFuriousDeath Apr 6, 2024
f6e5916
add comment
SuchAFuriousDeath Apr 6, 2024
31e0dd6
remove camera module
SuchAFuriousDeath Apr 6, 2024
c442e45
change mode
SuchAFuriousDeath Apr 6, 2024
89eebbc
put todo back
SuchAFuriousDeath Apr 7, 2024
cfd645b
refactors open
SuchAFuriousDeath Apr 7, 2024
9353bdc
add logging
SuchAFuriousDeath Apr 7, 2024
14d49b4
put back todo
SuchAFuriousDeath Apr 7, 2024
d36d78e
log proc type info
SuchAFuriousDeath Apr 7, 2024
66477ce
log dmem container
SuchAFuriousDeath Apr 7, 2024
30cf25e
roll back
SuchAFuriousDeath Apr 7, 2024
c77b231
move and rename bnet command
SuchAFuriousDeath Apr 7, 2024
a0b75a8
Merge branch 'main' into netcontrol
SuchAFuriousDeath Apr 7, 2024
cd84010
log proc type info
SuchAFuriousDeath Apr 7, 2024
9a3cd67
rng iocmds
SuchAFuriousDeath Apr 7, 2024
5e172d5
Merge branch 'main' into netcontrol
SuchAFuriousDeath Apr 7, 2024
af5fe0a
fix rng
SuchAFuriousDeath Apr 7, 2024
bb143b6
rename rng
SuchAFuriousDeath Apr 7, 2024
4d8255c
put back todos
SuchAFuriousDeath Apr 7, 2024
275d0f1
Merge branch 'main' into netcontrol
SuchAFuriousDeath Apr 7, 2024
aac88bb
address review
SuchAFuriousDeath Apr 7, 2024
e132556
Merge branch 'main' into netcontrol
SuchAFuriousDeath Apr 7, 2024
19fe85f
Merge branch 'main' into netcontrol
SuchAFuriousDeath Apr 8, 2024
c8777c5
fix build
SuchAFuriousDeath Apr 8, 2024
aef68a1
ref
SuchAFuriousDeath Apr 8, 2024
9b52af6
fix logical error
SuchAFuriousDeath Apr 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/kernel/src/budget/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,18 @@ pub enum BudgetType {
FdIpcSocket = 11,
}

#[allow(dead_code)]
#[repr(C)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ProcType {
BigApp,
MiniApp,
System, // TODO: Verify this.
BigApp = 0,
#[allow(unused)]
MiniApp = 1,
#[allow(unused)]
System = 2, // TODO: Verify this.
}

impl Into<u32> for ProcType {
fn into(self) -> u32 {
match self {
ProcType::BigApp => 0,
ProcType::MiniApp => 1,
ProcType::System => 2,
}
self as u32
}
}
4 changes: 2 additions & 2 deletions src/kernel/src/dev/dipsw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,13 @@ impl DeviceDriver for Dipsw {
let td = td.unwrap();

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

Ok(())
Expand Down
4 changes: 2 additions & 2 deletions src/kernel/src/dev/rng.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ impl DeviceDriver for Rng {
_: Option<&VThread>,
) -> Result<(), Box<dyn Errno>> {
match cmd {
IoCmd::RNG1 => todo!(),
IoCmd::RNG2 => todo!(),
IoCmd::RNGGETGENUINE(_) => todo!(),
IoCmd::RNGFIPS(_) => todo!(),
_ => todo!(), // ENOIOCTL,
}
}
Expand Down
5 changes: 2 additions & 3 deletions src/kernel/src/dev/ttyconsole.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,12 +87,11 @@ impl Tty {
}

/// See `tty_ioctl` on the PS4 for a reference.
fn generic_ioctl(&self, cmd: IoCmd, td: Option<&VThread>) -> Result<(), TtyIoctlError> {
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(()),
IoCmd::TIOCSCTTY => todo!(),
_ => todo!(),
}
}
Expand Down
11 changes: 9 additions & 2 deletions src/kernel/src/dmem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
use crate::dev::{Dmem, DmemContainer};
use crate::errno::EINVAL;
use crate::fs::{
make_dev, CharacterDevice, DriverFlags, Fs, MakeDevError, MakeDevFlags, Mode, VFile, VFileType,
make_dev, CharacterDevice, DriverFlags, Fs, MakeDevError, MakeDevFlags, Mode, VFile,
VFileFlags, VFileType,
};
use crate::info;
use crate::process::VThread;
Expand Down Expand Up @@ -116,6 +117,8 @@ impl DmemManager {
let dmem_container = td.proc().dmem_container_mut();
let current_container = *dmem_container;

info!("Getting dmem container");

if dmem_id != -1 {
todo!()
}
Expand All @@ -132,10 +135,14 @@ impl DmemManager {

let bp = BlockPool::new();

let flags = VFileFlags::from_bits_retain(flags) | VFileFlags::WRITE;

let fd = td
.proc()
.files()
.alloc(Arc::new(VFile::new(VFileType::Blockpool(bp))));
.alloc(Arc::new(VFile::new(VFileType::Blockpool(bp), flags)));

info!("Opened a blockpool at fd = {fd}");

Ok(fd.into())
}
Expand Down
3 changes: 1 addition & 2 deletions src/kernel/src/fs/dev/vnode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,7 @@ impl crate::fs::VnodeBackend for VnodeBackend {
}

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

fn read(
Expand Down
8 changes: 2 additions & 6 deletions src/kernel/src/fs/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,12 +24,12 @@ pub struct VFile {
}

impl VFile {
pub fn new(ty: VFileType) -> Self {
pub fn new(ty: VFileType, flags: VFileFlags) -> Self {
let gg = GutexGroup::new();

Self {
ty,
flags: VFileFlags::empty(),
flags,
offset: gg.spawn(0),
}
}
Expand All @@ -38,10 +38,6 @@ impl VFile {
self.flags
}

pub fn flags_mut(&mut self) -> &mut VFileFlags {
&mut self.flags
}

/// Checking if this returns `Some` is equivalent to when FreeBSD and the PS4 check
/// fp->f_ops->fo_flags & DFLAG_SEEKABLE != 0, therefore we use this instead.
pub fn seekable_vnode(&self) -> Option<&Arc<Vnode>> {
Expand Down
13 changes: 9 additions & 4 deletions src/kernel/src/fs/ioctl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,9 @@ commands! {
/// sceKernelMemoryPoolGetBlockStats
BPOOLSTATS(&mut BlockpoolStats) = 0x4010A802,

/// An unkown bnet command, called from libSceNet
BNETUNK(&Unknown36) = 0x802450c9,

/// Get media size in bytes.
DIOCGMEDIASIZE(&mut i64) = 0x40086418,

Expand Down Expand Up @@ -147,10 +150,10 @@ commands! {
/// Seek hole.
FIOSEEKHOLE(&mut i64) = 0xC0086662,

/// Unkown rng command
RNG1 = 0x40445301,
/// Unkown rng command
RNG2 = 0x40445302,
/// Get genuine random
RNGGETGENUINE(&mut Unknown68) = 0x40445301,
/// Fips186Prng
RNGFIPS(&mut Unknown68) = 0x40445302,

/// Become controlling terminal.
TIOCSCTTY = 0x20007461,
Expand All @@ -160,6 +163,8 @@ commands! {
type Unknown2 = Unknown<2>;
type Unknown8 = Unknown<8>;
type Unknown16 = Unknown<16>;
type Unknown36 = Unknown<36>;
type Unknown68 = Unknown<68>;

/// A dummy type to be used as a placeholder for unknown data.
#[derive(Debug)]
Expand Down
21 changes: 13 additions & 8 deletions src/kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,12 @@ impl Fs {
self.root.read().clone()
}

pub fn open(&self, path: impl AsRef<VPath>, td: Option<&VThread>) -> Result<VFile, OpenError> {
pub fn open(
&self,
path: impl AsRef<VPath>,
flags: VFileFlags,
td: Option<&VThread>,
) -> Result<VFile, OpenError> {
let vnode = self
.lookup(path, true, td)
.map_err(OpenError::LookupFailed)?;
Expand All @@ -181,7 +186,7 @@ impl Fs {
VFileType::Vnode(vnode.clone())
};

Ok(VFile::new(ty))
Ok(VFile::new(ty, flags))
}

pub fn lookup(
Expand Down Expand Up @@ -389,7 +394,6 @@ impl Fs {
// Get arguments.
let path = unsafe { i.args[0].to_path()?.unwrap() };
let flags: OpenFlags = i.args[1].try_into().unwrap();
let mode: u32 = i.args[2].try_into().unwrap();

// Check flags.
if flags.intersects(OpenFlags::O_EXEC) {
Expand All @@ -409,16 +413,17 @@ impl Fs {
todo!("open({path}) with flags & O_EXLOCK");
} else if flags.intersects(OpenFlags::O_TRUNC) {
todo!("open({path}) with flags & O_TRUNC");
} else if mode != 0 {
todo!("open({path}, {flags}) with mode = {mode}");
} else {
let mode: u32 = i.args[2].try_into().unwrap();
if mode != 0 {
todo!("open({path}, {flags}) with mode = {mode}");
}
}

info!("Opening {path} with flags = {flags}.");

// Lookup file.
let mut file = self.open(path, Some(td))?;

*file.flags_mut() = flags.into_fflags();
let mut file = self.open(path, flags.into_fflags(), Some(td))?;

// Install to descriptor table.
let fd = td.proc().files().alloc(Arc::new(file));
Expand Down
6 changes: 4 additions & 2 deletions src/kernel/src/kqueue/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,11 @@ impl KernelQueueManager {

filedesc.insert_kqueue(kq.clone());

Ok(VFileType::KernelQueue(kq))
Ok(VFile::new(
VFileType::KernelQueue(kq),
VFileFlags::READ | VFileFlags::WRITE,
))
},
VFileFlags::READ | VFileFlags::WRITE,
BudgetType::FdEqueue,
)?;

Expand Down
58 changes: 52 additions & 6 deletions src/kernel/src/net/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::budget::BudgetType;
use crate::errno::{Errno, EFAULT, EINVAL};
use crate::fs::{IoVec, VFileFlags, VFileType};
use crate::info;
use crate::fs::{IoVec, VFile, VFileFlags, VFileType};
use crate::{arnd, info};
use crate::{
process::VThread,
syscalls::{SysErr, SysIn, SysOut, Syscalls},
Expand Down Expand Up @@ -30,6 +30,7 @@ impl NetManager {
sys.register(28, &net, Self::sys_sendmsg);
sys.register(29, &net, Self::sys_recvfrom);
sys.register(97, &net, Self::sys_socket);
sys.register(99, &net, Self::sys_netcontrol);
sys.register(105, &net, Self::sys_setsockopt);
sys.register(113, &net, Self::sys_socketex);
sys.register(114, &net, Self::sys_socketclose);
Expand Down Expand Up @@ -77,6 +78,45 @@ impl NetManager {
todo!()
}

fn sys_netcontrol(self: &Arc<Self>, td: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
let fd: i32 = i.args[0].try_into().unwrap();
let op: i32 = i.args[1].try_into().unwrap();
let buf: *mut u8 = i.args[2].into();
let buflen: u32 = i.args[3].try_into().unwrap();

info!("Netcontrol called with op = {op}.");

let buf = if buf.is_null() {
None
} else {
if buflen > 160 {
return Err(SysErr::Raw(EINVAL));
}

Some(unsafe { core::slice::from_raw_parts_mut(buf, buflen as usize) })
SuchAFuriousDeath marked this conversation as resolved.
Show resolved Hide resolved
};

let _ = if fd < 0 {
} else {
todo!()
};

match buf {
Some(buf) => match op {
// bnet_get_secure_seed
0x14 if buf.len() > 3 => arnd::rand_bytes(&mut buf[..4]),
_ => todo!("netcontrol with op = {op}"),
},
None => todo!("netcontrol with buf = null"),
}

if fd > -1 {
todo!()
}

Ok(SysOut::ZERO)
}

fn sys_setsockopt(self: &Arc<Self>, td: &VThread, i: &SysIn) -> Result<SysOut, SysErr> {
let fd: i32 = i.args[0].try_into().unwrap();
let level: i32 = i.args[1].try_into().unwrap();
Expand Down Expand Up @@ -110,12 +150,13 @@ impl NetManager {
VFileType::Socket(so)
};

Ok(ty)
Ok(VFile::new(ty, VFileFlags::READ | VFileFlags::WRITE))
},
VFileFlags::WRITE | VFileFlags::READ,
budget,
)?;

info!("Opened a socket at fd {fd}.");

Ok(fd.into())
}

Expand Down Expand Up @@ -153,12 +194,17 @@ impl NetManager {
VFileType::Socket(so)
};

Ok(ty)
Ok(VFile::new(ty, VFileFlags::READ | VFileFlags::WRITE))
},
VFileFlags::WRITE | VFileFlags::READ,
budget,
)?;

if let Some(name) = name {
info!("Opened a socket with name = {name} at fd {fd}.");
} else {
info!("Opened a socket at fd {fd}.");
}

Ok(fd.into())
}

Expand Down
36 changes: 28 additions & 8 deletions src/kernel/src/process/filedesc.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::budget::BudgetType;
use crate::errno::{Errno, EBADF};
use crate::fs::{VFile, VFileFlags, VFileType, Vnode};
use crate::fs::{VFile, VFileFlags, Vnode};
use crate::kqueue::KernelQueue;
use gmtx::{Gutex, GutexGroup};
use macros::Errno;
Expand Down Expand Up @@ -55,20 +55,40 @@ impl FileDesc {
#[allow(unused_variables)] // TODO: remove when implementing; add budget argument
pub fn alloc_with_budget<E: Errno>(
&self,
constructor: impl FnOnce(i32) -> Result<VFileType, E>,
flags: VFileFlags,
budget: BudgetType,
constructor: impl FnOnce(i32) -> Result<VFile, E>,
_budget: BudgetType,
) -> Result<i32, FileAllocError<E>> {
todo!()
// TODO: check budget

self.alloc_without_budget(constructor)
}

#[allow(unused_variables)] // TODO: remove when implementing;
pub fn alloc_without_budget<E: Errno>(
&self,
constructor: impl FnOnce(i32) -> Result<VFileType, E>,
flags: VFileFlags,
constructor: impl FnOnce(i32) -> Result<VFile, E>,
) -> Result<i32, FileAllocError<E>> {
todo!()
// TODO: Implement fdalloc.
let mut files = self.files.write();

for i in 0..=(i32::MAX) as usize {
if i == files.len() {
let file = constructor(i as i32).map_err(FileAllocError::Inner)?;

files.push(Some(Arc::new(file)));
} else if files[i].is_none() {
let file = constructor(i as i32).map_err(FileAllocError::Inner)?;

files[i] = Some(Arc::new(file));
} else {
continue;
}

return Ok(i as i32);
}

// This should never happen.
panic!("Too many files has been opened.");
}

/// See `finstall` on the PS4 for a reference.
Expand Down
Loading