syscall/
schemev2.rs

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