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