Skip to content

Commit

Permalink
Use errors instead of options
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 0cac724 commit 02e3082
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 24 deletions.
13 changes: 9 additions & 4 deletions aarch64/src/kalloc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ struct FreeList {
}
unsafe impl Send for FreeList {}

#[derive(Debug)]
pub enum Error {
NoFreeBlocks,
}

impl FreeList {
pub fn put(&mut self, page: &mut Page4K) {
let ptr = (page as *mut Page4K).addr();
Expand All @@ -22,13 +27,13 @@ impl FreeList {
self.next = ptr::NonNull::new(f);
}

pub fn get(&mut self) -> Option<&'static mut Page4K> {
let mut next = self.next?;
pub fn get(&mut self) -> Result<&'static mut Page4K, Error> {
let mut next = self.next.ok_or(Error::NoFreeBlocks)?;
let next = unsafe { next.as_mut() };
self.next = next.next;
let pg = unsafe { &mut *(next as *mut FreeList as *mut Page4K) };
pg.clear();
Some(pg)
Ok(pg)
}
}

Expand All @@ -41,7 +46,7 @@ pub unsafe fn free_pages(pages: &mut [Page4K]) {
}
}

pub fn alloc() -> Option<&'static mut Page4K> {
pub fn alloc() -> Result<&'static mut Page4K, Error> {
static mut NODE: LockNode = LockNode::new();
let mut lock = FREE_LIST.lock(unsafe { &NODE });
let fl = &mut *lock;
Expand Down
2 changes: 0 additions & 2 deletions aarch64/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ use vm::PageTable;
#[cfg(not(test))]
core::arch::global_asm!(include_str!("l.S"));

type Result<T> = core::result::Result<T, &'static str>;

static mut KPGTBL: PageTable = PageTable::empty();

unsafe fn print_memory_range(name: &str, start: &*const c_void, end: &*const c_void) {
Expand Down
50 changes: 32 additions & 18 deletions aarch64/src/vm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ use crate::{
etext_addr, heap_addr, text_addr, PhysAddr,
},
registers::rpi_mmio,
Result,
};
use bitstruct::bitstruct;
use core::fmt;
Expand All @@ -17,6 +16,17 @@ 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 @@ -248,8 +258,8 @@ impl Table {
}
}

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

fn child_table(&self, entry: Entry) -> Option<&Table> {
Expand All @@ -261,12 +271,14 @@ impl Table {
}

fn next(&self, level: Level, va: usize) -> Option<&Table> {
let idx = Self::index(level, va);
let entry = self.entries[idx];
let index = Self::index(level, va);
let entry = self.entries[index];
self.child_table(entry)
}

fn next_mut(&mut self, level: Level, va: usize) -> Option<&mut Table> {
// TODO return Result
fn next_mut(&mut self, level: Level, va: usize) -> Result<&mut Table, VmError> {
// 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];
// println!("next_mut(level:{:?}, va:{:016x}, index:{}): entry:{:?}", level, va, index, entry);
Expand All @@ -278,9 +290,13 @@ impl Table {
write_volatile(&mut self.entries[index], entry);
}
}

// TODO Check that the entry is a table

// Return the address of the next table, as found in the entry.
let raw_ptr = entry.virt_page_addr();
let next_table = unsafe { &mut *(raw_ptr as *mut Table) };
Some(next_table)
Ok(next_table)
}
}

Expand All @@ -291,7 +307,7 @@ impl PageTable {
PageTable { entries: [Entry::empty(); 512] }
}

pub fn map_to(&mut self, entry: Entry, va: usize, page_size: PageSize) -> Result<()> {
pub fn map_to(&mut self, entry: Entry, va: usize, page_size: PageSize) -> Result<(), VmError> {
// println!("map_to(entry: {:?}, va: {:#x}, page_size {:?})", entry, va, page_size);
let old_entry = match page_size {
PageSize::Page4K => self
Expand All @@ -308,16 +324,14 @@ impl PageTable {
}
};

if let Some(old_entry) = old_entry {
let entry = entry.with_valid(true);
// println!("Some {:?}, New {:?}", old_entry, entry);
// println!("{:p}", old_entry);
unsafe {
write_volatile(old_entry, entry);
}
return Ok(());
let old_entry = old_entry?;
let entry = entry.with_valid(true);
// println!("Some {:?}, New {:?}", old_entry, entry);
// println!("{:p}", old_entry);
unsafe {
write_volatile(old_entry, entry);
}
Err("Allocation failed")
return Ok(());
}

pub fn map_phys_range(
Expand All @@ -326,7 +340,7 @@ impl PageTable {
end: PhysAddr,
entry: Entry,
page_size: PageSize,
) -> Result<()> {
) -> Result<(), VmError> {
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 02e3082

Please sign in to comment.