luxos/Kernel/library/memory/memory_init.c

94 lines
2.3 KiB
C
Raw Normal View History

/*
* 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)
MemPhReserveBlocks((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
MemPhReserveBlocks(0x00F00000, 0x00100000);
}
}
void MemoryInitialize (MultibootInfo* info)
{
// Get total system memory
uint32 totalSystemMemory = _memory_get_total_mem(info);
// Initialize physical & virtual memory managers
uint32 end = mem_kernel_end + 0x80000;
MemPhInitialize(totalSystemMemory);
PagingInitialize(&end);
// Reserve physical blocks
_memory_reserve_system(info);
uint32 i;
// Allocate some space for the kernel heap
for (i = KERNEL_HEAP_START; i <= KERNEL_HEAP_END; i += 0x1000)
PagingMapPage(MemPhAllocateBlock(), i, PageWriteable, KernelDirectory);
// Create the kernel heap
KernelHeap = MemHeapCreate(KERNEL_HEAP_START, KERNEL_HEAP_START
+ KERNEL_HEAP_INITIAL_SIZE, 0xCFFFF000, 3); // is kernel, writeable
Log("Mem", "Done initializing memory!\n");
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", ColorWhite, mem_kernel_end);
}