vmm_memory.c
vmm/vmm.h
sys/io.h
ubixos/kpanic.h
lib/kprintf.h
lib/kmalloc.h
ubixos/vitals.h
ubixos/spinlock.h
assert.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
ubixos/spinlock.h
ubixos/vitals.h
ubixos/types.h
sys/_types.h
vmm/paging.h
ubixos/time.h
vfs/file.h
sys/tss.h
lib/kmalloc.h
vmm/vmm.h
vfs/vfs.h
vfs/mount.h
lib/kprintf.h
assert.h
ubixos/ubthread.h
ubixfs/dirCache.h
sys/sysproto.h
src/sys/vmm/vmm_memory.c
sys/signal.h
ubixos/tty.h
sys/io.h
int int adjustCowCounter (uInt32 baseAddr, int adjustment) adjustCowCounter uInt32 baseAddr int adjustment assert mMap::cowCounter vitalsStruct::freePages freePages memAvail mMap::pid spinLock spinUnlock mMap::status systemVitals vmmCowSpinLock vmmID vmmMemoryMap freePage vmm_pageFault vmmCopyVirtualSpace vmmFreeProcessPages
int int countMemory () countMemory cr0 inportByte outportByte vmmMemMapInit
int int freePage (uInt32 pageAddr) freePage uInt32 pageAddr adjustCowCounter assert mMap::cowCounter vitalsStruct::freePages freePages memAvail mMap::pid spinLock spinUnlock mMap::status systemVitals vmmMemoryMap vmmSpinLock vmm_remapPage
uInt32 uInt32 vmmFindFreePage (pidType pid) vmmFindFreePage pidType pid vitalsStruct::freePages freePages kpanic memAvail memNotavail numPages mMap::pid spinLock spinUnlock status sysID systemVitals vmmMemoryMap vmmSpinLock elf_loadfile execFile kmod_load ldEnable obreak sys_exec sysExec vmm_getFreeMallocPage vmm_pageFault vmm_pagingInit vmm_remapPage vmmGetFreeKernelPage vmmGetFreePage vmmGetFreeVirtualPage
void void vmmFreeProcessPages (pidType pid) vmmFreeProcessPages pidType pid adjustCowCounter mMap::cowCounter vitalsStruct::freePages freePages memAvail numPages PAGE_COW pageEntries parentPageDirAddr mMap::pid spinLock spinUnlock status systemVitals tablesBaseAddress vmmID vmmMemoryMap vmmSpinLock x1000 systemTask
int int vmmMemMapInit () vmmMemMapInit countMemory freePages kprintf memAvail memNotavail numPages mMap::pageAddr mMap::pid status mMap::status vmmID vmmMemoryMap x1000 vmm_init
uInt32 uInt32 freePages freePages 0 adjustCowCounter freePage vmmFindFreePage vmmFreeProcessPages vmmMemMapInit
int int numPages numPages 0x0 vmm_pagingInit vmmFindFreePage vmmFreeProcessPages vmmMemMapInit
spinLock_t spinLock_t vmmCowSpinLock vmmCowSpinLock SPIN_LOCK_INITIALIZER adjustCowCounter
mMap * mMap* vmmMemoryMap vmmMemoryMap (mMap *) 0x101000 adjustCowCounter freePage kmain vmm_pagingInit vmmFindFreePage vmmFreeProcessPages vmmMemMapInit
spinLock_t spinLock_t vmmSpinLock vmmSpinLock SPIN_LOCK_INITIALIZER freePage vmmFindFreePage vmmFreeProcessPages

    /*****************************************************************************************
    Copyright(c)2002-2004TheUbixOSProject
    Allrightsreserved.

    Redistributionanduseinsourceandbinaryforms,withorwithoutmodification,are
    permittedprovidedthatthefollowingconditionsaremet:

    Redistributionsofsourcecodemustretaintheabovecopyrightnotice,thislistof
    conditions,thefollowingdisclaimerandthelistofauthors.Redistributionsinbinary
    formmustreproducetheabovecopyrightnotice,thislistofconditions,thefollowing
    disclaimerandthelistofauthorsinthedocumentationand/orothermaterialsprovided
    withthedistribution.NeitherthenameoftheUbixOSProjectnorthenamesofits
    contributorsmaybeusedtoendorseorpromoteproductsderivedfromthissoftware
    withoutspecificpriorwrittenpermission.

    THISSOFTWAREISPROVIDEDBYTHECOPYRIGHTHOLDERSANDCONTRIBUTORS"ASIS"ANDANY
    EXPRESSORIMPLIEDWARRANTIES,INCLUDING,BUTNOTLIMITEDTO,THEIMPLIEDWARRANTIESOF
    MERCHANTABILITYANDFITNESSFORAPARTICULARPURPOSEAREDISCLAIMED.INNOEVENTSHALL
    THECOPYRIGHTOWNERORCONTRIBUTORSBELIABLEFORANYDIRECT,INDIRECT,INCIDENTAL,
    SPECIAL,EXEMPLARY,ORCONSEQUENTIALDAMAGES(INCLUDING,BUTNOTLIMITEDTO,PROCUREMENT
    OFSUBSTITUTEGOODSORSERVICES;LOSSOFUSE,DATA,ORPROFITS;ORBUSINESSINTERRUPTION)
    HOWEVERCAUSEDANDONANYTHEORYOFLIABILITY,WHETHERINCONTRACT,STRICTLIABILITY,OR
    TORT(INCLUDINGNEGLIGENCEOROTHERWISE)ARISINGINANYWAYOUTOFTHEUSEOFTHIS
    SOFTWARE,EVENIFADVISEDOFTHEPOSSIBILITYOFSUCHDAMAGE.

    $Id: vmm__memory_8c.xml 88 2016-01-12 00:11:29Z reddawg $

    *****************************************************************************************/

    #include<vmm/vmm.h>
    #include<sys/io.h>
    #include<ubixos/kpanic.h>
    #include<lib/kprintf.h>
    #include<lib/kmalloc.h>
    #include<ubixos/vitals.h>
    #include<ubixos/spinlock.h>
    #include<assert.h>

    staticuInt32freePages=0;
    staticspinLock_tvmmSpinLock=SPIN_LOCK_INITIALIZER;
    staticspinLock_tvmmCowSpinLock=SPIN_LOCK_INITIALIZER;

    intnumPages=0x0;
    mMap*vmmMemoryMap=(mMap*)0x101000;

    /************************************************************************

    Function:voidvmmMemMapInit();
    Description:ThisFunctionInitializesTheMemoryMapFortheSystem
    Notes:

    02/20/2004-MadeItReportRealAndAvailableMemory

    ************************************************************************/
    intvmmMemMapInit(){
    inti=0x0;
    intmemStart=0x0;

    /*CountSystemMemory*/
    numPages=countMemory();

    /*SetMemoryMapToPointToFirstPhysicalPageThatWeWillUse*/
    vmmMemoryMap=(mMap*)0x101000;

    /*InitializeMapMakeAllPagesNotAvailable*/
    for(i=0x0;i<numPages;i++){
    vmmMemoryMap[i].cowCounter=0x0;
    vmmMemoryMap[i].status=memNotavail;
    vmmMemoryMap[i].pid=vmmID;
    vmmMemoryMap[i].pageAddr=i*4096;
    }

    /*CalculateStartOfFreeMemory*/
    memStart=(0x101000/0x1000);
    memStart+=(((sizeof(mMap)*numPages)+(sizeof(mMap)-1))/0x1000);

    /*InitializeAllFreePagesToAvailable*/
    vmmMemoryMap[(0x100000/0x1000)].status=memAvail;
    freePages++;
    for(i=memStart;i<numPages;i++){
    vmmMemoryMap[i].status=memAvail;
    freePages++;
    }

    /*PrintOutAmountOfMemory*/
    kprintf("RealMemory:%iKB\n",numPages*4);
    kprintf("AvailableMemory:%iKB\n",freePages*4);

    /*Return*/
    return(0);
    }

    /************************************************************************

    Function:intcountMemory();
    Description:ThisFunctionCountsTheSystemsPhysicalMemory
    Notes:

    02/20/2004-InspectForQualityAndApproved

    ************************************************************************/
    intcountMemory(){
    registeruInt32*mem=0x0;
    unsignedlongmemCount=-1,tempMemory=0x0;
    unsignedshortmemKb=0;
    unsignedcharirq1State,irq2State;
    unsignedlongcr0=0x0;

    /*
    *SaveTheStatesOfBothIRQ1And2SoWeCanTurnThemOffAndRestore
    *ThemLater
    */
    irq1State=inportByte(0x21);
    irq2State=inportByte(0xA1);

    /*TurnOffIRQ1And2ToPreventChancesOfFaultsWhileExaminingMemory*/
    outportByte(0x21,0xFF);
    outportByte(0xA1,0xFF);

    /*SaveTheStateOfRegisterCR0*/
    asmvolatile(
    "movl%%cr0,%%ebx\n"
    :"=a"(cr0)
    :
    :"ebx"
    );

    asmvolatile("wbinvd");
    asmvolatile(
    "movl%%ebx,%%cr0\n"
    :
    :"a"(cr0|0x00000001|0x40000000|0x20000000)
    :"ebx"
    );

    while(memKb<4096&&memCount!=0){
    memKb++;
    if(memCount==-1)
    memCount=0;
    memCount+=1024*1024;
    mem=(uInt32*)memCount;
    tempMemory=*mem;
    *mem=0x55AA55AA;
    asm("":::"memory");
    if(*mem!=0x55AA55AA){
    memCount=0;
    }
    else{
    *mem=0xAA55AA55;
    asm("":::"memory");
    if(*mem!=0xAA55AA55){
    memCount=0;
    }
    }
    asm("":::"memory");
    *mem=tempMemory;
    }

    asmvolatile(
    "movl%%ebx,%%cr0\n"
    :
    :"a"(cr0)
    :"ebx"
    );

    /*RestoreStatesForBothIRQ1And2*/
    outportByte(0x21,irq1State);
    outportByte(0xA1,irq2State);

    /*ReturnAmountOfMemoryInPages*/
    return((memKb*1024*1024)/4096);
    }

    /************************************************************************

    Function:uInt32vmmFindFreePage(pid_tpid);

    Description:ThisReturnsAFreePhysicalPageAddressThenMarksIt
    NotAvailableAsWellAsSettingThePIDToTheProccess
    AllocatingThisPage
    Notes:

    ************************************************************************/
    uInt32vmmFindFreePage(pidTypepid){
    inti=0x0;

    /*LetsLookForAFreePage*/
    if(pid<sysID)
    kpanic("Error:invalidPID%i\n",pid);

    spinLock(&vmmSpinLock);

    for(i=0;i<=numPages;i++){

    /*
    *IfWeFoundAFreePageSetItToNotAvailableAfterThatSetItsOwn
    *AndReturnTheAddress
    */
    if((vmmMemoryMap[i].status==memAvail)&&(vmmMemoryMap[i].cowCounter==0)){
    vmmMemoryMap[i].status=memNotavail;
    vmmMemoryMap[i].pid=pid;
    freePages--;
    if(systemVitals)
    systemVitals->freePages=freePages;

    spinUnlock(&vmmSpinLock);
    return(vmmMemoryMap[i].pageAddr);
    }
    }

    /*IfNoFreeMemoryIsFoundReturnNULL*/
    kpanic("OutOfMemory!!!!");
    return(0x0);
    }

    /************************************************************************

    Function:intfreePage(uInt32pageAddr);

    Description:ThisFunctionMarksThePageAsFree

    Notes:

    ************************************************************************/
    intfreePage(uInt32pageAddr){
    intpageIndex=0x0;
    assert((pageAddr&0xFFF)==0x0);
    spinLock(&vmmSpinLock);

    /*FindThePageIndexToTheMemoryMap*/
    pageIndex=(pageAddr/4096);

    /*CheckIfPageCOWIsGreaterThen0IfItIsDecItIfNotFreeIt*/
    if(vmmMemoryMap[pageIndex].cowCounter==0){
    /*SetPageAsAvailSoItCanBeUsedAgain*/
    vmmMemoryMap[pageIndex].status=memAvail;
    vmmMemoryMap[pageIndex].cowCounter=0x0;
    vmmMemoryMap[pageIndex].pid=-2;
    freePages++;
    systemVitals->freePages=freePages;
    }
    else{
    /*AdjustTheCOWCounter*/
    adjustCowCounter(((uInt32)vmmMemoryMap[pageIndex].pageAddr),-1);
    }
    spinUnlock(&vmmSpinLock);
    /*Return*/
    return(0);
    }

    /************************************************************************

    Function:intadjustCowCounter(uInt32baseAddr,intadjustment);

    Description:ThisAdjustTheCOWCounterForPageAtbaseAddrItWill
    ErrorIfTheCountGoesBelow0

    Notes:

    08/01/02-IThinkIfCounterGetsTo0IShouldFreeThePage

    ************************************************************************/
    intadjustCowCounter(uInt32baseAddr,intadjustment){
    intvmmMemoryMapIndex=(baseAddr/4096);
    assert((baseAddr&0xFFF)==0x0);
    spinLock(&vmmCowSpinLock);
    /*AdjustCOWCounter*/
    vmmMemoryMap[vmmMemoryMapIndex].cowCounter+=adjustment;

    if(vmmMemoryMap[vmmMemoryMapIndex].cowCounter==0){
    vmmMemoryMap[vmmMemoryMapIndex].cowCounter=0x0;
    vmmMemoryMap[vmmMemoryMapIndex].pid=vmmID;
    vmmMemoryMap[vmmMemoryMapIndex].status=memAvail;
    freePages++;
    systemVitals->freePages=freePages;
    }
    spinUnlock(&vmmCowSpinLock);
    /*Return*/
    return(0);
    }

    /************************************************************************

    Function:voidvmmFreeProcessPages(pid_tpid);

    Description:ThisFunctionWillFreeUpMemoryForTheExitingProcess

    Notes:

    08/04/02-AddedCheckingForCOWPagesFirst

    ************************************************************************/
    voidvmmFreeProcessPages(pidTypepid){
    inti=0,x=0;
    uInt32*tmpPageTable=0x0;
    uInt32*tmpPageDir=(uInt32*)parentPageDirAddr;
    spinLock(&vmmSpinLock);
    /*CheckPageDirectoryForAnAvailPageTable*/
    for(i=0;i<=0x300;i++){
    if(tmpPageDir[i]!=0){
    /*SetUpPageTablePointer*/
    tmpPageTable=(uInt32*)(tablesBaseAddress+(i*0x1000));
    /*CheckThePageTableForCOWPages*/
    for(x=0;x<pageEntries;x++){
    /*IfThePageIsCOWAdjustCOWCounter*/
    if(((uInt32)tmpPageTable[x]&PAGE_COW)==PAGE_COW){
    adjustCowCounter(((uInt32)tmpPageTable[x]&0xFFFFF000),-1);
    }
    }
    }
    }

    /*LoopThroughPagesToFindPagesOwnedByProcess*/
    for(i=0;i<numPages;i++){
    if(vmmMemoryMap[i].pid==pid){
    /*CheckToSeeIfThecowCounterIsZeroIfSoWeCanReeIt*/
    if(vmmMemoryMap[i].cowCounter==0){
    vmmMemoryMap[i].status=memAvail;
    vmmMemoryMap[i].cowCounter=0x0;
    vmmMemoryMap[i].pid=vmmID;
    freePages++;
    systemVitals->freePages=freePages;
    }
    }
    }
    /*Return*/
    spinUnlock(&vmmSpinLock);
    return;
    }

    /***
    $Log: vmm__memory_8c.xml,v $
    Revision 1.6  2006/12/19 14:12:54  reddawg
    rtld-elf almost workign

    Revision1.12006/12/0118:46:19reddawg
    renamingfiles

    Revision1.22006/12/0105:12:35reddawg
    We'realmostthere...:)

    Revision1.1.1.12006/06/0112:46:13reddawg
    ubix2

    Revision1.52006/06/0112:42:09reddawg
    Gettingbacktothebasics

    Revision1.42006/06/0104:15:32reddawg
    Woot

    Revision1.32006/06/0103:58:33reddawg
    wonderingaboutthisstuffhere

    Revision1.22005/10/1200:13:38reddawg
    Removed

    Revision1.1.1.12005/09/2617:24:51reddawg
    nomessage

    Revision1.152004/09/1123:39:31reddawg
    oktimeforbed

    Revision1.142004/09/1116:39:19apwillia
    FixorderinadjustCowCountertopreventpotentialracecondition

    Revision1.132004/08/1411:23:03reddawg
    Changes

    Revision1.122004/08/0120:51:33reddawg
    adjustCowCounter:wenolongerneedtodebugunhandledadjustmentstheyarenormalsituationsnow

    Revision1.112004/07/2800:17:05reddawg
    Major:
    Disconnectedpage0x0fromthesystem...Unfortunatelythisbrokemanythings
    allofwhichhavebeenfixed.ThiswasgoodbecausenothingdeferencesNULL
    anymore.

    Thingsaffected:
    malloc,kmalloc,getfreepage,getfreevirtualpage,pagefault,fork,exec,ld,ld.so,exec,file

    Revision1.102004/07/2619:15:49reddawg
    testcode,fixesandthelike

    Revision1.92004/07/2423:04:44reddawg
    Changes...markletmeknowifyoufaultatpid185whenyoutypestress

    Revision1.82004/07/2417:47:28reddawg
    vmm_pageFault:deadlockresolvedthankstoaproppersolutionsuggestedbygeist

    Revision1.72004/07/1902:04:32reddawg
    memory.c:addedspinlockstovmmFindFreePageandvmmFreePagetopreventtwotasksfrompossiblyallocatingthesamepage

    Revision1.62004/06/1412:20:54reddawg
    notes:manybugsrepairedandldworks100%now.

    Revision1.52004/05/2115:34:23reddawg
    Fixedacoupleoftypo

    Revision1.42004/05/2114:50:10reddawg
    Cleanedup

    Revision1.32004/05/1917:28:28reddawg
    AddedthecorrectendTaskProcedure

    Revision1.22004/04/3014:16:04reddawg
    FixedallthedatatypestobeconsistantuInt8,uInt16,uInt32,Int8,Int16,Int32

    Revision1.1.1.12004/04/1512:06:52reddawg
    UbixOSv1.0

    Revision1.272004/04/1316:36:34reddawg
    Changedourcopyright,itisallnowunderaBSD-Stylelicense

    END
    ***/