/**************************************************************************************
 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: fork.c,v 1.11 2003/04/22 13:51:46 reddawg Exp $

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

#include <ubixos/fork.h>
#include <ubixos/types.h>
#include <ubixos/schedule.h>
#include <vmm/paging.h>


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

Function: void sysFork();
Description: This Function Forks A Task
Notes:

08/01/02 - This Seems To Be Working Fine However I'm Not Sure If I
           Chose The Best Path To Impliment It I Guess We Will See
           What The Future May Bring

************************************************************************/
void sysFork();
__asm__(
  ".global sysFork    \n"
  "sysFork:           \n"
  "  xor %eax,%eax    \n"
  "  call findTask    \n"
  "  testl %eax,%eax  \n"
  "  je 1f            \n"
  "  push  %gs        \n"
  "  pushl %esi       \n"
  "  pushl %edi       \n"
  "  pushl %ebp       \n"
  "  pushl %eax       \n"
  "  call copyProcess \n"
  "  movl %eax,(%ebx) \n"
  "  popl %eax        \n"
  "  popl %ebp        \n"
  "  popl %edi        \n"
  "  popl %esi        \n"
  "  pop  %gs         \n"
  "1:                 \n"
  "  ret              \n"
  );
  
int copyProcess(struct taskStruct *newProcess,long ebp,long edi,long esi,long gs,long none,long ebx,long ecx,long edx,long fs,long es,long ds,long eip,long cs,long eflags,long esp,long ss) {
  //Set Up New Tasks Information
  newProcess->uid              = _current->uid;
  newProcess->gid              = _current->gid;
  newProcess->tss.back_link    = 0x0;
  newProcess->tss.esp0         = _current->tss.esp0;
  newProcess->tss.ss0          = 0x10;
  newProcess->tss.esp1         = 0x0;
  newProcess->tss.ss1          = 0x0;
  newProcess->tss.esp2         = 0x0;
  newProcess->tss.ss2          = 0x0;
  newProcess->tss.eflags       = eflags;
  newProcess->tss.eax          = 0x0;
  newProcess->tss.ebx          = ebx;
  newProcess->tss.ecx          = ecx;
  newProcess->tss.edx          = edx;
  newProcess->tss.esi          = esi;
  newProcess->tss.edi          = edi;
  newProcess->tss.ebp          = ebp;
  newProcess->tss.esp          = esp;
  newProcess->tss.es           = es & 0xFF;
  newProcess->tss.cs           = cs & 0xFF;
  newProcess->tss.ss           = ss & 0xFF;
  newProcess->tss.ds           = ds & 0xFF;
  newProcess->tss.fs           = fs & 0xFF;
  newProcess->tss.gs           = gs & 0xFF;
  newProcess->tss.ldt          = 0x18;
  newProcess->tss.trace_bitmap = 0x0000;
  newProcess->tss.io_map       = 0x8000;
  newProcess->tss.eip          = eip;
  //Create A Copy Of The VM Space For New Task
  newProcess->tss.cr3 = (long)copyVirtualSpace(newProcess->id);
  newProcess->oInfo.curDir     = _current->oInfo.curDir;
  newProcess->oInfo.container  = _current->oInfo.container;  
  setTaskStatus(newProcess->id, READY);
  //Return
  return(newProcess->id);
  }

