103 lines
2.1 KiB
C
103 lines
2.1 KiB
C
|
/*
|
||
|
* mem-phys.c
|
||
|
*
|
||
|
* Created on: Aug 27, 2011
|
||
|
* Author: Tiberiu
|
||
|
*/
|
||
|
|
||
|
#include <stdlib.h>
|
||
|
#include "mem.h"
|
||
|
|
||
|
uint32* FrameMap;
|
||
|
uint32 TotalFrames;
|
||
|
uint32 TotalMemory;
|
||
|
|
||
|
void ConvertIndexToFrame (uint32 index, uint32* address, uint32* offset)
|
||
|
{
|
||
|
*address = (index >> 5);
|
||
|
*offset = index & 0x1f;
|
||
|
}
|
||
|
|
||
|
uint32 ConvertFrameToIndex (uint32 address, uint32 offset)
|
||
|
{
|
||
|
return (address<<5) | offset;
|
||
|
}
|
||
|
|
||
|
void MemPhSetFrame (uint32 frame, uint8 value)
|
||
|
{
|
||
|
uint32 addr, off;
|
||
|
ConvertIndexToFrame(frame, &addr, &off);
|
||
|
|
||
|
if (value) FrameMap[addr] |= 1<<off;
|
||
|
else FrameMap[addr] &= ~(1<<off);
|
||
|
}
|
||
|
|
||
|
uint32 MemPhGetFrame (uint32 frame)
|
||
|
{
|
||
|
uint32 addr, off;
|
||
|
ConvertIndexToFrame(frame, &addr, &off);
|
||
|
|
||
|
return FrameMap[addr] & (1<<off);
|
||
|
}
|
||
|
|
||
|
uint32 MemPhFindFreeFrame()
|
||
|
{
|
||
|
uint32 addr, pos;
|
||
|
|
||
|
for (addr = 0; addr < TotalFrames >> 5; addr++)
|
||
|
if (FrameMap[addr] != 0xffffffff)
|
||
|
{
|
||
|
for (pos = 0; (FrameMap[addr] & (1<<pos)) != 0; pos++) ;
|
||
|
|
||
|
return ConvertFrameToIndex(addr, pos);
|
||
|
}
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
|
||
|
void MemPhAllocFrame(uint8 isKernel, uint8 isWriteable)
|
||
|
{
|
||
|
//if (page->Frame != 0) return;
|
||
|
|
||
|
uint32 free = MemPhFindFreeFrame();
|
||
|
if (!free) return;
|
||
|
|
||
|
LogMem("Physical: allocated frame %u.\n", free);
|
||
|
|
||
|
MemPhSetFrame(free, 1);
|
||
|
/*page->Present = 1;
|
||
|
page->User = (isKernel) ? 0 : 1;
|
||
|
page->Writeable = (isWriteable) ? 1 : 0;
|
||
|
page->Frame = free;*/
|
||
|
}
|
||
|
|
||
|
/*void MemPhFreeFrame()//Page* page)
|
||
|
{
|
||
|
uint32 frame = page->Frame;
|
||
|
if (!frame) return;
|
||
|
|
||
|
MemPhSetFrame(frame, 0);
|
||
|
page->Frame = 0;
|
||
|
}*/
|
||
|
|
||
|
void MemPhInitialize(uint32 SystemMemoryKb)
|
||
|
{
|
||
|
LogMem("Starting physical memory manager...\n");
|
||
|
TotalFrames = SystemMemoryKb >> 2;
|
||
|
TotalMemory = SystemMemoryKb;
|
||
|
|
||
|
FrameMap = (uint32*) malloc(sizeof(uint32) * (1 + (TotalFrames>>5)));
|
||
|
memset(FrameMap, 0, sizeof(uint32) * (1 + (TotalFrames>>5)));
|
||
|
LogMem("Started ok!, found %ukb total_frames=%u\n", SystemMemoryKb, TotalFrames);
|
||
|
}
|
||
|
|
||
|
void MemPhReserveFrames (uint32 address, uint32 length)
|
||
|
{
|
||
|
address >>= 12;
|
||
|
length = (length>>12) + ((length & 0xfff) > 0);
|
||
|
uint32 end = address + length;
|
||
|
|
||
|
for (; address < end && address < TotalFrames ; address++)
|
||
|
MemPhSetFrame(address, 1);
|
||
|
}
|