From 29460e0bff55cfc63919d0152ea7b6495af91d1a Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jeremy@system76.com>
Date: Mon, 3 May 2021 12:14:49 -0600
Subject: [PATCH] Use RMM for some arch-specific paging functions

---
 src/arch/x86_64/paging/entry.rs | 13 +++++------
 src/arch/x86_64/paging/mod.rs   | 39 +++++++++++++++++++--------------
 2 files changed, 27 insertions(+), 25 deletions(-)

diff --git a/src/arch/x86_64/paging/entry.rs b/src/arch/x86_64/paging/entry.rs
index 2c521724..c3e68107 100644
--- a/src/arch/x86_64/paging/entry.rs
+++ b/src/arch/x86_64/paging/entry.rs
@@ -3,7 +3,7 @@
 
 use crate::memory::Frame;
 
-use super::PhysicalAddress;
+use super::{PhysicalAddress, RmmA, RmmArch};
 
 /// A page table entry
 #[repr(packed(8))]
@@ -11,16 +11,13 @@ pub struct Entry(u64);
 
 bitflags! {
     pub struct EntryFlags: u64 {
-        const PRESENT =         1;
-        const WRITABLE =        1 << 1;
-        const USER_ACCESSIBLE = 1 << 2;
-        const WRITE_THROUGH =   1 << 3;
+        const PRESENT =         RmmA::ENTRY_FLAG_PRESENT as u64;
+        const WRITABLE =        RmmA::ENTRY_FLAG_READWRITE as u64;
+        const USER_ACCESSIBLE = RmmA::ENTRY_FLAG_USER as u64;
         const NO_CACHE =        1 << 4;
-        const ACCESSED =        1 << 5;
-        const DIRTY =           1 << 6;
         const HUGE_PAGE =       1 << 7;
         const GLOBAL =          1 << 8;
-        const NO_EXECUTE =      1 << 63;
+        const NO_EXECUTE =      RmmA::ENTRY_FLAG_NO_EXEC as u64;
     }
 }
 
diff --git a/src/arch/x86_64/paging/mod.rs b/src/arch/x86_64/paging/mod.rs
index 852de3a3..7ef1b5d7 100644
--- a/src/arch/x86_64/paging/mod.rs
+++ b/src/arch/x86_64/paging/mod.rs
@@ -4,7 +4,7 @@
 use core::ops::{Deref, DerefMut};
 use core::{mem, ptr};
 use spin::Mutex;
-use x86::{controlregs, msr, tlb};
+use x86::msr;
 
 use crate::memory::Frame;
 
@@ -12,7 +12,12 @@ use self::entry::EntryFlags;
 use self::mapper::{Mapper, MapperFlushAll};
 use self::temporary_page::TemporaryPage;
 
-pub use rmm::{PhysicalAddress, VirtualAddress};
+pub use rmm::{
+    Arch as RmmArch,
+    PhysicalAddress,
+    VirtualAddress,
+    X8664Arch as RmmA,
+};
 
 pub mod entry;
 pub mod mapper;
@@ -259,25 +264,25 @@ impl ActivePageTable {
 
     pub fn switch(&mut self, new_table: InactivePageTable) -> InactivePageTable {
         let old_table = InactivePageTable {
-            p4_frame: Frame::containing_address(PhysicalAddress::new(
-                unsafe { controlregs::cr3() } as usize,
-            )),
+            frame: Frame::containing_address(unsafe {
+                RmmA::table()
+            })
         };
         unsafe {
-            controlregs::cr3_write(new_table.p4_frame.start_address().data() as u64);
+            RmmA::set_table(new_table.frame.start_address());
         }
         old_table
     }
 
     pub fn flush(&mut self, page: Page) {
         unsafe {
-            tlb::flush(page.start_address().data());
+            RmmA::invalidate(page.start_address());
         }
     }
 
     pub fn flush_all(&mut self) {
         unsafe {
-            tlb::flush_all();
+            RmmA::invalidate_all();
         }
     }
 
@@ -290,9 +295,9 @@ impl ActivePageTable {
         F: FnOnce(&mut Mapper),
     {
         {
-            let backup = Frame::containing_address(PhysicalAddress::new(unsafe {
-                controlregs::cr3() as usize
-            }));
+            let backup = Frame::containing_address(unsafe {
+                RmmA::table()
+            });
 
             // map temporary_page to current p4 table
             let p4_table = temporary_page.map_table_frame(
@@ -303,7 +308,7 @@ impl ActivePageTable {
 
             // overwrite recursive mapping
             self.p4_mut()[crate::RECURSIVE_PAGE_PML4].set(
-                table.p4_frame.clone(),
+                table.frame.clone(),
                 EntryFlags::PRESENT | EntryFlags::WRITABLE | EntryFlags::NO_EXECUTE,
             );
             self.flush_all();
@@ -323,7 +328,7 @@ impl ActivePageTable {
     }
 
     pub unsafe fn address(&self) -> usize {
-        controlregs::cr3() as usize
+        RmmA::table().data()
     }
 }
 
@@ -337,7 +342,7 @@ impl Drop for ActivePageTable {
 }
 
 pub struct InactivePageTable {
-    p4_frame: Frame,
+    frame: Frame,
 }
 
 impl InactivePageTable {
@@ -362,17 +367,17 @@ impl InactivePageTable {
         }
         temporary_page.unmap(active_table);
 
-        InactivePageTable { p4_frame: frame }
+        InactivePageTable { frame: frame }
     }
 
     pub unsafe fn from_address(cr3: usize) -> InactivePageTable {
         InactivePageTable {
-            p4_frame: Frame::containing_address(PhysicalAddress::new(cr3)),
+            frame: Frame::containing_address(PhysicalAddress::new(cr3)),
         }
     }
 
     pub unsafe fn address(&self) -> usize {
-        self.p4_frame.start_address().data()
+        self.frame.start_address().data()
     }
 }
 
-- 
GitLab