From 6a054a4b67106e42f1019a69900356aeb78864af Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Mon, 13 Jan 2020 16:12:30 +0100 Subject: [PATCH 1/2] 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 2/2] 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)