exec.c
ubixos/exec.h
ubixos/elf.h
ubixos/ld.h
ubixos/kpanic.h
ubixos/endtask.h
vmm/vmm.h
lib/kmalloc.h
lib/kprintf.h
lib/string.h
assert.h
sys/thread.h
ubixos/kpanic.h
ubixos/sched.h
ubixos/endtask.h
ubixos/types.h
sys/_types.h
lib/string.h
vmm/paging.h
vfs/file.h
sys/tss.h
lib/kmalloc.h
vmm/vmm.h
vfs/mount.h
lib/kprintf.h
ubixos/ld.h
assert.h
ubixfs/dirCache.h
ubixos/exec.h
sys/sysproto.h
ubixos/elf.h
sys/signal.h
ubixos/tty.h
src/sys/kernel/exec.c
STACK_ADDR
0xC800000
execFile
sysExec
void void execFile (char *file, int argc, char **argv, int console) execFile char * file int argc char ** argv int console _current assert tssStruct::back_link tssStruct::cr3 tssStruct::cs tssStruct::ds tssStruct::ebp tssStruct::edi elfHeader::eEntry tssStruct::eflags elfHeader::eIdent tssStruct::eip elfHeader::ePhnum elfHeader::ePhoff tssStruct::es tssStruct::esi tssStruct::esp tssStruct::esp0 tssStruct::esp1 tssStruct::esp2 elfHeader::eType fclose fopen fread tssStruct::fs fseek taskStruct::gid tssStruct::gs taskStruct::id tssStruct::io_map K_PANIC KERNEL_PAGE_DEFAULT kernelPageDirectory kfree kmalloc kpanic kprintf tssStruct::ldt memset taskStruct::oInfo tty_termNode::owner PAGE_DEFAULT PAGE_PRESENT PAGE_STACK PAGE_USER fileDescriptorStruct::perms elfProgramHeader::phMemsz elfProgramHeader::phVaddr READY sched_setStatus schedNewTask tssStruct::ss tssStruct::ss0 tssStruct::ss1 tssStruct::ss2 STACK_ADDR taskStruct::td taskStruct::term tssStruct::trace_bitmap taskStruct::tss tty_find taskStruct::uid thread::vm_daddr vmm_remapPage vmm_setPageAttributes vmmCreateVirtualSpace vmmFindFreePage osInfo::vmStart x1000 x18 x30 kmain
uInt32 uInt32 execThread (void(*tproc)(void), uInt32 stack, char *arg) execThread void(*)(void) tproc uInt32 stack char * arg assert tssStruct::back_link tssStruct::cr3 tssStruct::cs tssStruct::ds tssStruct::ebp tssStruct::edi tssStruct::eflags tssStruct::eip tssStruct::es tssStruct::esi tssStruct::esp tssStruct::esp0 tssStruct::esp1 tssStruct::esp2 tssStruct::fs tssStruct::gs taskStruct::id taskStruct::imageFd tssStruct::io_map kernelPageDirectory kpanic tssStruct::ldt taskStruct::oInfo READY sched_setStatus schedNewTask tssStruct::ss tssStruct::ss0 tssStruct::ss1 tssStruct::ss2 tssStruct::trace_bitmap taskStruct::tss osInfo::vmStart kmain ubthread_create
void void sys_exec (char *file, char *ap) sys_exec char * file char * ap

New exec...

Clean the virtual of COW pages left over from the fork

Adjust iframe

build argc and argv[]

Start of Env

_current
elfHeader::eEntry
elfHeader::eIdent
i386_frame::eip
elfHeader::ePhnum
elfHeader::ePhoff
ET_EXEC
elfHeader::eType
fclose
fopen
fread
fseek
taskStruct::id
taskStruct::imageFd
K_PANIC
kfree
kmalloc
kprintf
memset
taskStruct::oInfo
PAGE_DEFAULT
PAGE_PRESENT
PAGE_SHIFT
PAGE_USER
elfProgramHeader::phMemsz
elfProgramHeader::phOffset
elfProgramHeader::phVaddr
PT_INTERP
PT_LOAD
PT_PHDR
round_page
taskStruct::td
trunc_page
thread::vm_daddr
thread::vm_dsize
vmm_remapPage
vmm_setPageAttributes
vmmFindFreePage
osInfo::vmStart
x1000
sysExec
void void sysExec (char *file, char *ap) sysExec char * file char * ap

Clean the virtual of COW pages left over from the fork

Adjust iframe

build argc and argv[]

_current
assert
i386_frame::ebp
elfHeader::eEntry
elfHeader::eIdent
i386_frame::eip
endTask
elfHeader::ePhnum
elfHeader::ePhoff
elfHeader::eShnum
elfHeader::eShoff
tssStruct::esp0
elfHeader::eType
fclose
fopen
fread
fseek
taskStruct::id
taskStruct::imageFd
K_PANIC
kfree
kmalloc
kpanic
kprintf
ldEnable
memset
taskStruct::oInfo
PAGE_DEFAULT
PAGE_PRESENT
PAGE_SHIFT
PAGE_SIZE
PAGE_USER
fileDescriptorStruct::perms
elfProgramHeader::phMemsz
elfProgramHeader::phVaddr
PT_DYNAMIC
PT_INTERP
PT_LOAD
round_page
STACK_ADDR
strcpy
strlen
sys_exec
taskStruct::td
trunc_page
taskStruct::tss
i386_frame::user_esp
thread::vm_daddr
thread::vm_dsize
VM_TASK
vmm_cleanVirtualSpace
vmm_remapPage
vmm_setPageAttributes
vmmFindFreePage
vmmGetFreeVirtualPage
osInfo::vmStart
x1000

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

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

    #include<ubixos/exec.h>
    #include<ubixos/elf.h>
    #include<ubixos/ld.h>
    #include<ubixos/kpanic.h>
    #include<ubixos/endtask.h>
    #include<vmm/vmm.h>
    #include<lib/kmalloc.h>
    #include<lib/kprintf.h>
    #include<lib/string.h>
    #include<assert.h>

    #defineSTACK_ADDR0xC800000

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

    Function:execThread(void(*)(void),int,char*);
    Description:Thisfunctionwillcreateathreadfromcodeinthecurrentmemoryspace

    Notes:

    05/19/04-ThisdoesnotworkthewayIwantittoitstillmakesacopyofkernelspace
    sodonotuseoutsideofkernelspace

    *****************************************************************************************/
    uInt32execThread(void(*tproc)(void),uInt32stack,char*arg){
    kTask_t*newProcess=0x0;
    /*FindANewThread*/
    newProcess=schedNewTask();
    assert(newProcess);
    if(stack<0x100000)
    kpanic("exec:stacknotinvalidarea:[0x%X]\n",stack);

    /*SetAllTheCorrectThreadAttributes*/
    newProcess->tss.back_link=0x0;
    newProcess->tss.esp0=0x0;
    newProcess->tss.ss0=0x0;
    newProcess->tss.esp1=0x0;
    newProcess->tss.ss1=0x0;
    newProcess->tss.esp2=0x0;
    newProcess->tss.ss2=0x0;
    newProcess->tss.cr3=(unsignedint)kernelPageDirectory;
    newProcess->tss.eip=(unsignedint)tproc;
    newProcess->tss.eflags=0x206;
    newProcess->tss.esp=stack;
    newProcess->tss.ebp=stack;
    newProcess->tss.esi=0x0;
    newProcess->tss.edi=0x0;

    /*Settheseuptobering3tasks*/
    /*
    newProcess->tss.es=0x30+3;
    newProcess->tss.cs=0x28+3;
    newProcess->tss.ss=0x30+3;
    newProcess->tss.ds=0x30+3;
    newProcess->tss.fs=0x30+3;
    newProcess->tss.gs=0x30+3;
    */

    newProcess->tss.es=0x10;
    newProcess->tss.cs=0x08;
    newProcess->tss.ss=0x10;
    newProcess->tss.ds=0x10;
    newProcess->tss.fs=0x10;
    newProcess->tss.gs=0x10;

    newProcess->tss.ldt=0x18;
    newProcess->tss.trace_bitmap=0x0000;
    newProcess->tss.io_map=0x8000;
    newProcess->oInfo.vmStart=0x6400000;

    newProcess->imageFd=0x0;

    /*Setupdefaultstackforthreadherefilledwitharglist3times*/
    asmvolatile(
    "pusha\n"
    "movl%%esp,%%ecx\n"
    "movl%1,%%eax\n"
    "movl%%eax,%%esp\n"
    "pushl%%ebx\n"
    "pushl%%ebx\n"
    "pushl%%ebx\n"
    "movl%%esp,%%eax\n"
    "movl%%eax,%1\n"
    "movl%%ecx,%%esp\n"
    "popa\n"
    :
    :"b"(arg),"m"(newProcess->tss.esp)
    );

    /*PutnewthreadintotheREADYstate*/
    sched_setStatus(newProcess->id,READY);

    /*ReturnwiththenewprocessID*/
    return((uInt32)newProcess);
    }

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

    Function:voidexecFile(char*file);
    Description:ThisFunctionExecutesAKileIntoANewVMSpaceWithOut
    HavingToFork
    Notes:

    07/30/02-IHaveMadeSomeHeavyChangesToThisAsWellAsFixedAFew
    MemoryLeaksTheMemoryAllocatedToLoadTheBinaryIntoIs
    NowUnmappedSoItCanBeUsedAgainAndNotHeldOntoUntil
    TheProgramExits

    07/30/02-NowIHaveToMakeABetterMemoryAllocatorSoWeCanSetUp
    TheFreshlyAllocatedPagesWithTheCorrectPermissions

    *****************************************************************************************/
    voidexecFile(char*file,intargc,char**argv,intconsole){

    inti=0x0;
    intx=0x0;
    u_int32_t*tmp=0x0;

    fileDescriptor*tmpFd=0x0;
    elfHeader*binaryHeader=0x0;
    elfProgramHeader*programHeader=0x0;

    /*GetANewTaskForThisProccess*/
    _current=schedNewTask();
    assert(_current);
    _current->gid=0x0;
    _current->uid=0x0;
    _current->term=tty_find(console);
    if(_current->term==0x0)
    kprintf("Error:invalidconsole\n");

    /*Setttyownership*/
    _current->term->owner=_current->id;

    /*NowWeMustCreateAVirtualSpaceForThisProccessToRunIn*/
    _current->tss.cr3=(uInt32)vmmCreateVirtualSpace(_current->id);

    /*ToBetterLoadThisApplicationWeWillSwitchOverToItsVMSpace*/
    asmvolatile(
    "movl%0,%%eax\n"
    "movl%%eax,%%cr3\n"
    ::"d"((uInt32*)(_current->tss.cr3))
    );

    /*LetsFindTheFile*/
    tmpFd=fopen(file,"r");

    /*IfWeDontFindtheFileReturn*/
    if(tmpFd==0x0){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    fclose(tmpFd);
    return;
    }
    if(tmpFd->perms==0x0){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    fclose(tmpFd);
    return;
    }

    /*LoadELFHeader*/
    binaryHeader=(elfHeader*)kmalloc(sizeof(elfHeader));

    //kprintf(">a:%i:0x%X:0x%X<",sizeof(elfHeader),binaryHeader,tmpFd);
    fread(binaryHeader,sizeof(elfHeader),1,tmpFd);

    /*CheckIfAppIsARealApplication*/
    if((binaryHeader->eIdent[1]!='E')&&(binaryHeader->eIdent[2]!='L')&&(binaryHeader->eIdent[3]!='F')){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    kfree(binaryHeader);
    fclose(tmpFd);
    return;
    }
    elseif(binaryHeader->eType!=2){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    kfree(binaryHeader);
    fclose(tmpFd);
    return;
    }
    elseif(binaryHeader->eEntry==0x300000){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    kfree(binaryHeader);
    fclose(tmpFd);
    return;
    }

    /*LoadTheProgramHeader(s)*/
    programHeader=(elfProgramHeader*)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum);
    fseek(tmpFd,binaryHeader->ePhoff,0);

    //kprintf(">c:%i:0x%X:0x%X<",sizeof(elfProgramHeader)*binaryHeader->ePhnum,programHeader,tmpFd);
    fread(programHeader,(sizeof(elfProgramHeader)*binaryHeader->ePhnum),1,tmpFd);
    //kprintf(">d<");

    /*LoopThroughTheHeaderAndLoadSectionsWhichNeedToBeLoaded*/
    for(i=0;i<binaryHeader->ePhnum;i++){
    if(programHeader[i].phType==1){
    /*
    AllocateMemoryImGoingToHaveToMakeThisLoadMemoryWithCorrect
    Settingssoithelpsusinthefuture
    */
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    /*Makereadonlyandread/write!!!*/
    if(vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr&0xFFFFF000)+x),PAGE_DEFAULT)==0x0)
    K_PANIC("RemapPageFailed");

    memset((void*)((programHeader[i].phVaddr&0xFFFFF000)+x),0x0,0x1000);
    }
    _current->oInfo.vmStart=0x80000000;
    _current->td.vm_daddr=(char*)(programHeader[i].phVaddr&0xFFFFF000);
    /*NowLoadSectionToMemory*/
    fseek(tmpFd,programHeader[i].phOffset,0);
    fread((void*)programHeader[i].phVaddr,programHeader[i].phFilesz,1,tmpFd);
    if((programHeader[i].phFlags&0x2)!=0x2){
    kprintf("pH:[0x%X]\n",programHeader[i].phMemsz);
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    if((vmm_setPageAttributes((programHeader[i].phVaddr&0xFFFFF000)+x,PAGE_PRESENT|PAGE_USER))!=0x0)
    kpanic("Error:vmm_setPageAttributesfailed,File:%s,Line:%i\n",__FILE__,__LINE__);
    }
    }
    }
    }

    /*SetVirtualMemoryStart*/
    _current->oInfo.vmStart=0x80000000;
    _current->td.vm_daddr=(char*)(programHeader[i].phVaddr&0xFFFFF000);

    /*SetUpStackSpace*/
    for(x=1;x<100;x++){
    vmm_remapPage(vmmFindFreePage(_current->id),STACK_ADDR-(x*0x1000),PAGE_DEFAULT|PAGE_STACK);
    }

    /*KernelStack0x2000byteslong*/
    vmm_remapPage(vmmFindFreePage(_current->id),0x5BC000,KERNEL_PAGE_DEFAULT|PAGE_STACK);
    vmm_remapPage(vmmFindFreePage(_current->id),0x5BB000,KERNEL_PAGE_DEFAULT|PAGE_STACK);

    /*SetAllTheProperInformationForTheTask*/
    _current->tss.back_link=0x0;
    _current->tss.esp0=0x5BC000;
    _current->tss.ss0=0x10;
    _current->tss.esp1=0x0;
    _current->tss.ss1=0x0;
    _current->tss.esp2=0x0;
    _current->tss.ss2=0x0;
    _current->tss.eip=(long)binaryHeader->eEntry;
    _current->tss.eflags=0x206;
    _current->tss.esp=STACK_ADDR-12;
    _current->tss.ebp=STACK_ADDR;
    _current->tss.esi=0x0;
    _current->tss.edi=0x0;

    /*Settheseuptobering3tasks*/
    _current->tss.es=0x30+3;
    _current->tss.cs=0x28+3;
    _current->tss.ss=0x30+3;
    _current->tss.ds=0x30+3;
    _current->tss.fs=0x30+3;
    _current->tss.gs=0x30+3;

    _current->tss.ldt=0x18;
    _current->tss.trace_bitmap=0x0000;
    _current->tss.io_map=0x8000;

    sched_setStatus(_current->id,READY);

    kfree(binaryHeader);
    kfree(programHeader);
    fclose(tmpFd);

    tmp=(uInt32*)_current->tss.esp0-5;
    tmp[0]=binaryHeader->eEntry;
    tmp[3]=STACK_ADDR-12;

    tmp=(uInt32*)STACK_ADDR-2;

    if(_current->id>4)
    kprintf("argv[0]:[%s]\n",argv[0]);
    kprintf("argv:[0x%X]\n",argv);
    tmp[0]=(u_int32_t)argv;
    tmp[1]=(u_int32_t)argv;

    /*SwitchBackToTheKernelsVMSpace*/
    asmvolatile(
    "movl%0,%%eax\n"
    "movl%%eax,%%cr3\n"
    ::"d"((uInt32*)(kernelPageDirectory))
    );

    /*FinallyReturn*/
    return;
    }

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

    Function:voidsysExec();
    Description:ThisIsTheSystemCallToExecuteANewTask

    Notes:
    04-22-03-ItNowLoadsSectionsNotTheFullFile

    *****************************************************************************************/
    voidsysExec(char*file,char*ap){
    inti=0x0;
    intx=0x0;
    intargc=0x0;
    uInt32*tmp=0x0;
    uInt32ldAddr=0x0;
    uInt32seg_size=0x0;
    uInt32seg_addr=0x0;
    char*interp=0x0;
    char**argv=0x0;
    char**argvNew=0x0;
    char*args=0x0;

    fileDescriptor*tmpFd=0x0;
    elfHeader*binaryHeader=0x0;
    elfProgramHeader*programHeader=0x0;
    elfSectionHeader*sectionHeader=0x0;
    elfDynamic*elfDynamicS=0x0;
    structi386_frame*iFrame=0x0;

    if(_current->id>4){
    sys_exec(file,ap);
    return;
    }

    tmpFd=fopen(file,"r");
    _current->imageFd=tmpFd;
    /*IfWeDontFindtheFileReturn*/
    if(tmpFd==0x0){
    return;
    }
    if(tmpFd->perms==0){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    fclose(tmpFd);
    return;
    }

    /*LoadELFHeader*/

    if((binaryHeader=(elfHeader*)kmalloc(sizeof(elfHeader)))==0x0)
    endTask(_current->id);
    fread(binaryHeader,sizeof(elfHeader),1,tmpFd);
    /*SetsectionHeaderToPointToLoadedBinaryToWeCanGatherInfo*/

    /*CheckIfAppIsARealApplication*/
    if((binaryHeader->eIdent[1]!='E')&&(binaryHeader->eIdent[2]!='L')&&(binaryHeader->eIdent[3]!='F')){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    kfree(binaryHeader);
    fclose(tmpFd);

    return;
    }
    elseif(binaryHeader->eType!=2){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    kfree(binaryHeader);
    fclose(tmpFd);
    return;
    }
    elseif(binaryHeader->eEntry==0x300000){
    kprintf("ExecFormatError:BinaryFileNotExecutable.\n");
    kfree(binaryHeader);
    fclose(tmpFd);
    return;
    }

    /*LoadTheProgramHeader(s)*/
    if((programHeader=(elfProgramHeader*)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum))==0x0)
    endTask(_current->id);

    assert(programHeader);
    fseek(tmpFd,binaryHeader->ePhoff,0);
    fread(programHeader,(sizeof(elfProgramHeader)*binaryHeader->ePhnum),1,tmpFd);

    if((sectionHeader=(elfSectionHeader*)kmalloc(sizeof(elfSectionHeader)*binaryHeader->eShnum))==0x0)
    endTask(_current->id);

    assert(sectionHeader);
    fseek(tmpFd,binaryHeader->eShoff,0);
    fread(sectionHeader,sizeof(elfSectionHeader)*binaryHeader->eShnum,1,tmpFd);

    /*LoopThroughTheHeaderAndLoadSectionsWhichNeedToBeLoaded*/
    for(i=0;i<binaryHeader->ePhnum;i++){
    switch(programHeader[i].phType){
    casePT_LOAD:
    seg_addr=trunc_page(programHeader[i].phVaddr);
    seg_size=round_page(programHeader[i].phMemsz+programHeader[i].phVaddr-seg_addr);

    /*
    AllocateMemoryImGoingToHaveToMakeThisLoadMemoryWithCorrect
    Settingssoithelpsusinthefuture
    */
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    /*Makereadonlyandread/write!!!*/
    if(vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr&0xFFFFF000)+x),PAGE_DEFAULT)==0x0)
    K_PANIC("Error:RemapPageFailed");
    memset((void*)((programHeader[i].phVaddr&0xFFFFF000)+x),0x0,0x1000);
    }

    /*NowLoadSectionToMemory*/
    fseek(tmpFd,programHeader[i].phOffset,0);
    fread((void*)programHeader[i].phVaddr,programHeader[i].phFilesz,1,tmpFd);
    if((programHeader[i].phFlags&0x2)!=0x2){
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    if((vmm_setPageAttributes((programHeader[i].phVaddr&0xFFFFF000)+x,PAGE_PRESENT|PAGE_USER))!=0x0)
    kpanic("Error:vmm_setPageAttributesfailed,File:%s,Line:%i\n",__FILE__,__LINE__);
    }
    }
    kprintf("settingdaddr\n");
    if(binaryHeader->eEntry>=programHeader[i].phVaddr&&binaryHeader->eEntry<(programHeader[i].phVaddr+programHeader[i].phMemsz)){
    /*We'resuposedtodosomethinghere?*/
    }
    else{
    _current->td.vm_dsize=seg_size>>PAGE_SHIFT;
    _current->td.vm_daddr=(char*)seg_addr;
    }

    _current->oInfo.vmStart=((programHeader[i].phVaddr&0xFFFFF000)+0xA900000);
    break;
    casePT_DYNAMIC:
    //newLoc=(char*)programHeader[i].phVaddr;
    elfDynamicS=(elfDynamic*)programHeader[i].phVaddr;
    fseek(tmpFd,programHeader[i].phOffset,0);
    fread((void*)programHeader[i].phVaddr,programHeader[i].phFilesz,1,tmpFd);
    break;
    casePT_INTERP:
    interp=(char*)kmalloc(programHeader[i].phFilesz);
    fseek(tmpFd,programHeader[i].phOffset,0);
    fread((void*)interp,programHeader[i].phFilesz,1,tmpFd);
    kprintf("Interp:[%s]\n",interp);
    ldAddr=ldEnable();
    break;
    default:
    break;
    }
    }

    /*Whatisthisdoing?11/23/06*/
    if(elfDynamicS!=0x0){
    for(i=0;i<12;i++){
    if(elfDynamicS[i].dynVal==0x3){
    tmp=(uInt32*)elfDynamicS[i].dynPtr;
    if(tmp==0x0)
    kpanic("tmp:NULL\n");
    tmp[2]=(uInt32)ldAddr;
    tmp[1]=(uInt32)tmpFd;
    break;
    }
    /*
    else{
    kprintf("dyn_val:%i",elfDynamicS[i].dynVal);
    }
    */
    }
    }

    _current->td.vm_dsize=seg_size>>PAGE_SHIFT;
    _current->td.vm_daddr=(char*)seg_addr;

    argv=ap;

    if(argv[1]!=0x0){
    argc=argv[0];
    args=(char*)vmmGetFreeVirtualPage(_current->id,1,VM_TASK);
    memset(args,0x0,0x1000);
    x=0x0;
    argvNew=(char**)kmalloc(sizeof(char*)*argc);
    for(i=0x0;i<argc;i++){
    strcpy(args+x,argv[i+1]);
    argvNew[i]=args+x;
    x+=strlen(argv[i+1])+1;
    //args[x]='\0';
    //x++;
    }
    argv=argvNew;
    }

    vmm_cleanVirtualSpace(_current->td.vm_daddr+(_current->td.vm_dsize<<PAGE_SIZE));

    iFrame=_current->tss.esp0-sizeof(structi386_frame);
    iFrame->ebp=STACK_ADDR;
    iFrame->eip=binaryHeader->eEntry;
    iFrame->user_esp=STACK_ADDR-12;

    //if(_current->id>3){

    iFrame->user_esp=((u_int32_t)STACK_ADDR)-(sizeof(u_int32_t)*(argc+3));
    tmp=iFrame->user_esp;

    tmp[0]=argc;
    for(i=0;i<argc;i++){
    tmp[i+1]=argv[i];
    }
    tmp[argc+1]=0x0;
    tmp[argc+2]=0x1;
    //}
    //else{
    //tmp=(u_int32_t*)STACK_ADDR-2;
    //tmp[0]=0x1;
    //tmp[1]=0x0;
    //tmp[1]=(u_int32_t)argv;
    //}
    kfree(argvNew);
    /*NowThatWeRelocatedTheBinaryWeCanUnmapAndFreeHeaderInfo*/
    kfree(binaryHeader);
    kfree(programHeader);

    return;
    }

    voidsys_exec(char*file,char*ap){
    interror=0x0;
    inti=0x0;
    intx=0x0;
    intargc=0x0;
    u_int32_tseg_size=0x0;
    u_int32_tseg_addr=0x0;
    u_int32_taddr=0x0;
    u_int32_teip=0x0;
    u_int32_tproghdr=0x0;
    char*args=0x0;
    char*interp=0x0;
    char**argv=0x0;
    char**argvNew=0x0;
    elfHeader*binaryHeader=0x0;
    elfProgramHeader*programHeader=0x0;
    structi386_frame*iFrame=0x0;
    Elf_Auxargs*auxargs=0x0;

    _current->imageFd=fopen(file,"r");
    if(_current->imageFd==0x0)
    return(-1);

    /*LoadtheELFheader*/
    if((binaryHeader=(elfHeader*)kmalloc(sizeof(elfHeader)))==0x0)
    K_PANIC("mallocfailed!");
    fread(binaryHeader,sizeof(elfHeader),1,_current->imageFd);

    /*CheckIfAppIsARealApplication*/
    if(((binaryHeader->eIdent[1]!='E')&&(binaryHeader->eIdent[2]!='L')&&(binaryHeader->eIdent[3]!='F'))||(binaryHeader->eType!=ET_EXEC)){
    kfree(binaryHeader);
    fclose(_current->imageFd);
    return(-1);
    }

    /*LoadTheProgramHeader(s)*/
    if((programHeader=(elfProgramHeader*)kmalloc(sizeof(elfProgramHeader)*binaryHeader->ePhnum))==0x0)
    K_PANIC("mallocfailed!");
    fseek(_current->imageFd,binaryHeader->ePhoff,0);
    fread(programHeader,(sizeof(elfProgramHeader)*binaryHeader->ePhnum),1,_current->imageFd);

    /*LoopThroughTheHeaderAndLoadSectionsWhichNeedToBeLoaded*/
    for(i=0x0;i<binaryHeader->ePhnum;i++){
    switch(programHeader[i].phType){
    casePT_LOAD:
    seg_addr=trunc_page(programHeader[i].phVaddr);
    seg_size=round_page(programHeader[i].phMemsz+programHeader[i].phVaddr-seg_addr);

    /*
    AllocateMemoryImGoingToHaveToMakeThisLoadMemoryWithCorrect
    Settingssoithelpsusinthefuture
    */
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    /*Makereadonlyandread/write!!!*/
    if(vmm_remapPage(vmmFindFreePage(_current->id),((programHeader[i].phVaddr&0xFFFFF000)+x),PAGE_DEFAULT)==0x0)
    K_PANIC("Error:RemapPageFailed");
    memset((void*)((programHeader[i].phVaddr&0xFFFFF000)+x),0x0,0x1000);
    }

    /*NowLoadSectionToMemory*/
    fseek(_current->imageFd,programHeader[i].phOffset,0);
    fread((void*)programHeader[i].phVaddr,programHeader[i].phFilesz,1,_current->imageFd);
    if((programHeader[i].phFlags&0x2)!=0x2){
    for(x=0x0;x<(programHeader[i].phMemsz);x+=0x1000){
    if((vmm_setPageAttributes((programHeader[i].phVaddr&0xFFFFF000)+x,PAGE_PRESENT|PAGE_USER))!=0x0)
    K_PANIC("vmm_setPageAttributesfailed");
    }
    }
    if(binaryHeader->eEntry>=programHeader[i].phVaddr&&binaryHeader->eEntry<(programHeader[i].phVaddr+programHeader[i].phMemsz)){
    /*We'resuposedtodosomethinghere?*/
    }
    else{
    _current->td.vm_dsize=seg_size>>PAGE_SHIFT;
    _current->td.vm_daddr=(char*)seg_addr;
    }

    _current->oInfo.vmStart=((programHeader[i].phVaddr&0xFFFFF000)+0xA900000);
    break;
    casePT_INTERP:
    interp=(char*)kmalloc(programHeader[i].phFilesz);
    if(interp==0x0)
    K_PANIC("mallocfailed");

    fseek(_current->imageFd,programHeader[i].phOffset,0);
    fread((void*)interp,programHeader[i].phFilesz,1,_current->imageFd);
    kprintf("Interp:[%s]\n",interp);
    //ldAddr=ldEnable();
    break;
    casePT_PHDR:
    proghdr=programHeader[i].phVaddr;
    break;
    default:
    break;
    }
    }

    addr=LD_START;

    if(interp!=0x0){
    kprintf("TEST");
    elf_loadfile(_current,interp,&addr,&eip);
    }
    kprintf("[0x%X][0x%X]\n",eip,addr);

    _current->td.vm_dsize=seg_size>>PAGE_SHIFT;
    _current->td.vm_daddr=(char*)seg_addr;

    argv=ap;

    if(argv[1]!=0x0){
    argc=argv[0];
    args=(char*)vmmGetFreeVirtualPage(_current->id,1,VM_TASK);
    memset(args,0x0,0x1000);
    x=0x0;
    argvNew=(char**)kmalloc(sizeof(char*)*argc);
    for(i=0x0;i<argc;i++){
    strcpy(args+x,argv[i+1]);
    argvNew[i]=args+x;
    x+=strlen(argv[i+1])+1;
    }
    argv=argvNew;
    }

    vmm_cleanVirtualSpace(_current->td.vm_daddr+(_current->td.vm_dsize<<PAGE_SIZE));

    iFrame=_current->tss.esp0-sizeof(structi386_frame);
    iFrame->ebp=STACK_ADDR;
    iFrame->eip=eip;

    //if(_current->id>3){

    iFrame->user_esp=((u_int32_t)STACK_ADDR)-(sizeof(u_int32_t)*(argc+3+sizeof(Elf_Auxargs)));
    args=iFrame->user_esp;

    args[0]=argc;
    for(i=0;i<argc;i++){
    args[i+1]=argv[i];
    }
    args[argc+2]=0x0;
    auxargs=iFrame->user_esp+argc+2;
    auxargs->execfd=-1;
    auxargs->phdr=proghdr;
    auxargs->phent=binaryHeader->ePhentsize;
    auxargs->phnum=binaryHeader->ePhnum;
    auxargs->pagesz=PAGE_SIZE;
    auxargs->base=addr;
    auxargs->flags=0x0;
    auxargs->entry=binaryHeader->eEntry;
    auxargs->trace=0x0;
    kprintf("\n\nDOH\n\n");

    //while(1);
    /*
    error=elf_loadfile(_current,file,0x0,0x0);
    if(error)
    K_PANIC("elf_loadfilefailed");
    */
    return;
    }

    /***
    END
    ***/