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

127 lines
4.2 KiB
C
Raw Normal View History

2020-01-12 17:21:12 +01:00
//----------------------------------------------------------------------------//
// 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>
//---------
2020-01-13 16:12:30 +01:00
enum
{
Whatever = 1UL << 52,
Whatever2 = 1UL << 62
};
2020-01-12 17:21:12 +01:00
2020-01-13 20:48:24 +01:00
static AllocatedPage_t busyPagesList = { (void*)0, 0, (AllocatedPage_t*)0 };
2020-01-12 17:21:12 +01:00
//---------
2020-01-13 16:12:30 +01:00
2020-01-14 00:37:30 +01:00
static bool isPageBusy(void *phyPageAddr) {
2020-01-13 20:48:24 +01:00
AllocatedPage_t *busyPage = &busyPagesList;
bool isBusy = false;
while(busyPage->next) {
2020-01-14 00:37:30 +01:00
busyPage = busyPage->next;
2020-01-13 20:48:24 +01:00
DebugLog("Busy page at %p\n", busyPage->phyAddress);
if (phyPageAddr == busyPage->phyAddress) {
isBusy = true;
break;
}
}
return isBusy;
}
2020-01-14 00:37:30 +01:00
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;
}
}
}
2020-01-13 16:12:30 +01:00
//
// Returns a structure that describes a pageframe
//
2020-01-13 20:48:24 +01:00
error_t MmGetFreePageFrame(void **framePtr, size_t *pageNumber, size_t size) {
*pageNumber = (ulong)size / KPAGESIZE;
2020-01-13 16:12:30 +01:00
2020-01-14 01:10:23 +01:00
2020-01-13 20:48:24 +01:00
return EOK;
}
2020-01-13 16:12:30 +01:00
2020-01-14 00:37:30 +01:00
error_t MmTestBusyPage(void) {
2020-01-13 16:12:30 +01:00
2020-01-14 00:37:30 +01:00
DebugLog("Adding pages \n");
addPageToBusyList((void*)0x123456789, 56);
addPageToBusyList((void*)0x555666666, 69);
addPageToBusyList((void*)0x454545454, 5);
2020-01-13 16:12:30 +01:00
2020-01-14 00:37:30 +01:00
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;
}
2020-01-13 16:12:30 +01:00