diff --git a/Kernel/MemoryManager.cpp b/Kernel/MemoryManager.cpp index cc6185ca3f4..6bc79673185 100644 --- a/Kernel/MemoryManager.cpp +++ b/Kernel/MemoryManager.cpp @@ -38,7 +38,10 @@ void MemoryManager::initializePaging() kprintf("MM: Page table zero @ %p\n", m_pageTableZero); kprintf("MM: Page table one @ %p\n", m_pageTableOne); - identityMap(LinearAddress(0), 4 * MB); + // Make null dereferences crash. + protectMap(LinearAddress(0), 4 * KB); + + identityMap(LinearAddress(4096), 4 * MB); // Put pages between 4MB and 16MB in the page freelist. for (size_t i = (4 * MB) + 1024; i < (16 * MB); i += PAGE_SIZE) { @@ -79,6 +82,19 @@ auto MemoryManager::ensurePTE(LinearAddress linearAddress) -> PageTableEntry return PageTableEntry(&pde.pageTableBase()[pageTableIndex]); } +void MemoryManager::protectMap(LinearAddress linearAddress, size_t length) +{ + // FIXME: ASSERT(linearAddress is 4KB aligned); + for (dword offset = 0; offset < length; offset += 4096) { + auto pteAddress = linearAddress.offset(offset); + auto pte = ensurePTE(pteAddress); + pte.setPhysicalPageBase(pteAddress.get()); + pte.setUserAllowed(false); + pte.setPresent(false); + pte.setWritable(false); + } +} + void MemoryManager::identityMap(LinearAddress linearAddress, size_t length) { // FIXME: ASSERT(linearAddress is 4KB aligned); diff --git a/Kernel/MemoryManager.h b/Kernel/MemoryManager.h index 048834a21c0..7c6910a5da1 100644 --- a/Kernel/MemoryManager.h +++ b/Kernel/MemoryManager.h @@ -58,6 +58,7 @@ private: void initializePaging(); + void protectMap(LinearAddress, size_t length); void identityMap(LinearAddress, size_t length); Vector allocatePhysicalPages(size_t count);