Clan x86

Technical (Development, Security, etc.) => General Programming => Operating System Development => Topic started by: Warrior on February 14, 2006, 09:26:20 pm

Title: Memory Management Notes
Post by: Warrior on February 14, 2006, 09:26:20 pm
Memory Management in Optimix:

Our Memory Manager will have three compoenents

- Physical
- Virtual
- Linear

Physical is initialized first and it is located in a "physHeap" right after the kernel in memory, it provides
two functions (physAlloc() and physFree(unsigned long addr)) they deal with serving up 4KB physical pages of memory.

Paging is initialized next which uses Physical to help map PageTables which are non present and it will free pages
which are being overwritten (If they are found present by the Mapper, it is unmapped then free'd)

Linear is what comes after Paging, it is the "Kernel Memory Manager" since it manages the kernel heap (Virtual)
and is what the C Std Lib "malloc" and friends wrap around.

The good thing about having these three components is they are completely transparent from eachother.
Physical is never used out of Paging (Actually the cool part is we selfmap the Physical heap to the same virt addr, so we can continue to use it for paging even after we enable paging which may be good for things like spawning tasks), Paging is never used
other than spawning/deleting tasks and threads and of course Page faults.

The only thing in the end widely used is the Linear Manager which is the end result after all this shifting and mapping.

Title: Re: Memory Management Notes
Post by: Warrior on February 17, 2006, 06:17:28 am
Code: [Select]
/*  Optimix Operating System
 *  optimix/kernel/mm/phys.c
 * 
 *  Copyright (C) 2005,2006 Nelson Carrillo
 */

#include <optimix/mm/phys.h>

extern keEnd;

static int allocTimes = 0;

void* physAlloc()
{
int tmpAlloc;

memoryBlock* memBlock = (memoryBlock*)keEnd;
memoryBlock* memBest = NULL;

if (allocTimes == 0)
{
memBlock->used = true;
memBest = memBlock;

return (void*)memBest;
} else {
tmpAlloc = allocTimes;

while (tmpAlloc < 0)
{
if (memBlock->used = false)
{
memBest = memBlock;
break;
}

memBlock = (void*)memBlock + sizeof(memoryBlock) + 4096;
tmpAlloc--;
}

if (memBest == NULL)
return NULL

return (void*)memBest;
}
}

void physFree( void* addr)
{
memoryBlock block;

if (addr == NULL) return;

block = (void*)addr - sizeof(memoryBlock);

block->used = false;

return;
}

Implemented an (untested) physical memory manager. Serves up 4KB pages of physical memory.