Skip to content

Commit

Permalink
Revises how proc is constructed (#541)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Dec 30, 2023
1 parent d71667b commit fdeec50
Show file tree
Hide file tree
Showing 14 changed files with 162 additions and 199 deletions.
19 changes: 6 additions & 13 deletions src/kernel/src/budget/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
use crate::errno::{ENOENT, ENOSYS, ESRCH};
use crate::idt::Idt;
use crate::info;
use crate::process::{VProc, VThread};
use crate::process::VThread;
use crate::syscalls::{SysErr, SysIn, SysOut, Syscalls};
use std::ops::Deref;
use std::sync::{Arc, Mutex};

/// An implementation of budget system on the PS4.
pub struct BudgetManager {
vp: Arc<VProc>,
budgets: Mutex<Idt<Arc<Budget>>>,
}

impl BudgetManager {
pub fn new(vp: &Arc<VProc>, sys: &mut Syscalls) -> Arc<Self> {
pub fn new(sys: &mut Syscalls) -> Arc<Self> {
let mgr = Arc::new(Self {
vp: vp.clone(),
budgets: Mutex::new(Idt::new(0x1000)),
});

Expand All @@ -42,15 +39,11 @@ impl BudgetManager {

info!("Getting budget process type for process {pid}.");

if td.cred().is_system() || pid == -1 || pid == self.vp.id().get() {
if pid == -1 || pid == self.vp.id().get() {
// Get budget ID.
let id = match self.vp.budget().deref() {
Some(v) => v.0,
None => return Err(SysErr::Raw(ENOENT)),
};

if td.cred().is_system() || pid == -1 || pid == td.proc().id().get() {
if pid == -1 || pid == td.proc().id().get() {
// Lookup budget.
let id = td.proc().budget_id();

match self.budgets.lock().unwrap().get_mut(id, Some(0x2000)) {
Some(v) => Ok((v.data().ptype as i32).into()),
None => Err(SysErr::Raw(ENOENT)),
Expand Down
25 changes: 11 additions & 14 deletions src/kernel/src/dmem/mod.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
use crate::errno::EINVAL;
use crate::fs::Fs;
use crate::info;
use crate::process::VProc;
use crate::process::VThread;
use crate::syscalls::{SysErr, SysIn, SysOut, Syscalls};
use std::sync::Arc;

/// An implementation of direct memory system on the PS4.
pub struct DmemManager {
vp: Arc<VProc>,
fs: Arc<Fs>,
}

impl DmemManager {
pub fn new(vp: &Arc<VProc>, fs: &Arc<Fs>, sys: &mut Syscalls) -> Arc<Self> {
let dmem = Arc::new(Self {
vp: vp.clone(),
fs: fs.clone(),
});
pub fn new(fs: &Arc<Fs>, sys: &mut Syscalls) -> Arc<Self> {
let dmem = Arc::new(Self { fs: fs.clone() });

sys.register(586, &dmem, Self::sys_dmem_container);
sys.register(653, &dmem, Self::sys_blockpool_open);
Expand All @@ -25,13 +21,15 @@ impl DmemManager {
}

fn sys_dmem_container(self: &Arc<Self>, i: &SysIn) -> Result<SysOut, SysErr> {
let update: i32 = i.args[0].try_into().unwrap();
let td = VThread::current().unwrap();
let set: i32 = i.args[0].try_into().unwrap();
let get: i32 = td.proc().dmem_container().try_into().unwrap();

if update != -1 {
if set != -1 {
todo!("sys_dmem_container with update != -1");
}

Ok((*self.vp.dmem_container()).into())
Ok(get.into())
}

fn sys_blockpool_open(self: &Arc<Self>, i: &SysIn) -> Result<SysOut, SysErr> {
Expand All @@ -41,11 +39,10 @@ impl DmemManager {
return Err(SysErr::Raw(EINVAL));
}

//TODO: actually allocate a blockpool. set filops, etc.

// TODO: actually allocate a blockpool. set filops, etc.
let file = self.fs.alloc();

let fd = self.vp.files().alloc(Arc::new(file));
let td = VThread::current().unwrap();
let fd = td.proc().files().alloc(Arc::new(file));

info!("File descriptor {fd} was allocated for a new blockpool");

Expand Down
6 changes: 3 additions & 3 deletions src/kernel/src/fs/dev/dmem1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use thiserror::Error;
pub struct Dmem1 {
vp: Arc<VProc>,
total_size: usize,
number: i32,
number: usize,
}

impl Dmem1 {
pub const PATH: &VPath = vpath!("/dev/dmem1");
pub const PATH: &'static VPath = vpath!("/dev/dmem1");

pub const DMEM_GRP: u8 = 0x80;

Expand Down Expand Up @@ -48,7 +48,7 @@ impl VFileOps for Dmem1 {
return Err(Box::new(IoctlErr::BadCredentials));
}

if self.number != 2 && self.number != *self.vp.dmem_container() && !cred.is_system() {
if self.number != 2 && self.number != self.vp.dmem_container() && !cred.is_system() {
return Err(Box::new(IoctlErr::BadCredentials));
}

Expand Down
49 changes: 20 additions & 29 deletions src/kernel/src/fs/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ pub use self::perm::*;
pub use self::vnode::*;
use crate::errno::{Errno, EBADF, EINVAL, ENAMETOOLONG, ENODEV, ENOENT, ENOTCAPABLE};
use crate::info;
use crate::process::{VProc, VThread};
use crate::process::VThread;
use crate::syscalls::{SysArg, SysErr, SysIn, SysOut, Syscalls};
use crate::ucred::{Privilege, Ucred};
use bitflags::bitflags;
Expand Down Expand Up @@ -39,7 +39,6 @@ mod vnode;
/// A virtual filesystem for emulating a PS4 filesystem.
#[derive(Debug)]
pub struct Fs {
vp: Arc<VProc>,
mounts: Gutex<Mounts>, // mountlist
root: Gutex<Arc<Vnode>>, // rootvnode
opens: AtomicI32, // openfiles
Expand All @@ -51,7 +50,6 @@ impl Fs {
game: G,
param: &Arc<Param>,
cred: &Ucred,
vp: &Arc<VProc>,
sys: &mut Syscalls,
) -> Arc<Self>
where
Expand All @@ -70,9 +68,6 @@ impl Fs {
// Get an initial root vnode.
let root = (init.fs().ops.root)(&mounts.push(init));

vp.files().set_cwd(root.clone());
*vp.files().root_mut() = Some(root.clone());

// Setup mount options for root FS.
let mut opts: HashMap<String, Box<dyn Any>> = HashMap::new();

Expand All @@ -87,7 +82,6 @@ impl Fs {
// Mount root FS.
let gg = GutexGroup::new();
let fs = Arc::new(Self {
vp: vp.clone(),
mounts: gg.spawn(mounts),
root: gg.spawn(root),
opens: AtomicI32::new(0),
Expand All @@ -108,10 +102,6 @@ impl Fs {
old
};

// Update process location.
vp.files().set_cwd(root.clone());
*vp.files().root_mut() = Some(root);

// Disconnect devfs from the old root.
*(om.fs().ops.root)(&om).item_mut() = None;

Expand Down Expand Up @@ -160,13 +150,18 @@ impl Fs {
nd.loopcnt = 0;

// TODO: Implement ktrnamei.
nd.rootdir = self.vp.files().root().clone();
nd.topdir = self.vp.files().jail().clone();
nd.rootdir = Some(
nd.cnd
.thread
.map_or_else(|| self.root(), |t| t.proc().files().root()),
);

let mut dp = if nd.cnd.pnbuf[0] != b'/' {
todo!("namei with relative path");
} else {
self.vp.files().cwd().clone()
nd.cnd
.thread
.map_or_else(|| self.root(), |t| t.proc().files().cwd())
};

if nd.startdir.is_some() {
Expand Down Expand Up @@ -254,18 +249,16 @@ impl Fs {
return Err(SysErr::Raw(EINVAL));
}

let buf = unsafe { std::slice::from_raw_parts(ptr, len) };

let file = self.vp.files().get(fd).ok_or(SysErr::Raw(EBADF))?;
let ops = file.ops().ok_or(SysErr::Raw(EBADF))?;

let td = VThread::current().unwrap();
let file = td.proc().files().get(fd).ok_or(SysErr::Raw(EBADF))?;
let ops = file.ops().ok_or(SysErr::Raw(EBADF))?;

info!("Writing {len} bytes to fd {fd}.");

let bytes_written = ops.write(file.as_ref(), buf, td.cred(), td.as_ref())?;
let buf = unsafe { std::slice::from_raw_parts(ptr, len) };
let written = ops.write(file.as_ref(), buf, td.cred(), td.as_ref())?;

Ok(bytes_written.into())
Ok(written.into())
}

fn sys_open(self: &Arc<Self>, i: &SysIn) -> Result<SysOut, SysErr> {
Expand Down Expand Up @@ -307,7 +300,6 @@ impl Fs {
dirp: path,
startdir: None,
rootdir: None,
topdir: None,
strictrelative: 0,
loopcnt: 0,
cnd: ComponentName {
Expand All @@ -320,22 +312,23 @@ impl Fs {
};

*file.flags_mut() = flags.to_fflags();
file.set_ops(Some(self.namei(&mut nd)?.open(&self.vp)?));
file.set_ops(Some(self.namei(&mut nd)?.open(td.proc())?));

// Install to descriptor table.
let fd = self.vp.files().alloc(Arc::new(file));
let fd = td.proc().files().alloc(Arc::new(file));

info!("File descriptor {fd} was allocated for {path}.");

Ok(fd.into())
}

fn sys_close(self: &Arc<Self>, i: &SysIn) -> Result<SysOut, SysErr> {
let td = VThread::current().unwrap();
let fd: i32 = i.args[0].try_into().unwrap();

info!("Closing fd {fd}.");

self.vp.files().free(fd)?;
td.proc().files().free(fd)?;

Ok(SysOut::ZERO)
}
Expand Down Expand Up @@ -372,7 +365,8 @@ impl Fs {
}

// Get target file.
let file = self.vp.files().get(fd).ok_or(SysErr::Raw(EBADF))?;
let td = VThread::current().unwrap();
let file = td.proc().files().get(fd).ok_or(SysErr::Raw(EBADF))?;
let ops = file.ops().ok_or(SysErr::Raw(EBADF))?;

if !file
Expand All @@ -383,8 +377,6 @@ impl Fs {
}

// Execute the operation.
let td = VThread::current().unwrap();

info!("Executing ioctl({com}) on {file}.");

match com {
Expand Down Expand Up @@ -421,7 +413,6 @@ impl Fs {
dirp: path,
startdir: None,
rootdir: None,
topdir: None,
strictrelative: 0,
loopcnt: 0,
cnd: ComponentName {
Expand Down
1 change: 0 additions & 1 deletion src/kernel/src/fs/namei.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ pub struct NameiData<'a> {
pub dirp: &'a str, // ni_dirp
pub startdir: Option<Arc<Vnode>>, // ni_startdir
pub rootdir: Option<Arc<Vnode>>, // ni_rootdir
pub topdir: Option<Arc<Vnode>>, // ni_topdir
pub strictrelative: i32, // ni_strictrelative
pub loopcnt: u32, // ni_loopcnt
pub cnd: ComponentName<'a>, // ni_cnd
Expand Down
Loading

0 comments on commit fdeec50

Please sign in to comment.