1
0
mirror of https://gitlab.os-k.eu/os-k-team/os-k.git synced 2023-08-25 14:03:10 +02:00

Dynamic paging : functionnal

This commit is contained in:
Adrien Bourmault 2020-01-10 19:54:37 +01:00
commit 951c99ad56
5 changed files with 64 additions and 21 deletions

View File

@ -63,13 +63,15 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg)
// Memory // Memory
MmInitMemoryMap(); MmInitMemoryMap();
MmInitGdt(); MmInitGdt();
MmInitHeap();
MmInitPaging();
// IDT // Interrupts
KeSetupIDT(); KeSetupIDT();
KeEnableIRQs(); KeEnableIRQs();
// Memory (2)
MmInitHeap();
MmInitPaging();
// Interrupt handlers // Interrupt handlers
MmActivatePageHandler(); MmActivatePageHandler();
KeEnableRTC(); KeEnableRTC();

View File

@ -26,7 +26,6 @@
#include <init/boot.h> #include <init/boot.h>
#include <ke/idt.h> #include <ke/idt.h>
#include <io/vga.h> #include <io/vga.h>
#include <mm/mm.h>
#include <io/spkr.h> #include <io/spkr.h>
IdtEntry_t idt[256] = { 0 }; IdtEntry_t idt[256] = { 0 };

View File

@ -11,7 +11,8 @@
//----------- //-----------
volatile pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE)));
ulong *MmPhysicalPageTable;
extern ulong _text; extern ulong _text;
extern ulong _text_end; extern ulong _text_end;
@ -47,15 +48,16 @@ void MmInitPaging(void)
pdpe_t *MmPDP = NULL; pdpe_t *MmPDP = NULL;
pde_t *MmPD = NULL; pde_t *MmPD = NULL;
pte_t *MmPT = NULL; pte_t *MmPT = NULL;
ulong index; ulong index, xedni;
ulong lastKernelAddr = (ulong)(_heap_start + _heap_max);
ulong firstDirectoryAddr = 0; ulong firstDirectoryAddr = 0;
ulong lastDirectoryAddr = 0; ulong lastDirectoryAddr = 0;
ulong phDirSize = 0;
// Maximum PHYSICAL address in memory // Maximum PHYSICAL address in memory
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);
ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE; ulong diffKernUsr = (ulong)USERSPACE - lastKernelAddr - KPAGESIZE;
// Maximum VIRTUAL address in memory // Maximum VIRTUAL address in memory
@ -64,6 +66,10 @@ void MmInitPaging(void)
//DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); //DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress);
memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4)); memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4));
phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~(KPAGESIZE - 1));
MmPhysicalPageTable = (ulong*)malloc(phDirSize);
//DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB);
for (ulong curAddrPML4 = 0; for (ulong curAddrPML4 = 0;
curAddrPML4 < MmVirtLastAddress; curAddrPML4 < MmVirtLastAddress;
@ -117,38 +123,45 @@ void MmInitPaging(void)
// Create an entry in PT each page of 4KB // Create an entry in PT each page of 4KB
index = (curAddrPT / ((ulong)KPAGESIZE)) % 512; index = (curAddrPT / ((ulong)KPAGESIZE)) % 512;
xedni = (curAddrPT / ((ulong)KPAGESIZE));
//DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT); //DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT);
// STACK GUARD PAGE */ // STACK GUARD PAGE */
if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) {
MmPT[index] = (ulong)curAddrPT | PRESENT; MmPT[index] = (ulong)curAddrPT | PRESENT;
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;
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) {
MmPT[index] = (ulong)curAddrPT | PRESENT; MmPT[index] = (ulong)curAddrPT | PRESENT;
DebugLog("\tSection .text at %p\n", curAddrPT); MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
//DebugLog("\tSection .text at %p\n", curAddrPT);
} }
// SECTION .DATA PROTECTION // SECTION .DATA PROTECTION
else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) {
MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE | NX; MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | READWRITE | NX;
DebugLog("\tSection .data at %p\n", curAddrPT); MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
//DebugLog("\tSection .data at %p\n", curAddrPT);
} }
// SECTION .RODATA PROTECTION // SECTION .RODATA PROTECTION
else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) {
MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX; MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX;
DebugLog("\tSection .rodata at %p\n", curAddrPT); MmPhysicalPageTable[xedni] = (ulong)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 <= lastKernelAddr) {
MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE;
MmPhysicalPageTable[xedni] = (ulong)curAddrPT;
if ((ulong)curAddrPT == lastKernelAddr) { if ((ulong)curAddrPT == lastKernelAddr) {
//DebugLog("\tLast page of kernel at %p\n", curAddrPT); //DebugLog("\tLast page of kernel at %p\n", curAddrPT);
@ -157,6 +170,8 @@ void MmInitPaging(void)
// 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) | PRESENT; // Not present for instance
xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE));
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);
@ -174,9 +189,9 @@ void MmInitPaging(void)
lastDirectoryAddr = (ulong)MmPT; lastDirectoryAddr = (ulong)MmPT;
MmLoadPML4((void *)MmPageMapLevel4); MmLoadPML4((void *)MmPageMapLevel4);
//MmEnableWriteProtect();
MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB);
DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB);
} }
// //
@ -221,7 +236,10 @@ void *MmTransVirtToPhyAddr(void* virtualAddr)
void *MmTransPhyToVirtAddr(void* physicalAddr) void *MmTransPhyToVirtAddr(void* physicalAddr)
{ {
return (void*)0; ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1));
return (void*)( MmPhysicalPageTable[(ulong)physicalAddr
/ ((ulong)KPAGESIZE)
+ ((ulong)physicalAddr - phyAddrPage) ] );
} }
// //
@ -272,6 +290,28 @@ void MmUnmapPage(void* virtualAddr)
KeFlushTlbSingle(*page); KeFlushTlbSingle(*page);
} }
//
// Kernel Page allocator
//
void *MmKAllocPageBlock(void *start) {
pte_t *startPage = MmGetPageDescriptorFromVirtual(start);
//for (ulong curPage = 0; curPage < )
return NULL;
}
//
// User page allocator
//
void *MmUAllocPageBlock(void *start) {
pte_t *startPage = MmGetPageDescriptorFromVirtual(start);
//for (ulong curPage = 0; curPage < )
return NULL;
}
//----------- //-----------
// //

View File

@ -40,7 +40,7 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline)
ulong flags = KePauseIRQs(); ulong flags = KePauseIRQs();
heap_start = (size_t)_heap_start; heap_start = (size_t)_heap_start;
heap_end = (size_t)_heap_start; heap_end = (size_t)_heap_end;
heap_max = _heap_max; heap_max = _heap_max;
KeRestoreIRQs(flags); KeRestoreIRQs(flags);

View File

@ -112,7 +112,7 @@ error_t CmdDumpATASect(int argc, char **argv, char *cmdline)
error_t CmdDumpMem(int argc, char **argv, char *cmdline) error_t CmdDumpMem(int argc, char **argv, char *cmdline)
{ {
char sector[1024] = {0}; char sector[1024] = {0};
char *address = (char*)atol(argv[1]); char *address = (char*)strtoul(argv[1], NULL, 16);
int nb = 1; //atoi(argv[2]); int nb = 1; //atoi(argv[2]);
int x = 0; int x = 0;
int step = 16; int step = 16;
@ -193,7 +193,7 @@ error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline)
{ {
void *address = (void*)atoul(argv[1]); void *address = (void*)atoul(argv[1]);
if (!(void*)atoul(argv[1])) { if (!(void*)strtoul(argv[1], NULL, 16)) {
KernLog("No argument : translating the userspace address\n"); KernLog("No argument : translating the userspace address\n");
address = (void *)0x80000000; address = (void *)0x80000000;
} }
@ -219,8 +219,8 @@ enum
error_t CmdPageMap(int argc, char **argv, char *cmdline) error_t CmdPageMap(int argc, char **argv, char *cmdline)
{ {
void *virtual = (void*)atoul(argv[1]); void *virtual = (void*)strtoul(argv[1], NULL, 16);
void *physical = (void*)atoul(argv[2]); void *physical = (void*)strtoul(argv[2], NULL, 16);
MmMapPage(virtual, physical, PRESENT | READWRITE); MmMapPage(virtual, physical, PRESENT | READWRITE);
@ -229,7 +229,7 @@ error_t CmdPageMap(int argc, char **argv, char *cmdline)
error_t CmdPageUnmap(int argc, char **argv, char *cmdline) error_t CmdPageUnmap(int argc, char **argv, char *cmdline)
{ {
void *virtual = (void*)atoul(argv[1]); void *virtual = (void*)strtoul(argv[1], NULL, 16);
MmUnmapPage(virtual); MmUnmapPage(virtual);
@ -238,7 +238,7 @@ error_t CmdPageUnmap(int argc, char **argv, char *cmdline)
error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
{ {
void *address = (void*)atoul(argv[1]); void *address = (void*)strtoul(argv[1], NULL, 16);
/* if (!(void*)atoul(argv[1])) { */ /* if (!(void*)atoul(argv[1])) { */
/* address = (ulong *)0x80000000; */ /* address = (ulong *)0x80000000; */
@ -252,7 +252,7 @@ error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline)
error_t CmdPF(int argc, char **argv, char *cmdline) error_t CmdPF(int argc, char **argv, char *cmdline)
{ {
ulong *address = (ulong*)(ulong)atoul(argv[1]); ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16);
KernLog("Provoking Page Fault at %#x\n", address); KernLog("Provoking Page Fault at %#x\n", address);
@ -312,6 +312,8 @@ static Command_t testcmdtable[] =
{ "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"}, { "div", CmdFloatDiv, "Float div. Usage : div a b. Returns a/b"},
{ "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to" { "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to"
" physical address (paging)"}, " physical address (paging)"},
{ "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to"
" 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" },
{ "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"}, { "pf", CmdPF, "Provoke a PF. Usage: pfault <address>"},