Use TableKind for most table operations

This commit is contained in:
Jeremy Soller
2022-08-20 13:00:48 -06:00
parent c5eb435d79
commit df733ed571
10 changed files with 34 additions and 32 deletions
+1
View File
@@ -1 +1,2 @@
Cargo.lock
/target
Generated
-5
View File
@@ -1,5 +0,0 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
[[package]]
name = "rmm"
version = "0.1.0"
+2 -6
View File
@@ -61,10 +61,8 @@ impl Arch for AArch64Arch {
}
#[inline(always)]
unsafe fn table() -> PhysicalAddress {
unsafe fn table(table_kind: TableKind) -> PhysicalAddress {
let address: usize;
//TODO: set this dynamically
let table_kind = TableKind::Kernel;
match table_kind {
TableKind::User => {
asm!("mrs {0}, ttbr0_el1", out(reg) address);
@@ -77,9 +75,7 @@ impl Arch for AArch64Arch {
}
#[inline(always)]
unsafe fn set_table(address: PhysicalAddress) {
//TODO: set this dynamically
let table_kind = TableKind::Kernel;
unsafe fn set_table(table_kind: TableKind, address: PhysicalAddress) {
match table_kind {
TableKind::User => {
asm!("msr ttbr0_el1, {0}", in(reg) address.data());
+5 -3
View File
@@ -3,6 +3,7 @@ use core::ptr;
use crate::{
MemoryArea,
PhysicalAddress,
TableKind,
VirtualAddress,
};
@@ -86,12 +87,13 @@ pub trait Arch: Clone + Copy {
#[inline(always)]
unsafe fn invalidate_all() {
Self::set_table(Self::table());
//TODO: this stub only works on x86_64, maybe make the arch implement this?
Self::set_table(TableKind::User, Self::table(TableKind::User));
}
unsafe fn table() -> PhysicalAddress;
unsafe fn table(table_kind: TableKind) -> PhysicalAddress;
unsafe fn set_table(address: PhysicalAddress);
unsafe fn set_table(table_kind: TableKind, address: PhysicalAddress);
#[inline(always)]
unsafe fn phys_to_virt(phys: PhysicalAddress) -> VirtualAddress {
+3 -2
View File
@@ -4,6 +4,7 @@ use crate::{
Arch,
MemoryArea,
PhysicalAddress,
TableKind,
VirtualAddress,
};
@@ -44,7 +45,7 @@ impl Arch for RiscV64Sv39Arch {
}
#[inline(always)]
unsafe fn table() -> PhysicalAddress {
unsafe fn table(_table_kind: TableKind) -> PhysicalAddress {
let satp: usize;
asm!("csrr {0}, satp", out(reg) satp);
PhysicalAddress::new(
@@ -53,7 +54,7 @@ impl Arch for RiscV64Sv39Arch {
}
#[inline(always)]
unsafe fn set_table(address: PhysicalAddress) {
unsafe fn set_table(_table_kind: TableKind, address: PhysicalAddress) {
let satp =
(8 << 60) | // Sv39 MODE
(address.data() >> Self::PAGE_SHIFT); // Convert to PPN (TODO: ensure alignment)
+3 -2
View File
@@ -4,6 +4,7 @@ use crate::{
Arch,
MemoryArea,
PhysicalAddress,
TableKind,
VirtualAddress,
};
@@ -44,7 +45,7 @@ impl Arch for RiscV64Sv48Arch {
}
#[inline(always)]
unsafe fn table() -> PhysicalAddress {
unsafe fn table(_table_kind: TableKind) -> PhysicalAddress {
let satp: usize;
asm!("csrr {0}, satp", out(reg) satp);
PhysicalAddress::new(
@@ -53,7 +54,7 @@ impl Arch for RiscV64Sv48Arch {
}
#[inline(always)]
unsafe fn set_table(address: PhysicalAddress) {
unsafe fn set_table(_table_kind: TableKind, address: PhysicalAddress) {
let satp =
(9 << 60) | // Sv48 MODE
(address.data() >> Self::PAGE_SHIFT); // Convert to PPN (TODO: ensure alignment)
+3 -2
View File
@@ -5,6 +5,7 @@ use crate::{
Arch,
MemoryArea,
PhysicalAddress,
TableKind,
VirtualAddress,
};
@@ -40,14 +41,14 @@ impl Arch for X86Arch {
}
#[inline(always)]
unsafe fn table() -> PhysicalAddress {
unsafe fn table(_table_kind: TableKind) -> PhysicalAddress {
let address: usize;
asm!("mov {0}, cr3", out(reg) address);
PhysicalAddress::new(address)
}
#[inline(always)]
unsafe fn set_table(address: PhysicalAddress) {
unsafe fn set_table(_table_kind: TableKind, address: PhysicalAddress) {
asm!("mov cr3, {0}", in(reg) address.data());
}
+3 -2
View File
@@ -4,6 +4,7 @@ use crate::{
Arch,
MemoryArea,
PhysicalAddress,
TableKind,
VirtualAddress,
};
@@ -39,14 +40,14 @@ impl Arch for X8664Arch {
}
#[inline(always)]
unsafe fn table() -> PhysicalAddress {
unsafe fn table(_table_kind: TableKind) -> PhysicalAddress {
let address: usize;
asm!("mov {0}, cr3", out(reg) address);
PhysicalAddress::new(address)
}
#[inline(always)]
unsafe fn set_table(address: PhysicalAddress) {
unsafe fn set_table(_table_kind: TableKind, address: PhysicalAddress) {
asm!("mov cr3, {0}", in(reg) address.data());
}
+11 -8
View File
@@ -13,35 +13,38 @@ use crate::{
};
pub struct PageMapper<A, F> {
table_kind: TableKind,
table_addr: PhysicalAddress,
allocator: F,
_phantom: PhantomData<fn() -> A>,
}
impl<A: Arch, F: FrameAllocator> PageMapper<A, F> {
pub unsafe fn new(table_addr: PhysicalAddress, allocator: F) -> Self {
pub unsafe fn new(table_kind: TableKind, table_addr: PhysicalAddress, allocator: F) -> Self {
Self {
table_kind,
table_addr,
allocator,
_phantom: PhantomData,
}
}
pub unsafe fn create(mut allocator: F) -> Option<Self> {
pub unsafe fn create(table_kind: TableKind, mut allocator: F) -> Option<Self> {
let table_addr = allocator.allocate_one()?;
Some(Self::new(table_addr, allocator))
Some(Self::new(table_kind, table_addr, allocator))
}
pub unsafe fn current(allocator: F) -> Self {
let table_addr = A::table();
Self::new(table_addr, allocator)
pub unsafe fn current(table_kind: TableKind, allocator: F) -> Self {
let table_addr = A::table(table_kind);
Self::new(table_kind, table_addr, allocator)
}
pub fn is_current(&self) -> bool {
unsafe { self.table().phys() == A::table() }
unsafe { self.table().phys() == A::table(self.table_kind) }
}
pub unsafe fn make_current(&self) {
A::set_table(self.table_addr);
A::set_table(self.table_kind, self.table_addr);
}
pub fn table(&self) -> PageTable<A> {
+3 -2
View File
@@ -3,6 +3,7 @@ use core::marker::PhantomData;
use crate::{
Arch,
PhysicalAddress,
TableKind,
VirtualAddress,
};
use super::PageEntry;
@@ -19,10 +20,10 @@ impl<A: Arch> PageTable<A> {
Self { base, phys, level, phantom: PhantomData }
}
pub unsafe fn top() -> Self {
pub unsafe fn top(table_kind: TableKind) -> Self {
Self::new(
VirtualAddress::new(0),
A::table(),
A::table(table_kind),
A::PAGE_LEVELS - 1
)
}