[Zion] Mark pages as not present before calling invlpg.

This commit is contained in:
Drew Galbraith 2023-12-05 16:11:37 -08:00
parent 1f8085f791
commit 815a603c1c
6 changed files with 48 additions and 17 deletions

View file

@ -96,14 +96,14 @@ void CleanupPageStructure(uint64_t struct_phys, uint64_t level) {
phys_mem::FreePage(struct_phys);
}
} // namespace
uint64_t CurrCr3() {
uint64_t pml4_addr = 0;
asm volatile("mov %%cr3, %0;" : "=r"(pml4_addr));
return pml4_addr;
}
} // namespace
void InitializePml4(uint64_t pml4_physical_addr) {
uint64_t* pml4_virtual = reinterpret_cast<uint64_t*>(
boot::GetHigherHalfDirectMap() + pml4_physical_addr);
@ -167,6 +167,32 @@ void MapPage(uint64_t cr3, uint64_t vaddr, uint64_t paddr) {
}
}
void UnmapPage(uint64_t cr3, uint64_t vaddr) {
uint64_t* pml4_entry = Pml4Entry(cr3, vaddr);
if (!(*pml4_entry & PRESENT_BIT)) {
return;
}
uint64_t* pdp_entry = PageDirectoryPointerEntry(*pml4_entry, vaddr);
if (!(*pdp_entry & PRESENT_BIT)) {
return;
}
uint64_t* pd_entry = PageDirectoryEntry(*pdp_entry, vaddr);
if (!(*pd_entry & PRESENT_BIT)) {
return;
}
uint64_t* pt_entry = PageTableEntry(*pd_entry, vaddr);
if (!(*pt_entry & PRESENT_BIT)) {
return;
}
*pt_entry &= ~PRESENT_BIT;
if (cr3 == CurrCr3()) {
asm volatile("invlpg (%0)" : : "b"(vaddr) : "memory");
}
}
uint64_t AllocatePageIfNecessary(uint64_t addr) {
uint64_t cr3 = CurrCr3();
uint64_t phys = PagePhysIfResident(cr3, addr);

View file

@ -4,12 +4,11 @@
#include "object/process.h"
uint64_t CurrCr3();
void InitializePml4(uint64_t pml4_physical_addr);
void CleanupPml4(uint64_t pml4_physical_addr);
void MapPage(uint64_t cr3, uint64_t vaddr, uint64_t paddr);
void UnmapPage(uint64_t cr3, uint64_t vaddr);
uint64_t AllocatePageIfNecessary(uint64_t addr);
void EnsureResident(uint64_t addr, uint64_t size);