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;
58pub const F_DUPFD_CLOEXEC: usize = 1030;
59
60pub const FUTEX_WAIT: usize = 0;
61pub const FUTEX_WAKE: usize = 1;
62pub const FUTEX_REQUEUE: usize = 2;
63pub const FUTEX_WAIT64: usize = 3;
64
65// TODO: Split SendFdFlags into caller flags and flags that the scheme receives?
66bitflags::bitflags! {
67    #[derive(Clone, Copy, Debug)]
68    pub struct SendFdFlags: usize {
69        /// If set, the kernel will enforce that the file descriptors are exclusively owned.
70        ///
71        /// That is, there will no longer exist any other reference to those FDs when removed from
72        /// the file table (sendfd always removes the FDs from the file table, but without this
73        /// flag, it can be retained by SYS_DUPing them first).
74        const EXCLUSIVE = 1;
75
76        /// If set, the file descriptors will be cloned and *not* removed from the sender's file table.
77        /// By default, `SYS_SENDFD` moves the file descriptors, removing them from the sender.
78        const CLONE = 2;
79    }
80}
81bitflags::bitflags! {
82    #[derive(Clone, Copy, Debug)]
83    pub struct FobtainFdFlags: usize {
84        /// If set, the SYS_CALL payload specifies the destination file descriptor slots, otherwise the lowest
85        /// available slots will be selected, and placed in the usize pointed to by SYS_CALL
86        /// payload.
87        const MANUAL_FD = 1;
88
89        /// If set, the file descriptors received are guaranteed to be exclusively owned (by the file
90        /// table the obtainer is running in).
91        const EXCLUSIVE = 2;
92
93        /// If set, the file descriptors received will be placed into the *upper* file table.
94        const UPPER_TBL = 4;
95
96        /// If set, the received file descriptors are marked as close-on-exec.
97        const CLOEXEC = 8;
98
99        // No, cloexec won't be stored in the kernel in the future, when the stable ABI is moved to
100        // relibc, so no flag for that!
101    }
102}
103bitflags::bitflags! {
104    #[derive(Clone, Copy, Debug)]
105    pub struct RecvFdFlags: usize {
106        /// If set, the SYS_CALL payload specifies the destination file descriptor slots, otherwise the lowest
107        /// available slots will be selected, and placed in the usize pointed to by SYS_CALL
108        /// payload.
109        const MANUAL_FD = 1;
110
111        /// If set, the file descriptors received will be placed into the *upper* file table.
112        const UPPER_TBL = 2;
113
114        /// If set, the received file descriptors are marked as close-on-exec.
115        const CLOEXEC = 4;
116    }
117}
118bitflags::bitflags! {
119    #[derive(Clone, Copy, Debug)]
120    pub struct FmoveFdFlags: usize {
121        /// If set, the kernel will enforce that the file descriptors are exclusively owned.
122        ///
123        /// That is, there will no longer exist any other reference to those FDs when removed from
124        /// the file table (SYS_CALL always removes the FDs from the file table, but without this
125        /// flag, it can be retained by SYS_DUPing them first).
126        const EXCLUSIVE = 1;
127
128        /// If set, the file descriptors will be cloned and *not* removed from the sender's file table.
129        /// By default, sendfd moves the file descriptors, removing them from the sender.
130        const CLONE = 2;
131    }
132}
133
134bitflags! {
135    pub struct MapFlags: usize {
136        // TODO: Downgrade PROT_NONE to global constant? (bitflags specifically states zero flags
137        // can cause buggy behavior).
138        const PROT_NONE = 0x0000_0000;
139
140        const PROT_EXEC = 0x0001_0000;
141        const PROT_WRITE = 0x0002_0000;
142        const PROT_READ = 0x0004_0000;
143
144        const MAP_SHARED = 0x0001;
145        const MAP_PRIVATE = 0x0002;
146
147        const MAP_FIXED = 0x0004;
148        const MAP_FIXED_NOREPLACE = 0x000C;
149
150        /// For *userspace-backed mmaps*, return from the mmap call before all pages have been
151        /// provided by the scheme. This requires the scheme to be trusted, as the current context
152        /// can block indefinitely, if the scheme does not respond to the page fault handler's
153        /// request, as it tries to map the page by requesting it from the scheme.
154        ///
155        /// In some cases however, such as the program loader, the data needs to be trusted as much
156        /// with or without MAP_LAZY, and if so, mapping lazily will not cause insecureness by
157        /// itself.
158        ///
159        /// For kernel-backed mmaps, this flag has no effect at all. It is unspecified whether
160        /// kernel mmaps are lazy or not.
161        const MAP_LAZY = 0x0010;
162    }
163}
164bitflags! {
165    pub struct MunmapFlags: usize {
166        /// Indicates whether the funmap call must implicitly do an msync, for the changes to
167        /// become visible later.
168        ///
169        /// This flag will currently be set if and only if MAP_SHARED | PROT_WRITE are set.
170        const NEEDS_SYNC = 1;
171    }
172}
173
174pub const MODE_TYPE: u16 = 0xF000;
175pub const MODE_DIR: u16 = 0x4000;
176pub const MODE_FILE: u16 = 0x8000;
177pub const MODE_SYMLINK: u16 = 0xA000;
178pub const MODE_FIFO: u16 = 0x1000;
179pub const MODE_CHR: u16 = 0x2000;
180pub const MODE_SOCK: u16 = 0xC000;
181
182pub const MODE_PERM: u16 = 0x0FFF;
183pub const MODE_SETUID: u16 = 0o4000;
184pub const MODE_SETGID: u16 = 0o2000;
185
186pub const O_RDONLY: usize = 0x0001_0000;
187pub const O_WRONLY: usize = 0x0002_0000;
188pub const O_RDWR: usize = 0x0003_0000;
189pub const O_NONBLOCK: usize = 0x0004_0000;
190pub const O_APPEND: usize = 0x0008_0000;
191pub const O_SHLOCK: usize = 0x0010_0000;
192pub const O_EXLOCK: usize = 0x0020_0000;
193pub const O_ASYNC: usize = 0x0040_0000;
194pub const O_FSYNC: usize = 0x0080_0000;
195pub const O_CLOEXEC: usize = 0x0100_0000;
196pub const O_CREAT: usize = 0x0200_0000;
197pub const O_TRUNC: usize = 0x0400_0000;
198pub const O_EXCL: usize = 0x0800_0000;
199pub const O_DIRECTORY: usize = 0x1000_0000;
200pub const O_STAT: usize = 0x2000_0000;
201pub const O_SYMLINK: usize = 0x4000_0000;
202pub const O_NOFOLLOW: usize = 0x8000_0000;
203pub const O_ACCMODE: usize = O_RDONLY | O_WRONLY | O_RDWR;
204pub const O_FCNTL_MASK: usize = O_NONBLOCK | O_APPEND | O_ASYNC | O_FSYNC;
205
206/// Remove directory instead of unlinking file.
207pub const AT_REMOVEDIR: usize = 0x200;
208
209// The top 48 bits of PTRACE_* are reserved, for now
210
211// NOT ABI STABLE!
212#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
213#[repr(usize)]
214pub enum ContextStatus {
215    Runnable,
216    Blocked,
217    NotYetStarted,
218    Dead,
219    ForceKilled,
220    Stopped,
221    UnhandledExcp,
222    #[default]
223    Other, // reserved
224}
225
226#[derive(Clone, Copy, Debug, Eq, PartialEq)]
227#[repr(usize)]
228pub enum ContextVerb {
229    Stop = 1,
230    Unstop = 2,
231    Interrupt = 3,
232    ForceKill = usize::MAX,
233}
234impl ContextVerb {
235    pub fn try_from_raw(raw: usize) -> Option<Self> {
236        Some(match raw {
237            1 => Self::Stop,
238            2 => Self::Unstop,
239            3 => Self::Interrupt,
240            usize::MAX => Self::ForceKill,
241            _ => return None,
242        })
243    }
244}
245
246// NOT ABI STABLE!
247#[derive(Clone, Copy, Debug, Eq, PartialEq)]
248#[repr(u8)]
249pub enum ProcSchemeVerb {
250    Iopl = 255,
251}
252impl ProcSchemeVerb {
253    pub fn try_from_raw(verb: u8) -> Option<Self> {
254        Some(match verb {
255            255 => Self::Iopl,
256            _ => return None,
257        })
258    }
259}
260
261#[derive(Clone, Copy, Debug, Eq, PartialEq)]
262#[repr(usize)]
263pub enum SchemeSocketCall {
264    ObtainFd = 0,
265    MoveFd = 1,
266}
267impl SchemeSocketCall {
268    pub fn try_from_raw(raw: usize) -> Option<Self> {
269        Some(match raw {
270            0 => Self::ObtainFd,
271            1 => Self::MoveFd,
272            _ => return None,
273        })
274    }
275}
276
277#[derive(Clone, Copy, Debug, Eq, PartialEq)]
278#[repr(usize)]
279#[non_exhaustive]
280pub enum FsCall {
281    Connect = 0,
282}
283impl FsCall {
284    pub fn try_from_raw(raw: usize) -> Option<Self> {
285        Some(match raw {
286            0 => Self::Connect,
287            _ => return None,
288        })
289    }
290}
291
292bitflags! {
293    pub struct PtraceFlags: u64 {
294        /// Stop before a syscall is handled. Send PTRACE_FLAG_IGNORE to not
295        /// handle the syscall.
296        const PTRACE_STOP_PRE_SYSCALL = 0x0000_0000_0000_0001;
297        /// Stop after a syscall is handled.
298        const PTRACE_STOP_POST_SYSCALL = 0x0000_0000_0000_0002;
299        /// Stop after exactly one instruction. TODO: This may not handle
300        /// fexec/signal boundaries. Should it?
301        const PTRACE_STOP_SINGLESTEP = 0x0000_0000_0000_0004;
302        /// Stop before a signal is handled. Send PTRACE_FLAG_IGNORE to not
303        /// handle signal.
304        const PTRACE_STOP_SIGNAL = 0x0000_0000_0000_0008;
305        /// Stop on a software breakpoint, such as the int3 instruction for
306        /// x86_64.
307        const PTRACE_STOP_BREAKPOINT = 0x0000_0000_0000_0010;
308        /// Stop just before exiting for good.
309        const PTRACE_STOP_EXIT = 0x0000_0000_0000_0020;
310
311        const PTRACE_STOP_MASK = 0x0000_0000_0000_00FF;
312
313
314        /// Sent when a child is cloned, giving you the opportunity to trace it.
315        /// If you don't catch this, the child is started as normal.
316        const PTRACE_EVENT_CLONE = 0x0000_0000_0000_0100;
317
318        /// Sent when current-addrspace is changed, allowing the tracer to reopen the memory file.
319        const PTRACE_EVENT_ADDRSPACE_SWITCH = 0x0000_0000_0000_0200;
320
321        const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00;
322
323        /// Special meaning, depending on the event. Usually, when fired before
324        /// an action, it will skip performing that action.
325        const PTRACE_FLAG_IGNORE = 0x0000_0000_0000_1000;
326
327        const PTRACE_FLAG_MASK = 0x0000_0000_0000_F000;
328    }
329}
330impl Deref for PtraceFlags {
331    type Target = [u8];
332    fn deref(&self) -> &Self::Target {
333        // Same as to_ne_bytes but in-place
334        unsafe {
335            slice::from_raw_parts(&self.bits() as *const _ as *const u8, mem::size_of::<u64>())
336        }
337    }
338}
339
340pub const SEEK_SET: usize = 0;
341pub const SEEK_CUR: usize = 1;
342pub const SEEK_END: usize = 2;
343
344pub const SIGCHLD: usize = 17;
345pub const SIGTSTP: usize = 20;
346pub const SIGTTIN: usize = 21;
347pub const SIGTTOU: usize = 22;
348
349pub const ADDRSPACE_OP_MMAP: usize = 0;
350pub const ADDRSPACE_OP_MUNMAP: usize = 1;
351pub const ADDRSPACE_OP_MPROTECT: usize = 2;
352pub const ADDRSPACE_OP_TRANSFER: usize = 3;
353
354bitflags! {
355    pub struct MremapFlags: usize {
356        const FIXED = 1;
357        const FIXED_REPLACE = 3;
358        /// Alias's memory region at `old_address` to `new_address` such that both regions share
359        /// the same frames.
360        const KEEP_OLD = 1 << 2;
361        // TODO: MAYMOVE, DONTUNMAP
362    }
363}
364bitflags! {
365    pub struct RwFlags: u32 {
366        const NONBLOCK = 1;
367        const APPEND = 2;
368        // TODO: sync/dsync
369        // TODO: O_DIRECT?
370    }
371}
372bitflags! {
373    pub struct SigcontrolFlags: usize {
374        /// Prevents the kernel from jumping the context to the signal trampoline, but otherwise
375        /// has absolutely no effect on which signals are blocked etc. Meant to be used for
376        /// short-lived critical sections inside libc.
377        const INHIBIT_DELIVERY = 1;
378    }
379}
380bitflags! {
381    pub struct CallFlags: usize {
382        // reserved
383        const RSVD0 = 1 << 0;
384        const RSVD1 = 1 << 1;
385        const RSVD2 = 1 << 2;
386        const RSVD3 = 1 << 3;
387        const RSVD4 = 1 << 4;
388        const RSVD5 = 1 << 5;
389        const RSVD6 = 1 << 6;
390        const RSVD7 = 1 << 7;
391
392        /// Remove the fd from the caller's file table before sending the message.
393        const CONSUME = 1 << 8;
394
395        const WRITE = 1 << 9;
396        const READ = 1 << 10;
397
398        /// Indicates the request is a bulk fd passing request.
399        const FD = 1 << 11;
400        /// Flags for the fd passing request.
401        const FD_EXCLUSIVE = 1 << 12;
402        const FD_CLONE = 1 << 13;
403        const FD_UPPER = 1 << 14;
404        const FD_CLOEXEC = 1 << 15;
405    }
406}
407
408/// The tag for the fd number in the upper file descriptor table.
409pub const UPPER_FDTBL_TAG: usize = 1 << (usize::BITS - 2);