From 08d7de2b4048e6b002a82ff7267b6f49d399b9e9 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 12 Jan 2020 23:16:03 +0100 Subject: [PATCH 01/27] Working on page allocator #67 --- ProjectTree | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/ProjectTree b/ProjectTree index 1c063cc..eb17c00 100644 --- a/ProjectTree +++ b/ProjectTree @@ -74,7 +74,8 @@ │   │   ├── heap.h │   │   ├── malloc.h │   │   ├── map.h -│   │   └── paging.h +│   │   ├── paging.h +│   │   └── palloc.h │   ├── po │   │   └── shtdwn.h │   ├── sh @@ -97,6 +98,7 @@ │   │   │   ├── ata.c │   │   │   ├── cursor.c │   │   │   ├── keyb.c +│   │   │   ├── scan.c │   │   │   ├── spkr.c │   │   │   └── vga.c │   │   ├── ke @@ -116,7 +118,8 @@ │   │   │   ├── malloc.c │   │   │   ├── map.c │   │   │   ├── paging.asm -│   │   │   └── paging.c +│   │   │   ├── paging.c +│   │   │   └── palloc.c │   │   ├── po │   │   │   └── shtdwn.c │   │   ├── ps @@ -157,4 +160,4 @@ ├── ProjectTree └── README.md -28 directories, 104 files +28 directories, 107 files From ea8ffd66743fff683619df340f1a7d0da28dce48 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 12 Jan 2020 23:25:44 +0100 Subject: [PATCH 02/27] Working on page allocator #67 --- kaleid/kernel/mm/paging.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 6c3487f..8f8a8a9 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -157,13 +157,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) { From 6a054a4b67106e42f1019a69900356aeb78864af Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 13 Jan 2020 16:12:30 +0100 Subject: [PATCH 03/27] Working on page allocator #67 --- Makefile | 2 +- ProjectTree | 3 +-- include/mm/paging.h | 2 +- kaleid/kernel/mm/paging.c | 22 +++++++++++----------- kaleid/kernel/mm/palloc.c | 25 +++++++++++++++++++++++-- 5 files changed, 37 insertions(+), 17 deletions(-) diff --git a/Makefile b/Makefile index 05c091e..5e2b97f 100644 --- a/Makefile +++ b/Makefile @@ -115,7 +115,7 @@ KernSources = kernel/ke/cpuid.c kernel/mm/paging.c \ kernel/sh/shell.c kernel/sh/shcmds.c \ kernel/sh/musage.c kernel/io/ata.c \ kernel/sh/argv.c kernel/ke/pit.c \ - kernel/sh/testcmds.c + kernel/sh/testcmds.c kernel/mm/palloc.c KernObj=$(patsubst %.c,$(KOBJDIR)/%.o,$(KernSources)) KernDep=$(patsubst %.c,$(KOBJDIR)/%.d,$(KernSources)) diff --git a/ProjectTree b/ProjectTree index eb17c00..a5adff2 100644 --- a/ProjectTree +++ b/ProjectTree @@ -98,7 +98,6 @@ │   │   │   ├── ata.c │   │   │   ├── cursor.c │   │   │   ├── keyb.c -│   │   │   ├── scan.c │   │   │   ├── spkr.c │   │   │   └── vga.c │   │   ├── ke @@ -160,4 +159,4 @@ ├── ProjectTree └── README.md -28 directories, 107 files +28 directories, 106 files diff --git a/include/mm/paging.h b/include/mm/paging.h index d76f0ec..6dca6e1 100644 --- a/include/mm/paging.h +++ b/include/mm/paging.h @@ -33,7 +33,7 @@ #define KPAGESIZE (4 * KB) #define UPAGESIZE (4 * KB) -#define USERSPACE 0x80000000 +#define USERSPACE 0x100000000 //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 6c3487f..e463b4c 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -91,7 +91,7 @@ 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)); + phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~((KPAGESIZE - 1) | NX)); MmPhysicalPageTable = (ulong*)malloc(phDirSize); //DebugLog("\t\tRam %u MB, pagesize %u KB, size %u MB\n", phRamSize / MB, KPAGESIZE / KB, phDirSize / MB); @@ -179,7 +179,7 @@ void MmInitPaging(void) } // SECTION .RODATA PROTECTION else if ((ulong)curAddrPT >= (ulong)&_rodata && (ulong)curAddrPT <= (ulong)&_rodata_end) { - MmPT[index] = (ulong)curAddrPT | PRESENT | WRITETHR | NX; + MmPT[index] = (ulong)curAddrPT | PRESENT | NX; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; //DebugLog("\tSection .rodata at %p\n", curAddrPT); } @@ -224,17 +224,17 @@ void MmInitPaging(void) // static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); if (virtAddrPage > MmVirtLastAddress) { KeStartPanic("MmSetPage() Out of bound of the address space !"); } - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~(KPAGESIZE - 1)); + pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~((KPAGESIZE - 1) | NX | NX)); //DebugLog("pdp\t: %p\n", pdp); - pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~(KPAGESIZE - 1)); + pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~((KPAGESIZE - 1) | NX)); //DebugLog("pd\t: %p\n", pd); - pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~(KPAGESIZE - 1)); + pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~((KPAGESIZE - 1) | NX)); //DebugLog("pt\t: %p\n", pt); pte_t *page = &pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; @@ -249,19 +249,19 @@ static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr) // void *MmTransVirtToPhyAddr(void* virtualAddr) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); + ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - if (*page == (*page & ~(KPAGESIZE - 1))) { + if (*page == (*page & ~((KPAGESIZE - 1) | NX))) { return NULL; } - return (void*)((*page & ~(KPAGESIZE - 1))+ ((ulong)virtualAddr - (ulong)virtAddrPage)); + return (void*)((*page & ~((KPAGESIZE - 1) | NX))+ ((ulong)virtualAddr - (ulong)virtAddrPage)); } void *MmTransPhyToVirtAddr(void* physicalAddr) { - ulong phyAddrPage = (ulong)physicalAddr & ( ~(KPAGESIZE - 1)); + ulong phyAddrPage = (ulong)physicalAddr & ( ~((KPAGESIZE - 1) | NX)); return (void*)( MmPhysicalPageTable[(ulong)physicalAddr / ((ulong)KPAGESIZE) ] + ((ulong)physicalAddr - phyAddrPage)); @@ -298,7 +298,7 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - *page = ((ulong)physicalAddr & ~(KPAGESIZE - 1)) | flags; + *page = ((ulong)physicalAddr & ~((KPAGESIZE - 1) | NX)) | flags; KeFlushTlbSingle(*page); } diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 22a0fdf..3704863 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -31,8 +31,29 @@ //--------- +enum +{ + Whatever = 1UL << 52, + Whatever2 = 1UL << 62 +}; - +typedef struct { + void **FramePtr; + int pageNumber; +} FramePtr_t; //--------- - + +// +// Returns a structure that describes a pageframe +// +/* FramePtr_t MmAllocPageBlock(size_t size) { */ +/* ulong pageNumber = (ulong)size / KPAGESIZE; */ + + +/* return (FramePtr_t)0; */ +/* } */ + + + + From 459ed7c5b53481f504429f12931f2dbf137f4c10 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 13 Jan 2020 20:48:24 +0100 Subject: [PATCH 04/27] Working on page allocator (3) #67 --- include/mm/palloc.h | 8 ++++++++ kaleid/kernel/mm/paging.c | 6 ++++-- kaleid/kernel/mm/palloc.c | 31 ++++++++++++++++++++++--------- kaleid/kernel/sh/testcmds.c | 6 ++++-- 4 files changed, 38 insertions(+), 13 deletions(-) diff --git a/include/mm/palloc.h b/include/mm/palloc.h index bada93f..b3ffe15 100644 --- a/include/mm/palloc.h +++ b/include/mm/palloc.h @@ -31,7 +31,15 @@ //----------------------------------------------------------------------------// +typedef struct AllocatedPage_t{ + void *phyAddress; + ulong id; + struct AllocatedPage_t *next; +} AllocatedPage_t; +//----------------------------------------------------------------------------// + +error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index e463b4c..fbf9c1f 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -78,6 +78,8 @@ void MmInitPaging(void) ulong lastDirectoryAddr = 0; ulong phDirSize = 0; + KernLog("\tActivating paging...\n"); + // Maximum PHYSICAL address in memory ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; @@ -214,7 +216,7 @@ void MmInitPaging(void) lastDirectoryAddr = (ulong)MmPT; MmLoadPML4((void *)MmPageMapLevel4); - //MmEnableWriteProtect(); + MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); } @@ -415,5 +417,5 @@ static void PagingHandler(ISRFrame_t *regs) void MmActivatePageHandler(void) { KeRegisterISR(PagingHandler, 0xe); - DebugLog("\tPaging activated\n"); + //DebugLog("\tPage handler activated\n"); } diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 3704863..340a494 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -37,22 +37,35 @@ enum Whatever2 = 1UL << 62 }; -typedef struct { - void **FramePtr; - int pageNumber; -} FramePtr_t; +static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 }; //--------- +static bool MmIsPageBusy(void *phyAddr) { + AllocatedPage_t *busyPage = &busyPagesList; + bool isBusy = false; + + while(busyPage->next) { + busyPages = busyPage->next; + DebugLog("Busy page at %p\n", busyPage->phyAddress); + + if (phyPageAddr == busyPage->phyAddress) { + isBusy = true; + break; + } + } + + return isBusy; +} + // // Returns a structure that describes a pageframe // -/* FramePtr_t MmAllocPageBlock(size_t size) { */ -/* ulong pageNumber = (ulong)size / KPAGESIZE; */ +error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) { + *pageNumber = (ulong)size / KPAGESIZE; - -/* return (FramePtr_t)0; */ -/* } */ + return EOK; +} diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 271de10..07b90f5 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -24,6 +24,7 @@ #include #include +#include #include #include #include @@ -256,9 +257,10 @@ error_t CmdPageBlock(int argc, char **argv, char *cmdline) size_t size = (size_t)atoi(argv[1]); bool usermode = (bool)atoi(argv[2]); - //MmGetPhyPageBlock(size, usermode); + size_t pageNum = 0; - return EOK; + error_t err = MmGetFreePageFrame((void**)0x12345678, &pageNum, (size_t)4096); + return err; } error_t CmdPF(int argc, char **argv, char *cmdline) From 8437b854caef4e64e9192b5338ae45b3631ac824 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 14 Jan 2020 00:37:30 +0100 Subject: [PATCH 05/27] Working on page allocator (4) #67 --- include/mm/palloc.h | 1 + kaleid/kernel/mm/map.c | 28 ++++++++++++++++--- kaleid/kernel/mm/paging.c | 4 +-- kaleid/kernel/mm/palloc.c | 56 +++++++++++++++++++++++++++++++++++-- kaleid/kernel/sh/testcmds.c | 2 +- 5 files changed, 82 insertions(+), 9 deletions(-) diff --git a/include/mm/palloc.h b/include/mm/palloc.h index b3ffe15..ba591c9 100644 --- a/include/mm/palloc.h +++ b/include/mm/palloc.h @@ -40,6 +40,7 @@ typedef struct AllocatedPage_t{ //----------------------------------------------------------------------------// error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size); +error_t MmTestBusyPage(void); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index 64156fe..fabe03b 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -102,10 +102,6 @@ static error_t InitMemoryMap(void) KernLog("\tAvailable RAM size : %u MB\n", memoryMap.freeRamSize / MB); - // Magic value in memory to prevent smashing - ulong * heapStart = BtLoaderInfo.stackEndAddr + 8; - *heapStart = 0xbad00badbad00bad; - return EOK; } @@ -133,6 +129,30 @@ size_t MmGetAvailZoneSize(void *start) { return 0; } +bool MmIsFailingZoneSize(void *start) { + uint i; + + // Because the kernel is the kernel + if (start < BtLoaderInfo.stackEndAddr + 16) + return 0; + + // Search the zone where the start address is + for (i = 0; i < memoryMap.length; i++) { + // if the address is in a failing zone, we can return 1 + if ( + (memoryMap.entry[i].type == BADRAM_ZONE) && + (ulong)start >= (ulong)memoryMap.entry[i].addr && + (ulong)start < ((ulong)memoryMap.entry[i].addr + + (ulong)memoryMap.entry[i].length) + ) { + return 1; + } + } + + // If there is no zone, we return a 0 size + return 0; +} + void *MmGetFirstAvailZone(void *start) { uint i; void *current = 0; diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 04b5599..fbf9c1f 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -159,13 +159,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) { diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 340a494..a402034 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -41,12 +41,12 @@ static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 }; //--------- -static bool MmIsPageBusy(void *phyAddr) { +static bool isPageBusy(void *phyPageAddr) { AllocatedPage_t *busyPage = &busyPagesList; bool isBusy = false; while(busyPage->next) { - busyPages = busyPage->next; + busyPage = busyPage->next; DebugLog("Busy page at %p\n", busyPage->phyAddress); if (phyPageAddr == busyPage->phyAddress) { @@ -58,6 +58,35 @@ static bool MmIsPageBusy(void *phyAddr) { return isBusy; } +static void addPageToBusyList(void *phyPageAddr, ulong id) { + AllocatedPage_t *busyPage = &busyPagesList; + + while(busyPage->next) { + busyPage = busyPage->next; + } + + AllocatedPage_t *newBusyPage = (AllocatedPage_t*)malloc(sizeof(AllocatedPage_t)); + newBusyPage->phyAddress = phyPageAddr; + newBusyPage->id = id; + busyPage->next = newBusyPage; +} + +static void removePageFromBusyList(void *phyPageAddr) { + AllocatedPage_t *busyPage = &busyPagesList; + AllocatedPage_t *prevBusyPage = NULL; + + while(busyPage->next) { + prevBusyPage = busyPage; + busyPage = busyPage->next; + + if (phyPageAddr == busyPage->phyAddress) { + prevBusyPage->next = busyPage->next; + free(busyPage); + break; + } + } +} + // // Returns a structure that describes a pageframe // @@ -67,6 +96,29 @@ error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) { return EOK; } +error_t MmTestBusyPage(void) { + DebugLog("Adding pages \n"); + addPageToBusyList((void*)0x123456789, 56); + addPageToBusyList((void*)0x555666666, 69); + addPageToBusyList((void*)0x454545454, 5); + DebugLog("\nVerifying busy 1\n"); + isPageBusy((void*)0x123456789); + DebugLog("\nVerifying busy 2\n"); + isPageBusy((void*)0); + DebugLog("\nVerifying busy 3\n"); + isPageBusy((void*)0x1234567894); + + DebugLog("\nRemoving first\n"); + removePageFromBusyList((void*)0x123456789); + isPageBusy((void*)0x1234567894); + DebugLog("\nRemoving others\n"); + removePageFromBusyList((void*)0x555666666); + removePageFromBusyList((void*)0x454545454); + + isPageBusy((void*)0x1234567894); + + return EOK; +} diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 07b90f5..4207ae1 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -259,7 +259,7 @@ error_t CmdPageBlock(int argc, char **argv, char *cmdline) size_t pageNum = 0; - error_t err = MmGetFreePageFrame((void**)0x12345678, &pageNum, (size_t)4096); + error_t err = MmTestBusyPage(); return err; } From 1de0d05ad2a82f9197d78103aaf3d11c8105026c Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 14 Jan 2020 01:10:23 +0100 Subject: [PATCH 06/27] Working on page allocator (4) #67 --- kaleid/kernel/mm/palloc.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index a402034..3eb6547 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -93,6 +93,8 @@ static void removePageFromBusyList(void *phyPageAddr) { error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) { *pageNumber = (ulong)size / KPAGESIZE; + + return EOK; } From 2df15a1488f942ec4892b9cf41d0e1625f6173c7 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 14 Jan 2020 16:54:47 +0100 Subject: [PATCH 07/27] Page allocator functionnal #67 --- kaleid/kernel/mm/paging.c | 2 +- kaleid/kernel/mm/palloc.c | 160 +++++++++++++++++++++++++++++--------- 2 files changed, 126 insertions(+), 36 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index fbf9c1f..7d13543 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -46,7 +46,7 @@ extern ulong _data_end; extern MemoryMap_t memoryMap; -ulong MmStackGuards[2] = { 0 }; +static ulong MmStackGuards[2] = { 0 }; ulong MmVirtLastAddress = 0; ulong MmPhysLastKernAddress = 0; diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 3eb6547..98b61da 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -27,6 +27,7 @@ #include #include #include +#include #include //--------- @@ -38,27 +39,37 @@ enum }; static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 }; +extern MemoryMap_t memoryMap; +extern ulong MmPhysLastKernAddress; //--------- -static bool isPageBusy(void *phyPageAddr) { +static bool isPageBusy(void *phyPageAddr) +{ AllocatedPage_t *busyPage = &busyPagesList; bool isBusy = false; while(busyPage->next) { busyPage = busyPage->next; - DebugLog("Busy page at %p\n", busyPage->phyAddress); - if (phyPageAddr == busyPage->phyAddress) { isBusy = true; break; } } - return isBusy; } -static void addPageToBusyList(void *phyPageAddr, ulong id) { +static void printBusyPages(void) +{ + AllocatedPage_t *busyPage = &busyPagesList; + while(busyPage->next) { + busyPage = busyPage->next; + KernLog("Busy page at %p\n", busyPage->phyAddress); + } +} + +static void addPageToBusyList(void *phyPageAddr, ulong id) +{ AllocatedPage_t *busyPage = &busyPagesList; while(busyPage->next) { @@ -71,7 +82,8 @@ static void addPageToBusyList(void *phyPageAddr, ulong id) { busyPage->next = newBusyPage; } -static void removePageFromBusyList(void *phyPageAddr) { +static void removePageFromBusyList(void *phyPageAddr) +{ AllocatedPage_t *busyPage = &busyPagesList; AllocatedPage_t *prevBusyPage = NULL; @@ -88,38 +100,116 @@ static void removePageFromBusyList(void *phyPageAddr) { } // -// Returns a structure that describes a pageframe +// Returns an id to identify a page frame allocated // -error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) { - *pageNumber = (ulong)size / KPAGESIZE; +ulong MmAllocKernelPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous) +{ + static ulong id = 0; + *pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1; + frameListPtr = (void**)malloc(sizeof(void*)*(*pageNumber)); + size_t curNumber = 0; + bool inBlock = false; - - return EOK; -} - -error_t MmTestBusyPage(void) { - - DebugLog("Adding pages \n"); - addPageToBusyList((void*)0x123456789, 56); - addPageToBusyList((void*)0x555666666, 69); - addPageToBusyList((void*)0x454545454, 5); - - DebugLog("\nVerifying busy 1\n"); - isPageBusy((void*)0x123456789); - DebugLog("\nVerifying busy 2\n"); - isPageBusy((void*)0); - DebugLog("\nVerifying busy 3\n"); - isPageBusy((void*)0x1234567894); - - DebugLog("\nRemoving first\n"); - removePageFromBusyList((void*)0x123456789); - isPageBusy((void*)0x1234567894); - DebugLog("\nRemoving others\n"); - removePageFromBusyList((void*)0x555666666); - removePageFromBusyList((void*)0x454545454); - - isPageBusy((void*)0x1234567894); + // Incrementing id + id++; + + // Maximum PHYSICAL address in memory + ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; + + DebugLog("Allocating %d pages...\n", *pageNumber); + + for (void *curPage = (void*)MmPhysLastKernAddress; curPage < (void*)phRamSize; curPage += KPAGESIZE) { + if (!isPageBusy(curPage)) { + frameListPtr[curNumber] = curPage; + inBlock = true; + //DebugLog("Select page : %p\n", curPage); + if (++curNumber >= *pageNumber) { + break; + } + } else { + inBlock = false; + } + if (contiguous) + if (!inBlock) + curNumber = 0; + } + + for (size_t i = 0; i < *pageNumber; i++) { + addPageToBusyList(frameListPtr[i], id); + //DebugLog("Allocating page : %p\n", frameListPtr[i]); + } + + return id; +} + +void MmFreeKernelPageFrame(ulong id) +{ + AllocatedPage_t *busyPage = &busyPagesList; + + while(busyPage->next) { + busyPage = busyPage->next; + + if (id == busyPage->id) { + removePageFromBusyList(busyPage->phyAddress); + } + } +} + +error_t MmTestBusyPage(void) +{ + DebugLog("\nBusy pages\n"); + printBusyPages(); + + DebugLog("\nAlloc 6677 bytes\n"); + void **ptr = NULL; + size_t n = 0; + ulong id1 = MmAllocKernelPageFrame (ptr, &n, 6677, false); + + DebugLog("\nAlloc 9045 bytes\n"); + void **ptr2 = NULL; + size_t n2 = 0; + ulong id2 = MmAllocKernelPageFrame (ptr2, &n2, 9045, false); + + DebugLog("\nAlloc 1200 bytes\n"); + void **ptr3 = NULL; + size_t n3 = 0; + ulong id3 = MmAllocKernelPageFrame (ptr3, &n3, 1200, false); + + DebugLog("\nAlloc 4096 bytes\n"); + void **ptr4 = NULL; + size_t n4 = 0; + ulong id4 = MmAllocKernelPageFrame (ptr3, &n3, 4096, false); + + DebugLog("\nAlloc 4097 bytes\n"); + void **ptr5 = NULL; + size_t n5 = 0; + ulong id5 = MmAllocKernelPageFrame (ptr3, &n3, 4097, false); + + printBusyPages(); + + DebugLog("\nFree 6677 and 1200 bytes\n"); + MmFreeKernelPageFrame(id1); + MmFreeKernelPageFrame(id3); + + DebugLog("\nAlloc 10000 bytes\n"); + void **ptr6 = NULL; + size_t n6 = 0; + ulong id6 = MmAllocKernelPageFrame (ptr3, &n3, 10000, false); + + printBusyPages(); + + DebugLog("\nFree 10000 bytes\n"); + MmFreeKernelPageFrame(id6); + + printBusyPages(); + + DebugLog("\nAlloc 10000 bytes contiguous\n"); + void **ptr7 = NULL; + size_t n7 = 0; + ulong id7 = MmAllocKernelPageFrame (ptr3, &n3, 10000, true); + + printBusyPages(); return EOK; } From 2bec082b2d1373f1b592d731312281e0ef18c666 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 14 Jan 2020 16:58:38 +0100 Subject: [PATCH 08/27] Page allocator functionnal #67 --- include/mm/palloc.h | 4 +++- kaleid/kernel/mm/palloc.c | 26 +++++++++++++------------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/include/mm/palloc.h b/include/mm/palloc.h index ba591c9..ad7c688 100644 --- a/include/mm/palloc.h +++ b/include/mm/palloc.h @@ -39,7 +39,9 @@ typedef struct AllocatedPage_t{ //----------------------------------------------------------------------------// -error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size); +ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous); +void MmFreePageFrame(ulong id); + error_t MmTestBusyPage(void); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 98b61da..cb0c2d7 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -100,9 +100,9 @@ static void removePageFromBusyList(void *phyPageAddr) } // -// Returns an id to identify a page frame allocated +// Returns an id to identify a page frame allocated (kernel) // -ulong MmAllocKernelPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous) +ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous) { static ulong id = 0; *pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1; @@ -143,7 +143,7 @@ ulong MmAllocKernelPageFrame(void **frameListPtr, size_t *pageNumber, size_t siz return id; } -void MmFreeKernelPageFrame(ulong id) +void MmFreePageFrame(ulong id) { AllocatedPage_t *busyPage = &busyPagesList; @@ -164,50 +164,50 @@ error_t MmTestBusyPage(void) DebugLog("\nAlloc 6677 bytes\n"); void **ptr = NULL; size_t n = 0; - ulong id1 = MmAllocKernelPageFrame (ptr, &n, 6677, false); + ulong id1 = MmAllocPageFrame (ptr, &n, 6677, false); DebugLog("\nAlloc 9045 bytes\n"); void **ptr2 = NULL; size_t n2 = 0; - ulong id2 = MmAllocKernelPageFrame (ptr2, &n2, 9045, false); + ulong id2 = MmAllocPageFrame (ptr2, &n2, 9045, false); DebugLog("\nAlloc 1200 bytes\n"); void **ptr3 = NULL; size_t n3 = 0; - ulong id3 = MmAllocKernelPageFrame (ptr3, &n3, 1200, false); + ulong id3 = MmAllocPageFrame (ptr3, &n3, 1200, false); DebugLog("\nAlloc 4096 bytes\n"); void **ptr4 = NULL; size_t n4 = 0; - ulong id4 = MmAllocKernelPageFrame (ptr3, &n3, 4096, false); + ulong id4 = MmAllocPageFrame (ptr3, &n3, 4096, false); DebugLog("\nAlloc 4097 bytes\n"); void **ptr5 = NULL; size_t n5 = 0; - ulong id5 = MmAllocKernelPageFrame (ptr3, &n3, 4097, false); + ulong id5 = MmAllocPageFrame (ptr3, &n3, 4097, false); printBusyPages(); DebugLog("\nFree 6677 and 1200 bytes\n"); - MmFreeKernelPageFrame(id1); - MmFreeKernelPageFrame(id3); + MmFreePageFrame(id1); + MmFreePageFrame(id3); DebugLog("\nAlloc 10000 bytes\n"); void **ptr6 = NULL; size_t n6 = 0; - ulong id6 = MmAllocKernelPageFrame (ptr3, &n3, 10000, false); + ulong id6 = MmAllocPageFrame (ptr3, &n3, 10000, false); printBusyPages(); DebugLog("\nFree 10000 bytes\n"); - MmFreeKernelPageFrame(id6); + MmFreePageFrame(id6); printBusyPages(); DebugLog("\nAlloc 10000 bytes contiguous\n"); void **ptr7 = NULL; size_t n7 = 0; - ulong id7 = MmAllocKernelPageFrame (ptr3, &n3, 10000, true); + ulong id7 = MmAllocPageFrame (ptr3, &n3, 10000, true); printBusyPages(); From d13472712d9a64c5d838df8e199edf498cba9149 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Tue, 14 Jan 2020 17:07:23 +0100 Subject: [PATCH 09/27] Page allocator functionnal #67 --- include/mm/palloc.h | 3 +++ kaleid/kernel/mm/palloc.c | 18 ++++++++++-------- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/include/mm/palloc.h b/include/mm/palloc.h index ad7c688..65699be 100644 --- a/include/mm/palloc.h +++ b/include/mm/palloc.h @@ -37,6 +37,9 @@ typedef struct AllocatedPage_t{ struct AllocatedPage_t *next; } AllocatedPage_t; +#define CONTIGUOUS true +#define NORMAL false + //----------------------------------------------------------------------------// ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous); diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index cb0c2d7..c2c4b8f 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -143,6 +143,9 @@ ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, boo return id; } +// +// Frees a page frame by its id +// void MmFreePageFrame(ulong id) { AllocatedPage_t *busyPage = &busyPagesList; @@ -164,27 +167,27 @@ error_t MmTestBusyPage(void) DebugLog("\nAlloc 6677 bytes\n"); void **ptr = NULL; size_t n = 0; - ulong id1 = MmAllocPageFrame (ptr, &n, 6677, false); + ulong id1 = MmAllocPageFrame (ptr, &n, 6677, NORMAL); DebugLog("\nAlloc 9045 bytes\n"); void **ptr2 = NULL; size_t n2 = 0; - ulong id2 = MmAllocPageFrame (ptr2, &n2, 9045, false); + ulong id2 = MmAllocPageFrame (ptr2, &n2, 9045, NORMAL); DebugLog("\nAlloc 1200 bytes\n"); void **ptr3 = NULL; size_t n3 = 0; - ulong id3 = MmAllocPageFrame (ptr3, &n3, 1200, false); + ulong id3 = MmAllocPageFrame (ptr3, &n3, 1200, NORMAL); DebugLog("\nAlloc 4096 bytes\n"); void **ptr4 = NULL; size_t n4 = 0; - ulong id4 = MmAllocPageFrame (ptr3, &n3, 4096, false); + ulong id4 = MmAllocPageFrame (ptr3, &n3, 4096, NORMAL); DebugLog("\nAlloc 4097 bytes\n"); void **ptr5 = NULL; size_t n5 = 0; - ulong id5 = MmAllocPageFrame (ptr3, &n3, 4097, false); + ulong id5 = MmAllocPageFrame (ptr3, &n3, 4097, NORMAL); printBusyPages(); @@ -195,7 +198,7 @@ error_t MmTestBusyPage(void) DebugLog("\nAlloc 10000 bytes\n"); void **ptr6 = NULL; size_t n6 = 0; - ulong id6 = MmAllocPageFrame (ptr3, &n3, 10000, false); + ulong id6 = MmAllocPageFrame (ptr3, &n3, 10000, NORMAL); printBusyPages(); @@ -207,10 +210,9 @@ error_t MmTestBusyPage(void) DebugLog("\nAlloc 10000 bytes contiguous\n"); void **ptr7 = NULL; size_t n7 = 0; - ulong id7 = MmAllocPageFrame (ptr3, &n3, 10000, true); + ulong id7 = MmAllocPageFrame (ptr3, &n3, 10000, CONTIGUOUS); printBusyPages(); return EOK; } - From 69b0a65a0f2619d96e5686b8c532ed5bae385673 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 Jan 2020 01:34:46 +0100 Subject: [PATCH 10/27] misc modifications --- include/mm/palloc.h | 2 +- kaleid/kernel/mm/map.c | 12 ++++--- kaleid/kernel/mm/palloc.c | 71 ++++++++++++++++++++++++------------- kaleid/kernel/sh/testcmds.c | 5 --- 4 files changed, 55 insertions(+), 35 deletions(-) diff --git a/include/mm/palloc.h b/include/mm/palloc.h index 65699be..98fbc3d 100644 --- a/include/mm/palloc.h +++ b/include/mm/palloc.h @@ -42,7 +42,7 @@ typedef struct AllocatedPage_t{ //----------------------------------------------------------------------------// -ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous); +ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous); void MmFreePageFrame(ulong id); error_t MmTestBusyPage(void); diff --git a/kaleid/kernel/mm/map.c b/kaleid/kernel/mm/map.c index fabe03b..08868de 100644 --- a/kaleid/kernel/mm/map.c +++ b/kaleid/kernel/mm/map.c @@ -105,7 +105,8 @@ static error_t InitMemoryMap(void) return EOK; } -size_t MmGetAvailZoneSize(void *start) { +size_t MmGetAvailZoneSize(void *start) +{ uint i; // Because the kernel is the kernel @@ -129,7 +130,8 @@ size_t MmGetAvailZoneSize(void *start) { return 0; } -bool MmIsFailingZoneSize(void *start) { +bool MmIsFailingZoneSize(void *start) +{ uint i; // Because the kernel is the kernel @@ -153,7 +155,8 @@ bool MmIsFailingZoneSize(void *start) { return 0; } -void *MmGetFirstAvailZone(void *start) { +void *MmGetFirstAvailZone(void *start) +{ uint i; void *current = 0; @@ -194,7 +197,8 @@ void *MmGetFirstAvailZone(void *start) { return current; } -void MmPrintMemoryMap(void) { +void MmPrintMemoryMap(void) +{ char avStr[15]; extern int shcol; diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index c2c4b8f..ff8ebcc 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -49,6 +49,11 @@ static bool isPageBusy(void *phyPageAddr) AllocatedPage_t *busyPage = &busyPagesList; bool isBusy = false; + // In case of NVS, ACPI or BADRAM zone, considered busy + if (!MmGetAvailZoneSize(phyPageAddr)) + return true; + + // Search in the busylist if the phy addr is here while(busyPage->next) { busyPage = busyPage->next; if (phyPageAddr == busyPage->phyAddress) { @@ -62,9 +67,14 @@ static bool isPageBusy(void *phyPageAddr) static void printBusyPages(void) { AllocatedPage_t *busyPage = &busyPagesList; - while(busyPage->next) { - busyPage = busyPage->next; - KernLog("Busy page at %p\n", busyPage->phyAddress); + + if (!busyPage->next) { + KernLog("No busy page\n"); + } else { + while(busyPage->next) { + busyPage = busyPage->next; + KernLog("Busy page at %p\n", busyPage->phyAddress); + } } } @@ -102,12 +112,12 @@ static void removePageFromBusyList(void *phyPageAddr) // // Returns an id to identify a page frame allocated (kernel) // -ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, bool contiguous) +ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous) { static ulong id = 0; *pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1; - frameListPtr = (void**)malloc(sizeof(void*)*(*pageNumber)); + *frameListPtr = (void**)malloc(sizeof(void*)*(*pageNumber)); size_t curNumber = 0; bool inBlock = false; @@ -121,7 +131,7 @@ ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, boo for (void *curPage = (void*)MmPhysLastKernAddress; curPage < (void*)phRamSize; curPage += KPAGESIZE) { if (!isPageBusy(curPage)) { - frameListPtr[curNumber] = curPage; + (*frameListPtr)[curNumber] = curPage; inBlock = true; //DebugLog("Select page : %p\n", curPage); if (++curNumber >= *pageNumber) { @@ -136,8 +146,8 @@ ulong MmAllocPageFrame(void **frameListPtr, size_t *pageNumber, size_t size, boo } for (size_t i = 0; i < *pageNumber; i++) { - addPageToBusyList(frameListPtr[i], id); - //DebugLog("Allocating page : %p\n", frameListPtr[i]); + addPageToBusyList((*frameListPtr)[i], id); + //DebugLog("Allocating page : %p\n", *frameListPtr[i]); } return id; @@ -164,53 +174,64 @@ error_t MmTestBusyPage(void) DebugLog("\nBusy pages\n"); printBusyPages(); - DebugLog("\nAlloc 6677 bytes\n"); + void **ptr = NULL; size_t n = 0; - ulong id1 = MmAllocPageFrame (ptr, &n, 6677, NORMAL); + ulong id1 = MmAllocPageFrame(&ptr, &n, 6677, NORMAL); + DebugLog("\nAlloc 6677 bytes : %p, %d pages, first at %p\n", ptr, n, ptr[0]); - DebugLog("\nAlloc 9045 bytes\n"); void **ptr2 = NULL; size_t n2 = 0; - ulong id2 = MmAllocPageFrame (ptr2, &n2, 9045, NORMAL); + ulong id2 = MmAllocPageFrame(&ptr2, &n2, 9045, NORMAL); + DebugLog("\nAlloc 9045 bytes: %p, %d pages, first at %p\n", ptr2, n2, ptr2[0]); - DebugLog("\nAlloc 1200 bytes\n"); void **ptr3 = NULL; size_t n3 = 0; - ulong id3 = MmAllocPageFrame (ptr3, &n3, 1200, NORMAL); + ulong id3 = MmAllocPageFrame(&ptr3, &n3, 1200, NORMAL); + DebugLog("\nAlloc 1200 bytes: %p, %d pages, first at %p\n", ptr3, n3, ptr3[0]); - DebugLog("\nAlloc 4096 bytes\n"); void **ptr4 = NULL; size_t n4 = 0; - ulong id4 = MmAllocPageFrame (ptr3, &n3, 4096, NORMAL); - - DebugLog("\nAlloc 4097 bytes\n"); + ulong id4 = MmAllocPageFrame(&ptr4, &n4, 4096, NORMAL); + DebugLog("\nAlloc 4096 bytes: %p, %d pages, first at %p\n", ptr4, n4, ptr4[0]); void **ptr5 = NULL; size_t n5 = 0; - ulong id5 = MmAllocPageFrame (ptr3, &n3, 4097, NORMAL); + ulong id5 = MmAllocPageFrame(&ptr5, &n5, 4097, NORMAL); + DebugLog("\nAlloc 4097 bytes: %p, %d pages, first at %p\n", ptr5, n5, ptr5[0]); printBusyPages(); - DebugLog("\nFree 6677 and 1200 bytes\n"); MmFreePageFrame(id1); MmFreePageFrame(id3); + DebugLog("\nFree 6677 and 1200 bytes\n"); - DebugLog("\nAlloc 10000 bytes\n"); void **ptr6 = NULL; size_t n6 = 0; - ulong id6 = MmAllocPageFrame (ptr3, &n3, 10000, NORMAL); + ulong id6 = MmAllocPageFrame(&ptr6, &n6, 10000, NORMAL); + DebugLog("\nAlloc 10000 bytes: %p, %d pages, first at %p\n", ptr6, n6, ptr6[0]); printBusyPages(); - DebugLog("\nFree 10000 bytes\n"); MmFreePageFrame(id6); + DebugLog("\nFree 10000 bytes\n"); printBusyPages(); - DebugLog("\nAlloc 10000 bytes contiguous\n"); void **ptr7 = NULL; size_t n7 = 0; - ulong id7 = MmAllocPageFrame (ptr3, &n3, 10000, CONTIGUOUS); + ulong id7 = MmAllocPageFrame(&ptr7, &n7, 10000, CONTIGUOUS); + DebugLog("\nAlloc 10000 bytes contiguous: %p, %d pages, first at %p\n", ptr7, n7, ptr7[0]); + + printBusyPages(); + + MmFreePageFrame(id1); + MmFreePageFrame(id2); + MmFreePageFrame(id3); + MmFreePageFrame(id4); + MmFreePageFrame(id5); + MmFreePageFrame(id6); + MmFreePageFrame(id7); + DebugLog("\nFree all bytes\n"); printBusyPages(); diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 4207ae1..cbd1f8b 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -254,11 +254,6 @@ error_t CmdPageUnmap(int argc, char **argv, char *cmdline) error_t CmdPageBlock(int argc, char **argv, char *cmdline) { - size_t size = (size_t)atoi(argv[1]); - bool usermode = (bool)atoi(argv[2]); - - size_t pageNum = 0; - error_t err = MmTestBusyPage(); return err; } From 7a7d458bc6997b3f4a2b635636c4c95bf75b11d1 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 Jan 2020 16:12:13 +0100 Subject: [PATCH 11/27] Page allocator fully functionnal #67 --- include/mm/paging.h | 13 ++++++ kaleid/kernel/mm/paging.c | 21 ++++----- kaleid/kernel/mm/palloc.c | 87 ++++++++++++++++++++++++++++++++----- kaleid/kernel/sh/testcmds.c | 13 ------ 4 files changed, 97 insertions(+), 37 deletions(-) diff --git a/include/mm/paging.h b/include/mm/paging.h index 6dca6e1..b0f5568 100644 --- a/include/mm/paging.h +++ b/include/mm/paging.h @@ -49,6 +49,19 @@ typedef pde_t* pdpe_t; // Page directory L4 pointer offset typedef pdpe_t* pml4_t; +enum +{ + PRESENT = 1 << 0, + READWRITE = 1 << 1, + USERMODE = 1 << 2, + WRITETHR = 1 << 3, + CACHEDIS = 1 << 4, + ACCESSED = 1 << 5, + DIRTY = 1 << 6, + HUGE = 1 << 7, + NX = 1UL << 63 +}; + //----------------------------------------------------------------------------// // diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 7d13543..fdbf8b8 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -50,19 +50,6 @@ static ulong MmStackGuards[2] = { 0 }; ulong MmVirtLastAddress = 0; ulong MmPhysLastKernAddress = 0; -enum -{ - PRESENT = 1 << 0, - READWRITE = 1 << 1, - USERMODE = 1 << 2, - WRITETHR = 1 << 3, - CACHEDIS = 1 << 4, - ACCESSED = 1 << 5, - DIRTY = 1 << 6, - HUGE = 1 << 7, - NX = 1UL << 63 -}; - //----------- // @@ -302,6 +289,10 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) *page = ((ulong)physicalAddr & ~((KPAGESIZE - 1) | NX)) | flags; + MmPhysicalPageTable[(ulong)physicalAddr + / ((ulong)KPAGESIZE) + ] = (ulong)virtualAddr; + KeFlushTlbSingle(*page); } @@ -312,6 +303,10 @@ void MmUnmapPage(void* virtualAddr) { pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); + MmPhysicalPageTable[(ulong)(MmTransVirtToPhyAddr(virtualAddr)) + / ((ulong)KPAGESIZE) + ] = 0; + *page = 0; KeFlushTlbSingle(*page); diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index ff8ebcc..f021ddc 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -81,14 +81,22 @@ static void printBusyPages(void) static void addPageToBusyList(void *phyPageAddr, ulong id) { AllocatedPage_t *busyPage = &busyPagesList; + AllocatedPage_t *prevBusyPage = NULL; while(busyPage->next) { + prevBusyPage = busyPage; busyPage = busyPage->next; + + if (busyPage->phyAddress > phyPageAddr) { + busyPage = prevBusyPage; + break; + } } AllocatedPage_t *newBusyPage = (AllocatedPage_t*)malloc(sizeof(AllocatedPage_t)); newBusyPage->phyAddress = phyPageAddr; newBusyPage->id = id; + newBusyPage->next = busyPage->next; busyPage->next = newBusyPage; } @@ -129,7 +137,7 @@ ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bo DebugLog("Allocating %d pages...\n", *pageNumber); - for (void *curPage = (void*)MmPhysLastKernAddress; curPage < (void*)phRamSize; curPage += KPAGESIZE) { + for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { if (!isPageBusy(curPage)) { (*frameListPtr)[curNumber] = curPage; inBlock = true; @@ -169,6 +177,53 @@ void MmFreePageFrame(ulong id) } } +// +// Maps an allocated page frame to the given address +// +error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags) +{ + AllocatedPage_t *busyPage = &busyPagesList; + ulong offset = 0; + + while(busyPage->next) { + busyPage = busyPage->next; + + //DebugLog("Physical : %p is %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); + + if (MmTransPhyToVirtAddr(busyPage->phyAddress)) { + return EADDRINUSE; + } + + if (id == busyPage->id) { + MmMapPage((void*)((ulong)virtAddr + offset), busyPage->phyAddress, flags); + DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); + offset += KPAGESIZE; + } + } + + return EOK; +} + +error_t MmUnmapPageFrame(ulong id) +{ + AllocatedPage_t *busyPage = &busyPagesList; + void *actualPhys = 0; + + while(busyPage->next) { + busyPage = busyPage->next; + actualPhys = MmTransPhyToVirtAddr(busyPage->phyAddress); + + //DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys); + + if (actualPhys && id == busyPage->id) { + DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); + MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress)); + } + } + + return EOK; +} + error_t MmTestBusyPage(void) { DebugLog("\nBusy pages\n"); @@ -178,49 +233,49 @@ error_t MmTestBusyPage(void) void **ptr = NULL; size_t n = 0; ulong id1 = MmAllocPageFrame(&ptr, &n, 6677, NORMAL); - DebugLog("\nAlloc 6677 bytes : %p, %d pages, first at %p\n", ptr, n, ptr[0]); + DebugLog("Alloc 6677 bytes : %p, %d pages, first at %p\n", ptr, n, ptr[0]); void **ptr2 = NULL; size_t n2 = 0; ulong id2 = MmAllocPageFrame(&ptr2, &n2, 9045, NORMAL); - DebugLog("\nAlloc 9045 bytes: %p, %d pages, first at %p\n", ptr2, n2, ptr2[0]); + DebugLog("Alloc 9045 bytes: %p, %d pages, first at %p\n", ptr2, n2, ptr2[0]); void **ptr3 = NULL; size_t n3 = 0; ulong id3 = MmAllocPageFrame(&ptr3, &n3, 1200, NORMAL); - DebugLog("\nAlloc 1200 bytes: %p, %d pages, first at %p\n", ptr3, n3, ptr3[0]); + DebugLog("Alloc 1200 bytes: %p, %d pages, first at %p\n", ptr3, n3, ptr3[0]); void **ptr4 = NULL; size_t n4 = 0; ulong id4 = MmAllocPageFrame(&ptr4, &n4, 4096, NORMAL); - DebugLog("\nAlloc 4096 bytes: %p, %d pages, first at %p\n", ptr4, n4, ptr4[0]); + DebugLog("Alloc 4096 bytes: %p, %d pages, first at %p\n", ptr4, n4, ptr4[0]); void **ptr5 = NULL; size_t n5 = 0; ulong id5 = MmAllocPageFrame(&ptr5, &n5, 4097, NORMAL); - DebugLog("\nAlloc 4097 bytes: %p, %d pages, first at %p\n", ptr5, n5, ptr5[0]); + DebugLog("Alloc 4097 bytes: %p, %d pages, first at %p\n", ptr5, n5, ptr5[0]); printBusyPages(); MmFreePageFrame(id1); MmFreePageFrame(id3); - DebugLog("\nFree 6677 and 1200 bytes\n"); + DebugLog("Free 6677 and 1200 bytes\n"); void **ptr6 = NULL; size_t n6 = 0; ulong id6 = MmAllocPageFrame(&ptr6, &n6, 10000, NORMAL); - DebugLog("\nAlloc 10000 bytes: %p, %d pages, first at %p\n", ptr6, n6, ptr6[0]); + DebugLog("Alloc 10000 bytes: %p, %d pages, first at %p\n", ptr6, n6, ptr6[0]); printBusyPages(); MmFreePageFrame(id6); - DebugLog("\nFree 10000 bytes\n"); + DebugLog("Free 10000 bytes\n"); printBusyPages(); void **ptr7 = NULL; size_t n7 = 0; ulong id7 = MmAllocPageFrame(&ptr7, &n7, 10000, CONTIGUOUS); - DebugLog("\nAlloc 10000 bytes contiguous: %p, %d pages, first at %p\n", ptr7, n7, ptr7[0]); + DebugLog("Alloc 10000 bytes contiguous: %p, %d pages, first at %p\n", ptr7, n7, ptr7[0]); printBusyPages(); @@ -231,9 +286,19 @@ error_t MmTestBusyPage(void) MmFreePageFrame(id5); MmFreePageFrame(id6); MmFreePageFrame(id7); - DebugLog("\nFree all bytes\n"); + DebugLog("Free all bytes\n"); printBusyPages(); + id1 = MmAllocPageFrame(&ptr, &n, 1*MB, NORMAL); + error_t err = MmMapPageFrame(id1, (void*)USERSPACE, PRESENT | USERMODE | READWRITE); + + if (err == EOK) + DebugLog("Map status : OK\n"); + if (err == EADDRINUSE) + DebugLog("Map status : NOK\n"); + + MmUnmapPageFrame(id1); + return EOK; } diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index cbd1f8b..0973993 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -220,19 +220,6 @@ error_t CmdPageTranslatePhyToVirt(int argc, char **argv, char *cmdline) return EOK; } -enum -{ - PRESENT = 1 << 0, - READWRITE = 1 << 1, - USERMODE = 1 << 2, - WRITETHR = 1 << 3, - CACHEDIS = 1 << 4, - ACCESSED = 1 << 5, - DIRTY = 1 << 6, - HUGE = 1 << 7, - NX = 1UL << 63 -}; - error_t CmdPageMap(int argc, char **argv, char *cmdline) { void *virtual = (void*)strtoul(argv[1], NULL, 16); From 25b5eb6c783a76cbc0008f338211c5358890366d Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Wed, 15 Jan 2020 16:26:44 +0100 Subject: [PATCH 12/27] Cleanup in boot --- boot/loader/mem/management.inc | 39 ++++++++++++++-------------------- boot/loader/mem/structures.inc | 2 +- 2 files changed, 17 insertions(+), 24 deletions(-) diff --git a/boot/loader/mem/management.inc b/boot/loader/mem/management.inc index dc67950..961478f 100644 --- a/boot/loader/mem/management.inc +++ b/boot/loader/mem/management.inc @@ -23,43 +23,36 @@ ; along with OS/K. If not, see . ; ;=----------------------------------------------------------------------------=; -%define MAX_MEMORY 1 ; GiB - [BITS 32] [section .text] ; ---------------------------------------------------------------------------- ; ; Constructor for the page tables in protected mode ; ; ---------------------------------------------------------------------------- ; Setup_paging: - ;; Map the first PML4 entry to PDP table + ;; Map first PML4 entry to PDP table mov eax, PDP_table - or eax, 1 << 1 | 1 << 0 ; present + writable + or eax, 0b11 ; Present + writable mov [PML4_table], eax - ;; Map the PDP entries to PD tables - mov ebx, PD_table ; start address - mov ecx, 0x0 ; counter variable -.map_pdp_table: - mov eax, ebx - or eax, 1 << 1 | 1 << 0 ; present + writable - mov [PDP_table + 8 * ecx], eax - inc ecx - add ebx, 4096 - cmp ecx, MAX_MEMORY ; PDP table is mapped if MAX_MEMORY - jne .map_pdp_table ; else map the next entry + ;; Map first PDP entry to PD table + mov eax, PD_table + or eax, 0b11 ; Present + writable + mov [PDP_table], eax - ;; Map each PD entry to a 'huge' 4MiB page + ;; Map each PD entry to a huge 2MiB page + mov ecx, 0 ; counter variable - mov ecx, 0x0 ; counter variable .map_pd_table: - ;; map ecx-th PD entry to a huge page that starts at address 4MiB*ecx - mov eax, 0x200000 + ;; Map ecx-th PD entry to a huge page that starts at address 2MiB*ecx + mov eax, 0x200000 ; 2MiB mul ecx ; start address of ecx-th page - or eax, 1 << 7 | 1 << 1 | 1 << 0 ; present + writable + huge - mov [PD_table + ecx * 8], eax - inc ecx - cmp ecx, 512 * MAX_MEMORY ; PD table is mapped if 512 + or eax, 0b10000011 ; present + writable + huge + mov [PD_table + ecx * 8], eax ; map ecx-th entry + + inc ecx ; increase counter + cmp ecx, 512 ; if counter == 512, the whole P2 table is mapped jne .map_pd_table ; else map the next entry + ret ; ---------------------------------------------------------------------------- ; diff --git a/boot/loader/mem/structures.inc b/boot/loader/mem/structures.inc index 590f651..ebed229 100644 --- a/boot/loader/mem/structures.inc +++ b/boot/loader/mem/structures.inc @@ -63,4 +63,4 @@ PML4_table: PDP_table: resb 4096 PD_table: - times MAX_MEMORY resb 4096 + resb 4096 From de43801a48a83f72980638c844187a53968ffa4f Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 16 Jan 2020 00:08:04 +0100 Subject: [PATCH 13/27] [BUG] Problem with stack during paging init --- include/mm/palloc.h | 5 +- kaleid/kernel/mm/paging.c | 49 ++++++++-------- kaleid/kernel/mm/palloc.c | 116 ++++++++++++++------------------------ 3 files changed, 67 insertions(+), 103 deletions(-) diff --git a/include/mm/palloc.h b/include/mm/palloc.h index 98fbc3d..98dc5b2 100644 --- a/include/mm/palloc.h +++ b/include/mm/palloc.h @@ -42,8 +42,9 @@ typedef struct AllocatedPage_t{ //----------------------------------------------------------------------------// -ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous); -void MmFreePageFrame(ulong id); +ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous); +ulong MmAllocPageFrame(size_t size, bool contiguous); +void MmFreePageFrame(ulong id); error_t MmTestBusyPage(void); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index fdbf8b8..108911d 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -60,7 +60,7 @@ void MmInitPaging(void) pdpe_t *MmPDP = NULL; pde_t *MmPD = NULL; pte_t *MmPT = NULL; - ulong index, xedni; + register ulong index, xedni; ulong firstDirectoryAddr = 0; ulong lastDirectoryAddr = 0; ulong phDirSize = 0; @@ -74,19 +74,18 @@ void MmInitPaging(void) MmPhysLastKernAddress = (ulong)(_heap_start + _heap_max); ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastKernAddress - KPAGESIZE; + // Size of physical table + phDirSize = (((phRamSize + KPAGESIZE) / KPAGESIZE)*sizeof(ulong)); + // Maximum VIRTUAL address in memory MmVirtLastAddress = phRamSize + diffKernUsr; - //DebugLog("\tPaging gap : %u MB (%p)\n\tLast virtual address %p\n", diffKernUsr / MB, diffKernUsr, MmVirtLastAddress); + // Alloc structures + memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); + KalAllocMemoryEx(&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); - memzero((void *)&MmPageMapLevel4[0], sizeof(MmPageMapLevel4)); - phDirSize = ((phRamSize / KPAGESIZE)*sizeof(ulong) + KPAGESIZE) & ( ~((KPAGESIZE - 1) | NX)); - - 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; + for (register ulong curAddrPML4 = 0; + curAddrPML4 < phRamSize; curAddrPML4 += ((ulong)KPAGESIZE * 0x8000000)) { // Create an entry in PML4 each 512GB // 0x8000000 = 512 ^ 3 @@ -102,9 +101,9 @@ void MmInitPaging(void) //DebugLog("\t\t\t\tPDP %d : %p\n", index, MmPDP); MmPageMapLevel4[index] = (pdpe_t *)((ulong)MmPDP | PRESENT | READWRITE); - for (ulong curAddrPDP = curAddrPML4; + for (register ulong curAddrPDP = curAddrPML4; curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)) && - curAddrPDP < MmVirtLastAddress; + curAddrPDP < phRamSize; curAddrPDP += ((ulong)KPAGESIZE * 0x40000)) { // Create an intry in PDP each 1GB // 0x40000 = 512 ^ 2 @@ -116,9 +115,9 @@ void MmInitPaging(void) //DebugLog("\t\t\t\tPD %d : %p\n", index, MmPD); MmPDP[index] = (pde_t *)((ulong)MmPD | PRESENT | READWRITE); - for (ulong curAddrPD = curAddrPDP; + for (register ulong curAddrPD = curAddrPDP; curAddrPD < (curAddrPDP + ((ulong)KPAGESIZE * 0x40000)) && - curAddrPD < MmVirtLastAddress; + curAddrPD < phRamSize; curAddrPD += ((ulong)KPAGESIZE * 0x200)) { // Create an intry in PD each 2MB // 0x200 = 512 @@ -130,16 +129,17 @@ void MmInitPaging(void) //DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT); MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT | READWRITE); - for (ulong curAddrPT = curAddrPD; + for (register ulong curAddrPT = curAddrPD; curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)) && - curAddrPT < MmVirtLastAddress; + curAddrPT < phRamSize; curAddrPT += (ulong)KPAGESIZE) { // 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); + if (curAddrPT == 0x973db000) + DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT); // STACK GUARD PAGE */ if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { @@ -181,21 +181,13 @@ void MmInitPaging(void) //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 */ - /* xedni = (((ulong)curAddrPT - diffKernUsr) / ((ulong)KPAGESIZE)); */ - /* //MmPhysicalPageTable[xedni] = (ulong)curAddrPT; */ - - /* if ((ulong)curAddrPT == USERSPACE) { */ - /* DebugLog("\tUserspace at %p:%p\n", curAddrPT, curAddrPT - diffKernUsr); */ - /* } */ - /* } */ else { MmPT[index] = 0; + MmPhysicalPageTable[xedni] = 0; } KeFlushTlbSingle(curAddrPT); + asm (""); } } } @@ -294,6 +286,9 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) ] = (ulong)virtualAddr; KeFlushTlbSingle(*page); + + if (virtualAddr > MmVirtLastAddress) + MmVirtLastAddress = virtualAddr; } // diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index f021ddc..1e3bafe 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -29,6 +29,7 @@ #include #include #include +#include //--------- @@ -42,6 +43,9 @@ static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 }; extern MemoryMap_t memoryMap; extern ulong MmPhysLastKernAddress; +static ulong NSuccessfulAlloc = 0; +static ulong NSuccessfulFree = 0; + //--------- static bool isPageBusy(void *phyPageAddr) @@ -120,7 +124,7 @@ static void removePageFromBusyList(void *phyPageAddr) // // Returns an id to identify a page frame allocated (kernel) // -ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous) +ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous) { static ulong id = 0; *pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1; @@ -135,7 +139,7 @@ ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bo // Maximum PHYSICAL address in memory ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; - DebugLog("Allocating %d pages...\n", *pageNumber); + //DebugLog("Allocating %d pages...\n", *pageNumber); for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { if (!isPageBusy(curPage)) { @@ -153,28 +157,47 @@ ulong MmAllocPageFrame(void ***frameListPtr, size_t *pageNumber, size_t size, bo curNumber = 0; } + if (curNumber != *pageNumber) { + KeStartPanic("MmAllocPageFrameEx() : No more free pages to allocate"); + } + for (size_t i = 0; i < *pageNumber; i++) { addPageToBusyList((*frameListPtr)[i], id); //DebugLog("Allocating page : %p\n", *frameListPtr[i]); } + NSuccessfulAlloc++; + return id; } +ulong MmAllocPageFrame(size_t size, bool contiguous) +{ + void **ptr = NULL; + ulong d = 0; + return MmAllocPageFrameEx(&ptr, &d, size, contiguous); +} + + // // Frees a page frame by its id // void MmFreePageFrame(ulong id) { AllocatedPage_t *busyPage = &busyPagesList; + bool success = false; while(busyPage->next) { busyPage = busyPage->next; if (id == busyPage->id) { removePageFromBusyList(busyPage->phyAddress); + success = true; } } + + if (success) + NSuccessfulFree++; } // @@ -196,7 +219,7 @@ error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags) if (id == busyPage->id) { MmMapPage((void*)((ulong)virtAddr + offset), busyPage->phyAddress, flags); - DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); + //DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); offset += KPAGESIZE; } } @@ -216,7 +239,7 @@ error_t MmUnmapPageFrame(ulong id) //DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys); if (actualPhys && id == busyPage->id) { - DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); + //DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress)); } } @@ -226,79 +249,24 @@ error_t MmUnmapPageFrame(ulong id) error_t MmTestBusyPage(void) { - DebugLog("\nBusy pages\n"); - printBusyPages(); + ulong tab[2000] = {0}; + int j = 0; + for (int i = 0; i < 2000; i++) { + if (rand() %2) { + if (rand() %2) { + tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL); + } else { + tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS); + } + } else { + MmFreePageFrame(tab[rand() % (j+1)]); + } + } - void **ptr = NULL; - size_t n = 0; - ulong id1 = MmAllocPageFrame(&ptr, &n, 6677, NORMAL); - DebugLog("Alloc 6677 bytes : %p, %d pages, first at %p\n", ptr, n, ptr[0]); + //printBusyPages(); - void **ptr2 = NULL; - size_t n2 = 0; - ulong id2 = MmAllocPageFrame(&ptr2, &n2, 9045, NORMAL); - DebugLog("Alloc 9045 bytes: %p, %d pages, first at %p\n", ptr2, n2, ptr2[0]); - - void **ptr3 = NULL; - size_t n3 = 0; - ulong id3 = MmAllocPageFrame(&ptr3, &n3, 1200, NORMAL); - DebugLog("Alloc 1200 bytes: %p, %d pages, first at %p\n", ptr3, n3, ptr3[0]); - - void **ptr4 = NULL; - size_t n4 = 0; - ulong id4 = MmAllocPageFrame(&ptr4, &n4, 4096, NORMAL); - DebugLog("Alloc 4096 bytes: %p, %d pages, first at %p\n", ptr4, n4, ptr4[0]); - void **ptr5 = NULL; - size_t n5 = 0; - ulong id5 = MmAllocPageFrame(&ptr5, &n5, 4097, NORMAL); - DebugLog("Alloc 4097 bytes: %p, %d pages, first at %p\n", ptr5, n5, ptr5[0]); - - printBusyPages(); - - MmFreePageFrame(id1); - MmFreePageFrame(id3); - DebugLog("Free 6677 and 1200 bytes\n"); - - void **ptr6 = NULL; - size_t n6 = 0; - ulong id6 = MmAllocPageFrame(&ptr6, &n6, 10000, NORMAL); - DebugLog("Alloc 10000 bytes: %p, %d pages, first at %p\n", ptr6, n6, ptr6[0]); - - printBusyPages(); - - MmFreePageFrame(id6); - DebugLog("Free 10000 bytes\n"); - - printBusyPages(); - - void **ptr7 = NULL; - size_t n7 = 0; - ulong id7 = MmAllocPageFrame(&ptr7, &n7, 10000, CONTIGUOUS); - DebugLog("Alloc 10000 bytes contiguous: %p, %d pages, first at %p\n", ptr7, n7, ptr7[0]); - - printBusyPages(); - - MmFreePageFrame(id1); - MmFreePageFrame(id2); - MmFreePageFrame(id3); - MmFreePageFrame(id4); - MmFreePageFrame(id5); - MmFreePageFrame(id6); - MmFreePageFrame(id7); - DebugLog("Free all bytes\n"); - - printBusyPages(); - - id1 = MmAllocPageFrame(&ptr, &n, 1*MB, NORMAL); - error_t err = MmMapPageFrame(id1, (void*)USERSPACE, PRESENT | USERMODE | READWRITE); - - if (err == EOK) - DebugLog("Map status : OK\n"); - if (err == EADDRINUSE) - DebugLog("Map status : NOK\n"); - - MmUnmapPageFrame(id1); + DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); return EOK; } From d99e22fe41c8bb2159052901e69a6d9b87dc24b2 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 16 Jan 2020 15:51:03 +0100 Subject: [PATCH 14/27] =?UTF-8?q?[BUG]=C2=A0Stack=20smashed=20during=20pag?= =?UTF-8?q?ing=20init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Makefile | 2 +- kaleid/kernel/mm/paging.c | 79 ++++++++++++++++++++++++++------------- 2 files changed, 53 insertions(+), 28 deletions(-) diff --git a/Makefile b/Makefile index 5e2b97f..bb5f2a7 100644 --- a/Makefile +++ b/Makefile @@ -46,7 +46,7 @@ CINCLUDES=-Iinclude CFLAGS1=-nostdlib -ffreestanding -mcmodel=large -std=gnu11 -fstack-protector-all -fdump-rtl-expand CFLAGS2= -c -mno-red-zone -mno-mmx -mno-sse -mno-sse2 CFLAGS= $(CFLAGS1) $(CFLAGS2) -CFLAGS_MATHS= $(CFLAGS1) -c -mno-red-zone -mno-mmx -mno-sse2 +CFLAGS_MATHS= $(CFLAGS1) -c -mno-red-zone -mno-mmx ifeq ($(mode), release) CFLAGS += -D_NO_DEBUG diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 108911d..6a9bd1d 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -60,7 +60,12 @@ void MmInitPaging(void) pdpe_t *MmPDP = NULL; pde_t *MmPD = NULL; pte_t *MmPT = NULL; - register ulong index, xedni; + ulong index, xedni; + ulong curAddrPML4; + ulong curAddrPDP; + ulong curAddrPD; + ulong curAddrPT; + ulong firstDirectoryAddr = 0; ulong lastDirectoryAddr = 0; ulong phDirSize = 0; @@ -82,32 +87,50 @@ void MmInitPaging(void) // Alloc structures memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); - KalAllocMemoryEx(&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); + KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); - for (register ulong curAddrPML4 = 0; - curAddrPML4 < phRamSize; + DebugLog("PhDirSize : %d\n", phDirSize/sizeof(ulong)); + + for (curAddrPML4 = 0; + curAddrPML4 < 512 * KPAGESIZE * 0x8000000; curAddrPML4 += ((ulong)KPAGESIZE * 0x8000000)) { // Create an entry in PML4 each 512GB // 0x8000000 = 512 ^ 3 + index = (curAddrPML4 / ((ulong)KPAGESIZE * 0x8000000)) % 512; + + if (curAddrPML4 > phRamSize) { + MmPageMapLevel4[index] = (pdpe_t *)0; + //DebugLog("PML4 %d\n", index); + continue; + } + MmPDP = (pdpe_t *)malloc(512*sizeof(pde_t)); if (!firstDirectoryAddr) { firstDirectoryAddr = (ulong)MmPDP; } - index = (curAddrPML4 / ((ulong)KPAGESIZE * 0x8000000)) % 512; - //DebugLog("\t\t\t\tPDP %d : %p\n", index, MmPDP); MmPageMapLevel4[index] = (pdpe_t *)((ulong)MmPDP | PRESENT | READWRITE); - for (register ulong curAddrPDP = curAddrPML4; - curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)) && - curAddrPDP < phRamSize; + for (curAddrPDP = curAddrPML4; + curAddrPDP < (curAddrPML4 + ((ulong)KPAGESIZE * 0x8000000)); curAddrPDP += ((ulong)KPAGESIZE * 0x40000)) { // Create an intry in PDP each 1GB // 0x40000 = 512 ^ 2 + index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; + + if (curAddrPDP > phRamSize) { + MmPDP[index] = (pde_t *)0; + //DebugLog("PDP %d\n", index); + continue; + } + + if (index == 0x447c0ffe4dbf9e55) + KeStartPanic("ERROR"); + MmPD = (pde_t *)malloc(512*sizeof(pde_t)); index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; @@ -115,23 +138,30 @@ void MmInitPaging(void) //DebugLog("\t\t\t\tPD %d : %p\n", index, MmPD); MmPDP[index] = (pde_t *)((ulong)MmPD | PRESENT | READWRITE); - for (register ulong curAddrPD = curAddrPDP; - curAddrPD < (curAddrPDP + ((ulong)KPAGESIZE * 0x40000)) && - curAddrPD < phRamSize; + for (curAddrPD = curAddrPDP; + curAddrPD < (curAddrPDP + ((ulong)KPAGESIZE * 0x40000)); curAddrPD += ((ulong)KPAGESIZE * 0x200)) { // Create an intry in PD each 2MB // 0x200 = 512 - MmPT = (pte_t *)malloc(512*sizeof(pte_t)); - index = (curAddrPD / ((ulong)KPAGESIZE * 0x200)) % 512; + if (curAddrPD > phRamSize) { + MmPD[index] = (pte_t *)0; + //DebugLog("PD %d\n", index); + continue; + } + + if (index == 0x447c0ffe4dbf9e55) + KeStartPanic("ERROR"); + + MmPT = (pte_t *)malloc(512*sizeof(pte_t)); + //DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT); MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT | READWRITE); - for (register ulong curAddrPT = curAddrPD; - curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)) && - curAddrPT < phRamSize; + for (curAddrPT = curAddrPD; + curAddrPT < (curAddrPD + ((ulong)KPAGESIZE * 0x200)); curAddrPT += (ulong)KPAGESIZE) { // Create an entry in PT each page of 4KB @@ -139,7 +169,7 @@ void MmInitPaging(void) xedni = (curAddrPT / ((ulong)KPAGESIZE)); if (curAddrPT == 0x973db000) - DebugLog("\t\t\t\tPage %d : %p\n", index, curAddrPT); + DebugLog("ERR : %p\n", &MmPhysicalPageTable[xedni]); // STACK GUARD PAGE */ if ((ulong)curAddrPT == (ulong)BtLoaderInfo.stackEndAddr) { @@ -176,18 +206,13 @@ void MmInitPaging(void) else if ((ulong)curAddrPT <= MmPhysLastKernAddress) { MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; - - if ((ulong)curAddrPT == MmPhysLastKernAddress) { - //DebugLog("\tLast page of kernel at %p\n", curAddrPT); - } } else { - MmPT[index] = 0; - MmPhysicalPageTable[xedni] = 0; + MmPT[index] = (ulong)0; + MmPhysicalPageTable[xedni] = (ulong)0; } KeFlushTlbSingle(curAddrPT); - asm (""); } } } @@ -287,8 +312,8 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) KeFlushTlbSingle(*page); - if (virtualAddr > MmVirtLastAddress) - MmVirtLastAddress = virtualAddr; + if ((ulong)virtualAddr > MmVirtLastAddress) + MmVirtLastAddress = (ulong)virtualAddr; } // From a22eb6669d1de0c9b16518ee1e2b48e44a72f176 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 17 Jan 2020 14:09:45 +0100 Subject: [PATCH 15/27] =?UTF-8?q?[BUG]=C2=A0Stack=20smashed=20during=20pag?= =?UTF-8?q?ing=20init?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- kaleid/kernel/init/init.c | 6 ++++++ kaleid/kernel/init/ssp.c | 2 +- kaleid/kernel/mm/paging.c | 27 ++++++++++----------------- kaleid/kernel/mm/palloc.c | 34 +++++++++++++++++++--------------- 4 files changed, 36 insertions(+), 33 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 3a3144c..7febf67 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -81,9 +81,15 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) KeGetCpuInfos(); IoEnableKeyb(); + for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) { + DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); + } + // Command line (kernel mode) ShStartShell(); + //KeCrashSystem(); // Exit ! PoShutdown(); + } diff --git a/kaleid/kernel/init/ssp.c b/kaleid/kernel/init/ssp.c index 958f1b2..bf2d70d 100644 --- a/kaleid/kernel/init/ssp.c +++ b/kaleid/kernel/init/ssp.c @@ -24,7 +24,7 @@ #include -ulong __stack_chk_guard = 0x447c0ffe4dbf9e55; +ulong __stack_chk_guard = 0xec0ffec0ffec0ffe; noreturn void __stack_chk_fail(void) { diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 6a9bd1d..af14533 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -34,8 +34,8 @@ //----------- -pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); -ulong *MmPhysicalPageTable; +static pml4_t MmPageMapLevel4[512] __attribute__((__aligned__(KPAGESIZE))); +static ulong *MmPhysicalPageTable __attribute__((__aligned__(KPAGESIZE))); extern ulong _text; extern ulong _text_end; @@ -89,7 +89,7 @@ void MmInitPaging(void) memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); - DebugLog("PhDirSize : %d\n", phDirSize/sizeof(ulong)); + DebugLog("\t\t\t\tPhysical map addr : %p\n", MmPhysicalPageTable); for (curAddrPML4 = 0; curAddrPML4 < 512 * KPAGESIZE * 0x8000000; @@ -128,9 +128,6 @@ void MmInitPaging(void) continue; } - if (index == 0x447c0ffe4dbf9e55) - KeStartPanic("ERROR"); - MmPD = (pde_t *)malloc(512*sizeof(pde_t)); index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; @@ -168,21 +165,18 @@ void MmInitPaging(void) index = (curAddrPT / ((ulong)KPAGESIZE)) % 512; xedni = (curAddrPT / ((ulong)KPAGESIZE)); - if (curAddrPT == 0x973db000) - DebugLog("ERR : %p\n", &MmPhysicalPageTable[xedni]); - // 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); + 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) { @@ -207,12 +201,6 @@ void MmInitPaging(void) MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; } - else { - MmPT[index] = (ulong)0; - MmPhysicalPageTable[xedni] = (ulong)0; - } - - KeFlushTlbSingle(curAddrPT); } } } @@ -223,6 +211,11 @@ void MmInitPaging(void) MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); + + for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) { + DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); + } + DebugLog("\n"); } // diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 1e3bafe..1b663e7 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -249,24 +249,28 @@ error_t MmUnmapPageFrame(ulong id) error_t MmTestBusyPage(void) { - ulong tab[2000] = {0}; - int j = 0; - - for (int i = 0; i < 2000; i++) { - if (rand() %2) { - if (rand() %2) { - tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL); - } else { - tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS); - } - } else { - MmFreePageFrame(tab[rand() % (j+1)]); - } + for (ulong curAddrPT = 0x00971cb000; curAddrPT <= 0x00971fb000; curAddrPT+= (ulong)KPAGESIZE) { + DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); } - //printBusyPages(); + /* ulong tab[2000] = {0}; */ + /* int j = 0; */ - DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); + /* for (int i = 0; i < 2000; i++) { */ + /* if (rand() %2) { */ + /* if (rand() %2) { */ + /* tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL); */ + /* } else { */ + /* tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS); */ + /* } */ + /* } else { */ + /* MmFreePageFrame(tab[rand() % (j+1)]); */ + /* } */ + /* } */ + + /* //printBusyPages(); */ + + /* DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); */ return EOK; } From f7e857a9465cd791b78f35cf93f3a7d66192265e Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 17 Jan 2020 22:57:09 +0100 Subject: [PATCH 16/27] [BUG] --- kaleid/kernel/sh/testcmds.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index 0973993..9d9895e 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -113,19 +113,19 @@ error_t CmdDumpATASect(int argc, char **argv, char *cmdline) error_t CmdDumpMem(int argc, char **argv, char *cmdline) { - char sector[1024] = {0}; + char sector[8] = {0}; char *address = (char*)strtoul(argv[1], NULL, 16); int nb = 1; //atoi(argv[2]); int x = 0; - int step = 16; + int step = 8; KernLog("Address begin: %p\n", address); - for (int i = 0; i < 1024*nb; i++) { + for (int i = 0; i < 8*nb; i++) { sector[i] = *address++; } - while(x < 1024*nb) { + while(x < 8*nb) { KernLog("%C", shcol); for (int i = 0; i < step; i++) { KernLog("%02x ", (uchar)sector[i+x]); From 018bdc4ff6611c3337693d03e06501ba7513fa39 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Fri, 17 Jan 2020 23:13:52 +0100 Subject: [PATCH 17/27] Paging now functionnal --- kaleid/kernel/init/init.c | 4 ---- kaleid/kernel/mm/gdt.c | 6 +++--- kaleid/kernel/mm/paging.c | 9 ++------- 3 files changed, 5 insertions(+), 14 deletions(-) diff --git a/kaleid/kernel/init/init.c b/kaleid/kernel/init/init.c index 7febf67..a739f7a 100644 --- a/kaleid/kernel/init/init.c +++ b/kaleid/kernel/init/init.c @@ -81,10 +81,6 @@ noreturn void BtStartKern(multiboot_info_t *mbInfo, uint mbMagic, void *codeSeg) KeGetCpuInfos(); IoEnableKeyb(); - for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) { - DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); - } - // Command line (kernel mode) ShStartShell(); diff --git a/kaleid/kernel/mm/gdt.c b/kaleid/kernel/mm/gdt.c index 6a5c188..372bebe 100644 --- a/kaleid/kernel/mm/gdt.c +++ b/kaleid/kernel/mm/gdt.c @@ -58,9 +58,9 @@ void MmInitGdt(void) tssDesc.veryHighBase = ((ulong)&tss >> 32) & 0xFFFFFFFF; tssDesc.lowLimit = sizeof(tss); - tss.ist1 = (ulong)0x0007FFFF; // ISR RESCUE STACK, GARANTIED FREE FOR USE BY OSDEV.ORG - tss.ist2 = (ulong)0x00EFFFFF; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG - tss.ist3 = (ulong)0x00EF0000; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist1 = (ulong)0x00007BFF; // ISR RESCUE STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist2 = (ulong)0x00043F00; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG + tss.ist3 = (ulong)0x0007FFFF; // ISR STACK, GARANTIED FREE FOR USE BY OSDEV.ORG tss.iomap_base = sizeof(tss); memmove(&gdt[2], &tssDesc, sizeof(TssDescriptor_t)); diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index af14533..0bd1443 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -170,13 +170,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) { @@ -211,11 +211,6 @@ void MmInitPaging(void) MmEnableWriteProtect(); DebugLog("\tPage table size : %u MB\n", (lastDirectoryAddr - firstDirectoryAddr + phDirSize)/MB); - - for (ulong curAddrPT = 0x00971db000; curAddrPT <= 0x00971fc000; curAddrPT+= (ulong)KPAGESIZE) { - DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); - } - DebugLog("\n"); } // From 6a498c2d0809fa5be3a1f1c318cb4e5ee5df6a29 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 18 Jan 2020 00:48:05 +0100 Subject: [PATCH 18/27] Some tests --- include/ke/time.h | 2 +- kaleid/kernel/ke/pit.c | 5 +++++ kaleid/kernel/mm/paging.c | 12 +++++----- kaleid/kernel/mm/palloc.c | 47 ++++++++++++++++++++++++++++++--------- 4 files changed, 49 insertions(+), 17 deletions(-) diff --git a/include/ke/time.h b/include/ke/time.h index b205289..8407312 100644 --- a/include/ke/time.h +++ b/include/ke/time.h @@ -64,7 +64,7 @@ void KeEnablePIT(void); void KeSleep(uint); Timer_t *KeSetTimer(uint delay); int KeGetTimer(Timer_t*); - +ulong KeGetTicks(void); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/ke/pit.c b/kaleid/kernel/ke/pit.c index a9d961c..4a0ee7b 100644 --- a/kaleid/kernel/ke/pit.c +++ b/kaleid/kernel/ke/pit.c @@ -219,3 +219,8 @@ ulong KeGetTimeStamp(void) + (time->year + time->century * 100) * dpy * 24 * 60 * 60; } + +ulong KeGetTicks(void) +{ + return Ticks; +} diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 0bd1443..b13f0c6 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -89,7 +89,7 @@ void MmInitPaging(void) memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); - DebugLog("\t\t\t\tPhysical map addr : %p\n", MmPhysicalPageTable); + //DebugLog("\t\t\t\tPhysical map addr : %p\n", MmPhysicalPageTable); for (curAddrPML4 = 0; curAddrPML4 < 512 * KPAGESIZE * 0x8000000; @@ -170,31 +170,31 @@ 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) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; - //DebugLog("\tSection .text at %p\n", 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); + 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 | NX; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; - //DebugLog("\tSection .rodata at %p\n", curAddrPT); + DebugLog("\tSection .rodata at %p\n", curAddrPT); } // While we're inside the kernel pages else if ((ulong)curAddrPT <= MmPhysLastKernAddress) { diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 1b663e7..6ce3356 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -29,6 +29,7 @@ #include #include #include +#include #include //--------- @@ -82,6 +83,23 @@ static void printBusyPages(void) } } +static ulong MmBusyPagesSpace(void) +{ + ulong c = 0; + AllocatedPage_t *busyPage = &busyPagesList; + + if (!busyPage->next) { + return 0; + } else { + while(busyPage->next) { + busyPage = busyPage->next; + c += 4096; + } + } + return c; +} + + static void addPageToBusyList(void *phyPageAddr, ulong id) { AllocatedPage_t *busyPage = &busyPagesList; @@ -247,30 +265,39 @@ error_t MmUnmapPageFrame(ulong id) return EOK; } +ulong tab[4000] = {0}; + error_t MmTestBusyPage(void) { - for (ulong curAddrPT = 0x00971cb000; curAddrPT <= 0x00971fb000; curAddrPT+= (ulong)KPAGESIZE) { - DebugLog("\t\t\t\tTable %p : %p\n", MmTransPhyToVirtAddr((void*)curAddrPT), MmTransVirtToPhyAddr((void*)curAddrPT)); - } - - /* ulong tab[2000] = {0}; */ - /* int j = 0; */ + int j = 0; /* for (int i = 0; i < 2000; i++) { */ /* if (rand() %2) { */ /* if (rand() %2) { */ - /* tab[j++] = MmAllocPageFrame(rand()%65536, NORMAL); */ + /* tab[j++] = MmAllocPageFrame(rand()%6553689, NORMAL); */ /* } else { */ - /* tab[j++] = MmAllocPageFrame(rand()%65536, CONTIGUOUS); */ + /* tab[j++] = MmAllocPageFrame(rand()%6553689, CONTIGUOUS); */ /* } */ /* } else { */ /* MmFreePageFrame(tab[rand() % (j+1)]); */ /* } */ + /* DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */ /* } */ - /* //printBusyPages(); */ + ulong a = KeGetTicks(); + tab[j++] = MmAllocPageFrame(50*MB, NORMAL); + tab[j++] = MmAllocPageFrame(50*MB, NORMAL); + tab[j++] = MmAllocPageFrame(50*MB, NORMAL); + tab[j++] = MmAllocPageFrame(50*MB, NORMAL); + tab[j++] = MmAllocPageFrame(50*MB, NORMAL); + ulong b = KeGetTicks(); - /* DebugLog("Alloc : %d; Free : %d\n", NSuccessfulAlloc, NSuccessfulFree); */ + DebugLog("Alloc time per MB : %lu\n", (b-a)/5/5); + + //printBusyPages(); + DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); + + DebugLog("Finished !\n"); return EOK; } From ded1e99fb976636f5283d1b52b4cd1c5839b25f4 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 18 Jan 2020 12:50:36 +0100 Subject: [PATCH 19/27] Working on allocator --- include/mm/paging.h | 2 +- kaleid/kernel/mm/paging.c | 11 +++++- kaleid/kernel/mm/palloc.c | 78 +++++++++++++++++++++++++++------------ 3 files changed, 66 insertions(+), 25 deletions(-) diff --git a/include/mm/paging.h b/include/mm/paging.h index b0f5568..c4f2845 100644 --- a/include/mm/paging.h +++ b/include/mm/paging.h @@ -33,7 +33,7 @@ #define KPAGESIZE (4 * KB) #define UPAGESIZE (4 * KB) -#define USERSPACE 0x100000000 +#define USERSPACE 0x200000000 //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index b13f0c6..bbd6c46 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -201,6 +201,10 @@ void MmInitPaging(void) MmPT[index] = (ulong)curAddrPT | PRESENT | READWRITE; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; } + else { + MmPT[index] = (ulong)0; + MmPhysicalPageTable[xedni] = (ulong)0; + } } } } @@ -292,7 +296,12 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); - *page = ((ulong)physicalAddr & ~((KPAGESIZE - 1) | NX)) | flags; + DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags); + DebugLog("Page (at %p) %p : was %p\n", &page, page, *page); + + *page = (ulong)physicalAddr | flags; + + DebugLog("Page (at %p) %p : is %p\n", &page, page, *page); MmPhysicalPageTable[(ulong)physicalAddr / ((ulong)KPAGESIZE) diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 6ce3356..bd808a3 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -158,21 +158,32 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; //DebugLog("Allocating %d pages...\n", *pageNumber); - - for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { - if (!isPageBusy(curPage)) { - (*frameListPtr)[curNumber] = curPage; - inBlock = true; - //DebugLog("Select page : %p\n", curPage); - if (++curNumber >= *pageNumber) { - break; + if (contiguous) { + for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { + if (!isPageBusy(curPage)) { + (*frameListPtr)[curNumber] = curPage; + inBlock = true; + //DebugLog("Select page : %p\n", curPage); + if (++curNumber >= *pageNumber) { + break; + } + } else { + inBlock = false; + } + if (contiguous) + if (!inBlock) + curNumber = 0; + } + } else { + for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { + if (!isPageBusy(curPage)) { + (*frameListPtr)[curNumber] = curPage; + //DebugLog("Select page : %p\n", curPage); + if (++curNumber >= *pageNumber) { + break; + } } - } else { - inBlock = false; } - if (contiguous) - if (!inBlock) - curNumber = 0; } if (curNumber != *pageNumber) { @@ -236,8 +247,8 @@ error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags) } if (id == busyPage->id) { + DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); MmMapPage((void*)((ulong)virtAddr + offset), busyPage->phyAddress, flags); - //DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); offset += KPAGESIZE; } } @@ -285,18 +296,39 @@ error_t MmTestBusyPage(void) /* } */ ulong a = KeGetTicks(); - tab[j++] = MmAllocPageFrame(50*MB, NORMAL); - tab[j++] = MmAllocPageFrame(50*MB, NORMAL); - tab[j++] = MmAllocPageFrame(50*MB, NORMAL); - tab[j++] = MmAllocPageFrame(50*MB, NORMAL); - tab[j++] = MmAllocPageFrame(50*MB, NORMAL); + DebugLog("Start alloc : %lu s\n", a/1000); + tab[j++] = MmAllocPageFrame(30*MB, NORMAL); ulong b = KeGetTicks(); - - DebugLog("Alloc time per MB : %lu\n", (b-a)/5/5); - - //printBusyPages(); + DebugLog("End alloc : %lu s\n", b/1000); + DebugLog("Alloc time : %lu s\n", (b-a)/1000); DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); + a = KeGetTicks(); + DebugLog("Start alloc : %lu s\n", a/1000); + tab[j++] = MmAllocPageFrame(20*MB, NORMAL); + b = KeGetTicks(); + DebugLog("End alloc : %lu s\n", b/1000); + DebugLog("Alloc time : %lu s\n", (b-a)/1000); + DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); + + j = 0; + + a = KeGetTicks(); + DebugLog("Start free : %lu ms\n", a); + MmFreePageFrame(tab[j++]); + b = KeGetTicks(); + DebugLog("End free : %lu ms\n", b); + DebugLog("Free time : %lu ms\n", (b-a)); + DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); + + a = KeGetTicks(); + DebugLog("Start map : %lu ms\n", a); + MmMapPageFrame(tab[1], (void*)USERSPACE, PRESENT | USERMODE | READWRITE); + b = KeGetTicks(); + DebugLog("End map : %lu ms\n", b); + DebugLog("Map time : %lu ms\n", (b-a)); + //printBusyPages(); + DebugLog("Finished !\n"); return EOK; From 77f4895d48058267f4bd7dd84ebaf4808aac03bd Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sat, 18 Jan 2020 13:40:56 +0100 Subject: [PATCH 20/27] Working on allocator --- kaleid/kernel/mm/paging.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index bbd6c46..7b8d487 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -31,6 +31,7 @@ #include #include #include +#include //----------- @@ -220,7 +221,7 @@ void MmInitPaging(void) // // Get a page from an address // -static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr) +static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) //XXX MUST CREATE NEW ENTRIES IN TREE { ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); @@ -228,27 +229,29 @@ static pte_t *MmGetPageDescriptorFromVirtual(void *virtualAddr) KeStartPanic("MmSetPage() Out of bound of the address space !"); } - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512] & ~((KPAGESIZE - 1) | NX | NX)); - //DebugLog("pdp\t: %p\n", pdp); - pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] & ~((KPAGESIZE - 1) | NX)); - //DebugLog("pd\t: %p\n", pd); - pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] & ~((KPAGESIZE - 1) | NX)); - //DebugLog("pt\t: %p\n", pt); + pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512]); + DebugLog("pdp\t: %p\n", pdp); + pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] ); + DebugLog("pd\t: %p\n", pd); + pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] ); + DebugLog("pt\t: %p\n", pt); - pte_t *page = &pt[(virtAddrPage / ((ulong)KPAGESIZE)) % 512]; - //DebugLog("page (with flags): %p\n", page); + ulong index = ((ulong)virtualAddr / ((ulong)KPAGESIZE)) % 512; + + ulong *page = &(pt[index]); + DebugLog("page (with flags): %p\n", page); + KeSleep(6000); return page; } - // // Translates a virtual address to its physical equivalent // void *MmTransVirtToPhyAddr(void* virtualAddr) { ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); - pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); + ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); if (*page == (*page & ~((KPAGESIZE - 1) | NX))) { return NULL; @@ -270,7 +273,7 @@ void *MmTransPhyToVirtAddr(void* physicalAddr) // void MmSetPage(void* virtualAddr, ulong flags) { - pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); + ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); *page |= flags; @@ -282,7 +285,7 @@ void MmSetPage(void* virtualAddr, ulong flags) // void MmUnsetPage(void* virtualAddr, ulong flags) { - pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); + ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); *page &= (~flags); @@ -294,14 +297,11 @@ void MmUnsetPage(void* virtualAddr, ulong flags) // void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { - pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); + ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags); - DebugLog("Page (at %p) %p : was %p\n", &page, page, *page); - *page = (ulong)physicalAddr | flags; - - DebugLog("Page (at %p) %p : is %p\n", &page, page, *page); + page[0] = (ulong)physicalAddr | flags; MmPhysicalPageTable[(ulong)physicalAddr / ((ulong)KPAGESIZE) @@ -318,7 +318,7 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) // void MmUnmapPage(void* virtualAddr) { - pte_t *page = MmGetPageDescriptorFromVirtual(virtualAddr); + ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); MmPhysicalPageTable[(ulong)(MmTransVirtToPhyAddr(virtualAddr)) / ((ulong)KPAGESIZE) From 9e033fa4413e63fc7d77bfde6ba3eb07e1548e16 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 19 Jan 2020 01:44:29 +0100 Subject: [PATCH 21/27] Bug with MmGetPageDescriptorFromVirtual() --- boot/loader/mem/structures.inc | 2 +- kaleid/kernel/mm/paging.c | 103 ++++++++++++++++++++------------- kaleid/kernel/mm/palloc.c | 30 +++++----- 3 files changed, 80 insertions(+), 55 deletions(-) diff --git a/boot/loader/mem/structures.inc b/boot/loader/mem/structures.inc index ebed229..75d392e 100644 --- a/boot/loader/mem/structures.inc +++ b/boot/loader/mem/structures.inc @@ -31,7 +31,7 @@ global newStackEnd global GDT64 [section .text] -KERNEL_STACK equ 16 * 1024 ; 16KB of stack +KERNEL_STACK equ 64 * 1024 ; 64KB of stack newKernelEnd dq 0x0 newStackEnd dq 0x0 diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 7b8d487..a146a94 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -78,13 +78,12 @@ void MmInitPaging(void) // Difference between the end of kernel and the begin of userspace MmPhysLastKernAddress = (ulong)(_heap_start + _heap_max); - ulong diffKernUsr = (ulong)USERSPACE - MmPhysLastKernAddress - KPAGESIZE; // Size of physical table phDirSize = (((phRamSize + KPAGESIZE) / KPAGESIZE)*sizeof(ulong)); // Maximum VIRTUAL address in memory - MmVirtLastAddress = phRamSize + diffKernUsr; + MmVirtLastAddress = phRamSize; // Alloc structures memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); @@ -100,13 +99,13 @@ void MmInitPaging(void) index = (curAddrPML4 / ((ulong)KPAGESIZE * 0x8000000)) % 512; - if (curAddrPML4 > phRamSize) { + if (curAddrPML4 > MmPhysLastKernAddress) { MmPageMapLevel4[index] = (pdpe_t *)0; - //DebugLog("PML4 %d\n", index); + ////DebugLog("PML4 %d\n", index); continue; } - MmPDP = (pdpe_t *)malloc(512*sizeof(pde_t)); + KalAllocMemoryEx((void**)&MmPDP, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); if (!firstDirectoryAddr) { firstDirectoryAddr = (ulong)MmPDP; @@ -123,13 +122,13 @@ void MmInitPaging(void) index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; - if (curAddrPDP > phRamSize) { + if (curAddrPDP > MmPhysLastKernAddress) { MmPDP[index] = (pde_t *)0; //DebugLog("PDP %d\n", index); continue; } - MmPD = (pde_t *)malloc(512*sizeof(pde_t)); + KalAllocMemoryEx((void**)&MmPD, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; @@ -144,16 +143,13 @@ void MmInitPaging(void) index = (curAddrPD / ((ulong)KPAGESIZE * 0x200)) % 512; - if (curAddrPD > phRamSize) { + if (curAddrPD > MmPhysLastKernAddress) { MmPD[index] = (pte_t *)0; //DebugLog("PD %d\n", index); continue; } - if (index == 0x447c0ffe4dbf9e55) - KeStartPanic("ERROR"); - - MmPT = (pte_t *)malloc(512*sizeof(pte_t)); + KalAllocMemoryEx((void**)&MmPT, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); //DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT); MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT | READWRITE); @@ -171,31 +167,31 @@ 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) { MmPT[index] = (ulong)curAddrPT | PRESENT; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; - DebugLog("\tSection .text at %p\n", 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); + //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 | NX; MmPhysicalPageTable[xedni] = (ulong)curAddrPT; - DebugLog("\tSection .rodata at %p\n", curAddrPT); + //DebugLog("\tSection .rodata at %p\n", curAddrPT); } // While we're inside the kernel pages else if ((ulong)curAddrPT <= MmPhysLastKernAddress) { @@ -221,27 +217,54 @@ void MmInitPaging(void) // // Get a page from an address // -static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) //XXX MUST CREATE NEW ENTRIES IN TREE +static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); + volatile ulong virtAddrPage; + volatile pdpe_t *pdp; + volatile pde_t *pd; + volatile pte_t *pt; + volatile ulong *page; + volatile ulong index; - if (virtAddrPage > MmVirtLastAddress) { - KeStartPanic("MmSetPage() Out of bound of the address space !"); + DebugLog("Get virtual descriptor %p\n", virtualAddr); + while (virtualAddr) { + virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); + + index = (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512; + pdp = (pdpe_t*)((ulong)MmPageMapLevel4[index] & ( ~(KPAGESIZE - 1)) ); + DebugLog("pdp at %p\t: %p\n", &pdp, pdp); + if (!pdp) { + KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); + MmPageMapLevel4[index] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE); + //DebugLog("Created pdp\t: %p\n", pdp); + continue; + } + + index = (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512; + pd = (pde_t*)( (ulong)pdp[index] & ( ~(KPAGESIZE - 1)) ); + DebugLog("pd at %p\t: %p\n", &pd, pd); + if (!pd) { + KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); + pdp[index] = (pde_t *)((ulong)pd | PRESENT | READWRITE); + //DebugLog("Created pd\t: %p\n", pd); + continue; + } + + index = (virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512; + pt = (pte_t*)( (ulong)pd[index] & ( ~(KPAGESIZE - 1)) ); + DebugLog("pt at %p\t: %p\n", &pt, pt); + if (!pt) { + KalAllocMemoryEx((void**)&pt, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); + pd[index] = (pte_t *)((ulong)pt | PRESENT | READWRITE); + //DebugLog("Created pt\t: %p\n", pt); + continue; + } + break; } - pdpe_t *pdp = (pdpe_t*)((ulong)MmPageMapLevel4[(virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512]); - DebugLog("pdp\t: %p\n", pdp); - pde_t *pd = (pde_t*)( (ulong)pdp[(virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512] ); - DebugLog("pd\t: %p\n", pd); - pte_t *pt = (pte_t*)( (ulong)pd[(virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512] ); - DebugLog("pt\t: %p\n", pt); - - ulong index = ((ulong)virtualAddr / ((ulong)KPAGESIZE)) % 512; - - ulong *page = &(pt[index]); - DebugLog("page (with flags): %p\n", page); - KeSleep(6000); - + index = ((ulong)virtualAddr / ((ulong)KPAGESIZE)) % 512; + page = &(pt[index]); + DebugLog("page (with flags): %p\n", *page); return page; } @@ -250,10 +273,10 @@ static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) //XXX MUST CREAT // void *MmTransVirtToPhyAddr(void* virtualAddr) { - ulong virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); + ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - if (*page == (*page & ~((KPAGESIZE - 1) | NX))) { + if (!(*page)) { return NULL; } @@ -299,9 +322,9 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags); + //DebugLog("Request %p:%p with %lu, at page %p\n", virtualAddr, physicalAddr, flags, page); - page[0] = (ulong)physicalAddr | flags; + *page = (ulong)physicalAddr | flags; MmPhysicalPageTable[(ulong)physicalAddr / ((ulong)KPAGESIZE) @@ -309,8 +332,10 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) KeFlushTlbSingle(*page); + //DebugLog("Done %p at page %p\n", *page, page); + if ((ulong)virtualAddr > MmVirtLastAddress) - MmVirtLastAddress = (ulong)virtualAddr; + MmVirtLastAddress = (ulong)virtualAddr + KPAGESIZE; } // diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index bd808a3..62d960d 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -157,13 +157,13 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, // Maximum PHYSICAL address in memory ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; - //DebugLog("Allocating %d pages...\n", *pageNumber); + ////DebugLog("Allocating %d pages...\n", *pageNumber); if (contiguous) { for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { if (!isPageBusy(curPage)) { (*frameListPtr)[curNumber] = curPage; inBlock = true; - //DebugLog("Select page : %p\n", curPage); + ////DebugLog("Select page : %p\n", curPage); if (++curNumber >= *pageNumber) { break; } @@ -178,7 +178,7 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) { if (!isPageBusy(curPage)) { (*frameListPtr)[curNumber] = curPage; - //DebugLog("Select page : %p\n", curPage); + ////DebugLog("Select page : %p\n", curPage); if (++curNumber >= *pageNumber) { break; } @@ -192,7 +192,7 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, for (size_t i = 0; i < *pageNumber; i++) { addPageToBusyList((*frameListPtr)[i], id); - //DebugLog("Allocating page : %p\n", *frameListPtr[i]); + ////DebugLog("Allocating page : %p\n", *frameListPtr[i]); } NSuccessfulAlloc++; @@ -240,14 +240,14 @@ error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags) while(busyPage->next) { busyPage = busyPage->next; - //DebugLog("Physical : %p is %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); + ////DebugLog("Physical : %p is %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); if (MmTransPhyToVirtAddr(busyPage->phyAddress)) { return EADDRINUSE; } if (id == busyPage->id) { - DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); + //DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); MmMapPage((void*)((ulong)virtAddr + offset), busyPage->phyAddress, flags); offset += KPAGESIZE; } @@ -265,10 +265,10 @@ error_t MmUnmapPageFrame(ulong id) busyPage = busyPage->next; actualPhys = MmTransPhyToVirtAddr(busyPage->phyAddress); - //DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys); + ////DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys); if (actualPhys && id == busyPage->id) { - //DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); + ////DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress)); } } @@ -292,11 +292,11 @@ error_t MmTestBusyPage(void) /* } else { */ /* MmFreePageFrame(tab[rand() % (j+1)]); */ /* } */ - /* DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */ + /* //DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */ /* } */ ulong a = KeGetTicks(); - DebugLog("Start alloc : %lu s\n", a/1000); + DebugLog("Start alloc 30 MB: %lu s\n", a/1000); tab[j++] = MmAllocPageFrame(30*MB, NORMAL); ulong b = KeGetTicks(); DebugLog("End alloc : %lu s\n", b/1000); @@ -304,8 +304,8 @@ error_t MmTestBusyPage(void) DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); a = KeGetTicks(); - DebugLog("Start alloc : %lu s\n", a/1000); - tab[j++] = MmAllocPageFrame(20*MB, NORMAL); + DebugLog("Start alloc 30MB : %lu s\n", a/1000); + tab[j++] = MmAllocPageFrame(5*MB, NORMAL); b = KeGetTicks(); DebugLog("End alloc : %lu s\n", b/1000); DebugLog("Alloc time : %lu s\n", (b-a)/1000); @@ -322,14 +322,14 @@ error_t MmTestBusyPage(void) DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); a = KeGetTicks(); - DebugLog("Start map : %lu ms\n", a); - MmMapPageFrame(tab[1], (void*)USERSPACE, PRESENT | USERMODE | READWRITE); + DebugLog("Start map at %p: %lu ms\n", USERSPACE, a); + MmMapPageFrame(tab[1], (void*)USERSPACE, PRESENT | READWRITE); b = KeGetTicks(); DebugLog("End map : %lu ms\n", b); DebugLog("Map time : %lu ms\n", (b-a)); //printBusyPages(); - DebugLog("Finished !\n"); + //DebugLog("Finished !\n"); return EOK; } From d69e028f5686c80de911ca295edd70a4479f876c Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Sun, 19 Jan 2020 18:54:33 +0100 Subject: [PATCH 22/27] [Bug] Problem with mapping --- boot/loader/mem/structures.inc | 2 +- kaleid/kernel/mm/paging.c | 75 ++++++++++++++++++++++++++++++---- kaleid/kernel/mm/palloc.c | 47 ++++++++++----------- 3 files changed, 93 insertions(+), 31 deletions(-) diff --git a/boot/loader/mem/structures.inc b/boot/loader/mem/structures.inc index 75d392e..e52bba6 100644 --- a/boot/loader/mem/structures.inc +++ b/boot/loader/mem/structures.inc @@ -31,7 +31,7 @@ global newStackEnd global GDT64 [section .text] -KERNEL_STACK equ 64 * 1024 ; 64KB of stack +KERNEL_STACK equ 16 * 1024 * 1024 ; 16MB of stack newKernelEnd dq 0x0 newStackEnd dq 0x0 diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index a146a94..e311206 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -226,13 +226,13 @@ static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) volatile ulong *page; volatile ulong index; - DebugLog("Get virtual descriptor %p\n", virtualAddr); + //DebugLog("Get virtual descriptor %p\n", virtualAddr); while (virtualAddr) { virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); index = (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512; pdp = (pdpe_t*)((ulong)MmPageMapLevel4[index] & ( ~(KPAGESIZE - 1)) ); - DebugLog("pdp at %p\t: %p\n", &pdp, pdp); + //DebugLog("pdp at %p\t: %p\n", &pdp, pdp); if (!pdp) { KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); MmPageMapLevel4[index] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE); @@ -242,7 +242,7 @@ static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) index = (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512; pd = (pde_t*)( (ulong)pdp[index] & ( ~(KPAGESIZE - 1)) ); - DebugLog("pd at %p\t: %p\n", &pd, pd); + //DebugLog("pd at %p\t: %p\n", &pd, pd); if (!pd) { KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); pdp[index] = (pde_t *)((ulong)pd | PRESENT | READWRITE); @@ -320,17 +320,78 @@ void MmUnsetPage(void* virtualAddr, ulong flags) // void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { - ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); + //DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags); - //DebugLog("Request %p:%p with %lu, at page %p\n", virtualAddr, physicalAddr, flags, page); + register ulong virtAddrPage; + volatile pdpe_t *pdp; + volatile pde_t *pd; + volatile pte_t *pt; - *page = (ulong)physicalAddr | flags; + virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); + + //DebugLog("Get virtual descriptor %p\n", virtualAddr); + while (virtAddrPage) { + + pdp = (pdpe_t*)((ulong)MmPageMapLevel4[ + (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512 + ] & ( ~(KPAGESIZE - 1)) ); + //DebugLog("pdp at %p\t: %p\n", &pdp, pdp); + + if (!pdp) { + KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); + + MmPageMapLevel4[ + (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512 + ] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE); + + //DebugLog("Created pdp\t: %p\n", pdp); + continue; + } + + pd = (pde_t*)( (ulong)pdp[ + (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512 + ] & ( ~(KPAGESIZE - 1)) ); + //DebugLog("pd at %p\t: %p\n", &pd, pd); + + if (!pd) { + KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); + + pdp[ + (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512 + ] = (pde_t *)((ulong)pd | PRESENT | READWRITE); + DebugLog("Created pd\t: %p\n", pd); + continue; + } + + pt = (pte_t*)( (ulong)pd[ + (virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512 + ] & ( ~(KPAGESIZE - 1)) ); + //DebugLog("pt at %p\t: %p\n", &pt, pt); + + if (!pt) { + KalAllocMemoryEx((void**)&pt, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); + pd[ + (virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512 + ] = (pte_t *)((ulong)pt | PRESENT | READWRITE); + DebugLog("Created pt\t: %p\n", pt); + continue; + } + break; + } + + pt[ + (virtAddrPage / (ulong)KPAGESIZE) % 512 + ] = (ulong)physicalAddr | flags; MmPhysicalPageTable[(ulong)physicalAddr / ((ulong)KPAGESIZE) ] = (ulong)virtualAddr; - KeFlushTlbSingle(*page); + KeFlushTlbSingle( + pt[ + (virtAddrPage / (ulong)KPAGESIZE) % 512 + ] = (ulong)physicalAddr | flags + ); //DebugLog("Done %p at page %p\n", *page, page); diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 62d960d..bb80886 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -205,6 +205,8 @@ ulong MmAllocPageFrame(size_t size, bool contiguous) void **ptr = NULL; ulong d = 0; return MmAllocPageFrameEx(&ptr, &d, size, contiguous); + (void)ptr; + (void)d; } @@ -235,21 +237,18 @@ void MmFreePageFrame(ulong id) error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags) { AllocatedPage_t *busyPage = &busyPagesList; - ulong offset = 0; while(busyPage->next) { busyPage = busyPage->next; - ////DebugLog("Physical : %p is %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress)); - if (MmTransPhyToVirtAddr(busyPage->phyAddress)) { return EADDRINUSE; } if (id == busyPage->id) { - //DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr + offset); - MmMapPage((void*)((ulong)virtAddr + offset), busyPage->phyAddress, flags); - offset += KPAGESIZE; + DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr); + MmMapPage((void*)((ulong)virtAddr), busyPage->phyAddress, flags); + virtAddr += KPAGESIZE; } } @@ -297,36 +296,38 @@ error_t MmTestBusyPage(void) ulong a = KeGetTicks(); DebugLog("Start alloc 30 MB: %lu s\n", a/1000); - tab[j++] = MmAllocPageFrame(30*MB, NORMAL); + tab[j++] = MmAllocPageFrame(5*MB, NORMAL); + tab[j++] = MmAllocPageFrame(8*KB, NORMAL); ulong b = KeGetTicks(); DebugLog("End alloc : %lu s\n", b/1000); DebugLog("Alloc time : %lu s\n", (b-a)/1000); DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); - a = KeGetTicks(); - DebugLog("Start alloc 30MB : %lu s\n", a/1000); - tab[j++] = MmAllocPageFrame(5*MB, NORMAL); - b = KeGetTicks(); - DebugLog("End alloc : %lu s\n", b/1000); - DebugLog("Alloc time : %lu s\n", (b-a)/1000); - DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); + /* a = KeGetTicks(); */ + /* DebugLog("Start alloc 30MB : %lu s\n", a/1000); */ + /* tab[j++] = MmAllocPageFrame(5*MB, NORMAL); */ + /* b = KeGetTicks(); */ + /* DebugLog("End alloc : %lu s\n", b/1000); */ + /* DebugLog("Alloc time : %lu s\n", (b-a)/1000); */ + /* DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */ - j = 0; + /* j = 0; */ - a = KeGetTicks(); - DebugLog("Start free : %lu ms\n", a); - MmFreePageFrame(tab[j++]); - b = KeGetTicks(); - DebugLog("End free : %lu ms\n", b); - DebugLog("Free time : %lu ms\n", (b-a)); - DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); + /* a = KeGetTicks(); */ + /* DebugLog("Start free : %lu ms\n", a); */ + /* MmFreePageFrame(tab[j++]); */ + /* b = KeGetTicks(); */ + /* DebugLog("End free : %lu ms\n", b); */ + /* DebugLog("Free time : %lu ms\n", (b-a)); */ + /* DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */ a = KeGetTicks(); DebugLog("Start map at %p: %lu ms\n", USERSPACE, a); - MmMapPageFrame(tab[1], (void*)USERSPACE, PRESENT | READWRITE); + MmMapPageFrame(tab[1], (void*)(USERSPACE), PRESENT | READWRITE); b = KeGetTicks(); DebugLog("End map : %lu ms\n", b); DebugLog("Map time : %lu ms\n", (b-a)); + //printBusyPages(); //DebugLog("Finished !\n"); From 8d839cd5868c9464b058a9cea6460c6cf3b76a66 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 20 Jan 2020 01:17:10 +0100 Subject: [PATCH 23/27] [Bug] Problem with mapping --- kaleid/kernel/mm/paging.c | 175 ++++++++++++-------------------------- 1 file changed, 54 insertions(+), 121 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index e311206..b70f146 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -217,55 +217,51 @@ void MmInitPaging(void) // // Get a page from an address // -static ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) +ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) { - volatile ulong virtAddrPage; - volatile pdpe_t *pdp; - volatile pde_t *pd; - volatile pte_t *pt; - volatile ulong *page; - volatile ulong index; + register ulong pml4Index = ((ulong)virtualAddr & 0xFF8000000000) >> 39; // Select bit from 39 to 48 + register ulong pdpIndex = ((ulong)virtualAddr & 0x7FC0000000) >> 30; // Select bit from 39 to 48 + register ulong pdIndex = ((ulong)virtualAddr & 0x3FE00000) >> 21; // Select bit from 39 to 48 + register ulong ptIndex = ((ulong)virtualAddr & 0x1FF000) >> 12; // Select bit from 39 to 48 + pdpe_t *pdp = NULL; + pde_t *pd = NULL; + pte_t *pt = NULL; - //DebugLog("Get virtual descriptor %p\n", virtualAddr); - while (virtualAddr) { - virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); + DebugLog("PML4[%d], PDP[%d], PD[%d], PT[%d]\n", pml4Index, pdpIndex, pdIndex, ptIndex); - index = (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512; - pdp = (pdpe_t*)((ulong)MmPageMapLevel4[index] & ( ~(KPAGESIZE - 1)) ); - //DebugLog("pdp at %p\t: %p\n", &pdp, pdp); - if (!pdp) { - KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); - MmPageMapLevel4[index] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE); - //DebugLog("Created pdp\t: %p\n", pdp); - continue; - } - - index = (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512; - pd = (pde_t*)( (ulong)pdp[index] & ( ~(KPAGESIZE - 1)) ); - //DebugLog("pd at %p\t: %p\n", &pd, pd); - if (!pd) { - KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); - pdp[index] = (pde_t *)((ulong)pd | PRESENT | READWRITE); - //DebugLog("Created pd\t: %p\n", pd); - continue; - } - - index = (virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512; - pt = (pte_t*)( (ulong)pd[index] & ( ~(KPAGESIZE - 1)) ); - DebugLog("pt at %p\t: %p\n", &pt, pt); - if (!pt) { - KalAllocMemoryEx((void**)&pt, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); - pd[index] = (pte_t *)((ulong)pt | PRESENT | READWRITE); - //DebugLog("Created pt\t: %p\n", pt); - continue; - } - break; + if (!((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 + KalAllocMemoryEx((void**)&MmPageMapLevel4[pml4Index], 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); + pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000); + DebugLog("Create PDP\n"); + } else { + pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000); } - index = ((ulong)virtualAddr / ((ulong)KPAGESIZE)) % 512; - page = &(pt[index]); - DebugLog("page (with flags): %p\n", *page); - return page; + DebugLog("PDP at %p = %p\n", &pdp[0], pdp[pdpIndex]); + + if (!((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 + KalAllocMemoryEx((void**)&pdp[pdpIndex], 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); + pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000); + DebugLog("Create PD\n"); + } else { + pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000); + } + + DebugLog("PD at %p = %p\n", &pd[0], pd[pdIndex]); + + if (!((ulong)pd[pdIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 + KalAllocMemoryEx((void**)&pd[pdIndex], 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); + pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000); + DebugLog("Create PT\n"); + } else { + pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000); + } + + DebugLog("PT at %p = %p\n", &pt[0], pt[ptIndex]); + + MmLoadPML4((void *)MmPageMapLevel4); + + return &pt[ptIndex]; } // @@ -276,11 +272,11 @@ void *MmTransVirtToPhyAddr(void* virtualAddr) ulong virtAddrPage = (ulong)virtualAddr & ( ~(KPAGESIZE - 1)); ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - if (!(*page)) { + if (!(page)) { return NULL; } - return (void*)((*page & ~((KPAGESIZE - 1) | NX))+ ((ulong)virtualAddr - (ulong)virtAddrPage)); + return (void*)(((ulong)*page & 0xFFFFFFFFFF000)+ ((ulong)virtualAddr - (ulong)virtAddrPage)); } void *MmTransPhyToVirtAddr(void* physicalAddr) @@ -298,8 +294,6 @@ void MmSetPage(void* virtualAddr, ulong flags) { ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - *page |= flags; - KeFlushTlbSingle(*page); } @@ -310,8 +304,6 @@ void MmUnsetPage(void* virtualAddr, ulong flags) { ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - *page &= (~flags); - KeFlushTlbSingle(*page); } @@ -322,76 +314,15 @@ void MmMapPage(void* virtualAddr, void* physicalAddr, ulong flags) { //DebugLog("Request %p:%p with %lu\n", virtualAddr, physicalAddr, flags); - register ulong virtAddrPage; - volatile pdpe_t *pdp; - volatile pde_t *pd; - volatile pte_t *pt; + ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - virtAddrPage = (ulong)virtualAddr & ( ~((KPAGESIZE - 1) | NX)); - - //DebugLog("Get virtual descriptor %p\n", virtualAddr); - while (virtAddrPage) { - - pdp = (pdpe_t*)((ulong)MmPageMapLevel4[ - (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512 - ] & ( ~(KPAGESIZE - 1)) ); - //DebugLog("pdp at %p\t: %p\n", &pdp, pdp); - - if (!pdp) { - KalAllocMemoryEx((void**)&pdp, 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); - - MmPageMapLevel4[ - (virtAddrPage / ((ulong)KPAGESIZE * 0x8000000)) % 512 - ] = (pdpe_t *)((ulong)pdp | PRESENT | READWRITE); - - //DebugLog("Created pdp\t: %p\n", pdp); - continue; - } - - pd = (pde_t*)( (ulong)pdp[ - (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512 - ] & ( ~(KPAGESIZE - 1)) ); - //DebugLog("pd at %p\t: %p\n", &pd, pd); - - if (!pd) { - KalAllocMemoryEx((void**)&pd, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); - - pdp[ - (virtAddrPage / ((ulong)KPAGESIZE * 0x40000)) % 512 - ] = (pde_t *)((ulong)pd | PRESENT | READWRITE); - DebugLog("Created pd\t: %p\n", pd); - continue; - } - - pt = (pte_t*)( (ulong)pd[ - (virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512 - ] & ( ~(KPAGESIZE - 1)) ); - //DebugLog("pt at %p\t: %p\n", &pt, pt); - - if (!pt) { - KalAllocMemoryEx((void**)&pt, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); - pd[ - (virtAddrPage / ((ulong)KPAGESIZE * 0x200)) % 512 - ] = (pte_t *)((ulong)pt | PRESENT | READWRITE); - DebugLog("Created pt\t: %p\n", pt); - continue; - } - break; - } - - pt[ - (virtAddrPage / (ulong)KPAGESIZE) % 512 - ] = (ulong)physicalAddr | flags; + *page = (ulong)physicalAddr | flags; MmPhysicalPageTable[(ulong)physicalAddr - / ((ulong)KPAGESIZE) - ] = (ulong)virtualAddr; + / ((ulong)KPAGESIZE) + ] = (ulong)virtualAddr; - KeFlushTlbSingle( - pt[ - (virtAddrPage / (ulong)KPAGESIZE) % 512 - ] = (ulong)physicalAddr | flags - ); + KeFlushTlbSingle(virtualAddr); //DebugLog("Done %p at page %p\n", *page, page); @@ -406,13 +337,15 @@ void MmUnmapPage(void* virtualAddr) { ulong *page = MmGetPageDescriptorFromVirtual(virtualAddr); - MmPhysicalPageTable[(ulong)(MmTransVirtToPhyAddr(virtualAddr)) - / ((ulong)KPAGESIZE) - ] = 0; + /* MmPhysicalPageTable[(ulong)(MmTransVirtToPhyAddr(virtualAddr)) */ + /* / ((ulong)KPAGESIZE) */ + /* ] = 0; */ - *page = 0; + /* pt[ */ + /* (virtualAddr / (ulong)KPAGESIZE) % 512 */ + /* ] = 0; */ - KeFlushTlbSingle(*page); + KeFlushTlbSingle(virtualAddr); } //----------- From a8035052a9ee3f8541cee5e7b8865a3aa43006f7 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 20 Jan 2020 19:09:42 +0100 Subject: [PATCH 24/27] [Bug] Problem with mapping --- kaleid/kernel/mm/paging.c | 29 +++++++++++++++++++++++------ kaleid/kernel/mm/palloc.c | 2 +- 2 files changed, 24 insertions(+), 7 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index b70f146..16d574d 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -230,34 +230,51 @@ ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) DebugLog("PML4[%d], PDP[%d], PD[%d], PT[%d]\n", pml4Index, pdpIndex, pdIndex, ptIndex); if (!((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 + + // Alloc space KalAllocMemoryEx((void**)&MmPageMapLevel4[pml4Index], 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); + + // Set present + MmPageMapLevel4[pml4Index] = (pml4_t)((ulong)MmPageMapLevel4[pml4Index] | PRESENT | READWRITE); + pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000); - DebugLog("Create PDP\n"); + DebugLog("\tCreate PDP at %p\n", MmPageMapLevel4[pml4Index]); + } else { pdp = (pdpe_t *)((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000); } - DebugLog("PDP at %p = %p\n", &pdp[0], pdp[pdpIndex]); + DebugLog("\tPDP[%d] = %p\n", pdpIndex, pdp[pdpIndex]); if (!((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 + KalAllocMemoryEx((void**)&pdp[pdpIndex], 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); + + pdp[pdpIndex] = (pdpe_t)((ulong)pdp[pdpIndex] | PRESENT | READWRITE); + pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000); - DebugLog("Create PD\n"); + DebugLog("\tCreate PD at %p\n", (ulong)pdp[pdpIndex]); + } else { pd = (pde_t *)((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000); } - DebugLog("PD at %p = %p\n", &pd[0], pd[pdIndex]); + DebugLog("\tPD[%d] = %p\n", pdIndex, pd[pdIndex]); if (!((ulong)pd[pdIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 + // KalAllocMemoryEx((void**)&pd[pdIndex], 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); + + pd[pdIndex] = (pde_t)((ulong)pd[pdIndex] | PRESENT | READWRITE); + pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000); - DebugLog("Create PT\n"); + DebugLog("\tCreate PT at %p\n", (ulong)pd[pdIndex]); + } else { pt = (pte_t *)((ulong)pd[pdIndex] & 0xFFFFFFFFFF000); } - DebugLog("PT at %p = %p\n", &pt[0], pt[ptIndex]); + DebugLog("\tPT[%d] = %p\n", ptIndex, pt[ptIndex]); MmLoadPML4((void *)MmPageMapLevel4); diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index bb80886..03e79be 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -297,7 +297,7 @@ error_t MmTestBusyPage(void) ulong a = KeGetTicks(); DebugLog("Start alloc 30 MB: %lu s\n", a/1000); tab[j++] = MmAllocPageFrame(5*MB, NORMAL); - tab[j++] = MmAllocPageFrame(8*KB, NORMAL); + tab[j++] = MmAllocPageFrame(513*4*KB, NORMAL); ulong b = KeGetTicks(); DebugLog("End alloc : %lu s\n", b/1000); DebugLog("Alloc time : %lu s\n", (b-a)/1000); From 3bba87c2278bb4918736c1519632533310f03bf7 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 20 Jan 2020 19:17:05 +0100 Subject: [PATCH 25/27] up --- kaleid/kernel/mm/malloc.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/kaleid/kernel/mm/malloc.c b/kaleid/kernel/mm/malloc.c index cfd25e2..f513660 100644 --- a/kaleid/kernel/mm/malloc.c +++ b/kaleid/kernel/mm/malloc.c @@ -59,7 +59,7 @@ error_t KalAllocMemoryEx(void **ptr, size_t req, int flags, size_t align) KeStartPanic("KalAllocMemory: Out of memory"); } - *ptr = (void *)brk; + *ptr = (void *)_ALIGN_UP(brk, align); if (flags & M_ZEROED) { memzero(*ptr, req); From 5b69c0a1e90e9ad46b3c4f84176865c9a77717c5 Mon Sep 17 00:00:00 2001 From: Julian Barathieu Date: Mon, 20 Jan 2020 19:21:07 +0100 Subject: [PATCH 26/27] up --- include/libc.h | 1 + kaleid/libc/mem.c | 15 +++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/include/libc.h b/include/libc.h index bf96d63..21a52e2 100644 --- a/include/libc.h +++ b/include/libc.h @@ -168,6 +168,7 @@ unsigned long strtoul(const char *restrict, char **restrict, int); //----------------------------------------------------------------------------// void *calloc(size_t, size_t) __attribute__((__malloc__)); +void *memalign(size_t n, size_t align) __attribute__((__malloc__)); void *malloc(size_t) __attribute__((__malloc__)); void free(void *); diff --git a/kaleid/libc/mem.c b/kaleid/libc/mem.c index 7107bd0..46772d2 100644 --- a/kaleid/libc/mem.c +++ b/kaleid/libc/mem.c @@ -46,6 +46,21 @@ void *malloc(size_t n) return ptr; } +void *memalign(size_t n, size_t align) +{ + void *ptr; + error_t rc; + +#ifndef _KALEID_KERNEL + rc = KalAllocMemoryEx(&ptr, n, 0, align); +#else + rc = KalAllocMemoryEx(&ptr, n, M_ZEROED, align); +#endif + if (rc > 0) seterrno(rc); + + return ptr; +} + void *calloc(size_t n, size_t m) { void *ptr; From deb2c0ed3b6cd2aba8ad5f811ca0e2ba41892220 Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 20 Jan 2020 19:38:27 +0100 Subject: [PATCH 27/27] paging and allocator functional, heap bug resolved #67 --- kaleid/kernel/mm/paging.c | 16 ++++++++-------- kaleid/kernel/mm/palloc.c | 2 +- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/kaleid/kernel/mm/paging.c b/kaleid/kernel/mm/paging.c index 16d574d..ad253d6 100644 --- a/kaleid/kernel/mm/paging.c +++ b/kaleid/kernel/mm/paging.c @@ -87,7 +87,7 @@ void MmInitPaging(void) // Alloc structures memzero((void *)&MmPageMapLevel4[0], 512*sizeof(ulong)); - KalAllocMemoryEx((void**)&MmPhysicalPageTable, phDirSize, M_ZEROED, KPAGESIZE); + MmPhysicalPageTable = memalign(phDirSize, KPAGESIZE); //DebugLog("\t\t\t\tPhysical map addr : %p\n", MmPhysicalPageTable); @@ -105,7 +105,7 @@ void MmInitPaging(void) continue; } - KalAllocMemoryEx((void**)&MmPDP, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); + MmPDP = memalign(512*sizeof(pde_t), KPAGESIZE); if (!firstDirectoryAddr) { firstDirectoryAddr = (ulong)MmPDP; @@ -128,7 +128,7 @@ void MmInitPaging(void) continue; } - KalAllocMemoryEx((void**)&MmPD, 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); + MmPD = memalign(512*sizeof(pde_t), KPAGESIZE); index = (curAddrPDP / ((ulong)KPAGESIZE * 0x40000)) % 512; @@ -149,7 +149,7 @@ void MmInitPaging(void) continue; } - KalAllocMemoryEx((void**)&MmPT, 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); + MmPT = memalign(512*sizeof(pte_t), KPAGESIZE); //DebugLog("\t\t\t\tPT %d : %p\n", index, MmPT); MmPD[index] = (pte_t *)((ulong)MmPT | PRESENT | READWRITE); @@ -232,7 +232,7 @@ ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) if (!((ulong)MmPageMapLevel4[pml4Index] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 // Alloc space - KalAllocMemoryEx((void**)&MmPageMapLevel4[pml4Index], 512*sizeof(pdpe_t), M_ZEROED, KPAGESIZE); + MmPageMapLevel4[pml4Index] = memalign(512*sizeof(pdpe_t), KPAGESIZE); // Set present MmPageMapLevel4[pml4Index] = (pml4_t)((ulong)MmPageMapLevel4[pml4Index] | PRESENT | READWRITE); @@ -248,7 +248,7 @@ ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) if (!((ulong)pdp[pdpIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 - KalAllocMemoryEx((void**)&pdp[pdpIndex], 512*sizeof(pde_t), M_ZEROED, KPAGESIZE); + pdp[pdpIndex] = memalign(512*sizeof(pde_t), KPAGESIZE); pdp[pdpIndex] = (pdpe_t)((ulong)pdp[pdpIndex] | PRESENT | READWRITE); @@ -262,8 +262,8 @@ ulong *MmGetPageDescriptorFromVirtual(void *virtualAddr) DebugLog("\tPD[%d] = %p\n", pdIndex, pd[pdIndex]); if (!((ulong)pd[pdIndex] & 0xFFFFFFFFFF000)) { // Select bit from 12 to 51 - // - KalAllocMemoryEx((void**)&pd[pdIndex], 512*sizeof(pte_t), M_ZEROED, KPAGESIZE); + + pd[pdIndex] = memalign(512*sizeof(pte_t), KPAGESIZE); pd[pdIndex] = (pde_t)((ulong)pd[pdIndex] | PRESENT | READWRITE); diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 03e79be..31fb17a 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -297,7 +297,7 @@ error_t MmTestBusyPage(void) ulong a = KeGetTicks(); DebugLog("Start alloc 30 MB: %lu s\n", a/1000); tab[j++] = MmAllocPageFrame(5*MB, NORMAL); - tab[j++] = MmAllocPageFrame(513*4*KB, NORMAL); + tab[j++] = MmAllocPageFrame(5*MB, NORMAL); ulong b = KeGetTicks(); DebugLog("End alloc : %lu s\n", b/1000); DebugLog("Alloc time : %lu s\n", (b-a)/1000);