Skip to main content

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    StdFsCall = 33,
149}
150
151impl Opcode {
152    pub fn try_from_raw(raw: u8) -> Option<Self> {
153        use Opcode::*;
154
155        // TODO: Use a library where this match can be automated.
156        Some(match raw {
157            3 => Close,
158            4 => Dup,
159            5 => Read,
160            6 => Write,
161            7 => Fsize,
162            8 => Fchmod,
163            9 => Fchown,
164            10 => Fcntl,
165            11 => Fevent,
166            12 => Sendfd,
167            13 => Fpath,
168            14 => Frename,
169            15 => Fstat,
170            16 => Fstatvfs,
171            17 => Fsync,
172            18 => Ftruncate,
173            19 => Futimens,
174
175            20 => MmapPrep,
176            21 => RequestMmap,
177            22 => Mremap,
178            23 => Munmap,
179            24 => Msync,
180
181            25 => Cancel,
182            26 => Getdents,
183            27 => CloseMsg,
184            28 => Call,
185
186            29 => OpenAt,
187            30 => Flink,
188            31 => Recvfd,
189
190            32 => UnlinkAt,
191            33 => StdFsCall,
192
193            _ => return None,
194        })
195    }
196}