                                                       /**************************************************************************************
 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: idt.c,v 1.1 2003/01/10 18:33:01 reddawg Exp $

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

#include <ubixos/idt.h>
#include <ubixos/gdt.h>
#include <ubixos/vitals.h>
#include <ubixos/schedule.h>
#include <vmm/paging.h>
//#include <lib/kstdio.h>
//#include <drivers/display.h>
#include <drivers/8259.h>

descriptorTable(IDT, 256) {
  };

struct {
  unsigned short limit __attribute__ ((packed));
  union descriptorTableunion *idt  __attribute__ ((packed));
  } loadidt= { (256 * sizeof(union descriptorTableunion) - 1), IDT };

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

Function: void initIDT();
Description: This Function Will Initialize The IDT
Notes:

************************************************************************/
void initIDT() {
  int i = 0x0;
  //kprintf("Initializing IDT.........           ");
  for (i=0x0;i<256;i++) {
    setVector(&intNull, i, dPresent + dInt + dDpl3);
    }
  asm (
    "cli\n"
    "lidt (%0)                \n"   /* Load the IDT                */
    "pushfl                   \n"   /* Clear the NT flag           */
    "andl $0xffffbfff,(%%esp) \n"
    "popfl                    \n"
    "sti                      \n"
    :
    : "r" ((char *) &loadidt)
    );
  setVector(&_int0,0,dPresent + dTrap + dDpl3);
  setVector(&_int1,1,dPresent + dTrap + dDpl3);
  setVector(&_int2,2,dPresent + dTrap + dDpl3);
  setVector(&_int3,3,dPresent + dTrap + dDpl3);
  setVector(&_int4,4,dPresent + dTrap + dDpl3);
  setVector(&_int5,5,dPresent + dTrap + dDpl3);
  setVector(&_int6,6,dPresent + dTrap + dDpl3);
  setVector(&_int7,7,dPresent + dTrap + dDpl3);
  setTaskVector(8,dPresent + dTask + dDpl3,0x28);
  setVector(&_int9,9,dPresent + dTrap + dDpl3);
  setVector(&_int10,10,dPresent + dTrap + dDpl3);
  setVector(&_int11,11,dPresent + dTrap + dDpl3);
  setVector(&_int12,12,dPresent + dTrap + dDpl3);
  //dsetVector(&_int13,13,dPresent + dTrap + dDpl3);
  setVector(&pageFaultISR,14,dPresent + dTrap + dDpl3);
  setVector(&timerInt,0x68,dPresent + dInt + dDpl3);
  //setVector(&mouseISR,0x74,dPresent + dTrap + dDpl3);
  //kprintf("INITIALIZED\n");
  return;
  }

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

Function: void setVector();
Description: This Function Sets Up An IDT Vector
Notes:

************************************************************************/
void setVector(void *handler,uInt8 interrupt,uInt16 controlMajor) {
  uInt16 codesegment = 0x08;
  asm volatile("movw %%cs,%0":"=g" (codesegment));
  IDT[interrupt].gate.offsetLow  = (uInt16) (((uInt32)handler)&0xffff);
  IDT[interrupt].gate.selector   = codesegment;
  IDT[interrupt].gate.access     = controlMajor;
  IDT[interrupt].gate.offsetHigh = (uInt16) (((uInt32)handler) >> 16);
  }

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

Function: void setTaskVector(uInt8,uInt16,uInt8);
Description: This Function Sets Up An IDT Task Vector
Notes:

************************************************************************/  
void setTaskVector(uInt8 interrupt,uInt16 controlMajor,uInt8 selector) {
  uInt16 codesegment = 0x08;
  asm volatile("movw %%cs,%0":"=g" (codesegment));
  IDT[interrupt].gate.offsetLow  = 0x0;
  IDT[interrupt].gate.selector   = selector;
  IDT[interrupt].gate.access     = controlMajor;
  IDT[interrupt].gate.offsetHigh = 0x0;
  }  

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

Function: void intNull();
Description: This Function Is The Null Intterupt Descriptor
Notes:

************************************************************************/
void intNull() {
  //kprintf("Sorry This Is An Invalid Intterupt\n");
  //kprintf("Task ID: [%i]\n",_current->id);
  while(1);
  }

void _int0() {
  //kprintf("int0: Divide-by-Zero\n");
  while(1);
  }
void _int1() {
  //kprintf("int1: Debug exception\n");
  while(1);
  }
void _int2() {
  //kprintf("int2: unknown error\n");
  while(1);
  }
void _int3() {
  //kprintf("int3: Breakpoint\n");
  disableIrq(0);
  while(1);
  }
void _int4() {
  //kprintf("int4: Overflow\n");
  while(1);
  }
void _int5() {
  //kprintf("int5: Bounds check\n");
  while(1);
  }
void _int6() {
  //printColor = 0x9C;
  //kprintf("INVALID OPCODE - Task ID: %i\n",_current->id);
  //printColor = defaultColor;
  _current->status = EMPTY;
  while(1);
  schedule(); //I Believe This Is Safe
  return;
  }

void _int7() {
  //kprintf("int7: Device Not Available!\n");
  while(1);
  }

void _int9() {
  //kprintf("int9: Coprocessor Segment Overrun!\n");
  while(1);
  }

void _int10() {
  //kprintf("int10: Invalid TSS!\n");
  while(1);
  }

void _int11() {
  //kprintf("int11: Segment Not Present!\n");
  while(1);
  }

void _int12() {
  //kprintf("int12: Stack-Segment Fault!\n");
  while(1);
  }

void _int13() {
  uInt32 r1,r2,r3,r4,r5,r6,cs;
  asm(
    "popl %%eax\n"
    "movl %%eax,%0\n"
    "popl %%eax\n"
    "movl %%eax,%1\n"
    "popl %%eax\n"
    "movl %%eax,%2\n"
    "popl %%eax\n"
    "movl %%eax,%3\n"
    "popl %%eax\n"
    "movl %%eax,%4\n"
    "popl %%eax\n"
    "movl %%eax,%5\n"
    "movl %%cs,%6\n"
    :
    : "g" (r1),"g" (r2),"g" (r3),"g" (r4),"g" (r5),"g" (r6),"g" (cs)
    );
  //kprintf("int13: General Protection Fault!\n");
  //kprintf("[0x%X][0x%X][0x%X][0x%X][0x%X][0x%X][0x%X]\n",r1,r2,r3,r4,r5,r6,cs);
  while(1);
  }

__asm__ (
  ".globl timerInt           \n"
  "timerInt:                 \n"
  "  pusha                   \n"
  "  movl systemVitals,%ecx  \n"
  "  incl 4(%ecx)            \n"
  "  mov $0x20,%dx           \n"
  "  mov $0x20,%al           \n"
  "  outb %al,%dx            \n"  
  "  movl 4(%ecx),%eax       \n"
  "  movl $1000,%ebx         \n"
  "  xor %edx,%edx           \n"
  "  div %ebx                \n"
  "  test %edx,%edx          \n"
  "  jnz next                \n"
  "  incl 8(%ecx)            \n"
  "next:                     \n"
  "  movl 4(%ecx),%eax       \n"
  "  movl 12(%ecx),%ebx      \n"
  "  xor %edx,%edx           \n"
  "  div %ebx                \n"
  "  test %edx,%edx          \n"
  "  jnz done                \n"
  "  call schedule           \n"
  "  jmp done                \n"
  "done:                     \n"
  "  popa                    \n"
  "  iret                    \n"
  );  
