x86_64/MMU: Map the kernel page directory to virtual memory

This avoids depending on the kernel address space to create a new userspace one,
since there is no physical memory access.

This was fine for a single process, since its address space was created from the kernel one
and no more address spaces were created,
but for two or more, this started to become problematic, since we would create one address space
while being in another process's address space, which has no direct mapping of physical memory.
This commit is contained in:
apio 2023-01-22 14:46:03 +01:00
parent a7a38d3433
commit d5b1d72396
Signed by: apio
GPG Key ID: B8A7D06E42258954

View File

@ -1,4 +1,5 @@
#include "arch/MMU.h"
#include "Log.h"
#include "memory/MemoryManager.h"
#include <luna/CString.h>
#include <luna/Result.h>
@ -9,6 +10,7 @@
#pragma GCC diagnostic ignored "-Wconversion"
PageDirectory* g_kernel_directory;
u64 g_kernel_directory_virt;
void PageTableEntry::set_address(u64 addr)
{
@ -278,12 +280,18 @@ namespace MMU
{
PageDirectory* const dir = get_page_directory();
g_kernel_directory = dir;
const u64 paddr = (u64)dir;
PageTableEntry& recursive_entry = dir->entries[rindex];
recursive_entry.read_write = true;
recursive_entry.present = true;
recursive_entry.set_address(paddr);
flush_all();
g_kernel_directory_virt =
MemoryManager::get_kernel_mapping_for_frames((u64)dir, 1, MMU::ReadWrite | MMU::NoExecute).value();
kdbgln("MMU init page directory (ring0): virt %#.16lx, phys %p", g_kernel_directory_virt, g_kernel_directory);
}
Result<PageDirectory*> create_page_directory_for_userspace()
@ -298,7 +306,9 @@ namespace MMU
recursive_entry.present = true;
recursive_entry.set_address(directory_phys);
directory->entries[511] = g_kernel_directory->entries[511];
kdbgln("MMU init page directory (ring3): virt %p, phys %#.16lx", directory, directory_phys);
directory->entries[511] = ((PageDirectory*)g_kernel_directory_virt)->entries[511];
// From now on, we're only going to use the physical address, since accessing the PageDirectory will be dealt
// with using recursive mapping. So let's make sure we don't leak any VM.