From 90ebfe7f0a6aa9556e31bb55c6a9490f92a4a1ec Mon Sep 17 00:00:00 2001 From: Jonas 'Sortie' Termansen Date: Sun, 7 Aug 2011 00:47:36 +0200 Subject: [PATCH] Improvements to the address space switching function. It now only switches when needed and returns the old addr space. --- sortix/memorymanagement.cpp | 11 ++++++++--- sortix/memorymanagement.h | 2 +- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/sortix/memorymanagement.cpp b/sortix/memorymanagement.cpp index 2cb48229..4e43f3cd 100644 --- a/sortix/memorymanagement.cpp +++ b/sortix/memorymanagement.cpp @@ -226,7 +226,7 @@ namespace Sortix Table* BootstrapCreateTable(Dir* dir, addr_t where); void BootstrapMap(Dir* dir, addr_t where, addr_t physical); void BootstrapMapStructures(Dir* dir); - void SwitchDirectory(addr_t dir); + addr_t SwitchDirectory(addr_t dir); addr_t CreateDirectory(); #endif @@ -356,16 +356,21 @@ namespace Sortix return CreateDirectory(); } - void SwitchAddressSpace(addr_t addrspace) + addr_t SwitchAddressSpace(addr_t addrspace) { return SwitchDirectory(addrspace); } - void SwitchDirectory(addr_t dir) + addr_t SwitchDirectory(addr_t dir) { + // Don't switch if we are already there. + if ( dir == currentDirPhysical ) { return currentDirPhysical; } + + addr_t previous = currentDirPhysical; asm volatile("mov %0, %%cr3":: "r"(dir)); currentDirPhysical = dir; Flush(); + return previous; } addr_t CreateDirectory() diff --git a/sortix/memorymanagement.h b/sortix/memorymanagement.h index aa0ccdaf..f887c8dd 100644 --- a/sortix/memorymanagement.h +++ b/sortix/memorymanagement.h @@ -41,7 +41,7 @@ namespace Sortix void Init(); void Flush(); addr_t CreateAddressSpace(); - void SwitchAddressSpace(addr_t addrspace); + addr_t SwitchAddressSpace(addr_t addrspace); void MapKernel(addr_t where, addr_t physical); bool MapUser(addr_t where, addr_t physical); addr_t UnmapKernel(addr_t where);