From 0fa7d465111ac988cdbb095a473789605a01fe61 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 10 Jan 2020 12:51:23 +0100 Subject: [PATCH 1/4] minor modification to test commands --- kaleid/kernel/mm/paging.c | 2 +- kaleid/kernel/sh/testcmds.c | 14 +++++++------- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index c8f581a..af127e2 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -166,7 +166,7 @@ void MmInitPaging(void) MmPT[index] = 0; } - KeFlushTlbSingle(curAddrPT); + //KeFlushTlbSingle(curAddrPT); } } } diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 9fb9b11..00a68a3 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); From 36ce243b8c04c34858db37a42ac3003de492ffe3 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 10 Jan 2020 13:36:33 +0100 Subject: [PATCH 2/4] Bug with heap ? --- kaleid/kernel/init/init.c | 8 +++++--- kaleid/kernel/ke/idt.c | 1 - kaleid/kernel/mm/paging.c | 37 +++++++++++++++++++++++++++++++++---- 3 files changed, 38 insertions(+), 8 deletions(-) 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 af127e2..0bfd06f 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))); +static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +static ulong *MmPhysicalPageTable; extern ulong _text; extern ulong _text_end; @@ -51,6 +52,7 @@ void MmInitPaging(void) ulong lastKernelAddr = (ulong)(_heap_start + _heap_max); ulong firstDirectoryAddr = 0; ulong lastDirectoryAddr = 0; + ulong phDirSize = 0; // Maximum PHYSICAL address in memory ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; @@ -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); + + //MmPhysicalPageTable = (ulong*)malloc((phRamSize / KPAGESIZE)*sizeof(ulong)); + DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB); for (ulong curAddrPML4 = 0; curAddrPML4 < MmVirtLastAddress; @@ -157,6 +163,7 @@ 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 + //MmPhysicalPageTable[(ulong)curAddrPT - diffKernUsr] = curAddrPT; if ((ulong)curAddrPT == USERSPACE) { DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); @@ -174,9 +181,9 @@ void MmInitPaging(void) lastDirectoryAddr = (ulong)MmPT; MmLoadPML4((void *)MmPageMapLevel4); - MmEnableWriteProtect(); - DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr)/MB); + + DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); } // @@ -221,7 +228,7 @@ void *MmTransVirtToPhyAddr(void* virtualAddr) void *MmTransPhyToVirtAddr(void* physicalAddr) { - return (void*)0; + return (void*)MmPhysicalPageTable[(ulong)physicalAddr]; } // @@ -272,6 +279,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; +} + //----------- // From 102f3f027976c2ff675cafdd03da76e59be4894a Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Fri, 10 Jan 2020 13:57:42 +0100 Subject: [PATCH 3/4] up --- kaleid/kernel/sh/musage.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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); From f10f53ff0865f95296a0ff75a5b1fdb2e96dda0a Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 10 Jan 2020 19:25:33 +0100 Subject: [PATCH 4/4] Bug resolved : alignment issue --- kaleid/kernel/mm/paging.c | 33 ++++++++++++++++++++++----------- kaleid/kernel/sh/testcmds.c | 2 ++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 0bfd06f..00bdf27 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -11,8 +11,8 @@ //----------- -static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); -static ulong *MmPhysicalPageTable; +pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +ulong *MmPhysicalPageTable; extern ulong _text; extern ulong _text_end; @@ -48,8 +48,7 @@ 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; @@ -58,6 +57,7 @@ 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; // Maximum VIRTUAL address in memory @@ -66,10 +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); + phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~(KPAGESIZE - 1)); - //MmPhysicalPageTable = (ulong*)malloc((phRamSize / KPAGESIZE)*sizeof(ulong)); - DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB); + 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; @@ -123,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; + 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; + 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; + 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); @@ -163,7 +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 - //MmPhysicalPageTable[(ulong)curAddrPT - diffKernUsr] = curAddrPT; + xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE)); + MmPhysicalPageTable[xedni] = (ulong)curAddrPT; if ((ulong)curAddrPT == USERSPACE) { DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); @@ -173,7 +181,7 @@ void MmInitPaging(void) MmPT[index] = 0; } - //KeFlushTlbSingle(curAddrPT); + KeFlushTlbSingle(curAddrPT); } } } @@ -181,7 +189,7 @@ void MmInitPaging(void) lastDirectoryAddr = (ulong)MmPT; MmLoadPML4((void *)MmPageMapLevel4); - MmEnableWriteProtect(); + //MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); } @@ -228,7 +236,10 @@ void *MmTransVirtToPhyAddr(void* virtualAddr) void *MmTransPhyToVirtAddr(void* physicalAddr) { - return (void*)MmPhysicalPageTable[(ulong)physicalAddr]; + ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1)); + return (void*)( MmPhysicalPageTable[(ulong)physicalAddr + / ((ulong)KPAGESIZE) + + ((ulong)physicalAddr - phyAddrPage) ] ); } // diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 00a68a3..f528b71 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -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
"},