luxos/Kernel/library/memory/memory_init.c
2021-09-14 19:02:20 +03:00

89 lines
2.2 KiB
C

/*
* memory-init.c
*
* Created on: Aug 27, 2011
* Author: Tiberiu
*/
#include <memory-add.h>
#include <multiboot.h>
#include <debugio.h>
#include "../../drivers/cmos/cmos.h"
#include <stdio.h>
uint32 mem_kernel_end = 0;
uint8 mem_initialized = 0;
uint32 _memory_get_total_mem(MultibootInfo* info)
{
// Grub was nice enough to give us info
if (info->Flags & MultibootInfo_MEMORY) return (1024 + info->MemoryUpper);
// No? Get info from CMOS
uint8 low, high;
uint32 total;
low = CmosRead(0x30);
high = CmosRead(0x31);
total = (uint32)(low | high<<8) + 1024;
Error("Mem", "%#Missing memory info from bootloader. Reading from CMOS: %ukb\n", ColorLightRed, total);
return total;
}
void _memory_reserve_system(MultibootInfo* info)
{
MagicBreakpoint();
if ((info->Flags & MultibootInfo_MEM_MAP) != 0)
{
MultibootMemoryMapEntry* location = (MultibootMemoryMapEntry*)info->MemoryMapAddress;
while ((uint32)location < (info->MemoryMapAddress + info->MemoryMapLength))
{
if (location->Type > 1)
MemPhReserveFrames((uint32)location->Address, (uint32)location->Length);
location = (MultibootMemoryMapEntry*) ((uint32)location + location->Size + sizeof(uint32));
}
}
else
{
Error("Mem", "%#Missing %#memory map%# info from bootloader.\n", ColorLightRed, ColorWhite, ColorLightRed);
// Standard memory hole at 15mb
MemPhReserveFrames(0x00F00000, 0x00100000);
}
// Standard reserved memory areas
MemPhReserveFrames(0x0, 0x400 + 256); // Real mode IVT, BDA
MemPhReserveFrames(0x1000, 0x2400); // DMA buffer
MemPhReserveFrames(0x9FC00, 385*1024); // EBDA, Video memory, ROM area
}
void MemoryInitialize (MultibootInfo* info)
{
uint32 totalSystemMemory = _memory_get_total_mem(info);
MemPhInitialize(totalSystemMemory);
PagingInitialize((volatile uint32*)&mem_kernel_end);
_memory_reserve_system(info);
KernelHeap = MemHeapCreate(KERNEL_HEAP_START, KERNEL_HEAP_START
+ KERNEL_HEAP_INITIAL_SIZE, 0xCFFFF000, 3); // is kernel, writeable
Log("Mem", "Done initializing memory!");
mem_initialized = 2;
}
void MemoryTempInitialize (uint32 kernel_end)
{
mem_initialized = 1;
mem_kernel_end = kernel_end;
Log("Mem", "Initialized temporary memory manager, allocating from %#0x%x.\n", kernel_end);
}