/*****************************************************************************************
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>
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;
prioQueue_t *prioLevels[MAXPRIOLEVELS - 1];
prioQueue_t *prioQStart = 0x0;
prioQueue_t *prioQEnd = 0x0;
prioQueue_t *_currentQueue = 0x0;
/************************************************************************
Function: int schedInit()
Description: This function is used to enable the kernel scheduler
Notes:
02/20/2004 - Approved for quality
************************************************************************/
int
schedInit()
{
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;
struct tssStruct *gpfTSS = (struct tssStruct *)0x4200;
spinLock(&schedSpinLock);
gpfTSS->eip = (unsigned int)&_int13;
gpfTSS->esp = 0x1CFFF;
gpfTSS->ebp = 0x1CFFF;
gpfTSS->eflags = 0x206;
schedStart:
/* Yield the next task from the current prio queue */
for (tmpTask = _current->next; tmpTask; tmpTask = tmpTask->next) {
if (tmpTask->state > 0x0) {
_current = tmpTask;
break;
}
}
/* 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;
memAddr = (uInt32)&(_current->tss);
if (_current->state > 0x0) {
if (_current->oInfo.v86Task == 0x1)
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;
}
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);
/* 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;
spinLock(&schedTaskSpinLock);
/* Checking each task from the prio queue */
for (tmpTask = taskList; tmpTask; tmpTask = tmpTask->next) {
if (tmpTask->id == id) {
tmpTask->prev->next = tmpTask->next;
if (_current == tmpTask) {
_current == tmpTask->next;
}
}
}
kfree(tmpTask);
spinUnlock(&schedTaskSpinLock);
return(0x0);
}
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();
}
/***
$Log$
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
***/