Skip to content

Commit

Permalink
refactor(PageAlloc trait): add riscv64 support
Browse files Browse the repository at this point in the history
  • Loading branch information
n1tram1 committed Nov 29, 2023
1 parent ac94f64 commit 7e73956
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 29 deletions.
2 changes: 0 additions & 2 deletions hal_aarch64/src/mm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ pub fn enable_paging() {
unsafe {
load_pagetable(current());
};

log::trace!("hal_core::mm::init_paging finished");
}

unsafe fn load_pagetable(pt: &'static mut PageTable) {
Expand Down
6 changes: 3 additions & 3 deletions hal_riscv64/src/irq.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use hal_core::{
mm::{PageAllocFn, PageMap, Permissions, VAddr},
mm::{PageAlloc, PageMap, Permissions, VAddr},
Error, TimerCallbackFn,
};

Expand All @@ -20,7 +20,7 @@ pub fn init_exception_handlers() {

static mut IRQ_CHIP: Option<Plic> = None;

pub fn init_irq_chip(_dt_node: (), alloc: PageAllocFn) -> Result<(), Error> {
pub fn init_irq_chip(_dt_node: (), allocator: &impl PageAlloc) -> Result<(), Error> {
// TODO map the dt_node
let base = 0xc000000;
let max_offset = 0x3FFFFFC;
Expand All @@ -29,7 +29,7 @@ pub fn init_irq_chip(_dt_node: (), alloc: PageAllocFn) -> Result<(), Error> {
VAddr::new(base),
max_offset / mm::PAGE_SIZE + 1,
Permissions::READ | Permissions::WRITE,
alloc,
allocator,
)?;
unsafe {
IRQ_CHIP = Some(Plic::new(base));
Expand Down
36 changes: 21 additions & 15 deletions hal_riscv64/src/mm/mod.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use core::arch::asm;
use core::cell::OnceCell;

use hal_core::{
mm::{self, PageAllocFn, PageMap},
mm::{self, PageAlloc, PageMap},
AddressRange, Error,
};

Expand All @@ -17,28 +16,31 @@ pub fn current() -> &'static mut PageTable {
unsafe { *GPT.get_mut().unwrap() }
}

pub fn init_paging(
pub fn prefill_pagetable(
r: impl Iterator<Item = AddressRange>,
rw: impl Iterator<Item = AddressRange>,
rwx: impl Iterator<Item = AddressRange>,
pre_allocated: impl Iterator<Item = AddressRange>,
alloc: PageAllocFn,
allocator: &impl PageAlloc,
) -> Result<(), Error> {
hal_core::mm::init_paging::<PageTable>(r, rw, rwx, pre_allocated, alloc, |pt| {
// TODO: put into into the hal_core::Error
unsafe {
if GPT.set(pt).is_err() {
panic!("GPT is already set ?");
}
};
unsafe {
load_pagetable(current());
};
})?;
let pt = hal_core::mm::prefill_pagetable::<PageTable>(r, rw, rwx, pre_allocated, allocator)?;

// TODO: put into into the hal_core::Error
unsafe {
if GPT.set(pt).is_err() {
panic!("GPT is already set ?");
}
};

Ok(())
}

pub fn enable_paging() {
unsafe {
load_pagetable(current());
}
}

unsafe fn load_pagetable(pt: &'static mut PageTable) {
let pt_addr = pt as *mut PageTable as usize;
let ppn = pt_addr >> 12;
Expand All @@ -51,6 +53,10 @@ unsafe fn load_pagetable(pt: &'static mut PageTable) {
}
}

pub fn align_down(addr: usize) -> usize {
mm::align_down(addr, PageTable::PAGE_SIZE)
}

pub fn align_up(addr: usize) -> usize {
mm::align_up(addr, PageTable::PAGE_SIZE)
}
20 changes: 11 additions & 9 deletions hal_riscv64/src/mm/sv39.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use modular_bitfield::{bitfield, prelude::*};

use hal_core::mm::{self, PageAllocFn, PageEntry, PageMap};
use hal_core::mm::{self, PageAlloc, PageEntry, PageMap};
use hal_core::Error;

#[repr(C)]
Expand Down Expand Up @@ -167,14 +167,16 @@ impl PageMap for PageTable {
const PAGE_SIZE: usize = 4096;
type Entry = PageTableEntry;

fn new(alloc: PageAllocFn) -> Result<&'static mut Self, Error> {
let page = alloc(1);

let page_table = page.ptr_cast::<PageTable>();
fn new(allocator: &impl PageAlloc) -> Result<&'static mut Self, Error> {
let page = allocator.alloc(1)?;
let page_table = unsafe { page as *mut PageTable };
// Safety: the PMM gave us the memory, it should be a valid pointer.
let page_table = unsafe { page_table.as_mut().unwrap() };
let page_table: &mut PageTable = unsafe { page_table.as_mut().unwrap() };

page_table.entries.iter_mut().for_each(|pte| pte.clear());
page_table
.entries
.iter_mut()
.for_each(|pte| pte.set_invalid());

Ok(page_table)
}
Expand All @@ -184,7 +186,7 @@ impl PageMap for PageTable {
va: mm::VAddr,
pa: mm::PAddr,
perms: mm::Permissions,
alloc: PageAllocFn,
allocator: &impl PageAlloc,
) -> Result<&mut Self::Entry, Error> {
let paddr: PAddr = pa.into();
let vaddr: VAddr = va.into();
Expand All @@ -207,7 +209,7 @@ impl PageMap for PageTable {

// If the entry is not valid we will need to allocate a new PageTable
if !pte.is_valid() {
let new_page_table = PageTable::new(alloc);
let new_page_table = PageTable::new(allocator);

// Set new PageTable as target of this entry
pte.set_target(new_page_table? as *mut PageTable);
Expand Down

0 comments on commit 7e73956

Please sign in to comment.