From eecabef0a0c64111ba2fc6ee9c63884b5aad909e Mon Sep 17 00:00:00 2001 From: Adrien Bourmault Date: Thu, 23 Jan 2020 17:38:51 +0100 Subject: [PATCH] musage.c update with allocator --- include/mm/palloc.h | 6 +++- kaleid/kernel/mm/palloc.c | 55 +++++-------------------------------- kaleid/kernel/sh/musage.c | 34 +++++++++++++++++++++++ kaleid/kernel/sh/testcmds.c | 51 ++++++++++++++++++++++++++++++++-- 4 files changed, 94 insertions(+), 52 deletions(-) diff --git a/include/mm/palloc.h b/include/mm/palloc.h index 518aea7..51fda80 100644 --- a/include/mm/palloc.h +++ b/include/mm/palloc.h @@ -47,7 +47,11 @@ ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, ulong MmAllocPageFrame(size_t size, bool contiguous); void MmFreePageFrame(ulong id); -error_t MmTestBusyPage(ulong size, ulong flags); +ulong MmGetBusyPageSize(void); +ulong MmGetTotalPageSize(void); + +error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags); +error_t MmUnmapPageFrame(ulong id); //----------------------------------------------------------------------------// diff --git a/kaleid/kernel/mm/palloc.c b/kaleid/kernel/mm/palloc.c index 9b98233..5d37d7b 100644 --- a/kaleid/kernel/mm/palloc.c +++ b/kaleid/kernel/mm/palloc.c @@ -87,7 +87,7 @@ static void printBusyPages(void) } } -static ulong MmBusyPagesSpace(void) +ulong MmGetBusyPageSize(void) { ulong c = 0; AllocatedPage_t *busyPage = &busyPagesList; @@ -103,6 +103,12 @@ static ulong MmBusyPagesSpace(void) return c; } +ulong MmGetTotalPageSize(void) +{ + // Maximum PHYSICAL address in memory + ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize; + return (phRamSize - (MmPhysLastKernAddress + KPAGESIZE)); +} static void addPageToBusyList(void *phyPageAddrBegin, void *phyPageAddrEnd, ulong id) { @@ -275,50 +281,3 @@ error_t MmUnmapPageFrame(ulong id) return EOK; } - -ulong tab[4000] = {0}; - -error_t MmTestBusyPage(ulong size, ulong flags) -{ - /* for (int i = 0; i < 2000; i++) { */ - /* if (rand() %2) { */ - /* if (rand() %2) { */ - /* tab[j++] = MmAllocPageFrame(rand()%6553689, NORMAL); */ - /* } else { */ - /* 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); */ - /* } */ - - ulong a = KeGetTicks(); - DebugLog("Start alloc 1: %lu s\n", a/1000); - tab[0] = MmAllocPageFrame(size*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); - - printBusyPages(); - - a = KeGetTicks(); - DebugLog("Start map at %p wit %p: %lu ms\n", USERSPACE, flags, a); - MmMapPageFrame(tab[0], (void*)(USERSPACE), flags); - b = KeGetTicks(); - DebugLog("End map : %lu ms\n", b); - DebugLog("Map time : %lu ms\n", (b-a)); - - a = KeGetTicks(); - DebugLog("Start unmap at %p wit %p: %lu ms\n", USERSPACE, flags, a); - MmUnmapPageFrame(tab[0]); - b = KeGetTicks(); - DebugLog("End map : %lu ms\n", b); - DebugLog("Map time : %lu ms\n", (b-a)); - - - DebugLog("Finished !\n"); - - return EOK; -} diff --git a/kaleid/kernel/sh/musage.c b/kaleid/kernel/sh/musage.c index 6de60af..47f51ef 100644 --- a/kaleid/kernel/sh/musage.c +++ b/kaleid/kernel/sh/musage.c @@ -24,6 +24,8 @@ #include #include +#include +#include #include #include @@ -258,5 +260,37 @@ error_t CmdMemUsage(int argc, char **argv, char *cmdline) heap_max ); + KernLog("Kernel page allocator\n"); + + KernLog("\t%COccupied size:\t%C%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C (%d pages)\n", + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_MB(MmGetBusyPageSize()), + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_KB(MmGetBusyPageSize()), + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B(MmGetBusyPageSize()), + VGA_COLOR_DARK_GREY, + shcol, + MmGetBusyPageSize()/KPAGESIZE + ); + + KernLog("\t%CTotal size:\t%C%4lu%CMB%C + %4lu%CKB%C + %4lu%CB%C (%d pages)\n\n", + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_MB(MmGetTotalPageSize()), + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_KB(MmGetTotalPageSize()), + VGA_COLOR_DARK_GREY, + shcol, + _ADDR_TO_B(MmGetTotalPageSize()), + VGA_COLOR_DARK_GREY, + shcol, + MmGetTotalPageSize()/KPAGESIZE + ); + return EOK; } diff --git a/kaleid/kernel/sh/testcmds.c b/kaleid/kernel/sh/testcmds.c index c5bd3e0..08cf4a7 100644 --- a/kaleid/kernel/sh/testcmds.c +++ b/kaleid/kernel/sh/testcmds.c @@ -241,8 +241,51 @@ error_t CmdPageUnmap(int argc, char **argv, char *cmdline) error_t CmdPageAlloc(int argc, char **argv, char *cmdline) { - error_t err = MmTestBusyPage(strtoul(argv[1], NULL, 0), strtoul(argv[2], NULL, 0)); - return err; + if (argc != 4) { + KernLog("Invalid arguments !\n"); + return EINVAL; + } + + size_t size = (size_t)atol(argv[1]); + void *virtaddr = (void *)strtol(argv[2], NULL, 16); + ulong flags = (ulong)atol(argv[3]); + + KernLog("Allocating %d o...\n", size); + ulong id = MmAllocPageFrame(size, false); + KernLog("Allocated with id : %lu\n", id); + + KernLog("Mapping pages id %d at %p (flags %#x)...\n", id, virtaddr, flags); + error_t err = MmMapPageFrame(id, virtaddr, flags); + if (err == EOK) + KernLog("Successful mapped.\n"); + else + KernLog("Failed to map !\n"); + + return EOK; +} + +error_t CmdPageFree(int argc, char **argv, char *cmdline) +{ + if (argc != 2) { + KernLog("Invalid arguments !\n"); + return EINVAL; + } + + ulong id = (ulong)atol(argv[1]); + + KernLog("Unmapping pages id %d...\n", id); + error_t err = MmUnmapPageFrame(id); + if (err == EOK) + KernLog("Success unmaping.\n"); + else { + KernLog("Failed to unmap !\n"); + return EFAILED; + } + + KernLog("Freeing pages id %d...\n", id); + MmFreePageFrame(id); + + return EOK; } error_t CmdPF(int argc, char **argv, char *cmdline) @@ -311,7 +354,9 @@ static Command_t testcmdtable[] = " virtual address (paging)"}, { "pmap", CmdPageMap, "Map a page to given physical addr" }, { "punmap", CmdPageUnmap, "Unmap a page" }, - { "palloc", CmdPageAlloc, "Alloc x KB of pages" }, + { "palloc", CmdPageAlloc, "Alloc x B of pages at y address" + "with z flags"}, + { "pfree", CmdPageFree, "Free a page block of id x" }, { "pf", CmdPF, "Provoke a PF. Usage: pfault
"}, { "shell", CmdShell, "Start a new shell (nested)", }, { "stkov", CmdStackOverflow, "Provoke a stack overflow" },