Skip to main content

safa_abi/
mem.rs

1use core::ops::{BitAnd, BitOr};
2
3/// Passed to [`crate::syscalls::SyscallTable::SysMemMap`], to describe the address, the size, of the area to map to, and the resource it is associated with
4#[repr(C)]
5pub struct RawMemMapConfig {
6    /// Address hint can be null to indicate no hint
7    pub addr_hint: *const (),
8    /// The amount of pages to map
9    pub page_count: usize,
10    /// The amount of unmapped guard pages before and after the area
11    pub guard_pages_count: usize,
12    /// The ID of the associated Resource to map, ignored unless the flag [`MemMapFlags::MAP_RESOURCE`] was given
13    pub resource_to_map: usize,
14    /// If Mapping A Resource, Describes the offset to where the memory mapping
15    pub resource_off: isize,
16}
17
18/// Flags passed to [`crate::syscalls::SyscallTable::SysMemMap`]
19#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
20#[repr(transparent)]
21pub struct MemMapFlags(u8);
22
23impl MemMapFlags {
24    /// The given address is a just a hint unless specified with this flag
25    pub const FIXED: Self = Self(1 << 0);
26    /// Allows the memory to be written to, makes it RW, by default it is only R
27    pub const WRITE: Self = Self(1 << 1);
28    /// Don't allow the memory to be executed
29    pub const DISABLE_EXEC: Self = Self(1 << 2);
30    /// The given Resource ID is a valid resource to map, by default the given resource ID is ignored and the memory is treated as not associated with anything
31    pub const MAP_RESOURCE: Self = Self(1 << 3);
32}
33
34impl MemMapFlags {
35    pub fn from_bits_retaining(bits: u8) -> Self {
36        Self(bits)
37    }
38
39    pub fn contains(self, other: MemMapFlags) -> bool {
40        (self & other) == other
41    }
42}
43
44impl BitOr for MemMapFlags {
45    type Output = Self;
46    fn bitor(self, rhs: Self) -> Self::Output {
47        Self(self.0 | rhs.0)
48    }
49}
50
51impl BitAnd for MemMapFlags {
52    type Output = Self;
53    fn bitand(self, rhs: Self) -> Self::Output {
54        Self(self.0 & rhs.0)
55    }
56}
57
58/// Flags passed to [`crate::syscalls::SyscallTable::SysMemShmCreate`] and [`crate::syscalls::SyscallTable::SysMemShmOpen`]
59#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
60#[repr(transparent)]
61pub struct ShmFlags(u32);
62
63impl ShmFlags {
64    /// The opened Resource's lifetime is bound by the Current Thread and not the Process
65    pub const LOCAL: Self = Self(1 << 0);
66}
67
68impl ShmFlags {
69    pub const fn from_bits_retaining(bits: u32) -> Self {
70        Self(bits)
71    }
72
73    pub const fn contains(self, other: Self) -> bool {
74        (self.0 & other.0) == other.0
75    }
76}
77
78impl BitOr for ShmFlags {
79    type Output = Self;
80    fn bitor(self, rhs: Self) -> Self::Output {
81        Self(self.0 | rhs.0)
82    }
83}
84
85impl BitAnd for ShmFlags {
86    type Output = Self;
87    fn bitand(self, rhs: Self) -> Self::Output {
88        Self(self.0 & rhs.0)
89    }
90}