↑ Back to main site.

EPOS

Experimental Protected-mode Operating System

mem.c
/*
EPOS
http://www.atanaslaskov.com/epos/

FILE:           mem.c
DESCRIPTION:    Memory management (builds on top of kernel\memory\memory.c)
 
BSD LICENSE
Copyright (c) 2006, Atanas Laskov
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
   1. Redistributions of source code must retain the above copyright notice,
   this list of conditions and the following disclaimer.
   2. Redistributions in binary form must reproduce the above copyright notice,
   this list of conditions and the following disclaimer in the documentation
   and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL ATANAS LASKOV BE LIABLE FOR ANY
DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include "..\..\init\init.h"
#include "..\..\service\service.h"
#include "..\..\gdt\gdt.h"
#include "..\..\memory\memory.h"
#include "..\..\process\process.h"
#include "..\..\paging\paging.h"
#include "..\specmem\specmem.h"

#include "mem.h"

// Globals
//
typedef struct{
    unsigned long bValid;
    HMEMORY  hMem;
    SMHANDLE hSpecMem;
    unsigned long dwBase;
    unsigned long dwMapBase;
    unsigned long dwSize;
    unsigned long bSpecial;
    PID dwPID;
}ProcessMBlock;

typedef struct{
    ProcessMBlock arBlock[MAX_AREACNT_PER_PROCESS];
    unsigned long dwSegmentExtraSize;
}ProcessMemory;

ProcessMemory g_arPrcMem[MAX_PROCESS_CNT];

// Initialize memory managemen services
//
void k_memInit() {
    unsigned long i, j;
   
    for(i=0; i<MAX_PROCESS_CNT; i++) {
        for(j=0; j<MAX_AREACNT_PER_PROCESS; j++)
         g_arPrcMem[i].arBlock[j].bValid = 0;
       
        g_arPrcMem[i].dwSegmentExtraSize = 0;
    }
}

// This call indicates a change in process state
//
void k_memProcessIsLive(PID dwPID) {
    // Do nothing.
}

// User process requests a block of memory
//
void mAlloc(MALLOC *ma)
{
    unsigned long i, j, dwStart;
    MEMRET r;
    struct MemBlock Info;
    ProcessMBlock* arBlocks;
    char bAcceptible;

    // Find free descriptor
    //
    for(i=0; i<MAX_AREACNT_PER_PROCESS; i++)
        if(g_arPrcMem[tmGetCrProcessID()].arBlock[i].bValid==0)
            break;
       
    if(i>=MAX_AREACNT_PER_PROCESS){
        ma->st = MEM_OUT_OF_RES; return;
    }
   
    arBlocks = g_arPrcMem[tmGetCrProcessID()].arBlock;

    // Allocate memory
    //
    arBlocks[i].bSpecial = ma->bRequireSpecialMemory;
   
    switch( ma->bRequireSpecialMemory )
    {
    case MEM_SPECIAL_NO:
        {
        r = memAlloc(&(arBlocks[i].hMem), ma->dwSize);
        if(r!=MEMRET_OK){ma->st = MEM_OUT_OF_MEM; return;}
        memBlockInfo(arBlocks[i].hMem, &Info);
        arBlocks[i].dwBase = Info.dwStart;
        arBlocks[i].dwSize = Info.dwEnd - Info.dwStart;
        }
        break;
       
    case MEM_SPECIAL_LOWMEM:
        {
        struct SMArea a;
        a.dwStart = (unsigned long)ma->pMem;
        a.dwEnd   = a.dwStart + ma->dwSize;
        smLockArea(&a);
        if(a.st!=SM_ST_OK){ ma->st = MEM_OUT_OF_MEM;; return; }
        arBlocks[i].hSpecMem = a.hAreahandle;

        arBlocks[i].dwBase = a.dwStart;
        arBlocks[i].dwSize = ma->dwSize;
        }
        break;
       
    case MEM_SPECIAL_PROCESS_MEM:
        {
        PID dwPID = (PID)ma->pMem;
       
        if(tmGetProcessState(dwPID)!=T_STATE__SELECTED) {
            ma->st = MEM_OUT_OF_MEM;
            return;
        }
       
        if(tmGetProcessParent(dwPID)!=tmGetCrProcessID()) {
            ma->st = MEM_OUT_OF_MEM;
            return;
        }
       
        arBlocks[i].dwPID = dwPID;
        memBlockInfo(tmGetProcessMemory(dwPID), &Info);
        arBlocks[i].dwBase = Info.dwStart;
        arBlocks[i].dwSize = Info.dwEnd - Info.dwStart;
        ma->dwSize = arBlocks[i].dwSize;
        }
        break;
       
    default:
        ma->st = MEM_OUT_OF_MEM;
        return;
    }

    // Find free memory
    //
    for( dwStart=0x100000+tmGetProcessMemSize(tmGetCrProcessID());
         dwStart<memGetPhysicalMemorySize()-ma->dwSize;
         dwStart+=4*1024 )
    {
        // Check if the current base is free
        //
        bAcceptible = 1;
       
        for(j=0; j<MAX_AREACNT_PER_PROCESS; j++) if(arBlocks[j].bValid)
        {
            if( ((dwStart+ma->dwSize)<arBlocks[j].dwMapBase) ||
            (dwStart>(arBlocks[j].dwMapBase+arBlocks[j].dwSize)) )
                continue;
                   
            bAcceptible = 0;
            break;
        }
       
        if(bAcceptible) break;
    }

    if(dwStart>=(memGetPhysicalMemorySize()-ma->dwSize)) {
        ma->st = MEM_OUT_OF_MEM;
        return;
    }

    // Lock memory descriptor
    //
    arBlocks[i].bValid = 1;

    // Expand segments and page directory
    // for the client process
    //
    arBlocks[i].dwMapBase = dwStart;

    if( (arBlocks[i].dwMapBase + arBlocks[i].dwSize)
        > (0x100000+tmGetProcessMemSize(tmGetCrProcessID())
            + g_arPrcMem[tmGetCrProcessID()].dwSegmentExtraSize) )
    {
        struct GDTDescriptor* pDesc;

        g_arPrcMem[tmGetCrProcessID()].dwSegmentExtraSize
            += arBlocks[i].dwSize;

        pDesc = gdtGetDescriptorLDT(tmGetCrProcessID(), 0);
       
        gdtSetLimit(
            pDesc,
            tmGetProcessMemSize(tmGetCrProcessID())
            + g_arPrcMem[tmGetCrProcessID()].dwSegmentExtraSize
        );
       
        pDesc++;
       
        gdtSetLimit(
            pDesc,
            tmGetProcessMemSize(tmGetCrProcessID())
            + g_arPrcMem[tmGetCrProcessID()].dwSegmentExtraSize
        );

        pageDirectorySize(
            PAGING_DIR_PROCESS0 + tmGetCrProcessID(),
            0x1000000 + tmGetProcessMemSize(tmGetCrProcessID())
            + g_arPrcMem[tmGetCrProcessID()].dwSegmentExtraSize
        );
    }

    // Map new pages
    //
    pageMapPages(
        PAGING_DIR_PROCESS0 + tmGetCrProcessID(),
        dwStart, arBlocks[i].dwBase, arBlocks[i].dwSize
    );

    // OK
    //
    ma->pMem = (char*)( arBlocks[i].dwMapBase - 0x100000 );
   
    if( ma->bRequireSpecialMemory==MEM_SPECIAL_LOWMEM ) {
        ma->pMem = (char*)
            ( arBlocks[i].dwMapBase - 0x100000
            + (arBlocks[i].dwBase-(arBlocks[i].dwBase/0x1000)*0x1000));
    }
    ma->st = MEM_OK;
}

// Free memory block
//
void mFree(MFREE *mf){
    // This is not implemented yet :-).
}

// Request memory status
//
void mInfo(MINFO *mi) {
    unsigned long i;
    unsigned long dwBase = (unsigned long)(mi->pMem);
   
    for(i=0; i<MAX_AREACNT_PER_PROCESS; i++)
        if(g_arPrcMem[tmGetCrProcessID()].arBlock[i].dwBase == dwBase)
            break;
   
    if(i>=MAX_AREACNT_PER_PROCESS) {
        mi->dwSize = 0;
        mi->st     = MEM_INVALID_MEM;
        return;
    }
   
    mi->dwSize = g_arPrcMem[tmGetCrProcessID()].arBlock[i].dwSize;
    mi->st     = MEM_OK;
}