Skip to content

Commit

Permalink
Implements flags checking for fork1 implementation (#1081)
Browse files Browse the repository at this point in the history
  • Loading branch information
ultimaweapon authored Nov 2, 2024
1 parent 7c140d0 commit 829f54e
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 11 deletions.
3 changes: 3 additions & 0 deletions kernel/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ mod malloc;
mod panic;
mod proc;
mod sched;
mod signal;
mod trap;
mod uma;

Expand Down Expand Up @@ -87,6 +88,8 @@ fn create_init() {
let flags = Fork::new().with_copy_fd(true);

pmgr.fork(flags).unwrap();

todo!()
}

/// See `scheduler` function on the PS4 for a reference.
Expand Down
51 changes: 44 additions & 7 deletions kernel/src/proc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ pub use self::process::*;
pub use self::thread::*;

use crate::lock::{MappedMutex, Mutex, MutexGuard};
use crate::signal::Signal;
use alloc::sync::{Arc, Weak};
use bitfield_struct::bitfield;
use core::error::Error;
Expand Down Expand Up @@ -35,12 +36,15 @@ impl ProcMgr {

/// Our implementation imply `RFPROC` since it is always specified on the PS4.
///
/// We also imply `FSTOPPED` to make [`ProcMgr`] not depend on a scheduler.
/// We also imply `RFSTOPPED` to make [`ProcMgr`] not depend on a scheduler.
///
/// See `fork1` on the PS4 for a reference.
pub fn fork(&self, flags: Fork) -> Result<Arc<Proc>, ForkError> {
// TODO: Refactor this for readability.
if (flags.into_bits() & 0x60008f8b) != 0 {
if (flags.into_bits() & 0x60008f8b) != 0
|| flags.copy_fd() && flags.clear_fd()
|| flags.parent_signal().into_bits() != 0 && !flags.custom_signal()
{
return Err(ForkError::InvalidFlags);
}

Expand All @@ -51,14 +55,47 @@ impl ProcMgr {
/// Flags to control behavior of [`ProcMgr::fork()`].
#[bitfield(u32)]
pub struct Fork {
#[bits(2)]
__: u8,
/// Duplicate file descriptor table to the child instead of sharing it with the parent.
__: bool,
__: bool,
/// Duplicate file descriptor table to the child instead of sharing it with the parent. Cannot
/// used together with [`Self::clear_fd()`].
///
/// This has the same value as `RFFDG`.
pub copy_fd: bool,
#[bits(29)]
__: u32,
__: bool,
__: bool,
__: bool,
__: bool,
__: bool,
__: bool,
__: bool,
__: bool,
__: bool,
/// Create an empty file descriptor table for the child. Cannot used together with
/// [`Self::copy_fd()`].
///
/// This has the same value as `RFCFDG`.
pub clear_fd: bool,
__: bool,
__: bool,
__: bool,
__: bool,
__: bool,
__: bool,
/// Enable [`Self::parent_signal()`].
///
/// This has the same value as `RFTSIGZMB`.
pub custom_signal: bool,
/// Use this signal instead of `SIGCHLD` to notify the parent. Requires
/// [`Self::custom_signal()`] to be enabled.
///
/// This has the same value produced by `RFTSIGNUM` macro.
#[bits(8)]
pub parent_signal: Signal,
__: bool,
__: bool,
__: bool,
__: bool,
}

/// Represents an error when [`ProcMgr::fork()`] fails.
Expand Down
18 changes: 18 additions & 0 deletions kernel/src/signal/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/// Value of *nix signal.
#[derive(Debug, Clone, Copy)]
pub struct Signal(u8);

impl Signal {
const MAX: u8 = 128; // _SIG_MAXSIG

/// # Panics
/// If `v` is not a valid signal number.
pub const fn from_bits(v: u8) -> Self {
assert!(v <= Self::MAX);
Self(v)
}

pub const fn into_bits(self) -> u8 {
self.0
}
}
4 changes: 0 additions & 4 deletions src/kernel/src/signal/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,6 @@ impl SignalManager {
}
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Signal(NonZeroI32);

impl Signal {
pub const fn new(raw: i32) -> Option<Self> {
match raw {
Expand Down Expand Up @@ -159,7 +156,6 @@ pub fn strsignal(sig: Signal) -> Cow<'static, str> {
strsignal_impl(sig)
}

pub const SIG_MAXSIG: i32 = 128;
pub const SIG_IGN: usize = 1;
pub const SIG_DFL: usize = 0;

Expand Down

0 comments on commit 829f54e

Please sign in to comment.