From df733ed5714aaa7f7b3e15707bd7ef8a44802779 Mon Sep 17 00:00:00 2001 From: Jeremy Soller Date: Sat, 20 Aug 2022 13:00:48 -0600 Subject: [PATCH] Use TableKind for most table operations --- .gitignore | 1 + Cargo.lock | 5 ----- src/arch/aarch64.rs | 8 ++------ src/arch/mod.rs | 8 +++++--- src/arch/riscv64/sv39.rs | 5 +++-- src/arch/riscv64/sv48.rs | 5 +++-- src/arch/x86.rs | 5 +++-- src/arch/x86_64.rs | 5 +++-- src/page/mapper.rs | 19 +++++++++++-------- src/page/table.rs | 5 +++-- 10 files changed, 34 insertions(+), 32 deletions(-) delete mode 100644 Cargo.lock diff --git a/.gitignore b/.gitignore index ea8c4bf7f3..06aba01b65 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ +Cargo.lock /target diff --git a/Cargo.lock b/Cargo.lock deleted file mode 100644 index e919422bb9..0000000000 --- a/Cargo.lock +++ /dev/null @@ -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" diff --git a/src/arch/aarch64.rs b/src/arch/aarch64.rs index 608869918f..b9f3a62aaf 100644 --- a/src/arch/aarch64.rs +++ b/src/arch/aarch64.rs @@ -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()); diff --git a/src/arch/mod.rs b/src/arch/mod.rs index 8c9b0affab..2f16b7631a 100644 --- a/src/arch/mod.rs +++ b/src/arch/mod.rs @@ -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 { diff --git a/src/arch/riscv64/sv39.rs b/src/arch/riscv64/sv39.rs index c8e5ce1b66..e3f1926b06 100644 --- a/src/arch/riscv64/sv39.rs +++ b/src/arch/riscv64/sv39.rs @@ -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) diff --git a/src/arch/riscv64/sv48.rs b/src/arch/riscv64/sv48.rs index 89cbeb4b69..cea0a537ba 100644 --- a/src/arch/riscv64/sv48.rs +++ b/src/arch/riscv64/sv48.rs @@ -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) diff --git a/src/arch/x86.rs b/src/arch/x86.rs index d804ed213a..dcf0600941 100644 --- a/src/arch/x86.rs +++ b/src/arch/x86.rs @@ -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()); } diff --git a/src/arch/x86_64.rs b/src/arch/x86_64.rs index 1008df8e8f..0256e3b81e 100644 --- a/src/arch/x86_64.rs +++ b/src/arch/x86_64.rs @@ -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()); } diff --git a/src/page/mapper.rs b/src/page/mapper.rs index 1e0e98fa13..2c1c67ba38 100644 --- a/src/page/mapper.rs +++ b/src/page/mapper.rs @@ -13,35 +13,38 @@ use crate::{ }; pub struct PageMapper { + table_kind: TableKind, table_addr: PhysicalAddress, allocator: F, _phantom: PhantomData A>, } impl PageMapper { - 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 { + pub unsafe fn create(table_kind: TableKind, mut allocator: F) -> Option { 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 { diff --git a/src/page/table.rs b/src/page/table.rs index 0abc78486d..3c003cb7ec 100644 --- a/src/page/table.rs +++ b/src/page/table.rs @@ -3,6 +3,7 @@ use core::marker::PhantomData; use crate::{ Arch, PhysicalAddress, + TableKind, VirtualAddress, }; use super::PageEntry; @@ -19,10 +20,10 @@ impl PageTable { 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 ) }