mirror of
https://gitlab.os-k.eu/os-k-team/os-k.git
synced 2023-08-25 14:03:10 +02:00
Working on Paging API
This commit is contained in:
parent
4a652d7084
commit
ee73150891
@ -14,6 +14,8 @@
|
||||
pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
|
||||
ulong *MmPhysicalPageTable;
|
||||
|
||||
extern MemoryMap_t memoryMap;
|
||||
|
||||
extern ulong _text;
|
||||
extern ulong _text_end;
|
||||
extern ulong _rodata;
|
||||
@ -23,7 +25,7 @@ extern ulong _data_end;
|
||||
|
||||
ulong MmStackGuards[2] = { 0 };
|
||||
ulong MmVirtLastAddress = 0;
|
||||
ulong MmPhysLastAddress = 0;
|
||||
ulong MmPhysLastKernelAddress = 0;
|
||||
|
||||
enum
|
||||
{
|
||||
@ -45,7 +47,6 @@ enum
|
||||
//
|
||||
void MmInitPaging(void)
|
||||
{
|
||||
extern MemoryMap_t memoryMap;
|
||||
pdpe_t *MmPDP = NULL;
|
||||
pde_t *MmPD = NULL;
|
||||
pte_t *MmPT = NULL;
|
||||
@ -58,8 +59,8 @@ void MmInitPaging(void)
|
||||
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
||||
|
||||
// Difference between the end of kernel and the begin of userspace
|
||||
MmPhysLastAddress = (ulong)(_heap_start + _heap_max);
|
||||
ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastAddress - KPAGESIZE;
|
||||
MmPhysLastKernelAddress = (ulong)(_heap_start + _heap_max);
|
||||
ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastKernelAddress - KPAGESIZE;
|
||||
|
||||
// Maximum VIRTUAL address in memory
|
||||
MmVirtLastAddress = phRamSize + diffKernUsr;
|
||||
@ -160,11 +161,11 @@ void MmInitPaging(void)
|
||||
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
|
||||
}
|
||||
// While we're inside the kernel pages
|
||||
else if ((ulong)curAddrPT <= MmPhysLastAddress) {
|
||||
else if ((ulong)curAddrPT <= MmPhysLastKernelAddress) {
|
||||
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
|
||||
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
||||
|
||||
if ((ulong)curAddrPT == MmPhysLastAddress) {
|
||||
if ((ulong)curAddrPT == MmPhysLastKernelAddress) {
|
||||
//DebugLog("\tLast page of kernel at %p\n", curAddrPT);
|
||||
}
|
||||
}
|
||||
@ -284,23 +285,51 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
|
||||
//
|
||||
void MmUnmapPage(void* virtualAddr)
|
||||
{
|
||||
MmUnsetPage(virtualAddr, PRESENT); // Removing the present flag
|
||||
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
||||
|
||||
*page &= (~PRESENT);
|
||||
|
||||
KeFlushTlbSingle(*page);
|
||||
}
|
||||
|
||||
//
|
||||
// Find physical unallocated pages
|
||||
//
|
||||
void *MmGetPhyPageBlock(size_t size, bool usermode) {
|
||||
void *startPage = 0;
|
||||
void *startPhyPage = 0;
|
||||
void *endPhyPage = 0;
|
||||
size_t curSize = 0;
|
||||
|
||||
// Maximum PHYSICAL address in memory
|
||||
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
||||
|
||||
ulong curVirtAddr = 0;
|
||||
|
||||
if (!usermode) {
|
||||
startPage = MmTransPhyToVirtAddr(0);
|
||||
startPhyPage = MmTransVirtToPhyAddr((void*)KPAGESIZE + 1);
|
||||
endPhyPage = (void*)MmPhysLastKernelAddress;
|
||||
} else {
|
||||
startPage = MmTransVirtToPhyAddr((void*)USERSPACE);
|
||||
startPhyPage = MmTransVirtToPhyAddr((void*)USERSPACE);
|
||||
endPhyPage = (void*)(phRamSize & ~(KPAGESIZE - 1));
|
||||
}
|
||||
|
||||
DebugLog("Userspace at %p\n", (void*)USERSPACE);
|
||||
DebugLog("Userspace physical at %p\n", MmTransVirtToPhyAddr((void*)USERSPACE));
|
||||
DebugLog("Start phy at %p\n", startPhyPage);
|
||||
DebugLog("End phy at %p\n", endPhyPage);
|
||||
|
||||
for (ulong curPhyAddr = (ulong)startPhyPage;
|
||||
curPhyAddr <= (ulong)endPhyPage;
|
||||
curPhyAddr += KPAGESIZE) {
|
||||
curVirtAddr = (ulong)MmTransPhyToVirtAddr((void*)curPhyAddr);
|
||||
|
||||
if (curVirtAddr == 0) {
|
||||
DebugLog("CurrentAddr %p\n", curVirtAddr);
|
||||
DebugLog("\t Free !\n");
|
||||
curSize += KPAGESIZE;
|
||||
}
|
||||
|
||||
if (curSize >= size)
|
||||
break;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user