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;
147
148pub const MODE_PERM: u16 = 0x0FFF;
149pub const MODE_SETUID: u16 = 0o4000;
150pub const MODE_SETGID: u16 = 0o2000;
151
152pub const O_RDONLY: usize = 0x0001_0000;
153pub const O_WRONLY: usize = 0x0002_0000;
154pub const O_RDWR: usize = 0x0003_0000;
155pub const O_NONBLOCK: usize = 0x0004_0000;
156pub const O_APPEND: usize = 0x0008_0000;
157pub const O_SHLOCK: usize = 0x0010_0000;
158pub const O_EXLOCK: usize = 0x0020_0000;
159pub const O_ASYNC: usize = 0x0040_0000;
160pub const O_FSYNC: usize = 0x0080_0000;
161pub const O_CLOEXEC: usize = 0x0100_0000;
162pub const O_CREAT: usize = 0x0200_0000;
163pub const O_TRUNC: usize = 0x0400_0000;
164pub const O_EXCL: usize = 0x0800_0000;
165pub const O_DIRECTORY: usize = 0x1000_0000;
166pub const O_STAT: usize = 0x2000_0000;
167pub const O_SYMLINK: usize = 0x4000_0000;
168pub const O_NOFOLLOW: usize = 0x8000_0000;
169pub const O_ACCMODE: usize = O_RDONLY | O_WRONLY | O_RDWR;
170
171// The top 48 bits of PTRACE_* are reserved, for now
172
173// NOT ABI STABLE!
174#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
175#[repr(usize)]
176pub enum ContextStatus {
177    Runnable,
178    Blocked,
179    NotYetStarted,
180    Dead,
181    ForceKilled,
182    Stopped,
183    UnhandledExcp,
184    #[default]
185    Other, // reserved
186}
187
188#[derive(Clone, Copy, Debug, Eq, PartialEq)]
189#[repr(usize)]
190pub enum ContextVerb {
191    Stop = 1,
192    Unstop = 2,
193    Interrupt = 3,
194    ForceKill = usize::MAX,
195}
196impl ContextVerb {
197    pub fn try_from_raw(raw: usize) -> Option<Self> {
198        Some(match raw {
199            1 => Self::Stop,
200            2 => Self::Unstop,
201            3 => Self::Interrupt,
202            usize::MAX => Self::ForceKill,
203            _ => return None,
204        })
205    }
206}
207
208// NOT ABI STABLE!
209#[derive(Clone, Copy, Debug, Eq, PartialEq)]
210#[repr(u8)]
211pub enum ProcSchemeVerb {
212    Iopl = 255,
213}
214impl ProcSchemeVerb {
215    pub fn try_from_raw(verb: u8) -> Option<Self> {
216        Some(match verb {
217            255 => Self::Iopl,
218            _ => return None,
219        })
220    }
221}
222
223bitflags! {
224    pub struct PtraceFlags: u64 {
225        /// Stop before a syscall is handled. Send PTRACE_FLAG_IGNORE to not
226        /// handle the syscall.
227        const PTRACE_STOP_PRE_SYSCALL = 0x0000_0000_0000_0001;
228        /// Stop after a syscall is handled.
229        const PTRACE_STOP_POST_SYSCALL = 0x0000_0000_0000_0002;
230        /// Stop after exactly one instruction. TODO: This may not handle
231        /// fexec/signal boundaries. Should it?
232        const PTRACE_STOP_SINGLESTEP = 0x0000_0000_0000_0004;
233        /// Stop before a signal is handled. Send PTRACE_FLAG_IGNORE to not
234        /// handle signal.
235        const PTRACE_STOP_SIGNAL = 0x0000_0000_0000_0008;
236        /// Stop on a software breakpoint, such as the int3 instruction for
237        /// x86_64.
238        const PTRACE_STOP_BREAKPOINT = 0x0000_0000_0000_0010;
239        /// Stop just before exiting for good.
240        const PTRACE_STOP_EXIT = 0x0000_0000_0000_0020;
241
242        const PTRACE_STOP_MASK = 0x0000_0000_0000_00FF;
243
244
245        /// Sent when a child is cloned, giving you the opportunity to trace it.
246        /// If you don't catch this, the child is started as normal.
247        const PTRACE_EVENT_CLONE = 0x0000_0000_0000_0100;
248
249        /// Sent when current-addrspace is changed, allowing the tracer to reopen the memory file.
250        const PTRACE_EVENT_ADDRSPACE_SWITCH = 0x0000_0000_0000_0200;
251
252        const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00;
253
254        /// Special meaning, depending on the event. Usually, when fired before
255        /// an action, it will skip performing that action.
256        const PTRACE_FLAG_IGNORE = 0x0000_0000_0000_1000;
257
258        const PTRACE_FLAG_MASK = 0x0000_0000_0000_F000;
259    }
260}
261impl Deref for PtraceFlags {
262    type Target = [u8];
263    fn deref(&self) -> &Self::Target {
264        // Same as to_ne_bytes but in-place
265        unsafe {
266            slice::from_raw_parts(&self.bits() as *const _ as *const u8, mem::size_of::<u64>())
267        }
268    }
269}
270
271pub const SEEK_SET: usize = 0;
272pub const SEEK_CUR: usize = 1;
273pub const SEEK_END: usize = 2;
274
275pub const SIGCHLD: usize = 17;
276pub const SIGTSTP: usize = 20;
277pub const SIGTTIN: usize = 21;
278pub const SIGTTOU: usize = 22;
279
280pub const ADDRSPACE_OP_MMAP: usize = 0;
281pub const ADDRSPACE_OP_MUNMAP: usize = 1;
282pub const ADDRSPACE_OP_MPROTECT: usize = 2;
283pub const ADDRSPACE_OP_TRANSFER: usize = 3;
284
285bitflags! {
286    pub struct MremapFlags: usize {
287        const FIXED = 1;
288        const FIXED_REPLACE = 3;
289        /// Alias's memory region at `old_address` to `new_address` such that both regions share
290        /// the same frames.
291        const KEEP_OLD = 1 << 2;
292        // TODO: MAYMOVE, DONTUNMAP
293    }
294}
295bitflags! {
296    pub struct RwFlags: u32 {
297        const NONBLOCK = 1;
298        const APPEND = 2;
299        // TODO: sync/dsync
300        // TODO: O_DIRECT?
301    }
302}
303bitflags! {
304    pub struct SigcontrolFlags: usize {
305        /// Prevents the kernel from jumping the context to the signal trampoline, but otherwise
306        /// has absolutely no effect on which signals are blocked etc. Meant to be used for
307        /// short-lived critical sections inside libc.
308        const INHIBIT_DELIVERY = 1;
309    }
310}
311bitflags! {
312    pub struct CallFlags: usize {
313        // reserved
314        const RSVD0 = 1 << 0;
315        const RSVD1 = 1 << 1;
316        const RSVD2 = 1 << 2;
317        const RSVD3 = 1 << 3;
318        const RSVD4 = 1 << 4;
319        const RSVD5 = 1 << 5;
320        const RSVD6 = 1 << 6;
321        const RSVD7 = 1 << 7;
322
323        /// Remove the fd from the caller's file table before sending the message.
324        const CONSUME = 1 << 8;
325    }
326}