Skip to content

Commit

Permalink
More specific error
Browse files Browse the repository at this point in the history
Signed-off-by: Graham MacDonald <[email protected]>
  • Loading branch information
gmacd committed Sep 17, 2023
1 parent 02e3082 commit a8e29d4
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions aarch64/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,6 @@ use num_enum::{FromPrimitive, IntoPrimitive};
#[cfg(not(test))]
use port::println;

#[derive(Debug)]
pub enum VmError {
AllocationFailed(kalloc::Error),
}

impl From<kalloc::Error> for VmError {
fn from(err: kalloc::Error) -> VmError {
VmError::AllocationFailed(err)
}
}

pub const PAGE_SIZE_4K: usize = 4 * 1024;
pub const PAGE_SIZE_2M: usize = 2 * 1024 * 1024;
pub const PAGE_SIZE_1G: usize = 1 * 1024 * 1024 * 1024;
Expand Down Expand Up @@ -243,6 +232,18 @@ impl Level {
}
}

#[derive(Debug)]
pub enum PageTableError {
AllocationFailed(kalloc::Error),
EntryIsNotTable,
}

impl From<kalloc::Error> for PageTableError {
fn from(err: kalloc::Error) -> PageTableError {
PageTableError::AllocationFailed(err)
}
}

#[repr(C, align(4096))]
pub struct Table {
entries: [Entry; 512],
Expand All @@ -258,7 +259,7 @@ impl Table {
}
}

pub fn entry_mut(&mut self, level: Level, va: usize) -> Result<&mut Entry, VmError> {
pub fn entry_mut(&mut self, level: Level, va: usize) -> Result<&mut Entry, PageTableError> {
Ok(&mut self.entries[Self::index(level, va)])
}

Expand All @@ -277,7 +278,7 @@ impl Table {
}

// TODO return Result
fn next_mut(&mut self, level: Level, va: usize) -> Result<&mut Table, VmError> {
fn next_mut(&mut self, level: Level, va: usize) -> Result<&mut Table, PageTableError> {
// Try to get a valid page table entry. If it doesn't exist, create it.
let index = Self::index(level, va);
let mut entry = self.entries[index];
Expand All @@ -291,7 +292,9 @@ impl Table {
}
}

// TODO Check that the entry is a table
if !entry.table() {
return Err(PageTableError::EntryIsNotTable);
}

// Return the address of the next table, as found in the entry.
let raw_ptr = entry.virt_page_addr();
Expand All @@ -307,7 +310,12 @@ impl PageTable {
PageTable { entries: [Entry::empty(); 512] }
}

pub fn map_to(&mut self, entry: Entry, va: usize, page_size: PageSize) -> Result<(), VmError> {
pub fn map_to(
&mut self,
entry: Entry,
va: usize,
page_size: PageSize,
) -> Result<(), PageTableError> {
// println!("map_to(entry: {:?}, va: {:#x}, page_size {:?})", entry, va, page_size);
let old_entry = match page_size {
PageSize::Page4K => self
Expand Down Expand Up @@ -340,7 +348,7 @@ impl PageTable {
end: PhysAddr,
entry: Entry,
page_size: PageSize,
) -> Result<(), VmError> {
) -> Result<(), PageTableError> {
for pa in PhysAddr::step_by_rounded(start, end, page_size.size()) {
self.map_to(entry.with_phys_addr(pa), pa.to_virt(), page_size)?;
}
Expand Down

0 comments on commit a8e29d4

Please sign in to comment.