getfreevirtualpage.c
vmm/vmm.h
ubixos/sched.h
ubixos/kpanic.h
ubixos/spinlock.h
lib/kprint.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
ubixos/spinlock.h
ubixos/types.h
sys/_types.h
vmm/paging.h
vfs/file.h
sys/tss.h
lib/kprint.h
vmm/vmm.h
vfs/mount.h
ubixfs/dirCache.h
sys/sysproto.h
sys/signal.h
ubixos/tty.h
src/sys/vmm/getfreevirtualpage.c
void * void* vmmGetFreeVirtualPage (pidType pid, int count, int type) vmmGetFreeVirtualPage pidType pid int count int type _current btoc ctob fvpSpinLock taskStruct::id K_PANIC kpanic kprintf taskStruct::oInfo PAGE_COW PAGE_DEFAULT PAGE_PRESENT pageEntries parentPageDirAddr spinLock spinUnlock tablesBaseAddress taskStruct::td thread::vm_daddr thread::vm_dsize VM_TASK VM_THRD vmm_remapPage vmmClearVirtualPage vmmFindFreePage osInfo::vmStart x1 x1000 mmap sysExec sysGetFreePage vmm_pageFault
spinLock_t spinLock_t fvpSpinLock fvpSpinLock SPIN_LOCK_INITIALIZER vmmGetFreeVirtualPage

    /*****************************************************************************************
    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: getfreevirtualpage_8c.xml 88 2016-01-12 00:11:29Z reddawg $

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

    #include<vmm/vmm.h>
    #include<ubixos/sched.h>
    #include<ubixos/kpanic.h>
    #include<ubixos/spinlock.h>
    #include<lib/kprint.h>

    staticspinLock_tfvpSpinLock=SPIN_LOCK_INITIALIZER;

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

    Function:void*vmmGetFreeVirtualPage(pidTypepid,intcount);
    Description:ReturnsAFreePageMappedToTheVMSpace
    Notes:

    08/11/02-ThisWillReturnNextAvilableFreePageOfTasksVMSpace

    ************************************************************************/
    void*vmmGetFreeVirtualPage(pidTypepid,intcount,inttype){
    intx=0,y=0,c=0;
    uInt32*pageTableSrc=0x0;
    uInt32*pageDir=0x0;
    uInt32start_page=0x0;

    spinLock(&fvpSpinLock);

    pageDir=(uInt32*)parentPageDirAddr;

    /*LetsSearchForAFreePage*/
    if(_current->oInfo.vmStart<=0x100000)
    kpanic("InvalidvmStart\n");

    if(type==VM_THRD){
    start_page=(uint32_t)(_current->td.vm_daddr+ctob(_current->td.vm_dsize));
    }
    elseif(type==VM_TASK){
    //kprintf("vmStart");
    start_page=_current->oInfo.vmStart;
    }
    else
    K_PANIC("InvalidType");

    //for(x=((_current->td.vm_daddr+_current->td.vm_dsize)/(1024*4096));x<1024;x++){
    for(x=(start_page/(1024*4096));x<1024;x++){
    /*SetPageTableAddress*/
    if((pageDir[x]&PAGE_PRESENT)!=PAGE_PRESENT){
    /*IfPageTableIsNonExistantThenSetItUp*/
    pageDir[x]=(uInt32)vmmFindFreePage(_current->id)|PAGE_DEFAULT;
    /*AlsoAddItToVirtualSpaceSoWeCanMakeChangesLater*/
    pageTableSrc=(uInt32*)(tablesBaseAddress+(4096*767));
    pageTableSrc[x]=pageDir[x];
    y=1;
    /*ReloadPageDirectory*/
    asm(
    "movl%cr3,%eax\n"
    "movl%eax,%cr3\n"
    );
    }
    pageTableSrc=(uInt32*)(tablesBaseAddress+(0x1000*x));
    if(y!=0x0){
    for(y=0x0;y<pageEntries;y++){
    pageTableSrc[y]=(uInt32)0x0;
    }
    }
    for(y=0;y<1024;y++){
    /*LoopThroughThePageTableFindAnUnAllocatedPage*/
    if((pageTableSrc[y]&PAGE_COW)==PAGE_COW){
    kprintf("PAGE_COW");
    //_current->td.vm_dsize+=btoc(0x1000);
    /*HACKMEMORYLEAK*/
    //pageTableSrc[y]=0x0;
    }
    if((uInt32)pageTableSrc[y]==(uInt32)0x0){
    if(count>0x1){
    for(c=0;c<count;c++){
    if(y+c<1024){
    if((pageTableSrc[y+c]&PAGE_COW)==PAGE_COW){
    kprintf("PAGE-COW");
    //_current->td.vm_dsize+=btoc(0x1000);
    /*HACKMEMORYLEAK*/
    //pageTableSrc[y+c]=0x0;
    }

    if((uInt32)pageTableSrc[y+c]!=(uInt32)0x0){
    c=-1;
    break;
    }
    }
    }
    if(c!=-1){
    for(c=0;c<count;c++){
    if((vmm_remapPage((uInt32)vmmFindFreePage(pid),((x*(1024*4096))+((y+c)*4096)),PAGE_DEFAULT))==0x0)
    kpanic("vmmRemapPage:getFreeVirtualPage-1:[0x%X]\n",((x*(1024*4096))+((y+c)*4096)));
    vmmClearVirtualPage((uInt32)((x*(1024*4096))+((y+c)*4096)));
    }
    if(type==VM_THRD)
    _current->td.vm_dsize+=btoc(count*0x1000);
    spinUnlock(&fvpSpinLock);
    return((void*)((x*(1024*4096))+(y*4096)));
    }
    }else{
    /*MapAPhysicalPageToTheVirtualPage*/

    /*
    *remapPage((uInt32)vmmFindFreePage(pid),((x*(1024*4096))+(y*4096))
    *,pid);
    */
    if((vmm_remapPage((uInt32)vmmFindFreePage(pid),((x*(1024*4096))+(y*4096)),PAGE_DEFAULT))==0x0)
    kpanic("vmmRemapPage:getFreeVirtualPage-2\n");

    /*ClearThisPageSoNoGarbageIsThere*/
    vmmClearVirtualPage((uInt32)((x*(1024*4096))+(y*4096)));

    /*ReturnTheAddressOfTheNewlyAllocatePage*/
    if(type==VM_THRD){
    _current->td.vm_dsize+=btoc(count*0x1000);
    kprintf("vm_dsize:[0x%X]][0x%X]\n",ctob(_current->td.vm_dsize),_current->td.vm_dsize);
    }
    //kprintf("(0x%X:0x%X)",_current->td.vm_dsize,vmm_getPhysicalAddr(((x*(1024*4096))+(y*4096))));
    //kprintf("(0x%X:0x%X)",_current->td.vm_dsize+_current->td.vm_daddr,((x*(1024*4096))+(y*4096)));
    spinUnlock(&fvpSpinLock);
    return((void*)((x*(1024*4096))+(y*4096)));
    }
    }
    }
    }
    /*IfNoFreePageWasFoundReturnNULL*/
    spinUnlock(&fvpSpinLock);
    return(0x0);
    }

    /***
    END
    ***/