Working on Paging API
This commit is contained in:
parent
5fba767e64
commit
4a652d7084
|
@ -188,6 +188,9 @@ void MmUnSetPage(void* virtualAddr, ulong flags);
|
||||||
void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags);
|
void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags);
|
||||||
void MmUnmapPage(void* virtualAddr);
|
void MmUnmapPage(void* virtualAddr);
|
||||||
|
|
||||||
|
// Allocations
|
||||||
|
void *MmGetPhyPageBlock(size_t size, bool usermode);
|
||||||
|
|
||||||
// Page table entry
|
// Page table entry
|
||||||
typedef ulong pte_t;
|
typedef ulong pte_t;
|
||||||
|
|
||||||
|
|
|
@ -44,7 +44,7 @@ void KeGetCpuInfos(void)
|
||||||
CpuInfo.frequency = KeGetCpuFrequency();
|
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,
|
CpuInfo.vendorStr,
|
||||||
(long)(CpuInfo.frequency / 1000.0),
|
(long)(CpuInfo.frequency / 1000.0),
|
||||||
CpuInfo.featureFlag
|
CpuInfo.featureFlag
|
||||||
|
|
|
@ -23,6 +23,7 @@ extern ulong _data_end;
|
||||||
|
|
||||||
ulong MmStackGuards[2] = { 0 };
|
ulong MmStackGuards[2] = { 0 };
|
||||||
ulong MmVirtLastAddress = 0;
|
ulong MmVirtLastAddress = 0;
|
||||||
|
ulong MmPhysLastAddress = 0;
|
||||||
|
|
||||||
enum
|
enum
|
||||||
{
|
{
|
||||||
|
@ -57,8 +58,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
|
||||||
ulong lastKernelAddr = (ulong)(_heap_start + _heap_max);
|
MmPhysLastAddress = (ulong)(_heap_start + _heap_max);
|
||||||
ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE;
|
ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastAddress - KPAGESIZE;
|
||||||
|
|
||||||
// Maximum VIRTUAL address in memory
|
// Maximum VIRTUAL address in memory
|
||||||
MmVirtLastAddress = phRamSize + diffKernUsr;
|
MmVirtLastAddress = phRamSize + diffKernUsr;
|
||||||
|
@ -132,13 +133,13 @@ void MmInitPaging(void)
|
||||||
MmPT[index] = (ulong)curAddrPT | PRESENT;
|
MmPT[index] = (ulong)curAddrPT | PRESENT;
|
||||||
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
||||||
MmStackGuards[0] = (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) {
|
else if ((ulong)curAddrPT == (ulong)BtLoaderInfo.kernelEndAddr) {
|
||||||
MmPT[index] = (ulong)curAddrPT | PRESENT;
|
MmPT[index] = (ulong)curAddrPT | PRESENT;
|
||||||
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
||||||
MmStackGuards[1] = (ulong)curAddrPT;
|
MmStackGuards[1] = (ulong)curAddrPT;
|
||||||
DebugLog("\tStack Guard at %p\n", curAddrPT);
|
//DebugLog("\tStack Guard at %p\n", curAddrPT);
|
||||||
}
|
}
|
||||||
// SECTION .TEXT PROTECTION
|
// SECTION .TEXT PROTECTION
|
||||||
else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) {
|
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);
|
//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 <= lastKernelAddr) {
|
else if ((ulong)curAddrPT <= MmPhysLastAddress) {
|
||||||
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
|
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
|
||||||
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
|
||||||
|
|
||||||
if ((ulong)curAddrPT == lastKernelAddr) {
|
if ((ulong)curAddrPT == MmPhysLastAddress) {
|
||||||
//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) | PRESENT; // Not present for instance
|
MmPT[index] = ((ulong)curAddrPT - diffKernUsr); // Not present for instance, unallocated
|
||||||
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 {
|
||||||
|
@ -227,7 +228,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 == (*page & ~(KPAGESIZE - 1))) {
|
if (*page & PRESENT) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -283,31 +284,23 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags)
|
||||||
//
|
//
|
||||||
void MmUnmapPage(void* virtualAddr)
|
void MmUnmapPage(void* virtualAddr)
|
||||||
{
|
{
|
||||||
pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr);
|
MmUnsetPage(virtualAddr, PRESENT); // Removing the present flag
|
||||||
|
|
||||||
*page = 0;
|
|
||||||
|
|
||||||
KeFlushTlbSingle(*page);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Kernel Page allocator
|
// Find physical unallocated pages
|
||||||
//
|
//
|
||||||
void *MmKAllocPageBlock(void *start) {
|
void *MmGetPhyPageBlock(size_t size, bool usermode) {
|
||||||
pte_t *startPage = MmGetPageDescriptorFromVirtual(start);
|
void *startPage = 0;
|
||||||
|
|
||||||
//for (ulong curPage = 0; curPage < )
|
if (!usermode) {
|
||||||
|
startPage = MmTransPhyToVirtAddr(0);
|
||||||
|
} else {
|
||||||
|
startPage = MmTransVirtToPhyAddr((void*)USERSPACE);
|
||||||
|
}
|
||||||
|
|
||||||
return NULL;
|
DebugLog("Userspace at %p\n", (void*)USERSPACE);
|
||||||
}
|
DebugLog("Userspace physical at %p\n", MmTransVirtToPhyAddr((void*)USERSPACE));
|
||||||
|
|
||||||
//
|
|
||||||
// User page allocator
|
|
||||||
//
|
|
||||||
void *MmUAllocPageBlock(void *start) {
|
|
||||||
pte_t *startPage = MmGetPageDescriptorFromVirtual(start);
|
|
||||||
|
|
||||||
//for (ulong curPage = 0; curPage < )
|
|
||||||
|
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
|
@ -204,6 +204,20 @@ error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline)
|
||||||
return EOK;
|
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
|
enum
|
||||||
{
|
{
|
||||||
PRESENT = 1 << 0,
|
PRESENT = 1 << 0,
|
||||||
|
@ -236,17 +250,13 @@ error_t CmdPageUnmap(int argc, char **argv, char *cmdline)
|
||||||
return EOK;
|
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])) { */
|
MmGetPhyPageBlock(size, usermode);
|
||||||
/* address = (ulong *)0x80000000; */
|
|
||||||
/* } */
|
|
||||||
|
|
||||||
void *translation = MmTransPhyToVirtAddr(address);
|
|
||||||
|
|
||||||
KernLog("Translation of %p is %p\n", address, translation);
|
|
||||||
return EOK;
|
return EOK;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -316,6 +326,7 @@ static Command_t testcmdtable[] =
|
||||||
" virtual address (paging)"},
|
" virtual address (paging)"},
|
||||||
{ "pmap", CmdPageMap, "Map a page to given physical addr" },
|
{ "pmap", CmdPageMap, "Map a page to given physical addr" },
|
||||||
{ "punmap", CmdPageUnmap, "Unmap a page" },
|
{ "punmap", CmdPageUnmap, "Unmap a page" },
|
||||||
|
{ "pblck", CmdPageBlock, "Find a free block of pages" },
|
||||||
{ "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"},
|
{ "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"},
|
||||||
{ "shell", CmdShell, "Start a new shell (nested)", },
|
{ "shell", CmdShell, "Start a new shell (nested)", },
|
||||||
{ "stkov", CmdStackOverflow, "Provoke a stack overflow" },
|
{ "stkov", CmdStackOverflow, "Provoke a stack overflow" },
|
||||||
|
|
Loading…
Reference in New Issue