os-k/kaleid/kernel/mm/palloc.c

304 lines
8.7 KiB
C

//----------------------------------------------------------------------------//
// GNU GPL OS/K //
// //
// Desc: Page allocator related functions //
// //
// //
// Copyright © 2018-2019 The OS/K Team //
// //
// This file is part of OS/K. //
// //
// OS/K is free software: you can redistribute it and/or modify //
// it under the terms of the GNU General Public License as published by //
// the Free Software Foundation, either version 3 of the License, or //
// any later version. //
// //
// OS/K is distributed in the hope that it will be useful, //
// but WITHOUT ANY WARRANTY//without even the implied warranty of //
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the //
// GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with OS/K. If not, see <https://www.gnu.org/licenses/>. //
//----------------------------------------------------------------------------//
#include <kernel.h>
#include <init/boot.h>
#include <ex/malloc.h>
#include <mm/paging.h>
#include <mm/palloc.h>
#include <mm/map.h>
#include <io/vga.h>
#include <lib/buf.h>
#include <ke/time.h>
//---------
enum
{
Whatever = 1UL << 52,
Whatever2 = 1UL << 62
};
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)
{
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) {
isBusy = true;
break;
}
}
return isBusy;
}
static void printBusyPages(void)
{
AllocatedPage_t *busyPage = &busyPagesList;
if (!busyPage->next) {
KernLog("No busy page\n");
} else {
while(busyPage->next) {
busyPage = busyPage->next;
KernLog("Busy page at %p\n", busyPage->phyAddress);
}
}
}
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;
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;
}
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 an id to identify a page frame allocated (kernel)
//
ulong MmAllocPageFrameEx(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;
// 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 + 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;
}
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++;
}
//
// 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;
}
ulong tab[4000] = {0};
error_t MmTestBusyPage(void)
{
int j = 0;
/* 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();
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 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;
}