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-18 00:48:05 +01:00
|
|
|
#include <lib/buf.h>
|
2020-01-16 00:08:04 +01:00
|
|
|
#include <ke/time.h>
|
2020-01-12 17:21:12 +01:00
|
|
|
|
|
|
|
//---------
|
|
|
|
|
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-16 00:08:04 +01:00
|
|
|
static ulong NSuccessfulAlloc = 0;
|
|
|
|
static ulong NSuccessfulFree = 0;
|
|
|
|
|
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;
|
|
|
|
|
2020-01-15 01:34:46 +01:00
|
|
|
// 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
|
2020-01-13 20:48:24 +01:00
|
|
|
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;
|
2020-01-15 01:34:46 +01:00
|
|
|
|
|
|
|
if (!busyPage->next) {
|
|
|
|
KernLog("No busy page\n");
|
|
|
|
} else {
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
KernLog("Busy page at %p\n", busyPage->phyAddress);
|
|
|
|
}
|
2020-01-14 16:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:48:05 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
static void addPageToBusyList(void *phyPageAddr, ulong id)
|
|
|
|
{
|
2020-01-14 00:37:30 +01:00
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
2020-01-15 16:12:13 +01:00
|
|
|
AllocatedPage_t *prevBusyPage = NULL;
|
2020-01-14 00:37:30 +01:00
|
|
|
|
|
|
|
while(busyPage->next) {
|
2020-01-15 16:12:13 +01:00
|
|
|
prevBusyPage = busyPage;
|
2020-01-14 00:37:30 +01:00
|
|
|
busyPage = busyPage->next;
|
2020-01-15 16:12:13 +01:00
|
|
|
|
|
|
|
if (busyPage->phyAddress > phyPageAddr) {
|
|
|
|
busyPage = prevBusyPage;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-14 00:37:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
AllocatedPage_t *newBusyPage = (AllocatedPage_t*)malloc(sizeof(AllocatedPage_t));
|
|
|
|
newBusyPage->phyAddress = phyPageAddr;
|
|
|
|
newBusyPage->id = id;
|
2020-01-15 16:12:13 +01:00
|
|
|
newBusyPage->next = busyPage->next;
|
2020-01-14 00:37:30 +01:00
|
|
|
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:58:38 +01:00
|
|
|
// Returns an id to identify a page frame allocated (kernel)
|
2020-01-13 16:12:30 +01:00
|
|
|
//
|
2020-01-16 00:08:04 +01:00
|
|
|
ulong MmAllocPageFrameEx(void ***frameListPtr, size_t *pageNumber, size_t size, bool contiguous)
|
2020-01-14 16:54:47 +01:00
|
|
|
{
|
|
|
|
static ulong id = 0;
|
|
|
|
*pageNumber = (((ulong)size - 1) / KPAGESIZE) + 1;
|
|
|
|
|
2020-01-15 01:34:46 +01:00
|
|
|
*frameListPtr = (void**)malloc(sizeof(void*)*(*pageNumber));
|
2020-01-14 16:54:47 +01:00
|
|
|
size_t curNumber = 0;
|
|
|
|
bool inBlock = false;
|
|
|
|
|
|
|
|
// Incrementing id
|
|
|
|
id++;
|
|
|
|
|
|
|
|
// Maximum PHYSICAL address in memory
|
|
|
|
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
|
|
|
|
2020-01-19 01:44:29 +01:00
|
|
|
////DebugLog("Allocating %d pages...\n", *pageNumber);
|
2020-01-18 12:50:36 +01:00
|
|
|
if (contiguous) {
|
|
|
|
for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) {
|
|
|
|
if (!isPageBusy(curPage)) {
|
|
|
|
(*frameListPtr)[curNumber] = curPage;
|
|
|
|
inBlock = true;
|
2020-01-19 01:44:29 +01:00
|
|
|
////DebugLog("Select page : %p\n", curPage);
|
2020-01-18 12:50:36 +01:00
|
|
|
if (++curNumber >= *pageNumber) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inBlock = false;
|
|
|
|
}
|
|
|
|
if (contiguous)
|
|
|
|
if (!inBlock)
|
|
|
|
curNumber = 0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
for (void *curPage = (void*)(MmPhysLastKernAddress + KPAGESIZE); curPage < (void*)phRamSize; curPage += KPAGESIZE) {
|
|
|
|
if (!isPageBusy(curPage)) {
|
|
|
|
(*frameListPtr)[curNumber] = curPage;
|
2020-01-19 01:44:29 +01:00
|
|
|
////DebugLog("Select page : %p\n", curPage);
|
2020-01-18 12:50:36 +01:00
|
|
|
if (++curNumber >= *pageNumber) {
|
|
|
|
break;
|
|
|
|
}
|
2020-01-14 16:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-16 00:08:04 +01:00
|
|
|
if (curNumber != *pageNumber) {
|
|
|
|
KeStartPanic("MmAllocPageFrameEx() : No more free pages to allocate");
|
|
|
|
}
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
for (size_t i = 0; i < *pageNumber; i++) {
|
2020-01-15 01:34:46 +01:00
|
|
|
addPageToBusyList((*frameListPtr)[i], id);
|
2020-01-19 01:44:29 +01:00
|
|
|
////DebugLog("Allocating page : %p\n", *frameListPtr[i]);
|
2020-01-14 16:54:47 +01:00
|
|
|
}
|
2020-01-14 01:10:23 +01:00
|
|
|
|
2020-01-16 00:08:04 +01:00
|
|
|
NSuccessfulAlloc++;
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
return id;
|
|
|
|
}
|
2020-01-14 01:10:23 +01:00
|
|
|
|
2020-01-16 00:08:04 +01:00
|
|
|
ulong MmAllocPageFrame(size_t size, bool contiguous)
|
|
|
|
{
|
|
|
|
void **ptr = NULL;
|
|
|
|
ulong d = 0;
|
|
|
|
return MmAllocPageFrameEx(&ptr, &d, size, contiguous);
|
2020-01-19 18:54:33 +01:00
|
|
|
(void)ptr;
|
|
|
|
(void)d;
|
2020-01-16 00:08:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-01-14 17:07:23 +01:00
|
|
|
//
|
|
|
|
// Frees a page frame by its id
|
|
|
|
//
|
2020-01-14 16:58:38 +01:00
|
|
|
void MmFreePageFrame(ulong id)
|
2020-01-14 16:54:47 +01:00
|
|
|
{
|
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
2020-01-16 00:08:04 +01:00
|
|
|
bool success = false;
|
2020-01-14 16:54:47 +01:00
|
|
|
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
|
|
|
|
if (id == busyPage->id) {
|
|
|
|
removePageFromBusyList(busyPage->phyAddress);
|
2020-01-16 00:08:04 +01:00
|
|
|
success = true;
|
2020-01-14 16:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
2020-01-16 00:08:04 +01:00
|
|
|
|
|
|
|
if (success)
|
|
|
|
NSuccessfulFree++;
|
2020-01-13 20:48:24 +01:00
|
|
|
}
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-15 16:12:13 +01:00
|
|
|
//
|
|
|
|
// Maps an allocated page frame to the given address
|
|
|
|
//
|
|
|
|
error_t MmMapPageFrame(ulong id, void *virtAddr, ulong flags)
|
|
|
|
{
|
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
|
|
|
|
if (MmTransPhyToVirtAddr(busyPage->phyAddress)) {
|
|
|
|
return EADDRINUSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (id == busyPage->id) {
|
2020-01-19 18:54:33 +01:00
|
|
|
DebugLog("Map %p at %p\n", busyPage->phyAddress, virtAddr);
|
|
|
|
MmMapPage((void*)((ulong)virtAddr), busyPage->phyAddress, flags);
|
|
|
|
virtAddr += KPAGESIZE;
|
2020-01-15 16:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_t MmUnmapPageFrame(ulong id)
|
|
|
|
{
|
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
void *actualPhys = 0;
|
|
|
|
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
actualPhys = MmTransPhyToVirtAddr(busyPage->phyAddress);
|
|
|
|
|
2020-01-19 01:44:29 +01:00
|
|
|
////DebugLog("Physical : %p is %p\n", busyPage->phyAddress, actualPhys);
|
2020-01-15 16:12:13 +01:00
|
|
|
|
|
|
|
if (actualPhys && id == busyPage->id) {
|
2020-01-19 01:44:29 +01:00
|
|
|
////DebugLog("Unmap %p from %p\n", busyPage->phyAddress, MmTransPhyToVirtAddr(busyPage->phyAddress));
|
2020-01-15 16:12:13 +01:00
|
|
|
MmUnmapPage(MmTransPhyToVirtAddr(busyPage->phyAddress));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
2020-01-18 00:48:05 +01:00
|
|
|
ulong tab[4000] = {0};
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
error_t MmTestBusyPage(void)
|
|
|
|
{
|
2020-01-18 00:48:05 +01:00
|
|
|
int j = 0;
|
2020-01-17 14:09:45 +01:00
|
|
|
|
|
|
|
/* for (int i = 0; i < 2000; i++) { */
|
|
|
|
/* if (rand() %2) { */
|
|
|
|
/* if (rand() %2) { */
|
2020-01-18 00:48:05 +01:00
|
|
|
/* tab[j++] = MmAllocPageFrame(rand()%6553689, NORMAL); */
|
2020-01-17 14:09:45 +01:00
|
|
|
/* } else { */
|
2020-01-18 00:48:05 +01:00
|
|
|
/* tab[j++] = MmAllocPageFrame(rand()%6553689, CONTIGUOUS); */
|
2020-01-17 14:09:45 +01:00
|
|
|
/* } */
|
|
|
|
/* } else { */
|
|
|
|
/* MmFreePageFrame(tab[rand() % (j+1)]); */
|
|
|
|
/* } */
|
2020-01-19 01:44:29 +01:00
|
|
|
/* //DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */
|
2020-01-17 14:09:45 +01:00
|
|
|
/* } */
|
|
|
|
|
2020-01-18 00:48:05 +01:00
|
|
|
ulong a = KeGetTicks();
|
2020-01-19 01:44:29 +01:00
|
|
|
DebugLog("Start alloc 30 MB: %lu s\n", a/1000);
|
2020-01-19 18:54:33 +01:00
|
|
|
tab[j++] = MmAllocPageFrame(5*MB, NORMAL);
|
2020-01-20 19:38:27 +01:00
|
|
|
tab[j++] = MmAllocPageFrame(5*MB, NORMAL);
|
2020-01-18 00:48:05 +01:00
|
|
|
ulong b = KeGetTicks();
|
2020-01-18 12:50:36 +01:00
|
|
|
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);
|
2020-01-18 00:48:05 +01:00
|
|
|
|
2020-01-19 18:54:33 +01:00
|
|
|
/* a = KeGetTicks(); */
|
|
|
|
/* DebugLog("Start alloc 30MB : %lu s\n", a/1000); */
|
|
|
|
/* tab[j++] = MmAllocPageFrame(5*MB, NORMAL); */
|
|
|
|
/* 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); */
|
2020-01-18 00:48:05 +01:00
|
|
|
|
2020-01-19 18:54:33 +01:00
|
|
|
/* j = 0; */
|
2020-01-18 12:50:36 +01:00
|
|
|
|
2020-01-19 18:54:33 +01:00
|
|
|
/* a = KeGetTicks(); */
|
|
|
|
/* DebugLog("Start free : %lu ms\n", a); */
|
|
|
|
/* MmFreePageFrame(tab[j++]); */
|
|
|
|
/* b = KeGetTicks(); */
|
|
|
|
/* DebugLog("End free : %lu ms\n", b); */
|
|
|
|
/* DebugLog("Free time : %lu ms\n", (b-a)); */
|
|
|
|
/* DebugLog("Alloc : %d; Free : %d; Count : %lu Mo\n", NSuccessfulAlloc, NSuccessfulFree, MmBusyPagesSpace() / MB); */
|
2020-01-15 01:34:46 +01:00
|
|
|
|
2020-01-18 12:50:36 +01:00
|
|
|
a = KeGetTicks();
|
2020-01-19 01:44:29 +01:00
|
|
|
DebugLog("Start map at %p: %lu ms\n", USERSPACE, a);
|
2020-01-19 18:54:33 +01:00
|
|
|
MmMapPageFrame(tab[1], (void*)(USERSPACE), PRESENT | READWRITE);
|
2020-01-18 12:50:36 +01:00
|
|
|
b = KeGetTicks();
|
|
|
|
DebugLog("End map : %lu ms\n", b);
|
|
|
|
DebugLog("Map time : %lu ms\n", (b-a));
|
2020-01-19 18:54:33 +01:00
|
|
|
|
2020-01-18 12:50:36 +01:00
|
|
|
//printBusyPages();
|
|
|
|
|
2020-01-19 01:44:29 +01:00
|
|
|
//DebugLog("Finished !\n");
|
2020-01-15 16:12:13 +01:00
|
|
|
|
2020-01-14 00:37:30 +01:00
|
|
|
return EOK;
|
|
|
|
}
|