syscall/
schemev2.rs

1use core::{
2    mem,
3    ops::{Deref, DerefMut},
4    slice,
5};
6
7use bitflags::bitflags;
8
9pub struct CallerCtx {
10    pub pid: usize,
11    pub uid: u32,
12    pub gid: u32,
13}
14
15pub enum OpenResult {
16    ThisScheme { number: usize },
17    OtherScheme { fd: usize },
18}
19
20#[repr(C)]
21#[derive(Clone, Copy, Debug, Default)]
22pub struct Sqe {
23    pub opcode: u8,
24    pub sqe_flags: SqeFlags,
25    pub _rsvd: u16, // TODO: priority
26    pub tag: u32,
27    pub args: [u64; 6],
28    pub caller: u64,
29}
30impl Deref for Sqe {
31    type Target = [u8];
32    fn deref(&self) -> &[u8] {
33        unsafe { slice::from_raw_parts(self as *const Sqe as *const u8, mem::size_of::<Sqe>()) }
34    }
35}
36
37impl DerefMut for Sqe {
38    fn deref_mut(&mut self) -> &mut [u8] {
39        unsafe { slice::from_raw_parts_mut(self as *mut Sqe as *mut u8, mem::size_of::<Sqe>()) }
40    }
41}
42
43bitflags! {
44    #[derive(Clone, Copy, Debug, Default)]
45    pub struct SqeFlags: u8 {
46        // If zero, the message is bidirectional, and the scheme is expected to pass the Ksmsg's
47        // tag field to the Skmsg. Some opcodes require this flag to be set.
48        const ONEWAY = 1;
49    }
50}
51
52#[repr(C)]
53#[derive(Clone, Copy, Debug, Default)]
54pub struct Cqe {
55    pub flags: u8, // bits 2:0 are CqeOpcode
56    pub extra_raw: [u8; 3],
57    pub tag: u32,
58    pub result: u64,
59}
60impl Deref for Cqe {
61    type Target = [u8];
62    fn deref(&self) -> &[u8] {
63        unsafe { slice::from_raw_parts(self as *const Cqe as *const u8, mem::size_of::<Cqe>()) }
64    }
65}
66
67impl DerefMut for Cqe {
68    fn deref_mut(&mut self) -> &mut [u8] {
69        unsafe { slice::from_raw_parts_mut(self as *mut Cqe as *mut u8, mem::size_of::<Cqe>()) }
70    }
71}
72
73bitflags! {
74    #[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
75    pub struct NewFdFlags: u8 {
76        const POSITIONED = 1;
77    }
78}
79
80impl Cqe {
81    pub fn extra(&self) -> u32 {
82        u32::from_ne_bytes([self.extra_raw[0], self.extra_raw[1], self.extra_raw[2], 0])
83    }
84}
85
86#[repr(u8)]
87#[derive(Clone, Copy, Debug, Eq, PartialEq)]
88pub enum CqeOpcode {
89    RespondRegular,
90    RespondWithFd,
91    SendFevent, // no tag
92    ObtainFd,
93    RespondWithMultipleFds,
94    // TODO: ProvideMmap
95}
96impl CqeOpcode {
97    pub fn try_from_raw(raw: u8) -> Option<Self> {
98        Some(match raw {
99            0 => Self::RespondRegular,
100            1 => Self::RespondWithFd,
101            2 => Self::SendFevent,
102            3 => Self::ObtainFd,
103            4 => Self::RespondWithMultipleFds,
104            _ => return None,
105        })
106    }
107}
108
109#[repr(u8)]
110#[non_exhaustive]
111#[derive(Clone, Copy, Debug)]
112pub enum Opcode {
113    Close = 3,   // fd
114    Dup = 4,     // old fd, buf_ptr, buf_len
115    Read = 5,    // fd, buf_ptr, buf_len, TODO offset, TODO flags, _
116    Write = 6,   // fd, buf_ptr, buf_len, TODO offset, TODO flags)
117    Fsize = 7,   // fd
118    Fchmod = 8,  // fd, new mode
119    Fchown = 9,  // fd, new uid, new gid
120    Fcntl = 10,  // fd, cmd, arg
121    Fevent = 11, // fd, requested mask
122    Sendfd = 12,
123    Fpath = 13, // fd, buf_ptr, buf_len
124    Frename = 14,
125    Fstat = 15,     // fd, buf_ptr, buf_len
126    Fstatvfs = 16,  // fd, buf_ptr, buf_len
127    Fsync = 17,     // fd
128    Ftruncate = 18, // fd, new len
129    Futimens = 19,  // fd, times_buf, times_len
130
131    MmapPrep = 20,
132    RequestMmap = 21,
133    Mremap = 22,
134    Munmap = 23,
135    Msync = 24, // TODO
136
137    Cancel = 25, // @tag
138
139    Getdents = 26,
140    CloseMsg = 27,
141    Call = 28,
142
143    OpenAt = 29, // fd, buf_ptr, buf_len, flags
144    Flink = 30,
145    Recvfd = 31,
146
147    UnlinkAt = 32, // fd, path_ptr, path_len (utf8), flags
148}
149
150impl Opcode {
151    pub fn try_from_raw(raw: u8) -> Option<Self> {
152        use Opcode::*;
153
154        // TODO: Use a library where this match can be automated.
155        Some(match raw {
156            3 => Close,
157            4 => Dup,
158            5 => Read,
159            6 => Write,
160            7 => Fsize,
161            8 => Fchmod,
162            9 => Fchown,
163            10 => Fcntl,
164            11 => Fevent,
165            12 => Sendfd,
166            13 => Fpath,
167            14 => Frename,
168            15 => Fstat,
169            16 => Fstatvfs,
170            17 => Fsync,
171            18 => Ftruncate,
172            19 => Futimens,
173
174            20 => MmapPrep,
175            21 => RequestMmap,
176            22 => Mremap,
177            23 => Munmap,
178            24 => Msync,
179
180            25 => Cancel,
181            26 => Getdents,
182            27 => CloseMsg,
183            28 => Call,
184
185            29 => OpenAt,
186            30 => Flink,
187            31 => Recvfd,
188
189            32 => UnlinkAt,
190
191            _ => return None,
192        })
193    }
194}