/*****************************************************************************************
 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 <ubixos/endtask.h>
#include <ubixos/vitals.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>
#include <assert.h>

extern union descriptorTableUnion ubixGDT[5];

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

static prioQueue_t *prioLevels[MAXPRIOLEVELS - 1];
static prioQueue_t *prioQStart = 0x0;
static prioQueue_t *prioQEnd = 0x0;
static prioQueue_t *_currentQueue = 0x0;

static kTask_t *taskList = 0x0;
static uInt32  nextID    = -1;

kTask_t *_current = 0x0;
kTask_t *_usedMath = 0x0;



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

Function: int sched_init()

Description: This function is used to enable the kernel scheduler

Notes:

02/20/2004 - Approved for quality

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


int 
sched_init() 
{        
        int i;

        for (i = 0; i < MAXPRIOLEVELS; i++) {
                prioQueue_t *tmpPrioQ;
                kTask_t *tmpTask;
                
                tmpPrioQ = (prioQueue_t *)kmalloc(sizeof(prioQueue_t));
                tmpTask = (kTask_t *)kmalloc(sizeof(kTask_t));

                if (tmpPrioQ == 0x0) {
                        kpanic("Error: schedInit() - kmalloc failed trying to initialize a prioQueue_t struct\n");
                        return(0x1);
                }
                
                if (tmpTask == 0x0) {
                        kpanic("Error: schedInit() - kmalloc failed trying to initialize a kTask_t struct\n");
                        return(0x1);
                }
                
                tmpPrioQ->start = tmpTask;
                tmpPrioQ->end   = tmpTask;
                tmpPrioQ->prio  = i;
                prioLevels[i]   = tmpPrioQ;

                tmpTask->prev = 0x0;
                tmpTask->next = 0x0;
                tmpTask->id   = -1;
                tmpTask->state = PLACEHOLDER;
                tmpTask->nice  = i;
                tmpTask->timeSlice = systemVitals->dQuantum;

                /* Linking the previous placeholder with the current one */
                if (i > 0) {
                        prioLevels[i - 1]->start->next = tmpTask;
                        tmpTask->prev = prioLevels[i - 1]->end;
                }

                if (0x0 == prioQStart) {
                        tmpPrioQ->next = 0x0;
                        tmpPrioQ->prev = 0x0;
                        prioQStart = prioQEnd = tmpPrioQ;
                }  else {
                        tmpPrioQ->prev = prioQEnd;
                        prioQEnd->next = tmpPrioQ;
                        tmpPrioQ->next = 0x0;
                        prioQEnd = tmpPrioQ;
                }
        }

        taskList = prioLevels[0]->start;
        _current = prioLevels[MAXPRIOLEVELS/2]->start;
        _currentQueue = prioLevels[0];

        nextID++;

        /* Print out information on scheduler */
        kprintf("sched0 solar 0.2.1 - Address: [0x%X]\n", prioQStart);
        
        /* Return so we know everything went well */
        return(0x0); 
}


void sched(){
  uInt32 memAddr   = 0x0;
  kTask_t *tmpTask = 0x0;

  schedStart:
  /* Yield the next task from the current prio queue */
  for (tmpTask = _current->next; tmpTask; tmpTask = tmpTask->next) {
    if (tmpTask->state > 0x0) {
      _current = tmpTask;
      if (_current->state == FORK)
        _current->state = READY;

      break;
      }
/*
		else if (tmpTask->state == DEAD) {
                  if (tmpTask == _current) 
                    kpanic("unhandled situation");
		  if (schedDeleteTask(tmpTask->id) != 0x0) {
                    kpanic("Error: schedDeleteTask");;
                    }
		  goto schedStart;
                  }
*/
    }

        /* Finished all the tasks, restarting the list */
        if (0x0 == tmpTask) {
                _current = taskList;
                goto schedStart;
        }

        /* Setting the timeslice this task is allowed to run */
        systemVitals->quantum = _current->timeSlice;
        
        
        if (_current->state > 0x0) {
                if (_current->oInfo.v86Task == 0x1)
                        irqDisable(0x0);
	        asm("cli");
                memAddr = (uInt32)&(_current->tss);
                ubixGDT[4].descriptor.baseLow  = (memAddr & 0xFFFF);
                ubixGDT[4].descriptor.baseMed  = ((memAddr >> 16) & 0xFF);
                ubixGDT[4].descriptor.baseHigh = (memAddr >> 24);
                ubixGDT[4].descriptor.access   = '\x89';
                asm("ljmp $0x20,$0\n");
        }

        return;
}


kTask_t *
schedNewTask() 
{
        kTask_t *tmpTask  = (kTask_t *)kmalloc(sizeof(kTask_t));

        if (tmpTask == 0x0) {
                kpanic("Error: schedNewTask() - kmalloc failed trying to initialize a new task struct\n");
                return(0x0);
        }

        //spinLock(&schedTaskSpinLock);
        
        /* Filling in tasks attrs */
        tmpTask->usedMath   = 0x0;
        tmpTask->id         = ++nextID;
        tmpTask->state      = NEW;
        tmpTask->nice       = _current->nice;

        tmpTask->timeSlice  = systemVitals->dQuantum * tmpTask->nice;
        tmpTask->oInfo.cwd  = (char *)kmalloc(1024);
	assert(tmpTask->oInfo.cwd);

        /* Are we adding a task at the end of the last prio queue? */
        if (0x0 == prioLevels[_current->nice]->next)
                tmpTask->next = 0x0;
        else
                tmpTask->next = prioLevels[_current->nice + 1]->start;

        tmpTask->prev = prioLevels[_current->nice]->end;

        /* 
         * Inserting the task at the end of the proper prio queue; 
         * this gives me the feeling of O(1) for task insertion 
         */
        prioLevels[_current->nice]->end->next = tmpTask;
        prioLevels[_current->nice]->end = tmpTask;
      
        //spinUnlock(&schedTaskSpinLock);
        return(tmpTask);
}


/*
 * This is at least O(N)... it was written to 
 * have it functional; soon it will be changed to O(1).
 */

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

  /* Checking each task from the prio queue */
  for (tmpTask = taskList; tmpTask != 0x0; tmpTask = tmpTask->next) {
   if (tmpTask->id == id) {
     if (_current == tmpTask) 
       kpanic("you can not delete yourself!!!\n");
     if (tmpTask == taskList)
       kpanic("Whoa");
     if (tmpTask->prev)
       tmpTask->prev->next = tmpTask->next;
     if (tmpTask->next)
       tmpTask->next->prev = tmpTask->prev;
     kfree(tmpTask->oInfo.cwd);
     kfree(tmpTask);
     return(0x0);
     }
   }
  return(0x1);
  }


kTask_t * 
schedFindTask(uInt32 id) 
{
        kTask_t *tmpTask = 0x0; 

        for (tmpTask = taskList; tmpTask; tmpTask = tmpTask->next) {
                if (tmpTask->id == id)
                        return(tmpTask);
        }

        return(0x0);
}


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

 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();
  }

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

Function: int sched_setStatus(pidType pid,tState state)

Description: Change the tasks status

Notes:

************************************************************************/
int sched_setStatus(pidType pid,tState state) {
  kTask_t *tmpTask = schedFindTask(pid);
  if (tmpTask == 0x0)
    return(0x1);
  tmpTask->state = state;
  return(0x0);
  }

/***
 $Log$
 Revision 1.26  2004/08/02 18:50:13  reddawg
 Updates to make some variable volatile to make work with gcc 3.3. However there are still some issues but we have not caused new issues with gcc 2.95

 Revision 1.25  2004/08/01 20:24:51  reddawg
 Fixens

 Revision 1.24  2004/08/01 17:50:46  reddawg
 Fixens

 Revision 1.23  2004/07/29 21:32:16  reddawg
 My quick lunchs breaks worth of updates....

 Revision 1.22  2004/07/28 16:29:30  reddawg
 old sched back

 Revision 1.20  2004/07/25 06:04:00  reddawg
 Last of my fixes for the morning

 Revision 1.19  2004/07/24 15:12:56  reddawg
 Now I'm current

 Revision 1.18  2004/07/21 10:02:09  reddawg
 devfs: renamed functions
 device system: renamed functions
 fdc: fixed a few potential bugs and cleaned up some unused variables
 strol: fixed definition
 endtask: made it print out freepage debug info
 kmalloc: fixed a huge memory leak we had some unhandled descriptor insertion so some descriptors were lost
 ld: fixed a pointer conversion
 file: cleaned up a few unused variables
 sched: broke task deletion
 kprintf: fixed ogPrintf definition

 Revision 1.17  2004/07/20 18:58:24  reddawg
 Few fixes

 Revision 1.16  2004/07/20 18:51:00  reddawg
 sched: hold that was my error

 Revision 1.15  2004/07/19 02:32:21  reddawg
 sched: we now set task status to dead which then makes the scheduler do some clean it could be some minor overhead but i feel this is our most efficient approach right now to prevent corruption of the queues

 Revision 1.14  2004/07/19 01:56:31  reddawg
 schedDeleteTask: I am pondering the possability that this could not finish in a quantum....

 Revision 1.13  2004/07/18 05:24:15  reddawg
 Fixens

 Revision 1.12  2004/07/09 13:23:20  reddawg
 sched: schedInit to sched_init
 Adjusted initialization routines

 Revision 1.11  2004/06/22 14:03:10  solar
 Making the task queue with priorities functional

 Revision 1.10  2004/06/18 13:05:47  solar
 Changed the scheduler by adding the first rudiments of priority; vitals.c initializes the system's default quantum to 40

 Revision 1.9  2004/06/04 13:29:56  reddawg
 libc: modified mkdir(); interface
 kpanic: kPanic(); now says kPanic: %s
 system: now reboots when receives message for reboot
         also when command start sde is received by system the STD is started

 Revision 1.8  2004/05/25 16:10:46  reddawg
 Spin Locks

 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
 ***/
