diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 73e26d3..12d3792 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -63,13 +63,15 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) // Memory MmInitMemoryMap(); MmInitGdt(); - MmInitHeap(); - MmInitPaging(); - // IDT + // Interrupts KeSetupIDT(); KeEnableIRQs(); + // Memory (2) + MmInitHeap(); + MmInitPaging(); + // Interrupt handlers MmActivatePageHandler(); KeEnableRTC(); diff --git a/kaleid/kernel/ke/idt.c b/kaleid/kernel/ke/idt.c index 251cf7a..d6fc0fc 100644 --- a/kaleid/kernel/ke/idt.c +++ b/kaleid/kernel/ke/idt.c @@ -26,7 +26,6 @@ #include #include #include -#include #include IdtEntry_t idt[256] = { 0 }; diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 008986f..5a7e77e 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -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_end; @@ -47,15 +48,16 @@ void MmInitPaging(void) pdpe_t *MmPDP = NULL; pde_t *MmPD = NULL; pte_t *MmPT = NULL; - ulong index; - ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); + ulong index, xedni; ulong firstDirectoryAddr = 0; ulong lastDirectoryAddr = 0; + ulong phDirSize = 0; // Maximum PHYSICAL address in memory 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; // 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); 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; curAddrPML4 < MmVirtLastAddress; @@ -117,38 +123,45 @@ void MmInitPaging(void) // Create an entry in PT each page of 4KB index = (curAddrPT / ((ulong)KPAGESIZE)) % 512; + xedni = (curAddrPT / ((ulong)KPAGESIZE)); //DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT); // STACK GUARD PAGE */ if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; MmStackGuards[0] = (ulong)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); } // SECTION .TEXT PROTECTION else if ((ulong)curAddrPT >= (ulong)&_text && (ulong)curAddrPT <= (ulong)&_text_end) { 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 else if ((ulong)curAddrPT >= (ulong)&_data && (ulong)curAddrPT <= (ulong)&_data_end) { 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 else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { 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 else if ((ulong)curAddrPT <= lastKernelAddr) { MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; if ((ulong)curAddrPT == lastKernelAddr) { //DebugLog("\tLast page of kernel at %p\n", curAddrPT); @@ -157,6 +170,8 @@ void MmInitPaging(void) // While we're inside the userspace pages else if ((ulong)curAddrPT >= USERSPACE) { MmPT[index] = ((ulong)curAddrPT - diffKernUsr) | PRESENT; // Not present for instance + xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE)); + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; if ((ulong)curAddrPT == USERSPACE) { DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); @@ -174,9 +189,9 @@ void MmInitPaging(void) lastDirectoryAddr = (ulong)MmPT; MmLoadPML4((void *)MmPageMapLevel4); + //MmEnableWriteProtect(); - MmEnableWriteProtect(); - DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB); + DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); } // @@ -221,7 +236,10 @@ void *MmTransVirtToPhyAddr(void* virtualAddr) 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); } +// +// 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; +} + //----------- // diff --git a/kaleid/kernel/sh/musage.c b/kaleid/kernel/sh/musage.c index b7fba04..6de60af 100644 --- a/kaleid/kernel/sh/musage.c +++ b/kaleid/kernel/sh/musage.c @@ -40,7 +40,7 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline) ulong flags = KePauseIRQs(); heap_start = (size_t)_heap_start; - heap_end = (size_t)_heap_start; + heap_end = (size_t)_heap_end; heap_max = _heap_max; KeRestoreIRQs(flags); diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 9fb9b11..f528b71 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -112,7 +112,7 @@ error_t CmdDumpATASect(int argc, char **argv, char *cmdline) error_t CmdDumpMem(int argc, char **argv, char *cmdline) { 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 x = 0; int step = 16; @@ -193,7 +193,7 @@ error_t CmdPageTranslateVirtToPhy(int argc, char **argv, char *cmdline) { 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"); address = (void *)0x80000000; } @@ -219,8 +219,8 @@ enum error_t CmdPageMap(int argc, char **argv, char *cmdline) { - void *virtual = (void*)atoul(argv[1]); - void *physical = (void*)atoul(argv[2]); + void *virtual = (void*)strtoul(argv[1], NULL, 16); + void *physical = (void*)strtoul(argv[2], NULL, 16); 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) { - void *virtual = (void*)atoul(argv[1]); + void *virtual = (void*)strtoul(argv[1], NULL, 16); MmUnmapPage(virtual); @@ -238,7 +238,7 @@ error_t CmdPageUnmap(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])) { */ /* 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) { - ulong *address = (ulong*)(ulong)atoul(argv[1]); + ulong *address = (ulong*)(ulong)strtoul(argv[1], NULL, 16); 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"}, { "transvtp", CmdPageTranslateVirtToPhy, "Translate a virtual to" " physical address (paging)"}, + { "transptv", CmdPageTranslatePhyToVirt, "Translate a physical to" + " virtual address (paging)"}, { "pmap", CmdPageMap, "Map a page to given physical addr" }, { "punmap", CmdPageUnmap, "Unmap a page" }, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"},