Protect the first 4 KB of memory.

This makes null pointers crashy, tremendously useful :^)
This commit is contained in:
Andreas Kling 2018-10-21 21:57:59 +02:00
parent dd6706a1a1
commit d5ec18027e
Notes: sideshowbarker 2024-07-19 18:45:59 +09:00
2 changed files with 18 additions and 1 deletions

View file

@ -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);

View file

@ -58,6 +58,7 @@ private:
void initializePaging();
void protectMap(LinearAddress, size_t length);
void identityMap(LinearAddress, size_t length);
Vector<PhysicalAddress> allocatePhysicalPages(size_t count);