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);