safa_abi/raw/
processes.rs

1use core::ops::BitOr;
2
3use super::{Optional, RawSlice, RawSliceMut};
4
5#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
6#[repr(C)]
7/// ABI structures are structures that are passed to tasks by the parent task
8/// for now only stdio file descriptors are passed
9/// you get a pointer to them in the `r8` register at _start (the 5th argument)
10pub struct AbiStructures {
11    pub stdio: TaskStdio,
12}
13
14#[derive(Debug, Default, Clone, Copy, PartialEq, Eq)]
15#[repr(C)]
16pub struct TaskStdio {
17    pub stdout: Optional<usize>,
18    pub stdin: Optional<usize>,
19    pub stderr: Optional<usize>,
20}
21
22impl TaskStdio {
23    pub fn new(stdout: Option<usize>, stdin: Option<usize>, stderr: Option<usize>) -> Self {
24        Self {
25            stdout: stdout.into(),
26            stdin: stdin.into(),
27            stderr: stderr.into(),
28        }
29    }
30}
31
32#[derive(Debug, Clone, Copy)]
33#[repr(C)]
34/// Flags for the [crate::syscalls::SyscallTable::SysPSpawn] syscall
35pub struct SpawnFlags(u8);
36impl SpawnFlags {
37    pub const CLONE_RESOURCES: Self = Self(1 << 0);
38    pub const CLONE_CWD: Self = Self(1 << 1);
39    pub const EMPTY: Self = Self(0);
40}
41
42impl BitOr for SpawnFlags {
43    type Output = Self;
44    fn bitor(self, rhs: Self) -> Self::Output {
45        Self(self.0 | rhs.0)
46    }
47}
48
49/// configuration for the spawn syscall
50#[repr(C)]
51pub struct SpawnConfig {
52    /// config version for compatibility
53    /// added in kernel version 0.2.1 and therefore breaking compatibility with any program compiled for version below 0.2.1
54    pub version: u8,
55    pub name: RawSlice<u8>,
56    pub argv: RawSliceMut<RawSlice<u8>>,
57    pub flags: SpawnFlags,
58    pub stdio: *const TaskStdio,
59    pub env: RawSliceMut<RawSlice<u8>>,
60}
61
62impl SpawnConfig {
63    pub fn new(
64        name: &str,
65        argv: *mut [&[u8]],
66        env: *mut [&[u8]],
67        flags: SpawnFlags,
68        stdio: &TaskStdio,
69    ) -> Self {
70        let name = unsafe { RawSlice::from_slice(name.as_bytes()) };
71        let argv = unsafe { RawSliceMut::from_slices(argv) };
72        let env = unsafe { RawSliceMut::from_slices(env) };
73
74        Self {
75            version: 1,
76            name,
77            argv,
78            env,
79            flags,
80            stdio: stdio as *const TaskStdio,
81        }
82    }
83}