Working on Paging API #67
This commit is contained in:
parent
ee73150891
commit
40cd623b97
|
@ -14,8 +14,6 @@
|
||||||
pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
|
pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
|
||||||
ulong *MmPhysicalPageTable;
|
ulong *MmPhysicalPageTable;
|
||||||
|
|
||||||
extern MemoryMap_t memoryMap;
|
|
||||||
|
|
||||||
extern ulong _text;
|
extern ulong _text;
|
||||||
extern ulong _text_end;
|
extern ulong _text_end;
|
||||||
extern ulong _rodata;
|
extern ulong _rodata;
|
||||||
|
@ -23,9 +21,11 @@ extern ulong _rodata_end;
|
||||||
extern ulong _data;
|
extern ulong _data;
|
||||||
extern ulong _data_end;
|
extern ulong _data_end;
|
||||||
|
|
||||||
|
extern MemoryMap_t memoryMap;
|
||||||
|
|
||||||
ulong MmStackGuards[2] = { 0 };
|
ulong MmStackGuards[2] = { 0 };
|
||||||
ulong MmVirtLastAddress = 0;
|
ulong MmVirtLastAddress = 0;
|
||||||
ulong MmPhysLastKernelAddress = 0;
|
ulong MmPhysLastKernAddress = 0;
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
|
@ -59,8 +59,8 @@ void MmInitPaging(void)
|
||||||
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
||||||
|
|
||||||
// Difference between the end of kernel and the begin of userspace
|
// Difference between the end of kernel and the begin of userspace
|
||||||
MmPhysLastKernelAddress = (ulong)(_heap_start + _heap_max);
|
MmPhysLastKernAddress = (ulong)(_heap_start + _heap_max);
|
||||||
ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastKernelAddress - KPAGESIZE;
|
ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastKernAddress - KPAGESIZE;
|
||||||
|
|
||||||
// Maximum VIRTUAL address in memory
|
// Maximum VIRTUAL address in memory
|
||||||
MmVirtLastAddress = phRamSize + diffKernUsr;
|
MmVirtLastAddress = phRamSize + diffKernUsr;
|
||||||
|
@ -161,22 +161,22 @@ void MmInitPaging(void)
|
||||||
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
|
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
|
||||||
}
|
}
|
||||||
// While we're inside the kernel pages
|
// While we're inside the kernel pages
|
||||||
else if ((ulong)curAddrPT <= MmPhysLastKernelAddress) {
|
else if ((ulong)curAddrPT <= MmPhysLastKernAddress) {
|
||||||
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
|
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
|
||||||
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
||||||
|
|
||||||
if ((ulong)curAddrPT == MmPhysLastKernelAddress) {
|
if ((ulong)curAddrPT == MmPhysLastKernAddress) {
|
||||||
//DebugLog("\tLast page of kernel at %p\n", curAddrPT);
|
//DebugLog("\tLast page of kernel at %p\n", curAddrPT);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
// While we're inside the userspace pages
|
// While we're inside the userspace pages
|
||||||
else if ((ulong)curAddrPT >= USERSPACE) {
|
else if ((ulong)curAddrPT >= USERSPACE) {
|
||||||
MmPT[index] = ((ulong)curAddrPT - diffKernUsr); // Not present for instance, unallocated
|
MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance
|
||||||
xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE));
|
xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE));
|
||||||
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
||||||
|
|
||||||
if ((ulong)curAddrPT == USERSPACE) {
|
if ((ulong)curAddrPT == USERSPACE) {
|
||||||
//DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr);
|
DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
|
@ -229,7 +229,7 @@ void *MmTransVirtToPhyAddr(void* virtualAddr)
|
||||||
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
|
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
|
||||||
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
||||||
|
|
||||||
if (*page & PRESENT) {
|
if (*page == (*page & ~(KPAGESIZE - 1))) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -241,7 +241,7 @@ void *MmTransPhyToVirtAddr(void* physicalAddr)
|
||||||
ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1));
|
ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1));
|
||||||
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
|
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
|
||||||
/ ((ulong)KPAGESIZE)
|
/ ((ulong)KPAGESIZE)
|
||||||
+ ((ulong)physicalAddr - phyAddrPage) ] );
|
] + ((ulong)physicalAddr - phyAddrPage));
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
|
@ -287,13 +287,13 @@ void MmUnmapPage(void* virtualAddr)
|
||||||
{
|
{
|
||||||
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
||||||
|
|
||||||
*page &= (~PRESENT);
|
*page = 0;
|
||||||
|
|
||||||
KeFlushTlbSingle(*page);
|
KeFlushTlbSingle(*page);
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Find physical unallocated pages
|
// Find a free block of pages
|
||||||
//
|
//
|
||||||
void *MmGetPhyPageBlock(size_t size, bool usermode) {
|
void *MmGetPhyPageBlock(size_t size, bool usermode) {
|
||||||
void *startPhyPage = 0;
|
void *startPhyPage = 0;
|
||||||
|
@ -307,7 +307,7 @@ void *MmGetPhyPageBlock(size_t size, bool usermode) {
|
||||||
|
|
||||||
if (!usermode) {
|
if (!usermode) {
|
||||||
startPhyPage = MmTransVirtToPhyAddr((void*)KPAGESIZE + 1);
|
startPhyPage = MmTransVirtToPhyAddr((void*)KPAGESIZE + 1);
|
||||||
endPhyPage = (void*)MmPhysLastKernelAddress;
|
endPhyPage = (void*)MmPhysLastKernAddress;
|
||||||
} else {
|
} else {
|
||||||
startPhyPage = MmTransVirtToPhyAddr((void*)USERSPACE);
|
startPhyPage = MmTransVirtToPhyAddr((void*)USERSPACE);
|
||||||
endPhyPage = (void*)(phRamSize & ~(KPAGESIZE - 1));
|
endPhyPage = (void*)(phRamSize & ~(KPAGESIZE - 1));
|
||||||
|
|
Loading…
Reference in New Issue