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 3: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    // TODO: ProvideMmap
83}
84impl CqeOpcode {
85    pub fn try_from_raw(raw: u8) -> Option<Self> {
86        Some(match raw {
87            0 => Self::RespondRegular,
88            1 => Self::RespondWithFd,
89            2 => Self::SendFevent,
90            3 => Self::ObtainFd,
91            _ => return None,
92        })
93    }
94}
95
96#[repr(u8)]
97#[non_exhaustive]
98#[derive(Clone, Copy, Debug)]
99pub enum Opcode {
100    Open = 0,    // path_ptr, path_len (utf8), flags
101    Rmdir = 1,   // path_ptr, path_len (utf8)
102    Unlink = 2,  // path_ptr, path_len (utf8)
103    Close = 3,   // fd
104    Dup = 4,     // old fd, buf_ptr, buf_len
105    Read = 5,    // fd, buf_ptr, buf_len, TODO offset, TODO flags, _
106    Write = 6,   // fd, buf_ptr, buf_len, TODO offset, TODO flags)
107    Fsize = 7,   // fd
108    Fchmod = 8,  // fd, new mode
109    Fchown = 9,  // fd, new uid, new gid
110    Fcntl = 10,  // fd, cmd, arg
111    Fevent = 11, // fd, requested mask
112    Sendfd = 12,
113    Fpath = 13, // fd, buf_ptr, buf_len
114    Frename = 14,
115    Fstat = 15,     // fd, buf_ptr, buf_len
116    Fstatvfs = 16,  // fd, buf_ptr, buf_len
117    Fsync = 17,     // fd
118    Ftruncate = 18, // fd, new len
119    Futimens = 19,  // fd, times_buf, times_len
120
121    MmapPrep = 20,
122    RequestMmap = 21,
123    Mremap = 22,
124    Munmap = 23,
125    Msync = 24, // TODO
126
127    Cancel = 25, // @tag
128
129    Getdents = 26,
130    CloseMsg = 27,
131}
132
133impl Opcode {
134    pub fn try_from_raw(raw: u8) -> Option<Self> {
135        use Opcode::*;
136
137        // TODO: Use a library where this match can be automated.
138        Some(match raw {
139            0 => Open,
140            1 => Rmdir,
141            2 => Unlink,
142            3 => Close,
143            4 => Dup,
144            5 => Read,
145            6 => Write,
146            7 => Fsize,
147            8 => Fchmod,
148            9 => Fchown,
149            10 => Fcntl,
150            11 => Fevent,
151            12 => Sendfd,
152            13 => Fpath,
153            14 => Frename,
154            15 => Fstat,
155            16 => Fstatvfs,
156            17 => Fsync,
157            18 => Ftruncate,
158            19 => Futimens,
159
160            20 => MmapPrep,
161            21 => RequestMmap,
162            22 => Mremap,
163            23 => Munmap,
164            24 => Msync,
165
166            25 => Cancel,
167            26 => Getdents,
168            27 => CloseMsg,
169
170            _ => return None,
171        })
172    }
173}