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>
|
2020-01-14 16:54:47 +01:00
|
|
|
#include <mm/map.h>
|
2020-01-12 17:21:12 +01:00
|
|
|
#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-14 16:54:47 +01:00
|
|
|
extern MemoryMap_t memoryMap;
|
|
|
|
extern ulong MmPhysLastKernAddress;
|
2020-01-12 17:21:12 +01:00
|
|
|
|
|
|
|
//---------
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-14 16:54:47 +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
|
|
|
if (phyPageAddr == busyPage->phyAddress) {
|
|
|
|
isBusy = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return isBusy;
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
static void printBusyPages(void)
|
|
|
|
{
|
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
KernLog("Busy page at %p\n", busyPage->phyAddress);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void addPageToBusyList(void *phyPageAddr, ulong id)
|
|
|
|
{
|
2020-01-14 00:37:30 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
static void removePageFromBusyList(void *phyPageAddr)
|
|
|
|
{
|
2020-01-14 00:37:30 +01:00
|
|
|
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
|
|
|
//
|
2020-01-14 16:54:47 +01:00
|
|
|
// Returns an id to identify a page frame allocated
|
2020-01-13 16:12:30 +01:00
|
|
|
//
|
2020-01-14 16:54:47 +01:00
|
|
|
ulong MmAllocKernelPageFrame(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; 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;
|
|
|
|
}
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
for (size_t i = 0; i < *pageNumber; i++) {
|
|
|
|
addPageToBusyList(frameListPtr[i], id);
|
|
|
|
//DebugLog("Allocating page : %p\n", frameListPtr[i]);
|
|
|
|
}
|
2020-01-14 01:10:23 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
return id;
|
|
|
|
}
|
2020-01-14 01:10:23 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
void MmFreeKernelPageFrame(ulong id)
|
|
|
|
{
|
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
|
|
|
|
if (id == busyPage->id) {
|
|
|
|
removePageFromBusyList(busyPage->phyAddress);
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 20:48:24 +01:00
|
|
|
}
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
error_t MmTestBusyPage(void)
|
|
|
|
{
|
|
|
|
DebugLog("\nBusy pages\n");
|
|
|
|
printBusyPages();
|
|
|
|
|
|
|
|
DebugLog("\nAlloc 6677 bytes\n");
|
|
|
|
void **ptr = NULL;
|
|
|
|
size_t n = 0;
|
|
|
|
ulong id1 = MmAllocKernelPageFrame (ptr, &n, 6677, false);
|
|
|
|
|
|
|
|
DebugLog("\nAlloc 9045 bytes\n");
|
|
|
|
void **ptr2 = NULL;
|
|
|
|
size_t n2 = 0;
|
|
|
|
ulong id2 = MmAllocKernelPageFrame (ptr2, &n2, 9045, false);
|
|
|
|
|
|
|
|
DebugLog("\nAlloc 1200 bytes\n");
|
|
|
|
void **ptr3 = NULL;
|
|
|
|
size_t n3 = 0;
|
|
|
|
ulong id3 = MmAllocKernelPageFrame (ptr3, &n3, 1200, false);
|
|
|
|
|
|
|
|
DebugLog("\nAlloc 4096 bytes\n");
|
|
|
|
void **ptr4 = NULL;
|
|
|
|
size_t n4 = 0;
|
|
|
|
ulong id4 = MmAllocKernelPageFrame (ptr3, &n3, 4096, false);
|
|
|
|
|
|
|
|
DebugLog("\nAlloc 4097 bytes\n");
|
|
|
|
void **ptr5 = NULL;
|
|
|
|
size_t n5 = 0;
|
|
|
|
ulong id5 = MmAllocKernelPageFrame (ptr3, &n3, 4097, false);
|
|
|
|
|
|
|
|
printBusyPages();
|
|
|
|
|
|
|
|
DebugLog("\nFree 6677 and 1200 bytes\n");
|
|
|
|
MmFreeKernelPageFrame(id1);
|
|
|
|
MmFreeKernelPageFrame(id3);
|
|
|
|
|
|
|
|
DebugLog("\nAlloc 10000 bytes\n");
|
|
|
|
void **ptr6 = NULL;
|
|
|
|
size_t n6 = 0;
|
|
|
|
ulong id6 = MmAllocKernelPageFrame (ptr3, &n3, 10000, false);
|
|
|
|
|
|
|
|
printBusyPages();
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
DebugLog("\nFree 10000 bytes\n");
|
|
|
|
MmFreeKernelPageFrame(id6);
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
printBusyPages();
|
2020-01-14 00:37:30 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
DebugLog("\nAlloc 10000 bytes contiguous\n");
|
|
|
|
void **ptr7 = NULL;
|
|
|
|
size_t n7 = 0;
|
|
|
|
ulong id7 = MmAllocKernelPageFrame (ptr3, &n3, 10000, true);
|
2020-01-14 00:37:30 +01:00
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
printBusyPages();
|
2020-01-14 00:37:30 +01:00
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
2020-01-13 16:12:30 +01:00
|
|
|
|