/**************************************************************************************
 Copyright (c) 2002
      The UbixOS Project

 $Id: exec.c,v 1.8 2002/04/27 12:21:54 reddawg Exp $
**************************************************************************************/

#include <ubixfs/ubixfs.h>
#include <ubixos/schedule.h>
#include <mm/paging.h>
#include <ubixos/video.h>

void execThread(void (* tproc)(void),int stack,char *descr) {
  int pid = 0;
  pid = findTask();
  taskList[pid].tss.back_link    = 0x0;
  taskList[pid].tss.esp0         = stack;
  taskList[pid].tss.ss0          = 0x10;
  taskList[pid].tss.esp1         = 0x0;
  taskList[pid].tss.ss1          = 0x10;
  taskList[pid].tss.esp2         = 0x0;
  taskList[pid].tss.ss2          = 0x10;
  taskList[pid].tss.cr3          = (unsigned int)pageDir;
  taskList[pid].tss.eip          = (unsigned int)tproc;
  taskList[pid].tss.eflags       = 0x206;
  taskList[pid].tss.esp          = stack;
  taskList[pid].tss.ebp          = stack;
  taskList[pid].tss.esi          = 0x0;
  taskList[pid].tss.edi          = 0x0;
  taskList[pid].tss.es           = 0x10;
  taskList[pid].tss.cs           = 0x08;
  taskList[pid].tss.ss           = 0x10;
  taskList[pid].tss.ds           = 0x10;
  taskList[pid].tss.fs           = 0x10;
  taskList[pid].tss.gs           = 0x10;
  taskList[pid].tss.ldt          = 0x18;
  taskList[pid].tss.trace_bitmap = 0x80000000;
  taskList[pid].status = RUNABLE;
  }

void execFile(char *file) {
  int result,e_start,e_offset,i,pid;
  char *buf = 0x0,*BINARY = 0x0;
  pid = findTask();
  result = getdata(file,0,49152,buf);
  if (result > 0) {
    if (buf[1] == 'E' && buf[2] == 'L' && buf[3] == 'F') {
      e_start = 0;
      e_offset = 0;
      e_start += buf[27]* 256 * 256 * 256;
      e_start += buf[26] * 256 * 256;
      e_start += buf[25]* 256;
      e_start += buf[24];
      e_offset += buf[63] * 256 * 256 * 256;
      e_offset += buf[62] * 256 * 256;
      e_offset += buf[61] * 256;
      e_offset += buf[60];
      (int)BINARY = e_offset;
      for (i=0;i<result;i++) {
        BINARY[i] = buf[i];
        }
      taskList[pid].tss.back_link    = 0x0;
      taskList[pid].tss.esp0         = 0x300000;
      taskList[pid].tss.ss0          = 0x10;
      taskList[pid].tss.esp1         = 0x0;
      taskList[pid].tss.ss1          = 0x10;
      taskList[pid].tss.esp2         = 0x0;
      taskList[pid].tss.ss2          = 0x10;
      taskList[pid].tss.cr3          = (unsigned int)pageDir;
      taskList[pid].tss.eip          = (unsigned int)e_start;
      taskList[pid].tss.eflags       = 0x206;
      taskList[pid].tss.esp          = 0x300000;
      taskList[pid].tss.ebp          = 0x300000;
      taskList[pid].tss.esi          = 0x0;
      taskList[pid].tss.edi          = 0x0;
      taskList[pid].tss.es           = 0x10;
      taskList[pid].tss.cs           = 0x08;
      taskList[pid].tss.ss           = 0x10;
      taskList[pid].tss.ds           = 0x10;
      taskList[pid].tss.fs           = 0x10;
      taskList[pid].tss.gs           = 0x10;
      taskList[pid].tss.ldt          = 0x18;
      taskList[pid].tss.trace_bitmap = 0x80000000;
      taskList[pid].status = RUNABLE;
      }
    }
  }
