2020-01-12 17:21:12 +01:00
|
|
|
//----------------------------------------------------------------------------//
|
2020-09-27 17:33:48 +02:00
|
|
|
// OS on Kaleid //
|
2020-01-12 17:21:12 +01:00
|
|
|
// //
|
|
|
|
// Desc: Page allocator related functions //
|
|
|
|
// //
|
|
|
|
// //
|
2020-02-06 14:23:26 +01:00
|
|
|
// Copyright © 2018-2020 The OS/K Team //
|
2020-01-12 17:21:12 +01:00
|
|
|
// //
|
|
|
|
// 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-02-19 22:19:58 +01:00
|
|
|
#include <libbuf.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-02-12 17:51:14 +01:00
|
|
|
static AllocatedPage_t busyPagesList =
|
|
|
|
{ (void*)0, (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-21 01:08:28 +01:00
|
|
|
// Returns 0 if page is not busy, its last address if yes
|
|
|
|
static ulong isPageBusy(void *phyPageAddr)
|
2020-01-14 16:54:47 +01:00
|
|
|
{
|
2020-01-13 20:48:24 +01:00
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
2020-01-21 01:08:28 +01:00
|
|
|
ulong isBusy = 0;
|
2020-01-13 20:48:24 +01:00
|
|
|
|
2020-01-15 01:34:46 +01:00
|
|
|
// In case of NVS, ACPI or BADRAM zone, considered busy
|
2020-01-27 15:28:44 +01:00
|
|
|
if (MmIsBusyZone(phyPageAddr)) {
|
|
|
|
return 1;
|
|
|
|
}
|
2020-01-15 01:34:46 +01:00
|
|
|
|
|
|
|
// 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-21 01:08:28 +01:00
|
|
|
if (phyPageAddr >= busyPage->phyAddressBegin
|
|
|
|
&& phyPageAddr <= busyPage->phyAddressEnd
|
|
|
|
) {
|
|
|
|
isBusy = (ulong)busyPage->phyAddressEnd;
|
2020-01-13 20:48:24 +01:00
|
|
|
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;
|
2020-01-21 01:08:28 +01:00
|
|
|
KernLog("Busy page at %p-%p\n", busyPage->phyAddressBegin,
|
|
|
|
busyPage->phyAddressEnd);
|
2020-01-15 01:34:46 +01:00
|
|
|
}
|
2020-01-14 16:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:38:51 +01:00
|
|
|
ulong MmGetBusyPageSize(void)
|
2020-01-18 00:48:05 +01:00
|
|
|
{
|
|
|
|
ulong c = 0;
|
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
|
|
|
|
if (!busyPage->next) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
2020-01-21 01:08:28 +01:00
|
|
|
c += 4096 + (busyPage->phyAddressEnd - busyPage->phyAddressBegin);
|
2020-01-18 00:48:05 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return c;
|
|
|
|
}
|
|
|
|
|
2020-01-23 17:38:51 +01:00
|
|
|
ulong MmGetTotalPageSize(void)
|
|
|
|
{
|
|
|
|
// Maximum PHYSICAL address in memory
|
2020-01-27 15:28:44 +01:00
|
|
|
ulong phRamSize = memoryMap.freeRamSize;
|
|
|
|
return ((phRamSize & 0xFFFFFFFFFF000) -
|
|
|
|
((MmPhysLastKernAddress + KPAGESIZE) & 0xFFFFFFFFFF000));
|
2020-01-23 17:38:51 +01:00
|
|
|
}
|
2020-01-18 00:48:05 +01:00
|
|
|
|
2020-02-12 17:51:14 +01:00
|
|
|
static void addPageToBusyList(void *phyPageAddrBegin,
|
|
|
|
void *phyPageAddrEnd, ulong id)
|
2020-01-14 16:54:47 +01:00
|
|
|
{
|
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
|
|
|
|
2020-01-21 01:08:28 +01:00
|
|
|
if (busyPage->phyAddressBegin > phyPageAddrEnd) {
|
2020-01-15 16:12:13 +01:00
|
|
|
busyPage = prevBusyPage;
|
|
|
|
break;
|
|
|
|
}
|
2020-01-14 00:37:30 +01:00
|
|
|
}
|
|
|
|
|
2020-01-20 20:05:02 +01:00
|
|
|
AllocatedPage_t *newBusyPage =
|
|
|
|
(AllocatedPage_t*)malloc(sizeof(AllocatedPage_t));
|
2020-01-21 01:08:28 +01:00
|
|
|
newBusyPage->phyAddressBegin = phyPageAddrBegin;
|
|
|
|
newBusyPage->phyAddressEnd = phyPageAddrEnd;
|
2020-01-14 00:37:30 +01:00
|
|
|
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-21 01:08:28 +01:00
|
|
|
static void removePageFromBusyList(void *phyPageAddrBegin)
|
2020-01-14 16:54:47 +01:00
|
|
|
{
|
2020-01-14 00:37:30 +01:00
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
AllocatedPage_t *prevBusyPage = NULL;
|
|
|
|
|
|
|
|
while(busyPage->next) {
|
|
|
|
prevBusyPage = busyPage;
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
|
2020-01-21 01:08:28 +01:00
|
|
|
if (phyPageAddrBegin == busyPage->phyAddressBegin) {
|
2020-01-14 00:37:30 +01:00
|
|
|
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-21 01:08:28 +01:00
|
|
|
ulong MmAllocPageFrame(size_t size, bool contiguous)
|
2020-01-14 16:54:47 +01:00
|
|
|
{
|
2020-01-27 15:28:44 +01:00
|
|
|
assert(size);
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
static ulong id = 0;
|
2020-01-27 15:28:44 +01:00
|
|
|
ulong pageNumber = (((ulong)size) / KPAGESIZE) + 1;
|
2020-01-14 16:54:47 +01:00
|
|
|
|
2020-01-27 15:28:44 +01:00
|
|
|
ulong curNumber = 0;
|
2020-01-21 01:08:28 +01:00
|
|
|
|
|
|
|
void *futureBegin = 0;
|
|
|
|
void *futureEnd = 0;
|
|
|
|
|
|
|
|
ulong busyLastAddr = 0;
|
2020-01-14 16:54:47 +01:00
|
|
|
|
|
|
|
// Incrementing id
|
|
|
|
id++;
|
|
|
|
|
|
|
|
// Maximum PHYSICAL address in memory
|
|
|
|
ulong phRamSize = memoryMap.freeRamSize + memoryMap.nonfreeRamSize;
|
|
|
|
|
2020-01-27 15:28:44 +01:00
|
|
|
DebugLog("Allocating %d pages (%u o)...\n", pageNumber, size);
|
|
|
|
|
|
|
|
// Through the pages in physical memory...
|
2020-02-12 17:51:14 +01:00
|
|
|
for (void *curPage =
|
|
|
|
(void*)((MmPhysLastKernAddress + KPAGESIZE) & 0xFFFFFFFFFF000);
|
2020-01-27 15:28:44 +01:00
|
|
|
curPage < (void*)(phRamSize & 0xFFFFFFFFFF000); curPage += KPAGESIZE) {
|
2020-01-21 01:08:28 +01:00
|
|
|
busyLastAddr = isPageBusy(curPage);
|
|
|
|
|
|
|
|
if (!busyLastAddr) {
|
|
|
|
if (!futureBegin) {
|
|
|
|
futureBegin = curPage;
|
|
|
|
//DebugLog("Select begin : %p\n", curPage);
|
2020-01-18 12:50:36 +01:00
|
|
|
}
|
2020-01-21 01:08:28 +01:00
|
|
|
|
|
|
|
futureEnd = curPage;
|
|
|
|
//DebugLog("Select page : %p\n", curPage);
|
|
|
|
if (++curNumber >= pageNumber) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
if (!contiguous) {
|
|
|
|
if (futureBegin && futureEnd)
|
|
|
|
addPageToBusyList(futureBegin, futureEnd, id);
|
|
|
|
futureBegin = 0;
|
|
|
|
} else {
|
|
|
|
curNumber = 0;
|
|
|
|
futureBegin = 0;
|
2020-01-14 16:54:47 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-01-13 16:12:30 +01:00
|
|
|
|
2020-01-27 15:28:44 +01:00
|
|
|
if (futureBegin && futureEnd)
|
|
|
|
addPageToBusyList(futureBegin, futureEnd, id);
|
|
|
|
|
2020-01-21 01:08:28 +01:00
|
|
|
if (curNumber != pageNumber) {
|
2020-01-27 15:28:44 +01:00
|
|
|
return 0;
|
2020-01-16 00:08:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
NSuccessfulAlloc++;
|
|
|
|
|
2020-02-06 17:45:44 +01:00
|
|
|
DebugLog("Allocate page frame id %d, size %d MB, contiguousness %d\n",
|
|
|
|
id, size / MB, contiguous
|
|
|
|
);
|
|
|
|
|
2020-01-14 16:54:47 +01:00
|
|
|
return id;
|
|
|
|
}
|
2020-01-14 01:10:23 +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
|
|
|
{
|
2020-01-27 15:28:44 +01:00
|
|
|
assert(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) {
|
2020-01-21 01:08:28 +01:00
|
|
|
removePageFromBusyList(busyPage->phyAddressBegin);
|
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
|
|
|
|
2020-02-06 17:45:44 +01:00
|
|
|
if (success) {
|
2020-01-16 00:08:04 +01:00
|
|
|
NSuccessfulFree++;
|
2020-02-06 17:45:44 +01:00
|
|
|
DebugLog("Free page frame id %d\n",
|
|
|
|
id
|
|
|
|
);
|
|
|
|
}
|
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)
|
|
|
|
{
|
2020-01-27 15:28:44 +01:00
|
|
|
assert(id);
|
2020-01-15 16:12:13 +01:00
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
|
2020-02-10 19:46:20 +01:00
|
|
|
void *virtAddrBegin = virtAddr;
|
|
|
|
|
2020-01-15 16:12:13 +01:00
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
|
|
|
|
if (id == busyPage->id) {
|
2020-02-12 17:51:14 +01:00
|
|
|
for (void *addr = busyPage->phyAddressBegin;
|
|
|
|
addr <= busyPage->phyAddressEnd; addr += KPAGESIZE) {
|
2020-01-21 01:08:28 +01:00
|
|
|
if (MmTransPhyToVirtAddr(addr)) {
|
|
|
|
return EADDRINUSE;
|
|
|
|
}
|
|
|
|
//DebugLog("Map %p at %p\n", addr, virtAddr);
|
|
|
|
MmMapPage((void*)((ulong)virtAddr), (void*)addr, flags);
|
|
|
|
virtAddr += KPAGESIZE;
|
|
|
|
}
|
2020-01-15 16:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:46:20 +01:00
|
|
|
DebugLog("Page frame id %d mapped from %p to %p\n",
|
|
|
|
id, virtAddrBegin, virtAddr);
|
|
|
|
|
2020-01-15 16:12:13 +01:00
|
|
|
return EOK;
|
|
|
|
}
|
|
|
|
|
|
|
|
error_t MmUnmapPageFrame(ulong id)
|
|
|
|
{
|
2020-01-27 15:28:44 +01:00
|
|
|
assert(id);
|
2020-01-15 16:12:13 +01:00
|
|
|
AllocatedPage_t *busyPage = &busyPagesList;
|
|
|
|
void *actualPhys = 0;
|
|
|
|
|
|
|
|
while(busyPage->next) {
|
|
|
|
busyPage = busyPage->next;
|
|
|
|
|
2020-01-21 01:08:28 +01:00
|
|
|
if (id == busyPage->id) {
|
2020-02-12 17:51:14 +01:00
|
|
|
for (void *addr = busyPage->phyAddressBegin;
|
|
|
|
addr <= busyPage->phyAddressEnd; addr += KPAGESIZE) {
|
2020-01-21 01:08:28 +01:00
|
|
|
actualPhys = MmTransPhyToVirtAddr(addr);
|
|
|
|
//DebugLog("Map %p at %p\n", addr, virtAddr);
|
|
|
|
if (actualPhys && id == busyPage->id) {
|
|
|
|
MmUnmapPage(MmTransPhyToVirtAddr(addr));
|
|
|
|
}
|
|
|
|
}
|
2020-01-15 16:12:13 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-10 19:46:20 +01:00
|
|
|
DebugLog("Page frame id %d unmapped\n", id);
|
|
|
|
|
2020-01-15 16:12:13 +01:00
|
|
|
return EOK;
|
|
|
|
}
|