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
173bitflags! {
174    pub struct PtraceFlags: u64 {
175        /// Stop before a syscall is handled. Send PTRACE_FLAG_IGNORE to not
176        /// handle the syscall.
177        const PTRACE_STOP_PRE_SYSCALL = 0x0000_0000_0000_0001;
178        /// Stop after a syscall is handled.
179        const PTRACE_STOP_POST_SYSCALL = 0x0000_0000_0000_0002;
180        /// Stop after exactly one instruction. TODO: This may not handle
181        /// fexec/signal boundaries. Should it?
182        const PTRACE_STOP_SINGLESTEP = 0x0000_0000_0000_0004;
183        /// Stop before a signal is handled. Send PTRACE_FLAG_IGNORE to not
184        /// handle signal.
185        const PTRACE_STOP_SIGNAL = 0x0000_0000_0000_0008;
186        /// Stop on a software breakpoint, such as the int3 instruction for
187        /// x86_64.
188        const PTRACE_STOP_BREAKPOINT = 0x0000_0000_0000_0010;
189        /// Stop just before exiting for good.
190        const PTRACE_STOP_EXIT = 0x0000_0000_0000_0020;
191
192        const PTRACE_STOP_MASK = 0x0000_0000_0000_00FF;
193
194
195        /// Sent when a child is cloned, giving you the opportunity to trace it.
196        /// If you don't catch this, the child is started as normal.
197        const PTRACE_EVENT_CLONE = 0x0000_0000_0000_0100;
198
199        /// Sent when current-addrspace is changed, allowing the tracer to reopen the memory file.
200        const PTRACE_EVENT_ADDRSPACE_SWITCH = 0x0000_0000_0000_0200;
201
202        const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00;
203
204        /// Special meaning, depending on the event. Usually, when fired before
205        /// an action, it will skip performing that action.
206        const PTRACE_FLAG_IGNORE = 0x0000_0000_0000_1000;
207
208        const PTRACE_FLAG_MASK = 0x0000_0000_0000_F000;
209    }
210}
211impl Deref for PtraceFlags {
212    type Target = [u8];
213    fn deref(&self) -> &Self::Target {
214        // Same as to_ne_bytes but in-place
215        unsafe {
216            slice::from_raw_parts(&self.bits() as *const _ as *const u8, mem::size_of::<u64>())
217        }
218    }
219}
220
221pub const SEEK_SET: usize = 0;
222pub const SEEK_CUR: usize = 1;
223pub const SEEK_END: usize = 2;
224
225pub const SIGHUP: usize = 1;
226pub const SIGINT: usize = 2;
227pub const SIGQUIT: usize = 3;
228pub const SIGILL: usize = 4;
229pub const SIGTRAP: usize = 5;
230pub const SIGABRT: usize = 6;
231pub const SIGBUS: usize = 7;
232pub const SIGFPE: usize = 8;
233pub const SIGKILL: usize = 9;
234pub const SIGUSR1: usize = 10;
235pub const SIGSEGV: usize = 11;
236pub const SIGUSR2: usize = 12;
237pub const SIGPIPE: usize = 13;
238pub const SIGALRM: usize = 14;
239pub const SIGTERM: usize = 15;
240pub const SIGSTKFLT: usize = 16;
241pub const SIGCHLD: usize = 17;
242pub const SIGCONT: usize = 18;
243pub const SIGSTOP: usize = 19;
244pub const SIGTSTP: usize = 20;
245pub const SIGTTIN: usize = 21;
246pub const SIGTTOU: usize = 22;
247pub const SIGURG: usize = 23;
248pub const SIGXCPU: usize = 24;
249pub const SIGXFSZ: usize = 25;
250pub const SIGVTALRM: usize = 26;
251pub const SIGPROF: usize = 27;
252pub const SIGWINCH: usize = 28;
253pub const SIGIO: usize = 29;
254pub const SIGPWR: usize = 30;
255pub const SIGSYS: usize = 31;
256
257bitflags! {
258    pub struct WaitFlags: usize {
259        const WNOHANG =    0x01;
260        const WUNTRACED =  0x02;
261        const WCONTINUED = 0x08;
262    }
263}
264
265pub const ADDRSPACE_OP_MMAP: usize = 0;
266pub const ADDRSPACE_OP_MUNMAP: usize = 1;
267pub const ADDRSPACE_OP_MPROTECT: usize = 2;
268pub const ADDRSPACE_OP_TRANSFER: usize = 3;
269
270/// True if status indicates the child is stopped.
271pub fn wifstopped(status: usize) -> bool {
272    (status & 0xff) == 0x7f
273}
274
275/// If wifstopped(status), the signal that stopped the child.
276pub fn wstopsig(status: usize) -> usize {
277    (status >> 8) & 0xff
278}
279
280/// True if status indicates the child continued after a stop.
281pub fn wifcontinued(status: usize) -> bool {
282    status == 0xffff
283}
284
285/// True if STATUS indicates termination by a signal.
286pub fn wifsignaled(status: usize) -> bool {
287    ((status & 0x7f) + 1) as i8 >= 2
288}
289
290/// If wifsignaled(status), the terminating signal.
291pub fn wtermsig(status: usize) -> usize {
292    status & 0x7f
293}
294
295/// True if status indicates normal termination.
296pub fn wifexited(status: usize) -> bool {
297    wtermsig(status) == 0
298}
299
300/// If wifexited(status), the exit status.
301pub fn wexitstatus(status: usize) -> usize {
302    (status >> 8) & 0xff
303}
304
305/// True if status indicates a core dump was created.
306pub fn wcoredump(status: usize) -> bool {
307    (status & 0x80) != 0
308}
309
310bitflags! {
311    pub struct MremapFlags: usize {
312        const FIXED = 1;
313        const FIXED_REPLACE = 3;
314        /// Alias's memory region at `old_address` to `new_address` such that both regions share
315        /// the same frames.
316        const KEEP_OLD = 1 << 2;
317        // TODO: MAYMOVE, DONTUNMAP
318    }
319}
320bitflags! {
321    pub struct RwFlags: u32 {
322        const NONBLOCK = 1;
323        const APPEND = 2;
324        // TODO: sync/dsync
325        // TODO: O_DIRECT?
326    }
327}
328bitflags! {
329    pub struct SigcontrolFlags: usize {
330        /// Prevents the kernel from jumping the context to the signal trampoline, but otherwise
331        /// has absolutely no effect on which signals are blocked etc. Meant to be used for
332        /// short-lived critical sections inside libc.
333        const INHIBIT_DELIVERY = 1;
334    }
335}