Working on Paging API

This commit is contained in:
Adrien Bourmault 2020-01-10 22:41:25 +01:00
parent 5fba767e64
commit 4a652d7084
4 changed files with 44 additions and 37 deletions

View File

@ -188,6 +188,9 @@ void MmUnSetPage(void* virtualAddr, ulong flags);
void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags);
void MmUnmapPage(void* virtualAddr);
// Allocations
void *MmGetPhyPageBlock(size_t size, bool usermode);
// Page table entry
typedef ulong pte_t;

View File

@ -44,7 +44,7 @@ void KeGetCpuInfos(void)
CpuInfo.frequency = KeGetCpuFrequency();
}
DebugLog("\tCPU %s %#d KHz detected with features %#x\n",
DebugLog("\tCPU %s %#d MHz detected with features %#x\n",
CpuInfo.vendorStr,
(long)(CpuInfo.frequency / 1000.0),
CpuInfo.featureFlag

View File

@ -23,6 +23,7 @@ extern ulong _data_end;
ulong MmStackGuards[2] = { 0 };
ulong MmVirtLastAddress = 0;
ulong MmPhysLastAddress = 0;
enum
{
@ -57,8 +58,8 @@ void MmInitPaging(void)
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
// Difference between the end of kernel and the begin of userspace
ulong lastKernelAddr = (ulong)(_heap_start + _heap_max);
ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE;
MmPhysLastAddress = (ulong)(_heap_start + _heap_max);
ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastAddress - KPAGESIZE;
// Maximum VIRTUAL address in memory
MmVirtLastAddress = phRamSize + diffKernUsr;
@ -132,13 +133,13 @@ void MmInitPaging(void)
MmPT[index] = (ulong)curAddrPT | PRESENT;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
MmStackGuards[0] = (ulong)curAddrPT;
DebugLog("\tStack Guard at %p\n", curAddrPT);
//DebugLog("\tStack Guard at %p\n", curAddrPT);
}
else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) {
MmPT[index] = (ulong)curAddrPT | PRESENT;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
MmStackGuards[1] = (ulong)curAddrPT;
DebugLog("\tStack Guard at %p\n", curAddrPT);
//DebugLog("\tStack Guard at %p\n", curAddrPT);
}
// SECTION .TEXT PROTECTION
else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) {
@ -159,22 +160,22 @@ void MmInitPaging(void)
//DebugLog("\tSection .rodata at %p\n", curAddrPT);
}
// While we're inside the kernel pages
else if ((ulong)curAddrPT <= lastKernelAddr) {
else if ((ulong)curAddrPT <= MmPhysLastAddress) {
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
if ((ulong)curAddrPT == lastKernelAddr) {
if ((ulong)curAddrPT == MmPhysLastAddress) {
//DebugLog("\tLast page of kernel at %p\n", curAddrPT);
}
}
// While we're inside the userspace pages
else if ((ulong)curAddrPT >= USERSPACE) {
MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance
MmPT[index] = ((ulong)curAddrPT - diffKernUsr); // Not present for instance, unallocated
xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE));
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
if ((ulong)curAddrPT == USERSPACE) {
DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr);
//DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr);
}
}
else {
@ -227,7 +228,7 @@ void *MmTransVirtToPhyAddr(void* virtualAddr)
ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1));
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
if (*page == (*page & ~(KPAGESIZE - 1))) {
if (*page & PRESENT) {
return NULL;
}
@ -283,31 +284,23 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
//
void MmUnmapPage(void* virtualAddr)
{
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
*page = 0;
KeFlushTlbSingle(*page);
MmUnsetPage(virtualAddr, PRESENT); // Removing the present flag
}
//
// Kernel Page allocator
// Find physical unallocated pages
//
void *MmKAllocPageBlock(void *start) {
pte_t *startPage = MmGetPageDescriptorFromVirtual(start);
void *MmGetPhyPageBlock(size_t size, bool usermode) {
void *startPage = 0;
//for (ulong curPage = 0; curPage < )
if (!usermode) {
startPage = MmTransPhyToVirtAddr(0);
} else {
startPage = MmTransVirtToPhyAddr((void*)USERSPACE);
}
return NULL;
}
//
// User page allocator
//
void *MmUAllocPageBlock(void *start) {
pte_t *startPage = MmGetPageDescriptorFromVirtual(start);
//for (ulong curPage = 0; curPage < )
DebugLog("Userspace at %p\n", (void*)USERSPACE);
DebugLog("Userspace physical at %p\n", MmTransVirtToPhyAddr((void*)USERSPACE));
return NULL;
}

View File

@ -204,6 +204,20 @@ error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline)
return EOK;
}
error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
{
void *address = (void*)strtoul(argv[1], NULL, 16);
/* if (!(void*)atoul(argv[1])) { */
/* address = (ulong *)0x80000000; */
/* } */
void *translation = MmTransPhyToVirtAddr(address);
KernLog("Translation of %p is %p\n", address, translation);
return EOK;
}
enum
{
PRESENT = 1 << 0,
@ -236,17 +250,13 @@ error_t CmdPageUnmap(int argc, char **argv, char *cmdline)
return EOK;
}
error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
error_t CmdPageBlock(int argc, char **argv, char *cmdline)
{
void *address = (void*)strtoul(argv[1], NULL, 16);
size_t size = (size_t)atoi(argv[1]);
bool usermode = (bool)atoi(argv[2]);
/* if (!(void*)atoul(argv[1])) { */
/* address = (ulong *)0x80000000; */
/* } */
MmGetPhyPageBlock(size, usermode);
void *translation = MmTransPhyToVirtAddr(address);
KernLog("Translation of %p is %p\n", address, translation);
return EOK;
}
@ -316,6 +326,7 @@ static Command_t testcmdtable[] =
" virtual address (paging)"},
{ "pmap", CmdPageMap, "Map a page to given physical addr" },
{ "punmap", CmdPageUnmap, "Unmap a page" },
{ "pblck", CmdPageBlock, "Find a free block of pages" },
{ "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"},
{ "shell", CmdShell, "Start a new shell (nested)", },
{ "stkov", CmdStackOverflow, "Provoke a stack overflow" },