/*****************************************************************************************
 Copyright (c) 2002-2004 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/sched.h>
#include <ubixos/kpanic.h>
#include <ubixos/spinlock.h>
#include <vfs/mount.h>
#include <lib/kmalloc.h>
#include <lib/kprintf.h>
#include <vmm/vmm.h>
#include <sys/gdt.h>
#include <sys/idt.h>
#include <isa/8259.h>

extern union descriptorTableUnion ubixGDT[5];

static spinLock_t schedSpinLock = SPIN_LOCK_INITIALIZER;
static spinLock_t schedTaskSpinLock = SPIN_LOCK_INITIALIZER;

kTask_t *taskList = 0x0;
uInt32  nextID    = -1;
kTask_t *_current = 0x0;
kTask_t *_usedMath = 0x0;

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

Function: int schedInit()

Description: This function is used to enable the kernel scheduler

Notes:

02/20/2004 - Approved for quality

************************************************************************/
int schedInit() {

  /* Allocate memory for task list */
  taskList        = (kTask_t *)kmalloc(sizeof(kTask_t));
  if (taskList == 0x0) {
    kpanic("Error: kmalloc failed for taskList\n");
    return(0x1);
    }

  /* Set up the defaults for the initial task. */
  taskList->prev  = 0x0;
  taskList->next  = 0x0;
  taskList->id    = nextID++;
  taskList->state = DEAD;
  _current        = taskList;

  /* Print out information on scheduler */
  kprintf("sched0 - Address: [0x%X]\n",taskList);

  /* Return so we know everything went well */
  return(0x0); 
  }

void sched() {
  uInt32 memAddr   = 0x0;
  kTask_t *tmpTask = 0x0;
  struct tssStruct *gpfTSS = (struct tssStruct *)0x4200;

  spinLock(&schedSpinLock);

  gpfTSS->eip            = (unsigned int)&_int13;
  gpfTSS->esp            = 0x1CFFF;
  gpfTSS->ebp            = 0x1CFFF;
  gpfTSS->eflags         = 0x206;


schedStart:
  for (tmpTask=_current->next;tmpTask;tmpTask=tmpTask->next) {

    if (tmpTask->state > 0x0) {
      _current = tmpTask;
      break;
      } /* if tmpTask->state > 0x0 */
    } /* for */

  if (0x0 == tmpTask) {
     _current = taskList;
    goto schedStart;
    } /* if 0x0 == tmpTask */

  memAddr = (uInt32)&(_current->tss);

  if (_current->state > 0x0) {
    if (_current->oInfo.v86Task == 0x1) {
      //kprintf("v86Task\n");
      irqDisable(0x0);
      }
    ubixGDT[4].descriptor.baseLow  = (memAddr & 0xFFFF);
    ubixGDT[4].descriptor.baseMed  = ((memAddr >> 16) & 0xFF);
    ubixGDT[4].descriptor.baseHigh = (memAddr >> 24);
    ubixGDT[4].descriptor.access   = '\x89';
    spinUnlock(&schedSpinLock);
    asm("ljmp $0x20,$0\n");
    }
  spinUnlock(&schedSpinLock);
  return;
  } /* sched() */

kTask_t *schedNewTask() {
  kTask_t *tmpTask  = (kTask_t *)kmalloc(sizeof(kTask_t));
  kTask_t *listLoop = 0x0;

  spinLock(&schedTaskSpinLock);

  for (listLoop = taskList; listLoop; listLoop = listLoop->next) {

    if (0x0 == listLoop->next) {
      tmpTask->prev = listLoop;
      tmpTask->next = 0x0;
      listLoop->next = tmpTask;
      break;
      } /* if listLoop->next == 0x0 */

    }  /* for */

  tmpTask->usedMath = 0x0;
  tmpTask->id       = nextID++;
  tmpTask->state    = NEW;
  //tmpTask->oInfo.container = findMount("s");
  tmpTask->oInfo.cwd      = (char *)kmalloc(1024);

  spinUnlock(&schedTaskSpinLock);
  return(tmpTask);
  } /* schedNewTask() */

int schedDeleteTask(uInt32 id) {
  kTask_t *tmpTask = 0x0; 
 
  spinLock(&schedTaskSpinLock);

  for (tmpTask = taskList; tmpTask; tmpTask=tmpTask->next) {

    if (tmpTask->id == id) {

      tmpTask->prev->next = tmpTask->next;
      tmpTask->next->prev = tmpTask->prev;

      if (_current == tmpTask) {
        _current = taskList;
        } /* if _current == tmpTask */

      kfree(tmpTask);

      } /* if tmpTask-> == id */
    } /* for */

  spinUnlock(&schedTaskSpinLock);
  return(0);
  } /* deleteTask() */


kTask_t *schedFindTask(uInt32 id) {
  kTask_t *tmpTask = 0x0;
  for (tmpTask = taskList; tmpTask != 0x0; tmpTask = tmpTask->next) {

    if (tmpTask->id == id) {
      return(tmpTask);
      } /* if tmpTask-> == id */

    } /* for */
  return(0x0);
  } /* findTask() */

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

 Function: void schedEndTask()

 Description: This function will end a task

 Notes:

  02/20/2004 - Approved for quality

************************************************************************/
void schedEndTask(pidType pid) {
  endTask(_current->id);
  schedYield();
  }

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

Function: int schedEndTask()

Description: This function will yield a task 

Notes:

02/20/2004 - Approved for quality

************************************************************************/
void schedYield() {
  sched();
  }

/***
 $Log$
 Revision 1.7  2004/05/21 21:15:04  reddawg
 Fixed a few bugs which prevented the system from loadin

 Revision 1.6  2004/05/21 15:49:13  reddawg
 The os does better housekeeping now when a task is exited

 Revision 1.5  2004/05/21 12:44:17  reddawg
 Cleaned Up

 Revision 1.4  2004/05/19 04:07:43  reddawg
 kmalloc(size,pid) no more it is no kmalloc(size); the way it should of been

 Revision 1.3  2004/05/15 02:30:28  reddawg
 Lots of changes


 END
 ***/

