Skip to main content

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// TODO: Split SendFdFlags into caller flags and flags that the scheme receives?
65bitflags::bitflags! {
66    #[derive(Clone, Copy, Debug)]
67    pub struct SendFdFlags: usize {
68        /// If set, the kernel will enforce that the file descriptors are exclusively owned.
69        ///
70        /// That is, there will no longer exist any other reference to those FDs when removed from
71        /// the file table (sendfd always removes the FDs from the file table, but without this
72        /// flag, it can be retained by SYS_DUPing them first).
73        const EXCLUSIVE = 1;
74
75        /// If set, the file descriptors will be cloned and *not* removed from the sender's file table.
76        /// By default, `SYS_SENDFD` moves the file descriptors, removing them from the sender.
77        const CLONE = 2;
78    }
79}
80bitflags::bitflags! {
81    #[derive(Clone, Copy, Debug)]
82    pub struct FobtainFdFlags: usize {
83        /// If set, the SYS_CALL payload specifies the destination file descriptor slots, otherwise the lowest
84        /// available slots will be selected, and placed in the usize pointed to by SYS_CALL
85        /// payload.
86        const MANUAL_FD = 1;
87
88        /// If set, the file descriptors received are guaranteed to be exclusively owned (by the file
89        /// table the obtainer is running in).
90        const EXCLUSIVE = 2;
91
92        /// If set, the file descriptors received will be placed into the *upper* file table.
93        const UPPER_TBL = 4;
94
95        /// If set, the received file descriptors are marked as close-on-exec.
96        const CLOEXEC = 8;
97
98        // No, cloexec won't be stored in the kernel in the future, when the stable ABI is moved to
99        // relibc, so no flag for that!
100    }
101}
102bitflags::bitflags! {
103    #[derive(Clone, Copy, Debug)]
104    pub struct RecvFdFlags: usize {
105        /// If set, the SYS_CALL payload specifies the destination file descriptor slots, otherwise the lowest
106        /// available slots will be selected, and placed in the usize pointed to by SYS_CALL
107        /// payload.
108        const MANUAL_FD = 1;
109
110        /// If set, the file descriptors received will be placed into the *upper* file table.
111        const UPPER_TBL = 2;
112
113        /// If set, the received file descriptors are marked as close-on-exec.
114        const CLOEXEC = 4;
115    }
116}
117bitflags::bitflags! {
118    #[derive(Clone, Copy, Debug)]
119    pub struct FmoveFdFlags: usize {
120        /// If set, the kernel will enforce that the file descriptors are exclusively owned.
121        ///
122        /// That is, there will no longer exist any other reference to those FDs when removed from
123        /// the file table (SYS_CALL always removes the FDs from the file table, but without this
124        /// flag, it can be retained by SYS_DUPing them first).
125        const EXCLUSIVE = 1;
126
127        /// If set, the file descriptors will be cloned and *not* removed from the sender's file table.
128        /// By default, sendfd moves the file descriptors, removing them from the sender.
129        const CLONE = 2;
130    }
131}
132
133bitflags! {
134    pub struct MapFlags: usize {
135        // TODO: Downgrade PROT_NONE to global constant? (bitflags specifically states zero flags
136        // can cause buggy behavior).
137        const PROT_NONE = 0x0000_0000;
138
139        const PROT_EXEC = 0x0001_0000;
140        const PROT_WRITE = 0x0002_0000;
141        const PROT_READ = 0x0004_0000;
142
143        const MAP_SHARED = 0x0001;
144        const MAP_PRIVATE = 0x0002;
145
146        const MAP_FIXED = 0x0004;
147        const MAP_FIXED_NOREPLACE = 0x000C;
148
149        /// For *userspace-backed mmaps*, return from the mmap call before all pages have been
150        /// provided by the scheme. This requires the scheme to be trusted, as the current context
151        /// can block indefinitely, if the scheme does not respond to the page fault handler's
152        /// request, as it tries to map the page by requesting it from the scheme.
153        ///
154        /// In some cases however, such as the program loader, the data needs to be trusted as much
155        /// with or without MAP_LAZY, and if so, mapping lazily will not cause insecureness by
156        /// itself.
157        ///
158        /// For kernel-backed mmaps, this flag has no effect at all. It is unspecified whether
159        /// kernel mmaps are lazy or not.
160        const MAP_LAZY = 0x0010;
161    }
162}
163bitflags! {
164    pub struct MunmapFlags: usize {
165        /// Indicates whether the funmap call must implicitly do an msync, for the changes to
166        /// become visible later.
167        ///
168        /// This flag will currently be set if and only if MAP_SHARED | PROT_WRITE are set.
169        const NEEDS_SYNC = 1;
170    }
171}
172
173pub const MODE_TYPE: u16 = 0xF000;
174pub const MODE_DIR: u16 = 0x4000;
175pub const MODE_FILE: u16 = 0x8000;
176pub const MODE_SYMLINK: u16 = 0xA000;
177pub const MODE_FIFO: u16 = 0x1000;
178pub const MODE_CHR: u16 = 0x2000;
179pub const MODE_SOCK: u16 = 0xC000;
180
181pub const MODE_PERM: u16 = 0x0FFF;
182pub const MODE_SETUID: u16 = 0o4000;
183pub const MODE_SETGID: u16 = 0o2000;
184
185pub const O_RDONLY: usize = 0x0001_0000;
186pub const O_WRONLY: usize = 0x0002_0000;
187pub const O_RDWR: usize = 0x0003_0000;
188pub const O_NONBLOCK: usize = 0x0004_0000;
189pub const O_APPEND: usize = 0x0008_0000;
190pub const O_SHLOCK: usize = 0x0010_0000;
191pub const O_EXLOCK: usize = 0x0020_0000;
192pub const O_ASYNC: usize = 0x0040_0000;
193pub const O_FSYNC: usize = 0x0080_0000;
194pub const O_CREAT: usize = 0x0200_0000;
195pub const O_TRUNC: usize = 0x0400_0000;
196pub const O_EXCL: usize = 0x0800_0000;
197pub const O_DIRECTORY: usize = 0x1000_0000;
198pub const O_STAT: usize = 0x2000_0000;
199pub const O_SYMLINK: usize = 0x4000_0000;
200pub const O_NOFOLLOW: usize = 0x8000_0000;
201pub const O_ACCMODE: usize = O_RDONLY | O_WRONLY | O_RDWR;
202pub const O_FCNTL_MASK: usize = O_NONBLOCK | O_APPEND | O_ASYNC | O_FSYNC;
203
204/// Remove directory instead of unlinking file.
205pub const AT_REMOVEDIR: usize = 0x200;
206
207// The top 48 bits of PTRACE_* are reserved, for now
208
209// NOT ABI STABLE!
210#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
211#[repr(usize)]
212pub enum ContextStatus {
213    Runnable,
214    Blocked,
215    NotYetStarted,
216    Dead,
217    ForceKilled,
218    Stopped,
219    UnhandledExcp,
220    #[default]
221    Other, // reserved
222}
223
224#[derive(Clone, Copy, Debug, Eq, PartialEq)]
225#[repr(usize)]
226pub enum ContextVerb {
227    Stop = 1,
228    Unstop = 2,
229    Interrupt = 3,
230    ForceKill = usize::MAX,
231}
232impl ContextVerb {
233    pub fn try_from_raw(raw: usize) -> Option<Self> {
234        Some(match raw {
235            1 => Self::Stop,
236            2 => Self::Unstop,
237            3 => Self::Interrupt,
238            usize::MAX => Self::ForceKill,
239            _ => return None,
240        })
241    }
242}
243
244// NOT ABI STABLE!
245#[derive(Clone, Copy, Debug, Eq, PartialEq)]
246#[repr(u8)]
247pub enum AddrSpaceVerb {
248    MmapMin = 255,
249}
250impl AddrSpaceVerb {
251    pub fn try_from_raw(verb: u8) -> Option<Self> {
252        Some(match verb {
253            255 => Self::MmapMin,
254
255            _ => return None,
256        })
257    }
258}
259
260// NOT ABI STABLE!
261#[derive(Clone, Copy, Debug, Eq, PartialEq)]
262#[repr(u8)]
263pub enum ProcSchemeVerb {
264    RegsInt = 250,
265    RegsFloat = 251,
266    RegsEnv = 252,
267    SchedAffinity = 253,
268    Start = 254,
269    Iopl = 255,
270}
271impl ProcSchemeVerb {
272    pub fn try_from_raw(verb: u8) -> Option<Self> {
273        Some(match verb {
274            250 => Self::RegsInt,
275            251 => Self::RegsFloat,
276            252 => Self::RegsEnv,
277            253 => Self::SchedAffinity,
278            254 => Self::Start,
279            255 => Self::Iopl,
280            _ => return None,
281        })
282    }
283}
284
285#[derive(Clone, Copy, Debug, Eq, PartialEq)]
286pub enum FileTableVerb {
287    Close = 1,
288    Dup2 = 2,
289    Reserved1 = 3,
290    Resize = 4,
291}
292impl FileTableVerb {
293    pub fn try_from_raw(value: u8) -> Option<Self> {
294        Some(match value {
295            1 => Self::Close,
296            2 => Self::Dup2,
297            3 => Self::Reserved1,
298            4 => Self::Resize,
299            _ => return None,
300        })
301    }
302}
303
304// NOT ABI-STABLE!
305#[derive(Clone, Copy, Debug, Eq, PartialEq)]
306#[repr(u64)]
307pub enum AcpiVerb {
308    // copies the rsdt/xsdt to the payload buffer (the number of bytes that fit), and returns the
309    // rsdt/xsdt length regardless
310    ReadRxsdt = 1,
311    // no payload, just returns 0 or 1
312    CheckShutdown = 2,
313}
314impl AcpiVerb {
315    pub const fn try_from_raw(value: u64) -> Option<Self> {
316        Some(match value {
317            1 => Self::ReadRxsdt,
318            2 => Self::CheckShutdown,
319            _ => return None,
320        })
321    }
322}
323
324#[derive(Clone, Copy, Debug, Eq, PartialEq)]
325pub enum NumaVerb {
326    SetMemPolicy = 1,
327    GetMemPolicy = 2,
328}
329impl NumaVerb {
330    pub fn try_from_raw(value: u64) -> Option<Self> {
331        Some(match value {
332            1 => Self::SetMemPolicy,
333            2 => Self::GetMemPolicy,
334            _ => return None,
335        })
336    }
337}
338
339#[derive(Clone, Copy, Debug, Eq, PartialEq)]
340#[repr(usize)]
341pub enum SchemeSocketCall {
342    ObtainFd = 0,
343    MoveFd = 1,
344}
345impl SchemeSocketCall {
346    pub fn try_from_raw(raw: usize) -> Option<Self> {
347        Some(match raw {
348            0 => Self::ObtainFd,
349            1 => Self::MoveFd,
350            _ => return None,
351        })
352    }
353}
354
355#[derive(Clone, Copy, Debug, Eq, PartialEq)]
356#[repr(usize)]
357#[non_exhaustive]
358pub enum FsCall {
359    Connect = 0,
360}
361impl FsCall {
362    pub fn try_from_raw(raw: usize) -> Option<Self> {
363        Some(match raw {
364            0 => Self::Connect,
365            _ => return None,
366        })
367    }
368}
369
370bitflags! {
371    pub struct PtraceFlags: u64 {
372        /// Stop before a syscall is handled. Send PTRACE_FLAG_IGNORE to not
373        /// handle the syscall.
374        const PTRACE_STOP_PRE_SYSCALL = 0x0000_0000_0000_0001;
375        /// Stop after a syscall is handled.
376        const PTRACE_STOP_POST_SYSCALL = 0x0000_0000_0000_0002;
377        /// Stop after exactly one instruction. TODO: This may not handle
378        /// fexec/signal boundaries. Should it?
379        const PTRACE_STOP_SINGLESTEP = 0x0000_0000_0000_0004;
380        /// Stop before a signal is handled. Send PTRACE_FLAG_IGNORE to not
381        /// handle signal.
382        const PTRACE_STOP_SIGNAL = 0x0000_0000_0000_0008;
383        /// Stop on a software breakpoint, such as the int3 instruction for
384        /// x86_64.
385        const PTRACE_STOP_BREAKPOINT = 0x0000_0000_0000_0010;
386        /// Stop just before exiting for good.
387        const PTRACE_STOP_EXIT = 0x0000_0000_0000_0020;
388
389        const PTRACE_STOP_MASK = 0x0000_0000_0000_00FF;
390
391
392        /// Sent when a child is cloned, giving you the opportunity to trace it.
393        /// If you don't catch this, the child is started as normal.
394        const PTRACE_EVENT_CLONE = 0x0000_0000_0000_0100;
395
396        /// Sent when current-addrspace is changed, allowing the tracer to reopen the memory file.
397        const PTRACE_EVENT_ADDRSPACE_SWITCH = 0x0000_0000_0000_0200;
398
399        const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00;
400
401        /// Special meaning, depending on the event. Usually, when fired before
402        /// an action, it will skip performing that action.
403        const PTRACE_FLAG_IGNORE = 0x0000_0000_0000_1000;
404
405        const PTRACE_FLAG_MASK = 0x0000_0000_0000_F000;
406    }
407}
408impl Deref for PtraceFlags {
409    type Target = [u8];
410    fn deref(&self) -> &Self::Target {
411        // Same as to_ne_bytes but in-place
412        unsafe {
413            slice::from_raw_parts(&self.bits() as *const _ as *const u8, mem::size_of::<u64>())
414        }
415    }
416}
417
418pub const SEEK_SET: usize = 0;
419pub const SEEK_CUR: usize = 1;
420pub const SEEK_END: usize = 2;
421
422pub const SIGCHLD: usize = 17;
423pub const SIGTSTP: usize = 20;
424pub const SIGTTIN: usize = 21;
425pub const SIGTTOU: usize = 22;
426
427pub const ADDRSPACE_OP_MMAP: usize = 0;
428pub const ADDRSPACE_OP_MUNMAP: usize = 1;
429pub const ADDRSPACE_OP_MPROTECT: usize = 2;
430pub const ADDRSPACE_OP_TRANSFER: usize = 3;
431
432bitflags! {
433    pub struct MremapFlags: usize {
434        const FIXED = 1;
435        const FIXED_REPLACE = 3;
436        /// Alias's memory region at `old_address` to `new_address` such that both regions share
437        /// the same frames.
438        const KEEP_OLD = 1 << 2;
439        // TODO: MAYMOVE, DONTUNMAP
440    }
441}
442bitflags! {
443    pub struct RwFlags: u32 {
444        const NONBLOCK = 1;
445        const APPEND = 2;
446        // TODO: sync/dsync
447        // TODO: O_DIRECT?
448    }
449}
450bitflags! {
451    pub struct SigcontrolFlags: usize {
452        /// Prevents the kernel from jumping the context to the signal trampoline, but otherwise
453        /// has absolutely no effect on which signals are blocked etc. Meant to be used for
454        /// short-lived critical sections inside libc.
455        const INHIBIT_DELIVERY = 1;
456    }
457}
458bitflags! {
459    pub struct CallFlags: usize {
460        // reserved
461        const RSVD0 = 1 << 0;
462        const RSVD1 = 1 << 1;
463        const RSVD2 = 1 << 2;
464        const RSVD3 = 1 << 3;
465        const RSVD4 = 1 << 4;
466        const RSVD5 = 1 << 5;
467        const RSVD6 = 1 << 6;
468        const RSVD7 = 1 << 7;
469
470        /// Remove the fd from the caller's file table before sending the message.
471        const CONSUME = 1 << 8;
472
473        const WRITE = 1 << 9;
474        const READ = 1 << 10;
475
476        /// Indicates the request is a bulk fd passing request.
477        const FD = 1 << 11;
478        /// Flags for the fd passing request.
479        const FD_EXCLUSIVE = 1 << 12;
480        const FD_CLONE = 1 << 13;
481        const FD_UPPER = 1 << 14;
482        const FD_CLOEXEC = 1 << 15;
483
484        /// Call is a standard fs call, with metadata defined in `StdFsCallMeta`
485        const STD_FS = 1 << 16;
486
487        /// Call is taking multiple fds as an argument
488        const MULTIPLE_FDS = 1 << 17;
489    }
490}
491
492#[repr(u8)]
493#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
494pub enum StdFsCallKind {
495    // TODO: remove old syscalls
496    Fchmod = 1,
497    Fchown = 2,
498    Getdents = 3,
499    Fstat = 4,
500    Fstatvfs = 5,
501    Fsync = 6,
502    Ftruncate = 7,
503    Futimens = 8,
504    // 9 reserved in fscall RFC
505    // Unlinkat = 10,
506    Relpathat = 11,
507    Lock = 12,
508    Unlock = 13,
509    GetLock = 14,
510}
511
512impl StdFsCallKind {
513    pub fn try_from_raw(raw: u8) -> Option<Self> {
514        use StdFsCallKind::*;
515
516        // TODO: Use a library where this match can be automated.
517        Some(match raw {
518            1 => Fchmod,
519            2 => Fchown,
520            3 => Getdents,
521            4 => Fstat,
522            5 => Fstatvfs,
523            6 => Fsync,
524            7 => Ftruncate,
525            8 => Futimens,
526            // 9 reserved in fscall RFC
527            // 10 => Unlinkat,
528            11 => Relpathat,
529            12 => Lock,
530            13 => Unlock,
531            14 => GetLock,
532            _ => return None,
533        })
534    }
535}
536
537/// The tag for the fd number in the upper file descriptor table.
538pub const UPPER_FDTBL_TAG: usize = 1 << (usize::BITS - 2);
539
540/// The identifier for registering event timeout
541pub const EVENT_TIMEOUT_ID: usize = usize::MAX - 2;