2021-09-14 15:48:57 +00:00
|
|
|
/*
|
|
|
|
* mem-phys.c
|
|
|
|
*
|
|
|
|
* Created on: Aug 27, 2011
|
|
|
|
* Author: Tiberiu
|
|
|
|
*/
|
|
|
|
#include <memory-add.h>
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
uint32* FrameMap;
|
|
|
|
uint32 TotalFrames;
|
2021-09-14 15:48:57 +00:00
|
|
|
uint32 TotalMemory;
|
2021-09-14 16:02:20 +00:00
|
|
|
uint32 UsedFrames;
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
inline void ConvertIndexToFrame (uint32 index, uint32* address, uint32* offset)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
*address = (index >> 5);
|
|
|
|
*offset = index & 0x1f;
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
inline uint32 ConvertFrameToIndex (uint32 address, uint32 offset)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
return (address<<5) | offset;
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
void MemPhSetFrame (uint32 frame, uint8 value)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
uint32 addr, off;
|
2021-09-14 16:02:20 +00:00
|
|
|
ConvertIndexToFrame(frame, &addr, &off);
|
2021-09-14 15:48:57 +00:00
|
|
|
|
|
|
|
if (value) {
|
2021-09-14 16:02:20 +00:00
|
|
|
if ((FrameMap[addr] & (1<<off)) == 0) UsedFrames++;
|
|
|
|
FrameMap[addr] |= 1<<off;
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
else {
|
2021-09-14 16:02:20 +00:00
|
|
|
if (FrameMap[addr] & (1<<off)) UsedFrames--;
|
|
|
|
FrameMap[addr] &= ~(1<<off);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
uint32 MemPhGetFrame (uint32 frame)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
uint32 addr, off;
|
2021-09-14 16:02:20 +00:00
|
|
|
ConvertIndexToFrame(frame, &addr, &off);
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
return FrameMap[addr] & (1<<off);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
uint32 MemPhFindFreeFrame()
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
uint32 addr, pos;
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
for (addr = 0; addr < TotalFrames >> 5; addr++)
|
|
|
|
if (FrameMap[addr] != 0xffffffff)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
2021-09-14 16:02:20 +00:00
|
|
|
for (pos = 0; (FrameMap[addr] & (1<<pos)) != 0; pos++) ;
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
return ConvertFrameToIndex(addr, pos);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0xffffffff;
|
|
|
|
}
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
void MemPhAllocFrame(Page* page, uint8 isKernel, uint8 isWriteable)
|
|
|
|
{
|
|
|
|
if ((*page & PageFrame) != 0) return;
|
|
|
|
|
|
|
|
uint32 free = MemPhFindFreeFrame();
|
|
|
|
if (free == 0xffffffff) {
|
|
|
|
Panic("Mem", "%#Failed allocation free=0x%x page=0x%x\n", ColorRed, free, *page);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
MemPhSetFrame(free, 1);
|
|
|
|
*page |= PagePresent;
|
|
|
|
*page |= (isKernel) ? 0 : PageUser;
|
|
|
|
*page |= (isWriteable) ? PageWriteable : 0;
|
|
|
|
*page |= (free<<12) & PageFrame;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MemPhFreeFrame(Page* page)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
2021-09-14 16:02:20 +00:00
|
|
|
uint32 frame = *page & PageFrame;
|
|
|
|
if (!frame) return;
|
2021-09-14 15:48:57 +00:00
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
MemPhSetFrame(frame, 0);
|
|
|
|
*page &= ~PageFrame;
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void MemPhInitialize(uint32 SystemMemoryKb)
|
|
|
|
{
|
2021-09-14 16:02:20 +00:00
|
|
|
TotalFrames = SystemMemoryKb >> 2;
|
2021-09-14 15:48:57 +00:00
|
|
|
TotalMemory = SystemMemoryKb;
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
FrameMap = (uint32*) kmalloc(sizeof(uint32) * (1 + (TotalFrames>>5)));
|
|
|
|
memset(FrameMap, 0, sizeof(uint32) * (1 + (TotalFrames>>5)));
|
2021-09-14 15:51:43 +00:00
|
|
|
Log("Mem", "%#Started physical memory manager ok!, found %ukb\n", ColorLightGreen, SystemMemoryKb);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|
|
|
|
|
2021-09-14 16:02:20 +00:00
|
|
|
void MemPhReserveFrames (uint32 address, uint32 length)
|
2021-09-14 15:48:57 +00:00
|
|
|
{
|
|
|
|
address >>= 12;
|
|
|
|
length = (length>>12) + ((length & 0xfff) > 0);
|
|
|
|
uint32 end = address + length;
|
|
|
|
|
|
|
|
for (; address < end ; address++)
|
2021-09-14 16:02:20 +00:00
|
|
|
MemPhSetFrame(address, 1);
|
2021-09-14 15:48:57 +00:00
|
|
|
}
|