1 use crate::backend::c;
2 use bitflags::bitflags;
3 
4 bitflags! {
5     /// `PROT_*` flags for use with [`mmap`].
6     ///
7     /// For `PROT_NONE`, use `ProtFlags::empty()`.
8     ///
9     /// [`mmap`]: crate::mm::mmap
10     #[repr(transparent)]
11     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
12     pub struct ProtFlags: u32 {
13         /// `PROT_READ`
14         const READ = bitcast!(c::PROT_READ);
15         /// `PROT_WRITE`
16         const WRITE = bitcast!(c::PROT_WRITE);
17         /// `PROT_EXEC`
18         const EXEC = bitcast!(c::PROT_EXEC);
19 
20         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
21         const _ = !0;
22     }
23 }
24 
25 bitflags! {
26     /// `PROT_*` flags for use with [`mprotect`].
27     ///
28     /// For `PROT_NONE`, use `MprotectFlags::empty()`.
29     ///
30     /// [`mprotect`]: crate::mm::mprotect
31     #[repr(transparent)]
32     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
33     pub struct MprotectFlags: u32 {
34         /// `PROT_READ`
35         const READ = bitcast!(c::PROT_READ);
36         /// `PROT_WRITE`
37         const WRITE = bitcast!(c::PROT_WRITE);
38         /// `PROT_EXEC`
39         const EXEC = bitcast!(c::PROT_EXEC);
40         /// `PROT_GROWSUP`
41         #[cfg(linux_kernel)]
42         const GROWSUP = bitcast!(c::PROT_GROWSUP);
43         /// `PROT_GROWSDOWN`
44         #[cfg(linux_kernel)]
45         const GROWSDOWN = bitcast!(c::PROT_GROWSDOWN);
46         /// `PROT_SEM`
47         #[cfg(feature = "linux-raw-sys")]
48         const SEM = linux_raw_sys::general::PROT_SEM;
49         /// `PROT_BTI`
50         #[cfg(all(feature = "linux-raw-sys", target_arch = "aarch64"))]
51         const BTI = linux_raw_sys::general::PROT_BTI;
52         /// `PROT_MTE`
53         #[cfg(all(feature = "linux-raw-sys", target_arch = "aarch64"))]
54         const MTE = linux_raw_sys::general::PROT_MTE;
55         /// `PROT_SAO`
56         #[cfg(all(feature = "linux-raw-sys", any(target_arch = "powerpc", target_arch = "powerpc64")))]
57         const SAO = linux_raw_sys::general::PROT_SAO;
58         /// `PROT_ADI`
59         #[cfg(all(feature = "linux-raw-sys", any(target_arch = "sparc", target_arch = "sparc64")))]
60         const ADI = linux_raw_sys::general::PROT_ADI;
61 
62         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
63         const _ = !0;
64     }
65 }
66 
67 bitflags! {
68     /// `MAP_*` flags for use with [`mmap`].
69     ///
70     /// For `MAP_ANONYMOUS` (aka `MAP_ANON`), see [`mmap_anonymous`].
71     ///
72     /// [`mmap`]: crate::mm::mmap
73     /// [`mmap_anonymous`]: crates::mm::mmap_anonymous
74     #[repr(transparent)]
75     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
76     pub struct MapFlags: u32 {
77         /// `MAP_SHARED`
78         const SHARED = bitcast!(c::MAP_SHARED);
79         /// `MAP_SHARED_VALIDATE`
80         #[cfg(not(any(
81             bsd,
82             solarish,
83             target_os = "aix",
84             target_os = "android",
85             target_os = "emscripten",
86             target_os = "fuchsia",
87             target_os = "haiku",
88             target_os = "nto",
89             target_os = "redox",
90         )))]
91         const SHARED_VALIDATE = bitcast!(c::MAP_SHARED_VALIDATE);
92         /// `MAP_PRIVATE`
93         const PRIVATE = bitcast!(c::MAP_PRIVATE);
94         /// `MAP_DENYWRITE`
95         #[cfg(not(any(
96             bsd,
97             solarish,
98             target_os = "aix",
99             target_os = "haiku",
100             target_os = "nto",
101             target_os = "redox",
102         )))]
103         const DENYWRITE = bitcast!(c::MAP_DENYWRITE);
104         /// `MAP_FIXED`
105         const FIXED = bitcast!(c::MAP_FIXED);
106         /// `MAP_FIXED_NOREPLACE`
107         #[cfg(not(any(
108             bsd,
109             solarish,
110             target_os = "aix",
111             target_os = "android",
112             target_os = "emscripten",
113             target_os = "fuchsia",
114             target_os = "haiku",
115             target_os = "nto",
116             target_os = "redox",
117         )))]
118         const FIXED_NOREPLACE = bitcast!(c::MAP_FIXED_NOREPLACE);
119         /// `MAP_GROWSDOWN`
120         #[cfg(not(any(
121             bsd,
122             solarish,
123             target_os = "aix",
124             target_os = "haiku",
125             target_os = "nto",
126             target_os = "redox",
127         )))]
128         const GROWSDOWN = bitcast!(c::MAP_GROWSDOWN);
129         /// `MAP_HUGETLB`
130         #[cfg(not(any(
131             bsd,
132             solarish,
133             target_os = "aix",
134             target_os = "haiku",
135             target_os = "nto",
136             target_os = "redox",
137         )))]
138         const HUGETLB = bitcast!(c::MAP_HUGETLB);
139         /// `MAP_HUGE_2MB`
140         #[cfg(not(any(
141             bsd,
142             solarish,
143             target_os = "aix",
144             target_os = "android",
145             target_os = "emscripten",
146             target_os = "fuchsia",
147             target_os = "haiku",
148             target_os = "nto",
149             target_os = "redox",
150         )))]
151         const HUGE_2MB = bitcast!(c::MAP_HUGE_2MB);
152         /// `MAP_HUGE_1GB`
153         #[cfg(not(any(
154             bsd,
155             solarish,
156             target_os = "aix",
157             target_os = "android",
158             target_os = "emscripten",
159             target_os = "fuchsia",
160             target_os = "haiku",
161             target_os = "nto",
162             target_os = "redox",
163         )))]
164         const HUGE_1GB = bitcast!(c::MAP_HUGE_1GB);
165         /// `MAP_LOCKED`
166         #[cfg(not(any(
167             bsd,
168             solarish,
169             target_os = "aix",
170             target_os = "haiku",
171             target_os = "nto",
172             target_os = "redox",
173         )))]
174         const LOCKED = bitcast!(c::MAP_LOCKED);
175         /// `MAP_NOCORE`
176         #[cfg(freebsdlike)]
177         const NOCORE = bitcast!(c::MAP_NOCORE);
178         /// `MAP_NORESERVE`
179         #[cfg(not(any(
180             freebsdlike,
181             target_os = "aix",
182             target_os = "nto",
183             target_os = "redox",
184         )))]
185         const NORESERVE = bitcast!(c::MAP_NORESERVE);
186         /// `MAP_NOSYNC`
187         #[cfg(freebsdlike)]
188         const NOSYNC = bitcast!(c::MAP_NOSYNC);
189         /// `MAP_POPULATE`
190         #[cfg(not(any(
191             bsd,
192             solarish,
193             target_os = "aix",
194             target_os = "haiku",
195             target_os = "nto",
196             target_os = "redox",
197         )))]
198         const POPULATE = bitcast!(c::MAP_POPULATE);
199         /// `MAP_STACK`
200         #[cfg(not(any(
201             apple,
202             solarish,
203             target_os = "aix",
204             target_os = "dragonfly",
205             target_os = "haiku",
206             target_os = "netbsd",
207             target_os = "redox",
208         )))]
209         const STACK = bitcast!(c::MAP_STACK);
210         /// `MAP_PREFAULT_READ`
211         #[cfg(target_os = "freebsd")]
212         const PREFAULT_READ = bitcast!(c::MAP_PREFAULT_READ);
213         /// `MAP_SYNC`
214         #[cfg(not(any(
215             bsd,
216             solarish,
217             target_os = "aix",
218             target_os = "android",
219             target_os = "emscripten",
220             target_os = "fuchsia",
221             target_os = "haiku",
222             target_os = "nto",
223             target_os = "redox",
224             all(
225                 linux_kernel,
226                 any(target_arch = "mips", target_arch = "mips32r6", target_arch = "mips64", target_arch = "mips64r6"),
227             )
228         )))]
229         const SYNC = bitcast!(c::MAP_SYNC);
230         /// `MAP_UNINITIALIZED`
231         #[cfg(any())]
232         const UNINITIALIZED = bitcast!(c::MAP_UNINITIALIZED);
233 
234         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
235         const _ = !0;
236     }
237 }
238 
239 #[cfg(any(target_os = "emscripten", target_os = "linux"))]
240 bitflags! {
241     /// `MREMAP_*` flags for use with [`mremap`].
242     ///
243     /// For `MREMAP_FIXED`, see [`mremap_fixed`].
244     ///
245     /// [`mremap`]: crate::mm::mremap
246     /// [`mremap_fixed`]: crate::mm::mremap_fixed
247     #[repr(transparent)]
248     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
249     pub struct MremapFlags: u32 {
250         /// `MREMAP_MAYMOVE`
251         const MAYMOVE = bitcast!(c::MREMAP_MAYMOVE);
252 
253         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
254         const _ = !0;
255     }
256 }
257 
258 bitflags! {
259     /// `MS_*` flags for use with [`msync`].
260     ///
261     /// [`msync`]: crate::mm::msync
262     #[repr(transparent)]
263     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
264     pub struct MsyncFlags: u32 {
265         /// `MS_SYNC`—Requests an update and waits for it to complete.
266         const SYNC = bitcast!(c::MS_SYNC);
267         /// `MS_ASYNC`—Specifies that an update be scheduled, but the call
268         /// returns immediately.
269         const ASYNC = bitcast!(c::MS_ASYNC);
270         /// `MS_INVALIDATE`—Asks to invalidate other mappings of the same
271         /// file (so that they can be updated with the fresh values just
272         /// written).
273         const INVALIDATE = bitcast!(c::MS_INVALIDATE);
274 
275         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
276         const _ = !0;
277     }
278 }
279 
280 #[cfg(linux_kernel)]
281 bitflags! {
282     /// `MLOCK_*` flags for use with [`mlock_with`].
283     ///
284     /// [`mlock_with`]: crate::mm::mlock_with
285     #[repr(transparent)]
286     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
287     pub struct MlockFlags: u32 {
288         /// `MLOCK_ONFAULT`
289         const ONFAULT = bitcast!(c::MLOCK_ONFAULT);
290 
291         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
292         const _ = !0;
293     }
294 }
295 
296 /// `POSIX_MADV_*` constants for use with [`madvise`].
297 ///
298 /// [`madvise`]: crate::mm::madvise
299 #[cfg(not(target_os = "redox"))]
300 #[derive(Debug, Copy, Clone, Eq, PartialEq)]
301 #[repr(u32)]
302 #[non_exhaustive]
303 pub enum Advice {
304     /// `POSIX_MADV_NORMAL`
305     #[cfg(not(any(target_os = "android", target_os = "haiku")))]
306     Normal = bitcast!(c::POSIX_MADV_NORMAL),
307 
308     /// `POSIX_MADV_NORMAL`
309     #[cfg(any(target_os = "android", target_os = "haiku"))]
310     Normal = bitcast!(c::MADV_NORMAL),
311 
312     /// `POSIX_MADV_SEQUENTIAL`
313     #[cfg(not(any(target_os = "android", target_os = "haiku")))]
314     Sequential = bitcast!(c::POSIX_MADV_SEQUENTIAL),
315 
316     /// `POSIX_MADV_SEQUENTIAL`
317     #[cfg(any(target_os = "android", target_os = "haiku"))]
318     Sequential = bitcast!(c::MADV_SEQUENTIAL),
319 
320     /// `POSIX_MADV_RANDOM`
321     #[cfg(not(any(target_os = "android", target_os = "haiku")))]
322     Random = bitcast!(c::POSIX_MADV_RANDOM),
323 
324     /// `POSIX_MADV_RANDOM`
325     #[cfg(any(target_os = "android", target_os = "haiku"))]
326     Random = bitcast!(c::MADV_RANDOM),
327 
328     /// `POSIX_MADV_WILLNEED`
329     #[cfg(not(any(target_os = "android", target_os = "haiku")))]
330     WillNeed = bitcast!(c::POSIX_MADV_WILLNEED),
331 
332     /// `POSIX_MADV_WILLNEED`
333     #[cfg(any(target_os = "android", target_os = "haiku"))]
334     WillNeed = bitcast!(c::MADV_WILLNEED),
335 
336     /// `POSIX_MADV_DONTNEED`
337     #[cfg(not(any(target_os = "android", target_os = "emscripten", target_os = "haiku")))]
338     DontNeed = bitcast!(c::POSIX_MADV_DONTNEED),
339 
340     /// `POSIX_MADV_DONTNEED`
341     #[cfg(any(target_os = "android", target_os = "haiku"))]
342     DontNeed = bitcast!(i32::MAX - 1),
343 
344     /// `MADV_DONTNEED`
345     // `MADV_DONTNEED` has the same value as `POSIX_MADV_DONTNEED`. We don't
346     // have a separate `posix_madvise` from `madvise`, so we expose a special
347     // value which we special-case.
348     #[cfg(target_os = "linux")]
349     LinuxDontNeed = bitcast!(i32::MAX),
350 
351     /// `MADV_DONTNEED`
352     #[cfg(target_os = "android")]
353     LinuxDontNeed = bitcast!(c::MADV_DONTNEED),
354     /// `MADV_FREE`
355     #[cfg(linux_kernel)]
356     LinuxFree = bitcast!(c::MADV_FREE),
357     /// `MADV_REMOVE`
358     #[cfg(linux_kernel)]
359     LinuxRemove = bitcast!(c::MADV_REMOVE),
360     /// `MADV_DONTFORK`
361     #[cfg(linux_kernel)]
362     LinuxDontFork = bitcast!(c::MADV_DONTFORK),
363     /// `MADV_DOFORK`
364     #[cfg(linux_kernel)]
365     LinuxDoFork = bitcast!(c::MADV_DOFORK),
366     /// `MADV_HWPOISON`
367     #[cfg(linux_kernel)]
368     LinuxHwPoison = bitcast!(c::MADV_HWPOISON),
369     /// `MADV_SOFT_OFFLINE`
370     #[cfg(all(
371         linux_kernel,
372         not(any(
373             target_arch = "mips",
374             target_arch = "mips32r6",
375             target_arch = "mips64",
376             target_arch = "mips64r6"
377         ))
378     ))]
379     LinuxSoftOffline = bitcast!(c::MADV_SOFT_OFFLINE),
380     /// `MADV_MERGEABLE`
381     #[cfg(linux_kernel)]
382     LinuxMergeable = bitcast!(c::MADV_MERGEABLE),
383     /// `MADV_UNMERGEABLE`
384     #[cfg(linux_kernel)]
385     LinuxUnmergeable = bitcast!(c::MADV_UNMERGEABLE),
386     /// `MADV_HUGEPAGE`
387     #[cfg(linux_kernel)]
388     LinuxHugepage = bitcast!(c::MADV_HUGEPAGE),
389     /// `MADV_NOHUGEPAGE`
390     #[cfg(linux_kernel)]
391     LinuxNoHugepage = bitcast!(c::MADV_NOHUGEPAGE),
392     /// `MADV_DONTDUMP` (since Linux 3.4)
393     #[cfg(linux_kernel)]
394     LinuxDontDump = bitcast!(c::MADV_DONTDUMP),
395     /// `MADV_DODUMP` (since Linux 3.4)
396     #[cfg(linux_kernel)]
397     LinuxDoDump = bitcast!(c::MADV_DODUMP),
398     /// `MADV_WIPEONFORK` (since Linux 4.14)
399     #[cfg(linux_kernel)]
400     LinuxWipeOnFork = bitcast!(c::MADV_WIPEONFORK),
401     /// `MADV_KEEPONFORK` (since Linux 4.14)
402     #[cfg(linux_kernel)]
403     LinuxKeepOnFork = bitcast!(c::MADV_KEEPONFORK),
404     /// `MADV_COLD` (since Linux 5.4)
405     #[cfg(linux_kernel)]
406     LinuxCold = bitcast!(c::MADV_COLD),
407     /// `MADV_PAGEOUT` (since Linux 5.4)
408     #[cfg(linux_kernel)]
409     LinuxPageOut = bitcast!(c::MADV_PAGEOUT),
410     /// `MADV_POPULATE_READ` (since Linux 5.14)
411     #[cfg(linux_kernel)]
412     LinuxPopulateRead = bitcast!(c::MADV_POPULATE_READ),
413     /// `MADV_POPULATE_WRITE` (since Linux 5.14)
414     #[cfg(linux_kernel)]
415     LinuxPopulateWrite = bitcast!(c::MADV_POPULATE_WRITE),
416     /// `MADV_DONTNEED_LOCKED` (since Linux 5.18)
417     #[cfg(linux_kernel)]
418     LinuxDontneedLocked = bitcast!(c::MADV_DONTNEED_LOCKED),
419 }
420 
421 #[cfg(target_os = "emscripten")]
422 #[allow(non_upper_case_globals)]
423 impl Advice {
424     /// `POSIX_MADV_DONTNEED`
425     pub const DontNeed: Self = Self::Normal;
426 }
427 
428 #[cfg(linux_kernel)]
429 bitflags! {
430     /// `O_*` flags for use with [`userfaultfd`].
431     ///
432     /// [`userfaultfd`]: crate::mm::userfaultfd
433     #[repr(transparent)]
434     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
435     pub struct UserfaultfdFlags: u32 {
436         /// `O_CLOEXEC`
437         const CLOEXEC = bitcast!(c::O_CLOEXEC);
438         /// `O_NONBLOCK`
439         const NONBLOCK = bitcast!(c::O_NONBLOCK);
440 
441         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
442         const _ = !0;
443     }
444 }
445 
446 #[cfg(any(linux_kernel, freebsdlike, netbsdlike))]
447 bitflags! {
448     /// `MCL_*` flags for use with [`mlockall`].
449     ///
450     /// [`mlockall`]: crate::mm::mlockall
451     #[repr(transparent)]
452     #[derive(Copy, Clone, Eq, PartialEq, Hash, Debug)]
453     pub struct MlockAllFlags: u32 {
454         /// Used together with `MCL_CURRENT`, `MCL_FUTURE`, or both. Mark all
455         /// current (with `MCL_CURRENT`) or future (with `MCL_FUTURE`) mappings
456         /// to lock pages when they are faulted in. When used with
457         /// `MCL_CURRENT`, all present pages are locked, but `mlockall` will
458         /// not fault in non-present pages. When used with `MCL_FUTURE`, all
459         /// future mappings will be marked to lock pages when they are faulted
460         /// in, but they will not be populated by the lock when the mapping is
461         /// created. `MCL_ONFAULT` must be used with either `MCL_CURRENT` or
462         /// `MCL_FUTURE` or both.
463         #[cfg(linux_kernel)]
464         const ONFAULT = bitcast!(libc::MCL_ONFAULT);
465         /// Lock all pages which will become mapped into the address space of
466         /// the process in the future. These could be, for instance, new pages
467         /// required by a growing heap and stack as well as new memory-mapped
468         /// files or shared memory regions.
469         const FUTURE = bitcast!(libc::MCL_FUTURE);
470         /// Lock all pages which are currently mapped into the address space of
471         /// the process.
472         const CURRENT = bitcast!(libc::MCL_CURRENT);
473 
474         /// <https://docs.rs/bitflags/*/bitflags/#externally-defined-flags>
475         const _ = !0;
476     }
477 }
478