Newer
Older
ubixos / src / sys / kernel / schedule.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/schedule.h>
#include <ubixos/kmalloc.h>
#include <ubixos/io.h>
#include <ubixos/gdt.h>
#include <ubixos/idt.h>
#include <drivers/8259.h>
#include <drivers/video.h>
#include <ubixos/spinlock.h>

#ifndef CURRENT_CPU
#define CURRENT_CPU 0
#endif

const uint32_t schedMagicNumber = 0x253B9CF9;
spinlock_t schedSpinlock;


int currentProc = -1;
struct taskStruct *taskList = (struct taskStruct *)0xE0000000;
struct taskStruct *_current,*_usedMath = 0x0;

extern union descriptorTableunion GDT[7];
extern int testVal;

int switches = 0;
uint8_t paniced = 0;

void kpanic(char * mess)
{
    kprintf(mess);
    paniced = 1;
    schedule();
}

/* Initialize Scheduler */
void initScheduler(void) {
  int i;
  taskList[-1].id = -1;
  taskList[-1].status = AVAILABLE;
  for (i=0;i<numTasks;i++) {
    taskList[i].id = i;
    taskList[i].status = AVAILABLE;
    taskList[i].usedMath = 0;
    taskList[i].cpuTime = 0;
    taskList[i].uid = -1;
    taskList[i].gid = -1;
    }
  currentProc = -1;
  _current = &taskList[-1];

    spinlockInit(&schedSpinlock);
  
  setVector(timerInt, mVec, (dInt + dPresent + dDpl3));
  }

/* Finds A Free Task */
int findTask() {
  int i;
  for (i=0;i<numTasks;i++) {
    if (taskList[i].status == AVAILABLE) {
      return(i);
      }
    }
  return(-1);
  }

void schedule() {
  int i,proc = -1,memAddr;
  outportByte(0x20, 0x20);
    
    if (paniced == 1)
	while (1)
	    ;
    
    spinlockLock(&schedSpinlock, CURRENT_CPU);
     
   /* if ((currentProc == 1) && (taskList[2].status == RUNABLE))
      taskList[1].status = INACTIVE; */
  
  i = currentProc + 1;
  while ((taskList[i].status != RUNABLE) && (taskList[i].status != ACTIVE))
  {
      i++;
      i %= numTasks;
  }
  proc = i;
  if (proc != -1) {
    if (taskList[currentProc].status == ACTIVE)
    {
	taskList[currentProc].status = RUNABLE;
    }
    currentProc = proc;
    _current = &taskList[proc];
    taskList[proc].status = ACTIVE;
    taskList[proc].cpuTime++;
    memAddr = (unsigned int)&taskList[proc].tss;
    GDT[4].descriptor.access = 0x89;
    GDT[4].descriptor.baseLow = (memAddr & 0xFFFF);
    GDT[4].descriptor.baseMed = ((memAddr >> 16) & 0xff);
    GDT[4].descriptor.baseHigh = (memAddr >> 24);
    //kprintf("Task: [%i][%i][%i]\n",_current->id,_current->tss.cr3,&_current->tss.cr3);

    spinlockUnlock(&schedSpinlock, CURRENT_CPU);
    
    asm("ljmp $0x20,$0\n");
    }
  }


/* Timer Interupt */
asm (
  ".globl timerInt         \n"
  "timerInt:               \n"
  "  pusha                 \n"
  "  call schedule         \n"
  "  popa                  \n"
  "  iret                  \n"
  );

// New stuff commented out for now.

waitQueue_t * waitQueueCreate(int type)
{
	waitQueue_t * queue = (waitQueue_t *) kmalloc(sizeof(waitQueue_t));
	
	if (queue == NULL)
		return NULL;
	
	memset(queue, '\0', sizeof(waitQueue_t));
	
	queue->magic_number = schedMagicNumber;
	queue->type = type;
	
	return queue;
}

void waitQueueInit(waitQueue_t * queue, int type)
{
	memset(queue, '\0', sizeof(*queue));
	
	queue->magic_number = schedMagicNumber;
	queue->type = type;
	
	return;

}

void waitQueueDelete(waitQueue_t * queue)
{
	struct waitQueueNode * temp;
	
	if (queue == NULL)
		return;
		
	if (queue->magic_number != schedMagicNumber)
		return;
		
	while (queue->first != NULL)
	{
		temp = queue->first;
		queue->first = queue->first->next;
		kfree(temp);
	}
	
	memset(queue, '\0', sizeof(waitQueue_t));
	kfree(queue);		
}

void waitQueueUnInit(waitQueue_t * queue)
{
	struct waitQueueNode * temp;
	
	if (queue->magic_number != schedMagicNumber)
		return;
		
	while (queue->first != NULL)
	{
		temp = queue->first;
		queue->first = queue->first->next;
		kfree(temp);
	}
	
	memset(queue, '\0', sizeof(waitQueue_t));
}

void waitQueueInsert(waitQueue_t * queue, void * data, int id)
{
	struct waitQueueNode * temp;
	
	if (queue == NULL)
		return;
		
	if (queue->magic_number != schedMagicNumber)
		return;
	
	temp = (struct waitQueueNode *) kmalloc(sizeof(struct waitQueueNode));
	memset(temp, '\0', sizeof(struct waitQueueNode));
	
	if (queue->first == NULL)
	{
		queue->first = queue->last = temp;
	}
	else
	{
		queue->last->next = temp;
		temp->prev = queue->last->next;
		queue->last = temp;
	}
	
	temp->data = data;
	temp->id = id;
	
	return;
}

void * waitQueueRemove(waitQueue_t * queue, int * id)
{
	void * temp;
			
	if (queue == NULL)
		return NULL;
		
	if (queue->magic_number != schedMagicNumber)
		return NULL;

	if (queue->first == NULL)
		return NULL;
	
	temp = queue->first->data;
	*id = queue->first->id;
			
	if (queue->first->next == NULL)
	{
		kfree(queue->first);
		queue->first = queue->last = NULL;
	}
	else
	{
		queue->first = queue->first->next;
		kfree(queue->first->prev);
		queue->first->prev = NULL;
	}
	
	return temp;

}