1 use bitflags::bitflags as inner_bitflags;
2 use core::{mem, ops::Deref, slice};
3 
4 macro_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(Default)]
17             $(#[$outer])*
18             pub struct $BitFlags: $T {
19                 $(
20                     $(#[$inner $($args)*])*
21                     const $Flag = $value;
22                 )+
23             }
24         }
25 
26         // Secondly, re-export all inner constants
27         // (`pub use self::Struct::*` doesn't work)
28         $(
29             $(#[$inner $($args)*])*
30             pub const $Flag: $BitFlags = $BitFlags::$Flag;
31         )+
32     }
33 }
34 
35 bitflags! {
36     pub struct CloneFlags: usize {
37         const CLONE_VM = 0x100;
38         const CLONE_FS = 0x200;
39         const CLONE_FILES = 0x400;
40         const CLONE_SIGHAND = 0x800;
41         const CLONE_VFORK = 0x4000;
42         const CLONE_THREAD = 0x10000;
43         const CLONE_STACK = 0x1000_0000;
44     }
45 }
46 
47 pub const CLOCK_REALTIME: usize = 1;
48 pub const CLOCK_MONOTONIC: usize = 4;
49 
50 bitflags! {
51     pub struct EventFlags: usize {
52         const EVENT_NONE = 0;
53         const EVENT_READ = 1;
54         const EVENT_WRITE = 2;
55     }
56 }
57 
58 pub const F_DUPFD: usize = 0;
59 pub const F_GETFD: usize = 1;
60 pub const F_SETFD: usize = 2;
61 pub const F_GETFL: usize = 3;
62 pub const F_SETFL: usize = 4;
63 
64 pub const FUTEX_WAIT: usize = 0;
65 pub const FUTEX_WAKE: usize = 1;
66 pub const FUTEX_REQUEUE: usize = 2;
67 pub const FUTEX_WAIT64: usize = 3;
68 
69 bitflags! {
70     pub struct MapFlags: usize {
71         const PROT_NONE = 0x0000_0000;
72         const PROT_EXEC = 0x0001_0000;
73         const PROT_WRITE = 0x0002_0000;
74         const PROT_READ = 0x0004_0000;
75 
76         const MAP_SHARED = 0x0001;
77         const MAP_PRIVATE = 0x0002;
78 
79         /// Only accepted for mmap2(2).
80         const MAP_FIXED = 0x0004;
81         const MAP_FIXED_NOREPLACE = 0x000C;
82     }
83 }
84 
85 pub const MODE_TYPE: u16 = 0xF000;
86 pub const MODE_DIR: u16 = 0x4000;
87 pub const MODE_FILE: u16 = 0x8000;
88 pub const MODE_SYMLINK: u16 = 0xA000;
89 pub const MODE_FIFO: u16 = 0x1000;
90 pub const MODE_CHR: u16 = 0x2000;
91 
92 pub const MODE_PERM: u16 = 0x0FFF;
93 pub const MODE_SETUID: u16 = 0o4000;
94 pub const MODE_SETGID: u16 = 0o2000;
95 
96 pub const O_RDONLY: usize =     0x0001_0000;
97 pub const O_WRONLY: usize =     0x0002_0000;
98 pub const O_RDWR: usize =       0x0003_0000;
99 pub const O_NONBLOCK: usize =   0x0004_0000;
100 pub const O_APPEND: usize =     0x0008_0000;
101 pub const O_SHLOCK: usize =     0x0010_0000;
102 pub const O_EXLOCK: usize =     0x0020_0000;
103 pub const O_ASYNC: usize =      0x0040_0000;
104 pub const O_FSYNC: usize =      0x0080_0000;
105 pub const O_CLOEXEC: usize =    0x0100_0000;
106 pub const O_CREAT: usize =      0x0200_0000;
107 pub const O_TRUNC: usize =      0x0400_0000;
108 pub const O_EXCL: usize =       0x0800_0000;
109 pub const O_DIRECTORY: usize =  0x1000_0000;
110 pub const O_STAT: usize =       0x2000_0000;
111 pub const O_SYMLINK: usize =    0x4000_0000;
112 pub const O_NOFOLLOW: usize =   0x8000_0000;
113 pub const O_ACCMODE: usize =    O_RDONLY | O_WRONLY | O_RDWR;
114 
115 bitflags! {
116     pub struct PhysmapFlags: usize {
117         const PHYSMAP_WRITE = 0x0000_0001;
118         const PHYSMAP_WRITE_COMBINE = 0x0000_0002;
119         const PHYSMAP_NO_CACHE = 0x0000_0004;
120     }
121 }
122 bitflags! {
123     /// Extra flags for [`physalloc2`] or [`physalloc3`].
124     ///
125     /// [`physalloc2`]: ../call/fn.physalloc2.html
126     /// [`physalloc3`]: ../call/fn.physalloc3.html
127     pub struct PhysallocFlags: usize {
128         /// Only allocate memory within the 32-bit physical memory space. This is necessary for
129         /// some devices may not support 64-bit memory.
130         const SPACE_32 =        0x0000_0001;
131 
132         /// The frame that will be allocated, is going to reside anywhere in 64-bit space. This
133         /// flag is redundant for the most part, except when overriding some other default.
134         const SPACE_64 =        0x0000_0002;
135 
136         /// Do a "partial allocation", which means that not all of the frames specified in the
137         /// frame count `size` actually have to be allocated. This means that if the allocator was
138         /// unable to find a physical memory range large enough, it can instead return whatever
139         /// range it decides is optimal. Thus, instead of letting one driver get an expensive
140         /// 128MiB physical memory range when the physical memory has become fragmented, and
141         /// failing, it can instead be given a more optimal range. If the device supports
142         /// scatter-gather lists, then the driver only has to allocate more ranges, and the device
143         /// will do vectored I/O.
144         ///
145         /// PARTIAL_ALLOC supports different allocation strategies, refer to
146         /// [`Optimal`], [`GreatestRange`].
147         ///
148         /// [`Optimal`]: ./enum.PartialAllocStrategy.html
149         /// [`GreatestRange`]: ./enum.PartialAllocStrategy.html
150         const PARTIAL_ALLOC =   0x0000_0004;
151     }
152 }
153 
154 /// The bitmask of the partial allocation strategy. Currently four different strategies are
155 /// supported. If [`PARTIAL_ALLOC`] is not set, this bitmask is no longer reserved.
156 pub const PARTIAL_ALLOC_STRATEGY_MASK: usize = 0x0003_0000;
157 
158 #[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
159 #[repr(usize)]
160 pub enum PartialAllocStrategy {
161     /// The allocator decides itself the size of the memory range, based on e.g. free memory ranges
162     /// and other processes which require large physical memory chunks.
163     Optimal = 0x0001_0000,
164 
165     /// The allocator returns the absolute greatest range it can find.
166     GreatestRange = 0x0002_0000,
167 
168     /// The allocator returns the first range that fits the minimum count, without searching extra.
169     Greedy = 0x0003_0000,
170 }
171 impl Default for PartialAllocStrategy {
default() -> Self172     fn default() -> Self {
173         Self::Optimal
174     }
175 }
176 
177 impl PartialAllocStrategy {
from_raw(raw: usize) -> Option<Self>178     pub fn from_raw(raw: usize) -> Option<Self> {
179         match raw {
180             0x0001_0000 => Some(Self::Optimal),
181             0x0002_0000 => Some(Self::GreatestRange),
182             0x0003_0000 => Some(Self::Greedy),
183             _ => None,
184         }
185     }
186 }
187 
188 // The top 48 bits of PTRACE_* are reserved, for now
189 
190 bitflags! {
191     pub struct PtraceFlags: u64 {
192         /// Stop before a syscall is handled. Send PTRACE_FLAG_IGNORE to not
193         /// handle the syscall.
194         const PTRACE_STOP_PRE_SYSCALL = 0x0000_0000_0000_0001;
195         /// Stop after a syscall is handled.
196         const PTRACE_STOP_POST_SYSCALL = 0x0000_0000_0000_0002;
197         /// Stop after exactly one instruction. TODO: This may not handle
198         /// fexec/signal boundaries. Should it?
199         const PTRACE_STOP_SINGLESTEP = 0x0000_0000_0000_0004;
200         /// Stop before a signal is handled. Send PTRACE_FLAG_IGNORE to not
201         /// handle signal.
202         const PTRACE_STOP_SIGNAL = 0x0000_0000_0000_0008;
203         /// Stop on a software breakpoint, such as the int3 instruction for
204         /// x86_64.
205         const PTRACE_STOP_BREAKPOINT = 0x0000_0000_0000_0010;
206         /// Stop just before exiting for good.
207         const PTRACE_STOP_EXIT = 0x0000_0000_0000_0020;
208 
209         const PTRACE_STOP_MASK = 0x0000_0000_0000_00FF;
210 
211 
212         /// Sent when a child is cloned, giving you the opportunity to trace it.
213         /// If you don't catch this, the child is started as normal.
214         const PTRACE_EVENT_CLONE = 0x0000_0000_0000_0100;
215 
216         const PTRACE_EVENT_MASK = 0x0000_0000_0000_0F00;
217 
218 
219         /// Special meaning, depending on the event. Usually, when fired before
220         /// an action, it will skip performing that action.
221         const PTRACE_FLAG_IGNORE = 0x0000_0000_0000_1000;
222 
223         const PTRACE_FLAG_MASK = 0x0000_0000_0000_F000;
224     }
225 }
226 impl Deref for PtraceFlags {
227     type Target = [u8];
deref(&self) -> &Self::Target228     fn deref(&self) -> &Self::Target {
229         // Same as to_ne_bytes but in-place
230         unsafe {
231             slice::from_raw_parts(
232                 &self.bits as *const _ as *const u8,
233                 mem::size_of::<u64>()
234             )
235         }
236     }
237 }
238 
239 pub const SEEK_SET: usize = 0;
240 pub const SEEK_CUR: usize = 1;
241 pub const SEEK_END: usize = 2;
242 
243 pub const SIGHUP: usize =   1;
244 pub const SIGINT: usize =   2;
245 pub const SIGQUIT: usize =  3;
246 pub const SIGILL: usize =   4;
247 pub const SIGTRAP: usize =  5;
248 pub const SIGABRT: usize =  6;
249 pub const SIGBUS: usize =   7;
250 pub const SIGFPE: usize =   8;
251 pub const SIGKILL: usize =  9;
252 pub const SIGUSR1: usize =  10;
253 pub const SIGSEGV: usize =  11;
254 pub const SIGUSR2: usize =  12;
255 pub const SIGPIPE: usize =  13;
256 pub const SIGALRM: usize =  14;
257 pub const SIGTERM: usize =  15;
258 pub const SIGSTKFLT: usize= 16;
259 pub const SIGCHLD: usize =  17;
260 pub const SIGCONT: usize =  18;
261 pub const SIGSTOP: usize =  19;
262 pub const SIGTSTP: usize =  20;
263 pub const SIGTTIN: usize =  21;
264 pub const SIGTTOU: usize =  22;
265 pub const SIGURG: usize =   23;
266 pub const SIGXCPU: usize =  24;
267 pub const SIGXFSZ: usize =  25;
268 pub const SIGVTALRM: usize= 26;
269 pub const SIGPROF: usize =  27;
270 pub const SIGWINCH: usize = 28;
271 pub const SIGIO: usize =    29;
272 pub const SIGPWR: usize =   30;
273 pub const SIGSYS: usize =   31;
274 
275 pub const SIG_DFL: usize = 0;
276 pub const SIG_IGN: usize = 1;
277 
278 pub const SIG_BLOCK: usize = 0;
279 pub const SIG_UNBLOCK: usize = 1;
280 pub const SIG_SETMASK: usize = 2;
281 
282 bitflags! {
283     pub struct SigActionFlags: usize {
284         const SA_NOCLDSTOP = 0x00000001;
285         const SA_NOCLDWAIT = 0x00000002;
286         const SA_SIGINFO =   0x00000004;
287         const SA_RESTORER =  0x04000000;
288         const SA_ONSTACK =   0x08000000;
289         const SA_RESTART =   0x10000000;
290         const SA_NODEFER =   0x40000000;
291         const SA_RESETHAND = 0x80000000;
292     }
293 }
294 
295 // Auxiliery vector types
296 pub const AT_NULL: usize = 0;
297 pub const AT_PHDR: usize = 3;
298 pub const AT_PHENT: usize = 4;
299 pub const AT_PHNUM: usize = 5;
300 pub const AT_ENTRY: usize = 9;
301 
302 bitflags! {
303     pub struct WaitFlags: usize {
304         const WNOHANG =    0x01;
305         const WUNTRACED =  0x02;
306         const WCONTINUED = 0x08;
307     }
308 }
309 
310 /// True if status indicates the child is stopped.
wifstopped(status: usize) -> bool311 pub fn wifstopped(status: usize) -> bool {
312     (status & 0xff) == 0x7f
313 }
314 
315 /// If wifstopped(status), the signal that stopped the child.
wstopsig(status: usize) -> usize316 pub fn wstopsig(status: usize) -> usize {
317     (status >> 8) & 0xff
318 }
319 
320 /// True if status indicates the child continued after a stop.
wifcontinued(status: usize) -> bool321 pub fn wifcontinued(status: usize) -> bool {
322     status == 0xffff
323 }
324 
325 /// True if STATUS indicates termination by a signal.
wifsignaled(status: usize) -> bool326 pub fn wifsignaled(status: usize) -> bool {
327     ((status & 0x7f) + 1) as i8 >= 2
328 }
329 
330 /// If wifsignaled(status), the terminating signal.
wtermsig(status: usize) -> usize331 pub fn wtermsig(status: usize) -> usize {
332     status & 0x7f
333 }
334 
335 /// True if status indicates normal termination.
wifexited(status: usize) -> bool336 pub fn wifexited(status: usize) -> bool {
337     wtermsig(status) == 0
338 }
339 
340 /// If wifexited(status), the exit status.
wexitstatus(status: usize) -> usize341 pub fn wexitstatus(status: usize) -> usize {
342     (status >> 8) & 0xff
343 }
344 
345 /// True if status indicates a core dump was created.
wcoredump(status: usize) -> bool346 pub fn wcoredump(status: usize) -> bool {
347     (status & 0x80) != 0
348 }
349