Skip to content

Commit

Permalink
Kernel: Add getter and setter for the X86 CR3 register
Browse files Browse the repository at this point in the history
This gets rid of a bunch of inline assembly.
  • Loading branch information
awesomekling committed Feb 10, 2020
1 parent 7323d08 commit 27f0102
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 31 deletions.
23 changes: 16 additions & 7 deletions Kernel/Arch/i386/CPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,9 +178,7 @@ static void dump(const RegisterDump& regs)
u32 cr2;
asm("movl %%cr2, %%eax"
: "=a"(cr2));
u32 cr3;
asm("movl %%cr3, %%eax"
: "=a"(cr3));
u32 cr3 = read_cr3();
u32 cr4;
asm("movl %%cr4, %%eax"
: "=a"(cr4));
Expand Down Expand Up @@ -271,11 +269,8 @@ void page_fault_handler(RegisterDump regs)
asm("movl %%cr2, %%eax"
: "=a"(fault_address));

u32 fault_page_directory;
asm("movl %%cr3, %%eax"
: "=a"(fault_page_directory));

#ifdef PAGE_FAULT_DEBUG
u32 fault_page_directory = read_cr3();
dbgprintf("%s(%u): ring%u %s page fault in PD=%x, %s%s V%08x\n",
current ? current->process().name().characters() : "(none)",
current ? current->pid() : 0,
Expand Down Expand Up @@ -716,3 +711,17 @@ void cpu_setup()
kprintf("x86: No RDRAND support detected. Randomness will be shitty\n");
}
}

u32 read_cr3()
{
u32 cr3;
asm("movl %%cr3, %%eax"
: "=a"(cr3));
return cr3;
}

void write_cr3(u32 cr3)
{
asm volatile("movl %%eax, %%cr3" ::"a"(cr3)
: "memory");
}
12 changes: 3 additions & 9 deletions Kernel/Arch/i386/CPU.h
Original file line number Diff line number Diff line change
Expand Up @@ -278,15 +278,6 @@ void handle_crash(RegisterDump&, const char* description, int signal);
: "memory")
#define memory_barrier() asm volatile("" :: \
: "memory")

inline u32 cpu_cr3()
{
u32 cr3;
asm volatile("movl %%cr3, %%eax"
: "=a"(cr3));
return cr3;
}

inline u32 cpu_flags()
{
u32 flags;
Expand Down Expand Up @@ -473,6 +464,9 @@ inline uintptr_t offset_in_page(const void* address)
return offset_in_page((uintptr_t)address);
}

u32 read_cr3();
void write_cr3(u32);

class CPUID {
public:
CPUID(u32 function) { asm volatile("cpuid"
Expand Down
21 changes: 6 additions & 15 deletions Kernel/VM/MemoryManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,8 @@ MemoryManager& MM
MemoryManager::MemoryManager()
{
m_kernel_page_directory = PageDirectory::create_kernel_page_directory();

parse_memory_map();

asm volatile("movl %%eax, %%cr3" ::"a"(kernel_page_directory().cr3()));

write_cr3(kernel_page_directory().cr3());
setup_low_identity_mapping();
protect_kernel_image();
}
Expand Down Expand Up @@ -266,7 +263,7 @@ Region* MemoryManager::region_from_vaddr(VirtualAddress vaddr)
{
if (auto* region = kernel_region_from_vaddr(vaddr))
return region;
auto page_directory = PageDirectory::find_by_cr3(cpu_cr3());
auto page_directory = PageDirectory::find_by_cr3(read_cr3());
if (!page_directory)
return nullptr;
ASSERT(page_directory->process());
Expand Down Expand Up @@ -475,16 +472,12 @@ void MemoryManager::enter_process_paging_scope(Process& process)
InterruptDisabler disabler;

current->tss().cr3 = process.page_directory().cr3();
asm volatile("movl %%eax, %%cr3" ::"a"(process.page_directory().cr3())
: "memory");
write_cr3(process.page_directory().cr3());
}

void MemoryManager::flush_entire_tlb()
{
asm volatile(
"mov %%cr3, %%eax\n"
"mov %%eax, %%cr3\n" ::
: "%eax", "memory");
write_cr3(read_cr3());
}

void MemoryManager::flush_tlb(VirtualAddress vaddr)
Expand Down Expand Up @@ -676,15 +669,13 @@ void MemoryManager::dump_kernel_regions()
ProcessPagingScope::ProcessPagingScope(Process& process)
{
ASSERT(current);
asm("movl %%cr3, %%eax"
: "=a"(m_previous_cr3));
m_previous_cr3 = read_cr3();
MM.enter_process_paging_scope(process);
}

ProcessPagingScope::~ProcessPagingScope()
{
InterruptDisabler disabler;
current->tss().cr3 = m_previous_cr3;
asm volatile("movl %%eax, %%cr3" ::"a"(m_previous_cr3)
: "memory");
write_cr3(m_previous_cr3);
}

0 comments on commit 27f0102

Please sign in to comment.