2019-04-03 15:13:07 +02:00
|
|
|
#include <Kernel/Process.h>
|
|
|
|
|
#include <Kernel/Thread.h>
|
2019-06-07 11:43:58 +02:00
|
|
|
#include <Kernel/VM/MemoryManager.h>
|
|
|
|
|
#include <Kernel/VM/PageDirectory.h>
|
2019-04-03 15:13:07 +02:00
|
|
|
|
2019-07-03 21:17:35 +02:00
|
|
|
static const u32 userspace_range_base = 0x01000000;
|
|
|
|
|
static const u32 kernelspace_range_base = 0xc0000000;
|
2019-05-20 04:46:29 +02:00
|
|
|
|
2019-12-25 02:46:17 +01:00
|
|
|
static HashMap<u32, PageDirectory*>& cr3_map()
|
2019-08-06 11:19:16 +02:00
|
|
|
{
|
|
|
|
|
ASSERT_INTERRUPTS_DISABLED();
|
|
|
|
|
static HashMap<u32, PageDirectory*>* map;
|
|
|
|
|
if (!map)
|
|
|
|
|
map = new HashMap<u32, PageDirectory*>;
|
|
|
|
|
return *map;
|
|
|
|
|
}
|
|
|
|
|
|
2019-12-25 02:46:17 +01:00
|
|
|
RefPtr<PageDirectory> PageDirectory::find_by_cr3(u32 cr3)
|
2019-08-06 11:19:16 +02:00
|
|
|
{
|
|
|
|
|
InterruptDisabler disabler;
|
2019-12-25 02:46:17 +01:00
|
|
|
return cr3_map().get(cr3).value_or({});
|
2019-08-06 11:19:16 +02:00
|
|
|
}
|
|
|
|
|
|
2019-04-03 15:13:07 +02:00
|
|
|
PageDirectory::PageDirectory(PhysicalAddress paddr)
|
2019-11-23 17:27:09 +01:00
|
|
|
: m_range_allocator(VirtualAddress(0xc0000000), 0x3f000000)
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
2019-06-14 17:10:49 +03:00
|
|
|
m_directory_page = PhysicalPage::create(paddr, true, false);
|
2019-08-06 11:19:16 +02:00
|
|
|
InterruptDisabler disabler;
|
2019-12-25 02:46:17 +01:00
|
|
|
cr3_map().set(cr3(), this);
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-08-06 11:19:16 +02:00
|
|
|
PageDirectory::PageDirectory(Process& process, const RangeAllocator* parent_range_allocator)
|
|
|
|
|
: m_process(&process)
|
|
|
|
|
, m_range_allocator(parent_range_allocator ? RangeAllocator(*parent_range_allocator) : RangeAllocator(VirtualAddress(userspace_range_base), kernelspace_range_base - userspace_range_base))
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
|
|
|
|
MM.populate_page_directory(*this);
|
2019-08-06 11:19:16 +02:00
|
|
|
InterruptDisabler disabler;
|
2019-12-25 02:46:17 +01:00
|
|
|
cr3_map().set(cr3(), this);
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
PageDirectory::~PageDirectory()
|
|
|
|
|
{
|
|
|
|
|
#ifdef MM_DEBUG
|
|
|
|
|
dbgprintf("MM: ~PageDirectory K%x\n", this);
|
|
|
|
|
#endif
|
2019-08-06 11:19:16 +02:00
|
|
|
InterruptDisabler disabler;
|
2019-12-25 02:46:17 +01:00
|
|
|
cr3_map().remove(cr3());
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
|
|
|
|
|
2019-06-07 12:56:50 +02:00
|
|
|
void PageDirectory::flush(VirtualAddress vaddr)
|
2019-04-03 15:13:07 +02:00
|
|
|
{
|
|
|
|
|
#ifdef MM_DEBUG
|
2019-08-23 16:18:27 +02:00
|
|
|
dbgprintf("MM: Flush page V%p\n", vaddr.get());
|
2019-04-03 15:13:07 +02:00
|
|
|
#endif
|
|
|
|
|
if (!current)
|
|
|
|
|
return;
|
2019-06-01 17:25:36 +02:00
|
|
|
if (this == &MM.kernel_page_directory() || ¤t->process().page_directory() == this)
|
2019-06-07 12:56:50 +02:00
|
|
|
MM.flush_tlb(vaddr);
|
2019-04-03 15:13:07 +02:00
|
|
|
}
|
2019-11-27 12:40:42 +01:00
|
|
|
|
|
|
|
|
void PageDirectory::update_kernel_mappings()
|
|
|
|
|
{
|
|
|
|
|
// This ensures that the kernel virtual address space is up-to-date in this page directory.
|
|
|
|
|
// This may be necessary to avoid triple faulting when entering a process's paging scope
|
|
|
|
|
// whose mappings are out-of-date.
|
|
|
|
|
memcpy(entries() + 768, MM.kernel_page_directory().entries() + 768, sizeof(PageDirectoryEntry) * 256);
|
|
|
|
|
}
|