luxos/Kernel/memory/mem-phys.c

109 lines
2.2 KiB
C
Raw Permalink Normal View History

/*
* 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;
uint32 TotalMemory;
2021-09-14 16:02:20 +00:00
uint32 UsedFrames;
2021-09-14 16:02:20 +00:00
inline void ConvertIndexToFrame (uint32 index, uint32* address, uint32* offset)
{
*address = (index >> 5);
*offset = index & 0x1f;
}
2021-09-14 16:02:20 +00:00
inline uint32 ConvertFrameToIndex (uint32 address, uint32 offset)
{
return (address<<5) | offset;
}
2021-09-14 16:02:20 +00:00
void MemPhSetFrame (uint32 frame, uint8 value)
{
uint32 addr, off;
2021-09-14 16:02:20 +00:00
ConvertIndexToFrame(frame, &addr, &off);
if (value) {
2021-09-14 16:02:20 +00:00
if ((FrameMap[addr] & (1<<off)) == 0) UsedFrames++;
FrameMap[addr] |= 1<<off;
}
else {
2021-09-14 16:02:20 +00:00
if (FrameMap[addr] & (1<<off)) UsedFrames--;
FrameMap[addr] &= ~(1<<off);
}
}
2021-09-14 16:02:20 +00:00
uint32 MemPhGetFrame (uint32 frame)
{
uint32 addr, off;
2021-09-14 16:02:20 +00:00
ConvertIndexToFrame(frame, &addr, &off);
2021-09-14 16:02:20 +00:00
return FrameMap[addr] & (1<<off);
}
2021-09-14 16:02:20 +00:00
uint32 MemPhFindFreeFrame()
{
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 16:02:20 +00:00
for (pos = 0; (FrameMap[addr] & (1<<pos)) != 0; pos++) ;
2021-09-14 16:02:20 +00:00
return ConvertFrameToIndex(addr, pos);
}
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 16:02:20 +00:00
uint32 frame = *page & PageFrame;
if (!frame) return;
2021-09-14 16:02:20 +00:00
MemPhSetFrame(frame, 0);
*page &= ~PageFrame;
}
void MemPhInitialize(uint32 SystemMemoryKb)
{
2021-09-14 16:02:20 +00:00
TotalFrames = SystemMemoryKb >> 2;
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)));
Log("Mem", "%#Started physical memory manager ok!, found %ukb\n", ColorLightGreen, SystemMemoryKb);
}
2021-09-14 16:02:20 +00:00
void MemPhReserveFrames (uint32 address, uint32 length)
{
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);
}