Newer
Older
ubixos / src / sys / kernel / exec.c
@reddawg reddawg on 23 Sep 2002 12 KB Semi Wokring
/**************************************************************************************
 Copyright (c) 2002 The UbixOS Project
 All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

Redistributions of source code must retain the above copyright notice, this list of conditions, the following disclaimer and the list of authors.
Redistributions in binary form must reproduce the above copyright notice, this list of conditions, the following disclaimer and the list of authors
in the documentation and/or other materials provided with the distribution. Neither the name of the UbixOS Project nor the names of its
contributors may be used to endorse or promote products derived from this software without specific prior written permission.

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 THE COPYRIGHT OWNER OR CONTRIBUTORS 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.

 $Id$

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

#include <ubixos/exec.h>
#include <ubixos/schedule.h>
#include <ubixos/elf.h>
#include <vmm/paging.h>
#include <vmm/memory.h>
#include <ubixfs/file.h>
#include <drivers/video.h>

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

Function: void execFile(char *file);
Description: This Function Executes A Kile Into A New VM Space With Out
             Having To Fork
Notes:

07/30/02 - I Have Made Some Heavy Changes To This As Well As Fixed A Few
           Memory Leaks The Memory Allocated To Load The Binary Into Is
           Now Unmapped So It Can Be Used Again And Not Held Onto Until
           The Program Exits

07/30/02 - Now I Have To Make A Better Memory Allocator So We Can Set Up
           The Freshly Allocated Pages With The Correct Permissions

************************************************************************/
void execFile(char *file) {
  int i=0,x=0,eStart=0;
  char *binarySpace = (char *)0x7C0000;
  char *newLoc;
  fileDescriptor *tmpFd = 0x0;
  elfHeader *binaryHeader = (elfHeader *)0x7C0000;
  elfProgramheader *programHeader;
  //Get A New Task For This Proccess
  _current = findTask();
  //Now We Must Create A Virtual Space For This Proccess To Run In
  _current->tss.cr3 = (uLong)createVirtualSpace(_current->id);
  //To Better Load This Application We Will Switch Over To Its VM Space
  asm(
    "movl %0,%%eax          \n"
    "movl %%eax,%%cr3       \n"
    : : "d" ((uLong *)(_current->tss.cr3))
    );
  //Lets Find The File
  tmpFd = fopen(file,"r");
  //If We Dont Find the File Return
  if (!tmpFd) {
    return;
    }
  //Now We Must Allocate Memory To Load The Binary Into
  for (i=0;i<((tmpFd->size+4095)/4096);i++) {
    remapPage(findFreePage(_current->id),(0x7C0000 + (0x1000 * i)));
    }
  //Load The Binary Into Memory Byte For Byte I Should Find A Faster Way
  for (i=0;feof(tmpFd) == 0;i++) {
    binarySpace[i] = fgetc(tmpFd);
    }    
  //Close The File
  fclose(tmpFd);
  //Set programHeader To Point To Loaded Binary So We Can Gather Info
  programHeader = (elfProgramheader *)(0x7C0000 + binaryHeader->ePhoff);
  //Loop Through The Header And Load Sections Which Need To Be Loaded
  for (i=0;i<binaryHeader->ePhnum;i++) {
    newLoc = (char *)programHeader[i].phVaddr;
    /*
    Allocate Memory Im Going To Have To Make This Load Memory With Corrent
    Settings so it helps us in the future
    */
    for (x=0;x<=((programHeader[i].phMemsz & 0xFFFFF000)+4095);x+=4096) {
      remapPage(findFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x)));
      _current->vmStart = ((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x) + 0x1000);
      }
    //Now Copy The Binary To Its Correct Location
    for (x=0;x<programHeader[i].phFilesz;x++) {
      newLoc[x] = binarySpace[(programHeader[i].phOffset+x)];
      }
    }
  //Get The Starting Point
  eStart = binaryHeader->eEntry;
  //Now That We Relocated The Binary We Can Unmap And Free Old Pages
  for (i=0;i<((tmpFd->size+4095)/4096);i++) {
    unmapPage((0x7C0000 + (0x1000 * i)),0);
    }
  //Now Lets Make A Clean Stack
  newLoc = (char *)0x5DB000;
  remapPage(findFreePage(_current->id),0x5DB000);
  setPageAttribute(0x5DB000,(pageDefault | pageStack));  
  remapPage(findFreePage(_current->id),0x5DC000);
  setPageAttribute(0x5DC000,(pageDefault | pageStack));
  for (i=0;i<(4096 * 2);i++) {
    newLoc[i] = 0x0;
    }
  i = 0x5DC000+4095;
  //Set All The Proper Information For The Task
  _current->tss.back_link    = 0x0;
  _current->tss.esp0         = 0x5000;
  _current->tss.ss0          = 0x10;
  _current->tss.esp1         = 0x4000;
  _current->tss.ss1          = 0x10;
  _current->tss.esp2         = 0x3000;
  _current->tss.ss2          = 0x10;
  _current->tss.eip          = (long)eStart;
  _current->tss.eflags       = 0x206;
  _current->tss.esp          = i;
  _current->tss.ebp          = i;
  _current->tss.esi          = 0x0;
  _current->tss.edi          = 0x0;
  _current->tss.es           = 0x10;
  _current->tss.cs           = 0x08;
  _current->tss.ss           = 0x10;
  _current->tss.ds           = 0x10;
  _current->tss.fs           = 0x10;
  _current->tss.gs           = 0x10;
  _current->tss.ldt          = 0x18;
  _current->tss.trace_bitmap = 0x80000000;
  _current->status = READY;
  //Switch Back To The Kernels VM Space
  asm(
    "movl %0,%%eax          \n"
    "movl %%eax,%%cr3       \n"
    : : "d" ((uLong *)(kernelPageDirectory))
    );
  //kprintf("Temp: [%i](%i)\n",getTask(0),getTask(0)->status);
  //while(1);
  //Finally Return
  return;
  }

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

Function: void execThread(void (* tproc)(void),int stack,char *descr);
Description: This Function Executes A New Thread For Kernel And Does
             Not Create Its Own VM Space
Notes:

************************************************************************/  
void execThread(void (* tproc)(void),int stack,char *descr) {
  kTask_t * newProcess;
  //Find A New Thread
  newProcess = findTask();
  //kprintf("newProcess: [%i](%i)\n",newProcess,newProcess->status);
  //Set All The Correct Thread Attributes
  newProcess->tss.back_link    = 0x0;
  newProcess->tss.esp0         = 0x5000;
  newProcess->tss.ss0          = 0x10;
  newProcess->tss.esp1         = 0x4000;
  newProcess->tss.ss1          = 0x10;
  newProcess->tss.esp2         = 0x3000;
  newProcess->tss.ss2          = 0x10;
  newProcess->tss.cr3          = (unsigned int)kernelPageDirectory;
  newProcess->tss.eip          = (unsigned int)tproc;
  newProcess->tss.eflags       = 0x206;
  newProcess->tss.esp          = stack;
  newProcess->tss.ebp          = stack;
  newProcess->tss.esi          = 0x0;
  newProcess->tss.edi          = 0x0;
  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 = 0x80000000;
  newProcess->status = READY;
  //kprintf("newProcess: [%i](%i){%i}\n",newProcess,newProcess->status,newProcess->id);
  //newProcess = getTask(0);
  //kprintf("newProcess: [%i](%i){%i}\n",newProcess,newProcess->status,newProcess->id);  
  //kprintf("Temp: [%i](%i)\n",getTask(0),getTask(0)->status);
  //while(1);
  //Return
  return;
  }

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

Function: void sysExec();
Description: This Is The System Call To Execute A New Task
Notes:

************************************************************************/  
void sysExec() {
  int i = 0,x = 0,y = 0x0;
  int *status       = 0x0;
  int fdSize        = 0x0;
  uLong eStart      = 0x0;
  char *binarySpace = (char *)0x7C0000;
  char *librarySpace = (char *)0x6400000;
  char *newLoc      = 0x0;
  char *file        = 0x0;
  char *binDynStr      = 0x0;
  char *libDynStr   = 0x0;
  
  fileDescriptor *tmpFd = 0x0;
  fileDescriptor *libFd = 0x0;

  elfHeader        *binaryHeader     = (elfHeader *)0x7C0000;
  elfHeader        *libraryHeader    = (elfHeader *)0x6400000;  
  elfProgramheader *programHeader    = 0x0;
  elfSectionheader *sectionHeader    = 0x0;
  elfProgramheader *libProgramHeader = 0x0;
  elfSectionheader *libSectionHeader = 0x0;
  elfDynsym        *libDynSym        = 0x0;
  elfDynsym        *binDynSym        = 0x0;
  
  asm("":"=b"(file),"=c"(status));
  asm("sti");
  tmpFd = fopen(file,"r");
  //If We Dont Find the File Return
  if (tmpFd == 0x0) {
    *status = 0;
    return;
    }
  //Now We Must Allocate Memory To Load The Binary Into
  for (i=0;i<((tmpFd->size+4095)/4096);i++) {
    remapPage(findFreePage(_current->id),(0x7C0000 + (0x1000 * i)));
    }
  //Load The Binary Into Memory Byte For Byte I Should Find A Faster Way
  for (i=0;feof(tmpFd) == 0;i++) {
    binarySpace[i] = fgetc(tmpFd);
    fdSize = i;
    }
  //Close The File
  fclose(tmpFd);
  //Set programHeader To Point To Loaded Binary So We Can Gather Info
  programHeader = (elfProgramheader *)(0x7C0000 + binaryHeader->ePhoff);
  //Set sectionHeader To Point To Loaded Binary To We Can Gather Info
  sectionHeader = (elfSectionheader *)(0x7C0000 + binaryHeader->eShoff);
  //Loop Through The Header And Load Sections Which Need To Be Loaded
  for (i=0;i<binaryHeader->ePhnum;i++) {
    if (programHeader[i].phType == 1) {
      newLoc = (char *)programHeader[i].phVaddr;
      /*
      Allocate Memory Im Going To Have To Make This Load Memory With Corrent
      Settings so it helps us in the future
      */
      for (x=0;x<=((programHeader[i].phMemsz & 0xFFFFF000)+4095);x+=4096) {
        remapPage(findFreePage(_current->id),((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x)));
        _current->vmStart = ((programHeader[i].phVaddr & 0xFFFFF000) + (0x1000 * x) + 0x1000);
        }
      //Now Copy The Binary To Its Correct Location
      for (x=0;x<programHeader[i].phFilesz;x++) {
        newLoc[x] = binarySpace[(programHeader[i].phOffset+x)];
        }
      }
    else if (programHeader[i].phType == 3) {
      kprintf("Sorry Dynamicly Linked Applications Are Not Supported!\n");
      return;
    /*
      binDynStr = (void *)binarySpace + sectionHeader[4].shOffset;
      binDynSym = (void *)binarySpace + sectionHeader[3].shOffset;
      libFd = fopen("libc.so","r");
      for (x=0;x<((libFd->size+4095)/4096);x++) {
        remapPage(findFreePage(_current->id),((int)(void *)librarySpace + (0x1000 * x)));
        }
      for (x=0;feof(libFd) == 0;x++) {
        librarySpace[x] = fgetc(libFd);
        }
      kprintf("Size: [%i]\n",x);
      fclose(libFd);
      libProgramHeader = (void *)((void *)librarySpace + libraryHeader->ePhoff);
      libSectionHeader = (void *)((void *)librarySpace + libraryHeader->eShoff);
      kprintf("Interpreter: [%s] Required.\n",(char *)(binarySpace + programHeader[i].phOffset));
      kprintf("Library [%s] Required.\n",(char *)(binDynStr + 1));
      libDynStr = (void *)((void *)librarySpace + libSectionHeader[3].shOffset);
      libDynSym = (void *)((void *)librarySpace + libSectionHeader[2].shOffset);
      for (x=0;x<(libSectionHeader[2].shSize/sizeof(elfDynsym));x++) {
        if (libDynSym[x].dynValue > 0x0) {
          libDynSym[x].dynValue += (int)(void *)librarySpace;
          }
        }
      for (x=0;x<(sectionHeader[3].shSize/sizeof(elfDynsym));x++) {
        if (binDynSym[x].dynInfo == 0x12) {
          kprintf("Relocating: [%s]",(void *)binDynStr+binDynSym[x].dynName);
          for (y=0;y<(libSectionHeader[2].shSize/sizeof(elfDynsym));y++) {
            if (!kstrcmp((void *)binDynStr+binDynSym[x].dynName,(void *)libDynStr+libDynSym[y].dynName)) {
              binDynSym[x].dynValue=libDynSym[y].dynValue;
              kprintf("Got A Match\n");
              }
            }
          }
        }
    */
      }
    }
  //Get The Starting Point
  eStart = binaryHeader->eEntry;
  //Now That We Relocated The Binary We Can Unmap And Free Old Pages
  for (i=0;i<((fdSize+4095)/4096);i++) {
    unmapPage((0x7C0000 + (0x1000 * i)),0);
    }
  //Set Up Args
  //argv =
  //Jump To Start Of New Binary
  asm(
    "pushl %0\n"
    "jmp  *%1\n"
    :
    : "g" (file), "g" (eStart)
    );
  }