diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index d3265e5..653ffb9 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -36,6 +36,8 @@ enum NX = 1UL << 63 }; +//----------- + // // Creates our new page table structure and loads it // @@ -177,26 +179,38 @@ void MmInitPaging(void) DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB); } +// +// Get a page from an address +// +static pte_t MmGetPageDescriptor(void *virtualAddr) +{ + ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + + if (virtAddrPage > MmVirtLastAddress) { + KeStartPanic("MmSetPage() Out of bound of the address space !"); + } + + pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1)); + //DebugLog("pdp\t: %p\n", pdp); + pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1)); + //DebugLog("pd\t: %p\n", pd); + pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); + //DebugLog("pt\t: %p\n", pt); + + ulong page = (ulong)pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; + //DebugLog("page (with flags): %p\n", page); + + return page; +} + + // // Translates a virtual address to its physical equivalent // void *MmTransVirtToPhyAddr(void* virtualAddr) { ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); - - if (virtAddrPage > MmVirtLastAddress) { - KeStartPanic("MmTransVirtToPhyAddr() Out of bound of the address space !"); - } - - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pdp\t: %p\n", pdp); - pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pd\t: %p\n", pd); - pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); - DebugLog("pt\t: %p\n", pt); - - ulong page = (ulong)pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; - DebugLog("page (with flags): %p\n", page); + pte_t page = MmGetPageDescriptor(virtualAddr); if (page == (page & ~(KPAGESIZE - 1))) { return NULL; @@ -215,19 +229,11 @@ void *MmTransPhyToVirtAddr(void* physicalAddr) // void MmSetPage(void* virtualAddr, ulong flags) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + pte_t page = MmGetPageDescriptor(virtualAddr); - if (virtAddrPage > MmVirtLastAddress) { - KeStartPanic("MmSetPage() Out of bound of the address space !"); - } + page |= flags; - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)] & ~(KPAGESIZE - 1)); - pde_t *pd = (pde_t*)( (ulong)pdp[virtAddrPage / ((ulong)KPAGESIZE * 0x40000)] & ~(KPAGESIZE - 1)); - pte_t *pt = (pte_t*)( (ulong)pd[virtAddrPage / ((ulong)KPAGESIZE * 0x200)] & ~(KPAGESIZE - 1)); - - pt[virtAddrPage / ((ulong)KPAGESIZE)] |= flags; - - KeFlushTlbSingle(virtAddrPage); + KeFlushTlbSingle(page); } // @@ -235,21 +241,31 @@ void MmSetPage(void* virtualAddr, ulong flags) // void MmUnsetPage(void* virtualAddr, ulong flags) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + pte_t page = MmGetPageDescriptor(virtualAddr); - if (virtAddrPage > MmVirtLastAddress) { - KeStartPanic("MmUnsetPage() Out of bound of the address space !"); - } + page &= (~flags); - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)] & ~(KPAGESIZE - 1)); - pde_t *pd = (pde_t*)( (ulong)pdp[virtAddrPage / ((ulong)KPAGESIZE * 0x40000)] & ~(KPAGESIZE - 1)); - pte_t *pt = (pte_t*)( (ulong)pd[virtAddrPage / ((ulong)KPAGESIZE * 0x200)] & ~(KPAGESIZE - 1)); - - pt[virtAddrPage / ((ulong)KPAGESIZE)] &= (~flags); - - KeFlushTlbSingle(virtAddrPage); + KeFlushTlbSingle(page); } +// +// Map a page in memory +// +void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) +{ + +} + +// +// Unmap a page in memory +// +void MmUnmapPage(void* virtualAddr) +{ + +} + +//----------- + // // Returns the rank of the Stack Guards //