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