luxos/Kernel/memory/mem-phys.c
Tiberiu Chibici caa7718af9 [GOOD] BUILD 0.1.0.551 DATE 9/03/2011 AT 9:25 AM
====================================================
Mainly changed: HAL.FSs
+ Updated 'mount' call in floppy driver, now done after controller is
initialized
+ Added 'detect' function for FAT file systems
+ Implemented 'initdr' driver, however still bugged
+ Improved logger
2021-09-14 18:51:43 +03:00

109 lines
2.2 KiB
C

/*
* mem-phys.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
uint32* FrameMap;
uint32 TotalFrames;
uint32 TotalMemory;
uint32 UsedFrames;
inline void ConvertIndexToFrame (uint32 index, uint32* address, uint32* offset)
{
*address = (index >> 5);
*offset = index & 0x1f;
}
inline 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) {
if ((FrameMap[addr] & (1<<off)) == 0) UsedFrames++;
FrameMap[addr] |= 1<<off;
}
else {
if (FrameMap[addr] & (1<<off)) UsedFrames--;
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 0xffffffff;
}
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)
{
uint32 frame = *page & PageFrame;
if (!frame) return;
MemPhSetFrame(frame, 0);
*page &= ~PageFrame;
}
void MemPhInitialize(uint32 SystemMemoryKb)
{
TotalFrames = SystemMemoryKb >> 2;
TotalMemory = SystemMemoryKb;
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);
}
void MemPhReserveFrames (uint32 address, uint32 length)
{
address >>= 12;
length = (length>>12) + ((length & 0xfff) > 0);
uint32 end = address + length;
for (; address < end ; address++)
MemPhSetFrame(address, 1);
}