1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-06-01 08:08:10 +00:00

Kernel: Add getter and setter for the X86 CR3 register

This gets rid of a bunch of inline assembly.
This commit is contained in:
Andreas Kling 2020-02-10 20:00:32 +01:00
parent 7323d085dd
commit 27f0102bbe
3 changed files with 25 additions and 31 deletions

View file

@ -178,9 +178,7 @@ static void dump(const RegisterDump& regs)
u32 cr2; u32 cr2;
asm("movl %%cr2, %%eax" asm("movl %%cr2, %%eax"
: "=a"(cr2)); : "=a"(cr2));
u32 cr3; u32 cr3 = read_cr3();
asm("movl %%cr3, %%eax"
: "=a"(cr3));
u32 cr4; u32 cr4;
asm("movl %%cr4, %%eax" asm("movl %%cr4, %%eax"
: "=a"(cr4)); : "=a"(cr4));
@ -271,11 +269,8 @@ void page_fault_handler(RegisterDump regs)
asm("movl %%cr2, %%eax" asm("movl %%cr2, %%eax"
: "=a"(fault_address)); : "=a"(fault_address));
u32 fault_page_directory;
asm("movl %%cr3, %%eax"
: "=a"(fault_page_directory));
#ifdef PAGE_FAULT_DEBUG #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", dbgprintf("%s(%u): ring%u %s page fault in PD=%x, %s%s V%08x\n",
current ? current->process().name().characters() : "(none)", current ? current->process().name().characters() : "(none)",
current ? current->pid() : 0, current ? current->pid() : 0,
@ -716,3 +711,17 @@ void cpu_setup()
kprintf("x86: No RDRAND support detected. Randomness will be shitty\n"); 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");
}

View file

@ -278,15 +278,6 @@ void handle_crash(RegisterDump&, const char* description, int signal);
: "memory") : "memory")
#define memory_barrier() asm volatile("" :: \ #define memory_barrier() asm volatile("" :: \
: "memory") : "memory")
inline u32 cpu_cr3()
{
u32 cr3;
asm volatile("movl %%cr3, %%eax"
: "=a"(cr3));
return cr3;
}
inline u32 cpu_flags() inline u32 cpu_flags()
{ {
u32 flags; u32 flags;
@ -473,6 +464,9 @@ inline uintptr_t offset_in_page(const void* address)
return offset_in_page((uintptr_t)address); return offset_in_page((uintptr_t)address);
} }
u32 read_cr3();
void write_cr3(u32);
class CPUID { class CPUID {
public: public:
CPUID(u32 function) { asm volatile("cpuid" CPUID(u32 function) { asm volatile("cpuid"

View file

@ -49,11 +49,8 @@ MemoryManager& MM
MemoryManager::MemoryManager() MemoryManager::MemoryManager()
{ {
m_kernel_page_directory = PageDirectory::create_kernel_page_directory(); m_kernel_page_directory = PageDirectory::create_kernel_page_directory();
parse_memory_map(); parse_memory_map();
write_cr3(kernel_page_directory().cr3());
asm volatile("movl %%eax, %%cr3" ::"a"(kernel_page_directory().cr3()));
setup_low_identity_mapping(); setup_low_identity_mapping();
protect_kernel_image(); protect_kernel_image();
} }
@ -266,7 +263,7 @@ Region* MemoryManager::region_from_vaddr(VirtualAddress vaddr)
{ {
if (auto* region = kernel_region_from_vaddr(vaddr)) if (auto* region = kernel_region_from_vaddr(vaddr))
return region; return region;
auto page_directory = PageDirectory::find_by_cr3(cpu_cr3()); auto page_directory = PageDirectory::find_by_cr3(read_cr3());
if (!page_directory) if (!page_directory)
return nullptr; return nullptr;
ASSERT(page_directory->process()); ASSERT(page_directory->process());
@ -475,16 +472,12 @@ void MemoryManager::enter_process_paging_scope(Process& process)
InterruptDisabler disabler; InterruptDisabler disabler;
current->tss().cr3 = process.page_directory().cr3(); current->tss().cr3 = process.page_directory().cr3();
asm volatile("movl %%eax, %%cr3" ::"a"(process.page_directory().cr3()) write_cr3(process.page_directory().cr3());
: "memory");
} }
void MemoryManager::flush_entire_tlb() void MemoryManager::flush_entire_tlb()
{ {
asm volatile( write_cr3(read_cr3());
"mov %%cr3, %%eax\n"
"mov %%eax, %%cr3\n" ::
: "%eax", "memory");
} }
void MemoryManager::flush_tlb(VirtualAddress vaddr) void MemoryManager::flush_tlb(VirtualAddress vaddr)
@ -676,8 +669,7 @@ void MemoryManager::dump_kernel_regions()
ProcessPagingScope::ProcessPagingScope(Process& process) ProcessPagingScope::ProcessPagingScope(Process& process)
{ {
ASSERT(current); ASSERT(current);
asm("movl %%cr3, %%eax" m_previous_cr3 = read_cr3();
: "=a"(m_previous_cr3));
MM.enter_process_paging_scope(process); MM.enter_process_paging_scope(process);
} }
@ -685,6 +677,5 @@ ProcessPagingScope::~ProcessPagingScope()
{ {
InterruptDisabler disabler; InterruptDisabler disabler;
current->tss().cr3 = m_previous_cr3; current->tss().cr3 = m_previous_cr3;
asm volatile("movl %%eax, %%cr3" ::"a"(m_previous_cr3) write_cr3(m_previous_cr3);
: "memory");
} }