From 9371120f0f6c1ec388a962f0115cdc19fa4b4774 Mon Sep 17 00:00:00 2001
From: Jeremy Soller <jackpot51@gmail.com>
Date: Sun, 11 Sep 2016 15:02:35 -0600
Subject: [PATCH] Flush TLB correctly when remapping Seperate mouse and
 keyboard structs in PS/2 driver

---
 elf.rs             | 29 ++++++++++-------------------
 syscall/process.rs |  2 ++
 2 files changed, 12 insertions(+), 19 deletions(-)

diff --git a/elf.rs b/elf.rs
index b379bba6..10be116b 100644
--- a/elf.rs
+++ b/elf.rs
@@ -13,7 +13,6 @@ use goblin::elf64::{header, program_header};
 use arch::externs::{memcpy, memset};
 use arch::paging::{entry, ActivePageTable, Page, VirtualAddress};
 use arch::start::usermode;
-use arch::x86::tlb;
 
 /// An ELF executable
 pub struct Elf<'a> {
@@ -63,11 +62,9 @@ impl<'a> Elf<'a> {
                 for page in Page::range_inclusive(start_page, end_page) {
                     active_table.map(page, entry::NO_EXECUTE | entry::WRITABLE);
                 }
+                active_table.flush_all();
 
                 unsafe {
-                    // Update the page table
-                    tlb::flush_all();
-
                     // Copy file data
                     memcpy(segment.p_vaddr as *mut u8,
                             (self.data.as_ptr() as usize + segment.p_offset as usize) as *const u8,
@@ -94,26 +91,20 @@ impl<'a> Elf<'a> {
                 for page in Page::range_inclusive(start_page, end_page) {
                     active_table.remap(page, flags);
                 }
-
-                unsafe {
-                    // Update the page table
-                    tlb::flush_all();
-                }
+                active_table.flush_all();
             }
         }
 
-        unsafe {
-            // Map stack
-            let start_page = Page::containing_address(VirtualAddress::new(0x80000000));
-            let end_page = Page::containing_address(VirtualAddress::new(0x80000000 + 64*1024 - 1));
+        // Map stack
+        let start_page = Page::containing_address(VirtualAddress::new(0x80000000));
+        let end_page = Page::containing_address(VirtualAddress::new(0x80000000 + 64*1024 - 1));
 
-            for page in Page::range_inclusive(start_page, end_page) {
-                active_table.map(page, entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE);
-            }
-
-            // Update the page table
-            tlb::flush_all();
+        for page in Page::range_inclusive(start_page, end_page) {
+            active_table.map(page, entry::NO_EXECUTE | entry::WRITABLE | entry::USER_ACCESSIBLE);
+        }
+        active_table.flush_all();
 
+        unsafe {
             // Clear stack
             memset(0x80000000 as *mut u8, 0, 64 * 1024);
 
diff --git a/syscall/process.rs b/syscall/process.rs
index d15c2394..f357d36c 100644
--- a/syscall/process.rs
+++ b/syscall/process.rs
@@ -34,6 +34,7 @@ pub fn brk(address: usize) -> Result<usize> {
             if active_table.translate_page(page).is_none() {
                 //println!("Not found - mapping");
                 active_table.map(page, entry::PRESENT | entry::WRITABLE | entry::NO_EXECUTE | entry::USER_ACCESSIBLE);
+                active_table.flush(page);
             } else {
                 //println!("Found - skipping");
             }
@@ -49,6 +50,7 @@ pub fn brk(address: usize) -> Result<usize> {
             if active_table.translate_page(page).is_some() {
                 //println!("Found - unmapping");
                 active_table.unmap(page);
+                active_table.flush(page);
             } else {
                 //println!("Not found - skipping");
             }
-- 
GitLab