starina_types/
vmspace.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use core::ops;

#[derive(Debug, Copy, Clone, Eq, PartialEq)]
#[repr(transparent)]
pub struct PageProtect(u8);

impl PageProtect {
    pub const READABLE: PageProtect = PageProtect::from_raw(1 << 1);
    pub const WRITABLE: PageProtect = PageProtect::from_raw(1 << 2);
    pub const EXECUTABLE: PageProtect = PageProtect::from_raw(1 << 3);
    pub const USER: PageProtect = PageProtect::from_raw(1 << 4);

    pub const fn zeroed() -> PageProtect {
        PageProtect(0)
    }

    pub const fn from_raw(value: u8) -> PageProtect {
        PageProtect(value)
    }

    pub fn contains(&self, other: PageProtect) -> bool {
        (self.0 & other.0) != 0
    }

    pub fn user_allowed_flags(&self) -> bool {
        let allowed = Self::READABLE | Self::WRITABLE | Self::EXECUTABLE;
        (self.0 & allowed.0) == self.0
    }

    pub fn as_raw(&self) -> u8 {
        self.0
    }
}

impl ops::BitOr for PageProtect {
    type Output = Self;

    fn bitor(self, rhs: Self) -> Self {
        PageProtect(self.0 | rhs.0)
    }
}

impl ops::BitAnd for PageProtect {
    type Output = Self;

    fn bitand(self, rhs: Self) -> Self {
        PageProtect(self.0 & rhs.0)
    }
}

impl ops::BitOrAssign for PageProtect {
    fn bitor_assign(&mut self, rhs: Self) {
        self.0 |= rhs.0;
    }
}

impl ops::BitAndAssign for PageProtect {
    fn bitand_assign(&mut self, rhs: Self) {
        self.0 &= rhs.0;
    }
}