syscall/
flag.rs

1use bitflags::bitflags as inner_bitflags;
2use core::{mem, ops::Deref, slice};
3
4macro_rules! bitflags {
5    (
6        $(#[$outer:meta])*
7        pub struct $BitFlags:ident: $T:ty {
8            $(
9                $(#[$inner:ident $($args:tt)*])*
10                const $Flag:ident = $value:expr;
11            )+
12        }
13    ) => {
14        // First, use the inner bitflags
15        inner_bitflags! {
16            #[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Debug, Clone, Copy, Default)]
17            $(#[$outer])*
18            pub struct $BitFlags: $T {
19                $(
20                    $(#[$inner $($args)*])*
21                    const $Flag = $value;
22                )+
23            }
24        }
25
26        impl $BitFlags {
27            #[deprecated = "use the safe `from_bits_retain` method instead"]
28            pub unsafe fn from_bits_unchecked(bits: $T) -> Self {
29                Self::from_bits_retain(bits)
30            }
31        }
32
33        // Secondly, re-export all inner constants
34        // (`pub use self::Struct::*` doesn't work)
35        $(
36            $(#[$inner $($args)*])*
37            pub const $Flag: $BitFlags = $BitFlags::$Flag;
38        )+
39    }
40}
41
42pub const CLOCK_REALTIME: usize = 1;
43pub const CLOCK_MONOTONIC: usize = 4;
44
45bitflags! {
46    pub struct EventFlags: usize {
47        const EVENT_NONE = 0;
48        const EVENT_READ = 1;
49        const EVENT_WRITE = 2;
50    }
51}
52
53pub const F_DUPFD: usize = 0;
54pub const F_GETFD: usize = 1;
55pub const F_SETFD: usize = 2;
56pub const F_GETFL: usize = 3;
57pub const F_SETFL: usize = 4;
58
59pub const FUTEX_WAIT: usize = 0;
60pub const FUTEX_WAKE: usize = 1;
61pub const FUTEX_REQUEUE: usize = 2;
62pub const FUTEX_WAIT64: usize = 3;
63
64// packet.c = fd
65pub const SKMSG_FRETURNFD: usize = 0;
66
67// packet.uid:packet.gid = offset, packet.c = base address, packet.d = page count
68pub const SKMSG_PROVIDE_MMAP: usize = 1;
69
70// packet.id provides state, packet.c = dest fd or pointer to dest fd, packet.d = flags
71pub const SKMSG_FOBTAINFD: usize = 2;
72
73// TODO: Split SendFdFlags into caller flags and flags that the scheme receives?
74bitflags::bitflags! {
75    #[derive(Clone, Copy, Debug)]
76    pub struct SendFdFlags: usize {
77        /// If set, the kernel will enforce that the file descriptor is exclusively owned.
78        ///
79        /// That is, there will no longer exist any other reference to that FD when removed from
80        /// the file table (SYS_SENDFD always removes the FD from the file table, but without this
81        /// flag, it can be retained by SYS_DUPing it first).
82        const EXCLUSIVE = 1;
83    }
84}
85bitflags::bitflags! {
86    #[derive(Clone, Copy, Debug)]
87    pub struct FobtainFdFlags: usize {
88        /// If set, `packet.c` specifies the destination file descriptor slot, otherwise the lowest
89        /// available slot will be selected, and placed in the usize pointed to by `packet.c`.
90        const MANUAL_FD = 1;
91
92        // If set, the file descriptor received is guaranteed to be exclusively owned (by the file
93        // table the obtainer is running in).
94        const EXCLUSIVE = 2;
95
96        // No, cloexec won't be stored in the kernel in the future, when the stable ABI is moved to
97        // relibc, so no flag for that!
98    }
99}
100
101bitflags! {
102    pub struct MapFlags: usize {
103        // TODO: Downgrade PROT_NONE to global constant? (bitflags specifically states zero flags
104        // can cause buggy behavior).
105        const PROT_NONE = 0x0000_0000;
106
107        const PROT_EXEC = 0x0001_0000;
108        const PROT_WRITE = 0x0002_0000;
109        const PROT_READ = 0x0004_0000;
110
111        const MAP_SHARED = 0x0001;
112        const MAP_PRIVATE = 0x0002;
113
114        const MAP_FIXED = 0x0004;
115        const MAP_FIXED_NOREPLACE = 0x000C;
116
117        /// For *userspace-backed mmaps*, return from the mmap call before all pages have been
118        /// provided by the scheme. This requires the scheme to be trusted, as the current context
119        /// can block indefinitely, if the scheme does not respond to the page fault handler's
120        /// request, as it tries to map the page by requesting it from the scheme.
121        ///
122        /// In some cases however, such as the program loader, the data needs to be trusted as much
123        /// with or without MAP_LAZY, and if so, mapping lazily will not cause insecureness by
124        /// itself.
125        ///
126        /// For kernel-backed mmaps, this flag has no effect at all. It is unspecified whether
127        /// kernel mmaps are lazy or not.
128        const MAP_LAZY = 0x0010;
129    }
130}
131bitflags! {
132    pub struct MunmapFlags: usize {
133        /// Indicates whether the funmap call must implicitly do an msync, for the changes to
134        /// become visible later.
135        ///
136        /// This flag will currently be set if and only if MAP_SHARED | PROT_WRITE are set.
137        const NEEDS_SYNC = 1;
138    }
139}
140
141pub const MODE_TYPE: u16 = 0xF000;
142pub const MODE_DIR: u16 = 0x4000;
143pub const MODE_FILE: u16 = 0x8000;
144pub const MODE_SYMLINK: u16 = 0xA000;
145pub const MODE_FIFO: u16 = 0x1000;
146pub const MODE_CHR: u16 = 0x2000;
147pub const MODE_SOCK: u16 = 0xC000;
148
149pub const MODE_PERM: u16 = 0x0FFF;
150pub const MODE_SETUID: u16 = 0o4000;
151pub const MODE_SETGID: u16 = 0o2000;
152
153pub const O_RDONLY: usize = 0x0001_0000;
154pub const O_WRONLY: usize = 0x0002_0000;
155pub const O_RDWR: usize = 0x0003_0000;
156pub const O_NONBLOCK: usize = 0x0004_0000;
157pub const O_APPEND: usize = 0x0008_0000;
158pub const O_SHLOCK: usize = 0x0010_0000;
159pub const O_EXLOCK: usize = 0x0020_0000;
160pub const O_ASYNC: usize = 0x0040_0000;
161pub const O_FSYNC: usize = 0x0080_0000;
162pub const O_CLOEXEC: usize = 0x0100_0000;
163pub const O_CREAT: usize = 0x0200_0000;
164pub const O_TRUNC: usize = 0x0400_0000;
165pub const O_EXCL: usize = 0x0800_0000;
166pub const O_DIRECTORY: usize = 0x1000_0000;
167pub const O_STAT: usize = 0x2000_0000;
168pub const O_SYMLINK: usize = 0x4000_0000;
169pub const O_NOFOLLOW: usize = 0x8000_0000;
170pub const O_ACCMODE: usize = O_RDONLY | O_WRONLY | O_RDWR;
171
172// The top 48 bits of PTRACE_* are reserved, for now
173
174// NOT ABI STABLE!
175#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
176#[repr(usize)]
177pub enum ContextStatus {
178    Runnable,
179    Blocked,
180    NotYetStarted,
181    Dead,
182    ForceKilled,
183    Stopped,
184    UnhandledExcp,
185    #[default]
186    Other, // reserved
187}
188
189#[derive(Clone, Copy, Debug, Eq, PartialEq)]
190#[repr(usize)]
191pub enum ContextVerb {
192    Stop = 1,
193    Unstop = 2,
194    Interrupt = 3,
195    ForceKill = usize::MAX,
196}
197impl ContextVerb {
198    pub fn try_from_raw(raw: usize) -> Option<Self> {
199        Some(match raw {
200            1 => Self::Stop,
201            2 => Self::Unstop,
202            3 => Self::Interrupt,
203            usize::MAX => Self::ForceKill,
204            _ => return None,
205        })
206    }
207}
208
209// NOT ABI STABLE!
210#[derive(Clone, Copy, Debug, Eq, PartialEq)]
211#[repr(u8)]
212pub enum ProcSchemeVerb {
213    Iopl = 255,
214}
215impl ProcSchemeVerb {
216    pub fn try_from_raw(verb: u8) -> Option<Self> {
217        Some(match verb {
218            255 => Self::Iopl,
219            _ => return None,
220        })
221    }
222}
223
224#[derive(Clone, Copy, Debug, Eq, PartialEq)]
225#[repr(usize)]
226#[non_exhaustive]
227pub enum FsCall {
228    Connect = 0,
229}
230impl FsCall {
231    pub fn try_from_raw(raw: usize) -> Option<Self> {
232        Some(match raw {
233            0 => Self::Connect,
234            _ => return None,
235        })
236    }
237}
238
239bitflags! {
240    pub struct PtraceFlags: u64 {
241        /// Stop before a syscall is handled. Send PTRACE_FLAG_IGNORE to not
242        /// handle the syscall.
243        const PTRACE_STOP_PRE_SYSCALL = 0x0000_0000_0000_0001;
244        /// Stop after a syscall is handled.
245        const PTRACE_STOP_POST_SYSCALL = 0x0000_0000_0000_0002;
246        /// Stop after exactly one instruction. TODO: This may not handle
247        /// fexec/signal boundaries. Should it?
248        const PTRACE_STOP_SINGLESTEP = 0x0000_0000_0000_0004;
249        /// Stop before a signal is handled. Send PTRACE_FLAG_IGNORE to not
250        /// handle signal.
251        const PTRACE_STOP_SIGNAL = 0x0000_0000_0000_0008;
252        /// Stop on a software breakpoint, such as the int3 instruction for
253        /// x86_64.
254        const PTRACE_STOP_BREAKPOINT = 0x0000_0000_0000_0010;
255        /// Stop just before exiting for good.
256        const PTRACE_STOP_EXIT = 0x0000_0000_0000_0020;
257
258        const PTRACE_STOP_MASK = 0x0000_0000_0000_00FF;
259
260
261        /// Sent when a child is cloned, giving you the opportunity to trace it.
262        /// If you don't catch this, the child is started as normal.
263        const PTRACE_EVENT_CLONE = 0x0000_0000_0000_0100;
264
265        /// Sent when current-addrspace is changed, allowing the tracer to reopen the memory file.
266        const PTRACE_EVENT_ADDRSPACE_SWITCH = 0x0000_0000_0000_0200;
267
268        const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00;
269
270        /// Special meaning, depending on the event. Usually, when fired before
271        /// an action, it will skip performing that action.
272        const PTRACE_FLAG_IGNORE = 0x0000_0000_0000_1000;
273
274        const PTRACE_FLAG_MASK = 0x0000_0000_0000_F000;
275    }
276}
277impl Deref for PtraceFlags {
278    type Target = [u8];
279    fn deref(&self) -> &Self::Target {
280        // Same as to_ne_bytes but in-place
281        unsafe {
282            slice::from_raw_parts(&self.bits() as *const _ as *const u8, mem::size_of::<u64>())
283        }
284    }
285}
286
287pub const SEEK_SET: usize = 0;
288pub const SEEK_CUR: usize = 1;
289pub const SEEK_END: usize = 2;
290
291pub const SIGCHLD: usize = 17;
292pub const SIGTSTP: usize = 20;
293pub const SIGTTIN: usize = 21;
294pub const SIGTTOU: usize = 22;
295
296pub const ADDRSPACE_OP_MMAP: usize = 0;
297pub const ADDRSPACE_OP_MUNMAP: usize = 1;
298pub const ADDRSPACE_OP_MPROTECT: usize = 2;
299pub const ADDRSPACE_OP_TRANSFER: usize = 3;
300
301bitflags! {
302    pub struct MremapFlags: usize {
303        const FIXED = 1;
304        const FIXED_REPLACE = 3;
305        /// Alias's memory region at `old_address` to `new_address` such that both regions share
306        /// the same frames.
307        const KEEP_OLD = 1 << 2;
308        // TODO: MAYMOVE, DONTUNMAP
309    }
310}
311bitflags! {
312    pub struct RwFlags: u32 {
313        const NONBLOCK = 1;
314        const APPEND = 2;
315        // TODO: sync/dsync
316        // TODO: O_DIRECT?
317    }
318}
319bitflags! {
320    pub struct SigcontrolFlags: usize {
321        /// Prevents the kernel from jumping the context to the signal trampoline, but otherwise
322        /// has absolutely no effect on which signals are blocked etc. Meant to be used for
323        /// short-lived critical sections inside libc.
324        const INHIBIT_DELIVERY = 1;
325    }
326}
327bitflags! {
328    pub struct CallFlags: usize {
329        // reserved
330        const RSVD0 = 1 << 0;
331        const RSVD1 = 1 << 1;
332        const RSVD2 = 1 << 2;
333        const RSVD3 = 1 << 3;
334        const RSVD4 = 1 << 4;
335        const RSVD5 = 1 << 5;
336        const RSVD6 = 1 << 6;
337        const RSVD7 = 1 << 7;
338
339        /// Remove the fd from the caller's file table before sending the message.
340        const CONSUME = 1 << 8;
341    }
342}