mirror of
https://github.com/phil-opp/blog_os.git
synced 2025-12-16 14:27:49 +00:00
52 lines
1.3 KiB
Rust
52 lines
1.3 KiB
Rust
use memory::paging::entry::*;
|
|
use memory::paging::ENTRY_COUNT;
|
|
use core::ops::{Index, IndexMut};
|
|
|
|
pub const P4: *mut Table = 0xffffffff_fffff000 as *mut _;
|
|
|
|
pub struct Table {
|
|
entries: [Entry; ENTRY_COUNT],
|
|
}
|
|
|
|
impl Table {
|
|
pub fn zero(&mut self) {
|
|
for entry in self.entries.iter_mut() {
|
|
entry.set_unused();
|
|
}
|
|
}
|
|
|
|
fn next_table_address(&self, index: usize) -> Option<usize> {
|
|
let entry_flags = self[index].flags();
|
|
if entry_flags.contains(PRESENT) && !entry_flags.contains(HUGE_PAGE) {
|
|
let table_address = self as *const _ as usize;
|
|
Some((table_address << 9) | (index << 12))
|
|
} else {
|
|
None
|
|
}
|
|
}
|
|
|
|
pub unsafe fn next_table(&self, index: usize) -> Option<&Table> {
|
|
self.next_table_address(index)
|
|
.map(|t| unsafe { &*(t as *const _) })
|
|
}
|
|
|
|
pub unsafe fn next_table_mut(&mut self, index: usize) -> Option<&mut Table> {
|
|
self.next_table_address(index)
|
|
.map(|t| unsafe { &mut *(t as *mut _) })
|
|
}
|
|
}
|
|
|
|
impl Index<usize> for Table {
|
|
type Output = Entry;
|
|
|
|
fn index(&self, index: usize) -> &Entry {
|
|
&self.entries[index]
|
|
}
|
|
}
|
|
|
|
impl IndexMut<usize> for Table {
|
|
fn index_mut(&mut self, index: usize) -> &mut Entry {
|
|
&mut self.entries[index]
|
|
}
|
|
}
|