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
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
use super::*;

/// Defines a mapping from virtual to physical address space.
#[repr(align(4096))]
pub struct PageTable {
    pub entries: [Entry; 512],
}

/// Page table entry.
#[derive(Debug, Clone, Copy)]
pub struct Entry {
    entry: u64,
}

/// Properties of a page table entry.
#[repr(u64)]
#[derive(Debug, Clone, Copy)]
pub enum Bit {
    Present  = 0,
    Writable = 1,
    User     = 2,
    Direct   = 3,
    Nocache  = 4,
    Accessed = 5,
    Dirty    = 6,
    Huge     = 7,
    Global   = 8,
    Noexec   = 63,
}

add_indexing!(PageTable, Entry);

impl PageTable {
    /// Create a new PageTable.
    pub const fn new() -> Self {
        PageTable {
            entries: [Entry::new(); 512],
        }
    }

    /// Clear the page table.
    pub fn clear(&mut self) {
        for entry in self.entries.iter_mut() {
            entry.clear();
        }
    }
}

impl Entry {
    /// Create a new page table entry.
    pub const fn new() -> Self {
        Entry {
            entry: 0,
        }
    }

    /// Volatile read of the entry.
    fn read(&self) -> u64 {
        let entry_ptr = &self.entry as *const u64;
        unsafe { core::ptr::read_volatile(entry_ptr) }
    }

    /// Volatile write of the entry.
    fn write(&mut self, entry: u64) {
        let entry_ptr = &mut self.entry as *mut u64;
        unsafe { core::ptr::write_volatile(entry_ptr, entry); }
    }

    /// Update the entry value using volatile read/write.
    fn update<F>(&mut self, f: F) where F: Fn(u64) -> u64 {
        self.write(f(self.read()));
    }

    /// Set the entry to 0.
    pub fn clear(&mut self) {
        self.write(0);
    }

    /// Physical memory address referenced by this entry.
    pub fn address(&self) -> PhysicalAddress {
        self.read() & 0x000ffffffffff000
    }

    /// Set the physical memory address of this entry.
    pub fn set_address(&mut self, address: PhysicalAddress) -> &mut Self {
        assert!(address % 0x1000 == 0);
        assert!(address < 0xfff0_0000_0000_0000);
        self.update(|entry| {
            (entry & !0x000ffffffffff000) | address
        });
        self
    }

    /// Bits available to operating system.
    pub fn avail(&self) -> u8 {
        ((self.read() & 0x0e00) >> 9) as u8
    }

    /// Set available bits.
    pub fn set_avail(&mut self, val: u8) -> &mut Self {
        if val > 7 {
            panic!("Avail value out ouf bounds");
        }
        self.update(|entry| {
           (entry & !0x0e00) | ((val as u64) << 9)
        });
        self
    }

    /// Whether a certain bit is set.
    pub fn bit(&self, bit: Bit) -> bool {
        get_bit!(self.read(), bit as u64)
    }

    /// Set or unset a bit.
    fn modify_bit(&mut self, bit: Bit, val: bool) {
        self.update(|mut entry| {
            set_bit!(entry, bit as u64, val);
            entry
        });
    }

    /// Set a bit.
    pub fn set_bit(&mut self, bit: Bit) -> &mut Self {
        self.modify_bit(bit, true);
        self
    }

    /// Unset a bit.
    pub fn unset_bit(&mut self, bit: Bit) -> &mut Self {
        self.modify_bit(bit, false);
        self
    }
}

#[test]
fn int_consistency() {
    let mut entry = Entry::new();
    entry.set_address(0x4242000);
    assert_eq!(entry.address(), 0x4242000);

    entry.set_avail(7);
    assert_eq!(entry.avail(), 7);
    entry.set_avail(0);
    assert_eq!(entry.avail(), 0);
}

#[test]
fn bit_consistency() {
    let mut entry = Entry::new();
    entry.set_address(0x000ffffffffff000);
    assert!(!entry.bit(Bit::Present));
    assert!(!entry.bit(Bit::Writable));
    assert!(!entry.bit(Bit::User));
    assert!(!entry.bit(Bit::Direct));
    assert!(!entry.bit(Bit::Nocache));
    assert!(!entry.bit(Bit::Accessed));
    assert!(!entry.bit(Bit::Dirty));
    assert!(!entry.bit(Bit::Huge));
    assert!(!entry.bit(Bit::Global));
    assert!(!entry.bit(Bit::Noexec));

    entry.set_bit(Bit::Present);
    assert!(entry.bit(Bit::Present));
    entry.unset_bit(Bit::Present);

    entry.set_bit(Bit::Writable);
    assert!(entry.bit(Bit::Writable));
    entry.unset_bit(Bit::Writable);

    entry.set_bit(Bit::User);
    assert!(entry.bit(Bit::User));
    entry.unset_bit(Bit::User);

    entry.set_bit(Bit::Direct);
    assert!(entry.bit(Bit::Direct));
    entry.unset_bit(Bit::Direct);

    entry.set_bit(Bit::Nocache);
    assert!(entry.bit(Bit::Nocache));
    entry.unset_bit(Bit::Nocache);

    entry.set_bit(Bit::Accessed);
    assert!(entry.bit(Bit::Accessed));
    entry.unset_bit(Bit::Accessed);

    entry.set_bit(Bit::Dirty);
    assert!(entry.bit(Bit::Dirty));
    entry.unset_bit(Bit::Dirty);

    entry.set_bit(Bit::Huge);
    assert!(entry.bit(Bit::Huge));
    entry.unset_bit(Bit::Huge);

    entry.set_bit(Bit::Global);
    assert!(entry.bit(Bit::Global));
    entry.unset_bit(Bit::Global);

    entry.set_bit(Bit::Noexec);
    assert!(entry.bit(Bit::Noexec));
    entry.unset_bit(Bit::Noexec);
}