Newer
Older
ubixos / src / sys / kernel / fork.c
/**************************************************************************************
 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/fork.h>
#include <ubixos/types.h>
#include <ubixos/schedule.h>
#include <vmm/paging.h>


/* Main Fork Function */
pid_t fork() {
  int i=0,x=0;
  uChar *src,*dst;
  uLong *pageDirsrc,*pageTablesrc,*pageDirdst,*pageTabledst;
  pid_t cPid = findTask(); //Find An Empty Task
  schedule();
  src = &_current->tss;
  dst = &taskList[cPid].tss;
  for (i=0;i<sizeof(struct taskStruct);i++) {
    dst[i] = src[i];
    }
  //taskList[cPid].status = AVAILABLE;
  taskList[cPid].id = cPid;
  pageDirsrc = (uLong *)_current->tss.cr3;
  pageDirdst = (uLong *)allocPage();
  //taskList[cPid].tss.cr3 = pageDirdst;
  for (i=0;i<1024;i++) {
    if (pageDirsrc[i] > 0) {
      pageTablesrc = (uLong *)(pageDirsrc[i] & 0xFFFFF000);
      pageDirdst[i] = allocPage() | pageDefault;
      pageTabledst = (uLong *)(pageDirdst[i] & 0xFFFFF000);
      for (x=0;x<1024;x++) {
        if (pageTablesrc[x] > 0) {
/*          if (((pageTablesrc[x] & 0xFFF) == 7) || ((pageTablesrc[x] & 0xFFF) == 103)) {
            kprintf("(%i){%i}",pageTablesrc[x],(pageTablesrc[x] & 0xFFF));
            pageTabledst[x] = pageTablesrc[x];
            }
          else { */
            pageTabledst[x] = (pageTablesrc[x] & 0xFFFFFFBD);//(pageTablesrc[x] & 0xFFFFF000) | (pagePresent|pageUser);// & 0xFFFFFFD;
            if ((pageTablesrc[x] & 0xFFF) == 103) {
              kprintf("[%i]",pageTablesrc[x]);
              pageTabledst[x] = pageTablesrc[x];
              }
//            }
          }
        else {
          pageTabledst[x] = 0x0;
          }
        }
      }
    else {
      pageDirdst[i] = 0x0;
      }
    }
  kprintf("Changing Page Dirs");
  kprintf("Pid-Eh: [%i]\n",cPid);
  kprintf("[%i][%i]\n",_current->status,taskList[cPid].status);
  //taskList[cPid].status = ACTIVE;
  if (_current->id == cPid) {
    return(0);
    }
  else {
    return(cPid);
    }
  }

pid_t testFork(int nr,long ebp,long edi,long esi,long gs,long none,
                long ebx,long ecx,long edx,
                long fs,long es,long ds,
                long cs,long eflags,long esp,long ss) {
  kprintf("nr:     [%i]\n",nr);
  kprintf("ebp:    [%i]\n",ebp);
  kprintf("edi:    [%i]\n",edi);
  kprintf("esi:    [%i]\n",esi);
  kprintf("gs:     [%i]\n",gs);
  kprintf("none:   [%i]\n",none);
  kprintf("ebx:    [%i]\n",ebx);
  kprintf("ecx:    [%i]\n",ecx);
  kprintf("edx:    [%i]\n",edx);
  kprintf("fs:     [%i]\n",fs);
  kprintf("es:     [%i]\n",es);
  kprintf("ds:     [%i]\n",ds);
  kprintf("cs:     [%i]\n",cs);
  kprintf("eflags: [%i]\n",eflags);
  kprintf("esp:    [%i]\n",esp);
  kprintf("ss:     [%i]\n",ss);
  return(-1);
  }

void sysFork() {
  pid_t cPid = findTask();
  uLong *pageDirsrc,*pageTablesrc,*stackSrc,*pageDirdst,*pageTabledst,*stackDst;
  int i,x,*ret;
  uChar *src,*dst;
  asm("":"=b" (ret));
  schedule();
  src = &_current->tss;
  dst = &taskList[cPid].tss;
  for (i=0;i<sizeof(struct taskStruct);i++) {
    dst[i] = src[i];
    }
  taskList[cPid].id = cPid;
  taskList[cPid].status = AVAILABLE;
  pageDirsrc = (uLong *)_current->tss.cr3;
  pageDirdst = (uLong *)allocPage();
  taskList[cPid].tss.cr3 = pageDirdst;
  for (i=0;i<1024;i++) {
    if (pageDirsrc[i] > 0) {
      pageTablesrc = (uLong *)(pageDirsrc[i] & 0xFFFFF000);
      pageDirdst[i] = allocPage() | pageDefault;
      pageTabledst = (uLong *)(pageDirdst[i] & 0xFFFFF000);
      for (x=0;x<1024;x++) {
        if (pageTablesrc[x] > 0) {
          pageTabledst[x] = pageTablesrc[x]; //(pageTablesrc[x] & 0xFFFFFFBD);//(pageTablesrc[x] & 0xFFFFF000) | (pagePresent|pageUser);// & 0xFFFFFFD;
          if ((pageTablesrc[x] & 0xFFF) == 103) {
            kprintf("[%i]",pageTablesrc[x]);
            pageTabledst[x] = pageTablesrc[x];
            }
          }
        else {
          pageTabledst[x] = 0x0;
          }
        }
      }
    else {
      pageDirdst[i] = 0x0;
      }
    }
  i = (_current->tss.esp & 0xFFFFF000)/(1024*4096);
  pageTabledst = (uLong *) (pageDirdst[i] & 0xFFFFF000);
  i = ((_current->tss.esp & 0xFFFFF000) - (((_current->tss.esp & 0xFFFFF000)/(1024*4096))*(1024*4096)))/4096;
  pageTabledst[i] = allocPage() | pageDefault;
  stackDst = (uLong *) (pageTabledst[i] & 0xFFFFF000);
  stackSrc = (uLong *) (_current->tss.esp & 0xFFFFF000);
  for (i=0;i<1024;i++) {
    stackDst[i] = stackSrc[i];
    }
  kprintf("Stack: [%i]\n",(_current->tss.esp & 0xFFFFF000)+0x1000);
  kprintf("Changing Page Dirs");
  schedule();
  taskList[cPid].tss.eip = _current->tss.eip;
  taskList[cPid].status = ACTIVE;
//  taskList[cPid].tss.eip = &&retHere;
//  retHere:
  kprintf("[%i][%i][%i]\n",ret,cPid,_current->id);
  if (_current->id == cPid) {
    ret[0] = 0;
    //asm("movl $0,%ebx");
    }
  else {
    ret[0] = cPid;
    //asm("movl %0,%%ebx"::"g" (cPid));
    }
  }

/*
asm(
  ".global sysFork \n"
  "sysFork:        \n"
  "  pushl %ss     \n"
  "  pushl %esp    \n"
  "  pushf         \n"
  "  pushl %cs     \n"
  "  pushl %ds     \n"
  "  pushl %es     \n"
  "  pushl %fs     \n"
  "  pushl %edx    \n"
  "  pushl %ecx    \n"
  "  pushl %ebx    \n"
  "  pushl $3      \n"
  "  pushl %gs     \n"
  "  pushl %esi    \n"
  "  pushl %edi    \n"
  "  pushl %ebp    \n"
  "  pushl $1      \n"
  "  call testFork \n"
  );
*/