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

127 lines
4.2 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 <io/vga.h>
//---------
enum
{
Whatever = 1UL << 52,
Whatever2 = 1UL << 62
};
static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 };
//---------
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) {
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
//
error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) {
*pageNumber = (ulong)size / KPAGESIZE;
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;
}